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/DominatorInternals.h"
22 #include "llvm/IR/Instructions.h"
23 #include "llvm/Support/CFG.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/Compiler.h"
26 #include "llvm/Support/Debug.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 DominatorInternals.h.
61 //===----------------------------------------------------------------------===//
63 TEMPLATE_INSTANTIATION(class llvm::DomTreeNodeBase<BasicBlock>);
64 TEMPLATE_INSTANTIATION(class llvm::DominatorTreeBase<BasicBlock>);
66 char DominatorTree::ID = 0;
67 INITIALIZE_PASS(DominatorTree, "domtree",
68 "Dominator Tree Construction", true, true)
70 bool DominatorTree::runOnFunction(Function &F) {
75 void DominatorTree::verifyAnalysis() const {
76 if (!VerifyDomInfo) return;
78 Function &F = *getRoot()->getParent();
80 DominatorTree OtherDT;
81 OtherDT.getBase().recalculate(F);
82 if (compare(OtherDT)) {
83 errs() << "DominatorTree is not up to date!\nComputed:\n";
85 errs() << "\nActual:\n";
86 OtherDT.print(errs());
91 void DominatorTree::print(raw_ostream &OS, const Module *) const {
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();
103 // Any unreachable use is dominated, even if Def == User.
104 if (!isReachableFromEntry(UseBB))
107 // Unreachable definitions don't dominate anything.
108 if (!isReachableFromEntry(DefBB))
111 // An instruction doesn't dominate a use in itself.
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
119 if (isa<InvokeInst>(Def) || isa<PHINode>(User))
120 return dominates(Def, UseBB);
123 return dominates(DefBB, UseBB);
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)
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();
139 // Any unreachable use is dominated, even if DefBB == UseBB.
140 if (!isReachableFromEntry(UseBB))
143 // Unreachable definitions don't dominate anything.
144 if (!isReachableFromEntry(DefBB))
150 const InvokeInst *II = dyn_cast<InvokeInst>(Def);
152 return dominates(DefBB, UseBB);
154 // Invoke results are only usable in the normal destination, not in the
155 // exceptional destination.
156 BasicBlock *NormalDest = II->getNormalDest();
157 BasicBlockEdge E(DefBB, NormalDest);
158 return dominates(E, UseBB);
161 bool DominatorTree::dominates(const BasicBlockEdge &BBE,
162 const BasicBlock *UseBB) const {
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());
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))
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())
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:
195 // Given the definition of dominance, NormalDest is dominated by X iff X
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.
200 for (const_pred_iterator PI = pred_begin(End), E = pred_end(End);
202 const BasicBlock *BB = *PI;
206 if (!dominates(End, BB))
212 bool DominatorTree::dominates(const BasicBlockEdge &BBE,
213 const Use &U) const {
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());
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())
226 // Otherwise use the edge-dominates-block query, which
227 // handles the crazy critical edge cases properly.
228 const BasicBlock *UseBB;
230 UseBB = PN->getIncomingBlock(U);
232 UseBB = UserInst->getParent();
233 return dominates(BBE, UseBB);
236 bool DominatorTree::dominates(const Instruction *Def,
237 const Use &U) const {
238 Instruction *UserInst = cast<Instruction>(U.getUser());
239 const BasicBlock *DefBB = Def->getParent();
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);
248 UseBB = UserInst->getParent();
250 // Any unreachable use is dominated, even if Def == User.
251 if (!isReachableFromEntry(UseBB))
254 // Unreachable definitions don't dominate anything.
255 if (!isReachableFromEntry(DefBB))
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)) {
264 BasicBlock *NormalDest = II->getNormalDest();
265 BasicBlockEdge E(DefBB, NormalDest);
266 return dominates(E, U);
269 // If the def and use are in different blocks, do a simple CFG dominator
272 return dominates(DefBB, UseBB);
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))
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)
285 return &*I != UserInst;
288 bool DominatorTree::isReachableFromEntry(const Use &U) const {
289 Instruction *I = dyn_cast<Instruction>(U.getUser());
291 // ConstantExprs aren't really reachable from the entry block, but they
292 // don't need to be treated like unreachable code either.
295 // PHI nodes use their operands on their incoming edges.
296 if (PHINode *PN = dyn_cast<PHINode>(I))
297 return isReachableFromEntry(PN->getIncomingBlock(U));
299 // Everything else uses their operands in their own block.
300 return isReachableFromEntry(I->getParent());