blob: f9f9a42acc5578bb179c42c6236810585be103ac [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//===----------------------------------------------------------------------===//
Chris Lattner4c9df7c2002-08-02 16:43:03 +000022// PostDominatorSet Implementation
Chris Lattner17152292001-07-02 05:46:38 +000023//===----------------------------------------------------------------------===//
24
Chris Lattner1e435162002-07-26 21:12:44 +000025static RegisterAnalysis<PostDominatorSet>
Chris Lattner17689df2002-07-30 16:27:52 +000026B("postdomset", "Post-Dominator Set Construction", true);
Chris Lattner94108ab2001-07-06 16:58:22 +000027
Chris Lattnerce6ef112002-07-26 18:40:14 +000028// Postdominator set construction. This converts the specified function to only
29// have a single exit node (return stmt), then calculates the post dominance
30// sets for the function.
Chris Lattner94108ab2001-07-06 16:58:22 +000031//
Chris Lattnerce6ef112002-07-26 18:40:14 +000032bool PostDominatorSet::runOnFunction(Function &F) {
33 Doms.clear(); // Reset from the last time we were run...
Chris Lattner94108ab2001-07-06 16:58:22 +000034
Chris Lattner706e61e2003-09-10 20:37:08 +000035 // Scan the function looking for the root nodes of the post-dominance
36 // relationships. These blocks end with return and unwind instructions.
37 // While we are iterating over the function, we also initialize all of the
38 // domsets to empty.
39 Roots.clear();
40 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
41 Doms[I]; // Initialize to empty
42
Chris Lattnerec7c1ab2004-10-16 18:21:33 +000043 if (succ_begin(I) == succ_end(I))
Chris Lattner706e61e2003-09-10 20:37:08 +000044 Roots.push_back(I);
Chris Lattner384e5b12001-08-23 17:07:19 +000045 }
Chris Lattner94108ab2001-07-06 16:58:22 +000046
Chris Lattner706e61e2003-09-10 20:37:08 +000047 // If there are no exit nodes for the function, postdomsets are all empty.
48 // This can happen if the function just contains an infinite loop, for
49 // example.
50 if (Roots.empty()) return false;
51
52 // If we have more than one root, we insert an artificial "null" exit, which
53 // has "virtual edges" to each of the real exit nodes.
54 if (Roots.size() > 1)
55 Doms[0].insert(0);
56
Chris Lattner94108ab2001-07-06 16:58:22 +000057 bool Changed;
58 do {
59 Changed = false;
60
Chris Lattner50b5d712003-10-13 16:36:06 +000061 std::set<BasicBlock*> Visited;
Chris Lattner94108ab2001-07-06 16:58:22 +000062 DomSetType WorkingSet;
Chris Lattner94108ab2001-07-06 16:58:22 +000063
Chris Lattner706e61e2003-09-10 20:37:08 +000064 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
Chris Lattner50b5d712003-10-13 16:36:06 +000065 for (idf_ext_iterator<BasicBlock*> It = idf_ext_begin(Roots[i], Visited),
66 E = idf_ext_end(Roots[i], Visited); It != E; ++It) {
Chris Lattner706e61e2003-09-10 20:37:08 +000067 BasicBlock *BB = *It;
68 succ_iterator SI = succ_begin(BB), SE = succ_end(BB);
69 if (SI != SE) { // Is there SOME successor?
70 // Loop until we get to a successor that has had it's dom set filled
71 // in at least once. We are guaranteed to have this because we are
72 // traversing the graph in DFO and have handled start nodes specially.
73 //
74 while (Doms[*SI].size() == 0) ++SI;
75 WorkingSet = Doms[*SI];
Misha Brukman2b37d7c2005-04-21 21:13:18 +000076
Chris Lattner706e61e2003-09-10 20:37:08 +000077 for (++SI; SI != SE; ++SI) { // Intersect all of the successor sets
78 DomSetType &SuccSet = Doms[*SI];
79 if (SuccSet.size())
80 set_intersect(WorkingSet, SuccSet);
81 }
82 } else {
83 // If this node has no successors, it must be one of the root nodes.
84 // We will already take care of the notion that the node
85 // post-dominates itself. The only thing we have to add is that if
86 // there are multiple root nodes, we want to insert a special "null"
87 // exit node which dominates the roots as well.
88 if (Roots.size() > 1)
89 WorkingSet.insert(0);
90 }
Misha Brukmandedf2bd2005-04-22 04:01:18 +000091
Chris Lattner706e61e2003-09-10 20:37:08 +000092 WorkingSet.insert(BB); // A block always dominates itself
93 DomSetType &BBSet = Doms[BB];
94 if (BBSet != WorkingSet) {
95 BBSet.swap(WorkingSet); // Constant time operation!
96 Changed = true; // The sets changed.
97 }
98 WorkingSet.clear(); // Clear out the set for next iteration
Chris Lattner94108ab2001-07-06 16:58:22 +000099 }
Chris Lattner94108ab2001-07-06 16:58:22 +0000100 } while (Changed);
Chris Lattnerce6ef112002-07-26 18:40:14 +0000101 return false;
Chris Lattner17152292001-07-02 05:46:38 +0000102}
103
Chris Lattner17152292001-07-02 05:46:38 +0000104//===----------------------------------------------------------------------===//
Chris Lattner4c9df7c2002-08-02 16:43:03 +0000105// ImmediatePostDominators Implementation
Chris Lattner17152292001-07-02 05:46:38 +0000106//===----------------------------------------------------------------------===//
107
Chris Lattner1e435162002-07-26 21:12:44 +0000108static RegisterAnalysis<ImmediatePostDominators>
Chris Lattner17689df2002-07-30 16:27:52 +0000109D("postidom", "Immediate Post-Dominators Construction", true);
Chris Lattner93193f82002-01-31 00:42:27 +0000110
Chris Lattnercd7c2872003-12-07 00:35:42 +0000111
112// calcIDoms - Calculate the immediate dominator mapping, given a set of
113// dominators for every basic block.
114void ImmediatePostDominators::calcIDoms(const DominatorSetBase &DS) {
115 // Loop over all of the nodes that have dominators... figuring out the IDOM
116 // for each node...
117 //
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000118 for (DominatorSet::const_iterator DI = DS.begin(), DEnd = DS.end();
Chris Lattnercd7c2872003-12-07 00:35:42 +0000119 DI != DEnd; ++DI) {
120 BasicBlock *BB = DI->first;
121 const DominatorSet::DomSetType &Dominators = DI->second;
122 unsigned DomSetSize = Dominators.size();
123 if (DomSetSize == 1) continue; // Root node... IDom = null
124
125 // Loop over all dominators of this node. This corresponds to looping over
126 // nodes in the dominator chain, looking for a node whose dominator set is
127 // equal to the current nodes, except that the current node does not exist
128 // in it. This means that it is one level higher in the dom chain than the
129 // current node, and it is our idom!
130 //
131 DominatorSet::DomSetType::const_iterator I = Dominators.begin();
132 DominatorSet::DomSetType::const_iterator End = Dominators.end();
133 for (; I != End; ++I) { // Iterate over dominators...
134 // All of our dominators should form a chain, where the number of elements
135 // in the dominator set indicates what level the node is at in the chain.
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000136 // We want the node immediately above us, so it will have an identical
Chris Lattnercd7c2872003-12-07 00:35:42 +0000137 // dominator set, except that BB will not dominate it... therefore it's
138 // dominator set size will be one less than BB's...
139 //
140 if (DS.getDominators(*I).size() == DomSetSize - 1) {
Misha Brukmandedf2bd2005-04-22 04:01:18 +0000141 IDoms[BB] = *I;
142 break;
Chris Lattnercd7c2872003-12-07 00:35:42 +0000143 }
144 }
145 }
146}
147
Chris Lattner17152292001-07-02 05:46:38 +0000148//===----------------------------------------------------------------------===//
Chris Lattner4c9df7c2002-08-02 16:43:03 +0000149// PostDominatorTree Implementation
Chris Lattner17152292001-07-02 05:46:38 +0000150//===----------------------------------------------------------------------===//
151
Chris Lattner1e435162002-07-26 21:12:44 +0000152static RegisterAnalysis<PostDominatorTree>
Chris Lattner17689df2002-07-30 16:27:52 +0000153F("postdomtree", "Post-Dominator Tree Construction", true);
Chris Lattner93193f82002-01-31 00:42:27 +0000154
Chris Lattnerce6ef112002-07-26 18:40:14 +0000155void PostDominatorTree::calculate(const PostDominatorSet &DS) {
Chris Lattner706e61e2003-09-10 20:37:08 +0000156 if (Roots.empty()) return;
157 BasicBlock *Root = Roots.size() == 1 ? Roots[0] : 0;
Chris Lattnerce6ef112002-07-26 18:40:14 +0000158
Chris Lattner706e61e2003-09-10 20:37:08 +0000159 Nodes[Root] = RootNode = new Node(Root, 0); // Add a node for the root...
160
161 // Iterate over all nodes in depth first order...
162 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
163 for (idf_iterator<BasicBlock*> I = idf_begin(Roots[i]),
164 E = idf_end(Roots[i]); I != E; ++I) {
Chris Lattnera298d272002-04-28 00:15:57 +0000165 BasicBlock *BB = *I;
Chris Lattner94108ab2001-07-06 16:58:22 +0000166 const DominatorSet::DomSetType &Dominators = DS.getDominators(BB);
167 unsigned DomSetSize = Dominators.size();
168 if (DomSetSize == 1) continue; // Root node... IDom = null
Chris Lattner706e61e2003-09-10 20:37:08 +0000169
170 // If we have already computed the immediate dominator for this node,
171 // don't revisit. This can happen due to nodes reachable from multiple
172 // roots, but which the idf_iterator doesn't know about.
173 if (Nodes.find(BB) != Nodes.end()) continue;
174
Chris Lattner3ff43872001-09-28 22:56:31 +0000175 // Loop over all dominators of this node. This corresponds to looping
176 // over nodes in the dominator chain, looking for a node whose dominator
177 // set is equal to the current nodes, except that the current node does
178 // not exist in it. This means that it is one level higher in the dom
179 // chain than the current node, and it is our idom! We know that we have
180 // already added a DominatorTree node for our idom, because the idom must
181 // be a predecessor in the depth first order that we are iterating through
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000182 // the function.
Chris Lattner94108ab2001-07-06 16:58:22 +0000183 //
Chris Lattner979c38b2004-10-14 14:59:16 +0000184 for (DominatorSet::DomSetType::const_iterator I = Dominators.begin(),
185 E = Dominators.end(); I != E; ++I) { // Iterate over dominators.
Chris Lattner706e61e2003-09-10 20:37:08 +0000186 // All of our dominators should form a chain, where the number
187 // of elements in the dominator set indicates what level the
188 // node is at in the chain. We want the node immediately
189 // above us, so it will have an identical dominator set,
190 // except that BB will not dominate it... therefore it's
191 // dominator set size will be one less than BB's...
192 //
193 if (DS.getDominators(*I).size() == DomSetSize - 1) {
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000194 // We know that the immediate dominator should already have a node,
Chris Lattner706e61e2003-09-10 20:37:08 +0000195 // because we are traversing the CFG in depth first order!
196 //
197 Node *IDomNode = Nodes[*I];
198 assert(IDomNode && "No node for IDOM?");
Misha Brukmandedf2bd2005-04-22 04:01:18 +0000199
Chris Lattner706e61e2003-09-10 20:37:08 +0000200 // Add a new tree node for this BasicBlock, and link it as a child of
201 // IDomNode
202 Nodes[BB] = IDomNode->addChild(new Node(BB, IDomNode));
203 break;
204 }
Chris Lattner17152292001-07-02 05:46:38 +0000205 }
206 }
Chris Lattner17152292001-07-02 05:46:38 +0000207}
Chris Lattnerccacd3c2006-01-08 08:22:18 +0000208//===----------------------------------------------------------------------===//
209// PostETForest Implementation
210//===----------------------------------------------------------------------===//
211
212static RegisterAnalysis<PostETForest>
213G("postetforest", "Post-ET-Forest Construction", true);
214
215ETNode *PostETForest::getNodeForBlock(BasicBlock *BB) {
216 ETNode *&BBNode = Nodes[BB];
217 if (BBNode) return BBNode;
218
219 // Haven't calculated this node yet? Get or calculate the node for the
220 // immediate dominator.
221 BasicBlock *IDom = getAnalysis<ImmediatePostDominators>()[BB];
222
223 // If we are unreachable, we may not have an immediate dominator.
224 if (!IDom)
225 return BBNode = new ETNode(BB);
226 else {
227 ETNode *IDomNode = getNodeForBlock(IDom);
228
229 // Add a new tree node for this BasicBlock, and link it as a child of
230 // IDomNode
231 BBNode = new ETNode(BB);
232 BBNode->setFather(IDomNode);
233 return BBNode;
234 }
235}
236
237void PostETForest::calculate(const ImmediatePostDominators &ID) {
238 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
239 Nodes[Roots[i]] = new ETNode(Roots[i]); // Add a node for the root
240
241 // Iterate over all nodes in inverse depth first order.
242 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
243 for (idf_iterator<BasicBlock*> I = idf_begin(Roots[i]),
244 E = idf_end(Roots[i]); I != E; ++I) {
245 BasicBlock *BB = *I;
246 ETNode *&BBNode = Nodes[BB];
247 if (!BBNode) {
248 ETNode *IDomNode = NULL;
249
250 if (ID.get(BB))
251 IDomNode = getNodeForBlock(ID.get(BB));
252
253 // Add a new ETNode for this BasicBlock, and set it's parent
254 // to it's immediate dominator.
255 BBNode = new ETNode(BB);
256 if (IDomNode)
257 BBNode->setFather(IDomNode);
258 }
259 }
260
261 int dfsnum = 0;
262 // Iterate over all nodes in depth first order...
263 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
264 for (idf_iterator<BasicBlock*> I = idf_begin(Roots[i]),
265 E = idf_end(Roots[i]); I != E; ++I) {
266 if (!getNodeForBlock(*I)->hasFather())
267 getNodeForBlock(*I)->assignDFSNumber(dfsnum);
268 }
269 DFSInfoValid = true;
270}
Chris Lattner17152292001-07-02 05:46:38 +0000271
Chris Lattner17152292001-07-02 05:46:38 +0000272//===----------------------------------------------------------------------===//
Chris Lattner4c9df7c2002-08-02 16:43:03 +0000273// PostDominanceFrontier Implementation
Chris Lattner17152292001-07-02 05:46:38 +0000274//===----------------------------------------------------------------------===//
275
Chris Lattner1e435162002-07-26 21:12:44 +0000276static RegisterAnalysis<PostDominanceFrontier>
Chris Lattner17689df2002-07-30 16:27:52 +0000277H("postdomfrontier", "Post-Dominance Frontier Construction", true);
Chris Lattner93193f82002-01-31 00:42:27 +0000278
Chris Lattner1b7f7dc2002-04-28 16:21:30 +0000279const DominanceFrontier::DomSetType &
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000280PostDominanceFrontier::calculate(const PostDominatorTree &DT,
Chris Lattnerce6ef112002-07-26 18:40:14 +0000281 const DominatorTree::Node *Node) {
Chris Lattner94108ab2001-07-06 16:58:22 +0000282 // Loop over CFG successors to calculate DFlocal[Node]
Chris Lattnerc444a422003-09-11 16:26:13 +0000283 BasicBlock *BB = Node->getBlock();
Chris Lattner94108ab2001-07-06 16:58:22 +0000284 DomSetType &S = Frontiers[BB]; // The new set to fill in...
Chris Lattner706e61e2003-09-10 20:37:08 +0000285 if (getRoots().empty()) return S;
Chris Lattner94108ab2001-07-06 16:58:22 +0000286
Chris Lattner706e61e2003-09-10 20:37:08 +0000287 if (BB)
288 for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB);
289 SI != SE; ++SI)
Misha Brukman2f2d0652003-09-11 18:14:24 +0000290 // Does Node immediately dominate this predecessor?
Chris Lattner706e61e2003-09-10 20:37:08 +0000291 if (DT[*SI]->getIDom() != Node)
292 S.insert(*SI);
Chris Lattner94108ab2001-07-06 16:58:22 +0000293
294 // At this point, S is DFlocal. Now we union in DFup's of our children...
295 // Loop through and visit the nodes that Node immediately dominates (Node's
296 // children in the IDomTree)
297 //
Chris Lattnerce6ef112002-07-26 18:40:14 +0000298 for (PostDominatorTree::Node::const_iterator
299 NI = Node->begin(), NE = Node->end(); NI != NE; ++NI) {
Chris Lattner94108ab2001-07-06 16:58:22 +0000300 DominatorTree::Node *IDominee = *NI;
Chris Lattnerce6ef112002-07-26 18:40:14 +0000301 const DomSetType &ChildDF = calculate(DT, IDominee);
Chris Lattner94108ab2001-07-06 16:58:22 +0000302
303 DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end();
304 for (; CDFI != CDFE; ++CDFI) {
Chris Lattner4b5086c2005-11-18 07:28:26 +0000305 if (!Node->properlyDominates(DT[*CDFI]))
Misha Brukmandedf2bd2005-04-22 04:01:18 +0000306 S.insert(*CDFI);
Chris Lattner94108ab2001-07-06 16:58:22 +0000307 }
308 }
309
310 return S;
311}
Chris Lattnera69fd902002-08-21 23:43:50 +0000312
313// stub - a dummy function to make linking work ok.
314void PostDominanceFrontier::stub() {
315}
Brian Gaeked0fde302003-11-11 22:41:34 +0000316