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 <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 {
115 return End;
116 }
117
118 /// Check if this is the only edge between Start and End.
119 LLVM_ABI bool isSingleEdge() const;
120};
121
124
125 LLVM_ABI static unsigned getHashValue(const BasicBlockEdge *V);
126
127 static inline BasicBlockEdge getEmptyKey() {
128 return BasicBlockEdge(BBInfo::getEmptyKey(), BBInfo::getEmptyKey());
129 }
130
132 return BasicBlockEdge(BBInfo::getTombstoneKey(), BBInfo::getTombstoneKey());
133 }
134
135 static unsigned getHashValue(const BasicBlockEdge &Edge) {
136 return hash_combine(BBInfo::getHashValue(Edge.getStart()),
137 BBInfo::getHashValue(Edge.getEnd()));
138 }
139
140 static bool isEqual(const BasicBlockEdge &LHS, const BasicBlockEdge &RHS) {
141 return BBInfo::isEqual(LHS.getStart(), RHS.getStart()) &&
142 BBInfo::isEqual(LHS.getEnd(), RHS.getEnd());
143 }
144};
145
146/// Concrete subclass of DominatorTreeBase that is used to compute a
147/// normal dominator tree.
148///
149/// Definition: A block is said to be forward statically reachable if there is
150/// a path from the entry of the function to the block. A statically reachable
151/// block may become statically unreachable during optimization.
152///
153/// A forward unreachable block may appear in the dominator tree, or it may
154/// not. If it does, dominance queries will return results as if all reachable
155/// blocks dominate it. When asking for a Node corresponding to a potentially
156/// unreachable block, calling code must handle the case where the block was
157/// unreachable and the result of getNode() is nullptr.
158///
159/// Generally, a block known to be unreachable when the dominator tree is
160/// constructed will not be in the tree. One which becomes unreachable after
161/// the dominator tree is initially constructed may still exist in the tree,
162/// even if the tree is properly updated. Calling code should not rely on the
163/// preceding statements; this is stated only to assist human understanding.
165 public:
167
168 DominatorTree() = default;
173
174 /// Handle invalidation explicitly.
175 LLVM_ABI bool invalidate(Function &F, const PreservedAnalyses &PA,
176 FunctionAnalysisManager::Invalidator &);
177
178 // Ensure base-class overloads are visible.
179 using Base::dominates;
180
181 /// Return true if the (end of the) basic block BB dominates the use U.
182 LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const;
183
184 /// Return true if value Def dominates use U, in the sense that Def is
185 /// available at U, and could be substituted as the used value without
186 /// violating the SSA dominance requirement.
187 ///
188 /// In particular, it is worth noting that:
189 /// * Non-instruction Defs dominate everything.
190 /// * Def does not dominate a use in Def itself (outside of degenerate cases
191 /// like unreachable code or trivial phi cycles).
192 /// * Invoke Defs only dominate uses in their default destination.
193 LLVM_ABI bool dominates(const Value *Def, const Use &U) const;
194
195 /// Return true if value Def dominates all possible uses inside instruction
196 /// User. Same comments as for the Use-based API apply.
197 LLVM_ABI bool dominates(const Value *Def, const Instruction *User) const;
198 bool dominates(const Value *Def, BasicBlock::iterator User) const {
199 return dominates(Def, &*User);
200 }
201
202 /// Returns true if Def would dominate a use in any instruction in BB.
203 /// If Def is an instruction in BB, then Def does not dominate BB.
204 ///
205 /// Does not accept Value to avoid ambiguity with dominance checks between
206 /// two basic blocks.
207 LLVM_ABI bool dominates(const Instruction *Def, const BasicBlock *BB) const;
208
209 /// Return true if an edge dominates a use.
210 ///
211 /// If BBE is not a unique edge between start and end of the edge, it can
212 /// never dominate the use.
213 LLVM_ABI bool dominates(const BasicBlockEdge &BBE, const Use &U) const;
214 LLVM_ABI bool dominates(const BasicBlockEdge &BBE,
215 const BasicBlock *BB) const;
216 /// Returns true if edge \p BBE1 dominates edge \p BBE2.
217 LLVM_ABI bool dominates(const BasicBlockEdge &BBE1,
218 const BasicBlockEdge &BBE2) const;
219
220 // Ensure base class overloads are visible.
221 using Base::isReachableFromEntry;
222
223 /// Provide an overload for a Use.
224 LLVM_ABI bool isReachableFromEntry(const Use &U) const;
225
226 // Ensure base class overloads are visible.
227 using Base::findNearestCommonDominator;
228
229 /// Find the nearest instruction I that dominates both I1 and I2, in the sense
230 /// that a result produced before I will be available at both I1 and I2.
231 LLVM_ABI Instruction *findNearestCommonDominator(Instruction *I1,
232 Instruction *I2) const;
233
234 // Pop up a GraphViz/gv window with the Dominator Tree rendered using `dot`.
235 LLVM_ABI void viewGraph(const Twine &Name, const Twine &Title);
236 LLVM_ABI void viewGraph();
237};
238
239//===-------------------------------------
240// DominatorTree GraphTraits specializations so the DominatorTree can be
241// iterable by generic graph iterators.
242
243template <class Node, class ChildIterator> struct DomTreeGraphTraitsBase {
244 using NodeRef = Node *;
245 using ChildIteratorType = ChildIterator;
247
248 static NodeRef getEntryNode(NodeRef N) { return N; }
249 static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
250 static ChildIteratorType child_end(NodeRef N) { return N->end(); }
251
255
257};
258
259template <>
263
264template <>
268
269template <> struct GraphTraits<DominatorTree*>
271 static NodeRef getEntryNode(DominatorTree *DT) { return DT->getRootNode(); }
272
276
280};
281
282/// Analysis pass which computes a \c DominatorTree.
283class DominatorTreeAnalysis : public AnalysisInfoMixin<DominatorTreeAnalysis> {
285 LLVM_ABI static AnalysisKey Key;
286
287public:
288 /// Provide the result typedef for this analysis pass.
290
291 /// Run the analysis pass over a function and produce a dominator tree.
293};
294
295/// Printer pass for the \c DominatorTree.
297 : public PassInfoMixin<DominatorTreePrinterPass> {
298 raw_ostream &OS;
299
300public:
302
304
305 static bool isRequired() { return true; }
306};
307
308/// Verifier pass for the \c DominatorTree.
309struct DominatorTreeVerifierPass : PassInfoMixin<DominatorTreeVerifierPass> {
311 static bool isRequired() { return true; }
312};
313
314/// Enables verification of dominator trees.
315///
316/// This check is expensive and is disabled by default. `-verify-dom-info`
317/// allows selectively enabling the check without needing to recompile.
318LLVM_ABI extern bool VerifyDomInfo;
319
320/// Legacy analysis pass which computes a \c DominatorTree.
322 DominatorTree DT;
323
324public:
325 static char ID;
326
328
329 DominatorTree &getDomTree() { return DT; }
330 const DominatorTree &getDomTree() const { return DT; }
331
332 bool runOnFunction(Function &F) override;
333
334 void verifyAnalysis() const override;
335
336 void getAnalysisUsage(AnalysisUsage &AU) const override {
337 AU.setPreservesAll();
338 }
339
340 void releaseMemory() override { DT.reset(); }
341
342 void print(raw_ostream &OS, const Module *M = nullptr) const override;
343};
344} // end namespace llvm
345
346#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:41
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.
typename SmallVector< DomTreeNodeBase *, 4 >::const_iterator const_iterator
Analysis pass which computes a DominatorTree.
Definition Dominators.h:283
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:289
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:329
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition Dominators.h:336
const DominatorTree & getDomTree() const
Definition Dominators.h:330
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:340
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:164
bool dominates(const Value *Def, BasicBlock::iterator User) const
Definition Dominators.h:198
DominatorTree()=default
DominatorTreeBase< BasicBlock, false > Base
Definition Dominators.h:166
DominatorTree(Function &F)
Definition Dominators.h:169
DominatorTree(DominatorTree &DT, DomTreeBuilder::BBUpdates U)
Definition Dominators.h:170
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.
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:92
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:127
static BasicBlockEdge getTombstoneKey()
Definition Dominators.h:131
DenseMapInfo< const BasicBlock * > BBInfo
Definition Dominators.h:123
static unsigned getHashValue(const BasicBlockEdge &Edge)
Definition Dominators.h:135
static LLVM_ABI unsigned getHashValue(const BasicBlockEdge *V)
static bool isEqual(const BasicBlockEdge &LHS, const BasicBlockEdge &RHS)
Definition Dominators.h:140
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:250
static NodeRef getEntryNode(NodeRef N)
Definition Dominators.h:248
ChildIterator ChildIteratorType
Definition Dominators.h:245
df_iterator< Node *, df_iterator_default_set< Node * > > nodes_iterator
Definition Dominators.h:246
static nodes_iterator nodes_begin(NodeRef N)
Definition Dominators.h:252
static nodes_iterator nodes_end(NodeRef N)
Definition Dominators.h:256
static ChildIteratorType child_begin(NodeRef N)
Definition Dominators.h:249
Verifier pass for the DominatorTree.
Definition Dominators.h:309
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
static nodes_iterator nodes_end(DominatorTree *N)
Definition Dominators.h:277
static NodeRef getEntryNode(DominatorTree *DT)
Definition Dominators.h:271
static nodes_iterator nodes_begin(DominatorTree *N)
Definition Dominators.h:273
typename DominatorTree *::UnknownGraphTypeError NodeRef
Definition GraphTraits.h:95
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition PassManager.h:69