Index: llvm/include/llvm/Transforms/InstCombine/InstCombiner.h =================================================================== --- llvm/include/llvm/Transforms/InstCombine/InstCombiner.h +++ llvm/include/llvm/Transforms/InstCombine/InstCombiner.h @@ -394,18 +394,17 @@ /// /// 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); } Index: llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -3774,7 +3774,7 @@ 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()); Index: llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp @@ -112,7 +112,7 @@ } Res->takeName(I); - return InsertNewInstWith(Res, *I); + return InsertNewInstWith(Res, I->getIterator()); } Instruction::CastOps Index: llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp @@ -224,7 +224,7 @@ 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. @@ -380,7 +380,7 @@ 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(I)) { @@ -398,7 +398,7 @@ 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(I)) { @@ -408,14 +408,14 @@ cast(BC->getType()), 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(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(I)) { @@ -450,7 +450,7 @@ 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); @@ -1663,7 +1663,7 @@ PHINode *PN = PHINode::Create(MergedVal->getType(), 2, "storemerge"); PN->addIncoming(SI.getOperand(0), SI.getParent()); PN->addIncoming(OtherStore->getOperand(0), OtherBB); - MergedVal = InsertNewInstBefore(PN, DestBB->front()); + MergedVal = InsertNewInstBefore(PN, DestBB->begin()); PN->setDebugLoc(MergedLoc); } @@ -1672,7 +1672,7 @@ 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}); Index: llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp +++ llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp @@ -248,7 +248,7 @@ PHINode *NewPtrPHI = PHINode::Create( IntToPtr->getType(), PN.getNumIncomingValues(), PN.getName() + ".ptr"); - InsertNewInstBefore(NewPtrPHI, PN); + InsertNewInstBefore(NewPtrPHI, PN.getIterator()); SmallDenseMap Casts; for (auto Incoming : zip(PN.blocks(), AvailablePtrVals)) { auto *IncomingBB = std::get<0>(Incoming); @@ -285,10 +285,10 @@ if (isa(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); @@ -353,7 +353,7 @@ NewOperand->addIncoming( cast(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. @@ -391,7 +391,7 @@ NewAggregateOperand->addIncoming( cast(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, @@ -450,7 +450,7 @@ 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; } @@ -458,7 +458,7 @@ 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; } @@ -581,7 +581,7 @@ 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; @@ -769,7 +769,7 @@ 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 @@ -853,7 +853,7 @@ 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()); } @@ -943,7 +943,7 @@ PhiVal = InVal; delete NewPN; } else { - InsertNewInstBefore(NewPN, PN); + InsertNewInstBefore(NewPN, PN.getIterator()); PhiVal = NewPN; } Index: llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp @@ -805,7 +805,7 @@ auto *FalseValI = cast(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); } @@ -3076,7 +3076,7 @@ Value *Op1 = IsAnd ? TrueVal : FalseVal; if (isCheckForZeroAndMulWithOverflow(CondVal, Op1, IsAnd, Y)) { auto *FI = new FreezeInst(*Y, (*Y)->getName() + ".fr"); - InsertNewInstBefore(FI, *cast(Y->getUser())); + InsertNewInstBefore(FI, cast(Y->getUser())->getIterator()); replaceUse(*Y, FI); return replaceInstUsesWith(SI, Op1); } Index: llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp @@ -709,13 +709,13 @@ 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()); } } } Index: llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp @@ -286,7 +286,7 @@ 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 @@ -298,7 +298,7 @@ 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 @@ -330,11 +330,11 @@ 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; @@ -462,7 +462,7 @@ 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 @@ -586,7 +586,7 @@ 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: @@ -595,7 +595,7 @@ 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); @@ -627,7 +627,7 @@ 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()); } } @@ -705,7 +705,7 @@ 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()); } } } @@ -747,7 +747,7 @@ // 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; @@ -784,7 +784,7 @@ BinaryOperator *LShr = BinaryOperator::CreateLShr(I->getOperand(0), I->getOperand(1)); LShr->setIsExact(cast(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; } @@ -891,7 +891,7 @@ 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; } @@ -918,7 +918,7 @@ NewVal = BinaryOperator::CreateShl( II->getArgOperand(0), ConstantInt::get(VTy, NTZ - NLZ)); NewVal->takeName(I); - return InsertNewInstWith(NewVal, *I); + return InsertNewInstWith(NewVal, I->getIterator()); } break; } @@ -1214,7 +1214,7 @@ New->setIsExact(true); } - return InsertNewInstWith(New, *Shl); + return InsertNewInstWith(New, Shl->getIterator()); } return nullptr; @@ -1544,7 +1544,7 @@ Instruction *New = InsertElementInst::Create( Op, Value, ConstantInt::get(Type::getInt32Ty(I->getContext()), Idx), Shuffle->getName()); - InsertNewInstWith(New, *Shuffle); + InsertNewInstWith(New, Shuffle->getIterator()); return New; } } Index: llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp @@ -132,7 +132,7 @@ // Create a scalar PHI node that will replace the vector PHI node // just before the current PHI node. PHINode *scalarPHI = cast(InsertNewInstWith( - PHINode::Create(EI.getType(), PN->getNumIncomingValues(), ""), *PN)); + PHINode::Create(EI.getType(), PN->getNumIncomingValues(), ""), PN->getIterator())); // Scalarize each PHI operand. for (unsigned i = 0; i < PN->getNumIncomingValues(); i++) { Value *PHIInVal = PN->getIncomingValue(i); @@ -148,10 +148,10 @@ Value *Op = InsertNewInstWith( ExtractElementInst::Create(B0->getOperand(opId), Elt, B0->getOperand(opId)->getName() + ".Elt"), - *B0); + B0->getIterator()); Value *newPHIUser = InsertNewInstWith( BinaryOperator::CreateWithCopiedFlags(B0->getOpcode(), - scalarPHI, Op, B0), *B0); + scalarPHI, Op, B0), B0->getIterator()); scalarPHI->addIncoming(newPHIUser, inBB); } else { // Scalarize PHI input: @@ -165,7 +165,7 @@ InsertPos = inBB->getFirstInsertionPt(); } - InsertNewInstWith(newEI, *InsertPos); + InsertNewInstWith(newEI, InsertPos); scalarPHI->addIncoming(newEI, inBB); } @@ -738,7 +738,7 @@ if (ExtVecOpInst && !isa(ExtVecOpInst)) WideVec->insertAfter(ExtVecOpInst); else - IC.InsertNewInstWith(WideVec, *ExtElt->getParent()->getFirstInsertionPt()); + IC.InsertNewInstWith(WideVec, ExtElt->getParent()->getFirstInsertionPt()); // Replace extracts from the original narrow vector with extracts from the new // wide vector. Index: llvm/lib/Transforms/InstCombine/InstructionCombining.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -540,7 +540,7 @@ Flags &= Op1->getFastMathFlags(); NewBO->setFastMathFlags(Flags); } - InsertNewInstWith(NewBO, I); + InsertNewInstWith(NewBO, I.getIterator()); NewBO->takeName(Op1); replaceOperand(I, 0, NewBO); replaceOperand(I, 1, CRes); @@ -1058,7 +1058,7 @@ Value *NewOp, InstCombiner &IC) { Instruction *Clone = I.clone(); Clone->replaceUsesOfWith(SI, NewOp); - IC.InsertNewInstBefore(Clone, *SI); + IC.InsertNewInstBefore(Clone, SI->getIterator()); return Clone; } @@ -1192,7 +1192,7 @@ // Okay, we can do the transformation: create the new PHI node. PHINode *NewPN = PHINode::Create(I.getType(), PN->getNumIncomingValues()); - InsertNewInstBefore(NewPN, *PN); + InsertNewInstBefore(NewPN, PN->getIterator()); NewPN->takeName(PN); // If we are going to have to insert a new computation, do so right before the @@ -1206,7 +1206,7 @@ else U = U->DoPHITranslation(PN->getParent(), NonSimplifiedBB); } - InsertNewInstBefore(Clone, *NonSimplifiedBB->getTerminator()); + InsertNewInstBefore(Clone, NonSimplifiedBB->getTerminator()->getIterator()); } for (unsigned i = 0; i != NumPHIValues; ++i) { @@ -2255,7 +2255,7 @@ NewGEP->setOperand(DI, NewPN); } - NewGEP->insertInto(GEP.getParent(), GEP.getParent()->getFirstInsertionPt()); + NewGEP->insertBefore(*GEP.getParent(), GEP.getParent()->getFirstInsertionPt()); return replaceOperand(GEP, 0, NewGEP); }