blob: 68424400dbfb5f4bfddb711e25141e82f934780b [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//===----------------------------------------------------------------------===//
Owen Anderson3dc67762007-04-15 08:47:27 +000022// PostDominatorTree Implementation
Nate Begeman442b32b2006-03-11 02:20:46 +000023//===----------------------------------------------------------------------===//
24
Devang Patel19974732007-05-03 01:11:54 +000025char PostDominatorTree::ID = 0;
26char PostDominanceFrontier::ID = 0;
27char PostETForest::ID = 0;
Owen Anderson3dc67762007-04-15 08:47:27 +000028static RegisterPass<PostDominatorTree>
29F("postdomtree", "Post-Dominator Tree Construction", true);
Nate Begeman442b32b2006-03-11 02:20:46 +000030
Owen Anderson3dc67762007-04-15 08:47:27 +000031unsigned PostDominatorTree::DFSPass(BasicBlock *V, InfoRec &VInfo,
Nate Begeman442b32b2006-03-11 02:20:46 +000032 unsigned N) {
Devang Patelc8719e92006-09-07 23:22:37 +000033 std::vector<std::pair<BasicBlock *, InfoRec *> > workStack;
Devang Patel79db5b72006-09-27 17:18:05 +000034 std::set<BasicBlock *> visited;
Devang Patelc8719e92006-09-07 23:22:37 +000035 workStack.push_back(std::make_pair(V, &VInfo));
36
37 do {
38 BasicBlock *currentBB = workStack.back().first;
39 InfoRec *currentVInfo = workStack.back().second;
Devang Patelc8719e92006-09-07 23:22:37 +000040
Devang Patel79db5b72006-09-27 17:18:05 +000041 // Visit each block only once.
42 if (visited.count(currentBB) == 0) {
Devang Patelc8719e92006-09-07 23:22:37 +000043
Devang Patel79db5b72006-09-27 17:18:05 +000044 visited.insert(currentBB);
45 currentVInfo->Semi = ++N;
46 currentVInfo->Label = currentBB;
47
48 Vertex.push_back(currentBB); // Vertex[n] = current;
49 // Info[currentBB].Ancestor = 0;
50 // Ancestor[n] = 0
51 // Child[currentBB] = 0;
52 currentVInfo->Size = 1; // Size[currentBB] = 1
53 }
Devang Patelc8719e92006-09-07 23:22:37 +000054
Devang Patel79db5b72006-09-27 17:18:05 +000055 // Visit children
56 bool visitChild = false;
Devang Patelc8719e92006-09-07 23:22:37 +000057 for (pred_iterator PI = pred_begin(currentBB), PE = pred_end(currentBB);
Devang Patel79db5b72006-09-27 17:18:05 +000058 PI != PE && !visitChild; ++PI) {
Devang Patelc8719e92006-09-07 23:22:37 +000059 InfoRec &SuccVInfo = Info[*PI];
60 if (SuccVInfo.Semi == 0) {
Devang Patelf93f6832006-09-07 23:29:19 +000061 SuccVInfo.Parent = currentBB;
Devang Patel79db5b72006-09-27 17:18:05 +000062 if (visited.count (*PI) == 0) {
63 workStack.push_back(std::make_pair(*PI, &SuccVInfo));
64 visitChild = true;
65 }
Devang Patelc8719e92006-09-07 23:22:37 +000066 }
Nate Begeman442b32b2006-03-11 02:20:46 +000067 }
Devang Patel79db5b72006-09-27 17:18:05 +000068
69 // If all children are visited or if this block has no child then pop this
70 // block out of workStack.
71 if (!visitChild)
72 workStack.pop_back();
73
Devang Patelc8719e92006-09-07 23:22:37 +000074 } while (!workStack.empty());
Devang Patel79db5b72006-09-27 17:18:05 +000075
Nate Begeman442b32b2006-03-11 02:20:46 +000076 return N;
77}
78
Owen Anderson3dc67762007-04-15 08:47:27 +000079void PostDominatorTree::Compress(BasicBlock *V, InfoRec &VInfo) {
Nate Begeman442b32b2006-03-11 02:20:46 +000080 BasicBlock *VAncestor = VInfo.Ancestor;
81 InfoRec &VAInfo = Info[VAncestor];
82 if (VAInfo.Ancestor == 0)
83 return;
84
85 Compress(VAncestor, VAInfo);
86
87 BasicBlock *VAncestorLabel = VAInfo.Label;
88 BasicBlock *VLabel = VInfo.Label;
89 if (Info[VAncestorLabel].Semi < Info[VLabel].Semi)
90 VInfo.Label = VAncestorLabel;
91
92 VInfo.Ancestor = VAInfo.Ancestor;
93}
94
Owen Anderson3dc67762007-04-15 08:47:27 +000095BasicBlock *PostDominatorTree::Eval(BasicBlock *V) {
Nate Begeman442b32b2006-03-11 02:20:46 +000096 InfoRec &VInfo = Info[V];
97
98 // Higher-complexity but faster implementation
99 if (VInfo.Ancestor == 0)
100 return V;
101 Compress(V, VInfo);
102 return VInfo.Label;
103}
104
Owen Anderson3dc67762007-04-15 08:47:27 +0000105void PostDominatorTree::Link(BasicBlock *V, BasicBlock *W,
Nate Begeman442b32b2006-03-11 02:20:46 +0000106 InfoRec &WInfo) {
107 // Higher-complexity but faster implementation
108 WInfo.Ancestor = V;
109}
110
Owen Anderson3dc67762007-04-15 08:47:27 +0000111void PostDominatorTree::calculate(Function &F) {
Nate Begeman442b32b2006-03-11 02:20:46 +0000112 // 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
Chris Lattner706e61e2003-09-10 20:37:08 +0000162 if (Roots.empty()) return;
Nate Begeman442b32b2006-03-11 02:20:46 +0000163
164 // Add a node for the root. This node might be the actual root, if there is
165 // one exit block, or it may be the virtual exit (denoted by (BasicBlock *)0)
166 // which postdominates all real exits if there are multiple exit blocks.
Chris Lattner706e61e2003-09-10 20:37:08 +0000167 BasicBlock *Root = Roots.size() == 1 ? Roots[0] : 0;
Nate Begeman442b32b2006-03-11 02:20:46 +0000168 Nodes[Root] = RootNode = new Node(Root, 0);
169
Nate Begeman442b32b2006-03-11 02:20:46 +0000170 // Loop over all of the reachable blocks in the function...
Owen Anderson3dc67762007-04-15 08:47:27 +0000171 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
172 if (BasicBlock *ImmPostDom = getIDom(I)) { // Reachable block.
Nate Begeman442b32b2006-03-11 02:20:46 +0000173 Node *&BBNode = Nodes[I];
174 if (!BBNode) { // Haven't calculated this node yet?
175 // Get or calculate the node for the immediate dominator
176 Node *IPDomNode = getNodeForBlock(ImmPostDom);
177
178 // Add a new tree node for this BasicBlock, and link it as a child of
179 // IDomNode
180 BBNode = IPDomNode->addChild(new Node(I, IPDomNode));
Chris Lattner17152292001-07-02 05:46:38 +0000181 }
182 }
Owen Anderson3dc67762007-04-15 08:47:27 +0000183
184 // Free temporary memory used to construct idom's
185 IDoms.clear();
186 Info.clear();
187 std::vector<BasicBlock*>().swap(Vertex);
188}
189
190
191DominatorTreeBase::Node *PostDominatorTree::getNodeForBlock(BasicBlock *BB) {
192 Node *&BBNode = Nodes[BB];
193 if (BBNode) return BBNode;
194
195 // Haven't calculated this node yet? Get or calculate the node for the
196 // immediate postdominator.
197 BasicBlock *IPDom = getIDom(BB);
198 Node *IPDomNode = getNodeForBlock(IPDom);
199
200 // Add a new tree node for this BasicBlock, and link it as a child of
201 // IDomNode
202 return BBNode = IPDomNode->addChild(new Node(BB, IPDomNode));
Chris Lattner17152292001-07-02 05:46:38 +0000203}
Nate Begeman442b32b2006-03-11 02:20:46 +0000204
Chris Lattnerccacd3c2006-01-08 08:22:18 +0000205//===----------------------------------------------------------------------===//
206// PostETForest Implementation
207//===----------------------------------------------------------------------===//
208
Chris Lattner5d8925c2006-08-27 22:30:17 +0000209static RegisterPass<PostETForest>
Chris Lattnerccacd3c2006-01-08 08:22:18 +0000210G("postetforest", "Post-ET-Forest Construction", true);
211
212ETNode *PostETForest::getNodeForBlock(BasicBlock *BB) {
213 ETNode *&BBNode = Nodes[BB];
214 if (BBNode) return BBNode;
215
216 // Haven't calculated this node yet? Get or calculate the node for the
217 // immediate dominator.
Owen Anderson3dc67762007-04-15 08:47:27 +0000218 PostDominatorTree::Node *node = getAnalysis<PostDominatorTree>().getNode(BB);
Chris Lattnerccacd3c2006-01-08 08:22:18 +0000219
220 // If we are unreachable, we may not have an immediate dominator.
Owen Anderson3dc67762007-04-15 08:47:27 +0000221 if (!node)
Owen Andersone934fef2007-04-15 23:14:18 +0000222 return 0;
223 else if (!node->getIDom())
Chris Lattnerccacd3c2006-01-08 08:22:18 +0000224 return BBNode = new ETNode(BB);
225 else {
Owen Anderson3dc67762007-04-15 08:47:27 +0000226 ETNode *IDomNode = getNodeForBlock(node->getIDom()->getBlock());
Chris Lattnerccacd3c2006-01-08 08:22:18 +0000227
228 // Add a new tree node for this BasicBlock, and link it as a child of
229 // IDomNode
230 BBNode = new ETNode(BB);
231 BBNode->setFather(IDomNode);
232 return BBNode;
233 }
234}
235
Owen Anderson3dc67762007-04-15 08:47:27 +0000236void PostETForest::calculate(const PostDominatorTree &DT) {
Chris Lattnerccacd3c2006-01-08 08:22:18 +0000237 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
238 Nodes[Roots[i]] = new ETNode(Roots[i]); // Add a node for the root
239
240 // Iterate over all nodes in inverse depth first order.
241 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
242 for (idf_iterator<BasicBlock*> I = idf_begin(Roots[i]),
243 E = idf_end(Roots[i]); I != E; ++I) {
244 BasicBlock *BB = *I;
245 ETNode *&BBNode = Nodes[BB];
246 if (!BBNode) {
247 ETNode *IDomNode = NULL;
Owen Andersone934fef2007-04-15 23:14:18 +0000248 PostDominatorTree::Node *node = DT.getNode(BB);
Owen Anderson3dc67762007-04-15 08:47:27 +0000249 if (node && node->getIDom())
250 IDomNode = getNodeForBlock(node->getIDom()->getBlock());
Chris Lattnerccacd3c2006-01-08 08:22:18 +0000251
252 // Add a new ETNode for this BasicBlock, and set it's parent
253 // to it's immediate dominator.
254 BBNode = new ETNode(BB);
255 if (IDomNode)
256 BBNode->setFather(IDomNode);
257 }
258 }
259
260 int dfsnum = 0;
261 // Iterate over all nodes in depth first order...
262 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
263 for (idf_iterator<BasicBlock*> I = idf_begin(Roots[i]),
264 E = idf_end(Roots[i]); I != E; ++I) {
265 if (!getNodeForBlock(*I)->hasFather())
266 getNodeForBlock(*I)->assignDFSNumber(dfsnum);
267 }
268 DFSInfoValid = true;
269}
Chris Lattner17152292001-07-02 05:46:38 +0000270
Chris Lattner17152292001-07-02 05:46:38 +0000271//===----------------------------------------------------------------------===//
Chris Lattner4c9df7c2002-08-02 16:43:03 +0000272// PostDominanceFrontier Implementation
Chris Lattner17152292001-07-02 05:46:38 +0000273//===----------------------------------------------------------------------===//
274
Chris Lattner5d8925c2006-08-27 22:30:17 +0000275static RegisterPass<PostDominanceFrontier>
Chris Lattner17689df2002-07-30 16:27:52 +0000276H("postdomfrontier", "Post-Dominance Frontier Construction", true);
Chris Lattner93193f82002-01-31 00:42:27 +0000277
Chris Lattner1b7f7dc2002-04-28 16:21:30 +0000278const DominanceFrontier::DomSetType &
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000279PostDominanceFrontier::calculate(const PostDominatorTree &DT,
Chris Lattnerce6ef112002-07-26 18:40:14 +0000280 const DominatorTree::Node *Node) {
Chris Lattner94108ab2001-07-06 16:58:22 +0000281 // Loop over CFG successors to calculate DFlocal[Node]
Chris Lattnerc444a422003-09-11 16:26:13 +0000282 BasicBlock *BB = Node->getBlock();
Chris Lattner94108ab2001-07-06 16:58:22 +0000283 DomSetType &S = Frontiers[BB]; // The new set to fill in...
Chris Lattner706e61e2003-09-10 20:37:08 +0000284 if (getRoots().empty()) return S;
Chris Lattner94108ab2001-07-06 16:58:22 +0000285
Chris Lattner706e61e2003-09-10 20:37:08 +0000286 if (BB)
287 for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB);
Devang Patel5a713cc2007-04-18 01:19:55 +0000288 SI != SE; ++SI) {
Misha Brukman2f2d0652003-09-11 18:14:24 +0000289 // Does Node immediately dominate this predecessor?
Devang Patel5a713cc2007-04-18 01:19:55 +0000290 DominatorTree::Node *SINode = DT[*SI];
291 if (SINode && SINode->getIDom() != Node)
Chris Lattner706e61e2003-09-10 20:37:08 +0000292 S.insert(*SI);
Devang Patel5a713cc2007-04-18 01:19:55 +0000293 }
Chris Lattner94108ab2001-07-06 16:58:22 +0000294
295 // At this point, S is DFlocal. Now we union in DFup's of our children...
296 // Loop through and visit the nodes that Node immediately dominates (Node's
297 // children in the IDomTree)
298 //
Chris Lattnerce6ef112002-07-26 18:40:14 +0000299 for (PostDominatorTree::Node::const_iterator
300 NI = Node->begin(), NE = Node->end(); NI != NE; ++NI) {
Chris Lattner94108ab2001-07-06 16:58:22 +0000301 DominatorTree::Node *IDominee = *NI;
Chris Lattnerce6ef112002-07-26 18:40:14 +0000302 const DomSetType &ChildDF = calculate(DT, IDominee);
Chris Lattner94108ab2001-07-06 16:58:22 +0000303
304 DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end();
305 for (; CDFI != CDFE; ++CDFI) {
Chris Lattner4b5086c2005-11-18 07:28:26 +0000306 if (!Node->properlyDominates(DT[*CDFI]))
Misha Brukmandedf2bd2005-04-22 04:01:18 +0000307 S.insert(*CDFI);
Chris Lattner94108ab2001-07-06 16:58:22 +0000308 }
309 }
310
311 return S;
312}
Chris Lattnera69fd902002-08-21 23:43:50 +0000313
Reid Spencer4f1bd9e2006-06-07 22:00:26 +0000314// Ensure that this .cpp file gets linked when PostDominators.h is used.
315DEFINING_FILE_FOR(PostDominanceFrontier)