LLVM  5.0.0
Dominators.h
Go to the documentation of this file.
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"
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"
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, false>; // DomTree
38 extern template class DominatorTreeBase<BasicBlock, true>; // PostDomTree
39 
40 namespace DomTreeBuilder {
43 
44 extern template void Calculate<BBDomTree, Function>(BBDomTree &DT, Function &F);
45 extern template void Calculate<BBPostDomTree, Function>(BBPostDomTree &DT,
46  Function &F);
47 
48 extern template void InsertEdge<BBDomTree>(BBDomTree &DT, BasicBlock *From,
49  BasicBlock *To);
50 extern template void InsertEdge<BBPostDomTree>(BBPostDomTree &DT,
51  BasicBlock *From,
52  BasicBlock *To);
53 
54 extern template void DeleteEdge<BBDomTree>(BBDomTree &DT, BasicBlock *From,
55  BasicBlock *To);
56 extern template void DeleteEdge<BBPostDomTree>(BBPostDomTree &DT,
57  BasicBlock *From,
58  BasicBlock *To);
59 
60 extern template bool Verify<BBDomTree>(const BBDomTree &DT);
61 extern template bool Verify<BBPostDomTree>(const BBPostDomTree &DT);
62 } // namespace DomTreeBuilder
63 
65 
67  const BasicBlock *Start;
68  const BasicBlock *End;
69 
70 public:
71  BasicBlockEdge(const BasicBlock *Start_, const BasicBlock *End_) :
72  Start(Start_), End(End_) {}
73 
74  BasicBlockEdge(const std::pair<BasicBlock *, BasicBlock *> &Pair)
75  : Start(Pair.first), End(Pair.second) {}
76 
77  BasicBlockEdge(const std::pair<const BasicBlock *, const BasicBlock *> &Pair)
78  : Start(Pair.first), End(Pair.second) {}
79 
80  const BasicBlock *getStart() const {
81  return Start;
82  }
83 
84  const BasicBlock *getEnd() const {
85  return End;
86  }
87 
88  /// Check if this is the only edge between Start and End.
89  bool isSingleEdge() const;
90 };
91 
92 template <> struct DenseMapInfo<BasicBlockEdge> {
94 
95  static unsigned getHashValue(const BasicBlockEdge *V);
96 
97  static inline BasicBlockEdge getEmptyKey() {
98  return BasicBlockEdge(BBInfo::getEmptyKey(), BBInfo::getEmptyKey());
99  }
100 
101  static inline BasicBlockEdge getTombstoneKey() {
102  return BasicBlockEdge(BBInfo::getTombstoneKey(), BBInfo::getTombstoneKey());
103  }
104 
105  static unsigned getHashValue(const BasicBlockEdge &Edge) {
106  return hash_combine(BBInfo::getHashValue(Edge.getStart()),
107  BBInfo::getHashValue(Edge.getEnd()));
108  }
109 
110  static bool isEqual(const BasicBlockEdge &LHS, const BasicBlockEdge &RHS) {
111  return BBInfo::isEqual(LHS.getStart(), RHS.getStart()) &&
112  BBInfo::isEqual(LHS.getEnd(), RHS.getEnd());
113  }
114 };
115 
116 /// \brief Concrete subclass of DominatorTreeBase that is used to compute a
117 /// normal dominator tree.
118 ///
119 /// Definition: A block is said to be forward statically reachable if there is
120 /// a path from the entry of the function to the block. A statically reachable
121 /// block may become statically unreachable during optimization.
122 ///
123 /// A forward unreachable block may appear in the dominator tree, or it may
124 /// not. If it does, dominance queries will return results as if all reachable
125 /// blocks dominate it. When asking for a Node corresponding to a potentially
126 /// unreachable block, calling code must handle the case where the block was
127 /// unreachable and the result of getNode() is nullptr.
128 ///
129 /// Generally, a block known to be unreachable when the dominator tree is
130 /// constructed will not be in the tree. One which becomes unreachable after
131 /// the dominator tree is initially constructed may still exist in the tree,
132 /// even if the tree is properly updated. Calling code should not rely on the
133 /// preceding statements; this is stated only to assist human understanding.
135  public:
137 
138  DominatorTree() = default;
139  explicit DominatorTree(Function &F) { recalculate(F); }
140 
141  /// Handle invalidation explicitly.
142  bool invalidate(Function &F, const PreservedAnalyses &PA,
144 
145  /// \brief Returns *false* if the other dominator tree matches this dominator
146  /// tree.
147  inline bool compare(const DominatorTree &Other) const {
148  const DomTreeNode *R = getRootNode();
149  const DomTreeNode *OtherR = Other.getRootNode();
150  return !R || !OtherR || R->getBlock() != OtherR->getBlock() ||
151  Base::compare(Other);
152  }
153 
154  // Ensure base-class overloads are visible.
155  using Base::dominates;
156 
157  /// \brief Return true if Def dominates a use in User.
158  ///
159  /// This performs the special checks necessary if Def and User are in the same
160  /// basic block. Note that Def doesn't dominate a use in Def itself!
161  bool dominates(const Instruction *Def, const Use &U) const;
162  bool dominates(const Instruction *Def, const Instruction *User) const;
163  bool dominates(const Instruction *Def, const BasicBlock *BB) const;
164 
165  /// Return true if an edge dominates a use.
166  ///
167  /// If BBE is not a unique edge between start and end of the edge, it can
168  /// never dominate the use.
169  bool dominates(const BasicBlockEdge &BBE, const Use &U) const;
170  bool dominates(const BasicBlockEdge &BBE, const BasicBlock *BB) const;
171 
172  // Ensure base class overloads are visible.
173  using Base::isReachableFromEntry;
174 
175  /// \brief Provide an overload for a Use.
176  bool isReachableFromEntry(const Use &U) const;
177 
178  /// \brief Verify the correctness of the domtree by re-computing it.
179  ///
180  /// This should only be used for debugging as it aborts the program if the
181  /// verification fails.
182  void verifyDomTree() const;
183 
184  // Pop up a GraphViz/gv window with the Dominator Tree rendered using `dot`.
185  void viewGraph(const Twine &Name, const Twine &Title);
186  void viewGraph();
187 };
188 
189 //===-------------------------------------
190 // DominatorTree GraphTraits specializations so the DominatorTree can be
191 // iterable by generic graph iterators.
192 
193 template <class Node, class ChildIterator> struct DomTreeGraphTraitsBase {
194  using NodeRef = Node *;
195  using ChildIteratorType = ChildIterator;
197 
198  static NodeRef getEntryNode(NodeRef N) { return N; }
199  static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
200  static ChildIteratorType child_end(NodeRef N) { return N->end(); }
201 
203  return df_begin(getEntryNode(N));
204  }
205 
206  static nodes_iterator nodes_end(NodeRef N) { return df_end(getEntryNode(N)); }
207 };
208 
209 template <>
212 
213 template <>
215  : public DomTreeGraphTraitsBase<const DomTreeNode,
217 
218 template <> struct GraphTraits<DominatorTree*>
219  : public GraphTraits<DomTreeNode*> {
220  static NodeRef getEntryNode(DominatorTree *DT) { return DT->getRootNode(); }
221 
223  return df_begin(getEntryNode(N));
224  }
225 
227  return df_end(getEntryNode(N));
228  }
229 };
230 
231 /// \brief Analysis pass which computes a \c DominatorTree.
232 class DominatorTreeAnalysis : public AnalysisInfoMixin<DominatorTreeAnalysis> {
234  static AnalysisKey Key;
235 
236 public:
237  /// \brief Provide the result typedef for this analysis pass.
239 
240  /// \brief Run the analysis pass over a function and produce a dominator tree.
242 };
243 
244 /// \brief Printer pass for the \c DominatorTree.
246  : public PassInfoMixin<DominatorTreePrinterPass> {
247  raw_ostream &OS;
248 
249 public:
251 
253 };
254 
255 /// \brief Verifier pass for the \c DominatorTree.
256 struct DominatorTreeVerifierPass : PassInfoMixin<DominatorTreeVerifierPass> {
258 };
259 
260 /// \brief Legacy analysis pass which computes a \c DominatorTree.
262  DominatorTree DT;
263 
264 public:
265  static char ID;
266 
269  }
270 
271  DominatorTree &getDomTree() { return DT; }
272  const DominatorTree &getDomTree() const { return DT; }
273 
274  bool runOnFunction(Function &F) override;
275 
276  void verifyAnalysis() const override;
277 
278  void getAnalysisUsage(AnalysisUsage &AU) const override {
279  AU.setPreservesAll();
280  }
281 
282  void releaseMemory() override { DT.releaseMemory(); }
283 
284  void print(raw_ostream &OS, const Module *M = nullptr) const override;
285 };
286 
287 } // end namespace llvm
288 
289 #endif // LLVM_IR_DOMINATORS_H
bool compare(const DominatorTree &Other) const
Returns false if the other dominator tree matches this dominator tree.
Definition: Dominators.h:147
template void Calculate< BBPostDomTree, Function >(BBPostDomTree &DT, Function &F)
typename std::vector< DomTreeNodeBase * >::const_iterator const_iterator
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
DominatorTree(Function &F)
Definition: Dominators.h:139
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
static bool isEqual(const BasicBlockEdge &LHS, const BasicBlockEdge &RHS)
Definition: Dominators.h:110
template void DeleteEdge< BBPostDomTree >(BBPostDomTree &DT, BasicBlock *From, BasicBlock *To)
static NodeRef getEntryNode(NodeRef N)
Definition: Dominators.h:198
const BasicBlock * getStart() const
Definition: Dominators.h:80
template bool Verify< BBDomTree >(const BBDomTree &DT)
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Dominators.h:278
unsigned second
BasicBlockEdge(const std::pair< BasicBlock *, BasicBlock * > &Pair)
Definition: Dominators.h:74
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:232
const DominatorTree & getDomTree() const
Definition: Dominators.h:272
static nodes_iterator nodes_begin(NodeRef N)
Definition: Dominators.h:202
static nodes_iterator nodes_begin(DominatorTree *N)
Definition: Dominators.h:222
BasicBlockEdge(const std::pair< const BasicBlock *, const BasicBlock * > &Pair)
Definition: Dominators.h:77
void initializeDominatorTreeWrapperPassPass(PassRegistry &)
template void Calculate< BBDomTree, Function >(BBDomTree &DT, Function &F)
void verifyAnalysis() const override
verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis infor...
Definition: Dominators.cpp:374
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass...
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
static BasicBlockEdge getEmptyKey()
Definition: Dominators.h:97
A Use represents the edge between a Value definition and its users.
Definition: Use.h:56
DominatorTree & getDomTree()
Definition: Dominators.h:271
ELFYAML::ELF_STO Other
Definition: ELFYAML.cpp:663
#define F(x, y, z)
Definition: MD5.cpp:55
static bool isEqual(const Function &Caller, const Function &Callee)
static nodes_iterator nodes_end(DominatorTree *N)
Definition: Dominators.h:226
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:365
Base class for the actual dominator tree node.
DominatorTreePrinterPass(raw_ostream &OS)
Definition: Dominators.cpp:339
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:134
NodeT * getBlock() const
const char * Name
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
Definition: Dominators.h:282
static nodes_iterator nodes_end(NodeRef N)
Definition: Dominators.h:206
Core dominator tree base class.
Definition: LoopInfo.h:60
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:153
LLVM Basic Block Representation.
Definition: BasicBlock.h:59
DominatorTree run(Function &F, FunctionAnalysisManager &)
Run the analysis pass over a function and produce a dominator tree.
Definition: Dominators.cpp:330
df_iterator< T > df_end(const T &G)
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:382
Represent the analysis usage information of a pass.
static const unsigned End
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
Printer pass for the DominatorTree.
Definition: Dominators.h:245
Verifier pass for the DominatorTree.
Definition: Dominators.h:256
const BasicBlock * getEnd() const
Definition: Dominators.h:84
unsigned first
static ChildIteratorType child_begin(NodeRef N)
Definition: Dominators.h:199
static ChildIteratorType child_end(NodeRef N)
Definition: Dominators.h:200
template void InsertEdge< BBPostDomTree >(BBPostDomTree &DT, BasicBlock *From, BasicBlock *To)
const size_t N
df_iterator< T > df_begin(const T &G)
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition: Hashing.h:602
void setPreservesAll()
Set by analyses that do not transform their input at all.
void print(raw_ostream &OS, const Module *M=nullptr) const override
print - Print out the internal state of the pass.
Definition: Dominators.cpp:379
template void DeleteEdge< BBDomTree >(BBDomTree &DT, BasicBlock *From, BasicBlock *To)
static unsigned getHashValue(const BasicBlockEdge &Edge)
Definition: Dominators.h:105
static NodeRef getEntryNode(DominatorTree *DT)
Definition: Dominators.h:220
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:559
int compare(DigitsT LDigits, int16_t LScale, DigitsT RDigits, int16_t RScale)
Compare two scaled numbers.
Definition: ScaledNumber.h:252
BasicBlockEdge(const BasicBlock *Start_, const BasicBlock *End_)
Definition: Dominators.h:71
aarch64 promote const
DomTreeNodeBase< BasicBlock > DomTreeNode
Definition: Dominators.h:64
DomTreeNodeBase< NodeT > * getRootNode()
getRootNode - This returns the entry node for the CFG of the function.
template bool Verify< BBPostDomTree >(const BBPostDomTree &DT)
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: Dominators.cpp:341
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
This file defines a set of templates that efficiently compute a dominator tree over a generic graph...
A container for analyses that lazily runs them and caches their results.
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:261
This header defines various interfaces for pass management in LLVM.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:70
template void InsertEdge< BBDomTree >(BBDomTree &DT, BasicBlock *From, BasicBlock *To)
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: Dominators.cpp:349
static BasicBlockEdge getTombstoneKey()
Definition: Dominators.h:101