Unreachable block is not a root node in post dominator tree.
[oota-llvm.git] / lib / Analysis / PostDominators.cpp
1 //===- PostDominators.cpp - Post-Dominator Calculation --------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the post-dominator construction algorithms.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Analysis/PostDominators.h"
15 #include "llvm/Instructions.h"
16 #include "llvm/Support/CFG.h"
17 #include "llvm/ADT/DepthFirstIterator.h"
18 #include "llvm/ADT/SetOperations.h"
19 using namespace llvm;
20
21 //===----------------------------------------------------------------------===//
22 //  PostDominatorTree Implementation
23 //===----------------------------------------------------------------------===//
24
25 char PostDominatorTree::ID = 0;
26 char PostDominanceFrontier::ID = 0;
27 static RegisterPass<PostDominatorTree>
28 F("postdomtree", "Post-Dominator Tree Construction", true);
29
30 unsigned PostDominatorTree::DFSPass(BasicBlock *V, InfoRec &VInfo,
31                                           unsigned N) {
32   std::vector<std::pair<BasicBlock *, InfoRec *> > workStack;
33   std::set<BasicBlock *> visited;
34   workStack.push_back(std::make_pair(V, &VInfo));
35
36   do {
37     BasicBlock *currentBB = workStack.back().first; 
38     InfoRec *currentVInfo = workStack.back().second;
39
40     // Visit each block only once.
41     if (visited.count(currentBB) == 0) {
42
43       visited.insert(currentBB);
44       currentVInfo->Semi = ++N;
45       currentVInfo->Label = currentBB;
46       
47       Vertex.push_back(currentBB);  // Vertex[n] = current;
48       // Info[currentBB].Ancestor = 0;     
49       // Ancestor[n] = 0
50       // Child[currentBB] = 0;
51       currentVInfo->Size = 1;       // Size[currentBB] = 1
52     }
53
54     // Visit children
55     bool visitChild = false;
56     for (pred_iterator PI = pred_begin(currentBB), PE = pred_end(currentBB); 
57          PI != PE && !visitChild; ++PI) {
58       InfoRec &SuccVInfo = Info[*PI];
59       if (SuccVInfo.Semi == 0) {
60         SuccVInfo.Parent = currentBB;
61         if (visited.count (*PI) == 0) {
62           workStack.push_back(std::make_pair(*PI, &SuccVInfo));   
63           visitChild = true;
64         }
65       }
66     }
67
68     // If all children are visited or if this block has no child then pop this
69     // block out of workStack.
70     if (!visitChild)
71       workStack.pop_back();
72
73   } while (!workStack.empty());
74
75   return N;
76 }
77
78 void PostDominatorTree::Compress(BasicBlock *V, InfoRec &VInfo) {
79   BasicBlock *VAncestor = VInfo.Ancestor;
80   InfoRec &VAInfo = Info[VAncestor];
81   if (VAInfo.Ancestor == 0)
82     return;
83   
84   Compress(VAncestor, VAInfo);
85   
86   BasicBlock *VAncestorLabel = VAInfo.Label;
87   BasicBlock *VLabel = VInfo.Label;
88   if (Info[VAncestorLabel].Semi < Info[VLabel].Semi)
89     VInfo.Label = VAncestorLabel;
90   
91   VInfo.Ancestor = VAInfo.Ancestor;
92 }
93
94 BasicBlock *PostDominatorTree::Eval(BasicBlock *V) {
95   InfoRec &VInfo = Info[V];
96
97   // Higher-complexity but faster implementation
98   if (VInfo.Ancestor == 0)
99     return V;
100   Compress(V, VInfo);
101   return VInfo.Label;
102 }
103
104 void PostDominatorTree::Link(BasicBlock *V, BasicBlock *W, 
105                                    InfoRec &WInfo) {
106   // Higher-complexity but faster implementation
107   WInfo.Ancestor = V;
108 }
109
110 void PostDominatorTree::calculate(Function &F) {
111   // Step #0: Scan the function looking for the root nodes of the post-dominance
112   // relationships.  These blocks, which have no successors, end with return and
113   // unwind instructions.
114   for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
115     if (succ_begin(I) == succ_end(I)) {
116       Instruction *Insn = I->getTerminator();
117       // Unreachable block is not a root node.
118       if (!isa<UnreachableInst>(Insn))
119         Roots.push_back(I);
120     }
121   
122   Vertex.push_back(0);
123   
124   // Step #1: Number blocks in depth-first order and initialize variables used
125   // in later stages of the algorithm.
126   unsigned N = 0;
127   for (unsigned i = 0, e = Roots.size(); i != e; ++i)
128     N = DFSPass(Roots[i], Info[Roots[i]], N);
129   
130   for (unsigned i = N; i >= 2; --i) {
131     BasicBlock *W = Vertex[i];
132     InfoRec &WInfo = Info[W];
133     
134     // Step #2: Calculate the semidominators of all vertices
135     for (succ_iterator SI = succ_begin(W), SE = succ_end(W); SI != SE; ++SI)
136       if (Info.count(*SI)) {  // Only if this predecessor is reachable!
137         unsigned SemiU = Info[Eval(*SI)].Semi;
138         if (SemiU < WInfo.Semi)
139           WInfo.Semi = SemiU;
140       }
141         
142     Info[Vertex[WInfo.Semi]].Bucket.push_back(W);
143     
144     BasicBlock *WParent = WInfo.Parent;
145     Link(WParent, W, WInfo);
146     
147     // Step #3: Implicitly define the immediate dominator of vertices
148     std::vector<BasicBlock*> &WParentBucket = Info[WParent].Bucket;
149     while (!WParentBucket.empty()) {
150       BasicBlock *V = WParentBucket.back();
151       WParentBucket.pop_back();
152       BasicBlock *U = Eval(V);
153       IDoms[V] = Info[U].Semi < Info[V].Semi ? U : WParent;
154     }
155   }
156   
157   // Step #4: Explicitly define the immediate dominator of each vertex
158   for (unsigned i = 2; i <= N; ++i) {
159     BasicBlock *W = Vertex[i];
160     BasicBlock *&WIDom = IDoms[W];
161     if (WIDom != Vertex[Info[W].Semi])
162       WIDom = IDoms[WIDom];
163   }
164   
165   if (Roots.empty()) return;
166
167   // Add a node for the root.  This node might be the actual root, if there is
168   // one exit block, or it may be the virtual exit (denoted by (BasicBlock *)0)
169   // which postdominates all real exits if there are multiple exit blocks.
170   BasicBlock *Root = Roots.size() == 1 ? Roots[0] : 0;
171   DomTreeNodes[Root] = RootNode = new DomTreeNode(Root, 0);
172   
173   // Loop over all of the reachable blocks in the function...
174   for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
175     if (BasicBlock *ImmPostDom = getIDom(I)) {  // Reachable block.
176       DomTreeNode *&BBNode = DomTreeNodes[I];
177       if (!BBNode) {  // Haven't calculated this node yet?
178                       // Get or calculate the node for the immediate dominator
179         DomTreeNode *IPDomNode = getNodeForBlock(ImmPostDom);
180         
181         // Add a new tree node for this BasicBlock, and link it as a child of
182         // IDomNode
183         DomTreeNode *C = new DomTreeNode(I, IPDomNode);
184         DomTreeNodes[I] = C;
185         BBNode = IPDomNode->addChild(C);
186       }
187     }
188
189   // Free temporary memory used to construct idom's
190   IDoms.clear();
191   Info.clear();
192   std::vector<BasicBlock*>().swap(Vertex);
193
194   int dfsnum = 0;
195   // Iterate over all nodes in depth first order...
196   for (unsigned i = 0, e = Roots.size(); i != e; ++i)
197     for (idf_iterator<BasicBlock*> I = idf_begin(Roots[i]),
198            E = idf_end(Roots[i]); I != E; ++I) {
199       if (!getNodeForBlock(*I)->getIDom())
200         getNodeForBlock(*I)->assignDFSNumber(dfsnum);
201     }
202   DFSInfoValid = true;
203 }
204
205
206 DomTreeNode *PostDominatorTree::getNodeForBlock(BasicBlock *BB) {
207   DomTreeNode *&BBNode = DomTreeNodes[BB];
208   if (BBNode) return BBNode;
209   
210   // Haven't calculated this node yet?  Get or calculate the node for the
211   // immediate postdominator.
212   BasicBlock *IPDom = getIDom(BB);
213   DomTreeNode *IPDomNode = getNodeForBlock(IPDom);
214   
215   // Add a new tree node for this BasicBlock, and link it as a child of
216   // IDomNode
217   DomTreeNode *C = new DomTreeNode(BB, IPDomNode);
218   DomTreeNodes[BB] = C;
219   return BBNode = IPDomNode->addChild(C);
220 }
221
222 //===----------------------------------------------------------------------===//
223 //  PostDominanceFrontier Implementation
224 //===----------------------------------------------------------------------===//
225
226 static RegisterPass<PostDominanceFrontier>
227 H("postdomfrontier", "Post-Dominance Frontier Construction", true);
228
229 const DominanceFrontier::DomSetType &
230 PostDominanceFrontier::calculate(const PostDominatorTree &DT,
231                                  const DomTreeNode *Node) {
232   // Loop over CFG successors to calculate DFlocal[Node]
233   BasicBlock *BB = Node->getBlock();
234   DomSetType &S = Frontiers[BB];       // The new set to fill in...
235   if (getRoots().empty()) return S;
236
237   if (BB)
238     for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB);
239          SI != SE; ++SI) {
240       // Does Node immediately dominate this predecessor?
241       DomTreeNode *SINode = DT[*SI];
242       if (SINode && SINode->getIDom() != Node)
243         S.insert(*SI);
244     }
245
246   // At this point, S is DFlocal.  Now we union in DFup's of our children...
247   // Loop through and visit the nodes that Node immediately dominates (Node's
248   // children in the IDomTree)
249   //
250   for (DomTreeNode::const_iterator
251          NI = Node->begin(), NE = Node->end(); NI != NE; ++NI) {
252     DomTreeNode *IDominee = *NI;
253     const DomSetType &ChildDF = calculate(DT, IDominee);
254
255     DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end();
256     for (; CDFI != CDFE; ++CDFI) {
257       if (!DT.properlyDominates(Node, DT[*CDFI]))
258         S.insert(*CDFI);
259     }
260   }
261
262   return S;
263 }
264
265 // Ensure that this .cpp file gets linked when PostDominators.h is used.
266 DEFINING_FILE_FOR(PostDominanceFrontier)