LLVM  10.0.0svn
Dominators.h
Go to the documentation of this file.
1 //===- Dominators.h - Dominator Info Calculation ----------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the DominatorTree class, which provides fast and efficient
10 // dominance queries.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_IR_DOMINATORS_H
15 #define LLVM_IR_DOMINATORS_H
16 
17 #include "llvm/ADT/DenseMapInfo.h"
19 #include "llvm/ADT/GraphTraits.h"
20 #include "llvm/ADT/Hashing.h"
21 #include "llvm/IR/BasicBlock.h"
22 #include "llvm/IR/CFG.h"
23 #include "llvm/IR/PassManager.h"
24 #include "llvm/Pass.h"
26 #include <utility>
27 
28 namespace llvm {
29 
30 class Function;
31 class Instruction;
32 class Module;
33 class raw_ostream;
34 
35 extern template class DomTreeNodeBase<BasicBlock>;
36 extern template class DominatorTreeBase<BasicBlock, false>; // DomTree
37 extern template class DominatorTreeBase<BasicBlock, true>; // PostDomTree
38 
39 extern template class cfg::Update<BasicBlock *>;
40 
41 namespace DomTreeBuilder {
44 
46 
47 extern template void Calculate<BBDomTree>(BBDomTree &DT);
48 extern template void CalculateWithUpdates<BBDomTree>(BBDomTree &DT,
49  BBUpdates U);
50 
51 extern template void Calculate<BBPostDomTree>(BBPostDomTree &DT);
52 
53 extern template void InsertEdge<BBDomTree>(BBDomTree &DT, BasicBlock *From,
54  BasicBlock *To);
55 extern template void InsertEdge<BBPostDomTree>(BBPostDomTree &DT,
57  BasicBlock *To);
58 
59 extern template void DeleteEdge<BBDomTree>(BBDomTree &DT, BasicBlock *From,
60  BasicBlock *To);
61 extern template void DeleteEdge<BBPostDomTree>(BBPostDomTree &DT,
63  BasicBlock *To);
64 
65 extern template void ApplyUpdates<BBDomTree>(BBDomTree &DT, BBUpdates);
66 extern template void ApplyUpdates<BBPostDomTree>(BBPostDomTree &DT, BBUpdates);
67 
68 extern template bool Verify<BBDomTree>(const BBDomTree &DT,
70 extern template bool Verify<BBPostDomTree>(const BBPostDomTree &DT,
72 } // namespace DomTreeBuilder
73 
75 
77  const BasicBlock *Start;
78  const BasicBlock *End;
79 
80 public:
81  BasicBlockEdge(const BasicBlock *Start_, const BasicBlock *End_) :
82  Start(Start_), End(End_) {}
83 
84  BasicBlockEdge(const std::pair<BasicBlock *, BasicBlock *> &Pair)
85  : Start(Pair.first), End(Pair.second) {}
86 
87  BasicBlockEdge(const std::pair<const BasicBlock *, const BasicBlock *> &Pair)
88  : Start(Pair.first), End(Pair.second) {}
89 
90  const BasicBlock *getStart() const {
91  return Start;
92  }
93 
94  const BasicBlock *getEnd() const {
95  return End;
96  }
97 
98  /// Check if this is the only edge between Start and End.
99  bool isSingleEdge() const;
100 };
101 
102 template <> struct DenseMapInfo<BasicBlockEdge> {
104 
105  static unsigned getHashValue(const BasicBlockEdge *V);
106 
107  static inline BasicBlockEdge getEmptyKey() {
108  return BasicBlockEdge(BBInfo::getEmptyKey(), BBInfo::getEmptyKey());
109  }
110 
111  static inline BasicBlockEdge getTombstoneKey() {
112  return BasicBlockEdge(BBInfo::getTombstoneKey(), BBInfo::getTombstoneKey());
113  }
114 
115  static unsigned getHashValue(const BasicBlockEdge &Edge) {
116  return hash_combine(BBInfo::getHashValue(Edge.getStart()),
117  BBInfo::getHashValue(Edge.getEnd()));
118  }
119 
120  static bool isEqual(const BasicBlockEdge &LHS, const BasicBlockEdge &RHS) {
121  return BBInfo::isEqual(LHS.getStart(), RHS.getStart()) &&
122  BBInfo::isEqual(LHS.getEnd(), RHS.getEnd());
123  }
124 };
125 
126 /// Concrete subclass of DominatorTreeBase that is used to compute a
127 /// normal dominator tree.
128 ///
129 /// Definition: A block is said to be forward statically reachable if there is
130 /// a path from the entry of the function to the block. A statically reachable
131 /// block may become statically unreachable during optimization.
132 ///
133 /// A forward unreachable block may appear in the dominator tree, or it may
134 /// not. If it does, dominance queries will return results as if all reachable
135 /// blocks dominate it. When asking for a Node corresponding to a potentially
136 /// unreachable block, calling code must handle the case where the block was
137 /// unreachable and the result of getNode() is nullptr.
138 ///
139 /// Generally, a block known to be unreachable when the dominator tree is
140 /// constructed will not be in the tree. One which becomes unreachable after
141 /// the dominator tree is initially constructed may still exist in the tree,
142 /// even if the tree is properly updated. Calling code should not rely on the
143 /// preceding statements; this is stated only to assist human understanding.
145  public:
147 
148  DominatorTree() = default;
149  explicit DominatorTree(Function &F) { recalculate(F); }
151  recalculate(*DT.Parent, U);
152  }
153 
154  /// Handle invalidation explicitly.
155  bool invalidate(Function &F, const PreservedAnalyses &PA,
157 
158  // Ensure base-class overloads are visible.
159  using Base::dominates;
160 
161  /// Return true if Def dominates a use in User.
162  ///
163  /// This performs the special checks necessary if Def and User are in the same
164  /// basic block. Note that Def doesn't dominate a use in Def itself!
165  bool dominates(const Instruction *Def, const Use &U) const;
166  bool dominates(const Instruction *Def, const Instruction *User) const;
167  bool dominates(const Instruction *Def, const BasicBlock *BB) const;
168 
169  /// Return true if an edge dominates a use.
170  ///
171  /// If BBE is not a unique edge between start and end of the edge, it can
172  /// never dominate the use.
173  bool dominates(const BasicBlockEdge &BBE, const Use &U) const;
174  bool dominates(const BasicBlockEdge &BBE, const BasicBlock *BB) const;
175 
176  // Ensure base class overloads are visible.
177  using Base::isReachableFromEntry;
178 
179  /// Provide an overload for a Use.
180  bool isReachableFromEntry(const Use &U) const;
181 
182  // Pop up a GraphViz/gv window with the Dominator Tree rendered using `dot`.
183  void viewGraph(const Twine &Name, const Twine &Title);
184  void viewGraph();
185 };
186 
187 //===-------------------------------------
188 // DominatorTree GraphTraits specializations so the DominatorTree can be
189 // iterable by generic graph iterators.
190 
191 template <class Node, class ChildIterator> struct DomTreeGraphTraitsBase {
192  using NodeRef = Node *;
193  using ChildIteratorType = ChildIterator;
195 
196  static NodeRef getEntryNode(NodeRef N) { return N; }
197  static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
198  static ChildIteratorType child_end(NodeRef N) { return N->end(); }
199 
201  return df_begin(getEntryNode(N));
202  }
203 
204  static nodes_iterator nodes_end(NodeRef N) { return df_end(getEntryNode(N)); }
205 };
206 
207 template <>
210 
211 template <>
213  : public DomTreeGraphTraitsBase<const DomTreeNode,
215 
216 template <> struct GraphTraits<DominatorTree*>
217  : public GraphTraits<DomTreeNode*> {
218  static NodeRef getEntryNode(DominatorTree *DT) { return DT->getRootNode(); }
219 
220  static nodes_iterator nodes_begin(DominatorTree *N) {
221  return df_begin(getEntryNode(N));
222  }
223 
224  static nodes_iterator nodes_end(DominatorTree *N) {
225  return df_end(getEntryNode(N));
226  }
227 };
228 
229 /// Analysis pass which computes a \c DominatorTree.
230 class DominatorTreeAnalysis : public AnalysisInfoMixin<DominatorTreeAnalysis> {
232  static AnalysisKey Key;
233 
234 public:
235  /// Provide the result typedef for this analysis pass.
237 
238  /// Run the analysis pass over a function and produce a dominator tree.
240 };
241 
242 /// Printer pass for the \c DominatorTree.
244  : public PassInfoMixin<DominatorTreePrinterPass> {
245  raw_ostream &OS;
246 
247 public:
249 
251 };
252 
253 /// Verifier pass for the \c DominatorTree.
254 struct DominatorTreeVerifierPass : PassInfoMixin<DominatorTreeVerifierPass> {
256 };
257 
258 /// Legacy analysis pass which computes a \c DominatorTree.
260  DominatorTree DT;
261 
262 public:
263  static char ID;
264 
267  }
268 
269  DominatorTree &getDomTree() { return DT; }
270  const DominatorTree &getDomTree() const { return DT; }
271 
272  bool runOnFunction(Function &F) override;
273 
274  void verifyAnalysis() const override;
275 
276  void getAnalysisUsage(AnalysisUsage &AU) const override {
277  AU.setPreservesAll();
278  }
279 
280  void releaseMemory() override { DT.releaseMemory(); }
281 
282  void print(raw_ostream &OS, const Module *M = nullptr) const override;
283 };
284 } // end namespace llvm
285 
286 #endif // LLVM_IR_DOMINATORS_H
typename std::vector< DomTreeNodeBase *>::const_iterator const_iterator
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This class represents lattice values for constants.
Definition: AllocatorList.h:23
DominatorTree(Function &F)
Definition: Dominators.h:149
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:66
static bool isEqual(const BasicBlockEdge &LHS, const BasicBlockEdge &RHS)
Definition: Dominators.h:120
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
template void DeleteEdge< BBPostDomTree >(BBPostDomTree &DT, BasicBlock *From, BasicBlock *To)
static NodeRef getEntryNode(NodeRef N)
Definition: Dominators.h:196
template void ApplyUpdates< BBDomTree >(BBDomTree &DT, BBUpdates)
template void CalculateWithUpdates< BBDomTree >(BBDomTree &DT, BBUpdates U)
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Dominators.h:276
unsigned second
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:230
F(f)
const BasicBlock * getEnd() const
Definition: Dominators.h:94
static nodes_iterator nodes_begin(NodeRef N)
Definition: Dominators.h:200
static nodes_iterator nodes_begin(DominatorTree *N)
Definition: Dominators.h:220
void initializeDominatorTreeWrapperPassPass(PassRegistry &)
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
BasicBlockEdge(const std::pair< BasicBlock *, BasicBlock *> &Pair)
Definition: Dominators.h:84
static BasicBlockEdge getEmptyKey()
Definition: Dominators.h:107
A Use represents the edge between a Value definition and its users.
Definition: Use.h:55
DominatorTree & getDomTree()
Definition: Dominators.h:269
template void ApplyUpdates< BBPostDomTree >(BBPostDomTree &DT, BBUpdates)
static bool isEqual(const Function &Caller, const Function &Callee)
static nodes_iterator nodes_end(DominatorTree *N)
Definition: Dominators.h:224
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:373
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:144
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:280
static nodes_iterator nodes_end(NodeRef N)
Definition: Dominators.h:204
Core dominator tree base class.
Definition: LoopInfo.h:66
static bool runOnFunction(Function &F, bool PostInlining)
typename DomTreeNode *::UnknownGraphTypeError NodeRef
Definition: GraphTraits.h:78
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:154
template bool Verify< BBDomTree >(const BBDomTree &DT, BBDomTree::VerificationLevel VL)
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
DominatorTree run(Function &F, FunctionAnalysisManager &)
Run the analysis pass over a function and produce a dominator tree.
Definition: Dominators.cpp:323
df_iterator< T > df_end(const T &G)
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:390
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:284
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
Printer pass for the DominatorTree.
Definition: Dominators.h:243
ArrayRef< llvm::cfg::Update< BasicBlock * > > BBUpdates
Definition: Dominators.h:45
template void Calculate< BBDomTree >(BBDomTree &DT)
DomTreeNodeBase< NodeT > * getRootNode()
getRootNode - This returns the entry node for the CFG of the function.
Verifier pass for the DominatorTree.
Definition: Dominators.h:254
unsigned first
static ChildIteratorType child_begin(NodeRef N)
Definition: Dominators.h:197
static ChildIteratorType child_end(NodeRef N)
Definition: Dominators.h:198
BlockVerifier::State From
DominatorTree(DominatorTree &DT, DomTreeBuilder::BBUpdates U)
Definition: Dominators.h:150
template void InsertEdge< BBPostDomTree >(BBPostDomTree &DT, BasicBlock *From, BasicBlock *To)
const DominatorTree & getDomTree() const
Definition: Dominators.h:270
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:599
void setPreservesAll()
Set by analyses that do not transform their input at all.
template void DeleteEdge< BBDomTree >(BBDomTree &DT, BasicBlock *From, BasicBlock *To)
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
#define N
static unsigned getHashValue(const BasicBlockEdge &Edge)
Definition: Dominators.h:115
static NodeRef getEntryNode(DominatorTree *DT)
Definition: Dominators.h:218
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:649
template bool Verify< BBPostDomTree >(const BBPostDomTree &DT, BBPostDomTree::VerificationLevel VL)
BasicBlockEdge(const BasicBlock *Start_, const BasicBlock *End_)
Definition: Dominators.h:81
const BasicBlock * getStart() const
Definition: Dominators.h:90
aarch64 promote const
DomTreeNodeBase< BasicBlock > DomTreeNode
Definition: Dominators.h:74
template void Calculate< BBPostDomTree >(BBPostDomTree &DT)
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
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:259
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:71
BasicBlockEdge(const std::pair< const BasicBlock *, const BasicBlock *> &Pair)
Definition: Dominators.h:87
template void InsertEdge< BBDomTree >(BBDomTree &DT, BasicBlock *From, BasicBlock *To)
static BasicBlockEdge getTombstoneKey()
Definition: Dominators.h:111