[llvm] f3f0b99 - [M68k] Add stub M68kMCExpr and migrate away from MCSymbolRefExpr::VariantKind

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sat Mar 22 17:51:53 PDT 2025


Author: Fangrui Song
Date: 2025-03-22T17:51:48-07:00
New Revision: f3f0b99b1c7520b065cf79f0e0901fc8d2c1d4ec

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

LOG: [M68k] Add stub M68kMCExpr and migrate away from MCSymbolRefExpr::VariantKind

Similar to previous migration done for other targets (PowerPC, X86, ARM,
etc).

In the future, relocation specifiers should be encoded as part of
M68kMCExpr instead of MCSymbolRefExpr.

Added: 
    llvm/lib/Target/M68k/MCTargetDesc/M68kMCExpr.cpp
    llvm/lib/Target/M68k/MCTargetDesc/M68kMCExpr.h

Modified: 
    llvm/lib/Target/M68k/M68kISelLowering.cpp
    llvm/lib/Target/M68k/M68kMCInstLower.cpp
    llvm/lib/Target/M68k/MCTargetDesc/CMakeLists.txt
    llvm/lib/Target/M68k/MCTargetDesc/M68kELFObjectWriter.cpp
    llvm/lib/Target/M68k/MCTargetDesc/M68kMCAsmInfo.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/M68k/M68kISelLowering.cpp b/llvm/lib/Target/M68k/M68kISelLowering.cpp
index aebeaf0e3db5a..616b1f622619c 100644
--- a/llvm/lib/Target/M68k/M68kISelLowering.cpp
+++ b/llvm/lib/Target/M68k/M68kISelLowering.cpp
@@ -18,6 +18,7 @@
 #include "M68kSubtarget.h"
 #include "M68kTargetMachine.h"
 #include "M68kTargetObjectFile.h"
+#include "MCTargetDesc/M68kMCExpr.h"
 
 #include "llvm/ADT/Statistic.h"
 #include "llvm/CodeGen/CallingConvLower.h"
@@ -2831,8 +2832,7 @@ unsigned M68kTargetLowering::getJumpTableEncoding() const {
 const MCExpr *M68kTargetLowering::LowerCustomJumpTableEntry(
     const MachineJumpTableInfo *MJTI, const MachineBasicBlock *MBB,
     unsigned uid, MCContext &Ctx) const {
-  return MCSymbolRefExpr::create(MBB->getSymbol(), MCSymbolRefExpr::VK_GOTOFF,
-                                 Ctx);
+  return MCSymbolRefExpr::create(MBB->getSymbol(), M68kMCExpr::VK_GOTOFF, Ctx);
 }
 
 SDValue M68kTargetLowering::getPICJumpTableRelocBase(SDValue Table,

diff  --git a/llvm/lib/Target/M68k/M68kMCInstLower.cpp b/llvm/lib/Target/M68k/M68kMCInstLower.cpp
index b24d2d231c454..957c0f9d3da82 100644
--- a/llvm/lib/Target/M68k/M68kMCInstLower.cpp
+++ b/llvm/lib/Target/M68k/M68kMCInstLower.cpp
@@ -18,6 +18,7 @@
 #include "M68kInstrInfo.h"
 
 #include "MCTargetDesc/M68kBaseInfo.h"
+#include "MCTargetDesc/M68kMCExpr.h"
 
 #include "llvm/CodeGen/MachineFunction.h"
 #include "llvm/CodeGen/MachineInstr.h"
@@ -75,7 +76,7 @@ MCOperand M68kMCInstLower::LowerSymbolOperand(const MachineOperand &MO,
   // FIXME We would like an efficient form for this, so we don't have to do a
   // lot of extra uniquing. This fixme is originally from X86
   const MCExpr *Expr = nullptr;
-  MCSymbolRefExpr::VariantKind RefKind = MCSymbolRefExpr::VK_None;
+  M68kMCExpr::Specifier RefKind = M68kMCExpr::VK_None;
 
   switch (MO.getTargetFlags()) {
   default:
@@ -85,31 +86,31 @@ MCOperand M68kMCInstLower::LowerSymbolOperand(const MachineOperand &MO,
   case M68kII::MO_PC_RELATIVE_ADDRESS:
     break;
   case M68kII::MO_GOTPCREL:
-    RefKind = MCSymbolRefExpr::VK_GOTPCREL;
+    RefKind = M68kMCExpr::VK_GOTPCREL;
     break;
   case M68kII::MO_GOT:
-    RefKind = MCSymbolRefExpr::VK_GOT;
+    RefKind = M68kMCExpr::VK_GOT;
     break;
   case M68kII::MO_GOTOFF:
-    RefKind = MCSymbolRefExpr::VK_GOTOFF;
+    RefKind = M68kMCExpr::VK_GOTOFF;
     break;
   case M68kII::MO_PLT:
-    RefKind = MCSymbolRefExpr::VK_PLT;
+    RefKind = M68kMCExpr::VK_PLT;
     break;
   case M68kII::MO_TLSGD:
-    RefKind = MCSymbolRefExpr::VK_TLSGD;
+    RefKind = M68kMCExpr::VK_TLSGD;
     break;
   case M68kII::MO_TLSLD:
-    RefKind = MCSymbolRefExpr::VK_TLSLD;
+    RefKind = M68kMCExpr::VK_TLSLD;
     break;
   case M68kII::MO_TLSLDM:
-    RefKind = MCSymbolRefExpr::VK_TLSLDM;
+    RefKind = M68kMCExpr::VK_TLSLDM;
     break;
   case M68kII::MO_TLSIE:
-    RefKind = MCSymbolRefExpr::VK_GOTTPOFF;
+    RefKind = M68kMCExpr::VK_GOTTPOFF;
     break;
   case M68kII::MO_TLSLE:
-    RefKind = MCSymbolRefExpr::VK_TPOFF;
+    RefKind = M68kMCExpr::VK_TPOFF;
     break;
   }
 

diff  --git a/llvm/lib/Target/M68k/MCTargetDesc/CMakeLists.txt b/llvm/lib/Target/M68k/MCTargetDesc/CMakeLists.txt
index 1127b3b547f1e..0146e21acf363 100644
--- a/llvm/lib/Target/M68k/MCTargetDesc/CMakeLists.txt
+++ b/llvm/lib/Target/M68k/MCTargetDesc/CMakeLists.txt
@@ -4,6 +4,7 @@ add_llvm_component_library(LLVMM68kDesc
   M68kInstPrinter.cpp
   M68kMCAsmInfo.cpp
   M68kMCCodeEmitter.cpp
+  M68kMCExpr.cpp
   M68kMCTargetDesc.cpp
 
   LINK_COMPONENTS

diff  --git a/llvm/lib/Target/M68k/MCTargetDesc/M68kELFObjectWriter.cpp b/llvm/lib/Target/M68k/MCTargetDesc/M68kELFObjectWriter.cpp
index cac068e4dddf8..950f14d3b910d 100644
--- a/llvm/lib/Target/M68k/MCTargetDesc/M68kELFObjectWriter.cpp
+++ b/llvm/lib/Target/M68k/MCTargetDesc/M68kELFObjectWriter.cpp
@@ -12,6 +12,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "MCTargetDesc/M68kFixupKinds.h"
+#include "MCTargetDesc/M68kMCExpr.h"
 #include "MCTargetDesc/M68kMCTargetDesc.h"
 
 #include "llvm/BinaryFormat/ELF.h"
@@ -44,8 +45,8 @@ M68kELFObjectWriter::~M68kELFObjectWriter() {}
 
 enum M68kRelType { RT_32, RT_16, RT_8 };
 
-static M68kRelType
-getType(unsigned Kind, MCSymbolRefExpr::VariantKind &Modifier, bool &IsPCRel) {
+static M68kRelType getType(unsigned Kind, M68kMCExpr::Specifier &Modifier,
+                           bool &IsPCRel) {
   switch (Kind) {
   case FK_Data_4:
   case FK_PCRel_4:
@@ -64,14 +65,14 @@ unsigned M68kELFObjectWriter::getRelocType(MCContext &Ctx,
                                            const MCValue &Target,
                                            const MCFixup &Fixup,
                                            bool IsPCRel) const {
-  MCSymbolRefExpr::VariantKind Modifier = Target.getAccessVariant();
+  auto Specifier = M68kMCExpr::Specifier(Target.getAccessVariant());
   unsigned Kind = Fixup.getKind();
-  M68kRelType Type = getType(Kind, Modifier, IsPCRel);
-  switch (Modifier) {
+  M68kRelType Type = getType(Kind, Specifier, IsPCRel);
+  switch (Specifier) {
   default:
     llvm_unreachable("Unimplemented");
 
-  case MCSymbolRefExpr::VK_TLSGD:
+  case M68kMCExpr::VK_TLSGD:
     switch (Type) {
     case RT_32:
       return ELF::R_68K_TLS_GD32;
@@ -81,7 +82,7 @@ unsigned M68kELFObjectWriter::getRelocType(MCContext &Ctx,
       return ELF::R_68K_TLS_GD8;
     }
     llvm_unreachable("Unrecognized size");
-  case MCSymbolRefExpr::VK_TLSLDM:
+  case M68kMCExpr::VK_TLSLDM:
     switch (Type) {
     case RT_32:
       return ELF::R_68K_TLS_LDM32;
@@ -91,7 +92,7 @@ unsigned M68kELFObjectWriter::getRelocType(MCContext &Ctx,
       return ELF::R_68K_TLS_LDM8;
     }
     llvm_unreachable("Unrecognized size");
-  case MCSymbolRefExpr::VK_TLSLD:
+  case M68kMCExpr::VK_TLSLD:
     switch (Type) {
     case RT_32:
       return ELF::R_68K_TLS_LDO32;
@@ -101,7 +102,7 @@ unsigned M68kELFObjectWriter::getRelocType(MCContext &Ctx,
       return ELF::R_68K_TLS_LDO8;
     }
     llvm_unreachable("Unrecognized size");
-  case MCSymbolRefExpr::VK_GOTTPOFF:
+  case M68kMCExpr::VK_GOTTPOFF:
     switch (Type) {
     case RT_32:
       return ELF::R_68K_TLS_IE32;
@@ -111,7 +112,7 @@ unsigned M68kELFObjectWriter::getRelocType(MCContext &Ctx,
       return ELF::R_68K_TLS_IE8;
     }
     llvm_unreachable("Unrecognized size");
-  case MCSymbolRefExpr::VK_TPOFF:
+  case M68kMCExpr::VK_TPOFF:
     switch (Type) {
     case RT_32:
       return ELF::R_68K_TLS_LE32;
@@ -121,7 +122,7 @@ unsigned M68kELFObjectWriter::getRelocType(MCContext &Ctx,
       return ELF::R_68K_TLS_LE8;
     }
     llvm_unreachable("Unrecognized size");
-  case MCSymbolRefExpr::VK_None:
+  case M68kMCExpr::VK_None:
     switch (Type) {
     case RT_32:
       return IsPCRel ? ELF::R_68K_PC32 : ELF::R_68K_32;
@@ -131,7 +132,7 @@ unsigned M68kELFObjectWriter::getRelocType(MCContext &Ctx,
       return IsPCRel ? ELF::R_68K_PC8 : ELF::R_68K_8;
     }
     llvm_unreachable("Unrecognized size");
-  case MCSymbolRefExpr::VK_GOTPCREL:
+  case M68kMCExpr::VK_GOTPCREL:
     switch (Type) {
     case RT_32:
       return ELF::R_68K_GOTPCREL32;
@@ -141,7 +142,7 @@ unsigned M68kELFObjectWriter::getRelocType(MCContext &Ctx,
       return ELF::R_68K_GOTPCREL8;
     }
     llvm_unreachable("Unrecognized size");
-  case MCSymbolRefExpr::VK_GOTOFF:
+  case M68kMCExpr::VK_GOTOFF:
     assert(!IsPCRel);
     switch (Type) {
     case RT_32:
@@ -152,7 +153,7 @@ unsigned M68kELFObjectWriter::getRelocType(MCContext &Ctx,
       return ELF::R_68K_GOTOFF8;
     }
     llvm_unreachable("Unrecognized size");
-  case MCSymbolRefExpr::VK_PLT:
+  case M68kMCExpr::VK_PLT:
     switch (Type) {
     case RT_32:
       return ELF::R_68K_PLT32;

diff  --git a/llvm/lib/Target/M68k/MCTargetDesc/M68kMCAsmInfo.cpp b/llvm/lib/Target/M68k/MCTargetDesc/M68kMCAsmInfo.cpp
index 53534a02111e8..ba1b0dc2bb090 100644
--- a/llvm/lib/Target/M68k/MCTargetDesc/M68kMCAsmInfo.cpp
+++ b/llvm/lib/Target/M68k/MCTargetDesc/M68kMCAsmInfo.cpp
@@ -12,6 +12,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "M68kMCAsmInfo.h"
+#include "MCTargetDesc/M68kMCExpr.h"
 
 #include "llvm/MC/MCExpr.h"
 #include "llvm/TargetParser/Triple.h"
@@ -19,14 +20,14 @@
 using namespace llvm;
 
 const MCAsmInfo::VariantKindDesc variantKindDescs[] = {
-    {MCSymbolRefExpr::VK_GOTOFF, "GOTOFF"},
-    {MCSymbolRefExpr::VK_GOTPCREL, "GOTPCREL"},
-    {MCSymbolRefExpr::VK_GOTTPOFF, "GOTTPOFF"},
-    {MCSymbolRefExpr::VK_PLT, "PLT"},
-    {MCSymbolRefExpr::VK_TLSGD, "TLSGD"},
-    {MCSymbolRefExpr::VK_TLSLD, "TLSLD"},
-    {MCSymbolRefExpr::VK_TLSLDM, "TLSLDM"},
-    {MCSymbolRefExpr::VK_TPOFF, "TPOFF"},
+    {M68kMCExpr::VK_GOTOFF, "GOTOFF"},
+    {M68kMCExpr::VK_GOTPCREL, "GOTPCREL"},
+    {M68kMCExpr::VK_GOTTPOFF, "GOTTPOFF"},
+    {M68kMCExpr::VK_PLT, "PLT"},
+    {M68kMCExpr::VK_TLSGD, "TLSGD"},
+    {M68kMCExpr::VK_TLSLD, "TLSLD"},
+    {M68kMCExpr::VK_TLSLDM, "TLSLDM"},
+    {M68kMCExpr::VK_TPOFF, "TPOFF"},
 };
 
 void M68kELFMCAsmInfo::anchor() {}

diff  --git a/llvm/lib/Target/M68k/MCTargetDesc/M68kMCExpr.cpp b/llvm/lib/Target/M68k/MCTargetDesc/M68kMCExpr.cpp
new file mode 100644
index 0000000000000..d8019f5eb785f
--- /dev/null
+++ b/llvm/lib/Target/M68k/MCTargetDesc/M68kMCExpr.cpp
@@ -0,0 +1,33 @@
+//===- M68k specific MC expression classes ----------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "M68kMCExpr.h"
+#include "llvm/MC/MCContext.h"
+#include "llvm/MC/MCStreamer.h"
+#include "llvm/MC/MCValue.h"
+
+using namespace llvm;
+
+const M68kMCExpr *M68kMCExpr::create(const MCExpr *Expr, Specifier S,
+                                     MCContext &Ctx) {
+  return new (Ctx) M68kMCExpr(Expr, S);
+}
+
+void M68kMCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {}
+
+bool M68kMCExpr::evaluateAsRelocatableImpl(MCValue &Res,
+                                           const MCAssembler *Asm) const {
+  if (!getSubExpr()->evaluateAsRelocatable(Res, Asm))
+    return false;
+
+  Res =
+      MCValue::get(Res.getSymA(), Res.getSymB(), Res.getConstant(), specifier);
+  return Res.getSymB() ? specifier == VK_None : true;
+}
+
+void M68kMCExpr::visitUsedExpr(MCStreamer &S) const { S.visitUsedExpr(*Expr); }

diff  --git a/llvm/lib/Target/M68k/MCTargetDesc/M68kMCExpr.h b/llvm/lib/Target/M68k/MCTargetDesc/M68kMCExpr.h
new file mode 100644
index 0000000000000..a38569e322ae4
--- /dev/null
+++ b/llvm/lib/Target/M68k/MCTargetDesc/M68kMCExpr.h
@@ -0,0 +1,59 @@
+//===- M68k specific MC expression classes ----------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// The MCTargetExpr subclass describes a relocatable expression with a
+// M68k-specific relocation specifier.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIB_TARGET_M68K_MCTARGETDESC_M68KMCEXPR_H
+#define LLVM_LIB_TARGET_M68K_MCTARGETDESC_M68KMCEXPR_H
+
+#include "llvm/MC/MCExpr.h"
+
+namespace llvm {
+
+class M68kMCExpr : public MCTargetExpr {
+public:
+  enum Specifier {
+    VK_None,
+
+    VK_GOT = MCSymbolRefExpr::FirstTargetSpecifier,
+    VK_GOTOFF,
+    VK_GOTPCREL,
+    VK_GOTTPOFF,
+    VK_PLT,
+    VK_TLSGD,
+    VK_TLSLD,
+    VK_TLSLDM,
+    VK_TPOFF,
+  };
+
+private:
+  const MCExpr *Expr;
+  const Specifier specifier;
+
+protected:
+  explicit M68kMCExpr(const MCExpr *Expr, Specifier S)
+      : Expr(Expr), specifier(S) {}
+
+public:
+  static const M68kMCExpr *create(const MCExpr *, Specifier, MCContext &);
+
+  Specifier getSpecifier() const { return specifier; }
+  const MCExpr *getSubExpr() const { return Expr; }
+
+  void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const override;
+  bool evaluateAsRelocatableImpl(MCValue &Res,
+                                 const MCAssembler *Asm) const override;
+  void visitUsedExpr(MCStreamer &Streamer) const override;
+  MCFragment *findAssociatedFragment() const override;
+};
+} // namespace llvm
+
+#endif


        


More information about the llvm-commits mailing list