LLVM 20.0.0git
MachineDominators.h
Go to the documentation of this file.
1//==- llvm/CodeGen/MachineDominators.h - Machine Dom 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 classes mirroring those in llvm/Analysis/Dominators.h,
10// but for target-specific code rather than target-independent IR.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CODEGEN_MACHINEDOMINATORS_H
15#define LLVM_CODEGEN_MACHINEDOMINATORS_H
16
17#include "llvm/ADT/SmallSet.h"
25#include <cassert>
26#include <memory>
27#include <optional>
28
29namespace llvm {
30class AnalysisUsage;
31class MachineFunction;
32class Module;
33class raw_ostream;
34
35template <>
38 this->Roots.push_back(MBB);
39}
40
41extern template class DomTreeNodeBase<MachineBasicBlock>;
42extern template class DominatorTreeBase<MachineBasicBlock, false>; // DomTree
43
45
46namespace DomTreeBuilder {
50
51extern template void Calculate<MBBDomTree>(MBBDomTree &DT);
52extern template void CalculateWithUpdates<MBBDomTree>(MBBDomTree &DT,
53 MBBUpdates U);
54
55extern template void InsertEdge<MBBDomTree>(MBBDomTree &DT,
58
59extern template void DeleteEdge<MBBDomTree>(MBBDomTree &DT,
62
63extern template void ApplyUpdates<MBBDomTree>(MBBDomTree &DT,
66
67extern template bool Verify<MBBDomTree>(const MBBDomTree &DT,
69} // namespace DomTreeBuilder
70
71//===-------------------------------------
72/// DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
73/// compute a normal dominator tree.
74///
76 /// Helper structure used to hold all the basic blocks
77 /// involved in the split of a critical edge.
78 struct CriticalEdge {
79 MachineBasicBlock *FromBB;
81 MachineBasicBlock *NewBB;
82 };
83
84 /// Pile up all the critical edges to be split.
85 /// The splitting of a critical edge is local and thus, it is possible
86 /// to apply several of those changes at the same time.
87 mutable SmallVector<CriticalEdge, 32> CriticalEdgesToSplit;
88
89 /// Remember all the basic blocks that are inserted during
90 /// edge splitting.
91 /// Invariant: NewBBs == all the basic blocks contained in the NewBB
92 /// field of all the elements of CriticalEdgesToSplit.
93 /// I.e., forall elt in CriticalEdgesToSplit, it exists BB in NewBBs
94 /// such as BB == elt.NewBB.
96
97 /// Apply all the recorded critical edges to the DT.
98 /// This updates the underlying DT information in a way that uses
99 /// the fast query path of DT as much as possible.
100 /// FIXME: This method should not be a const member!
101 ///
102 /// \post CriticalEdgesToSplit.empty().
103 void applySplitCriticalEdges() const;
104
105public:
107
109 explicit MachineDominatorTree(MachineFunction &MF) { calculate(MF); }
110
111 /// Handle invalidation explicitly.
112 bool invalidate(MachineFunction &, const PreservedAnalyses &PA,
114
115 // FIXME: If there is an updater for MachineDominatorTree,
116 // migrate to this updater and remove these wrappers.
117
119 applySplitCriticalEdges();
120 return *this;
121 }
122
124 applySplitCriticalEdges();
125 return Base::getRoot();
126 }
127
129 applySplitCriticalEdges();
130 return const_cast<MachineDomTreeNode *>(Base::getRootNode());
131 }
132
133 void calculate(MachineFunction &F);
134
136 const MachineDomTreeNode *B) const {
137 applySplitCriticalEdges();
138 return Base::dominates(A, B);
139 }
140
143 applySplitCriticalEdges();
144 Base::getDescendants(A, Result);
145 }
146
147 bool dominates(const MachineBasicBlock *A, const MachineBasicBlock *B) const {
148 applySplitCriticalEdges();
149 return Base::dominates(A, B);
150 }
151
152 // dominates - Return true if A dominates B. This performs the
153 // special checks necessary if A and B are in the same basic block.
154 bool dominates(const MachineInstr *A, const MachineInstr *B) const {
155 applySplitCriticalEdges();
156 const MachineBasicBlock *BBA = A->getParent(), *BBB = B->getParent();
157 if (BBA != BBB)
158 return Base::dominates(BBA, BBB);
159
160 // Loop through the basic block until we find A or B.
162 for (; &*I != A && &*I != B; ++I)
163 /*empty*/ ;
164
165 return &*I == A;
166 }
167
169 const MachineDomTreeNode *B) const {
170 applySplitCriticalEdges();
171 return Base::properlyDominates(A, B);
172 }
173
175 const MachineBasicBlock *B) const {
176 applySplitCriticalEdges();
177 return Base::properlyDominates(A, B);
178 }
179
180 /// findNearestCommonDominator - Find nearest common dominator basic block
181 /// for basic block A and B. If there is no such block then return NULL.
184 applySplitCriticalEdges();
185 return Base::findNearestCommonDominator(A, B);
186 }
187
189 applySplitCriticalEdges();
190 return Base::getNode(BB);
191 }
192
193 /// getNode - return the (Post)DominatorTree node for the specified basic
194 /// block. This is the same as using operator[] on this class.
195 ///
197 applySplitCriticalEdges();
198 return Base::getNode(BB);
199 }
200
201 /// addNewBlock - Add a new node to the dominator tree information. This
202 /// creates a new node as a child of DomBB dominator node,linking it into
203 /// the children list of the immediate dominator.
205 MachineBasicBlock *DomBB) {
206 applySplitCriticalEdges();
207 return Base::addNewBlock(BB, DomBB);
208 }
209
210 /// changeImmediateDominator - This method is used to update the dominator
211 /// tree information when a node's immediate dominator changes.
212 ///
214 MachineBasicBlock *NewIDom) {
215 applySplitCriticalEdges();
216 Base::changeImmediateDominator(N, NewIDom);
217 }
218
220 MachineDomTreeNode *NewIDom) {
221 applySplitCriticalEdges();
222 Base::changeImmediateDominator(N, NewIDom);
223 }
224
225 /// eraseNode - Removes a node from the dominator tree. Block must not
226 /// dominate any other blocks. Removes node from its immediate dominator's
227 /// children list. Deletes dominator node associated with basic block BB.
229 applySplitCriticalEdges();
230 Base::eraseNode(BB);
231 }
232
233 /// splitBlock - BB is split and now it has one successor. Update dominator
234 /// tree to reflect this change.
236 applySplitCriticalEdges();
237 Base::splitBlock(NewBB);
238 }
239
240 /// isReachableFromEntry - Return true if A is dominated by the entry
241 /// block of the function containing it.
243 applySplitCriticalEdges();
244 return Base::isReachableFromEntry(A);
245 }
246
247 /// Record that the critical edge (FromBB, ToBB) has been
248 /// split with NewBB.
249 /// This is best to use this method instead of directly update the
250 /// underlying information, because this helps mitigating the
251 /// number of time the DT information is invalidated.
252 ///
253 /// \note Do not use this method with regular edges.
254 ///
255 /// \note To benefit from the compile time improvement incurred by this
256 /// method, the users of this method have to limit the queries to the DT
257 /// interface between two edges splitting. In other words, they have to
258 /// pack the splitting of critical edges as much as possible.
260 MachineBasicBlock *ToBB,
261 MachineBasicBlock *NewBB) {
262 bool Inserted = NewBBs.insert(NewBB).second;
263 (void)Inserted;
264 assert(Inserted &&
265 "A basic block inserted via edge splitting cannot appear twice");
266 CriticalEdgesToSplit.push_back({FromBB, ToBB, NewBB});
267 }
268};
269
270/// \brief Analysis pass which computes a \c MachineDominatorTree.
274
275 static AnalysisKey Key;
276
277public:
279
281};
282
283/// \brief Machine function pass which print \c MachineDominatorTree.
287
288public:
292 static bool isRequired() { return true; }
293};
294
295/// \brief Analysis pass which computes a \c MachineDominatorTree.
297 // MachineFunctionPass may verify the analysis result without running pass,
298 // e.g. when `F.hasAvailableExternallyLinkage` is true.
299 std::optional<MachineDominatorTree> DT;
300
301public:
302 static char ID;
303
305
307 const MachineDominatorTree &getDomTree() const { return *DT; }
308
309 bool runOnMachineFunction(MachineFunction &MF) override;
310
311 void verifyAnalysis() const override;
312
313 void getAnalysisUsage(AnalysisUsage &AU) const override {
314 AU.setPreservesAll();
316 }
317
318 void releaseMemory() override;
319
320 void print(raw_ostream &OS, const Module *M = nullptr) const override;
321};
322
323//===-------------------------------------
324/// DominatorTree GraphTraits specialization so the DominatorTree can be
325/// iterable by generic graph iterators.
326///
327
328template <class Node, class ChildIterator>
330 using NodeRef = Node *;
331 using ChildIteratorType = ChildIterator;
332
333 static NodeRef getEntryNode(NodeRef N) { return N; }
334 static ChildIteratorType child_begin(NodeRef N) { return N->begin(); }
335 static ChildIteratorType child_end(NodeRef N) { return N->end(); }
336};
337
338template <class T> struct GraphTraits;
339
340template <>
344};
345
346template <>
350};
351
355 return DT->getRootNode();
356 }
357};
358
359} // end namespace llvm
360
361#endif // LLVM_CODEGEN_MACHINEDOMINATORS_H
aarch64 promote const
MachineBasicBlock & MBB
BlockVerifier::State From
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
This file defines a set of templates that efficiently compute a dominator tree over a generic graph.
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
Machine Check Debug Module
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
This file defines the SmallSet class.
This file defines the SmallVector class.
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:292
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
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
Base class for the actual dominator tree node.
typename SmallVector< DomTreeNodeBase *, 4 >::const_iterator const_iterator
Core dominator tree base class.
void addRoot(NodeT *BB)
Analysis pass which computes a MachineDominatorTree.
Machine function pass which print MachineDominatorTree.
Analysis pass which computes a MachineDominatorTree.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
const MachineDominatorTree & getDomTree() const
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
bool dominates(const MachineBasicBlock *A, const MachineBasicBlock *B) const
bool dominates(const MachineInstr *A, const MachineInstr *B) const
void changeImmediateDominator(MachineDomTreeNode *N, MachineDomTreeNode *NewIDom)
MachineBasicBlock * findNearestCommonDominator(MachineBasicBlock *A, MachineBasicBlock *B)
findNearestCommonDominator - Find nearest common dominator basic block for basic block A and B.
MachineDomTreeNode * addNewBlock(MachineBasicBlock *BB, MachineBasicBlock *DomBB)
addNewBlock - Add a new node to the dominator tree information.
bool isReachableFromEntry(const MachineBasicBlock *A)
isReachableFromEntry - Return true if A is dominated by the entry block of the function containing it...
MachineDomTreeNode * getRootNode() const
MachineDomTreeNode * getNode(MachineBasicBlock *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
void eraseNode(MachineBasicBlock *BB)
eraseNode - Removes a node from the dominator tree.
bool dominates(const MachineDomTreeNode *A, const MachineDomTreeNode *B) const
bool properlyDominates(const MachineBasicBlock *A, const MachineBasicBlock *B) const
void getDescendants(MachineBasicBlock *A, SmallVectorImpl< MachineBasicBlock * > &Result)
void splitBlock(MachineBasicBlock *NewBB)
splitBlock - BB is split and now it has one successor.
bool properlyDominates(const MachineDomTreeNode *A, const MachineDomTreeNode *B) const
MachineDominatorTree & getBase()
MachineBasicBlock * getRoot() const
void changeImmediateDominator(MachineBasicBlock *N, MachineBasicBlock *NewIDom)
changeImmediateDominator - This method is used to update the dominator tree information when a node's...
MachineDominatorTree(MachineFunction &MF)
void recordSplitCriticalEdge(MachineBasicBlock *FromBB, MachineBasicBlock *ToBB, MachineBasicBlock *NewBB)
Record that the critical edge (FromBB, ToBB) has been split with NewBB.
MachineDomTreeNode * operator[](MachineBasicBlock *BB) const
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Representation of each machine instruction.
Definition: MachineInstr.h:69
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:111
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:135
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition: SmallSet.h:179
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
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
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr)
DomTreeNodeBase< MachineBasicBlock > MachineDomTreeNode
#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:28
static NodeRef getEntryNode(MachineDominatorTree *DT)
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
static ChildIteratorType child_end(NodeRef N)
static NodeRef getEntryNode(NodeRef N)
static ChildIteratorType child_begin(NodeRef N)
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:69