1 //==- DominatorInternals.cpp - Dominator Calculation -------------*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
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.
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
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 //===----------------------------------------------------------------------===//
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)
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
);
61 // Update VInfo based on Ancestor info
62 if (VAInfo
.Ancestor
== 0)
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)
81 // Lower-complexity but slower implementation
82 if (VInfo
.Ancestor
== 0)
85 BasicBlock
*VLabel
= VInfo
.Label
;
87 BasicBlock
*VAncestorLabel
= DT
.Info
[VInfo
.Ancestor
].Label
;
88 if (DT
.Info
[VAncestorLabel
].Semi
>= DT
.Info
[VLabel
].Semi
)
91 return VAncestorLabel
;
95 void Link(DominatorTreeBase
& DT
, BasicBlock
*V
, BasicBlock
*W
,
96 DominatorTreeBase::InfoRec
&WInfo
) {
97 #if !BALANCE_IDOM_TREE
98 // Higher-complexity but faster implementation
101 // Lower-complexity but slower implementation
102 BasicBlock
*WLabel
= WInfo
.Label
;
103 unsigned WLabelSemi
= DT
.Info
[WLabel
].Semi
;
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
];
117 SChildInfo
->Size
= SInfo
->Size
;
118 S
= SInfo
->Ancestor
= SChild
;
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
);
133 std::swap(S
, VInfo
.Child
);