LLVM 22.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 <algorithm>
36#include <utility>
37
38namespace llvm {
39
40class Function;
41class Instruction;
42class Module;
43class Value;
44class raw_ostream;
45template <class GraphType> struct GraphTraits;
46
48extern template class LLVM_TEMPLATE_ABI
50extern template class LLVM_TEMPLATE_ABI
52
53extern template class cfg::Update<BasicBlock *>;
54
55namespace DomTreeBuilder {
58
60
63
64extern template LLVM_TEMPLATE_ABI void Calculate<BBDomTree>(BBDomTree &DT);
65extern template LLVM_TEMPLATE_ABI void
66CalculateWithUpdates<BBDomTree>(BBDomTree &DT, BBUpdates U);
67
68extern template LLVM_TEMPLATE_ABI void
69Calculate<BBPostDomTree>(BBPostDomTree &DT);
70
71extern template LLVM_TEMPLATE_ABI void
72InsertEdge<BBDomTree>(BBDomTree &DT, BasicBlock *From, BasicBlock *To);
73extern template LLVM_TEMPLATE_ABI void
74InsertEdge<BBPostDomTree>(BBPostDomTree &DT, BasicBlock *From, BasicBlock *To);
75
76extern template LLVM_TEMPLATE_ABI void
77DeleteEdge<BBDomTree>(BBDomTree &DT, BasicBlock *From, BasicBlock *To);
78extern template LLVM_TEMPLATE_ABI void
79DeleteEdge<BBPostDomTree>(BBPostDomTree &DT, BasicBlock *From, BasicBlock *To);
80
81extern template LLVM_TEMPLATE_ABI void
82ApplyUpdates<BBDomTree>(BBDomTree &DT, BBDomTreeGraphDiff &,
84extern template LLVM_TEMPLATE_ABI void
85ApplyUpdates<BBPostDomTree>(BBPostDomTree &DT, BBPostDomTreeGraphDiff &,
87
88extern template LLVM_TEMPLATE_ABI bool
89Verify<BBDomTree>(const BBDomTree &DT, BBDomTree::VerificationLevel VL);
90extern template LLVM_TEMPLATE_ABI bool
91Verify<BBPostDomTree>(const BBPostDomTree &DT,
93} // namespace DomTreeBuilder
94
96
98 const BasicBlock *Start;
99 const BasicBlock *End;
100
101public:
102 BasicBlockEdge(const BasicBlock *Start_, const BasicBlock *End_) :
103 Start(Start_), End(End_) {}
104
105 BasicBlockEdge(const std::pair<BasicBlock *, BasicBlock *> &Pair)
106 : Start(Pair.first), End(Pair.second) {}
107
108 BasicBlockEdge(const std::pair<const BasicBlock *, const BasicBlock *> &Pair)
109 : Start(Pair.first), End(Pair.second) {}
110
111 const BasicBlock *getStart() const {
112 return Start;
113 }
114
115 const BasicBlock *getEnd() const {
116 return End;
117 }
118
119 /// Check if this is the only edge between Start and End.
120 LLVM_ABI bool isSingleEdge() const;
121};
122
125
126 LLVM_ABI static unsigned getHashValue(const BasicBlockEdge *V);
127
128 static inline BasicBlockEdge getEmptyKey() {
129 return BasicBlockEdge(BBInfo::getEmptyKey(), BBInfo::getEmptyKey());
130 }
131
133 return BasicBlockEdge(BBInfo::getTombstoneKey(), BBInfo::getTombstoneKey());
134 }
135
136 static unsigned getHashValue(const BasicBlockEdge &Edge) {
137 return hash_combine(BBInfo::getHashValue(Edge.getStart()),
138 BBInfo::getHashValue(Edge.getEnd()));
139 }
140
141 static bool isEqual(const BasicBlockEdge &LHS, const BasicBlockEdge &RHS) {
142 return BBInfo::isEqual(LHS.getStart(), RHS.getStart()) &&
143 BBInfo::isEqual(LHS.getEnd(), RHS.getEnd());
144 }
145};
146
147/// Concrete subclass of DominatorTreeBase that is used to compute a
148/// normal dominator tree.
149///
150/// Definition: A block is said to be forward statically reachable if there is
151/// a path from the entry of the function to the block. A statically reachable
152/// block may become statically unreachable during optimization.
153///
154/// A forward unreachable block may appear in the dominator tree, or it may
155/// not. If it does, dominance queries will return results as if all reachable
156/// blocks dominate it. When asking for a Node corresponding to a potentially
157/// unreachable block, calling code must handle the case where the block was
158/// unreachable and the result of getNode() is nullptr.
159///
160/// Generally, a block known to be unreachable when the dominator tree is
161/// constructed will not be in the tree. One which becomes unreachable after
162/// the dominator tree is initially constructed may still exist in the tree,
163/// even if the tree is properly updated. Calling code should not rely on the
164/// preceding statements; this is stated only to assist human understanding.
166 public:
168
169 DominatorTree() = default;
170 explicit DominatorTree(Function &F) { recalculate(F); }
172 recalculate(*DT.Parent, U);
173 }
174
175 /// Handle invalidation explicitly.
176 LLVM_ABI bool invalidate(Function &F, const PreservedAnalyses &PA,
178
179 // Ensure base-class overloads are visible.
180 using Base::dominates;
181
182 /// Return true if the (end of the) basic block BB dominates the use U.
183 LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const;
184
185 /// Return true if value Def dominates use U, in the sense that Def is
186 /// available at U, and could be substituted as the used value without
187 /// violating the SSA dominance requirement.
188 ///
189 /// In particular, it is worth noting that:
190 /// * Non-instruction Defs dominate everything.
191 /// * Def does not dominate a use in Def itself (outside of degenerate cases
192 /// like unreachable code or trivial phi cycles).
193 /// * Invoke Defs only dominate uses in their default destination.
194 LLVM_ABI bool dominates(const Value *Def, const Use &U) const;
195
196 /// Return true if value Def dominates all possible uses inside instruction
197 /// User. Same comments as for the Use-based API apply.
198 LLVM_ABI bool dominates(const Value *Def, const Instruction *User) const;
199 bool dominates(const Value *Def, BasicBlock::iterator User) const {
200 return dominates(Def, &*User);
201 }
202
203 /// Returns true if Def would dominate a use in any instruction in BB.
204 /// If Def is an instruction in BB, then Def does not dominate BB.
205 ///
206 /// Does not accept Value to avoid ambiguity with dominance checks between
207 /// two basic blocks.
208 LLVM_ABI bool dominates(const Instruction *Def, const BasicBlock *BB) const;
209
210 /// Return true if an edge dominates a use.
211 ///
212 /// If BBE is not a unique edge between start and end of the edge, it can
213 /// never dominate the use.
214 LLVM_ABI bool dominates(const BasicBlockEdge &BBE, const Use &U) const;
215 LLVM_ABI bool dominates(const BasicBlockEdge &BBE,
216 const BasicBlock *BB) const;
217 /// Returns true if edge \p BBE1 dominates edge \p BBE2.
218 LLVM_ABI bool dominates(const BasicBlockEdge &BBE1,
219 const BasicBlockEdge &BBE2) const;
220
221 // Ensure base class overloads are visible.
222 using Base::isReachableFromEntry;
223
224 /// Provide an overload for a Use.
225 LLVM_ABI bool isReachableFromEntry(const Use &U) const;
226
227 // Ensure base class overloads are visible.
228 using Base::findNearestCommonDominator;
229
230 /// Find the nearest instruction I that dominates both I1 and I2, in the sense
231 /// that a result produced before I will be available at both I1 and I2.
232 LLVM_ABI Instruction *findNearestCommonDominator(Instruction *I1,
233 Instruction *I2) const;
234
235 // Pop up a GraphViz/gv window with the Dominator Tree rendered using `dot`.
236 LLVM_ABI void viewGraph(const Twine &Name, const Twine &Title);
237 LLVM_ABI void viewGraph();
238};
239
240//===-------------------------------------
241// DominatorTree GraphTraits specializations so the DominatorTree can be
242// iterable by generic graph iterators.
243
244template <class Node, class ChildIterator> struct DomTreeGraphTraitsBase {
245 using NodeRef = Node *;
246 using ChildIteratorType = ChildIterator;
248
249 static NodeRef getEntryNode(NodeRef N) { return N; }
250 static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
251 static ChildIteratorType child_end(NodeRef N) { return N->end(); }
252
254 return df_begin(getEntryNode(N));
255 }
256
257 static nodes_iterator nodes_end(NodeRef N) { return df_end(getEntryNode(N)); }
258};
259
260template <>
263};
264
265template <>
267 : public DomTreeGraphTraitsBase<const DomTreeNode,
269
270template <> struct GraphTraits<DominatorTree*>
272 static NodeRef getEntryNode(DominatorTree *DT) { return DT->getRootNode(); }
273
275 return df_begin(getEntryNode(N));
276 }
277
279 return df_end(getEntryNode(N));
280 }
281};
282
283/// Analysis pass which computes a \c DominatorTree.
284class DominatorTreeAnalysis : public AnalysisInfoMixin<DominatorTreeAnalysis> {
286 LLVM_ABI static AnalysisKey Key;
287
288public:
289 /// Provide the result typedef for this analysis pass.
291
292 /// Run the analysis pass over a function and produce a dominator tree.
294};
295
296/// Printer pass for the \c DominatorTree.
298 : public PassInfoMixin<DominatorTreePrinterPass> {
299 raw_ostream &OS;
300
301public:
303
305
306 static bool isRequired() { return true; }
307};
308
309/// Verifier pass for the \c DominatorTree.
310struct DominatorTreeVerifierPass : PassInfoMixin<DominatorTreeVerifierPass> {
312 static bool isRequired() { return true; }
313};
314
315/// Enables verification of dominator trees.
316///
317/// This check is expensive and is disabled by default. `-verify-dom-info`
318/// allows selectively enabling the check without needing to recompile.
319LLVM_ABI extern bool VerifyDomInfo;
320
321/// Legacy analysis pass which computes a \c DominatorTree.
323 DominatorTree DT;
324
325public:
326 static char ID;
327
329
330 DominatorTree &getDomTree() { return DT; }
331 const DominatorTree &getDomTree() const { return DT; }
332
333 bool runOnFunction(Function &F) override;
334
335 void verifyAnalysis() const override;
336
337 void getAnalysisUsage(AnalysisUsage &AU) const override {
338 AU.setPreservesAll();
339 }
340
341 void releaseMemory() override { DT.reset(); }
342
343 void print(raw_ostream &OS, const Module *M = nullptr) const override;
344};
345} // end namespace llvm
346
347#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)
BlockVerifier::State From
#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.
std::string Name
bool End
Definition: ELF_riscv.cpp:480
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:55
Machine Check Debug Module
This file defines the PointerIntPair class.
static bool dominates(InstrPosIndexes &PosIndexes, const MachineInstr &A, const MachineInstr &B)
std::pair< BasicBlock *, BasicBlock * > Edge
raw_pwrite_stream & OS
This file defines the SmallVector class.
Value * RHS
Value * LHS
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:294
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:255
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:41
const BasicBlock * getEnd() const
Definition: Dominators.h:115
const BasicBlock * getStart() const
Definition: Dominators.h:111
BasicBlockEdge(const std::pair< const BasicBlock *, const BasicBlock * > &Pair)
Definition: Dominators.h:108
BasicBlockEdge(const BasicBlock *Start_, const BasicBlock *End_)
Definition: Dominators.h:102
BasicBlockEdge(const std::pair< BasicBlock *, BasicBlock * > &Pair)
Definition: Dominators.h:105
LLVM Basic Block Representation.
Definition: BasicBlock.h:62
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:170
typename SmallVector< DomTreeNodeBase *, 4 >::const_iterator const_iterator
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:284
LLVM_ABI DominatorTree run(Function &F, FunctionAnalysisManager &)
Run the analysis pass over a function and produce a dominator tree.
Definition: Dominators.cpp:384
Core dominator tree base class.
DomTreeNodeBase< NodeT > * getRootNode()
getRootNode - This returns the entry node for the CFG of the function.
Printer pass for the DominatorTree.
Definition: Dominators.h:298
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: Dominators.cpp:395
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:322
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:330
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Dominators.h:337
const DominatorTree & getDomTree() const
Definition: Dominators.h:331
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:341
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:165
bool dominates(const Value *Def, BasicBlock::iterator User) const
Definition: Dominators.h:199
DominatorTree()=default
DominatorTree(Function &F)
Definition: Dominators.h:170
DominatorTree(DominatorTree &DT, DomTreeBuilder::BBUpdates U)
Definition: Dominators.h:171
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:314
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
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
df_iterator< T > df_begin(const T &G)
template class LLVM_TEMPLATE_ABI DomTreeNodeBase< BasicBlock >
template class LLVM_TEMPLATE_ABI DominatorTreeBase< BasicBlock, true >
DomTreeNodeBase< BasicBlock > DomTreeNode
Definition: Dominators.h:95
template class LLVM_TEMPLATE_ABI DominatorTreeBase< BasicBlock, false >
LLVM_ABI bool VerifyDomInfo
Enables verification of dominator trees.
Definition: Dominators.cpp:41
df_iterator< T > df_end(const T &G)
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition: Hashing.h:595
#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:128
static BasicBlockEdge getTombstoneKey()
Definition: Dominators.h:132
static unsigned getHashValue(const BasicBlockEdge &Edge)
Definition: Dominators.h:136
static LLVM_ABI unsigned getHashValue(const BasicBlockEdge *V)
static bool isEqual(const BasicBlockEdge &LHS, const BasicBlockEdge &RHS)
Definition: Dominators.h:141
An information struct used to provide DenseMap with the various necessary components for a given valu...
Definition: DenseMapInfo.h:54
static ChildIteratorType child_end(NodeRef N)
Definition: Dominators.h:251
static NodeRef getEntryNode(NodeRef N)
Definition: Dominators.h:249
ChildIterator ChildIteratorType
Definition: Dominators.h:246
static nodes_iterator nodes_begin(NodeRef N)
Definition: Dominators.h:253
static nodes_iterator nodes_end(NodeRef N)
Definition: Dominators.h:257
static ChildIteratorType child_begin(NodeRef N)
Definition: Dominators.h:250
Verifier pass for the DominatorTree.
Definition: Dominators.h:310
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: Dominators.cpp:403
static nodes_iterator nodes_end(DominatorTree *N)
Definition: Dominators.h:278
static NodeRef getEntryNode(DominatorTree *DT)
Definition: Dominators.h:272
static nodes_iterator nodes_begin(DominatorTree *N)
Definition: Dominators.h:274
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:70