1 //===- DominatorSet.cpp - Dominator Set Calculation --------------*- C++ -*--=//
3 // This file provides a simple class to calculate the dominator set of a method.
5 //===----------------------------------------------------------------------===//
7 #include "llvm/Analysis/Dominators.h"
8 #include "llvm/Analysis/SimplifyCFG.h" // To get cfg::UnifyAllExitNodes
10 #include "llvm/Support/STLExtras.h"
13 //===----------------------------------------------------------------------===//
15 //===----------------------------------------------------------------------===//
17 // set_intersect - Identical to set_intersection, except that it works on
18 // set<>'s and is nicer to use. Functionally, this iterates through S1,
19 // removing elements that are not contained in S2.
21 template <class Ty, class Ty2>
22 void set_intersect(set<Ty> &S1, const set<Ty2> &S2) {
23 for (typename set<Ty>::iterator I = S1.begin(); I != S1.end();) {
26 if (!S2.count(E)) S1.erase(E); // Erase element if not in S2
30 //===----------------------------------------------------------------------===//
31 // DominatorBase Implementation
32 //===----------------------------------------------------------------------===//
34 bool cfg::DominatorBase::isPostDominator() const {
35 // Root can be null if there is no exit node from the CFG and is postdom set
36 return Root == 0 || Root != Root->getParent()->front();
40 //===----------------------------------------------------------------------===//
41 // DominatorSet Implementation
42 //===----------------------------------------------------------------------===//
44 // DominatorSet ctor - Build either the dominator set or the post-dominator
45 // set for a method...
47 cfg::DominatorSet::DominatorSet(const Method *M) : DominatorBase(M->front()) {
48 calcForwardDominatorSet(M);
51 // calcForwardDominatorSet - This method calculates the forward dominator sets
52 // for the specified method.
54 void cfg::DominatorSet::calcForwardDominatorSet(const Method *M) {
55 assert(Root && M && "Can't build dominator set of null method!");
56 assert(Root->use_size() == 0 && "Root node has predecessors in method!");
61 DomSetType WorkingSet;
62 df_const_iterator It = df_begin(M), End = df_end(M);
63 for ( ; It != End; ++It) {
64 const BasicBlock *BB = *It;
65 pred_const_iterator PI = pred_begin(BB), PEnd = pred_end(BB);
66 if (PI != PEnd) { // Is there SOME predecessor?
67 // Loop until we get to a predecessor that has had it's dom set filled
68 // in at least once. We are guaranteed to have this because we are
69 // traversing the graph in DFO and have handled start nodes specially.
71 while (Doms[*PI].size() == 0) ++PI;
72 WorkingSet = Doms[*PI];
74 for (++PI; PI != PEnd; ++PI) { // Intersect all of the predecessor sets
75 DomSetType &PredSet = Doms[*PI];
77 set_intersect(WorkingSet, PredSet);
81 WorkingSet.insert(BB); // A block always dominates itself
82 DomSetType &BBSet = Doms[BB];
83 if (BBSet != WorkingSet) {
84 BBSet.swap(WorkingSet); // Constant time operation!
85 Changed = true; // The sets changed.
87 WorkingSet.clear(); // Clear out the set for next iteration
92 // Postdominator set constructor. This ctor converts the specified method to
93 // only have a single exit node (return stmt), then calculates the post
94 // dominance sets for the method.
96 cfg::DominatorSet::DominatorSet(Method *M, bool PostDomSet)
97 : DominatorBase(M->front()) {
98 if (!PostDomSet) { calcForwardDominatorSet(M); return; }
100 Root = cfg::UnifyAllExitNodes(M);
101 if (Root == 0) { // No exit node for the method? Postdomsets are all empty
102 for (Method::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI)
103 Doms[*MI] = DomSetType();
111 set<const BasicBlock*> Visited;
112 DomSetType WorkingSet;
113 idf_const_iterator It = idf_begin(Root), End = idf_end(Root);
114 for ( ; It != End; ++It) {
115 const BasicBlock *BB = *It;
116 succ_const_iterator PI = succ_begin(BB), PEnd = succ_end(BB);
117 if (PI != PEnd) { // Is there SOME predecessor?
118 // Loop until we get to a successor that has had it's dom set filled
119 // in at least once. We are guaranteed to have this because we are
120 // traversing the graph in DFO and have handled start nodes specially.
122 while (Doms[*PI].size() == 0) ++PI;
123 WorkingSet = Doms[*PI];
125 for (++PI; PI != PEnd; ++PI) { // Intersect all of the successor sets
126 DomSetType &PredSet = Doms[*PI];
128 set_intersect(WorkingSet, PredSet);
132 WorkingSet.insert(BB); // A block always dominates itself
133 DomSetType &BBSet = Doms[BB];
134 if (BBSet != WorkingSet) {
135 BBSet.swap(WorkingSet); // Constant time operation!
136 Changed = true; // The sets changed.
138 WorkingSet.clear(); // Clear out the set for next iteration
144 //===----------------------------------------------------------------------===//
145 // ImmediateDominators Implementation
146 //===----------------------------------------------------------------------===//
148 // calcIDoms - Calculate the immediate dominator mapping, given a set of
149 // dominators for every basic block.
150 void cfg::ImmediateDominators::calcIDoms(const DominatorSet &DS) {
151 // Loop over all of the nodes that have dominators... figuring out the IDOM
154 for (DominatorSet::const_iterator DI = DS.begin(), DEnd = DS.end();
156 const BasicBlock *BB = DI->first;
157 const DominatorSet::DomSetType &Dominators = DI->second;
158 unsigned DomSetSize = Dominators.size();
159 if (DomSetSize == 1) continue; // Root node... IDom = null
161 // Loop over all dominators of this node. This corresponds to looping over
162 // nodes in the dominator chain, looking for a node whose dominator set is
163 // equal to the current nodes, except that the current node does not exist
164 // in it. This means that it is one level higher in the dom chain than the
165 // current node, and it is our idom!
167 DominatorSet::DomSetType::const_iterator I = Dominators.begin();
168 DominatorSet::DomSetType::const_iterator End = Dominators.end();
169 for (; I != End; ++I) { // Iterate over dominators...
170 // All of our dominators should form a chain, where the number of elements
171 // in the dominator set indicates what level the node is at in the chain.
172 // We want the node immediately above us, so it will have an identical
173 // dominator set, except that BB will not dominate it... therefore it's
174 // dominator set size will be one less than BB's...
176 if (DS.getDominators(*I).size() == DomSetSize - 1) {
185 //===----------------------------------------------------------------------===//
186 // DominatorTree Implementation
187 //===----------------------------------------------------------------------===//
189 // DominatorTree dtor - Free all of the tree node memory.
191 cfg::DominatorTree::~DominatorTree() {
192 for (NodeMapType::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I)
197 cfg::DominatorTree::DominatorTree(const ImmediateDominators &IDoms)
198 : DominatorBase(IDoms.getRoot()) {
199 const Method *M = Root->getParent();
201 Nodes[Root] = new Node(Root, 0); // Add a node for the root...
203 // Iterate over all nodes in depth first order...
204 for (df_const_iterator I = df_begin(M), E = df_end(M); I != E; ++I) {
205 const BasicBlock *BB = *I, *IDom = IDoms[*I];
207 if (IDom != 0) { // Ignore the root node and other nasty nodes
208 // We know that the immediate dominator should already have a node,
209 // because we are traversing the CFG in depth first order!
211 assert(Nodes[IDom] && "No node for IDOM?");
212 Node *IDomNode = Nodes[IDom];
214 // Add a new tree node for this BasicBlock, and link it as a child of
216 Nodes[BB] = IDomNode->addChild(new Node(BB, IDomNode));
221 void cfg::DominatorTree::calculate(const DominatorSet &DS) {
222 Nodes[Root] = new Node(Root, 0); // Add a node for the root...
224 if (!isPostDominator()) {
225 // Iterate over all nodes in depth first order...
226 for (df_const_iterator I = df_begin(Root), E = df_end(Root); I != E; ++I) {
227 const BasicBlock *BB = *I;
228 const DominatorSet::DomSetType &Dominators = DS.getDominators(BB);
229 unsigned DomSetSize = Dominators.size();
230 if (DomSetSize == 1) continue; // Root node... IDom = null
232 // Loop over all dominators of this node. This corresponds to looping over
233 // nodes in the dominator chain, looking for a node whose dominator set is
234 // equal to the current nodes, except that the current node does not exist
235 // in it. This means that it is one level higher in the dom chain than the
236 // current node, and it is our idom! We know that we have already added
237 // a DominatorTree node for our idom, because the idom must be a
238 // predecessor in the depth first order that we are iterating through the
241 DominatorSet::DomSetType::const_iterator I = Dominators.begin();
242 DominatorSet::DomSetType::const_iterator End = Dominators.end();
243 for (; I != End; ++I) { // Iterate over dominators...
244 // All of our dominators should form a chain, where the number of elements
245 // in the dominator set indicates what level the node is at in the chain.
246 // We want the node immediately above us, so it will have an identical
247 // dominator set, except that BB will not dominate it... therefore it's
248 // dominator set size will be one less than BB's...
250 if (DS.getDominators(*I).size() == DomSetSize - 1) {
251 // We know that the immediate dominator should already have a node,
252 // because we are traversing the CFG in depth first order!
254 Node *IDomNode = Nodes[*I];
255 assert(IDomNode && "No node for IDOM?");
257 // Add a new tree node for this BasicBlock, and link it as a child of
259 Nodes[BB] = IDomNode->addChild(new Node(BB, IDomNode));
265 // Iterate over all nodes in depth first order...
266 for (idf_const_iterator I = idf_begin(Root), E = idf_end(Root); I != E; ++I) {
267 const BasicBlock *BB = *I;
268 const DominatorSet::DomSetType &Dominators = DS.getDominators(BB);
269 unsigned DomSetSize = Dominators.size();
270 if (DomSetSize == 1) continue; // Root node... IDom = null
272 // Loop over all dominators of this node. This corresponds to looping over
273 // nodes in the dominator chain, looking for a node whose dominator set is
274 // equal to the current nodes, except that the current node does not exist
275 // in it. This means that it is one level higher in the dom chain than the
276 // current node, and it is our idom! We know that we have already added
277 // a DominatorTree node for our idom, because the idom must be a
278 // predecessor in the depth first order that we are iterating through the
281 DominatorSet::DomSetType::const_iterator I = Dominators.begin();
282 DominatorSet::DomSetType::const_iterator End = Dominators.end();
283 for (; I != End; ++I) { // Iterate over dominators...
284 // All of our dominators should form a chain, where the number of elements
285 // in the dominator set indicates what level the node is at in the chain.
286 // We want the node immediately above us, so it will have an identical
287 // dominator set, except that BB will not dominate it... therefore it's
288 // dominator set size will be one less than BB's...
290 if (DS.getDominators(*I).size() == DomSetSize - 1) {
291 // We know that the immediate dominator should already have a node,
292 // because we are traversing the CFG in depth first order!
294 Node *IDomNode = Nodes[*I];
295 assert(IDomNode && "No node for IDOM?");
297 // Add a new tree node for this BasicBlock, and link it as a child of
299 Nodes[BB] = IDomNode->addChild(new Node(BB, IDomNode));
309 //===----------------------------------------------------------------------===//
310 // DominanceFrontier Implementation
311 //===----------------------------------------------------------------------===//
313 const cfg::DominanceFrontier::DomSetType &
314 cfg::DominanceFrontier::calcDomFrontier(const DominatorTree &DT,
315 const DominatorTree::Node *Node) {
316 // Loop over CFG successors to calculate DFlocal[Node]
317 const BasicBlock *BB = Node->getNode();
318 DomSetType &S = Frontiers[BB]; // The new set to fill in...
320 for (succ_const_iterator SI = succ_begin(BB), SE = succ_end(BB);
322 // Does Node immediately dominate this successor?
323 if (DT[*SI]->getIDom() != Node)
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)
331 for (DominatorTree::Node::const_iterator NI = Node->begin(), NE = Node->end();
333 DominatorTree::Node *IDominee = *NI;
334 const DomSetType &ChildDF = calcDomFrontier(DT, IDominee);
336 DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end();
337 for (; CDFI != CDFE; ++CDFI) {
338 if (!Node->dominates(DT[*CDFI]))
346 const cfg::DominanceFrontier::DomSetType &
347 cfg::DominanceFrontier::calcPostDomFrontier(const DominatorTree &DT,
348 const DominatorTree::Node *Node) {
349 // Loop over CFG successors to calculate DFlocal[Node]
350 const BasicBlock *BB = Node->getNode();
351 DomSetType &S = Frontiers[BB]; // The new set to fill in...
354 for (pred_const_iterator SI = pred_begin(BB), SE = pred_end(BB);
356 // Does Node immediately dominate this predeccessor?
357 if (DT[*SI]->getIDom() != Node)
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)
365 for (DominatorTree::Node::const_iterator NI = Node->begin(), NE = Node->end();
367 DominatorTree::Node *IDominee = *NI;
368 const DomSetType &ChildDF = calcPostDomFrontier(DT, IDominee);
370 DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end();
371 for (; CDFI != CDFE; ++CDFI) {
372 if (!Node->dominates(DT[*CDFI]))