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

via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 21 19:24:47 PDT 2024


Author: paperchalice
Date: 2024-06-22T10:24:44+08:00
New Revision: a41a46c665ffb50f129daadc8c7b081ddf0b1e37

URL: https://github.com/llvm/llvm-project/commit/a41a46c665ffb50f129daadc8c7b081ddf0b1e37
DIFF: https://github.com/llvm/llvm-project/commit/a41a46c665ffb50f129daadc8c7b081ddf0b1e37.diff

LOG: [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`.

Added: 
    

Modified: 
    llvm/include/llvm/CodeGen/MachineDominators.h
    llvm/include/llvm/Passes/MachinePassRegistry.def
    llvm/lib/CodeGen/MachineDominators.cpp
    llvm/lib/Passes/PassBuilder.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/CodeGen/MachineDominators.h b/llvm/include/llvm/CodeGen/MachineDominators.h
index 6f889fc2ea37f..01f3783a4ffb4 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,30 @@ class MachineDominatorTree : public DomTreeBase<MachineBasicBlock> {
   }
 };
 
+/// \brief Analysis pass which computes a \c MachineDominatorTree.
+class MachineDominatorTreeAnalysis
+    : public AnalysisInfoMixin<MachineDominatorTreeAnalysis> {
+  friend AnalysisInfoMixin<MachineDominatorTreeAnalysis>;
+
+  static AnalysisKey Key;
+
+public:
+  using Result = MachineDominatorTree;
+
+  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..b989a1ee8306c 100644
--- a/llvm/include/llvm/Passes/MachinePassRegistry.def
+++ b/llvm/include/llvm/Passes/MachinePassRegistry.def
@@ -89,6 +89,7 @@ LOOP_PASS("loop-reduce", LoopStrengthReducePass())
 #ifndef MACHINE_FUNCTION_ANALYSIS
 #define MACHINE_FUNCTION_ANALYSIS(NAME, CREATE_PASS)
 #endif
+MACHINE_FUNCTION_ANALYSIS("machine-dom-tree", MachineDominatorTreeAnalysis())
 MACHINE_FUNCTION_ANALYSIS("pass-instrumentation", PassInstrumentationAnalysis(PIC))
 // LiveVariables currently requires pure SSA form.
 // FIXME: Once TwoAddressInstruction pass no longer uses kill flags,
@@ -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..a2cc8fdfa7c9f 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 af83e2261a333..3aa0a600dfd52 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