LLVM 23.0.0git
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/APInt.h"
18#include "llvm/ADT/ArrayRef.h"
21#include "llvm/ADT/Hashing.h"
24#include "llvm/ADT/Twine.h"
26#include "llvm/IR/BasicBlock.h"
27#include "llvm/IR/CFG.h"
28#include "llvm/IR/PassManager.h"
29#include "llvm/IR/Use.h"
30#include "llvm/Pass.h"
35#include <utility>
36
37namespace llvm {
38
39class Function;
40class Instruction;
41class Module;
42class Value;
43class raw_ostream;
44template <class GraphType> struct GraphTraits;
45
47extern template class LLVM_TEMPLATE_ABI
49extern template class LLVM_TEMPLATE_ABI
51
52extern template class cfg::Update<BasicBlock *>;
53
54namespace DomTreeBuilder {
57
59
62
64extern template LLVM_TEMPLATE_ABI void
66
67extern template LLVM_TEMPLATE_ABI void
69
70extern template LLVM_TEMPLATE_ABI void
72extern template LLVM_TEMPLATE_ABI void
74
75extern template LLVM_TEMPLATE_ABI void
77extern template LLVM_TEMPLATE_ABI void
79
80extern template LLVM_TEMPLATE_ABI void
83extern template LLVM_TEMPLATE_ABI void
86
87extern template LLVM_TEMPLATE_ABI bool
89extern template LLVM_TEMPLATE_ABI bool
92} // namespace DomTreeBuilder
93
95
97 const BasicBlock *Start;
98 const BasicBlock *End;
99
100public:
101 BasicBlockEdge(const BasicBlock *Start_, const BasicBlock *End_) :
102 Start(Start_), End(End_) {}
103
104 BasicBlockEdge(const std::pair<BasicBlock *, BasicBlock *> &Pair)
105 : Start(Pair.first), End(Pair.second) {}
106
107 BasicBlockEdge(const std::pair<const BasicBlock *, const BasicBlock *> &Pair)
108 : Start(Pair.first), End(Pair.second) {}
109
110 const BasicBlock *getStart() const {
111 return Start;
112 }
113
114 const BasicBlock *getEnd() const { return End; }
115};
116
119
120 LLVM_ABI static unsigned getHashValue(const BasicBlockEdge *V);
121
122 static inline BasicBlockEdge getEmptyKey() {
123 return BasicBlockEdge(BBInfo::getEmptyKey(), BBInfo::getEmptyKey());
124 }
125
127 return BasicBlockEdge(BBInfo::getTombstoneKey(), BBInfo::getTombstoneKey());
128 }
129
130 static unsigned getHashValue(const BasicBlockEdge &Edge) {
131 return hash_combine(BBInfo::getHashValue(Edge.getStart()),
132 BBInfo::getHashValue(Edge.getEnd()));
133 }
134
135 static bool isEqual(const BasicBlockEdge &LHS, const BasicBlockEdge &RHS) {
136 return BBInfo::isEqual(LHS.getStart(), RHS.getStart()) &&
137 BBInfo::isEqual(LHS.getEnd(), RHS.getEnd());
138 }
139};
140
141/// Concrete subclass of DominatorTreeBase that is used to compute a
142/// normal dominator tree.
143///
144/// Definition: A block is said to be forward statically reachable if there is
145/// a path from the entry of the function to the block. A statically reachable
146/// block may become statically unreachable during optimization.
147///
148/// A forward unreachable block may appear in the dominator tree, or it may
149/// not. If it does, dominance queries will return results as if all reachable
150/// blocks dominate it. When asking for a Node corresponding to a potentially
151/// unreachable block, calling code must handle the case where the block was
152/// unreachable and the result of getNode() is nullptr.
153///
154/// Generally, a block known to be unreachable when the dominator tree is
155/// constructed will not be in the tree. One which becomes unreachable after
156/// the dominator tree is initially constructed may still exist in the tree,
157/// even if the tree is properly updated. Calling code should not rely on the
158/// preceding statements; this is stated only to assist human understanding.
160 public:
162
163 DominatorTree() = default;
168
169 /// Handle invalidation explicitly.
170 LLVM_ABI bool invalidate(Function &F, const PreservedAnalyses &PA,
171 FunctionAnalysisManager::Invalidator &);
172
173 // Ensure base-class overloads are visible.
174 using Base::dominates;
175
176 /// Return true if the (end of the) basic block BB dominates the use U.
177 LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const;
178
179 /// Return true if value Def dominates use U, in the sense that Def is
180 /// available at U, and could be substituted as the used value without
181 /// violating the SSA dominance requirement.
182 ///
183 /// In particular, it is worth noting that:
184 /// * Non-instruction Defs dominate everything.
185 /// * Def does not dominate a use in Def itself (outside of degenerate cases
186 /// like unreachable code or trivial phi cycles).
187 /// * Invoke Defs only dominate uses in their default destination.
188 LLVM_ABI bool dominates(const Value *Def, const Use &U) const;
189
190 /// Return true if value Def dominates all possible uses inside instruction
191 /// User. Same comments as for the Use-based API apply.
192 LLVM_ABI bool dominates(const Value *Def, const Instruction *User) const;
193 bool dominates(const Value *Def, BasicBlock::iterator User) const {
194 return dominates(Def, &*User);
195 }
196
197 /// Returns true if Def would dominate a use in any instruction in BB.
198 /// If Def is an instruction in BB, then Def does not dominate BB.
199 ///
200 /// Does not accept Value to avoid ambiguity with dominance checks between
201 /// two basic blocks.
202 LLVM_ABI bool dominates(const Instruction *Def, const BasicBlock *BB) const;
203
204 /// Return true if an edge dominates a use.
205 ///
206 /// If BBE is not a unique edge between start and end of the edge, it can
207 /// never dominate the use.
208 LLVM_ABI bool dominates(const BasicBlockEdge &BBE, const Use &U) const;
209 LLVM_ABI bool dominates(const BasicBlockEdge &BBE,
210 const BasicBlock *BB) const;
211 /// Returns true if edge \p BBE1 dominates edge \p BBE2.
212 LLVM_ABI bool dominates(const BasicBlockEdge &BBE1,
213 const BasicBlockEdge &BBE2) const;
214
215 // Ensure base class overloads are visible.
216 using Base::isReachableFromEntry;
217
218 /// Provide an overload for a Use.
219 LLVM_ABI bool isReachableFromEntry(const Use &U) const;
220
221 // Ensure base class overloads are visible.
222 using Base::findNearestCommonDominator;
223
224 /// Find the nearest instruction I that dominates both I1 and I2, in the sense
225 /// that a result produced before I will be available at both I1 and I2.
226 LLVM_ABI Instruction *findNearestCommonDominator(Instruction *I1,
227 Instruction *I2) const;
228
229 // Pop up a GraphViz/gv window with the Dominator Tree rendered using `dot`.
230 LLVM_ABI void viewGraph(const Twine &Name, const Twine &Title);
231 LLVM_ABI void viewGraph();
232};
233
234//===-------------------------------------
235// DominatorTree GraphTraits specializations so the DominatorTree can be
236// iterable by generic graph iterators.
237
238template <class Node, class ChildIterator> struct DomTreeGraphTraitsBase {
239 using NodeRef = Node *;
240 using ChildIteratorType = ChildIterator;
242
243 static NodeRef getEntryNode(NodeRef N) { return N; }
244 static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
245 static ChildIteratorType child_end(NodeRef N) { return N->end(); }
246
250
252};
253
254template <>
258
259template <>
261 : public DomTreeGraphTraitsBase<const DomTreeNode,
262 DomTreeNode::const_iterator> {};
263
264template <> struct GraphTraits<DominatorTree*>
266 static NodeRef getEntryNode(DominatorTree *DT) { return DT->getRootNode(); }
267
271
275};
276
277/// Analysis pass which computes a \c DominatorTree.
278class DominatorTreeAnalysis : public AnalysisInfoMixin<DominatorTreeAnalysis> {
280 LLVM_ABI static AnalysisKey Key;
281
282public:
283 /// Provide the result typedef for this analysis pass.
285
286 /// Run the analysis pass over a function and produce a dominator tree.
288};
289
290/// Printer pass for the \c DominatorTree.
292 : public PassInfoMixin<DominatorTreePrinterPass> {
293 raw_ostream &OS;
294
295public:
297
299
300 static bool isRequired() { return true; }
301};
302
303/// Verifier pass for the \c DominatorTree.
304struct DominatorTreeVerifierPass : PassInfoMixin<DominatorTreeVerifierPass> {
306 static bool isRequired() { return true; }
307};
308
309/// Enables verification of dominator trees.
310///
311/// This check is expensive and is disabled by default. `-verify-dom-info`
312/// allows selectively enabling the check without needing to recompile.
313LLVM_ABI extern bool VerifyDomInfo;
314
315/// Legacy analysis pass which computes a \c DominatorTree.
317 DominatorTree DT;
318
319public:
320 static char ID;
321
323
324 DominatorTree &getDomTree() { return DT; }
325 const DominatorTree &getDomTree() const { return DT; }
326
327 bool runOnFunction(Function &F) override;
328
329 void verifyAnalysis() const override;
330
331 void getAnalysisUsage(AnalysisUsage &AU) const override {
332 AU.setPreservesAll();
333 }
334
335 void releaseMemory() override { DT.reset(); }
336
337 void print(raw_ostream &OS, const Module *M = nullptr) const override;
338};
339} // end namespace llvm
340
341#endif // LLVM_IR_DOMINATORS_H
aarch64 promote const
This file implements a class to represent arbitrary precision integral constant values and operations...
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
#define LLVM_ABI
Definition Compiler.h:213
#define LLVM_TEMPLATE_ABI
Definition Compiler.h:214
This file defines DenseMapInfo traits for DenseMap.
This file builds on the ADT/GraphTraits.h file to build generic depth first graph iterator.
This file defines a set of templates that efficiently compute a dominator tree over a generic graph.
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
This header defines various interfaces for pass management in LLVM.
This defines the Use class.
#define F(x, y, z)
Definition MD5.cpp:54
This file defines the PointerIntPair class.
static bool dominates(InstrPosIndexes &PosIndexes, const MachineInstr &A, const MachineInstr &B)
This file defines the SmallVector class.
Value * RHS
Value * LHS
Represent the analysis usage information of a pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
const BasicBlock * getEnd() const
Definition Dominators.h:114
const BasicBlock * getStart() const
Definition Dominators.h:110
BasicBlockEdge(const std::pair< const BasicBlock *, const BasicBlock * > &Pair)
Definition Dominators.h:107
BasicBlockEdge(const BasicBlock *Start_, const BasicBlock *End_)
Definition Dominators.h:101
BasicBlockEdge(const std::pair< BasicBlock *, BasicBlock * > &Pair)
Definition Dominators.h:104
LLVM Basic Block Representation.
Definition BasicBlock.h:62
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
Base class for the actual dominator tree node.
Analysis pass which computes a DominatorTree.
Definition Dominators.h:278
LLVM_ABI DominatorTree run(Function &F, FunctionAnalysisManager &)
Run the analysis pass over a function and produce a dominator tree.
DominatorTree Result
Provide the result typedef for this analysis pass.
Definition Dominators.h:284
Core dominator tree base class.
DomTreeNodeBase< NodeT > * getRootNode()
getRootNode - This returns the entry node for the CFG of the function.
LLVM_ABI DominatorTreePrinterPass(raw_ostream &OS)
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
DominatorTree & getDomTree()
Definition Dominators.h:324
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition Dominators.h:331
const DominatorTree & getDomTree() const
Definition Dominators.h:325
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:335
void verifyAnalysis() const override
verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis infor...
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:159
bool dominates(const Value *Def, BasicBlock::iterator User) const
Definition Dominators.h:193
DominatorTree()=default
DominatorTreeBase< BasicBlock, false > Base
Definition Dominators.h:161
DominatorTree(Function &F)
Definition Dominators.h:164
DominatorTree(DominatorTree &DT, DomTreeBuilder::BBUpdates U)
Definition Dominators.h:165
FunctionPass(char &pid)
Definition Pass.h:316
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
LLVM Value Representation.
Definition Value.h:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
bool Verify(const DomTreeT &DT, typename DomTreeT::VerificationLevel VL)
GraphDiff< BasicBlock *, false > BBDomTreeGraphDiff
Definition Dominators.h:60
void CalculateWithUpdates(DomTreeT &DT, ArrayRef< typename DomTreeT::UpdateType > Updates)
ArrayRef< llvm::cfg::Update< BasicBlock * > > BBUpdates
Definition Dominators.h:58
GraphDiff< BasicBlock *, true > BBPostDomTreeGraphDiff
Definition Dominators.h:61
void DeleteEdge(DomTreeT &DT, typename DomTreeT::NodePtr From, typename DomTreeT::NodePtr To)
PostDomTreeBase< BasicBlock > BBPostDomTree
Definition Dominators.h:56
void ApplyUpdates(DomTreeT &DT, GraphDiff< typename DomTreeT::NodePtr, DomTreeT::IsPostDominator > &PreViewCFG, GraphDiff< typename DomTreeT::NodePtr, DomTreeT::IsPostDominator > *PostViewCFG)
void InsertEdge(DomTreeT &DT, typename DomTreeT::NodePtr From, typename DomTreeT::NodePtr To)
DomTreeBase< BasicBlock > BBDomTree
Definition Dominators.h:55
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
df_iterator< T > df_begin(const T &G)
DominatorTreeBase< T, true > PostDomTreeBase
DomTreeNodeBase< BasicBlock > DomTreeNode
Definition Dominators.h:94
DominatorTreeBase< T, false > DomTreeBase
LLVM_ABI bool VerifyDomInfo
Enables verification of dominator trees.
df_iterator< T > df_end(const T &G)
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition Hashing.h:592
#define N
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition PassManager.h:93
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29
static BasicBlockEdge getEmptyKey()
Definition Dominators.h:122
static BasicBlockEdge getTombstoneKey()
Definition Dominators.h:126
DenseMapInfo< const BasicBlock * > BBInfo
Definition Dominators.h:118
static unsigned getHashValue(const BasicBlockEdge &Edge)
Definition Dominators.h:130
static LLVM_ABI unsigned getHashValue(const BasicBlockEdge *V)
static bool isEqual(const BasicBlockEdge &LHS, const BasicBlockEdge &RHS)
Definition Dominators.h:135
An information struct used to provide DenseMap with the various necessary components for a given valu...
static ChildIteratorType child_end(NodeRef N)
Definition Dominators.h:245
static NodeRef getEntryNode(NodeRef N)
Definition Dominators.h:243
ChildIterator ChildIteratorType
Definition Dominators.h:240
df_iterator< Node *, df_iterator_default_set< Node * > > nodes_iterator
Definition Dominators.h:241
static nodes_iterator nodes_begin(NodeRef N)
Definition Dominators.h:247
static nodes_iterator nodes_end(NodeRef N)
Definition Dominators.h:251
static ChildIteratorType child_begin(NodeRef N)
Definition Dominators.h:244
Verifier pass for the DominatorTree.
Definition Dominators.h:304
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
static nodes_iterator nodes_end(DominatorTree *N)
Definition Dominators.h:272
static NodeRef getEntryNode(DominatorTree *DT)
Definition Dominators.h:266
static nodes_iterator nodes_begin(DominatorTree *N)
Definition Dominators.h:268
typename DominatorTree *::UnknownGraphTypeError NodeRef
Definition GraphTraits.h:95
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition PassManager.h:70