1 //===- PostDominators.cpp - Post-Dominator Calculation --------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the post-dominator construction algorithms.
12 //===----------------------------------------------------------------------===//
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"
22 //===----------------------------------------------------------------------===//
23 // ImmediatePostDominators Implementation
24 //===----------------------------------------------------------------------===//
26 static RegisterPass<ImmediatePostDominators>
27 D("postidom", "Immediate Post-Dominators Construction", true);
29 unsigned ImmediatePostDominators::DFSPass(BasicBlock *V, InfoRec &VInfo,
32 std::vector<std::pair<BasicBlock *, InfoRec *> > workStack;
33 workStack.push_back(std::make_pair(V, &VInfo));
36 BasicBlock *currentBB = workStack.back().first;
37 InfoRec *currentVInfo = workStack.back().second;
40 currentVInfo->Semi = ++N;
41 currentVInfo->Label = currentBB;
43 Vertex.push_back(currentBB); // Vertex[n] = current;
44 // Info[currentBB].Ancestor = 0;
46 // Child[currentBB] = 0;
47 currentVInfo->Size = 1; // Size[currentBB] = 1
49 // For PostDominators, we want to walk predecessors rather than successors
50 // as we do in forward Dominators.
51 for (pred_iterator PI = pred_begin(currentBB), PE = pred_end(currentBB);
53 InfoRec &SuccVInfo = Info[*PI];
54 if (SuccVInfo.Semi == 0) {
55 SuccVInfo.Parent = currentBB;
57 workStack.push_back(std::make_pair(*PI, &SuccVInfo));
60 } while (!workStack.empty());
64 void ImmediatePostDominators::Compress(BasicBlock *V, InfoRec &VInfo) {
65 BasicBlock *VAncestor = VInfo.Ancestor;
66 InfoRec &VAInfo = Info[VAncestor];
67 if (VAInfo.Ancestor == 0)
70 Compress(VAncestor, VAInfo);
72 BasicBlock *VAncestorLabel = VAInfo.Label;
73 BasicBlock *VLabel = VInfo.Label;
74 if (Info[VAncestorLabel].Semi < Info[VLabel].Semi)
75 VInfo.Label = VAncestorLabel;
77 VInfo.Ancestor = VAInfo.Ancestor;
80 BasicBlock *ImmediatePostDominators::Eval(BasicBlock *V) {
81 InfoRec &VInfo = Info[V];
83 // Higher-complexity but faster implementation
84 if (VInfo.Ancestor == 0)
90 void ImmediatePostDominators::Link(BasicBlock *V, BasicBlock *W,
92 // Higher-complexity but faster implementation
96 bool ImmediatePostDominators::runOnFunction(Function &F) {
97 IDoms.clear(); // Reset from the last time we were run...
100 // Step #0: Scan the function looking for the root nodes of the post-dominance
101 // relationships. These blocks, which have no successors, end with return and
102 // unwind instructions.
103 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
104 if (succ_begin(I) == succ_end(I))
109 // Step #1: Number blocks in depth-first order and initialize variables used
110 // in later stages of the algorithm.
112 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
113 N = DFSPass(Roots[i], Info[Roots[i]], N);
115 for (unsigned i = N; i >= 2; --i) {
116 BasicBlock *W = Vertex[i];
117 InfoRec &WInfo = Info[W];
119 // Step #2: Calculate the semidominators of all vertices
120 for (succ_iterator SI = succ_begin(W), SE = succ_end(W); SI != SE; ++SI)
121 if (Info.count(*SI)) { // Only if this predecessor is reachable!
122 unsigned SemiU = Info[Eval(*SI)].Semi;
123 if (SemiU < WInfo.Semi)
127 Info[Vertex[WInfo.Semi]].Bucket.push_back(W);
129 BasicBlock *WParent = WInfo.Parent;
130 Link(WParent, W, WInfo);
132 // Step #3: Implicitly define the immediate dominator of vertices
133 std::vector<BasicBlock*> &WParentBucket = Info[WParent].Bucket;
134 while (!WParentBucket.empty()) {
135 BasicBlock *V = WParentBucket.back();
136 WParentBucket.pop_back();
137 BasicBlock *U = Eval(V);
138 IDoms[V] = Info[U].Semi < Info[V].Semi ? U : WParent;
142 // Step #4: Explicitly define the immediate dominator of each vertex
143 for (unsigned i = 2; i <= N; ++i) {
144 BasicBlock *W = Vertex[i];
145 BasicBlock *&WIDom = IDoms[W];
146 if (WIDom != Vertex[Info[W].Semi])
147 WIDom = IDoms[WIDom];
150 // Free temporary memory used to construct idom's
152 std::vector<BasicBlock*>().swap(Vertex);
157 //===----------------------------------------------------------------------===//
158 // PostDominatorSet Implementation
159 //===----------------------------------------------------------------------===//
161 static RegisterPass<PostDominatorSet>
162 B("postdomset", "Post-Dominator Set Construction", true);
164 // Postdominator set construction. This converts the specified function to only
165 // have a single exit node (return stmt), then calculates the post dominance
166 // sets for the function.
168 bool PostDominatorSet::runOnFunction(Function &F) {
169 // Scan the function looking for the root nodes of the post-dominance
170 // relationships. These blocks end with return and unwind instructions.
171 // While we are iterating over the function, we also initialize all of the
174 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
175 if (succ_begin(I) == succ_end(I))
178 // If there are no exit nodes for the function, postdomsets are all empty.
179 // This can happen if the function just contains an infinite loop, for
181 ImmediatePostDominators &IPD = getAnalysis<ImmediatePostDominators>();
182 Doms.clear(); // Reset from the last time we were run...
183 if (Roots.empty()) return false;
185 // If we have more than one root, we insert an artificial "null" exit, which
186 // has "virtual edges" to each of the real exit nodes.
187 //if (Roots.size() > 1)
188 // Doms[0].insert(0);
190 // Root nodes only dominate themselves.
191 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
192 Doms[Roots[i]].insert(Roots[i]);
194 // Loop over all of the blocks in the function, calculating dominator sets for
196 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
197 if (BasicBlock *IPDom = IPD[I]) { // Get idom if block is reachable
198 DomSetType &DS = Doms[I];
199 assert(DS.empty() && "PostDomset already filled in for this block?");
200 DS.insert(I); // Blocks always dominate themselves
202 // Insert all dominators into the set...
204 // If we have already computed the dominator sets for our immediate post
205 // dominator, just use it instead of walking all the way up to the root.
206 DomSetType &IPDS = Doms[IPDom];
208 DS.insert(IPDS.begin(), IPDS.end());
216 // Ensure that every basic block has at least an empty set of nodes. This
217 // is important for the case when there is unreachable blocks.
224 //===----------------------------------------------------------------------===//
225 // PostDominatorTree Implementation
226 //===----------------------------------------------------------------------===//
228 static RegisterPass<PostDominatorTree>
229 F("postdomtree", "Post-Dominator Tree Construction", true);
231 DominatorTreeBase::Node *PostDominatorTree::getNodeForBlock(BasicBlock *BB) {
232 Node *&BBNode = Nodes[BB];
233 if (BBNode) return BBNode;
235 // Haven't calculated this node yet? Get or calculate the node for the
236 // immediate postdominator.
237 BasicBlock *IPDom = getAnalysis<ImmediatePostDominators>()[BB];
238 Node *IPDomNode = getNodeForBlock(IPDom);
240 // Add a new tree node for this BasicBlock, and link it as a child of
242 return BBNode = IPDomNode->addChild(new Node(BB, IPDomNode));
245 void PostDominatorTree::calculate(const ImmediatePostDominators &IPD) {
246 if (Roots.empty()) return;
248 // Add a node for the root. This node might be the actual root, if there is
249 // one exit block, or it may be the virtual exit (denoted by (BasicBlock *)0)
250 // which postdominates all real exits if there are multiple exit blocks.
251 BasicBlock *Root = Roots.size() == 1 ? Roots[0] : 0;
252 Nodes[Root] = RootNode = new Node(Root, 0);
254 Function *F = Roots[0]->getParent();
255 // Loop over all of the reachable blocks in the function...
256 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
257 if (BasicBlock *ImmPostDom = IPD.get(I)) { // Reachable block.
258 Node *&BBNode = Nodes[I];
259 if (!BBNode) { // Haven't calculated this node yet?
260 // Get or calculate the node for the immediate dominator
261 Node *IPDomNode = getNodeForBlock(ImmPostDom);
263 // Add a new tree node for this BasicBlock, and link it as a child of
265 BBNode = IPDomNode->addChild(new Node(I, IPDomNode));
270 //===----------------------------------------------------------------------===//
271 // PostETForest Implementation
272 //===----------------------------------------------------------------------===//
274 static RegisterPass<PostETForest>
275 G("postetforest", "Post-ET-Forest Construction", true);
277 ETNode *PostETForest::getNodeForBlock(BasicBlock *BB) {
278 ETNode *&BBNode = Nodes[BB];
279 if (BBNode) return BBNode;
281 // Haven't calculated this node yet? Get or calculate the node for the
282 // immediate dominator.
283 BasicBlock *IDom = getAnalysis<ImmediatePostDominators>()[BB];
285 // If we are unreachable, we may not have an immediate dominator.
287 return BBNode = new ETNode(BB);
289 ETNode *IDomNode = getNodeForBlock(IDom);
291 // Add a new tree node for this BasicBlock, and link it as a child of
293 BBNode = new ETNode(BB);
294 BBNode->setFather(IDomNode);
299 void PostETForest::calculate(const ImmediatePostDominators &ID) {
300 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
301 Nodes[Roots[i]] = new ETNode(Roots[i]); // Add a node for the root
303 // Iterate over all nodes in inverse depth first order.
304 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
305 for (idf_iterator<BasicBlock*> I = idf_begin(Roots[i]),
306 E = idf_end(Roots[i]); I != E; ++I) {
308 ETNode *&BBNode = Nodes[BB];
310 ETNode *IDomNode = NULL;
313 IDomNode = getNodeForBlock(ID.get(BB));
315 // Add a new ETNode for this BasicBlock, and set it's parent
316 // to it's immediate dominator.
317 BBNode = new ETNode(BB);
319 BBNode->setFather(IDomNode);
324 // Iterate over all nodes in depth first order...
325 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
326 for (idf_iterator<BasicBlock*> I = idf_begin(Roots[i]),
327 E = idf_end(Roots[i]); I != E; ++I) {
328 if (!getNodeForBlock(*I)->hasFather())
329 getNodeForBlock(*I)->assignDFSNumber(dfsnum);
334 //===----------------------------------------------------------------------===//
335 // PostDominanceFrontier Implementation
336 //===----------------------------------------------------------------------===//
338 static RegisterPass<PostDominanceFrontier>
339 H("postdomfrontier", "Post-Dominance Frontier Construction", true);
341 const DominanceFrontier::DomSetType &
342 PostDominanceFrontier::calculate(const PostDominatorTree &DT,
343 const DominatorTree::Node *Node) {
344 // Loop over CFG successors to calculate DFlocal[Node]
345 BasicBlock *BB = Node->getBlock();
346 DomSetType &S = Frontiers[BB]; // The new set to fill in...
347 if (getRoots().empty()) return S;
350 for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB);
352 // Does Node immediately dominate this predecessor?
353 if (DT[*SI]->getIDom() != Node)
356 // At this point, S is DFlocal. Now we union in DFup's of our children...
357 // Loop through and visit the nodes that Node immediately dominates (Node's
358 // children in the IDomTree)
360 for (PostDominatorTree::Node::const_iterator
361 NI = Node->begin(), NE = Node->end(); NI != NE; ++NI) {
362 DominatorTree::Node *IDominee = *NI;
363 const DomSetType &ChildDF = calculate(DT, IDominee);
365 DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end();
366 for (; CDFI != CDFE; ++CDFI) {
367 if (!Node->properlyDominates(DT[*CDFI]))
375 // Ensure that this .cpp file gets linked when PostDominators.h is used.
376 DEFINING_FILE_FOR(PostDominanceFrontier)