llvm.org GIT mirror llvm / 68099d5
Merge significant portions of the DomTree and PostDomTree implementations. The two remaining unmerged parts are DFSPass, and the Calculate(). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@42255 91177308-0d34-0410-b5e6-96231b3b80d8 Owen Anderson 13 years ago
5 changed file(s) with 156 addition(s) and 152 deletion(s). Raw diff Collapse all Expand all
274274 virtual void dump();
275275
276276 protected:
277 friend void Compress(DominatorTreeBase& DT, BasicBlock *VIn);
278 friend BasicBlock *Eval(DominatorTreeBase& DT, BasicBlock *V);
279 friend void Link(DominatorTreeBase& DT, BasicBlock *V,
280 BasicBlock *W, InfoRec &WInfo);
281
277282 /// updateDFSNumbers - Assign In and Out numbers to the nodes while walking
278283 /// dominator tree in dfs order.
279284 void updateDFSNumbers();
313318
314319 private:
315320 friend void DTcalculate(DominatorTree& DT, Function& F);
316 friend void DTCompress(DominatorTree& DT, BasicBlock *VIn);
317 friend BasicBlock *DTEval(DominatorTree& DT, BasicBlock *v);
318 friend void DTLink(DominatorTree& DT, BasicBlock *V,
319 BasicBlock *W, InfoRec &WInfo);
320321
321322 unsigned DFSPass(BasicBlock *V, unsigned N);
322323 };
3838 private:
3939 unsigned DFSPass(BasicBlock *V, unsigned N);
4040 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);
4441 friend void PDTLink(PostDominatorTree& PDT,BasicBlock *V,
4542 BasicBlock *W, InfoRec &WInfo);
4643 };
1515 #include "llvm/Analysis/PostDominators.h"
1616
1717 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 }
5118
5219 void PDTcalculate(PostDominatorTree& PDT, Function &F) {
5320 // Step #0: Scan the function looking for the root nodes of the post-dominance
8148 // Step #2: Calculate the semidominators of all vertices
8249 for (succ_iterator SI = succ_begin(W), SE = succ_end(W); SI != SE; ++SI)
8350 if (PDT.Info.count(*SI)) { // Only if this predecessor is reachable!
84 unsigned SemiU = PDT.Info[PDTEval(PDT, *SI)].Semi;
51 unsigned SemiU = PDT.Info[Eval(PDT, *SI)].Semi;
8552 if (SemiU < WInfo.Semi)
8653 WInfo.Semi = SemiU;
8754 }
8956 PDT.Info[PDT.Vertex[WInfo.Semi]].Bucket.push_back(W);
9057
9158 BasicBlock *WParent = WInfo.Parent;
92 PDTLink(PDT, WParent, W, WInfo);
59 Link(PDT, WParent, W, WInfo);
9360
9461 // Step #3: Implicitly define the immediate dominator of vertices
9562 std::vector &WParentBucket = PDT.Info[WParent].Bucket;
9663 while (!WParentBucket.empty()) {
9764 BasicBlock *V = WParentBucket.back();
9865 WParentBucket.pop_back();
99 BasicBlock *U = PDTEval(PDT, V);
66 BasicBlock *U = Eval(PDT, V);
10067 PDT.IDoms[V] = PDT.Info[U].Semi < PDT.Info[V].Semi ? U : WParent;
10168 }
10269 }
3232
3333 namespace llvm {
3434
35 void DTCompress(DominatorTree& DT, BasicBlock *VIn) {
36
37 std::vector Work;
38 SmallPtrSet Visited;
39 BasicBlock *VInAncestor = DT.Info[VIn].Ancestor;
40 DominatorTree::InfoRec &VInVAInfo = DT.Info[VInAncestor];
41
42 if (VInVAInfo.Ancestor != 0)
43 Work.push_back(VIn);
44
45 while (!Work.empty()) {
46 BasicBlock *V = Work.back();
47 DominatorTree::InfoRec &VInfo = DT.Info[V];
48 BasicBlock *VAncestor = VInfo.Ancestor;
49 DominatorTree::InfoRec &VAInfo = DT.Info[VAncestor];
50
51 // Process Ancestor first
52 if (Visited.insert(VAncestor) &&
53 VAInfo.Ancestor != 0) {
54 Work.push_back(VAncestor);
55 continue;
56 }
57 Work.pop_back();
58
59 // Update VInfo based on Ancestor info
60 if (VAInfo.Ancestor == 0)
61 continue;
62 BasicBlock *VAncestorLabel = VAInfo.Label;
63 BasicBlock *VLabel = VInfo.Label;
64 if (DT.Info[VAncestorLabel].Semi < DT.Info[VLabel].Semi)
65 VInfo.Label = VAncestorLabel;
66 VInfo.Ancestor = VAInfo.Ancestor;
67 }
68 }
69
70 BasicBlock *DTEval(DominatorTree& DT, BasicBlock *V) {
71 DominatorTree::InfoRec &VInfo = DT.Info[V];
72 #if !BALANCE_IDOM_TREE
73 // Higher-complexity but faster implementation
74 if (VInfo.Ancestor == 0)
75 return V;
76 DTCompress(DT, V);
77 return VInfo.Label;
78 #else
79 // Lower-complexity but slower implementation
80 if (VInfo.Ancestor == 0)
81 return VInfo.Label;
82 DTCompress(DT, V);
83 BasicBlock *VLabel = VInfo.Label;
84
85 BasicBlock *VAncestorLabel = DT.Info[VInfo.Ancestor].Label;
86 if (DT.Info[VAncestorLabel].Semi >= DT.Info[VLabel].Semi)
87 return VLabel;
88 else
89 return VAncestorLabel;
90 #endif
91 }
92
93 void DTLink(DominatorTree& DT, BasicBlock *V, BasicBlock *W,
94 DominatorTree::InfoRec &WInfo) {
95 #if !BALANCE_IDOM_TREE
96 // Higher-complexity but faster implementation
97 WInfo.Ancestor = V;
98 #else
99 // Lower-complexity but slower implementation
100 BasicBlock *WLabel = WInfo.Label;
101 unsigned WLabelSemi = Info[WLabel].Semi;
102 BasicBlock *S = W;
103 InfoRec *SInfo = &Info[S];
104
105 BasicBlock *SChild = SInfo->Child;
106 InfoRec *SChildInfo = &Info[SChild];
107
108 while (WLabelSemi < Info[SChildInfo->Label].Semi) {
109 BasicBlock *SChildChild = SChildInfo->Child;
110 if (SInfo->Size+Info[SChildChild].Size >= 2*SChildInfo->Size) {
111 SChildInfo->Ancestor = S;
112 SInfo->Child = SChild = SChildChild;
113 SChildInfo = &Info[SChild];
114 } else {
115 SChildInfo->Size = SInfo->Size;
116 S = SInfo->Ancestor = SChild;
117 SInfo = SChildInfo;
118 SChild = SChildChild;
119 SChildInfo = &Info[SChild];
120 }
121 }
122
123 InfoRec &VInfo = Info[V];
124 SInfo->Label = WLabel;
125
126 assert(V != W && "The optimization here will not work in this case!");
127 unsigned WSize = WInfo.Size;
128 unsigned VSize = (VInfo.Size += WSize);
129
130 if (VSize < 2*WSize)
131 std::swap(S, VInfo.Child);
132
133 while (S) {
134 SInfo = &Info[S];
135 SInfo->Ancestor = V;
136 S = SInfo->Child;
137 }
138 #endif
139 }
140
14135 void DTcalculate(DominatorTree& DT, Function &F) {
14236 BasicBlock* Root = DT.Roots[0];
14337
15751 // Step #2: Calculate the semidominators of all vertices
15852 for (pred_iterator PI = pred_begin(W), E = pred_end(W); PI != E; ++PI)
15953 if (DT.Info.count(*PI)) { // Only if this predecessor is reachable!
160 unsigned SemiU = DT.Info[DTEval(DT, *PI)].Semi;
54 unsigned SemiU = DT.Info[Eval(DT, *PI)].Semi;
16155 if (SemiU < WInfo.Semi)
16256 WInfo.Semi = SemiU;
16357 }
16559 DT.Info[DT.Vertex[WInfo.Semi]].Bucket.push_back(W);
16660
16761 BasicBlock *WParent = WInfo.Parent;
168 DTLink(DT, WParent, W, WInfo);
62 Link(DT, WParent, W, WInfo);
16963
17064 // Step #3: Implicitly define the immediate dominator of vertices
17165 std::vector &WParentBucket = DT.Info[WParent].Bucket;
17266 while (!WParentBucket.empty()) {
17367 BasicBlock *V = WParentBucket.back();
17468 WParentBucket.pop_back();
175 BasicBlock *U = DTEval(DT, V);
69 BasicBlock *U = Eval(DT, V);
17670 DT.IDoms[V] = DT.Info[U].Semi < DT.Info[V].Semi ? U : WParent;
17771 }
17872 }
0 //==- DominatorInternals.cpp - 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 #ifndef LLVM_ANALYSIS_DOMINATOR_INTERNALS_H
10 #define LLVM_ANALYSIS_DOMINATOR_INTERNALS_H
11
12 #include "llvm/Analysis/Dominators.h"
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/SmallPtrSet.h"
15 //===----------------------------------------------------------------------===//
16 //
17 // DominatorTree construction - This pass constructs immediate dominator
18 // information for a flow-graph based on the algorithm described in this
19 // document:
20 //
21 // A Fast Algorithm for Finding Dominators in a Flowgraph
22 // T. Lengauer & R. Tarjan, ACM TOPLAS July 1979, pgs 121-141.
23 //
24 // This implements both the O(n*ack(n)) and the O(n*log(n)) versions of EVAL and
25 // LINK, but it turns out that the theoretically slower O(n*log(n))
26 // implementation is actually faster than the "efficient" algorithm (even for
27 // large CFGs) because the constant overheads are substantially smaller. The
28 // lower-complexity version can be enabled with the following #define:
29 //
30 #define BALANCE_IDOM_TREE 0
31 //
32 //===----------------------------------------------------------------------===//
33
34 namespace llvm {
35
36 void Compress(DominatorTreeBase& DT, BasicBlock *VIn) {
37
38 std::vector Work;
39 SmallPtrSet Visited;
40 BasicBlock *VInAncestor = DT.Info[VIn].Ancestor;
41 DominatorTreeBase::InfoRec &VInVAInfo = DT.Info[VInAncestor];
42
43 if (VInVAInfo.Ancestor != 0)
44 Work.push_back(VIn);
45
46 while (!Work.empty()) {
47 BasicBlock *V = Work.back();
48 DominatorTree::InfoRec &VInfo = DT.Info[V];
49 BasicBlock *VAncestor = VInfo.Ancestor;
50 DominatorTreeBase::InfoRec &VAInfo = DT.Info[VAncestor];
51
52 // Process Ancestor first
53 if (Visited.insert(VAncestor) &&
54 VAInfo.Ancestor != 0) {
55 Work.push_back(VAncestor);
56 continue;
57 }
58 Work.pop_back();
59
60 // Update VInfo based on Ancestor info
61 if (VAInfo.Ancestor == 0)
62 continue;
63 BasicBlock *VAncestorLabel = VAInfo.Label;
64 BasicBlock *VLabel = VInfo.Label;
65 if (DT.Info[VAncestorLabel].Semi < DT.Info[VLabel].Semi)
66 VInfo.Label = VAncestorLabel;
67 VInfo.Ancestor = VAInfo.Ancestor;
68 }
69 }
70
71 BasicBlock *Eval(DominatorTreeBase& DT, BasicBlock *V) {
72 DominatorTreeBase::InfoRec &VInfo = DT.Info[V];
73 #if !BALANCE_IDOM_TREE
74 // Higher-complexity but faster implementation
75 if (VInfo.Ancestor == 0)
76 return V;
77 Compress(DT, V);
78 return VInfo.Label;
79 #else
80 // Lower-complexity but slower implementation
81 if (VInfo.Ancestor == 0)
82 return VInfo.Label;
83 Compress(DT, V);
84 BasicBlock *VLabel = VInfo.Label;
85
86 BasicBlock *VAncestorLabel = DT.Info[VInfo.Ancestor].Label;
87 if (DT.Info[VAncestorLabel].Semi >= DT.Info[VLabel].Semi)
88 return VLabel;
89 else
90 return VAncestorLabel;
91 #endif
92 }
93
94 void Link(DominatorTreeBase& DT, BasicBlock *V, BasicBlock *W,
95 DominatorTreeBase::InfoRec &WInfo) {
96 #if !BALANCE_IDOM_TREE
97 // Higher-complexity but faster implementation
98 WInfo.Ancestor = V;
99 #else
100 // Lower-complexity but slower implementation
101 BasicBlock *WLabel = WInfo.Label;
102 unsigned WLabelSemi = DT.Info[WLabel].Semi;
103 BasicBlock *S = W;
104 InfoRec *SInfo = &DT.Info[S];
105
106 BasicBlock *SChild = SInfo->Child;
107 InfoRec *SChildInfo = &DT.Info[SChild];
108
109 while (WLabelSemi < DT.Info[SChildInfo->Label].Semi) {
110 BasicBlock *SChildChild = SChildInfo->Child;
111 if (SInfo->Size+DT.Info[SChildChild].Size >= 2*SChildInfo->Size) {
112 SChildInfo->Ancestor = S;
113 SInfo->Child = SChild = SChildChild;
114 SChildInfo = &DT.Info[SChild];
115 } else {
116 SChildInfo->Size = SInfo->Size;
117 S = SInfo->Ancestor = SChild;
118 SInfo = SChildInfo;
119 SChild = SChildChild;
120 SChildInfo = &DT.Info[SChild];
121 }
122 }
123
124 DominatorTreeBase::InfoRec &VInfo = DT.Info[V];
125 SInfo->Label = WLabel;
126
127 assert(V != W && "The optimization here will not work in this case!");
128 unsigned WSize = WInfo.Size;
129 unsigned VSize = (VInfo.Size += WSize);
130
131 if (VSize < 2*WSize)
132 std::swap(S, VInfo.Child);
133
134 while (S) {
135 SInfo = &DT.Info[S];
136 SInfo->Ancestor = V;
137 S = SInfo->Child;
138 }
139 #endif
140 }
141
142 }
143
144 #endif