Convert DFSPass into a templated friend function, in preparation for making it common...
[oota-llvm.git] / lib / VMCore / DominatorInternals.cpp
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 //===----------------------------------------------------------------------===//
9
10 #ifndef LIB_LLVM_ANALYSIS_DOMINATOR_INTERNALS_H
11 #define LIB_LLVM_ANALYSIS_DOMINATOR_INTERNALS_H
12
13 #include "llvm/Analysis/Dominators.h"
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/ADT/SmallPtrSet.h"
16 //===----------------------------------------------------------------------===//
17 //
18 // DominatorTree construction - This pass constructs immediate dominator
19 // information for a flow-graph based on the algorithm described in this
20 // document:
21 //
22 //   A Fast Algorithm for Finding Dominators in a Flowgraph
23 //   T. Lengauer & R. Tarjan, ACM TOPLAS July 1979, pgs 121-141.
24 //
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:
30 //
31 #define BALANCE_IDOM_TREE 0
32 //
33 //===----------------------------------------------------------------------===//
34
35 namespace llvm {
36
37 void Compress(DominatorTreeBase& DT, BasicBlock *VIn) {
38
39   std::vector<BasicBlock *> Work;
40   SmallPtrSet<BasicBlock *, 32> Visited;
41   BasicBlock *VInAncestor = DT.Info[VIn].Ancestor;
42   DominatorTreeBase::InfoRec &VInVAInfo = DT.Info[VInAncestor];
43
44   if (VInVAInfo.Ancestor != 0)
45     Work.push_back(VIn);
46   
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];
52
53     // Process Ancestor first
54     if (Visited.insert(VAncestor) &&
55         VAInfo.Ancestor != 0) {
56       Work.push_back(VAncestor);
57       continue;
58     } 
59     Work.pop_back(); 
60
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;
69   }
70 }
71
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;
86
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
93 }
94
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];
106
107   BasicBlock *SChild = SInfo->Child;
108   InfoRec *SChildInfo = &DT.Info[SChild];
109
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];
122     }
123   }
124
125   DominatorTreeBase::InfoRec &VInfo = DT.Info[V];
126   SInfo->Label = WLabel;
127
128   assert(V != W && "The optimization here will not work in this case!");
129   unsigned WSize = WInfo.Size;
130   unsigned VSize = (VInfo.Size += WSize);
131
132   if (VSize < 2*WSize)
133     std::swap(S, VInfo.Child);
134
135   while (S) {
136     SInfo = &DT.Info[S];
137     SInfo->Ancestor = V;
138     S = SInfo->Child;
139   }
140 #endif
141 }
142
143 }
144
145 #endif