LLVM  8.0.1
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 extern template class cfg::Update<BasicBlock *>;
41 
42 namespace DomTreeBuilder {
45 
47 
48 extern template void Calculate<BBDomTree>(BBDomTree &DT);
49 extern template void CalculateWithUpdates<BBDomTree>(BBDomTree &DT,
50  BBUpdates U);
51 
52 extern template void Calculate<BBPostDomTree>(BBPostDomTree &DT);
53 
54 extern template void InsertEdge<BBDomTree>(BBDomTree &DT, BasicBlock *From,
55  BasicBlock *To);
56 extern template void InsertEdge<BBPostDomTree>(BBPostDomTree &DT,
58  BasicBlock *To);
59 
60 extern template void DeleteEdge<BBDomTree>(BBDomTree &DT, BasicBlock *From,
61  BasicBlock *To);
62 extern template void DeleteEdge<BBPostDomTree>(BBPostDomTree &DT,
64  BasicBlock *To);
65 
66 extern template void ApplyUpdates<BBDomTree>(BBDomTree &DT, BBUpdates);
67 extern template void ApplyUpdates<BBPostDomTree>(BBPostDomTree &DT, BBUpdates);
68 
69 extern template bool Verify<BBDomTree>(const BBDomTree &DT,
71 extern template bool Verify<BBPostDomTree>(const BBPostDomTree &DT,
73 } // namespace DomTreeBuilder
74 
76 
78  const BasicBlock *Start;
79  const BasicBlock *End;
80 
81 public:
82  BasicBlockEdge(const BasicBlock *Start_, const BasicBlock *End_) :
83  Start(Start_), End(End_) {}
84 
85  BasicBlockEdge(const std::pair<BasicBlock *, BasicBlock *> &Pair)
86  : Start(Pair.first), End(Pair.second) {}
87 
88  BasicBlockEdge(const std::pair<const BasicBlock *, const BasicBlock *> &Pair)
89  : Start(Pair.first), End(Pair.second) {}
90 
91  const BasicBlock *getStart() const {
92  return Start;
93  }
94 
95  const BasicBlock *getEnd() const {
96  return End;
97  }
98 
99  /// Check if this is the only edge between Start and End.
100  bool isSingleEdge() const;
101 };
102 
103 template <> struct DenseMapInfo<BasicBlockEdge> {
105 
106  static unsigned getHashValue(const BasicBlockEdge *V);
107 
108  static inline BasicBlockEdge getEmptyKey() {
109  return BasicBlockEdge(BBInfo::getEmptyKey(), BBInfo::getEmptyKey());
110  }
111 
112  static inline BasicBlockEdge getTombstoneKey() {
113  return BasicBlockEdge(BBInfo::getTombstoneKey(), BBInfo::getTombstoneKey());
114  }
115 
116  static unsigned getHashValue(const BasicBlockEdge &Edge) {
117  return hash_combine(BBInfo::getHashValue(Edge.getStart()),
118  BBInfo::getHashValue(Edge.getEnd()));
119  }
120 
121  static bool isEqual(const BasicBlockEdge &LHS, const BasicBlockEdge &RHS) {
122  return BBInfo::isEqual(LHS.getStart(), RHS.getStart()) &&
123  BBInfo::isEqual(LHS.getEnd(), RHS.getEnd());
124  }
125 };
126 
127 /// Concrete subclass of DominatorTreeBase that is used to compute a
128 /// normal dominator tree.
129 ///
130 /// Definition: A block is said to be forward statically reachable if there is
131 /// a path from the entry of the function to the block. A statically reachable
132 /// block may become statically unreachable during optimization.
133 ///
134 /// A forward unreachable block may appear in the dominator tree, or it may
135 /// not. If it does, dominance queries will return results as if all reachable
136 /// blocks dominate it. When asking for a Node corresponding to a potentially
137 /// unreachable block, calling code must handle the case where the block was
138 /// unreachable and the result of getNode() is nullptr.
139 ///
140 /// Generally, a block known to be unreachable when the dominator tree is
141 /// constructed will not be in the tree. One which becomes unreachable after
142 /// the dominator tree is initially constructed may still exist in the tree,
143 /// even if the tree is properly updated. Calling code should not rely on the
144 /// preceding statements; this is stated only to assist human understanding.
146  public:
148 
149  DominatorTree() = default;
150  explicit DominatorTree(Function &F) { recalculate(F); }
152  recalculate(*DT.Parent, U);
153  }
154 
155  /// Handle invalidation explicitly.
156  bool invalidate(Function &F, const PreservedAnalyses &PA,
158 
159  // Ensure base-class overloads are visible.
160  using Base::dominates;
161 
162  /// Return true if Def dominates a use in User.
163  ///
164  /// This performs the special checks necessary if Def and User are in the same
165  /// basic block. Note that Def doesn't dominate a use in Def itself!
166  bool dominates(const Instruction *Def, const Use &U) const;
167  bool dominates(const Instruction *Def, const Instruction *User) const;
168  bool dominates(const Instruction *Def, const BasicBlock *BB) const;
169 
170  /// Return true if an edge dominates a use.
171  ///
172  /// If BBE is not a unique edge between start and end of the edge, it can
173  /// never dominate the use.
174  bool dominates(const BasicBlockEdge &BBE, const Use &U) const;
175  bool dominates(const BasicBlockEdge &BBE, const BasicBlock *BB) const;
176 
177  // Ensure base class overloads are visible.
178  using Base::isReachableFromEntry;
179 
180  /// Provide an overload for a Use.
181  bool isReachableFromEntry(const Use &U) const;
182 
183  // Pop up a GraphViz/gv window with the Dominator Tree rendered using `dot`.
184  void viewGraph(const Twine &Name, const Twine &Title);
185  void viewGraph();
186 };
187 
188 //===-------------------------------------
189 // DominatorTree GraphTraits specializations so the DominatorTree can be
190 // iterable by generic graph iterators.
191 
192 template <class Node, class ChildIterator> struct DomTreeGraphTraitsBase {
193  using NodeRef = Node *;
194  using ChildIteratorType = ChildIterator;
196 
197  static NodeRef getEntryNode(NodeRef N) { return N; }
198  static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
199  static ChildIteratorType child_end(NodeRef N) { return N->end(); }
200 
202  return df_begin(getEntryNode(N));
203  }
204 
205  static nodes_iterator nodes_end(NodeRef N) { return df_end(getEntryNode(N)); }
206 };
207 
208 template <>
211 
212 template <>
214  : public DomTreeGraphTraitsBase<const DomTreeNode,
216 
217 template <> struct GraphTraits<DominatorTree*>
218  : public GraphTraits<DomTreeNode*> {
219  static NodeRef getEntryNode(DominatorTree *DT) { return DT->getRootNode(); }
220 
221  static nodes_iterator nodes_begin(DominatorTree *N) {
222  return df_begin(getEntryNode(N));
223  }
224 
225  static nodes_iterator nodes_end(DominatorTree *N) {
226  return df_end(getEntryNode(N));
227  }
228 };
229 
230 /// Analysis pass which computes a \c DominatorTree.
231 class DominatorTreeAnalysis : public AnalysisInfoMixin<DominatorTreeAnalysis> {
233  static AnalysisKey Key;
234 
235 public:
236  /// Provide the result typedef for this analysis pass.
238 
239  /// Run the analysis pass over a function and produce a dominator tree.
241 };
242 
243 /// Printer pass for the \c DominatorTree.
245  : public PassInfoMixin<DominatorTreePrinterPass> {
246  raw_ostream &OS;
247 
248 public:
250 
252 };
253 
254 /// Verifier pass for the \c DominatorTree.
255 struct DominatorTreeVerifierPass : PassInfoMixin<DominatorTreeVerifierPass> {
257 };
258 
259 /// Legacy analysis pass which computes a \c DominatorTree.
261  DominatorTree DT;
262 
263 public:
264  static char ID;
265 
268  }
269 
270  DominatorTree &getDomTree() { return DT; }
271  const DominatorTree &getDomTree() const { return DT; }
272 
273  bool runOnFunction(Function &F) override;
274 
275  void verifyAnalysis() const override;
276 
277  void getAnalysisUsage(AnalysisUsage &AU) const override {
278  AU.setPreservesAll();
279  }
280 
281  void releaseMemory() override { DT.releaseMemory(); }
282 
283  void print(raw_ostream &OS, const Module *M = nullptr) const override;
284 };
285 } // end namespace llvm
286 
287 #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:24
DominatorTree(Function &F)
Definition: Dominators.h:150
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
static bool isEqual(const BasicBlockEdge &LHS, const BasicBlockEdge &RHS)
Definition: Dominators.h:121
template void DeleteEdge< BBPostDomTree >(BBPostDomTree &DT, BasicBlock *From, BasicBlock *To)
static NodeRef getEntryNode(NodeRef N)
Definition: Dominators.h:197
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:277
unsigned second
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:231
F(f)
const BasicBlock * getEnd() const
Definition: Dominators.h:95
static nodes_iterator nodes_begin(NodeRef N)
Definition: Dominators.h:201
static nodes_iterator nodes_begin(DominatorTree *N)
Definition: Dominators.h:221
void initializeDominatorTreeWrapperPassPass(PassRegistry &)
amdgpu Simplify well known AMD library false Value Value const Twine & Name
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
BasicBlockEdge(const std::pair< BasicBlock *, BasicBlock *> &Pair)
Definition: Dominators.h:85
static BasicBlockEdge getEmptyKey()
Definition: Dominators.h:108
A Use represents the edge between a Value definition and its users.
Definition: Use.h:56
DominatorTree & getDomTree()
Definition: Dominators.h:270
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:225
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:366
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:145
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:281
static nodes_iterator nodes_end(NodeRef N)
Definition: Dominators.h:205
Core dominator tree base class.
Definition: LoopInfo.h:61
static bool runOnFunction(Function &F, bool PostInlining)
typename DomTreeNode *::UnknownGraphTypeError NodeRef
Definition: GraphTraits.h:79
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:58
DominatorTree run(Function &F, FunctionAnalysisManager &)
Run the analysis pass over a function and produce a dominator tree.
Definition: Dominators.cpp:324
df_iterator< T > df_end(const T &G)
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:383
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:285
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
Printer pass for the DominatorTree.
Definition: Dominators.h:244
ArrayRef< llvm::cfg::Update< BasicBlock * > > BBUpdates
Definition: Dominators.h:46
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:255
unsigned first
static ChildIteratorType child_begin(NodeRef N)
Definition: Dominators.h:198
static ChildIteratorType child_end(NodeRef N)
Definition: Dominators.h:199
BlockVerifier::State From
DominatorTree(DominatorTree &DT, DomTreeBuilder::BBUpdates U)
Definition: Dominators.h:151
template void InsertEdge< BBPostDomTree >(BBPostDomTree &DT, BasicBlock *From, BasicBlock *To)
const DominatorTree & getDomTree() const
Definition: Dominators.h:271
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:601
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:116
static NodeRef getEntryNode(DominatorTree *DT)
Definition: Dominators.h:219
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:642
template bool Verify< BBPostDomTree >(const BBPostDomTree &DT, BBPostDomTree::VerificationLevel VL)
BasicBlockEdge(const BasicBlock *Start_, const BasicBlock *End_)
Definition: Dominators.h:82
const BasicBlock * getStart() const
Definition: Dominators.h:91
aarch64 promote const
DomTreeNodeBase< BasicBlock > DomTreeNode
Definition: Dominators.h:75
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:46
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:260
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:88
template void InsertEdge< BBDomTree >(BBDomTree &DT, BasicBlock *From, BasicBlock *To)
static BasicBlockEdge getTombstoneKey()
Definition: Dominators.h:112