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/Analysis/Dominators.h"
18 #include "llvm/Support/CFG.h"
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/ADT/DepthFirstIterator.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/Analysis/DominatorInternals.h"
25 #include "llvm/Assembly/Writer.h"
26 #include "llvm/Instructions.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/Support/CommandLine.h"
32 // Always verify dominfo if expensive checking is enabled.
34 static bool VerifyDomInfo = true;
36 static bool VerifyDomInfo = false;
38 static cl::opt<bool,true>
39 VerifyDomInfoX("verify-dom-info", cl::location(VerifyDomInfo),
40 cl::desc("Verify dominator info (time consuming)"));
42 //===----------------------------------------------------------------------===//
43 // DominatorTree Implementation
44 //===----------------------------------------------------------------------===//
46 // Provide public access to DominatorTree information. Implementation details
47 // can be found in DominatorInternals.h.
49 //===----------------------------------------------------------------------===//
51 TEMPLATE_INSTANTIATION(class llvm::DomTreeNodeBase<BasicBlock>);
52 TEMPLATE_INSTANTIATION(class llvm::DominatorTreeBase<BasicBlock>);
54 char DominatorTree::ID = 0;
55 INITIALIZE_PASS(DominatorTree, "domtree",
56 "Dominator Tree Construction", true, true)
58 bool DominatorTree::runOnFunction(Function &F) {
63 void DominatorTree::verifyAnalysis() const {
64 if (!VerifyDomInfo) return;
66 Function &F = *getRoot()->getParent();
68 DominatorTree OtherDT;
69 OtherDT.getBase().recalculate(F);
70 if (compare(OtherDT)) {
71 errs() << "DominatorTree is not up to date!\nComputed:\n";
73 errs() << "\nActual:\n";
74 OtherDT.print(errs());
79 void DominatorTree::print(raw_ostream &OS, const Module *) const {
83 // dominates - Return true if Def dominates a use in User. This performs
84 // the special checks necessary if Def and User are in the same basic block.
85 // Note that Def doesn't dominate a use in Def itself!
86 bool DominatorTree::dominates(const Instruction *Def,
87 const Instruction *User) const {
88 const BasicBlock *UseBB = User->getParent();
89 const BasicBlock *DefBB = Def->getParent();
91 // Any unreachable use is dominated, even if Def == User.
92 if (!isReachableFromEntry(UseBB))
95 // Unreachable definitions don't dominate anything.
96 if (!isReachableFromEntry(DefBB))
99 // An instruction doesn't dominate a use in itself.
103 // The value defined by an invoke dominates an instruction only if
104 // it dominates every instruction in UseBB.
105 // A PHI is dominated only if the instruction dominates every possible use
107 if (isa<InvokeInst>(Def) || isa<PHINode>(User))
108 return dominates(Def, UseBB);
111 return dominates(DefBB, UseBB);
113 // Loop through the basic block until we find Def or User.
114 BasicBlock::const_iterator I = DefBB->begin();
115 for (; &*I != Def && &*I != User; ++I)
121 // true if Def would dominate a use in any instruction in UseBB.
122 // note that dominates(Def, Def->getParent()) is false.
123 bool DominatorTree::dominates(const Instruction *Def,
124 const BasicBlock *UseBB) const {
125 const BasicBlock *DefBB = Def->getParent();
127 // Any unreachable use is dominated, even if DefBB == UseBB.
128 if (!isReachableFromEntry(UseBB))
131 // Unreachable definitions don't dominate anything.
132 if (!isReachableFromEntry(DefBB))
138 const InvokeInst *II = dyn_cast<InvokeInst>(Def);
140 return dominates(DefBB, UseBB);
142 // Invoke results are only usable in the normal destination, not in the
143 // exceptional destination.
144 BasicBlock *NormalDest = II->getNormalDest();
145 BasicBlockEdge E(DefBB, NormalDest);
146 return dominates(E, UseBB);
149 bool DominatorTree::dominates(const BasicBlockEdge &BBE,
150 const BasicBlock *UseBB) const {
151 // If the BB the edge ends in doesn't dominate the use BB, then the
152 // edge also doesn't.
153 const BasicBlock *Start = BBE.getStart();
154 const BasicBlock *End = BBE.getEnd();
155 if (!dominates(End, UseBB))
158 // Simple case: if the end BB has a single predecessor, the fact that it
159 // dominates the use block implies that the edge also does.
160 if (End->getSinglePredecessor())
163 // The normal edge from the invoke is critical. Conceptually, what we would
164 // like to do is split it and check if the new block dominates the use.
165 // With X being the new block, the graph would look like:
178 // Given the definition of dominance, NormalDest is dominated by X iff X
179 // dominates all of NormalDest's predecessors (X, B, C in the example). X
180 // trivially dominates itself, so we only have to find if it dominates the
181 // other predecessors. Since the only way out of X is via NormalDest, X can
182 // only properly dominate a node if NormalDest dominates that node too.
183 for (const_pred_iterator PI = pred_begin(End), E = pred_end(End);
185 const BasicBlock *BB = *PI;
189 if (!dominates(End, BB))
195 bool DominatorTree::dominates(const BasicBlockEdge &BBE,
196 const Use &U) const {
197 Instruction *UserInst = cast<Instruction>(U.getUser());
198 // A PHI in the end of the edge is dominated by it.
199 PHINode *PN = dyn_cast<PHINode>(UserInst);
200 if (PN && PN->getParent() == BBE.getEnd() &&
201 PN->getIncomingBlock(U) == BBE.getStart())
204 // Otherwise use the edge-dominates-block query, which
205 // handles the crazy critical edge cases properly.
206 const BasicBlock *UseBB;
208 UseBB = PN->getIncomingBlock(U);
210 UseBB = UserInst->getParent();
211 return dominates(BBE, UseBB);
214 bool DominatorTree::dominates(const Instruction *Def,
215 const Use &U) const {
216 Instruction *UserInst = cast<Instruction>(U.getUser());
217 const BasicBlock *DefBB = Def->getParent();
219 // Determine the block in which the use happens. PHI nodes use
220 // their operands on edges; simulate this by thinking of the use
221 // happening at the end of the predecessor block.
222 const BasicBlock *UseBB;
223 if (PHINode *PN = dyn_cast<PHINode>(UserInst))
224 UseBB = PN->getIncomingBlock(U);
226 UseBB = UserInst->getParent();
228 // Any unreachable use is dominated, even if Def == User.
229 if (!isReachableFromEntry(UseBB))
232 // Unreachable definitions don't dominate anything.
233 if (!isReachableFromEntry(DefBB))
236 // Invoke instructions define their return values on the edges
237 // to their normal successors, so we have to handle them specially.
238 // Among other things, this means they don't dominate anything in
239 // their own block, except possibly a phi, so we don't need to
240 // walk the block in any case.
241 if (const InvokeInst *II = dyn_cast<InvokeInst>(Def)) {
242 BasicBlock *NormalDest = II->getNormalDest();
243 BasicBlockEdge E(DefBB, NormalDest);
244 return dominates(E, U);
247 // If the def and use are in different blocks, do a simple CFG dominator
250 return dominates(DefBB, UseBB);
252 // Ok, def and use are in the same block. If the def is an invoke, it
253 // doesn't dominate anything in the block. If it's a PHI, it dominates
254 // everything in the block.
255 if (isa<PHINode>(UserInst))
258 // Otherwise, just loop through the basic block until we find Def or User.
259 BasicBlock::const_iterator I = DefBB->begin();
260 for (; &*I != Def && &*I != UserInst; ++I)
263 return &*I != UserInst;
266 bool DominatorTree::isReachableFromEntry(const Use &U) const {
267 Instruction *I = dyn_cast<Instruction>(U.getUser());
269 // ConstantExprs aren't really reachable from the entry block, but they
270 // don't need to be treated like unreachable code either.
273 // PHI nodes use their operands on their incoming edges.
274 if (PHINode *PN = dyn_cast<PHINode>(I))
275 return isReachableFromEntry(PN->getIncomingBlock(U));
277 // Everything else uses their operands in their own block.
278 return isReachableFromEntry(I->getParent());