blob: bfa152035e0f57d50ce6b875bfb4e84d530ddc79 [file] [log] [blame]
Chris Lattner4c9df7c2002-08-02 16:43:03 +00001//===- PostDominators.cpp - Post-Dominator Calculation --------------------===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner17152292001-07-02 05:46:38 +00009//
Chris Lattner4c9df7c2002-08-02 16:43:03 +000010// This file implements the post-dominator construction algorithms.
Chris Lattner17152292001-07-02 05:46:38 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnera69fd902002-08-21 23:43:50 +000014#include "llvm/Analysis/PostDominators.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000015#include "llvm/Instructions.h"
Chris Lattner221d6882002-02-12 21:07:25 +000016#include "llvm/Support/CFG.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000017#include "llvm/ADT/DepthFirstIterator.h"
18#include "llvm/ADT/SetOperations.h"
Nate Begeman442b32b2006-03-11 02:20:46 +000019#include <iostream>
Chris Lattnercd7c2872003-12-07 00:35:42 +000020using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000021
Chris Lattner94108ab2001-07-06 16:58:22 +000022//===----------------------------------------------------------------------===//
Nate Begeman442b32b2006-03-11 02:20:46 +000023// ImmediatePostDominators Implementation
24//===----------------------------------------------------------------------===//
25
Chris Lattner5d8925c2006-08-27 22:30:17 +000026static RegisterPass<ImmediatePostDominators>
Nate Begeman442b32b2006-03-11 02:20:46 +000027D("postidom", "Immediate Post-Dominators Construction", true);
28
29unsigned ImmediatePostDominators::DFSPass(BasicBlock *V, InfoRec &VInfo,
30 unsigned N) {
Devang Patelc8719e92006-09-07 23:22:37 +000031
32 std::vector<std::pair<BasicBlock *, InfoRec *> > workStack;
33 workStack.push_back(std::make_pair(V, &VInfo));
34
35 do {
36 BasicBlock *currentBB = workStack.back().first;
37 InfoRec *currentVInfo = workStack.back().second;
38 workStack.pop_back();
39
40 currentVInfo->Semi = ++N;
41 currentVInfo->Label = currentBB;
42
43 Vertex.push_back(currentBB); // Vertex[n] = current;
44 // Info[currentBB].Ancestor = 0;
45 // Ancestor[n] = 0
46 // Child[currentBB] = 0;
47 currentVInfo->Size = 1; // Size[currentBB] = 1
48
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);
Devang Patelf93f6832006-09-07 23:29:19 +000052 PI != PE; ++PI) {
Devang Patelc8719e92006-09-07 23:22:37 +000053 InfoRec &SuccVInfo = Info[*PI];
54 if (SuccVInfo.Semi == 0) {
Devang Patelf93f6832006-09-07 23:29:19 +000055 SuccVInfo.Parent = currentBB;
Devang Patelc8719e92006-09-07 23:22:37 +000056
Devang Patelf93f6832006-09-07 23:29:19 +000057 workStack.push_back(std::make_pair(*PI, &SuccVInfo));
Devang Patelc8719e92006-09-07 23:22:37 +000058 }
Nate Begeman442b32b2006-03-11 02:20:46 +000059 }
Devang Patelc8719e92006-09-07 23:22:37 +000060 } while (!workStack.empty());
Nate Begeman442b32b2006-03-11 02:20:46 +000061 return N;
62}
63
64void ImmediatePostDominators::Compress(BasicBlock *V, InfoRec &VInfo) {
65 BasicBlock *VAncestor = VInfo.Ancestor;
66 InfoRec &VAInfo = Info[VAncestor];
67 if (VAInfo.Ancestor == 0)
68 return;
69
70 Compress(VAncestor, VAInfo);
71
72 BasicBlock *VAncestorLabel = VAInfo.Label;
73 BasicBlock *VLabel = VInfo.Label;
74 if (Info[VAncestorLabel].Semi < Info[VLabel].Semi)
75 VInfo.Label = VAncestorLabel;
76
77 VInfo.Ancestor = VAInfo.Ancestor;
78}
79
80BasicBlock *ImmediatePostDominators::Eval(BasicBlock *V) {
81 InfoRec &VInfo = Info[V];
82
83 // Higher-complexity but faster implementation
84 if (VInfo.Ancestor == 0)
85 return V;
86 Compress(V, VInfo);
87 return VInfo.Label;
88}
89
90void ImmediatePostDominators::Link(BasicBlock *V, BasicBlock *W,
91 InfoRec &WInfo) {
92 // Higher-complexity but faster implementation
93 WInfo.Ancestor = V;
94}
95
96bool ImmediatePostDominators::runOnFunction(Function &F) {
97 IDoms.clear(); // Reset from the last time we were run...
98 Roots.clear();
99
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))
105 Roots.push_back(I);
106
107 Vertex.push_back(0);
108
109 // Step #1: Number blocks in depth-first order and initialize variables used
110 // in later stages of the algorithm.
111 unsigned N = 0;
112 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
113 N = DFSPass(Roots[i], Info[Roots[i]], N);
114
115 for (unsigned i = N; i >= 2; --i) {
116 BasicBlock *W = Vertex[i];
117 InfoRec &WInfo = Info[W];
118
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)
124 WInfo.Semi = SemiU;
125 }
126
127 Info[Vertex[WInfo.Semi]].Bucket.push_back(W);
128
129 BasicBlock *WParent = WInfo.Parent;
130 Link(WParent, W, WInfo);
131
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;
139 }
140 }
141
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];
148 }
149
150 // Free temporary memory used to construct idom's
151 Info.clear();
152 std::vector<BasicBlock*>().swap(Vertex);
153
154 return false;
155}
156
157//===----------------------------------------------------------------------===//
Chris Lattner4c9df7c2002-08-02 16:43:03 +0000158// PostDominatorSet Implementation
Chris Lattner17152292001-07-02 05:46:38 +0000159//===----------------------------------------------------------------------===//
160
Chris Lattner5d8925c2006-08-27 22:30:17 +0000161static RegisterPass<PostDominatorSet>
Chris Lattner17689df2002-07-30 16:27:52 +0000162B("postdomset", "Post-Dominator Set Construction", true);
Chris Lattner94108ab2001-07-06 16:58:22 +0000163
Chris Lattnerce6ef112002-07-26 18:40:14 +0000164// 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.
Chris Lattner94108ab2001-07-06 16:58:22 +0000167//
Chris Lattnerce6ef112002-07-26 18:40:14 +0000168bool PostDominatorSet::runOnFunction(Function &F) {
Chris Lattner706e61e2003-09-10 20:37:08 +0000169 // 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
172 // domsets to empty.
173 Roots.clear();
Nate Begeman442b32b2006-03-11 02:20:46 +0000174 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000175 if (succ_begin(I) == succ_end(I))
Chris Lattner706e61e2003-09-10 20:37:08 +0000176 Roots.push_back(I);
Chris Lattner94108ab2001-07-06 16:58:22 +0000177
Chris Lattner706e61e2003-09-10 20:37:08 +0000178 // 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
180 // example.
Nate Begeman442b32b2006-03-11 02:20:46 +0000181 ImmediatePostDominators &IPD = getAnalysis<ImmediatePostDominators>();
182 Doms.clear(); // Reset from the last time we were run...
Chris Lattner706e61e2003-09-10 20:37:08 +0000183 if (Roots.empty()) return false;
184
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.
Nate Begeman442b32b2006-03-11 02:20:46 +0000187 //if (Roots.size() > 1)
188 // Doms[0].insert(0);
Chris Lattner706e61e2003-09-10 20:37:08 +0000189
Nate Begeman442b32b2006-03-11 02:20:46 +0000190 // Root nodes only dominate themselves.
191 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
192 Doms[Roots[i]].insert(Roots[i]);
193
194 // Loop over all of the blocks in the function, calculating dominator sets for
195 // each function.
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
Chris Lattner94108ab2001-07-06 16:58:22 +0000201
Nate Begeman442b32b2006-03-11 02:20:46 +0000202 // Insert all dominators into the set...
203 while (IPDom) {
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];
207 if (!IPDS.empty()) {
208 DS.insert(IPDS.begin(), IPDS.end());
209 break;
Chris Lattner706e61e2003-09-10 20:37:08 +0000210 } else {
Nate Begeman442b32b2006-03-11 02:20:46 +0000211 DS.insert(IPDom);
212 IPDom = IPD[IPDom];
Chris Lattner706e61e2003-09-10 20:37:08 +0000213 }
Chris Lattner94108ab2001-07-06 16:58:22 +0000214 }
Nate Begeman442b32b2006-03-11 02:20:46 +0000215 } else {
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.
218 Doms[I];
Chris Lattnercd7c2872003-12-07 00:35:42 +0000219 }
Nate Begeman442b32b2006-03-11 02:20:46 +0000220
221 return false;
Chris Lattnercd7c2872003-12-07 00:35:42 +0000222}
223
Chris Lattner17152292001-07-02 05:46:38 +0000224//===----------------------------------------------------------------------===//
Chris Lattner4c9df7c2002-08-02 16:43:03 +0000225// PostDominatorTree Implementation
Chris Lattner17152292001-07-02 05:46:38 +0000226//===----------------------------------------------------------------------===//
227
Chris Lattner5d8925c2006-08-27 22:30:17 +0000228static RegisterPass<PostDominatorTree>
Chris Lattner17689df2002-07-30 16:27:52 +0000229F("postdomtree", "Post-Dominator Tree Construction", true);
Chris Lattner93193f82002-01-31 00:42:27 +0000230
Nate Begeman442b32b2006-03-11 02:20:46 +0000231DominatorTreeBase::Node *PostDominatorTree::getNodeForBlock(BasicBlock *BB) {
232 Node *&BBNode = Nodes[BB];
233 if (BBNode) return BBNode;
234
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);
239
240 // Add a new tree node for this BasicBlock, and link it as a child of
241 // IDomNode
242 return BBNode = IPDomNode->addChild(new Node(BB, IPDomNode));
243}
244
245void PostDominatorTree::calculate(const ImmediatePostDominators &IPD) {
Chris Lattner706e61e2003-09-10 20:37:08 +0000246 if (Roots.empty()) return;
Nate Begeman442b32b2006-03-11 02:20:46 +0000247
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.
Chris Lattner706e61e2003-09-10 20:37:08 +0000251 BasicBlock *Root = Roots.size() == 1 ? Roots[0] : 0;
Nate Begeman442b32b2006-03-11 02:20:46 +0000252 Nodes[Root] = RootNode = new Node(Root, 0);
253
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);
262
263 // Add a new tree node for this BasicBlock, and link it as a child of
264 // IDomNode
265 BBNode = IPDomNode->addChild(new Node(I, IPDomNode));
Chris Lattner17152292001-07-02 05:46:38 +0000266 }
267 }
Chris Lattner17152292001-07-02 05:46:38 +0000268}
Nate Begeman442b32b2006-03-11 02:20:46 +0000269
Chris Lattnerccacd3c2006-01-08 08:22:18 +0000270//===----------------------------------------------------------------------===//
271// PostETForest Implementation
272//===----------------------------------------------------------------------===//
273
Chris Lattner5d8925c2006-08-27 22:30:17 +0000274static RegisterPass<PostETForest>
Chris Lattnerccacd3c2006-01-08 08:22:18 +0000275G("postetforest", "Post-ET-Forest Construction", true);
276
277ETNode *PostETForest::getNodeForBlock(BasicBlock *BB) {
278 ETNode *&BBNode = Nodes[BB];
279 if (BBNode) return BBNode;
280
281 // Haven't calculated this node yet? Get or calculate the node for the
282 // immediate dominator.
283 BasicBlock *IDom = getAnalysis<ImmediatePostDominators>()[BB];
284
285 // If we are unreachable, we may not have an immediate dominator.
286 if (!IDom)
287 return BBNode = new ETNode(BB);
288 else {
289 ETNode *IDomNode = getNodeForBlock(IDom);
290
291 // Add a new tree node for this BasicBlock, and link it as a child of
292 // IDomNode
293 BBNode = new ETNode(BB);
294 BBNode->setFather(IDomNode);
295 return BBNode;
296 }
297}
298
299void 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
302
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) {
307 BasicBlock *BB = *I;
308 ETNode *&BBNode = Nodes[BB];
309 if (!BBNode) {
310 ETNode *IDomNode = NULL;
311
312 if (ID.get(BB))
313 IDomNode = getNodeForBlock(ID.get(BB));
314
315 // Add a new ETNode for this BasicBlock, and set it's parent
316 // to it's immediate dominator.
317 BBNode = new ETNode(BB);
318 if (IDomNode)
319 BBNode->setFather(IDomNode);
320 }
321 }
322
323 int dfsnum = 0;
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);
330 }
331 DFSInfoValid = true;
332}
Chris Lattner17152292001-07-02 05:46:38 +0000333
Chris Lattner17152292001-07-02 05:46:38 +0000334//===----------------------------------------------------------------------===//
Chris Lattner4c9df7c2002-08-02 16:43:03 +0000335// PostDominanceFrontier Implementation
Chris Lattner17152292001-07-02 05:46:38 +0000336//===----------------------------------------------------------------------===//
337
Chris Lattner5d8925c2006-08-27 22:30:17 +0000338static RegisterPass<PostDominanceFrontier>
Chris Lattner17689df2002-07-30 16:27:52 +0000339H("postdomfrontier", "Post-Dominance Frontier Construction", true);
Chris Lattner93193f82002-01-31 00:42:27 +0000340
Chris Lattner1b7f7dc2002-04-28 16:21:30 +0000341const DominanceFrontier::DomSetType &
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000342PostDominanceFrontier::calculate(const PostDominatorTree &DT,
Chris Lattnerce6ef112002-07-26 18:40:14 +0000343 const DominatorTree::Node *Node) {
Chris Lattner94108ab2001-07-06 16:58:22 +0000344 // Loop over CFG successors to calculate DFlocal[Node]
Chris Lattnerc444a422003-09-11 16:26:13 +0000345 BasicBlock *BB = Node->getBlock();
Chris Lattner94108ab2001-07-06 16:58:22 +0000346 DomSetType &S = Frontiers[BB]; // The new set to fill in...
Chris Lattner706e61e2003-09-10 20:37:08 +0000347 if (getRoots().empty()) return S;
Chris Lattner94108ab2001-07-06 16:58:22 +0000348
Chris Lattner706e61e2003-09-10 20:37:08 +0000349 if (BB)
350 for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB);
351 SI != SE; ++SI)
Misha Brukman2f2d0652003-09-11 18:14:24 +0000352 // Does Node immediately dominate this predecessor?
Chris Lattner706e61e2003-09-10 20:37:08 +0000353 if (DT[*SI]->getIDom() != Node)
354 S.insert(*SI);
Chris Lattner94108ab2001-07-06 16:58:22 +0000355
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)
359 //
Chris Lattnerce6ef112002-07-26 18:40:14 +0000360 for (PostDominatorTree::Node::const_iterator
361 NI = Node->begin(), NE = Node->end(); NI != NE; ++NI) {
Chris Lattner94108ab2001-07-06 16:58:22 +0000362 DominatorTree::Node *IDominee = *NI;
Chris Lattnerce6ef112002-07-26 18:40:14 +0000363 const DomSetType &ChildDF = calculate(DT, IDominee);
Chris Lattner94108ab2001-07-06 16:58:22 +0000364
365 DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end();
366 for (; CDFI != CDFE; ++CDFI) {
Chris Lattner4b5086c2005-11-18 07:28:26 +0000367 if (!Node->properlyDominates(DT[*CDFI]))
Misha Brukmandedf2bd2005-04-22 04:01:18 +0000368 S.insert(*CDFI);
Chris Lattner94108ab2001-07-06 16:58:22 +0000369 }
370 }
371
372 return S;
373}
Chris Lattnera69fd902002-08-21 23:43:50 +0000374
Reid Spencer4f1bd9e2006-06-07 22:00:26 +0000375// Ensure that this .cpp file gets linked when PostDominators.h is used.
376DEFINING_FILE_FOR(PostDominanceFrontier)