LLVM API Documentation
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 // forward dominator (set, immediate dominators, tree, and frontier) 00015 // information. 00016 // 00017 //===----------------------------------------------------------------------===// 00018 00019 #define DEBUG_TYPE "break-crit-edges" 00020 #include "llvm/Transforms/Scalar.h" 00021 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 00022 #include "llvm/Analysis/Dominators.h" 00023 #include "llvm/Analysis/LoopInfo.h" 00024 #include "llvm/Function.h" 00025 #include "llvm/Instructions.h" 00026 #include "llvm/Type.h" 00027 #include "llvm/Support/CFG.h" 00028 #include "llvm/Support/Compiler.h" 00029 #include "llvm/ADT/SmallVector.h" 00030 #include "llvm/ADT/Statistic.h" 00031 using namespace llvm; 00032 00033 STATISTIC(NumBroken, "Number of blocks inserted"); 00034 00035 namespace { 00036 struct VISIBILITY_HIDDEN BreakCriticalEdges : public FunctionPass { 00037 static char ID; // Pass identification, replacement for typeid 00038 BreakCriticalEdges() : FunctionPass(&ID) {} 00039 00040 virtual bool runOnFunction(Function &F); 00041 00042 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 00043 AU.addPreserved<DominatorTree>(); 00044 AU.addPreserved<DominanceFrontier>(); 00045 AU.addPreserved<LoopInfo>(); 00046 00047 // No loop canonicalization guarantees are broken by this pass. 00048 AU.addPreservedID(LoopSimplifyID); 00049 } 00050 }; 00051 } 00052 00053 char BreakCriticalEdges::ID = 0; 00054 static RegisterPass<BreakCriticalEdges> 00055 X("break-crit-edges", "Break critical edges in CFG"); 00056 00057 // Publically exposed interface to pass... 00058 const PassInfo *const llvm::BreakCriticalEdgesID = &X; 00059 FunctionPass *llvm::createBreakCriticalEdgesPass() { 00060 return new BreakCriticalEdges(); 00061 } 00062 00063 // runOnFunction - Loop over all of the edges in the CFG, breaking critical 00064 // edges as they are found. 00065 // 00066 bool BreakCriticalEdges::runOnFunction(Function &F) { 00067 bool Changed = false; 00068 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) { 00069 TerminatorInst *TI = I->getTerminator(); 00070 if (TI->getNumSuccessors() > 1) 00071 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) 00072 if (SplitCriticalEdge(TI, i, this)) { 00073 ++NumBroken; 00074 Changed = true; 00075 } 00076 } 00077 00078 return Changed; 00079 } 00080 00081 //===----------------------------------------------------------------------===// 00082 // Implementation of the external critical edge manipulation functions 00083 //===----------------------------------------------------------------------===// 00084 00085 // isCriticalEdge - Return true if the specified edge is a critical edge. 00086 // Critical edges are edges from a block with multiple successors to a block 00087 // with multiple predecessors. 00088 // 00089 bool llvm::isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum, 00090 bool AllowIdenticalEdges) { 00091 assert(SuccNum < TI->getNumSuccessors() && "Illegal edge specification!"); 00092 if (TI->getNumSuccessors() == 1) return false; 00093 00094 const BasicBlock *Dest = TI->getSuccessor(SuccNum); 00095 pred_const_iterator I = pred_begin(Dest), E = pred_end(Dest); 00096 00097 // If there is more than one predecessor, this is a critical edge... 00098 assert(I != E && "No preds, but we have an edge to the block?"); 00099 const BasicBlock *FirstPred = *I; 00100 ++I; // Skip one edge due to the incoming arc from TI. 00101 if (!AllowIdenticalEdges) 00102 return I != E; 00103 00104 // If AllowIdenticalEdges is true, then we allow this edge to be considered 00105 // non-critical iff all preds come from TI's block. 00106 while (I != E) { 00107 if (*I != FirstPred) 00108 return true; 00109 // Note: leave this as is until no one ever compiles with either gcc 4.0.1 00110 // or Xcode 2. This seems to work around the pred_iterator assert in PR 2207 00111 E = pred_end(*I); 00112 ++I; 00113 } 00114 return false; 00115 } 00116 00117 /// SplitCriticalEdge - If this edge is a critical edge, insert a new node to 00118 /// split the critical edge. This will update DominatorTree and 00119 /// DominatorFrontier information if it is available, thus calling this pass 00120 /// will not invalidate any of them. This returns true if the edge was split, 00121 /// false otherwise. This ensures that all edges to that dest go to one block 00122 /// instead of each going to a different block. 00123 // 00124 bool llvm::SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, Pass *P, 00125 bool MergeIdenticalEdges) { 00126 if (!isCriticalEdge(TI, SuccNum, MergeIdenticalEdges)) return false; 00127 BasicBlock *TIBB = TI->getParent(); 00128 BasicBlock *DestBB = TI->getSuccessor(SuccNum); 00129 00130 // Create a new basic block, linking it into the CFG. 00131 BasicBlock *NewBB = BasicBlock::Create(TIBB->getName() + "." + 00132 DestBB->getName() + "_crit_edge"); 00133 // Create our unconditional branch... 00134 BranchInst::Create(DestBB, NewBB); 00135 00136 // Branch to the new block, breaking the edge. 00137 TI->setSuccessor(SuccNum, NewBB); 00138 00139 // Insert the block into the function... right after the block TI lives in. 00140 Function &F = *TIBB->getParent(); 00141 Function::iterator FBBI = TIBB; 00142 F.getBasicBlockList().insert(++FBBI, NewBB); 00143 00144 // If there are any PHI nodes in DestBB, we need to update them so that they 00145 // merge incoming values from NewBB instead of from TIBB. 00146 // 00147 for (BasicBlock::iterator I = DestBB->begin(); isa<PHINode>(I); ++I) { 00148 PHINode *PN = cast<PHINode>(I); 00149 // We no longer enter through TIBB, now we come in through NewBB. Revector 00150 // exactly one entry in the PHI node that used to come from TIBB to come 00151 // from NewBB. 00152 int BBIdx = PN->getBasicBlockIndex(TIBB); 00153 PN->setIncomingBlock(BBIdx, NewBB); 00154 } 00155 00156 // If there are any other edges from TIBB to DestBB, update those to go 00157 // through the split block, making those edges non-critical as well (and 00158 // reducing the number of phi entries in the DestBB if relevant). 00159 if (MergeIdenticalEdges) { 00160 for (unsigned i = SuccNum+1, e = TI->getNumSuccessors(); i != e; ++i) { 00161 if (TI->getSuccessor(i) != DestBB) continue; 00162 00163 // Remove an entry for TIBB from DestBB phi nodes. 00164 DestBB->removePredecessor(TIBB); 00165 00166 // We found another edge to DestBB, go to NewBB instead. 00167 TI->setSuccessor(i, NewBB); 00168 } 00169 } 00170 00171 00172 00173 // If we don't have a pass object, we can't update anything... 00174 if (P == 0) return true; 00175 00176 // Now update analysis information. Since the only predecessor of NewBB is 00177 // the TIBB, TIBB clearly dominates NewBB. TIBB usually doesn't dominate 00178 // anything, as there are other successors of DestBB. However, if all other 00179 // predecessors of DestBB are already dominated by DestBB (e.g. DestBB is a 00180 // loop header) then NewBB dominates DestBB. 00181 SmallVector<BasicBlock*, 8> OtherPreds; 00182 00183 for (pred_iterator I = pred_begin(DestBB), E = pred_end(DestBB); I != E; ++I) 00184 if (*I != NewBB) 00185 OtherPreds.push_back(*I); 00186 00187 bool NewBBDominatesDestBB = true; 00188 00189 // Should we update DominatorTree information? 00190 if (DominatorTree *DT = P->getAnalysisIfAvailable<DominatorTree>()) { 00191 DomTreeNode *TINode = DT->getNode(TIBB); 00192 00193 // The new block is not the immediate dominator for any other nodes, but 00194 // TINode is the immediate dominator for the new node. 00195 // 00196 if (TINode) { // Don't break unreachable code! 00197 DomTreeNode *NewBBNode = DT->addNewBlock(NewBB, TIBB); 00198 DomTreeNode *DestBBNode = 0; 00199 00200 // If NewBBDominatesDestBB hasn't been computed yet, do so with DT. 00201 if (!OtherPreds.empty()) { 00202 DestBBNode = DT->getNode(DestBB); 00203 while (!OtherPreds.empty() && NewBBDominatesDestBB) { 00204 if (DomTreeNode *OPNode = DT->getNode(OtherPreds.back())) 00205 NewBBDominatesDestBB = DT->dominates(DestBBNode, OPNode); 00206 OtherPreds.pop_back(); 00207 } 00208 OtherPreds.clear(); 00209 } 00210 00211 // If NewBBDominatesDestBB, then NewBB dominates DestBB, otherwise it 00212 // doesn't dominate anything. 00213 if (NewBBDominatesDestBB) { 00214 if (!DestBBNode) DestBBNode = DT->getNode(DestBB); 00215 DT->changeImmediateDominator(DestBBNode, NewBBNode); 00216 } 00217 } 00218 } 00219 00220 // Should we update DominanceFrontier information? 00221 if (DominanceFrontier *DF = P->getAnalysisIfAvailable<DominanceFrontier>()) { 00222 // If NewBBDominatesDestBB hasn't been computed yet, do so with DF. 00223 if (!OtherPreds.empty()) { 00224 // FIXME: IMPLEMENT THIS! 00225 assert(0 && "Requiring domfrontiers but not idom/domtree/domset." 00226 " not implemented yet!"); 00227 } 00228 00229 // Since the new block is dominated by its only predecessor TIBB, 00230 // it cannot be in any block's dominance frontier. If NewBB dominates 00231 // DestBB, its dominance frontier is the same as DestBB's, otherwise it is 00232 // just {DestBB}. 00233 DominanceFrontier::DomSetType NewDFSet; 00234 if (NewBBDominatesDestBB) { 00235 DominanceFrontier::iterator I = DF->find(DestBB); 00236 if (I != DF->end()) { 00237 DF->addBasicBlock(NewBB, I->second); 00238 00239 if (I->second.count(DestBB)) { 00240 // However NewBB's frontier does not include DestBB. 00241 DominanceFrontier::iterator NF = DF->find(NewBB); 00242 DF->removeFromFrontier(NF, DestBB); 00243 } 00244 } 00245 else 00246 DF->addBasicBlock(NewBB, DominanceFrontier::DomSetType()); 00247 } else { 00248 DominanceFrontier::DomSetType NewDFSet; 00249 NewDFSet.insert(DestBB); 00250 DF->addBasicBlock(NewBB, NewDFSet); 00251 } 00252 } 00253 00254 // Update LoopInfo if it is around. 00255 if (LoopInfo *LI = P->getAnalysisIfAvailable<LoopInfo>()) { 00256 // If one or the other blocks were not in a loop, the new block is not 00257 // either, and thus LI doesn't need to be updated. 00258 if (Loop *TIL = LI->getLoopFor(TIBB)) 00259 if (Loop *DestLoop = LI->getLoopFor(DestBB)) { 00260 if (TIL == DestLoop) { 00261 // Both in the same loop, the NewBB joins loop. 00262 DestLoop->addBasicBlockToLoop(NewBB, LI->getBase()); 00263 } else if (TIL->contains(DestLoop->getHeader())) { 00264 // Edge from an outer loop to an inner loop. Add to the outer loop. 00265 TIL->addBasicBlockToLoop(NewBB, LI->getBase()); 00266 } else if (DestLoop->contains(TIL->getHeader())) { 00267 // Edge from an inner loop to an outer loop. Add to the outer loop. 00268 DestLoop->addBasicBlockToLoop(NewBB, LI->getBase()); 00269 } else { 00270 // Edge from two loops with no containment relation. Because these 00271 // are natural loops, we know that the destination block must be the 00272 // header of its loop (adding a branch into a loop elsewhere would 00273 // create an irreducible loop). 00274 assert(DestLoop->getHeader() == DestBB && 00275 "Should not create irreducible loops!"); 00276 if (Loop *P = DestLoop->getParentLoop()) 00277 P->addBasicBlockToLoop(NewBB, LI->getBase()); 00278 } 00279 } 00280 } 00281 return true; 00282 }