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/ADT/DepthFirstIterator.h"
21 #include "llvm/ADT/SetOperations.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/Analysis/DominatorInternals.h"
25 #include "llvm/Instructions.h"
26 #include "llvm/Support/raw_ostream.h"
30 //===----------------------------------------------------------------------===//
31 // DominatorTree Implementation
32 //===----------------------------------------------------------------------===//
34 // Provide public access to DominatorTree information. Implementation details
35 // can be found in DominatorCalculation.h.
37 //===----------------------------------------------------------------------===//
39 TEMPLATE_INSTANTIATION(class DomTreeNodeBase<BasicBlock>);
40 TEMPLATE_INSTANTIATION(class DominatorTreeBase<BasicBlock>);
42 char DominatorTree::ID = 0;
43 static RegisterPass<DominatorTree>
44 E("domtree", "Dominator Tree Construction", true, true);
46 bool DominatorTree::runOnFunction(Function &F) {
51 void DominatorTree::print(raw_ostream &OS, const Module *) const {
55 // dominates - Return true if A dominates a use in B. This performs the
56 // special checks necessary if A and B are in the same basic block.
57 bool DominatorTree::dominates(const Instruction *A, const Instruction *B) const{
58 const BasicBlock *BBA = A->getParent(), *BBB = B->getParent();
60 // If A is an invoke instruction, its value is only available in this normal
62 if (const InvokeInst *II = dyn_cast<InvokeInst>(A))
63 BBA = II->getNormalDest();
65 if (BBA != BBB) return dominates(BBA, BBB);
67 // It is not possible to determine dominance between two PHI nodes
68 // based on their ordering.
69 if (isa<PHINode>(A) && isa<PHINode>(B))
72 // Loop through the basic block until we find A or B.
73 BasicBlock::const_iterator I = BBA->begin();
74 for (; &*I != A && &*I != B; ++I)
82 //===----------------------------------------------------------------------===//
83 // DominanceFrontier Implementation
84 //===----------------------------------------------------------------------===//
86 char DominanceFrontier::ID = 0;
87 static RegisterPass<DominanceFrontier>
88 G("domfrontier", "Dominance Frontier Construction", true, true);
90 // NewBB is split and now it has one successor. Update dominace frontier to
91 // reflect this change.
92 void DominanceFrontier::splitBlock(BasicBlock *NewBB) {
93 assert(NewBB->getTerminator()->getNumSuccessors() == 1
94 && "NewBB should have a single successor!");
95 BasicBlock *NewBBSucc = NewBB->getTerminator()->getSuccessor(0);
97 SmallVector<BasicBlock*, 8> PredBlocks;
98 for (pred_iterator PI = pred_begin(NewBB), PE = pred_end(NewBB);
100 PredBlocks.push_back(*PI);
102 if (PredBlocks.empty())
103 // If NewBB does not have any predecessors then it is a entry block.
104 // In this case, NewBB and its successor NewBBSucc dominates all
108 // NewBBSucc inherits original NewBB frontier.
109 DominanceFrontier::iterator NewBBI = find(NewBB);
110 if (NewBBI != end()) {
111 DominanceFrontier::DomSetType NewBBSet = NewBBI->second;
112 DominanceFrontier::DomSetType NewBBSuccSet;
113 NewBBSuccSet.insert(NewBBSet.begin(), NewBBSet.end());
114 addBasicBlock(NewBBSucc, NewBBSuccSet);
117 // If NewBB dominates NewBBSucc, then DF(NewBB) is now going to be the
118 // DF(PredBlocks[0]) without the stuff that the new block does not dominate
120 DominatorTree &DT = getAnalysis<DominatorTree>();
121 if (DT.dominates(NewBB, NewBBSucc)) {
122 DominanceFrontier::iterator DFI = find(PredBlocks[0]);
124 DominanceFrontier::DomSetType Set = DFI->second;
125 // Filter out stuff in Set that we do not dominate a predecessor of.
126 for (DominanceFrontier::DomSetType::iterator SetI = Set.begin(),
127 E = Set.end(); SetI != E;) {
128 bool DominatesPred = false;
129 for (pred_iterator PI = pred_begin(*SetI), E = pred_end(*SetI);
131 if (DT.dominates(NewBB, *PI))
132 DominatesPred = true;
139 if (NewBBI != end()) {
140 for (DominanceFrontier::DomSetType::iterator SetI = Set.begin(),
141 E = Set.end(); SetI != E; ++SetI) {
142 BasicBlock *SB = *SetI;
143 addToFrontier(NewBBI, SB);
146 addBasicBlock(NewBB, Set);
150 // DF(NewBB) is {NewBBSucc} because NewBB does not strictly dominate
151 // NewBBSucc, but it does dominate itself (and there is an edge (NewBB ->
152 // NewBBSucc)). NewBBSucc is the single successor of NewBB.
153 DominanceFrontier::DomSetType NewDFSet;
154 NewDFSet.insert(NewBBSucc);
155 addBasicBlock(NewBB, NewDFSet);
158 // Now we must loop over all of the dominance frontiers in the function,
159 // replacing occurrences of NewBBSucc with NewBB in some cases. All
160 // blocks that dominate a block in PredBlocks and contained NewBBSucc in
161 // their dominance frontier must be updated to contain NewBB instead.
163 for (Function::iterator FI = NewBB->getParent()->begin(),
164 FE = NewBB->getParent()->end(); FI != FE; ++FI) {
165 DominanceFrontier::iterator DFI = find(FI);
166 if (DFI == end()) continue; // unreachable block.
168 // Only consider nodes that have NewBBSucc in their dominator frontier.
169 if (!DFI->second.count(NewBBSucc)) continue;
171 // Verify whether this block dominates a block in predblocks. If not, do
173 bool BlockDominatesAny = false;
174 for (SmallVectorImpl<BasicBlock*>::const_iterator BI = PredBlocks.begin(),
175 BE = PredBlocks.end(); BI != BE; ++BI) {
176 if (DT.dominates(FI, *BI)) {
177 BlockDominatesAny = true;
182 // If NewBBSucc should not stay in our dominator frontier, remove it.
183 // We remove it unless there is a predecessor of NewBBSucc that we
184 // dominate, but we don't strictly dominate NewBBSucc.
185 bool ShouldRemove = true;
186 if ((BasicBlock*)FI == NewBBSucc || !DT.dominates(FI, NewBBSucc)) {
187 // Okay, we know that PredDom does not strictly dominate NewBBSucc.
188 // Check to see if it dominates any predecessors of NewBBSucc.
189 for (pred_iterator PI = pred_begin(NewBBSucc),
190 E = pred_end(NewBBSucc); PI != E; ++PI)
191 if (DT.dominates(FI, *PI)) {
192 ShouldRemove = false;
198 removeFromFrontier(DFI, NewBBSucc);
199 if (BlockDominatesAny && (&*FI == NewBB || !DT.dominates(FI, NewBB)))
200 addToFrontier(DFI, NewBB);
205 class DFCalculateWorkObject {
207 DFCalculateWorkObject(BasicBlock *B, BasicBlock *P,
208 const DomTreeNode *N,
209 const DomTreeNode *PN)
210 : currentBB(B), parentBB(P), Node(N), parentNode(PN) {}
211 BasicBlock *currentBB;
212 BasicBlock *parentBB;
213 const DomTreeNode *Node;
214 const DomTreeNode *parentNode;
218 const DominanceFrontier::DomSetType &
219 DominanceFrontier::calculate(const DominatorTree &DT,
220 const DomTreeNode *Node) {
221 BasicBlock *BB = Node->getBlock();
222 DomSetType *Result = NULL;
224 std::vector<DFCalculateWorkObject> workList;
225 SmallPtrSet<BasicBlock *, 32> visited;
227 workList.push_back(DFCalculateWorkObject(BB, NULL, Node, NULL));
229 DFCalculateWorkObject *currentW = &workList.back();
230 assert (currentW && "Missing work object.");
232 BasicBlock *currentBB = currentW->currentBB;
233 BasicBlock *parentBB = currentW->parentBB;
234 const DomTreeNode *currentNode = currentW->Node;
235 const DomTreeNode *parentNode = currentW->parentNode;
236 assert (currentBB && "Invalid work object. Missing current Basic Block");
237 assert (currentNode && "Invalid work object. Missing current Node");
238 DomSetType &S = Frontiers[currentBB];
240 // Visit each block only once.
241 if (visited.count(currentBB) == 0) {
242 visited.insert(currentBB);
244 // Loop over CFG successors to calculate DFlocal[currentNode]
245 for (succ_iterator SI = succ_begin(currentBB), SE = succ_end(currentBB);
247 // Does Node immediately dominate this successor?
248 if (DT[*SI]->getIDom() != currentNode)
253 // At this point, S is DFlocal. Now we union in DFup's of our children...
254 // Loop through and visit the nodes that Node immediately dominates (Node's
255 // children in the IDomTree)
256 bool visitChild = false;
257 for (DomTreeNode::const_iterator NI = currentNode->begin(),
258 NE = currentNode->end(); NI != NE; ++NI) {
259 DomTreeNode *IDominee = *NI;
260 BasicBlock *childBB = IDominee->getBlock();
261 if (visited.count(childBB) == 0) {
262 workList.push_back(DFCalculateWorkObject(childBB, currentBB,
263 IDominee, currentNode));
268 // If all children are visited or there is any child then pop this block
269 // from the workList.
277 DomSetType::const_iterator CDFI = S.begin(), CDFE = S.end();
278 DomSetType &parentSet = Frontiers[parentBB];
279 for (; CDFI != CDFE; ++CDFI) {
280 if (!DT.properlyDominates(parentNode, DT[*CDFI]))
281 parentSet.insert(*CDFI);
286 } while (!workList.empty());
291 void DominanceFrontierBase::print(raw_ostream &OS, const Module* ) const {
292 for (const_iterator I = begin(), E = end(); I != E; ++I) {
293 OS << " DomFrontier for BB";
295 WriteAsOperand(OS, I->first, false);
297 OS << " <<exit node>>";
300 const std::set<BasicBlock*> &BBs = I->second;
302 for (std::set<BasicBlock*>::const_iterator I = BBs.begin(), E = BBs.end();
305 WriteAsOperand(OS, *I, false);
307 OS << " <<exit node>>";