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