blob: 2e49854249216474e345482d791c7b4517a96fcb [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
7#include "llvm/Analysis/Dominators.h"
Chris Lattnerfc514f42002-05-07 19:18:48 +00008#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.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 +000012using std::set;
13
Chris Lattner94108ab2001-07-06 16:58:22 +000014//===----------------------------------------------------------------------===//
Chris Lattner4c9df7c2002-08-02 16:43:03 +000015// PostDominatorSet Implementation
Chris Lattner17152292001-07-02 05:46:38 +000016//===----------------------------------------------------------------------===//
17
Chris Lattner1e435162002-07-26 21:12:44 +000018static RegisterAnalysis<PostDominatorSet>
Chris Lattner17689df2002-07-30 16:27:52 +000019B("postdomset", "Post-Dominator Set Construction", true);
Chris Lattnera59cbb22002-07-27 01:12:17 +000020AnalysisID PostDominatorSet::ID = B;
Chris Lattner94108ab2001-07-06 16:58:22 +000021
Chris Lattnerce6ef112002-07-26 18:40:14 +000022// Postdominator set construction. This converts the specified function to only
23// have a single exit node (return stmt), then calculates the post dominance
24// sets for the function.
Chris Lattner94108ab2001-07-06 16:58:22 +000025//
Chris Lattnerce6ef112002-07-26 18:40:14 +000026bool PostDominatorSet::runOnFunction(Function &F) {
27 Doms.clear(); // Reset from the last time we were run...
Chris Lattner93193f82002-01-31 00:42:27 +000028 // Since we require that the unify all exit nodes pass has been run, we know
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000029 // that there can be at most one return instruction in the function left.
Chris Lattner93193f82002-01-31 00:42:27 +000030 // Get it.
31 //
Chris Lattner483e14e2002-04-27 07:27:19 +000032 Root = getAnalysis<UnifyFunctionExitNodes>().getExitNode();
Chris Lattner94108ab2001-07-06 16:58:22 +000033
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000034 if (Root == 0) { // No exit node for the function? Postdomsets are all empty
Chris Lattner7e708292002-06-25 16:13:24 +000035 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
36 Doms[FI] = DomSetType();
Chris Lattnerce6ef112002-07-26 18:40:14 +000037 return false;
Chris Lattner384e5b12001-08-23 17:07:19 +000038 }
Chris Lattner94108ab2001-07-06 16:58:22 +000039
40 bool Changed;
41 do {
42 Changed = false;
43
44 set<const BasicBlock*> Visited;
45 DomSetType WorkingSet;
Chris Lattner93193f82002-01-31 00:42:27 +000046 idf_iterator<BasicBlock*> It = idf_begin(Root), End = idf_end(Root);
Chris Lattner94108ab2001-07-06 16:58:22 +000047 for ( ; It != End; ++It) {
Chris Lattnera298d272002-04-28 00:15:57 +000048 BasicBlock *BB = *It;
49 succ_iterator PI = succ_begin(BB), PEnd = succ_end(BB);
Chris Lattner94108ab2001-07-06 16:58:22 +000050 if (PI != PEnd) { // Is there SOME predecessor?
51 // Loop until we get to a successor that has had it's dom set filled
52 // in at least once. We are guaranteed to have this because we are
53 // traversing the graph in DFO and have handled start nodes specially.
54 //
55 while (Doms[*PI].size() == 0) ++PI;
56 WorkingSet = Doms[*PI];
57
58 for (++PI; PI != PEnd; ++PI) { // Intersect all of the successor sets
59 DomSetType &PredSet = Doms[*PI];
60 if (PredSet.size())
61 set_intersect(WorkingSet, PredSet);
62 }
63 }
64
65 WorkingSet.insert(BB); // A block always dominates itself
66 DomSetType &BBSet = Doms[BB];
67 if (BBSet != WorkingSet) {
68 BBSet.swap(WorkingSet); // Constant time operation!
69 Changed = true; // The sets changed.
70 }
71 WorkingSet.clear(); // Clear out the set for next iteration
72 }
73 } while (Changed);
Chris Lattnerce6ef112002-07-26 18:40:14 +000074 return false;
Chris Lattner17152292001-07-02 05:46:38 +000075}
76
Chris Lattnerce6ef112002-07-26 18:40:14 +000077// getAnalysisUsage - This obviously provides a post-dominator set, but it also
78// requires the UnifyFunctionExitNodes pass.
Chris Lattner93193f82002-01-31 00:42:27 +000079//
Chris Lattnerce6ef112002-07-26 18:40:14 +000080void PostDominatorSet::getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerf57b8452002-04-27 06:56:12 +000081 AU.setPreservesAll();
Chris Lattnerdd5b4952002-08-08 19:01:28 +000082 AU.addRequired<UnifyFunctionExitNodes>();
Chris Lattner93193f82002-01-31 00:42:27 +000083}
84
Chris Lattner17152292001-07-02 05:46:38 +000085//===----------------------------------------------------------------------===//
Chris Lattner4c9df7c2002-08-02 16:43:03 +000086// ImmediatePostDominators Implementation
Chris Lattner17152292001-07-02 05:46:38 +000087//===----------------------------------------------------------------------===//
88
Chris Lattner1e435162002-07-26 21:12:44 +000089static RegisterAnalysis<ImmediatePostDominators>
Chris Lattner17689df2002-07-30 16:27:52 +000090D("postidom", "Immediate Post-Dominators Construction", true);
Chris Lattnera59cbb22002-07-27 01:12:17 +000091AnalysisID ImmediatePostDominators::ID = D;
Chris Lattner93193f82002-01-31 00:42:27 +000092
Chris Lattner17152292001-07-02 05:46:38 +000093//===----------------------------------------------------------------------===//
Chris Lattner4c9df7c2002-08-02 16:43:03 +000094// PostDominatorTree Implementation
Chris Lattner17152292001-07-02 05:46:38 +000095//===----------------------------------------------------------------------===//
96
Chris Lattner1e435162002-07-26 21:12:44 +000097static RegisterAnalysis<PostDominatorTree>
Chris Lattner17689df2002-07-30 16:27:52 +000098F("postdomtree", "Post-Dominator Tree Construction", true);
Chris Lattnera59cbb22002-07-27 01:12:17 +000099AnalysisID PostDominatorTree::ID = F;
Chris Lattner93193f82002-01-31 00:42:27 +0000100
Chris Lattnerce6ef112002-07-26 18:40:14 +0000101void PostDominatorTree::calculate(const PostDominatorSet &DS) {
102 Nodes[Root] = new Node(Root, 0); // Add a node for the root...
103
104 if (Root) {
Chris Lattner94108ab2001-07-06 16:58:22 +0000105 // Iterate over all nodes in depth first order...
Chris Lattner93193f82002-01-31 00:42:27 +0000106 for (idf_iterator<BasicBlock*> I = idf_begin(Root), E = idf_end(Root);
Chris Lattner3ff43872001-09-28 22:56:31 +0000107 I != E; ++I) {
Chris Lattnera298d272002-04-28 00:15:57 +0000108 BasicBlock *BB = *I;
Chris Lattner94108ab2001-07-06 16:58:22 +0000109 const DominatorSet::DomSetType &Dominators = DS.getDominators(BB);
110 unsigned DomSetSize = Dominators.size();
111 if (DomSetSize == 1) continue; // Root node... IDom = null
112
Chris Lattner3ff43872001-09-28 22:56:31 +0000113 // Loop over all dominators of this node. This corresponds to looping
114 // over nodes in the dominator chain, looking for a node whose dominator
115 // set is equal to the current nodes, except that the current node does
116 // not exist in it. This means that it is one level higher in the dom
117 // chain than the current node, and it is our idom! We know that we have
118 // already added a DominatorTree node for our idom, because the idom must
119 // be a predecessor in the depth first order that we are iterating through
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000120 // the function.
Chris Lattner94108ab2001-07-06 16:58:22 +0000121 //
122 DominatorSet::DomSetType::const_iterator I = Dominators.begin();
123 DominatorSet::DomSetType::const_iterator End = Dominators.end();
124 for (; I != End; ++I) { // Iterate over dominators...
Chris Lattner93193f82002-01-31 00:42:27 +0000125 // All of our dominators should form a chain, where the number
126 // of elements in the dominator set indicates what level the
127 // node is at in the chain. We want the node immediately
128 // above us, so it will have an identical dominator set,
129 // except that BB will not dominate it... therefore it's
Chris Lattner94108ab2001-07-06 16:58:22 +0000130 // dominator set size will be one less than BB's...
131 //
132 if (DS.getDominators(*I).size() == DomSetSize - 1) {
133 // We know that the immediate dominator should already have a node,
134 // because we are traversing the CFG in depth first order!
135 //
136 Node *IDomNode = Nodes[*I];
137 assert(IDomNode && "No node for IDOM?");
138
139 // Add a new tree node for this BasicBlock, and link it as a child of
140 // IDomNode
141 Nodes[BB] = IDomNode->addChild(new Node(BB, IDomNode));
142 break;
143 }
Chris Lattner17152292001-07-02 05:46:38 +0000144 }
145 }
146 }
147}
148
Chris Lattner17152292001-07-02 05:46:38 +0000149//===----------------------------------------------------------------------===//
Chris Lattner4c9df7c2002-08-02 16:43:03 +0000150// PostDominanceFrontier Implementation
Chris Lattner17152292001-07-02 05:46:38 +0000151//===----------------------------------------------------------------------===//
152
Chris Lattner1e435162002-07-26 21:12:44 +0000153static RegisterAnalysis<PostDominanceFrontier>
Chris Lattner17689df2002-07-30 16:27:52 +0000154H("postdomfrontier", "Post-Dominance Frontier Construction", true);
Chris Lattnera59cbb22002-07-27 01:12:17 +0000155AnalysisID PostDominanceFrontier::ID = H;
Chris Lattner93193f82002-01-31 00:42:27 +0000156
Chris Lattner1b7f7dc2002-04-28 16:21:30 +0000157const DominanceFrontier::DomSetType &
Chris Lattnerce6ef112002-07-26 18:40:14 +0000158PostDominanceFrontier::calculate(const PostDominatorTree &DT,
159 const DominatorTree::Node *Node) {
Chris Lattner94108ab2001-07-06 16:58:22 +0000160 // Loop over CFG successors to calculate DFlocal[Node]
Chris Lattnera298d272002-04-28 00:15:57 +0000161 BasicBlock *BB = Node->getNode();
Chris Lattner94108ab2001-07-06 16:58:22 +0000162 DomSetType &S = Frontiers[BB]; // The new set to fill in...
Chris Lattner384e5b12001-08-23 17:07:19 +0000163 if (!Root) return S;
Chris Lattner94108ab2001-07-06 16:58:22 +0000164
Chris Lattnera298d272002-04-28 00:15:57 +0000165 for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB);
Chris Lattner455889a2002-02-12 22:39:50 +0000166 SI != SE; ++SI) {
Chris Lattner94108ab2001-07-06 16:58:22 +0000167 // Does Node immediately dominate this predeccessor?
168 if (DT[*SI]->getIDom() != Node)
169 S.insert(*SI);
170 }
171
172 // At this point, S is DFlocal. Now we union in DFup's of our children...
173 // Loop through and visit the nodes that Node immediately dominates (Node's
174 // children in the IDomTree)
175 //
Chris Lattnerce6ef112002-07-26 18:40:14 +0000176 for (PostDominatorTree::Node::const_iterator
177 NI = Node->begin(), NE = Node->end(); NI != NE; ++NI) {
Chris Lattner94108ab2001-07-06 16:58:22 +0000178 DominatorTree::Node *IDominee = *NI;
Chris Lattnerce6ef112002-07-26 18:40:14 +0000179 const DomSetType &ChildDF = calculate(DT, IDominee);
Chris Lattner94108ab2001-07-06 16:58:22 +0000180
181 DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end();
182 for (; CDFI != CDFE; ++CDFI) {
183 if (!Node->dominates(DT[*CDFI]))
184 S.insert(*CDFI);
185 }
186 }
187
188 return S;
189}