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