blob: 7dbeb35c253a37f87d275a4384c0bc4f40efc62d [file] [log] [blame]
Chris Lattner4c9df7c2002-08-02 16:43:03 +00001//===- PostDominators.cpp - Post-Dominator Calculation --------------------===//
Chris Lattner17152292001-07-02 05:46:38 +00002//
Chris Lattner4c9df7c2002-08-02 16:43:03 +00003// This file implements the post-dominator construction algorithms.
Chris Lattner17152292001-07-02 05:46:38 +00004//
5//===----------------------------------------------------------------------===//
6
Chris Lattnera69fd902002-08-21 23:43:50 +00007#include "llvm/Analysis/PostDominators.h"
Chris Lattner706e61e2003-09-10 20:37:08 +00008#include "llvm/iTerminators.h"
Chris Lattner221d6882002-02-12 21:07:25 +00009#include "llvm/Support/CFG.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000010#include "Support/DepthFirstIterator.h"
Chris Lattnereb5230c2002-02-05 03:35:31 +000011#include "Support/SetOperations.h"
Chris Lattner697954c2002-01-20 22:54:45 +000012
Chris Lattner94108ab2001-07-06 16:58:22 +000013//===----------------------------------------------------------------------===//
Chris Lattner4c9df7c2002-08-02 16:43:03 +000014// PostDominatorSet Implementation
Chris Lattner17152292001-07-02 05:46:38 +000015//===----------------------------------------------------------------------===//
16
Chris Lattner1e435162002-07-26 21:12:44 +000017static RegisterAnalysis<PostDominatorSet>
Chris Lattner17689df2002-07-30 16:27:52 +000018B("postdomset", "Post-Dominator Set Construction", true);
Chris Lattner94108ab2001-07-06 16:58:22 +000019
Chris Lattnerce6ef112002-07-26 18:40:14 +000020// Postdominator set construction. This converts the specified function to only
21// have a single exit node (return stmt), then calculates the post dominance
22// sets for the function.
Chris Lattner94108ab2001-07-06 16:58:22 +000023//
Chris Lattnerce6ef112002-07-26 18:40:14 +000024bool PostDominatorSet::runOnFunction(Function &F) {
25 Doms.clear(); // Reset from the last time we were run...
Chris Lattner94108ab2001-07-06 16:58:22 +000026
Chris Lattner706e61e2003-09-10 20:37:08 +000027 // Scan the function looking for the root nodes of the post-dominance
28 // relationships. These blocks end with return and unwind instructions.
29 // While we are iterating over the function, we also initialize all of the
30 // domsets to empty.
31 Roots.clear();
32 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
33 Doms[I]; // Initialize to empty
34
35 if (isa<ReturnInst>(I->getTerminator()) ||
36 isa<UnwindInst>(I->getTerminator()))
37 Roots.push_back(I);
Chris Lattner384e5b12001-08-23 17:07:19 +000038 }
Chris Lattner94108ab2001-07-06 16:58:22 +000039
Chris Lattner706e61e2003-09-10 20:37:08 +000040 // If there are no exit nodes for the function, postdomsets are all empty.
41 // This can happen if the function just contains an infinite loop, for
42 // example.
43 if (Roots.empty()) return false;
44
45 // If we have more than one root, we insert an artificial "null" exit, which
46 // has "virtual edges" to each of the real exit nodes.
47 if (Roots.size() > 1)
48 Doms[0].insert(0);
49
Chris Lattner94108ab2001-07-06 16:58:22 +000050 bool Changed;
51 do {
52 Changed = false;
53
Chris Lattner2446b0a2003-09-10 16:08:03 +000054 std::set<const BasicBlock*> Visited;
Chris Lattner94108ab2001-07-06 16:58:22 +000055 DomSetType WorkingSet;
Chris Lattner94108ab2001-07-06 16:58:22 +000056
Chris Lattner706e61e2003-09-10 20:37:08 +000057 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
58 for (idf_iterator<BasicBlock*> It = idf_begin(Roots[i]),
59 E = idf_end(Roots[i]); It != E; ++It) {
60 BasicBlock *BB = *It;
61 succ_iterator SI = succ_begin(BB), SE = succ_end(BB);
62 if (SI != SE) { // Is there SOME successor?
63 // Loop until we get to a successor 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[*SI].size() == 0) ++SI;
68 WorkingSet = Doms[*SI];
69
70 for (++SI; SI != SE; ++SI) { // Intersect all of the successor sets
71 DomSetType &SuccSet = Doms[*SI];
72 if (SuccSet.size())
73 set_intersect(WorkingSet, SuccSet);
74 }
75 } else {
76 // If this node has no successors, it must be one of the root nodes.
77 // We will already take care of the notion that the node
78 // post-dominates itself. The only thing we have to add is that if
79 // there are multiple root nodes, we want to insert a special "null"
80 // exit node which dominates the roots as well.
81 if (Roots.size() > 1)
82 WorkingSet.insert(0);
83 }
Chris Lattner94108ab2001-07-06 16:58:22 +000084
Chris Lattner706e61e2003-09-10 20:37:08 +000085 WorkingSet.insert(BB); // A block always dominates itself
86 DomSetType &BBSet = Doms[BB];
87 if (BBSet != WorkingSet) {
88 BBSet.swap(WorkingSet); // Constant time operation!
89 Changed = true; // The sets changed.
90 }
91 WorkingSet.clear(); // Clear out the set for next iteration
Chris Lattner94108ab2001-07-06 16:58:22 +000092 }
Chris Lattner94108ab2001-07-06 16:58:22 +000093 } while (Changed);
Chris Lattnerce6ef112002-07-26 18:40:14 +000094 return false;
Chris Lattner17152292001-07-02 05:46:38 +000095}
96
Chris Lattner17152292001-07-02 05:46:38 +000097//===----------------------------------------------------------------------===//
Chris Lattner4c9df7c2002-08-02 16:43:03 +000098// ImmediatePostDominators Implementation
Chris Lattner17152292001-07-02 05:46:38 +000099//===----------------------------------------------------------------------===//
100
Chris Lattner1e435162002-07-26 21:12:44 +0000101static RegisterAnalysis<ImmediatePostDominators>
Chris Lattner17689df2002-07-30 16:27:52 +0000102D("postidom", "Immediate Post-Dominators Construction", true);
Chris Lattner93193f82002-01-31 00:42:27 +0000103
Chris Lattner17152292001-07-02 05:46:38 +0000104//===----------------------------------------------------------------------===//
Chris Lattner4c9df7c2002-08-02 16:43:03 +0000105// PostDominatorTree Implementation
Chris Lattner17152292001-07-02 05:46:38 +0000106//===----------------------------------------------------------------------===//
107
Chris Lattner1e435162002-07-26 21:12:44 +0000108static RegisterAnalysis<PostDominatorTree>
Chris Lattner17689df2002-07-30 16:27:52 +0000109F("postdomtree", "Post-Dominator Tree Construction", true);
Chris Lattner93193f82002-01-31 00:42:27 +0000110
Chris Lattnerce6ef112002-07-26 18:40:14 +0000111void PostDominatorTree::calculate(const PostDominatorSet &DS) {
Chris Lattner706e61e2003-09-10 20:37:08 +0000112 if (Roots.empty()) return;
113 BasicBlock *Root = Roots.size() == 1 ? Roots[0] : 0;
Chris Lattnerce6ef112002-07-26 18:40:14 +0000114
Chris Lattner706e61e2003-09-10 20:37:08 +0000115 Nodes[Root] = RootNode = new Node(Root, 0); // Add a node for the root...
116
117 // Iterate over all nodes in depth first order...
118 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
119 for (idf_iterator<BasicBlock*> I = idf_begin(Roots[i]),
120 E = idf_end(Roots[i]); I != E; ++I) {
Chris Lattnera298d272002-04-28 00:15:57 +0000121 BasicBlock *BB = *I;
Chris Lattner94108ab2001-07-06 16:58:22 +0000122 const DominatorSet::DomSetType &Dominators = DS.getDominators(BB);
123 unsigned DomSetSize = Dominators.size();
124 if (DomSetSize == 1) continue; // Root node... IDom = null
Chris Lattner706e61e2003-09-10 20:37:08 +0000125
126 // If we have already computed the immediate dominator for this node,
127 // don't revisit. This can happen due to nodes reachable from multiple
128 // roots, but which the idf_iterator doesn't know about.
129 if (Nodes.find(BB) != Nodes.end()) continue;
130
Chris Lattner3ff43872001-09-28 22:56:31 +0000131 // Loop over all dominators of this node. This corresponds to looping
132 // over nodes in the dominator chain, looking for a node whose dominator
133 // set is equal to the current nodes, except that the current node does
134 // not exist in it. This means that it is one level higher in the dom
135 // chain than the current node, and it is our idom! We know that we have
136 // already added a DominatorTree node for our idom, because the idom must
137 // be a predecessor in the depth first order that we are iterating through
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000138 // the function.
Chris Lattner94108ab2001-07-06 16:58:22 +0000139 //
140 DominatorSet::DomSetType::const_iterator I = Dominators.begin();
141 DominatorSet::DomSetType::const_iterator End = Dominators.end();
142 for (; I != End; ++I) { // Iterate over dominators...
Chris Lattner706e61e2003-09-10 20:37:08 +0000143 // All of our dominators should form a chain, where the number
144 // of elements in the dominator set indicates what level the
145 // node is at in the chain. We want the node immediately
146 // above us, so it will have an identical dominator set,
147 // except that BB will not dominate it... therefore it's
148 // dominator set size will be one less than BB's...
149 //
150 if (DS.getDominators(*I).size() == DomSetSize - 1) {
151 // We know that the immediate dominator should already have a node,
152 // because we are traversing the CFG in depth first order!
153 //
154 Node *IDomNode = Nodes[*I];
155 assert(IDomNode && "No node for IDOM?");
Chris Lattner94108ab2001-07-06 16:58:22 +0000156
Chris Lattner706e61e2003-09-10 20:37:08 +0000157 // Add a new tree node for this BasicBlock, and link it as a child of
158 // IDomNode
159 Nodes[BB] = IDomNode->addChild(new Node(BB, IDomNode));
160 break;
161 }
Chris Lattner17152292001-07-02 05:46:38 +0000162 }
163 }
Chris Lattner17152292001-07-02 05:46:38 +0000164}
165
Chris Lattner17152292001-07-02 05:46:38 +0000166//===----------------------------------------------------------------------===//
Chris Lattner4c9df7c2002-08-02 16:43:03 +0000167// PostDominanceFrontier Implementation
Chris Lattner17152292001-07-02 05:46:38 +0000168//===----------------------------------------------------------------------===//
169
Chris Lattner1e435162002-07-26 21:12:44 +0000170static RegisterAnalysis<PostDominanceFrontier>
Chris Lattner17689df2002-07-30 16:27:52 +0000171H("postdomfrontier", "Post-Dominance Frontier Construction", true);
Chris Lattner93193f82002-01-31 00:42:27 +0000172
Chris Lattner1b7f7dc2002-04-28 16:21:30 +0000173const DominanceFrontier::DomSetType &
Chris Lattnerce6ef112002-07-26 18:40:14 +0000174PostDominanceFrontier::calculate(const PostDominatorTree &DT,
175 const DominatorTree::Node *Node) {
Chris Lattner94108ab2001-07-06 16:58:22 +0000176 // Loop over CFG successors to calculate DFlocal[Node]
Chris Lattnerc444a422003-09-11 16:26:13 +0000177 BasicBlock *BB = Node->getBlock();
Chris Lattner94108ab2001-07-06 16:58:22 +0000178 DomSetType &S = Frontiers[BB]; // The new set to fill in...
Chris Lattner706e61e2003-09-10 20:37:08 +0000179 if (getRoots().empty()) return S;
Chris Lattner94108ab2001-07-06 16:58:22 +0000180
Chris Lattner706e61e2003-09-10 20:37:08 +0000181 if (BB)
182 for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB);
183 SI != SE; ++SI)
184 // Does Node immediately dominate this predeccessor?
185 if (DT[*SI]->getIDom() != Node)
186 S.insert(*SI);
Chris Lattner94108ab2001-07-06 16:58:22 +0000187
188 // At this point, S is DFlocal. Now we union in DFup's of our children...
189 // Loop through and visit the nodes that Node immediately dominates (Node's
190 // children in the IDomTree)
191 //
Chris Lattnerce6ef112002-07-26 18:40:14 +0000192 for (PostDominatorTree::Node::const_iterator
193 NI = Node->begin(), NE = Node->end(); NI != NE; ++NI) {
Chris Lattner94108ab2001-07-06 16:58:22 +0000194 DominatorTree::Node *IDominee = *NI;
Chris Lattnerce6ef112002-07-26 18:40:14 +0000195 const DomSetType &ChildDF = calculate(DT, IDominee);
Chris Lattner94108ab2001-07-06 16:58:22 +0000196
197 DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end();
198 for (; CDFI != CDFE; ++CDFI) {
199 if (!Node->dominates(DT[*CDFI]))
200 S.insert(*CDFI);
201 }
202 }
203
204 return S;
205}
Chris Lattnera69fd902002-08-21 23:43:50 +0000206
207// stub - a dummy function to make linking work ok.
208void PostDominanceFrontier::stub() {
209}