Put all LLVM code into the llvm namespace, as per bug 109.
[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/iTerminators.h"
16 #include "llvm/Support/CFG.h"
17 #include "Support/DepthFirstIterator.h"
18 #include "Support/SetOperations.h"
19
20 namespace llvm {
21
22 //===----------------------------------------------------------------------===//
23 //  PostDominatorSet Implementation
24 //===----------------------------------------------------------------------===//
25
26 static RegisterAnalysis<PostDominatorSet>
27 B("postdomset", "Post-Dominator Set Construction", true);
28
29 // Postdominator set construction.  This converts the specified function to only
30 // have a single exit node (return stmt), then calculates the post dominance
31 // sets for the function.
32 //
33 bool PostDominatorSet::runOnFunction(Function &F) {
34   Doms.clear();   // Reset from the last time we were run...
35
36   // Scan the function looking for the root nodes of the post-dominance
37   // relationships.  These blocks end with return and unwind instructions.
38   // While we are iterating over the function, we also initialize all of the
39   // domsets to empty.
40   Roots.clear();
41   for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
42     Doms[I];  // Initialize to empty
43
44     if (isa<ReturnInst>(I->getTerminator()) ||
45         isa<UnwindInst>(I->getTerminator()))
46       Roots.push_back(I);
47   }
48
49   // If there are no exit nodes for the function, postdomsets are all empty.
50   // This can happen if the function just contains an infinite loop, for
51   // example.
52   if (Roots.empty()) return false;
53
54   // If we have more than one root, we insert an artificial "null" exit, which
55   // has "virtual edges" to each of the real exit nodes.
56   if (Roots.size() > 1)
57     Doms[0].insert(0);
58
59   bool Changed;
60   do {
61     Changed = false;
62
63     std::set<BasicBlock*> Visited;
64     DomSetType WorkingSet;
65
66     for (unsigned i = 0, e = Roots.size(); i != e; ++i)
67       for (idf_ext_iterator<BasicBlock*> It = idf_ext_begin(Roots[i], Visited),
68              E = idf_ext_end(Roots[i], Visited); It != E; ++It) {
69         BasicBlock *BB = *It;
70         succ_iterator SI = succ_begin(BB), SE = succ_end(BB);
71         if (SI != SE) {                // Is there SOME successor?
72           // Loop until we get to a successor that has had it's dom set filled
73           // in at least once.  We are guaranteed to have this because we are
74           // traversing the graph in DFO and have handled start nodes specially.
75           //
76           while (Doms[*SI].size() == 0) ++SI;
77           WorkingSet = Doms[*SI];
78           
79           for (++SI; SI != SE; ++SI) { // Intersect all of the successor sets
80             DomSetType &SuccSet = Doms[*SI];
81             if (SuccSet.size())
82               set_intersect(WorkingSet, SuccSet);
83           }
84         } else {
85           // If this node has no successors, it must be one of the root nodes.
86           // We will already take care of the notion that the node
87           // post-dominates itself.  The only thing we have to add is that if
88           // there are multiple root nodes, we want to insert a special "null"
89           // exit node which dominates the roots as well.
90           if (Roots.size() > 1)
91             WorkingSet.insert(0);
92         }
93         
94         WorkingSet.insert(BB);           // A block always dominates itself
95         DomSetType &BBSet = Doms[BB];
96         if (BBSet != WorkingSet) {
97           BBSet.swap(WorkingSet);        // Constant time operation!
98           Changed = true;                // The sets changed.
99         }
100         WorkingSet.clear();              // Clear out the set for next iteration
101       }
102   } while (Changed);
103   return false;
104 }
105
106 //===----------------------------------------------------------------------===//
107 //  ImmediatePostDominators Implementation
108 //===----------------------------------------------------------------------===//
109
110 static RegisterAnalysis<ImmediatePostDominators>
111 D("postidom", "Immediate Post-Dominators Construction", true);
112
113 //===----------------------------------------------------------------------===//
114 //  PostDominatorTree Implementation
115 //===----------------------------------------------------------------------===//
116
117 static RegisterAnalysis<PostDominatorTree>
118 F("postdomtree", "Post-Dominator Tree Construction", true);
119
120 void PostDominatorTree::calculate(const PostDominatorSet &DS) {
121   if (Roots.empty()) return;
122   BasicBlock *Root = Roots.size() == 1 ? Roots[0] : 0;
123
124   Nodes[Root] = RootNode = new Node(Root, 0);   // Add a node for the root...
125
126   // Iterate over all nodes in depth first order...
127   for (unsigned i = 0, e = Roots.size(); i != e; ++i)
128     for (idf_iterator<BasicBlock*> I = idf_begin(Roots[i]),
129            E = idf_end(Roots[i]); I != E; ++I) {
130       BasicBlock *BB = *I;
131       const DominatorSet::DomSetType &Dominators = DS.getDominators(BB);
132       unsigned DomSetSize = Dominators.size();
133       if (DomSetSize == 1) continue;  // Root node... IDom = null
134
135       // If we have already computed the immediate dominator for this node,
136       // don't revisit.  This can happen due to nodes reachable from multiple
137       // roots, but which the idf_iterator doesn't know about.
138       if (Nodes.find(BB) != Nodes.end()) continue;
139
140       // Loop over all dominators of this node.  This corresponds to looping
141       // over nodes in the dominator chain, looking for a node whose dominator
142       // set is equal to the current nodes, except that the current node does
143       // not exist in it.  This means that it is one level higher in the dom
144       // chain than the current node, and it is our idom!  We know that we have
145       // already added a DominatorTree node for our idom, because the idom must
146       // be a predecessor in the depth first order that we are iterating through
147       // the function.
148       //
149       DominatorSet::DomSetType::const_iterator I = Dominators.begin();
150       DominatorSet::DomSetType::const_iterator End = Dominators.end();
151       for (; I != End; ++I) {   // Iterate over dominators...
152         // All of our dominators should form a chain, where the number
153         // of elements in the dominator set indicates what level the
154         // node is at in the chain.  We want the node immediately
155         // above us, so it will have an identical dominator set,
156         // except that BB will not dominate it... therefore it's
157         // dominator set size will be one less than BB's...
158         //
159         if (DS.getDominators(*I).size() == DomSetSize - 1) {
160           // We know that the immediate dominator should already have a node, 
161           // because we are traversing the CFG in depth first order!
162           //
163           Node *IDomNode = Nodes[*I];
164           assert(IDomNode && "No node for IDOM?");
165           
166           // Add a new tree node for this BasicBlock, and link it as a child of
167           // IDomNode
168           Nodes[BB] = IDomNode->addChild(new Node(BB, IDomNode));
169           break;
170         }
171       }
172     }
173 }
174
175 //===----------------------------------------------------------------------===//
176 //  PostDominanceFrontier Implementation
177 //===----------------------------------------------------------------------===//
178
179 static RegisterAnalysis<PostDominanceFrontier>
180 H("postdomfrontier", "Post-Dominance Frontier Construction", true);
181
182 const DominanceFrontier::DomSetType &
183 PostDominanceFrontier::calculate(const PostDominatorTree &DT, 
184                                  const DominatorTree::Node *Node) {
185   // Loop over CFG successors to calculate DFlocal[Node]
186   BasicBlock *BB = Node->getBlock();
187   DomSetType &S = Frontiers[BB];       // The new set to fill in...
188   if (getRoots().empty()) return S;
189
190   if (BB)
191     for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB);
192          SI != SE; ++SI)
193       // Does Node immediately dominate this predecessor?
194       if (DT[*SI]->getIDom() != Node)
195         S.insert(*SI);
196
197   // At this point, S is DFlocal.  Now we union in DFup's of our children...
198   // Loop through and visit the nodes that Node immediately dominates (Node's
199   // children in the IDomTree)
200   //
201   for (PostDominatorTree::Node::const_iterator
202          NI = Node->begin(), NE = Node->end(); NI != NE; ++NI) {
203     DominatorTree::Node *IDominee = *NI;
204     const DomSetType &ChildDF = calculate(DT, IDominee);
205
206     DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end();
207     for (; CDFI != CDFE; ++CDFI) {
208       if (!Node->dominates(DT[*CDFI]))
209         S.insert(*CDFI);
210     }
211   }
212
213   return S;
214 }
215
216 // stub - a dummy function to make linking work ok.
217 void PostDominanceFrontier::stub() {
218 }
219
220 } // End llvm namespace