Index: llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h =================================================================== --- llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h +++ 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. @@ -419,12 +436,22 @@ /// Updates DT and LI if given. /// /// FIXME: deprecated, switch to the DomTreeUpdater-based one. -Instruction *SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore, +Instruction *SplitBlockAndInsertIfThen(Value *Cond, BasicBlock::iterator SplitBefore, bool Unreachable, MDNode *BranchWeights, DominatorTree *DT, LoopInfo *LI = nullptr, BasicBlock *ThenBlock = nullptr); +inline Instruction *SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore, + bool Unreachable, MDNode *BranchWeights, + DominatorTree *DT, + LoopInfo *LI = nullptr, + BasicBlock *ThenBlock = nullptr) { + return SplitBlockAndInsertIfThen(Cond, SplitBefore->getIterator(), Unreachable, BranchWeights, DT, LI, ThenBlock); +} + + + /// 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 @@ -446,13 +473,22 @@ /// Returns the NewBasicBlock's terminator. /// /// Updates DT 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); +} + /// SplitBlockAndInsertIfThenElse is similar to SplitBlockAndInsertIfThen, /// but also creates the ElseBlock. /// Before: Index: llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp =================================================================== --- llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp +++ llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp @@ -608,7 +608,7 @@ UnswitchedBB = LoopExitBB; } else { UnswitchedBB = - SplitBlock(LoopExitBB, &LoopExitBB->front(), &DT, &LI, MSSAU); + SplitBlock(LoopExitBB, LoopExitBB->begin(), &DT, &LI, MSSAU, "", false); } if (MSSAU && VerifyMemorySSA) @@ -881,7 +881,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); @@ -908,7 +908,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); @@ -1209,7 +1209,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 Index: llvm/lib/Transforms/Utils/BasicBlockUtils.cpp =================================================================== --- llvm/lib/Transforms/Utils/BasicBlockUtils.cpp +++ 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(); @@ -1474,13 +1474,13 @@ } static Instruction * -SplitBlockAndInsertIfThenImpl(Value *Cond, Instruction *SplitBefore, +SplitBlockAndInsertIfThenImpl(Value *Cond, BasicBlock::iterator SplitBefore, bool Unreachable, MDNode *BranchWeights, DomTreeUpdater *DTU, DominatorTree *DT, LoopInfo *LI, BasicBlock *ThenBlock) { SmallVector Updates; BasicBlock *Head = SplitBefore->getParent(); - BasicBlock *Tail = Head->splitBasicBlock(SplitBefore->getIterator()); + BasicBlock *Tail = Head->splitBasicBlock(SplitBefore, "", false); DebugLoc DbgLoc = SplitBefore->getStableDebugLoc(); if (DTU) { SmallPtrSet UniqueSuccessorsOfHead; @@ -1544,7 +1544,7 @@ } Instruction *llvm::SplitBlockAndInsertIfThen(Value *Cond, - Instruction *SplitBefore, + BasicBlock::iterator SplitBefore, bool Unreachable, MDNode *BranchWeights, DominatorTree *DT, LoopInfo *LI, @@ -1554,7 +1554,7 @@ /*DTU=*/nullptr, DT, LI, ThenBlock); } Instruction *llvm::SplitBlockAndInsertIfThen(Value *Cond, - Instruction *SplitBefore, + BasicBlock::iterator SplitBefore, bool Unreachable, MDNode *BranchWeights, DomTreeUpdater *DTU, LoopInfo *LI, Index: llvm/lib/Transforms/Utils/SimplifyCFG.cpp =================================================================== --- llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ llvm/lib/Transforms/Utils/SimplifyCFG.cpp @@ -4002,9 +4002,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()));