]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/IR/Dominators.h
Merge ^/head r314270 through r314419.
[FreeBSD/FreeBSD.git] / contrib / llvm / 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/DenseMapInfo.h"
19 #include "llvm/ADT/GraphTraits.h"
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/IR/CFG.h"
22 #include "llvm/IR/PassManager.h"
23 #include "llvm/Pass.h"
24 #include "llvm/Support/GenericDomTree.h"
25
26 namespace llvm {
27
28 class Function;
29 class BasicBlock;
30 class raw_ostream;
31
32 extern template class DomTreeNodeBase<BasicBlock>;
33 extern template class DominatorTreeBase<BasicBlock>;
34
35 extern template void Calculate<Function, BasicBlock *>(
36     DominatorTreeBaseByGraphTraits<GraphTraits<BasicBlock *>> &DT, Function &F);
37 extern template void Calculate<Function, Inverse<BasicBlock *>>(
38     DominatorTreeBaseByGraphTraits<GraphTraits<Inverse<BasicBlock *>>> &DT,
39     Function &F);
40
41 typedef DomTreeNodeBase<BasicBlock> DomTreeNode;
42
43 class BasicBlockEdge {
44   const BasicBlock *Start;
45   const BasicBlock *End;
46 public:
47   BasicBlockEdge(const BasicBlock *Start_, const BasicBlock *End_) :
48     Start(Start_), End(End_) { }
49   const BasicBlock *getStart() const {
50     return Start;
51   }
52   const BasicBlock *getEnd() const {
53     return End;
54   }
55   bool isSingleEdge() const;
56 };
57
58 template <> struct DenseMapInfo<BasicBlockEdge> {
59   static unsigned getHashValue(const BasicBlockEdge *V);
60   typedef DenseMapInfo<const BasicBlock *> BBInfo;
61   static inline BasicBlockEdge getEmptyKey() {
62     return BasicBlockEdge(BBInfo::getEmptyKey(), BBInfo::getEmptyKey());
63   }
64   static inline BasicBlockEdge getTombstoneKey() {
65     return BasicBlockEdge(BBInfo::getTombstoneKey(), BBInfo::getTombstoneKey());
66   }
67
68   static unsigned getHashValue(const BasicBlockEdge &Edge) {
69     return hash_combine(BBInfo::getHashValue(Edge.getStart()),
70                         BBInfo::getHashValue(Edge.getEnd()));
71   }
72   static bool isEqual(const BasicBlockEdge &LHS, const BasicBlockEdge &RHS) {
73     return BBInfo::isEqual(LHS.getStart(), RHS.getStart()) &&
74            BBInfo::isEqual(LHS.getEnd(), RHS.getEnd());
75   }
76 };
77
78 /// \brief Concrete subclass of DominatorTreeBase that is used to compute a
79 /// normal dominator tree.
80 ///
81 /// Definition: A block is said to be forward statically reachable if there is
82 /// a path from the entry of the function to the block.  A statically reachable
83 /// block may become statically unreachable during optimization.
84 ///
85 /// A forward unreachable block may appear in the dominator tree, or it may
86 /// not.  If it does, dominance queries will return results as if all reachable
87 /// blocks dominate it.  When asking for a Node corresponding to a potentially
88 /// unreachable block, calling code must handle the case where the block was
89 /// unreachable and the result of getNode() is nullptr.
90 ///
91 /// Generally, a block known to be unreachable when the dominator tree is
92 /// constructed will not be in the tree.  One which becomes unreachable after
93 /// the dominator tree is initially constructed may still exist in the tree,
94 /// even if the tree is properly updated. Calling code should not rely on the
95 /// preceding statements; this is stated only to assist human understanding.
96 class DominatorTree : public DominatorTreeBase<BasicBlock> {
97 public:
98   typedef DominatorTreeBase<BasicBlock> Base;
99
100   DominatorTree() : DominatorTreeBase<BasicBlock>(false) {}
101   explicit DominatorTree(Function &F) : DominatorTreeBase<BasicBlock>(false) {
102     recalculate(F);
103   }
104
105   /// \brief Returns *false* if the other dominator tree matches this dominator
106   /// tree.
107   inline bool compare(const DominatorTree &Other) const {
108     const DomTreeNode *R = getRootNode();
109     const DomTreeNode *OtherR = Other.getRootNode();
110
111     if (!R || !OtherR || R->getBlock() != OtherR->getBlock())
112       return true;
113
114     if (Base::compare(Other))
115       return true;
116
117     return false;
118   }
119
120   // Ensure base-class overloads are visible.
121   using Base::dominates;
122
123   /// \brief Return true if Def dominates a use in User.
124   ///
125   /// This performs the special checks necessary if Def and User are in the same
126   /// basic block. Note that Def doesn't dominate a use in Def itself!
127   bool dominates(const Instruction *Def, const Use &U) const;
128   bool dominates(const Instruction *Def, const Instruction *User) const;
129   bool dominates(const Instruction *Def, const BasicBlock *BB) const;
130   bool dominates(const BasicBlockEdge &BBE, const Use &U) const;
131   bool dominates(const BasicBlockEdge &BBE, const BasicBlock *BB) const;
132
133   // Ensure base class overloads are visible.
134   using Base::isReachableFromEntry;
135
136   /// \brief Provide an overload for a Use.
137   bool isReachableFromEntry(const Use &U) const;
138
139   /// \brief Verify the correctness of the domtree by re-computing it.
140   ///
141   /// This should only be used for debugging as it aborts the program if the
142   /// verification fails.
143   void verifyDomTree() const;
144 };
145
146 //===-------------------------------------
147 // DominatorTree GraphTraits specializations so the DominatorTree can be
148 // iterable by generic graph iterators.
149
150 template <class Node, class ChildIterator> struct DomTreeGraphTraitsBase {
151   typedef Node *NodeRef;
152   typedef ChildIterator ChildIteratorType;
153   typedef df_iterator<Node *, df_iterator_default_set<Node*>> nodes_iterator;
154
155   static NodeRef getEntryNode(NodeRef N) { return N; }
156   static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
157   static ChildIteratorType child_end(NodeRef N) { return N->end(); }
158
159   static nodes_iterator nodes_begin(NodeRef N) {
160     return df_begin(getEntryNode(N));
161   }
162
163   static nodes_iterator nodes_end(NodeRef N) { return df_end(getEntryNode(N)); }
164 };
165
166 template <>
167 struct GraphTraits<DomTreeNode *>
168     : public DomTreeGraphTraitsBase<DomTreeNode, DomTreeNode::iterator> {};
169
170 template <>
171 struct GraphTraits<const DomTreeNode *>
172     : public DomTreeGraphTraitsBase<const DomTreeNode,
173                                     DomTreeNode::const_iterator> {};
174
175 template <> struct GraphTraits<DominatorTree*>
176   : public GraphTraits<DomTreeNode*> {
177   static NodeRef getEntryNode(DominatorTree *DT) { return DT->getRootNode(); }
178
179   static nodes_iterator nodes_begin(DominatorTree *N) {
180     return df_begin(getEntryNode(N));
181   }
182
183   static nodes_iterator nodes_end(DominatorTree *N) {
184     return df_end(getEntryNode(N));
185   }
186 };
187
188 /// \brief Analysis pass which computes a \c DominatorTree.
189 class DominatorTreeAnalysis : public AnalysisInfoMixin<DominatorTreeAnalysis> {
190   friend AnalysisInfoMixin<DominatorTreeAnalysis>;
191   static AnalysisKey Key;
192
193 public:
194   /// \brief Provide the result typedef for this analysis pass.
195   typedef DominatorTree Result;
196
197   /// \brief Run the analysis pass over a function and produce a dominator tree.
198   DominatorTree run(Function &F, FunctionAnalysisManager &);
199 };
200
201 /// \brief Printer pass for the \c DominatorTree.
202 class DominatorTreePrinterPass
203     : public PassInfoMixin<DominatorTreePrinterPass> {
204   raw_ostream &OS;
205
206 public:
207   explicit DominatorTreePrinterPass(raw_ostream &OS);
208   PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
209 };
210
211 /// \brief Verifier pass for the \c DominatorTree.
212 struct DominatorTreeVerifierPass : PassInfoMixin<DominatorTreeVerifierPass> {
213   PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
214 };
215
216 /// \brief Legacy analysis pass which computes a \c DominatorTree.
217 class DominatorTreeWrapperPass : public FunctionPass {
218   DominatorTree DT;
219
220 public:
221   static char ID;
222
223   DominatorTreeWrapperPass() : FunctionPass(ID) {
224     initializeDominatorTreeWrapperPassPass(*PassRegistry::getPassRegistry());
225   }
226
227   DominatorTree &getDomTree() { return DT; }
228   const DominatorTree &getDomTree() const { return DT; }
229
230   bool runOnFunction(Function &F) override;
231
232   void verifyAnalysis() const override;
233
234   void getAnalysisUsage(AnalysisUsage &AU) const override {
235     AU.setPreservesAll();
236   }
237
238   void releaseMemory() override { DT.releaseMemory(); }
239
240   void print(raw_ostream &OS, const Module *M = nullptr) const override;
241 };
242
243 } // End llvm namespace
244
245 #endif