6e58985ba123129c79d31562d6b13fbf48d48d4a
[oota-llvm.git] / lib / VMCore / Dominators.cpp
1 //===- Dominators.cpp - Dominator Calculation -----------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #include "llvm/Analysis/DominanceFrontier.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/SetOperations.h"
23 #include "llvm/ADT/SmallPtrSet.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/Analysis/DominatorInternals.h"
26 #include "llvm/Assembly/Writer.h"
27 #include "llvm/Instructions.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/Support/CommandLine.h"
30 #include <algorithm>
31 using namespace llvm;
32
33 // Always verify dominfo if expensive checking is enabled.
34 #ifdef XDEBUG
35 static bool VerifyDomInfo = true;
36 #else
37 static bool VerifyDomInfo = false;
38 #endif
39 static cl::opt<bool,true>
40 VerifyDomInfoX("verify-dom-info", cl::location(VerifyDomInfo),
41                cl::desc("Verify dominator info (time consuming)"));
42
43 //===----------------------------------------------------------------------===//
44 //  DominatorTree Implementation
45 //===----------------------------------------------------------------------===//
46 //
47 // Provide public access to DominatorTree information.  Implementation details
48 // can be found in DominatorCalculation.h.
49 //
50 //===----------------------------------------------------------------------===//
51
52 TEMPLATE_INSTANTIATION(class llvm::DomTreeNodeBase<BasicBlock>);
53 TEMPLATE_INSTANTIATION(class llvm::DominatorTreeBase<BasicBlock>);
54
55 char DominatorTree::ID = 0;
56 INITIALIZE_PASS(DominatorTree, "domtree",
57                 "Dominator Tree Construction", true, true)
58
59 bool DominatorTree::runOnFunction(Function &F) {
60   DT->recalculate(F);
61   return false;
62 }
63
64 void DominatorTree::verifyAnalysis() const {
65   if (!VerifyDomInfo) return;
66
67   Function &F = *getRoot()->getParent();
68
69   DominatorTree OtherDT;
70   OtherDT.getBase().recalculate(F);
71   if (compare(OtherDT)) {
72     errs() << "DominatorTree is not up to date!  Computed:\n";
73     print(errs());
74     
75     errs() << "\nActual:\n";
76     OtherDT.print(errs());
77     abort();
78   }
79 }
80
81 void DominatorTree::print(raw_ostream &OS, const Module *) const {
82   DT->print(OS);
83 }
84
85 // dominates - Return true if A dominates a use in B. This performs the
86 // special checks necessary if A and B are in the same basic block.
87 bool DominatorTree::dominates(const Instruction *A, const Instruction *B) const{
88   const BasicBlock *BBA = A->getParent(), *BBB = B->getParent();
89   
90   // If A is an invoke instruction, its value is only available in this normal
91   // successor block.
92   if (const InvokeInst *II = dyn_cast<InvokeInst>(A))
93     BBA = II->getNormalDest();
94   
95   if (BBA != BBB) return dominates(BBA, BBB);
96   
97   // It is not possible to determine dominance between two PHI nodes 
98   // based on their ordering.
99   if (isa<PHINode>(A) && isa<PHINode>(B)) 
100     return false;
101   
102   // Loop through the basic block until we find A or B.
103   BasicBlock::const_iterator I = BBA->begin();
104   for (; &*I != A && &*I != B; ++I)
105     /*empty*/;
106   
107   return &*I == A;
108 }
109
110
111
112 //===----------------------------------------------------------------------===//
113 //  DominanceFrontier Implementation
114 //===----------------------------------------------------------------------===//
115
116 char DominanceFrontier::ID = 0;
117 INITIALIZE_PASS_BEGIN(DominanceFrontier, "domfrontier",
118                 "Dominance Frontier Construction", true, true)
119 INITIALIZE_PASS_DEPENDENCY(DominatorTree)
120 INITIALIZE_PASS_END(DominanceFrontier, "domfrontier",
121                 "Dominance Frontier Construction", true, true)
122
123 void DominanceFrontier::verifyAnalysis() const {
124   if (!VerifyDomInfo) return;
125
126   DominatorTree &DT = getAnalysis<DominatorTree>();
127
128   DominanceFrontier OtherDF;
129   const std::vector<BasicBlock*> &DTRoots = DT.getRoots();
130   OtherDF.calculate(DT, DT.getNode(DTRoots[0]));
131   assert(!compare(OtherDF) && "Invalid DominanceFrontier info!");
132 }
133
134 namespace {
135   class DFCalculateWorkObject {
136   public:
137     DFCalculateWorkObject(BasicBlock *B, BasicBlock *P, 
138                           const DomTreeNode *N,
139                           const DomTreeNode *PN)
140     : currentBB(B), parentBB(P), Node(N), parentNode(PN) {}
141     BasicBlock *currentBB;
142     BasicBlock *parentBB;
143     const DomTreeNode *Node;
144     const DomTreeNode *parentNode;
145   };
146 }
147
148 const DominanceFrontier::DomSetType &
149 DominanceFrontier::calculate(const DominatorTree &DT,
150                              const DomTreeNode *Node) {
151   BasicBlock *BB = Node->getBlock();
152   DomSetType *Result = NULL;
153
154   std::vector<DFCalculateWorkObject> workList;
155   SmallPtrSet<BasicBlock *, 32> visited;
156
157   workList.push_back(DFCalculateWorkObject(BB, NULL, Node, NULL));
158   do {
159     DFCalculateWorkObject *currentW = &workList.back();
160     assert (currentW && "Missing work object.");
161
162     BasicBlock *currentBB = currentW->currentBB;
163     BasicBlock *parentBB = currentW->parentBB;
164     const DomTreeNode *currentNode = currentW->Node;
165     const DomTreeNode *parentNode = currentW->parentNode;
166     assert (currentBB && "Invalid work object. Missing current Basic Block");
167     assert (currentNode && "Invalid work object. Missing current Node");
168     DomSetType &S = Frontiers[currentBB];
169
170     // Visit each block only once.
171     if (visited.count(currentBB) == 0) {
172       visited.insert(currentBB);
173
174       // Loop over CFG successors to calculate DFlocal[currentNode]
175       for (succ_iterator SI = succ_begin(currentBB), SE = succ_end(currentBB);
176            SI != SE; ++SI) {
177         // Does Node immediately dominate this successor?
178         if (DT[*SI]->getIDom() != currentNode)
179           S.insert(*SI);
180       }
181     }
182
183     // At this point, S is DFlocal.  Now we union in DFup's of our children...
184     // Loop through and visit the nodes that Node immediately dominates (Node's
185     // children in the IDomTree)
186     bool visitChild = false;
187     for (DomTreeNode::const_iterator NI = currentNode->begin(), 
188            NE = currentNode->end(); NI != NE; ++NI) {
189       DomTreeNode *IDominee = *NI;
190       BasicBlock *childBB = IDominee->getBlock();
191       if (visited.count(childBB) == 0) {
192         workList.push_back(DFCalculateWorkObject(childBB, currentBB,
193                                                  IDominee, currentNode));
194         visitChild = true;
195       }
196     }
197
198     // If all children are visited or there is any child then pop this block
199     // from the workList.
200     if (!visitChild) {
201
202       if (!parentBB) {
203         Result = &S;
204         break;
205       }
206
207       DomSetType::const_iterator CDFI = S.begin(), CDFE = S.end();
208       DomSetType &parentSet = Frontiers[parentBB];
209       for (; CDFI != CDFE; ++CDFI) {
210         if (!DT.properlyDominates(parentNode, DT[*CDFI]))
211           parentSet.insert(*CDFI);
212       }
213       workList.pop_back();
214     }
215
216   } while (!workList.empty());
217
218   return *Result;
219 }
220
221 void DominanceFrontierBase::print(raw_ostream &OS, const Module* ) const {
222   for (const_iterator I = begin(), E = end(); I != E; ++I) {
223     OS << "  DomFrontier for BB ";
224     if (I->first)
225       WriteAsOperand(OS, I->first, false);
226     else
227       OS << " <<exit node>>";
228     OS << " is:\t";
229     
230     const std::set<BasicBlock*> &BBs = I->second;
231     
232     for (std::set<BasicBlock*>::const_iterator I = BBs.begin(), E = BBs.end();
233          I != E; ++I) {
234       OS << ' ';
235       if (*I)
236         WriteAsOperand(OS, *I, false);
237       else
238         OS << "<<exit node>>";
239     }
240     OS << "\n";
241   }
242 }
243
244 void DominanceFrontierBase::dump() const {
245   print(dbgs());
246 }
247