diff options
Diffstat (limited to 'llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp')
-rw-r--r-- | llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp | 33 |
1 files changed, 22 insertions, 11 deletions
diff --git a/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp b/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp index fdb3211..6a3b0e1 100644 --- a/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp +++ b/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp @@ -1245,9 +1245,12 @@ static BasicBlock *buildClonedLoopBlocks( if (SE && isa<PHINode>(I)) SE->forgetValue(&I); + BasicBlock::iterator InsertPt = MergeBB->getFirstInsertionPt(); + auto *MergePN = PHINode::Create(I.getType(), /*NumReservedValues*/ 2, ".us-phi"); - MergePN->insertBefore(MergeBB->getFirstInsertionPt()); + MergePN->insertBefore(InsertPt); + MergePN->setDebugLoc(InsertPt->getDebugLoc()); I.replaceAllUsesWith(MergePN); MergePN->addIncoming(&I, ExitBB); MergePN->addIncoming(&ClonedI, ClonedExitBB); @@ -1306,8 +1309,9 @@ static BasicBlock *buildClonedLoopBlocks( else if (auto *SI = dyn_cast<SwitchInst>(ClonedTerminator)) ClonedConditionToErase = SI->getCondition(); + Instruction *BI = BranchInst::Create(ClonedSuccBB, ClonedParentBB); + BI->setDebugLoc(ClonedTerminator->getDebugLoc()); ClonedTerminator->eraseFromParent(); - BranchInst::Create(ClonedSuccBB, ClonedParentBB); if (ClonedConditionToErase) RecursivelyDeleteTriviallyDeadInstructions(ClonedConditionToErase, nullptr, @@ -2334,22 +2338,27 @@ static void unswitchNontrivialInvariants( // nuke the initial terminator placed in the split block. SplitBB->getTerminator()->eraseFromParent(); if (FullUnswitch) { - // Splice the terminator from the original loop and rewrite its - // successors. - TI.moveBefore(*SplitBB, SplitBB->end()); - // Keep a clone of the terminator for MSSA updates. Instruction *NewTI = TI.clone(); NewTI->insertInto(ParentBB, ParentBB->end()); + // Splice the terminator from the original loop and rewrite its + // successors. + TI.moveBefore(*SplitBB, SplitBB->end()); + TI.dropLocation(); + // First wire up the moved terminator to the preheaders. if (BI) { BasicBlock *ClonedPH = ClonedPHs.begin()->second; BI->setSuccessor(ClonedSucc, ClonedPH); BI->setSuccessor(1 - ClonedSucc, LoopPH); Value *Cond = skipTrivialSelect(BI->getCondition()); - if (InsertFreeze) + if (InsertFreeze) { + // We don't give any debug location to the new freeze, because the + // BI (`dyn_cast<BranchInst>(TI)`) is an in-loop instruction hoisted + // out of the loop. Cond = new FreezeInst(Cond, Cond->getName() + ".fr", BI->getIterator()); + } BI->setCondition(Cond); DTUpdates.push_back({DominatorTree::Insert, SplitBB, ClonedPH}); } else { @@ -2432,12 +2441,13 @@ static void unswitchNontrivialInvariants( DTUpdates.push_back({DominatorTree::Delete, ParentBB, SuccBB}); } - // After MSSAU update, remove the cloned terminator instruction NewTI. - ParentBB->getTerminator()->eraseFromParent(); - // Create a new unconditional branch to the continuing block (as opposed to // the one cloned). - BranchInst::Create(RetainedSuccBB, ParentBB); + Instruction *NewBI = BranchInst::Create(RetainedSuccBB, ParentBB); + NewBI->setDebugLoc(NewTI->getDebugLoc()); + + // After MSSAU update, remove the cloned terminator instruction NewTI. + NewTI->eraseFromParent(); } else { assert(BI && "Only branches have partial unswitching."); assert(UnswitchedSuccBBs.size() == 1 && @@ -2710,6 +2720,7 @@ static BranchInst *turnSelectIntoBranch(SelectInst *SI, DominatorTree &DT, PHINode::Create(SI->getType(), 2, "unswitched.select", SI->getIterator()); Phi->addIncoming(SI->getTrueValue(), ThenBB); Phi->addIncoming(SI->getFalseValue(), HeadBB); + Phi->setDebugLoc(SI->getDebugLoc()); SI->replaceAllUsesWith(Phi); SI->eraseFromParent(); |