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