diff --git a/llvm/include/llvm/IR/BasicBlock.h b/llvm/include/llvm/IR/BasicBlock.h --- a/llvm/include/llvm/IR/BasicBlock.h +++ b/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. diff --git a/llvm/include/llvm/IR/Instruction.h b/llvm/include/llvm/IR/Instruction.h --- a/llvm/include/llvm/IR/Instruction.h +++ b/llvm/include/llvm/IR/Instruction.h @@ -123,6 +123,9 @@ /// Insert an unlinked instruction into a basic block immediately before /// the specified instruction. void insertBefore(Instruction *InsertPos); + void insertBefore(SymbolTableList::iterator InsertPos) { + insertBefore(&*InsertPos); + } /// Insert an unlinked instruction into a basic block immediately after the /// specified instruction. @@ -133,6 +136,11 @@ SymbolTableList::iterator insertInto(BasicBlock *ParentBB, SymbolTableList::iterator It); + void insertBefore(BasicBlock &BB, + SymbolTableList::iterator InsertPos) { + insertInto(&BB, InsertPos); + } + /// Unlink this instruction from its current basic block and insert it into /// the basic block that MovePos lives in, right before MovePos. void moveBefore(Instruction *MovePos); diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp --- a/llvm/lib/CodeGen/CodeGenPrepare.cpp +++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp @@ -7097,7 +7097,8 @@ // to get the PHI operand. for (SelectInst *SI : llvm::reverse(ASI)) { // The select itself is replaced with a PHI Node. - PHINode *PN = PHINode::Create(SI->getType(), 2, "", &EndBlock->front()); + PHINode *PN = PHINode::Create(SI->getType(), 2, ""); + PN->insertBefore(EndBlock->begin()); PN->takeName(SI); PN->addIncoming(getTrueOrFalseValue(SI, true, INS), TrueBlock); PN->addIncoming(getTrueOrFalseValue(SI, false, INS), FalseBlock); diff --git a/llvm/lib/CodeGen/SelectOptimize.cpp b/llvm/lib/CodeGen/SelectOptimize.cpp --- a/llvm/lib/CodeGen/SelectOptimize.cpp +++ b/llvm/lib/CodeGen/SelectOptimize.cpp @@ -505,7 +505,8 @@ for (auto It = ASI.rbegin(); It != ASI.rend(); ++It) { SelectInst *SI = *It; // The select itself is replaced with a PHI Node. - PHINode *PN = PHINode::Create(SI->getType(), 2, "", &EndBlock->front()); + PHINode *PN = PHINode::Create(SI->getType(), 2, ""); + PN->insertBefore(EndBlock->begin()); PN->takeName(SI); PN->addIncoming(getTrueOrFalseValue(SI, true, INS), TrueBlock); PN->addIncoming(getTrueOrFalseValue(SI, false, INS), FalseBlock); diff --git a/llvm/lib/IR/BasicBlock.cpp b/llvm/lib/IR/BasicBlock.cpp --- a/llvm/lib/IR/BasicBlock.cpp +++ b/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)) diff --git a/llvm/lib/Target/AMDGPU/SIAnnotateControlFlow.cpp b/llvm/lib/Target/AMDGPU/SIAnnotateControlFlow.cpp --- a/llvm/lib/Target/AMDGPU/SIAnnotateControlFlow.cpp +++ b/llvm/lib/Target/AMDGPU/SIAnnotateControlFlow.cpp @@ -268,7 +268,8 @@ return false; BasicBlock *Target = Term->getSuccessor(1); - PHINode *Broken = PHINode::Create(IntMask, 0, "phi.broken", &Target->front()); + PHINode *Broken = PHINode::Create(IntMask, 0, "phi.broken"); + Broken->insertBefore(Target->begin()); Value *Cond = Term->getCondition(); Term->setCondition(BoolTrue); diff --git a/llvm/lib/Target/PowerPC/PPCLoopInstrFormPrep.cpp b/llvm/lib/Target/PowerPC/PPCLoopInstrFormPrep.cpp --- a/llvm/lib/Target/PowerPC/PPCLoopInstrFormPrep.cpp +++ b/llvm/lib/Target/PowerPC/PPCLoopInstrFormPrep.cpp @@ -707,8 +707,8 @@ BasicBlock *LoopPredecessor = L->getLoopPredecessor(); PHINode *NewPHI = PHINode::Create(I8PtrTy, HeaderLoopPredCount, - getInstrName(BaseMemI, PHINodeNameSuffix), - Header->getFirstNonPHI()); + getInstrName(BaseMemI, PHINodeNameSuffix)); + NewPHI->insertBefore(Header->getFirstNonPHIIt()); Value *BasePtrStart = SCEVE.expandCodeFor(BasePtrStartSCEV, I8PtrTy, LoopPredecessor->getTerminator()); diff --git a/llvm/lib/Transforms/Coroutines/CoroFrame.cpp b/llvm/lib/Transforms/Coroutines/CoroFrame.cpp --- a/llvm/lib/Transforms/Coroutines/CoroFrame.cpp +++ b/llvm/lib/Transforms/Coroutines/CoroFrame.cpp @@ -2051,8 +2051,8 @@ int Index = PN->getBasicBlockIndex(InsertedBB); Value *V = PN->getIncomingValue(Index); PHINode *InputV = PHINode::Create( - V->getType(), 1, V->getName() + Twine(".") + SuccBB->getName(), - &InsertedBB->front()); + V->getType(), 1, V->getName() + Twine(".") + SuccBB->getName()); + InputV->insertBefore(InsertedBB->begin()); InputV->addIncoming(V, PredBB); PN->setIncomingValue(Index, InputV); PN = dyn_cast(PN->getNextNode()); @@ -2198,7 +2198,8 @@ // ehAwareSplitEdge will clone the LandingPad in all the edge blocks. // We replace the original landing pad with a PHINode that will collect the // results from all of them. - ReplPHI = PHINode::Create(LandingPad->getType(), 1, "", LandingPad); + ReplPHI = PHINode::Create(LandingPad->getType(), 1, ""); + ReplPHI->insertBefore(LandingPad->getIterator()); ReplPHI->takeName(LandingPad); LandingPad->replaceAllUsesWith(ReplPHI); // We will erase the original landing pad at the end of this function after diff --git a/llvm/lib/Transforms/Coroutines/CoroSplit.cpp b/llvm/lib/Transforms/Coroutines/CoroSplit.cpp --- a/llvm/lib/Transforms/Coroutines/CoroSplit.cpp +++ b/llvm/lib/Transforms/Coroutines/CoroSplit.cpp @@ -457,7 +457,8 @@ Switch->addCase(IndexVal, ResumeBB); cast(SuspendBB->getTerminator())->setSuccessor(0, LandingBB); - auto *PN = PHINode::Create(Builder.getInt8Ty(), 2, "", &LandingBB->front()); + auto *PN = PHINode::Create(Builder.getInt8Ty(), 2, ""); + PN->insertBefore(LandingBB->begin()); S->replaceAllUsesWith(PN); PN->addIncoming(Builder.getInt8(-1), SuspendBB); PN->addIncoming(S, ResumeBB); diff --git a/llvm/lib/Transforms/IPO/PartialInlining.cpp b/llvm/lib/Transforms/IPO/PartialInlining.cpp --- a/llvm/lib/Transforms/IPO/PartialInlining.cpp +++ b/llvm/lib/Transforms/IPO/PartialInlining.cpp @@ -1042,7 +1042,7 @@ ClonedOI->ReturnBlock = ClonedOI->ReturnBlock->splitBasicBlock( ClonedOI->ReturnBlock->getFirstNonPHI()->getIterator()); BasicBlock::iterator I = PreReturn->begin(); - Instruction *Ins = &ClonedOI->ReturnBlock->front(); + BasicBlock::iterator Ins = ClonedOI->ReturnBlock->begin(); SmallVector DeadPhis; while (I != PreReturn->end()) { PHINode *OldPhi = dyn_cast(I); @@ -1050,9 +1050,10 @@ break; PHINode *RetPhi = - PHINode::Create(OldPhi->getType(), NumPredsFromEntries + 1, "", Ins); + PHINode::Create(OldPhi->getType(), NumPredsFromEntries + 1, ""); + RetPhi->insertBefore(Ins); OldPhi->replaceAllUsesWith(RetPhi); - Ins = ClonedOI->ReturnBlock->getFirstNonPHI(); + Ins = ClonedOI->ReturnBlock->getFirstNonPHIIt(); RetPhi->addIncoming(&*I, PreReturn); for (BasicBlock *E : ClonedOI->ReturnBlockPreds) { diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp --- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -3680,7 +3680,7 @@ bool Changed = false; if (&FI != MoveBefore) { - FI.moveBefore(MoveBefore); + FI.moveBefore(*MoveBefore->getParent(), MoveBefore->getIterator()); Changed = true; } @@ -3883,7 +3883,7 @@ /// the new position. BasicBlock::iterator InsertPos = DestBlock->getFirstInsertionPt(); - I->moveBefore(&*InsertPos); + I->moveBefore(*DestBlock, InsertPos); ++NumSunkInst; // Also sink all related debug uses from the source basic block. Otherwise we diff --git a/llvm/lib/Transforms/Instrumentation/ControlHeightReduction.cpp b/llvm/lib/Transforms/Instrumentation/ControlHeightReduction.cpp --- a/llvm/lib/Transforms/Instrumentation/ControlHeightReduction.cpp +++ b/llvm/lib/Transforms/Instrumentation/ControlHeightReduction.cpp @@ -1593,8 +1593,8 @@ // Insert a trivial phi for I (phi [&I, P0], [&I, P1], ...) at // ExitBlock. Replace I with the new phi in UI unless UI is another // phi at ExitBlock. - PHINode *PN = PHINode::Create(I.getType(), pred_size(ExitBlock), "", - &ExitBlock->front()); + PHINode *PN = PHINode::Create(I.getType(), pred_size(ExitBlock), ""); + PN->insertBefore(ExitBlock->begin()); for (BasicBlock *Pred : predecessors(ExitBlock)) { PN->addIncoming(&I, Pred); } diff --git a/llvm/lib/Transforms/Scalar/GVN.cpp b/llvm/lib/Transforms/Scalar/GVN.cpp --- a/llvm/lib/Transforms/Scalar/GVN.cpp +++ b/llvm/lib/Transforms/Scalar/GVN.cpp @@ -2987,9 +2987,9 @@ ++NumGVNPRE; // Create a PHI to make the value available in this block. - PHINode *Phi = - PHINode::Create(CurInst->getType(), predMap.size(), - CurInst->getName() + ".pre-phi", &CurrentBlock->front()); + PHINode *Phi = PHINode::Create(CurInst->getType(), predMap.size(), + CurInst->getName() + ".pre-phi"); + Phi->insertBefore(CurrentBlock->begin()); for (unsigned i = 0, e = predMap.size(); i != e; ++i) { if (Value *V = predMap[i].first) { // If we use an existing value in this phi, we have to patch the original diff --git a/llvm/lib/Transforms/Scalar/GVNSink.cpp b/llvm/lib/Transforms/Scalar/GVNSink.cpp --- a/llvm/lib/Transforms/Scalar/GVNSink.cpp +++ b/llvm/lib/Transforms/Scalar/GVNSink.cpp @@ -850,8 +850,9 @@ // Create a new PHI in the successor block and populate it. auto *Op = I0->getOperand(O); assert(!Op->getType()->isTokenTy() && "Can't PHI tokens!"); - auto *PN = PHINode::Create(Op->getType(), Insts.size(), - Op->getName() + ".sink", &BBEnd->front()); + auto *PN = + PHINode::Create(Op->getType(), Insts.size(), Op->getName() + ".sink"); + PN->insertBefore(BBEnd->begin()); for (auto *I : Insts) PN->addIncoming(I->getOperand(O), I->getParent()); NewOperands.push_back(PN); diff --git a/llvm/lib/Transforms/Scalar/JumpThreading.cpp b/llvm/lib/Transforms/Scalar/JumpThreading.cpp --- a/llvm/lib/Transforms/Scalar/JumpThreading.cpp +++ b/llvm/lib/Transforms/Scalar/JumpThreading.cpp @@ -1433,8 +1433,8 @@ // Create a PHI node at the start of the block for the PRE'd load value. pred_iterator PB = pred_begin(LoadBB), PE = pred_end(LoadBB); - PHINode *PN = PHINode::Create(LoadI->getType(), std::distance(PB, PE), "", - &LoadBB->front()); + PHINode *PN = PHINode::Create(LoadI->getType(), std::distance(PB, PE), ""); + PN->insertBefore(LoadBB->begin()); PN->takeName(LoadI); PN->setDebugLoc(LoadI->getDebugLoc()); diff --git a/llvm/lib/Transforms/Scalar/LICM.cpp b/llvm/lib/Transforms/Scalar/LICM.cpp --- a/llvm/lib/Transforms/Scalar/LICM.cpp +++ b/llvm/lib/Transforms/Scalar/LICM.cpp @@ -1489,8 +1489,9 @@ if (LI->wouldBeOutOfLoopUseRequiringLCSSA(Op.get(), PN.getParent())) { auto *OInst = cast(Op.get()); PHINode *OpPN = - PHINode::Create(OInst->getType(), PN.getNumIncomingValues(), - OInst->getName() + ".lcssa", &ExitBlock.front()); + PHINode::Create(OInst->getType(), PN.getNumIncomingValues(), + OInst->getName() + ".lcssa"); + OpPN->insertBefore(ExitBlock.begin()); for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) OpPN->addIncoming(OInst, PN.getIncomingBlock(i)); Op = OpPN; @@ -1832,7 +1833,8 @@ // We need to create an LCSSA PHI node for the incoming value and // store that. PHINode *PN = PHINode::Create(I->getType(), PredCache.size(BB), - I->getName() + ".lcssa", &BB->front()); + I->getName() + ".lcssa"); + PN->insertBefore(BB->begin()); for (BasicBlock *Pred : PredCache.get(BB)) PN->addIncoming(I, Pred); return PN; diff --git a/llvm/lib/Transforms/Scalar/LoopFuse.cpp b/llvm/lib/Transforms/Scalar/LoopFuse.cpp --- a/llvm/lib/Transforms/Scalar/LoopFuse.cpp +++ b/llvm/lib/Transforms/Scalar/LoopFuse.cpp @@ -1473,12 +1473,13 @@ for (Instruction *I : HoistInsts) { assert(I->getParent() == FC1.Preheader); - I->moveBefore(FC0.Preheader->getTerminator()); + I->moveBefore(*FC0.Preheader, + FC0.Preheader->getTerminator()->getIterator()); } // insert instructions in reverse order to maintain dominance relationship for (Instruction *I : reverse(SinkInsts)) { assert(I->getParent() == FC1.Preheader); - I->moveBefore(&*FC1.ExitBlock->getFirstInsertionPt()); + I->moveBefore(*FC1.ExitBlock, FC1.ExitBlock->getFirstInsertionPt()); } } @@ -1671,7 +1672,7 @@ // exiting the first and jumping to the header of the second does not break // the SSA property of the phis originally in the first loop. See also the // comment above. - Instruction *L1HeaderIP = &FC1.Header->front(); + BasicBlock::iterator L1HeaderIP = FC1.Header->begin(); for (PHINode *LCPHI : OriginalFC0PHIs) { int L1LatchBBIdx = LCPHI->getBasicBlockIndex(FC1.Latch); assert(L1LatchBBIdx >= 0 && @@ -1679,8 +1680,9 @@ Value *LCV = LCPHI->getIncomingValue(L1LatchBBIdx); - PHINode *L1HeaderPHI = PHINode::Create( - LCV->getType(), 2, LCPHI->getName() + ".afterFC0", L1HeaderIP); + PHINode *L1HeaderPHI = + PHINode::Create(LCV->getType(), 2, LCPHI->getName() + ".afterFC0"); + L1HeaderPHI->insertBefore(L1HeaderIP); L1HeaderPHI->addIncoming(LCV, FC0.Latch); L1HeaderPHI->addIncoming(UndefValue::get(LCV->getType()), FC0.ExitingBlock); @@ -1953,7 +1955,7 @@ // exiting the first and jumping to the header of the second does not break // the SSA property of the phis originally in the first loop. See also the // comment above. - Instruction *L1HeaderIP = &FC1.Header->front(); + BasicBlock::iterator L1HeaderIP = FC1.Header->begin(); for (PHINode *LCPHI : OriginalFC0PHIs) { int L1LatchBBIdx = LCPHI->getBasicBlockIndex(FC1.Latch); assert(L1LatchBBIdx >= 0 && @@ -1961,8 +1963,9 @@ Value *LCV = LCPHI->getIncomingValue(L1LatchBBIdx); - PHINode *L1HeaderPHI = PHINode::Create( - LCV->getType(), 2, LCPHI->getName() + ".afterFC0", L1HeaderIP); + PHINode *L1HeaderPHI = + PHINode::Create(LCV->getType(), 2, LCPHI->getName() + ".afterFC0"); + L1HeaderPHI->insertBefore(L1HeaderIP); L1HeaderPHI->addIncoming(LCV, FC0.Latch); L1HeaderPHI->addIncoming(UndefValue::get(LCV->getType()), FC0.ExitingBlock); diff --git a/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp b/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp --- a/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp +++ b/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp @@ -2026,7 +2026,8 @@ auto *LbBr = cast(Body->getTerminator()); ICmpInst *LbCond = cast(LbBr->getCondition()); - PHINode *TcPhi = PHINode::Create(CountTy, 2, "tcphi", &Body->front()); + PHINode *TcPhi = PHINode::Create(CountTy, 2, "tcphi"); + TcPhi->insertBefore(Body->begin()); Builder.SetInsertPoint(LbCond); Instruction *TcDec = cast(Builder.CreateSub( @@ -2132,7 +2133,8 @@ ICmpInst *LbCond = cast(LbBr->getCondition()); Type *Ty = TripCnt->getType(); - PHINode *TcPhi = PHINode::Create(Ty, 2, "tcphi", &Body->front()); + PHINode *TcPhi = PHINode::Create(Ty, 2, "tcphi"); + TcPhi->insertBefore(Body->begin()); Builder.SetInsertPoint(LbCond); Instruction *TcDec = cast( diff --git a/llvm/lib/Transforms/Scalar/LoopLoadElimination.cpp b/llvm/lib/Transforms/Scalar/LoopLoadElimination.cpp --- a/llvm/lib/Transforms/Scalar/LoopLoadElimination.cpp +++ b/llvm/lib/Transforms/Scalar/LoopLoadElimination.cpp @@ -443,8 +443,8 @@ Cand.Load->getType(), InitialPtr, "load_initial", /* isVolatile */ false, Cand.Load->getAlign(), PH->getTerminator()); - PHINode *PHI = PHINode::Create(Initial->getType(), 2, "store_forwarded", - &L->getHeader()->front()); + PHINode *PHI = PHINode::Create(Initial->getType(), 2, "store_forwarded"); + PHI->insertBefore(L->getHeader()->begin()); PHI->addIncoming(Initial, PH); Type *LoadType = Initial->getType(); diff --git a/llvm/lib/Transforms/Scalar/MergedLoadStoreMotion.cpp b/llvm/lib/Transforms/Scalar/MergedLoadStoreMotion.cpp --- a/llvm/lib/Transforms/Scalar/MergedLoadStoreMotion.cpp +++ b/llvm/lib/Transforms/Scalar/MergedLoadStoreMotion.cpp @@ -217,8 +217,8 @@ if (Opd1 == Opd2) return nullptr; - auto *NewPN = PHINode::Create(Opd1->getType(), 2, Opd2->getName() + ".sink", - &BB->front()); + auto *NewPN = PHINode::Create(Opd1->getType(), 2, Opd2->getName() + ".sink"); + NewPN->insertBefore(BB->begin()); NewPN->applyMergedLocation(S0->getDebugLoc(), S1->getDebugLoc()); NewPN->addIncoming(Opd1, S0->getParent()); NewPN->addIncoming(Opd2, S1->getParent()); @@ -269,7 +269,7 @@ // Create the new store to be inserted at the join point. StoreInst *SNew = cast(S0->clone()); - SNew->insertBefore(&*InsertPt); + SNew->insertBefore(InsertPt); // New PHI operand? Use it. if (PHINode *NewPN = getPHIOperand(BB, S0, S1)) SNew->setOperand(0, NewPN); diff --git a/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp b/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp --- a/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp +++ b/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp @@ -368,10 +368,11 @@ bool FullUnswitch) { assert(&ExitBB != &UnswitchedBB && "Must have different loop exit and unswitched blocks!"); - Instruction *InsertPt = &*UnswitchedBB.begin(); + BasicBlock::iterator InsertPt = UnswitchedBB.begin(); for (PHINode &PN : ExitBB.phis()) { auto *NewPN = PHINode::Create(PN.getType(), /*NumReservedValues*/ 2, - PN.getName() + ".split", InsertPt); + PN.getName() + ".split"); + NewPN->insertBefore(InsertPt); // Walk backwards over the old PHI node's inputs to minimize the cost of // removing each one. We have to do this weird loop manually so that we @@ -623,7 +624,7 @@ // If fully unswitching, we can use the existing branch instruction. // Splice it into the old PH to gate reaching the new preheader and re-point // its successors. - OldPH->splice(OldPH->end(), BI.getParent(), BI.getIterator()); + BI.moveBefore(*OldPH, OldPH->end()); BI.setCondition(Cond); if (MSSAU) { // Temporarily clone the terminator, to make MSSA update cheaper by @@ -1246,8 +1247,8 @@ SE->forgetValue(&I); auto *MergePN = - PHINode::Create(I.getType(), /*NumReservedValues*/ 2, ".us-phi", - &*MergeBB->getFirstInsertionPt()); + PHINode::Create(I.getType(), /*NumReservedValues*/ 2, ".us-phi"); + MergePN->insertBefore(MergeBB->getFirstInsertionPt()); I.replaceAllUsesWith(MergePN); MergePN->addIncoming(&I, ExitBB); MergePN->addIncoming(&ClonedI, ClonedExitBB); @@ -2295,7 +2296,7 @@ if (FullUnswitch) { // Splice the terminator from the original loop and rewrite its // successors. - SplitBB->splice(SplitBB->end(), ParentBB, TI.getIterator()); + TI.moveBefore(*SplitBB, SplitBB->end()); // Keep a clone of the terminator for MSSA updates. Instruction *NewTI = TI.clone(); diff --git a/llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp b/llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp --- a/llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp +++ b/llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp @@ -518,10 +518,10 @@ // block, insert a PHI node for each argument of the function. // For now, we initialize each PHI to only have the real arguments // which are passed in. - Instruction *InsertPos = &HeaderBB->front(); + BasicBlock::iterator InsertPos = HeaderBB->begin(); for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) { - PHINode *PN = - PHINode::Create(I->getType(), 2, I->getName() + ".tr", InsertPos); + PHINode *PN = PHINode::Create(I->getType(), 2, I->getName() + ".tr"); + PN->insertBefore(InsertPos); I->replaceAllUsesWith(PN); // Everyone use the PHI node now! PN->addIncoming(&*I, NewEntry); ArgumentPHIs.push_back(PN); @@ -534,8 +534,10 @@ Type *RetType = F.getReturnType(); if (!RetType->isVoidTy()) { Type *BoolType = Type::getInt1Ty(F.getContext()); - RetPN = PHINode::Create(RetType, 2, "ret.tr", InsertPos); - RetKnownPN = PHINode::Create(BoolType, 2, "ret.known.tr", InsertPos); + RetPN = PHINode::Create(RetType, 2, "ret.tr"); + RetPN->insertBefore(InsertPos); + RetKnownPN = PHINode::Create(BoolType, 2, "ret.known.tr"); + RetKnownPN->insertBefore(InsertPos); RetPN->addIncoming(PoisonValue::get(RetType), NewEntry); RetKnownPN->addIncoming(ConstantInt::getFalse(BoolType), NewEntry); @@ -555,7 +557,8 @@ // Start by inserting a new PHI node for the accumulator. pred_iterator PB = pred_begin(HeaderBB), PE = pred_end(HeaderBB); AccPN = PHINode::Create(F.getReturnType(), std::distance(PB, PE) + 1, - "accumulator.tr", &HeaderBB->front()); + "accumulator.tr"); + AccPN->insertBefore(HeaderBB->begin()); // Loop over all of the predecessors of the tail recursion block. For the // real entry into the function we seed the PHI with the identity constant for diff --git a/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp b/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp --- a/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp +++ b/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp @@ -852,9 +852,11 @@ continue; // Otherwise a new PHI is needed. Create one and populate it. - PHINode *NewPN = PHINode::Create( - PN.getType(), Preds.size(), "split", - SplitBB->isLandingPad() ? &SplitBB->front() : SplitBB->getTerminator()); + PHINode *NewPN = PHINode::Create(PN.getType(), Preds.size(), "split"); + BasicBlock::iterator InsertPos = + SplitBB->isLandingPad() ? SplitBB->begin() + : SplitBB->getTerminator()->getIterator(); + NewPN->insertBefore(InsertPos); for (BasicBlock *BB : Preds) NewPN->addIncoming(V, BB); diff --git a/llvm/lib/Transforms/Utils/BreakCriticalEdges.cpp b/llvm/lib/Transforms/Utils/BreakCriticalEdges.cpp --- a/llvm/lib/Transforms/Utils/BreakCriticalEdges.cpp +++ b/llvm/lib/Transforms/Utils/BreakCriticalEdges.cpp @@ -449,8 +449,8 @@ // Create a PHI in the body block, to merge the direct and indirect // predecessors. - PHINode *MergePHI = - PHINode::Create(IndPHI->getType(), 2, "merge", &*MergeInsert); + PHINode *MergePHI = PHINode::Create(IndPHI->getType(), 2, "merge"); + MergePHI->insertBefore(MergeInsert); MergePHI->addIncoming(NewIndPHI, Target); MergePHI->addIncoming(DirPHI, DirectSucc); diff --git a/llvm/lib/Transforms/Utils/CodeExtractor.cpp b/llvm/lib/Transforms/Utils/CodeExtractor.cpp --- a/llvm/lib/Transforms/Utils/CodeExtractor.cpp +++ b/llvm/lib/Transforms/Utils/CodeExtractor.cpp @@ -721,7 +721,8 @@ // Create a new PHI node in the new region, which has an incoming value // from OldPred of PN. PHINode *NewPN = PHINode::Create(PN->getType(), 1 + NumPredsFromRegion, - PN->getName() + ".ce", &NewBB->front()); + PN->getName() + ".ce"); + NewPN->insertBefore(NewBB->begin()); PN->replaceAllUsesWith(NewPN); NewPN->addIncoming(PN, OldPred); @@ -775,9 +776,9 @@ } // Split this PHI. - PHINode *NewPN = - PHINode::Create(PN.getType(), IncomingVals.size(), - PN.getName() + ".ce", NewBB->getFirstNonPHI()); + PHINode *NewPN = PHINode::Create(PN.getType(), IncomingVals.size(), + PN.getName() + ".ce"); + NewPN->insertBefore(NewBB->getFirstNonPHIIt()); for (unsigned i : IncomingVals) NewPN->addIncoming(PN.getIncomingValue(i), PN.getIncomingBlock(i)); for (unsigned i : reverse(IncomingVals)) diff --git a/llvm/lib/Transforms/Utils/InlineFunction.cpp b/llvm/lib/Transforms/Utils/InlineFunction.cpp --- a/llvm/lib/Transforms/Utils/InlineFunction.cpp +++ b/llvm/lib/Transforms/Utils/InlineFunction.cpp @@ -190,20 +190,21 @@ const unsigned PHICapacity = 2; // Create corresponding new PHIs for all the PHIs in the outer landing pad. - Instruction *InsertPoint = &InnerResumeDest->front(); + BasicBlock::iterator InsertPoint = InnerResumeDest->begin(); BasicBlock::iterator I = OuterResumeDest->begin(); for (unsigned i = 0, e = UnwindDestPHIValues.size(); i != e; ++i, ++I) { PHINode *OuterPHI = cast(I); PHINode *InnerPHI = PHINode::Create(OuterPHI->getType(), PHICapacity, - OuterPHI->getName() + ".lpad-body", - InsertPoint); + OuterPHI->getName() + ".lpad-body"); + InnerPHI->insertBefore(InsertPoint); OuterPHI->replaceAllUsesWith(InnerPHI); InnerPHI->addIncoming(OuterPHI, OuterResumeDest); } // Create a PHI for the exception values. - InnerEHValuesPHI = PHINode::Create(CallerLPad->getType(), PHICapacity, - "eh.lpad-body", InsertPoint); + InnerEHValuesPHI = + PHINode::Create(CallerLPad->getType(), PHICapacity, "eh.lpad-body"); + InnerEHValuesPHI->insertBefore(InsertPoint); CallerLPad->replaceAllUsesWith(InnerEHValuesPHI); InnerEHValuesPHI->addIncoming(CallerLPad, OuterResumeDest); @@ -1514,10 +1515,10 @@ if (ByValAlignment) Alignment = std::max(Alignment, *ByValAlignment); - Value *NewAlloca = - new AllocaInst(ByValType, DL.getAllocaAddrSpace(), nullptr, Alignment, - Arg->getName(), &*Caller->begin()->begin()); - IFI.StaticAllocas.push_back(cast(NewAlloca)); + AllocaInst *NewAlloca = new AllocaInst(ByValType, DL.getAllocaAddrSpace(), + nullptr, Alignment, Arg->getName()); + NewAlloca->insertBefore(Caller->begin()->begin()); + IFI.StaticAllocas.push_back(NewAlloca); // Uses of the argument in the function should use our new alloca // instead. @@ -2767,8 +2768,8 @@ // The PHI node should go at the front of the new basic block to merge all // possible incoming values. if (!CB.use_empty()) { - PHI = PHINode::Create(RTy, Returns.size(), CB.getName(), - &AfterCallBB->front()); + PHI = PHINode::Create(RTy, Returns.size(), CB.getName()); + PHI->insertBefore(AfterCallBB->begin()); // Anything that used the result of the function call should now use the // PHI node as their operand. CB.replaceAllUsesWith(PHI); diff --git a/llvm/lib/Transforms/Utils/LoopUnrollRuntime.cpp b/llvm/lib/Transforms/Utils/LoopUnrollRuntime.cpp --- a/llvm/lib/Transforms/Utils/LoopUnrollRuntime.cpp +++ b/llvm/lib/Transforms/Utils/LoopUnrollRuntime.cpp @@ -105,8 +105,8 @@ // PrologLatch. When supporting multiple-exiting block loops, we can have // two or more blocks that have the LatchExit as the target in the // original loop. - PHINode *NewPN = PHINode::Create(PN.getType(), 2, PN.getName() + ".unr", - PrologExit->getFirstNonPHI()); + PHINode *NewPN = PHINode::Create(PN.getType(), 2, PN.getName() + ".unr"); + NewPN->insertBefore(PrologExit->getFirstNonPHIIt()); // Adding a value to the new PHI node from the original loop preheader. // This is the value that skips all the prolog code. if (L->contains(&PN)) { @@ -269,8 +269,8 @@ for (PHINode &PN : Succ->phis()) { // Add new PHI nodes to the loop exit block and update epilog // PHIs with the new PHI values. - PHINode *NewPN = PHINode::Create(PN.getType(), 2, PN.getName() + ".unr", - NewExit->getFirstNonPHI()); + PHINode *NewPN = PHINode::Create(PN.getType(), 2, PN.getName() + ".unr"); + NewPN->insertBefore(NewExit->getFirstNonPHIIt()); // Adding a value to the new PHI node from the unrolling loop preheader. NewPN->addIncoming(PN.getIncomingValueForBlock(NewPreHeader), PreHeader); // Adding a value to the new PHI node from the unrolling loop latch. @@ -363,12 +363,13 @@ BasicBlock *FirstLoopBB = cast(VMap[Header]); BranchInst *LatchBR = cast(NewBB->getTerminator()); IRBuilder<> Builder(LatchBR); - PHINode *NewIdx = PHINode::Create(NewIter->getType(), 2, - suffix + ".iter", - FirstLoopBB->getFirstNonPHI()); + PHINode *NewIdx = + PHINode::Create(NewIter->getType(), 2, suffix + ".iter"); + NewIdx->insertBefore(FirstLoopBB->getFirstNonPHIIt()); auto *Zero = ConstantInt::get(NewIdx->getType(), 0); auto *One = ConstantInt::get(NewIdx->getType(), 1); - Value *IdxNext = Builder.CreateAdd(NewIdx, One, NewIdx->getName() + ".next"); + Value *IdxNext = + Builder.CreateAdd(NewIdx, One, NewIdx->getName() + ".next"); Value *IdxCmp = Builder.CreateICmpNE(IdxNext, NewIter, NewIdx->getName() + ".cmp"); Builder.CreateCondBr(IdxCmp, FirstLoopBB, InsertBot); NewIdx->addIncoming(Zero, InsertTop); @@ -912,8 +913,8 @@ IRBuilder<> B2(NewPreHeader->getTerminator()); Value *TestVal = B2.CreateSub(TripCount, ModVal, "unroll_iter"); BranchInst *LatchBR = cast(Latch->getTerminator()); - PHINode *NewIdx = PHINode::Create(TestVal->getType(), 2, "niter", - Header->getFirstNonPHI()); + PHINode *NewIdx = PHINode::Create(TestVal->getType(), 2, "niter"); + NewIdx->insertBefore(Header->getFirstNonPHIIt()); B2.SetInsertPoint(LatchBR); auto *Zero = ConstantInt::get(NewIdx->getType(), 0); auto *One = ConstantInt::get(NewIdx->getType(), 1); diff --git a/llvm/lib/Transforms/Utils/LoopVersioning.cpp b/llvm/lib/Transforms/Utils/LoopVersioning.cpp --- a/llvm/lib/Transforms/Utils/LoopVersioning.cpp +++ b/llvm/lib/Transforms/Utils/LoopVersioning.cpp @@ -145,8 +145,8 @@ } // If not create it. if (!PN) { - PN = PHINode::Create(Inst->getType(), 2, Inst->getName() + ".lver", - &PHIBlock->front()); + PN = PHINode::Create(Inst->getType(), 2, Inst->getName() + ".lver"); + PN->insertBefore(PHIBlock->begin()); SmallVector UsersToUpdate; for (User *U : Inst->users()) if (!VersionedLoop->contains(cast(U)->getParent())) diff --git a/llvm/lib/Transforms/Utils/MoveAutoInit.cpp b/llvm/lib/Transforms/Utils/MoveAutoInit.cpp --- a/llvm/lib/Transforms/Utils/MoveAutoInit.cpp +++ b/llvm/lib/Transforms/Utils/MoveAutoInit.cpp @@ -202,7 +202,7 @@ // if two instructions are moved from the same BB to the same BB, we insert // the second one in the front, then the first on top of it. for (auto &Job : reverse(JobList)) { - Job.first->moveBefore(&*Job.second->getFirstInsertionPt()); + Job.first->moveBefore(*Job.second, Job.second->getFirstInsertionPt()); MSSAU.moveToPlace(MSSA.getMemoryAccess(Job.first), Job.first->getParent(), MemorySSA::InsertionPlace::Beginning); } diff --git a/llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp b/llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp --- a/llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp +++ b/llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp @@ -981,8 +981,8 @@ // Create a PhiNode using the dereferenced type... and add the phi-node to the // BasicBlock. PN = PHINode::Create(Allocas[AllocaNo]->getAllocatedType(), getNumPreds(BB), - Allocas[AllocaNo]->getName() + "." + Twine(Version++), - &BB->front()); + Allocas[AllocaNo]->getName() + "." + Twine(Version++)); + PN->insertBefore(BB->begin()); ++NumPHIInsert; PhiToAllocaMap[PN] = AllocaNo; return true; diff --git a/llvm/lib/Transforms/Utils/SSAUpdater.cpp b/llvm/lib/Transforms/Utils/SSAUpdater.cpp --- a/llvm/lib/Transforms/Utils/SSAUpdater.cpp +++ b/llvm/lib/Transforms/Utils/SSAUpdater.cpp @@ -156,8 +156,9 @@ } // Ok, we have no way out, insert a new one now. - PHINode *InsertedPHI = PHINode::Create(ProtoType, PredValues.size(), - ProtoName, &BB->front()); + PHINode *InsertedPHI = + PHINode::Create(ProtoType, PredValues.size(), ProtoName); + InsertedPHI->insertBefore(BB->begin()); // Fill in all the predecessors of the PHI. for (const auto &PredValue : PredValues) @@ -295,8 +296,9 @@ /// Reserve space for the operands but do not fill them in yet. static Value *CreateEmptyPHI(BasicBlock *BB, unsigned NumPreds, SSAUpdater *Updater) { - PHINode *PHI = PHINode::Create(Updater->ProtoType, NumPreds, - Updater->ProtoName, &BB->front()); + PHINode *PHI = + PHINode::Create(Updater->ProtoType, NumPreds, Updater->ProtoName); + PHI->insertBefore(BB->begin()); return PHI; } diff --git a/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp b/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp --- a/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp +++ b/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp @@ -1291,8 +1291,8 @@ // specified loop. BasicBlock *Header = L->getHeader(); pred_iterator HPB = pred_begin(Header), HPE = pred_end(Header); - CanonicalIV = PHINode::Create(Ty, std::distance(HPB, HPE), "indvar", - &Header->front()); + CanonicalIV = PHINode::Create(Ty, std::distance(HPB, HPE), "indvar"); + CanonicalIV->insertBefore(Header->begin()); rememberInstruction(CanonicalIV); SmallSet PredSeen; diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp --- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp @@ -1962,8 +1962,9 @@ // Create a new PHI in the successor block and populate it. auto *Op = I0->getOperand(O); assert(!Op->getType()->isTokenTy() && "Can't PHI tokens!"); - auto *PN = PHINode::Create(Op->getType(), Insts.size(), - Op->getName() + ".sink", &BBEnd->front()); + auto *PN = + PHINode::Create(Op->getType(), Insts.size(), Op->getName() + ".sink"); + PN->insertBefore(BBEnd->begin()); for (auto *I : Insts) PN->addIncoming(I->getOperand(O), I->getParent()); NewOperands.push_back(PN); @@ -1973,7 +1974,8 @@ // and move it to the start of the successor block. for (unsigned O = 0, E = I0->getNumOperands(); O != E; ++O) I0->getOperandUse(O).set(NewOperands[O]); - I0->moveBefore(&*BBEnd->getFirstInsertionPt()); + + I0->moveBefore(*BBEnd, BBEnd->getFirstInsertionPt()); // Update metadata and IR flags, and merge debug locations. for (auto *I : Insts) @@ -3877,7 +3879,8 @@ (!isa(V) || cast(V)->getParent() != BB)) return V; - PHI = PHINode::Create(V->getType(), 2, "simplifycfg.merge", &Succ->front()); + PHI = PHINode::Create(V->getType(), 2, "simplifycfg.merge"); + PHI->insertBefore(Succ->begin()); PHI->addIncoming(V, BB); for (BasicBlock *PredBB : predecessors(Succ)) if (PredBB != BB) diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp --- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp +++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp @@ -7912,8 +7912,8 @@ // Generate a resume induction for the vector epilogue and put it in the // vector epilogue preheader Type *IdxTy = Legal->getWidestInductionType(); - PHINode *EPResumeVal = PHINode::Create(IdxTy, 2, "vec.epilog.resume.val", - LoopVectorPreHeader->getFirstNonPHI()); + PHINode *EPResumeVal = PHINode::Create(IdxTy, 2, "vec.epilog.resume.val"); + EPResumeVal->insertBefore(LoopVectorPreHeader->getFirstNonPHIIt()); EPResumeVal->addIncoming(EPI.VectorTripCount, VecEpilogueIterationCountCheck); EPResumeVal->addIncoming(ConstantInt::get(IdxTy, 0), EPI.MainLoopIterationCountCheck); diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp --- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp +++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp @@ -11082,7 +11082,8 @@ Instruction *I = EEIt->second; if (Builder.GetInsertPoint() != Builder.GetInsertBlock()->end() && Builder.GetInsertPoint()->comesBefore(I)) - I->moveBefore(&*Builder.GetInsertPoint()); + I->moveBefore(*Builder.GetInsertPoint()->getParent(), + Builder.GetInsertPoint()); Ex = I; } } diff --git a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp --- a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp +++ b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp @@ -919,8 +919,8 @@ // We may need to add the step a number of times, depending on the unroll // factor. The last of those goes into the PHI. - PHINode *VecInd = PHINode::Create(SteppedStart->getType(), 2, "vec.ind", - &*State.CFG.PrevBB->getFirstInsertionPt()); + PHINode *VecInd = PHINode::Create(SteppedStart->getType(), 2, "vec.ind"); + VecInd->insertBefore(State.CFG.PrevBB->getFirstInsertionPt()); VecInd->setDebugLoc(EntryVal->getDebugLoc()); Instruction *LastInduction = VecInd; for (unsigned Part = 0; Part < State.UF; ++Part) { @@ -1402,8 +1402,8 @@ void VPCanonicalIVPHIRecipe::execute(VPTransformState &State) { Value *Start = getStartValue()->getLiveInIRValue(); - PHINode *EntryPart = PHINode::Create( - Start->getType(), 2, "index", &*State.CFG.PrevBB->getFirstInsertionPt()); + PHINode *EntryPart = PHINode::Create(Start->getType(), 2, "index"); + EntryPart->insertBefore(State.CFG.PrevBB->getFirstInsertionPt()); BasicBlock *VectorPH = State.CFG.getPreheaderBBFor(this); EntryPart->addIncoming(Start, VectorPH); @@ -1530,8 +1530,8 @@ } // Create a phi node for the new recurrence. - PHINode *EntryPart = PHINode::Create( - VecTy, 2, "vector.recur", &*State.CFG.PrevBB->getFirstInsertionPt()); + PHINode *EntryPart = PHINode::Create(VecTy, 2, "vector.recur"); + EntryPart->insertBefore(State.CFG.PrevBB->getFirstInsertionPt()); EntryPart->addIncoming(VectorInit, VectorPH); State.set(this, EntryPart, 0); } @@ -1563,8 +1563,8 @@ "recipe must be in the vector loop header"); unsigned LastPartForNewPhi = isOrdered() ? 1 : State.UF; for (unsigned Part = 0; Part < LastPartForNewPhi; ++Part) { - Value *EntryPart = - PHINode::Create(VecTy, 2, "vec.phi", &*HeaderBB->getFirstInsertionPt()); + Instruction *EntryPart = PHINode::Create(VecTy, 2, "vec.phi"); + EntryPart->insertBefore(HeaderBB->getFirstInsertionPt()); State.set(this, EntryPart, Part); }