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 | |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 17 | #include "llvm/IR/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" |
Nico Weber | 432a388 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 20 | #include "llvm/Config/llvm-config.h" |
Chandler Carruth | 1305dc3 | 2014-03-04 11:45:46 +0000 | [diff] [blame] | 21 | #include "llvm/IR/CFG.h" |
Brian M. Rzycki | 9b7ae23 | 2018-01-12 21:06:48 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Constants.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Instructions.h" |
Chandler Carruth | 64764b4 | 2015-01-14 10:19:28 +0000 | [diff] [blame] | 24 | #include "llvm/IR/PassManager.h" |
Dan Gohman | 4dbb301 | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CommandLine.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Debug.h" |
Chandler Carruth | e509db4 | 2014-01-13 10:52:56 +0000 | [diff] [blame] | 27 | #include "llvm/Support/GenericDomTreeConstruction.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | c5e0be6 | 2004-06-05 00:24:59 +0000 | [diff] [blame] | 29 | #include <algorithm> |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 30 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 31 | |
Serge Pavlov | 69b3ff9 | 2017-01-24 05:52:07 +0000 | [diff] [blame] | 32 | bool llvm::VerifyDomInfo = false; |
Zachary Turner | 8065f0b | 2017-12-01 00:53:10 +0000 | [diff] [blame] | 33 | static cl::opt<bool, true> |
| 34 | VerifyDomInfoX("verify-dom-info", cl::location(VerifyDomInfo), cl::Hidden, |
| 35 | cl::desc("Verify dominator info (time consuming)")); |
Dan Gohman | 4dbb301 | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 36 | |
Jakub Kuderski | ffb4fb7 | 2018-01-24 02:40:35 +0000 | [diff] [blame] | 37 | #ifdef EXPENSIVE_CHECKS |
| 38 | static constexpr bool ExpensiveChecksEnabled = true; |
| 39 | #else |
| 40 | static constexpr bool ExpensiveChecksEnabled = false; |
| 41 | #endif |
| 42 | |
Rafael Espindola | cc80cde | 2012-08-16 15:09:43 +0000 | [diff] [blame] | 43 | bool BasicBlockEdge::isSingleEdge() const { |
Chandler Carruth | edb12a8 | 2018-10-15 10:04:59 +0000 | [diff] [blame] | 44 | const Instruction *TI = Start->getTerminator(); |
Rafael Espindola | cc80cde | 2012-08-16 15:09:43 +0000 | [diff] [blame] | 45 | unsigned NumEdgesToEnd = 0; |
| 46 | for (unsigned int i = 0, n = TI->getNumSuccessors(); i < n; ++i) { |
| 47 | if (TI->getSuccessor(i) == End) |
| 48 | ++NumEdgesToEnd; |
| 49 | if (NumEdgesToEnd >= 2) |
| 50 | return false; |
| 51 | } |
| 52 | assert(NumEdgesToEnd == 1); |
| 53 | return true; |
Rafael Espindola | 1187077 | 2012-08-10 14:05:55 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Chris Lattner | c385beb | 2001-07-06 16:58:22 +0000 | [diff] [blame] | 56 | //===----------------------------------------------------------------------===// |
Owen Anderson | f35a1db | 2007-04-15 08:47:27 +0000 | [diff] [blame] | 57 | // DominatorTree Implementation |
Chris Lattner | 00f5167 | 2003-12-07 00:38:08 +0000 | [diff] [blame] | 58 | //===----------------------------------------------------------------------===// |
| 59 | // |
Owen Anderson | 84c357f | 2007-09-23 21:31:44 +0000 | [diff] [blame] | 60 | // Provide public access to DominatorTree information. Implementation details |
Chandler Carruth | e509db4 | 2014-01-13 10:52:56 +0000 | [diff] [blame] | 61 | // can be found in Dominators.h, GenericDomTree.h, and |
| 62 | // GenericDomTreeConstruction.h. |
Chris Lattner | 00f5167 | 2003-12-07 00:38:08 +0000 | [diff] [blame] | 63 | // |
| 64 | //===----------------------------------------------------------------------===// |
| 65 | |
Benjamin Kramer | a667d1a | 2015-07-13 17:21:31 +0000 | [diff] [blame] | 66 | template class llvm::DomTreeNodeBase<BasicBlock>; |
Jakub Kuderski | b292c22 | 2017-07-14 18:26:09 +0000 | [diff] [blame] | 67 | template class llvm::DominatorTreeBase<BasicBlock, false>; // DomTreeBase |
| 68 | template class llvm::DominatorTreeBase<BasicBlock, true>; // PostDomTreeBase |
Owen Anderson | 4187801 | 2007-10-16 19:59:25 +0000 | [diff] [blame] | 69 | |
Alina Sbirlea | 148c445 | 2018-08-14 17:12:30 +0000 | [diff] [blame] | 70 | template class llvm::cfg::Update<BasicBlock *>; |
Jakub Kuderski | 624463a | 2017-08-16 16:12:52 +0000 | [diff] [blame] | 71 | |
Jakub Kuderski | c271dea | 2017-07-26 18:07:40 +0000 | [diff] [blame] | 72 | template void llvm::DomTreeBuilder::Calculate<DomTreeBuilder::BBDomTree>( |
| 73 | DomTreeBuilder::BBDomTree &DT); |
Alina Sbirlea | d4b3f19 | 2018-08-16 21:54:33 +0000 | [diff] [blame] | 74 | template void |
| 75 | llvm::DomTreeBuilder::CalculateWithUpdates<DomTreeBuilder::BBDomTree>( |
| 76 | DomTreeBuilder::BBDomTree &DT, BBUpdates U); |
| 77 | |
Jakub Kuderski | c271dea | 2017-07-26 18:07:40 +0000 | [diff] [blame] | 78 | template void llvm::DomTreeBuilder::Calculate<DomTreeBuilder::BBPostDomTree>( |
| 79 | DomTreeBuilder::BBPostDomTree &DT); |
Alina Sbirlea | d4b3f19 | 2018-08-16 21:54:33 +0000 | [diff] [blame] | 80 | // No CalculateWithUpdates<PostDomTree> instantiation, unless a usecase arises. |
Jakub Kuderski | 5af07f5 | 2017-07-13 20:45:32 +0000 | [diff] [blame] | 81 | |
Jakub Kuderski | 13e9ef1 | 2017-07-14 21:17:33 +0000 | [diff] [blame] | 82 | template void llvm::DomTreeBuilder::InsertEdge<DomTreeBuilder::BBDomTree>( |
| 83 | DomTreeBuilder::BBDomTree &DT, BasicBlock *From, BasicBlock *To); |
| 84 | template void llvm::DomTreeBuilder::InsertEdge<DomTreeBuilder::BBPostDomTree>( |
| 85 | DomTreeBuilder::BBPostDomTree &DT, BasicBlock *From, BasicBlock *To); |
| 86 | |
Jakub Kuderski | eb59ff2 | 2017-07-14 21:58:53 +0000 | [diff] [blame] | 87 | template void llvm::DomTreeBuilder::DeleteEdge<DomTreeBuilder::BBDomTree>( |
| 88 | DomTreeBuilder::BBDomTree &DT, BasicBlock *From, BasicBlock *To); |
| 89 | template void llvm::DomTreeBuilder::DeleteEdge<DomTreeBuilder::BBPostDomTree>( |
| 90 | DomTreeBuilder::BBPostDomTree &DT, BasicBlock *From, BasicBlock *To); |
| 91 | |
Jakub Kuderski | 624463a | 2017-08-16 16:12:52 +0000 | [diff] [blame] | 92 | template void llvm::DomTreeBuilder::ApplyUpdates<DomTreeBuilder::BBDomTree>( |
| 93 | DomTreeBuilder::BBDomTree &DT, DomTreeBuilder::BBUpdates); |
| 94 | template void llvm::DomTreeBuilder::ApplyUpdates<DomTreeBuilder::BBPostDomTree>( |
| 95 | DomTreeBuilder::BBPostDomTree &DT, DomTreeBuilder::BBUpdates); |
| 96 | |
Jakub Kuderski | 5af07f5 | 2017-07-13 20:45:32 +0000 | [diff] [blame] | 97 | template bool llvm::DomTreeBuilder::Verify<DomTreeBuilder::BBDomTree>( |
Jakub Kuderski | ffb4fb7 | 2018-01-24 02:40:35 +0000 | [diff] [blame] | 98 | const DomTreeBuilder::BBDomTree &DT, |
| 99 | DomTreeBuilder::BBDomTree::VerificationLevel VL); |
Jakub Kuderski | b292c22 | 2017-07-14 18:26:09 +0000 | [diff] [blame] | 100 | template bool llvm::DomTreeBuilder::Verify<DomTreeBuilder::BBPostDomTree>( |
Jakub Kuderski | ffb4fb7 | 2018-01-24 02:40:35 +0000 | [diff] [blame] | 101 | const DomTreeBuilder::BBPostDomTree &DT, |
| 102 | DomTreeBuilder::BBPostDomTree::VerificationLevel VL); |
Rafael Espindola | 3061636 | 2014-02-14 22:36:16 +0000 | [diff] [blame] | 103 | |
Chandler Carruth | ca68a3e | 2017-01-15 06:32:49 +0000 | [diff] [blame] | 104 | bool DominatorTree::invalidate(Function &F, const PreservedAnalyses &PA, |
| 105 | FunctionAnalysisManager::Invalidator &) { |
| 106 | // Check whether the analysis, all analyses on functions, or the function's |
| 107 | // CFG have been preserved. |
| 108 | auto PAC = PA.getChecker<DominatorTreeAnalysis>(); |
| 109 | return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() || |
| 110 | PAC.preservedSet<CFGAnalyses>()); |
| 111 | } |
| 112 | |
Rafael Espindola | 94df267 | 2012-02-26 02:19:19 +0000 | [diff] [blame] | 113 | // dominates - Return true if Def dominates a use in User. This performs |
| 114 | // the special checks necessary if Def and User are in the same basic block. |
| 115 | // Note that Def doesn't dominate a use in Def itself! |
| 116 | bool DominatorTree::dominates(const Instruction *Def, |
| 117 | const Instruction *User) const { |
| 118 | const BasicBlock *UseBB = User->getParent(); |
| 119 | const BasicBlock *DefBB = Def->getParent(); |
Rafael Espindola | 082d482 | 2012-02-18 19:46:02 +0000 | [diff] [blame] | 120 | |
Rafael Espindola | a53c46a | 2012-03-30 16:46:21 +0000 | [diff] [blame] | 121 | // Any unreachable use is dominated, even if Def == User. |
| 122 | if (!isReachableFromEntry(UseBB)) |
| 123 | return true; |
| 124 | |
| 125 | // Unreachable definitions don't dominate anything. |
| 126 | if (!isReachableFromEntry(DefBB)) |
| 127 | return false; |
Rafael Espindola | 082d482 | 2012-02-18 19:46:02 +0000 | [diff] [blame] | 128 | |
Rafael Espindola | 94df267 | 2012-02-26 02:19:19 +0000 | [diff] [blame] | 129 | // An instruction doesn't dominate a use in itself. |
| 130 | if (Def == User) |
Chris Lattner | 22151ce | 2009-09-21 22:30:50 +0000 | [diff] [blame] | 131 | return false; |
Rafael Espindola | 082d482 | 2012-02-18 19:46:02 +0000 | [diff] [blame] | 132 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 133 | // The value defined by an invoke dominates an instruction only if it |
| 134 | // dominates every instruction in UseBB. |
| 135 | // A PHI is dominated only if the instruction dominates every possible use in |
| 136 | // the UseBB. |
| 137 | if (isa<InvokeInst>(Def) || isa<PHINode>(User)) |
Rafael Espindola | 94df267 | 2012-02-26 02:19:19 +0000 | [diff] [blame] | 138 | return dominates(Def, UseBB); |
| 139 | |
| 140 | if (DefBB != UseBB) |
| 141 | return dominates(DefBB, UseBB); |
| 142 | |
| 143 | // Loop through the basic block until we find Def or User. |
| 144 | BasicBlock::const_iterator I = DefBB->begin(); |
| 145 | for (; &*I != Def && &*I != User; ++I) |
Chris Lattner | 22151ce | 2009-09-21 22:30:50 +0000 | [diff] [blame] | 146 | /*empty*/; |
Rafael Espindola | 082d482 | 2012-02-18 19:46:02 +0000 | [diff] [blame] | 147 | |
Rafael Espindola | 94df267 | 2012-02-26 02:19:19 +0000 | [diff] [blame] | 148 | return &*I == Def; |
| 149 | } |
| 150 | |
| 151 | // true if Def would dominate a use in any instruction in UseBB. |
| 152 | // note that dominates(Def, Def->getParent()) is false. |
| 153 | bool DominatorTree::dominates(const Instruction *Def, |
| 154 | const BasicBlock *UseBB) const { |
| 155 | const BasicBlock *DefBB = Def->getParent(); |
| 156 | |
Rafael Espindola | a53c46a | 2012-03-30 16:46:21 +0000 | [diff] [blame] | 157 | // Any unreachable use is dominated, even if DefBB == UseBB. |
| 158 | if (!isReachableFromEntry(UseBB)) |
| 159 | return true; |
| 160 | |
| 161 | // Unreachable definitions don't dominate anything. |
| 162 | if (!isReachableFromEntry(DefBB)) |
| 163 | return false; |
Rafael Espindola | 94df267 | 2012-02-26 02:19:19 +0000 | [diff] [blame] | 164 | |
| 165 | if (DefBB == UseBB) |
| 166 | return false; |
| 167 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 168 | // Invoke results are only usable in the normal destination, not in the |
| 169 | // exceptional destination. |
David Majnemer | 0bc0eef | 2015-08-15 02:46:08 +0000 | [diff] [blame] | 170 | if (const auto *II = dyn_cast<InvokeInst>(Def)) { |
| 171 | BasicBlock *NormalDest = II->getNormalDest(); |
| 172 | BasicBlockEdge E(DefBB, NormalDest); |
| 173 | return dominates(E, UseBB); |
| 174 | } |
Rafael Espindola | 94df267 | 2012-02-26 02:19:19 +0000 | [diff] [blame] | 175 | |
David Majnemer | 0bc0eef | 2015-08-15 02:46:08 +0000 | [diff] [blame] | 176 | return dominates(DefBB, UseBB); |
Rafael Espindola | 5956407 | 2012-08-07 17:30:46 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | bool DominatorTree::dominates(const BasicBlockEdge &BBE, |
| 180 | const BasicBlock *UseBB) const { |
| 181 | // If the BB the edge ends in doesn't dominate the use BB, then the |
| 182 | // edge also doesn't. |
| 183 | const BasicBlock *Start = BBE.getStart(); |
| 184 | const BasicBlock *End = BBE.getEnd(); |
| 185 | if (!dominates(End, UseBB)) |
Rafael Espindola | 94df267 | 2012-02-26 02:19:19 +0000 | [diff] [blame] | 186 | return false; |
| 187 | |
Rafael Espindola | 5956407 | 2012-08-07 17:30:46 +0000 | [diff] [blame] | 188 | // Simple case: if the end BB has a single predecessor, the fact that it |
| 189 | // dominates the use block implies that the edge also does. |
| 190 | if (End->getSinglePredecessor()) |
Rafael Espindola | 94df267 | 2012-02-26 02:19:19 +0000 | [diff] [blame] | 191 | return true; |
| 192 | |
| 193 | // The normal edge from the invoke is critical. Conceptually, what we would |
| 194 | // like to do is split it and check if the new block dominates the use. |
| 195 | // With X being the new block, the graph would look like: |
| 196 | // |
| 197 | // DefBB |
| 198 | // /\ . . |
| 199 | // / \ . . |
| 200 | // / \ . . |
| 201 | // / \ | | |
| 202 | // A X B C |
| 203 | // | \ | / |
| 204 | // . \|/ |
| 205 | // . NormalDest |
| 206 | // . |
| 207 | // |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 208 | // Given the definition of dominance, NormalDest is dominated by X iff X |
Rafael Espindola | 94df267 | 2012-02-26 02:19:19 +0000 | [diff] [blame] | 209 | // dominates all of NormalDest's predecessors (X, B, C in the example). X |
| 210 | // trivially dominates itself, so we only have to find if it dominates the |
| 211 | // other predecessors. Since the only way out of X is via NormalDest, X can |
| 212 | // only properly dominate a node if NormalDest dominates that node too. |
Adam Nemet | 4ef096b | 2017-06-05 16:27:09 +0000 | [diff] [blame] | 213 | int IsDuplicateEdge = 0; |
Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 214 | for (const_pred_iterator PI = pred_begin(End), E = pred_end(End); |
| 215 | PI != E; ++PI) { |
| 216 | const BasicBlock *BB = *PI; |
Adam Nemet | 4ef096b | 2017-06-05 16:27:09 +0000 | [diff] [blame] | 217 | if (BB == Start) { |
| 218 | // If there are multiple edges between Start and End, by definition they |
| 219 | // can't dominate anything. |
| 220 | if (IsDuplicateEdge++) |
| 221 | return false; |
Rafael Espindola | 94df267 | 2012-02-26 02:19:19 +0000 | [diff] [blame] | 222 | continue; |
Adam Nemet | 4ef096b | 2017-06-05 16:27:09 +0000 | [diff] [blame] | 223 | } |
Rafael Espindola | 94df267 | 2012-02-26 02:19:19 +0000 | [diff] [blame] | 224 | |
Rafael Espindola | 5956407 | 2012-08-07 17:30:46 +0000 | [diff] [blame] | 225 | if (!dominates(End, BB)) |
Rafael Espindola | 94df267 | 2012-02-26 02:19:19 +0000 | [diff] [blame] | 226 | return false; |
| 227 | } |
| 228 | return true; |
Chris Lattner | 22151ce | 2009-09-21 22:30:50 +0000 | [diff] [blame] | 229 | } |
Dan Gohman | 7327327 | 2012-04-12 23:31:46 +0000 | [diff] [blame] | 230 | |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 231 | bool DominatorTree::dominates(const BasicBlockEdge &BBE, const Use &U) const { |
Rafael Espindola | 5956407 | 2012-08-07 17:30:46 +0000 | [diff] [blame] | 232 | Instruction *UserInst = cast<Instruction>(U.getUser()); |
| 233 | // A PHI in the end of the edge is dominated by it. |
| 234 | PHINode *PN = dyn_cast<PHINode>(UserInst); |
| 235 | if (PN && PN->getParent() == BBE.getEnd() && |
| 236 | PN->getIncomingBlock(U) == BBE.getStart()) |
| 237 | return true; |
| 238 | |
| 239 | // Otherwise use the edge-dominates-block query, which |
| 240 | // handles the crazy critical edge cases properly. |
| 241 | const BasicBlock *UseBB; |
| 242 | if (PN) |
| 243 | UseBB = PN->getIncomingBlock(U); |
| 244 | else |
| 245 | UseBB = UserInst->getParent(); |
| 246 | return dominates(BBE, UseBB); |
| 247 | } |
| 248 | |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 249 | bool DominatorTree::dominates(const Instruction *Def, const Use &U) const { |
Rafael Espindola | 5956407 | 2012-08-07 17:30:46 +0000 | [diff] [blame] | 250 | Instruction *UserInst = cast<Instruction>(U.getUser()); |
Dan Gohman | 7327327 | 2012-04-12 23:31:46 +0000 | [diff] [blame] | 251 | const BasicBlock *DefBB = Def->getParent(); |
| 252 | |
| 253 | // Determine the block in which the use happens. PHI nodes use |
| 254 | // their operands on edges; simulate this by thinking of the use |
| 255 | // happening at the end of the predecessor block. |
| 256 | const BasicBlock *UseBB; |
| 257 | if (PHINode *PN = dyn_cast<PHINode>(UserInst)) |
| 258 | UseBB = PN->getIncomingBlock(U); |
| 259 | else |
| 260 | UseBB = UserInst->getParent(); |
| 261 | |
| 262 | // Any unreachable use is dominated, even if Def == User. |
| 263 | if (!isReachableFromEntry(UseBB)) |
| 264 | return true; |
| 265 | |
| 266 | // Unreachable definitions don't dominate anything. |
| 267 | if (!isReachableFromEntry(DefBB)) |
| 268 | return false; |
| 269 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 270 | // Invoke instructions define their return values on the edges to their normal |
| 271 | // successors, so we have to handle them specially. |
Dan Gohman | 7327327 | 2012-04-12 23:31:46 +0000 | [diff] [blame] | 272 | // Among other things, this means they don't dominate anything in |
| 273 | // their own block, except possibly a phi, so we don't need to |
| 274 | // walk the block in any case. |
| 275 | if (const InvokeInst *II = dyn_cast<InvokeInst>(Def)) { |
Rafael Espindola | 5956407 | 2012-08-07 17:30:46 +0000 | [diff] [blame] | 276 | BasicBlock *NormalDest = II->getNormalDest(); |
| 277 | BasicBlockEdge E(DefBB, NormalDest); |
| 278 | return dominates(E, U); |
Dan Gohman | 7327327 | 2012-04-12 23:31:46 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | // If the def and use are in different blocks, do a simple CFG dominator |
| 282 | // tree query. |
| 283 | if (DefBB != UseBB) |
| 284 | return dominates(DefBB, UseBB); |
| 285 | |
| 286 | // Ok, def and use are in the same block. If the def is an invoke, it |
| 287 | // doesn't dominate anything in the block. If it's a PHI, it dominates |
| 288 | // everything in the block. |
| 289 | if (isa<PHINode>(UserInst)) |
| 290 | return true; |
| 291 | |
| 292 | // Otherwise, just loop through the basic block until we find Def or User. |
| 293 | BasicBlock::const_iterator I = DefBB->begin(); |
| 294 | for (; &*I != Def && &*I != UserInst; ++I) |
| 295 | /*empty*/; |
| 296 | |
| 297 | return &*I != UserInst; |
| 298 | } |
| 299 | |
| 300 | bool DominatorTree::isReachableFromEntry(const Use &U) const { |
| 301 | Instruction *I = dyn_cast<Instruction>(U.getUser()); |
| 302 | |
| 303 | // ConstantExprs aren't really reachable from the entry block, but they |
| 304 | // don't need to be treated like unreachable code either. |
| 305 | if (!I) return true; |
| 306 | |
| 307 | // PHI nodes use their operands on their incoming edges. |
| 308 | if (PHINode *PN = dyn_cast<PHINode>(I)) |
| 309 | return isReachableFromEntry(PN->getIncomingBlock(U)); |
| 310 | |
| 311 | // Everything else uses their operands in their own block. |
| 312 | return isReachableFromEntry(I->getParent()); |
| 313 | } |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 314 | |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 315 | //===----------------------------------------------------------------------===// |
Chandler Carruth | 64764b4 | 2015-01-14 10:19:28 +0000 | [diff] [blame] | 316 | // DominatorTreeAnalysis and related pass implementations |
| 317 | //===----------------------------------------------------------------------===// |
| 318 | // |
| 319 | // This implements the DominatorTreeAnalysis which is used with the new pass |
| 320 | // manager. It also implements some methods from utility passes. |
| 321 | // |
| 322 | //===----------------------------------------------------------------------===// |
| 323 | |
Chandler Carruth | 164a2aa6 | 2016-06-17 00:11:01 +0000 | [diff] [blame] | 324 | DominatorTree DominatorTreeAnalysis::run(Function &F, |
Sean Silva | 36e0d01 | 2016-08-09 00:28:15 +0000 | [diff] [blame] | 325 | FunctionAnalysisManager &) { |
Chandler Carruth | 64764b4 | 2015-01-14 10:19:28 +0000 | [diff] [blame] | 326 | DominatorTree DT; |
| 327 | DT.recalculate(F); |
| 328 | return DT; |
| 329 | } |
| 330 | |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 331 | AnalysisKey DominatorTreeAnalysis::Key; |
NAKAMURA Takumi | df0cd72 | 2016-02-28 17:17:00 +0000 | [diff] [blame] | 332 | |
Chandler Carruth | 64764b4 | 2015-01-14 10:19:28 +0000 | [diff] [blame] | 333 | DominatorTreePrinterPass::DominatorTreePrinterPass(raw_ostream &OS) : OS(OS) {} |
| 334 | |
| 335 | PreservedAnalyses DominatorTreePrinterPass::run(Function &F, |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 336 | FunctionAnalysisManager &AM) { |
Chandler Carruth | 64764b4 | 2015-01-14 10:19:28 +0000 | [diff] [blame] | 337 | OS << "DominatorTree for function: " << F.getName() << "\n"; |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 338 | AM.getResult<DominatorTreeAnalysis>(F).print(OS); |
Chandler Carruth | 64764b4 | 2015-01-14 10:19:28 +0000 | [diff] [blame] | 339 | |
| 340 | return PreservedAnalyses::all(); |
| 341 | } |
| 342 | |
| 343 | PreservedAnalyses DominatorTreeVerifierPass::run(Function &F, |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 344 | FunctionAnalysisManager &AM) { |
David Green | 7c35de1 | 2018-02-28 11:00:08 +0000 | [diff] [blame] | 345 | auto &DT = AM.getResult<DominatorTreeAnalysis>(F); |
| 346 | assert(DT.verify()); |
| 347 | (void)DT; |
Chandler Carruth | 64764b4 | 2015-01-14 10:19:28 +0000 | [diff] [blame] | 348 | return PreservedAnalyses::all(); |
| 349 | } |
| 350 | |
| 351 | //===----------------------------------------------------------------------===// |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 352 | // DominatorTreeWrapperPass Implementation |
| 353 | //===----------------------------------------------------------------------===// |
| 354 | // |
Chandler Carruth | 64764b4 | 2015-01-14 10:19:28 +0000 | [diff] [blame] | 355 | // The implementation details of the wrapper pass that holds a DominatorTree |
| 356 | // suitable for use with the legacy pass manager. |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 357 | // |
| 358 | //===----------------------------------------------------------------------===// |
| 359 | |
| 360 | char DominatorTreeWrapperPass::ID = 0; |
| 361 | INITIALIZE_PASS(DominatorTreeWrapperPass, "domtree", |
| 362 | "Dominator Tree Construction", true, true) |
| 363 | |
| 364 | bool DominatorTreeWrapperPass::runOnFunction(Function &F) { |
| 365 | DT.recalculate(F); |
| 366 | return false; |
| 367 | } |
| 368 | |
Adam Nemet | e340f85 | 2015-05-06 08:18:41 +0000 | [diff] [blame] | 369 | void DominatorTreeWrapperPass::verifyAnalysis() const { |
David Green | 7c35de1 | 2018-02-28 11:00:08 +0000 | [diff] [blame] | 370 | if (VerifyDomInfo) |
| 371 | assert(DT.verify(DominatorTree::VerificationLevel::Full)); |
| 372 | else if (ExpensiveChecksEnabled) |
| 373 | assert(DT.verify(DominatorTree::VerificationLevel::Basic)); |
Adam Nemet | e340f85 | 2015-05-06 08:18:41 +0000 | [diff] [blame] | 374 | } |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 375 | |
| 376 | void DominatorTreeWrapperPass::print(raw_ostream &OS, const Module *) const { |
| 377 | DT.print(OS); |
| 378 | } |
| 379 | |