Expose new "recalculate" method from dominatorset
[oota-llvm.git] / include / llvm / Analysis / Dominators.h
1 //===- llvm/Analysis/Dominators.h - Dominator Info Calculation ---*- C++ -*--=//
2 //
3 // This file defines the following classes:
4 //  1. DominatorSet: Calculates the [reverse] dominator set for a function
5 //  2. ImmediateDominators: Calculates and holds a mapping between BasicBlocks
6 //     and their immediate dominator.
7 //  3. DominatorTree: Represent the ImmediateDominator as an explicit tree
8 //     structure.
9 //  4. DominanceFrontier: Calculate and hold the dominance frontier for a 
10 //     function.
11 //
12 //  These data structures are listed in increasing order of complexity.  It
13 //  takes longer to calculate the dominator frontier, for example, than the 
14 //  ImmediateDominator mapping.
15 // 
16 //===----------------------------------------------------------------------===//
17
18 #ifndef LLVM_ANALYSIS_DOMINATORS_H
19 #define LLVM_ANALYSIS_DOMINATORS_H
20
21 #include "llvm/Pass.h"
22 #include <set>
23 class Instruction;
24
25 //===----------------------------------------------------------------------===//
26 //
27 // DominatorBase - Base class that other, more interesting dominator analyses
28 // inherit from.
29 //
30 class DominatorBase : public FunctionPass {
31 protected:
32   BasicBlock *Root;
33   const bool IsPostDominators;
34
35   inline DominatorBase(bool isPostDom) : Root(0), IsPostDominators(isPostDom) {}
36 public:
37   inline BasicBlock *getRoot() const { return Root; }
38
39   // Returns true if analysis based of postdoms
40   bool isPostDominator() const { return IsPostDominators; }
41 };
42
43 //===----------------------------------------------------------------------===//
44 //
45 // DominatorSet - Maintain a set<BasicBlock*> for every basic block in a
46 // function, that represents the blocks that dominate the block.
47 //
48 class DominatorSetBase : public DominatorBase {
49 public:
50   typedef std::set<BasicBlock*> DomSetType;    // Dom set for a bb
51   // Map of dom sets
52   typedef std::map<BasicBlock*, DomSetType> DomSetMapType;
53 protected:
54   DomSetMapType Doms;
55 public:
56   DominatorSetBase(bool isPostDom) : DominatorBase(isPostDom) {}
57
58   virtual void releaseMemory() { Doms.clear(); }
59
60   // Accessor interface:
61   typedef DomSetMapType::const_iterator const_iterator;
62   typedef DomSetMapType::iterator iterator;
63   inline const_iterator begin() const { return Doms.begin(); }
64   inline       iterator begin()       { return Doms.begin(); }
65   inline const_iterator end()   const { return Doms.end(); }
66   inline       iterator end()         { return Doms.end(); }
67   inline const_iterator find(BasicBlock* B) const { return Doms.find(B); }
68   inline       iterator find(BasicBlock* B)       { return Doms.find(B); }
69
70
71   /// getDominators - Return the set of basic blocks that dominate the specified
72   /// block.
73   ///
74   inline const DomSetType &getDominators(BasicBlock *BB) const {
75     const_iterator I = find(BB);
76     assert(I != end() && "BB not in function!");
77     return I->second;
78   }
79
80   /// dominates - Return true if A dominates B.
81   ///
82   inline bool dominates(BasicBlock *A, BasicBlock *B) const {
83     return getDominators(B).count(A) != 0;
84   }
85
86   /// properlyDominates - Return true if A dominates B and A != B.
87   ///
88   bool properlyDominates(BasicBlock *A, BasicBlock *B) const {
89     return dominates(A, B) && A != B;
90   }
91
92   /// print - Convert to human readable form
93   virtual void print(std::ostream &OS) const;
94
95   /// dominates - Return true if A dominates B.  This performs the special
96   /// checks neccesary if A and B are in the same basic block.
97   ///
98   bool dominates(Instruction *A, Instruction *B) const;
99
100   //===--------------------------------------------------------------------===//
101   // API to update (Post)DominatorSet information based on modifications to
102   // the CFG...
103
104   /// addBasicBlock - Call to update the dominator set with information about a
105   /// new block that was inserted into the function.
106   void addBasicBlock(BasicBlock *BB, const DomSetType &Dominators) {
107     assert(find(BB) == end() && "Block already in DominatorSet!");
108     Doms.insert(std::make_pair(BB, Dominators));
109   }
110
111   // addDominator - If a new block is inserted into the CFG, then method may be
112   // called to notify the blocks it dominates that it is in their set.
113   //
114   void addDominator(BasicBlock *BB, BasicBlock *NewDominator) {
115     iterator I = find(BB);
116     assert(I != end() && "BB is not in DominatorSet!");
117     I->second.insert(NewDominator);
118   }
119 };
120
121
122 //===-------------------------------------
123 // DominatorSet Class - Concrete subclass of DominatorSetBase that is used to
124 // compute a normal dominator set.
125 //
126 struct DominatorSet : public DominatorSetBase {
127   DominatorSet() : DominatorSetBase(false) {}
128
129   virtual bool runOnFunction(Function &F);
130
131   /// recalculate - This method may be called by external passes that modify the
132   /// CFG and then need dominator information recalculated.  This method is
133   /// obviously really slow, so it should be avoided if at all possible.
134   void recalculate();
135
136   // getAnalysisUsage - This simply provides a dominator set
137   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
138     AU.setPreservesAll();
139   }
140 private:
141   void calculateDominatorsFromBlock(BasicBlock *BB);
142 };
143
144
145 //===----------------------------------------------------------------------===//
146 //
147 // ImmediateDominators - Calculate the immediate dominator for each node in a
148 // function.
149 //
150 class ImmediateDominatorsBase : public DominatorBase {
151 protected:
152   std::map<BasicBlock*, BasicBlock*> IDoms;
153   void calcIDoms(const DominatorSetBase &DS);
154 public:
155   ImmediateDominatorsBase(bool isPostDom) : DominatorBase(isPostDom) {}
156
157   virtual void releaseMemory() { IDoms.clear(); }
158
159   // Accessor interface:
160   typedef std::map<BasicBlock*, BasicBlock*> IDomMapType;
161   typedef IDomMapType::const_iterator const_iterator;
162   inline const_iterator begin() const { return IDoms.begin(); }
163   inline const_iterator end()   const { return IDoms.end(); }
164   inline const_iterator find(BasicBlock* B) const { return IDoms.find(B);}
165
166   // operator[] - Return the idom for the specified basic block.  The start
167   // node returns null, because it does not have an immediate dominator.
168   //
169   inline BasicBlock *operator[](BasicBlock *BB) const {
170     return get(BB);
171   }
172
173   // get() - Synonym for operator[].
174   inline BasicBlock *get(BasicBlock *BB) const {
175     std::map<BasicBlock*, BasicBlock*>::const_iterator I = IDoms.find(BB);
176     return I != IDoms.end() ? I->second : 0;
177   }
178
179   //===--------------------------------------------------------------------===//
180   // API to update Immediate(Post)Dominators information based on modifications
181   // to the CFG...
182
183   /// addNewBlock - Add a new block to the CFG, with the specified immediate
184   /// dominator.
185   ///
186   void addNewBlock(BasicBlock *BB, BasicBlock *IDom) {
187     assert(get(BB) == 0 && "BasicBlock already in idom info!");
188     IDoms[BB] = IDom;
189   }
190
191   /// setImmediateDominator - Update the immediate dominator information to
192   /// change the current immediate dominator for the specified block to another
193   /// block.  This method requires that BB already have an IDom, otherwise just
194   /// use addNewBlock.
195   void setImmediateDominator(BasicBlock *BB, BasicBlock *NewIDom) {
196     assert(IDoms.find(BB) != IDoms.end() && "BB doesn't have idom yet!");
197     IDoms[BB] = NewIDom;
198   }
199
200   // print - Convert to human readable form
201   virtual void print(std::ostream &OS) const;
202 };
203
204 //===-------------------------------------
205 // ImmediateDominators Class - Concrete subclass of ImmediateDominatorsBase that
206 // is used to compute a normal immediate dominator set.
207 //
208 struct ImmediateDominators : public ImmediateDominatorsBase {
209   ImmediateDominators() : ImmediateDominatorsBase(false) {}
210
211   virtual bool runOnFunction(Function &F) {
212     IDoms.clear();     // Reset from the last time we were run...
213     DominatorSet &DS = getAnalysis<DominatorSet>();
214     Root = DS.getRoot();
215     calcIDoms(DS);
216     return false;
217   }
218
219   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
220     AU.setPreservesAll();
221     AU.addRequired<DominatorSet>();
222   }
223 };
224
225
226 //===----------------------------------------------------------------------===//
227 //
228 // DominatorTree - Calculate the immediate dominator tree for a function.
229 //
230 class DominatorTreeBase : public DominatorBase {
231 protected:
232   class Node2;
233 public:
234   typedef Node2 Node;
235 protected:
236   std::map<BasicBlock*, Node*> Nodes;
237   void reset();
238   typedef std::map<BasicBlock*, Node*> NodeMapType;
239 public:
240   class Node2 {
241     friend class DominatorTree;
242     friend class PostDominatorTree;
243     friend class DominatorTreeBase;
244     BasicBlock *TheNode;
245     Node2 *IDom;
246     std::vector<Node*> Children;
247   public:
248     typedef std::vector<Node*>::iterator iterator;
249     typedef std::vector<Node*>::const_iterator const_iterator;
250
251     iterator begin()             { return Children.begin(); }
252     iterator end()               { return Children.end(); }
253     const_iterator begin() const { return Children.begin(); }
254     const_iterator end()   const { return Children.end(); }
255
256     inline BasicBlock *getNode() const { return TheNode; }
257     inline Node2 *getIDom() const { return IDom; }
258     inline const std::vector<Node*> &getChildren() const { return Children; }
259
260     // dominates - Returns true iff this dominates N.  Note that this is not a 
261     // constant time operation!
262     inline bool dominates(const Node2 *N) const {
263       const Node2 *IDom;
264       while ((IDom = N->getIDom()) != 0 && IDom != this)
265         N = IDom;   // Walk up the tree
266       return IDom != 0;
267     }
268
269   private:
270     inline Node2(BasicBlock *node, Node *iDom) 
271       : TheNode(node), IDom(iDom) {}
272     inline Node2 *addChild(Node *C) { Children.push_back(C); return C; }
273
274     void setIDom(Node2 *NewIDom);
275   };
276
277 public:
278   DominatorTreeBase(bool isPostDom) : DominatorBase(isPostDom) {}
279   ~DominatorTreeBase() { reset(); }
280
281   virtual void releaseMemory() { reset(); }
282
283   /// getNode - return the (Post)DominatorTree node for the specified basic
284   /// block.  This is the same as using operator[] on this class.
285   ///
286   inline Node *getNode(BasicBlock *BB) const {
287     NodeMapType::const_iterator i = Nodes.find(BB);
288     return (i != Nodes.end()) ? i->second : 0;
289   }
290
291   inline Node *operator[](BasicBlock *BB) const {
292     return getNode(BB);
293   }
294
295   //===--------------------------------------------------------------------===//  // API to update (Post)DominatorTree information based on modifications to
296   // the CFG...
297
298   /// createNewNode - Add a new node to the dominator tree information.  This
299   /// creates a new node as a child of IDomNode, linking it into the children
300   /// list of the immediate dominator.
301   ///
302   Node *createNewNode(BasicBlock *BB, Node *IDomNode) {
303     assert(getNode(BB) == 0 && "Block already in dominator tree!");
304     assert(IDomNode && "Not immediate dominator specified for block!");
305     return Nodes[BB] = IDomNode->addChild(new Node(BB, IDomNode));
306   }
307
308   /// changeImmediateDominator - This method is used to update the dominator
309   /// tree information when a node's immediate dominator changes.
310   ///
311   void changeImmediateDominator(Node *Node, Node *NewIDom) {
312     assert(Node && NewIDom && "Cannot change null node pointers!");
313     Node->setIDom(NewIDom);
314   }
315
316   /// print - Convert to human readable form
317   virtual void print(std::ostream &OS) const;
318 };
319
320
321 //===-------------------------------------
322 // DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
323 // compute a normal dominator tree.
324 //
325 struct DominatorTree : public DominatorTreeBase {
326   DominatorTree() : DominatorTreeBase(false) {}
327
328   virtual bool runOnFunction(Function &F) {
329     reset();     // Reset from the last time we were run...
330     DominatorSet &DS = getAnalysis<DominatorSet>();
331     Root = DS.getRoot();
332     calculate(DS);
333     return false;
334   }
335
336   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
337     AU.setPreservesAll();
338     AU.addRequired<DominatorSet>();
339   }
340 private:
341   void calculate(const DominatorSet &DS);
342 };
343
344
345 //===----------------------------------------------------------------------===//
346 //
347 // DominanceFrontier - Calculate the dominance frontiers for a function.
348 //
349 class DominanceFrontierBase : public DominatorBase {
350 public:
351   typedef std::set<BasicBlock*>             DomSetType;    // Dom set for a bb
352   typedef std::map<BasicBlock*, DomSetType> DomSetMapType; // Dom set map
353 protected:
354   DomSetMapType Frontiers;
355 public:
356   DominanceFrontierBase(bool isPostDom) : DominatorBase(isPostDom) {}
357
358   virtual void releaseMemory() { Frontiers.clear(); }
359
360   // Accessor interface:
361   typedef DomSetMapType::const_iterator const_iterator;
362   inline const_iterator begin() const { return Frontiers.begin(); }
363   inline const_iterator end()   const { return Frontiers.end(); }
364   inline const_iterator find(BasicBlock* B) const { return Frontiers.find(B); }
365
366   // print - Convert to human readable form
367   virtual void print(std::ostream &OS) const;
368 };
369
370
371 //===-------------------------------------
372 // DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
373 // compute a normal dominator tree.
374 //
375 struct DominanceFrontier : public DominanceFrontierBase {
376   DominanceFrontier() : DominanceFrontierBase(false) {}
377
378   virtual bool runOnFunction(Function &) {
379     Frontiers.clear();
380     DominatorTree &DT = getAnalysis<DominatorTree>();
381     Root = DT.getRoot();
382     calculate(DT, DT[Root]);
383     return false;
384   }
385
386   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
387     AU.setPreservesAll();
388     AU.addRequired<DominatorTree>();
389   }
390 private:
391   const DomSetType &calculate(const DominatorTree &DT,
392                               const DominatorTree::Node *Node);
393 };
394
395 #endif