blob: a1160cdc83b13f85ec95b1dbfbdbcfc884cf374f [file] [log] [blame]
Chris Lattner4c9df7c2002-08-02 16:43:03 +00001//===- Dominators.cpp - Dominator Calculation -----------------------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner17152292001-07-02 05:46:38 +00009//
Chris Lattner4c9df7c2002-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 Lattner17152292001-07-02 05:46:38 +000014//
15//===----------------------------------------------------------------------===//
16
Cameron Zwarich46765992011-01-18 06:06:27 +000017#include "llvm/Analysis/Dominators.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000018#include "llvm/ADT/DepthFirstIterator.h"
Devang Patel2ad28e62007-03-27 20:50:46 +000019#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner3e089ae2007-08-08 05:51:24 +000020#include "llvm/ADT/SmallVector.h"
Owen Anderson9cb7f492007-10-03 21:25:45 +000021#include "llvm/Analysis/DominatorInternals.h"
Chris Lattner9fc5cdf2011-01-02 22:09:33 +000022#include "llvm/Assembly/Writer.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000023#include "llvm/IR/Instructions.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000024#include "llvm/Support/CFG.h"
Dan Gohman9450b0e2009-09-28 00:27:48 +000025#include "llvm/Support/CommandLine.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000026#include "llvm/Support/Compiler.h"
27#include "llvm/Support/Debug.h"
28#include "llvm/Support/raw_ostream.h"
Chris Lattner8e727492004-06-05 00:24:59 +000029#include <algorithm>
Chris Lattner31f84992003-11-21 20:23:48 +000030using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000031
Dan Gohman9450b0e2009-09-28 00:27:48 +000032// Always verify dominfo if expensive checking is enabled.
33#ifdef XDEBUG
Dan Gohmanb3579832010-04-15 17:08:50 +000034static bool VerifyDomInfo = true;
Dan Gohman9450b0e2009-09-28 00:27:48 +000035#else
Dan Gohmanb3579832010-04-15 17:08:50 +000036static bool VerifyDomInfo = false;
Dan Gohman9450b0e2009-09-28 00:27:48 +000037#endif
38static cl::opt<bool,true>
39VerifyDomInfoX("verify-dom-info", cl::location(VerifyDomInfo),
40 cl::desc("Verify dominator info (time consuming)"));
41
Rafael Espindola05130592012-08-16 15:09:43 +000042bool 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 Espindola25ac7512012-08-10 14:05:55 +000053}
54
Chris Lattner94108ab2001-07-06 16:58:22 +000055//===----------------------------------------------------------------------===//
Owen Anderson3dc67762007-04-15 08:47:27 +000056// DominatorTree Implementation
Chris Lattner16addf82003-12-07 00:38:08 +000057//===----------------------------------------------------------------------===//
58//
Owen Andersond20c8242007-09-23 21:31:44 +000059// Provide public access to DominatorTree information. Implementation details
Cameron Zwaricha9ba4562011-01-20 03:58:43 +000060// can be found in DominatorInternals.h.
Chris Lattner16addf82003-12-07 00:38:08 +000061//
62//===----------------------------------------------------------------------===//
63
John McCallc63ca0a2009-12-19 00:55:12 +000064TEMPLATE_INSTANTIATION(class llvm::DomTreeNodeBase<BasicBlock>);
65TEMPLATE_INSTANTIATION(class llvm::DominatorTreeBase<BasicBlock>);
Owen Anderson49b653a2007-10-16 19:59:25 +000066
Devang Patel19974732007-05-03 01:11:54 +000067char DominatorTree::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +000068INITIALIZE_PASS(DominatorTree, "domtree",
Owen Andersonce665bd2010-10-07 22:25:06 +000069 "Dominator Tree Construction", true, true)
Chris Lattner16addf82003-12-07 00:38:08 +000070
Owen Anderson3dc67762007-04-15 08:47:27 +000071bool DominatorTree::runOnFunction(Function &F) {
Owen Andersond20cc142007-10-23 20:58:37 +000072 DT->recalculate(F);
Owen Anderson3dc67762007-04-15 08:47:27 +000073 return false;
74}
Chris Lattner17152292001-07-02 05:46:38 +000075
Dan Gohman9450b0e2009-09-28 00:27:48 +000076void DominatorTree::verifyAnalysis() const {
Dan Gohmana9245d62009-09-28 00:44:15 +000077 if (!VerifyDomInfo) return;
Dan Gohman9450b0e2009-09-28 00:27:48 +000078
79 Function &F = *getRoot()->getParent();
80
81 DominatorTree OtherDT;
82 OtherDT.getBase().recalculate(F);
Chris Lattner57863b82011-01-08 19:55:55 +000083 if (compare(OtherDT)) {
Bill Wendling0f415872011-03-29 04:28:26 +000084 errs() << "DominatorTree is not up to date!\nComputed:\n";
Chris Lattner57863b82011-01-08 19:55:55 +000085 print(errs());
Chris Lattner57863b82011-01-08 19:55:55 +000086 errs() << "\nActual:\n";
87 OtherDT.print(errs());
88 abort();
89 }
Dan Gohman9450b0e2009-09-28 00:27:48 +000090}
91
Chris Lattner45cfe542009-08-23 06:03:38 +000092void DominatorTree::print(raw_ostream &OS, const Module *) const {
93 DT->print(OS);
Chris Lattner791102f2009-08-23 05:17:37 +000094}
95
Rafael Espindolac9ae8cc2012-02-26 02:19:19 +000096// dominates - Return true if Def dominates a use in User. This performs
97// the special checks necessary if Def and User are in the same basic block.
98// Note that Def doesn't dominate a use in Def itself!
99bool DominatorTree::dominates(const Instruction *Def,
100 const Instruction *User) const {
101 const BasicBlock *UseBB = User->getParent();
102 const BasicBlock *DefBB = Def->getParent();
Rafael Espindolab155c232012-02-18 19:46:02 +0000103
Rafael Espindola092c5cc2012-03-30 16:46:21 +0000104 // Any unreachable use is dominated, even if Def == User.
105 if (!isReachableFromEntry(UseBB))
106 return true;
107
108 // Unreachable definitions don't dominate anything.
109 if (!isReachableFromEntry(DefBB))
110 return false;
Rafael Espindolab155c232012-02-18 19:46:02 +0000111
Rafael Espindolac9ae8cc2012-02-26 02:19:19 +0000112 // An instruction doesn't dominate a use in itself.
113 if (Def == User)
Chris Lattner75c7c992009-09-21 22:30:50 +0000114 return false;
Rafael Espindolab155c232012-02-18 19:46:02 +0000115
Rafael Espindolac9ae8cc2012-02-26 02:19:19 +0000116 // The value defined by an invoke dominates an instruction only if
117 // it dominates every instruction in UseBB.
118 // A PHI is dominated only if the instruction dominates every possible use
119 // in the UseBB.
120 if (isa<InvokeInst>(Def) || isa<PHINode>(User))
121 return dominates(Def, UseBB);
122
123 if (DefBB != UseBB)
124 return dominates(DefBB, UseBB);
125
126 // Loop through the basic block until we find Def or User.
127 BasicBlock::const_iterator I = DefBB->begin();
128 for (; &*I != Def && &*I != User; ++I)
Chris Lattner75c7c992009-09-21 22:30:50 +0000129 /*empty*/;
Rafael Espindolab155c232012-02-18 19:46:02 +0000130
Rafael Espindolac9ae8cc2012-02-26 02:19:19 +0000131 return &*I == Def;
132}
133
134// true if Def would dominate a use in any instruction in UseBB.
135// note that dominates(Def, Def->getParent()) is false.
136bool DominatorTree::dominates(const Instruction *Def,
137 const BasicBlock *UseBB) const {
138 const BasicBlock *DefBB = Def->getParent();
139
Rafael Espindola092c5cc2012-03-30 16:46:21 +0000140 // Any unreachable use is dominated, even if DefBB == UseBB.
141 if (!isReachableFromEntry(UseBB))
142 return true;
143
144 // Unreachable definitions don't dominate anything.
145 if (!isReachableFromEntry(DefBB))
146 return false;
Rafael Espindolac9ae8cc2012-02-26 02:19:19 +0000147
148 if (DefBB == UseBB)
149 return false;
150
151 const InvokeInst *II = dyn_cast<InvokeInst>(Def);
152 if (!II)
153 return dominates(DefBB, UseBB);
154
155 // Invoke results are only usable in the normal destination, not in the
156 // exceptional destination.
157 BasicBlock *NormalDest = II->getNormalDest();
Rafael Espindola702bcce2012-08-07 17:30:46 +0000158 BasicBlockEdge E(DefBB, NormalDest);
159 return dominates(E, UseBB);
160}
161
162bool DominatorTree::dominates(const BasicBlockEdge &BBE,
163 const BasicBlock *UseBB) const {
Rafael Espindolad5118c82012-08-17 18:21:28 +0000164 // Assert that we have a single edge. We could handle them by simply
165 // returning false, but since isSingleEdge is linear on the number of
166 // edges, the callers can normally handle them more efficiently.
167 assert(BBE.isSingleEdge());
168
Rafael Espindola702bcce2012-08-07 17:30:46 +0000169 // If the BB the edge ends in doesn't dominate the use BB, then the
170 // edge also doesn't.
171 const BasicBlock *Start = BBE.getStart();
172 const BasicBlock *End = BBE.getEnd();
173 if (!dominates(End, UseBB))
Rafael Espindolac9ae8cc2012-02-26 02:19:19 +0000174 return false;
175
Rafael Espindola702bcce2012-08-07 17:30:46 +0000176 // Simple case: if the end BB has a single predecessor, the fact that it
177 // dominates the use block implies that the edge also does.
178 if (End->getSinglePredecessor())
Rafael Espindolac9ae8cc2012-02-26 02:19:19 +0000179 return true;
180
181 // The normal edge from the invoke is critical. Conceptually, what we would
182 // like to do is split it and check if the new block dominates the use.
183 // With X being the new block, the graph would look like:
184 //
185 // DefBB
186 // /\ . .
187 // / \ . .
188 // / \ . .
189 // / \ | |
190 // A X B C
191 // | \ | /
192 // . \|/
193 // . NormalDest
194 // .
195 //
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000196 // Given the definition of dominance, NormalDest is dominated by X iff X
Rafael Espindolac9ae8cc2012-02-26 02:19:19 +0000197 // dominates all of NormalDest's predecessors (X, B, C in the example). X
198 // trivially dominates itself, so we only have to find if it dominates the
199 // other predecessors. Since the only way out of X is via NormalDest, X can
200 // only properly dominate a node if NormalDest dominates that node too.
Rafael Espindola702bcce2012-08-07 17:30:46 +0000201 for (const_pred_iterator PI = pred_begin(End), E = pred_end(End);
202 PI != E; ++PI) {
Rafael Espindolac9ae8cc2012-02-26 02:19:19 +0000203 const BasicBlock *BB = *PI;
Rafael Espindola702bcce2012-08-07 17:30:46 +0000204 if (BB == Start)
Rafael Espindolac9ae8cc2012-02-26 02:19:19 +0000205 continue;
206
Rafael Espindola702bcce2012-08-07 17:30:46 +0000207 if (!dominates(End, BB))
Rafael Espindolac9ae8cc2012-02-26 02:19:19 +0000208 return false;
209 }
210 return true;
Chris Lattner75c7c992009-09-21 22:30:50 +0000211}
Dan Gohman558ece22012-04-12 23:31:46 +0000212
Rafael Espindola702bcce2012-08-07 17:30:46 +0000213bool DominatorTree::dominates(const BasicBlockEdge &BBE,
214 const Use &U) const {
Rafael Espindolad5118c82012-08-17 18:21:28 +0000215 // Assert that we have a single edge. We could handle them by simply
216 // returning false, but since isSingleEdge is linear on the number of
217 // edges, the callers can normally handle them more efficiently.
218 assert(BBE.isSingleEdge());
219
Rafael Espindola702bcce2012-08-07 17:30:46 +0000220 Instruction *UserInst = cast<Instruction>(U.getUser());
221 // A PHI in the end of the edge is dominated by it.
222 PHINode *PN = dyn_cast<PHINode>(UserInst);
223 if (PN && PN->getParent() == BBE.getEnd() &&
224 PN->getIncomingBlock(U) == BBE.getStart())
225 return true;
226
227 // Otherwise use the edge-dominates-block query, which
228 // handles the crazy critical edge cases properly.
229 const BasicBlock *UseBB;
230 if (PN)
231 UseBB = PN->getIncomingBlock(U);
232 else
233 UseBB = UserInst->getParent();
234 return dominates(BBE, UseBB);
235}
236
Dan Gohman558ece22012-04-12 23:31:46 +0000237bool DominatorTree::dominates(const Instruction *Def,
238 const Use &U) const {
Rafael Espindola702bcce2012-08-07 17:30:46 +0000239 Instruction *UserInst = cast<Instruction>(U.getUser());
Dan Gohman558ece22012-04-12 23:31:46 +0000240 const BasicBlock *DefBB = Def->getParent();
241
242 // Determine the block in which the use happens. PHI nodes use
243 // their operands on edges; simulate this by thinking of the use
244 // happening at the end of the predecessor block.
245 const BasicBlock *UseBB;
246 if (PHINode *PN = dyn_cast<PHINode>(UserInst))
247 UseBB = PN->getIncomingBlock(U);
248 else
249 UseBB = UserInst->getParent();
250
251 // Any unreachable use is dominated, even if Def == User.
252 if (!isReachableFromEntry(UseBB))
253 return true;
254
255 // Unreachable definitions don't dominate anything.
256 if (!isReachableFromEntry(DefBB))
257 return false;
258
259 // Invoke instructions define their return values on the edges
260 // to their normal successors, so we have to handle them specially.
261 // Among other things, this means they don't dominate anything in
262 // their own block, except possibly a phi, so we don't need to
263 // walk the block in any case.
264 if (const InvokeInst *II = dyn_cast<InvokeInst>(Def)) {
Rafael Espindola702bcce2012-08-07 17:30:46 +0000265 BasicBlock *NormalDest = II->getNormalDest();
266 BasicBlockEdge E(DefBB, NormalDest);
267 return dominates(E, U);
Dan Gohman558ece22012-04-12 23:31:46 +0000268 }
269
270 // If the def and use are in different blocks, do a simple CFG dominator
271 // tree query.
272 if (DefBB != UseBB)
273 return dominates(DefBB, UseBB);
274
275 // Ok, def and use are in the same block. If the def is an invoke, it
276 // doesn't dominate anything in the block. If it's a PHI, it dominates
277 // everything in the block.
278 if (isa<PHINode>(UserInst))
279 return true;
280
281 // Otherwise, just loop through the basic block until we find Def or User.
282 BasicBlock::const_iterator I = DefBB->begin();
283 for (; &*I != Def && &*I != UserInst; ++I)
284 /*empty*/;
285
286 return &*I != UserInst;
287}
288
289bool DominatorTree::isReachableFromEntry(const Use &U) const {
290 Instruction *I = dyn_cast<Instruction>(U.getUser());
291
292 // ConstantExprs aren't really reachable from the entry block, but they
293 // don't need to be treated like unreachable code either.
294 if (!I) return true;
295
296 // PHI nodes use their operands on their incoming edges.
297 if (PHINode *PN = dyn_cast<PHINode>(I))
298 return isReachableFromEntry(PN->getIncomingBlock(U));
299
300 // Everything else uses their operands in their own block.
301 return isReachableFromEntry(I->getParent());
302}