Skip to content

Commit 837dc54

Browse files
authored
[CodeGen][NewPM] Split MachineDominatorTree into a concrete analysis result (#94571)
Prepare for new pass manager version of `MachineDominatorTreeAnalysis`. We may need a machine dominator tree version of `DomTreeUpdater` to handle `SplitCriticalEdge` in some CodeGen passes.
1 parent 2b15fb1 commit 837dc54

File tree

93 files changed

+414
-336
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+414
-336
lines changed

llvm/include/llvm/CodeGen/MachineDominators.h

Lines changed: 82 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "llvm/Support/GenericDomTreeConstruction.h"
2525
#include <cassert>
2626
#include <memory>
27+
#include <optional>
2728

2829
namespace llvm {
2930
class AnalysisUsage;
@@ -39,16 +40,39 @@ inline void DominatorTreeBase<MachineBasicBlock, false>::addRoot(
3940

4041
extern template class DomTreeNodeBase<MachineBasicBlock>;
4142
extern template class DominatorTreeBase<MachineBasicBlock, false>; // DomTree
42-
extern template class DominatorTreeBase<MachineBasicBlock, true>; // PostDomTree
4343

44-
using MachineDomTree = DomTreeBase<MachineBasicBlock>;
4544
using MachineDomTreeNode = DomTreeNodeBase<MachineBasicBlock>;
4645

46+
namespace DomTreeBuilder {
47+
using MBBDomTree = DomTreeBase<MachineBasicBlock>;
48+
using MBBUpdates = ArrayRef<llvm::cfg::Update<MachineBasicBlock *>>;
49+
using MBBDomTreeGraphDiff = GraphDiff<MachineBasicBlock *, false>;
50+
51+
extern template void Calculate<MBBDomTree>(MBBDomTree &DT);
52+
extern template void CalculateWithUpdates<MBBDomTree>(MBBDomTree &DT,
53+
MBBUpdates U);
54+
55+
extern template void InsertEdge<MBBDomTree>(MBBDomTree &DT,
56+
MachineBasicBlock *From,
57+
MachineBasicBlock *To);
58+
59+
extern template void DeleteEdge<MBBDomTree>(MBBDomTree &DT,
60+
MachineBasicBlock *From,
61+
MachineBasicBlock *To);
62+
63+
extern template void ApplyUpdates<MBBDomTree>(MBBDomTree &DT,
64+
MBBDomTreeGraphDiff &,
65+
MBBDomTreeGraphDiff *);
66+
67+
extern template bool Verify<MBBDomTree>(const MBBDomTree &DT,
68+
MBBDomTree::VerificationLevel VL);
69+
} // namespace DomTreeBuilder
70+
4771
//===-------------------------------------
4872
/// DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
4973
/// compute a normal dominator tree.
5074
///
51-
class MachineDominatorTree : public MachineFunctionPass {
75+
class MachineDominatorTree : public DomTreeBase<MachineBasicBlock> {
5276
/// Helper structure used to hold all the basic blocks
5377
/// involved in the split of a critical edge.
5478
struct CriticalEdge {
@@ -70,70 +94,64 @@ class MachineDominatorTree : public MachineFunctionPass {
7094
/// such as BB == elt.NewBB.
7195
mutable SmallSet<MachineBasicBlock *, 32> NewBBs;
7296

73-
/// The DominatorTreeBase that is used to compute a normal dominator tree.
74-
std::unique_ptr<MachineDomTree> DT;
75-
7697
/// Apply all the recorded critical edges to the DT.
7798
/// This updates the underlying DT information in a way that uses
7899
/// the fast query path of DT as much as possible.
100+
/// FIXME: This method should not be a const member!
79101
///
80102
/// \post CriticalEdgesToSplit.empty().
81103
void applySplitCriticalEdges() const;
82104

83105
public:
84-
static char ID; // Pass ID, replacement for typeid
106+
using Base = DomTreeBase<MachineBasicBlock>;
85107

86-
MachineDominatorTree();
87-
explicit MachineDominatorTree(MachineFunction &MF) : MachineFunctionPass(ID) {
88-
calculate(MF);
89-
}
108+
MachineDominatorTree() = default;
109+
explicit MachineDominatorTree(MachineFunction &MF) { calculate(MF); }
90110

91-
MachineDomTree &getBase() {
92-
if (!DT)
93-
DT.reset(new MachineDomTree());
111+
// FIXME: If there is an updater for MachineDominatorTree,
112+
// migrate to this updater and remove these wrappers.
113+
114+
MachineDominatorTree &getBase() {
94115
applySplitCriticalEdges();
95-
return *DT;
116+
return *this;
96117
}
97118

98-
void getAnalysisUsage(AnalysisUsage &AU) const override;
99-
100119
MachineBasicBlock *getRoot() const {
101120
applySplitCriticalEdges();
102-
return DT->getRoot();
121+
return Base::getRoot();
103122
}
104123

105124
MachineDomTreeNode *getRootNode() const {
106125
applySplitCriticalEdges();
107-
return DT->getRootNode();
126+
return const_cast<MachineDomTreeNode *>(Base::getRootNode());
108127
}
109128

110-
bool runOnMachineFunction(MachineFunction &F) override;
111-
112129
void calculate(MachineFunction &F);
113130

114131
bool dominates(const MachineDomTreeNode *A,
115132
const MachineDomTreeNode *B) const {
116133
applySplitCriticalEdges();
117-
return DT->dominates(A, B);
134+
return Base::dominates(A, B);
118135
}
119136

120137
void getDescendants(MachineBasicBlock *A,
121138
SmallVectorImpl<MachineBasicBlock *> &Result) {
122139
applySplitCriticalEdges();
123-
DT->getDescendants(A, Result);
140+
Base::getDescendants(A, Result);
124141
}
125142

126143
bool dominates(const MachineBasicBlock *A, const MachineBasicBlock *B) const {
127144
applySplitCriticalEdges();
128-
return DT->dominates(A, B);
145+
return Base::dominates(A, B);
129146
}
130147

131148
// dominates - Return true if A dominates B. This performs the
132149
// special checks necessary if A and B are in the same basic block.
133150
bool dominates(const MachineInstr *A, const MachineInstr *B) const {
134151
applySplitCriticalEdges();
135152
const MachineBasicBlock *BBA = A->getParent(), *BBB = B->getParent();
136-
if (BBA != BBB) return DT->dominates(BBA, BBB);
153+
if (BBA != BBB)
154+
return Base::dominates(BBA, BBB);
137155

138156
// Loop through the basic block until we find A or B.
139157
MachineBasicBlock::const_iterator I = BBA->begin();
@@ -146,34 +164,34 @@ class MachineDominatorTree : public MachineFunctionPass {
146164
bool properlyDominates(const MachineDomTreeNode *A,
147165
const MachineDomTreeNode *B) const {
148166
applySplitCriticalEdges();
149-
return DT->properlyDominates(A, B);
167+
return Base::properlyDominates(A, B);
150168
}
151169

152170
bool properlyDominates(const MachineBasicBlock *A,
153171
const MachineBasicBlock *B) const {
154172
applySplitCriticalEdges();
155-
return DT->properlyDominates(A, B);
173+
return Base::properlyDominates(A, B);
156174
}
157175

158176
/// findNearestCommonDominator - Find nearest common dominator basic block
159177
/// for basic block A and B. If there is no such block then return NULL.
160178
MachineBasicBlock *findNearestCommonDominator(MachineBasicBlock *A,
161179
MachineBasicBlock *B) {
162180
applySplitCriticalEdges();
163-
return DT->findNearestCommonDominator(A, B);
181+
return Base::findNearestCommonDominator(A, B);
164182
}
165183

166184
MachineDomTreeNode *operator[](MachineBasicBlock *BB) const {
167185
applySplitCriticalEdges();
168-
return DT->getNode(BB);
186+
return Base::getNode(BB);
169187
}
170188

171189
/// getNode - return the (Post)DominatorTree node for the specified basic
172190
/// block. This is the same as using operator[] on this class.
173191
///
174192
MachineDomTreeNode *getNode(MachineBasicBlock *BB) const {
175193
applySplitCriticalEdges();
176-
return DT->getNode(BB);
194+
return Base::getNode(BB);
177195
}
178196

179197
/// addNewBlock - Add a new node to the dominator tree information. This
@@ -182,7 +200,7 @@ class MachineDominatorTree : public MachineFunctionPass {
182200
MachineDomTreeNode *addNewBlock(MachineBasicBlock *BB,
183201
MachineBasicBlock *DomBB) {
184202
applySplitCriticalEdges();
185-
return DT->addNewBlock(BB, DomBB);
203+
return Base::addNewBlock(BB, DomBB);
186204
}
187205

188206
/// changeImmediateDominator - This method is used to update the dominator
@@ -191,43 +209,37 @@ class MachineDominatorTree : public MachineFunctionPass {
191209
void changeImmediateDominator(MachineBasicBlock *N,
192210
MachineBasicBlock *NewIDom) {
193211
applySplitCriticalEdges();
194-
DT->changeImmediateDominator(N, NewIDom);
212+
Base::changeImmediateDominator(N, NewIDom);
195213
}
196214

197215
void changeImmediateDominator(MachineDomTreeNode *N,
198216
MachineDomTreeNode *NewIDom) {
199217
applySplitCriticalEdges();
200-
DT->changeImmediateDominator(N, NewIDom);
218+
Base::changeImmediateDominator(N, NewIDom);
201219
}
202220

203221
/// eraseNode - Removes a node from the dominator tree. Block must not
204222
/// dominate any other blocks. Removes node from its immediate dominator's
205223
/// children list. Deletes dominator node associated with basic block BB.
206224
void eraseNode(MachineBasicBlock *BB) {
207225
applySplitCriticalEdges();
208-
DT->eraseNode(BB);
226+
Base::eraseNode(BB);
209227
}
210228

211229
/// splitBlock - BB is split and now it has one successor. Update dominator
212230
/// tree to reflect this change.
213231
void splitBlock(MachineBasicBlock* NewBB) {
214232
applySplitCriticalEdges();
215-
DT->splitBlock(NewBB);
233+
Base::splitBlock(NewBB);
216234
}
217235

218236
/// isReachableFromEntry - Return true if A is dominated by the entry
219237
/// block of the function containing it.
220238
bool isReachableFromEntry(const MachineBasicBlock *A) {
221239
applySplitCriticalEdges();
222-
return DT->isReachableFromEntry(A);
240+
return Base::isReachableFromEntry(A);
223241
}
224242

225-
void releaseMemory() override;
226-
227-
void verifyAnalysis() const override;
228-
229-
void print(raw_ostream &OS, const Module*) const override;
230-
231243
/// Record that the critical edge (FromBB, ToBB) has been
232244
/// split with NewBB.
233245
/// This is best to use this method instead of directly update the
@@ -251,6 +263,34 @@ class MachineDominatorTree : public MachineFunctionPass {
251263
}
252264
};
253265

266+
/// \brief Analysis pass which computes a \c MachineDominatorTree.
267+
class MachineDominatorTreeWrapperPass : public MachineFunctionPass {
268+
// MachineFunctionPass may verify the analysis result without running pass,
269+
// e.g. when `F.hasAvailableExternallyLinkage` is true.
270+
std::optional<MachineDominatorTree> DT;
271+
272+
public:
273+
static char ID;
274+
275+
MachineDominatorTreeWrapperPass();
276+
277+
MachineDominatorTree &getDomTree() { return *DT; }
278+
const MachineDominatorTree &getDomTree() const { return *DT; }
279+
280+
bool runOnMachineFunction(MachineFunction &MF) override;
281+
282+
void verifyAnalysis() const override;
283+
284+
void getAnalysisUsage(AnalysisUsage &AU) const override {
285+
AU.setPreservesAll();
286+
MachineFunctionPass::getAnalysisUsage(AU);
287+
}
288+
289+
void releaseMemory() override;
290+
291+
void print(raw_ostream &OS, const Module *M = nullptr) const override;
292+
};
293+
254294
//===-------------------------------------
255295
/// DominatorTree GraphTraits specialization so the DominatorTree can be
256296
/// iterable by generic graph iterators.

llvm/include/llvm/CodeGen/MachineUniformityAnalysis.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ using MachineUniformityInfo = GenericUniformityInfo<MachineSSAContext>;
3030
/// everything is uniform.
3131
MachineUniformityInfo computeMachineUniformityInfo(
3232
MachineFunction &F, const MachineCycleInfo &cycleInfo,
33-
const MachineDomTree &domTree, bool HasBranchDivergence);
33+
const MachineDominatorTree &domTree, bool HasBranchDivergence);
3434

3535
/// Legacy analysis pass which computes a \ref MachineUniformityInfo.
3636
class MachineUniformityAnalysisPass : public MachineFunctionPass {

llvm/include/llvm/InitializePasses.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ void initializeMachineCopyPropagationPass(PassRegistry&);
190190
void initializeMachineCycleInfoPrinterPassPass(PassRegistry &);
191191
void initializeMachineCycleInfoWrapperPassPass(PassRegistry &);
192192
void initializeMachineDominanceFrontierPass(PassRegistry&);
193-
void initializeMachineDominatorTreePass(PassRegistry&);
193+
void initializeMachineDominatorTreeWrapperPassPass(PassRegistry &);
194194
void initializeMachineFunctionPrinterPassPass(PassRegistry&);
195195
void initializeMachineFunctionSplitterPass(PassRegistry &);
196196
void initializeMachineLateInstrsCleanupPass(PassRegistry&);

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1728,7 +1728,8 @@ void AsmPrinter::emitFunctionBody() {
17281728

17291729
if (isVerbose()) {
17301730
// Get MachineDominatorTree or compute it on the fly if it's unavailable
1731-
MDT = getAnalysisIfAvailable<MachineDominatorTree>();
1731+
auto MDTWrapper = getAnalysisIfAvailable<MachineDominatorTreeWrapperPass>();
1732+
MDT = MDTWrapper ? &MDTWrapper->getDomTree() : nullptr;
17321733
if (!MDT) {
17331734
OwnedMDT = std::make_unique<MachineDominatorTree>();
17341735
OwnedMDT->getBase().recalculate(*MF);

llvm/lib/CodeGen/CodeGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
8080
initializeMachineCopyPropagationPass(Registry);
8181
initializeMachineCycleInfoPrinterPassPass(Registry);
8282
initializeMachineCycleInfoWrapperPassPass(Registry);
83-
initializeMachineDominatorTreePass(Registry);
83+
initializeMachineDominatorTreeWrapperPassPass(Registry);
8484
initializeMachineFunctionPrinterPassPass(Registry);
8585
initializeMachineLateInstrsCleanupPass(Registry);
8686
initializeMachineLICMPass(Registry);

llvm/lib/CodeGen/EarlyIfConversion.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -790,15 +790,15 @@ char &llvm::EarlyIfConverterID = EarlyIfConverter::ID;
790790
INITIALIZE_PASS_BEGIN(EarlyIfConverter, DEBUG_TYPE,
791791
"Early If Converter", false, false)
792792
INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
793-
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
793+
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
794794
INITIALIZE_PASS_DEPENDENCY(MachineTraceMetrics)
795795
INITIALIZE_PASS_END(EarlyIfConverter, DEBUG_TYPE,
796796
"Early If Converter", false, false)
797797

798798
void EarlyIfConverter::getAnalysisUsage(AnalysisUsage &AU) const {
799799
AU.addRequired<MachineBranchProbabilityInfo>();
800-
AU.addRequired<MachineDominatorTree>();
801-
AU.addPreserved<MachineDominatorTree>();
800+
AU.addRequired<MachineDominatorTreeWrapperPass>();
801+
AU.addPreserved<MachineDominatorTreeWrapperPass>();
802802
AU.addRequired<MachineLoopInfo>();
803803
AU.addPreserved<MachineLoopInfo>();
804804
AU.addRequired<MachineTraceMetrics>();
@@ -1089,7 +1089,7 @@ bool EarlyIfConverter::runOnMachineFunction(MachineFunction &MF) {
10891089
TRI = STI.getRegisterInfo();
10901090
SchedModel = STI.getSchedModel();
10911091
MRI = &MF.getRegInfo();
1092-
DomTree = &getAnalysis<MachineDominatorTree>();
1092+
DomTree = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
10931093
Loops = &getAnalysis<MachineLoopInfo>();
10941094
Traces = &getAnalysis<MachineTraceMetrics>();
10951095
MinInstr = nullptr;
@@ -1144,15 +1144,15 @@ char &llvm::EarlyIfPredicatorID = EarlyIfPredicator::ID;
11441144

11451145
INITIALIZE_PASS_BEGIN(EarlyIfPredicator, DEBUG_TYPE, "Early If Predicator",
11461146
false, false)
1147-
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
1147+
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
11481148
INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
11491149
INITIALIZE_PASS_END(EarlyIfPredicator, DEBUG_TYPE, "Early If Predicator", false,
11501150
false)
11511151

11521152
void EarlyIfPredicator::getAnalysisUsage(AnalysisUsage &AU) const {
11531153
AU.addRequired<MachineBranchProbabilityInfo>();
1154-
AU.addRequired<MachineDominatorTree>();
1155-
AU.addPreserved<MachineDominatorTree>();
1154+
AU.addRequired<MachineDominatorTreeWrapperPass>();
1155+
AU.addPreserved<MachineDominatorTreeWrapperPass>();
11561156
AU.addRequired<MachineLoopInfo>();
11571157
AU.addPreserved<MachineLoopInfo>();
11581158
MachineFunctionPass::getAnalysisUsage(AU);
@@ -1223,7 +1223,7 @@ bool EarlyIfPredicator::runOnMachineFunction(MachineFunction &MF) {
12231223
TRI = STI.getRegisterInfo();
12241224
MRI = &MF.getRegInfo();
12251225
SchedModel.init(&STI);
1226-
DomTree = &getAnalysis<MachineDominatorTree>();
1226+
DomTree = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
12271227
Loops = &getAnalysis<MachineLoopInfo>();
12281228
MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
12291229

0 commit comments

Comments
 (0)