diff options
| author | Dimitry Andric <dim@FreeBSD.org> | 2016-07-23 20:41:05 +0000 | 
|---|---|---|
| committer | Dimitry Andric <dim@FreeBSD.org> | 2016-07-23 20:41:05 +0000 | 
| commit | 01095a5d43bbfde13731688ddcf6048ebb8b7721 (patch) | |
| tree | 4def12e759965de927d963ac65840d663ef9d1ea /lib/Transforms/Utils/BasicBlockUtils.cpp | |
| parent | f0f4822ed4b66e3579e92a89f368f8fb860e218e (diff) | |
Notes
Diffstat (limited to 'lib/Transforms/Utils/BasicBlockUtils.cpp')
| -rw-r--r-- | lib/Transforms/Utils/BasicBlockUtils.cpp | 169 | 
1 files changed, 27 insertions, 142 deletions
diff --git a/lib/Transforms/Utils/BasicBlockUtils.cpp b/lib/Transforms/Utils/BasicBlockUtils.cpp index 72db980cf572a..b90349d3cdad1 100644 --- a/lib/Transforms/Utils/BasicBlockUtils.cpp +++ b/lib/Transforms/Utils/BasicBlockUtils.cpp @@ -31,8 +31,6 @@  #include <algorithm>  using namespace llvm; -/// DeleteDeadBlock - Delete the specified block, which must have no -/// predecessors.  void llvm::DeleteDeadBlock(BasicBlock *BB) {    assert((pred_begin(BB) == pred_end(BB) ||           // Can delete self loop. @@ -61,12 +59,8 @@ void llvm::DeleteDeadBlock(BasicBlock *BB) {    BB->eraseFromParent();  } -/// FoldSingleEntryPHINodes - We know that BB has one predecessor.  If there are -/// any single-entry PHI nodes in it, fold them away.  This handles the case -/// when all entries to the PHI nodes in a block are guaranteed equal, such as -/// when the block has exactly one predecessor.  void llvm::FoldSingleEntryPHINodes(BasicBlock *BB, -                                   MemoryDependenceAnalysis *MemDep) { +                                   MemoryDependenceResults *MemDep) {    if (!isa<PHINode>(BB->begin())) return;    while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) { @@ -82,11 +76,6 @@ void llvm::FoldSingleEntryPHINodes(BasicBlock *BB,    }  } - -/// DeleteDeadPHIs - Examine each PHI in the given block and delete it if it -/// is dead. Also recursively delete any operands that become dead as -/// a result. This includes tracing the def-use list from the PHI to see if -/// it is ultimately unused or if it reaches an unused cycle.  bool llvm::DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI) {    // Recursively deleting a PHI may cause multiple PHIs to be deleted    // or RAUW'd undef, so use an array of WeakVH for the PHIs to delete. @@ -103,11 +92,9 @@ bool llvm::DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI) {    return Changed;  } -/// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor, -/// if possible.  The return value indicates success or failure.  bool llvm::MergeBlockIntoPredecessor(BasicBlock *BB, DominatorTree *DT,                                       LoopInfo *LI, -                                     MemoryDependenceAnalysis *MemDep) { +                                     MemoryDependenceResults *MemDep) {    // Don't merge away blocks who have their address taken.    if (BB->hasAddressTaken()) return false; @@ -165,10 +152,8 @@ bool llvm::MergeBlockIntoPredecessor(BasicBlock *BB, DominatorTree *DT,      if (DomTreeNode *DTN = DT->getNode(BB)) {        DomTreeNode *PredDTN = DT->getNode(PredBB);        SmallVector<DomTreeNode *, 8> Children(DTN->begin(), DTN->end()); -      for (SmallVectorImpl<DomTreeNode *>::iterator DI = Children.begin(), -                                                    DE = Children.end(); -           DI != DE; ++DI) -        DT->changeImmediateDominator(*DI, PredDTN); +      for (DomTreeNode *DI : Children) +        DT->changeImmediateDominator(DI, PredDTN);        DT->eraseNode(BB);      } @@ -183,9 +168,6 @@ bool llvm::MergeBlockIntoPredecessor(BasicBlock *BB, DominatorTree *DT,    return true;  } -/// ReplaceInstWithValue - Replace all uses of an instruction (specified by BI) -/// with a value, then remove and delete the original instruction. -///  void llvm::ReplaceInstWithValue(BasicBlock::InstListType &BIL,                                  BasicBlock::iterator &BI, Value *V) {    Instruction &I = *BI; @@ -200,11 +182,6 @@ void llvm::ReplaceInstWithValue(BasicBlock::InstListType &BIL,    BI = BIL.erase(BI);  } - -/// ReplaceInstWithInst - Replace the instruction specified by BI with the -/// instruction specified by I.  The original instruction is deleted and BI is -/// updated to point to the new instruction. -///  void llvm::ReplaceInstWithInst(BasicBlock::InstListType &BIL,                                 BasicBlock::iterator &BI, Instruction *I) {    assert(I->getParent() == nullptr && @@ -225,16 +202,11 @@ void llvm::ReplaceInstWithInst(BasicBlock::InstListType &BIL,    BI = New;  } -/// ReplaceInstWithInst - Replace the instruction specified by From with the -/// instruction specified by To. -///  void llvm::ReplaceInstWithInst(Instruction *From, Instruction *To) {    BasicBlock::iterator BI(From);    ReplaceInstWithInst(From->getParent()->getInstList(), BI, To);  } -/// SplitEdge -  Split the edge connecting specified block. Pass P must -/// not be NULL.  BasicBlock *llvm::SplitEdge(BasicBlock *BB, BasicBlock *Succ, DominatorTree *DT,                              LoopInfo *LI) {    unsigned SuccNum = GetSuccessorNumber(BB, Succ); @@ -266,8 +238,8 @@ unsigned  llvm::SplitAllCriticalEdges(Function &F,                              const CriticalEdgeSplittingOptions &Options) {    unsigned NumBroken = 0; -  for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) { -    TerminatorInst *TI = I->getTerminator(); +  for (BasicBlock &BB : F) { +    TerminatorInst *TI = BB.getTerminator();      if (TI->getNumSuccessors() > 1 && !isa<IndirectBrInst>(TI))        for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)          if (SplitCriticalEdge(TI, i, Options)) @@ -276,11 +248,6 @@ llvm::SplitAllCriticalEdges(Function &F,    return NumBroken;  } -/// SplitBlock - Split the specified block at the specified instruction - every -/// thing before SplitPt stays in Old and everything starting with SplitPt moves -/// to a new block.  The two blocks are joined by an unconditional branch and -/// the loop info is updated. -///  BasicBlock *llvm::SplitBlock(BasicBlock *Old, Instruction *SplitPt,                               DominatorTree *DT, LoopInfo *LI) {    BasicBlock::iterator SplitIt = SplitPt->getIterator(); @@ -297,22 +264,17 @@ BasicBlock *llvm::SplitBlock(BasicBlock *Old, Instruction *SplitPt,    if (DT)      // Old dominates New. New node dominates all other nodes dominated by Old.      if (DomTreeNode *OldNode = DT->getNode(Old)) { -      std::vector<DomTreeNode *> Children; -      for (DomTreeNode::iterator I = OldNode->begin(), E = OldNode->end(); -           I != E; ++I) -        Children.push_back(*I); +      std::vector<DomTreeNode *> Children(OldNode->begin(), OldNode->end());        DomTreeNode *NewNode = DT->addNewBlock(New, Old); -      for (std::vector<DomTreeNode *>::iterator I = Children.begin(), -             E = Children.end(); I != E; ++I) -        DT->changeImmediateDominator(*I, NewNode); +      for (DomTreeNode *I : Children) +        DT->changeImmediateDominator(I, NewNode);      }    return New;  } -/// UpdateAnalysisInformation - Update DominatorTree, LoopInfo, and LCCSA -/// analysis information. +/// Update DominatorTree, LoopInfo, and LCCSA analysis information.  static void UpdateAnalysisInformation(BasicBlock *OldBB, BasicBlock *NewBB,                                        ArrayRef<BasicBlock *> Preds,                                        DominatorTree *DT, LoopInfo *LI, @@ -331,10 +293,7 @@ static void UpdateAnalysisInformation(BasicBlock *OldBB, BasicBlock *NewBB,    // this split will affect loops.    bool IsLoopEntry = !!L;    bool SplitMakesNewLoopHeader = false; -  for (ArrayRef<BasicBlock *>::iterator i = Preds.begin(), e = Preds.end(); -       i != e; ++i) { -    BasicBlock *Pred = *i; - +  for (BasicBlock *Pred : Preds) {      // If we need to preserve LCSSA, determine if any of the preds is a loop      // exit.      if (PreserveLCSSA) @@ -362,9 +321,7 @@ static void UpdateAnalysisInformation(BasicBlock *OldBB, BasicBlock *NewBB,      // loops enclose them, and select the most-nested loop which contains the      // loop containing the block being split.      Loop *InnermostPredLoop = nullptr; -    for (ArrayRef<BasicBlock*>::iterator -           i = Preds.begin(), e = Preds.end(); i != e; ++i) { -      BasicBlock *Pred = *i; +    for (BasicBlock *Pred : Preds) {        if (Loop *PredLoop = LI->getLoopFor(Pred)) {          // Seek a loop which actually contains the block being split (to avoid          // adjacent loops). @@ -388,8 +345,8 @@ static void UpdateAnalysisInformation(BasicBlock *OldBB, BasicBlock *NewBB,    }  } -/// UpdatePHINodes - Update the PHI nodes in OrigBB to include the values coming -/// from NewBB. This also updates AliasAnalysis, if available. +/// Update the PHI nodes in OrigBB to include the values coming from NewBB. +/// This also updates AliasAnalysis, if available.  static void UpdatePHINodes(BasicBlock *OrigBB, BasicBlock *NewBB,                             ArrayRef<BasicBlock *> Preds, BranchInst *BI,                             bool HasLoopExit) { @@ -456,21 +413,6 @@ static void UpdatePHINodes(BasicBlock *OrigBB, BasicBlock *NewBB,    }  } -/// SplitBlockPredecessors - 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. The new predecessors are indicated by the -/// Preds array. The new block is given a suffix of 'Suffix'. Returns new basic -/// block to which predecessors from Preds are now pointing. -/// -/// If BB is a landingpad block then additional basicblock might be introduced. -/// It will have suffix of 'Suffix'+".split_lp". -/// See SplitLandingPadPredecessors for more details on this case. -/// -/// This currently updates the LLVM IR, AliasAnalysis, DominatorTree, -/// LoopInfo, and LCCSA but no other analyses. In particular, it does not -/// preserve LoopSimplify (because it's complicated to handle the case where one -/// of the edges being split is an exit of a loop with other exits). -///  BasicBlock *llvm::SplitBlockPredecessors(BasicBlock *BB,                                           ArrayRef<BasicBlock *> Preds,                                           const char *Suffix, DominatorTree *DT, @@ -529,19 +471,6 @@ BasicBlock *llvm::SplitBlockPredecessors(BasicBlock *BB,    return NewBB;  } -/// SplitLandingPadPredecessors - This method transforms the landing pad, -/// OrigBB, by introducing two new basic blocks into the function. One of those -/// new basic blocks gets the predecessors listed in Preds. The other basic -/// block gets the remaining predecessors of OrigBB. The landingpad instruction -/// OrigBB is clone into both of the new basic blocks. The new blocks are given -/// the suffixes 'Suffix1' and 'Suffix2', and are returned in the NewBBs vector. -/// -/// This currently updates the LLVM IR, AliasAnalysis, DominatorTree, -/// DominanceFrontier, LoopInfo, and LCCSA but no other analyses. In particular, -/// it does not preserve LoopSimplify (because it's complicated to handle the -/// case where one of the edges being split is an exit of a loop with other -/// exits). -///  void llvm::SplitLandingPadPredecessors(BasicBlock *OrigBB,                                         ArrayRef<BasicBlock *> Preds,                                         const char *Suffix1, const char *Suffix2, @@ -603,9 +532,8 @@ void llvm::SplitLandingPadPredecessors(BasicBlock *OrigBB,      BI2->setDebugLoc(OrigBB->getFirstNonPHI()->getDebugLoc());      // Move the remaining edges from OrigBB to point to NewBB2. -    for (SmallVectorImpl<BasicBlock*>::iterator -           i = NewBB2Preds.begin(), e = NewBB2Preds.end(); i != e; ++i) -      (*i)->getTerminator()->replaceUsesOfWith(OrigBB, NewBB2); +    for (BasicBlock *NewBB2Pred : NewBB2Preds) +      NewBB2Pred->getTerminator()->replaceUsesOfWith(OrigBB, NewBB2);      // Update DominatorTree, LoopInfo, and LCCSA analysis information.      HasLoopExit = false; @@ -646,11 +574,6 @@ void llvm::SplitLandingPadPredecessors(BasicBlock *OrigBB,    }  } -/// FoldReturnIntoUncondBranch - This method duplicates the specified return -/// instruction into a predecessor which ends in an unconditional branch. If -/// the return instruction returns a value defined by a PHI, propagate the -/// right value into the return. It returns the new return instruction in the -/// predecessor.  ReturnInst *llvm::FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB,                                               BasicBlock *Pred) {    Instruction *UncondBranch = Pred->getTerminator(); @@ -689,31 +612,10 @@ ReturnInst *llvm::FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB,    return cast<ReturnInst>(NewRet);  } -/// SplitBlockAndInsertIfThen - Split the containing block at the -/// specified instruction - everything before and including SplitBefore stays -/// in the old basic block, and everything after SplitBefore is moved to a -/// new block. The two blocks are connected by a conditional branch -/// (with value of Cmp being the condition). -/// Before: -///   Head -///   SplitBefore -///   Tail -/// After: -///   Head -///   if (Cond) -///     ThenBlock -///   SplitBefore -///   Tail -/// -/// If Unreachable is true, then ThenBlock ends with -/// UnreachableInst, otherwise it branches to Tail. -/// Returns the NewBasicBlock's terminator. - -TerminatorInst *llvm::SplitBlockAndInsertIfThen(Value *Cond, -                                                Instruction *SplitBefore, -                                                bool Unreachable, -                                                MDNode *BranchWeights, -                                                DominatorTree *DT) { +TerminatorInst * +llvm::SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore, +                                bool Unreachable, MDNode *BranchWeights, +                                DominatorTree *DT, LoopInfo *LI) {    BasicBlock *Head = SplitBefore->getParent();    BasicBlock *Tail = Head->splitBasicBlock(SplitBefore->getIterator());    TerminatorInst *HeadOldTerm = Head->getTerminator(); @@ -735,7 +637,7 @@ TerminatorInst *llvm::SplitBlockAndInsertIfThen(Value *Cond,        std::vector<DomTreeNode *> Children(OldNode->begin(), OldNode->end());        DomTreeNode *NewNode = DT->addNewBlock(Tail, Head); -      for (auto Child : Children) +      for (DomTreeNode *Child : Children)          DT->changeImmediateDominator(Child, NewNode);        // Head dominates ThenBlock. @@ -743,23 +645,15 @@ TerminatorInst *llvm::SplitBlockAndInsertIfThen(Value *Cond,      }    } +  if (LI) { +    Loop *L = LI->getLoopFor(Head); +    L->addBasicBlockToLoop(ThenBlock, *LI); +    L->addBasicBlockToLoop(Tail, *LI); +  } +    return CheckTerm;  } -/// SplitBlockAndInsertIfThenElse is similar to SplitBlockAndInsertIfThen, -/// but also creates the ElseBlock. -/// Before: -///   Head -///   SplitBefore -///   Tail -/// After: -///   Head -///   if (Cond) -///     ThenBlock -///   else -///     ElseBlock -///   SplitBefore -///   Tail  void llvm::SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore,                                           TerminatorInst **ThenTerm,                                           TerminatorInst **ElseTerm, @@ -781,15 +675,6 @@ void llvm::SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore,  } -/// GetIfCondition - Given a basic block (BB) with two predecessors, -/// check to see if the merge at this block is due -/// to an "if condition".  If so, return the boolean condition that determines -/// which entry into BB will be taken.  Also, return by references the block -/// that will be entered from if the condition is true, and the block that will -/// be entered if the condition is false. -/// -/// This does no checking to see if the true/false blocks have large or unsavory -/// instructions in them.  Value *llvm::GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue,                               BasicBlock *&IfFalse) {    PHINode *SomePHI = dyn_cast<PHINode>(BB->begin());  | 
