1 //===- llvm/Analysis/Dominators.h - Dominator Info Calculation --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file defines the following classes:
11 // 1. DominatorSet: Calculates the [reverse] dominator set for a function
12 // 2. ImmediateDominators: Calculates and holds a mapping between BasicBlocks
13 // and their immediate dominator.
14 // 3. DominatorTree: Represent the ImmediateDominator as an explicit tree
16 // 4. DominanceFrontier: Calculate and hold the dominance frontier for a
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.
23 //===----------------------------------------------------------------------===//
25 #ifndef LLVM_ANALYSIS_DOMINATORS_H
26 #define LLVM_ANALYSIS_DOMINATORS_H
28 #include "llvm/Pass.h"
35 template <typename GraphType> struct GraphTraits;
37 //===----------------------------------------------------------------------===//
39 // DominatorBase - Base class that other, more interesting dominator analyses
42 class DominatorBase : public FunctionPass {
44 std::vector<BasicBlock*> Roots;
45 const bool IsPostDominators;
47 inline DominatorBase(bool isPostDom) : Roots(), IsPostDominators(isPostDom) {}
49 // Return the root blocks of the current CFG. This may include multiple
50 // blocks if we are computing post dominators. For forward dominators, this
51 // will always be a single block (the entry node).
52 inline const std::vector<BasicBlock*> &getRoots() const { return Roots; }
54 // Returns true if analysis based of postdoms
55 bool isPostDominator() const { return IsPostDominators; }
58 //===----------------------------------------------------------------------===//
60 // DominatorSet - Maintain a set<BasicBlock*> for every basic block in a
61 // function, that represents the blocks that dominate the block. If the block
62 // is unreachable in this function, the set will be empty. This cannot happen
63 // for reachable code, because every block dominates at least itself.
65 struct DominatorSetBase : public DominatorBase {
66 typedef std::set<BasicBlock*> DomSetType; // Dom set for a bb
68 typedef std::map<BasicBlock*, DomSetType> DomSetMapType;
72 DominatorSetBase(bool isPostDom) : DominatorBase(isPostDom) {}
74 virtual void releaseMemory() { Doms.clear(); }
76 // Accessor interface:
77 typedef DomSetMapType::const_iterator const_iterator;
78 typedef DomSetMapType::iterator iterator;
79 inline const_iterator begin() const { return Doms.begin(); }
80 inline iterator begin() { return Doms.begin(); }
81 inline const_iterator end() const { return Doms.end(); }
82 inline iterator end() { return Doms.end(); }
83 inline const_iterator find(BasicBlock* B) const { return Doms.find(B); }
84 inline iterator find(BasicBlock* B) { return Doms.find(B); }
87 /// getDominators - Return the set of basic blocks that dominate the specified
90 inline const DomSetType &getDominators(BasicBlock *BB) const {
91 const_iterator I = find(BB);
92 assert(I != end() && "BB not in function!");
96 /// isReachable - Return true if the specified basicblock is reachable. If
97 /// the block is reachable, we have dominator set information for it.
98 bool isReachable(BasicBlock *BB) const {
99 return !getDominators(BB).empty();
102 /// dominates - Return true if A dominates B.
104 inline bool dominates(BasicBlock *A, BasicBlock *B) const {
105 return getDominators(B).count(A) != 0;
108 /// properlyDominates - Return true if A dominates B and A != B.
110 bool properlyDominates(BasicBlock *A, BasicBlock *B) const {
111 return dominates(A, B) && A != B;
114 /// print - Convert to human readable form
115 virtual void print(std::ostream &OS) const;
117 /// dominates - Return true if A dominates B. This performs the special
118 /// checks necessary if A and B are in the same basic block.
120 bool dominates(Instruction *A, Instruction *B) const;
122 //===--------------------------------------------------------------------===//
123 // API to update (Post)DominatorSet information based on modifications to
126 /// addBasicBlock - Call to update the dominator set with information about a
127 /// new block that was inserted into the function.
128 void addBasicBlock(BasicBlock *BB, const DomSetType &Dominators) {
129 assert(find(BB) == end() && "Block already in DominatorSet!");
130 Doms.insert(std::make_pair(BB, Dominators));
133 // addDominator - If a new block is inserted into the CFG, then method may be
134 // called to notify the blocks it dominates that it is in their set.
136 void addDominator(BasicBlock *BB, BasicBlock *NewDominator) {
137 iterator I = find(BB);
138 assert(I != end() && "BB is not in DominatorSet!");
139 I->second.insert(NewDominator);
144 //===-------------------------------------
145 // DominatorSet Class - Concrete subclass of DominatorSetBase that is used to
146 // compute a normal dominator set.
148 struct DominatorSet : public DominatorSetBase {
149 DominatorSet() : DominatorSetBase(false) {}
151 virtual bool runOnFunction(Function &F);
153 /// recalculate - This method may be called by external passes that modify the
154 /// CFG and then need dominator information recalculated. This method is
155 /// obviously really slow, so it should be avoided if at all possible.
158 BasicBlock *getRoot() const {
159 assert(Roots.size() == 1 && "Should always have entry node!");
163 // getAnalysisUsage - This simply provides a dominator set
164 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
165 AU.setPreservesAll();
168 void calculateDominatorsFromBlock(BasicBlock *BB);
172 //===----------------------------------------------------------------------===//
174 // ImmediateDominators - Calculate the immediate dominator for each node in a
177 class ImmediateDominatorsBase : public DominatorBase {
179 std::map<BasicBlock*, BasicBlock*> IDoms;
180 void calcIDoms(const DominatorSetBase &DS);
182 ImmediateDominatorsBase(bool isPostDom) : DominatorBase(isPostDom) {}
184 virtual void releaseMemory() { IDoms.clear(); }
186 // Accessor interface:
187 typedef std::map<BasicBlock*, BasicBlock*> IDomMapType;
188 typedef IDomMapType::const_iterator const_iterator;
189 inline const_iterator begin() const { return IDoms.begin(); }
190 inline const_iterator end() const { return IDoms.end(); }
191 inline const_iterator find(BasicBlock* B) const { return IDoms.find(B);}
193 // operator[] - Return the idom for the specified basic block. The start
194 // node returns null, because it does not have an immediate dominator.
196 inline BasicBlock *operator[](BasicBlock *BB) const {
200 // get() - Synonym for operator[].
201 inline BasicBlock *get(BasicBlock *BB) const {
202 std::map<BasicBlock*, BasicBlock*>::const_iterator I = IDoms.find(BB);
203 return I != IDoms.end() ? I->second : 0;
206 //===--------------------------------------------------------------------===//
207 // API to update Immediate(Post)Dominators information based on modifications
210 /// addNewBlock - Add a new block to the CFG, with the specified immediate
213 void addNewBlock(BasicBlock *BB, BasicBlock *IDom) {
214 assert(get(BB) == 0 && "BasicBlock already in idom info!");
218 /// setImmediateDominator - Update the immediate dominator information to
219 /// change the current immediate dominator for the specified block to another
220 /// block. This method requires that BB already have an IDom, otherwise just
222 void setImmediateDominator(BasicBlock *BB, BasicBlock *NewIDom) {
223 assert(IDoms.find(BB) != IDoms.end() && "BB doesn't have idom yet!");
227 // print - Convert to human readable form
228 virtual void print(std::ostream &OS) const;
231 //===-------------------------------------
232 // ImmediateDominators Class - Concrete subclass of ImmediateDominatorsBase that
233 // is used to compute a normal immediate dominator set.
235 struct ImmediateDominators : public ImmediateDominatorsBase {
236 ImmediateDominators() : ImmediateDominatorsBase(false) {}
238 BasicBlock *getRoot() const {
239 assert(Roots.size() == 1 && "Should always have entry node!");
243 virtual bool runOnFunction(Function &F) {
244 IDoms.clear(); // Reset from the last time we were run...
245 DominatorSet &DS = getAnalysis<DominatorSet>();
246 Roots = DS.getRoots();
251 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
252 AU.setPreservesAll();
253 AU.addRequired<DominatorSet>();
258 //===----------------------------------------------------------------------===//
260 // DominatorTree - Calculate the immediate dominator tree for a function.
262 struct DominatorTreeBase : public DominatorBase {
265 std::map<BasicBlock*, Node*> Nodes;
267 typedef std::map<BasicBlock*, Node*> NodeMapType;
272 friend class DominatorTree;
273 friend class PostDominatorTree;
274 friend class DominatorTreeBase;
277 std::vector<Node*> Children;
279 typedef std::vector<Node*>::iterator iterator;
280 typedef std::vector<Node*>::const_iterator const_iterator;
282 iterator begin() { return Children.begin(); }
283 iterator end() { return Children.end(); }
284 const_iterator begin() const { return Children.begin(); }
285 const_iterator end() const { return Children.end(); }
287 inline BasicBlock *getBlock() const { return TheBB; }
288 inline Node *getIDom() const { return IDom; }
289 inline const std::vector<Node*> &getChildren() const { return Children; }
291 // dominates - Returns true iff this dominates N. Note that this is not a
292 // constant time operation!
293 inline bool dominates(const Node *N) const {
295 while ((IDom = N->getIDom()) != 0 && IDom != this)
296 N = IDom; // Walk up the tree
301 inline Node(BasicBlock *BB, Node *iDom)
302 : TheBB(BB), IDom(iDom) {}
303 inline Node *addChild(Node *C) { Children.push_back(C); return C; }
305 void setIDom(Node *NewIDom);
309 DominatorTreeBase(bool isPostDom) : DominatorBase(isPostDom) {}
310 ~DominatorTreeBase() { reset(); }
312 virtual void releaseMemory() { reset(); }
314 /// getNode - return the (Post)DominatorTree node for the specified basic
315 /// block. This is the same as using operator[] on this class.
317 inline Node *getNode(BasicBlock *BB) const {
318 NodeMapType::const_iterator i = Nodes.find(BB);
319 return (i != Nodes.end()) ? i->second : 0;
322 inline Node *operator[](BasicBlock *BB) const {
326 // getRootNode - This returns the entry node for the CFG of the function. If
327 // this tree represents the post-dominance relations for a function, however,
328 // this root may be a node with the block == NULL. This is the case when
329 // there are multiple exit nodes from a particular function. Consumers of
330 // post-dominance information must be capable of dealing with this
333 Node *getRootNode() { return RootNode; }
334 const Node *getRootNode() const { return RootNode; }
336 //===--------------------------------------------------------------------===//
337 // API to update (Post)DominatorTree information based on modifications to
340 /// createNewNode - Add a new node to the dominator tree information. This
341 /// creates a new node as a child of IDomNode, linking it into the children
342 /// list of the immediate dominator.
344 Node *createNewNode(BasicBlock *BB, Node *IDomNode) {
345 assert(getNode(BB) == 0 && "Block already in dominator tree!");
346 assert(IDomNode && "Not immediate dominator specified for block!");
347 return Nodes[BB] = IDomNode->addChild(new Node(BB, IDomNode));
350 /// changeImmediateDominator - This method is used to update the dominator
351 /// tree information when a node's immediate dominator changes.
353 void changeImmediateDominator(Node *Node, Node *NewIDom) {
354 assert(Node && NewIDom && "Cannot change null node pointers!");
355 Node->setIDom(NewIDom);
358 /// print - Convert to human readable form
359 virtual void print(std::ostream &OS) const;
363 //===-------------------------------------
364 // DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
365 // compute a normal dominator tree.
367 struct DominatorTree : public DominatorTreeBase {
368 DominatorTree() : DominatorTreeBase(false) {}
370 BasicBlock *getRoot() const {
371 assert(Roots.size() == 1 && "Should always have entry node!");
375 virtual bool runOnFunction(Function &F) {
376 reset(); // Reset from the last time we were run...
377 DominatorSet &DS = getAnalysis<DominatorSet>();
378 Roots = DS.getRoots();
383 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
384 AU.setPreservesAll();
385 AU.addRequired<DominatorSet>();
388 void calculate(const DominatorSet &DS);
391 //===-------------------------------------
392 // DominatorTree GraphTraits specialization so the DominatorTree can be
393 // iterable by generic graph iterators.
395 template <> struct GraphTraits<DominatorTree::Node*> {
396 typedef DominatorTree::Node NodeType;
397 typedef NodeType::iterator ChildIteratorType;
399 static NodeType *getEntryNode(NodeType *N) {
402 static inline ChildIteratorType child_begin(NodeType* N) {
405 static inline ChildIteratorType child_end(NodeType* N) {
410 template <> struct GraphTraits<DominatorTree*>
411 : public GraphTraits<DominatorTree::Node*> {
412 static NodeType *getEntryNode(DominatorTree *DT) {
413 return DT->getRootNode();
417 //===----------------------------------------------------------------------===//
419 // DominanceFrontier - Calculate the dominance frontiers for a function.
421 struct DominanceFrontierBase : public DominatorBase {
422 typedef std::set<BasicBlock*> DomSetType; // Dom set for a bb
423 typedef std::map<BasicBlock*, DomSetType> DomSetMapType; // Dom set map
425 DomSetMapType Frontiers;
427 DominanceFrontierBase(bool isPostDom) : DominatorBase(isPostDom) {}
429 virtual void releaseMemory() { Frontiers.clear(); }
431 // Accessor interface:
432 typedef DomSetMapType::iterator iterator;
433 typedef DomSetMapType::const_iterator const_iterator;
434 iterator begin() { return Frontiers.begin(); }
435 const_iterator begin() const { return Frontiers.begin(); }
436 iterator end() { return Frontiers.end(); }
437 const_iterator end() const { return Frontiers.end(); }
438 iterator find(BasicBlock *B) { return Frontiers.find(B); }
439 const_iterator find(BasicBlock *B) const { return Frontiers.find(B); }
441 void addBasicBlock(BasicBlock *BB, const DomSetType &frontier) {
442 assert(find(BB) == end() && "Block already in DominanceFrontier!");
443 Frontiers.insert(std::make_pair(BB, frontier));
446 void addToFrontier(iterator I, BasicBlock *Node) {
447 assert(I != end() && "BB is not in DominanceFrontier!");
448 I->second.insert(Node);
451 void removeFromFrontier(iterator I, BasicBlock *Node) {
452 assert(I != end() && "BB is not in DominanceFrontier!");
453 assert(I->second.count(Node) && "Node is not in DominanceFrontier of BB");
454 I->second.erase(Node);
457 // print - Convert to human readable form
458 virtual void print(std::ostream &OS) const;
462 //===-------------------------------------
463 // DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
464 // compute a normal dominator tree.
466 struct DominanceFrontier : public DominanceFrontierBase {
467 DominanceFrontier() : DominanceFrontierBase(false) {}
469 BasicBlock *getRoot() const {
470 assert(Roots.size() == 1 && "Should always have entry node!");
474 virtual bool runOnFunction(Function &) {
476 DominatorTree &DT = getAnalysis<DominatorTree>();
477 Roots = DT.getRoots();
478 assert(Roots.size() == 1 && "Only one entry block for forward domfronts!");
479 calculate(DT, DT[Roots[0]]);
483 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
484 AU.setPreservesAll();
485 AU.addRequired<DominatorTree>();
488 const DomSetType &calculate(const DominatorTree &DT,
489 const DominatorTree::Node *Node);
492 } // End llvm namespace