LLVM 24.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
63} // namespace DomTreeBuilder
64
66
68 const BasicBlock *Start;
69 const BasicBlock *End;
70
71public:
72 BasicBlockEdge(const BasicBlock *Start_, const BasicBlock *End_) :
73 Start(Start_), End(End_) {}
74
75 BasicBlockEdge(const std::pair<BasicBlock *, BasicBlock *> &Pair)
76 : Start(Pair.first), End(Pair.second) {}
77
78 BasicBlockEdge(const std::pair<const BasicBlock *, const BasicBlock *> &Pair)
79 : Start(Pair.first), End(Pair.second) {}
80
81 const BasicBlock *getStart() const {
82 return Start;
83 }
84
85 const BasicBlock *getEnd() const { return End; }
86};
87
88template <> struct DenseMapInfo<BasicBlockEdge> {
90
91 LLVM_ABI static unsigned getHashValue(const BasicBlockEdge *V);
92
93 static unsigned getHashValue(const BasicBlockEdge &Edge) {
94 return hash_combine(BBInfo::getHashValue(Edge.getStart()),
95 BBInfo::getHashValue(Edge.getEnd()));
96 }
97
98 static bool isEqual(const BasicBlockEdge &LHS, const BasicBlockEdge &RHS) {
99 return BBInfo::isEqual(LHS.getStart(), RHS.getStart()) &&
100 BBInfo::isEqual(LHS.getEnd(), RHS.getEnd());
101 }
102};
103
104/// Concrete subclass of DominatorTreeBase that is used to compute a
105/// normal dominator tree.
106///
107/// Definition: A block is said to be forward statically reachable if there is
108/// a path from the entry of the function to the block. A statically reachable
109/// block may become statically unreachable during optimization.
110///
111/// A forward unreachable block may appear in the dominator tree, or it may
112/// not. If it does, dominance queries will return results as if all reachable
113/// blocks dominate it. When asking for a Node corresponding to a potentially
114/// unreachable block, calling code must handle the case where the block was
115/// unreachable and the result of getNode() is nullptr.
116///
117/// Generally, a block known to be unreachable when the dominator tree is
118/// constructed will not be in the tree. One which becomes unreachable after
119/// the dominator tree is initially constructed may still exist in the tree,
120/// even if the tree is properly updated. Calling code should not rely on the
121/// preceding statements; this is stated only to assist human understanding.
122class DominatorTree : public DominatorTreeBase<BasicBlock, false> {
123 public:
125
126 DominatorTree() = default;
131
132 /// Handle invalidation explicitly.
134 FunctionAnalysisManager::Invalidator &);
135
136 // Ensure base-class overloads are visible.
137 using Base::dominates;
138
139 /// Return true if the (end of the) basic block BB dominates the use U.
140 LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const;
141
142 /// Return true if value Def dominates use U, in the sense that Def is
143 /// available at U, and could be substituted as the used value without
144 /// violating the SSA dominance requirement.
145 ///
146 /// In particular, it is worth noting that:
147 /// * Non-instruction Defs dominate everything.
148 /// * Def does not dominate a use in Def itself (outside of degenerate cases
149 /// like unreachable code or trivial phi cycles).
150 /// * Invoke Defs only dominate uses in their default destination.
151 LLVM_ABI bool dominates(const Value *Def, const Use &U) const;
152
153 /// Return true if value Def dominates all possible uses inside instruction
154 /// User. Same comments as for the Use-based API apply.
155 LLVM_ABI bool dominates(const Value *Def, const Instruction *User) const;
156 bool dominates(const Value *Def, BasicBlock::iterator User) const {
157 return dominates(Def, &*User);
158 }
159
160 /// Returns true if Def would dominate a use in any instruction in BB.
161 /// If Def is an instruction in BB, then Def does not dominate BB.
162 ///
163 /// Does not accept Value to avoid ambiguity with dominance checks between
164 /// two basic blocks.
165 LLVM_ABI bool dominates(const Instruction *Def, const BasicBlock *BB) const;
166
167 /// Return true if an edge dominates a use.
168 ///
169 /// If BBE is not a unique edge between start and end of the edge, it can
170 /// never dominate the use.
171 LLVM_ABI bool dominates(const BasicBlockEdge &BBE, const Use &U) const;
172 LLVM_ABI bool dominates(const BasicBlockEdge &BBE,
173 const BasicBlock *BB) const;
174 /// Returns true if edge \p BBE1 dominates edge \p BBE2.
175 LLVM_ABI bool dominates(const BasicBlockEdge &BBE1,
176 const BasicBlockEdge &BBE2) const;
177
178 // Ensure base class overloads are visible.
180
181 /// Provide an overload for a Use.
182 LLVM_ABI bool isReachableFromEntry(const Use &U) const;
183
184 // Ensure base class overloads are visible.
186
187 /// Find the nearest instruction I that dominates both I1 and I2, in the sense
188 /// that a result produced before I will be available at both I1 and I2.
190 Instruction *I2) const;
191
192 // Pop up a GraphViz/gv window with the Dominator Tree rendered using `dot`.
193 LLVM_ABI void viewGraph(const Twine &Name, const Twine &Title);
194 LLVM_ABI void viewGraph();
195};
196
197//===-------------------------------------
198// DominatorTree GraphTraits specializations so the DominatorTree can be
199// iterable by generic graph iterators.
200
201template <class Node, class ChildIterator> struct DomTreeGraphTraitsBase {
202 using NodeRef = Node *;
203 using ChildIteratorType = ChildIterator;
205
206 static NodeRef getEntryNode(NodeRef N) { return N; }
207 static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
208 static ChildIteratorType child_end(NodeRef N) { return N->end(); }
209
213
215};
216
217template <>
221
222template <>
224 : public DomTreeGraphTraitsBase<const DomTreeNode,
225 DomTreeNode::const_iterator> {};
226
227template <> struct GraphTraits<DominatorTree*>
229 static NodeRef getEntryNode(DominatorTree *DT) { return DT->getRootNode(); }
230
234
238};
239
240/// Analysis pass which computes a \c DominatorTree.
241class DominatorTreeAnalysis : public AnalysisInfoMixin<DominatorTreeAnalysis> {
243 LLVM_ABI static AnalysisKey Key;
244
245public:
246 /// Provide the result typedef for this analysis pass.
248
249 /// Run the analysis pass over a function and produce a dominator tree.
251};
252
253/// Printer pass for the \c DominatorTree.
255 : public RequiredPassInfoMixin<DominatorTreePrinterPass> {
256 raw_ostream &OS;
257
258public:
260
262};
263
264/// Verifier pass for the \c DominatorTree.
269
270/// Enables verification of dominator trees.
271///
272/// This check is expensive and is disabled by default. `-verify-dom-info`
273/// allows selectively enabling the check without needing to recompile.
274LLVM_ABI extern bool VerifyDomInfo;
275
276/// Legacy analysis pass which computes a \c DominatorTree.
278 DominatorTree DT;
279
280public:
281 static char ID;
282
284
285 DominatorTree &getDomTree() { return DT; }
286 const DominatorTree &getDomTree() const { return DT; }
287
288 bool runOnFunction(Function &F) override;
289
290 void verifyAnalysis() const override;
291
292 void getAnalysisUsage(AnalysisUsage &AU) const override {
293 AU.setPreservesAll();
294 }
295
296 void releaseMemory() override { DT.reset(); }
297
298 void print(raw_ostream &OS, const Module *M = nullptr) const override;
299};
300} // end namespace llvm
301
302#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:215
#define LLVM_TEMPLATE_ABI
Definition Compiler.h:216
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.
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:85
const BasicBlock * getStart() const
Definition Dominators.h:81
BasicBlockEdge(const std::pair< const BasicBlock *, const BasicBlock * > &Pair)
Definition Dominators.h:78
BasicBlockEdge(const BasicBlock *Start_, const BasicBlock *End_)
Definition Dominators.h:72
BasicBlockEdge(const std::pair< BasicBlock *, BasicBlock * > &Pair)
Definition Dominators.h:75
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:241
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:247
Core dominator tree base class.
DomTreeNodeBase< NodeT > * getRootNode()
getRootNode - This returns the entry node for the CFG of the function.
BasicBlock * findNearestCommonDominator(BasicBlock *A, BasicBlock *B) const
bool dominates(const DomTreeNodeBase< BasicBlock > *A, const DomTreeNodeBase< BasicBlock > *B) const
bool isReachableFromEntry(const BasicBlock *A) const
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:285
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition Dominators.h:292
const DominatorTree & getDomTree() const
Definition Dominators.h:286
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:296
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:122
bool dominates(const Value *Def, BasicBlock::iterator User) const
Definition Dominators.h:156
LLVM_ABI bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
LLVM_ABI Instruction * findNearestCommonDominator(Instruction *I1, Instruction *I2) const
Find the nearest instruction I that dominates both I1 and I2, in the sense that a result produced bef...
DominatorTree()=default
LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
LLVM_ABI void viewGraph()
DominatorTreeBase< BasicBlock, false > Base
Definition Dominators.h:124
DominatorTree(Function &F)
Definition Dominators.h:127
LLVM_ABI bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &)
Handle invalidation explicitly.
DominatorTree(DominatorTree &DT, DomTreeBuilder::BBUpdates U)
Definition Dominators.h:128
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
GraphDiff< BasicBlock *, false > BBDomTreeGraphDiff
Definition Dominators.h:60
ArrayRef< llvm::cfg::Update< BasicBlock * > > BBUpdates
Definition Dominators.h:58
GraphDiff< BasicBlock *, true > BBPostDomTreeGraphDiff
Definition Dominators.h:61
PostDomTreeBase< BasicBlock > BBPostDomTree
Definition Dominators.h:56
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:65
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:305
#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:89
static unsigned getHashValue(const BasicBlockEdge &Edge)
Definition Dominators.h:93
static LLVM_ABI unsigned getHashValue(const BasicBlockEdge *V)
static bool isEqual(const BasicBlockEdge &LHS, const BasicBlockEdge &RHS)
Definition Dominators.h:98
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:208
static NodeRef getEntryNode(NodeRef N)
Definition Dominators.h:206
ChildIterator ChildIteratorType
Definition Dominators.h:203
df_iterator< Node *, df_iterator_default_set< Node * > > nodes_iterator
Definition Dominators.h:204
static nodes_iterator nodes_begin(NodeRef N)
Definition Dominators.h:210
static nodes_iterator nodes_end(NodeRef N)
Definition Dominators.h:214
static ChildIteratorType child_begin(NodeRef N)
Definition Dominators.h:207
Verifier pass for the DominatorTree.
Definition Dominators.h:266
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
static nodes_iterator nodes_end(DominatorTree *N)
Definition Dominators.h:235
static NodeRef getEntryNode(DominatorTree *DT)
Definition Dominators.h:229
static nodes_iterator nodes_begin(DominatorTree *N)
Definition Dominators.h:231
typename DominatorTree *::UnknownGraphTypeError NodeRef
Definition GraphTraits.h:95
A CRTP mix-in for passes that should not be skipped.