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" |
Chandler Carruth | 1305dc3 | 2014-03-04 11:45:46 +0000 | [diff] [blame] | 20 | #include "llvm/IR/CFG.h" |
Brian M. Rzycki | 9b7ae23 | 2018-01-12 21:06:48 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Constants.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Instructions.h" |
Chandler Carruth | 64764b4 | 2015-01-14 10:19:28 +0000 | [diff] [blame] | 23 | #include "llvm/IR/PassManager.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/Debug.h" |
Chandler Carruth | e509db4 | 2014-01-13 10:52:56 +0000 | [diff] [blame] | 26 | #include "llvm/Support/GenericDomTreeConstruction.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 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 | |
Serge Pavlov | 69b3ff9 | 2017-01-24 05:52:07 +0000 | [diff] [blame] | 31 | bool llvm::VerifyDomInfo = false; |
Zachary Turner | 8065f0b | 2017-12-01 00:53:10 +0000 | [diff] [blame] | 32 | static cl::opt<bool, true> |
| 33 | VerifyDomInfoX("verify-dom-info", cl::location(VerifyDomInfo), cl::Hidden, |
| 34 | cl::desc("Verify dominator info (time consuming)")); |
Dan Gohman | 4dbb301 | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 35 | |
Jakub Kuderski | ffb4fb7 | 2018-01-24 02:40:35 +0000 | [diff] [blame] | 36 | #ifdef EXPENSIVE_CHECKS |
| 37 | static constexpr bool ExpensiveChecksEnabled = true; |
| 38 | #else |
| 39 | static constexpr bool ExpensiveChecksEnabled = false; |
| 40 | #endif |
| 41 | |
Rafael Espindola | cc80cde | 2012-08-16 15:09:43 +0000 | [diff] [blame] | 42 | bool BasicBlockEdge::isSingleEdge() const { |
| 43 | const TerminatorInst *TI = Start->getTerminator(); |
| 44 | unsigned NumEdgesToEnd = 0; |
| 45 | for (unsigned int i = 0, n = TI->getNumSuccessors(); i < n; ++i) { |
| 46 | if (TI->getSuccessor(i) == End) |
| 47 | ++NumEdgesToEnd; |
| 48 | if (NumEdgesToEnd >= 2) |
| 49 | return false; |
| 50 | } |
| 51 | assert(NumEdgesToEnd == 1); |
| 52 | return true; |
Rafael Espindola | 1187077 | 2012-08-10 14:05:55 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Chris Lattner | c385beb | 2001-07-06 16:58:22 +0000 | [diff] [blame] | 55 | //===----------------------------------------------------------------------===// |
Owen Anderson | f35a1db | 2007-04-15 08:47:27 +0000 | [diff] [blame] | 56 | // DominatorTree Implementation |
Chris Lattner | 00f5167 | 2003-12-07 00:38:08 +0000 | [diff] [blame] | 57 | //===----------------------------------------------------------------------===// |
| 58 | // |
Owen Anderson | 84c357f | 2007-09-23 21:31:44 +0000 | [diff] [blame] | 59 | // Provide public access to DominatorTree information. Implementation details |
Chandler Carruth | e509db4 | 2014-01-13 10:52:56 +0000 | [diff] [blame] | 60 | // can be found in Dominators.h, GenericDomTree.h, and |
| 61 | // GenericDomTreeConstruction.h. |
Chris Lattner | 00f5167 | 2003-12-07 00:38:08 +0000 | [diff] [blame] | 62 | // |
| 63 | //===----------------------------------------------------------------------===// |
| 64 | |
Benjamin Kramer | a667d1a | 2015-07-13 17:21:31 +0000 | [diff] [blame] | 65 | template class llvm::DomTreeNodeBase<BasicBlock>; |
Jakub Kuderski | b292c22 | 2017-07-14 18:26:09 +0000 | [diff] [blame] | 66 | template class llvm::DominatorTreeBase<BasicBlock, false>; // DomTreeBase |
| 67 | template class llvm::DominatorTreeBase<BasicBlock, true>; // PostDomTreeBase |
Owen Anderson | 4187801 | 2007-10-16 19:59:25 +0000 | [diff] [blame] | 68 | |
Jakub Kuderski | 624463a | 2017-08-16 16:12:52 +0000 | [diff] [blame] | 69 | template struct llvm::DomTreeBuilder::Update<BasicBlock *>; |
| 70 | |
Jakub Kuderski | c271dea | 2017-07-26 18:07:40 +0000 | [diff] [blame] | 71 | template void llvm::DomTreeBuilder::Calculate<DomTreeBuilder::BBDomTree>( |
| 72 | DomTreeBuilder::BBDomTree &DT); |
| 73 | template void llvm::DomTreeBuilder::Calculate<DomTreeBuilder::BBPostDomTree>( |
| 74 | DomTreeBuilder::BBPostDomTree &DT); |
Jakub Kuderski | 5af07f5 | 2017-07-13 20:45:32 +0000 | [diff] [blame] | 75 | |
Jakub Kuderski | 13e9ef1 | 2017-07-14 21:17:33 +0000 | [diff] [blame] | 76 | template void llvm::DomTreeBuilder::InsertEdge<DomTreeBuilder::BBDomTree>( |
| 77 | DomTreeBuilder::BBDomTree &DT, BasicBlock *From, BasicBlock *To); |
| 78 | template void llvm::DomTreeBuilder::InsertEdge<DomTreeBuilder::BBPostDomTree>( |
| 79 | DomTreeBuilder::BBPostDomTree &DT, BasicBlock *From, BasicBlock *To); |
| 80 | |
Jakub Kuderski | eb59ff2 | 2017-07-14 21:58:53 +0000 | [diff] [blame] | 81 | template void llvm::DomTreeBuilder::DeleteEdge<DomTreeBuilder::BBDomTree>( |
| 82 | DomTreeBuilder::BBDomTree &DT, BasicBlock *From, BasicBlock *To); |
| 83 | template void llvm::DomTreeBuilder::DeleteEdge<DomTreeBuilder::BBPostDomTree>( |
| 84 | DomTreeBuilder::BBPostDomTree &DT, BasicBlock *From, BasicBlock *To); |
| 85 | |
Jakub Kuderski | 624463a | 2017-08-16 16:12:52 +0000 | [diff] [blame] | 86 | template void llvm::DomTreeBuilder::ApplyUpdates<DomTreeBuilder::BBDomTree>( |
| 87 | DomTreeBuilder::BBDomTree &DT, DomTreeBuilder::BBUpdates); |
| 88 | template void llvm::DomTreeBuilder::ApplyUpdates<DomTreeBuilder::BBPostDomTree>( |
| 89 | DomTreeBuilder::BBPostDomTree &DT, DomTreeBuilder::BBUpdates); |
| 90 | |
Jakub Kuderski | 5af07f5 | 2017-07-13 20:45:32 +0000 | [diff] [blame] | 91 | template bool llvm::DomTreeBuilder::Verify<DomTreeBuilder::BBDomTree>( |
Jakub Kuderski | ffb4fb7 | 2018-01-24 02:40:35 +0000 | [diff] [blame] | 92 | const DomTreeBuilder::BBDomTree &DT, |
| 93 | DomTreeBuilder::BBDomTree::VerificationLevel VL); |
Jakub Kuderski | b292c22 | 2017-07-14 18:26:09 +0000 | [diff] [blame] | 94 | template bool llvm::DomTreeBuilder::Verify<DomTreeBuilder::BBPostDomTree>( |
Jakub Kuderski | ffb4fb7 | 2018-01-24 02:40:35 +0000 | [diff] [blame] | 95 | const DomTreeBuilder::BBPostDomTree &DT, |
| 96 | DomTreeBuilder::BBPostDomTree::VerificationLevel VL); |
Rafael Espindola | 3061636 | 2014-02-14 22:36:16 +0000 | [diff] [blame] | 97 | |
Chandler Carruth | ca68a3e | 2017-01-15 06:32:49 +0000 | [diff] [blame] | 98 | bool DominatorTree::invalidate(Function &F, const PreservedAnalyses &PA, |
| 99 | FunctionAnalysisManager::Invalidator &) { |
| 100 | // Check whether the analysis, all analyses on functions, or the function's |
| 101 | // CFG have been preserved. |
| 102 | auto PAC = PA.getChecker<DominatorTreeAnalysis>(); |
| 103 | return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() || |
| 104 | PAC.preservedSet<CFGAnalyses>()); |
| 105 | } |
| 106 | |
Rafael Espindola | 94df267 | 2012-02-26 02:19:19 +0000 | [diff] [blame] | 107 | // dominates - Return true if Def dominates a use in User. This performs |
| 108 | // the special checks necessary if Def and User are in the same basic block. |
| 109 | // Note that Def doesn't dominate a use in Def itself! |
| 110 | bool DominatorTree::dominates(const Instruction *Def, |
| 111 | const Instruction *User) const { |
| 112 | const BasicBlock *UseBB = User->getParent(); |
| 113 | const BasicBlock *DefBB = Def->getParent(); |
Rafael Espindola | 082d482 | 2012-02-18 19:46:02 +0000 | [diff] [blame] | 114 | |
Rafael Espindola | a53c46a | 2012-03-30 16:46:21 +0000 | [diff] [blame] | 115 | // Any unreachable use is dominated, even if Def == User. |
| 116 | if (!isReachableFromEntry(UseBB)) |
| 117 | return true; |
| 118 | |
| 119 | // Unreachable definitions don't dominate anything. |
| 120 | if (!isReachableFromEntry(DefBB)) |
| 121 | return false; |
Rafael Espindola | 082d482 | 2012-02-18 19:46:02 +0000 | [diff] [blame] | 122 | |
Rafael Espindola | 94df267 | 2012-02-26 02:19:19 +0000 | [diff] [blame] | 123 | // An instruction doesn't dominate a use in itself. |
| 124 | if (Def == User) |
Chris Lattner | 22151ce | 2009-09-21 22:30:50 +0000 | [diff] [blame] | 125 | return false; |
Rafael Espindola | 082d482 | 2012-02-18 19:46:02 +0000 | [diff] [blame] | 126 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 127 | // The value defined by an invoke dominates an instruction only if it |
| 128 | // dominates every instruction in UseBB. |
| 129 | // A PHI is dominated only if the instruction dominates every possible use in |
| 130 | // the UseBB. |
| 131 | if (isa<InvokeInst>(Def) || isa<PHINode>(User)) |
Rafael Espindola | 94df267 | 2012-02-26 02:19:19 +0000 | [diff] [blame] | 132 | return dominates(Def, UseBB); |
| 133 | |
| 134 | if (DefBB != UseBB) |
| 135 | return dominates(DefBB, UseBB); |
| 136 | |
| 137 | // Loop through the basic block until we find Def or User. |
| 138 | BasicBlock::const_iterator I = DefBB->begin(); |
| 139 | for (; &*I != Def && &*I != User; ++I) |
Chris Lattner | 22151ce | 2009-09-21 22:30:50 +0000 | [diff] [blame] | 140 | /*empty*/; |
Rafael Espindola | 082d482 | 2012-02-18 19:46:02 +0000 | [diff] [blame] | 141 | |
Rafael Espindola | 94df267 | 2012-02-26 02:19:19 +0000 | [diff] [blame] | 142 | return &*I == Def; |
| 143 | } |
| 144 | |
| 145 | // true if Def would dominate a use in any instruction in UseBB. |
| 146 | // note that dominates(Def, Def->getParent()) is false. |
| 147 | bool DominatorTree::dominates(const Instruction *Def, |
| 148 | const BasicBlock *UseBB) const { |
| 149 | const BasicBlock *DefBB = Def->getParent(); |
| 150 | |
Rafael Espindola | a53c46a | 2012-03-30 16:46:21 +0000 | [diff] [blame] | 151 | // Any unreachable use is dominated, even if DefBB == UseBB. |
| 152 | if (!isReachableFromEntry(UseBB)) |
| 153 | return true; |
| 154 | |
| 155 | // Unreachable definitions don't dominate anything. |
| 156 | if (!isReachableFromEntry(DefBB)) |
| 157 | return false; |
Rafael Espindola | 94df267 | 2012-02-26 02:19:19 +0000 | [diff] [blame] | 158 | |
| 159 | if (DefBB == UseBB) |
| 160 | return false; |
| 161 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 162 | // Invoke results are only usable in the normal destination, not in the |
| 163 | // exceptional destination. |
David Majnemer | 0bc0eef | 2015-08-15 02:46:08 +0000 | [diff] [blame] | 164 | if (const auto *II = dyn_cast<InvokeInst>(Def)) { |
| 165 | BasicBlock *NormalDest = II->getNormalDest(); |
| 166 | BasicBlockEdge E(DefBB, NormalDest); |
| 167 | return dominates(E, UseBB); |
| 168 | } |
Rafael Espindola | 94df267 | 2012-02-26 02:19:19 +0000 | [diff] [blame] | 169 | |
David Majnemer | 0bc0eef | 2015-08-15 02:46:08 +0000 | [diff] [blame] | 170 | return dominates(DefBB, UseBB); |
Rafael Espindola | 5956407 | 2012-08-07 17:30:46 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | bool DominatorTree::dominates(const BasicBlockEdge &BBE, |
| 174 | const BasicBlock *UseBB) const { |
| 175 | // If the BB the edge ends in doesn't dominate the use BB, then the |
| 176 | // edge also doesn't. |
| 177 | const BasicBlock *Start = BBE.getStart(); |
| 178 | const BasicBlock *End = BBE.getEnd(); |
| 179 | if (!dominates(End, UseBB)) |
Rafael Espindola | 94df267 | 2012-02-26 02:19:19 +0000 | [diff] [blame] | 180 | return false; |
| 181 | |
Rafael Espindola | 5956407 | 2012-08-07 17:30:46 +0000 | [diff] [blame] | 182 | // Simple case: if the end BB has a single predecessor, the fact that it |
| 183 | // dominates the use block implies that the edge also does. |
| 184 | if (End->getSinglePredecessor()) |
Rafael Espindola | 94df267 | 2012-02-26 02:19:19 +0000 | [diff] [blame] | 185 | return true; |
| 186 | |
| 187 | // The normal edge from the invoke is critical. Conceptually, what we would |
| 188 | // like to do is split it and check if the new block dominates the use. |
| 189 | // With X being the new block, the graph would look like: |
| 190 | // |
| 191 | // DefBB |
| 192 | // /\ . . |
| 193 | // / \ . . |
| 194 | // / \ . . |
| 195 | // / \ | | |
| 196 | // A X B C |
| 197 | // | \ | / |
| 198 | // . \|/ |
| 199 | // . NormalDest |
| 200 | // . |
| 201 | // |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 202 | // Given the definition of dominance, NormalDest is dominated by X iff X |
Rafael Espindola | 94df267 | 2012-02-26 02:19:19 +0000 | [diff] [blame] | 203 | // dominates all of NormalDest's predecessors (X, B, C in the example). X |
| 204 | // trivially dominates itself, so we only have to find if it dominates the |
| 205 | // other predecessors. Since the only way out of X is via NormalDest, X can |
| 206 | // only properly dominate a node if NormalDest dominates that node too. |
Adam Nemet | 4ef096b | 2017-06-05 16:27:09 +0000 | [diff] [blame] | 207 | int IsDuplicateEdge = 0; |
Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 208 | for (const_pred_iterator PI = pred_begin(End), E = pred_end(End); |
| 209 | PI != E; ++PI) { |
| 210 | const BasicBlock *BB = *PI; |
Adam Nemet | 4ef096b | 2017-06-05 16:27:09 +0000 | [diff] [blame] | 211 | if (BB == Start) { |
| 212 | // If there are multiple edges between Start and End, by definition they |
| 213 | // can't dominate anything. |
| 214 | if (IsDuplicateEdge++) |
| 215 | return false; |
Rafael Espindola | 94df267 | 2012-02-26 02:19:19 +0000 | [diff] [blame] | 216 | continue; |
Adam Nemet | 4ef096b | 2017-06-05 16:27:09 +0000 | [diff] [blame] | 217 | } |
Rafael Espindola | 94df267 | 2012-02-26 02:19:19 +0000 | [diff] [blame] | 218 | |
Rafael Espindola | 5956407 | 2012-08-07 17:30:46 +0000 | [diff] [blame] | 219 | if (!dominates(End, BB)) |
Rafael Espindola | 94df267 | 2012-02-26 02:19:19 +0000 | [diff] [blame] | 220 | return false; |
| 221 | } |
| 222 | return true; |
Chris Lattner | 22151ce | 2009-09-21 22:30:50 +0000 | [diff] [blame] | 223 | } |
Dan Gohman | 7327327 | 2012-04-12 23:31:46 +0000 | [diff] [blame] | 224 | |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 225 | bool DominatorTree::dominates(const BasicBlockEdge &BBE, const Use &U) const { |
Rafael Espindola | 5956407 | 2012-08-07 17:30:46 +0000 | [diff] [blame] | 226 | Instruction *UserInst = cast<Instruction>(U.getUser()); |
| 227 | // A PHI in the end of the edge is dominated by it. |
| 228 | PHINode *PN = dyn_cast<PHINode>(UserInst); |
| 229 | if (PN && PN->getParent() == BBE.getEnd() && |
| 230 | PN->getIncomingBlock(U) == BBE.getStart()) |
| 231 | return true; |
| 232 | |
| 233 | // Otherwise use the edge-dominates-block query, which |
| 234 | // handles the crazy critical edge cases properly. |
| 235 | const BasicBlock *UseBB; |
| 236 | if (PN) |
| 237 | UseBB = PN->getIncomingBlock(U); |
| 238 | else |
| 239 | UseBB = UserInst->getParent(); |
| 240 | return dominates(BBE, UseBB); |
| 241 | } |
| 242 | |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 243 | bool DominatorTree::dominates(const Instruction *Def, const Use &U) const { |
Rafael Espindola | 5956407 | 2012-08-07 17:30:46 +0000 | [diff] [blame] | 244 | Instruction *UserInst = cast<Instruction>(U.getUser()); |
Dan Gohman | 7327327 | 2012-04-12 23:31:46 +0000 | [diff] [blame] | 245 | const BasicBlock *DefBB = Def->getParent(); |
| 246 | |
| 247 | // Determine the block in which the use happens. PHI nodes use |
| 248 | // their operands on edges; simulate this by thinking of the use |
| 249 | // happening at the end of the predecessor block. |
| 250 | const BasicBlock *UseBB; |
| 251 | if (PHINode *PN = dyn_cast<PHINode>(UserInst)) |
| 252 | UseBB = PN->getIncomingBlock(U); |
| 253 | else |
| 254 | UseBB = UserInst->getParent(); |
| 255 | |
| 256 | // Any unreachable use is dominated, even if Def == User. |
| 257 | if (!isReachableFromEntry(UseBB)) |
| 258 | return true; |
| 259 | |
| 260 | // Unreachable definitions don't dominate anything. |
| 261 | if (!isReachableFromEntry(DefBB)) |
| 262 | return false; |
| 263 | |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 264 | // Invoke instructions define their return values on the edges to their normal |
| 265 | // successors, so we have to handle them specially. |
Dan Gohman | 7327327 | 2012-04-12 23:31:46 +0000 | [diff] [blame] | 266 | // Among other things, this means they don't dominate anything in |
| 267 | // their own block, except possibly a phi, so we don't need to |
| 268 | // walk the block in any case. |
| 269 | if (const InvokeInst *II = dyn_cast<InvokeInst>(Def)) { |
Rafael Espindola | 5956407 | 2012-08-07 17:30:46 +0000 | [diff] [blame] | 270 | BasicBlock *NormalDest = II->getNormalDest(); |
| 271 | BasicBlockEdge E(DefBB, NormalDest); |
| 272 | return dominates(E, U); |
Dan Gohman | 7327327 | 2012-04-12 23:31:46 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | // If the def and use are in different blocks, do a simple CFG dominator |
| 276 | // tree query. |
| 277 | if (DefBB != UseBB) |
| 278 | return dominates(DefBB, UseBB); |
| 279 | |
| 280 | // Ok, def and use are in the same block. If the def is an invoke, it |
| 281 | // doesn't dominate anything in the block. If it's a PHI, it dominates |
| 282 | // everything in the block. |
| 283 | if (isa<PHINode>(UserInst)) |
| 284 | return true; |
| 285 | |
| 286 | // Otherwise, just loop through the basic block until we find Def or User. |
| 287 | BasicBlock::const_iterator I = DefBB->begin(); |
| 288 | for (; &*I != Def && &*I != UserInst; ++I) |
| 289 | /*empty*/; |
| 290 | |
| 291 | return &*I != UserInst; |
| 292 | } |
| 293 | |
| 294 | bool DominatorTree::isReachableFromEntry(const Use &U) const { |
| 295 | Instruction *I = dyn_cast<Instruction>(U.getUser()); |
| 296 | |
| 297 | // ConstantExprs aren't really reachable from the entry block, but they |
| 298 | // don't need to be treated like unreachable code either. |
| 299 | if (!I) return true; |
| 300 | |
| 301 | // PHI nodes use their operands on their incoming edges. |
| 302 | if (PHINode *PN = dyn_cast<PHINode>(I)) |
| 303 | return isReachableFromEntry(PN->getIncomingBlock(U)); |
| 304 | |
| 305 | // Everything else uses their operands in their own block. |
| 306 | return isReachableFromEntry(I->getParent()); |
| 307 | } |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 308 | |
| 309 | void DominatorTree::verifyDomTree() const { |
Jakub Kuderski | 069e5cfa | 2017-06-30 16:33:04 +0000 | [diff] [blame] | 310 | // Perform the expensive checks only when VerifyDomInfo is set. |
Jakub Kuderski | ffb4fb7 | 2018-01-24 02:40:35 +0000 | [diff] [blame] | 311 | VerificationLevel VL = VerificationLevel::Fast; |
| 312 | if (VerifyDomInfo) |
| 313 | VL = VerificationLevel::Full; |
| 314 | else if (ExpensiveChecksEnabled) |
| 315 | VL = VerificationLevel::Basic; |
| 316 | |
| 317 | if (!verify(VL)) { |
Jakub Kuderski | f922336 | 2017-06-29 17:45:51 +0000 | [diff] [blame] | 318 | errs() << "\n~~~~~~~~~~~\n\t\tDomTree verification failed!\n~~~~~~~~~~~\n"; |
Jakub Kuderski | 624463a | 2017-08-16 16:12:52 +0000 | [diff] [blame] | 319 | errs() << "\nCFG:\n"; |
Jakub Kuderski | ffb4fb7 | 2018-01-24 02:40:35 +0000 | [diff] [blame] | 320 | getRoot()->getParent()->print(errs()); |
Jakub Kuderski | 624463a | 2017-08-16 16:12:52 +0000 | [diff] [blame] | 321 | errs().flush(); |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 322 | abort(); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | //===----------------------------------------------------------------------===// |
Chandler Carruth | 64764b4 | 2015-01-14 10:19:28 +0000 | [diff] [blame] | 327 | // DominatorTreeAnalysis and related pass implementations |
| 328 | //===----------------------------------------------------------------------===// |
| 329 | // |
| 330 | // This implements the DominatorTreeAnalysis which is used with the new pass |
| 331 | // manager. It also implements some methods from utility passes. |
| 332 | // |
| 333 | //===----------------------------------------------------------------------===// |
| 334 | |
Chandler Carruth | 164a2aa6 | 2016-06-17 00:11:01 +0000 | [diff] [blame] | 335 | DominatorTree DominatorTreeAnalysis::run(Function &F, |
Sean Silva | 36e0d01 | 2016-08-09 00:28:15 +0000 | [diff] [blame] | 336 | FunctionAnalysisManager &) { |
Chandler Carruth | 64764b4 | 2015-01-14 10:19:28 +0000 | [diff] [blame] | 337 | DominatorTree DT; |
| 338 | DT.recalculate(F); |
| 339 | return DT; |
| 340 | } |
| 341 | |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 342 | AnalysisKey DominatorTreeAnalysis::Key; |
NAKAMURA Takumi | df0cd72 | 2016-02-28 17:17:00 +0000 | [diff] [blame] | 343 | |
Chandler Carruth | 64764b4 | 2015-01-14 10:19:28 +0000 | [diff] [blame] | 344 | DominatorTreePrinterPass::DominatorTreePrinterPass(raw_ostream &OS) : OS(OS) {} |
| 345 | |
| 346 | PreservedAnalyses DominatorTreePrinterPass::run(Function &F, |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 347 | FunctionAnalysisManager &AM) { |
Chandler Carruth | 64764b4 | 2015-01-14 10:19:28 +0000 | [diff] [blame] | 348 | OS << "DominatorTree for function: " << F.getName() << "\n"; |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 349 | AM.getResult<DominatorTreeAnalysis>(F).print(OS); |
Chandler Carruth | 64764b4 | 2015-01-14 10:19:28 +0000 | [diff] [blame] | 350 | |
| 351 | return PreservedAnalyses::all(); |
| 352 | } |
| 353 | |
| 354 | PreservedAnalyses DominatorTreeVerifierPass::run(Function &F, |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 355 | FunctionAnalysisManager &AM) { |
| 356 | AM.getResult<DominatorTreeAnalysis>(F).verifyDomTree(); |
Chandler Carruth | 64764b4 | 2015-01-14 10:19:28 +0000 | [diff] [blame] | 357 | |
| 358 | return PreservedAnalyses::all(); |
| 359 | } |
| 360 | |
| 361 | //===----------------------------------------------------------------------===// |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 362 | // DominatorTreeWrapperPass Implementation |
| 363 | //===----------------------------------------------------------------------===// |
| 364 | // |
Chandler Carruth | 64764b4 | 2015-01-14 10:19:28 +0000 | [diff] [blame] | 365 | // The implementation details of the wrapper pass that holds a DominatorTree |
| 366 | // suitable for use with the legacy pass manager. |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 367 | // |
| 368 | //===----------------------------------------------------------------------===// |
| 369 | |
| 370 | char DominatorTreeWrapperPass::ID = 0; |
| 371 | INITIALIZE_PASS(DominatorTreeWrapperPass, "domtree", |
| 372 | "Dominator Tree Construction", true, true) |
| 373 | |
| 374 | bool DominatorTreeWrapperPass::runOnFunction(Function &F) { |
| 375 | DT.recalculate(F); |
| 376 | return false; |
| 377 | } |
| 378 | |
Adam Nemet | e340f85 | 2015-05-06 08:18:41 +0000 | [diff] [blame] | 379 | void DominatorTreeWrapperPass::verifyAnalysis() const { |
Jakub Kuderski | ffb4fb7 | 2018-01-24 02:40:35 +0000 | [diff] [blame] | 380 | if (ExpensiveChecksEnabled || VerifyDomInfo) |
| 381 | DT.verifyDomTree(); |
Adam Nemet | e340f85 | 2015-05-06 08:18:41 +0000 | [diff] [blame] | 382 | } |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 383 | |
| 384 | void DominatorTreeWrapperPass::print(raw_ostream &OS, const Module *) const { |
| 385 | DT.print(OS); |
| 386 | } |
| 387 | |
Brian M. Rzycki | 9b7ae23 | 2018-01-12 21:06:48 +0000 | [diff] [blame] | 388 | //===----------------------------------------------------------------------===// |
| 389 | // DeferredDominance Implementation |
| 390 | //===----------------------------------------------------------------------===// |
| 391 | // |
| 392 | // The implementation details of the DeferredDominance class which allows |
| 393 | // one to queue updates to a DominatorTree. |
| 394 | // |
| 395 | //===----------------------------------------------------------------------===// |
| 396 | |
| 397 | /// \brief Queues multiple updates and discards duplicates. |
| 398 | void DeferredDominance::applyUpdates( |
| 399 | ArrayRef<DominatorTree::UpdateType> Updates) { |
| 400 | SmallVector<DominatorTree::UpdateType, 8> Seen; |
| 401 | for (auto U : Updates) |
| 402 | // Avoid duplicates to applyUpdate() to save on analysis. |
| 403 | if (std::none_of(Seen.begin(), Seen.end(), |
| 404 | [U](DominatorTree::UpdateType S) { return S == U; })) { |
| 405 | Seen.push_back(U); |
| 406 | applyUpdate(U.getKind(), U.getFrom(), U.getTo()); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | /// \brief Helper method for a single edge insertion. It's almost always better |
| 411 | /// to batch updates and call applyUpdates to quickly remove duplicate edges. |
| 412 | /// This is best used when there is only a single insertion needed to update |
| 413 | /// Dominators. |
| 414 | void DeferredDominance::insertEdge(BasicBlock *From, BasicBlock *To) { |
| 415 | applyUpdate(DominatorTree::Insert, From, To); |
| 416 | } |
| 417 | |
| 418 | /// \brief Helper method for a single edge deletion. It's almost always better |
| 419 | /// to batch updates and call applyUpdates to quickly remove duplicate edges. |
| 420 | /// This is best used when there is only a single deletion needed to update |
| 421 | /// Dominators. |
| 422 | void DeferredDominance::deleteEdge(BasicBlock *From, BasicBlock *To) { |
| 423 | applyUpdate(DominatorTree::Delete, From, To); |
| 424 | } |
| 425 | |
| 426 | /// \brief Delays the deletion of a basic block until a flush() event. |
| 427 | void DeferredDominance::deleteBB(BasicBlock *DelBB) { |
| 428 | assert(DelBB && "Invalid push_back of nullptr DelBB."); |
| 429 | assert(pred_empty(DelBB) && "DelBB has one or more predecessors."); |
| 430 | // DelBB is unreachable and all its instructions are dead. |
| 431 | while (!DelBB->empty()) { |
| 432 | Instruction &I = DelBB->back(); |
| 433 | // Replace used instructions with an arbitrary value (undef). |
| 434 | if (!I.use_empty()) |
| 435 | I.replaceAllUsesWith(llvm::UndefValue::get(I.getType())); |
| 436 | DelBB->getInstList().pop_back(); |
| 437 | } |
| 438 | // Make sure DelBB has a valid terminator instruction. As long as DelBB is a |
| 439 | // Child of Function F it must contain valid IR. |
| 440 | new UnreachableInst(DelBB->getContext(), DelBB); |
| 441 | DeletedBBs.insert(DelBB); |
| 442 | } |
| 443 | |
| 444 | /// \brief Returns true if DelBB is awaiting deletion at a flush() event. |
| 445 | bool DeferredDominance::pendingDeletedBB(BasicBlock *DelBB) { |
| 446 | if (DeletedBBs.empty()) |
| 447 | return false; |
| 448 | return DeletedBBs.count(DelBB) != 0; |
| 449 | } |
| 450 | |
Brian M. Rzycki | f1a7df5 | 2018-02-16 16:35:17 +0000 | [diff] [blame^] | 451 | /// \brief Returns true if pending DT updates are queued for a flush() event. |
| 452 | bool DeferredDominance::pending() { return !PendUpdates.empty(); } |
| 453 | |
Brian M. Rzycki | 9b7ae23 | 2018-01-12 21:06:48 +0000 | [diff] [blame] | 454 | /// \brief Flushes all pending updates and block deletions. Returns a |
| 455 | /// correct DominatorTree reference to be used by the caller for analysis. |
| 456 | DominatorTree &DeferredDominance::flush() { |
| 457 | // Updates to DT must happen before blocks are deleted below. Otherwise the |
| 458 | // DT traversal will encounter badref blocks and assert. |
| 459 | if (!PendUpdates.empty()) { |
| 460 | DT.applyUpdates(PendUpdates); |
| 461 | PendUpdates.clear(); |
| 462 | } |
| 463 | flushDelBB(); |
| 464 | return DT; |
| 465 | } |
| 466 | |
| 467 | /// \brief Drops all internal state and forces a (slow) recalculation of the |
| 468 | /// DominatorTree based on the current state of the LLVM IR in F. This should |
| 469 | /// only be used in corner cases such as the Entry block of F being deleted. |
| 470 | void DeferredDominance::recalculate(Function &F) { |
| 471 | // flushDelBB must be flushed before the recalculation. The state of the IR |
| 472 | // must be consistent before the DT traversal algorithm determines the |
| 473 | // actual DT. |
| 474 | if (flushDelBB() || !PendUpdates.empty()) { |
| 475 | DT.recalculate(F); |
| 476 | PendUpdates.clear(); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | /// \brief Debug method to help view the state of pending updates. |
| 481 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 482 | LLVM_DUMP_METHOD void DeferredDominance::dump() const { |
| 483 | raw_ostream &OS = llvm::dbgs(); |
| 484 | OS << "PendUpdates:\n"; |
| 485 | int I = 0; |
| 486 | for (auto U : PendUpdates) { |
| 487 | OS << " " << I << " : "; |
| 488 | ++I; |
| 489 | if (U.getKind() == DominatorTree::Insert) |
| 490 | OS << "Insert, "; |
| 491 | else |
| 492 | OS << "Delete, "; |
| 493 | BasicBlock *From = U.getFrom(); |
| 494 | if (From) { |
| 495 | auto S = From->getName(); |
| 496 | if (!From->hasName()) |
| 497 | S = "(no name)"; |
| 498 | OS << S << "(" << From << "), "; |
| 499 | } else { |
| 500 | OS << "(badref), "; |
| 501 | } |
| 502 | BasicBlock *To = U.getTo(); |
| 503 | if (To) { |
| 504 | auto S = To->getName(); |
| 505 | if (!To->hasName()) |
| 506 | S = "(no_name)"; |
| 507 | OS << S << "(" << To << ")\n"; |
| 508 | } else { |
| 509 | OS << "(badref)\n"; |
| 510 | } |
| 511 | } |
| 512 | OS << "DeletedBBs:\n"; |
| 513 | I = 0; |
| 514 | for (auto BB : DeletedBBs) { |
| 515 | OS << " " << I << " : "; |
| 516 | ++I; |
| 517 | if (BB->hasName()) |
| 518 | OS << BB->getName() << "("; |
| 519 | else |
| 520 | OS << "(no_name)("; |
| 521 | OS << BB << ")\n"; |
| 522 | } |
| 523 | } |
| 524 | #endif |
| 525 | |
| 526 | /// Apply an update (Kind, From, To) to the internal queued updates. The |
| 527 | /// update is only added when determined to be necessary. Checks for |
| 528 | /// self-domination, unnecessary updates, duplicate requests, and balanced |
| 529 | /// pairs of requests are all performed. Returns true if the update is |
| 530 | /// queued and false if it is discarded. |
| 531 | bool DeferredDominance::applyUpdate(DominatorTree::UpdateKind Kind, |
| 532 | BasicBlock *From, BasicBlock *To) { |
| 533 | if (From == To) |
| 534 | return false; // Cannot dominate self; discard update. |
| 535 | |
| 536 | // Discard updates by inspecting the current state of successors of From. |
| 537 | // Since applyUpdate() must be called *after* the Terminator of From is |
| 538 | // altered we can determine if the update is unnecessary. |
| 539 | bool HasEdge = std::any_of(succ_begin(From), succ_end(From), |
| 540 | [To](BasicBlock *B) { return B == To; }); |
| 541 | if (Kind == DominatorTree::Insert && !HasEdge) |
| 542 | return false; // Unnecessary Insert: edge does not exist in IR. |
| 543 | if (Kind == DominatorTree::Delete && HasEdge) |
| 544 | return false; // Unnecessary Delete: edge still exists in IR. |
| 545 | |
| 546 | // Analyze pending updates to determine if the update is unnecessary. |
| 547 | DominatorTree::UpdateType Update = {Kind, From, To}; |
| 548 | DominatorTree::UpdateType Invert = {Kind != DominatorTree::Insert |
| 549 | ? DominatorTree::Insert |
| 550 | : DominatorTree::Delete, |
| 551 | From, To}; |
| 552 | for (auto I = PendUpdates.begin(), E = PendUpdates.end(); I != E; ++I) { |
| 553 | if (Update == *I) |
| 554 | return false; // Discard duplicate updates. |
| 555 | if (Invert == *I) { |
| 556 | // Update and Invert are both valid (equivalent to a no-op). Remove |
| 557 | // Invert from PendUpdates and discard the Update. |
| 558 | PendUpdates.erase(I); |
| 559 | return false; |
| 560 | } |
| 561 | } |
| 562 | PendUpdates.push_back(Update); // Save the valid update. |
| 563 | return true; |
| 564 | } |
| 565 | |
| 566 | /// Performs all pending basic block deletions. We have to defer the deletion |
| 567 | /// of these blocks until after the DominatorTree updates are applied. The |
| 568 | /// internal workings of the DominatorTree code expect every update's From |
| 569 | /// and To blocks to exist and to be a member of the same Function. |
| 570 | bool DeferredDominance::flushDelBB() { |
| 571 | if (DeletedBBs.empty()) |
| 572 | return false; |
| 573 | for (auto *BB : DeletedBBs) |
| 574 | BB->eraseFromParent(); |
| 575 | DeletedBBs.clear(); |
| 576 | return true; |
| 577 | } |