Couple of fixes to mention bunzip2 and make instructions more clear.
[llvm-complete.git] / lib / VMCore / DominatorInternals.cpp
blobb40e2d97e2d76699922eb283a987b825416e9eaf
1 //==- DominatorInternals.cpp - Dominator Calculation -------------*- C++ -*-==//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Owen Anderson and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_ANALYSIS_DOMINATOR_INTERNALS_H
11 #define LLVM_ANALYSIS_DOMINATOR_INTERNALS_H
13 #include "llvm/Analysis/Dominators.h"
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/ADT/SmallPtrSet.h"
16 //===----------------------------------------------------------------------===//
18 // DominatorTree construction - This pass constructs immediate dominator
19 // information for a flow-graph based on the algorithm described in this
20 // document:
22 // A Fast Algorithm for Finding Dominators in a Flowgraph
23 // T. Lengauer & R. Tarjan, ACM TOPLAS July 1979, pgs 121-141.
25 // This implements both the O(n*ack(n)) and the O(n*log(n)) versions of EVAL and
26 // LINK, but it turns out that the theoretically slower O(n*log(n))
27 // implementation is actually faster than the "efficient" algorithm (even for
28 // large CFGs) because the constant overheads are substantially smaller. The
29 // lower-complexity version can be enabled with the following #define:
31 #define BALANCE_IDOM_TREE 0
33 //===----------------------------------------------------------------------===//
35 namespace llvm {
37 void Compress(DominatorTreeBase& DT, BasicBlock *VIn) {
39 std::vector<BasicBlock *> Work;
40 SmallPtrSet<BasicBlock *, 32> Visited;
41 BasicBlock *VInAncestor = DT.Info[VIn].Ancestor;
42 DominatorTreeBase::InfoRec &VInVAInfo = DT.Info[VInAncestor];
44 if (VInVAInfo.Ancestor != 0)
45 Work.push_back(VIn);
47 while (!Work.empty()) {
48 BasicBlock *V = Work.back();
49 DominatorTree::InfoRec &VInfo = DT.Info[V];
50 BasicBlock *VAncestor = VInfo.Ancestor;
51 DominatorTreeBase::InfoRec &VAInfo = DT.Info[VAncestor];
53 // Process Ancestor first
54 if (Visited.insert(VAncestor) &&
55 VAInfo.Ancestor != 0) {
56 Work.push_back(VAncestor);
57 continue;
59 Work.pop_back();
61 // Update VInfo based on Ancestor info
62 if (VAInfo.Ancestor == 0)
63 continue;
64 BasicBlock *VAncestorLabel = VAInfo.Label;
65 BasicBlock *VLabel = VInfo.Label;
66 if (DT.Info[VAncestorLabel].Semi < DT.Info[VLabel].Semi)
67 VInfo.Label = VAncestorLabel;
68 VInfo.Ancestor = VAInfo.Ancestor;
72 BasicBlock *Eval(DominatorTreeBase& DT, BasicBlock *V) {
73 DominatorTreeBase::InfoRec &VInfo = DT.Info[V];
74 #if !BALANCE_IDOM_TREE
75 // Higher-complexity but faster implementation
76 if (VInfo.Ancestor == 0)
77 return V;
78 Compress(DT, V);
79 return VInfo.Label;
80 #else
81 // Lower-complexity but slower implementation
82 if (VInfo.Ancestor == 0)
83 return VInfo.Label;
84 Compress(DT, V);
85 BasicBlock *VLabel = VInfo.Label;
87 BasicBlock *VAncestorLabel = DT.Info[VInfo.Ancestor].Label;
88 if (DT.Info[VAncestorLabel].Semi >= DT.Info[VLabel].Semi)
89 return VLabel;
90 else
91 return VAncestorLabel;
92 #endif
95 void Link(DominatorTreeBase& DT, BasicBlock *V, BasicBlock *W,
96 DominatorTreeBase::InfoRec &WInfo) {
97 #if !BALANCE_IDOM_TREE
98 // Higher-complexity but faster implementation
99 WInfo.Ancestor = V;
100 #else
101 // Lower-complexity but slower implementation
102 BasicBlock *WLabel = WInfo.Label;
103 unsigned WLabelSemi = DT.Info[WLabel].Semi;
104 BasicBlock *S = W;
105 InfoRec *SInfo = &DT.Info[S];
107 BasicBlock *SChild = SInfo->Child;
108 InfoRec *SChildInfo = &DT.Info[SChild];
110 while (WLabelSemi < DT.Info[SChildInfo->Label].Semi) {
111 BasicBlock *SChildChild = SChildInfo->Child;
112 if (SInfo->Size+DT.Info[SChildChild].Size >= 2*SChildInfo->Size) {
113 SChildInfo->Ancestor = S;
114 SInfo->Child = SChild = SChildChild;
115 SChildInfo = &DT.Info[SChild];
116 } else {
117 SChildInfo->Size = SInfo->Size;
118 S = SInfo->Ancestor = SChild;
119 SInfo = SChildInfo;
120 SChild = SChildChild;
121 SChildInfo = &DT.Info[SChild];
125 DominatorTreeBase::InfoRec &VInfo = DT.Info[V];
126 SInfo->Label = WLabel;
128 assert(V != W && "The optimization here will not work in this case!");
129 unsigned WSize = WInfo.Size;
130 unsigned VSize = (VInfo.Size += WSize);
132 if (VSize < 2*WSize)
133 std::swap(S, VInfo.Child);
135 while (S) {
136 SInfo = &DT.Info[S];
137 SInfo->Ancestor = V;
138 S = SInfo->Child;
140 #endif
145 #endif