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