Index: llvm/include/llvm/IR/BasicBlock.h =================================================================== --- llvm/include/llvm/IR/BasicBlock.h +++ llvm/include/llvm/IR/BasicBlock.h @@ -173,6 +173,15 @@ static_cast(this)->getFirstNonPHI()); } + /// Iterator returning form of getFirstNonPHI. Installed as a placeholder for + /// the RemoveDIs project that will eventually remove debug intrinsics. + InstListType::const_iterator getFirstNonPHIIt() const; + InstListType::iterator getFirstNonPHIIt() { + BasicBlock::iterator It = + static_cast(this)->getFirstNonPHIIt().getNonConst(); + return It; + } + /// Returns a pointer to the first instruction in this block that is not a /// PHINode or a debug intrinsic, or any pseudo operation if \c SkipPseudoOp /// is true. Index: llvm/include/llvm/IR/IRBuilder.h =================================================================== --- llvm/include/llvm/IR/IRBuilder.h +++ llvm/include/llvm/IR/IRBuilder.h @@ -188,7 +188,7 @@ BB = I->getParent(); InsertPt = I->getIterator(); assert(InsertPt != BB->end() && "Can't read debug loc from end()"); - SetCurrentDebugLocation(I->getDebugLoc()); + SetCurrentDebugLocation(I->getStableDebugLoc()); } /// This specifies that created instructions should be inserted at the @@ -197,7 +197,7 @@ BB = TheBB; InsertPt = IP; if (IP != TheBB->end()) - SetCurrentDebugLocation(IP->getDebugLoc()); + SetCurrentDebugLocation(IP->getStableDebugLoc()); } /// This specifies that created instructions should inserted at the beginning Index: llvm/include/llvm/IR/Instruction.h =================================================================== --- llvm/include/llvm/IR/Instruction.h +++ llvm/include/llvm/IR/Instruction.h @@ -364,6 +364,10 @@ /// Return the debug location for this node as a DebugLoc. const DebugLoc &getDebugLoc() const { return DbgLoc; } + /// Fetch the debug location for this node, unless this is a debug intrinsic, + /// in which case fetch the debug location of the next non-debug node. + const DebugLoc &getStableDebugLoc() const; + /// Set or clear the nuw flag on this instruction, which must be an operator /// which supports this flag. See LangRef.html for the meaning of this flag. void setHasNoUnsignedWrap(bool b = true); Index: llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h =================================================================== --- llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h +++ llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h @@ -351,6 +351,10 @@ Builder.SetInsertPoint(IP); } + void setInsertPoint(BasicBlock::iterator IP) { + Builder.SetInsertPoint(IP->getParent(), IP); + } + /// Clear the current insertion point. This is useful if the instruction /// that had been serving as the insertion point may have been deleted. void clearInsertPoint() { Builder.ClearInsertionPoint(); } @@ -404,7 +408,10 @@ /// program. The code is inserted into the specified block. If \p /// Root is true, this indicates that \p SH is the top-level expression to /// expand passed from an external client call. - Value *expandCodeForImpl(const SCEV *SH, Type *Ty, Instruction *I); + Value *expandCodeForImpl(const SCEV *SH, Type *Ty, BasicBlock::iterator I); + Value *expandCodeForImpl(const SCEV *SH, Type *Ty, Instruction *I) { + return expandCodeForImpl(SH, Ty, I->getIterator()); + } /// Recursive helper function for isHighCostExpansion. bool isHighCostExpansionHelper(const SCEVOperand &WorkItem, Loop *L, Index: llvm/lib/Analysis/MemoryBuiltins.cpp =================================================================== --- llvm/lib/Analysis/MemoryBuiltins.cpp +++ llvm/lib/Analysis/MemoryBuiltins.cpp @@ -1191,7 +1191,8 @@ // Compute offset/size for each PHI incoming pointer. for (unsigned i = 0, e = PHI.getNumIncomingValues(); i != e; ++i) { - Builder.SetInsertPoint(&*PHI.getIncomingBlock(i)->getFirstInsertionPt()); + BasicBlock *IncomingBlock = PHI.getIncomingBlock(i); + Builder.SetInsertPoint(IncomingBlock, IncomingBlock->getFirstInsertionPt()); SizeOffsetEvalType EdgeData = compute_(PHI.getIncomingValue(i)); if (!bothKnown(EdgeData)) { @@ -1203,8 +1204,8 @@ InsertedInstructions.erase(SizePHI); return unknown(); } - SizePHI->addIncoming(EdgeData.first, PHI.getIncomingBlock(i)); - OffsetPHI->addIncoming(EdgeData.second, PHI.getIncomingBlock(i)); + SizePHI->addIncoming(EdgeData.first, IncomingBlock); + OffsetPHI->addIncoming(EdgeData.second, IncomingBlock); } Value *Size = SizePHI, *Offset = OffsetPHI; Index: llvm/lib/CodeGen/CodeGenPrepare.cpp =================================================================== --- llvm/lib/CodeGen/CodeGenPrepare.cpp +++ llvm/lib/CodeGen/CodeGenPrepare.cpp @@ -2263,7 +2263,7 @@ // Create a PHI in the end block to select either the output of the intrinsic // or the bit width of the operand. - Builder.SetInsertPoint(&EndBlock->front()); + Builder.SetInsertPoint(EndBlock, EndBlock->begin()); PHINode *PN = Builder.CreatePHI(Ty, 2, "ctz"); replaceAllUsesWith(CountZeros, PN, FreshBBs, IsHugeFunc); Value *BitWidth = Builder.getInt(APInt(SizeInBits, SizeInBits)); Index: llvm/lib/CodeGen/ExpandMemCmp.cpp =================================================================== --- llvm/lib/CodeGen/ExpandMemCmp.cpp +++ llvm/lib/CodeGen/ExpandMemCmp.cpp @@ -558,7 +558,7 @@ } void MemCmpExpansion::setupEndBlockPHINodes() { - Builder.SetInsertPoint(&EndBlock->front()); + Builder.SetInsertPoint(EndBlock, EndBlock->begin()); PhiRes = Builder.CreatePHI(Type::getInt32Ty(CI->getContext()), 2, "phi.res"); } Index: llvm/lib/CodeGen/WasmEHPrepare.cpp =================================================================== --- llvm/lib/CodeGen/WasmEHPrepare.cpp +++ llvm/lib/CodeGen/WasmEHPrepare.cpp @@ -279,7 +279,7 @@ unsigned Index) { assert(BB->isEHPad() && "BB is not an EHPad!"); IRBuilder<> IRB(BB->getContext()); - IRB.SetInsertPoint(&*BB->getFirstInsertionPt()); + IRB.SetInsertPoint(BB, BB->getFirstInsertionPt()); auto *FPI = cast(BB->getFirstNonPHI()); Instruction *GetExnCI = nullptr, *GetSelectorCI = nullptr; Index: llvm/lib/IR/BasicBlock.cpp =================================================================== --- llvm/lib/IR/BasicBlock.cpp +++ llvm/lib/IR/BasicBlock.cpp @@ -220,6 +220,10 @@ return nullptr; } +BasicBlock::const_iterator BasicBlock::getFirstNonPHIIt() const { + return getFirstNonPHI()->getIterator(); +} + const Instruction *BasicBlock::getFirstNonPHIOrDbg(bool SkipPseudoOp) const { for (const Instruction &I : *this) { if (isa(I) || isa(I)) Index: llvm/lib/IR/Instruction.cpp =================================================================== --- llvm/lib/IR/Instruction.cpp +++ llvm/lib/IR/Instruction.cpp @@ -885,6 +885,12 @@ return nullptr; } +const DebugLoc &Instruction::getStableDebugLoc() const { + if (isa(this)) + return getNextNonDebugInstruction()->getDebugLoc(); + return getDebugLoc(); +} + bool Instruction::isAssociative() const { unsigned Opcode = getOpcode(); if (isAssociative(Opcode)) Index: llvm/lib/Target/AMDGPU/AMDGPUAtomicOptimizer.cpp =================================================================== --- llvm/lib/Target/AMDGPU/AMDGPUAtomicOptimizer.cpp +++ llvm/lib/Target/AMDGPU/AMDGPUAtomicOptimizer.cpp @@ -984,7 +984,7 @@ if (IsPixelShader) { // Need a final PHI to reconverge to above the helper lane branch mask. - B.SetInsertPoint(PixelExitBB->getFirstNonPHI()); + B.SetInsertPoint(PixelExitBB, PixelExitBB->getFirstNonPHIIt()); PHINode *const PHI = B.CreatePHI(Ty, 2); PHI->addIncoming(PoisonValue::get(Ty), PixelEntryBB); Index: llvm/lib/Target/AMDGPU/AMDGPULowerModuleLDSPass.cpp =================================================================== --- llvm/lib/Target/AMDGPU/AMDGPULowerModuleLDSPass.cpp +++ llvm/lib/Target/AMDGPU/AMDGPULowerModuleLDSPass.cpp @@ -294,7 +294,8 @@ // equivalent target specific intrinsic which lasts until immediately after // codegen would suffice for that, but one would still need to ensure that // the variables are allocated in the anticpated order. - IRBuilder<> Builder(Func->getEntryBlock().getFirstNonPHI()); + BasicBlock *Entry = &Func->getEntryBlock(); + IRBuilder<> Builder(Entry, Entry->getFirstNonPHIIt()); Function *Decl = Intrinsic::getDeclaration(Func->getParent(), Intrinsic::donothing, {}); Index: llvm/lib/Target/ARM/MVETailPredication.cpp =================================================================== --- llvm/lib/Target/ARM/MVETailPredication.cpp +++ llvm/lib/Target/ARM/MVETailPredication.cpp @@ -381,7 +381,7 @@ cast(ActiveLaneMask->getType())->getNumElements(); // Insert a phi to count the number of elements processed by the loop. - Builder.SetInsertPoint(L->getHeader()->getFirstNonPHI()); + Builder.SetInsertPoint(L->getHeader(), L->getHeader()->getFirstNonPHIIt()); PHINode *Processed = Builder.CreatePHI(Ty, 2); Processed->addIncoming(Start, L->getLoopPreheader()); Index: llvm/lib/Target/Hexagon/HexagonVectorLoopCarriedReuse.cpp =================================================================== --- llvm/lib/Target/Hexagon/HexagonVectorLoopCarriedReuse.cpp +++ llvm/lib/Target/Hexagon/HexagonVectorLoopCarriedReuse.cpp @@ -552,7 +552,7 @@ } BasicBlock *BB = BEInst->getParent(); IRBuilder<> IRB(BB); - IRB.SetInsertPoint(BB->getFirstNonPHI()); + IRB.SetInsertPoint(BB, BB->getFirstNonPHIIt()); Value *BEVal = BEInst; PHINode *NewPhi; for (int i = Iterations-1; i >=0 ; --i) { Index: llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp =================================================================== --- llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp +++ llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp @@ -474,7 +474,7 @@ AggrStores.insert(&I); } - IRB->SetInsertPoint(&Func.getEntryBlock().front()); + IRB->SetInsertPoint(&Func.getEntryBlock(), Func.getEntryBlock().begin()); for (auto &GV : Func.getParent()->globals()) processGlobalValue(GV); Index: llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp =================================================================== --- llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp +++ llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp @@ -1336,7 +1336,7 @@ // Add a phi to the tail, which will be the output of setjmp, which // indicates if this is the first call or a longjmp back. The phi directly // uses the right value based on where we arrive from - IRB.SetInsertPoint(Tail->getFirstNonPHI()); + IRB.SetInsertPoint(Tail, Tail->getFirstNonPHIIt()); PHINode *SetjmpRet = IRB.CreatePHI(IRB.getInt32Ty(), 2, "setjmp.ret"); // setjmp initial call returns 0 Index: llvm/lib/Target/X86/X86LowerAMXIntrinsics.cpp =================================================================== --- llvm/lib/Target/X86/X86LowerAMXIntrinsics.cpp +++ llvm/lib/Target/X86/X86LowerAMXIntrinsics.cpp @@ -495,7 +495,7 @@ KDWord, C, A, B); // we cannot assume there always be bitcast after tiledpbssd. So we need to // insert one bitcast as required - Builder.SetInsertPoint(End->getFirstNonPHI()); + Builder.SetInsertPoint(End, End->getFirstNonPHIIt()); Value *ResAMX = Builder.CreateBitCast(ResVec, Type::getX86_AMXTy(Builder.getContext())); // Delete TileDP intrinsic and do some clean-up. @@ -539,7 +539,7 @@ if (IsTileLoad) { // we cannot assume there always be bitcast after tileload. So we need to // insert one bitcast as required - Builder.SetInsertPoint(End->getFirstNonPHI()); + Builder.SetInsertPoint(End, End->getFirstNonPHIIt()); Value *ResAMX = Builder.CreateBitCast(ResVec, Type::getX86_AMXTy(Builder.getContext())); // Delete tileloadd6 intrinsic and do some clean-up Index: llvm/lib/Transforms/Coroutines/CoroFrame.cpp =================================================================== --- llvm/lib/Transforms/Coroutines/CoroFrame.cpp +++ llvm/lib/Transforms/Coroutines/CoroFrame.cpp @@ -1777,13 +1777,13 @@ auto SpillAlignment = Align(FrameData.getAlign(Def)); // Create a store instruction storing the value into the // coroutine frame. - Instruction *InsertPt = nullptr; + BasicBlock::iterator InsertPt; Type *ByValTy = nullptr; if (auto *Arg = dyn_cast(Def)) { // For arguments, we will place the store instruction right after // the coroutine frame pointer instruction, i.e. bitcast of // coro.begin from i8* to %f.frame*. - InsertPt = Shape.getInsertPtAfterFramePtr(); + InsertPt = Shape.getInsertPtAfterFramePtr()->getIterator(); // If we're spilling an Argument, make sure we clear 'nocapture' // from the coroutine function. @@ -1794,35 +1794,35 @@ } else if (auto *CSI = dyn_cast(Def)) { // Don't spill immediately after a suspend; splitting assumes // that the suspend will be followed by a branch. - InsertPt = CSI->getParent()->getSingleSuccessor()->getFirstNonPHI(); + InsertPt = CSI->getParent()->getSingleSuccessor()->getFirstNonPHIIt(); } else { auto *I = cast(Def); if (!DT.dominates(CB, I)) { // If it is not dominated by CoroBegin, then spill should be // inserted immediately after CoroFrame is computed. - InsertPt = Shape.getInsertPtAfterFramePtr(); + InsertPt = Shape.getInsertPtAfterFramePtr()->getIterator(); } else if (auto *II = dyn_cast(I)) { // If we are spilling the result of the invoke instruction, split // the normal edge and insert the spill in the new block. auto *NewBB = SplitEdge(II->getParent(), II->getNormalDest()); - InsertPt = NewBB->getTerminator(); + InsertPt = NewBB->getTerminator()->getIterator(); } else if (isa(I)) { // Skip the PHINodes and EH pads instructions. BasicBlock *DefBlock = I->getParent(); if (auto *CSI = dyn_cast(DefBlock->getTerminator())) - InsertPt = splitBeforeCatchSwitch(CSI); + InsertPt = splitBeforeCatchSwitch(CSI)->getIterator(); else - InsertPt = &*DefBlock->getFirstInsertionPt(); + InsertPt = DefBlock->getFirstInsertionPt(); } else { assert(!I->isTerminator() && "unexpected terminator"); // For all other values, the spill is placed immediately after // the definition. - InsertPt = I->getNextNode(); + InsertPt = I->getNextNode()->getIterator(); } } auto Index = FrameData.getFieldIndex(Def); - Builder.SetInsertPoint(InsertPt); + Builder.SetInsertPoint(InsertPt->getParent(), InsertPt); auto *G = Builder.CreateConstInBoundsGEP2_32( FrameTy, FramePtr, 0, Index, Def->getName() + Twine(".spill.addr")); if (ByValTy) { @@ -1842,7 +1842,8 @@ // reference provided with the frame GEP. if (CurrentBlock != U->getParent()) { CurrentBlock = U->getParent(); - Builder.SetInsertPoint(&*CurrentBlock->getFirstInsertionPt()); + Builder.SetInsertPoint(CurrentBlock, + CurrentBlock->getFirstInsertionPt()); auto *GEP = GetFramePointer(E.first); GEP->setName(E.first->getName() + Twine(".reload.addr")); @@ -1916,7 +1917,7 @@ if (Shape.ABI == coro::ABI::Retcon || Shape.ABI == coro::ABI::RetconOnce || Shape.ABI == coro::ABI::Async) { // If we found any allocas, replace all of their remaining uses with Geps. - Builder.SetInsertPoint(&SpillBlock->front()); + Builder.SetInsertPoint(SpillBlock, SpillBlock->begin()); for (const auto &P : FrameData.Allocas) { AllocaInst *Alloca = P.Alloca; auto *G = GetFramePointer(Alloca); @@ -1935,7 +1936,8 @@ // dbg.declares and dbg.values with the reload from the frame. // Note: We cannot replace the alloca with GEP instructions indiscriminately, // as some of the uses may not be dominated by CoroBegin. - Builder.SetInsertPoint(&Shape.AllocaSpillBlock->front()); + Builder.SetInsertPoint(Shape.AllocaSpillBlock, + Shape.AllocaSpillBlock->begin()); SmallVector UsersToUpdate; for (const auto &A : FrameData.Allocas) { AllocaInst *Alloca = A.Alloca; Index: llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -514,7 +514,8 @@ static void setInsertionPoint(IRBuilder<> &Builder, Value *V, bool Before = true) { if (auto *PHI = dyn_cast(V)) { - Builder.SetInsertPoint(&*PHI->getParent()->getFirstInsertionPt()); + BasicBlock *Parent = PHI->getParent(); + Builder.SetInsertPoint(Parent, Parent->getFirstInsertionPt()); return; } if (auto *I = dyn_cast(V)) { @@ -526,7 +527,7 @@ if (auto *A = dyn_cast(V)) { // Set the insertion point in the entry block. BasicBlock &Entry = A->getParent()->getEntryBlock(); - Builder.SetInsertPoint(&*Entry.getFirstInsertionPt()); + Builder.SetInsertPoint(&Entry, Entry.getFirstInsertionPt()); return; } // Otherwise, this is a constant and we don't need to set a new Index: llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp +++ llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp @@ -1368,7 +1368,7 @@ // sinking. auto InsertPt = BB->getFirstInsertionPt(); if (InsertPt != BB->end()) { - Self.Builder.SetInsertPoint(&*InsertPt); + Self.Builder.SetInsertPoint(&*BB, InsertPt); return Self.Builder.CreateNot(Cond); } Index: llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp @@ -2567,7 +2567,7 @@ return nullptr; } - Builder.SetInsertPoint(&*BB->begin()); + Builder.SetInsertPoint(BB, BB->begin()); auto *PN = Builder.CreatePHI(Sel.getType(), Inputs.size()); for (auto *Pred : predecessors(BB)) PN->addIncoming(Inputs[Pred], Pred); Index: llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp @@ -1121,7 +1121,7 @@ // Note that the same block can be a predecessor more than once, // and we need to preserve that invariant for the PHI node. BuilderTy::InsertPointGuard Guard(Builder); - Builder.SetInsertPoint(UseBB->getFirstNonPHI()); + Builder.SetInsertPoint(UseBB, UseBB->getFirstNonPHIIt()); auto *PHI = Builder.CreatePHI(AggTy, Preds.size(), OrigIVI.getName() + ".merged"); for (BasicBlock *Pred : Preds) Index: llvm/lib/Transforms/Scalar/LoopBoundSplit.cpp =================================================================== --- llvm/lib/Transforms/Scalar/LoopBoundSplit.cpp +++ llvm/lib/Transforms/Scalar/LoopBoundSplit.cpp @@ -430,7 +430,7 @@ ExitingCond.BI->setSuccessor(1, PostLoopPreHeader); // Update phi node in exit block of post-loop. - Builder.SetInsertPoint(&PostLoopPreHeader->front()); + Builder.SetInsertPoint(PostLoopPreHeader, PostLoopPreHeader->begin()); for (PHINode &PN : PostLoop->getExitBlock()->phis()) { for (auto i : seq(0, PN.getNumOperands())) { // Check incoming block is pre-loop's exiting block. Index: llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp =================================================================== --- llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp +++ llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp @@ -2493,7 +2493,7 @@ // Step 4: Rewrite the loop into a countable form, with canonical IV. // The new canonical induction variable. - Builder.SetInsertPoint(&LoopHeaderBB->front()); + Builder.SetInsertPoint(LoopHeaderBB, LoopHeaderBB->begin()); auto *IV = Builder.CreatePHI(Ty, 2, CurLoop->getName() + ".iv"); // The induction itself. @@ -2817,11 +2817,11 @@ // Step 3: Rewrite the loop into a countable form, with canonical IV. // The new canonical induction variable. - Builder.SetInsertPoint(&LoopHeaderBB->front()); + Builder.SetInsertPoint(LoopHeaderBB, LoopHeaderBB->begin()); auto *CIV = Builder.CreatePHI(Ty, 2, CurLoop->getName() + ".iv"); // The induction itself. - Builder.SetInsertPoint(LoopHeaderBB->getFirstNonPHI()); + Builder.SetInsertPoint(LoopHeaderBB, LoopHeaderBB->getFirstNonPHIIt()); auto *CIVNext = Builder.CreateAdd(CIV, ConstantInt::get(Ty, 1), CIV->getName() + ".next", /*HasNUW=*/true, /*HasNSW=*/Bitwidth != 2); Index: llvm/lib/Transforms/Scalar/RewriteStatepointsForGC.cpp =================================================================== --- llvm/lib/Transforms/Scalar/RewriteStatepointsForGC.cpp +++ llvm/lib/Transforms/Scalar/RewriteStatepointsForGC.cpp @@ -1827,7 +1827,7 @@ UnwindBlock->getUniquePredecessor() && "can't safely insert in this block!"); - Builder.SetInsertPoint(&*UnwindBlock->getFirstInsertionPt()); + Builder.SetInsertPoint(UnwindBlock, UnwindBlock->getFirstInsertionPt()); Builder.SetCurrentDebugLocation(II->getDebugLoc()); // Attach exceptional gc relocates to the landingpad. @@ -1842,7 +1842,7 @@ NormalDest->getUniquePredecessor() && "can't safely insert in this block!"); - Builder.SetInsertPoint(&*NormalDest->getFirstInsertionPt()); + Builder.SetInsertPoint(NormalDest, NormalDest->getFirstInsertionPt()); // gc relocates will be generated later as if it were regular call // statepoint Index: llvm/lib/Transforms/Scalar/SROA.cpp =================================================================== --- llvm/lib/Transforms/Scalar/SROA.cpp +++ llvm/lib/Transforms/Scalar/SROA.cpp @@ -3430,7 +3430,8 @@ // dominate the PHI. IRBuilderBase::InsertPointGuard Guard(IRB); if (isa(OldPtr)) - IRB.SetInsertPoint(&*OldPtr->getParent()->getFirstInsertionPt()); + IRB.SetInsertPoint(OldPtr->getParent(), + OldPtr->getParent()->getFirstInsertionPt()); else IRB.SetInsertPoint(OldPtr); IRB.SetCurrentDebugLocation(OldPtr->getDebugLoc()); @@ -3819,7 +3820,7 @@ SmallVector Index(GEPI.indices()); bool IsInBounds = GEPI.isInBounds(); - IRB.SetInsertPoint(GEPI.getParent()->getFirstNonPHI()); + IRB.SetInsertPoint(GEPI.getParent(), GEPI.getParent()->getFirstNonPHIIt()); PHINode *NewPN = IRB.CreatePHI(GEPI.getType(), PHI->getNumIncomingValues(), PHI->getName() + ".sroa.phi"); for (unsigned I = 0, E = PHI->getNumIncomingValues(); I != E; ++I) { Index: llvm/lib/Transforms/Utils/CallPromotionUtils.cpp =================================================================== --- llvm/lib/Transforms/Utils/CallPromotionUtils.cpp +++ llvm/lib/Transforms/Utils/CallPromotionUtils.cpp @@ -111,7 +111,7 @@ if (OrigInst->getType()->isVoidTy() || OrigInst->use_empty()) return; - Builder.SetInsertPoint(&MergeBlock->front()); + Builder.SetInsertPoint(MergeBlock, MergeBlock->begin()); PHINode *Phi = Builder.CreatePHI(OrigInst->getType(), 0); SmallVector UsersToUpdate(OrigInst->users()); for (User *U : UsersToUpdate) Index: llvm/lib/Transforms/Utils/InlineFunction.cpp =================================================================== --- llvm/lib/Transforms/Utils/InlineFunction.cpp +++ llvm/lib/Transforms/Utils/InlineFunction.cpp @@ -2438,7 +2438,7 @@ // `Caller->isPresplitCoroutine()` would affect AlwaysInliner at O0 only. if ((InsertLifetime || Caller->isPresplitCoroutine()) && !IFI.StaticAllocas.empty()) { - IRBuilder<> builder(&FirstNewBlock->front()); + IRBuilder<> builder(&*FirstNewBlock, FirstNewBlock->begin()); for (unsigned ai = 0, ae = IFI.StaticAllocas.size(); ai != ae; ++ai) { AllocaInst *AI = IFI.StaticAllocas[ai]; // Don't mark swifterror allocas. They can't have bitcast uses. Index: llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp =================================================================== --- llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp +++ llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp @@ -1014,8 +1014,8 @@ if (useSubtract) Step = SE.getNegativeSCEV(Step); // Expand the step somewhere that dominates the loop header. - Value *StepV = expandCodeForImpl( - Step, IntTy, &*L->getHeader()->getFirstInsertionPt()); + Value *StepV = + expandCodeForImpl(Step, IntTy, L->getHeader()->getFirstInsertionPt()); // The no-wrap behavior proved by IsIncrement(NUW|NSW) is only applicable if // we actually do emit an addition. It does not apply if we emit a @@ -1173,8 +1173,8 @@ { // Expand the step somewhere that dominates the loop header. SCEVInsertPointGuard Guard(Builder, this); - StepV = expandCodeForImpl( - Step, IntTy, &*L->getHeader()->getFirstInsertionPt()); + StepV = expandCodeForImpl(Step, IntTy, + L->getHeader()->getFirstInsertionPt()); } Result = expandIVInc(PN, StepV, L, ExpandTy, IntTy, useSubtract); } @@ -1260,7 +1260,7 @@ BasicBlock::iterator NewInsertPt = findInsertPointAfter(cast(V), &*Builder.GetInsertPoint()); V = expandCodeForImpl(SE.getTruncateExpr(SE.getUnknown(V), Ty), nullptr, - &*NewInsertPt); + NewInsertPt); return V; } @@ -1440,7 +1440,7 @@ } Value *SCEVExpander::expandCodeForImpl(const SCEV *SH, Type *Ty, - Instruction *IP) { + BasicBlock::iterator IP) { setInsertPoint(IP); Value *V = expandCodeForImpl(SH, Ty); return V; @@ -1556,7 +1556,7 @@ Value *SCEVExpander::expand(const SCEV *S) { // Compute an insertion point for this SCEV object. Hoist the instructions // as far out in the loop nest as possible. - Instruction *InsertPt = &*Builder.GetInsertPoint(); + BasicBlock::iterator InsertPt = Builder.GetInsertPoint(); // We can move insertion point only if there is no div or rem operations // otherwise we are risky to move it over the check for zero denominator. @@ -1580,24 +1580,25 @@ L = L->getParentLoop()) { if (SE.isLoopInvariant(S, L)) { if (!L) break; - if (BasicBlock *Preheader = L->getLoopPreheader()) - InsertPt = Preheader->getTerminator(); - else + if (BasicBlock *Preheader = L->getLoopPreheader()) { + InsertPt = Preheader->getTerminator()->getIterator(); + } else { // LSR sets the insertion point for AddRec start/step values to the // block start to simplify value reuse, even though it's an invalid // position. SCEVExpander must correct for this in all cases. - InsertPt = &*L->getHeader()->getFirstInsertionPt(); + InsertPt = L->getHeader()->getFirstInsertionPt(); + } } else { // If the SCEV is computable at this level, insert it into the header // after the PHIs (and after any other instructions that we've inserted // there) so that it is guaranteed to dominate any user inside the loop. if (L && SE.hasComputableLoopEvolution(S, L) && !PostIncLoops.count(L)) - InsertPt = &*L->getHeader()->getFirstInsertionPt(); + InsertPt = L->getHeader()->getFirstInsertionPt(); - while (InsertPt->getIterator() != Builder.GetInsertPoint() && - (isInsertedInstruction(InsertPt) || - isa(InsertPt))) { - InsertPt = &*std::next(InsertPt->getIterator()); + while (InsertPt != Builder.GetInsertPoint() && + (isInsertedInstruction(&*InsertPt) || + isa(&*InsertPt))) { + InsertPt = std::next(InsertPt); } break; } @@ -1605,16 +1606,16 @@ } // Check to see if we already expanded this here. - auto I = InsertedExpressions.find(std::make_pair(S, InsertPt)); + auto I = InsertedExpressions.find(std::make_pair(S, &*InsertPt)); if (I != InsertedExpressions.end()) return I->second; SCEVInsertPointGuard Guard(Builder, this); - Builder.SetInsertPoint(InsertPt); + Builder.SetInsertPoint(InsertPt->getParent(), InsertPt); // Expand the expression into instructions. SmallVector DropPoisonGeneratingInsts; - Value *V = FindValueInExprValueMap(S, InsertPt, DropPoisonGeneratingInsts); + Value *V = FindValueInExprValueMap(S, &*InsertPt, DropPoisonGeneratingInsts); if (!V) { V = visit(S); V = fixupLCSSAFormFor(V); @@ -1628,7 +1629,7 @@ // the expression at this insertion point. If the mapped value happened to be // a postinc expansion, it could be reused by a non-postinc user, but only if // its insertion point was already at the head of the loop. - InsertedExpressions[std::make_pair(S, InsertPt)] = V; + InsertedExpressions[std::make_pair(S, &*InsertPt)] = V; return V; } @@ -1765,13 +1766,13 @@ << *IsomorphicInc << '\n'); Value *NewInc = OrigInc; if (OrigInc->getType() != IsomorphicInc->getType()) { - Instruction *IP = nullptr; + BasicBlock::iterator IP; if (PHINode *PN = dyn_cast(OrigInc)) - IP = &*PN->getParent()->getFirstInsertionPt(); + IP = PN->getParent()->getFirstInsertionPt(); else - IP = OrigInc->getNextNode(); + IP = OrigInc->getNextNonDebugInstruction()->getIterator(); - IRBuilder<> Builder(IP); + IRBuilder<> Builder(IP->getParent(), IP); Builder.SetCurrentDebugLocation(IsomorphicInc->getDebugLoc()); NewInc = Builder.CreateTruncOrBitCast( OrigInc, IsomorphicInc->getType(), IVName); @@ -1789,7 +1790,8 @@ ++NumElim; Value *NewIV = OrigPhiRef; if (OrigPhiRef->getType() != Phi->getType()) { - IRBuilder<> Builder(&*L->getHeader()->getFirstInsertionPt()); + IRBuilder<> Builder(L->getHeader(), + L->getHeader()->getFirstInsertionPt()); Builder.SetCurrentDebugLocation(Phi->getDebugLoc()); NewIV = Builder.CreateTruncOrBitCast(OrigPhiRef, Phi->getType(), IVName); } Index: llvm/lib/Transforms/Utils/SimplifyCFG.cpp =================================================================== --- llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ llvm/lib/Transforms/Utils/SimplifyCFG.cpp @@ -4000,7 +4000,9 @@ Value *QPHI = ensureValueAvailableInSuccessor(QStore->getValueOperand(), QStore->getParent(), PPHI); - IRBuilder<> QB(&*PostBB->getFirstInsertionPt()); + BasicBlock::iterator PostBBFirst = PostBB->getFirstInsertionPt(); + IRBuilder<> QB(PostBB, PostBBFirst); + QB.SetCurrentDebugLocation(PostBBFirst->getStableDebugLoc()); Value *PPred = PStore->getParent() == PTB ? PCond : QB.CreateNot(PCond); Value *QPred = QStore->getParent() == QTB ? QCond : QB.CreateNot(QCond); Index: llvm/lib/Transforms/Utils/SimplifyIndVar.cpp =================================================================== --- llvm/lib/Transforms/Utils/SimplifyIndVar.cpp +++ llvm/lib/Transforms/Utils/SimplifyIndVar.cpp @@ -1673,7 +1673,8 @@ assert(LoopExitingBlock && L->contains(LoopExitingBlock) && "Not a LCSSA Phi?"); WidePN->addIncoming(WideBO, LoopExitingBlock); - Builder.SetInsertPoint(&*User->getParent()->getFirstInsertionPt()); + Builder.SetInsertPoint(User->getParent(), + User->getParent()->getFirstInsertionPt()); auto *TruncPN = Builder.CreateTrunc(WidePN, User->getType()); User->replaceAllUsesWith(TruncPN); DeadInsts.emplace_back(User); @@ -1726,7 +1727,8 @@ PHINode::Create(DU.WideDef->getType(), 1, UsePhi->getName() + ".wide", UsePhi); WidePhi->addIncoming(DU.WideDef, UsePhi->getIncomingBlock(0)); - IRBuilder<> Builder(&*WidePhi->getParent()->getFirstInsertionPt()); + BasicBlock *WidePhiBB = WidePhi->getParent(); + IRBuilder<> Builder(WidePhiBB, WidePhiBB->getFirstInsertionPt()); Value *Trunc = Builder.CreateTrunc(WidePhi, DU.NarrowDef->getType()); UsePhi->replaceAllUsesWith(Trunc); DeadInsts.emplace_back(UsePhi); Index: llvm/lib/Transforms/Vectorize/LoopVectorize.cpp =================================================================== --- llvm/lib/Transforms/Vectorize/LoopVectorize.cpp +++ llvm/lib/Transforms/Vectorize/LoopVectorize.cpp @@ -2986,7 +2986,8 @@ // Compute the end value for the additional bypass (if applicable). if (AdditionalBypass.first) { - B.SetInsertPoint(&(*AdditionalBypass.first->getFirstInsertionPt())); + B.SetInsertPoint(AdditionalBypass.first, + AdditionalBypass.first->getFirstInsertionPt()); EndValueFromAdditionalBypass = emitTransformedIndex(B, AdditionalBypass.second, II.getStartValue(), Step, II.getKind(), II.getInductionBinOp()); @@ -3581,7 +3582,8 @@ // Fix LCSSA phis not already fixed earlier. Extracts may need to be generated // in the exit block, so update the builder. - State.Builder.SetInsertPoint(State.CFG.ExitBB->getFirstNonPHI()); + State.Builder.SetInsertPoint(State.CFG.ExitBB, + State.CFG.ExitBB->getFirstNonPHIIt()); for (const auto &KV : Plan.getLiveOuts()) KV.second->fixPhi(Plan, State); @@ -3766,7 +3768,7 @@ } // Fix the initial value of the original recurrence in the scalar loop. - Builder.SetInsertPoint(&*LoopScalarPreHeader->begin()); + Builder.SetInsertPoint(LoopScalarPreHeader, LoopScalarPreHeader->begin()); PHINode *Phi = cast(PhiR->getUnderlyingValue()); auto *Start = Builder.CreatePHI(Phi->getType(), 2, "scalar.recur.init"); auto *ScalarInit = PhiR->getStartValue()->getLiveInIRValue(); @@ -3801,7 +3803,8 @@ // the PHIs and the values we are going to write. // This allows us to write both PHINodes and the extractelement // instructions. - Builder.SetInsertPoint(&*LoopMiddleBlock->getFirstInsertionPt()); + Builder.SetInsertPoint(LoopMiddleBlock, + LoopMiddleBlock->getFirstInsertionPt()); State.setDebugLocFrom(LoopExitInst->getDebugLoc()); @@ -3866,7 +3869,8 @@ RdxParts[Part] = Extnd; } } - Builder.SetInsertPoint(&*LoopMiddleBlock->getFirstInsertionPt()); + Builder.SetInsertPoint(LoopMiddleBlock, + LoopMiddleBlock->getFirstInsertionPt()); for (unsigned Part = 0; Part < UF; ++Part) { RdxParts[Part] = Builder.CreateTrunc(RdxParts[Part], RdxVecTy); State.reset(LoopExitInstDef, RdxParts[Part], Part); Index: llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp =================================================================== --- llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp +++ llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp @@ -9351,18 +9351,20 @@ auto *Front = E->getMainOp(); Instruction *LastInst = &getLastInstructionInBundle(E); assert(LastInst && "Failed to find last instruction in bundle"); + BasicBlock::iterator LastInstIt = LastInst->getIterator(); // If the instruction is PHI, set the insert point after all the PHIs. bool IsPHI = isa(LastInst); if (IsPHI) - LastInst = LastInst->getParent()->getFirstNonPHI(); + LastInstIt = LastInst->getParent()->getFirstNonPHIIt(); if (IsPHI || (E->State != TreeEntry::NeedToGather && doesNotNeedToSchedule(E->Scalars))) { - Builder.SetInsertPoint(LastInst); + Builder.SetInsertPoint(LastInst->getParent(), LastInstIt); } else { // Set the insertion point after the last instruction in the bundle. Set the // debug location to Front. - Builder.SetInsertPoint(LastInst->getParent(), - std::next(LastInst->getIterator())); + Builder.SetInsertPoint( + LastInst->getParent(), + LastInst->getNextNonDebugInstruction()->getIterator()); } Builder.SetCurrentDebugLocation(Front->getDebugLoc()); } @@ -9877,7 +9879,7 @@ E->getOpcode() != Instruction::PHI) { Instruction *LastInst = &getLastInstructionInBundle(E); assert(LastInst && "Failed to find last instruction in bundle"); - Builder.SetInsertPoint(LastInst); + Builder.SetInsertPoint(LastInst->getParent(), LastInst->getIterator()); } return vectorizeTree(I->get()); } @@ -10292,13 +10294,15 @@ !E->UserTreeIndices.empty()) && "PHI reordering is free."); auto *PH = cast(VL0); - Builder.SetInsertPoint(PH->getParent()->getFirstNonPHI()); + Builder.SetInsertPoint(PH->getParent(), + PH->getParent()->getFirstNonPHIIt()); Builder.SetCurrentDebugLocation(PH->getDebugLoc()); PHINode *NewPhi = Builder.CreatePHI(VecTy, PH->getNumIncomingValues()); Value *V = NewPhi; // Adjust insertion point once all PHI's have been generated. - Builder.SetInsertPoint(&*PH->getParent()->getFirstInsertionPt()); + Builder.SetInsertPoint(PH->getParent(), + PH->getParent()->getFirstInsertionPt()); Builder.SetCurrentDebugLocation(PH->getDebugLoc()); V = FinalShuffle(V, E); @@ -10946,8 +10950,12 @@ // need to rebuild it. EntryToLastInstruction.clear(); - Builder.SetInsertPoint(ReductionRoot ? ReductionRoot - : &F->getEntryBlock().front()); + if (ReductionRoot) + Builder.SetInsertPoint(ReductionRoot->getParent(), + ReductionRoot->getIterator()); + else + Builder.SetInsertPoint(&F->getEntryBlock(), F->getEntryBlock().begin()); + auto *VectorRoot = vectorizeTree(VectorizableTree[0].get()); // Run through the list of postponed gathers and emit them, replacing the temp // emitted allocas with actual vector instructions. @@ -10990,7 +10998,8 @@ // If current instr is a phi and not the last phi, insert it after the // last phi node. if (isa(I)) - Builder.SetInsertPoint(&*I->getParent()->getFirstInsertionPt()); + Builder.SetInsertPoint(I->getParent(), + I->getParent()->getFirstInsertionPt()); else Builder.SetInsertPoint(&*++BasicBlock::iterator(I)); } @@ -11090,12 +11099,13 @@ "ExternallyUsedValues map"); if (auto *VecI = dyn_cast(Vec)) { if (auto *PHI = dyn_cast(VecI)) - Builder.SetInsertPoint(PHI->getParent()->getFirstNonPHI()); + Builder.SetInsertPoint(PHI->getParent(), + PHI->getParent()->getFirstNonPHIIt()); else Builder.SetInsertPoint(VecI->getParent(), std::next(VecI->getIterator())); } else { - Builder.SetInsertPoint(&F->getEntryBlock().front()); + Builder.SetInsertPoint(&F->getEntryBlock(), F->getEntryBlock().begin()); } Value *NewInst = ExtractAndExtendIfNeeded(Vec); // Required to update internally referenced instructions. @@ -11193,7 +11203,7 @@ User->replaceUsesOfWith(Scalar, NewInst); } } else { - Builder.SetInsertPoint(&F->getEntryBlock().front()); + Builder.SetInsertPoint(&F->getEntryBlock(), F->getEntryBlock().begin()); Value *NewInst = ExtractAndExtendIfNeeded(Vec); User->replaceUsesOfWith(Scalar, NewInst); }