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