1 //===- Dominators.cpp - Dominator Calculation -----------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements simple dominator construction algorithms for finding 11 // forward dominators. Postdominators are available in libanalysis, but are not 12 // included in libvmcore, because it's not needed. Forward dominators are 13 // needed to support the Verifier pass. 14 // 15 //===----------------------------------------------------------------------===// 16 17 #include "llvm/IR/Dominators.h" 18 #include "llvm/ADT/DepthFirstIterator.h" 19 #include "llvm/ADT/SmallPtrSet.h" 20 #include "llvm/ADT/SmallVector.h" 21 #include "llvm/IR/Instructions.h" 22 #include "llvm/Support/CFG.h" 23 #include "llvm/Support/CommandLine.h" 24 #include "llvm/Support/Compiler.h" 25 #include "llvm/Support/Debug.h" 26 #include "llvm/Support/GenericDomTreeConstruction.h" 27 #include "llvm/Support/raw_ostream.h" 28 #include <algorithm> 29 using namespace llvm; 30 31 // Always verify dominfo if expensive checking is enabled. 32 #ifdef XDEBUG 33 static bool VerifyDomInfo = true; 34 #else 35 static bool VerifyDomInfo = false; 36 #endif 37 static cl::opt<bool,true> 38 VerifyDomInfoX("verify-dom-info", cl::location(VerifyDomInfo), 39 cl::desc("Verify dominator info (time consuming)")); 40 41 bool BasicBlockEdge::isSingleEdge() const { 42 const TerminatorInst *TI = Start->getTerminator(); 43 unsigned NumEdgesToEnd = 0; 44 for (unsigned int i = 0, n = TI->getNumSuccessors(); i < n; ++i) { 45 if (TI->getSuccessor(i) == End) 46 ++NumEdgesToEnd; 47 if (NumEdgesToEnd >= 2) 48 return false; 49 } 50 assert(NumEdgesToEnd == 1); 51 return true; 52 } 53 54 //===----------------------------------------------------------------------===// 55 // DominatorTree Implementation 56 //===----------------------------------------------------------------------===// 57 // 58 // Provide public access to DominatorTree information. Implementation details 59 // can be found in Dominators.h, GenericDomTree.h, and 60 // GenericDomTreeConstruction.h. 61 // 62 //===----------------------------------------------------------------------===// 63 64 TEMPLATE_INSTANTIATION(class llvm::DomTreeNodeBase<BasicBlock>); 65 TEMPLATE_INSTANTIATION(class llvm::DominatorTreeBase<BasicBlock>); 66 67 // dominates - Return true if Def dominates a use in User. This performs 68 // the special checks necessary if Def and User are in the same basic block. 69 // Note that Def doesn't dominate a use in Def itself! 70 bool DominatorTree::dominates(const Instruction *Def, 71 const Instruction *User) const { 72 const BasicBlock *UseBB = User->getParent(); 73 const BasicBlock *DefBB = Def->getParent(); 74 75 // Any unreachable use is dominated, even if Def == User. 76 if (!isReachableFromEntry(UseBB)) 77 return true; 78 79 // Unreachable definitions don't dominate anything. 80 if (!isReachableFromEntry(DefBB)) 81 return false; 82 83 // An instruction doesn't dominate a use in itself. 84 if (Def == User) 85 return false; 86 87 // The value defined by an invoke dominates an instruction only if 88 // it dominates every instruction in UseBB. 89 // A PHI is dominated only if the instruction dominates every possible use 90 // in the UseBB. 91 if (isa<InvokeInst>(Def) || isa<PHINode>(User)) 92 return dominates(Def, UseBB); 93 94 if (DefBB != UseBB) 95 return dominates(DefBB, UseBB); 96 97 // Loop through the basic block until we find Def or User. 98 BasicBlock::const_iterator I = DefBB->begin(); 99 for (; &*I != Def && &*I != User; ++I) 100 /*empty*/; 101 102 return &*I == Def; 103 } 104 105 // true if Def would dominate a use in any instruction in UseBB. 106 // note that dominates(Def, Def->getParent()) is false. 107 bool DominatorTree::dominates(const Instruction *Def, 108 const BasicBlock *UseBB) const { 109 const BasicBlock *DefBB = Def->getParent(); 110 111 // Any unreachable use is dominated, even if DefBB == UseBB. 112 if (!isReachableFromEntry(UseBB)) 113 return true; 114 115 // Unreachable definitions don't dominate anything. 116 if (!isReachableFromEntry(DefBB)) 117 return false; 118 119 if (DefBB == UseBB) 120 return false; 121 122 const InvokeInst *II = dyn_cast<InvokeInst>(Def); 123 if (!II) 124 return dominates(DefBB, UseBB); 125 126 // Invoke results are only usable in the normal destination, not in the 127 // exceptional destination. 128 BasicBlock *NormalDest = II->getNormalDest(); 129 BasicBlockEdge E(DefBB, NormalDest); 130 return dominates(E, UseBB); 131 } 132 133 bool DominatorTree::dominates(const BasicBlockEdge &BBE, 134 const BasicBlock *UseBB) const { 135 // Assert that we have a single edge. We could handle them by simply 136 // returning false, but since isSingleEdge is linear on the number of 137 // edges, the callers can normally handle them more efficiently. 138 assert(BBE.isSingleEdge()); 139 140 // If the BB the edge ends in doesn't dominate the use BB, then the 141 // edge also doesn't. 142 const BasicBlock *Start = BBE.getStart(); 143 const BasicBlock *End = BBE.getEnd(); 144 if (!dominates(End, UseBB)) 145 return false; 146 147 // Simple case: if the end BB has a single predecessor, the fact that it 148 // dominates the use block implies that the edge also does. 149 if (End->getSinglePredecessor()) 150 return true; 151 152 // The normal edge from the invoke is critical. Conceptually, what we would 153 // like to do is split it and check if the new block dominates the use. 154 // With X being the new block, the graph would look like: 155 // 156 // DefBB 157 // /\ . . 158 // / \ . . 159 // / \ . . 160 // / \ | | 161 // A X B C 162 // | \ | / 163 // . \|/ 164 // . NormalDest 165 // . 166 // 167 // Given the definition of dominance, NormalDest is dominated by X iff X 168 // dominates all of NormalDest's predecessors (X, B, C in the example). X 169 // trivially dominates itself, so we only have to find if it dominates the 170 // other predecessors. Since the only way out of X is via NormalDest, X can 171 // only properly dominate a node if NormalDest dominates that node too. 172 for (const_pred_iterator PI = pred_begin(End), E = pred_end(End); 173 PI != E; ++PI) { 174 const BasicBlock *BB = *PI; 175 if (BB == Start) 176 continue; 177 178 if (!dominates(End, BB)) 179 return false; 180 } 181 return true; 182 } 183 184 bool DominatorTree::dominates(const BasicBlockEdge &BBE, const Use &U) const { 185 // Assert that we have a single edge. We could handle them by simply 186 // returning false, but since isSingleEdge is linear on the number of 187 // edges, the callers can normally handle them more efficiently. 188 assert(BBE.isSingleEdge()); 189 190 Instruction *UserInst = cast<Instruction>(U.getUser()); 191 // A PHI in the end of the edge is dominated by it. 192 PHINode *PN = dyn_cast<PHINode>(UserInst); 193 if (PN && PN->getParent() == BBE.getEnd() && 194 PN->getIncomingBlock(U) == BBE.getStart()) 195 return true; 196 197 // Otherwise use the edge-dominates-block query, which 198 // handles the crazy critical edge cases properly. 199 const BasicBlock *UseBB; 200 if (PN) 201 UseBB = PN->getIncomingBlock(U); 202 else 203 UseBB = UserInst->getParent(); 204 return dominates(BBE, UseBB); 205 } 206 207 bool DominatorTree::dominates(const Instruction *Def, const Use &U) const { 208 Instruction *UserInst = cast<Instruction>(U.getUser()); 209 const BasicBlock *DefBB = Def->getParent(); 210 211 // Determine the block in which the use happens. PHI nodes use 212 // their operands on edges; simulate this by thinking of the use 213 // happening at the end of the predecessor block. 214 const BasicBlock *UseBB; 215 if (PHINode *PN = dyn_cast<PHINode>(UserInst)) 216 UseBB = PN->getIncomingBlock(U); 217 else 218 UseBB = UserInst->getParent(); 219 220 // Any unreachable use is dominated, even if Def == User. 221 if (!isReachableFromEntry(UseBB)) 222 return true; 223 224 // Unreachable definitions don't dominate anything. 225 if (!isReachableFromEntry(DefBB)) 226 return false; 227 228 // Invoke instructions define their return values on the edges 229 // to their normal successors, so we have to handle them specially. 230 // Among other things, this means they don't dominate anything in 231 // their own block, except possibly a phi, so we don't need to 232 // walk the block in any case. 233 if (const InvokeInst *II = dyn_cast<InvokeInst>(Def)) { 234 BasicBlock *NormalDest = II->getNormalDest(); 235 BasicBlockEdge E(DefBB, NormalDest); 236 return dominates(E, U); 237 } 238 239 // If the def and use are in different blocks, do a simple CFG dominator 240 // tree query. 241 if (DefBB != UseBB) 242 return dominates(DefBB, UseBB); 243 244 // Ok, def and use are in the same block. If the def is an invoke, it 245 // doesn't dominate anything in the block. If it's a PHI, it dominates 246 // everything in the block. 247 if (isa<PHINode>(UserInst)) 248 return true; 249 250 // Otherwise, just loop through the basic block until we find Def or User. 251 BasicBlock::const_iterator I = DefBB->begin(); 252 for (; &*I != Def && &*I != UserInst; ++I) 253 /*empty*/; 254 255 return &*I != UserInst; 256 } 257 258 bool DominatorTree::isReachableFromEntry(const Use &U) const { 259 Instruction *I = dyn_cast<Instruction>(U.getUser()); 260 261 // ConstantExprs aren't really reachable from the entry block, but they 262 // don't need to be treated like unreachable code either. 263 if (!I) return true; 264 265 // PHI nodes use their operands on their incoming edges. 266 if (PHINode *PN = dyn_cast<PHINode>(I)) 267 return isReachableFromEntry(PN->getIncomingBlock(U)); 268 269 // Everything else uses their operands in their own block. 270 return isReachableFromEntry(I->getParent()); 271 } 272 273 void DominatorTree::verifyDomTree() const { 274 if (!VerifyDomInfo) 275 return; 276 277 Function &F = *getRoot()->getParent(); 278 279 DominatorTree OtherDT; 280 OtherDT.recalculate(F); 281 if (compare(OtherDT)) { 282 errs() << "DominatorTree is not up to date!\nComputed:\n"; 283 print(errs()); 284 errs() << "\nActual:\n"; 285 OtherDT.print(errs()); 286 abort(); 287 } 288 } 289 290 //===----------------------------------------------------------------------===// 291 // DominatorTreeWrapperPass Implementation 292 //===----------------------------------------------------------------------===// 293 // 294 // The implementation details of the wrapper pass that holds a DominatorTree. 295 // 296 //===----------------------------------------------------------------------===// 297 298 char DominatorTreeWrapperPass::ID = 0; 299 INITIALIZE_PASS(DominatorTreeWrapperPass, "domtree", 300 "Dominator Tree Construction", true, true) 301 302 bool DominatorTreeWrapperPass::runOnFunction(Function &F) { 303 DT.recalculate(F); 304 return false; 305 } 306 307 void DominatorTreeWrapperPass::verifyAnalysis() const { DT.verifyDomTree(); } 308 309 void DominatorTreeWrapperPass::print(raw_ostream &OS, const Module *) const { 310 DT.print(OS); 311 } 312 313