[opaque pointer types] Push the passing of value types up from Function/GlobalVariabl...
[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 // FIXME: Replace this brittle forward declaration with the include of the new
35 // PassManager.h when doing so doesn't break the PassManagerBuilder.
36 template <typename IRUnitT> class AnalysisManager;
37 class PreservedAnalyses;
38
39 extern template class DomTreeNodeBase<BasicBlock>;
40 extern template class DominatorTreeBase<BasicBlock>;
41
42 extern template void Calculate<Function, BasicBlock *>(
43     DominatorTreeBase<GraphTraits<BasicBlock *>::NodeType> &DT, Function &F);
44 extern template void Calculate<Function, Inverse<BasicBlock *>>(
45     DominatorTreeBase<GraphTraits<Inverse<BasicBlock *>>::NodeType> &DT,
46     Function &F);
47
48 typedef DomTreeNodeBase<BasicBlock> DomTreeNode;
49
50 class BasicBlockEdge {
51   const BasicBlock *Start;
52   const BasicBlock *End;
53 public:
54   BasicBlockEdge(const BasicBlock *Start_, const BasicBlock *End_) :
55     Start(Start_), End(End_) { }
56   const BasicBlock *getStart() const {
57     return Start;
58   }
59   const BasicBlock *getEnd() const {
60     return End;
61   }
62   bool isSingleEdge() const;
63 };
64
65 /// \brief Concrete subclass of DominatorTreeBase that is used to compute a
66 /// normal dominator tree.
67 class DominatorTree : public DominatorTreeBase<BasicBlock> {
68 public:
69   typedef DominatorTreeBase<BasicBlock> Base;
70
71   DominatorTree() : DominatorTreeBase<BasicBlock>(false) {}
72   explicit DominatorTree(Function &F) : DominatorTreeBase<BasicBlock>(false) {
73     recalculate(F);
74   }
75
76   DominatorTree(DominatorTree &&Arg)
77       : Base(std::move(static_cast<Base &>(Arg))) {}
78   DominatorTree &operator=(DominatorTree &&RHS) {
79     Base::operator=(std::move(static_cast<Base &>(RHS)));
80     return *this;
81   }
82
83   /// \brief Returns *false* if the other dominator tree matches this dominator
84   /// tree.
85   inline bool compare(const DominatorTree &Other) const {
86     const DomTreeNode *R = getRootNode();
87     const DomTreeNode *OtherR = Other.getRootNode();
88
89     if (!R || !OtherR || R->getBlock() != OtherR->getBlock())
90       return true;
91
92     if (Base::compare(Other))
93       return true;
94
95     return false;
96   }
97
98   // Ensure base-class overloads are visible.
99   using Base::dominates;
100
101   /// \brief Return true if Def dominates a use in User.
102   ///
103   /// This performs the special checks necessary if Def and User are in the same
104   /// basic block. Note that Def doesn't dominate a use in Def itself!
105   bool dominates(const Instruction *Def, const Use &U) const;
106   bool dominates(const Instruction *Def, const Instruction *User) const;
107   bool dominates(const Instruction *Def, const BasicBlock *BB) const;
108   bool dominates(const BasicBlockEdge &BBE, const Use &U) const;
109   bool dominates(const BasicBlockEdge &BBE, const BasicBlock *BB) const;
110
111   // Ensure base class overloads are visible.
112   using Base::isReachableFromEntry;
113
114   /// \brief Provide an overload for a Use.
115   bool isReachableFromEntry(const Use &U) const;
116
117   /// \brief Verify the correctness of the domtree by re-computing it.
118   ///
119   /// This should only be used for debugging as it aborts the program if the
120   /// verification fails.
121   void verifyDomTree() const;
122 };
123
124 //===-------------------------------------
125 // DominatorTree GraphTraits specializations so the DominatorTree can be
126 // iterable by generic graph iterators.
127
128 template <class Node, class ChildIterator> struct DomTreeGraphTraitsBase {
129   typedef Node NodeType;
130   typedef ChildIterator ChildIteratorType;
131   typedef df_iterator<Node *, SmallPtrSet<NodeType *, 8>> nodes_iterator;
132
133   static NodeType *getEntryNode(NodeType *N) { return N; }
134   static inline ChildIteratorType child_begin(NodeType *N) {
135     return N->begin();
136   }
137   static inline ChildIteratorType child_end(NodeType *N) { return N->end(); }
138
139   static nodes_iterator nodes_begin(NodeType *N) {
140     return df_begin(getEntryNode(N));
141   }
142
143   static nodes_iterator nodes_end(NodeType *N) {
144     return df_end(getEntryNode(N));
145   }
146 };
147
148 template <>
149 struct GraphTraits<DomTreeNode *>
150     : public DomTreeGraphTraitsBase<DomTreeNode, DomTreeNode::iterator> {};
151
152 template <>
153 struct GraphTraits<const DomTreeNode *>
154     : public DomTreeGraphTraitsBase<const DomTreeNode,
155                                     DomTreeNode::const_iterator> {};
156
157 template <> struct GraphTraits<DominatorTree*>
158   : public GraphTraits<DomTreeNode*> {
159   static NodeType *getEntryNode(DominatorTree *DT) {
160     return DT->getRootNode();
161   }
162
163   static nodes_iterator nodes_begin(DominatorTree *N) {
164     return df_begin(getEntryNode(N));
165   }
166
167   static nodes_iterator nodes_end(DominatorTree *N) {
168     return df_end(getEntryNode(N));
169   }
170 };
171
172 /// \brief Analysis pass which computes a \c DominatorTree.
173 class DominatorTreeAnalysis {
174 public:
175   /// \brief Provide the result typedef for this analysis pass.
176   typedef DominatorTree Result;
177
178   /// \brief Opaque, unique identifier for this analysis pass.
179   static void *ID() { return (void *)&PassID; }
180
181   /// \brief Run the analysis pass over a function and produce a dominator tree.
182   DominatorTree run(Function &F);
183
184   /// \brief Provide access to a name for this pass for debugging purposes.
185   static StringRef name() { return "DominatorTreeAnalysis"; }
186
187 private:
188   static char PassID;
189 };
190
191 /// \brief Printer pass for the \c DominatorTree.
192 class DominatorTreePrinterPass {
193   raw_ostream &OS;
194
195 public:
196   explicit DominatorTreePrinterPass(raw_ostream &OS);
197   PreservedAnalyses run(Function &F, AnalysisManager<Function> *AM);
198
199   static StringRef name() { return "DominatorTreePrinterPass"; }
200 };
201
202 /// \brief Verifier pass for the \c DominatorTree.
203 struct DominatorTreeVerifierPass {
204   PreservedAnalyses run(Function &F, AnalysisManager<Function> *AM);
205
206   static StringRef name() { return "DominatorTreeVerifierPass"; }
207 };
208
209 /// \brief Legacy analysis pass which computes a \c DominatorTree.
210 class DominatorTreeWrapperPass : public FunctionPass {
211   DominatorTree DT;
212
213 public:
214   static char ID;
215
216   DominatorTreeWrapperPass() : FunctionPass(ID) {
217     initializeDominatorTreeWrapperPassPass(*PassRegistry::getPassRegistry());
218   }
219
220   DominatorTree &getDomTree() { return DT; }
221   const DominatorTree &getDomTree() const { return DT; }
222
223   bool runOnFunction(Function &F) override;
224
225   void verifyAnalysis() const override;
226
227   void getAnalysisUsage(AnalysisUsage &AU) const override {
228     AU.setPreservesAll();
229   }
230
231   void releaseMemory() override { DT.releaseMemory(); }
232
233   void print(raw_ostream &OS, const Module *M = nullptr) const override;
234 };
235
236 } // End llvm namespace
237
238 #endif