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/Dominators.h"
00023 #include "llvm/Analysis/LoopInfo.h"
00024 #include "llvm/Analysis/ProfileInfo.h"
00025 #include "llvm/IR/Function.h"
00026 #include "llvm/IR/Instructions.h"
00027 #include "llvm/IR/Type.h"
00028 #include "llvm/Support/CFG.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     virtual bool runOnFunction(Function &F);
00043 
00044     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00045       AU.addPreserved<DominatorTree>();
00046       AU.addPreserved<LoopInfo>();
00047       AU.addPreserved<ProfileInfo>();
00048 
00049       // No loop canonicalization guarantees are broken by this pass.
00050       AU.addPreservedID(LoopSimplifyID);
00051     }
00052   };
00053 }
00054 
00055 char BreakCriticalEdges::ID = 0;
00056 INITIALIZE_PASS(BreakCriticalEdges, "break-crit-edges",
00057                 "Break critical edges in CFG", false, false)
00058 
00059 // Publicly exposed interface to pass...
00060 char &llvm::BreakCriticalEdgesID = BreakCriticalEdges::ID;
00061 FunctionPass *llvm::createBreakCriticalEdgesPass() {
00062   return new BreakCriticalEdges();
00063 }
00064 
00065 // runOnFunction - Loop over all of the edges in the CFG, breaking critical
00066 // edges as they are found.
00067 //
00068 bool BreakCriticalEdges::runOnFunction(Function &F) {
00069   bool Changed = false;
00070   for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
00071     TerminatorInst *TI = I->getTerminator();
00072     if (TI->getNumSuccessors() > 1 && !isa<IndirectBrInst>(TI))
00073       for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
00074         if (SplitCriticalEdge(TI, i, this)) {
00075           ++NumBroken;
00076           Changed = true;
00077         }
00078   }
00079 
00080   return Changed;
00081 }
00082 
00083 //===----------------------------------------------------------------------===//
00084 //    Implementation of the external critical edge manipulation functions
00085 //===----------------------------------------------------------------------===//
00086 
00087 // isCriticalEdge - Return true if the specified edge is a critical edge.
00088 // Critical edges are edges from a block with multiple successors to a block
00089 // with multiple predecessors.
00090 //
00091 bool llvm::isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum,
00092                           bool AllowIdenticalEdges) {
00093   assert(SuccNum < TI->getNumSuccessors() && "Illegal edge specification!");
00094   if (TI->getNumSuccessors() == 1) return false;
00095 
00096   const BasicBlock *Dest = TI->getSuccessor(SuccNum);
00097   const_pred_iterator I = pred_begin(Dest), E = pred_end(Dest);
00098 
00099   // If there is more than one predecessor, this is a critical edge...
00100   assert(I != E && "No preds, but we have an edge to the block?");
00101   const BasicBlock *FirstPred = *I;
00102   ++I;        // Skip one edge due to the incoming arc from TI.
00103   if (!AllowIdenticalEdges)
00104     return I != E;
00105 
00106   // If AllowIdenticalEdges is true, then we allow this edge to be considered
00107   // non-critical iff all preds come from TI's block.
00108   while (I != E) {
00109     const BasicBlock *P = *I;
00110     if (P != FirstPred)
00111       return true;
00112     // Note: leave this as is until no one ever compiles with either gcc 4.0.1
00113     // or Xcode 2. This seems to work around the pred_iterator assert in PR 2207
00114     E = pred_end(P);
00115     ++I;
00116   }
00117   return false;
00118 }
00119 
00120 /// createPHIsForSplitLoopExit - When a loop exit edge is split, LCSSA form
00121 /// may require new PHIs in the new exit block. This function inserts the
00122 /// new PHIs, as needed. Preds is a list of preds inside the loop, SplitBB
00123 /// is the new loop exit block, and DestBB is the old loop exit, now the
00124 /// successor of SplitBB.
00125 static void createPHIsForSplitLoopExit(ArrayRef<BasicBlock *> Preds,
00126                                        BasicBlock *SplitBB,
00127                                        BasicBlock *DestBB) {
00128   // SplitBB shouldn't have anything non-trivial in it yet.
00129   assert((SplitBB->getFirstNonPHI() == SplitBB->getTerminator() ||
00130           SplitBB->isLandingPad()) && "SplitBB has non-PHI nodes!");
00131 
00132   // For each PHI in the destination block.
00133   for (BasicBlock::iterator I = DestBB->begin();
00134        PHINode *PN = dyn_cast<PHINode>(I); ++I) {
00135     unsigned Idx = PN->getBasicBlockIndex(SplitBB);
00136     Value *V = PN->getIncomingValue(Idx);
00137 
00138     // If the input is a PHI which already satisfies LCSSA, don't create
00139     // a new one.
00140     if (const PHINode *VP = dyn_cast<PHINode>(V))
00141       if (VP->getParent() == SplitBB)
00142         continue;
00143 
00144     // Otherwise a new PHI is needed. Create one and populate it.
00145     PHINode *NewPN =
00146       PHINode::Create(PN->getType(), Preds.size(), "split",
00147                       SplitBB->isLandingPad() ?
00148                       SplitBB->begin() : SplitBB->getTerminator());
00149     for (unsigned i = 0, e = Preds.size(); i != e; ++i)
00150       NewPN->addIncoming(V, Preds[i]);
00151 
00152     // Update the original PHI.
00153     PN->setIncomingValue(Idx, NewPN);
00154   }
00155 }
00156 
00157 /// SplitCriticalEdge - If this edge is a critical edge, insert a new node to
00158 /// split the critical edge.  This will update DominatorTree information if it
00159 /// is available, thus calling this pass will not invalidate either of them.
00160 /// This returns the new block if the edge was split, null otherwise.
00161 ///
00162 /// If MergeIdenticalEdges is true (not the default), *all* edges from TI to the
00163 /// specified successor will be merged into the same critical edge block.
00164 /// This is most commonly interesting with switch instructions, which may
00165 /// have many edges to any one destination.  This ensures that all edges to that
00166 /// dest go to one block instead of each going to a different block, but isn't
00167 /// the standard definition of a "critical edge".
00168 ///
00169 /// It is invalid to call this function on a critical edge that starts at an
00170 /// IndirectBrInst.  Splitting these edges will almost always create an invalid
00171 /// program because the address of the new block won't be the one that is jumped
00172 /// to.
00173 ///
00174 BasicBlock *llvm::SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum,
00175                                     Pass *P, bool MergeIdenticalEdges,
00176                                     bool DontDeleteUselessPhis,
00177                                     bool SplitLandingPads) {
00178   if (!isCriticalEdge(TI, SuccNum, MergeIdenticalEdges)) return 0;
00179 
00180   assert(!isa<IndirectBrInst>(TI) &&
00181          "Cannot split critical edge from IndirectBrInst");
00182 
00183   BasicBlock *TIBB = TI->getParent();
00184   BasicBlock *DestBB = TI->getSuccessor(SuccNum);
00185 
00186   // Splitting the critical edge to a landing pad block is non-trivial. Don't do
00187   // it in this generic function.
00188   if (DestBB->isLandingPad()) return 0;
00189 
00190   // Create a new basic block, linking it into the CFG.
00191   BasicBlock *NewBB = BasicBlock::Create(TI->getContext(),
00192                       TIBB->getName() + "." + DestBB->getName() + "_crit_edge");
00193   // Create our unconditional branch.
00194   BranchInst *NewBI = BranchInst::Create(DestBB, NewBB);
00195   NewBI->setDebugLoc(TI->getDebugLoc());
00196 
00197   // Branch to the new block, breaking the edge.
00198   TI->setSuccessor(SuccNum, NewBB);
00199 
00200   // Insert the block into the function... right after the block TI lives in.
00201   Function &F = *TIBB->getParent();
00202   Function::iterator FBBI = TIBB;
00203   F.getBasicBlockList().insert(++FBBI, NewBB);
00204 
00205   // If there are any PHI nodes in DestBB, we need to update them so that they
00206   // merge incoming values from NewBB instead of from TIBB.
00207   {
00208     unsigned BBIdx = 0;
00209     for (BasicBlock::iterator I = DestBB->begin(); isa<PHINode>(I); ++I) {
00210       // We no longer enter through TIBB, now we come in through NewBB.
00211       // Revector exactly one entry in the PHI node that used to come from
00212       // TIBB to come from NewBB.
00213       PHINode *PN = cast<PHINode>(I);
00214 
00215       // Reuse the previous value of BBIdx if it lines up.  In cases where we
00216       // have multiple phi nodes with *lots* of predecessors, this is a speed
00217       // win because we don't have to scan the PHI looking for TIBB.  This
00218       // happens because the BB list of PHI nodes are usually in the same
00219       // order.
00220       if (PN->getIncomingBlock(BBIdx) != TIBB)
00221         BBIdx = PN->getBasicBlockIndex(TIBB);
00222       PN->setIncomingBlock(BBIdx, NewBB);
00223     }
00224   }
00225 
00226   // If there are any other edges from TIBB to DestBB, update those to go
00227   // through the split block, making those edges non-critical as well (and
00228   // reducing the number of phi entries in the DestBB if relevant).
00229   if (MergeIdenticalEdges) {
00230     for (unsigned i = SuccNum+1, e = TI->getNumSuccessors(); i != e; ++i) {
00231       if (TI->getSuccessor(i) != DestBB) continue;
00232 
00233       // Remove an entry for TIBB from DestBB phi nodes.
00234       DestBB->removePredecessor(TIBB, DontDeleteUselessPhis);
00235 
00236       // We found another edge to DestBB, go to NewBB instead.
00237       TI->setSuccessor(i, NewBB);
00238     }
00239   }
00240 
00241 
00242 
00243   // If we don't have a pass object, we can't update anything...
00244   if (P == 0) return NewBB;
00245 
00246   DominatorTree *DT = P->getAnalysisIfAvailable<DominatorTree>();
00247   LoopInfo *LI = P->getAnalysisIfAvailable<LoopInfo>();
00248   ProfileInfo *PI = P->getAnalysisIfAvailable<ProfileInfo>();
00249 
00250   // If we have nothing to update, just return.
00251   if (DT == 0 && LI == 0 && PI == 0)
00252     return NewBB;
00253 
00254   // Now update analysis information.  Since the only predecessor of NewBB is
00255   // the TIBB, TIBB clearly dominates NewBB.  TIBB usually doesn't dominate
00256   // anything, as there are other successors of DestBB.  However, if all other
00257   // predecessors of DestBB are already dominated by DestBB (e.g. DestBB is a
00258   // loop header) then NewBB dominates DestBB.
00259   SmallVector<BasicBlock*, 8> OtherPreds;
00260 
00261   // If there is a PHI in the block, loop over predecessors with it, which is
00262   // faster than iterating pred_begin/end.
00263   if (PHINode *PN = dyn_cast<PHINode>(DestBB->begin())) {
00264     for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
00265       if (PN->getIncomingBlock(i) != NewBB)
00266         OtherPreds.push_back(PN->getIncomingBlock(i));
00267   } else {
00268     for (pred_iterator I = pred_begin(DestBB), E = pred_end(DestBB);
00269          I != E; ++I) {
00270       BasicBlock *P = *I;
00271       if (P != NewBB)
00272         OtherPreds.push_back(P);
00273     }
00274   }
00275 
00276   bool NewBBDominatesDestBB = true;
00277 
00278   // Should we update DominatorTree information?
00279   if (DT) {
00280     DomTreeNode *TINode = DT->getNode(TIBB);
00281 
00282     // The new block is not the immediate dominator for any other nodes, but
00283     // TINode is the immediate dominator for the new node.
00284     //
00285     if (TINode) {       // Don't break unreachable code!
00286       DomTreeNode *NewBBNode = DT->addNewBlock(NewBB, TIBB);
00287       DomTreeNode *DestBBNode = 0;
00288 
00289       // If NewBBDominatesDestBB hasn't been computed yet, do so with DT.
00290       if (!OtherPreds.empty()) {
00291         DestBBNode = DT->getNode(DestBB);
00292         while (!OtherPreds.empty() && NewBBDominatesDestBB) {
00293           if (DomTreeNode *OPNode = DT->getNode(OtherPreds.back()))
00294             NewBBDominatesDestBB = DT->dominates(DestBBNode, OPNode);
00295           OtherPreds.pop_back();
00296         }
00297         OtherPreds.clear();
00298       }
00299 
00300       // If NewBBDominatesDestBB, then NewBB dominates DestBB, otherwise it
00301       // doesn't dominate anything.
00302       if (NewBBDominatesDestBB) {
00303         if (!DestBBNode) DestBBNode = DT->getNode(DestBB);
00304         DT->changeImmediateDominator(DestBBNode, NewBBNode);
00305       }
00306     }
00307   }
00308 
00309   // Update LoopInfo if it is around.
00310   if (LI) {
00311     if (Loop *TIL = LI->getLoopFor(TIBB)) {
00312       // If one or the other blocks were not in a loop, the new block is not
00313       // either, and thus LI doesn't need to be updated.
00314       if (Loop *DestLoop = LI->getLoopFor(DestBB)) {
00315         if (TIL == DestLoop) {
00316           // Both in the same loop, the NewBB joins loop.
00317           DestLoop->addBasicBlockToLoop(NewBB, LI->getBase());
00318         } else if (TIL->contains(DestLoop)) {
00319           // Edge from an outer loop to an inner loop.  Add to the outer loop.
00320           TIL->addBasicBlockToLoop(NewBB, LI->getBase());
00321         } else if (DestLoop->contains(TIL)) {
00322           // Edge from an inner loop to an outer loop.  Add to the outer loop.
00323           DestLoop->addBasicBlockToLoop(NewBB, LI->getBase());
00324         } else {
00325           // Edge from two loops with no containment relation.  Because these
00326           // are natural loops, we know that the destination block must be the
00327           // header of its loop (adding a branch into a loop elsewhere would
00328           // create an irreducible loop).
00329           assert(DestLoop->getHeader() == DestBB &&
00330                  "Should not create irreducible loops!");
00331           if (Loop *P = DestLoop->getParentLoop())
00332             P->addBasicBlockToLoop(NewBB, LI->getBase());
00333         }
00334       }
00335       // If TIBB is in a loop and DestBB is outside of that loop, split the
00336       // other exit blocks of the loop that also have predecessors outside
00337       // the loop, to maintain a LoopSimplify guarantee.
00338       if (!TIL->contains(DestBB) &&
00339           P->mustPreserveAnalysisID(LoopSimplifyID)) {
00340         assert(!TIL->contains(NewBB) &&
00341                "Split point for loop exit is contained in loop!");
00342 
00343         // Update LCSSA form in the newly created exit block.
00344         if (P->mustPreserveAnalysisID(LCSSAID))
00345           createPHIsForSplitLoopExit(TIBB, NewBB, DestBB);
00346 
00347         // For each unique exit block...
00348         // FIXME: This code is functionally equivalent to the corresponding
00349         // loop in LoopSimplify.
00350         SmallVector<BasicBlock *, 4> ExitBlocks;
00351         TIL->getExitBlocks(ExitBlocks);
00352         for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) {
00353           // Collect all the preds that are inside the loop, and note
00354           // whether there are any preds outside the loop.
00355           SmallVector<BasicBlock *, 4> Preds;
00356           bool HasPredOutsideOfLoop = false;
00357           BasicBlock *Exit = ExitBlocks[i];
00358           for (pred_iterator I = pred_begin(Exit), E = pred_end(Exit);
00359                I != E; ++I) {
00360             BasicBlock *P = *I;
00361             if (TIL->contains(P)) {
00362               if (isa<IndirectBrInst>(P->getTerminator())) {
00363                 Preds.clear();
00364                 break;
00365               }
00366               Preds.push_back(P);
00367             } else {
00368               HasPredOutsideOfLoop = true;
00369             }
00370           }
00371           // If there are any preds not in the loop, we'll need to split
00372           // the edges. The Preds.empty() check is needed because a block
00373           // may appear multiple times in the list. We can't use
00374           // getUniqueExitBlocks above because that depends on LoopSimplify
00375           // form, which we're in the process of restoring!
00376           if (!Preds.empty() && HasPredOutsideOfLoop) {
00377             if (!Exit->isLandingPad()) {
00378               BasicBlock *NewExitBB =
00379                 SplitBlockPredecessors(Exit, Preds, "split", P);
00380               if (P->mustPreserveAnalysisID(LCSSAID))
00381                 createPHIsForSplitLoopExit(Preds, NewExitBB, Exit);
00382             } else if (SplitLandingPads) {
00383               SmallVector<BasicBlock*, 8> NewBBs;
00384               SplitLandingPadPredecessors(Exit, Preds,
00385                                           ".split1", ".split2",
00386                                           P, NewBBs);
00387               if (P->mustPreserveAnalysisID(LCSSAID))
00388                 createPHIsForSplitLoopExit(Preds, NewBBs[0], Exit);
00389             }
00390           }
00391         }
00392       }
00393       // LCSSA form was updated above for the case where LoopSimplify is
00394       // available, which means that all predecessors of loop exit blocks
00395       // are within the loop. Without LoopSimplify form, it would be
00396       // necessary to insert a new phi.
00397       assert((!P->mustPreserveAnalysisID(LCSSAID) ||
00398               P->mustPreserveAnalysisID(LoopSimplifyID)) &&
00399              "SplitCriticalEdge doesn't know how to update LCCSA form "
00400              "without LoopSimplify!");
00401     }
00402   }
00403 
00404   // Update ProfileInfo if it is around.
00405   if (PI)
00406     PI->splitEdge(TIBB, DestBB, NewBB, MergeIdenticalEdges);
00407 
00408   return NewBB;
00409 }