blob: 2589c8577396e41182a3df673730e71f0cfd29be [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"
Chris Lattner706e61e2003-09-10 20:37:08 +000015#include "llvm/iTerminators.h"
Chris Lattner221d6882002-02-12 21:07:25 +000016#include "llvm/Support/CFG.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000017#include "Support/DepthFirstIterator.h"
Chris Lattnereb5230c2002-02-05 03:35:31 +000018#include "Support/SetOperations.h"
Chris Lattner697954c2002-01-20 22:54:45 +000019
Brian Gaeked0fde302003-11-11 22:41:34 +000020namespace llvm {
21
Chris Lattner94108ab2001-07-06 16:58:22 +000022//===----------------------------------------------------------------------===//
Chris Lattner4c9df7c2002-08-02 16:43:03 +000023// PostDominatorSet Implementation
Chris Lattner17152292001-07-02 05:46:38 +000024//===----------------------------------------------------------------------===//
25
Chris Lattner1e435162002-07-26 21:12:44 +000026static RegisterAnalysis<PostDominatorSet>
Chris Lattner17689df2002-07-30 16:27:52 +000027B("postdomset", "Post-Dominator Set Construction", true);
Chris Lattner94108ab2001-07-06 16:58:22 +000028
Chris Lattnerce6ef112002-07-26 18:40:14 +000029// Postdominator set construction. This converts the specified function to only
30// have a single exit node (return stmt), then calculates the post dominance
31// sets for the function.
Chris Lattner94108ab2001-07-06 16:58:22 +000032//
Chris Lattnerce6ef112002-07-26 18:40:14 +000033bool PostDominatorSet::runOnFunction(Function &F) {
34 Doms.clear(); // Reset from the last time we were run...
Chris Lattner94108ab2001-07-06 16:58:22 +000035
Chris Lattner706e61e2003-09-10 20:37:08 +000036 // Scan the function looking for the root nodes of the post-dominance
37 // relationships. These blocks end with return and unwind instructions.
38 // While we are iterating over the function, we also initialize all of the
39 // domsets to empty.
40 Roots.clear();
41 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
42 Doms[I]; // Initialize to empty
43
44 if (isa<ReturnInst>(I->getTerminator()) ||
45 isa<UnwindInst>(I->getTerminator()))
46 Roots.push_back(I);
Chris Lattner384e5b12001-08-23 17:07:19 +000047 }
Chris Lattner94108ab2001-07-06 16:58:22 +000048
Chris Lattner706e61e2003-09-10 20:37:08 +000049 // If there are no exit nodes for the function, postdomsets are all empty.
50 // This can happen if the function just contains an infinite loop, for
51 // example.
52 if (Roots.empty()) return false;
53
54 // If we have more than one root, we insert an artificial "null" exit, which
55 // has "virtual edges" to each of the real exit nodes.
56 if (Roots.size() > 1)
57 Doms[0].insert(0);
58
Chris Lattner94108ab2001-07-06 16:58:22 +000059 bool Changed;
60 do {
61 Changed = false;
62
Chris Lattner50b5d712003-10-13 16:36:06 +000063 std::set<BasicBlock*> Visited;
Chris Lattner94108ab2001-07-06 16:58:22 +000064 DomSetType WorkingSet;
Chris Lattner94108ab2001-07-06 16:58:22 +000065
Chris Lattner706e61e2003-09-10 20:37:08 +000066 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
Chris Lattner50b5d712003-10-13 16:36:06 +000067 for (idf_ext_iterator<BasicBlock*> It = idf_ext_begin(Roots[i], Visited),
68 E = idf_ext_end(Roots[i], Visited); It != E; ++It) {
Chris Lattner706e61e2003-09-10 20:37:08 +000069 BasicBlock *BB = *It;
70 succ_iterator SI = succ_begin(BB), SE = succ_end(BB);
71 if (SI != SE) { // Is there SOME successor?
72 // Loop until we get to a successor that has had it's dom set filled
73 // in at least once. We are guaranteed to have this because we are
74 // traversing the graph in DFO and have handled start nodes specially.
75 //
76 while (Doms[*SI].size() == 0) ++SI;
77 WorkingSet = Doms[*SI];
78
79 for (++SI; SI != SE; ++SI) { // Intersect all of the successor sets
80 DomSetType &SuccSet = Doms[*SI];
81 if (SuccSet.size())
82 set_intersect(WorkingSet, SuccSet);
83 }
84 } else {
85 // If this node has no successors, it must be one of the root nodes.
86 // We will already take care of the notion that the node
87 // post-dominates itself. The only thing we have to add is that if
88 // there are multiple root nodes, we want to insert a special "null"
89 // exit node which dominates the roots as well.
90 if (Roots.size() > 1)
91 WorkingSet.insert(0);
92 }
Chris Lattner94108ab2001-07-06 16:58:22 +000093
Chris Lattner706e61e2003-09-10 20:37:08 +000094 WorkingSet.insert(BB); // A block always dominates itself
95 DomSetType &BBSet = Doms[BB];
96 if (BBSet != WorkingSet) {
97 BBSet.swap(WorkingSet); // Constant time operation!
98 Changed = true; // The sets changed.
99 }
100 WorkingSet.clear(); // Clear out the set for next iteration
Chris Lattner94108ab2001-07-06 16:58:22 +0000101 }
Chris Lattner94108ab2001-07-06 16:58:22 +0000102 } while (Changed);
Chris Lattnerce6ef112002-07-26 18:40:14 +0000103 return false;
Chris Lattner17152292001-07-02 05:46:38 +0000104}
105
Chris Lattner17152292001-07-02 05:46:38 +0000106//===----------------------------------------------------------------------===//
Chris Lattner4c9df7c2002-08-02 16:43:03 +0000107// ImmediatePostDominators Implementation
Chris Lattner17152292001-07-02 05:46:38 +0000108//===----------------------------------------------------------------------===//
109
Chris Lattner1e435162002-07-26 21:12:44 +0000110static RegisterAnalysis<ImmediatePostDominators>
Chris Lattner17689df2002-07-30 16:27:52 +0000111D("postidom", "Immediate Post-Dominators Construction", true);
Chris Lattner93193f82002-01-31 00:42:27 +0000112
Chris Lattner17152292001-07-02 05:46:38 +0000113//===----------------------------------------------------------------------===//
Chris Lattner4c9df7c2002-08-02 16:43:03 +0000114// PostDominatorTree Implementation
Chris Lattner17152292001-07-02 05:46:38 +0000115//===----------------------------------------------------------------------===//
116
Chris Lattner1e435162002-07-26 21:12:44 +0000117static RegisterAnalysis<PostDominatorTree>
Chris Lattner17689df2002-07-30 16:27:52 +0000118F("postdomtree", "Post-Dominator Tree Construction", true);
Chris Lattner93193f82002-01-31 00:42:27 +0000119
Chris Lattnerce6ef112002-07-26 18:40:14 +0000120void PostDominatorTree::calculate(const PostDominatorSet &DS) {
Chris Lattner706e61e2003-09-10 20:37:08 +0000121 if (Roots.empty()) return;
122 BasicBlock *Root = Roots.size() == 1 ? Roots[0] : 0;
Chris Lattnerce6ef112002-07-26 18:40:14 +0000123
Chris Lattner706e61e2003-09-10 20:37:08 +0000124 Nodes[Root] = RootNode = new Node(Root, 0); // Add a node for the root...
125
126 // Iterate over all nodes in depth first order...
127 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
128 for (idf_iterator<BasicBlock*> I = idf_begin(Roots[i]),
129 E = idf_end(Roots[i]); I != E; ++I) {
Chris Lattnera298d272002-04-28 00:15:57 +0000130 BasicBlock *BB = *I;
Chris Lattner94108ab2001-07-06 16:58:22 +0000131 const DominatorSet::DomSetType &Dominators = DS.getDominators(BB);
132 unsigned DomSetSize = Dominators.size();
133 if (DomSetSize == 1) continue; // Root node... IDom = null
Chris Lattner706e61e2003-09-10 20:37:08 +0000134
135 // If we have already computed the immediate dominator for this node,
136 // don't revisit. This can happen due to nodes reachable from multiple
137 // roots, but which the idf_iterator doesn't know about.
138 if (Nodes.find(BB) != Nodes.end()) continue;
139
Chris Lattner3ff43872001-09-28 22:56:31 +0000140 // Loop over all dominators of this node. This corresponds to looping
141 // over nodes in the dominator chain, looking for a node whose dominator
142 // set is equal to the current nodes, except that the current node does
143 // not exist in it. This means that it is one level higher in the dom
144 // chain than the current node, and it is our idom! We know that we have
145 // already added a DominatorTree node for our idom, because the idom must
146 // be a predecessor in the depth first order that we are iterating through
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000147 // the function.
Chris Lattner94108ab2001-07-06 16:58:22 +0000148 //
149 DominatorSet::DomSetType::const_iterator I = Dominators.begin();
150 DominatorSet::DomSetType::const_iterator End = Dominators.end();
151 for (; I != End; ++I) { // Iterate over dominators...
Chris Lattner706e61e2003-09-10 20:37:08 +0000152 // All of our dominators should form a chain, where the number
153 // of elements in the dominator set indicates what level the
154 // node is at in the chain. We want the node immediately
155 // above us, so it will have an identical dominator set,
156 // except that BB will not dominate it... therefore it's
157 // dominator set size will be one less than BB's...
158 //
159 if (DS.getDominators(*I).size() == DomSetSize - 1) {
160 // We know that the immediate dominator should already have a node,
161 // because we are traversing the CFG in depth first order!
162 //
163 Node *IDomNode = Nodes[*I];
164 assert(IDomNode && "No node for IDOM?");
Chris Lattner94108ab2001-07-06 16:58:22 +0000165
Chris Lattner706e61e2003-09-10 20:37:08 +0000166 // Add a new tree node for this BasicBlock, and link it as a child of
167 // IDomNode
168 Nodes[BB] = IDomNode->addChild(new Node(BB, IDomNode));
169 break;
170 }
Chris Lattner17152292001-07-02 05:46:38 +0000171 }
172 }
Chris Lattner17152292001-07-02 05:46:38 +0000173}
174
Chris Lattner17152292001-07-02 05:46:38 +0000175//===----------------------------------------------------------------------===//
Chris Lattner4c9df7c2002-08-02 16:43:03 +0000176// PostDominanceFrontier Implementation
Chris Lattner17152292001-07-02 05:46:38 +0000177//===----------------------------------------------------------------------===//
178
Chris Lattner1e435162002-07-26 21:12:44 +0000179static RegisterAnalysis<PostDominanceFrontier>
Chris Lattner17689df2002-07-30 16:27:52 +0000180H("postdomfrontier", "Post-Dominance Frontier Construction", true);
Chris Lattner93193f82002-01-31 00:42:27 +0000181
Chris Lattner1b7f7dc2002-04-28 16:21:30 +0000182const DominanceFrontier::DomSetType &
Chris Lattnerce6ef112002-07-26 18:40:14 +0000183PostDominanceFrontier::calculate(const PostDominatorTree &DT,
184 const DominatorTree::Node *Node) {
Chris Lattner94108ab2001-07-06 16:58:22 +0000185 // Loop over CFG successors to calculate DFlocal[Node]
Chris Lattnerc444a422003-09-11 16:26:13 +0000186 BasicBlock *BB = Node->getBlock();
Chris Lattner94108ab2001-07-06 16:58:22 +0000187 DomSetType &S = Frontiers[BB]; // The new set to fill in...
Chris Lattner706e61e2003-09-10 20:37:08 +0000188 if (getRoots().empty()) return S;
Chris Lattner94108ab2001-07-06 16:58:22 +0000189
Chris Lattner706e61e2003-09-10 20:37:08 +0000190 if (BB)
191 for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB);
192 SI != SE; ++SI)
Misha Brukman2f2d0652003-09-11 18:14:24 +0000193 // Does Node immediately dominate this predecessor?
Chris Lattner706e61e2003-09-10 20:37:08 +0000194 if (DT[*SI]->getIDom() != Node)
195 S.insert(*SI);
Chris Lattner94108ab2001-07-06 16:58:22 +0000196
197 // At this point, S is DFlocal. Now we union in DFup's of our children...
198 // Loop through and visit the nodes that Node immediately dominates (Node's
199 // children in the IDomTree)
200 //
Chris Lattnerce6ef112002-07-26 18:40:14 +0000201 for (PostDominatorTree::Node::const_iterator
202 NI = Node->begin(), NE = Node->end(); NI != NE; ++NI) {
Chris Lattner94108ab2001-07-06 16:58:22 +0000203 DominatorTree::Node *IDominee = *NI;
Chris Lattnerce6ef112002-07-26 18:40:14 +0000204 const DomSetType &ChildDF = calculate(DT, IDominee);
Chris Lattner94108ab2001-07-06 16:58:22 +0000205
206 DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end();
207 for (; CDFI != CDFE; ++CDFI) {
208 if (!Node->dominates(DT[*CDFI]))
209 S.insert(*CDFI);
210 }
211 }
212
213 return S;
214}
Chris Lattnera69fd902002-08-21 23:43:50 +0000215
216// stub - a dummy function to make linking work ok.
217void PostDominanceFrontier::stub() {
218}
Brian Gaeked0fde302003-11-11 22:41:34 +0000219
220} // End llvm namespace