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,
31 std::vector<std::pair<BasicBlock *, InfoRec *> > workStack;
32 std::set<BasicBlock *> visited;
33 workStack.push_back(std::make_pair(V, &VInfo));
36 BasicBlock *currentBB = workStack.back().first;
37 InfoRec *currentVInfo = workStack.back().second;
39 // Visit each block only once.
40 if (visited.count(currentBB) == 0) {
42 visited.insert(currentBB);
43 currentVInfo->Semi = ++N;
44 currentVInfo->Label = currentBB;
46 Vertex.push_back(currentBB); // Vertex[n] = current;
47 // Info[currentBB].Ancestor = 0;
49 // Child[currentBB] = 0;
50 currentVInfo->Size = 1; // Size[currentBB] = 1
54 bool visitChild = false;
55 for (pred_iterator PI = pred_begin(currentBB), PE = pred_end(currentBB);
56 PI != PE && !visitChild; ++PI) {
57 InfoRec &SuccVInfo = Info[*PI];
58 if (SuccVInfo.Semi == 0) {
59 SuccVInfo.Parent = currentBB;
60 if (visited.count (*PI) == 0) {
61 workStack.push_back(std::make_pair(*PI, &SuccVInfo));
67 // If all children are visited or if this block has no child then pop this
68 // block out of workStack.
72 } while (!workStack.empty());
77 void ImmediatePostDominators::Compress(BasicBlock *V, InfoRec &VInfo) {
78 BasicBlock *VAncestor = VInfo.Ancestor;
79 InfoRec &VAInfo = Info[VAncestor];
80 if (VAInfo.Ancestor == 0)
83 Compress(VAncestor, VAInfo);
85 BasicBlock *VAncestorLabel = VAInfo.Label;
86 BasicBlock *VLabel = VInfo.Label;
87 if (Info[VAncestorLabel].Semi < Info[VLabel].Semi)
88 VInfo.Label = VAncestorLabel;
90 VInfo.Ancestor = VAInfo.Ancestor;
93 BasicBlock *ImmediatePostDominators::Eval(BasicBlock *V) {
94 InfoRec &VInfo = Info[V];
96 // Higher-complexity but faster implementation
97 if (VInfo.Ancestor == 0)
103 void ImmediatePostDominators::Link(BasicBlock *V, BasicBlock *W,
105 // Higher-complexity but faster implementation
109 bool ImmediatePostDominators::runOnFunction(Function &F) {
110 IDoms.clear(); // Reset from the last time we were run...
113 // Step #0: Scan the function looking for the root nodes of the post-dominance
114 // relationships. These blocks, which have no successors, end with return and
115 // unwind instructions.
116 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
117 if (succ_begin(I) == succ_end(I))
122 // Step #1: Number blocks in depth-first order and initialize variables used
123 // in later stages of the algorithm.
125 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
126 N = DFSPass(Roots[i], Info[Roots[i]], N);
128 for (unsigned i = N; i >= 2; --i) {
129 BasicBlock *W = Vertex[i];
130 InfoRec &WInfo = Info[W];
132 // Step #2: Calculate the semidominators of all vertices
133 for (succ_iterator SI = succ_begin(W), SE = succ_end(W); SI != SE; ++SI)
134 if (Info.count(*SI)) { // Only if this predecessor is reachable!
135 unsigned SemiU = Info[Eval(*SI)].Semi;
136 if (SemiU < WInfo.Semi)
140 Info[Vertex[WInfo.Semi]].Bucket.push_back(W);
142 BasicBlock *WParent = WInfo.Parent;
143 Link(WParent, W, WInfo);
145 // Step #3: Implicitly define the immediate dominator of vertices
146 std::vector<BasicBlock*> &WParentBucket = Info[WParent].Bucket;
147 while (!WParentBucket.empty()) {
148 BasicBlock *V = WParentBucket.back();
149 WParentBucket.pop_back();
150 BasicBlock *U = Eval(V);
151 IDoms[V] = Info[U].Semi < Info[V].Semi ? U : WParent;
155 // Step #4: Explicitly define the immediate dominator of each vertex
156 for (unsigned i = 2; i <= N; ++i) {
157 BasicBlock *W = Vertex[i];
158 BasicBlock *&WIDom = IDoms[W];
159 if (WIDom != Vertex[Info[W].Semi])
160 WIDom = IDoms[WIDom];
163 // Free temporary memory used to construct idom's
165 std::vector<BasicBlock*>().swap(Vertex);
170 //===----------------------------------------------------------------------===//
171 // PostDominatorSet Implementation
172 //===----------------------------------------------------------------------===//
174 static RegisterPass<PostDominatorSet>
175 B("postdomset", "Post-Dominator Set Construction", true);
177 // Postdominator set construction. This converts the specified function to only
178 // have a single exit node (return stmt), then calculates the post dominance
179 // sets for the function.
181 bool PostDominatorSet::runOnFunction(Function &F) {
182 // Scan the function looking for the root nodes of the post-dominance
183 // relationships. These blocks end with return and unwind instructions.
184 // While we are iterating over the function, we also initialize all of the
187 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
188 if (succ_begin(I) == succ_end(I))
191 // If there are no exit nodes for the function, postdomsets are all empty.
192 // This can happen if the function just contains an infinite loop, for
194 ImmediatePostDominators &IPD = getAnalysis<ImmediatePostDominators>();
195 Doms.clear(); // Reset from the last time we were run...
196 if (Roots.empty()) return false;
198 // If we have more than one root, we insert an artificial "null" exit, which
199 // has "virtual edges" to each of the real exit nodes.
200 //if (Roots.size() > 1)
201 // Doms[0].insert(0);
203 // Root nodes only dominate themselves.
204 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
205 Doms[Roots[i]].insert(Roots[i]);
207 // Loop over all of the blocks in the function, calculating dominator sets for
209 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
210 if (BasicBlock *IPDom = IPD[I]) { // Get idom if block is reachable
211 DomSetType &DS = Doms[I];
212 assert(DS.empty() && "PostDomset already filled in for this block?");
213 DS.insert(I); // Blocks always dominate themselves
215 // Insert all dominators into the set...
217 // If we have already computed the dominator sets for our immediate post
218 // dominator, just use it instead of walking all the way up to the root.
219 DomSetType &IPDS = Doms[IPDom];
221 DS.insert(IPDS.begin(), IPDS.end());
229 // Ensure that every basic block has at least an empty set of nodes. This
230 // is important for the case when there is unreachable blocks.
237 //===----------------------------------------------------------------------===//
238 // PostDominatorTree Implementation
239 //===----------------------------------------------------------------------===//
241 static RegisterPass<PostDominatorTree>
242 F("postdomtree", "Post-Dominator Tree Construction", true);
244 DominatorTreeBase::Node *PostDominatorTree::getNodeForBlock(BasicBlock *BB) {
245 Node *&BBNode = Nodes[BB];
246 if (BBNode) return BBNode;
248 // Haven't calculated this node yet? Get or calculate the node for the
249 // immediate postdominator.
250 BasicBlock *IPDom = getAnalysis<ImmediatePostDominators>()[BB];
251 Node *IPDomNode = getNodeForBlock(IPDom);
253 // Add a new tree node for this BasicBlock, and link it as a child of
255 return BBNode = IPDomNode->addChild(new Node(BB, IPDomNode));
258 void PostDominatorTree::calculate(const ImmediatePostDominators &IPD) {
259 if (Roots.empty()) return;
261 // Add a node for the root. This node might be the actual root, if there is
262 // one exit block, or it may be the virtual exit (denoted by (BasicBlock *)0)
263 // which postdominates all real exits if there are multiple exit blocks.
264 BasicBlock *Root = Roots.size() == 1 ? Roots[0] : 0;
265 Nodes[Root] = RootNode = new Node(Root, 0);
267 Function *F = Roots[0]->getParent();
268 // Loop over all of the reachable blocks in the function...
269 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
270 if (BasicBlock *ImmPostDom = IPD.get(I)) { // Reachable block.
271 Node *&BBNode = Nodes[I];
272 if (!BBNode) { // Haven't calculated this node yet?
273 // Get or calculate the node for the immediate dominator
274 Node *IPDomNode = getNodeForBlock(ImmPostDom);
276 // Add a new tree node for this BasicBlock, and link it as a child of
278 BBNode = IPDomNode->addChild(new Node(I, IPDomNode));
283 //===----------------------------------------------------------------------===//
284 // PostETForest Implementation
285 //===----------------------------------------------------------------------===//
287 static RegisterPass<PostETForest>
288 G("postetforest", "Post-ET-Forest Construction", true);
290 ETNode *PostETForest::getNodeForBlock(BasicBlock *BB) {
291 ETNode *&BBNode = Nodes[BB];
292 if (BBNode) return BBNode;
294 // Haven't calculated this node yet? Get or calculate the node for the
295 // immediate dominator.
296 BasicBlock *IDom = getAnalysis<ImmediatePostDominators>()[BB];
298 // If we are unreachable, we may not have an immediate dominator.
300 return BBNode = new ETNode(BB);
302 ETNode *IDomNode = getNodeForBlock(IDom);
304 // Add a new tree node for this BasicBlock, and link it as a child of
306 BBNode = new ETNode(BB);
307 BBNode->setFather(IDomNode);
312 void PostETForest::calculate(const ImmediatePostDominators &ID) {
313 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
314 Nodes[Roots[i]] = new ETNode(Roots[i]); // Add a node for the root
316 // Iterate over all nodes in inverse depth first order.
317 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
318 for (idf_iterator<BasicBlock*> I = idf_begin(Roots[i]),
319 E = idf_end(Roots[i]); I != E; ++I) {
321 ETNode *&BBNode = Nodes[BB];
323 ETNode *IDomNode = NULL;
326 IDomNode = getNodeForBlock(ID.get(BB));
328 // Add a new ETNode for this BasicBlock, and set it's parent
329 // to it's immediate dominator.
330 BBNode = new ETNode(BB);
332 BBNode->setFather(IDomNode);
337 // Iterate over all nodes in depth first order...
338 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
339 for (idf_iterator<BasicBlock*> I = idf_begin(Roots[i]),
340 E = idf_end(Roots[i]); I != E; ++I) {
341 if (!getNodeForBlock(*I)->hasFather())
342 getNodeForBlock(*I)->assignDFSNumber(dfsnum);
347 //===----------------------------------------------------------------------===//
348 // PostDominanceFrontier Implementation
349 //===----------------------------------------------------------------------===//
351 static RegisterPass<PostDominanceFrontier>
352 H("postdomfrontier", "Post-Dominance Frontier Construction", true);
354 const DominanceFrontier::DomSetType &
355 PostDominanceFrontier::calculate(const PostDominatorTree &DT,
356 const DominatorTree::Node *Node) {
357 // Loop over CFG successors to calculate DFlocal[Node]
358 BasicBlock *BB = Node->getBlock();
359 DomSetType &S = Frontiers[BB]; // The new set to fill in...
360 if (getRoots().empty()) return S;
363 for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB);
365 // Does Node immediately dominate this predecessor?
366 if (DT[*SI]->getIDom() != Node)
369 // At this point, S is DFlocal. Now we union in DFup's of our children...
370 // Loop through and visit the nodes that Node immediately dominates (Node's
371 // children in the IDomTree)
373 for (PostDominatorTree::Node::const_iterator
374 NI = Node->begin(), NE = Node->end(); NI != NE; ++NI) {
375 DominatorTree::Node *IDominee = *NI;
376 const DomSetType &ChildDF = calculate(DT, IDominee);
378 DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end();
379 for (; CDFI != CDFE; ++CDFI) {
380 if (!Node->properlyDominates(DT[*CDFI]))
388 // Ensure that this .cpp file gets linked when PostDominators.h is used.
389 DEFINING_FILE_FOR(PostDominanceFrontier)