1 //===- Dominators.cpp - Dominator Calculation -----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
15 //===----------------------------------------------------------------------===//
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"
31 // Always verify dominfo if expensive checking is enabled.
33 static bool VerifyDomInfo = true;
35 static bool VerifyDomInfo = false;
37 static cl::opt<bool,true>
38 VerifyDomInfoX("verify-dom-info", cl::location(VerifyDomInfo),
39 cl::desc("Verify dominator info (time consuming)"));
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)
47 if (NumEdgesToEnd >= 2)
50 assert(NumEdgesToEnd == 1);
54 //===----------------------------------------------------------------------===//
55 // DominatorTree Implementation
56 //===----------------------------------------------------------------------===//
58 // Provide public access to DominatorTree information. Implementation details
59 // can be found in Dominators.h, GenericDomTree.h, and
60 // GenericDomTreeConstruction.h.
62 //===----------------------------------------------------------------------===//
64 TEMPLATE_INSTANTIATION(class llvm::DomTreeNodeBase<BasicBlock>);
65 TEMPLATE_INSTANTIATION(class llvm::DominatorTreeBase<BasicBlock>);
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();
75 // Any unreachable use is dominated, even if Def == User.
76 if (!isReachableFromEntry(UseBB))
79 // Unreachable definitions don't dominate anything.
80 if (!isReachableFromEntry(DefBB))
83 // An instruction doesn't dominate a use in itself.
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
91 if (isa<InvokeInst>(Def) || isa<PHINode>(User))
92 return dominates(Def, UseBB);
95 return dominates(DefBB, UseBB);
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)
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();
111 // Any unreachable use is dominated, even if DefBB == UseBB.
112 if (!isReachableFromEntry(UseBB))
115 // Unreachable definitions don't dominate anything.
116 if (!isReachableFromEntry(DefBB))
122 const InvokeInst *II = dyn_cast<InvokeInst>(Def);
124 return dominates(DefBB, UseBB);
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);
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());
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))
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())
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:
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);
174 const BasicBlock *BB = *PI;
178 if (!dominates(End, BB))
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());
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())
197 // Otherwise use the edge-dominates-block query, which
198 // handles the crazy critical edge cases properly.
199 const BasicBlock *UseBB;
201 UseBB = PN->getIncomingBlock(U);
203 UseBB = UserInst->getParent();
204 return dominates(BBE, UseBB);
207 bool DominatorTree::dominates(const Instruction *Def, const Use &U) const {
208 Instruction *UserInst = cast<Instruction>(U.getUser());
209 const BasicBlock *DefBB = Def->getParent();
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);
218 UseBB = UserInst->getParent();
220 // Any unreachable use is dominated, even if Def == User.
221 if (!isReachableFromEntry(UseBB))
224 // Unreachable definitions don't dominate anything.
225 if (!isReachableFromEntry(DefBB))
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);
239 // If the def and use are in different blocks, do a simple CFG dominator
242 return dominates(DefBB, UseBB);
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))
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)
255 return &*I != UserInst;
258 bool DominatorTree::isReachableFromEntry(const Use &U) const {
259 Instruction *I = dyn_cast<Instruction>(U.getUser());
261 // ConstantExprs aren't really reachable from the entry block, but they
262 // don't need to be treated like unreachable code either.
265 // PHI nodes use their operands on their incoming edges.
266 if (PHINode *PN = dyn_cast<PHINode>(I))
267 return isReachableFromEntry(PN->getIncomingBlock(U));
269 // Everything else uses their operands in their own block.
270 return isReachableFromEntry(I->getParent());
273 void DominatorTree::verifyDomTree() const {
277 Function &F = *getRoot()->getParent();
279 DominatorTree OtherDT;
280 OtherDT.recalculate(F);
281 if (compare(OtherDT)) {
282 errs() << "DominatorTree is not up to date!\nComputed:\n";
284 errs() << "\nActual:\n";
285 OtherDT.print(errs());
290 //===----------------------------------------------------------------------===//
291 // DominatorTreeWrapperPass Implementation
292 //===----------------------------------------------------------------------===//
294 // The implementation details of the wrapper pass that holds a DominatorTree.
296 //===----------------------------------------------------------------------===//
298 char DominatorTreeWrapperPass::ID = 0;
299 INITIALIZE_PASS(DominatorTreeWrapperPass, "domtree",
300 "Dominator Tree Construction", true, true)
302 bool DominatorTreeWrapperPass::runOnFunction(Function &F) {
307 void DominatorTreeWrapperPass::verifyAnalysis() const { DT.verifyDomTree(); }
309 void DominatorTreeWrapperPass::print(raw_ostream &OS, const Module *) const {