1 //===- Dominators.cpp - Dominator Calculation -----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements simple dominator construction algorithms for finding
11 // forward dominators. Postdominators are available in libanalysis, but are not
12 // included in libvmcore, because it's not needed. Forward dominators are
13 // needed to support the Verifier pass.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Analysis/Dominators.h"
18 #include "llvm/Support/CFG.h"
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/ADT/DepthFirstIterator.h"
22 #include "llvm/ADT/SetOperations.h"
23 #include "llvm/ADT/SmallPtrSet.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/Analysis/DominatorInternals.h"
26 #include "llvm/Instructions.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/Support/CommandLine.h"
32 // Always verify dominfo if expensive checking is enabled.
34 static bool VerifyDomInfo
= true;
36 static bool VerifyDomInfo
= false;
38 static cl::opt
<bool,true>
39 VerifyDomInfoX("verify-dom-info", cl::location(VerifyDomInfo
),
40 cl::desc("Verify dominator info (time consuming)"));
42 //===----------------------------------------------------------------------===//
43 // DominatorTree Implementation
44 //===----------------------------------------------------------------------===//
46 // Provide public access to DominatorTree information. Implementation details
47 // can be found in DominatorCalculation.h.
49 //===----------------------------------------------------------------------===//
51 TEMPLATE_INSTANTIATION(class llvm::DomTreeNodeBase
<BasicBlock
>);
52 TEMPLATE_INSTANTIATION(class llvm::DominatorTreeBase
<BasicBlock
>);
54 char DominatorTree::ID
= 0;
55 INITIALIZE_PASS(DominatorTree
, "domtree",
56 "Dominator Tree Construction", true, true)
58 bool DominatorTree::runOnFunction(Function
&F
) {
63 void DominatorTree::verifyAnalysis() const {
64 if (!VerifyDomInfo
) return;
66 Function
&F
= *getRoot()->getParent();
68 DominatorTree OtherDT
;
69 OtherDT
.getBase().recalculate(F
);
70 assert(!compare(OtherDT
) && "Invalid DominatorTree info!");
73 void DominatorTree::print(raw_ostream
&OS
, const Module
*) const {
77 // dominates - Return true if A dominates a use in B. This performs the
78 // special checks necessary if A and B are in the same basic block.
79 bool DominatorTree::dominates(const Instruction
*A
, const Instruction
*B
) const{
80 const BasicBlock
*BBA
= A
->getParent(), *BBB
= B
->getParent();
82 // If A is an invoke instruction, its value is only available in this normal
84 if (const InvokeInst
*II
= dyn_cast
<InvokeInst
>(A
))
85 BBA
= II
->getNormalDest();
87 if (BBA
!= BBB
) return dominates(BBA
, BBB
);
89 // It is not possible to determine dominance between two PHI nodes
90 // based on their ordering.
91 if (isa
<PHINode
>(A
) && isa
<PHINode
>(B
))
94 // Loop through the basic block until we find A or B.
95 BasicBlock::const_iterator I
= BBA
->begin();
96 for (; &*I
!= A
&& &*I
!= B
; ++I
)
104 //===----------------------------------------------------------------------===//
105 // DominanceFrontier Implementation
106 //===----------------------------------------------------------------------===//
108 char DominanceFrontier::ID
= 0;
109 INITIALIZE_PASS_BEGIN(DominanceFrontier
, "domfrontier",
110 "Dominance Frontier Construction", true, true)
111 INITIALIZE_PASS_DEPENDENCY(DominatorTree
)
112 INITIALIZE_PASS_END(DominanceFrontier
, "domfrontier",
113 "Dominance Frontier Construction", true, true)
115 void DominanceFrontier::verifyAnalysis() const {
116 if (!VerifyDomInfo
) return;
118 DominatorTree
&DT
= getAnalysis
<DominatorTree
>();
120 DominanceFrontier OtherDF
;
121 const std::vector
<BasicBlock
*> &DTRoots
= DT
.getRoots();
122 OtherDF
.calculate(DT
, DT
.getNode(DTRoots
[0]));
123 assert(!compare(OtherDF
) && "Invalid DominanceFrontier info!");
126 // NewBB is split and now it has one successor. Update dominance frontier to
127 // reflect this change.
128 void DominanceFrontier::splitBlock(BasicBlock
*NewBB
) {
129 assert(NewBB
->getTerminator()->getNumSuccessors() == 1 &&
130 "NewBB should have a single successor!");
131 BasicBlock
*NewBBSucc
= NewBB
->getTerminator()->getSuccessor(0);
133 // NewBBSucc inherits original NewBB frontier.
134 DominanceFrontier::iterator NewBBI
= find(NewBB
);
136 addBasicBlock(NewBBSucc
, NewBBI
->second
);
138 // If NewBB dominates NewBBSucc, then DF(NewBB) is now going to be the
139 // DF(NewBBSucc) without the stuff that the new block does not dominate
141 DominatorTree
&DT
= getAnalysis
<DominatorTree
>();
142 DomTreeNode
*NewBBNode
= DT
.getNode(NewBB
);
143 DomTreeNode
*NewBBSuccNode
= DT
.getNode(NewBBSucc
);
144 if (DT
.dominates(NewBBNode
, NewBBSuccNode
)) {
145 DominanceFrontier::iterator DFI
= find(NewBBSucc
);
147 DominanceFrontier::DomSetType Set
= DFI
->second
;
148 // Filter out stuff in Set that we do not dominate a predecessor of.
149 for (DominanceFrontier::DomSetType::iterator SetI
= Set
.begin(),
150 E
= Set
.end(); SetI
!= E
;) {
151 bool DominatesPred
= false;
152 for (pred_iterator PI
= pred_begin(*SetI
), E
= pred_end(*SetI
);
154 if (DT
.dominates(NewBBNode
, DT
.getNode(*PI
))) {
155 DominatesPred
= true;
164 if (NewBBI
!= end()) {
165 for (DominanceFrontier::DomSetType::iterator SetI
= Set
.begin(),
166 E
= Set
.end(); SetI
!= E
; ++SetI
) {
167 BasicBlock
*SB
= *SetI
;
168 addToFrontier(NewBBI
, SB
);
171 addBasicBlock(NewBB
, Set
);
175 // DF(NewBB) is {NewBBSucc} because NewBB does not strictly dominate
176 // NewBBSucc, but it does dominate itself (and there is an edge (NewBB ->
177 // NewBBSucc)). NewBBSucc is the single successor of NewBB.
178 DominanceFrontier::DomSetType NewDFSet
;
179 NewDFSet
.insert(NewBBSucc
);
180 addBasicBlock(NewBB
, NewDFSet
);
183 // Now update dominance frontiers which either used to contain NewBBSucc
184 // or which now need to include NewBB.
186 // Collect the set of blocks which dominate a predecessor of NewBB or
187 // NewSuccBB and which don't dominate both. This is an initial
188 // approximation of the blocks whose dominance frontiers will need updates.
189 SmallVector
<DomTreeNode
*, 16> AllPredDoms
;
191 // Compute the block which dominates both NewBBSucc and NewBB. This is
192 // the immediate dominator of NewBBSucc unless NewBB dominates NewBBSucc.
193 // The code below which climbs dominator trees will stop at this point,
194 // because from this point up, dominance frontiers are unaffected.
195 DomTreeNode
*DominatesBoth
= 0;
197 DominatesBoth
= NewBBSuccNode
->getIDom();
198 if (DominatesBoth
== NewBBNode
)
199 DominatesBoth
= NewBBNode
->getIDom();
202 // Collect the set of all blocks which dominate a predecessor of NewBB.
203 SmallPtrSet
<DomTreeNode
*, 8> NewBBPredDoms
;
204 for (pred_iterator PI
= pred_begin(NewBB
), E
= pred_end(NewBB
); PI
!= E
; ++PI
)
205 for (DomTreeNode
*DTN
= DT
.getNode(*PI
); DTN
; DTN
= DTN
->getIDom()) {
206 if (DTN
== DominatesBoth
)
208 if (!NewBBPredDoms
.insert(DTN
))
210 AllPredDoms
.push_back(DTN
);
213 // Collect the set of all blocks which dominate a predecessor of NewSuccBB.
214 SmallPtrSet
<DomTreeNode
*, 8> NewBBSuccPredDoms
;
215 for (pred_iterator PI
= pred_begin(NewBBSucc
),
216 E
= pred_end(NewBBSucc
); PI
!= E
; ++PI
)
217 for (DomTreeNode
*DTN
= DT
.getNode(*PI
); DTN
; DTN
= DTN
->getIDom()) {
218 if (DTN
== DominatesBoth
)
220 if (!NewBBSuccPredDoms
.insert(DTN
))
222 if (!NewBBPredDoms
.count(DTN
))
223 AllPredDoms
.push_back(DTN
);
226 // Visit all relevant dominance frontiers and make any needed updates.
227 for (SmallVectorImpl
<DomTreeNode
*>::const_iterator I
= AllPredDoms
.begin(),
228 E
= AllPredDoms
.end(); I
!= E
; ++I
) {
229 DomTreeNode
*DTN
= *I
;
230 iterator DFI
= find((*I
)->getBlock());
232 // Only consider nodes that have NewBBSucc in their dominator frontier.
233 if (DFI
== end() || !DFI
->second
.count(NewBBSucc
)) continue;
235 // If the block dominates a predecessor of NewBB but does not properly
236 // dominate NewBB itself, add NewBB to its dominance frontier.
237 if (NewBBPredDoms
.count(DTN
) &&
238 !DT
.properlyDominates(DTN
, NewBBNode
))
239 addToFrontier(DFI
, NewBB
);
241 // If the block does not dominate a predecessor of NewBBSucc or
242 // properly dominates NewBBSucc itself, remove NewBBSucc from its
243 // dominance frontier.
244 if (!NewBBSuccPredDoms
.count(DTN
) ||
245 DT
.properlyDominates(DTN
, NewBBSuccNode
))
246 removeFromFrontier(DFI
, NewBBSucc
);
251 class DFCalculateWorkObject
{
253 DFCalculateWorkObject(BasicBlock
*B
, BasicBlock
*P
,
254 const DomTreeNode
*N
,
255 const DomTreeNode
*PN
)
256 : currentBB(B
), parentBB(P
), Node(N
), parentNode(PN
) {}
257 BasicBlock
*currentBB
;
258 BasicBlock
*parentBB
;
259 const DomTreeNode
*Node
;
260 const DomTreeNode
*parentNode
;
264 const DominanceFrontier::DomSetType
&
265 DominanceFrontier::calculate(const DominatorTree
&DT
,
266 const DomTreeNode
*Node
) {
267 BasicBlock
*BB
= Node
->getBlock();
268 DomSetType
*Result
= NULL
;
270 std::vector
<DFCalculateWorkObject
> workList
;
271 SmallPtrSet
<BasicBlock
*, 32> visited
;
273 workList
.push_back(DFCalculateWorkObject(BB
, NULL
, Node
, NULL
));
275 DFCalculateWorkObject
*currentW
= &workList
.back();
276 assert (currentW
&& "Missing work object.");
278 BasicBlock
*currentBB
= currentW
->currentBB
;
279 BasicBlock
*parentBB
= currentW
->parentBB
;
280 const DomTreeNode
*currentNode
= currentW
->Node
;
281 const DomTreeNode
*parentNode
= currentW
->parentNode
;
282 assert (currentBB
&& "Invalid work object. Missing current Basic Block");
283 assert (currentNode
&& "Invalid work object. Missing current Node");
284 DomSetType
&S
= Frontiers
[currentBB
];
286 // Visit each block only once.
287 if (visited
.count(currentBB
) == 0) {
288 visited
.insert(currentBB
);
290 // Loop over CFG successors to calculate DFlocal[currentNode]
291 for (succ_iterator SI
= succ_begin(currentBB
), SE
= succ_end(currentBB
);
293 // Does Node immediately dominate this successor?
294 if (DT
[*SI
]->getIDom() != currentNode
)
299 // At this point, S is DFlocal. Now we union in DFup's of our children...
300 // Loop through and visit the nodes that Node immediately dominates (Node's
301 // children in the IDomTree)
302 bool visitChild
= false;
303 for (DomTreeNode::const_iterator NI
= currentNode
->begin(),
304 NE
= currentNode
->end(); NI
!= NE
; ++NI
) {
305 DomTreeNode
*IDominee
= *NI
;
306 BasicBlock
*childBB
= IDominee
->getBlock();
307 if (visited
.count(childBB
) == 0) {
308 workList
.push_back(DFCalculateWorkObject(childBB
, currentBB
,
309 IDominee
, currentNode
));
314 // If all children are visited or there is any child then pop this block
315 // from the workList.
323 DomSetType::const_iterator CDFI
= S
.begin(), CDFE
= S
.end();
324 DomSetType
&parentSet
= Frontiers
[parentBB
];
325 for (; CDFI
!= CDFE
; ++CDFI
) {
326 if (!DT
.properlyDominates(parentNode
, DT
[*CDFI
]))
327 parentSet
.insert(*CDFI
);
332 } while (!workList
.empty());
337 void DominanceFrontierBase::print(raw_ostream
&OS
, const Module
* ) const {
338 for (const_iterator I
= begin(), E
= end(); I
!= E
; ++I
) {
339 OS
<< " DomFrontier for BB ";
341 WriteAsOperand(OS
, I
->first
, false);
343 OS
<< " <<exit node>>";
346 const std::set
<BasicBlock
*> &BBs
= I
->second
;
348 for (std::set
<BasicBlock
*>::const_iterator I
= BBs
.begin(), E
= BBs
.end();
352 WriteAsOperand(OS
, *I
, false);
354 OS
<< "<<exit node>>";
360 void DominanceFrontierBase::dump() const {