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/DominanceFrontier.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/Assembly/Writer.h"
27 #include "llvm/Instructions.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/Support/CommandLine.h"
33 // Always verify dominfo if expensive checking is enabled.
35 static bool VerifyDomInfo = true;
37 static bool VerifyDomInfo = false;
39 static cl::opt<bool,true>
40 VerifyDomInfoX("verify-dom-info", cl::location(VerifyDomInfo),
41 cl::desc("Verify dominator info (time consuming)"));
43 //===----------------------------------------------------------------------===//
44 // DominatorTree Implementation
45 //===----------------------------------------------------------------------===//
47 // Provide public access to DominatorTree information. Implementation details
48 // can be found in DominatorCalculation.h.
50 //===----------------------------------------------------------------------===//
52 TEMPLATE_INSTANTIATION(class llvm::DomTreeNodeBase<BasicBlock>);
53 TEMPLATE_INSTANTIATION(class llvm::DominatorTreeBase<BasicBlock>);
55 char DominatorTree::ID = 0;
56 INITIALIZE_PASS(DominatorTree, "domtree",
57 "Dominator Tree Construction", true, true)
59 bool DominatorTree::runOnFunction(Function &F) {
64 void DominatorTree::verifyAnalysis() const {
65 if (!VerifyDomInfo) return;
67 Function &F = *getRoot()->getParent();
69 DominatorTree OtherDT;
70 OtherDT.getBase().recalculate(F);
71 if (compare(OtherDT)) {
72 errs() << "DominatorTree is not up to date! Computed:\n";
75 errs() << "\nActual:\n";
76 OtherDT.print(errs());
81 void DominatorTree::print(raw_ostream &OS, const Module *) const {
85 // dominates - Return true if A dominates a use in B. This performs the
86 // special checks necessary if A and B are in the same basic block.
87 bool DominatorTree::dominates(const Instruction *A, const Instruction *B) const{
88 const BasicBlock *BBA = A->getParent(), *BBB = B->getParent();
90 // If A is an invoke instruction, its value is only available in this normal
92 if (const InvokeInst *II = dyn_cast<InvokeInst>(A))
93 BBA = II->getNormalDest();
95 if (BBA != BBB) return dominates(BBA, BBB);
97 // It is not possible to determine dominance between two PHI nodes
98 // based on their ordering.
99 if (isa<PHINode>(A) && isa<PHINode>(B))
102 // Loop through the basic block until we find A or B.
103 BasicBlock::const_iterator I = BBA->begin();
104 for (; &*I != A && &*I != B; ++I)
112 //===----------------------------------------------------------------------===//
113 // DominanceFrontier Implementation
114 //===----------------------------------------------------------------------===//
116 char DominanceFrontier::ID = 0;
117 INITIALIZE_PASS_BEGIN(DominanceFrontier, "domfrontier",
118 "Dominance Frontier Construction", true, true)
119 INITIALIZE_PASS_DEPENDENCY(DominatorTree)
120 INITIALIZE_PASS_END(DominanceFrontier, "domfrontier",
121 "Dominance Frontier Construction", true, true)
124 class DFCalculateWorkObject {
126 DFCalculateWorkObject(BasicBlock *B, BasicBlock *P,
127 const DomTreeNode *N,
128 const DomTreeNode *PN)
129 : currentBB(B), parentBB(P), Node(N), parentNode(PN) {}
130 BasicBlock *currentBB;
131 BasicBlock *parentBB;
132 const DomTreeNode *Node;
133 const DomTreeNode *parentNode;
137 const DominanceFrontier::DomSetType &
138 DominanceFrontier::calculate(const DominatorTree &DT,
139 const DomTreeNode *Node) {
140 BasicBlock *BB = Node->getBlock();
141 DomSetType *Result = NULL;
143 std::vector<DFCalculateWorkObject> workList;
144 SmallPtrSet<BasicBlock *, 32> visited;
146 workList.push_back(DFCalculateWorkObject(BB, NULL, Node, NULL));
148 DFCalculateWorkObject *currentW = &workList.back();
149 assert (currentW && "Missing work object.");
151 BasicBlock *currentBB = currentW->currentBB;
152 BasicBlock *parentBB = currentW->parentBB;
153 const DomTreeNode *currentNode = currentW->Node;
154 const DomTreeNode *parentNode = currentW->parentNode;
155 assert (currentBB && "Invalid work object. Missing current Basic Block");
156 assert (currentNode && "Invalid work object. Missing current Node");
157 DomSetType &S = Frontiers[currentBB];
159 // Visit each block only once.
160 if (visited.count(currentBB) == 0) {
161 visited.insert(currentBB);
163 // Loop over CFG successors to calculate DFlocal[currentNode]
164 for (succ_iterator SI = succ_begin(currentBB), SE = succ_end(currentBB);
166 // Does Node immediately dominate this successor?
167 if (DT[*SI]->getIDom() != currentNode)
172 // At this point, S is DFlocal. Now we union in DFup's of our children...
173 // Loop through and visit the nodes that Node immediately dominates (Node's
174 // children in the IDomTree)
175 bool visitChild = false;
176 for (DomTreeNode::const_iterator NI = currentNode->begin(),
177 NE = currentNode->end(); NI != NE; ++NI) {
178 DomTreeNode *IDominee = *NI;
179 BasicBlock *childBB = IDominee->getBlock();
180 if (visited.count(childBB) == 0) {
181 workList.push_back(DFCalculateWorkObject(childBB, currentBB,
182 IDominee, currentNode));
187 // If all children are visited or there is any child then pop this block
188 // from the workList.
196 DomSetType::const_iterator CDFI = S.begin(), CDFE = S.end();
197 DomSetType &parentSet = Frontiers[parentBB];
198 for (; CDFI != CDFE; ++CDFI) {
199 if (!DT.properlyDominates(parentNode, DT[*CDFI]))
200 parentSet.insert(*CDFI);
205 } while (!workList.empty());
210 void DominanceFrontierBase::print(raw_ostream &OS, const Module* ) const {
211 for (const_iterator I = begin(), E = end(); I != E; ++I) {
212 OS << " DomFrontier for BB ";
214 WriteAsOperand(OS, I->first, false);
216 OS << " <<exit node>>";
219 const std::set<BasicBlock*> &BBs = I->second;
221 for (std::set<BasicBlock*>::const_iterator I = BBs.begin(), E = BBs.end();
225 WriteAsOperand(OS, *I, false);
227 OS << "<<exit node>>";
233 void DominanceFrontierBase::dump() const {