[llvm] fa12285 - [X86] Move ABS8 special case to fixupNeedsRelaxationAdvanced

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 18 22:12:39 PDT 2025


Author: Fangrui Song
Date: 2025-03-18T22:12:33-07:00
New Revision: fa1228552fd85a8d989d7ac42afa270cc1e371ec

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

LOG: [X86] Move ABS8 special case to fixupNeedsRelaxationAdvanced

And add a test that X86MCCodeEmitter doesn't utilize a 1-byte
immediate for `cmp (3+$foo)@ABS8, %edi`

Added: 
    

Modified: 
    llvm/lib/MC/MCAssembler.cpp
    llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp
    llvm/test/MC/X86/abs8.s

Removed: 
    


################################################################################
diff  --git a/llvm/lib/MC/MCAssembler.cpp b/llvm/lib/MC/MCAssembler.cpp
index 16f0cdc6b45c2..fb85accf267b4 100644
--- a/llvm/lib/MC/MCAssembler.cpp
+++ b/llvm/lib/MC/MCAssembler.cpp
@@ -1050,10 +1050,6 @@ bool MCAssembler::fixupNeedsRelaxation(const MCFixup &Fixup,
   bool WasForced;
   bool Resolved = evaluateFixup(Fixup, DF, Target, DF->getSubtargetInfo(),
                                 Value, WasForced);
-  if (Target.getSymA() &&
-      Target.getSymA()->getKind() == MCSymbolRefExpr::VK_X86_ABS8 &&
-      Fixup.getKind() == FK_Data_1)
-    return false;
   return getBackend().fixupNeedsRelaxationAdvanced(*this, Fixup, Resolved,
                                                    Value, DF, WasForced);
 }

diff  --git a/llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp b/llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp
index b7f8831bb88af..11ae2a90cbdec 100644
--- a/llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp
+++ b/llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp
@@ -176,8 +176,11 @@ class X86AsmBackend : public MCAsmBackend {
   bool mayNeedRelaxation(const MCInst &Inst,
                          const MCSubtargetInfo &STI) const override;
 
-  bool fixupNeedsRelaxation(const MCFixup &Fixup,
-                            uint64_t Value) const override;
+  bool fixupNeedsRelaxationAdvanced(const MCAssembler &Asm,
+                                    const MCFixup &Fixup, bool Resolved,
+                                    uint64_t Value,
+                                    const MCRelaxableFragment *DF,
+                                    const bool WasForced) const override;
 
   void relaxInstruction(MCInst &Inst,
                         const MCSubtargetInfo &STI) const override;
@@ -729,10 +732,24 @@ bool X86AsmBackend::mayNeedRelaxation(const MCInst &MI,
           MI.getOperand(MI.getNumOperands() - 1 - SkipOperands).isExpr());
 }
 
-bool X86AsmBackend::fixupNeedsRelaxation(const MCFixup &Fixup,
-                                         uint64_t Value) const {
-  // Relax if the value is too big for a (signed) i8.
-  return !isInt<8>(Value);
+bool X86AsmBackend::fixupNeedsRelaxationAdvanced(const MCAssembler &Asm,
+                                                 const MCFixup &Fixup,
+                                                 bool Resolved, uint64_t Value,
+                                                 const MCRelaxableFragment *DF,
+                                                 const bool WasForced) const {
+  // If resolved, relax if the value is too big for a (signed) i8.
+  if (Resolved)
+    return !isInt<8>(Value);
+
+  // Otherwise, relax unless there is a @ABS8 specifier.
+  if (Fixup.getKind() == FK_Data_1) {
+    MCValue Target;
+    if (Fixup.getValue()->evaluateAsRelocatable(Target, &Asm) &&
+        Target.getSymA() &&
+        Target.getSymA()->getKind() == MCSymbolRefExpr::VK_X86_ABS8)
+      return false;
+  }
+  return true;
 }
 
 // FIXME: Can tblgen help at all here to verify there aren't other instructions

diff  --git a/llvm/test/MC/X86/abs8.s b/llvm/test/MC/X86/abs8.s
index 71936acb37408..f4db57e771b3a 100644
--- a/llvm/test/MC/X86/abs8.s
+++ b/llvm/test/MC/X86/abs8.s
@@ -5,4 +5,10 @@
 // X86:   00000002:  R_386_8 foo
 // X64: 0: 83 ff 00  cmpl $0, %edi
 // X64:  0000000000000002:  R_X86_64_8 foo
+// X64-NEXT: 3: 3b 3c 25 00 00 00 00          cmpl    0, %edi
+// X64-NEXT:  0000000000000006:  R_X86_64_32  $foo+0x3
+// X64-NEXT: a: 3b 04 25 00 00 00 00          cmpl    0, %eax
+// X64-NEXT:  000000000000000d:  R_X86_64_32  $foo-0x4
 cmp $foo at ABS8, %edi
+cmp (3+$foo)@ABS8, %edi
+cmp ($foo-4)@ABS8, %eax


        


More information about the llvm-commits mailing list