llvm.org GIT mirror llvm / 04fa569
Factor the calculation details for PostDomTree out of PostDominators.cpp and into a separate header file. Next step: merging PostDominatorCalculation.h with DominatorCalculation.h. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@42251 91177308-0d34-0410-b5e6-96231b3b80d8 Owen Anderson 13 years ago
3 changed file(s) with 156 addition(s) and 147 deletion(s). Raw diff Collapse all Expand all
2828
2929 virtual bool runOnFunction(Function &F) {
3030 reset(); // Reset from the last time we were run...
31 calculate(F);
31 PDTcalculate(*this, F);
3232 return false;
3333 }
3434
3636 AU.setPreservesAll();
3737 }
3838 private:
39 void calculate(Function &F);
4039 unsigned DFSPass(BasicBlock *V, unsigned N);
41 void Compress(BasicBlock *V, InfoRec &VInfo);
42 BasicBlock *Eval(BasicBlock *V);
43 void Link(BasicBlock *V, BasicBlock *W, InfoRec &WInfo);
40 friend void PDTcalculate(PostDominatorTree& PDT, Function &F);
41 friend void PDTCompress(PostDominatorTree& PDT, BasicBlock *V,
42 InfoRec &VInfo);
43 friend BasicBlock *PDTEval(PostDominatorTree& PDT, BasicBlock *V);
44 friend void PDTLink(PostDominatorTree& PDT,BasicBlock *V,
45 BasicBlock *W, InfoRec &WInfo);
4446 };
4547
4648
0 //==- PostDominatorCalculation.h - Post-Dominator Calculation ----*- C++ -*-==//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file was developed by Owen Anderson and is distributed under
5 // the University of Illinois Open Source License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // PostDominatorTree calculation implementation.
10 //===----------------------------------------------------------------------===//
11
12 #ifndef LLVM_ANALYSIS_POST_DOMINATOR_CALCULATION_H
13 #define LLVM_ANALYSIS_POST_DOMINATOR_CALCULATION_H
14
15 #include "llvm/Analysis/PostDominators.h"
16
17 namespace llvm {
18
19 void PDTCompress(PostDominatorTree& PDT, BasicBlock *V,
20 PostDominatorTree::InfoRec &VInfo) {
21 BasicBlock *VAncestor = VInfo.Ancestor;
22 PostDominatorTree::InfoRec &VAInfo = PDT.Info[VAncestor];
23 if (VAInfo.Ancestor == 0)
24 return;
25
26 PDTCompress(PDT, VAncestor, VAInfo);
27
28 BasicBlock *VAncestorLabel = VAInfo.Label;
29 BasicBlock *VLabel = VInfo.Label;
30 if (PDT.Info[VAncestorLabel].Semi < PDT.Info[VLabel].Semi)
31 VInfo.Label = VAncestorLabel;
32
33 VInfo.Ancestor = VAInfo.Ancestor;
34 }
35
36 BasicBlock *PDTEval(PostDominatorTree& PDT, BasicBlock *V) {
37 PostDominatorTree::InfoRec &VInfo = PDT.Info[V];
38
39 // Higher-complexity but faster implementation
40 if (VInfo.Ancestor == 0)
41 return V;
42 PDTCompress(PDT, V, VInfo);
43 return VInfo.Label;
44 }
45
46 void PDTLink(PostDominatorTree& PDT, BasicBlock *V, BasicBlock *W,
47 PostDominatorTree::InfoRec &WInfo) {
48 // Higher-complexity but faster implementation
49 WInfo.Ancestor = V;
50 }
51
52 void PDTcalculate(PostDominatorTree& PDT, Function &F) {
53 // Step #0: Scan the function looking for the root nodes of the post-dominance
54 // relationships. These blocks, which have no successors, end with return and
55 // unwind instructions.
56 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
57 TerminatorInst *Insn = I->getTerminator();
58 if (Insn->getNumSuccessors() == 0) {
59 // Unreachable block is not a root node.
60 if (!isa(Insn))
61 PDT.Roots.push_back(I);
62 }
63
64 // Prepopulate maps so that we don't get iterator invalidation issues later.
65 PDT.IDoms[I] = 0;
66 PDT.DomTreeNodes[I] = 0;
67 }
68
69 PDT.Vertex.push_back(0);
70
71 // Step #1: Number blocks in depth-first order and initialize variables used
72 // in later stages of the algorithm.
73 unsigned N = 0;
74 for (unsigned i = 0, e = PDT.Roots.size(); i != e; ++i)
75 N = PDT.DFSPass(PDT.Roots[i], N);
76
77 for (unsigned i = N; i >= 2; --i) {
78 BasicBlock *W = PDT.Vertex[i];
79 PostDominatorTree::InfoRec &WInfo = PDT.Info[W];
80
81 // Step #2: Calculate the semidominators of all vertices
82 for (succ_iterator SI = succ_begin(W), SE = succ_end(W); SI != SE; ++SI)
83 if (PDT.Info.count(*SI)) { // Only if this predecessor is reachable!
84 unsigned SemiU = PDT.Info[PDTEval(PDT, *SI)].Semi;
85 if (SemiU < WInfo.Semi)
86 WInfo.Semi = SemiU;
87 }
88
89 PDT.Info[PDT.Vertex[WInfo.Semi]].Bucket.push_back(W);
90
91 BasicBlock *WParent = WInfo.Parent;
92 PDTLink(PDT, WParent, W, WInfo);
93
94 // Step #3: Implicitly define the immediate dominator of vertices
95 std::vector &WParentBucket = PDT.Info[WParent].Bucket;
96 while (!WParentBucket.empty()) {
97 BasicBlock *V = WParentBucket.back();
98 WParentBucket.pop_back();
99 BasicBlock *U = PDTEval(PDT, V);
100 PDT.IDoms[V] = PDT.Info[U].Semi < PDT.Info[V].Semi ? U : WParent;
101 }
102 }
103
104 // Step #4: Explicitly define the immediate dominator of each vertex
105 for (unsigned i = 2; i <= N; ++i) {
106 BasicBlock *W = PDT.Vertex[i];
107 BasicBlock *&WIDom = PDT.IDoms[W];
108 if (WIDom != PDT.Vertex[PDT.Info[W].Semi])
109 WIDom = PDT.IDoms[WIDom];
110 }
111
112 if (PDT.Roots.empty()) return;
113
114 // Add a node for the root. This node might be the actual root, if there is
115 // one exit block, or it may be the virtual exit (denoted by (BasicBlock *)0)
116 // which postdominates all real exits if there are multiple exit blocks.
117 BasicBlock *Root = PDT.Roots.size() == 1 ? PDT.Roots[0] : 0;
118 PDT.DomTreeNodes[Root] = PDT.RootNode = new DomTreeNode(Root, 0);
119
120 // Loop over all of the reachable blocks in the function...
121 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
122 if (BasicBlock *ImmPostDom = PDT.getIDom(I)) { // Reachable block.
123 DomTreeNode *&BBNode = PDT.DomTreeNodes[I];
124 if (!BBNode) { // Haven't calculated this node yet?
125 // Get or calculate the node for the immediate dominator
126 DomTreeNode *IPDomNode = PDT.getNodeForBlock(ImmPostDom);
127
128 // Add a new tree node for this BasicBlock, and link it as a child of
129 // IDomNode
130 DomTreeNode *C = new DomTreeNode(I, IPDomNode);
131 PDT.DomTreeNodes[I] = C;
132 BBNode = IPDomNode->addChild(C);
133 }
134 }
135
136 // Free temporary memory used to construct idom's
137 PDT.IDoms.clear();
138 PDT.Info.clear();
139 std::vector().swap(PDT.Vertex);
140
141 // Start out with the DFS numbers being invalid. Let them be computed if
142 // demanded.
143 PDT.DFSInfoValid = false;
144 }
145
146 }
147 #endif
1515 #include "llvm/Support/CFG.h"
1616 #include "llvm/ADT/DepthFirstIterator.h"
1717 #include "llvm/ADT/SetOperations.h"
18 #include "PostDominatorCalculation.h"
1819 using namespace llvm;
1920
2021 //===----------------------------------------------------------------------===//
7172 return N;
7273 }
7374
74 void PostDominatorTree::Compress(BasicBlock *V, InfoRec &VInfo) {
75 BasicBlock *VAncestor = VInfo.Ancestor;
76 InfoRec &VAInfo = Info[VAncestor];
77 if (VAInfo.Ancestor == 0)
78 return;
79
80 Compress(VAncestor, VAInfo);
81
82 BasicBlock *VAncestorLabel = VAInfo.Label;
83 BasicBlock *VLabel = VInfo.Label;
84 if (Info[VAncestorLabel].Semi < Info[VLabel].Semi)
85 VInfo.Label = VAncestorLabel;
86
87 VInfo.Ancestor = VAInfo.Ancestor;
88 }
89
90 BasicBlock *PostDominatorTree::Eval(BasicBlock *V) {
91 InfoRec &VInfo = Info[V];
92
93 // Higher-complexity but faster implementation
94 if (VInfo.Ancestor == 0)
95 return V;
96 Compress(V, VInfo);
97 return VInfo.Label;
98 }
99
100 void PostDominatorTree::Link(BasicBlock *V, BasicBlock *W,
101 InfoRec &WInfo) {
102 // Higher-complexity but faster implementation
103 WInfo.Ancestor = V;
104 }
105
106 void PostDominatorTree::calculate(Function &F) {
107 // Step #0: Scan the function looking for the root nodes of the post-dominance
108 // relationships. These blocks, which have no successors, end with return and
109 // unwind instructions.
110 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
111 TerminatorInst *Insn = I->getTerminator();
112 if (Insn->getNumSuccessors() == 0) {
113 // Unreachable block is not a root node.
114 if (!isa(Insn))
115 Roots.push_back(I);
116 }
117
118 // Prepopulate maps so that we don't get iterator invalidation issues later.
119 IDoms[I] = 0;
120 DomTreeNodes[I] = 0;
121 }
122
123 Vertex.push_back(0);
124
125 // Step #1: Number blocks in depth-first order and initialize variables used
126 // in later stages of the algorithm.
127 unsigned N = 0;
128 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
129 N = DFSPass(Roots[i], N);
130
131 for (unsigned i = N; i >= 2; --i) {
132 BasicBlock *W = Vertex[i];
133 InfoRec &WInfo = Info[W];
134
135 // Step #2: Calculate the semidominators of all vertices
136 for (succ_iterator SI = succ_begin(W), SE = succ_end(W); SI != SE; ++SI)
137 if (Info.count(*SI)) { // Only if this predecessor is reachable!
138 unsigned SemiU = Info[Eval(*SI)].Semi;
139 if (SemiU < WInfo.Semi)
140 WInfo.Semi = SemiU;
141 }
142
143 Info[Vertex[WInfo.Semi]].Bucket.push_back(W);
144
145 BasicBlock *WParent = WInfo.Parent;
146 Link(WParent, W, WInfo);
147
148 // Step #3: Implicitly define the immediate dominator of vertices
149 std::vector &WParentBucket = Info[WParent].Bucket;
150 while (!WParentBucket.empty()) {
151 BasicBlock *V = WParentBucket.back();
152 WParentBucket.pop_back();
153 BasicBlock *U = Eval(V);
154 IDoms[V] = Info[U].Semi < Info[V].Semi ? U : WParent;
155 }
156 }
157
158 // Step #4: Explicitly define the immediate dominator of each vertex
159 for (unsigned i = 2; i <= N; ++i) {
160 BasicBlock *W = Vertex[i];
161 BasicBlock *&WIDom = IDoms[W];
162 if (WIDom != Vertex[Info[W].Semi])
163 WIDom = IDoms[WIDom];
164 }
165
166 if (Roots.empty()) return;
167
168 // Add a node for the root. This node might be the actual root, if there is
169 // one exit block, or it may be the virtual exit (denoted by (BasicBlock *)0)
170 // which postdominates all real exits if there are multiple exit blocks.
171 BasicBlock *Root = Roots.size() == 1 ? Roots[0] : 0;
172 DomTreeNodes[Root] = RootNode = new DomTreeNode(Root, 0);
173
174 // Loop over all of the reachable blocks in the function...
175 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
176 if (BasicBlock *ImmPostDom = getIDom(I)) { // Reachable block.
177 DomTreeNode *&BBNode = DomTreeNodes[I];
178 if (!BBNode) { // Haven't calculated this node yet?
179 // Get or calculate the node for the immediate dominator
180 DomTreeNode *IPDomNode = getNodeForBlock(ImmPostDom);
181
182 // Add a new tree node for this BasicBlock, and link it as a child of
183 // IDomNode
184 DomTreeNode *C = new DomTreeNode(I, IPDomNode);
185 DomTreeNodes[I] = C;
186 BBNode = IPDomNode->addChild(C);
187 }
188 }
189
190 // Free temporary memory used to construct idom's
191 IDoms.clear();
192 Info.clear();
193 std::vector().swap(Vertex);
194
195 // Start out with the DFS numbers being invalid. Let them be computed if
196 // demanded.
197 DFSInfoValid = false;
198 }
199
200
201 DomTreeNode *PostDominatorTree::getNodeForBlock(BasicBlock *BB) {
202 DomTreeNode *&BBNode = DomTreeNodes[BB];
203 if (BBNode) return BBNode;
204
205 // Haven't calculated this node yet? Get or calculate the node for the
206 // immediate postdominator.
207 BasicBlock *IPDom = getIDom(BB);
208 DomTreeNode *IPDomNode = getNodeForBlock(IPDom);
209
210 // Add a new tree node for this BasicBlock, and link it as a child of
211 // IDomNode
212 DomTreeNode *C = new DomTreeNode(BB, IPDomNode);
213 return DomTreeNodes[BB] = IPDomNode->addChild(C);
214 }
215
21675 //===----------------------------------------------------------------------===//
21776 // PostDominanceFrontier Implementation
21877 //===----------------------------------------------------------------------===//