[llvm] [CodeGen][NewPM] Port machine dominator tree analysis to new pass manager (PR #95879)

via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 17 21:47:36 PDT 2024


https://github.com/paperchalice created https://github.com/llvm/llvm-project/pull/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`.

>From 8e731026e0d92a096b34754f43f8b85d02dfd6bd Mon Sep 17 00:00:00 2001
From: PaperChalice <liujunchang97 at outlook.com>
Date: Tue, 18 Jun 2024 12:45:31 +0800
Subject: [PATCH] [CodeGen][NewPM] Port `machinedomtree` analysis to new pass
 manager - 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`.

---
 llvm/include/llvm/CodeGen/MachineDominators.h | 26 ++++++++++++++++++
 .../llvm/Passes/MachinePassRegistry.def       |  4 ++-
 llvm/lib/CodeGen/MachineDominators.cpp        | 27 +++++++++++++++++++
 llvm/lib/Passes/PassBuilder.cpp               |  1 +
 4 files changed, 57 insertions(+), 1 deletion(-)

diff --git a/llvm/include/llvm/CodeGen/MachineDominators.h b/llvm/include/llvm/CodeGen/MachineDominators.h
index 6f889fc2ea37f..000909bc196e6 100644
--- a/llvm/include/llvm/CodeGen/MachineDominators.h
+++ b/llvm/include/llvm/CodeGen/MachineDominators.h
@@ -20,6 +20,7 @@
 #include "llvm/CodeGen/MachineFunctionPass.h"
 #include "llvm/CodeGen/MachineInstr.h"
 #include "llvm/CodeGen/MachineInstrBundleIterator.h"
+#include "llvm/CodeGen/MachinePassManager.h"
 #include "llvm/Support/GenericDomTree.h"
 #include <cassert>
 #include <memory>
@@ -107,6 +108,10 @@ class MachineDominatorTree : public DomTreeBase<MachineBasicBlock> {
   MachineDominatorTree() = default;
   explicit MachineDominatorTree(MachineFunction &MF) { calculate(MF); }
 
+  /// Handle invalidation explicitly.
+  bool invalidate(MachineFunction &, const PreservedAnalyses &PA,
+                  MachineFunctionAnalysisManager::Invalidator &);
+
   // FIXME: If there is an updater for MachineDominatorTree,
   // migrate to this updater and remove these wrappers.
 
@@ -262,6 +267,27 @@ class MachineDominatorTree : public DomTreeBase<MachineBasicBlock> {
   }
 };
 
+/// \brief Analysis pass which computes a \c MachineDominatorTree.
+class MachineDominatorTreeAnalysis
+    : public AnalysisInfoMixin<MachineDominatorTreeAnalysis> {
+public:
+  using Result = MachineDominatorTree;
+
+  static AnalysisKey Key;
+  Result run(MachineFunction &MF, MachineFunctionAnalysisManager &);
+};
+
+/// \brief Machine function pass which print \c MachineDominatorTree.
+class MachineDominatorTreePrinterPass
+    : public PassInfoMixin<MachineDominatorTreePrinterPass> {
+  raw_ostream &OS;
+
+public:
+  MachineDominatorTreePrinterPass(raw_ostream &OS) : OS(OS) {}
+  PreservedAnalyses run(MachineFunction &MF,
+                        MachineFunctionAnalysisManager &MFAM);
+};
+
 /// \brief Analysis pass which computes a \c MachineDominatorTree.
 class MachineDominatorTreeWrapperPass : public MachineFunctionPass {
   // MachineFunctionPass may verify the analysis result without running pass,
diff --git a/llvm/include/llvm/Passes/MachinePassRegistry.def b/llvm/include/llvm/Passes/MachinePassRegistry.def
index e59591e473b79..b18b49fc31edc 100644
--- a/llvm/include/llvm/Passes/MachinePassRegistry.def
+++ b/llvm/include/llvm/Passes/MachinePassRegistry.def
@@ -90,6 +90,7 @@ LOOP_PASS("loop-reduce", LoopStrengthReducePass())
 #define MACHINE_FUNCTION_ANALYSIS(NAME, CREATE_PASS)
 #endif
 MACHINE_FUNCTION_ANALYSIS("pass-instrumentation", PassInstrumentationAnalysis(PIC))
+MACHINE_FUNCTION_ANALYSIS("machine-dom-tree", MachineDominatorTreeAnalysis())
 // LiveVariables currently requires pure SSA form.
 // FIXME: Once TwoAddressInstruction pass no longer uses kill flags,
 // LiveVariables can be removed completely, and LiveIntervals can be directly
@@ -106,7 +107,6 @@ MACHINE_FUNCTION_ANALYSIS("pass-instrumentation", PassInstrumentationAnalysis(PI
 // MACHINE_FUNCTION_ANALYSIS("machine-loops", MachineLoopInfoAnalysis())
 // MACHINE_FUNCTION_ANALYSIS("machine-dom-frontier",
 // MachineDominanceFrontierAnalysis())
-// MACHINE_FUNCTION_ANALYSIS("machine-dom-tree", MachineDominatorTreeAnalysis())
 // MACHINE_FUNCTION_ANALYSIS("machine-ore",
 // MachineOptimizationRemarkEmitterPassAnalysis())
 // MACHINE_FUNCTION_ANALYSIS("machine-post-dom-tree",
@@ -128,6 +128,8 @@ MACHINE_FUNCTION_PASS("finalize-isel", FinalizeISelPass())
 MACHINE_FUNCTION_PASS("localstackalloc", LocalStackSlotAllocationPass())
 MACHINE_FUNCTION_PASS("no-op-machine-function", NoOpMachineFunctionPass())
 MACHINE_FUNCTION_PASS("print", PrintMIRPass())
+MACHINE_FUNCTION_PASS("print<machine-dom-tree>",
+                      MachineDominatorTreePrinterPass(dbgs()))
 MACHINE_FUNCTION_PASS("require-all-machine-function-properties",
                       RequireAllMachineFunctionPropertiesPass())
 MACHINE_FUNCTION_PASS("trigger-verifier-error", TriggerVerifierErrorPass())
diff --git a/llvm/lib/CodeGen/MachineDominators.cpp b/llvm/lib/CodeGen/MachineDominators.cpp
index 22d315a1d149a..4aa0d9b419f9c 100644
--- a/llvm/lib/CodeGen/MachineDominators.cpp
+++ b/llvm/lib/CodeGen/MachineDominators.cpp
@@ -57,6 +57,33 @@ template bool Verify<MBBDomTree>(const MBBDomTree &DT,
 } // namespace DomTreeBuilder
 }
 
+bool MachineDominatorTree::invalidate(
+    MachineFunction &, const PreservedAnalyses &PA,
+    MachineFunctionAnalysisManager::Invalidator &) {
+  // Check whether the analysis, all analyses on machine functions, or the
+  // machine function's CFG have been preserved.
+  auto PAC = PA.getChecker<MachineDominatorTreeAnalysis>();
+  return !(PAC.preserved() ||
+           PAC.preservedSet<AllAnalysesOn<MachineFunction>>() ||
+           PAC.preservedSet<CFGAnalyses>());
+}
+
+AnalysisKey MachineDominatorTreeAnalysis::Key;
+
+MachineDominatorTreeAnalysis::Result
+MachineDominatorTreeAnalysis::run(MachineFunction &MF,
+                                  MachineFunctionAnalysisManager &) {
+  return MachineDominatorTree(MF);
+}
+
+PreservedAnalyses
+MachineDominatorTreePrinterPass::run(MachineFunction &MF,
+                                     MachineFunctionAnalysisManager &MFAM) {
+  OS << "MachineDominatorTree for machine function: " << MF.getName() << '\n';
+  MFAM.getResult<MachineDominatorTreeAnalysis>(MF).print(OS);
+  return PreservedAnalyses::all();
+}
+
 char MachineDominatorTreeWrapperPass::ID = 0;
 
 INITIALIZE_PASS(MachineDominatorTreeWrapperPass, "machinedomtree",
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index 19e8a8ab68a73..4300d9332df19 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -93,6 +93,7 @@
 #include "llvm/CodeGen/LocalStackSlotAllocation.h"
 #include "llvm/CodeGen/LowerEmuTLS.h"
 #include "llvm/CodeGen/MIRPrinter.h"
+#include "llvm/CodeGen/MachineDominators.h"
 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
 #include "llvm/CodeGen/MachinePassManager.h"
 #include "llvm/CodeGen/MachineRegisterInfo.h"



More information about the llvm-commits mailing list