Dominators.h revision d49b12041419709a0690667ce1e2b5e9b9a11610
1//===- llvm/Analysis/Dominators.h - Dominator Info Calculation --*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file was developed by the LLVM research group and is distributed under 6// the University of Illinois Open Source License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines the following classes: 11// 1. ImmediateDominators: Calculates and holds a mapping between BasicBlocks 12// and their immediate dominator. 13// 2. DominatorSet: Calculates the [reverse] dominator set for a function 14// 3. DominatorTree: Represent the ImmediateDominator as an explicit tree 15// structure. 16// 4. DominanceFrontier: Calculate and hold the dominance frontier for a 17// function. 18// 19// These data structures are listed in increasing order of complexity. It 20// takes longer to calculate the dominator frontier, for example, than the 21// ImmediateDominator mapping. 22// 23//===----------------------------------------------------------------------===// 24 25#ifndef LLVM_ANALYSIS_DOMINATORS_H 26#define LLVM_ANALYSIS_DOMINATORS_H 27 28#include "llvm/Pass.h" 29#include <set> 30 31namespace llvm { 32 33class Instruction; 34 35template <typename GraphType> struct GraphTraits; 36 37//===----------------------------------------------------------------------===// 38/// DominatorBase - Base class that other, more interesting dominator analyses 39/// inherit from. 40/// 41class DominatorBase : public FunctionPass { 42protected: 43 std::vector<BasicBlock*> Roots; 44 const bool IsPostDominators; 45 46 inline DominatorBase(bool isPostDom) : Roots(), IsPostDominators(isPostDom) {} 47public: 48 /// getRoots - Return the root blocks of the current CFG. This may include 49 /// multiple blocks if we are computing post dominators. For forward 50 /// dominators, this will always be a single block (the entry node). 51 /// 52 inline const std::vector<BasicBlock*> &getRoots() const { return Roots; } 53 54 /// isPostDominator - Returns true if analysis based of postdoms 55 /// 56 bool isPostDominator() const { return IsPostDominators; } 57}; 58 59 60//===----------------------------------------------------------------------===// 61/// ImmediateDominators - Calculate the immediate dominator for each node in a 62/// function. 63/// 64class ImmediateDominatorsBase : public DominatorBase { 65protected: 66 std::map<BasicBlock*, BasicBlock*> IDoms; 67public: 68 ImmediateDominatorsBase(bool isPostDom) : DominatorBase(isPostDom) {} 69 70 virtual void releaseMemory() { IDoms.clear(); } 71 72 // Accessor interface: 73 typedef std::map<BasicBlock*, BasicBlock*> IDomMapType; 74 typedef IDomMapType::const_iterator const_iterator; 75 inline const_iterator begin() const { return IDoms.begin(); } 76 inline const_iterator end() const { return IDoms.end(); } 77 inline const_iterator find(BasicBlock* B) const { return IDoms.find(B);} 78 79 /// operator[] - Return the idom for the specified basic block. The start 80 /// node returns null, because it does not have an immediate dominator. 81 /// 82 inline BasicBlock *operator[](BasicBlock *BB) const { 83 return get(BB); 84 } 85 86 /// get() - Synonym for operator[]. 87 /// 88 inline BasicBlock *get(BasicBlock *BB) const { 89 std::map<BasicBlock*, BasicBlock*>::const_iterator I = IDoms.find(BB); 90 return I != IDoms.end() ? I->second : 0; 91 } 92 93 //===--------------------------------------------------------------------===// 94 // API to update Immediate(Post)Dominators information based on modifications 95 // to the CFG... 96 97 /// addNewBlock - Add a new block to the CFG, with the specified immediate 98 /// dominator. 99 /// 100 void addNewBlock(BasicBlock *BB, BasicBlock *IDom) { 101 assert(get(BB) == 0 && "BasicBlock already in idom info!"); 102 IDoms[BB] = IDom; 103 } 104 105 /// setImmediateDominator - Update the immediate dominator information to 106 /// change the current immediate dominator for the specified block to another 107 /// block. This method requires that BB already have an IDom, otherwise just 108 /// use addNewBlock. 109 /// 110 void setImmediateDominator(BasicBlock *BB, BasicBlock *NewIDom) { 111 assert(IDoms.find(BB) != IDoms.end() && "BB doesn't have idom yet!"); 112 IDoms[BB] = NewIDom; 113 } 114 115 /// print - Convert to human readable form 116 /// 117 virtual void print(std::ostream &OS) const; 118}; 119 120//===------------------------------------- 121/// ImmediateDominators Class - Concrete subclass of ImmediateDominatorsBase 122/// that is used to compute a normal immediate dominator set. 123/// 124struct ImmediateDominators : public ImmediateDominatorsBase { 125 ImmediateDominators() : ImmediateDominatorsBase(false) {} 126 127 BasicBlock *getRoot() const { 128 assert(Roots.size() == 1 && "Should always have entry node!"); 129 return Roots[0]; 130 } 131 132 virtual bool runOnFunction(Function &F); 133 134 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 135 AU.setPreservesAll(); 136 } 137 138private: 139 struct InfoRec { 140 unsigned Semi; 141 unsigned Size; 142 BasicBlock *Label, *Parent, *Child, *Ancestor; 143 144 std::vector<BasicBlock*> Bucket; 145 146 InfoRec() : Semi(0), Size(0), Label(0), Parent(0), Child(0), Ancestor(0){} 147 }; 148 149 // Vertex - Map the DFS number to the BasicBlock* 150 std::vector<BasicBlock*> Vertex; 151 152 // Info - Collection of information used during the computation of idoms. 153 std::map<BasicBlock*, InfoRec> Info; 154 155 unsigned DFSPass(BasicBlock *V, InfoRec &VInfo, unsigned N); 156 void Compress(BasicBlock *V, InfoRec &VInfo); 157 BasicBlock *Eval(BasicBlock *v); 158 void Link(BasicBlock *V, BasicBlock *W, InfoRec &WInfo); 159}; 160 161 162 163//===----------------------------------------------------------------------===// 164/// DominatorSet - Maintain a set<BasicBlock*> for every basic block in a 165/// function, that represents the blocks that dominate the block. If the block 166/// is unreachable in this function, the set will be empty. This cannot happen 167/// for reachable code, because every block dominates at least itself. 168/// 169struct DominatorSetBase : public DominatorBase { 170 typedef std::set<BasicBlock*> DomSetType; // Dom set for a bb 171 // Map of dom sets 172 typedef std::map<BasicBlock*, DomSetType> DomSetMapType; 173protected: 174 DomSetMapType Doms; 175public: 176 DominatorSetBase(bool isPostDom) : DominatorBase(isPostDom) {} 177 178 virtual void releaseMemory() { Doms.clear(); } 179 180 // Accessor interface: 181 typedef DomSetMapType::const_iterator const_iterator; 182 typedef DomSetMapType::iterator iterator; 183 inline const_iterator begin() const { return Doms.begin(); } 184 inline iterator begin() { return Doms.begin(); } 185 inline const_iterator end() const { return Doms.end(); } 186 inline iterator end() { return Doms.end(); } 187 inline const_iterator find(BasicBlock* B) const { return Doms.find(B); } 188 inline iterator find(BasicBlock* B) { return Doms.find(B); } 189 190 191 /// getDominators - Return the set of basic blocks that dominate the specified 192 /// block. 193 /// 194 inline const DomSetType &getDominators(BasicBlock *BB) const { 195 const_iterator I = find(BB); 196 assert(I != end() && "BB not in function!"); 197 return I->second; 198 } 199 200 /// isReachable - Return true if the specified basicblock is reachable. If 201 /// the block is reachable, we have dominator set information for it. 202 /// 203 bool isReachable(BasicBlock *BB) const { 204 return !getDominators(BB).empty(); 205 } 206 207 /// dominates - Return true if A dominates B. 208 /// 209 inline bool dominates(BasicBlock *A, BasicBlock *B) const { 210 return getDominators(B).count(A) != 0; 211 } 212 213 /// properlyDominates - Return true if A dominates B and A != B. 214 /// 215 bool properlyDominates(BasicBlock *A, BasicBlock *B) const { 216 return dominates(A, B) && A != B; 217 } 218 219 /// print - Convert to human readable form 220 /// 221 virtual void print(std::ostream &OS) const; 222 223 /// dominates - Return true if A dominates B. This performs the special 224 /// checks necessary if A and B are in the same basic block. 225 /// 226 bool dominates(Instruction *A, Instruction *B) const; 227 228 //===--------------------------------------------------------------------===// 229 // API to update (Post)DominatorSet information based on modifications to 230 // the CFG... 231 232 /// addBasicBlock - Call to update the dominator set with information about a 233 /// new block that was inserted into the function. 234 /// 235 void addBasicBlock(BasicBlock *BB, const DomSetType &Dominators) { 236 assert(find(BB) == end() && "Block already in DominatorSet!"); 237 Doms.insert(std::make_pair(BB, Dominators)); 238 } 239 240 /// addDominator - If a new block is inserted into the CFG, then method may be 241 /// called to notify the blocks it dominates that it is in their set. 242 /// 243 void addDominator(BasicBlock *BB, BasicBlock *NewDominator) { 244 iterator I = find(BB); 245 assert(I != end() && "BB is not in DominatorSet!"); 246 I->second.insert(NewDominator); 247 } 248}; 249 250 251//===------------------------------------- 252/// DominatorSet Class - Concrete subclass of DominatorSetBase that is used to 253/// compute a normal dominator set. 254/// 255struct DominatorSet : public DominatorSetBase { 256 DominatorSet() : DominatorSetBase(false) {} 257 258 virtual bool runOnFunction(Function &F); 259 260 BasicBlock *getRoot() const { 261 assert(Roots.size() == 1 && "Should always have entry node!"); 262 return Roots[0]; 263 } 264 265 /// getAnalysisUsage - This simply provides a dominator set 266 /// 267 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 268 AU.addRequired<ImmediateDominators>(); 269 AU.setPreservesAll(); 270 } 271}; 272 273 274//===----------------------------------------------------------------------===// 275/// DominatorTree - Calculate the immediate dominator tree for a function. 276/// 277struct DominatorTreeBase : public DominatorBase { 278 class Node; 279protected: 280 std::map<BasicBlock*, Node*> Nodes; 281 void reset(); 282 typedef std::map<BasicBlock*, Node*> NodeMapType; 283 284 Node *RootNode; 285public: 286 class Node { 287 friend class DominatorTree; 288 friend class PostDominatorTree; 289 friend class DominatorTreeBase; 290 BasicBlock *TheBB; 291 Node *IDom; 292 std::vector<Node*> Children; 293 public: 294 typedef std::vector<Node*>::iterator iterator; 295 typedef std::vector<Node*>::const_iterator const_iterator; 296 297 iterator begin() { return Children.begin(); } 298 iterator end() { return Children.end(); } 299 const_iterator begin() const { return Children.begin(); } 300 const_iterator end() const { return Children.end(); } 301 302 inline BasicBlock *getBlock() const { return TheBB; } 303 inline Node *getIDom() const { return IDom; } 304 inline const std::vector<Node*> &getChildren() const { return Children; } 305 306 /// dominates - Returns true iff this dominates N. Note that this is not a 307 /// constant time operation! 308 /// 309 inline bool dominates(const Node *N) const { 310 const Node *IDom; 311 while ((IDom = N->getIDom()) != 0 && IDom != this) 312 N = IDom; // Walk up the tree 313 return IDom != 0; 314 } 315 316 private: 317 inline Node(BasicBlock *BB, Node *iDom) : TheBB(BB), IDom(iDom) {} 318 inline Node *addChild(Node *C) { Children.push_back(C); return C; } 319 320 void setIDom(Node *NewIDom); 321 }; 322 323public: 324 DominatorTreeBase(bool isPostDom) : DominatorBase(isPostDom) {} 325 ~DominatorTreeBase() { reset(); } 326 327 virtual void releaseMemory() { reset(); } 328 329 /// getNode - return the (Post)DominatorTree node for the specified basic 330 /// block. This is the same as using operator[] on this class. 331 /// 332 inline Node *getNode(BasicBlock *BB) const { 333 NodeMapType::const_iterator i = Nodes.find(BB); 334 return (i != Nodes.end()) ? i->second : 0; 335 } 336 337 inline Node *operator[](BasicBlock *BB) const { 338 return getNode(BB); 339 } 340 341 /// getRootNode - This returns the entry node for the CFG of the function. If 342 /// this tree represents the post-dominance relations for a function, however, 343 /// this root may be a node with the block == NULL. This is the case when 344 /// there are multiple exit nodes from a particular function. Consumers of 345 /// post-dominance information must be capable of dealing with this 346 /// possibility. 347 /// 348 Node *getRootNode() { return RootNode; } 349 const Node *getRootNode() const { return RootNode; } 350 351 //===--------------------------------------------------------------------===// 352 // API to update (Post)DominatorTree information based on modifications to 353 // the CFG... 354 355 /// createNewNode - Add a new node to the dominator tree information. This 356 /// creates a new node as a child of IDomNode, linking it into the children 357 /// list of the immediate dominator. 358 /// 359 Node *createNewNode(BasicBlock *BB, Node *IDomNode) { 360 assert(getNode(BB) == 0 && "Block already in dominator tree!"); 361 assert(IDomNode && "Not immediate dominator specified for block!"); 362 return Nodes[BB] = IDomNode->addChild(new Node(BB, IDomNode)); 363 } 364 365 /// changeImmediateDominator - This method is used to update the dominator 366 /// tree information when a node's immediate dominator changes. 367 /// 368 void changeImmediateDominator(Node *N, Node *NewIDom) { 369 assert(N && NewIDom && "Cannot change null node pointers!"); 370 N->setIDom(NewIDom); 371 } 372 373 /// print - Convert to human readable form 374 /// 375 virtual void print(std::ostream &OS) const; 376}; 377 378 379//===------------------------------------- 380/// DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to 381/// compute a normal dominator tree. 382/// 383struct DominatorTree : public DominatorTreeBase { 384 DominatorTree() : DominatorTreeBase(false) {} 385 386 BasicBlock *getRoot() const { 387 assert(Roots.size() == 1 && "Should always have entry node!"); 388 return Roots[0]; 389 } 390 391 virtual bool runOnFunction(Function &F) { 392 reset(); // Reset from the last time we were run... 393 ImmediateDominators &ID = getAnalysis<ImmediateDominators>(); 394 Roots = ID.getRoots(); 395 calculate(ID); 396 return false; 397 } 398 399 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 400 AU.setPreservesAll(); 401 AU.addRequired<ImmediateDominators>(); 402 } 403private: 404 void calculate(const ImmediateDominators &ID); 405 Node *getNodeForBlock(BasicBlock *BB); 406}; 407 408//===------------------------------------- 409/// DominatorTree GraphTraits specialization so the DominatorTree can be 410/// iterable by generic graph iterators. 411/// 412template <> struct GraphTraits<DominatorTree::Node*> { 413 typedef DominatorTree::Node NodeType; 414 typedef NodeType::iterator ChildIteratorType; 415 416 static NodeType *getEntryNode(NodeType *N) { 417 return N; 418 } 419 static inline ChildIteratorType child_begin(NodeType* N) { 420 return N->begin(); 421 } 422 static inline ChildIteratorType child_end(NodeType* N) { 423 return N->end(); 424 } 425}; 426 427template <> struct GraphTraits<DominatorTree*> 428 : public GraphTraits<DominatorTree::Node*> { 429 static NodeType *getEntryNode(DominatorTree *DT) { 430 return DT->getRootNode(); 431 } 432}; 433 434//===----------------------------------------------------------------------===// 435/// DominanceFrontier - Calculate the dominance frontiers for a function. 436/// 437struct DominanceFrontierBase : public DominatorBase { 438 typedef std::set<BasicBlock*> DomSetType; // Dom set for a bb 439 typedef std::map<BasicBlock*, DomSetType> DomSetMapType; // Dom set map 440protected: 441 DomSetMapType Frontiers; 442public: 443 DominanceFrontierBase(bool isPostDom) : DominatorBase(isPostDom) {} 444 445 virtual void releaseMemory() { Frontiers.clear(); } 446 447 // Accessor interface: 448 typedef DomSetMapType::iterator iterator; 449 typedef DomSetMapType::const_iterator const_iterator; 450 iterator begin() { return Frontiers.begin(); } 451 const_iterator begin() const { return Frontiers.begin(); } 452 iterator end() { return Frontiers.end(); } 453 const_iterator end() const { return Frontiers.end(); } 454 iterator find(BasicBlock *B) { return Frontiers.find(B); } 455 const_iterator find(BasicBlock *B) const { return Frontiers.find(B); } 456 457 void addBasicBlock(BasicBlock *BB, const DomSetType &frontier) { 458 assert(find(BB) == end() && "Block already in DominanceFrontier!"); 459 Frontiers.insert(std::make_pair(BB, frontier)); 460 } 461 462 void addToFrontier(iterator I, BasicBlock *Node) { 463 assert(I != end() && "BB is not in DominanceFrontier!"); 464 I->second.insert(Node); 465 } 466 467 void removeFromFrontier(iterator I, BasicBlock *Node) { 468 assert(I != end() && "BB is not in DominanceFrontier!"); 469 assert(I->second.count(Node) && "Node is not in DominanceFrontier of BB"); 470 I->second.erase(Node); 471 } 472 473 /// print - Convert to human readable form 474 /// 475 virtual void print(std::ostream &OS) const; 476}; 477 478 479//===------------------------------------- 480/// DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to 481/// compute a normal dominator tree. 482/// 483struct DominanceFrontier : public DominanceFrontierBase { 484 DominanceFrontier() : DominanceFrontierBase(false) {} 485 486 BasicBlock *getRoot() const { 487 assert(Roots.size() == 1 && "Should always have entry node!"); 488 return Roots[0]; 489 } 490 491 virtual bool runOnFunction(Function &) { 492 Frontiers.clear(); 493 DominatorTree &DT = getAnalysis<DominatorTree>(); 494 Roots = DT.getRoots(); 495 assert(Roots.size() == 1 && "Only one entry block for forward domfronts!"); 496 calculate(DT, DT[Roots[0]]); 497 return false; 498 } 499 500 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 501 AU.setPreservesAll(); 502 AU.addRequired<DominatorTree>(); 503 } 504private: 505 const DomSetType &calculate(const DominatorTree &DT, 506 const DominatorTree::Node *Node); 507}; 508 509} // End llvm namespace 510 511#endif 512