Skip to content

Commit

Permalink
[NFC][RemoveDIs] Prefer iterators over inst-pointers in InstCombine
Browse files Browse the repository at this point in the history
As per my proposal for how to eliminate debug intrinsics [0], for various
places in InstCombine prefer to insert using an instruction iterator rather
than an instruction pointer. This is so that we can eventually pass more
information in the iterator class. These call-sites where I've changed the
spelling are those that necessary to build a stage2clang to produce an
identical binary in the coming no-debug-intrinsics mode.

[0] https://discourse.llvm.org/t/rfc-instruction-api-changes-needed-to-eliminate-debug-intrinsics-from-ir/68939

Differential Revision: https://reviews.llvm.org/D152543
  • Loading branch information
jmorse committed Sep 11, 2023
1 parent edc2fb0 commit d529943
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 58 deletions.
9 changes: 4 additions & 5 deletions llvm/include/llvm/Transforms/InstCombine/InstCombiner.h
Original file line number Diff line number Diff line change
Expand Up @@ -398,18 +398,17 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner {
///
/// Also adds the new instruction to the worklist and returns \p New so that
/// it is suitable for use as the return from the visitation patterns.
Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) {
Instruction *InsertNewInstBefore(Instruction *New, BasicBlock::iterator Old) {
assert(New && !New->getParent() &&
"New instruction already inserted into a basic block!");
BasicBlock *BB = Old.getParent();
New->insertInto(BB, Old.getIterator()); // Insert inst
New->insertBefore(Old); // Insert inst
Worklist.add(New);
return New;
}

/// Same as InsertNewInstBefore, but also sets the debug loc.
Instruction *InsertNewInstWith(Instruction *New, Instruction &Old) {
New->setDebugLoc(Old.getDebugLoc());
Instruction *InsertNewInstWith(Instruction *New, BasicBlock::iterator Old) {
New->setDebugLoc(Old->getDebugLoc());
return InsertNewInstBefore(New, Old);
}

Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3994,7 +3994,7 @@ bool InstCombinerImpl::transformConstExprCastCall(CallBase &Call) {

Instruction *InsertPt = NewCall->getInsertionPointAfterDef();
assert(InsertPt && "No place to insert cast");
InsertNewInstBefore(NC, *InsertPt);
InsertNewInstBefore(NC, InsertPt->getIterator());
Worklist.pushUsersToWorkList(*Caller);
} else {
NV = PoisonValue::get(Caller->getType());
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ Value *InstCombinerImpl::EvaluateInDifferentType(Value *V, Type *Ty,
}

Res->takeName(I);
return InsertNewInstWith(Res, *I);
return InsertNewInstWith(Res, I->getIterator());
}

Instruction::CastOps
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/InstCombine/InstCombineInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
auto *SI = new StoreInst(ConstantInt::getTrue(Ctx),
PoisonValue::get(PointerType::getUnqual(Ctx)),
/*isVolatile*/ false, Align(1));
InsertNewInstBefore(SI, *InsertAt);
InsertNewInstBefore(SI, InsertAt->getIterator());
}

/// Combiner aware instruction erasure.
Expand Down
18 changes: 9 additions & 9 deletions llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ static Instruction *simplifyAllocaArraySize(InstCombinerImpl &IC,
Value *Idx[2] = {NullIdx, NullIdx};
Instruction *GEP = GetElementPtrInst::CreateInBounds(
NewTy, New, Idx, New->getName() + ".sub");
IC.InsertNewInstBefore(GEP, *It);
IC.InsertNewInstBefore(GEP, It);

// Now make everything use the getelementptr instead of the original
// allocation.
Expand Down Expand Up @@ -380,7 +380,7 @@ void PointerReplacer::replace(Instruction *I) {
NewI->takeName(LT);
copyMetadataForLoad(*NewI, *LT);

IC.InsertNewInstWith(NewI, *LT);
IC.InsertNewInstWith(NewI, LT->getIterator());
IC.replaceInstUsesWith(*LT, NewI);
WorkMap[LT] = NewI;
} else if (auto *PHI = dyn_cast<PHINode>(I)) {
Expand All @@ -398,7 +398,7 @@ void PointerReplacer::replace(Instruction *I) {
Indices.append(GEP->idx_begin(), GEP->idx_end());
auto *NewI =
GetElementPtrInst::Create(GEP->getSourceElementType(), V, Indices);
IC.InsertNewInstWith(NewI, *GEP);
IC.InsertNewInstWith(NewI, GEP->getIterator());
NewI->takeName(GEP);
WorkMap[GEP] = NewI;
} else if (auto *BC = dyn_cast<BitCastInst>(I)) {
Expand All @@ -407,14 +407,14 @@ void PointerReplacer::replace(Instruction *I) {
auto *NewT = PointerType::get(BC->getType()->getContext(),
V->getType()->getPointerAddressSpace());
auto *NewI = new BitCastInst(V, NewT);
IC.InsertNewInstWith(NewI, *BC);
IC.InsertNewInstWith(NewI, BC->getIterator());
NewI->takeName(BC);
WorkMap[BC] = NewI;
} else if (auto *SI = dyn_cast<SelectInst>(I)) {
auto *NewSI = SelectInst::Create(
SI->getCondition(), getReplacement(SI->getTrueValue()),
getReplacement(SI->getFalseValue()), SI->getName(), nullptr, SI);
IC.InsertNewInstWith(NewSI, *SI);
IC.InsertNewInstWith(NewSI, SI->getIterator());
NewSI->takeName(SI);
WorkMap[SI] = NewSI;
} else if (auto *MemCpy = dyn_cast<MemTransferInst>(I)) {
Expand Down Expand Up @@ -449,7 +449,7 @@ void PointerReplacer::replace(Instruction *I) {
ASC->getType()->getPointerAddressSpace()) {
auto *NewI = new AddrSpaceCastInst(V, ASC->getType(), "");
NewI->takeName(ASC);
IC.InsertNewInstWith(NewI, *ASC);
IC.InsertNewInstWith(NewI, ASC->getIterator());
NewV = NewI;
}
IC.replaceInstUsesWith(*ASC, NewV);
Expand Down Expand Up @@ -1006,7 +1006,7 @@ static Instruction *replaceGEPIdxWithZero(InstCombinerImpl &IC, Value *Ptr,
Instruction *NewGEPI = GEPI->clone();
NewGEPI->setOperand(Idx,
ConstantInt::get(GEPI->getOperand(Idx)->getType(), 0));
IC.InsertNewInstBefore(NewGEPI, *GEPI);
IC.InsertNewInstBefore(NewGEPI, GEPI->getIterator());
return NewGEPI;
}
}
Expand Down Expand Up @@ -1669,7 +1669,7 @@ bool InstCombinerImpl::mergeStoreIntoSuccessor(StoreInst &SI) {
Builder.SetInsertPoint(OtherStore);
PN->addIncoming(Builder.CreateBitOrPointerCast(MergedVal, PN->getType()),
OtherBB);
MergedVal = InsertNewInstBefore(PN, DestBB->front());
MergedVal = InsertNewInstBefore(PN, DestBB->begin());
PN->setDebugLoc(MergedLoc);
}

Expand All @@ -1678,7 +1678,7 @@ bool InstCombinerImpl::mergeStoreIntoSuccessor(StoreInst &SI) {
StoreInst *NewSI =
new StoreInst(MergedVal, SI.getOperand(1), SI.isVolatile(), SI.getAlign(),
SI.getOrdering(), SI.getSyncScopeID());
InsertNewInstBefore(NewSI, *BBI);
InsertNewInstBefore(NewSI, BBI);
NewSI->setDebugLoc(MergedLoc);
NewSI->mergeDIAssignID({&SI, OtherStore});

Expand Down
22 changes: 11 additions & 11 deletions llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ bool InstCombinerImpl::foldIntegerTypedPHI(PHINode &PN) {
PHINode *NewPtrPHI = PHINode::Create(
IntToPtr->getType(), PN.getNumIncomingValues(), PN.getName() + ".ptr");

InsertNewInstBefore(NewPtrPHI, PN);
InsertNewInstBefore(NewPtrPHI, PN.getIterator());
SmallDenseMap<Value *, Instruction *> Casts;
for (auto Incoming : zip(PN.blocks(), AvailablePtrVals)) {
auto *IncomingBB = std::get<0>(Incoming);
Expand Down Expand Up @@ -285,10 +285,10 @@ bool InstCombinerImpl::foldIntegerTypedPHI(PHINode &PN) {
if (isa<PHINode>(IncomingI))
InsertPos = BB->getFirstInsertionPt();
assert(InsertPos != BB->end() && "should have checked above");
InsertNewInstBefore(CI, *InsertPos);
InsertNewInstBefore(CI, InsertPos);
} else {
auto *InsertBB = &IncomingBB->getParent()->getEntryBlock();
InsertNewInstBefore(CI, *InsertBB->getFirstInsertionPt());
InsertNewInstBefore(CI, InsertBB->getFirstInsertionPt());
}
}
NewPtrPHI->addIncoming(CI, IncomingBB);
Expand Down Expand Up @@ -353,7 +353,7 @@ InstCombinerImpl::foldPHIArgInsertValueInstructionIntoPHI(PHINode &PN) {
NewOperand->addIncoming(
cast<InsertValueInst>(std::get<1>(Incoming))->getOperand(OpIdx),
std::get<0>(Incoming));
InsertNewInstBefore(NewOperand, PN);
InsertNewInstBefore(NewOperand, PN.getIterator());
}

// And finally, create `insertvalue` over the newly-formed PHI nodes.
Expand Down Expand Up @@ -391,7 +391,7 @@ InstCombinerImpl::foldPHIArgExtractValueInstructionIntoPHI(PHINode &PN) {
NewAggregateOperand->addIncoming(
cast<ExtractValueInst>(std::get<1>(Incoming))->getAggregateOperand(),
std::get<0>(Incoming));
InsertNewInstBefore(NewAggregateOperand, PN);
InsertNewInstBefore(NewAggregateOperand, PN.getIterator());

// And finally, create `extractvalue` over the newly-formed PHI nodes.
auto *NewEVI = ExtractValueInst::Create(NewAggregateOperand,
Expand Down Expand Up @@ -450,15 +450,15 @@ Instruction *InstCombinerImpl::foldPHIArgBinOpIntoPHI(PHINode &PN) {
NewLHS = PHINode::Create(LHSType, PN.getNumIncomingValues(),
FirstInst->getOperand(0)->getName() + ".pn");
NewLHS->addIncoming(InLHS, PN.getIncomingBlock(0));
InsertNewInstBefore(NewLHS, PN);
InsertNewInstBefore(NewLHS, PN.getIterator());
LHSVal = NewLHS;
}

if (!RHSVal) {
NewRHS = PHINode::Create(RHSType, PN.getNumIncomingValues(),
FirstInst->getOperand(1)->getName() + ".pn");
NewRHS->addIncoming(InRHS, PN.getIncomingBlock(0));
InsertNewInstBefore(NewRHS, PN);
InsertNewInstBefore(NewRHS, PN.getIterator());
RHSVal = NewRHS;
}

Expand Down Expand Up @@ -581,7 +581,7 @@ Instruction *InstCombinerImpl::foldPHIArgGEPIntoPHI(PHINode &PN) {
Value *FirstOp = FirstInst->getOperand(I);
PHINode *NewPN =
PHINode::Create(FirstOp->getType(), E, FirstOp->getName() + ".pn");
InsertNewInstBefore(NewPN, PN);
InsertNewInstBefore(NewPN, PN.getIterator());

NewPN->addIncoming(FirstOp, PN.getIncomingBlock(0));
OperandPhis[I] = NewPN;
Expand Down Expand Up @@ -769,7 +769,7 @@ Instruction *InstCombinerImpl::foldPHIArgLoadIntoPHI(PHINode &PN) {
NewLI->setOperand(0, InVal);
delete NewPN;
} else {
InsertNewInstBefore(NewPN, PN);
InsertNewInstBefore(NewPN, PN.getIterator());
}

// If this was a volatile load that we are merging, make sure to loop through
Expand Down Expand Up @@ -853,7 +853,7 @@ Instruction *InstCombinerImpl::foldPHIArgZextsIntoPHI(PHINode &Phi) {
for (unsigned I = 0; I != NumIncomingValues; ++I)
NewPhi->addIncoming(NewIncoming[I], Phi.getIncomingBlock(I));

InsertNewInstBefore(NewPhi, Phi);
InsertNewInstBefore(NewPhi, Phi.getIterator());
return CastInst::CreateZExtOrBitCast(NewPhi, Phi.getType());
}

Expand Down Expand Up @@ -943,7 +943,7 @@ Instruction *InstCombinerImpl::foldPHIArgOpIntoPHI(PHINode &PN) {
PhiVal = InVal;
delete NewPN;
} else {
InsertNewInstBefore(NewPN, PN);
InsertNewInstBefore(NewPN, PN.getIterator());
PhiVal = NewPN;
}

Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@ static Instruction *foldSelectZeroOrMul(SelectInst &SI, InstCombinerImpl &IC) {

auto *FalseValI = cast<Instruction>(FalseVal);
auto *FrY = IC.InsertNewInstBefore(new FreezeInst(Y, Y->getName() + ".fr"),
*FalseValI);
FalseValI->getIterator());
IC.replaceOperand(*FalseValI, FalseValI->getOperand(0) == Y ? 0 : 1, FrY);
return IC.replaceInstUsesWith(SI, FalseValI);
}
Expand Down Expand Up @@ -3118,7 +3118,7 @@ Instruction *InstCombinerImpl::foldSelectOfBools(SelectInst &SI) {
Value *Op1 = IsAnd ? TrueVal : FalseVal;
if (isCheckForZeroAndMulWithOverflow(CondVal, Op1, IsAnd, Y)) {
auto *FI = new FreezeInst(*Y, (*Y)->getName() + ".fr");
InsertNewInstBefore(FI, *cast<Instruction>(Y->getUser()));
InsertNewInstBefore(FI, cast<Instruction>(Y->getUser())->getIterator());
replaceUse(*Y, FI);
return replaceInstUsesWith(SI, Op1);
}
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -709,13 +709,13 @@ static Value *getShiftedValue(Value *V, unsigned NumBits, bool isLeftShift,
case Instruction::Mul: {
assert(!isLeftShift && "Unexpected shift direction!");
auto *Neg = BinaryOperator::CreateNeg(I->getOperand(0));
IC.InsertNewInstWith(Neg, *I);
IC.InsertNewInstWith(Neg, I->getIterator());
unsigned TypeWidth = I->getType()->getScalarSizeInBits();
APInt Mask = APInt::getLowBitsSet(TypeWidth, TypeWidth - NumBits);
auto *And = BinaryOperator::CreateAnd(Neg,
ConstantInt::get(I->getType(), Mask));
And->takeName(I);
return IC.InsertNewInstWith(And, *I);
return IC.InsertNewInstWith(And, I->getIterator());
}
}
}
Expand Down
30 changes: 15 additions & 15 deletions llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
Instruction *Or =
BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1),
I->getName());
return InsertNewInstWith(Or, *I);
return InsertNewInstWith(Or, I->getIterator());
}

// If all of the demanded bits on one side are known, and all of the set
Expand All @@ -298,7 +298,7 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
Constant *AndC = Constant::getIntegerValue(VTy,
~RHSKnown.One & DemandedMask);
Instruction *And = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
return InsertNewInstWith(And, *I);
return InsertNewInstWith(And, I->getIterator());
}

// If the RHS is a constant, see if we can change it. Don't alter a -1
Expand Down Expand Up @@ -330,11 +330,11 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,

Constant *AndC = ConstantInt::get(VTy, NewMask & AndRHS->getValue());
Instruction *NewAnd = BinaryOperator::CreateAnd(I->getOperand(0), AndC);
InsertNewInstWith(NewAnd, *I);
InsertNewInstWith(NewAnd, I->getIterator());

Constant *XorC = ConstantInt::get(VTy, NewMask & XorRHS->getValue());
Instruction *NewXor = BinaryOperator::CreateXor(NewAnd, XorC);
return InsertNewInstWith(NewXor, *I);
return InsertNewInstWith(NewXor, I->getIterator());
}
}
break;
Expand Down Expand Up @@ -462,7 +462,7 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
DemandedMask.getActiveBits() <= SrcBitWidth) {
// Convert to ZExt cast.
CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName());
return InsertNewInstWith(NewCast, *I);
return InsertNewInstWith(NewCast, I->getIterator());
}

// If the sign bit of the input is known set or clear, then we know the
Expand Down Expand Up @@ -586,7 +586,7 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
if (match(I->getOperand(1), m_APInt(C)) && C->countr_zero() == CTZ) {
Constant *ShiftC = ConstantInt::get(VTy, CTZ);
Instruction *Shl = BinaryOperator::CreateShl(I->getOperand(0), ShiftC);
return InsertNewInstWith(Shl, *I);
return InsertNewInstWith(Shl, I->getIterator());
}
}
// For a squared value "X * X", the bottom 2 bits are 0 and X[0] because:
Expand All @@ -595,7 +595,7 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
if (I->getOperand(0) == I->getOperand(1) && DemandedMask.ult(4)) {
Constant *One = ConstantInt::get(VTy, 1);
Instruction *And1 = BinaryOperator::CreateAnd(I->getOperand(0), One);
return InsertNewInstWith(And1, *I);
return InsertNewInstWith(And1, I->getIterator());
}

computeKnownBits(I, Known, Depth, CxtI);
Expand Down Expand Up @@ -627,7 +627,7 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
Constant *NewC = ConstantExpr::getShl(C, LeftShiftAmtC);
if (ConstantExpr::getLShr(NewC, LeftShiftAmtC) == C) {
Instruction *Lshr = BinaryOperator::CreateLShr(NewC, X);
return InsertNewInstWith(Lshr, *I);
return InsertNewInstWith(Lshr, I->getIterator());
}
}

Expand Down Expand Up @@ -691,7 +691,7 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
Constant *NewC = ConstantExpr::getLShr(C, RightShiftAmtC);
if (ConstantExpr::getShl(NewC, RightShiftAmtC) == C) {
Instruction *Shl = BinaryOperator::CreateShl(NewC, X);
return InsertNewInstWith(Shl, *I);
return InsertNewInstWith(Shl, I->getIterator());
}
}
}
Expand Down Expand Up @@ -733,7 +733,7 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
// Perform the logical shift right.
Instruction *NewVal = BinaryOperator::CreateLShr(
I->getOperand(0), I->getOperand(1), I->getName());
return InsertNewInstWith(NewVal, *I);
return InsertNewInstWith(NewVal, I->getIterator());
}

const APInt *SA;
Expand Down Expand Up @@ -770,7 +770,7 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
BinaryOperator *LShr = BinaryOperator::CreateLShr(I->getOperand(0),
I->getOperand(1));
LShr->setIsExact(cast<BinaryOperator>(I)->isExact());
return InsertNewInstWith(LShr, *I);
return InsertNewInstWith(LShr, I->getIterator());
} else if (Known.One[BitWidth-ShiftAmt-1]) { // New bits are known one.
Known.One |= HighBits;
}
Expand Down Expand Up @@ -867,7 +867,7 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
match(II->getArgOperand(0), m_Not(m_Value(X)))) {
Function *Ctpop = Intrinsic::getDeclaration(
II->getModule(), Intrinsic::ctpop, VTy);
return InsertNewInstWith(CallInst::Create(Ctpop, {X}), *I);
return InsertNewInstWith(CallInst::Create(Ctpop, {X}), I->getIterator());
}
break;
}
Expand All @@ -894,7 +894,7 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
NewVal = BinaryOperator::CreateShl(
II->getArgOperand(0), ConstantInt::get(VTy, NTZ - NLZ));
NewVal->takeName(I);
return InsertNewInstWith(NewVal, *I);
return InsertNewInstWith(NewVal, I->getIterator());
}
break;
}
Expand Down Expand Up @@ -1219,7 +1219,7 @@ Value *InstCombinerImpl::simplifyShrShlDemandedBits(
New->setIsExact(true);
}

return InsertNewInstWith(New, *Shl);
return InsertNewInstWith(New, Shl->getIterator());
}

return nullptr;
Expand Down Expand Up @@ -1549,7 +1549,7 @@ Value *InstCombinerImpl::SimplifyDemandedVectorElts(Value *V,
Instruction *New = InsertElementInst::Create(
Op, Value, ConstantInt::get(Type::getInt64Ty(I->getContext()), Idx),
Shuffle->getName());
InsertNewInstWith(New, *Shuffle);
InsertNewInstWith(New, Shuffle->getIterator());
return New;
}
}
Expand Down
Loading

0 comments on commit d529943

Please sign in to comment.