Chris Lattner | d43023a | 2002-08-02 16:43:03 +0000 | [diff] [blame] | 1 | //===- Dominators.cpp - Dominator Calculation -----------------------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 081aabc | 2001-07-02 05:46:38 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | d43023a | 2002-08-02 16:43:03 +0000 | [diff] [blame] | 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. |
Chris Lattner | 081aabc | 2001-07-02 05:46:38 +0000 | [diff] [blame] | 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Cameron Zwarich | 6b0c4c9 | 2011-01-18 06:06:27 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/Dominators.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DepthFirstIterator.h" |
Devang Patel | 5a1bd40 | 2007-03-27 20:50:46 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallPtrSet.h" |
Chris Lattner | c63d4c2 | 2007-08-08 05:51:24 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallVector.h" |
Owen Anderson | 8313e75 | 2007-10-03 21:25:45 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/DominatorInternals.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Instructions.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 23 | #include "llvm/Support/CFG.h" |
Dan Gohman | 4dbb301 | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 24 | #include "llvm/Support/CommandLine.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Compiler.h" |
| 26 | #include "llvm/Support/Debug.h" |
| 27 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | c5e0be6 | 2004-06-05 00:24:59 +0000 | [diff] [blame] | 28 | #include <algorithm> |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 29 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 30 | |
Dan Gohman | 4dbb301 | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 31 | // Always verify dominfo if expensive checking is enabled. |
| 32 | #ifdef XDEBUG |
Dan Gohman | b29cda9 | 2010-04-15 17:08:50 +0000 | [diff] [blame] | 33 | static bool VerifyDomInfo = true; |
Dan Gohman | 4dbb301 | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 34 | #else |
Dan Gohman | b29cda9 | 2010-04-15 17:08:50 +0000 | [diff] [blame] | 35 | static bool VerifyDomInfo = false; |
Dan Gohman | 4dbb301 | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 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 | |
Rafael Espindola | cc80cde | 2012-08-16 15:09:43 +0000 | [diff] [blame] | 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; |
Rafael Espindola | 1187077 | 2012-08-10 14:05:55 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Chris Lattner | c385beb | 2001-07-06 16:58:22 +0000 | [diff] [blame] | 54 | //===----------------------------------------------------------------------===// |
Owen Anderson | f35a1db | 2007-04-15 08:47:27 +0000 | [diff] [blame] | 55 | // DominatorTree Implementation |
Chris Lattner | 00f5167 | 2003-12-07 00:38:08 +0000 | [diff] [blame] | 56 | //===----------------------------------------------------------------------===// |
| 57 | // |
Owen Anderson | 84c357f | 2007-09-23 21:31:44 +0000 | [diff] [blame] | 58 | // Provide public access to DominatorTree information. Implementation details |
Cameron Zwarich | 050eec1 | 2011-01-20 03:58:43 +0000 | [diff] [blame] | 59 | // can be found in DominatorInternals.h. |
Chris Lattner | 00f5167 | 2003-12-07 00:38:08 +0000 | [diff] [blame] | 60 | // |
| 61 | //===----------------------------------------------------------------------===// |
| 62 | |
John McCall | 086bb4e | 2009-12-19 00:55:12 +0000 | [diff] [blame] | 63 | TEMPLATE_INSTANTIATION(class llvm::DomTreeNodeBase<BasicBlock>); |
| 64 | TEMPLATE_INSTANTIATION(class llvm::DominatorTreeBase<BasicBlock>); |
Owen Anderson | 4187801 | 2007-10-16 19:59:25 +0000 | [diff] [blame] | 65 | |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 66 | char DominatorTree::ID = 0; |
Owen Anderson | a57b97e | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 67 | INITIALIZE_PASS(DominatorTree, "domtree", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 68 | "Dominator Tree Construction", true, true) |
Chris Lattner | 00f5167 | 2003-12-07 00:38:08 +0000 | [diff] [blame] | 69 | |
Owen Anderson | f35a1db | 2007-04-15 08:47:27 +0000 | [diff] [blame] | 70 | bool DominatorTree::runOnFunction(Function &F) { |
Owen Anderson | 9c61411 | 2007-10-23 20:58:37 +0000 | [diff] [blame] | 71 | DT->recalculate(F); |
Owen Anderson | f35a1db | 2007-04-15 08:47:27 +0000 | [diff] [blame] | 72 | return false; |
| 73 | } |
Chris Lattner | 081aabc | 2001-07-02 05:46:38 +0000 | [diff] [blame] | 74 | |
Dan Gohman | 4dbb301 | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 75 | void DominatorTree::verifyAnalysis() const { |
Dan Gohman | 08d86da | 2009-09-28 00:44:15 +0000 | [diff] [blame] | 76 | if (!VerifyDomInfo) return; |
Dan Gohman | 4dbb301 | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 77 | |
| 78 | Function &F = *getRoot()->getParent(); |
| 79 | |
| 80 | DominatorTree OtherDT; |
| 81 | OtherDT.getBase().recalculate(F); |
Chris Lattner | 5f7734c | 2011-01-08 19:55:55 +0000 | [diff] [blame] | 82 | if (compare(OtherDT)) { |
Bill Wendling | 225d9b1 | 2011-03-29 04:28:26 +0000 | [diff] [blame] | 83 | errs() << "DominatorTree is not up to date!\nComputed:\n"; |
Chris Lattner | 5f7734c | 2011-01-08 19:55:55 +0000 | [diff] [blame] | 84 | print(errs()); |
Chris Lattner | 5f7734c | 2011-01-08 19:55:55 +0000 | [diff] [blame] | 85 | errs() << "\nActual:\n"; |
| 86 | OtherDT.print(errs()); |
| 87 | abort(); |
| 88 | } |
Dan Gohman | 4dbb301 | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Chris Lattner | 1362602 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 91 | void DominatorTree::print(raw_ostream &OS, const Module *) const { |
| 92 | DT->print(OS); |
Chris Lattner | b1d782b | 2009-08-23 05:17:37 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Rafael Espindola | 94df267 | 2012-02-26 02:19:19 +0000 | [diff] [blame] | 95 | // dominates - Return true if Def dominates a use in User. This performs |
| 96 | // the special checks necessary if Def and User are in the same basic block. |
| 97 | // Note that Def doesn't dominate a use in Def itself! |
| 98 | bool DominatorTree::dominates(const Instruction *Def, |
| 99 | const Instruction *User) const { |
| 100 | const BasicBlock *UseBB = User->getParent(); |
| 101 | const BasicBlock *DefBB = Def->getParent(); |
Rafael Espindola | 082d482 | 2012-02-18 19:46:02 +0000 | [diff] [blame] | 102 | |
Rafael Espindola | a53c46a | 2012-03-30 16:46:21 +0000 | [diff] [blame] | 103 | // Any unreachable use is dominated, even if Def == User. |
| 104 | if (!isReachableFromEntry(UseBB)) |
| 105 | return true; |
| 106 | |
| 107 | // Unreachable definitions don't dominate anything. |
| 108 | if (!isReachableFromEntry(DefBB)) |
| 109 | return false; |
Rafael Espindola | 082d482 | 2012-02-18 19:46:02 +0000 | [diff] [blame] | 110 | |
Rafael Espindola | 94df267 | 2012-02-26 02:19:19 +0000 | [diff] [blame] | 111 | // An instruction doesn't dominate a use in itself. |
| 112 | if (Def == User) |
Chris Lattner | 22151ce | 2009-09-21 22:30:50 +0000 | [diff] [blame] | 113 | return false; |
Rafael Espindola | 082d482 | 2012-02-18 19:46:02 +0000 | [diff] [blame] | 114 | |
Rafael Espindola | 94df267 | 2012-02-26 02:19:19 +0000 | [diff] [blame] | 115 | // The value defined by an invoke dominates an instruction only if |
| 116 | // it dominates every instruction in UseBB. |
| 117 | // A PHI is dominated only if the instruction dominates every possible use |
| 118 | // in the UseBB. |
| 119 | if (isa<InvokeInst>(Def) || isa<PHINode>(User)) |
| 120 | return dominates(Def, UseBB); |
| 121 | |
| 122 | if (DefBB != UseBB) |
| 123 | return dominates(DefBB, UseBB); |
| 124 | |
| 125 | // Loop through the basic block until we find Def or User. |
| 126 | BasicBlock::const_iterator I = DefBB->begin(); |
| 127 | for (; &*I != Def && &*I != User; ++I) |
Chris Lattner | 22151ce | 2009-09-21 22:30:50 +0000 | [diff] [blame] | 128 | /*empty*/; |
Rafael Espindola | 082d482 | 2012-02-18 19:46:02 +0000 | [diff] [blame] | 129 | |
Rafael Espindola | 94df267 | 2012-02-26 02:19:19 +0000 | [diff] [blame] | 130 | return &*I == Def; |
| 131 | } |
| 132 | |
| 133 | // true if Def would dominate a use in any instruction in UseBB. |
| 134 | // note that dominates(Def, Def->getParent()) is false. |
| 135 | bool DominatorTree::dominates(const Instruction *Def, |
| 136 | const BasicBlock *UseBB) const { |
| 137 | const BasicBlock *DefBB = Def->getParent(); |
| 138 | |
Rafael Espindola | a53c46a | 2012-03-30 16:46:21 +0000 | [diff] [blame] | 139 | // Any unreachable use is dominated, even if DefBB == UseBB. |
| 140 | if (!isReachableFromEntry(UseBB)) |
| 141 | return true; |
| 142 | |
| 143 | // Unreachable definitions don't dominate anything. |
| 144 | if (!isReachableFromEntry(DefBB)) |
| 145 | return false; |
Rafael Espindola | 94df267 | 2012-02-26 02:19:19 +0000 | [diff] [blame] | 146 | |
| 147 | if (DefBB == UseBB) |
| 148 | return false; |
| 149 | |
| 150 | const InvokeInst *II = dyn_cast<InvokeInst>(Def); |
| 151 | if (!II) |
| 152 | return dominates(DefBB, UseBB); |
| 153 | |
| 154 | // Invoke results are only usable in the normal destination, not in the |
| 155 | // exceptional destination. |
| 156 | BasicBlock *NormalDest = II->getNormalDest(); |
Rafael Espindola | 5956407 | 2012-08-07 17:30:46 +0000 | [diff] [blame] | 157 | BasicBlockEdge E(DefBB, NormalDest); |
| 158 | return dominates(E, UseBB); |
| 159 | } |
| 160 | |
| 161 | bool DominatorTree::dominates(const BasicBlockEdge &BBE, |
| 162 | const BasicBlock *UseBB) const { |
Rafael Espindola | 9a16735 | 2012-08-17 18:21:28 +0000 | [diff] [blame] | 163 | // Assert that we have a single edge. We could handle them by simply |
| 164 | // returning false, but since isSingleEdge is linear on the number of |
| 165 | // edges, the callers can normally handle them more efficiently. |
| 166 | assert(BBE.isSingleEdge()); |
| 167 | |
Rafael Espindola | 5956407 | 2012-08-07 17:30:46 +0000 | [diff] [blame] | 168 | // If the BB the edge ends in doesn't dominate the use BB, then the |
| 169 | // edge also doesn't. |
| 170 | const BasicBlock *Start = BBE.getStart(); |
| 171 | const BasicBlock *End = BBE.getEnd(); |
| 172 | if (!dominates(End, UseBB)) |
Rafael Espindola | 94df267 | 2012-02-26 02:19:19 +0000 | [diff] [blame] | 173 | return false; |
| 174 | |
Rafael Espindola | 5956407 | 2012-08-07 17:30:46 +0000 | [diff] [blame] | 175 | // Simple case: if the end BB has a single predecessor, the fact that it |
| 176 | // dominates the use block implies that the edge also does. |
| 177 | if (End->getSinglePredecessor()) |
Rafael Espindola | 94df267 | 2012-02-26 02:19:19 +0000 | [diff] [blame] | 178 | return true; |
| 179 | |
| 180 | // The normal edge from the invoke is critical. Conceptually, what we would |
| 181 | // like to do is split it and check if the new block dominates the use. |
| 182 | // With X being the new block, the graph would look like: |
| 183 | // |
| 184 | // DefBB |
| 185 | // /\ . . |
| 186 | // / \ . . |
| 187 | // / \ . . |
| 188 | // / \ | | |
| 189 | // A X B C |
| 190 | // | \ | / |
| 191 | // . \|/ |
| 192 | // . NormalDest |
| 193 | // . |
| 194 | // |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 195 | // Given the definition of dominance, NormalDest is dominated by X iff X |
Rafael Espindola | 94df267 | 2012-02-26 02:19:19 +0000 | [diff] [blame] | 196 | // dominates all of NormalDest's predecessors (X, B, C in the example). X |
| 197 | // trivially dominates itself, so we only have to find if it dominates the |
| 198 | // other predecessors. Since the only way out of X is via NormalDest, X can |
| 199 | // only properly dominate a node if NormalDest dominates that node too. |
Rafael Espindola | 5956407 | 2012-08-07 17:30:46 +0000 | [diff] [blame] | 200 | for (const_pred_iterator PI = pred_begin(End), E = pred_end(End); |
| 201 | PI != E; ++PI) { |
Rafael Espindola | 94df267 | 2012-02-26 02:19:19 +0000 | [diff] [blame] | 202 | const BasicBlock *BB = *PI; |
Rafael Espindola | 5956407 | 2012-08-07 17:30:46 +0000 | [diff] [blame] | 203 | if (BB == Start) |
Rafael Espindola | 94df267 | 2012-02-26 02:19:19 +0000 | [diff] [blame] | 204 | continue; |
| 205 | |
Rafael Espindola | 5956407 | 2012-08-07 17:30:46 +0000 | [diff] [blame] | 206 | if (!dominates(End, BB)) |
Rafael Espindola | 94df267 | 2012-02-26 02:19:19 +0000 | [diff] [blame] | 207 | return false; |
| 208 | } |
| 209 | return true; |
Chris Lattner | 22151ce | 2009-09-21 22:30:50 +0000 | [diff] [blame] | 210 | } |
Dan Gohman | 7327327 | 2012-04-12 23:31:46 +0000 | [diff] [blame] | 211 | |
Rafael Espindola | 5956407 | 2012-08-07 17:30:46 +0000 | [diff] [blame] | 212 | bool DominatorTree::dominates(const BasicBlockEdge &BBE, |
| 213 | const Use &U) const { |
Rafael Espindola | 9a16735 | 2012-08-17 18:21:28 +0000 | [diff] [blame] | 214 | // Assert that we have a single edge. We could handle them by simply |
| 215 | // returning false, but since isSingleEdge is linear on the number of |
| 216 | // edges, the callers can normally handle them more efficiently. |
| 217 | assert(BBE.isSingleEdge()); |
| 218 | |
Rafael Espindola | 5956407 | 2012-08-07 17:30:46 +0000 | [diff] [blame] | 219 | Instruction *UserInst = cast<Instruction>(U.getUser()); |
| 220 | // A PHI in the end of the edge is dominated by it. |
| 221 | PHINode *PN = dyn_cast<PHINode>(UserInst); |
| 222 | if (PN && PN->getParent() == BBE.getEnd() && |
| 223 | PN->getIncomingBlock(U) == BBE.getStart()) |
| 224 | return true; |
| 225 | |
| 226 | // Otherwise use the edge-dominates-block query, which |
| 227 | // handles the crazy critical edge cases properly. |
| 228 | const BasicBlock *UseBB; |
| 229 | if (PN) |
| 230 | UseBB = PN->getIncomingBlock(U); |
| 231 | else |
| 232 | UseBB = UserInst->getParent(); |
| 233 | return dominates(BBE, UseBB); |
| 234 | } |
| 235 | |
Dan Gohman | 7327327 | 2012-04-12 23:31:46 +0000 | [diff] [blame] | 236 | bool DominatorTree::dominates(const Instruction *Def, |
| 237 | const Use &U) const { |
Rafael Espindola | 5956407 | 2012-08-07 17:30:46 +0000 | [diff] [blame] | 238 | Instruction *UserInst = cast<Instruction>(U.getUser()); |
Dan Gohman | 7327327 | 2012-04-12 23:31:46 +0000 | [diff] [blame] | 239 | const BasicBlock *DefBB = Def->getParent(); |
| 240 | |
| 241 | // Determine the block in which the use happens. PHI nodes use |
| 242 | // their operands on edges; simulate this by thinking of the use |
| 243 | // happening at the end of the predecessor block. |
| 244 | const BasicBlock *UseBB; |
| 245 | if (PHINode *PN = dyn_cast<PHINode>(UserInst)) |
| 246 | UseBB = PN->getIncomingBlock(U); |
| 247 | else |
| 248 | UseBB = UserInst->getParent(); |
| 249 | |
| 250 | // Any unreachable use is dominated, even if Def == User. |
| 251 | if (!isReachableFromEntry(UseBB)) |
| 252 | return true; |
| 253 | |
| 254 | // Unreachable definitions don't dominate anything. |
| 255 | if (!isReachableFromEntry(DefBB)) |
| 256 | return false; |
| 257 | |
| 258 | // Invoke instructions define their return values on the edges |
| 259 | // to their normal successors, so we have to handle them specially. |
| 260 | // Among other things, this means they don't dominate anything in |
| 261 | // their own block, except possibly a phi, so we don't need to |
| 262 | // walk the block in any case. |
| 263 | if (const InvokeInst *II = dyn_cast<InvokeInst>(Def)) { |
Rafael Espindola | 5956407 | 2012-08-07 17:30:46 +0000 | [diff] [blame] | 264 | BasicBlock *NormalDest = II->getNormalDest(); |
| 265 | BasicBlockEdge E(DefBB, NormalDest); |
| 266 | return dominates(E, U); |
Dan Gohman | 7327327 | 2012-04-12 23:31:46 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | // If the def and use are in different blocks, do a simple CFG dominator |
| 270 | // tree query. |
| 271 | if (DefBB != UseBB) |
| 272 | return dominates(DefBB, UseBB); |
| 273 | |
| 274 | // Ok, def and use are in the same block. If the def is an invoke, it |
| 275 | // doesn't dominate anything in the block. If it's a PHI, it dominates |
| 276 | // everything in the block. |
| 277 | if (isa<PHINode>(UserInst)) |
| 278 | return true; |
| 279 | |
| 280 | // Otherwise, just loop through the basic block until we find Def or User. |
| 281 | BasicBlock::const_iterator I = DefBB->begin(); |
| 282 | for (; &*I != Def && &*I != UserInst; ++I) |
| 283 | /*empty*/; |
| 284 | |
| 285 | return &*I != UserInst; |
| 286 | } |
| 287 | |
| 288 | bool DominatorTree::isReachableFromEntry(const Use &U) const { |
| 289 | Instruction *I = dyn_cast<Instruction>(U.getUser()); |
| 290 | |
| 291 | // ConstantExprs aren't really reachable from the entry block, but they |
| 292 | // don't need to be treated like unreachable code either. |
| 293 | if (!I) return true; |
| 294 | |
| 295 | // PHI nodes use their operands on their incoming edges. |
| 296 | if (PHINode *PN = dyn_cast<PHINode>(I)) |
| 297 | return isReachableFromEntry(PN->getIncomingBlock(U)); |
| 298 | |
| 299 | // Everything else uses their operands in their own block. |
| 300 | return isReachableFromEntry(I->getParent()); |
| 301 | } |