blob: a1260835799363a444e03af6655931071ce4a3b8 [file] [log] [blame]
Chris Lattner17152292001-07-02 05:46:38 +00001//===- DominatorSet.cpp - Dominator Set Calculation --------------*- C++ -*--=//
2//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00003// This file provides a simple class to calculate the dominator set of a
4// function.
Chris Lattner17152292001-07-02 05:46:38 +00005//
6//===----------------------------------------------------------------------===//
7
8#include "llvm/Analysis/Dominators.h"
Chris Lattner483e14e2002-04-27 07:27:19 +00009#include "llvm/Transforms/UnifyFunctionExitNodes.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000010#include "llvm/Function.h"
Chris Lattner221d6882002-02-12 21:07:25 +000011#include "llvm/Support/CFG.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000012#include "Support/DepthFirstIterator.h"
13#include "Support/STLExtras.h"
Chris Lattnereb5230c2002-02-05 03:35:31 +000014#include "Support/SetOperations.h"
Chris Lattner17152292001-07-02 05:46:38 +000015#include <algorithm>
Chris Lattner697954c2002-01-20 22:54:45 +000016using std::set;
17
Chris Lattner94108ab2001-07-06 16:58:22 +000018//===----------------------------------------------------------------------===//
Chris Lattner17152292001-07-02 05:46:38 +000019// DominatorSet Implementation
20//===----------------------------------------------------------------------===//
21
Chris Lattner93193f82002-01-31 00:42:27 +000022AnalysisID cfg::DominatorSet::ID(AnalysisID::create<cfg::DominatorSet>());
23AnalysisID cfg::DominatorSet::PostDomID(AnalysisID::create<cfg::DominatorSet>());
24
Chris Lattnerf57b8452002-04-27 06:56:12 +000025bool cfg::DominatorSet::runOnFunction(Function *F) {
Chris Lattner93193f82002-01-31 00:42:27 +000026 Doms.clear(); // Reset from the last time we were run...
27
28 if (isPostDominator())
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000029 calcPostDominatorSet(F);
Chris Lattner93193f82002-01-31 00:42:27 +000030 else
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000031 calcForwardDominatorSet(F);
Chris Lattner93193f82002-01-31 00:42:27 +000032 return false;
Chris Lattner94108ab2001-07-06 16:58:22 +000033}
34
Chris Lattner93193f82002-01-31 00:42:27 +000035
Chris Lattner94108ab2001-07-06 16:58:22 +000036// calcForwardDominatorSet - This method calculates the forward dominator sets
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000037// for the specified function.
Chris Lattner94108ab2001-07-06 16:58:22 +000038//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000039void cfg::DominatorSet::calcForwardDominatorSet(Function *M) {
Chris Lattner93193f82002-01-31 00:42:27 +000040 Root = M->getEntryNode();
Chris Lattner455889a2002-02-12 22:39:50 +000041 assert(pred_begin(Root) == pred_end(Root) &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000042 "Root node has predecessors in function!");
Chris Lattnerff5a8c42001-11-26 18:52:02 +000043
Chris Lattner17152292001-07-02 05:46:38 +000044 bool Changed;
45 do {
46 Changed = false;
47
48 DomSetType WorkingSet;
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000049 df_iterator<Function*> It = df_begin(M), End = df_end(M);
Chris Lattner17152292001-07-02 05:46:38 +000050 for ( ; It != End; ++It) {
Chris Lattnera298d272002-04-28 00:15:57 +000051 BasicBlock *BB = *It;
52 pred_iterator PI = pred_begin(BB), PEnd = pred_end(BB);
Chris Lattner17152292001-07-02 05:46:38 +000053 if (PI != PEnd) { // Is there SOME predecessor?
54 // Loop until we get to a predecessor that has had it's dom set filled
55 // in at least once. We are guaranteed to have this because we are
56 // traversing the graph in DFO and have handled start nodes specially.
57 //
58 while (Doms[*PI].size() == 0) ++PI;
59 WorkingSet = Doms[*PI];
60
61 for (++PI; PI != PEnd; ++PI) { // Intersect all of the predecessor sets
62 DomSetType &PredSet = Doms[*PI];
63 if (PredSet.size())
64 set_intersect(WorkingSet, PredSet);
65 }
66 }
67
68 WorkingSet.insert(BB); // A block always dominates itself
69 DomSetType &BBSet = Doms[BB];
70 if (BBSet != WorkingSet) {
71 BBSet.swap(WorkingSet); // Constant time operation!
72 Changed = true; // The sets changed.
73 }
74 WorkingSet.clear(); // Clear out the set for next iteration
75 }
76 } while (Changed);
Chris Lattner94108ab2001-07-06 16:58:22 +000077}
Chris Lattner17152292001-07-02 05:46:38 +000078
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000079// Postdominator set constructor. This ctor converts the specified function to
Chris Lattner94108ab2001-07-06 16:58:22 +000080// only have a single exit node (return stmt), then calculates the post
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000081// dominance sets for the function.
Chris Lattner94108ab2001-07-06 16:58:22 +000082//
Chris Lattnera298d272002-04-28 00:15:57 +000083void cfg::DominatorSet::calcPostDominatorSet(Function *F) {
Chris Lattner93193f82002-01-31 00:42:27 +000084 // Since we require that the unify all exit nodes pass has been run, we know
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000085 // that there can be at most one return instruction in the function left.
Chris Lattner93193f82002-01-31 00:42:27 +000086 // Get it.
87 //
Chris Lattner483e14e2002-04-27 07:27:19 +000088 Root = getAnalysis<UnifyFunctionExitNodes>().getExitNode();
Chris Lattner94108ab2001-07-06 16:58:22 +000089
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000090 if (Root == 0) { // No exit node for the function? Postdomsets are all empty
Chris Lattnera298d272002-04-28 00:15:57 +000091 for (Function::iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
92 Doms[*FI] = DomSetType();
Chris Lattner384e5b12001-08-23 17:07:19 +000093 return;
94 }
Chris Lattner94108ab2001-07-06 16:58:22 +000095
96 bool Changed;
97 do {
98 Changed = false;
99
100 set<const BasicBlock*> Visited;
101 DomSetType WorkingSet;
Chris Lattner93193f82002-01-31 00:42:27 +0000102 idf_iterator<BasicBlock*> It = idf_begin(Root), End = idf_end(Root);
Chris Lattner94108ab2001-07-06 16:58:22 +0000103 for ( ; It != End; ++It) {
Chris Lattnera298d272002-04-28 00:15:57 +0000104 BasicBlock *BB = *It;
105 succ_iterator PI = succ_begin(BB), PEnd = succ_end(BB);
Chris Lattner94108ab2001-07-06 16:58:22 +0000106 if (PI != PEnd) { // Is there SOME predecessor?
107 // Loop until we get to a successor that has had it's dom set filled
108 // in at least once. We are guaranteed to have this because we are
109 // traversing the graph in DFO and have handled start nodes specially.
110 //
111 while (Doms[*PI].size() == 0) ++PI;
112 WorkingSet = Doms[*PI];
113
114 for (++PI; PI != PEnd; ++PI) { // Intersect all of the successor sets
115 DomSetType &PredSet = Doms[*PI];
116 if (PredSet.size())
117 set_intersect(WorkingSet, PredSet);
118 }
119 }
120
121 WorkingSet.insert(BB); // A block always dominates itself
122 DomSetType &BBSet = Doms[BB];
123 if (BBSet != WorkingSet) {
124 BBSet.swap(WorkingSet); // Constant time operation!
125 Changed = true; // The sets changed.
126 }
127 WorkingSet.clear(); // Clear out the set for next iteration
128 }
129 } while (Changed);
Chris Lattner17152292001-07-02 05:46:38 +0000130}
131
Chris Lattnerf57b8452002-04-27 06:56:12 +0000132// getAnalysisUsage - This obviously provides a dominator set, but it also
133// uses the UnifyFunctionExitNodes pass if building post-dominators
Chris Lattner93193f82002-01-31 00:42:27 +0000134//
Chris Lattnerf57b8452002-04-27 06:56:12 +0000135void cfg::DominatorSet::getAnalysisUsage(AnalysisUsage &AU) const {
136 AU.setPreservesAll();
Chris Lattner65b97092002-01-31 18:29:24 +0000137 if (isPostDominator()) {
Chris Lattnerf57b8452002-04-27 06:56:12 +0000138 AU.addProvided(PostDomID);
Chris Lattner483e14e2002-04-27 07:27:19 +0000139 AU.addRequired(UnifyFunctionExitNodes::ID);
Chris Lattner65b97092002-01-31 18:29:24 +0000140 } else {
Chris Lattnerf57b8452002-04-27 06:56:12 +0000141 AU.addProvided(ID);
Chris Lattner65b97092002-01-31 18:29:24 +0000142 }
Chris Lattner93193f82002-01-31 00:42:27 +0000143}
144
Chris Lattner17152292001-07-02 05:46:38 +0000145
146//===----------------------------------------------------------------------===//
147// ImmediateDominators Implementation
148//===----------------------------------------------------------------------===//
149
Chris Lattner93193f82002-01-31 00:42:27 +0000150AnalysisID cfg::ImmediateDominators::ID(AnalysisID::create<cfg::ImmediateDominators>());
151AnalysisID cfg::ImmediateDominators::PostDomID(AnalysisID::create<cfg::ImmediateDominators>());
152
Chris Lattner17152292001-07-02 05:46:38 +0000153// calcIDoms - Calculate the immediate dominator mapping, given a set of
154// dominators for every basic block.
155void cfg::ImmediateDominators::calcIDoms(const DominatorSet &DS) {
156 // Loop over all of the nodes that have dominators... figuring out the IDOM
157 // for each node...
158 //
159 for (DominatorSet::const_iterator DI = DS.begin(), DEnd = DS.end();
160 DI != DEnd; ++DI) {
Chris Lattnera298d272002-04-28 00:15:57 +0000161 BasicBlock *BB = DI->first;
Chris Lattner17152292001-07-02 05:46:38 +0000162 const DominatorSet::DomSetType &Dominators = DI->second;
163 unsigned DomSetSize = Dominators.size();
164 if (DomSetSize == 1) continue; // Root node... IDom = null
165
166 // Loop over all dominators of this node. This corresponds to looping over
167 // nodes in the dominator chain, looking for a node whose dominator set is
168 // equal to the current nodes, except that the current node does not exist
169 // in it. This means that it is one level higher in the dom chain than the
170 // current node, and it is our idom!
171 //
172 DominatorSet::DomSetType::const_iterator I = Dominators.begin();
173 DominatorSet::DomSetType::const_iterator End = Dominators.end();
174 for (; I != End; ++I) { // Iterate over dominators...
175 // All of our dominators should form a chain, where the number of elements
176 // in the dominator set indicates what level the node is at in the chain.
177 // We want the node immediately above us, so it will have an identical
178 // dominator set, except that BB will not dominate it... therefore it's
179 // dominator set size will be one less than BB's...
180 //
181 if (DS.getDominators(*I).size() == DomSetSize - 1) {
182 IDoms[BB] = *I;
183 break;
184 }
185 }
186 }
187}
188
189
190//===----------------------------------------------------------------------===//
191// DominatorTree Implementation
192//===----------------------------------------------------------------------===//
193
Chris Lattner93193f82002-01-31 00:42:27 +0000194AnalysisID cfg::DominatorTree::ID(AnalysisID::create<cfg::DominatorTree>());
195AnalysisID cfg::DominatorTree::PostDomID(AnalysisID::create<cfg::DominatorTree>());
196
197// DominatorTree::reset - Free all of the tree node memory.
Chris Lattner17152292001-07-02 05:46:38 +0000198//
Chris Lattner93193f82002-01-31 00:42:27 +0000199void cfg::DominatorTree::reset() {
Chris Lattner17152292001-07-02 05:46:38 +0000200 for (NodeMapType::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I)
201 delete I->second;
Chris Lattner93193f82002-01-31 00:42:27 +0000202 Nodes.clear();
Chris Lattner17152292001-07-02 05:46:38 +0000203}
204
205
Chris Lattner93193f82002-01-31 00:42:27 +0000206#if 0
207// Given immediate dominators, we can also calculate the dominator tree
Chris Lattner17152292001-07-02 05:46:38 +0000208cfg::DominatorTree::DominatorTree(const ImmediateDominators &IDoms)
Chris Lattner94108ab2001-07-06 16:58:22 +0000209 : DominatorBase(IDoms.getRoot()) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000210 const Function *M = Root->getParent();
Chris Lattner17152292001-07-02 05:46:38 +0000211
212 Nodes[Root] = new Node(Root, 0); // Add a node for the root...
213
214 // Iterate over all nodes in depth first order...
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000215 for (df_iterator<const Function*> I = df_begin(M), E = df_end(M); I!=E; ++I) {
Chris Lattner17152292001-07-02 05:46:38 +0000216 const BasicBlock *BB = *I, *IDom = IDoms[*I];
217
218 if (IDom != 0) { // Ignore the root node and other nasty nodes
219 // We know that the immediate dominator should already have a node,
220 // because we are traversing the CFG in depth first order!
221 //
222 assert(Nodes[IDom] && "No node for IDOM?");
223 Node *IDomNode = Nodes[IDom];
224
225 // Add a new tree node for this BasicBlock, and link it as a child of
226 // IDomNode
227 Nodes[BB] = IDomNode->addChild(new Node(BB, IDomNode));
228 }
229 }
230}
Chris Lattner93193f82002-01-31 00:42:27 +0000231#endif
Chris Lattner17152292001-07-02 05:46:38 +0000232
233void cfg::DominatorTree::calculate(const DominatorSet &DS) {
Chris Lattner17152292001-07-02 05:46:38 +0000234 Nodes[Root] = new Node(Root, 0); // Add a node for the root...
235
Chris Lattner94108ab2001-07-06 16:58:22 +0000236 if (!isPostDominator()) {
237 // Iterate over all nodes in depth first order...
Chris Lattner93193f82002-01-31 00:42:27 +0000238 for (df_iterator<BasicBlock*> I = df_begin(Root), E = df_end(Root);
Chris Lattner3ff43872001-09-28 22:56:31 +0000239 I != E; ++I) {
Chris Lattnera298d272002-04-28 00:15:57 +0000240 BasicBlock *BB = *I;
Chris Lattner94108ab2001-07-06 16:58:22 +0000241 const DominatorSet::DomSetType &Dominators = DS.getDominators(BB);
242 unsigned DomSetSize = Dominators.size();
243 if (DomSetSize == 1) continue; // Root node... IDom = null
244
Chris Lattner3ff43872001-09-28 22:56:31 +0000245 // Loop over all dominators of this node. This corresponds to looping over
Chris Lattner94108ab2001-07-06 16:58:22 +0000246 // nodes in the dominator chain, looking for a node whose dominator set is
247 // equal to the current nodes, except that the current node does not exist
Chris Lattner3ff43872001-09-28 22:56:31 +0000248 // in it. This means that it is one level higher in the dom chain than the
Chris Lattner94108ab2001-07-06 16:58:22 +0000249 // current node, and it is our idom! We know that we have already added
250 // a DominatorTree node for our idom, because the idom must be a
251 // predecessor in the depth first order that we are iterating through the
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000252 // function.
Chris Lattner17152292001-07-02 05:46:38 +0000253 //
Chris Lattner94108ab2001-07-06 16:58:22 +0000254 DominatorSet::DomSetType::const_iterator I = Dominators.begin();
255 DominatorSet::DomSetType::const_iterator End = Dominators.end();
256 for (; I != End; ++I) { // Iterate over dominators...
Chris Lattner3ff43872001-09-28 22:56:31 +0000257 // All of our dominators should form a chain, where the number of
258 // elements in the dominator set indicates what level the node is at in
259 // the chain. We want the node immediately above us, so it will have
260 // an identical dominator set, except that BB will not dominate it...
261 // therefore it's dominator set size will be one less than BB's...
Chris Lattner17152292001-07-02 05:46:38 +0000262 //
Chris Lattner94108ab2001-07-06 16:58:22 +0000263 if (DS.getDominators(*I).size() == DomSetSize - 1) {
264 // We know that the immediate dominator should already have a node,
265 // because we are traversing the CFG in depth first order!
266 //
267 Node *IDomNode = Nodes[*I];
268 assert(IDomNode && "No node for IDOM?");
269
270 // Add a new tree node for this BasicBlock, and link it as a child of
271 // IDomNode
272 Nodes[BB] = IDomNode->addChild(new Node(BB, IDomNode));
273 break;
274 }
275 }
276 }
Chris Lattner384e5b12001-08-23 17:07:19 +0000277 } else if (Root) {
Chris Lattner94108ab2001-07-06 16:58:22 +0000278 // Iterate over all nodes in depth first order...
Chris Lattner93193f82002-01-31 00:42:27 +0000279 for (idf_iterator<BasicBlock*> I = idf_begin(Root), E = idf_end(Root);
Chris Lattner3ff43872001-09-28 22:56:31 +0000280 I != E; ++I) {
Chris Lattnera298d272002-04-28 00:15:57 +0000281 BasicBlock *BB = *I;
Chris Lattner94108ab2001-07-06 16:58:22 +0000282 const DominatorSet::DomSetType &Dominators = DS.getDominators(BB);
283 unsigned DomSetSize = Dominators.size();
284 if (DomSetSize == 1) continue; // Root node... IDom = null
285
Chris Lattner3ff43872001-09-28 22:56:31 +0000286 // Loop over all dominators of this node. This corresponds to looping
287 // over nodes in the dominator chain, looking for a node whose dominator
288 // set is equal to the current nodes, except that the current node does
289 // not exist in it. This means that it is one level higher in the dom
290 // chain than the current node, and it is our idom! We know that we have
291 // already added a DominatorTree node for our idom, because the idom must
292 // be a predecessor in the depth first order that we are iterating through
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000293 // the function.
Chris Lattner94108ab2001-07-06 16:58:22 +0000294 //
295 DominatorSet::DomSetType::const_iterator I = Dominators.begin();
296 DominatorSet::DomSetType::const_iterator End = Dominators.end();
297 for (; I != End; ++I) { // Iterate over dominators...
Chris Lattner93193f82002-01-31 00:42:27 +0000298 // All of our dominators should form a chain, where the number
299 // of elements in the dominator set indicates what level the
300 // node is at in the chain. We want the node immediately
301 // above us, so it will have an identical dominator set,
302 // except that BB will not dominate it... therefore it's
Chris Lattner94108ab2001-07-06 16:58:22 +0000303 // dominator set size will be one less than BB's...
304 //
305 if (DS.getDominators(*I).size() == DomSetSize - 1) {
306 // We know that the immediate dominator should already have a node,
307 // because we are traversing the CFG in depth first order!
308 //
309 Node *IDomNode = Nodes[*I];
310 assert(IDomNode && "No node for IDOM?");
311
312 // Add a new tree node for this BasicBlock, and link it as a child of
313 // IDomNode
314 Nodes[BB] = IDomNode->addChild(new Node(BB, IDomNode));
315 break;
316 }
Chris Lattner17152292001-07-02 05:46:38 +0000317 }
318 }
319 }
320}
321
322
323
324//===----------------------------------------------------------------------===//
325// DominanceFrontier Implementation
326//===----------------------------------------------------------------------===//
327
Chris Lattner93193f82002-01-31 00:42:27 +0000328AnalysisID cfg::DominanceFrontier::ID(AnalysisID::create<cfg::DominanceFrontier>());
329AnalysisID cfg::DominanceFrontier::PostDomID(AnalysisID::create<cfg::DominanceFrontier>());
330
Chris Lattner17152292001-07-02 05:46:38 +0000331const cfg::DominanceFrontier::DomSetType &
332cfg::DominanceFrontier::calcDomFrontier(const DominatorTree &DT,
333 const DominatorTree::Node *Node) {
334 // Loop over CFG successors to calculate DFlocal[Node]
Chris Lattnera298d272002-04-28 00:15:57 +0000335 BasicBlock *BB = Node->getNode();
Chris Lattner17152292001-07-02 05:46:38 +0000336 DomSetType &S = Frontiers[BB]; // The new set to fill in...
337
Chris Lattnera298d272002-04-28 00:15:57 +0000338 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB);
Chris Lattner455889a2002-02-12 22:39:50 +0000339 SI != SE; ++SI) {
Chris Lattner17152292001-07-02 05:46:38 +0000340 // Does Node immediately dominate this successor?
341 if (DT[*SI]->getIDom() != Node)
342 S.insert(*SI);
343 }
344
345 // At this point, S is DFlocal. Now we union in DFup's of our children...
346 // Loop through and visit the nodes that Node immediately dominates (Node's
347 // children in the IDomTree)
348 //
349 for (DominatorTree::Node::const_iterator NI = Node->begin(), NE = Node->end();
350 NI != NE; ++NI) {
351 DominatorTree::Node *IDominee = *NI;
352 const DomSetType &ChildDF = calcDomFrontier(DT, IDominee);
353
354 DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end();
355 for (; CDFI != CDFE; ++CDFI) {
356 if (!Node->dominates(DT[*CDFI]))
357 S.insert(*CDFI);
358 }
359 }
360
361 return S;
362}
Chris Lattner94108ab2001-07-06 16:58:22 +0000363
364const cfg::DominanceFrontier::DomSetType &
365cfg::DominanceFrontier::calcPostDomFrontier(const DominatorTree &DT,
366 const DominatorTree::Node *Node) {
367 // Loop over CFG successors to calculate DFlocal[Node]
Chris Lattnera298d272002-04-28 00:15:57 +0000368 BasicBlock *BB = Node->getNode();
Chris Lattner94108ab2001-07-06 16:58:22 +0000369 DomSetType &S = Frontiers[BB]; // The new set to fill in...
Chris Lattner384e5b12001-08-23 17:07:19 +0000370 if (!Root) return S;
Chris Lattner94108ab2001-07-06 16:58:22 +0000371
Chris Lattnera298d272002-04-28 00:15:57 +0000372 for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB);
Chris Lattner455889a2002-02-12 22:39:50 +0000373 SI != SE; ++SI) {
Chris Lattner94108ab2001-07-06 16:58:22 +0000374 // Does Node immediately dominate this predeccessor?
375 if (DT[*SI]->getIDom() != Node)
376 S.insert(*SI);
377 }
378
379 // At this point, S is DFlocal. Now we union in DFup's of our children...
380 // Loop through and visit the nodes that Node immediately dominates (Node's
381 // children in the IDomTree)
382 //
383 for (DominatorTree::Node::const_iterator NI = Node->begin(), NE = Node->end();
384 NI != NE; ++NI) {
385 DominatorTree::Node *IDominee = *NI;
Chris Lattner35908302001-07-08 05:54:09 +0000386 const DomSetType &ChildDF = calcPostDomFrontier(DT, IDominee);
Chris Lattner94108ab2001-07-06 16:58:22 +0000387
388 DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end();
389 for (; CDFI != CDFE; ++CDFI) {
390 if (!Node->dominates(DT[*CDFI]))
391 S.insert(*CDFI);
392 }
393 }
394
395 return S;
396}