- Doxygenize comments
[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
112
113 //===-------------------------------------
114 // DominatorSet Class - Concrete subclass of DominatorSetBase that is used to
115 // compute a normal dominator set.
116 //
117 struct DominatorSet : public DominatorSetBase {
118   DominatorSet() : DominatorSetBase(false) {}
119
120   virtual bool runOnFunction(Function &F);
121
122   // getAnalysisUsage - This simply provides a dominator set
123   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
124     AU.setPreservesAll();
125   }
126 private:
127   void calculateDominatorsFromBlock(BasicBlock *BB);
128 };
129
130
131 //===----------------------------------------------------------------------===//
132 //
133 // ImmediateDominators - Calculate the immediate dominator for each node in a
134 // function.
135 //
136 class ImmediateDominatorsBase : public DominatorBase {
137 protected:
138   std::map<BasicBlock*, BasicBlock*> IDoms;
139   void calcIDoms(const DominatorSetBase &DS);
140 public:
141   ImmediateDominatorsBase(bool isPostDom) : DominatorBase(isPostDom) {}
142
143   virtual void releaseMemory() { IDoms.clear(); }
144
145   // Accessor interface:
146   typedef std::map<BasicBlock*, BasicBlock*> IDomMapType;
147   typedef IDomMapType::const_iterator const_iterator;
148   inline const_iterator begin() const { return IDoms.begin(); }
149   inline const_iterator end()   const { return IDoms.end(); }
150   inline const_iterator find(BasicBlock* B) const { return IDoms.find(B);}
151
152   // operator[] - Return the idom for the specified basic block.  The start
153   // node returns null, because it does not have an immediate dominator.
154   //
155   inline BasicBlock *operator[](BasicBlock *BB) const {
156     return get(BB);
157   }
158
159   // get() - Synonym for operator[].
160   inline BasicBlock *get(BasicBlock *BB) const {
161     std::map<BasicBlock*, BasicBlock*>::const_iterator I = IDoms.find(BB);
162     return I != IDoms.end() ? I->second : 0;
163   }
164
165   //===--------------------------------------------------------------------===//
166   // API to update Immediate(Post)Dominators information based on modifications
167   // to the CFG...
168
169   /// addNewBlock - Add a new block to the CFG, with the specified immediate
170   /// dominator.
171   ///
172   void addNewBlock(BasicBlock *BB, BasicBlock *IDom) {
173     assert(get(BB) == 0 && "BasicBlock already in idom info!");
174     IDoms[BB] = IDom;
175   }
176
177
178   // print - Convert to human readable form
179   virtual void print(std::ostream &OS) const;
180 };
181
182 //===-------------------------------------
183 // ImmediateDominators Class - Concrete subclass of ImmediateDominatorsBase that
184 // is used to compute a normal immediate dominator set.
185 //
186 struct ImmediateDominators : public ImmediateDominatorsBase {
187   ImmediateDominators() : ImmediateDominatorsBase(false) {}
188
189   virtual bool runOnFunction(Function &F) {
190     IDoms.clear();     // Reset from the last time we were run...
191     DominatorSet &DS = getAnalysis<DominatorSet>();
192     Root = DS.getRoot();
193     calcIDoms(DS);
194     return false;
195   }
196
197   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
198     AU.setPreservesAll();
199     AU.addRequired<DominatorSet>();
200   }
201 };
202
203
204 //===----------------------------------------------------------------------===//
205 //
206 // DominatorTree - Calculate the immediate dominator tree for a function.
207 //
208 class DominatorTreeBase : public DominatorBase {
209 protected:
210   class Node2;
211 public:
212   typedef Node2 Node;
213 protected:
214   std::map<BasicBlock*, Node*> Nodes;
215   void reset();
216   typedef std::map<BasicBlock*, Node*> NodeMapType;
217 public:
218   class Node2 : public std::vector<Node*> {
219     friend class DominatorTree;
220     friend class PostDominatorTree;
221     friend class DominatorTreeBase;
222     BasicBlock *TheNode;
223     Node2 *IDom;
224   public:
225     inline BasicBlock *getNode() const { return TheNode; }
226     inline Node2 *getIDom() const { return IDom; }
227     inline const std::vector<Node*> &getChildren() const { return *this; }
228
229     // dominates - Returns true iff this dominates N.  Note that this is not a 
230     // constant time operation!
231     inline bool dominates(const Node2 *N) const {
232       const Node2 *IDom;
233       while ((IDom = N->getIDom()) != 0 && IDom != this)
234         N = IDom;   // Walk up the tree
235       return IDom != 0;
236     }
237
238   private:
239     inline Node2(BasicBlock *node, Node *iDom) 
240       : TheNode(node), IDom(iDom) {}
241     inline Node2 *addChild(Node *C) { push_back(C); return C; }
242   };
243
244 public:
245   DominatorTreeBase(bool isPostDom) : DominatorBase(isPostDom) {}
246   ~DominatorTreeBase() { reset(); }
247
248   virtual void releaseMemory() { reset(); }
249
250   /// getNode - return the (Post)DominatorTree node for the specified basic
251   /// block.  This is the same as using operator[] on this class.
252   ///
253   inline Node *getNode(BasicBlock *BB) const {
254     NodeMapType::const_iterator i = Nodes.find(BB);
255     return (i != Nodes.end()) ? i->second : 0;
256   }
257
258   inline Node *operator[](BasicBlock *BB) const {
259     return getNode(BB);
260   }
261
262   // API to update (Post)DominatorTree information based on modifications to
263   // the CFG...
264
265   /// createNewNode - Add a new node to the dominator tree information.  This
266   /// creates a new node as a child of IDomNode, linking it into the children
267   /// list of the immediate dominator.
268   ///
269   Node *createNewNode(BasicBlock *BB, Node *IDomNode) {
270     assert(getNode(BB) == 0 && "Block already in dominator tree!");
271     Node *New = Nodes[BB] = new Node(BB, IDomNode);
272     if (IDomNode) IDomNode->addChild(New);
273     return New;
274   }
275
276   /// print - Convert to human readable form
277   virtual void print(std::ostream &OS) const;
278 };
279
280
281 //===-------------------------------------
282 // DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
283 // compute a normal dominator tree.
284 //
285 struct DominatorTree : public DominatorTreeBase {
286   DominatorTree() : DominatorTreeBase(false) {}
287
288   virtual bool runOnFunction(Function &F) {
289     reset();     // Reset from the last time we were run...
290     DominatorSet &DS = getAnalysis<DominatorSet>();
291     Root = DS.getRoot();
292     calculate(DS);
293     return false;
294   }
295
296   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
297     AU.setPreservesAll();
298     AU.addRequired<DominatorSet>();
299   }
300 private:
301   void calculate(const DominatorSet &DS);
302 };
303
304
305 //===----------------------------------------------------------------------===//
306 //
307 // DominanceFrontier - Calculate the dominance frontiers for a function.
308 //
309 class DominanceFrontierBase : public DominatorBase {
310 public:
311   typedef std::set<BasicBlock*>             DomSetType;    // Dom set for a bb
312   typedef std::map<BasicBlock*, DomSetType> DomSetMapType; // Dom set map
313 protected:
314   DomSetMapType Frontiers;
315 public:
316   DominanceFrontierBase(bool isPostDom) : DominatorBase(isPostDom) {}
317
318   virtual void releaseMemory() { Frontiers.clear(); }
319
320   // Accessor interface:
321   typedef DomSetMapType::const_iterator const_iterator;
322   inline const_iterator begin() const { return Frontiers.begin(); }
323   inline const_iterator end()   const { return Frontiers.end(); }
324   inline const_iterator find(BasicBlock* B) const { return Frontiers.find(B); }
325
326   // print - Convert to human readable form
327   virtual void print(std::ostream &OS) const;
328 };
329
330
331 //===-------------------------------------
332 // DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
333 // compute a normal dominator tree.
334 //
335 struct DominanceFrontier : public DominanceFrontierBase {
336   DominanceFrontier() : DominanceFrontierBase(false) {}
337
338   virtual bool runOnFunction(Function &) {
339     Frontiers.clear();
340     DominatorTree &DT = getAnalysis<DominatorTree>();
341     Root = DT.getRoot();
342     calculate(DT, DT[Root]);
343     return false;
344   }
345
346   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
347     AU.setPreservesAll();
348     AU.addRequired<DominatorTree>();
349   }
350 private:
351   const DomSetType &calculate(const DominatorTree &DT,
352                               const DominatorTree::Node *Node);
353 };
354
355 #endif