From 73db5c137af2bceeb6f591fd8c7fe70675cbb75c Mon Sep 17 00:00:00 2001 From: Max Kazantsev Date: Fri, 15 Feb 2019 08:18:00 +0000 Subject: [PATCH] [NFC] Tweak SplitBlockAndInsertIfThen to use existing ThenBlock llvm-svn: 354107 --- .../llvm/Transforms/Utils/BasicBlockUtils.h | 6 ++++-- llvm/lib/Transforms/Utils/BasicBlockUtils.cpp | 24 ++++++++++++++-------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h b/llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h index 4e76328..e997ed0 100644 --- a/llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h +++ b/llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h @@ -270,7 +270,8 @@ ReturnInst *FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB, /// SplitBefore /// Tail /// -/// If Unreachable is true, then ThenBlock ends with +/// If \p ThenBlock is not specified, a new block will be created for it. +/// If \p Unreachable is true, the newly created block will end with /// UnreachableInst, otherwise it branches to Tail. /// Returns the NewBasicBlock's terminator. /// @@ -279,7 +280,8 @@ Instruction *SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore, bool Unreachable, MDNode *BranchWeights = nullptr, DominatorTree *DT = nullptr, - LoopInfo *LI = nullptr); + LoopInfo *LI = nullptr, + BasicBlock *ThenBlock = nullptr); /// SplitBlockAndInsertIfThenElse is similar to SplitBlockAndInsertIfThen, /// but also creates the ElseBlock. diff --git a/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp b/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp index 5ca9222..6f49eae 100644 --- a/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp +++ b/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp @@ -730,18 +730,23 @@ Instruction *llvm::SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore, bool Unreachable, MDNode *BranchWeights, - DominatorTree *DT, LoopInfo *LI) { + DominatorTree *DT, LoopInfo *LI, + BasicBlock *ThenBlock) { BasicBlock *Head = SplitBefore->getParent(); BasicBlock *Tail = Head->splitBasicBlock(SplitBefore->getIterator()); Instruction *HeadOldTerm = Head->getTerminator(); LLVMContext &C = Head->getContext(); - BasicBlock *ThenBlock = BasicBlock::Create(C, "", Head->getParent(), Tail); Instruction *CheckTerm; - if (Unreachable) - CheckTerm = new UnreachableInst(C, ThenBlock); - else - CheckTerm = BranchInst::Create(Tail, ThenBlock); - CheckTerm->setDebugLoc(SplitBefore->getDebugLoc()); + bool CreateThenBlock = (ThenBlock == nullptr); + if (CreateThenBlock) { + ThenBlock = BasicBlock::Create(C, "", Head->getParent(), Tail); + if (Unreachable) + CheckTerm = new UnreachableInst(C, ThenBlock); + else + CheckTerm = BranchInst::Create(Tail, ThenBlock); + CheckTerm->setDebugLoc(SplitBefore->getDebugLoc()); + } else + CheckTerm = ThenBlock->getTerminator(); BranchInst *HeadNewTerm = BranchInst::Create(/*ifTrue*/ThenBlock, /*ifFalse*/Tail, Cond); HeadNewTerm->setMetadata(LLVMContext::MD_prof, BranchWeights); @@ -756,7 +761,10 @@ Instruction *llvm::SplitBlockAndInsertIfThen(Value *Cond, DT->changeImmediateDominator(Child, NewNode); // Head dominates ThenBlock. - DT->addNewBlock(ThenBlock, Head); + if (CreateThenBlock) + DT->addNewBlock(ThenBlock, Head); + else + DT->changeImmediateDominator(ThenBlock, Head); } } -- 2.7.4