blob: ddf8d7fb5a054f37fb9d5615fe27e6a13f8ce78c [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 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 +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 Lattner93193f82002-01-31 00:42:27 +000026 // Since we require that the unify all exit nodes pass has been run, we know
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000027 // that there can be at most one return instruction in the function left.
Chris Lattner93193f82002-01-31 00:42:27 +000028 // Get it.
29 //
Chris Lattner483e14e2002-04-27 07:27:19 +000030 Root = getAnalysis<UnifyFunctionExitNodes>().getExitNode();
Chris Lattner94108ab2001-07-06 16:58:22 +000031
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000032 if (Root == 0) { // No exit node for the function? Postdomsets are all empty
Chris Lattner7e708292002-06-25 16:13:24 +000033 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
34 Doms[FI] = DomSetType();
Chris Lattnerce6ef112002-07-26 18:40:14 +000035 return false;
Chris Lattner384e5b12001-08-23 17:07:19 +000036 }
Chris Lattner94108ab2001-07-06 16:58:22 +000037
38 bool Changed;
39 do {
40 Changed = false;
41
Chris Lattner2446b0a2003-09-10 16:08:03 +000042 std::set<const BasicBlock*> Visited;
Chris Lattner94108ab2001-07-06 16:58:22 +000043 DomSetType WorkingSet;
Chris Lattner93193f82002-01-31 00:42:27 +000044 idf_iterator<BasicBlock*> It = idf_begin(Root), End = idf_end(Root);
Chris Lattner94108ab2001-07-06 16:58:22 +000045 for ( ; It != End; ++It) {
Chris Lattnera298d272002-04-28 00:15:57 +000046 BasicBlock *BB = *It;
47 succ_iterator PI = succ_begin(BB), PEnd = succ_end(BB);
Chris Lattner94108ab2001-07-06 16:58:22 +000048 if (PI != PEnd) { // Is there SOME predecessor?
49 // Loop until we get to a successor that has had it's dom set filled
50 // in at least once. We are guaranteed to have this because we are
51 // traversing the graph in DFO and have handled start nodes specially.
52 //
53 while (Doms[*PI].size() == 0) ++PI;
54 WorkingSet = Doms[*PI];
55
56 for (++PI; PI != PEnd; ++PI) { // Intersect all of the successor sets
57 DomSetType &PredSet = Doms[*PI];
58 if (PredSet.size())
59 set_intersect(WorkingSet, PredSet);
60 }
Chris Lattner7d821db2002-10-04 14:50:20 +000061 } else if (BB != Root) {
62 // If this isn't the root basic block and it has no successors, it must
63 // be an non-returning block. Fib a bit by saying that the root node
64 // postdominates this unreachable node. This isn't exactly true,
65 // because there is no path from this node to the root node, but it is
66 // sorta true because any paths to the exit node would have to go
67 // through this node.
68 //
69 // This allows for postdominator properties to be built for code that
70 // doesn't return in a reasonable manner.
71 //
72 WorkingSet = Doms[Root];
Chris Lattner94108ab2001-07-06 16:58:22 +000073 }
74
75 WorkingSet.insert(BB); // A block always dominates itself
76 DomSetType &BBSet = Doms[BB];
77 if (BBSet != WorkingSet) {
78 BBSet.swap(WorkingSet); // Constant time operation!
79 Changed = true; // The sets changed.
80 }
81 WorkingSet.clear(); // Clear out the set for next iteration
82 }
83 } while (Changed);
Chris Lattnerce6ef112002-07-26 18:40:14 +000084 return false;
Chris Lattner17152292001-07-02 05:46:38 +000085}
86
Chris Lattnerce6ef112002-07-26 18:40:14 +000087// getAnalysisUsage - This obviously provides a post-dominator set, but it also
88// requires the UnifyFunctionExitNodes pass.
Chris Lattner93193f82002-01-31 00:42:27 +000089//
Chris Lattnerce6ef112002-07-26 18:40:14 +000090void PostDominatorSet::getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerf57b8452002-04-27 06:56:12 +000091 AU.setPreservesAll();
Chris Lattnerdd5b4952002-08-08 19:01:28 +000092 AU.addRequired<UnifyFunctionExitNodes>();
Chris Lattner93193f82002-01-31 00:42:27 +000093}
94
Chris Lattner17152292001-07-02 05:46:38 +000095//===----------------------------------------------------------------------===//
Chris Lattner4c9df7c2002-08-02 16:43:03 +000096// ImmediatePostDominators Implementation
Chris Lattner17152292001-07-02 05:46:38 +000097//===----------------------------------------------------------------------===//
98
Chris Lattner1e435162002-07-26 21:12:44 +000099static RegisterAnalysis<ImmediatePostDominators>
Chris Lattner17689df2002-07-30 16:27:52 +0000100D("postidom", "Immediate Post-Dominators Construction", true);
Chris Lattner93193f82002-01-31 00:42:27 +0000101
Chris Lattner17152292001-07-02 05:46:38 +0000102//===----------------------------------------------------------------------===//
Chris Lattner4c9df7c2002-08-02 16:43:03 +0000103// PostDominatorTree Implementation
Chris Lattner17152292001-07-02 05:46:38 +0000104//===----------------------------------------------------------------------===//
105
Chris Lattner1e435162002-07-26 21:12:44 +0000106static RegisterAnalysis<PostDominatorTree>
Chris Lattner17689df2002-07-30 16:27:52 +0000107F("postdomtree", "Post-Dominator Tree Construction", true);
Chris Lattner93193f82002-01-31 00:42:27 +0000108
Chris Lattnerce6ef112002-07-26 18:40:14 +0000109void PostDominatorTree::calculate(const PostDominatorSet &DS) {
110 Nodes[Root] = new Node(Root, 0); // Add a node for the root...
111
112 if (Root) {
Chris Lattner94108ab2001-07-06 16:58:22 +0000113 // Iterate over all nodes in depth first order...
Chris Lattner93193f82002-01-31 00:42:27 +0000114 for (idf_iterator<BasicBlock*> I = idf_begin(Root), E = idf_end(Root);
Chris Lattner3ff43872001-09-28 22:56:31 +0000115 I != E; ++I) {
Chris Lattnera298d272002-04-28 00:15:57 +0000116 BasicBlock *BB = *I;
Chris Lattner94108ab2001-07-06 16:58:22 +0000117 const DominatorSet::DomSetType &Dominators = DS.getDominators(BB);
118 unsigned DomSetSize = Dominators.size();
119 if (DomSetSize == 1) continue; // Root node... IDom = null
120
Chris Lattner3ff43872001-09-28 22:56:31 +0000121 // Loop over all dominators of this node. This corresponds to looping
122 // over nodes in the dominator chain, looking for a node whose dominator
123 // set is equal to the current nodes, except that the current node does
124 // not exist in it. This means that it is one level higher in the dom
125 // chain than the current node, and it is our idom! We know that we have
126 // already added a DominatorTree node for our idom, because the idom must
127 // be a predecessor in the depth first order that we are iterating through
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000128 // the function.
Chris Lattner94108ab2001-07-06 16:58:22 +0000129 //
130 DominatorSet::DomSetType::const_iterator I = Dominators.begin();
131 DominatorSet::DomSetType::const_iterator End = Dominators.end();
132 for (; I != End; ++I) { // Iterate over dominators...
Chris Lattner93193f82002-01-31 00:42:27 +0000133 // All of our dominators should form a chain, where the number
134 // of elements in the dominator set indicates what level the
135 // node is at in the chain. We want the node immediately
136 // above us, so it will have an identical dominator set,
137 // except that BB will not dominate it... therefore it's
Chris Lattner94108ab2001-07-06 16:58:22 +0000138 // dominator set size will be one less than BB's...
139 //
140 if (DS.getDominators(*I).size() == DomSetSize - 1) {
141 // We know that the immediate dominator should already have a node,
142 // because we are traversing the CFG in depth first order!
143 //
144 Node *IDomNode = Nodes[*I];
145 assert(IDomNode && "No node for IDOM?");
146
147 // Add a new tree node for this BasicBlock, and link it as a child of
148 // IDomNode
149 Nodes[BB] = IDomNode->addChild(new Node(BB, IDomNode));
150 break;
151 }
Chris Lattner17152292001-07-02 05:46:38 +0000152 }
153 }
154 }
155}
156
Chris Lattner17152292001-07-02 05:46:38 +0000157//===----------------------------------------------------------------------===//
Chris Lattner4c9df7c2002-08-02 16:43:03 +0000158// PostDominanceFrontier Implementation
Chris Lattner17152292001-07-02 05:46:38 +0000159//===----------------------------------------------------------------------===//
160
Chris Lattner1e435162002-07-26 21:12:44 +0000161static RegisterAnalysis<PostDominanceFrontier>
Chris Lattner17689df2002-07-30 16:27:52 +0000162H("postdomfrontier", "Post-Dominance Frontier Construction", true);
Chris Lattner93193f82002-01-31 00:42:27 +0000163
Chris Lattner1b7f7dc2002-04-28 16:21:30 +0000164const DominanceFrontier::DomSetType &
Chris Lattnerce6ef112002-07-26 18:40:14 +0000165PostDominanceFrontier::calculate(const PostDominatorTree &DT,
166 const DominatorTree::Node *Node) {
Chris Lattner94108ab2001-07-06 16:58:22 +0000167 // Loop over CFG successors to calculate DFlocal[Node]
Chris Lattnera298d272002-04-28 00:15:57 +0000168 BasicBlock *BB = Node->getNode();
Chris Lattner94108ab2001-07-06 16:58:22 +0000169 DomSetType &S = Frontiers[BB]; // The new set to fill in...
Chris Lattner384e5b12001-08-23 17:07:19 +0000170 if (!Root) return S;
Chris Lattner94108ab2001-07-06 16:58:22 +0000171
Chris Lattnera298d272002-04-28 00:15:57 +0000172 for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB);
Chris Lattner455889a2002-02-12 22:39:50 +0000173 SI != SE; ++SI) {
Chris Lattner94108ab2001-07-06 16:58:22 +0000174 // Does Node immediately dominate this predeccessor?
175 if (DT[*SI]->getIDom() != Node)
176 S.insert(*SI);
177 }
178
179 // At this point, S is DFlocal. Now we union in DFup's of our children...
180 // Loop through and visit the nodes that Node immediately dominates (Node's
181 // children in the IDomTree)
182 //
Chris Lattnerce6ef112002-07-26 18:40:14 +0000183 for (PostDominatorTree::Node::const_iterator
184 NI = Node->begin(), NE = Node->end(); NI != NE; ++NI) {
Chris Lattner94108ab2001-07-06 16:58:22 +0000185 DominatorTree::Node *IDominee = *NI;
Chris Lattnerce6ef112002-07-26 18:40:14 +0000186 const DomSetType &ChildDF = calculate(DT, IDominee);
Chris Lattner94108ab2001-07-06 16:58:22 +0000187
188 DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end();
189 for (; CDFI != CDFE; ++CDFI) {
190 if (!Node->dominates(DT[*CDFI]))
191 S.insert(*CDFI);
192 }
193 }
194
195 return S;
196}
Chris Lattnera69fd902002-08-21 23:43:50 +0000197
198// stub - a dummy function to make linking work ok.
199void PostDominanceFrontier::stub() {
200}