xref: /aosp_15_r20/external/llvm/lib/IR/Dominators.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===- Dominators.cpp - Dominator Calculation -----------------------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements simple dominator construction algorithms for finding
11*9880d681SAndroid Build Coastguard Worker // forward dominators.  Postdominators are available in libanalysis, but are not
12*9880d681SAndroid Build Coastguard Worker // included in libvmcore, because it's not needed.  Forward dominators are
13*9880d681SAndroid Build Coastguard Worker // needed to support the Verifier pass.
14*9880d681SAndroid Build Coastguard Worker //
15*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
16*9880d681SAndroid Build Coastguard Worker 
17*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Dominators.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/DepthFirstIterator.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallPtrSet.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/CFG.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Instructions.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/PassManager.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/CommandLine.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/GenericDomTreeConstruction.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
27*9880d681SAndroid Build Coastguard Worker #include <algorithm>
28*9880d681SAndroid Build Coastguard Worker using namespace llvm;
29*9880d681SAndroid Build Coastguard Worker 
30*9880d681SAndroid Build Coastguard Worker // Always verify dominfo if expensive checking is enabled.
31*9880d681SAndroid Build Coastguard Worker #ifdef EXPENSIVE_CHECKS
32*9880d681SAndroid Build Coastguard Worker static bool VerifyDomInfo = true;
33*9880d681SAndroid Build Coastguard Worker #else
34*9880d681SAndroid Build Coastguard Worker static bool VerifyDomInfo = false;
35*9880d681SAndroid Build Coastguard Worker #endif
36*9880d681SAndroid Build Coastguard Worker static cl::opt<bool,true>
37*9880d681SAndroid Build Coastguard Worker VerifyDomInfoX("verify-dom-info", cl::location(VerifyDomInfo),
38*9880d681SAndroid Build Coastguard Worker                cl::desc("Verify dominator info (time consuming)"));
39*9880d681SAndroid Build Coastguard Worker 
isSingleEdge() const40*9880d681SAndroid Build Coastguard Worker bool BasicBlockEdge::isSingleEdge() const {
41*9880d681SAndroid Build Coastguard Worker   const TerminatorInst *TI = Start->getTerminator();
42*9880d681SAndroid Build Coastguard Worker   unsigned NumEdgesToEnd = 0;
43*9880d681SAndroid Build Coastguard Worker   for (unsigned int i = 0, n = TI->getNumSuccessors(); i < n; ++i) {
44*9880d681SAndroid Build Coastguard Worker     if (TI->getSuccessor(i) == End)
45*9880d681SAndroid Build Coastguard Worker       ++NumEdgesToEnd;
46*9880d681SAndroid Build Coastguard Worker     if (NumEdgesToEnd >= 2)
47*9880d681SAndroid Build Coastguard Worker       return false;
48*9880d681SAndroid Build Coastguard Worker   }
49*9880d681SAndroid Build Coastguard Worker   assert(NumEdgesToEnd == 1);
50*9880d681SAndroid Build Coastguard Worker   return true;
51*9880d681SAndroid Build Coastguard Worker }
52*9880d681SAndroid Build Coastguard Worker 
53*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
54*9880d681SAndroid Build Coastguard Worker //  DominatorTree Implementation
55*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
56*9880d681SAndroid Build Coastguard Worker //
57*9880d681SAndroid Build Coastguard Worker // Provide public access to DominatorTree information.  Implementation details
58*9880d681SAndroid Build Coastguard Worker // can be found in Dominators.h, GenericDomTree.h, and
59*9880d681SAndroid Build Coastguard Worker // GenericDomTreeConstruction.h.
60*9880d681SAndroid Build Coastguard Worker //
61*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
62*9880d681SAndroid Build Coastguard Worker 
63*9880d681SAndroid Build Coastguard Worker template class llvm::DomTreeNodeBase<BasicBlock>;
64*9880d681SAndroid Build Coastguard Worker template class llvm::DominatorTreeBase<BasicBlock>;
65*9880d681SAndroid Build Coastguard Worker 
66*9880d681SAndroid Build Coastguard Worker template void llvm::Calculate<Function, BasicBlock *>(
67*9880d681SAndroid Build Coastguard Worker     DominatorTreeBase<GraphTraits<BasicBlock *>::NodeType> &DT, Function &F);
68*9880d681SAndroid Build Coastguard Worker template void llvm::Calculate<Function, Inverse<BasicBlock *>>(
69*9880d681SAndroid Build Coastguard Worker     DominatorTreeBase<GraphTraits<Inverse<BasicBlock *>>::NodeType> &DT,
70*9880d681SAndroid Build Coastguard Worker     Function &F);
71*9880d681SAndroid Build Coastguard Worker 
72*9880d681SAndroid Build Coastguard Worker // dominates - Return true if Def dominates a use in User. This performs
73*9880d681SAndroid Build Coastguard Worker // the special checks necessary if Def and User are in the same basic block.
74*9880d681SAndroid Build Coastguard Worker // Note that Def doesn't dominate a use in Def itself!
dominates(const Instruction * Def,const Instruction * User) const75*9880d681SAndroid Build Coastguard Worker bool DominatorTree::dominates(const Instruction *Def,
76*9880d681SAndroid Build Coastguard Worker                               const Instruction *User) const {
77*9880d681SAndroid Build Coastguard Worker   const BasicBlock *UseBB = User->getParent();
78*9880d681SAndroid Build Coastguard Worker   const BasicBlock *DefBB = Def->getParent();
79*9880d681SAndroid Build Coastguard Worker 
80*9880d681SAndroid Build Coastguard Worker   // Any unreachable use is dominated, even if Def == User.
81*9880d681SAndroid Build Coastguard Worker   if (!isReachableFromEntry(UseBB))
82*9880d681SAndroid Build Coastguard Worker     return true;
83*9880d681SAndroid Build Coastguard Worker 
84*9880d681SAndroid Build Coastguard Worker   // Unreachable definitions don't dominate anything.
85*9880d681SAndroid Build Coastguard Worker   if (!isReachableFromEntry(DefBB))
86*9880d681SAndroid Build Coastguard Worker     return false;
87*9880d681SAndroid Build Coastguard Worker 
88*9880d681SAndroid Build Coastguard Worker   // An instruction doesn't dominate a use in itself.
89*9880d681SAndroid Build Coastguard Worker   if (Def == User)
90*9880d681SAndroid Build Coastguard Worker     return false;
91*9880d681SAndroid Build Coastguard Worker 
92*9880d681SAndroid Build Coastguard Worker   // The value defined by an invoke dominates an instruction only if it
93*9880d681SAndroid Build Coastguard Worker   // dominates every instruction in UseBB.
94*9880d681SAndroid Build Coastguard Worker   // A PHI is dominated only if the instruction dominates every possible use in
95*9880d681SAndroid Build Coastguard Worker   // the UseBB.
96*9880d681SAndroid Build Coastguard Worker   if (isa<InvokeInst>(Def) || isa<PHINode>(User))
97*9880d681SAndroid Build Coastguard Worker     return dominates(Def, UseBB);
98*9880d681SAndroid Build Coastguard Worker 
99*9880d681SAndroid Build Coastguard Worker   if (DefBB != UseBB)
100*9880d681SAndroid Build Coastguard Worker     return dominates(DefBB, UseBB);
101*9880d681SAndroid Build Coastguard Worker 
102*9880d681SAndroid Build Coastguard Worker   // Loop through the basic block until we find Def or User.
103*9880d681SAndroid Build Coastguard Worker   BasicBlock::const_iterator I = DefBB->begin();
104*9880d681SAndroid Build Coastguard Worker   for (; &*I != Def && &*I != User; ++I)
105*9880d681SAndroid Build Coastguard Worker     /*empty*/;
106*9880d681SAndroid Build Coastguard Worker 
107*9880d681SAndroid Build Coastguard Worker   return &*I == Def;
108*9880d681SAndroid Build Coastguard Worker }
109*9880d681SAndroid Build Coastguard Worker 
110*9880d681SAndroid Build Coastguard Worker // true if Def would dominate a use in any instruction in UseBB.
111*9880d681SAndroid Build Coastguard Worker // note that dominates(Def, Def->getParent()) is false.
dominates(const Instruction * Def,const BasicBlock * UseBB) const112*9880d681SAndroid Build Coastguard Worker bool DominatorTree::dominates(const Instruction *Def,
113*9880d681SAndroid Build Coastguard Worker                               const BasicBlock *UseBB) const {
114*9880d681SAndroid Build Coastguard Worker   const BasicBlock *DefBB = Def->getParent();
115*9880d681SAndroid Build Coastguard Worker 
116*9880d681SAndroid Build Coastguard Worker   // Any unreachable use is dominated, even if DefBB == UseBB.
117*9880d681SAndroid Build Coastguard Worker   if (!isReachableFromEntry(UseBB))
118*9880d681SAndroid Build Coastguard Worker     return true;
119*9880d681SAndroid Build Coastguard Worker 
120*9880d681SAndroid Build Coastguard Worker   // Unreachable definitions don't dominate anything.
121*9880d681SAndroid Build Coastguard Worker   if (!isReachableFromEntry(DefBB))
122*9880d681SAndroid Build Coastguard Worker     return false;
123*9880d681SAndroid Build Coastguard Worker 
124*9880d681SAndroid Build Coastguard Worker   if (DefBB == UseBB)
125*9880d681SAndroid Build Coastguard Worker     return false;
126*9880d681SAndroid Build Coastguard Worker 
127*9880d681SAndroid Build Coastguard Worker   // Invoke results are only usable in the normal destination, not in the
128*9880d681SAndroid Build Coastguard Worker   // exceptional destination.
129*9880d681SAndroid Build Coastguard Worker   if (const auto *II = dyn_cast<InvokeInst>(Def)) {
130*9880d681SAndroid Build Coastguard Worker     BasicBlock *NormalDest = II->getNormalDest();
131*9880d681SAndroid Build Coastguard Worker     BasicBlockEdge E(DefBB, NormalDest);
132*9880d681SAndroid Build Coastguard Worker     return dominates(E, UseBB);
133*9880d681SAndroid Build Coastguard Worker   }
134*9880d681SAndroid Build Coastguard Worker 
135*9880d681SAndroid Build Coastguard Worker   return dominates(DefBB, UseBB);
136*9880d681SAndroid Build Coastguard Worker }
137*9880d681SAndroid Build Coastguard Worker 
dominates(const BasicBlockEdge & BBE,const BasicBlock * UseBB) const138*9880d681SAndroid Build Coastguard Worker bool DominatorTree::dominates(const BasicBlockEdge &BBE,
139*9880d681SAndroid Build Coastguard Worker                               const BasicBlock *UseBB) const {
140*9880d681SAndroid Build Coastguard Worker   // Assert that we have a single edge. We could handle them by simply
141*9880d681SAndroid Build Coastguard Worker   // returning false, but since isSingleEdge is linear on the number of
142*9880d681SAndroid Build Coastguard Worker   // edges, the callers can normally handle them more efficiently.
143*9880d681SAndroid Build Coastguard Worker   assert(BBE.isSingleEdge() &&
144*9880d681SAndroid Build Coastguard Worker          "This function is not efficient in handling multiple edges");
145*9880d681SAndroid Build Coastguard Worker 
146*9880d681SAndroid Build Coastguard Worker   // If the BB the edge ends in doesn't dominate the use BB, then the
147*9880d681SAndroid Build Coastguard Worker   // edge also doesn't.
148*9880d681SAndroid Build Coastguard Worker   const BasicBlock *Start = BBE.getStart();
149*9880d681SAndroid Build Coastguard Worker   const BasicBlock *End = BBE.getEnd();
150*9880d681SAndroid Build Coastguard Worker   if (!dominates(End, UseBB))
151*9880d681SAndroid Build Coastguard Worker     return false;
152*9880d681SAndroid Build Coastguard Worker 
153*9880d681SAndroid Build Coastguard Worker   // Simple case: if the end BB has a single predecessor, the fact that it
154*9880d681SAndroid Build Coastguard Worker   // dominates the use block implies that the edge also does.
155*9880d681SAndroid Build Coastguard Worker   if (End->getSinglePredecessor())
156*9880d681SAndroid Build Coastguard Worker     return true;
157*9880d681SAndroid Build Coastguard Worker 
158*9880d681SAndroid Build Coastguard Worker   // The normal edge from the invoke is critical. Conceptually, what we would
159*9880d681SAndroid Build Coastguard Worker   // like to do is split it and check if the new block dominates the use.
160*9880d681SAndroid Build Coastguard Worker   // With X being the new block, the graph would look like:
161*9880d681SAndroid Build Coastguard Worker   //
162*9880d681SAndroid Build Coastguard Worker   //        DefBB
163*9880d681SAndroid Build Coastguard Worker   //          /\      .  .
164*9880d681SAndroid Build Coastguard Worker   //         /  \     .  .
165*9880d681SAndroid Build Coastguard Worker   //        /    \    .  .
166*9880d681SAndroid Build Coastguard Worker   //       /      \   |  |
167*9880d681SAndroid Build Coastguard Worker   //      A        X  B  C
168*9880d681SAndroid Build Coastguard Worker   //      |         \ | /
169*9880d681SAndroid Build Coastguard Worker   //      .          \|/
170*9880d681SAndroid Build Coastguard Worker   //      .      NormalDest
171*9880d681SAndroid Build Coastguard Worker   //      .
172*9880d681SAndroid Build Coastguard Worker   //
173*9880d681SAndroid Build Coastguard Worker   // Given the definition of dominance, NormalDest is dominated by X iff X
174*9880d681SAndroid Build Coastguard Worker   // dominates all of NormalDest's predecessors (X, B, C in the example). X
175*9880d681SAndroid Build Coastguard Worker   // trivially dominates itself, so we only have to find if it dominates the
176*9880d681SAndroid Build Coastguard Worker   // other predecessors. Since the only way out of X is via NormalDest, X can
177*9880d681SAndroid Build Coastguard Worker   // only properly dominate a node if NormalDest dominates that node too.
178*9880d681SAndroid Build Coastguard Worker   for (const_pred_iterator PI = pred_begin(End), E = pred_end(End);
179*9880d681SAndroid Build Coastguard Worker        PI != E; ++PI) {
180*9880d681SAndroid Build Coastguard Worker     const BasicBlock *BB = *PI;
181*9880d681SAndroid Build Coastguard Worker     if (BB == Start)
182*9880d681SAndroid Build Coastguard Worker       continue;
183*9880d681SAndroid Build Coastguard Worker 
184*9880d681SAndroid Build Coastguard Worker     if (!dominates(End, BB))
185*9880d681SAndroid Build Coastguard Worker       return false;
186*9880d681SAndroid Build Coastguard Worker   }
187*9880d681SAndroid Build Coastguard Worker   return true;
188*9880d681SAndroid Build Coastguard Worker }
189*9880d681SAndroid Build Coastguard Worker 
dominates(const BasicBlockEdge & BBE,const Use & U) const190*9880d681SAndroid Build Coastguard Worker bool DominatorTree::dominates(const BasicBlockEdge &BBE, const Use &U) const {
191*9880d681SAndroid Build Coastguard Worker   // Assert that we have a single edge. We could handle them by simply
192*9880d681SAndroid Build Coastguard Worker   // returning false, but since isSingleEdge is linear on the number of
193*9880d681SAndroid Build Coastguard Worker   // edges, the callers can normally handle them more efficiently.
194*9880d681SAndroid Build Coastguard Worker   assert(BBE.isSingleEdge() &&
195*9880d681SAndroid Build Coastguard Worker          "This function is not efficient in handling multiple edges");
196*9880d681SAndroid Build Coastguard Worker 
197*9880d681SAndroid Build Coastguard Worker   Instruction *UserInst = cast<Instruction>(U.getUser());
198*9880d681SAndroid Build Coastguard Worker   // A PHI in the end of the edge is dominated by it.
199*9880d681SAndroid Build Coastguard Worker   PHINode *PN = dyn_cast<PHINode>(UserInst);
200*9880d681SAndroid Build Coastguard Worker   if (PN && PN->getParent() == BBE.getEnd() &&
201*9880d681SAndroid Build Coastguard Worker       PN->getIncomingBlock(U) == BBE.getStart())
202*9880d681SAndroid Build Coastguard Worker     return true;
203*9880d681SAndroid Build Coastguard Worker 
204*9880d681SAndroid Build Coastguard Worker   // Otherwise use the edge-dominates-block query, which
205*9880d681SAndroid Build Coastguard Worker   // handles the crazy critical edge cases properly.
206*9880d681SAndroid Build Coastguard Worker   const BasicBlock *UseBB;
207*9880d681SAndroid Build Coastguard Worker   if (PN)
208*9880d681SAndroid Build Coastguard Worker     UseBB = PN->getIncomingBlock(U);
209*9880d681SAndroid Build Coastguard Worker   else
210*9880d681SAndroid Build Coastguard Worker     UseBB = UserInst->getParent();
211*9880d681SAndroid Build Coastguard Worker   return dominates(BBE, UseBB);
212*9880d681SAndroid Build Coastguard Worker }
213*9880d681SAndroid Build Coastguard Worker 
dominates(const Instruction * Def,const Use & U) const214*9880d681SAndroid Build Coastguard Worker bool DominatorTree::dominates(const Instruction *Def, const Use &U) const {
215*9880d681SAndroid Build Coastguard Worker   Instruction *UserInst = cast<Instruction>(U.getUser());
216*9880d681SAndroid Build Coastguard Worker   const BasicBlock *DefBB = Def->getParent();
217*9880d681SAndroid Build Coastguard Worker 
218*9880d681SAndroid Build Coastguard Worker   // Determine the block in which the use happens. PHI nodes use
219*9880d681SAndroid Build Coastguard Worker   // their operands on edges; simulate this by thinking of the use
220*9880d681SAndroid Build Coastguard Worker   // happening at the end of the predecessor block.
221*9880d681SAndroid Build Coastguard Worker   const BasicBlock *UseBB;
222*9880d681SAndroid Build Coastguard Worker   if (PHINode *PN = dyn_cast<PHINode>(UserInst))
223*9880d681SAndroid Build Coastguard Worker     UseBB = PN->getIncomingBlock(U);
224*9880d681SAndroid Build Coastguard Worker   else
225*9880d681SAndroid Build Coastguard Worker     UseBB = UserInst->getParent();
226*9880d681SAndroid Build Coastguard Worker 
227*9880d681SAndroid Build Coastguard Worker   // Any unreachable use is dominated, even if Def == User.
228*9880d681SAndroid Build Coastguard Worker   if (!isReachableFromEntry(UseBB))
229*9880d681SAndroid Build Coastguard Worker     return true;
230*9880d681SAndroid Build Coastguard Worker 
231*9880d681SAndroid Build Coastguard Worker   // Unreachable definitions don't dominate anything.
232*9880d681SAndroid Build Coastguard Worker   if (!isReachableFromEntry(DefBB))
233*9880d681SAndroid Build Coastguard Worker     return false;
234*9880d681SAndroid Build Coastguard Worker 
235*9880d681SAndroid Build Coastguard Worker   // Invoke instructions define their return values on the edges to their normal
236*9880d681SAndroid Build Coastguard Worker   // successors, so we have to handle them specially.
237*9880d681SAndroid Build Coastguard Worker   // Among other things, this means they don't dominate anything in
238*9880d681SAndroid Build Coastguard Worker   // their own block, except possibly a phi, so we don't need to
239*9880d681SAndroid Build Coastguard Worker   // walk the block in any case.
240*9880d681SAndroid Build Coastguard Worker   if (const InvokeInst *II = dyn_cast<InvokeInst>(Def)) {
241*9880d681SAndroid Build Coastguard Worker     BasicBlock *NormalDest = II->getNormalDest();
242*9880d681SAndroid Build Coastguard Worker     BasicBlockEdge E(DefBB, NormalDest);
243*9880d681SAndroid Build Coastguard Worker     return dominates(E, U);
244*9880d681SAndroid Build Coastguard Worker   }
245*9880d681SAndroid Build Coastguard Worker 
246*9880d681SAndroid Build Coastguard Worker   // If the def and use are in different blocks, do a simple CFG dominator
247*9880d681SAndroid Build Coastguard Worker   // tree query.
248*9880d681SAndroid Build Coastguard Worker   if (DefBB != UseBB)
249*9880d681SAndroid Build Coastguard Worker     return dominates(DefBB, UseBB);
250*9880d681SAndroid Build Coastguard Worker 
251*9880d681SAndroid Build Coastguard Worker   // Ok, def and use are in the same block. If the def is an invoke, it
252*9880d681SAndroid Build Coastguard Worker   // doesn't dominate anything in the block. If it's a PHI, it dominates
253*9880d681SAndroid Build Coastguard Worker   // everything in the block.
254*9880d681SAndroid Build Coastguard Worker   if (isa<PHINode>(UserInst))
255*9880d681SAndroid Build Coastguard Worker     return true;
256*9880d681SAndroid Build Coastguard Worker 
257*9880d681SAndroid Build Coastguard Worker   // Otherwise, just loop through the basic block until we find Def or User.
258*9880d681SAndroid Build Coastguard Worker   BasicBlock::const_iterator I = DefBB->begin();
259*9880d681SAndroid Build Coastguard Worker   for (; &*I != Def && &*I != UserInst; ++I)
260*9880d681SAndroid Build Coastguard Worker     /*empty*/;
261*9880d681SAndroid Build Coastguard Worker 
262*9880d681SAndroid Build Coastguard Worker   return &*I != UserInst;
263*9880d681SAndroid Build Coastguard Worker }
264*9880d681SAndroid Build Coastguard Worker 
isReachableFromEntry(const Use & U) const265*9880d681SAndroid Build Coastguard Worker bool DominatorTree::isReachableFromEntry(const Use &U) const {
266*9880d681SAndroid Build Coastguard Worker   Instruction *I = dyn_cast<Instruction>(U.getUser());
267*9880d681SAndroid Build Coastguard Worker 
268*9880d681SAndroid Build Coastguard Worker   // ConstantExprs aren't really reachable from the entry block, but they
269*9880d681SAndroid Build Coastguard Worker   // don't need to be treated like unreachable code either.
270*9880d681SAndroid Build Coastguard Worker   if (!I) return true;
271*9880d681SAndroid Build Coastguard Worker 
272*9880d681SAndroid Build Coastguard Worker   // PHI nodes use their operands on their incoming edges.
273*9880d681SAndroid Build Coastguard Worker   if (PHINode *PN = dyn_cast<PHINode>(I))
274*9880d681SAndroid Build Coastguard Worker     return isReachableFromEntry(PN->getIncomingBlock(U));
275*9880d681SAndroid Build Coastguard Worker 
276*9880d681SAndroid Build Coastguard Worker   // Everything else uses their operands in their own block.
277*9880d681SAndroid Build Coastguard Worker   return isReachableFromEntry(I->getParent());
278*9880d681SAndroid Build Coastguard Worker }
279*9880d681SAndroid Build Coastguard Worker 
verifyDomTree() const280*9880d681SAndroid Build Coastguard Worker void DominatorTree::verifyDomTree() const {
281*9880d681SAndroid Build Coastguard Worker   Function &F = *getRoot()->getParent();
282*9880d681SAndroid Build Coastguard Worker 
283*9880d681SAndroid Build Coastguard Worker   DominatorTree OtherDT;
284*9880d681SAndroid Build Coastguard Worker   OtherDT.recalculate(F);
285*9880d681SAndroid Build Coastguard Worker   if (compare(OtherDT)) {
286*9880d681SAndroid Build Coastguard Worker     errs() << "DominatorTree is not up to date!\nComputed:\n";
287*9880d681SAndroid Build Coastguard Worker     print(errs());
288*9880d681SAndroid Build Coastguard Worker     errs() << "\nActual:\n";
289*9880d681SAndroid Build Coastguard Worker     OtherDT.print(errs());
290*9880d681SAndroid Build Coastguard Worker     abort();
291*9880d681SAndroid Build Coastguard Worker   }
292*9880d681SAndroid Build Coastguard Worker }
293*9880d681SAndroid Build Coastguard Worker 
294*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
295*9880d681SAndroid Build Coastguard Worker //  DominatorTreeAnalysis and related pass implementations
296*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
297*9880d681SAndroid Build Coastguard Worker //
298*9880d681SAndroid Build Coastguard Worker // This implements the DominatorTreeAnalysis which is used with the new pass
299*9880d681SAndroid Build Coastguard Worker // manager. It also implements some methods from utility passes.
300*9880d681SAndroid Build Coastguard Worker //
301*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
302*9880d681SAndroid Build Coastguard Worker 
run(Function & F,AnalysisManager<Function> &)303*9880d681SAndroid Build Coastguard Worker DominatorTree DominatorTreeAnalysis::run(Function &F,
304*9880d681SAndroid Build Coastguard Worker                                          AnalysisManager<Function> &) {
305*9880d681SAndroid Build Coastguard Worker   DominatorTree DT;
306*9880d681SAndroid Build Coastguard Worker   DT.recalculate(F);
307*9880d681SAndroid Build Coastguard Worker   return DT;
308*9880d681SAndroid Build Coastguard Worker }
309*9880d681SAndroid Build Coastguard Worker 
310*9880d681SAndroid Build Coastguard Worker char DominatorTreeAnalysis::PassID;
311*9880d681SAndroid Build Coastguard Worker 
DominatorTreePrinterPass(raw_ostream & OS)312*9880d681SAndroid Build Coastguard Worker DominatorTreePrinterPass::DominatorTreePrinterPass(raw_ostream &OS) : OS(OS) {}
313*9880d681SAndroid Build Coastguard Worker 
run(Function & F,FunctionAnalysisManager & AM)314*9880d681SAndroid Build Coastguard Worker PreservedAnalyses DominatorTreePrinterPass::run(Function &F,
315*9880d681SAndroid Build Coastguard Worker                                                 FunctionAnalysisManager &AM) {
316*9880d681SAndroid Build Coastguard Worker   OS << "DominatorTree for function: " << F.getName() << "\n";
317*9880d681SAndroid Build Coastguard Worker   AM.getResult<DominatorTreeAnalysis>(F).print(OS);
318*9880d681SAndroid Build Coastguard Worker 
319*9880d681SAndroid Build Coastguard Worker   return PreservedAnalyses::all();
320*9880d681SAndroid Build Coastguard Worker }
321*9880d681SAndroid Build Coastguard Worker 
run(Function & F,FunctionAnalysisManager & AM)322*9880d681SAndroid Build Coastguard Worker PreservedAnalyses DominatorTreeVerifierPass::run(Function &F,
323*9880d681SAndroid Build Coastguard Worker                                                  FunctionAnalysisManager &AM) {
324*9880d681SAndroid Build Coastguard Worker   AM.getResult<DominatorTreeAnalysis>(F).verifyDomTree();
325*9880d681SAndroid Build Coastguard Worker 
326*9880d681SAndroid Build Coastguard Worker   return PreservedAnalyses::all();
327*9880d681SAndroid Build Coastguard Worker }
328*9880d681SAndroid Build Coastguard Worker 
329*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
330*9880d681SAndroid Build Coastguard Worker //  DominatorTreeWrapperPass Implementation
331*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
332*9880d681SAndroid Build Coastguard Worker //
333*9880d681SAndroid Build Coastguard Worker // The implementation details of the wrapper pass that holds a DominatorTree
334*9880d681SAndroid Build Coastguard Worker // suitable for use with the legacy pass manager.
335*9880d681SAndroid Build Coastguard Worker //
336*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
337*9880d681SAndroid Build Coastguard Worker 
338*9880d681SAndroid Build Coastguard Worker char DominatorTreeWrapperPass::ID = 0;
339*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS(DominatorTreeWrapperPass, "domtree",
340*9880d681SAndroid Build Coastguard Worker                 "Dominator Tree Construction", true, true)
341*9880d681SAndroid Build Coastguard Worker 
runOnFunction(Function & F)342*9880d681SAndroid Build Coastguard Worker bool DominatorTreeWrapperPass::runOnFunction(Function &F) {
343*9880d681SAndroid Build Coastguard Worker   DT.recalculate(F);
344*9880d681SAndroid Build Coastguard Worker   return false;
345*9880d681SAndroid Build Coastguard Worker }
346*9880d681SAndroid Build Coastguard Worker 
verifyAnalysis() const347*9880d681SAndroid Build Coastguard Worker void DominatorTreeWrapperPass::verifyAnalysis() const {
348*9880d681SAndroid Build Coastguard Worker     if (VerifyDomInfo)
349*9880d681SAndroid Build Coastguard Worker       DT.verifyDomTree();
350*9880d681SAndroid Build Coastguard Worker }
351*9880d681SAndroid Build Coastguard Worker 
print(raw_ostream & OS,const Module *) const352*9880d681SAndroid Build Coastguard Worker void DominatorTreeWrapperPass::print(raw_ostream &OS, const Module *) const {
353*9880d681SAndroid Build Coastguard Worker   DT.print(OS);
354*9880d681SAndroid Build Coastguard Worker }
355*9880d681SAndroid Build Coastguard Worker 
356