Skip to content

Commit a41a46c

Browse files
authored
[CodeGen][NewPM] Port machine dominator tree analysis to new pass manager (#95879)
- Add `MachineDominatorTreeAnalysis` - Add `MachineDominatorTreePrinterPass` There is no test for this analysis in codebase. Also, the pass name is renamed to `machine-dom-tree` instead of `machinedomtree`.
1 parent ba2f496 commit a41a46c

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

llvm/include/llvm/CodeGen/MachineDominators.h

+29
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "llvm/CodeGen/MachineFunctionPass.h"
2121
#include "llvm/CodeGen/MachineInstr.h"
2222
#include "llvm/CodeGen/MachineInstrBundleIterator.h"
23+
#include "llvm/CodeGen/MachinePassManager.h"
2324
#include "llvm/Support/GenericDomTree.h"
2425
#include <cassert>
2526
#include <memory>
@@ -107,6 +108,10 @@ class MachineDominatorTree : public DomTreeBase<MachineBasicBlock> {
107108
MachineDominatorTree() = default;
108109
explicit MachineDominatorTree(MachineFunction &MF) { calculate(MF); }
109110

111+
/// Handle invalidation explicitly.
112+
bool invalidate(MachineFunction &, const PreservedAnalyses &PA,
113+
MachineFunctionAnalysisManager::Invalidator &);
114+
110115
// FIXME: If there is an updater for MachineDominatorTree,
111116
// migrate to this updater and remove these wrappers.
112117

@@ -262,6 +267,30 @@ class MachineDominatorTree : public DomTreeBase<MachineBasicBlock> {
262267
}
263268
};
264269

270+
/// \brief Analysis pass which computes a \c MachineDominatorTree.
271+
class MachineDominatorTreeAnalysis
272+
: public AnalysisInfoMixin<MachineDominatorTreeAnalysis> {
273+
friend AnalysisInfoMixin<MachineDominatorTreeAnalysis>;
274+
275+
static AnalysisKey Key;
276+
277+
public:
278+
using Result = MachineDominatorTree;
279+
280+
Result run(MachineFunction &MF, MachineFunctionAnalysisManager &);
281+
};
282+
283+
/// \brief Machine function pass which print \c MachineDominatorTree.
284+
class MachineDominatorTreePrinterPass
285+
: public PassInfoMixin<MachineDominatorTreePrinterPass> {
286+
raw_ostream &OS;
287+
288+
public:
289+
MachineDominatorTreePrinterPass(raw_ostream &OS) : OS(OS) {}
290+
PreservedAnalyses run(MachineFunction &MF,
291+
MachineFunctionAnalysisManager &MFAM);
292+
};
293+
265294
/// \brief Analysis pass which computes a \c MachineDominatorTree.
266295
class MachineDominatorTreeWrapperPass : public MachineFunctionPass {
267296
// MachineFunctionPass may verify the analysis result without running pass,

llvm/include/llvm/Passes/MachinePassRegistry.def

+3-1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ LOOP_PASS("loop-reduce", LoopStrengthReducePass())
8989
#ifndef MACHINE_FUNCTION_ANALYSIS
9090
#define MACHINE_FUNCTION_ANALYSIS(NAME, CREATE_PASS)
9191
#endif
92+
MACHINE_FUNCTION_ANALYSIS("machine-dom-tree", MachineDominatorTreeAnalysis())
9293
MACHINE_FUNCTION_ANALYSIS("pass-instrumentation", PassInstrumentationAnalysis(PIC))
9394
// LiveVariables currently requires pure SSA form.
9495
// FIXME: Once TwoAddressInstruction pass no longer uses kill flags,
@@ -106,7 +107,6 @@ MACHINE_FUNCTION_ANALYSIS("pass-instrumentation", PassInstrumentationAnalysis(PI
106107
// MACHINE_FUNCTION_ANALYSIS("machine-loops", MachineLoopInfoAnalysis())
107108
// MACHINE_FUNCTION_ANALYSIS("machine-dom-frontier",
108109
// MachineDominanceFrontierAnalysis())
109-
// MACHINE_FUNCTION_ANALYSIS("machine-dom-tree", MachineDominatorTreeAnalysis())
110110
// MACHINE_FUNCTION_ANALYSIS("machine-ore",
111111
// MachineOptimizationRemarkEmitterPassAnalysis())
112112
// MACHINE_FUNCTION_ANALYSIS("machine-post-dom-tree",
@@ -128,6 +128,8 @@ MACHINE_FUNCTION_PASS("finalize-isel", FinalizeISelPass())
128128
MACHINE_FUNCTION_PASS("localstackalloc", LocalStackSlotAllocationPass())
129129
MACHINE_FUNCTION_PASS("no-op-machine-function", NoOpMachineFunctionPass())
130130
MACHINE_FUNCTION_PASS("print", PrintMIRPass())
131+
MACHINE_FUNCTION_PASS("print<machine-dom-tree>",
132+
MachineDominatorTreePrinterPass(dbgs()))
131133
MACHINE_FUNCTION_PASS("require-all-machine-function-properties",
132134
RequireAllMachineFunctionPropertiesPass())
133135
MACHINE_FUNCTION_PASS("trigger-verifier-error", TriggerVerifierErrorPass())

llvm/lib/CodeGen/MachineDominators.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,33 @@ template bool Verify<MBBDomTree>(const MBBDomTree &DT,
5757
} // namespace DomTreeBuilder
5858
}
5959

60+
bool MachineDominatorTree::invalidate(
61+
MachineFunction &, const PreservedAnalyses &PA,
62+
MachineFunctionAnalysisManager::Invalidator &) {
63+
// Check whether the analysis, all analyses on machine functions, or the
64+
// machine function's CFG have been preserved.
65+
auto PAC = PA.getChecker<MachineDominatorTreeAnalysis>();
66+
return !PAC.preserved() &&
67+
!PAC.preservedSet<AllAnalysesOn<MachineFunction>>() &&
68+
!PAC.preservedSet<CFGAnalyses>();
69+
}
70+
71+
AnalysisKey MachineDominatorTreeAnalysis::Key;
72+
73+
MachineDominatorTreeAnalysis::Result
74+
MachineDominatorTreeAnalysis::run(MachineFunction &MF,
75+
MachineFunctionAnalysisManager &) {
76+
return MachineDominatorTree(MF);
77+
}
78+
79+
PreservedAnalyses
80+
MachineDominatorTreePrinterPass::run(MachineFunction &MF,
81+
MachineFunctionAnalysisManager &MFAM) {
82+
OS << "MachineDominatorTree for machine function: " << MF.getName() << '\n';
83+
MFAM.getResult<MachineDominatorTreeAnalysis>(MF).print(OS);
84+
return PreservedAnalyses::all();
85+
}
86+
6087
char MachineDominatorTreeWrapperPass::ID = 0;
6188

6289
INITIALIZE_PASS(MachineDominatorTreeWrapperPass, "machinedomtree",

llvm/lib/Passes/PassBuilder.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
#include "llvm/CodeGen/LocalStackSlotAllocation.h"
9494
#include "llvm/CodeGen/LowerEmuTLS.h"
9595
#include "llvm/CodeGen/MIRPrinter.h"
96+
#include "llvm/CodeGen/MachineDominators.h"
9697
#include "llvm/CodeGen/MachineFunctionAnalysis.h"
9798
#include "llvm/CodeGen/MachinePassManager.h"
9899
#include "llvm/CodeGen/MachineRegisterInfo.h"

0 commit comments

Comments
 (0)