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