blob: add25fdf42fe2e6bc70d249faa6b831ce5f4c7f8 [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
Owen Anderson3dc67762007-04-15 08:47:27 +000025static RegisterPass<PostDominatorTree>
26F("postdomtree", "Post-Dominator Tree Construction", true);
Nate Begeman442b32b2006-03-11 02:20:46 +000027
Owen Anderson3dc67762007-04-15 08:47:27 +000028unsigned PostDominatorTree::DFSPass(BasicBlock *V, InfoRec &VInfo,
Nate Begeman442b32b2006-03-11 02:20:46 +000029 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
Owen Anderson3dc67762007-04-15 08:47:27 +000076void PostDominatorTree::Compress(BasicBlock *V, InfoRec &VInfo) {
Nate Begeman442b32b2006-03-11 02:20:46 +000077 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
Owen Anderson3dc67762007-04-15 08:47:27 +000092BasicBlock *PostDominatorTree::Eval(BasicBlock *V) {
Nate Begeman442b32b2006-03-11 02:20:46 +000093 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
Owen Anderson3dc67762007-04-15 08:47:27 +0000102void PostDominatorTree::Link(BasicBlock *V, BasicBlock *W,
Nate Begeman442b32b2006-03-11 02:20:46 +0000103 InfoRec &WInfo) {
104 // Higher-complexity but faster implementation
105 WInfo.Ancestor = V;
106}
107
Owen Anderson3dc67762007-04-15 08:47:27 +0000108void PostDominatorTree::calculate(Function &F) {
Nate Begeman442b32b2006-03-11 02:20:46 +0000109 // Step #0: Scan the function looking for the root nodes of the post-dominance
110 // relationships. These blocks, which have no successors, end with return and
111 // unwind instructions.
112 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
113 if (succ_begin(I) == succ_end(I))
114 Roots.push_back(I);
115
116 Vertex.push_back(0);
117
118 // Step #1: Number blocks in depth-first order and initialize variables used
119 // in later stages of the algorithm.
120 unsigned N = 0;
121 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
122 N = DFSPass(Roots[i], Info[Roots[i]], N);
123
124 for (unsigned i = N; i >= 2; --i) {
125 BasicBlock *W = Vertex[i];
126 InfoRec &WInfo = Info[W];
127
128 // Step #2: Calculate the semidominators of all vertices
129 for (succ_iterator SI = succ_begin(W), SE = succ_end(W); SI != SE; ++SI)
130 if (Info.count(*SI)) { // Only if this predecessor is reachable!
131 unsigned SemiU = Info[Eval(*SI)].Semi;
132 if (SemiU < WInfo.Semi)
133 WInfo.Semi = SemiU;
134 }
135
136 Info[Vertex[WInfo.Semi]].Bucket.push_back(W);
137
138 BasicBlock *WParent = WInfo.Parent;
139 Link(WParent, W, WInfo);
140
141 // Step #3: Implicitly define the immediate dominator of vertices
142 std::vector<BasicBlock*> &WParentBucket = Info[WParent].Bucket;
143 while (!WParentBucket.empty()) {
144 BasicBlock *V = WParentBucket.back();
145 WParentBucket.pop_back();
146 BasicBlock *U = Eval(V);
147 IDoms[V] = Info[U].Semi < Info[V].Semi ? U : WParent;
148 }
149 }
150
151 // Step #4: Explicitly define the immediate dominator of each vertex
152 for (unsigned i = 2; i <= N; ++i) {
153 BasicBlock *W = Vertex[i];
154 BasicBlock *&WIDom = IDoms[W];
155 if (WIDom != Vertex[Info[W].Semi])
156 WIDom = IDoms[WIDom];
157 }
158
Chris Lattner706e61e2003-09-10 20:37:08 +0000159 if (Roots.empty()) return;
Nate Begeman442b32b2006-03-11 02:20:46 +0000160
161 // Add a node for the root. This node might be the actual root, if there is
162 // one exit block, or it may be the virtual exit (denoted by (BasicBlock *)0)
163 // which postdominates all real exits if there are multiple exit blocks.
Chris Lattner706e61e2003-09-10 20:37:08 +0000164 BasicBlock *Root = Roots.size() == 1 ? Roots[0] : 0;
Nate Begeman442b32b2006-03-11 02:20:46 +0000165 Nodes[Root] = RootNode = new Node(Root, 0);
166
Nate Begeman442b32b2006-03-11 02:20:46 +0000167 // Loop over all of the reachable blocks in the function...
Owen Anderson3dc67762007-04-15 08:47:27 +0000168 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
169 if (BasicBlock *ImmPostDom = getIDom(I)) { // Reachable block.
Nate Begeman442b32b2006-03-11 02:20:46 +0000170 Node *&BBNode = Nodes[I];
171 if (!BBNode) { // Haven't calculated this node yet?
172 // Get or calculate the node for the immediate dominator
173 Node *IPDomNode = getNodeForBlock(ImmPostDom);
174
175 // Add a new tree node for this BasicBlock, and link it as a child of
176 // IDomNode
177 BBNode = IPDomNode->addChild(new Node(I, IPDomNode));
Chris Lattner17152292001-07-02 05:46:38 +0000178 }
179 }
Owen Anderson3dc67762007-04-15 08:47:27 +0000180
181 // Free temporary memory used to construct idom's
182 IDoms.clear();
183 Info.clear();
184 std::vector<BasicBlock*>().swap(Vertex);
185}
186
187
188DominatorTreeBase::Node *PostDominatorTree::getNodeForBlock(BasicBlock *BB) {
189 Node *&BBNode = Nodes[BB];
190 if (BBNode) return BBNode;
191
192 // Haven't calculated this node yet? Get or calculate the node for the
193 // immediate postdominator.
194 BasicBlock *IPDom = getIDom(BB);
195 Node *IPDomNode = getNodeForBlock(IPDom);
196
197 // Add a new tree node for this BasicBlock, and link it as a child of
198 // IDomNode
199 return BBNode = IPDomNode->addChild(new Node(BB, IPDomNode));
Chris Lattner17152292001-07-02 05:46:38 +0000200}
Nate Begeman442b32b2006-03-11 02:20:46 +0000201
Chris Lattnerccacd3c2006-01-08 08:22:18 +0000202//===----------------------------------------------------------------------===//
203// PostETForest Implementation
204//===----------------------------------------------------------------------===//
205
Chris Lattner5d8925c2006-08-27 22:30:17 +0000206static RegisterPass<PostETForest>
Chris Lattnerccacd3c2006-01-08 08:22:18 +0000207G("postetforest", "Post-ET-Forest Construction", true);
208
209ETNode *PostETForest::getNodeForBlock(BasicBlock *BB) {
210 ETNode *&BBNode = Nodes[BB];
211 if (BBNode) return BBNode;
212
213 // Haven't calculated this node yet? Get or calculate the node for the
214 // immediate dominator.
Owen Anderson3dc67762007-04-15 08:47:27 +0000215 PostDominatorTree::Node *node = getAnalysis<PostDominatorTree>().getNode(BB);
Chris Lattnerccacd3c2006-01-08 08:22:18 +0000216
217 // If we are unreachable, we may not have an immediate dominator.
Owen Anderson3dc67762007-04-15 08:47:27 +0000218 if (!node)
Owen Andersone934fef2007-04-15 23:14:18 +0000219 return 0;
220 else if (!node->getIDom())
Chris Lattnerccacd3c2006-01-08 08:22:18 +0000221 return BBNode = new ETNode(BB);
222 else {
Owen Anderson3dc67762007-04-15 08:47:27 +0000223 ETNode *IDomNode = getNodeForBlock(node->getIDom()->getBlock());
Chris Lattnerccacd3c2006-01-08 08:22:18 +0000224
225 // Add a new tree node for this BasicBlock, and link it as a child of
226 // IDomNode
227 BBNode = new ETNode(BB);
228 BBNode->setFather(IDomNode);
229 return BBNode;
230 }
231}
232
Owen Anderson3dc67762007-04-15 08:47:27 +0000233void PostETForest::calculate(const PostDominatorTree &DT) {
Chris Lattnerccacd3c2006-01-08 08:22:18 +0000234 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
235 Nodes[Roots[i]] = new ETNode(Roots[i]); // Add a node for the root
236
237 // Iterate over all nodes in inverse depth first order.
238 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
239 for (idf_iterator<BasicBlock*> I = idf_begin(Roots[i]),
240 E = idf_end(Roots[i]); I != E; ++I) {
241 BasicBlock *BB = *I;
242 ETNode *&BBNode = Nodes[BB];
243 if (!BBNode) {
244 ETNode *IDomNode = NULL;
Owen Andersone934fef2007-04-15 23:14:18 +0000245 PostDominatorTree::Node *node = DT.getNode(BB);
Owen Anderson3dc67762007-04-15 08:47:27 +0000246 if (node && node->getIDom())
247 IDomNode = getNodeForBlock(node->getIDom()->getBlock());
Chris Lattnerccacd3c2006-01-08 08:22:18 +0000248
249 // Add a new ETNode for this BasicBlock, and set it's parent
250 // to it's immediate dominator.
251 BBNode = new ETNode(BB);
252 if (IDomNode)
253 BBNode->setFather(IDomNode);
254 }
255 }
256
257 int dfsnum = 0;
258 // Iterate over all nodes in depth first order...
259 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
260 for (idf_iterator<BasicBlock*> I = idf_begin(Roots[i]),
261 E = idf_end(Roots[i]); I != E; ++I) {
262 if (!getNodeForBlock(*I)->hasFather())
263 getNodeForBlock(*I)->assignDFSNumber(dfsnum);
264 }
265 DFSInfoValid = true;
266}
Chris Lattner17152292001-07-02 05:46:38 +0000267
Chris Lattner17152292001-07-02 05:46:38 +0000268//===----------------------------------------------------------------------===//
Chris Lattner4c9df7c2002-08-02 16:43:03 +0000269// PostDominanceFrontier Implementation
Chris Lattner17152292001-07-02 05:46:38 +0000270//===----------------------------------------------------------------------===//
271
Chris Lattner5d8925c2006-08-27 22:30:17 +0000272static RegisterPass<PostDominanceFrontier>
Chris Lattner17689df2002-07-30 16:27:52 +0000273H("postdomfrontier", "Post-Dominance Frontier Construction", true);
Chris Lattner93193f82002-01-31 00:42:27 +0000274
Chris Lattner1b7f7dc2002-04-28 16:21:30 +0000275const DominanceFrontier::DomSetType &
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000276PostDominanceFrontier::calculate(const PostDominatorTree &DT,
Chris Lattnerce6ef112002-07-26 18:40:14 +0000277 const DominatorTree::Node *Node) {
Chris Lattner94108ab2001-07-06 16:58:22 +0000278 // Loop over CFG successors to calculate DFlocal[Node]
Chris Lattnerc444a422003-09-11 16:26:13 +0000279 BasicBlock *BB = Node->getBlock();
Chris Lattner94108ab2001-07-06 16:58:22 +0000280 DomSetType &S = Frontiers[BB]; // The new set to fill in...
Chris Lattner706e61e2003-09-10 20:37:08 +0000281 if (getRoots().empty()) return S;
Chris Lattner94108ab2001-07-06 16:58:22 +0000282
Chris Lattner706e61e2003-09-10 20:37:08 +0000283 if (BB)
284 for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB);
Devang Patel5a713cc2007-04-18 01:19:55 +0000285 SI != SE; ++SI) {
Misha Brukman2f2d0652003-09-11 18:14:24 +0000286 // Does Node immediately dominate this predecessor?
Devang Patel5a713cc2007-04-18 01:19:55 +0000287 DominatorTree::Node *SINode = DT[*SI];
288 if (SINode && SINode->getIDom() != Node)
Chris Lattner706e61e2003-09-10 20:37:08 +0000289 S.insert(*SI);
Devang Patel5a713cc2007-04-18 01:19:55 +0000290 }
Chris Lattner94108ab2001-07-06 16:58:22 +0000291
292 // At this point, S is DFlocal. Now we union in DFup's of our children...
293 // Loop through and visit the nodes that Node immediately dominates (Node's
294 // children in the IDomTree)
295 //
Chris Lattnerce6ef112002-07-26 18:40:14 +0000296 for (PostDominatorTree::Node::const_iterator
297 NI = Node->begin(), NE = Node->end(); NI != NE; ++NI) {
Chris Lattner94108ab2001-07-06 16:58:22 +0000298 DominatorTree::Node *IDominee = *NI;
Chris Lattnerce6ef112002-07-26 18:40:14 +0000299 const DomSetType &ChildDF = calculate(DT, IDominee);
Chris Lattner94108ab2001-07-06 16:58:22 +0000300
301 DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end();
302 for (; CDFI != CDFE; ++CDFI) {
Chris Lattner4b5086c2005-11-18 07:28:26 +0000303 if (!Node->properlyDominates(DT[*CDFI]))
Misha Brukmandedf2bd2005-04-22 04:01:18 +0000304 S.insert(*CDFI);
Chris Lattner94108ab2001-07-06 16:58:22 +0000305 }
306 }
307
308 return S;
309}
Chris Lattnera69fd902002-08-21 23:43:50 +0000310
Reid Spencer4f1bd9e2006-06-07 22:00:26 +0000311// Ensure that this .cpp file gets linked when PostDominators.h is used.
312DEFINING_FILE_FOR(PostDominanceFrontier)