diff --git a/llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h b/llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h --- a/llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h +++ b/llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h @@ -280,10 +280,16 @@ /// branch. The new block with name \p BBName is returned. /// /// FIXME: deprecated, switch to the DomTreeUpdater-based one. -BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt, DominatorTree *DT, +BasicBlock *SplitBlock(BasicBlock *Old, BasicBlock::iterator SplitPt, DominatorTree *DT, LoopInfo *LI = nullptr, MemorySSAUpdater *MSSAU = nullptr, const Twine &BBName = "", bool Before = false); +inline BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt, DominatorTree *DT, + LoopInfo *LI = nullptr, + MemorySSAUpdater *MSSAU = nullptr, + const Twine &BBName = "", bool Before = false) { + return SplitBlock(Old, SplitPt->getIterator(), DT, LI, MSSAU, BBName, Before); +} /// Split the specified block at the specified instruction. /// @@ -293,19 +299,30 @@ /// Everything before \p SplitPt stays in \p Old and everything starting with \p /// SplitPt moves to a new block. The two blocks are joined by an unconditional /// branch. The new block with name \p BBName is returned. -BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt, +BasicBlock *SplitBlock(BasicBlock *Old, BasicBlock::iterator SplitPt, DomTreeUpdater *DTU = nullptr, LoopInfo *LI = nullptr, MemorySSAUpdater *MSSAU = nullptr, const Twine &BBName = "", bool Before = false); +inline BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt, + DomTreeUpdater *DTU = nullptr, LoopInfo *LI = nullptr, + MemorySSAUpdater *MSSAU = nullptr, + const Twine &BBName = "", bool Before = false) { + return SplitBlock(Old, SplitPt->getIterator(), DTU, LI, MSSAU, BBName, Before); +} /// Split the specified block at the specified instruction \p SplitPt. /// All instructions before \p SplitPt are moved to a new block and all /// instructions after \p SplitPt stay in the old block. The new block and the /// old block are joined by inserting an unconditional branch to the end of the /// new block. The new block with name \p BBName is returned. -BasicBlock *splitBlockBefore(BasicBlock *Old, Instruction *SplitPt, +BasicBlock *splitBlockBefore(BasicBlock *Old, BasicBlock::iterator SplitPt, DomTreeUpdater *DTU, LoopInfo *LI, MemorySSAUpdater *MSSAU, const Twine &BBName = ""); +inline BasicBlock *splitBlockBefore(BasicBlock *Old, Instruction *SplitPt, + DomTreeUpdater *DTU, LoopInfo *LI, + MemorySSAUpdater *MSSAU, const Twine &BBName = "") { + return splitBlockBefore(Old, SplitPt->getIterator(), DTU, LI, MSSAU, BBName); +} /// This method introduces at least one new basic block into the function and /// moves some of the predecessors of BB to be predecessors of the new block. @@ -417,22 +434,44 @@ /// Returns the NewBasicBlock's terminator. /// /// Updates DTU and LI if given. -Instruction *SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore, +Instruction *SplitBlockAndInsertIfThen(Value *Cond, BasicBlock::iterator SplitBefore, bool Unreachable, MDNode *BranchWeights = nullptr, DomTreeUpdater *DTU = nullptr, LoopInfo *LI = nullptr, BasicBlock *ThenBlock = nullptr); +inline Instruction *SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore, + bool Unreachable, + MDNode *BranchWeights = nullptr, + DomTreeUpdater *DTU = nullptr, + LoopInfo *LI = nullptr, + BasicBlock *ThenBlock = nullptr) { + return SplitBlockAndInsertIfThen(Cond, SplitBefore->getIterator(), + Unreachable, BranchWeights, DTU, LI, + ThenBlock); +} + /// Similar to SplitBlockAndInsertIfThen, but the inserted block is on the false /// path of the branch. -Instruction *SplitBlockAndInsertIfElse(Value *Cond, Instruction *SplitBefore, +Instruction *SplitBlockAndInsertIfElse(Value *Cond, BasicBlock::iterator SplitBefore, bool Unreachable, MDNode *BranchWeights = nullptr, DomTreeUpdater *DTU = nullptr, LoopInfo *LI = nullptr, BasicBlock *ElseBlock = nullptr); +inline Instruction *SplitBlockAndInsertIfElse(Value *Cond, Instruction *SplitBefore, + bool Unreachable, + MDNode *BranchWeights = nullptr, + DomTreeUpdater *DTU = nullptr, + LoopInfo *LI = nullptr, + BasicBlock *ElseBlock = nullptr) { + return SplitBlockAndInsertIfElse(Cond, SplitBefore->getIterator(), + Unreachable, BranchWeights, DTU, LI, + ElseBlock); +} + /// SplitBlockAndInsertIfThenElse is similar to SplitBlockAndInsertIfThen, /// but also creates the ElseBlock. /// Before: @@ -449,13 +488,25 @@ /// Tail /// /// Updates DT if given. -void SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore, +void SplitBlockAndInsertIfThenElse(Value *Cond, + BasicBlock::iterator SplitBefore, Instruction **ThenTerm, Instruction **ElseTerm, MDNode *BranchWeights = nullptr, DomTreeUpdater *DTU = nullptr, LoopInfo *LI = nullptr); +inline void SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore, + Instruction **ThenTerm, + Instruction **ElseTerm, + MDNode *BranchWeights = nullptr, + DomTreeUpdater *DTU = nullptr, + LoopInfo *LI = nullptr) +{ + SplitBlockAndInsertIfThenElse(Cond, SplitBefore->getIterator(), ThenTerm, + ElseTerm, BranchWeights, DTU, LI); +} + /// Split the containing block at the specified instruction - everything before /// SplitBefore stays in the old basic block, and the rest of the instructions /// in the BB are moved to a new block. The two blocks are connected by a @@ -483,7 +534,8 @@ /// caller must ensure that Tail is reachable from Head. /// Returns the newly created blocks in \p ThenBlock and \p ElseBlock. /// Updates DTU and LI if given. -void SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore, +void SplitBlockAndInsertIfThenElse(Value *Cond, + BasicBlock::iterator SplitBefore, BasicBlock **ThenBlock, BasicBlock **ElseBlock, bool UnreachableThen = false, @@ -492,6 +544,18 @@ DomTreeUpdater *DTU = nullptr, LoopInfo *LI = nullptr); +inline void SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore, + BasicBlock **ThenBlock, + BasicBlock **ElseBlock, + bool UnreachableThen = false, + bool UnreachableElse = false, + MDNode *BranchWeights = nullptr, + DomTreeUpdater *DTU = nullptr, + LoopInfo *LI = nullptr) { + SplitBlockAndInsertIfThenElse(Cond, SplitBefore->getIterator(), ThenBlock, + ElseBlock, UnreachableThen, UnreachableElse, BranchWeights, DTU, LI); +} + /// Insert a for (int i = 0; i < End; i++) loop structure (with the exception /// that \p End is assumed > 0, and thus not checked on entry) at \p /// SplitBefore. Returns the first insert point in the loop body, and the 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 @@ -610,7 +610,7 @@ UnswitchedBB = LoopExitBB; } else { UnswitchedBB = - SplitBlock(LoopExitBB, &LoopExitBB->front(), &DT, &LI, MSSAU); + SplitBlock(LoopExitBB, LoopExitBB->begin(), &DT, &LI, MSSAU, "", false); } if (MSSAU && VerifyMemorySSA) @@ -883,7 +883,7 @@ rewritePHINodesForUnswitchedExitBlock(*DefaultExitBB, *ParentBB, *OldPH); } else { auto *SplitBB = - SplitBlock(DefaultExitBB, &DefaultExitBB->front(), &DT, &LI, MSSAU); + SplitBlock(DefaultExitBB, DefaultExitBB->begin(), &DT, &LI, MSSAU); rewritePHINodesForExitAndUnswitchedBlocks(*DefaultExitBB, *SplitBB, *ParentBB, *OldPH, /*FullUnswitch*/ true); @@ -910,7 +910,7 @@ BasicBlock *&SplitExitBB = SplitExitBBMap[ExitBB]; if (!SplitExitBB) { // If this is the first time we see this, do the split and remember it. - SplitExitBB = SplitBlock(ExitBB, &ExitBB->front(), &DT, &LI, MSSAU); + SplitExitBB = SplitBlock(ExitBB, ExitBB->begin(), &DT, &LI, MSSAU); rewritePHINodesForExitAndUnswitchedBlocks(*ExitBB, *SplitExitBB, *ParentBB, *OldPH, /*FullUnswitch*/ true); @@ -1211,7 +1211,7 @@ // place to merge the CFG, so split the exit first. This is always safe to // do because there cannot be any non-loop predecessors of a loop exit in // loop simplified form. - auto *MergeBB = SplitBlock(ExitBB, &ExitBB->front(), &DT, &LI, MSSAU); + auto *MergeBB = SplitBlock(ExitBB, ExitBB->begin(), &DT, &LI, MSSAU); // Rearrange the names to make it easier to write test cases by having the // exit block carry the suffix rather than the merge block carrying the 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 @@ -879,7 +879,7 @@ return NumBroken; } -static BasicBlock *SplitBlockImpl(BasicBlock *Old, Instruction *SplitPt, +static BasicBlock *SplitBlockImpl(BasicBlock *Old, BasicBlock::iterator SplitPt, DomTreeUpdater *DTU, DominatorTree *DT, LoopInfo *LI, MemorySSAUpdater *MSSAU, const Twine &BBName, bool Before) { @@ -889,7 +889,7 @@ DTU ? DTU : (DT ? &LocalDTU : nullptr), LI, MSSAU, BBName); } - BasicBlock::iterator SplitIt = SplitPt->getIterator(); + BasicBlock::iterator SplitIt = SplitPt; while (isa(SplitIt) || SplitIt->isEHPad()) { ++SplitIt; assert(SplitIt != SplitPt->getParent()->end()); @@ -935,14 +935,14 @@ return New; } -BasicBlock *llvm::SplitBlock(BasicBlock *Old, Instruction *SplitPt, +BasicBlock *llvm::SplitBlock(BasicBlock *Old, BasicBlock::iterator SplitPt, DominatorTree *DT, LoopInfo *LI, MemorySSAUpdater *MSSAU, const Twine &BBName, bool Before) { return SplitBlockImpl(Old, SplitPt, /*DTU=*/nullptr, DT, LI, MSSAU, BBName, Before); } -BasicBlock *llvm::SplitBlock(BasicBlock *Old, Instruction *SplitPt, +BasicBlock *llvm::SplitBlock(BasicBlock *Old, BasicBlock::iterator SplitPt, DomTreeUpdater *DTU, LoopInfo *LI, MemorySSAUpdater *MSSAU, const Twine &BBName, bool Before) { @@ -950,12 +950,12 @@ Before); } -BasicBlock *llvm::splitBlockBefore(BasicBlock *Old, Instruction *SplitPt, +BasicBlock *llvm::splitBlockBefore(BasicBlock *Old, BasicBlock::iterator SplitPt, DomTreeUpdater *DTU, LoopInfo *LI, MemorySSAUpdater *MSSAU, const Twine &BBName) { - BasicBlock::iterator SplitIt = SplitPt->getIterator(); + BasicBlock::iterator SplitIt = SplitPt; while (isa(SplitIt) || SplitIt->isEHPad()) ++SplitIt; std::string Name = BBName.str(); @@ -1471,7 +1471,7 @@ } Instruction *llvm::SplitBlockAndInsertIfThen(Value *Cond, - Instruction *SplitBefore, + BasicBlock::iterator SplitBefore, bool Unreachable, MDNode *BranchWeights, DomTreeUpdater *DTU, LoopInfo *LI, @@ -1484,7 +1484,7 @@ } Instruction *llvm::SplitBlockAndInsertIfElse(Value *Cond, - Instruction *SplitBefore, + BasicBlock::iterator SplitBefore, bool Unreachable, MDNode *BranchWeights, DomTreeUpdater *DTU, LoopInfo *LI, @@ -1496,7 +1496,7 @@ return ElseBlock->getTerminator(); } -void llvm::SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore, +void llvm::SplitBlockAndInsertIfThenElse(Value *Cond, BasicBlock::iterator SplitBefore, Instruction **ThenTerm, Instruction **ElseTerm, MDNode *BranchWeights, @@ -1512,7 +1512,7 @@ } void llvm::SplitBlockAndInsertIfThenElse( - Value *Cond, Instruction *SplitBefore, BasicBlock **ThenBlock, + Value *Cond, BasicBlock::iterator SplitBefore, BasicBlock **ThenBlock, BasicBlock **ElseBlock, bool UnreachableThen, bool UnreachableElse, MDNode *BranchWeights, DomTreeUpdater *DTU, LoopInfo *LI) { assert((ThenBlock || ElseBlock) && @@ -1529,7 +1529,7 @@ } LLVMContext &C = Head->getContext(); - BasicBlock *Tail = Head->splitBasicBlock(SplitBefore->getIterator()); + BasicBlock *Tail = Head->splitBasicBlock(SplitBefore); BasicBlock *TrueBlock = Tail; BasicBlock *FalseBlock = Tail; bool ThenToTailEdge = false; 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 @@ -4015,9 +4015,11 @@ QPred = QB.CreateNot(QPred); Value *CombinedPred = QB.CreateOr(PPred, QPred); - auto *T = SplitBlockAndInsertIfThen(CombinedPred, &*QB.GetInsertPoint(), + BasicBlock::iterator InsertPt = QB.GetInsertPoint(); + auto *T = SplitBlockAndInsertIfThen(CombinedPred, InsertPt, /*Unreachable=*/false, /*BranchWeights=*/nullptr, DTU); + QB.SetInsertPoint(T); StoreInst *SI = cast(QB.CreateStore(QPHI, Address)); SI->setAAMetadata(PStore->getAAMetadata().merge(QStore->getAAMetadata()));