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