blob: d6649d6c7064bcc862abb2878b26ca524585da30 [file] [log] [blame]
Chris Lattnerd43023a2002-08-02 16:43:03 +00001//===- Dominators.cpp - Dominator Calculation -----------------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner081aabc2001-07-02 05:46:38 +00009//
Chris Lattnerd43023a2002-08-02 16:43:03 +000010// 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 Lattner081aabc2001-07-02 05:46:38 +000014//
15//===----------------------------------------------------------------------===//
16
Chandler Carruth5ad5f152014-01-13 09:26:24 +000017#include "llvm/IR/Dominators.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000018#include "llvm/ADT/DepthFirstIterator.h"
Devang Patel5a1bd402007-03-27 20:50:46 +000019#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerc63d4c22007-08-08 05:51:24 +000020#include "llvm/ADT/SmallVector.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000021#include "llvm/IR/CFG.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/Instructions.h"
Dan Gohman4dbb3012009-09-28 00:27:48 +000023#include "llvm/Support/CommandLine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/Support/Compiler.h"
25#include "llvm/Support/Debug.h"
Chandler Carruthe509db42014-01-13 10:52:56 +000026#include "llvm/Support/GenericDomTreeConstruction.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/Support/raw_ostream.h"
Chris Lattnerc5e0be62004-06-05 00:24:59 +000028#include <algorithm>
Chris Lattner189d19f2003-11-21 20:23:48 +000029using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000030
Dan Gohman4dbb3012009-09-28 00:27:48 +000031// Always verify dominfo if expensive checking is enabled.
32#ifdef XDEBUG
Dan Gohmanb29cda92010-04-15 17:08:50 +000033static bool VerifyDomInfo = true;
Dan Gohman4dbb3012009-09-28 00:27:48 +000034#else
Dan Gohmanb29cda92010-04-15 17:08:50 +000035static bool VerifyDomInfo = false;
Dan Gohman4dbb3012009-09-28 00:27:48 +000036#endif
37static cl::opt<bool,true>
38VerifyDomInfoX("verify-dom-info", cl::location(VerifyDomInfo),
39 cl::desc("Verify dominator info (time consuming)"));
40
Rafael Espindolacc80cde2012-08-16 15:09:43 +000041bool 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)
46 ++NumEdgesToEnd;
47 if (NumEdgesToEnd >= 2)
48 return false;
49 }
50 assert(NumEdgesToEnd == 1);
51 return true;
Rafael Espindola11870772012-08-10 14:05:55 +000052}
53
Chris Lattnerc385beb2001-07-06 16:58:22 +000054//===----------------------------------------------------------------------===//
Owen Andersonf35a1db2007-04-15 08:47:27 +000055// DominatorTree Implementation
Chris Lattner00f51672003-12-07 00:38:08 +000056//===----------------------------------------------------------------------===//
57//
Owen Anderson84c357f2007-09-23 21:31:44 +000058// Provide public access to DominatorTree information. Implementation details
Chandler Carruthe509db42014-01-13 10:52:56 +000059// can be found in Dominators.h, GenericDomTree.h, and
60// GenericDomTreeConstruction.h.
Chris Lattner00f51672003-12-07 00:38:08 +000061//
62//===----------------------------------------------------------------------===//
63
John McCall086bb4e2009-12-19 00:55:12 +000064TEMPLATE_INSTANTIATION(class llvm::DomTreeNodeBase<BasicBlock>);
65TEMPLATE_INSTANTIATION(class llvm::DominatorTreeBase<BasicBlock>);
Owen Anderson41878012007-10-16 19:59:25 +000066
Rafael Espindola30616362014-02-14 22:36:16 +000067#define LLVM_COMMA ,
68TEMPLATE_INSTANTIATION(void llvm::Calculate<Function LLVM_COMMA BasicBlock *>(
Rafael Espindola56b663b2014-02-16 14:12:35 +000069 DominatorTreeBase<GraphTraits<BasicBlock *>::NodeType> &DT LLVM_COMMA
70 Function &F));
71TEMPLATE_INSTANTIATION(
72 void llvm::Calculate<Function LLVM_COMMA Inverse<BasicBlock *> >(
73 DominatorTreeBase<GraphTraits<Inverse<BasicBlock *> >::NodeType> &DT
74 LLVM_COMMA Function &F));
Rafael Espindola30616362014-02-14 22:36:16 +000075#undef LLVM_COMMA
76
Rafael Espindola94df2672012-02-26 02:19:19 +000077// dominates - Return true if Def dominates a use in User. This performs
78// the special checks necessary if Def and User are in the same basic block.
79// Note that Def doesn't dominate a use in Def itself!
80bool DominatorTree::dominates(const Instruction *Def,
81 const Instruction *User) const {
82 const BasicBlock *UseBB = User->getParent();
83 const BasicBlock *DefBB = Def->getParent();
Rafael Espindola082d4822012-02-18 19:46:02 +000084
Rafael Espindolaa53c46a2012-03-30 16:46:21 +000085 // Any unreachable use is dominated, even if Def == User.
86 if (!isReachableFromEntry(UseBB))
87 return true;
88
89 // Unreachable definitions don't dominate anything.
90 if (!isReachableFromEntry(DefBB))
91 return false;
Rafael Espindola082d4822012-02-18 19:46:02 +000092
Rafael Espindola94df2672012-02-26 02:19:19 +000093 // An instruction doesn't dominate a use in itself.
94 if (Def == User)
Chris Lattner22151ce2009-09-21 22:30:50 +000095 return false;
Rafael Espindola082d4822012-02-18 19:46:02 +000096
Rafael Espindola94df2672012-02-26 02:19:19 +000097 // The value defined by an invoke dominates an instruction only if
98 // it dominates every instruction in UseBB.
99 // A PHI is dominated only if the instruction dominates every possible use
100 // in the UseBB.
101 if (isa<InvokeInst>(Def) || isa<PHINode>(User))
102 return dominates(Def, UseBB);
103
104 if (DefBB != UseBB)
105 return dominates(DefBB, UseBB);
106
107 // Loop through the basic block until we find Def or User.
108 BasicBlock::const_iterator I = DefBB->begin();
109 for (; &*I != Def && &*I != User; ++I)
Chris Lattner22151ce2009-09-21 22:30:50 +0000110 /*empty*/;
Rafael Espindola082d4822012-02-18 19:46:02 +0000111
Rafael Espindola94df2672012-02-26 02:19:19 +0000112 return &*I == Def;
113}
114
115// true if Def would dominate a use in any instruction in UseBB.
116// note that dominates(Def, Def->getParent()) is false.
117bool DominatorTree::dominates(const Instruction *Def,
118 const BasicBlock *UseBB) const {
119 const BasicBlock *DefBB = Def->getParent();
120
Rafael Espindolaa53c46a2012-03-30 16:46:21 +0000121 // Any unreachable use is dominated, even if DefBB == UseBB.
122 if (!isReachableFromEntry(UseBB))
123 return true;
124
125 // Unreachable definitions don't dominate anything.
126 if (!isReachableFromEntry(DefBB))
127 return false;
Rafael Espindola94df2672012-02-26 02:19:19 +0000128
129 if (DefBB == UseBB)
130 return false;
131
132 const InvokeInst *II = dyn_cast<InvokeInst>(Def);
133 if (!II)
134 return dominates(DefBB, UseBB);
135
136 // Invoke results are only usable in the normal destination, not in the
137 // exceptional destination.
138 BasicBlock *NormalDest = II->getNormalDest();
Rafael Espindola59564072012-08-07 17:30:46 +0000139 BasicBlockEdge E(DefBB, NormalDest);
140 return dominates(E, UseBB);
141}
142
143bool DominatorTree::dominates(const BasicBlockEdge &BBE,
144 const BasicBlock *UseBB) const {
Rafael Espindola9a167352012-08-17 18:21:28 +0000145 // Assert that we have a single edge. We could handle them by simply
146 // returning false, but since isSingleEdge is linear on the number of
147 // edges, the callers can normally handle them more efficiently.
148 assert(BBE.isSingleEdge());
149
Rafael Espindola59564072012-08-07 17:30:46 +0000150 // If the BB the edge ends in doesn't dominate the use BB, then the
151 // edge also doesn't.
152 const BasicBlock *Start = BBE.getStart();
153 const BasicBlock *End = BBE.getEnd();
154 if (!dominates(End, UseBB))
Rafael Espindola94df2672012-02-26 02:19:19 +0000155 return false;
156
Rafael Espindola59564072012-08-07 17:30:46 +0000157 // Simple case: if the end BB has a single predecessor, the fact that it
158 // dominates the use block implies that the edge also does.
159 if (End->getSinglePredecessor())
Rafael Espindola94df2672012-02-26 02:19:19 +0000160 return true;
161
162 // The normal edge from the invoke is critical. Conceptually, what we would
163 // like to do is split it and check if the new block dominates the use.
164 // With X being the new block, the graph would look like:
165 //
166 // DefBB
167 // /\ . .
168 // / \ . .
169 // / \ . .
170 // / \ | |
171 // A X B C
172 // | \ | /
173 // . \|/
174 // . NormalDest
175 // .
176 //
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000177 // Given the definition of dominance, NormalDest is dominated by X iff X
Rafael Espindola94df2672012-02-26 02:19:19 +0000178 // dominates all of NormalDest's predecessors (X, B, C in the example). X
179 // trivially dominates itself, so we only have to find if it dominates the
180 // other predecessors. Since the only way out of X is via NormalDest, X can
181 // only properly dominate a node if NormalDest dominates that node too.
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +0000182 for (const_pred_iterator PI = pred_begin(End), E = pred_end(End);
183 PI != E; ++PI) {
184 const BasicBlock *BB = *PI;
Rafael Espindola59564072012-08-07 17:30:46 +0000185 if (BB == Start)
Rafael Espindola94df2672012-02-26 02:19:19 +0000186 continue;
187
Rafael Espindola59564072012-08-07 17:30:46 +0000188 if (!dominates(End, BB))
Rafael Espindola94df2672012-02-26 02:19:19 +0000189 return false;
190 }
191 return true;
Chris Lattner22151ce2009-09-21 22:30:50 +0000192}
Dan Gohman73273272012-04-12 23:31:46 +0000193
Chandler Carruth73523022014-01-13 13:07:17 +0000194bool DominatorTree::dominates(const BasicBlockEdge &BBE, const Use &U) const {
Rafael Espindola9a167352012-08-17 18:21:28 +0000195 // Assert that we have a single edge. We could handle them by simply
196 // returning false, but since isSingleEdge is linear on the number of
197 // edges, the callers can normally handle them more efficiently.
198 assert(BBE.isSingleEdge());
199
Rafael Espindola59564072012-08-07 17:30:46 +0000200 Instruction *UserInst = cast<Instruction>(U.getUser());
201 // A PHI in the end of the edge is dominated by it.
202 PHINode *PN = dyn_cast<PHINode>(UserInst);
203 if (PN && PN->getParent() == BBE.getEnd() &&
204 PN->getIncomingBlock(U) == BBE.getStart())
205 return true;
206
207 // Otherwise use the edge-dominates-block query, which
208 // handles the crazy critical edge cases properly.
209 const BasicBlock *UseBB;
210 if (PN)
211 UseBB = PN->getIncomingBlock(U);
212 else
213 UseBB = UserInst->getParent();
214 return dominates(BBE, UseBB);
215}
216
Chandler Carruth73523022014-01-13 13:07:17 +0000217bool DominatorTree::dominates(const Instruction *Def, const Use &U) const {
Rafael Espindola59564072012-08-07 17:30:46 +0000218 Instruction *UserInst = cast<Instruction>(U.getUser());
Dan Gohman73273272012-04-12 23:31:46 +0000219 const BasicBlock *DefBB = Def->getParent();
220
221 // Determine the block in which the use happens. PHI nodes use
222 // their operands on edges; simulate this by thinking of the use
223 // happening at the end of the predecessor block.
224 const BasicBlock *UseBB;
225 if (PHINode *PN = dyn_cast<PHINode>(UserInst))
226 UseBB = PN->getIncomingBlock(U);
227 else
228 UseBB = UserInst->getParent();
229
230 // Any unreachable use is dominated, even if Def == User.
231 if (!isReachableFromEntry(UseBB))
232 return true;
233
234 // Unreachable definitions don't dominate anything.
235 if (!isReachableFromEntry(DefBB))
236 return false;
237
238 // Invoke instructions define their return values on the edges
239 // to their normal successors, so we have to handle them specially.
240 // Among other things, this means they don't dominate anything in
241 // their own block, except possibly a phi, so we don't need to
242 // walk the block in any case.
243 if (const InvokeInst *II = dyn_cast<InvokeInst>(Def)) {
Rafael Espindola59564072012-08-07 17:30:46 +0000244 BasicBlock *NormalDest = II->getNormalDest();
245 BasicBlockEdge E(DefBB, NormalDest);
246 return dominates(E, U);
Dan Gohman73273272012-04-12 23:31:46 +0000247 }
248
249 // If the def and use are in different blocks, do a simple CFG dominator
250 // tree query.
251 if (DefBB != UseBB)
252 return dominates(DefBB, UseBB);
253
254 // Ok, def and use are in the same block. If the def is an invoke, it
255 // doesn't dominate anything in the block. If it's a PHI, it dominates
256 // everything in the block.
257 if (isa<PHINode>(UserInst))
258 return true;
259
260 // Otherwise, just loop through the basic block until we find Def or User.
261 BasicBlock::const_iterator I = DefBB->begin();
262 for (; &*I != Def && &*I != UserInst; ++I)
263 /*empty*/;
264
265 return &*I != UserInst;
266}
267
268bool DominatorTree::isReachableFromEntry(const Use &U) const {
269 Instruction *I = dyn_cast<Instruction>(U.getUser());
270
271 // ConstantExprs aren't really reachable from the entry block, but they
272 // don't need to be treated like unreachable code either.
273 if (!I) return true;
274
275 // PHI nodes use their operands on their incoming edges.
276 if (PHINode *PN = dyn_cast<PHINode>(I))
277 return isReachableFromEntry(PN->getIncomingBlock(U));
278
279 // Everything else uses their operands in their own block.
280 return isReachableFromEntry(I->getParent());
281}
Chandler Carruth73523022014-01-13 13:07:17 +0000282
283void DominatorTree::verifyDomTree() const {
284 if (!VerifyDomInfo)
285 return;
286
287 Function &F = *getRoot()->getParent();
288
289 DominatorTree OtherDT;
290 OtherDT.recalculate(F);
291 if (compare(OtherDT)) {
292 errs() << "DominatorTree is not up to date!\nComputed:\n";
293 print(errs());
294 errs() << "\nActual:\n";
295 OtherDT.print(errs());
296 abort();
297 }
298}
299
300//===----------------------------------------------------------------------===//
301// DominatorTreeWrapperPass Implementation
302//===----------------------------------------------------------------------===//
303//
304// The implementation details of the wrapper pass that holds a DominatorTree.
305//
306//===----------------------------------------------------------------------===//
307
308char DominatorTreeWrapperPass::ID = 0;
309INITIALIZE_PASS(DominatorTreeWrapperPass, "domtree",
310 "Dominator Tree Construction", true, true)
311
312bool DominatorTreeWrapperPass::runOnFunction(Function &F) {
313 DT.recalculate(F);
314 return false;
315}
316
317void DominatorTreeWrapperPass::verifyAnalysis() const { DT.verifyDomTree(); }
318
319void DominatorTreeWrapperPass::print(raw_ostream &OS, const Module *) const {
320 DT.print(OS);
321}
322