550b634a24fd2887c7ceef3e043afddb24935e7b
[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/CFG.h"
25 #include "llvm/IR/Function.h"
26 #include "llvm/Pass.h"
27 #include "llvm/Support/Compiler.h"
28 #include "llvm/Support/GenericDomTree.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 #define LLVM_COMMA ,
38 EXTERN_TEMPLATE_INSTANTIATION(void Calculate<Function LLVM_COMMA BasicBlock *>(
39     DominatorTreeBase<GraphTraits<BasicBlock *>::NodeType> &DT LLVM_COMMA
40         Function &F));
41 EXTERN_TEMPLATE_INSTANTIATION(
42     void Calculate<Function LLVM_COMMA Inverse<BasicBlock *> >(
43         DominatorTreeBase<GraphTraits<Inverse<BasicBlock *> >::NodeType> &DT
44             LLVM_COMMA Function &F));
45 #undef LLVM_COMMA
46
47 typedef DomTreeNodeBase<BasicBlock> DomTreeNode;
48
49 class BasicBlockEdge {
50   const BasicBlock *Start;
51   const BasicBlock *End;
52 public:
53   BasicBlockEdge(const BasicBlock *Start_, const BasicBlock *End_) :
54     Start(Start_), End(End_) { }
55   const BasicBlock *getStart() const {
56     return Start;
57   }
58   const BasicBlock *getEnd() const {
59     return End;
60   }
61   bool isSingleEdge() const;
62 };
63
64 /// \brief Concrete subclass of DominatorTreeBase that is used to compute a
65 /// normal dominator tree.
66 class DominatorTree : public DominatorTreeBase<BasicBlock> {
67 public:
68   typedef DominatorTreeBase<BasicBlock> Base;
69
70   DominatorTree() : DominatorTreeBase<BasicBlock>(false) {}
71
72   // FIXME: This is no longer needed and should be removed when its uses are
73   // cleaned up.
74   Base& getBase() { return *this; }
75
76   /// \brief Returns *false* if the other dominator tree matches this dominator
77   /// tree.
78   inline bool compare(const DominatorTree &Other) const {
79     const DomTreeNode *R = getRootNode();
80     const DomTreeNode *OtherR = Other.getRootNode();
81
82     if (!R || !OtherR || R->getBlock() != OtherR->getBlock())
83       return true;
84
85     if (Base::compare(Other))
86       return true;
87
88     return false;
89   }
90
91   // Ensure base-class overloads are visible.
92   using Base::dominates;
93
94   /// \brief Return true if Def dominates a use in User.
95   ///
96   /// This performs the special checks necessary if Def and User are in the same
97   /// basic block. Note that Def doesn't dominate a use in Def itself!
98   bool dominates(const Instruction *Def, const Use &U) const;
99   bool dominates(const Instruction *Def, const Instruction *User) const;
100   bool dominates(const Instruction *Def, const BasicBlock *BB) const;
101   bool dominates(const BasicBlockEdge &BBE, const Use &U) const;
102   bool dominates(const BasicBlockEdge &BBE, const BasicBlock *BB) const;
103
104   inline DomTreeNode *operator[](BasicBlock *BB) const {
105     return getNode(BB);
106   }
107
108   // Ensure base class overloads are visible.
109   using Base::isReachableFromEntry;
110
111   /// \brief Provide an overload for a Use.
112   bool isReachableFromEntry(const Use &U) const;
113
114   /// \brief Verify the correctness of the domtree by re-computing it.
115   ///
116   /// This should only be used for debugging as it aborts the program if the
117   /// verification fails.
118   void verifyDomTree() const;
119 };
120
121 //===-------------------------------------
122 // DominatorTree GraphTraits specializations so the DominatorTree can be
123 // iterable by generic graph iterators.
124
125 template <> struct GraphTraits<DomTreeNode*> {
126   typedef DomTreeNode NodeType;
127   typedef NodeType::iterator  ChildIteratorType;
128
129   static NodeType *getEntryNode(NodeType *N) {
130     return N;
131   }
132   static inline ChildIteratorType child_begin(NodeType *N) {
133     return N->begin();
134   }
135   static inline ChildIteratorType child_end(NodeType *N) {
136     return N->end();
137   }
138
139   typedef df_iterator<DomTreeNode*> nodes_iterator;
140
141   static nodes_iterator nodes_begin(DomTreeNode *N) {
142     return df_begin(getEntryNode(N));
143   }
144
145   static nodes_iterator nodes_end(DomTreeNode *N) {
146     return df_end(getEntryNode(N));
147   }
148 };
149
150 template <> struct GraphTraits<DominatorTree*>
151   : public GraphTraits<DomTreeNode*> {
152   static NodeType *getEntryNode(DominatorTree *DT) {
153     return DT->getRootNode();
154   }
155
156   static nodes_iterator nodes_begin(DominatorTree *N) {
157     return df_begin(getEntryNode(N));
158   }
159
160   static nodes_iterator nodes_end(DominatorTree *N) {
161     return df_end(getEntryNode(N));
162   }
163 };
164
165 /// \brief Analysis pass which computes a \c DominatorTree.
166 class DominatorTreeWrapperPass : public FunctionPass {
167   DominatorTree DT;
168
169 public:
170   static char ID;
171
172   DominatorTreeWrapperPass() : FunctionPass(ID) {
173     initializeDominatorTreeWrapperPassPass(*PassRegistry::getPassRegistry());
174   }
175
176   DominatorTree &getDomTree() { return DT; }
177   const DominatorTree &getDomTree() const { return DT; }
178
179   virtual bool runOnFunction(Function &F);
180
181   virtual void verifyAnalysis() const;
182
183   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
184     AU.setPreservesAll();
185   }
186
187   virtual void releaseMemory() { DT.releaseMemory(); }
188
189   virtual void print(raw_ostream &OS, const Module *M = 0) const;
190 };
191
192 } // End llvm namespace
193
194 #endif