blob: e5908c496b4c95b2e8c43defb525d48a57161102 [file] [log] [blame]
Chris Lattner4c9df7c2002-08-02 16:43:03 +00001//===- PostDominators.cpp - Post-Dominator Calculation --------------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
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
43 if (isa<ReturnInst>(I->getTerminator()) ||
44 isa<UnwindInst>(I->getTerminator()))
45 Roots.push_back(I);
Chris Lattner384e5b12001-08-23 17:07:19 +000046 }
Chris Lattner94108ab2001-07-06 16:58:22 +000047
Chris Lattner706e61e2003-09-10 20:37:08 +000048 // If there are no exit nodes for the function, postdomsets are all empty.
49 // This can happen if the function just contains an infinite loop, for
50 // example.
51 if (Roots.empty()) return false;
52
53 // If we have more than one root, we insert an artificial "null" exit, which
54 // has "virtual edges" to each of the real exit nodes.
55 if (Roots.size() > 1)
56 Doms[0].insert(0);
57
Chris Lattner94108ab2001-07-06 16:58:22 +000058 bool Changed;
59 do {
60 Changed = false;
61
Chris Lattner50b5d712003-10-13 16:36:06 +000062 std::set<BasicBlock*> Visited;
Chris Lattner94108ab2001-07-06 16:58:22 +000063 DomSetType WorkingSet;
Chris Lattner94108ab2001-07-06 16:58:22 +000064
Chris Lattner706e61e2003-09-10 20:37:08 +000065 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
Chris Lattner50b5d712003-10-13 16:36:06 +000066 for (idf_ext_iterator<BasicBlock*> It = idf_ext_begin(Roots[i], Visited),
67 E = idf_ext_end(Roots[i], Visited); It != E; ++It) {
Chris Lattner706e61e2003-09-10 20:37:08 +000068 BasicBlock *BB = *It;
69 succ_iterator SI = succ_begin(BB), SE = succ_end(BB);
70 if (SI != SE) { // Is there SOME successor?
71 // Loop until we get to a successor that has had it's dom set filled
72 // in at least once. We are guaranteed to have this because we are
73 // traversing the graph in DFO and have handled start nodes specially.
74 //
75 while (Doms[*SI].size() == 0) ++SI;
76 WorkingSet = Doms[*SI];
77
78 for (++SI; SI != SE; ++SI) { // Intersect all of the successor sets
79 DomSetType &SuccSet = Doms[*SI];
80 if (SuccSet.size())
81 set_intersect(WorkingSet, SuccSet);
82 }
83 } else {
84 // If this node has no successors, it must be one of the root nodes.
85 // We will already take care of the notion that the node
86 // post-dominates itself. The only thing we have to add is that if
87 // there are multiple root nodes, we want to insert a special "null"
88 // exit node which dominates the roots as well.
89 if (Roots.size() > 1)
90 WorkingSet.insert(0);
91 }
Chris Lattner94108ab2001-07-06 16:58:22 +000092
Chris Lattner706e61e2003-09-10 20:37:08 +000093 WorkingSet.insert(BB); // A block always dominates itself
94 DomSetType &BBSet = Doms[BB];
95 if (BBSet != WorkingSet) {
96 BBSet.swap(WorkingSet); // Constant time operation!
97 Changed = true; // The sets changed.
98 }
99 WorkingSet.clear(); // Clear out the set for next iteration
Chris Lattner94108ab2001-07-06 16:58:22 +0000100 }
Chris Lattner94108ab2001-07-06 16:58:22 +0000101 } while (Changed);
Chris Lattnerce6ef112002-07-26 18:40:14 +0000102 return false;
Chris Lattner17152292001-07-02 05:46:38 +0000103}
104
Chris Lattner17152292001-07-02 05:46:38 +0000105//===----------------------------------------------------------------------===//
Chris Lattner4c9df7c2002-08-02 16:43:03 +0000106// ImmediatePostDominators Implementation
Chris Lattner17152292001-07-02 05:46:38 +0000107//===----------------------------------------------------------------------===//
108
Chris Lattner1e435162002-07-26 21:12:44 +0000109static RegisterAnalysis<ImmediatePostDominators>
Chris Lattner17689df2002-07-30 16:27:52 +0000110D("postidom", "Immediate Post-Dominators Construction", true);
Chris Lattner93193f82002-01-31 00:42:27 +0000111
Chris Lattnercd7c2872003-12-07 00:35:42 +0000112
113// calcIDoms - Calculate the immediate dominator mapping, given a set of
114// dominators for every basic block.
115void ImmediatePostDominators::calcIDoms(const DominatorSetBase &DS) {
116 // Loop over all of the nodes that have dominators... figuring out the IDOM
117 // for each node...
118 //
119 for (DominatorSet::const_iterator DI = DS.begin(), DEnd = DS.end();
120 DI != DEnd; ++DI) {
121 BasicBlock *BB = DI->first;
122 const DominatorSet::DomSetType &Dominators = DI->second;
123 unsigned DomSetSize = Dominators.size();
124 if (DomSetSize == 1) continue; // Root node... IDom = null
125
126 // Loop over all dominators of this node. This corresponds to looping over
127 // nodes in the dominator chain, looking for a node whose dominator set is
128 // equal to the current nodes, except that the current node does not exist
129 // in it. This means that it is one level higher in the dom chain than the
130 // current node, and it is our idom!
131 //
132 DominatorSet::DomSetType::const_iterator I = Dominators.begin();
133 DominatorSet::DomSetType::const_iterator End = Dominators.end();
134 for (; I != End; ++I) { // Iterate over dominators...
135 // All of our dominators should form a chain, where the number of elements
136 // in the dominator set indicates what level the node is at in the chain.
137 // We want the node immediately above us, so it will have an identical
138 // dominator set, except that BB will not dominate it... therefore it's
139 // dominator set size will be one less than BB's...
140 //
141 if (DS.getDominators(*I).size() == DomSetSize - 1) {
142 IDoms[BB] = *I;
143 break;
144 }
145 }
146 }
147}
148
Chris Lattner17152292001-07-02 05:46:38 +0000149//===----------------------------------------------------------------------===//
Chris Lattner4c9df7c2002-08-02 16:43:03 +0000150// PostDominatorTree Implementation
Chris Lattner17152292001-07-02 05:46:38 +0000151//===----------------------------------------------------------------------===//
152
Chris Lattner1e435162002-07-26 21:12:44 +0000153static RegisterAnalysis<PostDominatorTree>
Chris Lattner17689df2002-07-30 16:27:52 +0000154F("postdomtree", "Post-Dominator Tree Construction", true);
Chris Lattner93193f82002-01-31 00:42:27 +0000155
Chris Lattnerce6ef112002-07-26 18:40:14 +0000156void PostDominatorTree::calculate(const PostDominatorSet &DS) {
Chris Lattner706e61e2003-09-10 20:37:08 +0000157 if (Roots.empty()) return;
158 BasicBlock *Root = Roots.size() == 1 ? Roots[0] : 0;
Chris Lattnerce6ef112002-07-26 18:40:14 +0000159
Chris Lattner706e61e2003-09-10 20:37:08 +0000160 Nodes[Root] = RootNode = new Node(Root, 0); // Add a node for the root...
161
162 // Iterate over all nodes in depth first order...
163 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
164 for (idf_iterator<BasicBlock*> I = idf_begin(Roots[i]),
165 E = idf_end(Roots[i]); I != E; ++I) {
Chris Lattnera298d272002-04-28 00:15:57 +0000166 BasicBlock *BB = *I;
Chris Lattner94108ab2001-07-06 16:58:22 +0000167 const DominatorSet::DomSetType &Dominators = DS.getDominators(BB);
168 unsigned DomSetSize = Dominators.size();
169 if (DomSetSize == 1) continue; // Root node... IDom = null
Chris Lattner706e61e2003-09-10 20:37:08 +0000170
171 // If we have already computed the immediate dominator for this node,
172 // don't revisit. This can happen due to nodes reachable from multiple
173 // roots, but which the idf_iterator doesn't know about.
174 if (Nodes.find(BB) != Nodes.end()) continue;
175
Chris Lattner3ff43872001-09-28 22:56:31 +0000176 // Loop over all dominators of this node. This corresponds to looping
177 // over nodes in the dominator chain, looking for a node whose dominator
178 // set is equal to the current nodes, except that the current node does
179 // not exist in it. This means that it is one level higher in the dom
180 // chain than the current node, and it is our idom! We know that we have
181 // already added a DominatorTree node for our idom, because the idom must
182 // be a predecessor in the depth first order that we are iterating through
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000183 // the function.
Chris Lattner94108ab2001-07-06 16:58:22 +0000184 //
185 DominatorSet::DomSetType::const_iterator I = Dominators.begin();
186 DominatorSet::DomSetType::const_iterator End = Dominators.end();
187 for (; I != End; ++I) { // Iterate over dominators...
Chris Lattner706e61e2003-09-10 20:37:08 +0000188 // All of our dominators should form a chain, where the number
189 // of elements in the dominator set indicates what level the
190 // node is at in the chain. We want the node immediately
191 // above us, so it will have an identical dominator set,
192 // 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 // We know that the immediate dominator should already have a node,
197 // because we are traversing the CFG in depth first order!
198 //
199 Node *IDomNode = Nodes[*I];
200 assert(IDomNode && "No node for IDOM?");
Chris Lattner94108ab2001-07-06 16:58:22 +0000201
Chris Lattner706e61e2003-09-10 20:37:08 +0000202 // Add a new tree node for this BasicBlock, and link it as a child of
203 // IDomNode
204 Nodes[BB] = IDomNode->addChild(new Node(BB, IDomNode));
205 break;
206 }
Chris Lattner17152292001-07-02 05:46:38 +0000207 }
208 }
Chris Lattner17152292001-07-02 05:46:38 +0000209}
210
Chris Lattner17152292001-07-02 05:46:38 +0000211//===----------------------------------------------------------------------===//
Chris Lattner4c9df7c2002-08-02 16:43:03 +0000212// PostDominanceFrontier Implementation
Chris Lattner17152292001-07-02 05:46:38 +0000213//===----------------------------------------------------------------------===//
214
Chris Lattner1e435162002-07-26 21:12:44 +0000215static RegisterAnalysis<PostDominanceFrontier>
Chris Lattner17689df2002-07-30 16:27:52 +0000216H("postdomfrontier", "Post-Dominance Frontier Construction", true);
Chris Lattner93193f82002-01-31 00:42:27 +0000217
Chris Lattner1b7f7dc2002-04-28 16:21:30 +0000218const DominanceFrontier::DomSetType &
Chris Lattnerce6ef112002-07-26 18:40:14 +0000219PostDominanceFrontier::calculate(const PostDominatorTree &DT,
220 const DominatorTree::Node *Node) {
Chris Lattner94108ab2001-07-06 16:58:22 +0000221 // Loop over CFG successors to calculate DFlocal[Node]
Chris Lattnerc444a422003-09-11 16:26:13 +0000222 BasicBlock *BB = Node->getBlock();
Chris Lattner94108ab2001-07-06 16:58:22 +0000223 DomSetType &S = Frontiers[BB]; // The new set to fill in...
Chris Lattner706e61e2003-09-10 20:37:08 +0000224 if (getRoots().empty()) return S;
Chris Lattner94108ab2001-07-06 16:58:22 +0000225
Chris Lattner706e61e2003-09-10 20:37:08 +0000226 if (BB)
227 for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB);
228 SI != SE; ++SI)
Misha Brukman2f2d0652003-09-11 18:14:24 +0000229 // Does Node immediately dominate this predecessor?
Chris Lattner706e61e2003-09-10 20:37:08 +0000230 if (DT[*SI]->getIDom() != Node)
231 S.insert(*SI);
Chris Lattner94108ab2001-07-06 16:58:22 +0000232
233 // At this point, S is DFlocal. Now we union in DFup's of our children...
234 // Loop through and visit the nodes that Node immediately dominates (Node's
235 // children in the IDomTree)
236 //
Chris Lattnerce6ef112002-07-26 18:40:14 +0000237 for (PostDominatorTree::Node::const_iterator
238 NI = Node->begin(), NE = Node->end(); NI != NE; ++NI) {
Chris Lattner94108ab2001-07-06 16:58:22 +0000239 DominatorTree::Node *IDominee = *NI;
Chris Lattnerce6ef112002-07-26 18:40:14 +0000240 const DomSetType &ChildDF = calculate(DT, IDominee);
Chris Lattner94108ab2001-07-06 16:58:22 +0000241
242 DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end();
243 for (; CDFI != CDFE; ++CDFI) {
244 if (!Node->dominates(DT[*CDFI]))
245 S.insert(*CDFI);
246 }
247 }
248
249 return S;
250}
Chris Lattnera69fd902002-08-21 23:43:50 +0000251
252// stub - a dummy function to make linking work ok.
253void PostDominanceFrontier::stub() {
254}
Brian Gaeked0fde302003-11-11 22:41:34 +0000255