[Codegen] Add intrinsics 'absdiff' and corresponding SDNodes for absolute difference...
[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
73   DominatorTree(DominatorTree &&Arg)
74       : Base(std::move(static_cast<Base &>(Arg))) {}
75   DominatorTree &operator=(DominatorTree &&RHS) {
76     Base::operator=(std::move(static_cast<Base &>(RHS)));
77     return *this;
78   }
79
80   /// \brief Returns *false* if the other dominator tree matches this dominator
81   /// tree.
82   inline bool compare(const DominatorTree &Other) const {
83     const DomTreeNode *R = getRootNode();
84     const DomTreeNode *OtherR = Other.getRootNode();
85
86     if (!R || !OtherR || R->getBlock() != OtherR->getBlock())
87       return true;
88
89     if (Base::compare(Other))
90       return true;
91
92     return false;
93   }
94
95   // Ensure base-class overloads are visible.
96   using Base::dominates;
97
98   /// \brief Return true if Def dominates a use in User.
99   ///
100   /// This performs the special checks necessary if Def and User are in the same
101   /// basic block. Note that Def doesn't dominate a use in Def itself!
102   bool dominates(const Instruction *Def, const Use &U) const;
103   bool dominates(const Instruction *Def, const Instruction *User) const;
104   bool dominates(const Instruction *Def, const BasicBlock *BB) const;
105   bool dominates(const BasicBlockEdge &BBE, const Use &U) const;
106   bool dominates(const BasicBlockEdge &BBE, const BasicBlock *BB) const;
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 DominatorTreeAnalysis {
167 public:
168   /// \brief Provide the result typedef for this analysis pass.
169   typedef DominatorTree Result;
170
171   /// \brief Opaque, unique identifier for this analysis pass.
172   static void *ID() { return (void *)&PassID; }
173
174   /// \brief Run the analysis pass over a function and produce a dominator tree.
175   DominatorTree run(Function &F);
176
177   /// \brief Provide access to a name for this pass for debugging purposes.
178   static StringRef name() { return "DominatorTreeAnalysis"; }
179
180 private:
181   static char PassID;
182 };
183
184 /// \brief Printer pass for the \c DominatorTree.
185 class DominatorTreePrinterPass {
186   raw_ostream &OS;
187
188 public:
189   explicit DominatorTreePrinterPass(raw_ostream &OS);
190   PreservedAnalyses run(Function &F, AnalysisManager<Function> *AM);
191
192   static StringRef name() { return "DominatorTreePrinterPass"; }
193 };
194
195 /// \brief Verifier pass for the \c DominatorTree.
196 struct DominatorTreeVerifierPass {
197   PreservedAnalyses run(Function &F, AnalysisManager<Function> *AM);
198
199   static StringRef name() { return "DominatorTreeVerifierPass"; }
200 };
201
202 /// \brief Legacy analysis pass which computes a \c DominatorTree.
203 class DominatorTreeWrapperPass : public FunctionPass {
204   DominatorTree DT;
205
206 public:
207   static char ID;
208
209   DominatorTreeWrapperPass() : FunctionPass(ID) {
210     initializeDominatorTreeWrapperPassPass(*PassRegistry::getPassRegistry());
211   }
212
213   DominatorTree &getDomTree() { return DT; }
214   const DominatorTree &getDomTree() const { return DT; }
215
216   bool runOnFunction(Function &F) override;
217
218   void verifyAnalysis() const override;
219
220   void getAnalysisUsage(AnalysisUsage &AU) const override {
221     AU.setPreservesAll();
222   }
223
224   void releaseMemory() override { DT.releaseMemory(); }
225
226   void print(raw_ostream &OS, const Module *M = nullptr) const override;
227 };
228
229 } // End llvm namespace
230
231 #endif