LLVM API Documentation
00001 //===- Dominators.cpp - Dominator Calculation -----------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements simple dominator construction algorithms for finding 00011 // forward dominators. Postdominators are available in libanalysis, but are not 00012 // included in libvmcore, because it's not needed. Forward dominators are 00013 // needed to support the Verifier pass. 00014 // 00015 //===----------------------------------------------------------------------===// 00016 00017 #include "llvm/Analysis/Dominators.h" 00018 #include "llvm/Support/CFG.h" 00019 #include "llvm/Support/Compiler.h" 00020 #include "llvm/ADT/DepthFirstIterator.h" 00021 #include "llvm/ADT/SetOperations.h" 00022 #include "llvm/ADT/SmallPtrSet.h" 00023 #include "llvm/ADT/SmallVector.h" 00024 #include "llvm/Analysis/DominatorInternals.h" 00025 #include "llvm/Instructions.h" 00026 #include "llvm/Support/Streams.h" 00027 #include <algorithm> 00028 using namespace llvm; 00029 00030 namespace llvm { 00031 static std::ostream &operator<<(std::ostream &o, 00032 const std::set<BasicBlock*> &BBs) { 00033 for (std::set<BasicBlock*>::const_iterator I = BBs.begin(), E = BBs.end(); 00034 I != E; ++I) 00035 if (*I) 00036 WriteAsOperand(o, *I, false); 00037 else 00038 o << " <<exit node>>"; 00039 return o; 00040 } 00041 } 00042 00043 //===----------------------------------------------------------------------===// 00044 // DominatorTree Implementation 00045 //===----------------------------------------------------------------------===// 00046 // 00047 // Provide public access to DominatorTree information. Implementation details 00048 // can be found in DominatorCalculation.h. 00049 // 00050 //===----------------------------------------------------------------------===// 00051 00052 TEMPLATE_INSTANTIATION(class DomTreeNodeBase<BasicBlock>); 00053 TEMPLATE_INSTANTIATION(class DominatorTreeBase<BasicBlock>); 00054 00055 char DominatorTree::ID = 0; 00056 static RegisterPass<DominatorTree> 00057 E("domtree", "Dominator Tree Construction", true, true); 00058 00059 bool DominatorTree::runOnFunction(Function &F) { 00060 DT->recalculate(F); 00061 return false; 00062 } 00063 00064 //===----------------------------------------------------------------------===// 00065 // DominanceFrontier Implementation 00066 //===----------------------------------------------------------------------===// 00067 00068 char DominanceFrontier::ID = 0; 00069 static RegisterPass<DominanceFrontier> 00070 G("domfrontier", "Dominance Frontier Construction", true, true); 00071 00072 // NewBB is split and now it has one successor. Update dominace frontier to 00073 // reflect this change. 00074 void DominanceFrontier::splitBlock(BasicBlock *NewBB) { 00075 assert(NewBB->getTerminator()->getNumSuccessors() == 1 00076 && "NewBB should have a single successor!"); 00077 BasicBlock *NewBBSucc = NewBB->getTerminator()->getSuccessor(0); 00078 00079 std::vector<BasicBlock*> PredBlocks; 00080 for (pred_iterator PI = pred_begin(NewBB), PE = pred_end(NewBB); 00081 PI != PE; ++PI) 00082 PredBlocks.push_back(*PI); 00083 00084 if (PredBlocks.empty()) 00085 // If NewBB does not have any predecessors then it is a entry block. 00086 // In this case, NewBB and its successor NewBBSucc dominates all 00087 // other blocks. 00088 return; 00089 00090 // NewBBSucc inherits original NewBB frontier. 00091 DominanceFrontier::iterator NewBBI = find(NewBB); 00092 if (NewBBI != end()) { 00093 DominanceFrontier::DomSetType NewBBSet = NewBBI->second; 00094 DominanceFrontier::DomSetType NewBBSuccSet; 00095 NewBBSuccSet.insert(NewBBSet.begin(), NewBBSet.end()); 00096 addBasicBlock(NewBBSucc, NewBBSuccSet); 00097 } 00098 00099 // If NewBB dominates NewBBSucc, then DF(NewBB) is now going to be the 00100 // DF(PredBlocks[0]) without the stuff that the new block does not dominate 00101 // a predecessor of. 00102 DominatorTree &DT = getAnalysis<DominatorTree>(); 00103 if (DT.dominates(NewBB, NewBBSucc)) { 00104 DominanceFrontier::iterator DFI = find(PredBlocks[0]); 00105 if (DFI != end()) { 00106 DominanceFrontier::DomSetType Set = DFI->second; 00107 // Filter out stuff in Set that we do not dominate a predecessor of. 00108 for (DominanceFrontier::DomSetType::iterator SetI = Set.begin(), 00109 E = Set.end(); SetI != E;) { 00110 bool DominatesPred = false; 00111 for (pred_iterator PI = pred_begin(*SetI), E = pred_end(*SetI); 00112 PI != E; ++PI) 00113 if (DT.dominates(NewBB, *PI)) 00114 DominatesPred = true; 00115 if (!DominatesPred) 00116 Set.erase(SetI++); 00117 else 00118 ++SetI; 00119 } 00120 00121 if (NewBBI != end()) { 00122 for (DominanceFrontier::DomSetType::iterator SetI = Set.begin(), 00123 E = Set.end(); SetI != E; ++SetI) { 00124 BasicBlock *SB = *SetI; 00125 addToFrontier(NewBBI, SB); 00126 } 00127 } else 00128 addBasicBlock(NewBB, Set); 00129 } 00130 00131 } else { 00132 // DF(NewBB) is {NewBBSucc} because NewBB does not strictly dominate 00133 // NewBBSucc, but it does dominate itself (and there is an edge (NewBB -> 00134 // NewBBSucc)). NewBBSucc is the single successor of NewBB. 00135 DominanceFrontier::DomSetType NewDFSet; 00136 NewDFSet.insert(NewBBSucc); 00137 addBasicBlock(NewBB, NewDFSet); 00138 } 00139 00140 // Now we must loop over all of the dominance frontiers in the function, 00141 // replacing occurrences of NewBBSucc with NewBB in some cases. All 00142 // blocks that dominate a block in PredBlocks and contained NewBBSucc in 00143 // their dominance frontier must be updated to contain NewBB instead. 00144 // 00145 for (Function::iterator FI = NewBB->getParent()->begin(), 00146 FE = NewBB->getParent()->end(); FI != FE; ++FI) { 00147 DominanceFrontier::iterator DFI = find(FI); 00148 if (DFI == end()) continue; // unreachable block. 00149 00150 // Only consider nodes that have NewBBSucc in their dominator frontier. 00151 if (!DFI->second.count(NewBBSucc)) continue; 00152 00153 // Verify whether this block dominates a block in predblocks. If not, do 00154 // not update it. 00155 bool BlockDominatesAny = false; 00156 for (std::vector<BasicBlock*>::const_iterator BI = PredBlocks.begin(), 00157 BE = PredBlocks.end(); BI != BE; ++BI) { 00158 if (DT.dominates(FI, *BI)) { 00159 BlockDominatesAny = true; 00160 break; 00161 } 00162 } 00163 00164 if (!BlockDominatesAny) 00165 continue; 00166 00167 // If NewBBSucc should not stay in our dominator frontier, remove it. 00168 // We remove it unless there is a predecessor of NewBBSucc that we 00169 // dominate, but we don't strictly dominate NewBBSucc. 00170 bool ShouldRemove = true; 00171 if ((BasicBlock*)FI == NewBBSucc || !DT.dominates(FI, NewBBSucc)) { 00172 // Okay, we know that PredDom does not strictly dominate NewBBSucc. 00173 // Check to see if it dominates any predecessors of NewBBSucc. 00174 for (pred_iterator PI = pred_begin(NewBBSucc), 00175 E = pred_end(NewBBSucc); PI != E; ++PI) 00176 if (DT.dominates(FI, *PI)) { 00177 ShouldRemove = false; 00178 break; 00179 } 00180 } 00181 00182 if (ShouldRemove) 00183 removeFromFrontier(DFI, NewBBSucc); 00184 addToFrontier(DFI, NewBB); 00185 } 00186 } 00187 00188 namespace { 00189 class DFCalculateWorkObject { 00190 public: 00191 DFCalculateWorkObject(BasicBlock *B, BasicBlock *P, 00192 const DomTreeNode *N, 00193 const DomTreeNode *PN) 00194 : currentBB(B), parentBB(P), Node(N), parentNode(PN) {} 00195 BasicBlock *currentBB; 00196 BasicBlock *parentBB; 00197 const DomTreeNode *Node; 00198 const DomTreeNode *parentNode; 00199 }; 00200 } 00201 00202 const DominanceFrontier::DomSetType & 00203 DominanceFrontier::calculate(const DominatorTree &DT, 00204 const DomTreeNode *Node) { 00205 BasicBlock *BB = Node->getBlock(); 00206 DomSetType *Result = NULL; 00207 00208 std::vector<DFCalculateWorkObject> workList; 00209 SmallPtrSet<BasicBlock *, 32> visited; 00210 00211 workList.push_back(DFCalculateWorkObject(BB, NULL, Node, NULL)); 00212 do { 00213 DFCalculateWorkObject *currentW = &workList.back(); 00214 assert (currentW && "Missing work object."); 00215 00216 BasicBlock *currentBB = currentW->currentBB; 00217 BasicBlock *parentBB = currentW->parentBB; 00218 const DomTreeNode *currentNode = currentW->Node; 00219 const DomTreeNode *parentNode = currentW->parentNode; 00220 assert (currentBB && "Invalid work object. Missing current Basic Block"); 00221 assert (currentNode && "Invalid work object. Missing current Node"); 00222 DomSetType &S = Frontiers[currentBB]; 00223 00224 // Visit each block only once. 00225 if (visited.count(currentBB) == 0) { 00226 visited.insert(currentBB); 00227 00228 // Loop over CFG successors to calculate DFlocal[currentNode] 00229 for (succ_iterator SI = succ_begin(currentBB), SE = succ_end(currentBB); 00230 SI != SE; ++SI) { 00231 // Does Node immediately dominate this successor? 00232 if (DT[*SI]->getIDom() != currentNode) 00233 S.insert(*SI); 00234 } 00235 } 00236 00237 // At this point, S is DFlocal. Now we union in DFup's of our children... 00238 // Loop through and visit the nodes that Node immediately dominates (Node's 00239 // children in the IDomTree) 00240 bool visitChild = false; 00241 for (DomTreeNode::const_iterator NI = currentNode->begin(), 00242 NE = currentNode->end(); NI != NE; ++NI) { 00243 DomTreeNode *IDominee = *NI; 00244 BasicBlock *childBB = IDominee->getBlock(); 00245 if (visited.count(childBB) == 0) { 00246 workList.push_back(DFCalculateWorkObject(childBB, currentBB, 00247 IDominee, currentNode)); 00248 visitChild = true; 00249 } 00250 } 00251 00252 // If all children are visited or there is any child then pop this block 00253 // from the workList. 00254 if (!visitChild) { 00255 00256 if (!parentBB) { 00257 Result = &S; 00258 break; 00259 } 00260 00261 DomSetType::const_iterator CDFI = S.begin(), CDFE = S.end(); 00262 DomSetType &parentSet = Frontiers[parentBB]; 00263 for (; CDFI != CDFE; ++CDFI) { 00264 if (!DT.properlyDominates(parentNode, DT[*CDFI])) 00265 parentSet.insert(*CDFI); 00266 } 00267 workList.pop_back(); 00268 } 00269 00270 } while (!workList.empty()); 00271 00272 return *Result; 00273 } 00274 00275 void DominanceFrontierBase::print(std::ostream &o, const Module* ) const { 00276 for (const_iterator I = begin(), E = end(); I != E; ++I) { 00277 o << " DomFrontier for BB"; 00278 if (I->first) 00279 WriteAsOperand(o, I->first, false); 00280 else 00281 o << " <<exit node>>"; 00282 o << " is:\t" << I->second << "\n"; 00283 } 00284 } 00285 00286 void DominanceFrontierBase::dump() { 00287 print (llvm::cerr); 00288 } 00289