blob: e3169a5c83812300a392e86e05adb7772e984eb8 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- PostDominators.cpp - Post-Dominator Calculation --------------------===//
2//
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//===----------------------------------------------------------------------===//
9//
10// This file implements the post-dominator construction algorithms.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Analysis/PostDominators.h"
15#include "llvm/Instructions.h"
16#include "llvm/Support/CFG.h"
17#include "llvm/ADT/DepthFirstIterator.h"
18#include "llvm/ADT/SetOperations.h"
19using namespace llvm;
20
21//===----------------------------------------------------------------------===//
22// PostDominatorTree Implementation
23//===----------------------------------------------------------------------===//
24
25char PostDominatorTree::ID = 0;
26char PostDominanceFrontier::ID = 0;
27static RegisterPass<PostDominatorTree>
28F("postdomtree", "Post-Dominator Tree Construction", true);
29
30unsigned PostDominatorTree::DFSPass(BasicBlock *V, InfoRec &VInfo,
Chris Lattner59653532007-08-04 23:48:07 +000031 unsigned N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032 std::vector<std::pair<BasicBlock *, InfoRec *> > workStack;
33 std::set<BasicBlock *> visited;
34 workStack.push_back(std::make_pair(V, &VInfo));
35
36 do {
37 BasicBlock *currentBB = workStack.back().first;
38 InfoRec *currentVInfo = workStack.back().second;
39
40 // Visit each block only once.
41 if (visited.count(currentBB) == 0) {
42
43 visited.insert(currentBB);
44 currentVInfo->Semi = ++N;
45 currentVInfo->Label = currentBB;
46
47 Vertex.push_back(currentBB); // Vertex[n] = current;
48 // Info[currentBB].Ancestor = 0;
49 // Ancestor[n] = 0
50 // Child[currentBB] = 0;
51 currentVInfo->Size = 1; // Size[currentBB] = 1
52 }
53
54 // Visit children
55 bool visitChild = false;
56 for (pred_iterator PI = pred_begin(currentBB), PE = pred_end(currentBB);
57 PI != PE && !visitChild; ++PI) {
58 InfoRec &SuccVInfo = Info[*PI];
59 if (SuccVInfo.Semi == 0) {
60 SuccVInfo.Parent = currentBB;
61 if (visited.count (*PI) == 0) {
62 workStack.push_back(std::make_pair(*PI, &SuccVInfo));
63 visitChild = true;
64 }
65 }
66 }
67
68 // If all children are visited or if this block has no child then pop this
69 // block out of workStack.
70 if (!visitChild)
71 workStack.pop_back();
72
73 } while (!workStack.empty());
74
75 return N;
76}
77
78void PostDominatorTree::Compress(BasicBlock *V, InfoRec &VInfo) {
79 BasicBlock *VAncestor = VInfo.Ancestor;
80 InfoRec &VAInfo = Info[VAncestor];
81 if (VAInfo.Ancestor == 0)
82 return;
83
84 Compress(VAncestor, VAInfo);
85
86 BasicBlock *VAncestorLabel = VAInfo.Label;
87 BasicBlock *VLabel = VInfo.Label;
88 if (Info[VAncestorLabel].Semi < Info[VLabel].Semi)
89 VInfo.Label = VAncestorLabel;
90
91 VInfo.Ancestor = VAInfo.Ancestor;
92}
93
94BasicBlock *PostDominatorTree::Eval(BasicBlock *V) {
95 InfoRec &VInfo = Info[V];
96
97 // Higher-complexity but faster implementation
98 if (VInfo.Ancestor == 0)
99 return V;
100 Compress(V, VInfo);
101 return VInfo.Label;
102}
103
104void PostDominatorTree::Link(BasicBlock *V, BasicBlock *W,
105 InfoRec &WInfo) {
106 // Higher-complexity but faster implementation
107 WInfo.Ancestor = V;
108}
109
110void PostDominatorTree::calculate(Function &F) {
111 // Step #0: Scan the function looking for the root nodes of the post-dominance
112 // relationships. These blocks, which have no successors, end with return and
113 // unwind instructions.
Chris Lattner59653532007-08-04 23:48:07 +0000114 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
115 TerminatorInst *Insn = I->getTerminator();
116 if (Insn->getNumSuccessors() == 0) {
Devang Patel34ef1442007-07-24 01:02:25 +0000117 // Unreachable block is not a root node.
118 if (!isa<UnreachableInst>(Insn))
119 Roots.push_back(I);
120 }
Chris Lattner59653532007-08-04 23:48:07 +0000121
122 // Prepopulate maps so that we don't get iterator invalidation issues later.
123 IDoms[I] = 0;
124 DomTreeNodes[I] = 0;
125 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126
127 Vertex.push_back(0);
128
129 // Step #1: Number blocks in depth-first order and initialize variables used
130 // in later stages of the algorithm.
131 unsigned N = 0;
132 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
133 N = DFSPass(Roots[i], Info[Roots[i]], N);
134
135 for (unsigned i = N; i >= 2; --i) {
136 BasicBlock *W = Vertex[i];
137 InfoRec &WInfo = Info[W];
138
139 // Step #2: Calculate the semidominators of all vertices
140 for (succ_iterator SI = succ_begin(W), SE = succ_end(W); SI != SE; ++SI)
141 if (Info.count(*SI)) { // Only if this predecessor is reachable!
142 unsigned SemiU = Info[Eval(*SI)].Semi;
143 if (SemiU < WInfo.Semi)
144 WInfo.Semi = SemiU;
145 }
146
147 Info[Vertex[WInfo.Semi]].Bucket.push_back(W);
148
149 BasicBlock *WParent = WInfo.Parent;
150 Link(WParent, W, WInfo);
151
152 // Step #3: Implicitly define the immediate dominator of vertices
153 std::vector<BasicBlock*> &WParentBucket = Info[WParent].Bucket;
154 while (!WParentBucket.empty()) {
155 BasicBlock *V = WParentBucket.back();
156 WParentBucket.pop_back();
157 BasicBlock *U = Eval(V);
158 IDoms[V] = Info[U].Semi < Info[V].Semi ? U : WParent;
159 }
160 }
161
162 // Step #4: Explicitly define the immediate dominator of each vertex
163 for (unsigned i = 2; i <= N; ++i) {
164 BasicBlock *W = Vertex[i];
165 BasicBlock *&WIDom = IDoms[W];
166 if (WIDom != Vertex[Info[W].Semi])
167 WIDom = IDoms[WIDom];
168 }
169
170 if (Roots.empty()) return;
171
172 // Add a node for the root. This node might be the actual root, if there is
173 // one exit block, or it may be the virtual exit (denoted by (BasicBlock *)0)
174 // which postdominates all real exits if there are multiple exit blocks.
175 BasicBlock *Root = Roots.size() == 1 ? Roots[0] : 0;
176 DomTreeNodes[Root] = RootNode = new DomTreeNode(Root, 0);
177
178 // Loop over all of the reachable blocks in the function...
179 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
180 if (BasicBlock *ImmPostDom = getIDom(I)) { // Reachable block.
181 DomTreeNode *&BBNode = DomTreeNodes[I];
182 if (!BBNode) { // Haven't calculated this node yet?
183 // Get or calculate the node for the immediate dominator
184 DomTreeNode *IPDomNode = getNodeForBlock(ImmPostDom);
185
186 // Add a new tree node for this BasicBlock, and link it as a child of
187 // IDomNode
188 DomTreeNode *C = new DomTreeNode(I, IPDomNode);
189 DomTreeNodes[I] = C;
190 BBNode = IPDomNode->addChild(C);
191 }
192 }
193
194 // Free temporary memory used to construct idom's
195 IDoms.clear();
196 Info.clear();
197 std::vector<BasicBlock*>().swap(Vertex);
198
199 int dfsnum = 0;
200 // Iterate over all nodes in depth first order...
201 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
202 for (idf_iterator<BasicBlock*> I = idf_begin(Roots[i]),
203 E = idf_end(Roots[i]); I != E; ++I) {
204 if (!getNodeForBlock(*I)->getIDom())
205 getNodeForBlock(*I)->assignDFSNumber(dfsnum);
206 }
207 DFSInfoValid = true;
208}
209
210
211DomTreeNode *PostDominatorTree::getNodeForBlock(BasicBlock *BB) {
212 DomTreeNode *&BBNode = DomTreeNodes[BB];
213 if (BBNode) return BBNode;
214
215 // Haven't calculated this node yet? Get or calculate the node for the
216 // immediate postdominator.
217 BasicBlock *IPDom = getIDom(BB);
218 DomTreeNode *IPDomNode = getNodeForBlock(IPDom);
219
220 // Add a new tree node for this BasicBlock, and link it as a child of
221 // IDomNode
222 DomTreeNode *C = new DomTreeNode(BB, IPDomNode);
223 DomTreeNodes[BB] = C;
224 return BBNode = IPDomNode->addChild(C);
225}
226
227//===----------------------------------------------------------------------===//
228// PostDominanceFrontier Implementation
229//===----------------------------------------------------------------------===//
230
231static RegisterPass<PostDominanceFrontier>
232H("postdomfrontier", "Post-Dominance Frontier Construction", true);
233
234const DominanceFrontier::DomSetType &
235PostDominanceFrontier::calculate(const PostDominatorTree &DT,
236 const DomTreeNode *Node) {
237 // Loop over CFG successors to calculate DFlocal[Node]
238 BasicBlock *BB = Node->getBlock();
239 DomSetType &S = Frontiers[BB]; // The new set to fill in...
240 if (getRoots().empty()) return S;
241
242 if (BB)
243 for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB);
244 SI != SE; ++SI) {
245 // Does Node immediately dominate this predecessor?
246 DomTreeNode *SINode = DT[*SI];
247 if (SINode && SINode->getIDom() != Node)
248 S.insert(*SI);
249 }
250
251 // At this point, S is DFlocal. Now we union in DFup's of our children...
252 // Loop through and visit the nodes that Node immediately dominates (Node's
253 // children in the IDomTree)
254 //
255 for (DomTreeNode::const_iterator
256 NI = Node->begin(), NE = Node->end(); NI != NE; ++NI) {
257 DomTreeNode *IDominee = *NI;
258 const DomSetType &ChildDF = calculate(DT, IDominee);
259
260 DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end();
261 for (; CDFI != CDFE; ++CDFI) {
262 if (!DT.properlyDominates(Node, DT[*CDFI]))
263 S.insert(*CDFI);
264 }
265 }
266
267 return S;
268}
269
270// Ensure that this .cpp file gets linked when PostDominators.h is used.
271DEFINING_FILE_FOR(PostDominanceFrontier)