Skip to content

[Sparc] Use helper class for emitting CFI instructions into MIR #136027

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 17, 2025

Conversation

s-barannikov
Copy link
Contributor

Also, guard emission by needsFrameMoves() check. There are no changes in tests because cfi directives are currently suppressed by AsmPrinter when don't need to be printed.

Also, guard emission by `needsFrameMoves()` check.
There are no changes in tests because cfi directives are currently
suppressed by AsmPrinter when don't need to be printed.
@llvmbot
Copy link
Member

llvmbot commented Apr 16, 2025

@llvm/pr-subscribers-backend-sparc

Author: Sergei Barannikov (s-barannikov)

Changes

Also, guard emission by needsFrameMoves() check. There are no changes in tests because cfi directives are currently suppressed by AsmPrinter when don't need to be printed.


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

2 Files Affected:

  • (modified) llvm/include/llvm/CodeGen/CFIInstBuilder.h (+10)
  • (modified) llvm/lib/Target/Sparc/SparcFrameLowering.cpp (+7-27)
diff --git a/llvm/include/llvm/CodeGen/CFIInstBuilder.h b/llvm/include/llvm/CodeGen/CFIInstBuilder.h
index e799b47a0c974..48e2897747783 100644
--- a/llvm/include/llvm/CodeGen/CFIInstBuilder.h
+++ b/llvm/include/llvm/CodeGen/CFIInstBuilder.h
@@ -72,6 +72,16 @@ class CFIInstBuilder {
         nullptr, TRI.getDwarfRegNum(Reg, IsEH), Offset));
   }
 
+  void buildRegister(MCRegister Reg1, MCRegister Reg2) const {
+    insertCFIInst(MCCFIInstruction::createRegister(
+        nullptr, TRI.getDwarfRegNum(Reg1, IsEH),
+        TRI.getDwarfRegNum(Reg2, IsEH)));
+  }
+
+  void buildWindowSave() const {
+    insertCFIInst(MCCFIInstruction::createWindowSave(nullptr));
+  }
+
   void buildRestore(MCRegister Reg) const {
     insertCFIInst(MCCFIInstruction::createRestore(
         nullptr, TRI.getDwarfRegNum(Reg, IsEH)));
diff --git a/llvm/lib/Target/Sparc/SparcFrameLowering.cpp b/llvm/lib/Target/Sparc/SparcFrameLowering.cpp
index 14233a526eec1..2934c88b6bffc 100644
--- a/llvm/lib/Target/Sparc/SparcFrameLowering.cpp
+++ b/llvm/lib/Target/Sparc/SparcFrameLowering.cpp
@@ -14,6 +14,7 @@
 #include "SparcInstrInfo.h"
 #include "SparcMachineFunctionInfo.h"
 #include "SparcSubtarget.h"
+#include "llvm/CodeGen/CFIInstBuilder.h"
 #include "llvm/CodeGen/MachineFrameInfo.h"
 #include "llvm/CodeGen/MachineFunction.h"
 #include "llvm/CodeGen/MachineInstrBuilder.h"
@@ -88,14 +89,7 @@ void SparcFrameLowering::emitPrologue(MachineFunction &MF,
   assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
   MachineFrameInfo &MFI = MF.getFrameInfo();
   const SparcSubtarget &Subtarget = MF.getSubtarget<SparcSubtarget>();
-  const SparcInstrInfo &TII =
-      *static_cast<const SparcInstrInfo *>(Subtarget.getInstrInfo());
-  const SparcRegisterInfo &RegInfo =
-      *static_cast<const SparcRegisterInfo *>(Subtarget.getRegisterInfo());
   MachineBasicBlock::iterator MBBI = MBB.begin();
-  // Debug location must be unknown since the first debug location is used
-  // to determine the end of the prologue.
-  DebugLoc dl;
 
   // Get the number of bytes to allocate from the FrameInfo
   int NumBytes = (int) MFI.getStackSize();
@@ -141,26 +135,12 @@ void SparcFrameLowering::emitPrologue(MachineFunction &MF,
 
   emitSPAdjustment(MF, MBB, MBBI, -NumBytes, SAVErr, SAVEri);
 
-  unsigned regFP = RegInfo.getDwarfRegNum(SP::I6, true);
-
-  // Emit ".cfi_def_cfa_register 30".
-  unsigned CFIIndex =
-      MF.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, regFP));
-  BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
-      .addCFIIndex(CFIIndex);
-
-  // Emit ".cfi_window_save".
-  CFIIndex = MF.addFrameInst(MCCFIInstruction::createWindowSave(nullptr));
-  BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
-      .addCFIIndex(CFIIndex);
-
-  unsigned regInRA = RegInfo.getDwarfRegNum(SP::I7, true);
-  unsigned regOutRA = RegInfo.getDwarfRegNum(SP::O7, true);
-  // Emit ".cfi_register 15, 31".
-  CFIIndex = MF.addFrameInst(
-      MCCFIInstruction::createRegister(nullptr, regOutRA, regInRA));
-  BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
-      .addCFIIndex(CFIIndex);
+  if (MF.needsFrameMoves()) {
+    CFIInstBuilder CFIBuilder(MBB, MBBI, MachineInstr::NoFlags);
+    CFIBuilder.buildDefCFARegister(SP::I6);
+    CFIBuilder.buildWindowSave();
+    CFIBuilder.buildRegister(SP::O7, SP::I7);
+  }
 }
 
 MachineBasicBlock::iterator SparcFrameLowering::

Copy link
Contributor

@koachan koachan left a comment

Choose a reason for hiding this comment

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

Seems OK

@s-barannikov s-barannikov merged commit 78671db into llvm:main Apr 17, 2025
9 of 11 checks passed
@s-barannikov s-barannikov deleted the cfi/sparc branch April 17, 2025 00:53
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Apr 17, 2025
…o MIR (#136027)

Also, guard emission by `needsFrameMoves()` check. There are no changes
in tests because cfi instructions are currently ignored by AsmPrinter
when they don't need to be printed/encoded.

PR: llvm/llvm-project#136027
var-const pushed a commit to ldionne/llvm-project that referenced this pull request Apr 17, 2025
…#136027)

Also, guard emission by `needsFrameMoves()` check. There are no changes
in tests because cfi instructions are currently ignored by AsmPrinter
when they don't need to be printed/encoded.

PR: llvm#136027
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.

3 participants