blob: 777b3c4a9f0fa474882618b9b5e1a0670db13a8a [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 Lattnerfc514f42002-05-07 19:18:48 +00009#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
Chris Lattner221d6882002-02-12 21:07:25 +000010#include "llvm/Support/CFG.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000011#include "Support/DepthFirstIterator.h"
12#include "Support/STLExtras.h"
Chris Lattnereb5230c2002-02-05 03:35:31 +000013#include "Support/SetOperations.h"
Chris Lattner17152292001-07-02 05:46:38 +000014#include <algorithm>
Chris Lattner697954c2002-01-20 22:54:45 +000015using std::set;
16
Chris Lattner94108ab2001-07-06 16:58:22 +000017//===----------------------------------------------------------------------===//
Chris Lattner17152292001-07-02 05:46:38 +000018// DominatorSet Implementation
19//===----------------------------------------------------------------------===//
20
Chris Lattner07a228d2002-05-06 19:32:07 +000021AnalysisID DominatorSet::ID(AnalysisID::create<DominatorSet>(), true);
Chris Lattnerce6ef112002-07-26 18:40:14 +000022AnalysisID PostDominatorSet::ID(AnalysisID::create<PostDominatorSet>(), true);
Chris Lattner94108ab2001-07-06 16:58:22 +000023
Chris Lattneref704a22002-05-13 22:03:16 +000024// dominates - Return true if A dominates B. This performs the special checks
25// neccesary if A and B are in the same basic block.
26//
Chris Lattnerce6ef112002-07-26 18:40:14 +000027bool DominatorSetBase::dominates(Instruction *A, Instruction *B) const {
Chris Lattneref704a22002-05-13 22:03:16 +000028 BasicBlock *BBA = A->getParent(), *BBB = B->getParent();
29 if (BBA != BBB) return dominates(BBA, BBB);
30
31 // Loop through the basic block until we find A or B.
32 BasicBlock::iterator I = BBA->begin();
Chris Lattner7e708292002-06-25 16:13:24 +000033 for (; &*I != A && &*I != B; ++I) /*empty*/;
Chris Lattneref704a22002-05-13 22:03:16 +000034
35 // A dominates B if it is found first in the basic block...
Chris Lattner7e708292002-06-25 16:13:24 +000036 return &*I == A;
Chris Lattneref704a22002-05-13 22:03:16 +000037}
Chris Lattner93193f82002-01-31 00:42:27 +000038
Chris Lattnerce6ef112002-07-26 18:40:14 +000039// runOnFunction - This method calculates the forward dominator sets for the
40// specified function.
Chris Lattner94108ab2001-07-06 16:58:22 +000041//
Chris Lattnerce6ef112002-07-26 18:40:14 +000042bool DominatorSet::runOnFunction(Function &F) {
43 Doms.clear(); // Reset from the last time we were run...
Chris Lattner7e708292002-06-25 16:13:24 +000044 Root = &F.getEntryNode();
Chris Lattner455889a2002-02-12 22:39:50 +000045 assert(pred_begin(Root) == pred_end(Root) &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000046 "Root node has predecessors in function!");
Chris Lattnerff5a8c42001-11-26 18:52:02 +000047
Chris Lattner17152292001-07-02 05:46:38 +000048 bool Changed;
49 do {
50 Changed = false;
51
52 DomSetType WorkingSet;
Chris Lattner7e708292002-06-25 16:13:24 +000053 df_iterator<Function*> It = df_begin(&F), End = df_end(&F);
Chris Lattner17152292001-07-02 05:46:38 +000054 for ( ; It != End; ++It) {
Chris Lattnera298d272002-04-28 00:15:57 +000055 BasicBlock *BB = *It;
56 pred_iterator PI = pred_begin(BB), PEnd = pred_end(BB);
Chris Lattner17152292001-07-02 05:46:38 +000057 if (PI != PEnd) { // Is there SOME predecessor?
58 // Loop until we get to a predecessor that has had it's dom set filled
59 // in at least once. We are guaranteed to have this because we are
60 // traversing the graph in DFO and have handled start nodes specially.
61 //
62 while (Doms[*PI].size() == 0) ++PI;
63 WorkingSet = Doms[*PI];
64
65 for (++PI; PI != PEnd; ++PI) { // Intersect all of the predecessor sets
66 DomSetType &PredSet = Doms[*PI];
67 if (PredSet.size())
68 set_intersect(WorkingSet, PredSet);
69 }
70 }
71
72 WorkingSet.insert(BB); // A block always dominates itself
73 DomSetType &BBSet = Doms[BB];
74 if (BBSet != WorkingSet) {
75 BBSet.swap(WorkingSet); // Constant time operation!
76 Changed = true; // The sets changed.
77 }
78 WorkingSet.clear(); // Clear out the set for next iteration
79 }
80 } while (Changed);
Chris Lattnerce6ef112002-07-26 18:40:14 +000081 return false;
Chris Lattner94108ab2001-07-06 16:58:22 +000082}
Chris Lattner17152292001-07-02 05:46:38 +000083
Chris Lattnerce6ef112002-07-26 18:40:14 +000084
85// Postdominator set construction. This converts the specified function to only
86// have a single exit node (return stmt), then calculates the post dominance
87// sets for the function.
Chris Lattner94108ab2001-07-06 16:58:22 +000088//
Chris Lattnerce6ef112002-07-26 18:40:14 +000089bool PostDominatorSet::runOnFunction(Function &F) {
90 Doms.clear(); // Reset from the last time we were run...
Chris Lattner93193f82002-01-31 00:42:27 +000091 // Since we require that the unify all exit nodes pass has been run, we know
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000092 // that there can be at most one return instruction in the function left.
Chris Lattner93193f82002-01-31 00:42:27 +000093 // Get it.
94 //
Chris Lattner483e14e2002-04-27 07:27:19 +000095 Root = getAnalysis<UnifyFunctionExitNodes>().getExitNode();
Chris Lattner94108ab2001-07-06 16:58:22 +000096
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000097 if (Root == 0) { // No exit node for the function? Postdomsets are all empty
Chris Lattner7e708292002-06-25 16:13:24 +000098 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
99 Doms[FI] = DomSetType();
Chris Lattnerce6ef112002-07-26 18:40:14 +0000100 return false;
Chris Lattner384e5b12001-08-23 17:07:19 +0000101 }
Chris Lattner94108ab2001-07-06 16:58:22 +0000102
103 bool Changed;
104 do {
105 Changed = false;
106
107 set<const BasicBlock*> Visited;
108 DomSetType WorkingSet;
Chris Lattner93193f82002-01-31 00:42:27 +0000109 idf_iterator<BasicBlock*> It = idf_begin(Root), End = idf_end(Root);
Chris Lattner94108ab2001-07-06 16:58:22 +0000110 for ( ; It != End; ++It) {
Chris Lattnera298d272002-04-28 00:15:57 +0000111 BasicBlock *BB = *It;
112 succ_iterator PI = succ_begin(BB), PEnd = succ_end(BB);
Chris Lattner94108ab2001-07-06 16:58:22 +0000113 if (PI != PEnd) { // Is there SOME predecessor?
114 // Loop until we get to a successor that has had it's dom set filled
115 // in at least once. We are guaranteed to have this because we are
116 // traversing the graph in DFO and have handled start nodes specially.
117 //
118 while (Doms[*PI].size() == 0) ++PI;
119 WorkingSet = Doms[*PI];
120
121 for (++PI; PI != PEnd; ++PI) { // Intersect all of the successor sets
122 DomSetType &PredSet = Doms[*PI];
123 if (PredSet.size())
124 set_intersect(WorkingSet, PredSet);
125 }
126 }
127
128 WorkingSet.insert(BB); // A block always dominates itself
129 DomSetType &BBSet = Doms[BB];
130 if (BBSet != WorkingSet) {
131 BBSet.swap(WorkingSet); // Constant time operation!
132 Changed = true; // The sets changed.
133 }
134 WorkingSet.clear(); // Clear out the set for next iteration
135 }
136 } while (Changed);
Chris Lattnerce6ef112002-07-26 18:40:14 +0000137 return false;
Chris Lattner17152292001-07-02 05:46:38 +0000138}
139
Chris Lattnerce6ef112002-07-26 18:40:14 +0000140// getAnalysisUsage - This obviously provides a post-dominator set, but it also
141// requires the UnifyFunctionExitNodes pass.
Chris Lattner93193f82002-01-31 00:42:27 +0000142//
Chris Lattnerce6ef112002-07-26 18:40:14 +0000143void PostDominatorSet::getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerf57b8452002-04-27 06:56:12 +0000144 AU.setPreservesAll();
Chris Lattnerce6ef112002-07-26 18:40:14 +0000145 AU.addProvided(ID);
146 AU.addRequired(UnifyFunctionExitNodes::ID);
Chris Lattner93193f82002-01-31 00:42:27 +0000147}
148
Chris Lattner17152292001-07-02 05:46:38 +0000149
150//===----------------------------------------------------------------------===//
151// ImmediateDominators Implementation
152//===----------------------------------------------------------------------===//
153
Chris Lattner07a228d2002-05-06 19:32:07 +0000154AnalysisID ImmediateDominators::ID(AnalysisID::create<ImmediateDominators>(), true);
Chris Lattnerce6ef112002-07-26 18:40:14 +0000155AnalysisID ImmediatePostDominators::ID(AnalysisID::create<ImmediatePostDominators>(), true);
Chris Lattner93193f82002-01-31 00:42:27 +0000156
Chris Lattner17152292001-07-02 05:46:38 +0000157// calcIDoms - Calculate the immediate dominator mapping, given a set of
158// dominators for every basic block.
Chris Lattnerce6ef112002-07-26 18:40:14 +0000159void ImmediateDominatorsBase::calcIDoms(const DominatorSetBase &DS) {
Chris Lattner17152292001-07-02 05:46:38 +0000160 // Loop over all of the nodes that have dominators... figuring out the IDOM
161 // for each node...
162 //
163 for (DominatorSet::const_iterator DI = DS.begin(), DEnd = DS.end();
164 DI != DEnd; ++DI) {
Chris Lattnera298d272002-04-28 00:15:57 +0000165 BasicBlock *BB = DI->first;
Chris Lattner17152292001-07-02 05:46:38 +0000166 const DominatorSet::DomSetType &Dominators = DI->second;
167 unsigned DomSetSize = Dominators.size();
168 if (DomSetSize == 1) continue; // Root node... IDom = null
169
170 // Loop over all dominators of this node. This corresponds to looping over
171 // nodes in the dominator chain, looking for a node whose dominator set is
172 // equal to the current nodes, except that the current node does not exist
173 // in it. This means that it is one level higher in the dom chain than the
174 // current node, and it is our idom!
175 //
176 DominatorSet::DomSetType::const_iterator I = Dominators.begin();
177 DominatorSet::DomSetType::const_iterator End = Dominators.end();
178 for (; I != End; ++I) { // Iterate over dominators...
179 // All of our dominators should form a chain, where the number of elements
180 // in the dominator set indicates what level the node is at in the chain.
181 // We want the node immediately above us, so it will have an identical
182 // dominator set, except that BB will not dominate it... therefore it's
183 // dominator set size will be one less than BB's...
184 //
185 if (DS.getDominators(*I).size() == DomSetSize - 1) {
186 IDoms[BB] = *I;
187 break;
188 }
189 }
190 }
191}
192
193
194//===----------------------------------------------------------------------===//
195// DominatorTree Implementation
196//===----------------------------------------------------------------------===//
197
Chris Lattner07a228d2002-05-06 19:32:07 +0000198AnalysisID DominatorTree::ID(AnalysisID::create<DominatorTree>(), true);
Chris Lattnerce6ef112002-07-26 18:40:14 +0000199AnalysisID PostDominatorTree::ID(AnalysisID::create<PostDominatorTree>(), true);
Chris Lattner93193f82002-01-31 00:42:27 +0000200
Chris Lattnerce6ef112002-07-26 18:40:14 +0000201// DominatorTreeBase::reset - Free all of the tree node memory.
Chris Lattner17152292001-07-02 05:46:38 +0000202//
Chris Lattnerce6ef112002-07-26 18:40:14 +0000203void DominatorTreeBase::reset() {
Chris Lattner17152292001-07-02 05:46:38 +0000204 for (NodeMapType::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I)
205 delete I->second;
Chris Lattner93193f82002-01-31 00:42:27 +0000206 Nodes.clear();
Chris Lattner17152292001-07-02 05:46:38 +0000207}
208
209
Chris Lattner1b7f7dc2002-04-28 16:21:30 +0000210void DominatorTree::calculate(const DominatorSet &DS) {
Chris Lattner17152292001-07-02 05:46:38 +0000211 Nodes[Root] = new Node(Root, 0); // Add a node for the root...
212
Chris Lattnerce6ef112002-07-26 18:40:14 +0000213 // Iterate over all nodes in depth first order...
214 for (df_iterator<BasicBlock*> I = df_begin(Root), E = df_end(Root);
215 I != E; ++I) {
216 BasicBlock *BB = *I;
217 const DominatorSet::DomSetType &Dominators = DS.getDominators(BB);
218 unsigned DomSetSize = Dominators.size();
219 if (DomSetSize == 1) continue; // Root node... IDom = null
Chris Lattner94108ab2001-07-06 16:58:22 +0000220
Chris Lattnerce6ef112002-07-26 18:40:14 +0000221 // Loop over all dominators of this node. This corresponds to looping over
222 // nodes in the dominator chain, looking for a node whose dominator set is
223 // equal to the current nodes, except that the current node does not exist
224 // in it. This means that it is one level higher in the dom chain than the
225 // current node, and it is our idom! We know that we have already added
226 // a DominatorTree node for our idom, because the idom must be a
227 // predecessor in the depth first order that we are iterating through the
228 // function.
229 //
230 DominatorSet::DomSetType::const_iterator I = Dominators.begin();
231 DominatorSet::DomSetType::const_iterator End = Dominators.end();
232 for (; I != End; ++I) { // Iterate over dominators...
233 // All of our dominators should form a chain, where the number of
234 // elements in the dominator set indicates what level the node is at in
235 // the chain. We want the node immediately above us, so it will have
236 // an identical dominator set, except that BB will not dominate it...
237 // therefore it's dominator set size will be one less than BB's...
Chris Lattner17152292001-07-02 05:46:38 +0000238 //
Chris Lattnerce6ef112002-07-26 18:40:14 +0000239 if (DS.getDominators(*I).size() == DomSetSize - 1) {
240 // We know that the immediate dominator should already have a node,
241 // because we are traversing the CFG in depth first order!
242 //
243 Node *IDomNode = Nodes[*I];
244 assert(IDomNode && "No node for IDOM?");
245
246 // Add a new tree node for this BasicBlock, and link it as a child of
247 // IDomNode
248 Nodes[BB] = IDomNode->addChild(new Node(BB, IDomNode));
249 break;
Chris Lattner94108ab2001-07-06 16:58:22 +0000250 }
251 }
Chris Lattnerce6ef112002-07-26 18:40:14 +0000252 }
253}
254
255
256void PostDominatorTree::calculate(const PostDominatorSet &DS) {
257 Nodes[Root] = new Node(Root, 0); // Add a node for the root...
258
259 if (Root) {
Chris Lattner94108ab2001-07-06 16:58:22 +0000260 // Iterate over all nodes in depth first order...
Chris Lattner93193f82002-01-31 00:42:27 +0000261 for (idf_iterator<BasicBlock*> I = idf_begin(Root), E = idf_end(Root);
Chris Lattner3ff43872001-09-28 22:56:31 +0000262 I != E; ++I) {
Chris Lattnera298d272002-04-28 00:15:57 +0000263 BasicBlock *BB = *I;
Chris Lattner94108ab2001-07-06 16:58:22 +0000264 const DominatorSet::DomSetType &Dominators = DS.getDominators(BB);
265 unsigned DomSetSize = Dominators.size();
266 if (DomSetSize == 1) continue; // Root node... IDom = null
267
Chris Lattner3ff43872001-09-28 22:56:31 +0000268 // Loop over all dominators of this node. This corresponds to looping
269 // over nodes in the dominator chain, looking for a node whose dominator
270 // set is equal to the current nodes, except that the current node does
271 // not exist in it. This means that it is one level higher in the dom
272 // chain than the current node, and it is our idom! We know that we have
273 // already added a DominatorTree node for our idom, because the idom must
274 // be a predecessor in the depth first order that we are iterating through
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000275 // the function.
Chris Lattner94108ab2001-07-06 16:58:22 +0000276 //
277 DominatorSet::DomSetType::const_iterator I = Dominators.begin();
278 DominatorSet::DomSetType::const_iterator End = Dominators.end();
279 for (; I != End; ++I) { // Iterate over dominators...
Chris Lattner93193f82002-01-31 00:42:27 +0000280 // All of our dominators should form a chain, where the number
281 // of elements in the dominator set indicates what level the
282 // node is at in the chain. We want the node immediately
283 // above us, so it will have an identical dominator set,
284 // except that BB will not dominate it... therefore it's
Chris Lattner94108ab2001-07-06 16:58:22 +0000285 // dominator set size will be one less than BB's...
286 //
287 if (DS.getDominators(*I).size() == DomSetSize - 1) {
288 // We know that the immediate dominator should already have a node,
289 // because we are traversing the CFG in depth first order!
290 //
291 Node *IDomNode = Nodes[*I];
292 assert(IDomNode && "No node for IDOM?");
293
294 // Add a new tree node for this BasicBlock, and link it as a child of
295 // IDomNode
296 Nodes[BB] = IDomNode->addChild(new Node(BB, IDomNode));
297 break;
298 }
Chris Lattner17152292001-07-02 05:46:38 +0000299 }
300 }
301 }
302}
303
304
305
306//===----------------------------------------------------------------------===//
307// DominanceFrontier Implementation
308//===----------------------------------------------------------------------===//
309
Chris Lattner07a228d2002-05-06 19:32:07 +0000310AnalysisID DominanceFrontier::ID(AnalysisID::create<DominanceFrontier>(), true);
Chris Lattnerce6ef112002-07-26 18:40:14 +0000311AnalysisID PostDominanceFrontier::ID(AnalysisID::create<PostDominanceFrontier>(), true);
Chris Lattner93193f82002-01-31 00:42:27 +0000312
Chris Lattner1b7f7dc2002-04-28 16:21:30 +0000313const DominanceFrontier::DomSetType &
Chris Lattnerce6ef112002-07-26 18:40:14 +0000314DominanceFrontier::calculate(const DominatorTree &DT,
315 const DominatorTree::Node *Node) {
Chris Lattner17152292001-07-02 05:46:38 +0000316 // Loop over CFG successors to calculate DFlocal[Node]
Chris Lattnera298d272002-04-28 00:15:57 +0000317 BasicBlock *BB = Node->getNode();
Chris Lattner17152292001-07-02 05:46:38 +0000318 DomSetType &S = Frontiers[BB]; // The new set to fill in...
319
Chris Lattnera298d272002-04-28 00:15:57 +0000320 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB);
Chris Lattner455889a2002-02-12 22:39:50 +0000321 SI != SE; ++SI) {
Chris Lattner17152292001-07-02 05:46:38 +0000322 // Does Node immediately dominate this successor?
323 if (DT[*SI]->getIDom() != Node)
324 S.insert(*SI);
325 }
326
327 // At this point, S is DFlocal. Now we union in DFup's of our children...
328 // Loop through and visit the nodes that Node immediately dominates (Node's
329 // children in the IDomTree)
330 //
331 for (DominatorTree::Node::const_iterator NI = Node->begin(), NE = Node->end();
332 NI != NE; ++NI) {
333 DominatorTree::Node *IDominee = *NI;
Chris Lattnerce6ef112002-07-26 18:40:14 +0000334 const DomSetType &ChildDF = calculate(DT, IDominee);
Chris Lattner17152292001-07-02 05:46:38 +0000335
336 DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end();
337 for (; CDFI != CDFE; ++CDFI) {
338 if (!Node->dominates(DT[*CDFI]))
339 S.insert(*CDFI);
340 }
341 }
342
343 return S;
344}
Chris Lattner94108ab2001-07-06 16:58:22 +0000345
Chris Lattner1b7f7dc2002-04-28 16:21:30 +0000346const DominanceFrontier::DomSetType &
Chris Lattnerce6ef112002-07-26 18:40:14 +0000347PostDominanceFrontier::calculate(const PostDominatorTree &DT,
348 const DominatorTree::Node *Node) {
Chris Lattner94108ab2001-07-06 16:58:22 +0000349 // Loop over CFG successors to calculate DFlocal[Node]
Chris Lattnera298d272002-04-28 00:15:57 +0000350 BasicBlock *BB = Node->getNode();
Chris Lattner94108ab2001-07-06 16:58:22 +0000351 DomSetType &S = Frontiers[BB]; // The new set to fill in...
Chris Lattner384e5b12001-08-23 17:07:19 +0000352 if (!Root) return S;
Chris Lattner94108ab2001-07-06 16:58:22 +0000353
Chris Lattnera298d272002-04-28 00:15:57 +0000354 for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB);
Chris Lattner455889a2002-02-12 22:39:50 +0000355 SI != SE; ++SI) {
Chris Lattner94108ab2001-07-06 16:58:22 +0000356 // Does Node immediately dominate this predeccessor?
357 if (DT[*SI]->getIDom() != Node)
358 S.insert(*SI);
359 }
360
361 // At this point, S is DFlocal. Now we union in DFup's of our children...
362 // Loop through and visit the nodes that Node immediately dominates (Node's
363 // children in the IDomTree)
364 //
Chris Lattnerce6ef112002-07-26 18:40:14 +0000365 for (PostDominatorTree::Node::const_iterator
366 NI = Node->begin(), NE = Node->end(); NI != NE; ++NI) {
Chris Lattner94108ab2001-07-06 16:58:22 +0000367 DominatorTree::Node *IDominee = *NI;
Chris Lattnerce6ef112002-07-26 18:40:14 +0000368 const DomSetType &ChildDF = calculate(DT, IDominee);
Chris Lattner94108ab2001-07-06 16:58:22 +0000369
370 DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end();
371 for (; CDFI != CDFE; ++CDFI) {
372 if (!Node->dominates(DT[*CDFI]))
373 S.insert(*CDFI);
374 }
375 }
376
377 return S;
378}