Skip to content

Conversation

@boomanaiden154
Copy link
Contributor

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Nov 13, 2025

@llvm/pr-subscribers-backend-x86
@llvm/pr-subscribers-tools-llvm-exegesis

@llvm/pr-subscribers-debuginfo

Author: Aiden Grossman (boomanaiden154)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/167911.diff

7 Files Affected:

  • (modified) llvm/lib/Target/X86/X86.h (+8-2)
  • (modified) llvm/lib/Target/X86/X86FloatingPoint.cpp (+77-38)
  • (modified) llvm/lib/Target/X86/X86PassRegistry.def (+1-1)
  • (modified) llvm/lib/Target/X86/X86TargetMachine.cpp (+2-2)
  • (modified) llvm/test/CodeGen/X86/x87-stack-pop.mir (+2-1)
  • (modified) llvm/test/DebugInfo/MIR/InstrRef/x86-fp-stackifier-drop-locations.mir (+2-1)
  • (modified) llvm/tools/llvm-exegesis/lib/X86/Target.cpp (+1-1)
diff --git a/llvm/lib/Target/X86/X86.h b/llvm/lib/Target/X86/X86.h
index 200ca80adb232..03706aaaab237 100644
--- a/llvm/lib/Target/X86/X86.h
+++ b/llvm/lib/Target/X86/X86.h
@@ -44,7 +44,13 @@ FunctionPass *createCleanupLocalDynamicTLSPass();
 /// This function returns a pass which converts floating-point register
 /// references and pseudo instructions into floating-point stack references and
 /// physical instructions.
-FunctionPass *createX86FloatingPointStackifierPass();
+class X86FPStackifierPass : public PassInfoMixin<X86FPStackifierPass> {
+public:
+  PreservedAnalyses run(MachineFunction &MF,
+                        MachineFunctionAnalysisManager &MFAM);
+};
+
+FunctionPass *createX86FPStackifierLegacyPass();
 
 /// This pass inserts AVX vzeroupper instructions before each call to avoid
 /// transition penalty between functions encoded with AVX and SSE.
@@ -229,7 +235,6 @@ FunctionPass *createX86ArgumentStackSlotPass();
 FunctionPass *createX86SuppressAPXForRelocationPass();
 
 void initializeCompressEVEXPassPass(PassRegistry &);
-void initializeFPSPass(PassRegistry &);
 void initializeFixupBWInstPassPass(PassRegistry &);
 void initializeFixupLEAPassPass(PassRegistry &);
 void initializeX86ArgumentStackSlotPassPass(PassRegistry &);
@@ -246,6 +251,7 @@ void initializeX86DomainReassignmentPass(PassRegistry &);
 void initializeX86DynAllocaExpanderLegacyPass(PassRegistry &);
 void initializeX86ExecutionDomainFixPass(PassRegistry &);
 void initializeX86ExpandPseudoPass(PassRegistry &);
+void initializeX86FPStackifierLegacyPass(PassRegistry &);
 void initializeX86FastPreTileConfigPass(PassRegistry &);
 void initializeX86FastTileConfigPass(PassRegistry &);
 void initializeX86FixupSetCCPassPass(PassRegistry &);
diff --git a/llvm/lib/Target/X86/X86FloatingPoint.cpp b/llvm/lib/Target/X86/X86FloatingPoint.cpp
index bc9d354cd8e1a..6af2050d8b9d0 100644
--- a/llvm/lib/Target/X86/X86FloatingPoint.cpp
+++ b/llvm/lib/Target/X86/X86FloatingPoint.cpp
@@ -31,6 +31,8 @@
 #include "llvm/ADT/Statistic.h"
 #include "llvm/CodeGen/EdgeBundles.h"
 #include "llvm/CodeGen/LiveRegUnits.h"
+#include "llvm/CodeGen/MachineFunction.h"
+#include "llvm/CodeGen/MachineFunctionAnalysisManager.h"
 #include "llvm/CodeGen/MachineFunctionPass.h"
 #include "llvm/CodeGen/MachineInstrBuilder.h"
 #include "llvm/CodeGen/MachineRegisterInfo.h"
@@ -38,6 +40,7 @@
 #include "llvm/CodeGen/TargetInstrInfo.h"
 #include "llvm/CodeGen/TargetSubtargetInfo.h"
 #include "llvm/Config/llvm-config.h"
+#include "llvm/IR/Analysis.h"
 #include "llvm/IR/InlineAsm.h"
 #include "llvm/InitializePasses.h"
 #include "llvm/Support/Debug.h"
@@ -48,7 +51,7 @@
 #include <bitset>
 using namespace llvm;
 
-#define DEBUG_TYPE "x86-codegen"
+#define DEBUG_TYPE "x86-fp-stackifier"
 
 STATISTIC(NumFXCH, "Number of fxch instructions inserted");
 STATISTIC(NumFP, "Number of floating point instructions");
@@ -56,25 +59,10 @@ STATISTIC(NumFP, "Number of floating point instructions");
 namespace {
 const unsigned ScratchFPReg = 7;
 
-struct FPS : public MachineFunctionPass {
-  static char ID;
-  FPS() : MachineFunctionPass(ID) {}
-
-  void getAnalysisUsage(AnalysisUsage &AU) const override {
-    AU.setPreservesCFG();
-    AU.addRequired<EdgeBundlesWrapperLegacy>();
-    AU.addPreservedID(MachineLoopInfoID);
-    AU.addPreservedID(MachineDominatorsID);
-    MachineFunctionPass::getAnalysisUsage(AU);
-  }
-
-  bool runOnMachineFunction(MachineFunction &MF) override;
-
-  MachineFunctionProperties getRequiredProperties() const override {
-    return MachineFunctionProperties().setNoVRegs();
-  }
-
-  StringRef getPassName() const override { return "X86 FP Stackifier"; }
+class FPS {
+public:
+  bool shouldRun(MachineFunction &MF);
+  bool run(MachineFunction &MF, EdgeBundles *EdgeBundles);
 
 private:
   const TargetInstrInfo *TII = nullptr; // Machine instruction info.
@@ -292,15 +280,43 @@ struct FPS : public MachineFunctionPass {
 
   void setKillFlags(MachineBasicBlock &MBB) const;
 };
+
+class X86FPStackifierLegacy : public MachineFunctionPass {
+public:
+  X86FPStackifierLegacy() : MachineFunctionPass(ID) {}
+
+  static char ID;
+
+private:
+  void getAnalysisUsage(AnalysisUsage &AU) const override {
+    AU.setPreservesCFG();
+    AU.addRequired<EdgeBundlesWrapperLegacy>();
+    AU.addPreservedID(MachineLoopInfoID);
+    AU.addPreservedID(MachineDominatorsID);
+    MachineFunctionPass::getAnalysisUsage(AU);
+  }
+
+  bool runOnMachineFunction(MachineFunction &MF) override;
+
+  MachineFunctionProperties getRequiredProperties() const override {
+    return MachineFunctionProperties().setNoVRegs();
+  }
+
+  StringRef getPassName() const override { return "X86 FP Stackifier"; }
+};
 } // namespace
 
-char FPS::ID = 0;
+char X86FPStackifierLegacy::ID = 0;
 
-INITIALIZE_PASS_BEGIN(FPS, DEBUG_TYPE, "X86 FP Stackifier", false, false)
+INITIALIZE_PASS_BEGIN(X86FPStackifierLegacy, DEBUG_TYPE, "X86 FP Stackifier",
+                      false, false)
 INITIALIZE_PASS_DEPENDENCY(EdgeBundlesWrapperLegacy)
-INITIALIZE_PASS_END(FPS, DEBUG_TYPE, "X86 FP Stackifier", false, false)
+INITIALIZE_PASS_END(X86FPStackifierLegacy, DEBUG_TYPE, "X86 FP Stackifier",
+                    false, false)
 
-FunctionPass *llvm::createX86FloatingPointStackifierPass() { return new FPS(); }
+FunctionPass *llvm::createX86FPStackifierLegacyPass() {
+  return new X86FPStackifierLegacy();
+}
 
 /// getFPReg - Return the X86::FPx register number for the specified operand.
 /// For example, this returns 3 for X86::FP3.
@@ -311,28 +327,25 @@ static unsigned getFPReg(const MachineOperand &MO) {
   return Reg - X86::FP0;
 }
 
-/// runOnMachineFunction - Loop over all of the basic blocks, transforming FP
-/// register references into FP stack references.
-///
-bool FPS::runOnMachineFunction(MachineFunction &MF) {
+bool FPS::shouldRun(MachineFunction &MF) {
   // We only need to run this pass if there are any FP registers used in this
   // function.  If it is all integer, there is nothing for us to do!
-  bool FPIsUsed = false;
-
   static_assert(X86::FP6 == X86::FP0 + 6,
                 "Register enums aren't sorted right!");
   const MachineRegisterInfo &MRI = MF.getRegInfo();
-  for (unsigned i = 0; i <= 6; ++i)
-    if (!MRI.reg_nodbg_empty(X86::FP0 + i)) {
-      FPIsUsed = true;
-      break;
+  for (unsigned I = 0; I <= 6; ++I)
+    if (!MRI.reg_nodbg_empty(X86::FP0 + I)) {
+      return true;
     }
 
-  // Early exit.
-  if (!FPIsUsed)
-    return false;
+  return false;
+}
 
-  Bundles = &getAnalysis<EdgeBundlesWrapperLegacy>().getEdgeBundles();
+/// runOnMachineFunction - Loop over all of the basic blocks, transforming FP
+/// register references into FP stack references.
+///
+bool FPS::run(MachineFunction &MF, EdgeBundles *FunctionBundles) {
+  Bundles = FunctionBundles;
   TII = MF.getSubtarget().getInstrInfo();
 
   // Prepare cross-MBB liveness.
@@ -1812,3 +1825,29 @@ void FPS::setKillFlags(MachineBasicBlock &MBB) const {
     LPR.stepBackward(MI);
   }
 }
+
+bool X86FPStackifierLegacy::runOnMachineFunction(MachineFunction &MF) {
+  FPS Impl;
+  if (!Impl.shouldRun(MF))
+    return false;
+
+  EdgeBundles *Bundles =
+      &getAnalysis<EdgeBundlesWrapperLegacy>().getEdgeBundles();
+  return FPS().run(MF, Bundles);
+}
+
+PreservedAnalyses
+X86FPStackifierPass::run(MachineFunction &MF,
+                         MachineFunctionAnalysisManager &MFAM) {
+  FPS Impl;
+  if (!Impl.shouldRun(MF))
+    return PreservedAnalyses::all();
+
+  EdgeBundles *Bundles = &MFAM.getResult<EdgeBundlesAnalysis>(MF);
+  bool Changed = Impl.run(MF, Bundles);
+  if (!Changed)
+    return PreservedAnalyses::all();
+  PreservedAnalyses PA = PreservedAnalyses::none();
+  PA.preserveSet<CFGAnalyses>();
+  return PA;
+}
diff --git a/llvm/lib/Target/X86/X86PassRegistry.def b/llvm/lib/Target/X86/X86PassRegistry.def
index 0d7095b18daa8..b80ad38c146df 100644
--- a/llvm/lib/Target/X86/X86PassRegistry.def
+++ b/llvm/lib/Target/X86/X86PassRegistry.def
@@ -31,6 +31,7 @@ DUMMY_FUNCTION_PASS("x86-winehstate", WinEHStatePass())
 #endif
 MACHINE_FUNCTION_PASS("x86-avoid-trailing-call", X86AvoidTrailingCallPass())
 MACHINE_FUNCTION_PASS("x86-dyn-alloca-expander", X86DynAllocaExpanderPass())
+MACHINE_FUNCTION_PASS("x86-fp-stackifier", X86FPStackifierPass())
 MACHINE_FUNCTION_PASS("x86-isel", X86ISelDAGToDAGPass(*this))
 #undef MACHINE_FUNCTION_PASS
 
@@ -40,7 +41,6 @@ MACHINE_FUNCTION_PASS("x86-isel", X86ISelDAGToDAGPass(*this))
 DUMMY_MACHINE_FUNCTION_PASS("x86-avoid-SFB", X86AvoidSFBPass())
 DUMMY_MACHINE_FUNCTION_PASS("x86-cf-opt", X86CallFrameOptimization())
 DUMMY_MACHINE_FUNCTION_PASS("x86-cmov-conversion", X86CmovConverterPass())
-DUMMY_MACHINE_FUNCTION_PASS("x86-codege", FPS())
 DUMMY_MACHINE_FUNCTION_PASS("x86-compress-evex", CompressEVEXPass())
 DUMMY_MACHINE_FUNCTION_PASS("x86-domain-reassignment", X86DomainReassignment())
 DUMMY_MACHINE_FUNCTION_PASS("x86-execution-domain-fix", X86ExecutionDomainFix())
diff --git a/llvm/lib/Target/X86/X86TargetMachine.cpp b/llvm/lib/Target/X86/X86TargetMachine.cpp
index c1214149dfa1d..543220b2fd3b9 100644
--- a/llvm/lib/Target/X86/X86TargetMachine.cpp
+++ b/llvm/lib/Target/X86/X86TargetMachine.cpp
@@ -76,7 +76,7 @@ extern "C" LLVM_C_ABI void LLVMInitializeX86Target() {
   initializeFixupBWInstPassPass(PR);
   initializeCompressEVEXPassPass(PR);
   initializeFixupLEAPassPass(PR);
-  initializeFPSPass(PR);
+  initializeX86FPStackifierLegacyPass(PR);
   initializeX86FixupSetCCPassPass(PR);
   initializeX86CallFrameOptimizationPass(PR);
   initializeX86CmovConverterPassPass(PR);
@@ -531,7 +531,7 @@ void X86PassConfig::addMachineSSAOptimization() {
 
 void X86PassConfig::addPostRegAlloc() {
   addPass(createX86LowerTileCopyPass());
-  addPass(createX86FloatingPointStackifierPass());
+  addPass(createX86FPStackifierLegacyPass());
   // When -O0 is enabled, the Load Value Injection Hardening pass will fall back
   // to using the Speculative Execution Side Effect Suppression pass for
   // mitigation. This is to prevent slow downs due to
diff --git a/llvm/test/CodeGen/X86/x87-stack-pop.mir b/llvm/test/CodeGen/X86/x87-stack-pop.mir
index 1c4ffa54b150f..73144fd484521 100644
--- a/llvm/test/CodeGen/X86/x87-stack-pop.mir
+++ b/llvm/test/CodeGen/X86/x87-stack-pop.mir
@@ -1,5 +1,6 @@
 # NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
-# RUN: llc -mtriple=i686-- -run-pass x86-codegen -O2 -o - %s | FileCheck %s
+# RUN: llc -mtriple=i686-- -run-pass=x86-fp-stackifier -O2 -o - %s | FileCheck %s
+# RUN: llc -mtriple=i686-- -passes=x86-fp-stackifier -O2 -o - %s | FileCheck %s
 
 ---
 name: func_fxam
diff --git a/llvm/test/DebugInfo/MIR/InstrRef/x86-fp-stackifier-drop-locations.mir b/llvm/test/DebugInfo/MIR/InstrRef/x86-fp-stackifier-drop-locations.mir
index be12082c45b9a..ed7a41cc7fc1d 100644
--- a/llvm/test/DebugInfo/MIR/InstrRef/x86-fp-stackifier-drop-locations.mir
+++ b/llvm/test/DebugInfo/MIR/InstrRef/x86-fp-stackifier-drop-locations.mir
@@ -1,4 +1,5 @@
-# RUN: llc %s -run-pass=x86-codegen -o - -experimental-debug-variable-locations | FileCheck %s --implicit-check-not=debug-instr-number
+# RUN: llc %s -run-pass=x86-fp-stackifier -o - -experimental-debug-variable-locations | FileCheck %s --implicit-check-not=debug-instr-number
+# RUN: llc %s -passes=x86-fp-stackifier -o - -experimental-debug-variable-locations | FileCheck %s --implicit-check-not=debug-instr-number
 #
 # The x87 FP instructions below have debug instr numbers attached -- but the
 # operands get rewritten when it's converted to stack-form. Rather than trying
diff --git a/llvm/tools/llvm-exegesis/lib/X86/Target.cpp b/llvm/tools/llvm-exegesis/lib/X86/Target.cpp
index 77b2f491f6a72..3f2b9112e5c76 100644
--- a/llvm/tools/llvm-exegesis/lib/X86/Target.cpp
+++ b/llvm/tools/llvm-exegesis/lib/X86/Target.cpp
@@ -869,7 +869,7 @@ constexpr MCPhysReg kDefaultLoopCounterReg = X86::R8;
 
 void ExegesisX86Target::addTargetSpecificPasses(PassManagerBase &PM) const {
   // Lowers FP pseudo-instructions, e.g. ABS_Fp32 -> ABS_F.
-  PM.add(createX86FloatingPointStackifierPass());
+  PM.add(createX86FPStackifierLegacyPass());
 }
 
 MCRegister ExegesisX86Target::getScratchMemoryRegister(const Triple &TT) const {

boomanaiden154 added a commit to boomanaiden154/llvm-project that referenced this pull request Nov 13, 2025
Created using spr 1.3.7

[skip ci]
Created using spr 1.3.7
@boomanaiden154 boomanaiden154 changed the base branch from users/boomanaiden154/main.x86newpm-port-x86-fp-stackifier-pass-to-newpm to main November 13, 2025 18:53
boomanaiden154 added a commit to boomanaiden154/llvm-project that referenced this pull request Nov 13, 2025
Created using spr 1.3.7
Copy link
Collaborator

@RKSimon RKSimon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@boomanaiden154 boomanaiden154 merged commit c44bd37 into main Nov 13, 2025
8 of 9 checks passed
@boomanaiden154 boomanaiden154 deleted the users/boomanaiden154/x86newpm-port-x86-fp-stackifier-pass-to-newpm branch November 13, 2025 21:11
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Nov 13, 2025
Reviewers: arsenm, RKSimon, paperchalice, phoebewang

Reviewed By: arsenm, RKSimon

Pull Request: llvm/llvm-project#167911
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants