[PM][cleanup] Clean up comments and use modern doxygen in this file.
[oota-llvm.git] / include / llvm / IR / Dominators.h
1 //===- Dominators.h - Dominator Info Calculation ----------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the DominatorTree class, which provides fast and efficient
11 // dominance queries.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_IR_DOMINATORS_H
16 #define LLVM_IR_DOMINATORS_H
17
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/DepthFirstIterator.h"
20 #include "llvm/ADT/GraphTraits.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/IR/BasicBlock.h"
24 #include "llvm/IR/Function.h"
25 #include "llvm/Pass.h"
26 #include "llvm/Support/CFG.h"
27 #include "llvm/Support/GenericDomTree.h"
28 #include "llvm/Support/Compiler.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include <algorithm>
31
32 namespace llvm {
33
34 EXTERN_TEMPLATE_INSTANTIATION(class DomTreeNodeBase<BasicBlock>);
35 EXTERN_TEMPLATE_INSTANTIATION(class DominatorTreeBase<BasicBlock>);
36
37 typedef DomTreeNodeBase<BasicBlock> DomTreeNode;
38
39 class BasicBlockEdge {
40   const BasicBlock *Start;
41   const BasicBlock *End;
42 public:
43   BasicBlockEdge(const BasicBlock *Start_, const BasicBlock *End_) :
44     Start(Start_), End(End_) { }
45   const BasicBlock *getStart() const {
46     return Start;
47   }
48   const BasicBlock *getEnd() const {
49     return End;
50   }
51   bool isSingleEdge() const;
52 };
53
54 /// \brief Concrete subclass of DominatorTreeBase that is used to compute a
55 /// normal dominator tree.
56 class DominatorTree : public FunctionPass {
57 public:
58   static char ID; // Pass ID, replacement for typeid
59   DominatorTreeBase<BasicBlock>* DT;
60
61   DominatorTree() : FunctionPass(ID) {
62     initializeDominatorTreePass(*PassRegistry::getPassRegistry());
63     DT = new DominatorTreeBase<BasicBlock>(false);
64   }
65
66   ~DominatorTree() {
67     delete DT;
68   }
69
70   DominatorTreeBase<BasicBlock>& getBase() { return *DT; }
71
72   /// \brief Returns the root blocks of the current CFG.
73   ///
74   /// This may include multiple blocks if we are computing post dominators.
75   /// For forward dominators, this will always be a single block (the entry
76   /// node).
77   inline const std::vector<BasicBlock*> &getRoots() const {
78     return DT->getRoots();
79   }
80
81   inline BasicBlock *getRoot() const {
82     return DT->getRoot();
83   }
84
85   inline DomTreeNode *getRootNode() const {
86     return DT->getRootNode();
87   }
88
89   /// Get all nodes dominated by R, including R itself.
90   void getDescendants(BasicBlock *R,
91                      SmallVectorImpl<BasicBlock *> &Result) const {
92     DT->getDescendants(R, Result);
93   }
94
95   /// \brief Returns *false* if the other dominator tree matches this dominator
96   /// tree.
97   inline bool compare(DominatorTree &Other) const {
98     DomTreeNode *R = getRootNode();
99     DomTreeNode *OtherR = Other.getRootNode();
100
101     if (!R || !OtherR || R->getBlock() != OtherR->getBlock())
102       return true;
103
104     if (DT->compare(Other.getBase()))
105       return true;
106
107     return false;
108   }
109
110   virtual bool runOnFunction(Function &F);
111
112   virtual void verifyAnalysis() const;
113
114   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
115     AU.setPreservesAll();
116   }
117
118   inline bool dominates(const DomTreeNode* A, const DomTreeNode* B) const {
119     return DT->dominates(A, B);
120   }
121
122   inline bool dominates(const BasicBlock* A, const BasicBlock* B) const {
123     return DT->dominates(A, B);
124   }
125
126   // \brief Return true if Def dominates a use in User.
127   //
128   // This performs the special checks necessary if Def and User are in the same
129   // basic block. Note that Def doesn't dominate a use in Def itself!
130   bool dominates(const Instruction *Def, const Use &U) const;
131   bool dominates(const Instruction *Def, const Instruction *User) const;
132   bool dominates(const Instruction *Def, const BasicBlock *BB) const;
133   bool dominates(const BasicBlockEdge &BBE, const Use &U) const;
134   bool dominates(const BasicBlockEdge &BBE, const BasicBlock *BB) const;
135
136   bool properlyDominates(const DomTreeNode *A, const DomTreeNode *B) const {
137     return DT->properlyDominates(A, B);
138   }
139
140   bool properlyDominates(const BasicBlock *A, const BasicBlock *B) const {
141     return DT->properlyDominates(A, B);
142   }
143
144   /// \brief Find nearest common dominator basic block for basic block A and B.
145   ///
146   /// If there is no such block then return NULL.
147   inline BasicBlock *findNearestCommonDominator(BasicBlock *A, BasicBlock *B) {
148     return DT->findNearestCommonDominator(A, B);
149   }
150
151   inline const BasicBlock *findNearestCommonDominator(const BasicBlock *A,
152                                                       const BasicBlock *B) {
153     return DT->findNearestCommonDominator(A, B);
154   }
155
156   inline DomTreeNode *operator[](BasicBlock *BB) const {
157     return DT->getNode(BB);
158   }
159
160   /// \brief Returns the DominatorTree node for the specified basic block.
161   ///
162   /// This is the same as using operator[] on this class.
163   inline DomTreeNode *getNode(BasicBlock *BB) const {
164     return DT->getNode(BB);
165   }
166
167   /// \brief Add a new node to the dominator tree information.
168   ///
169   /// This creates a new node as a child of DomBB dominator node, linking it
170   /// into the children list of the immediate dominator.
171   inline DomTreeNode *addNewBlock(BasicBlock *BB, BasicBlock *DomBB) {
172     return DT->addNewBlock(BB, DomBB);
173   }
174
175   /// \brief Updates the dominator tree information when a node's immediate
176   /// dominator changes.
177   inline void changeImmediateDominator(BasicBlock *N, BasicBlock* NewIDom) {
178     DT->changeImmediateDominator(N, NewIDom);
179   }
180
181   inline void changeImmediateDominator(DomTreeNode *N, DomTreeNode* NewIDom) {
182     DT->changeImmediateDominator(N, NewIDom);
183   }
184
185   /// \brief Removes a node from the dominator tree.
186   ///
187   /// The block must not dominate any other blocks. Removes node from its
188   /// immediate dominator's children list. Deletes dominator node associated
189   /// with basic block BB.
190   inline void eraseNode(BasicBlock *BB) {
191     DT->eraseNode(BB);
192   }
193
194   /// \brief BB is split and now it has one successor; update dominator tree to
195   /// reflect this change.
196   inline void splitBlock(BasicBlock* NewBB) {
197     DT->splitBlock(NewBB);
198   }
199
200   bool isReachableFromEntry(const BasicBlock* A) const {
201     return DT->isReachableFromEntry(A);
202   }
203
204   bool isReachableFromEntry(const Use &U) const;
205
206
207   virtual void releaseMemory() {
208     DT->releaseMemory();
209   }
210
211   virtual void print(raw_ostream &OS, const Module* M= 0) const;
212 };
213
214 //===-------------------------------------
215 // DominatorTree GraphTraits specializations so the DominatorTree can be
216 // iterable by generic graph iterators.
217
218 template <> struct GraphTraits<DomTreeNode*> {
219   typedef DomTreeNode NodeType;
220   typedef NodeType::iterator  ChildIteratorType;
221
222   static NodeType *getEntryNode(NodeType *N) {
223     return N;
224   }
225   static inline ChildIteratorType child_begin(NodeType *N) {
226     return N->begin();
227   }
228   static inline ChildIteratorType child_end(NodeType *N) {
229     return N->end();
230   }
231
232   typedef df_iterator<DomTreeNode*> nodes_iterator;
233
234   static nodes_iterator nodes_begin(DomTreeNode *N) {
235     return df_begin(getEntryNode(N));
236   }
237
238   static nodes_iterator nodes_end(DomTreeNode *N) {
239     return df_end(getEntryNode(N));
240   }
241 };
242
243 template <> struct GraphTraits<DominatorTree*>
244   : public GraphTraits<DomTreeNode*> {
245   static NodeType *getEntryNode(DominatorTree *DT) {
246     return DT->getRootNode();
247   }
248
249   static nodes_iterator nodes_begin(DominatorTree *N) {
250     return df_begin(getEntryNode(N));
251   }
252
253   static nodes_iterator nodes_end(DominatorTree *N) {
254     return df_end(getEntryNode(N));
255   }
256 };
257
258 } // End llvm namespace
259
260 #endif