LLVM API Documentation
00001 //===- llvm/Analysis/Dominators.h - Dominator Info Calculation --*- C++ -*-===// 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 defines the following classes: 00011 // 1. DominatorTree: Represent dominators as an explicit tree structure. 00012 // 2. DominanceFrontier: Calculate and hold the dominance frontier for a 00013 // function. 00014 // 00015 // These data structures are listed in increasing order of complexity. It 00016 // takes longer to calculate the dominator frontier, for example, than the 00017 // DominatorTree mapping. 00018 // 00019 //===----------------------------------------------------------------------===// 00020 00021 #ifndef LLVM_ANALYSIS_DOMINATORS_H 00022 #define LLVM_ANALYSIS_DOMINATORS_H 00023 00024 #include "llvm/Pass.h" 00025 #include "llvm/BasicBlock.h" 00026 #include "llvm/Function.h" 00027 #include "llvm/Instruction.h" 00028 #include "llvm/Instructions.h" 00029 #include "llvm/ADT/DenseMap.h" 00030 #include "llvm/ADT/GraphTraits.h" 00031 #include "llvm/ADT/SmallPtrSet.h" 00032 #include "llvm/ADT/SmallVector.h" 00033 #include "llvm/Assembly/Writer.h" 00034 #include "llvm/Support/CFG.h" 00035 #include "llvm/Support/Compiler.h" 00036 #include <algorithm> 00037 #include <map> 00038 #include <set> 00039 00040 namespace llvm { 00041 00042 //===----------------------------------------------------------------------===// 00043 /// DominatorBase - Base class that other, more interesting dominator analyses 00044 /// inherit from. 00045 /// 00046 template <class NodeT> 00047 class DominatorBase { 00048 protected: 00049 std::vector<NodeT*> Roots; 00050 const bool IsPostDominators; 00051 inline explicit DominatorBase(bool isPostDom) : 00052 Roots(), IsPostDominators(isPostDom) {} 00053 public: 00054 00055 /// getRoots - Return the root blocks of the current CFG. This may include 00056 /// multiple blocks if we are computing post dominators. For forward 00057 /// dominators, this will always be a single block (the entry node). 00058 /// 00059 inline const std::vector<NodeT*> &getRoots() const { return Roots; } 00060 00061 /// isPostDominator - Returns true if analysis based of postdoms 00062 /// 00063 bool isPostDominator() const { return IsPostDominators; } 00064 }; 00065 00066 00067 //===----------------------------------------------------------------------===// 00068 // DomTreeNode - Dominator Tree Node 00069 template<class NodeT> class DominatorTreeBase; 00070 struct PostDominatorTree; 00071 class MachineBasicBlock; 00072 00073 template <class NodeT> 00074 class DomTreeNodeBase { 00075 NodeT *TheBB; 00076 DomTreeNodeBase<NodeT> *IDom; 00077 std::vector<DomTreeNodeBase<NodeT> *> Children; 00078 int DFSNumIn, DFSNumOut; 00079 00080 template<class N> friend class DominatorTreeBase; 00081 friend struct PostDominatorTree; 00082 public: 00083 typedef typename std::vector<DomTreeNodeBase<NodeT> *>::iterator iterator; 00084 typedef typename std::vector<DomTreeNodeBase<NodeT> *>::const_iterator 00085 const_iterator; 00086 00087 iterator begin() { return Children.begin(); } 00088 iterator end() { return Children.end(); } 00089 const_iterator begin() const { return Children.begin(); } 00090 const_iterator end() const { return Children.end(); } 00091 00092 NodeT *getBlock() const { return TheBB; } 00093 DomTreeNodeBase<NodeT> *getIDom() const { return IDom; } 00094 const std::vector<DomTreeNodeBase<NodeT>*> &getChildren() const { 00095 return Children; 00096 } 00097 00098 DomTreeNodeBase(NodeT *BB, DomTreeNodeBase<NodeT> *iDom) 00099 : TheBB(BB), IDom(iDom), DFSNumIn(-1), DFSNumOut(-1) { } 00100 00101 DomTreeNodeBase<NodeT> *addChild(DomTreeNodeBase<NodeT> *C) { 00102 Children.push_back(C); 00103 return C; 00104 } 00105 00106 size_t getNumChildren() const { 00107 return Children.size(); 00108 } 00109 00110 void clearAllChildren() { 00111 Children.clear(); 00112 } 00113 00114 bool compare(DomTreeNodeBase<NodeT> *Other) { 00115 if (getNumChildren() != Other->getNumChildren()) 00116 return true; 00117 00118 SmallPtrSet<NodeT *, 4> OtherChildren; 00119 for(iterator I = Other->begin(), E = Other->end(); I != E; ++I) { 00120 NodeT *Nd = (*I)->getBlock(); 00121 OtherChildren.insert(Nd); 00122 } 00123 00124 for(iterator I = begin(), E = end(); I != E; ++I) { 00125 NodeT *N = (*I)->getBlock(); 00126 if (OtherChildren.count(N) == 0) 00127 return true; 00128 } 00129 return false; 00130 } 00131 00132 void setIDom(DomTreeNodeBase<NodeT> *NewIDom) { 00133 assert(IDom && "No immediate dominator?"); 00134 if (IDom != NewIDom) { 00135 typename std::vector<DomTreeNodeBase<NodeT>*>::iterator I = 00136 std::find(IDom->Children.begin(), IDom->Children.end(), this); 00137 assert(I != IDom->Children.end() && 00138 "Not in immediate dominator children set!"); 00139 // I am no longer your child... 00140 IDom->Children.erase(I); 00141 00142 // Switch to new dominator 00143 IDom = NewIDom; 00144 IDom->Children.push_back(this); 00145 } 00146 } 00147 00148 /// getDFSNumIn/getDFSNumOut - These are an internal implementation detail, do 00149 /// not call them. 00150 unsigned getDFSNumIn() const { return DFSNumIn; } 00151 unsigned getDFSNumOut() const { return DFSNumOut; } 00152 private: 00153 // Return true if this node is dominated by other. Use this only if DFS info 00154 // is valid. 00155 bool DominatedBy(const DomTreeNodeBase<NodeT> *other) const { 00156 return this->DFSNumIn >= other->DFSNumIn && 00157 this->DFSNumOut <= other->DFSNumOut; 00158 } 00159 }; 00160 00161 EXTERN_TEMPLATE_INSTANTIATION(class DomTreeNodeBase<BasicBlock>); 00162 EXTERN_TEMPLATE_INSTANTIATION(class DomTreeNodeBase<MachineBasicBlock>); 00163 00164 template<class NodeT> 00165 static std::ostream &operator<<(std::ostream &o, 00166 const DomTreeNodeBase<NodeT> *Node) { 00167 if (Node->getBlock()) 00168 WriteAsOperand(o, Node->getBlock(), false); 00169 else 00170 o << " <<exit node>>"; 00171 00172 o << " {" << Node->getDFSNumIn() << "," << Node->getDFSNumOut() << "}"; 00173 00174 return o << "\n"; 00175 } 00176 00177 template<class NodeT> 00178 static void PrintDomTree(const DomTreeNodeBase<NodeT> *N, std::ostream &o, 00179 unsigned Lev) { 00180 o << std::string(2*Lev, ' ') << "[" << Lev << "] " << N; 00181 for (typename DomTreeNodeBase<NodeT>::const_iterator I = N->begin(), 00182 E = N->end(); I != E; ++I) 00183 PrintDomTree<NodeT>(*I, o, Lev+1); 00184 } 00185 00186 typedef DomTreeNodeBase<BasicBlock> DomTreeNode; 00187 00188 //===----------------------------------------------------------------------===// 00189 /// DominatorTree - Calculate the immediate dominator tree for a function. 00190 /// 00191 00192 template<class FuncT, class N> 00193 void Calculate(DominatorTreeBase<typename GraphTraits<N>::NodeType>& DT, 00194 FuncT& F); 00195 00196 template<class NodeT> 00197 class DominatorTreeBase : public DominatorBase<NodeT> { 00198 protected: 00199 typedef DenseMap<NodeT*, DomTreeNodeBase<NodeT>*> DomTreeNodeMapType; 00200 DomTreeNodeMapType DomTreeNodes; 00201 DomTreeNodeBase<NodeT> *RootNode; 00202 00203 bool DFSInfoValid; 00204 unsigned int SlowQueries; 00205 // Information record used during immediate dominators computation. 00206 struct InfoRec { 00207 unsigned DFSNum; 00208 unsigned Semi; 00209 unsigned Size; 00210 NodeT *Label, *Child; 00211 unsigned Parent, Ancestor; 00212 00213 std::vector<NodeT*> Bucket; 00214 00215 InfoRec() : DFSNum(0), Semi(0), Size(0), Label(0), Child(0), Parent(0), 00216 Ancestor(0) {} 00217 }; 00218 00219 DenseMap<NodeT*, NodeT*> IDoms; 00220 00221 // Vertex - Map the DFS number to the BasicBlock* 00222 std::vector<NodeT*> Vertex; 00223 00224 // Info - Collection of information used during the computation of idoms. 00225 DenseMap<NodeT*, InfoRec> Info; 00226 00227 void reset() { 00228 for (typename DomTreeNodeMapType::iterator I = this->DomTreeNodes.begin(), 00229 E = DomTreeNodes.end(); I != E; ++I) 00230 delete I->second; 00231 DomTreeNodes.clear(); 00232 IDoms.clear(); 00233 this->Roots.clear(); 00234 Vertex.clear(); 00235 RootNode = 0; 00236 } 00237 00238 // NewBB is split and now it has one successor. Update dominator tree to 00239 // reflect this change. 00240 template<class N, class GraphT> 00241 void Split(DominatorTreeBase<typename GraphT::NodeType>& DT, 00242 typename GraphT::NodeType* NewBB) { 00243 assert(std::distance(GraphT::child_begin(NewBB), GraphT::child_end(NewBB)) == 1 00244 && "NewBB should have a single successor!"); 00245 typename GraphT::NodeType* NewBBSucc = *GraphT::child_begin(NewBB); 00246 00247 std::vector<typename GraphT::NodeType*> PredBlocks; 00248 for (typename GraphTraits<Inverse<N> >::ChildIteratorType PI = 00249 GraphTraits<Inverse<N> >::child_begin(NewBB), 00250 PE = GraphTraits<Inverse<N> >::child_end(NewBB); PI != PE; ++PI) 00251 PredBlocks.push_back(*PI); 00252 00253 assert(!PredBlocks.empty() && "No predblocks??"); 00254 00255 // The newly inserted basic block will dominate existing basic blocks iff the 00256 // PredBlocks dominate all of the non-pred blocks. If all predblocks dominate 00257 // the non-pred blocks, then they all must be the same block! 00258 // 00259 bool NewBBDominatesNewBBSucc = true; 00260 { 00261 typename GraphT::NodeType* OnePred = PredBlocks[0]; 00262 size_t i = 1, e = PredBlocks.size(); 00263 for (i = 1; !DT.isReachableFromEntry(OnePred); ++i) { 00264 assert(i != e && "Didn't find reachable pred?"); 00265 OnePred = PredBlocks[i]; 00266 } 00267 00268 for (; i != e; ++i) 00269 if (PredBlocks[i] != OnePred && DT.isReachableFromEntry(OnePred)) { 00270 NewBBDominatesNewBBSucc = false; 00271 break; 00272 } 00273 00274 if (NewBBDominatesNewBBSucc) 00275 for (typename GraphTraits<Inverse<N> >::ChildIteratorType PI = 00276 GraphTraits<Inverse<N> >::child_begin(NewBBSucc), 00277 E = GraphTraits<Inverse<N> >::child_end(NewBBSucc); PI != E; ++PI) 00278 if (*PI != NewBB && !DT.dominates(NewBBSucc, *PI)) { 00279 NewBBDominatesNewBBSucc = false; 00280 break; 00281 } 00282 } 00283 00284 // The other scenario where the new block can dominate its successors are when 00285 // all predecessors of NewBBSucc that are not NewBB are dominated by NewBBSucc 00286 // already. 00287 if (!NewBBDominatesNewBBSucc) { 00288 NewBBDominatesNewBBSucc = true; 00289 for (typename GraphTraits<Inverse<N> >::ChildIteratorType PI = 00290 GraphTraits<Inverse<N> >::child_begin(NewBBSucc), 00291 E = GraphTraits<Inverse<N> >::child_end(NewBBSucc); PI != E; ++PI) 00292 if (*PI != NewBB && !DT.dominates(NewBBSucc, *PI)) { 00293 NewBBDominatesNewBBSucc = false; 00294 break; 00295 } 00296 } 00297 00298 // Find NewBB's immediate dominator and create new dominator tree node for 00299 // NewBB. 00300 NodeT *NewBBIDom = 0; 00301 unsigned i = 0; 00302 for (i = 0; i < PredBlocks.size(); ++i) 00303 if (DT.isReachableFromEntry(PredBlocks[i])) { 00304 NewBBIDom = PredBlocks[i]; 00305 break; 00306 } 00307 assert(i != PredBlocks.size() && "No reachable preds?"); 00308 for (i = i + 1; i < PredBlocks.size(); ++i) { 00309 if (DT.isReachableFromEntry(PredBlocks[i])) 00310 NewBBIDom = DT.findNearestCommonDominator(NewBBIDom, PredBlocks[i]); 00311 } 00312 assert(NewBBIDom && "No immediate dominator found??"); 00313 00314 // Create the new dominator tree node... and set the idom of NewBB. 00315 DomTreeNodeBase<NodeT> *NewBBNode = DT.addNewBlock(NewBB, NewBBIDom); 00316 00317 // If NewBB strictly dominates other blocks, then it is now the immediate 00318 // dominator of NewBBSucc. Update the dominator tree as appropriate. 00319 if (NewBBDominatesNewBBSucc) { 00320 DomTreeNodeBase<NodeT> *NewBBSuccNode = DT.getNode(NewBBSucc); 00321 DT.changeImmediateDominator(NewBBSuccNode, NewBBNode); 00322 } 00323 } 00324 00325 public: 00326 explicit DominatorTreeBase(bool isPostDom) 00327 : DominatorBase<NodeT>(isPostDom), DFSInfoValid(false), SlowQueries(0) {} 00328 virtual ~DominatorTreeBase() { reset(); } 00329 00330 // FIXME: Should remove this 00331 virtual bool runOnFunction(Function &F) { return false; } 00332 00333 /// compare - Return false if the other dominator tree base matches this 00334 /// dominator tree base. Otherwise return true. 00335 bool compare(DominatorTreeBase &Other) const { 00336 00337 const DomTreeNodeMapType &OtherDomTreeNodes = Other.DomTreeNodes; 00338 if (DomTreeNodes.size() != OtherDomTreeNodes.size()) 00339 return true; 00340 00341 SmallPtrSet<const NodeT *,4> MyBBs; 00342 for (typename DomTreeNodeMapType::const_iterator 00343 I = this->DomTreeNodes.begin(), 00344 E = this->DomTreeNodes.end(); I != E; ++I) { 00345 NodeT *BB = I->first; 00346 typename DomTreeNodeMapType::const_iterator OI = OtherDomTreeNodes.find(BB); 00347 if (OI == OtherDomTreeNodes.end()) 00348 return true; 00349 00350 DomTreeNodeBase<NodeT>* MyNd = I->second; 00351 DomTreeNodeBase<NodeT>* OtherNd = OI->second; 00352 00353 if (MyNd->compare(OtherNd)) 00354 return true; 00355 } 00356 00357 return false; 00358 } 00359 00360 virtual void releaseMemory() { reset(); } 00361 00362 /// getNode - return the (Post)DominatorTree node for the specified basic 00363 /// block. This is the same as using operator[] on this class. 00364 /// 00365 inline DomTreeNodeBase<NodeT> *getNode(NodeT *BB) const { 00366 typename DomTreeNodeMapType::const_iterator I = DomTreeNodes.find(BB); 00367 return I != DomTreeNodes.end() ? I->second : 0; 00368 } 00369 00370 /// getRootNode - This returns the entry node for the CFG of the function. If 00371 /// this tree represents the post-dominance relations for a function, however, 00372 /// this root may be a node with the block == NULL. This is the case when 00373 /// there are multiple exit nodes from a particular function. Consumers of 00374 /// post-dominance information must be capable of dealing with this 00375 /// possibility. 00376 /// 00377 DomTreeNodeBase<NodeT> *getRootNode() { return RootNode; } 00378 const DomTreeNodeBase<NodeT> *getRootNode() const { return RootNode; } 00379 00380 /// properlyDominates - Returns true iff this dominates N and this != N. 00381 /// Note that this is not a constant time operation! 00382 /// 00383 bool properlyDominates(const DomTreeNodeBase<NodeT> *A, 00384 DomTreeNodeBase<NodeT> *B) const { 00385 if (A == 0 || B == 0) return false; 00386 return dominatedBySlowTreeWalk(A, B); 00387 } 00388 00389 inline bool properlyDominates(NodeT *A, NodeT *B) { 00390 return properlyDominates(getNode(A), getNode(B)); 00391 } 00392 00393 bool dominatedBySlowTreeWalk(const DomTreeNodeBase<NodeT> *A, 00394 const DomTreeNodeBase<NodeT> *B) const { 00395 const DomTreeNodeBase<NodeT> *IDom; 00396 if (A == 0 || B == 0) return false; 00397 while ((IDom = B->getIDom()) != 0 && IDom != A && IDom != B) 00398 B = IDom; // Walk up the tree 00399 return IDom != 0; 00400 } 00401 00402 00403 /// isReachableFromEntry - Return true if A is dominated by the entry 00404 /// block of the function containing it. 00405 bool isReachableFromEntry(NodeT* A) { 00406 assert (!this->isPostDominator() 00407 && "This is not implemented for post dominators"); 00408 return dominates(&A->getParent()->front(), A); 00409 } 00410 00411 /// dominates - Returns true iff A dominates B. Note that this is not a 00412 /// constant time operation! 00413 /// 00414 inline bool dominates(const DomTreeNodeBase<NodeT> *A, 00415 DomTreeNodeBase<NodeT> *B) { 00416 if (B == A) 00417 return true; // A node trivially dominates itself. 00418 00419 if (A == 0 || B == 0) 00420 return false; 00421 00422 if (DFSInfoValid) 00423 return B->DominatedBy(A); 00424 00425 // If we end up with too many slow queries, just update the 00426 // DFS numbers on the theory that we are going to keep querying. 00427 SlowQueries++; 00428 if (SlowQueries > 32) { 00429 updateDFSNumbers(); 00430 return B->DominatedBy(A); 00431 } 00432 00433 return dominatedBySlowTreeWalk(A, B); 00434 } 00435 00436 inline bool dominates(NodeT *A, NodeT *B) { 00437 if (A == B) 00438 return true; 00439 00440 return dominates(getNode(A), getNode(B)); 00441 } 00442 00443 NodeT *getRoot() const { 00444 assert(this->Roots.size() == 1 && "Should always have entry node!"); 00445 return this->Roots[0]; 00446 } 00447 00448 /// findNearestCommonDominator - Find nearest common dominator basic block 00449 /// for basic block A and B. If there is no such block then return NULL. 00450 NodeT *findNearestCommonDominator(NodeT *A, NodeT *B) { 00451 00452 assert (!this->isPostDominator() 00453 && "This is not implemented for post dominators"); 00454 assert (A->getParent() == B->getParent() 00455 && "Two blocks are not in same function"); 00456 00457 // If either A or B is a entry block then it is nearest common dominator. 00458 NodeT &Entry = A->getParent()->front(); 00459 if (A == &Entry || B == &Entry) 00460 return &Entry; 00461 00462 // If B dominates A then B is nearest common dominator. 00463 if (dominates(B, A)) 00464 return B; 00465 00466 // If A dominates B then A is nearest common dominator. 00467 if (dominates(A, B)) 00468 return A; 00469 00470 DomTreeNodeBase<NodeT> *NodeA = getNode(A); 00471 DomTreeNodeBase<NodeT> *NodeB = getNode(B); 00472 00473 // Collect NodeA dominators set. 00474 SmallPtrSet<DomTreeNodeBase<NodeT>*, 16> NodeADoms; 00475 NodeADoms.insert(NodeA); 00476 DomTreeNodeBase<NodeT> *IDomA = NodeA->getIDom(); 00477 while (IDomA) { 00478 NodeADoms.insert(IDomA); 00479 IDomA = IDomA->getIDom(); 00480 } 00481 00482 // Walk NodeB immediate dominators chain and find common dominator node. 00483 DomTreeNodeBase<NodeT> *IDomB = NodeB->getIDom(); 00484 while(IDomB) { 00485 if (NodeADoms.count(IDomB) != 0) 00486 return IDomB->getBlock(); 00487 00488 IDomB = IDomB->getIDom(); 00489 } 00490 00491 return NULL; 00492 } 00493 00494 //===--------------------------------------------------------------------===// 00495 // API to update (Post)DominatorTree information based on modifications to 00496 // the CFG... 00497 00498 /// addNewBlock - Add a new node to the dominator tree information. This 00499 /// creates a new node as a child of DomBB dominator node,linking it into 00500 /// the children list of the immediate dominator. 00501 DomTreeNodeBase<NodeT> *addNewBlock(NodeT *BB, NodeT *DomBB) { 00502 assert(getNode(BB) == 0 && "Block already in dominator tree!"); 00503 DomTreeNodeBase<NodeT> *IDomNode = getNode(DomBB); 00504 assert(IDomNode && "Not immediate dominator specified for block!"); 00505 DFSInfoValid = false; 00506 return DomTreeNodes[BB] = 00507 IDomNode->addChild(new DomTreeNodeBase<NodeT>(BB, IDomNode)); 00508 } 00509 00510 /// changeImmediateDominator - This method is used to update the dominator 00511 /// tree information when a node's immediate dominator changes. 00512 /// 00513 void changeImmediateDominator(DomTreeNodeBase<NodeT> *N, 00514 DomTreeNodeBase<NodeT> *NewIDom) { 00515 assert(N && NewIDom && "Cannot change null node pointers!"); 00516 DFSInfoValid = false; 00517 N->setIDom(NewIDom); 00518 } 00519 00520 void changeImmediateDominator(NodeT *BB, NodeT *NewBB) { 00521 changeImmediateDominator(getNode(BB), getNode(NewBB)); 00522 } 00523 00524 /// eraseNode - Removes a node from the dominator tree. Block must not 00525 /// domiante any other blocks. Removes node from its immediate dominator's 00526 /// children list. Deletes dominator node associated with basic block BB. 00527 void eraseNode(NodeT *BB) { 00528 DomTreeNodeBase<NodeT> *Node = getNode(BB); 00529 assert (Node && "Removing node that isn't in dominator tree."); 00530 assert (Node->getChildren().empty() && "Node is not a leaf node."); 00531 00532 // Remove node from immediate dominator's children list. 00533 DomTreeNodeBase<NodeT> *IDom = Node->getIDom(); 00534 if (IDom) { 00535 typename std::vector<DomTreeNodeBase<NodeT>*>::iterator I = 00536 std::find(IDom->Children.begin(), IDom->Children.end(), Node); 00537 assert(I != IDom->Children.end() && 00538 "Not in immediate dominator children set!"); 00539 // I am no longer your child... 00540 IDom->Children.erase(I); 00541 } 00542 00543 DomTreeNodes.erase(BB); 00544 delete Node; 00545 } 00546 00547 /// removeNode - Removes a node from the dominator tree. Block must not 00548 /// dominate any other blocks. Invalidates any node pointing to removed 00549 /// block. 00550 void removeNode(NodeT *BB) { 00551 assert(getNode(BB) && "Removing node that isn't in dominator tree."); 00552 DomTreeNodes.erase(BB); 00553 } 00554 00555 /// splitBlock - BB is split and now it has one successor. Update dominator 00556 /// tree to reflect this change. 00557 void splitBlock(NodeT* NewBB) { 00558 if (this->IsPostDominators) 00559 this->Split<Inverse<NodeT*>, GraphTraits<Inverse<NodeT*> > >(*this, NewBB); 00560 else 00561 this->Split<NodeT*, GraphTraits<NodeT*> >(*this, NewBB); 00562 } 00563 00564 /// print - Convert to human readable form 00565 /// 00566 virtual void print(std::ostream &o, const Module* ) const { 00567 o << "=============================--------------------------------\n"; 00568 if (this->isPostDominator()) 00569 o << "Inorder PostDominator Tree: "; 00570 else 00571 o << "Inorder Dominator Tree: "; 00572 if (this->DFSInfoValid) 00573 o << "DFSNumbers invalid: " << SlowQueries << " slow queries."; 00574 o << "\n"; 00575 00576 PrintDomTree<NodeT>(getRootNode(), o, 1); 00577 } 00578 00579 void print(std::ostream *OS, const Module* M = 0) const { 00580 if (OS) print(*OS, M); 00581 } 00582 00583 virtual void dump() { 00584 print(llvm::cerr); 00585 } 00586 00587 protected: 00588 template<class GraphT> 00589 friend void Compress(DominatorTreeBase<typename GraphT::NodeType>& DT, 00590 typename GraphT::NodeType* VIn); 00591 00592 template<class GraphT> 00593 friend typename GraphT::NodeType* Eval( 00594 DominatorTreeBase<typename GraphT::NodeType>& DT, 00595 typename GraphT::NodeType* V); 00596 00597 template<class GraphT> 00598 friend void Link(DominatorTreeBase<typename GraphT::NodeType>& DT, 00599 unsigned DFSNumV, typename GraphT::NodeType* W, 00600 typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &WInfo); 00601 00602 template<class GraphT> 00603 friend unsigned DFSPass(DominatorTreeBase<typename GraphT::NodeType>& DT, 00604 typename GraphT::NodeType* V, 00605 unsigned N); 00606 00607 template<class FuncT, class N> 00608 friend void Calculate(DominatorTreeBase<typename GraphTraits<N>::NodeType>& DT, 00609 FuncT& F); 00610 00611 /// updateDFSNumbers - Assign In and Out numbers to the nodes while walking 00612 /// dominator tree in dfs order. 00613 void updateDFSNumbers() { 00614 unsigned DFSNum = 0; 00615 00616 SmallVector<std::pair<DomTreeNodeBase<NodeT>*, 00617 typename DomTreeNodeBase<NodeT>::iterator>, 32> WorkStack; 00618 00619 for (unsigned i = 0, e = (unsigned)this->Roots.size(); i != e; ++i) { 00620 DomTreeNodeBase<NodeT> *ThisRoot = getNode(this->Roots[i]); 00621 WorkStack.push_back(std::make_pair(ThisRoot, ThisRoot->begin())); 00622 ThisRoot->DFSNumIn = DFSNum++; 00623 00624 while (!WorkStack.empty()) { 00625 DomTreeNodeBase<NodeT> *Node = WorkStack.back().first; 00626 typename DomTreeNodeBase<NodeT>::iterator ChildIt = 00627 WorkStack.back().second; 00628 00629 // If we visited all of the children of this node, "recurse" back up the 00630 // stack setting the DFOutNum. 00631 if (ChildIt == Node->end()) { 00632 Node->DFSNumOut = DFSNum++; 00633 WorkStack.pop_back(); 00634 } else { 00635 // Otherwise, recursively visit this child. 00636 DomTreeNodeBase<NodeT> *Child = *ChildIt; 00637 ++WorkStack.back().second; 00638 00639 WorkStack.push_back(std::make_pair(Child, Child->begin())); 00640 Child->DFSNumIn = DFSNum++; 00641 } 00642 } 00643 } 00644 00645 SlowQueries = 0; 00646 DFSInfoValid = true; 00647 } 00648 00649 DomTreeNodeBase<NodeT> *getNodeForBlock(NodeT *BB) { 00650 if (DomTreeNodeBase<NodeT> *BBNode = this->DomTreeNodes[BB]) 00651 return BBNode; 00652 00653 // Haven't calculated this node yet? Get or calculate the node for the 00654 // immediate dominator. 00655 NodeT *IDom = getIDom(BB); 00656 00657 assert(IDom || this->DomTreeNodes[NULL]); 00658 DomTreeNodeBase<NodeT> *IDomNode = getNodeForBlock(IDom); 00659 00660 // Add a new tree node for this BasicBlock, and link it as a child of 00661 // IDomNode 00662 DomTreeNodeBase<NodeT> *C = new DomTreeNodeBase<NodeT>(BB, IDomNode); 00663 return this->DomTreeNodes[BB] = IDomNode->addChild(C); 00664 } 00665 00666 inline NodeT *getIDom(NodeT *BB) const { 00667 typename DenseMap<NodeT*, NodeT*>::const_iterator I = IDoms.find(BB); 00668 return I != IDoms.end() ? I->second : 0; 00669 } 00670 00671 inline void addRoot(NodeT* BB) { 00672 this->Roots.push_back(BB); 00673 } 00674 00675 public: 00676 /// recalculate - compute a dominator tree for the given function 00677 template<class FT> 00678 void recalculate(FT& F) { 00679 if (!this->IsPostDominators) { 00680 reset(); 00681 00682 // Initialize roots 00683 this->Roots.push_back(&F.front()); 00684 this->IDoms[&F.front()] = 0; 00685 this->DomTreeNodes[&F.front()] = 0; 00686 this->Vertex.push_back(0); 00687 00688 Calculate<FT, NodeT*>(*this, F); 00689 00690 updateDFSNumbers(); 00691 } else { 00692 reset(); // Reset from the last time we were run... 00693 00694 // Initialize the roots list 00695 for (typename FT::iterator I = F.begin(), E = F.end(); I != E; ++I) { 00696 if (std::distance(GraphTraits<FT*>::child_begin(I), 00697 GraphTraits<FT*>::child_end(I)) == 0) 00698 addRoot(I); 00699 00700 // Prepopulate maps so that we don't get iterator invalidation issues later. 00701 this->IDoms[I] = 0; 00702 this->DomTreeNodes[I] = 0; 00703 } 00704 00705 this->Vertex.push_back(0); 00706 00707 Calculate<FT, Inverse<NodeT*> >(*this, F); 00708 } 00709 } 00710 }; 00711 00712 EXTERN_TEMPLATE_INSTANTIATION(class DominatorTreeBase<BasicBlock>); 00713 00714 //===------------------------------------- 00715 /// DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to 00716 /// compute a normal dominator tree. 00717 /// 00718 class DominatorTree : public FunctionPass { 00719 public: 00720 static char ID; // Pass ID, replacement for typeid 00721 DominatorTreeBase<BasicBlock>* DT; 00722 00723 DominatorTree() : FunctionPass(intptr_t(&ID)) { 00724 DT = new DominatorTreeBase<BasicBlock>(false); 00725 } 00726 00727 ~DominatorTree() { 00728 DT->releaseMemory(); 00729 delete DT; 00730 } 00731 00732 DominatorTreeBase<BasicBlock>& getBase() { return *DT; } 00733 00734 /// getRoots - Return the root blocks of the current CFG. This may include 00735 /// multiple blocks if we are computing post dominators. For forward 00736 /// dominators, this will always be a single block (the entry node). 00737 /// 00738 inline const std::vector<BasicBlock*> &getRoots() const { 00739 return DT->getRoots(); 00740 } 00741 00742 inline BasicBlock *getRoot() const { 00743 return DT->getRoot(); 00744 } 00745 00746 inline DomTreeNode *getRootNode() const { 00747 return DT->getRootNode(); 00748 } 00749 00750 /// compare - Return false if the other dominator tree matches this 00751 /// dominator tree. Otherwise return true. 00752 inline bool compare(DominatorTree &Other) const { 00753 DomTreeNode *R = getRootNode(); 00754 DomTreeNode *OtherR = Other.getRootNode(); 00755 00756 if (!R || !OtherR || R->getBlock() != OtherR->getBlock()) 00757 return true; 00758 00759 if (DT->compare(Other.getBase())) 00760 return true; 00761 00762 return false; 00763 } 00764 00765 virtual bool runOnFunction(Function &F); 00766 00767 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 00768 AU.setPreservesAll(); 00769 } 00770 00771 inline bool dominates(DomTreeNode* A, DomTreeNode* B) const { 00772 return DT->dominates(A, B); 00773 } 00774 00775 inline bool dominates(BasicBlock* A, BasicBlock* B) const { 00776 return DT->dominates(A, B); 00777 } 00778 00779 // dominates - Return true if A dominates B. This performs the 00780 // special checks necessary if A and B are in the same basic block. 00781 bool dominates(Instruction *A, Instruction *B) const { 00782 BasicBlock *BBA = A->getParent(), *BBB = B->getParent(); 00783 if (BBA != BBB) return DT->dominates(BBA, BBB); 00784 00785 // It is not possible to determine dominance between two PHI nodes 00786 // based on their ordering. 00787 if (isa<PHINode>(A) && isa<PHINode>(B)) 00788 return false; 00789 00790 // Loop through the basic block until we find A or B. 00791 BasicBlock::iterator I = BBA->begin(); 00792 for (; &*I != A && &*I != B; ++I) /*empty*/; 00793 00794 //if(!DT.IsPostDominators) { 00795 // A dominates B if it is found first in the basic block. 00796 return &*I == A; 00797 //} else { 00798 // // A post-dominates B if B is found first in the basic block. 00799 // return &*I == B; 00800 //} 00801 } 00802 00803 inline bool properlyDominates(const DomTreeNode* A, DomTreeNode* B) const { 00804 return DT->properlyDominates(A, B); 00805 } 00806 00807 inline bool properlyDominates(BasicBlock* A, BasicBlock* B) const { 00808 return DT->properlyDominates(A, B); 00809 } 00810 00811 /// findNearestCommonDominator - Find nearest common dominator basic block 00812 /// for basic block A and B. If there is no such block then return NULL. 00813 inline BasicBlock *findNearestCommonDominator(BasicBlock *A, BasicBlock *B) { 00814 return DT->findNearestCommonDominator(A, B); 00815 } 00816 00817 inline DomTreeNode *operator[](BasicBlock *BB) const { 00818 return DT->getNode(BB); 00819 } 00820 00821 /// getNode - return the (Post)DominatorTree node for the specified basic 00822 /// block. This is the same as using operator[] on this class. 00823 /// 00824 inline DomTreeNode *getNode(BasicBlock *BB) const { 00825 return DT->getNode(BB); 00826 } 00827 00828 /// addNewBlock - Add a new node to the dominator tree information. This 00829 /// creates a new node as a child of DomBB dominator node,linking it into 00830 /// the children list of the immediate dominator. 00831 inline DomTreeNode *addNewBlock(BasicBlock *BB, BasicBlock *DomBB) { 00832 return DT->addNewBlock(BB, DomBB); 00833 } 00834 00835 /// changeImmediateDominator - This method is used to update the dominator 00836 /// tree information when a node's immediate dominator changes. 00837 /// 00838 inline void changeImmediateDominator(BasicBlock *N, BasicBlock* NewIDom) { 00839 DT->changeImmediateDominator(N, NewIDom); 00840 } 00841 00842 inline void changeImmediateDominator(DomTreeNode *N, DomTreeNode* NewIDom) { 00843 DT->changeImmediateDominator(N, NewIDom); 00844 } 00845 00846 /// eraseNode - Removes a node from the dominator tree. Block must not 00847 /// domiante any other blocks. Removes node from its immediate dominator's 00848 /// children list. Deletes dominator node associated with basic block BB. 00849 inline void eraseNode(BasicBlock *BB) { 00850 DT->eraseNode(BB); 00851 } 00852 00853 /// splitBlock - BB is split and now it has one successor. Update dominator 00854 /// tree to reflect this change. 00855 inline void splitBlock(BasicBlock* NewBB) { 00856 DT->splitBlock(NewBB); 00857 } 00858 00859 bool isReachableFromEntry(BasicBlock* A) { 00860 return DT->isReachableFromEntry(A); 00861 } 00862 00863 00864 virtual void releaseMemory() { 00865 DT->releaseMemory(); 00866 } 00867 00868 virtual void print(std::ostream &OS, const Module* M= 0) const { 00869 DT->print(OS, M); 00870 } 00871 }; 00872 00873 //===------------------------------------- 00874 /// DominatorTree GraphTraits specialization so the DominatorTree can be 00875 /// iterable by generic graph iterators. 00876 /// 00877 template <> struct GraphTraits<DomTreeNode *> { 00878 typedef DomTreeNode NodeType; 00879 typedef NodeType::iterator ChildIteratorType; 00880 00881 static NodeType *getEntryNode(NodeType *N) { 00882 return N; 00883 } 00884 static inline ChildIteratorType child_begin(NodeType* N) { 00885 return N->begin(); 00886 } 00887 static inline ChildIteratorType child_end(NodeType* N) { 00888 return N->end(); 00889 } 00890 }; 00891 00892 template <> struct GraphTraits<DominatorTree*> 00893 : public GraphTraits<DomTreeNode *> { 00894 static NodeType *getEntryNode(DominatorTree *DT) { 00895 return DT->getRootNode(); 00896 } 00897 }; 00898 00899 00900 //===----------------------------------------------------------------------===// 00901 /// DominanceFrontierBase - Common base class for computing forward and inverse 00902 /// dominance frontiers for a function. 00903 /// 00904 class DominanceFrontierBase : public FunctionPass { 00905 public: 00906 typedef std::set<BasicBlock*> DomSetType; // Dom set for a bb 00907 typedef std::map<BasicBlock*, DomSetType> DomSetMapType; // Dom set map 00908 protected: 00909 DomSetMapType Frontiers; 00910 std::vector<BasicBlock*> Roots; 00911 const bool IsPostDominators; 00912 00913 public: 00914 DominanceFrontierBase(intptr_t ID, bool isPostDom) 00915 : FunctionPass(ID), IsPostDominators(isPostDom) {} 00916 00917 /// getRoots - Return the root blocks of the current CFG. This may include 00918 /// multiple blocks if we are computing post dominators. For forward 00919 /// dominators, this will always be a single block (the entry node). 00920 /// 00921 inline const std::vector<BasicBlock*> &getRoots() const { return Roots; } 00922 00923 /// isPostDominator - Returns true if analysis based of postdoms 00924 /// 00925 bool isPostDominator() const { return IsPostDominators; } 00926 00927 virtual void releaseMemory() { Frontiers.clear(); } 00928 00929 // Accessor interface: 00930 typedef DomSetMapType::iterator iterator; 00931 typedef DomSetMapType::const_iterator const_iterator; 00932 iterator begin() { return Frontiers.begin(); } 00933 const_iterator begin() const { return Frontiers.begin(); } 00934 iterator end() { return Frontiers.end(); } 00935 const_iterator end() const { return Frontiers.end(); } 00936 iterator find(BasicBlock *B) { return Frontiers.find(B); } 00937 const_iterator find(BasicBlock *B) const { return Frontiers.find(B); } 00938 00939 void addBasicBlock(BasicBlock *BB, const DomSetType &frontier) { 00940 assert(find(BB) == end() && "Block already in DominanceFrontier!"); 00941 Frontiers.insert(std::make_pair(BB, frontier)); 00942 } 00943 00944 /// removeBlock - Remove basic block BB's frontier. 00945 void removeBlock(BasicBlock *BB) { 00946 assert(find(BB) != end() && "Block is not in DominanceFrontier!"); 00947 for (iterator I = begin(), E = end(); I != E; ++I) 00948 I->second.erase(BB); 00949 Frontiers.erase(BB); 00950 } 00951 00952 void addToFrontier(iterator I, BasicBlock *Node) { 00953 assert(I != end() && "BB is not in DominanceFrontier!"); 00954 I->second.insert(Node); 00955 } 00956 00957 void removeFromFrontier(iterator I, BasicBlock *Node) { 00958 assert(I != end() && "BB is not in DominanceFrontier!"); 00959 assert(I->second.count(Node) && "Node is not in DominanceFrontier of BB"); 00960 I->second.erase(Node); 00961 } 00962 00963 /// compareDomSet - Return false if two domsets match. Otherwise 00964 /// return true; 00965 bool compareDomSet(DomSetType &DS1, const DomSetType &DS2) const { 00966 std::set<BasicBlock *> tmpSet; 00967 for (DomSetType::const_iterator I = DS2.begin(), 00968 E = DS2.end(); I != E; ++I) 00969 tmpSet.insert(*I); 00970 00971 for (DomSetType::const_iterator I = DS1.begin(), 00972 E = DS1.end(); I != E; ++I) { 00973 BasicBlock *Node = *I; 00974 00975 if (tmpSet.erase(Node) == 0) 00976 // Node is in DS1 but not in DS2. 00977 return true; 00978 } 00979 00980 if(!tmpSet.empty()) 00981 // There are nodes that are in DS2 but not in DS1. 00982 return true; 00983 00984 // DS1 and DS2 matches. 00985 return false; 00986 } 00987 00988 /// compare - Return true if the other dominance frontier base matches 00989 /// this dominance frontier base. Otherwise return false. 00990 bool compare(DominanceFrontierBase &Other) const { 00991 DomSetMapType tmpFrontiers; 00992 for (DomSetMapType::const_iterator I = Other.begin(), 00993 E = Other.end(); I != E; ++I) 00994 tmpFrontiers.insert(std::make_pair(I->first, I->second)); 00995 00996 for (DomSetMapType::iterator I = tmpFrontiers.begin(), 00997 E = tmpFrontiers.end(); I != E; ++I) { 00998 BasicBlock *Node = I->first; 00999 const_iterator DFI = find(Node); 01000 if (DFI == end()) 01001 return true; 01002 01003 if (compareDomSet(I->second, DFI->second)) 01004 return true; 01005 01006 tmpFrontiers.erase(Node); 01007 } 01008 01009 if (!tmpFrontiers.empty()) 01010 return true; 01011 01012 return false; 01013 } 01014 01015 /// print - Convert to human readable form 01016 /// 01017 virtual void print(std::ostream &OS, const Module* = 0) const; 01018 void print(std::ostream *OS, const Module* M = 0) const { 01019 if (OS) print(*OS, M); 01020 } 01021 virtual void dump(); 01022 }; 01023 01024 01025 //===------------------------------------- 01026 /// DominanceFrontier Class - Concrete subclass of DominanceFrontierBase that is 01027 /// used to compute a forward dominator frontiers. 01028 /// 01029 class DominanceFrontier : public DominanceFrontierBase { 01030 public: 01031 static char ID; // Pass ID, replacement for typeid 01032 DominanceFrontier() : 01033 DominanceFrontierBase(intptr_t(&ID), false) {} 01034 01035 BasicBlock *getRoot() const { 01036 assert(Roots.size() == 1 && "Should always have entry node!"); 01037 return Roots[0]; 01038 } 01039 01040 virtual bool runOnFunction(Function &) { 01041 Frontiers.clear(); 01042 DominatorTree &DT = getAnalysis<DominatorTree>(); 01043 Roots = DT.getRoots(); 01044 assert(Roots.size() == 1 && "Only one entry block for forward domfronts!"); 01045 calculate(DT, DT[Roots[0]]); 01046 return false; 01047 } 01048 01049 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 01050 AU.setPreservesAll(); 01051 AU.addRequired<DominatorTree>(); 01052 } 01053 01054 /// splitBlock - BB is split and now it has one successor. Update dominance 01055 /// frontier to reflect this change. 01056 void splitBlock(BasicBlock *BB); 01057 01058 /// BasicBlock BB's new dominator is NewBB. Update BB's dominance frontier 01059 /// to reflect this change. 01060 void changeImmediateDominator(BasicBlock *BB, BasicBlock *NewBB, 01061 DominatorTree *DT) { 01062 // NewBB is now dominating BB. Which means BB's dominance 01063 // frontier is now part of NewBB's dominance frontier. However, BB 01064 // itself is not member of NewBB's dominance frontier. 01065 DominanceFrontier::iterator NewDFI = find(NewBB); 01066 DominanceFrontier::iterator DFI = find(BB); 01067 // If BB was an entry block then its frontier is empty. 01068 if (DFI == end()) 01069 return; 01070 DominanceFrontier::DomSetType BBSet = DFI->second; 01071 for (DominanceFrontier::DomSetType::iterator BBSetI = BBSet.begin(), 01072 BBSetE = BBSet.end(); BBSetI != BBSetE; ++BBSetI) { 01073 BasicBlock *DFMember = *BBSetI; 01074 // Insert only if NewBB dominates DFMember. 01075 if (!DT->dominates(NewBB, DFMember)) 01076 NewDFI->second.insert(DFMember); 01077 } 01078 NewDFI->second.erase(BB); 01079 } 01080 01081 const DomSetType &calculate(const DominatorTree &DT, 01082 const DomTreeNode *Node); 01083 }; 01084 01085 01086 } // End llvm namespace 01087 01088 #endif