blob: 9b35d16b9047fc01938a36b23bd36992cc001fed [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 Lattner1e435162002-07-26 21:12:44 +000021static RegisterAnalysis<DominatorSet>
22A("domset", "Dominator Set Construction");
23static RegisterAnalysis<PostDominatorSet>
24B("postdomset", "Post-Dominator Set Construction");
25
Chris Lattner07a228d2002-05-06 19:32:07 +000026AnalysisID DominatorSet::ID(AnalysisID::create<DominatorSet>(), true);
Chris Lattnerce6ef112002-07-26 18:40:14 +000027AnalysisID PostDominatorSet::ID(AnalysisID::create<PostDominatorSet>(), true);
Chris Lattner94108ab2001-07-06 16:58:22 +000028
Chris Lattneref704a22002-05-13 22:03:16 +000029// dominates - Return true if A dominates B. This performs the special checks
30// neccesary if A and B are in the same basic block.
31//
Chris Lattnerce6ef112002-07-26 18:40:14 +000032bool DominatorSetBase::dominates(Instruction *A, Instruction *B) const {
Chris Lattneref704a22002-05-13 22:03:16 +000033 BasicBlock *BBA = A->getParent(), *BBB = B->getParent();
34 if (BBA != BBB) return dominates(BBA, BBB);
35
36 // Loop through the basic block until we find A or B.
37 BasicBlock::iterator I = BBA->begin();
Chris Lattner7e708292002-06-25 16:13:24 +000038 for (; &*I != A && &*I != B; ++I) /*empty*/;
Chris Lattneref704a22002-05-13 22:03:16 +000039
40 // A dominates B if it is found first in the basic block...
Chris Lattner7e708292002-06-25 16:13:24 +000041 return &*I == A;
Chris Lattneref704a22002-05-13 22:03:16 +000042}
Chris Lattner93193f82002-01-31 00:42:27 +000043
Chris Lattnerce6ef112002-07-26 18:40:14 +000044// runOnFunction - This method calculates the forward dominator sets for the
45// specified function.
Chris Lattner94108ab2001-07-06 16:58:22 +000046//
Chris Lattnerce6ef112002-07-26 18:40:14 +000047bool DominatorSet::runOnFunction(Function &F) {
48 Doms.clear(); // Reset from the last time we were run...
Chris Lattner7e708292002-06-25 16:13:24 +000049 Root = &F.getEntryNode();
Chris Lattner455889a2002-02-12 22:39:50 +000050 assert(pred_begin(Root) == pred_end(Root) &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000051 "Root node has predecessors in function!");
Chris Lattnerff5a8c42001-11-26 18:52:02 +000052
Chris Lattner17152292001-07-02 05:46:38 +000053 bool Changed;
54 do {
55 Changed = false;
56
57 DomSetType WorkingSet;
Chris Lattner7e708292002-06-25 16:13:24 +000058 df_iterator<Function*> It = df_begin(&F), End = df_end(&F);
Chris Lattner17152292001-07-02 05:46:38 +000059 for ( ; It != End; ++It) {
Chris Lattnera298d272002-04-28 00:15:57 +000060 BasicBlock *BB = *It;
61 pred_iterator PI = pred_begin(BB), PEnd = pred_end(BB);
Chris Lattner17152292001-07-02 05:46:38 +000062 if (PI != PEnd) { // Is there SOME predecessor?
63 // Loop until we get to a predecessor that has had it's dom set filled
64 // in at least once. We are guaranteed to have this because we are
65 // traversing the graph in DFO and have handled start nodes specially.
66 //
67 while (Doms[*PI].size() == 0) ++PI;
68 WorkingSet = Doms[*PI];
69
70 for (++PI; PI != PEnd; ++PI) { // Intersect all of the predecessor sets
71 DomSetType &PredSet = Doms[*PI];
72 if (PredSet.size())
73 set_intersect(WorkingSet, PredSet);
74 }
75 }
76
77 WorkingSet.insert(BB); // A block always dominates itself
78 DomSetType &BBSet = Doms[BB];
79 if (BBSet != WorkingSet) {
80 BBSet.swap(WorkingSet); // Constant time operation!
81 Changed = true; // The sets changed.
82 }
83 WorkingSet.clear(); // Clear out the set for next iteration
84 }
85 } while (Changed);
Chris Lattnerce6ef112002-07-26 18:40:14 +000086 return false;
Chris Lattner94108ab2001-07-06 16:58:22 +000087}
Chris Lattner17152292001-07-02 05:46:38 +000088
Chris Lattnerce6ef112002-07-26 18:40:14 +000089
90// Postdominator set construction. This converts the specified function to only
91// have a single exit node (return stmt), then calculates the post dominance
92// sets for the function.
Chris Lattner94108ab2001-07-06 16:58:22 +000093//
Chris Lattnerce6ef112002-07-26 18:40:14 +000094bool PostDominatorSet::runOnFunction(Function &F) {
95 Doms.clear(); // Reset from the last time we were run...
Chris Lattner93193f82002-01-31 00:42:27 +000096 // Since we require that the unify all exit nodes pass has been run, we know
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000097 // that there can be at most one return instruction in the function left.
Chris Lattner93193f82002-01-31 00:42:27 +000098 // Get it.
99 //
Chris Lattner483e14e2002-04-27 07:27:19 +0000100 Root = getAnalysis<UnifyFunctionExitNodes>().getExitNode();
Chris Lattner94108ab2001-07-06 16:58:22 +0000101
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000102 if (Root == 0) { // No exit node for the function? Postdomsets are all empty
Chris Lattner7e708292002-06-25 16:13:24 +0000103 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
104 Doms[FI] = DomSetType();
Chris Lattnerce6ef112002-07-26 18:40:14 +0000105 return false;
Chris Lattner384e5b12001-08-23 17:07:19 +0000106 }
Chris Lattner94108ab2001-07-06 16:58:22 +0000107
108 bool Changed;
109 do {
110 Changed = false;
111
112 set<const BasicBlock*> Visited;
113 DomSetType WorkingSet;
Chris Lattner93193f82002-01-31 00:42:27 +0000114 idf_iterator<BasicBlock*> It = idf_begin(Root), End = idf_end(Root);
Chris Lattner94108ab2001-07-06 16:58:22 +0000115 for ( ; It != End; ++It) {
Chris Lattnera298d272002-04-28 00:15:57 +0000116 BasicBlock *BB = *It;
117 succ_iterator PI = succ_begin(BB), PEnd = succ_end(BB);
Chris Lattner94108ab2001-07-06 16:58:22 +0000118 if (PI != PEnd) { // Is there SOME predecessor?
119 // Loop until we get to a successor that has had it's dom set filled
120 // in at least once. We are guaranteed to have this because we are
121 // traversing the graph in DFO and have handled start nodes specially.
122 //
123 while (Doms[*PI].size() == 0) ++PI;
124 WorkingSet = Doms[*PI];
125
126 for (++PI; PI != PEnd; ++PI) { // Intersect all of the successor sets
127 DomSetType &PredSet = Doms[*PI];
128 if (PredSet.size())
129 set_intersect(WorkingSet, PredSet);
130 }
131 }
132
133 WorkingSet.insert(BB); // A block always dominates itself
134 DomSetType &BBSet = Doms[BB];
135 if (BBSet != WorkingSet) {
136 BBSet.swap(WorkingSet); // Constant time operation!
137 Changed = true; // The sets changed.
138 }
139 WorkingSet.clear(); // Clear out the set for next iteration
140 }
141 } while (Changed);
Chris Lattnerce6ef112002-07-26 18:40:14 +0000142 return false;
Chris Lattner17152292001-07-02 05:46:38 +0000143}
144
Chris Lattnerce6ef112002-07-26 18:40:14 +0000145// getAnalysisUsage - This obviously provides a post-dominator set, but it also
146// requires the UnifyFunctionExitNodes pass.
Chris Lattner93193f82002-01-31 00:42:27 +0000147//
Chris Lattnerce6ef112002-07-26 18:40:14 +0000148void PostDominatorSet::getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerf57b8452002-04-27 06:56:12 +0000149 AU.setPreservesAll();
Chris Lattnerce6ef112002-07-26 18:40:14 +0000150 AU.addProvided(ID);
151 AU.addRequired(UnifyFunctionExitNodes::ID);
Chris Lattner93193f82002-01-31 00:42:27 +0000152}
153
Chris Lattner17152292001-07-02 05:46:38 +0000154
155//===----------------------------------------------------------------------===//
156// ImmediateDominators Implementation
157//===----------------------------------------------------------------------===//
158
Chris Lattner1e435162002-07-26 21:12:44 +0000159static RegisterAnalysis<ImmediateDominators>
160C("idom", "Immediate Dominators Construction");
161static RegisterAnalysis<ImmediatePostDominators>
162D("postidom", "Immediate Post-Dominators Construction");
163
Chris Lattner07a228d2002-05-06 19:32:07 +0000164AnalysisID ImmediateDominators::ID(AnalysisID::create<ImmediateDominators>(), true);
Chris Lattnerce6ef112002-07-26 18:40:14 +0000165AnalysisID ImmediatePostDominators::ID(AnalysisID::create<ImmediatePostDominators>(), true);
Chris Lattner93193f82002-01-31 00:42:27 +0000166
Chris Lattner17152292001-07-02 05:46:38 +0000167// calcIDoms - Calculate the immediate dominator mapping, given a set of
168// dominators for every basic block.
Chris Lattnerce6ef112002-07-26 18:40:14 +0000169void ImmediateDominatorsBase::calcIDoms(const DominatorSetBase &DS) {
Chris Lattner17152292001-07-02 05:46:38 +0000170 // Loop over all of the nodes that have dominators... figuring out the IDOM
171 // for each node...
172 //
173 for (DominatorSet::const_iterator DI = DS.begin(), DEnd = DS.end();
174 DI != DEnd; ++DI) {
Chris Lattnera298d272002-04-28 00:15:57 +0000175 BasicBlock *BB = DI->first;
Chris Lattner17152292001-07-02 05:46:38 +0000176 const DominatorSet::DomSetType &Dominators = DI->second;
177 unsigned DomSetSize = Dominators.size();
178 if (DomSetSize == 1) continue; // Root node... IDom = null
179
180 // Loop over all dominators of this node. This corresponds to looping over
181 // nodes in the dominator chain, looking for a node whose dominator set is
182 // equal to the current nodes, except that the current node does not exist
183 // in it. This means that it is one level higher in the dom chain than the
184 // current node, and it is our idom!
185 //
186 DominatorSet::DomSetType::const_iterator I = Dominators.begin();
187 DominatorSet::DomSetType::const_iterator End = Dominators.end();
188 for (; I != End; ++I) { // Iterate over dominators...
189 // All of our dominators should form a chain, where the number of elements
190 // in the dominator set indicates what level the node is at in the chain.
191 // We want the node immediately above us, so it will have an identical
192 // dominator set, except that BB will not dominate it... therefore it's
193 // dominator set size will be one less than BB's...
194 //
195 if (DS.getDominators(*I).size() == DomSetSize - 1) {
196 IDoms[BB] = *I;
197 break;
198 }
199 }
200 }
201}
202
203
204//===----------------------------------------------------------------------===//
205// DominatorTree Implementation
206//===----------------------------------------------------------------------===//
207
Chris Lattner1e435162002-07-26 21:12:44 +0000208static RegisterAnalysis<DominatorTree>
209E("domtree", "Dominator Tree Construction");
210static RegisterAnalysis<PostDominatorTree>
211F("postdomtree", "Post-Dominator Tree Construction");
212
Chris Lattner07a228d2002-05-06 19:32:07 +0000213AnalysisID DominatorTree::ID(AnalysisID::create<DominatorTree>(), true);
Chris Lattnerce6ef112002-07-26 18:40:14 +0000214AnalysisID PostDominatorTree::ID(AnalysisID::create<PostDominatorTree>(), true);
Chris Lattner93193f82002-01-31 00:42:27 +0000215
Chris Lattnerce6ef112002-07-26 18:40:14 +0000216// DominatorTreeBase::reset - Free all of the tree node memory.
Chris Lattner17152292001-07-02 05:46:38 +0000217//
Chris Lattnerce6ef112002-07-26 18:40:14 +0000218void DominatorTreeBase::reset() {
Chris Lattner17152292001-07-02 05:46:38 +0000219 for (NodeMapType::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I)
220 delete I->second;
Chris Lattner93193f82002-01-31 00:42:27 +0000221 Nodes.clear();
Chris Lattner17152292001-07-02 05:46:38 +0000222}
223
224
Chris Lattner1b7f7dc2002-04-28 16:21:30 +0000225void DominatorTree::calculate(const DominatorSet &DS) {
Chris Lattner17152292001-07-02 05:46:38 +0000226 Nodes[Root] = new Node(Root, 0); // Add a node for the root...
227
Chris Lattnerce6ef112002-07-26 18:40:14 +0000228 // Iterate over all nodes in depth first order...
229 for (df_iterator<BasicBlock*> I = df_begin(Root), E = df_end(Root);
230 I != E; ++I) {
231 BasicBlock *BB = *I;
232 const DominatorSet::DomSetType &Dominators = DS.getDominators(BB);
233 unsigned DomSetSize = Dominators.size();
234 if (DomSetSize == 1) continue; // Root node... IDom = null
Chris Lattner94108ab2001-07-06 16:58:22 +0000235
Chris Lattnerce6ef112002-07-26 18:40:14 +0000236 // Loop over all dominators of this node. This corresponds to looping over
237 // nodes in the dominator chain, looking for a node whose dominator set is
238 // equal to the current nodes, except that the current node does not exist
239 // in it. This means that it is one level higher in the dom chain than the
240 // current node, and it is our idom! We know that we have already added
241 // a DominatorTree node for our idom, because the idom must be a
242 // predecessor in the depth first order that we are iterating through the
243 // function.
244 //
245 DominatorSet::DomSetType::const_iterator I = Dominators.begin();
246 DominatorSet::DomSetType::const_iterator End = Dominators.end();
247 for (; I != End; ++I) { // Iterate over dominators...
248 // All of our dominators should form a chain, where the number of
249 // elements in the dominator set indicates what level the node is at in
250 // the chain. We want the node immediately above us, so it will have
251 // an identical dominator set, except that BB will not dominate it...
252 // therefore it's dominator set size will be one less than BB's...
Chris Lattner17152292001-07-02 05:46:38 +0000253 //
Chris Lattnerce6ef112002-07-26 18:40:14 +0000254 if (DS.getDominators(*I).size() == DomSetSize - 1) {
255 // We know that the immediate dominator should already have a node,
256 // because we are traversing the CFG in depth first order!
257 //
258 Node *IDomNode = Nodes[*I];
259 assert(IDomNode && "No node for IDOM?");
260
261 // Add a new tree node for this BasicBlock, and link it as a child of
262 // IDomNode
263 Nodes[BB] = IDomNode->addChild(new Node(BB, IDomNode));
264 break;
Chris Lattner94108ab2001-07-06 16:58:22 +0000265 }
266 }
Chris Lattnerce6ef112002-07-26 18:40:14 +0000267 }
268}
269
270
271void PostDominatorTree::calculate(const PostDominatorSet &DS) {
272 Nodes[Root] = new Node(Root, 0); // Add a node for the root...
273
274 if (Root) {
Chris Lattner94108ab2001-07-06 16:58:22 +0000275 // Iterate over all nodes in depth first order...
Chris Lattner93193f82002-01-31 00:42:27 +0000276 for (idf_iterator<BasicBlock*> I = idf_begin(Root), E = idf_end(Root);
Chris Lattner3ff43872001-09-28 22:56:31 +0000277 I != E; ++I) {
Chris Lattnera298d272002-04-28 00:15:57 +0000278 BasicBlock *BB = *I;
Chris Lattner94108ab2001-07-06 16:58:22 +0000279 const DominatorSet::DomSetType &Dominators = DS.getDominators(BB);
280 unsigned DomSetSize = Dominators.size();
281 if (DomSetSize == 1) continue; // Root node... IDom = null
282
Chris Lattner3ff43872001-09-28 22:56:31 +0000283 // Loop over all dominators of this node. This corresponds to looping
284 // over nodes in the dominator chain, looking for a node whose dominator
285 // set is equal to the current nodes, except that the current node does
286 // not exist in it. This means that it is one level higher in the dom
287 // chain than the current node, and it is our idom! We know that we have
288 // already added a DominatorTree node for our idom, because the idom must
289 // be a predecessor in the depth first order that we are iterating through
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000290 // the function.
Chris Lattner94108ab2001-07-06 16:58:22 +0000291 //
292 DominatorSet::DomSetType::const_iterator I = Dominators.begin();
293 DominatorSet::DomSetType::const_iterator End = Dominators.end();
294 for (; I != End; ++I) { // Iterate over dominators...
Chris Lattner93193f82002-01-31 00:42:27 +0000295 // All of our dominators should form a chain, where the number
296 // of elements in the dominator set indicates what level the
297 // node is at in the chain. We want the node immediately
298 // above us, so it will have an identical dominator set,
299 // except that BB will not dominate it... therefore it's
Chris Lattner94108ab2001-07-06 16:58:22 +0000300 // dominator set size will be one less than BB's...
301 //
302 if (DS.getDominators(*I).size() == DomSetSize - 1) {
303 // We know that the immediate dominator should already have a node,
304 // because we are traversing the CFG in depth first order!
305 //
306 Node *IDomNode = Nodes[*I];
307 assert(IDomNode && "No node for IDOM?");
308
309 // Add a new tree node for this BasicBlock, and link it as a child of
310 // IDomNode
311 Nodes[BB] = IDomNode->addChild(new Node(BB, IDomNode));
312 break;
313 }
Chris Lattner17152292001-07-02 05:46:38 +0000314 }
315 }
316 }
317}
318
319
320
321//===----------------------------------------------------------------------===//
322// DominanceFrontier Implementation
323//===----------------------------------------------------------------------===//
324
Chris Lattner1e435162002-07-26 21:12:44 +0000325static RegisterAnalysis<DominanceFrontier>
326G("domfrontier", "Dominance Frontier Construction");
327static RegisterAnalysis<PostDominanceFrontier>
328H("postdomfrontier", "Post-Dominance Frontier Construction");
329
Chris Lattner07a228d2002-05-06 19:32:07 +0000330AnalysisID DominanceFrontier::ID(AnalysisID::create<DominanceFrontier>(), true);
Chris Lattnerce6ef112002-07-26 18:40:14 +0000331AnalysisID PostDominanceFrontier::ID(AnalysisID::create<PostDominanceFrontier>(), true);
Chris Lattner93193f82002-01-31 00:42:27 +0000332
Chris Lattner1b7f7dc2002-04-28 16:21:30 +0000333const DominanceFrontier::DomSetType &
Chris Lattnerce6ef112002-07-26 18:40:14 +0000334DominanceFrontier::calculate(const DominatorTree &DT,
335 const DominatorTree::Node *Node) {
Chris Lattner17152292001-07-02 05:46:38 +0000336 // Loop over CFG successors to calculate DFlocal[Node]
Chris Lattnera298d272002-04-28 00:15:57 +0000337 BasicBlock *BB = Node->getNode();
Chris Lattner17152292001-07-02 05:46:38 +0000338 DomSetType &S = Frontiers[BB]; // The new set to fill in...
339
Chris Lattnera298d272002-04-28 00:15:57 +0000340 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB);
Chris Lattner455889a2002-02-12 22:39:50 +0000341 SI != SE; ++SI) {
Chris Lattner17152292001-07-02 05:46:38 +0000342 // Does Node immediately dominate this successor?
343 if (DT[*SI]->getIDom() != Node)
344 S.insert(*SI);
345 }
346
347 // At this point, S is DFlocal. Now we union in DFup's of our children...
348 // Loop through and visit the nodes that Node immediately dominates (Node's
349 // children in the IDomTree)
350 //
351 for (DominatorTree::Node::const_iterator NI = Node->begin(), NE = Node->end();
352 NI != NE; ++NI) {
353 DominatorTree::Node *IDominee = *NI;
Chris Lattnerce6ef112002-07-26 18:40:14 +0000354 const DomSetType &ChildDF = calculate(DT, IDominee);
Chris Lattner17152292001-07-02 05:46:38 +0000355
356 DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end();
357 for (; CDFI != CDFE; ++CDFI) {
358 if (!Node->dominates(DT[*CDFI]))
359 S.insert(*CDFI);
360 }
361 }
362
363 return S;
364}
Chris Lattner94108ab2001-07-06 16:58:22 +0000365
Chris Lattner1b7f7dc2002-04-28 16:21:30 +0000366const DominanceFrontier::DomSetType &
Chris Lattnerce6ef112002-07-26 18:40:14 +0000367PostDominanceFrontier::calculate(const PostDominatorTree &DT,
368 const DominatorTree::Node *Node) {
Chris Lattner94108ab2001-07-06 16:58:22 +0000369 // Loop over CFG successors to calculate DFlocal[Node]
Chris Lattnera298d272002-04-28 00:15:57 +0000370 BasicBlock *BB = Node->getNode();
Chris Lattner94108ab2001-07-06 16:58:22 +0000371 DomSetType &S = Frontiers[BB]; // The new set to fill in...
Chris Lattner384e5b12001-08-23 17:07:19 +0000372 if (!Root) return S;
Chris Lattner94108ab2001-07-06 16:58:22 +0000373
Chris Lattnera298d272002-04-28 00:15:57 +0000374 for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB);
Chris Lattner455889a2002-02-12 22:39:50 +0000375 SI != SE; ++SI) {
Chris Lattner94108ab2001-07-06 16:58:22 +0000376 // Does Node immediately dominate this predeccessor?
377 if (DT[*SI]->getIDom() != Node)
378 S.insert(*SI);
379 }
380
381 // At this point, S is DFlocal. Now we union in DFup's of our children...
382 // Loop through and visit the nodes that Node immediately dominates (Node's
383 // children in the IDomTree)
384 //
Chris Lattnerce6ef112002-07-26 18:40:14 +0000385 for (PostDominatorTree::Node::const_iterator
386 NI = Node->begin(), NE = Node->end(); NI != NE; ++NI) {
Chris Lattner94108ab2001-07-06 16:58:22 +0000387 DominatorTree::Node *IDominee = *NI;
Chris Lattnerce6ef112002-07-26 18:40:14 +0000388 const DomSetType &ChildDF = calculate(DT, IDominee);
Chris Lattner94108ab2001-07-06 16:58:22 +0000389
390 DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end();
391 for (; CDFI != CDFE; ++CDFI) {
392 if (!Node->dominates(DT[*CDFI]))
393 S.insert(*CDFI);
394 }
395 }
396
397 return S;
398}