blob: d1fe9dd7f6614ef8e8e4be6eca4db92437922d73 [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"
Chris Lattnercd7c2872003-12-07 00:35:42 +000019using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000020
Chris Lattner94108ab2001-07-06 16:58:22 +000021//===----------------------------------------------------------------------===//
Nate Begeman442b32b2006-03-11 02:20:46 +000022// ImmediatePostDominators Implementation
23//===----------------------------------------------------------------------===//
24
Chris Lattner5d8925c2006-08-27 22:30:17 +000025static RegisterPass<ImmediatePostDominators>
Nate Begeman442b32b2006-03-11 02:20:46 +000026D("postidom", "Immediate Post-Dominators Construction", true);
27
28unsigned ImmediatePostDominators::DFSPass(BasicBlock *V, InfoRec &VInfo,
29 unsigned N) {
Devang Patelc8719e92006-09-07 23:22:37 +000030 std::vector<std::pair<BasicBlock *, InfoRec *> > workStack;
Devang Patel79db5b72006-09-27 17:18:05 +000031 std::set<BasicBlock *> visited;
Devang Patelc8719e92006-09-07 23:22:37 +000032 workStack.push_back(std::make_pair(V, &VInfo));
33
34 do {
35 BasicBlock *currentBB = workStack.back().first;
36 InfoRec *currentVInfo = workStack.back().second;
Devang Patelc8719e92006-09-07 23:22:37 +000037
Devang Patel79db5b72006-09-27 17:18:05 +000038 // Visit each block only once.
39 if (visited.count(currentBB) == 0) {
Devang Patelc8719e92006-09-07 23:22:37 +000040
Devang Patel79db5b72006-09-27 17:18:05 +000041 visited.insert(currentBB);
42 currentVInfo->Semi = ++N;
43 currentVInfo->Label = currentBB;
44
45 Vertex.push_back(currentBB); // Vertex[n] = current;
46 // Info[currentBB].Ancestor = 0;
47 // Ancestor[n] = 0
48 // Child[currentBB] = 0;
49 currentVInfo->Size = 1; // Size[currentBB] = 1
50 }
Devang Patelc8719e92006-09-07 23:22:37 +000051
Devang Patel79db5b72006-09-27 17:18:05 +000052 // Visit children
53 bool visitChild = false;
Devang Patelc8719e92006-09-07 23:22:37 +000054 for (pred_iterator PI = pred_begin(currentBB), PE = pred_end(currentBB);
Devang Patel79db5b72006-09-27 17:18:05 +000055 PI != PE && !visitChild; ++PI) {
Devang Patelc8719e92006-09-07 23:22:37 +000056 InfoRec &SuccVInfo = Info[*PI];
57 if (SuccVInfo.Semi == 0) {
Devang Patelf93f6832006-09-07 23:29:19 +000058 SuccVInfo.Parent = currentBB;
Devang Patel79db5b72006-09-27 17:18:05 +000059 if (visited.count (*PI) == 0) {
60 workStack.push_back(std::make_pair(*PI, &SuccVInfo));
61 visitChild = true;
62 }
Devang Patelc8719e92006-09-07 23:22:37 +000063 }
Nate Begeman442b32b2006-03-11 02:20:46 +000064 }
Devang Patel79db5b72006-09-27 17:18:05 +000065
66 // If all children are visited or if this block has no child then pop this
67 // block out of workStack.
68 if (!visitChild)
69 workStack.pop_back();
70
Devang Patelc8719e92006-09-07 23:22:37 +000071 } while (!workStack.empty());
Devang Patel79db5b72006-09-27 17:18:05 +000072
Nate Begeman442b32b2006-03-11 02:20:46 +000073 return N;
74}
75
76void ImmediatePostDominators::Compress(BasicBlock *V, InfoRec &VInfo) {
77 BasicBlock *VAncestor = VInfo.Ancestor;
78 InfoRec &VAInfo = Info[VAncestor];
79 if (VAInfo.Ancestor == 0)
80 return;
81
82 Compress(VAncestor, VAInfo);
83
84 BasicBlock *VAncestorLabel = VAInfo.Label;
85 BasicBlock *VLabel = VInfo.Label;
86 if (Info[VAncestorLabel].Semi < Info[VLabel].Semi)
87 VInfo.Label = VAncestorLabel;
88
89 VInfo.Ancestor = VAInfo.Ancestor;
90}
91
92BasicBlock *ImmediatePostDominators::Eval(BasicBlock *V) {
93 InfoRec &VInfo = Info[V];
94
95 // Higher-complexity but faster implementation
96 if (VInfo.Ancestor == 0)
97 return V;
98 Compress(V, VInfo);
99 return VInfo.Label;
100}
101
102void ImmediatePostDominators::Link(BasicBlock *V, BasicBlock *W,
103 InfoRec &WInfo) {
104 // Higher-complexity but faster implementation
105 WInfo.Ancestor = V;
106}
107
108bool ImmediatePostDominators::runOnFunction(Function &F) {
109 IDoms.clear(); // Reset from the last time we were run...
110 Roots.clear();
111
112 // Step #0: Scan the function looking for the root nodes of the post-dominance
113 // relationships. These blocks, which have no successors, end with return and
114 // unwind instructions.
115 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
116 if (succ_begin(I) == succ_end(I))
117 Roots.push_back(I);
118
119 Vertex.push_back(0);
120
121 // Step #1: Number blocks in depth-first order and initialize variables used
122 // in later stages of the algorithm.
123 unsigned N = 0;
124 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
125 N = DFSPass(Roots[i], Info[Roots[i]], N);
126
127 for (unsigned i = N; i >= 2; --i) {
128 BasicBlock *W = Vertex[i];
129 InfoRec &WInfo = Info[W];
130
131 // Step #2: Calculate the semidominators of all vertices
132 for (succ_iterator SI = succ_begin(W), SE = succ_end(W); SI != SE; ++SI)
133 if (Info.count(*SI)) { // Only if this predecessor is reachable!
134 unsigned SemiU = Info[Eval(*SI)].Semi;
135 if (SemiU < WInfo.Semi)
136 WInfo.Semi = SemiU;
137 }
138
139 Info[Vertex[WInfo.Semi]].Bucket.push_back(W);
140
141 BasicBlock *WParent = WInfo.Parent;
142 Link(WParent, W, WInfo);
143
144 // Step #3: Implicitly define the immediate dominator of vertices
145 std::vector<BasicBlock*> &WParentBucket = Info[WParent].Bucket;
146 while (!WParentBucket.empty()) {
147 BasicBlock *V = WParentBucket.back();
148 WParentBucket.pop_back();
149 BasicBlock *U = Eval(V);
150 IDoms[V] = Info[U].Semi < Info[V].Semi ? U : WParent;
151 }
152 }
153
154 // Step #4: Explicitly define the immediate dominator of each vertex
155 for (unsigned i = 2; i <= N; ++i) {
156 BasicBlock *W = Vertex[i];
157 BasicBlock *&WIDom = IDoms[W];
158 if (WIDom != Vertex[Info[W].Semi])
159 WIDom = IDoms[WIDom];
160 }
161
162 // Free temporary memory used to construct idom's
163 Info.clear();
164 std::vector<BasicBlock*>().swap(Vertex);
165
166 return false;
167}
168
169//===----------------------------------------------------------------------===//
Chris Lattner4c9df7c2002-08-02 16:43:03 +0000170// PostDominatorSet Implementation
Chris Lattner17152292001-07-02 05:46:38 +0000171//===----------------------------------------------------------------------===//
172
Chris Lattner5d8925c2006-08-27 22:30:17 +0000173static RegisterPass<PostDominatorSet>
Chris Lattner17689df2002-07-30 16:27:52 +0000174B("postdomset", "Post-Dominator Set Construction", true);
Chris Lattner94108ab2001-07-06 16:58:22 +0000175
Chris Lattnerce6ef112002-07-26 18:40:14 +0000176// Postdominator set construction. This converts the specified function to only
177// have a single exit node (return stmt), then calculates the post dominance
178// sets for the function.
Chris Lattner94108ab2001-07-06 16:58:22 +0000179//
Chris Lattnerce6ef112002-07-26 18:40:14 +0000180bool PostDominatorSet::runOnFunction(Function &F) {
Chris Lattner706e61e2003-09-10 20:37:08 +0000181 // Scan the function looking for the root nodes of the post-dominance
182 // relationships. These blocks end with return and unwind instructions.
183 // While we are iterating over the function, we also initialize all of the
184 // domsets to empty.
185 Roots.clear();
Nate Begeman442b32b2006-03-11 02:20:46 +0000186 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Chris Lattnerec7c1ab2004-10-16 18:21:33 +0000187 if (succ_begin(I) == succ_end(I))
Chris Lattner706e61e2003-09-10 20:37:08 +0000188 Roots.push_back(I);
Chris Lattner94108ab2001-07-06 16:58:22 +0000189
Chris Lattner706e61e2003-09-10 20:37:08 +0000190 // If there are no exit nodes for the function, postdomsets are all empty.
191 // This can happen if the function just contains an infinite loop, for
192 // example.
Nate Begeman442b32b2006-03-11 02:20:46 +0000193 ImmediatePostDominators &IPD = getAnalysis<ImmediatePostDominators>();
194 Doms.clear(); // Reset from the last time we were run...
Chris Lattner706e61e2003-09-10 20:37:08 +0000195 if (Roots.empty()) return false;
196
197 // If we have more than one root, we insert an artificial "null" exit, which
198 // has "virtual edges" to each of the real exit nodes.
Nate Begeman442b32b2006-03-11 02:20:46 +0000199 //if (Roots.size() > 1)
200 // Doms[0].insert(0);
Chris Lattner706e61e2003-09-10 20:37:08 +0000201
Nate Begeman442b32b2006-03-11 02:20:46 +0000202 // Root nodes only dominate themselves.
203 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
204 Doms[Roots[i]].insert(Roots[i]);
205
206 // Loop over all of the blocks in the function, calculating dominator sets for
207 // each function.
208 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
209 if (BasicBlock *IPDom = IPD[I]) { // Get idom if block is reachable
210 DomSetType &DS = Doms[I];
211 assert(DS.empty() && "PostDomset already filled in for this block?");
212 DS.insert(I); // Blocks always dominate themselves
Chris Lattner94108ab2001-07-06 16:58:22 +0000213
Nate Begeman442b32b2006-03-11 02:20:46 +0000214 // Insert all dominators into the set...
215 while (IPDom) {
216 // If we have already computed the dominator sets for our immediate post
217 // dominator, just use it instead of walking all the way up to the root.
218 DomSetType &IPDS = Doms[IPDom];
219 if (!IPDS.empty()) {
220 DS.insert(IPDS.begin(), IPDS.end());
221 break;
Chris Lattner706e61e2003-09-10 20:37:08 +0000222 } else {
Nate Begeman442b32b2006-03-11 02:20:46 +0000223 DS.insert(IPDom);
224 IPDom = IPD[IPDom];
Chris Lattner706e61e2003-09-10 20:37:08 +0000225 }
Chris Lattner94108ab2001-07-06 16:58:22 +0000226 }
Nate Begeman442b32b2006-03-11 02:20:46 +0000227 } else {
228 // Ensure that every basic block has at least an empty set of nodes. This
229 // is important for the case when there is unreachable blocks.
230 Doms[I];
Chris Lattnercd7c2872003-12-07 00:35:42 +0000231 }
Nate Begeman442b32b2006-03-11 02:20:46 +0000232
233 return false;
Chris Lattnercd7c2872003-12-07 00:35:42 +0000234}
235
Chris Lattner17152292001-07-02 05:46:38 +0000236//===----------------------------------------------------------------------===//
Chris Lattner4c9df7c2002-08-02 16:43:03 +0000237// PostDominatorTree Implementation
Chris Lattner17152292001-07-02 05:46:38 +0000238//===----------------------------------------------------------------------===//
239
Chris Lattner5d8925c2006-08-27 22:30:17 +0000240static RegisterPass<PostDominatorTree>
Chris Lattner17689df2002-07-30 16:27:52 +0000241F("postdomtree", "Post-Dominator Tree Construction", true);
Chris Lattner93193f82002-01-31 00:42:27 +0000242
Nate Begeman442b32b2006-03-11 02:20:46 +0000243DominatorTreeBase::Node *PostDominatorTree::getNodeForBlock(BasicBlock *BB) {
244 Node *&BBNode = Nodes[BB];
245 if (BBNode) return BBNode;
246
247 // Haven't calculated this node yet? Get or calculate the node for the
248 // immediate postdominator.
249 BasicBlock *IPDom = getAnalysis<ImmediatePostDominators>()[BB];
250 Node *IPDomNode = getNodeForBlock(IPDom);
251
252 // Add a new tree node for this BasicBlock, and link it as a child of
253 // IDomNode
254 return BBNode = IPDomNode->addChild(new Node(BB, IPDomNode));
255}
256
257void PostDominatorTree::calculate(const ImmediatePostDominators &IPD) {
Chris Lattner706e61e2003-09-10 20:37:08 +0000258 if (Roots.empty()) return;
Nate Begeman442b32b2006-03-11 02:20:46 +0000259
260 // Add a node for the root. This node might be the actual root, if there is
261 // one exit block, or it may be the virtual exit (denoted by (BasicBlock *)0)
262 // which postdominates all real exits if there are multiple exit blocks.
Chris Lattner706e61e2003-09-10 20:37:08 +0000263 BasicBlock *Root = Roots.size() == 1 ? Roots[0] : 0;
Nate Begeman442b32b2006-03-11 02:20:46 +0000264 Nodes[Root] = RootNode = new Node(Root, 0);
265
266 Function *F = Roots[0]->getParent();
267 // Loop over all of the reachable blocks in the function...
268 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
269 if (BasicBlock *ImmPostDom = IPD.get(I)) { // Reachable block.
270 Node *&BBNode = Nodes[I];
271 if (!BBNode) { // Haven't calculated this node yet?
272 // Get or calculate the node for the immediate dominator
273 Node *IPDomNode = getNodeForBlock(ImmPostDom);
274
275 // Add a new tree node for this BasicBlock, and link it as a child of
276 // IDomNode
277 BBNode = IPDomNode->addChild(new Node(I, IPDomNode));
Chris Lattner17152292001-07-02 05:46:38 +0000278 }
279 }
Chris Lattner17152292001-07-02 05:46:38 +0000280}
Nate Begeman442b32b2006-03-11 02:20:46 +0000281
Chris Lattnerccacd3c2006-01-08 08:22:18 +0000282//===----------------------------------------------------------------------===//
283// PostETForest Implementation
284//===----------------------------------------------------------------------===//
285
Chris Lattner5d8925c2006-08-27 22:30:17 +0000286static RegisterPass<PostETForest>
Chris Lattnerccacd3c2006-01-08 08:22:18 +0000287G("postetforest", "Post-ET-Forest Construction", true);
288
289ETNode *PostETForest::getNodeForBlock(BasicBlock *BB) {
290 ETNode *&BBNode = Nodes[BB];
291 if (BBNode) return BBNode;
292
293 // Haven't calculated this node yet? Get or calculate the node for the
294 // immediate dominator.
295 BasicBlock *IDom = getAnalysis<ImmediatePostDominators>()[BB];
296
297 // If we are unreachable, we may not have an immediate dominator.
298 if (!IDom)
299 return BBNode = new ETNode(BB);
300 else {
301 ETNode *IDomNode = getNodeForBlock(IDom);
302
303 // Add a new tree node for this BasicBlock, and link it as a child of
304 // IDomNode
305 BBNode = new ETNode(BB);
306 BBNode->setFather(IDomNode);
307 return BBNode;
308 }
309}
310
311void PostETForest::calculate(const ImmediatePostDominators &ID) {
312 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
313 Nodes[Roots[i]] = new ETNode(Roots[i]); // Add a node for the root
314
315 // Iterate over all nodes in inverse depth first order.
316 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
317 for (idf_iterator<BasicBlock*> I = idf_begin(Roots[i]),
318 E = idf_end(Roots[i]); I != E; ++I) {
319 BasicBlock *BB = *I;
320 ETNode *&BBNode = Nodes[BB];
321 if (!BBNode) {
322 ETNode *IDomNode = NULL;
323
324 if (ID.get(BB))
325 IDomNode = getNodeForBlock(ID.get(BB));
326
327 // Add a new ETNode for this BasicBlock, and set it's parent
328 // to it's immediate dominator.
329 BBNode = new ETNode(BB);
330 if (IDomNode)
331 BBNode->setFather(IDomNode);
332 }
333 }
334
335 int dfsnum = 0;
336 // Iterate over all nodes in depth first order...
337 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
338 for (idf_iterator<BasicBlock*> I = idf_begin(Roots[i]),
339 E = idf_end(Roots[i]); I != E; ++I) {
340 if (!getNodeForBlock(*I)->hasFather())
341 getNodeForBlock(*I)->assignDFSNumber(dfsnum);
342 }
343 DFSInfoValid = true;
344}
Chris Lattner17152292001-07-02 05:46:38 +0000345
Chris Lattner17152292001-07-02 05:46:38 +0000346//===----------------------------------------------------------------------===//
Chris Lattner4c9df7c2002-08-02 16:43:03 +0000347// PostDominanceFrontier Implementation
Chris Lattner17152292001-07-02 05:46:38 +0000348//===----------------------------------------------------------------------===//
349
Chris Lattner5d8925c2006-08-27 22:30:17 +0000350static RegisterPass<PostDominanceFrontier>
Chris Lattner17689df2002-07-30 16:27:52 +0000351H("postdomfrontier", "Post-Dominance Frontier Construction", true);
Chris Lattner93193f82002-01-31 00:42:27 +0000352
Chris Lattner1b7f7dc2002-04-28 16:21:30 +0000353const DominanceFrontier::DomSetType &
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000354PostDominanceFrontier::calculate(const PostDominatorTree &DT,
Chris Lattnerce6ef112002-07-26 18:40:14 +0000355 const DominatorTree::Node *Node) {
Chris Lattner94108ab2001-07-06 16:58:22 +0000356 // Loop over CFG successors to calculate DFlocal[Node]
Chris Lattnerc444a422003-09-11 16:26:13 +0000357 BasicBlock *BB = Node->getBlock();
Chris Lattner94108ab2001-07-06 16:58:22 +0000358 DomSetType &S = Frontiers[BB]; // The new set to fill in...
Chris Lattner706e61e2003-09-10 20:37:08 +0000359 if (getRoots().empty()) return S;
Chris Lattner94108ab2001-07-06 16:58:22 +0000360
Chris Lattner706e61e2003-09-10 20:37:08 +0000361 if (BB)
362 for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB);
363 SI != SE; ++SI)
Misha Brukman2f2d0652003-09-11 18:14:24 +0000364 // Does Node immediately dominate this predecessor?
Chris Lattner706e61e2003-09-10 20:37:08 +0000365 if (DT[*SI]->getIDom() != Node)
366 S.insert(*SI);
Chris Lattner94108ab2001-07-06 16:58:22 +0000367
368 // At this point, S is DFlocal. Now we union in DFup's of our children...
369 // Loop through and visit the nodes that Node immediately dominates (Node's
370 // children in the IDomTree)
371 //
Chris Lattnerce6ef112002-07-26 18:40:14 +0000372 for (PostDominatorTree::Node::const_iterator
373 NI = Node->begin(), NE = Node->end(); NI != NE; ++NI) {
Chris Lattner94108ab2001-07-06 16:58:22 +0000374 DominatorTree::Node *IDominee = *NI;
Chris Lattnerce6ef112002-07-26 18:40:14 +0000375 const DomSetType &ChildDF = calculate(DT, IDominee);
Chris Lattner94108ab2001-07-06 16:58:22 +0000376
377 DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end();
378 for (; CDFI != CDFE; ++CDFI) {
Chris Lattner4b5086c2005-11-18 07:28:26 +0000379 if (!Node->properlyDominates(DT[*CDFI]))
Misha Brukmandedf2bd2005-04-22 04:01:18 +0000380 S.insert(*CDFI);
Chris Lattner94108ab2001-07-06 16:58:22 +0000381 }
382 }
383
384 return S;
385}
Chris Lattnera69fd902002-08-21 23:43:50 +0000386
Reid Spencer4f1bd9e2006-06-07 22:00:26 +0000387// Ensure that this .cpp file gets linked when PostDominators.h is used.
388DEFINING_FILE_FOR(PostDominanceFrontier)