LLVM API Documentation

BreakCriticalEdges.cpp
Go to the documentation of this file.
00001 //===- BreakCriticalEdges.cpp - Critical Edge Elimination Pass ------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // BreakCriticalEdges pass - Break all of the critical edges in the CFG by
00011 // inserting a dummy basic block.  This pass may be "required" by passes that
00012 // cannot deal with critical edges.  For this usage, the structure type is
00013 // forward declared.  This pass obviously invalidates the CFG, but can update
00014 // dominator trees.
00015 //
00016 //===----------------------------------------------------------------------===//
00017 
00018 #define DEBUG_TYPE "break-crit-edges"
00019 #include "llvm/Transforms/Scalar.h"
00020 #include "llvm/ADT/SmallVector.h"
00021 #include "llvm/ADT/Statistic.h"
00022 #include "llvm/Analysis/CFG.h"
00023 #include "llvm/Analysis/LoopInfo.h"
00024 #include "llvm/IR/CFG.h"
00025 #include "llvm/IR/Dominators.h"
00026 #include "llvm/IR/Function.h"
00027 #include "llvm/IR/Instructions.h"
00028 #include "llvm/IR/Type.h"
00029 #include "llvm/Support/ErrorHandling.h"
00030 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
00031 using namespace llvm;
00032 
00033 STATISTIC(NumBroken, "Number of blocks inserted");
00034 
00035 namespace {
00036   struct BreakCriticalEdges : public FunctionPass {
00037     static char ID; // Pass identification, replacement for typeid
00038     BreakCriticalEdges() : FunctionPass(ID) {
00039       initializeBreakCriticalEdgesPass(*PassRegistry::getPassRegistry());
00040     }
00041 
00042     bool runOnFunction(Function &F) override;
00043 
00044     void getAnalysisUsage(AnalysisUsage &AU) const override {
00045       AU.addPreserved<DominatorTreeWrapperPass>();
00046       AU.addPreserved<LoopInfo>();
00047 
00048       // No loop canonicalization guarantees are broken by this pass.
00049       AU.addPreservedID(LoopSimplifyID);
00050     }
00051   };
00052 }
00053 
00054 char BreakCriticalEdges::ID = 0;
00055 INITIALIZE_PASS(BreakCriticalEdges, "break-crit-edges",
00056                 "Break critical edges in CFG", false, false)
00057 
00058 // Publicly exposed interface to pass...
00059 char &llvm::BreakCriticalEdgesID = BreakCriticalEdges::ID;
00060 FunctionPass *llvm::createBreakCriticalEdgesPass() {
00061   return new BreakCriticalEdges();
00062 }
00063 
00064 // runOnFunction - Loop over all of the edges in the CFG, breaking critical
00065 // edges as they are found.
00066 //
00067 bool BreakCriticalEdges::runOnFunction(Function &F) {
00068   bool Changed = false;
00069   for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
00070     TerminatorInst *TI = I->getTerminator();
00071     if (TI->getNumSuccessors() > 1 && !isa<IndirectBrInst>(TI))
00072       for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
00073         if (SplitCriticalEdge(TI, i, this)) {
00074           ++NumBroken;
00075           Changed = true;
00076         }
00077   }
00078 
00079   return Changed;
00080 }
00081 
00082 //===----------------------------------------------------------------------===//
00083 //    Implementation of the external critical edge manipulation functions
00084 //===----------------------------------------------------------------------===//
00085 
00086 /// createPHIsForSplitLoopExit - When a loop exit edge is split, LCSSA form
00087 /// may require new PHIs in the new exit block. This function inserts the
00088 /// new PHIs, as needed. Preds is a list of preds inside the loop, SplitBB
00089 /// is the new loop exit block, and DestBB is the old loop exit, now the
00090 /// successor of SplitBB.
00091 static void createPHIsForSplitLoopExit(ArrayRef<BasicBlock *> Preds,
00092                                        BasicBlock *SplitBB,
00093                                        BasicBlock *DestBB) {
00094   // SplitBB shouldn't have anything non-trivial in it yet.
00095   assert((SplitBB->getFirstNonPHI() == SplitBB->getTerminator() ||
00096           SplitBB->isLandingPad()) && "SplitBB has non-PHI nodes!");
00097 
00098   // For each PHI in the destination block.
00099   for (BasicBlock::iterator I = DestBB->begin();
00100        PHINode *PN = dyn_cast<PHINode>(I); ++I) {
00101     unsigned Idx = PN->getBasicBlockIndex(SplitBB);
00102     Value *V = PN->getIncomingValue(Idx);
00103 
00104     // If the input is a PHI which already satisfies LCSSA, don't create
00105     // a new one.
00106     if (const PHINode *VP = dyn_cast<PHINode>(V))
00107       if (VP->getParent() == SplitBB)
00108         continue;
00109 
00110     // Otherwise a new PHI is needed. Create one and populate it.
00111     PHINode *NewPN =
00112       PHINode::Create(PN->getType(), Preds.size(), "split",
00113                       SplitBB->isLandingPad() ?
00114                       SplitBB->begin() : SplitBB->getTerminator());
00115     for (unsigned i = 0, e = Preds.size(); i != e; ++i)
00116       NewPN->addIncoming(V, Preds[i]);
00117 
00118     // Update the original PHI.
00119     PN->setIncomingValue(Idx, NewPN);
00120   }
00121 }
00122 
00123 /// SplitCriticalEdge - If this edge is a critical edge, insert a new node to
00124 /// split the critical edge.  This will update DominatorTree information if it
00125 /// is available, thus calling this pass will not invalidate either of them.
00126 /// This returns the new block if the edge was split, null otherwise.
00127 ///
00128 /// If MergeIdenticalEdges is true (not the default), *all* edges from TI to the
00129 /// specified successor will be merged into the same critical edge block.
00130 /// This is most commonly interesting with switch instructions, which may
00131 /// have many edges to any one destination.  This ensures that all edges to that
00132 /// dest go to one block instead of each going to a different block, but isn't
00133 /// the standard definition of a "critical edge".
00134 ///
00135 /// It is invalid to call this function on a critical edge that starts at an
00136 /// IndirectBrInst.  Splitting these edges will almost always create an invalid
00137 /// program because the address of the new block won't be the one that is jumped
00138 /// to.
00139 ///
00140 BasicBlock *llvm::SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum,
00141                                     Pass *P, bool MergeIdenticalEdges,
00142                                     bool DontDeleteUselessPhis,
00143                                     bool SplitLandingPads) {
00144   if (!isCriticalEdge(TI, SuccNum, MergeIdenticalEdges)) return 0;
00145 
00146   assert(!isa<IndirectBrInst>(TI) &&
00147          "Cannot split critical edge from IndirectBrInst");
00148 
00149   BasicBlock *TIBB = TI->getParent();
00150   BasicBlock *DestBB = TI->getSuccessor(SuccNum);
00151 
00152   // Splitting the critical edge to a landing pad block is non-trivial. Don't do
00153   // it in this generic function.
00154   if (DestBB->isLandingPad()) return 0;
00155 
00156   // Create a new basic block, linking it into the CFG.
00157   BasicBlock *NewBB = BasicBlock::Create(TI->getContext(),
00158                       TIBB->getName() + "." + DestBB->getName() + "_crit_edge");
00159   // Create our unconditional branch.
00160   BranchInst *NewBI = BranchInst::Create(DestBB, NewBB);
00161   NewBI->setDebugLoc(TI->getDebugLoc());
00162 
00163   // Branch to the new block, breaking the edge.
00164   TI->setSuccessor(SuccNum, NewBB);
00165 
00166   // Insert the block into the function... right after the block TI lives in.
00167   Function &F = *TIBB->getParent();
00168   Function::iterator FBBI = TIBB;
00169   F.getBasicBlockList().insert(++FBBI, NewBB);
00170 
00171   // If there are any PHI nodes in DestBB, we need to update them so that they
00172   // merge incoming values from NewBB instead of from TIBB.
00173   {
00174     unsigned BBIdx = 0;
00175     for (BasicBlock::iterator I = DestBB->begin(); isa<PHINode>(I); ++I) {
00176       // We no longer enter through TIBB, now we come in through NewBB.
00177       // Revector exactly one entry in the PHI node that used to come from
00178       // TIBB to come from NewBB.
00179       PHINode *PN = cast<PHINode>(I);
00180 
00181       // Reuse the previous value of BBIdx if it lines up.  In cases where we
00182       // have multiple phi nodes with *lots* of predecessors, this is a speed
00183       // win because we don't have to scan the PHI looking for TIBB.  This
00184       // happens because the BB list of PHI nodes are usually in the same
00185       // order.
00186       if (PN->getIncomingBlock(BBIdx) != TIBB)
00187         BBIdx = PN->getBasicBlockIndex(TIBB);
00188       PN->setIncomingBlock(BBIdx, NewBB);
00189     }
00190   }
00191 
00192   // If there are any other edges from TIBB to DestBB, update those to go
00193   // through the split block, making those edges non-critical as well (and
00194   // reducing the number of phi entries in the DestBB if relevant).
00195   if (MergeIdenticalEdges) {
00196     for (unsigned i = SuccNum+1, e = TI->getNumSuccessors(); i != e; ++i) {
00197       if (TI->getSuccessor(i) != DestBB) continue;
00198 
00199       // Remove an entry for TIBB from DestBB phi nodes.
00200       DestBB->removePredecessor(TIBB, DontDeleteUselessPhis);
00201 
00202       // We found another edge to DestBB, go to NewBB instead.
00203       TI->setSuccessor(i, NewBB);
00204     }
00205   }
00206 
00207 
00208 
00209   // If we don't have a pass object, we can't update anything...
00210   if (P == 0) return NewBB;
00211 
00212   DominatorTreeWrapperPass *DTWP =
00213       P->getAnalysisIfAvailable<DominatorTreeWrapperPass>();
00214   DominatorTree *DT = DTWP ? &DTWP->getDomTree() : 0;
00215   LoopInfo *LI = P->getAnalysisIfAvailable<LoopInfo>();
00216 
00217   // If we have nothing to update, just return.
00218   if (DT == 0 && LI == 0)
00219     return NewBB;
00220 
00221   // Now update analysis information.  Since the only predecessor of NewBB is
00222   // the TIBB, TIBB clearly dominates NewBB.  TIBB usually doesn't dominate
00223   // anything, as there are other successors of DestBB.  However, if all other
00224   // predecessors of DestBB are already dominated by DestBB (e.g. DestBB is a
00225   // loop header) then NewBB dominates DestBB.
00226   SmallVector<BasicBlock*, 8> OtherPreds;
00227 
00228   // If there is a PHI in the block, loop over predecessors with it, which is
00229   // faster than iterating pred_begin/end.
00230   if (PHINode *PN = dyn_cast<PHINode>(DestBB->begin())) {
00231     for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
00232       if (PN->getIncomingBlock(i) != NewBB)
00233         OtherPreds.push_back(PN->getIncomingBlock(i));
00234   } else {
00235     for (pred_iterator I = pred_begin(DestBB), E = pred_end(DestBB);
00236          I != E; ++I) {
00237       BasicBlock *P = *I;
00238       if (P != NewBB)
00239         OtherPreds.push_back(P);
00240     }
00241   }
00242 
00243   bool NewBBDominatesDestBB = true;
00244 
00245   // Should we update DominatorTree information?
00246   if (DT) {
00247     DomTreeNode *TINode = DT->getNode(TIBB);
00248 
00249     // The new block is not the immediate dominator for any other nodes, but
00250     // TINode is the immediate dominator for the new node.
00251     //
00252     if (TINode) {       // Don't break unreachable code!
00253       DomTreeNode *NewBBNode = DT->addNewBlock(NewBB, TIBB);
00254       DomTreeNode *DestBBNode = 0;
00255 
00256       // If NewBBDominatesDestBB hasn't been computed yet, do so with DT.
00257       if (!OtherPreds.empty()) {
00258         DestBBNode = DT->getNode(DestBB);
00259         while (!OtherPreds.empty() && NewBBDominatesDestBB) {
00260           if (DomTreeNode *OPNode = DT->getNode(OtherPreds.back()))
00261             NewBBDominatesDestBB = DT->dominates(DestBBNode, OPNode);
00262           OtherPreds.pop_back();
00263         }
00264         OtherPreds.clear();
00265       }
00266 
00267       // If NewBBDominatesDestBB, then NewBB dominates DestBB, otherwise it
00268       // doesn't dominate anything.
00269       if (NewBBDominatesDestBB) {
00270         if (!DestBBNode) DestBBNode = DT->getNode(DestBB);
00271         DT->changeImmediateDominator(DestBBNode, NewBBNode);
00272       }
00273     }
00274   }
00275 
00276   // Update LoopInfo if it is around.
00277   if (LI) {
00278     if (Loop *TIL = LI->getLoopFor(TIBB)) {
00279       // If one or the other blocks were not in a loop, the new block is not
00280       // either, and thus LI doesn't need to be updated.
00281       if (Loop *DestLoop = LI->getLoopFor(DestBB)) {
00282         if (TIL == DestLoop) {
00283           // Both in the same loop, the NewBB joins loop.
00284           DestLoop->addBasicBlockToLoop(NewBB, LI->getBase());
00285         } else if (TIL->contains(DestLoop)) {
00286           // Edge from an outer loop to an inner loop.  Add to the outer loop.
00287           TIL->addBasicBlockToLoop(NewBB, LI->getBase());
00288         } else if (DestLoop->contains(TIL)) {
00289           // Edge from an inner loop to an outer loop.  Add to the outer loop.
00290           DestLoop->addBasicBlockToLoop(NewBB, LI->getBase());
00291         } else {
00292           // Edge from two loops with no containment relation.  Because these
00293           // are natural loops, we know that the destination block must be the
00294           // header of its loop (adding a branch into a loop elsewhere would
00295           // create an irreducible loop).
00296           assert(DestLoop->getHeader() == DestBB &&
00297                  "Should not create irreducible loops!");
00298           if (Loop *P = DestLoop->getParentLoop())
00299             P->addBasicBlockToLoop(NewBB, LI->getBase());
00300         }
00301       }
00302       // If TIBB is in a loop and DestBB is outside of that loop, we may need
00303       // to update LoopSimplify form and LCSSA form.
00304       if (!TIL->contains(DestBB) &&
00305           P->mustPreserveAnalysisID(LoopSimplifyID)) {
00306         assert(!TIL->contains(NewBB) &&
00307                "Split point for loop exit is contained in loop!");
00308 
00309         // Update LCSSA form in the newly created exit block.
00310         if (P->mustPreserveAnalysisID(LCSSAID))
00311           createPHIsForSplitLoopExit(TIBB, NewBB, DestBB);
00312 
00313         // The only that we can break LoopSimplify form by splitting a critical
00314         // edge is if after the split there exists some edge from TIL to DestBB
00315         // *and* the only edge into DestBB from outside of TIL is that of
00316         // NewBB. If the first isn't true, then LoopSimplify still holds, NewBB
00317         // is the new exit block and it has no non-loop predecessors. If the
00318         // second isn't true, then DestBB was not in LoopSimplify form prior to
00319         // the split as it had a non-loop predecessor. In both of these cases,
00320         // the predecessor must be directly in TIL, not in a subloop, or again
00321         // LoopSimplify doesn't hold.
00322         SmallVector<BasicBlock *, 4> LoopPreds;
00323         for (pred_iterator I = pred_begin(DestBB), E = pred_end(DestBB); I != E;
00324              ++I) {
00325           BasicBlock *P = *I;
00326           if (P == NewBB)
00327             continue; // The new block is known.
00328           if (LI->getLoopFor(P) != TIL) {
00329             // No need to re-simplify, it wasn't to start with.
00330             LoopPreds.clear();
00331             break;
00332           }
00333           LoopPreds.push_back(P);
00334         }
00335         if (!LoopPreds.empty()) {
00336           assert(!DestBB->isLandingPad() &&
00337                  "We don't split edges to landing pads!");
00338           BasicBlock *NewExitBB =
00339               SplitBlockPredecessors(DestBB, LoopPreds, "split", P);
00340           if (P->mustPreserveAnalysisID(LCSSAID))
00341             createPHIsForSplitLoopExit(LoopPreds, NewExitBB, DestBB);
00342         }
00343       }
00344       // LCSSA form was updated above for the case where LoopSimplify is
00345       // available, which means that all predecessors of loop exit blocks
00346       // are within the loop. Without LoopSimplify form, it would be
00347       // necessary to insert a new phi.
00348       assert((!P->mustPreserveAnalysisID(LCSSAID) ||
00349               P->mustPreserveAnalysisID(LoopSimplifyID)) &&
00350              "SplitCriticalEdge doesn't know how to update LCCSA form "
00351              "without LoopSimplify!");
00352     }
00353   }
00354 
00355   return NewBB;
00356 }