[llvm] d85536c - CSKYMCCodeEmitter: Set PCRel at fixup creation

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 4 20:51:47 PDT 2025


Author: Fangrui Song
Date: 2025-07-04T20:51:42-07:00
New Revision: d85536c5e362d9bf99ba26306cf483636b105fe3

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

LOG: CSKYMCCodeEmitter: Set PCRel at fixup creation

Avoid reliance on the MCAssembler::evaluateFixup workaround that checks
MCFixupKindInfo::FKF_IsPCRel. Additionally, standardize how fixups are
appended. This helper will facilitate future fixup data structure
optimizations.

Added: 
    

Modified: 
    llvm/lib/Target/CSKY/MCTargetDesc/CSKYMCCodeEmitter.cpp
    llvm/lib/Target/CSKY/MCTargetDesc/CSKYMCTargetDesc.cpp

Removed: 
    llvm/lib/Target/CSKY/MCTargetDesc/CSKYMCCodeEmitter.h


################################################################################
diff  --git a/llvm/lib/Target/CSKY/MCTargetDesc/CSKYMCCodeEmitter.cpp b/llvm/lib/Target/CSKY/MCTargetDesc/CSKYMCCodeEmitter.cpp
index fd7d306a64ac8..95a4d39970234 100644
--- a/llvm/lib/Target/CSKY/MCTargetDesc/CSKYMCCodeEmitter.cpp
+++ b/llvm/lib/Target/CSKY/MCTargetDesc/CSKYMCCodeEmitter.cpp
@@ -10,10 +10,13 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "CSKYMCCodeEmitter.h"
 #include "CSKYMCAsmInfo.h"
+#include "MCTargetDesc/CSKYFixupKinds.h"
+#include "MCTargetDesc/CSKYMCAsmInfo.h"
 #include "MCTargetDesc/CSKYMCTargetDesc.h"
 #include "llvm/ADT/Statistic.h"
+#include "llvm/MC/MCCodeEmitter.h"
+#include "llvm/MC/MCContext.h"
 #include "llvm/MC/MCInstBuilder.h"
 #include "llvm/MC/MCInstrInfo.h"
 #include "llvm/MC/MCRegisterInfo.h"
@@ -27,6 +30,186 @@ using namespace llvm;
 
 STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
 
+static void addFixup(SmallVectorImpl<MCFixup> &Fixups, uint32_t Offset,
+                     const MCExpr *Value, uint16_t Kind) {
+  bool PCRel = false;
+  switch (Kind) {
+  case CSKY::Fixups::fixup_csky_pcrel_imm16_scale2:
+  case CSKY::Fixups::fixup_csky_pcrel_uimm16_scale4:
+  case CSKY::Fixups::fixup_csky_pcrel_imm26_scale2:
+  case CSKY::Fixups::fixup_csky_pcrel_imm18_scale2:
+  case CSKY::Fixups::fixup_csky_gotpc:
+  case CSKY::Fixups::fixup_csky_pcrel_imm10_scale2:
+  case CSKY::Fixups::fixup_csky_pcrel_uimm7_scale4:
+  case CSKY::Fixups::fixup_csky_pcrel_uimm8_scale4:
+    PCRel = true;
+  }
+  Fixups.push_back(MCFixup::create(Offset, Value, Kind, PCRel));
+}
+
+namespace {
+class CSKYMCCodeEmitter : public MCCodeEmitter {
+  MCContext &Ctx;
+  const MCInstrInfo &MII;
+
+public:
+  CSKYMCCodeEmitter(MCContext &Ctx, const MCInstrInfo &MII)
+      : Ctx(Ctx), MII(MII) {}
+
+  ~CSKYMCCodeEmitter() {}
+
+  void encodeInstruction(const MCInst &Inst, SmallVectorImpl<char> &CB,
+                         SmallVectorImpl<MCFixup> &Fixups,
+                         const MCSubtargetInfo &STI) const override;
+
+  // Generated by tablegen.
+  uint64_t getBinaryCodeForInstr(const MCInst &MI,
+                                 SmallVectorImpl<MCFixup> &Fixups,
+                                 const MCSubtargetInfo &STI) const;
+
+  // Default encoding method used by tablegen.
+  unsigned getMachineOpValue(const MCInst &MI, const MCOperand &MO,
+                             SmallVectorImpl<MCFixup> &Fixups,
+                             const MCSubtargetInfo &STI) const;
+
+  template <int shift = 0>
+  unsigned getImmOpValue(const MCInst &MI, unsigned Idx,
+                         SmallVectorImpl<MCFixup> &Fixups,
+                         const MCSubtargetInfo &STI) const {
+    const MCOperand &MO = MI.getOperand(Idx);
+    if (MO.isImm())
+      return (MO.getImm() >> shift);
+
+    assert(MO.isExpr() && "Unexpected MO type.");
+
+    MCFixupKind Kind = getTargetFixup(MO.getExpr());
+    addFixup(Fixups, 0, MO.getExpr(), Kind);
+    return 0;
+  }
+
+  unsigned getRegSeqImmOpValue(const MCInst &MI, unsigned Idx,
+                               SmallVectorImpl<MCFixup> &Fixups,
+                               const MCSubtargetInfo &STI) const;
+
+  unsigned getRegisterSeqOpValue(const MCInst &MI, unsigned Op,
+                                 SmallVectorImpl<MCFixup> &Fixups,
+                                 const MCSubtargetInfo &STI) const;
+
+  unsigned getOImmOpValue(const MCInst &MI, unsigned Idx,
+                          SmallVectorImpl<MCFixup> &Fixups,
+                          const MCSubtargetInfo &STI) const;
+
+  unsigned getImmOpValueIDLY(const MCInst &MI, unsigned Idx,
+                             SmallVectorImpl<MCFixup> &Fixups,
+                             const MCSubtargetInfo &STI) const;
+
+  unsigned getImmJMPIX(const MCInst &MI, unsigned Idx,
+                       SmallVectorImpl<MCFixup> &Fixups,
+                       const MCSubtargetInfo &STI) const;
+
+  unsigned getImmOpValueMSBSize(const MCInst &MI, unsigned Idx,
+                                SmallVectorImpl<MCFixup> &Fixups,
+                                const MCSubtargetInfo &STI) const;
+
+  unsigned getImmShiftOpValue(const MCInst &MI, unsigned Idx,
+                              SmallVectorImpl<MCFixup> &Fixups,
+                              const MCSubtargetInfo &STI) const {
+    const MCOperand &MO = MI.getOperand(Idx);
+    assert(MO.isImm() && "Unexpected MO type.");
+    return 1 << MO.getImm();
+  }
+
+  MCFixupKind getTargetFixup(const MCExpr *Expr) const;
+
+  template <llvm::CSKY::Fixups FIXUP>
+  unsigned getBranchSymbolOpValue(const MCInst &MI, unsigned Idx,
+                                  SmallVectorImpl<MCFixup> &Fixups,
+                                  const MCSubtargetInfo &STI) const {
+    const MCOperand &MO = MI.getOperand(Idx);
+
+    if (MO.isImm())
+      return MO.getImm() >> 1;
+
+    assert(MO.isExpr() && "Unexpected MO type.");
+
+    MCFixupKind Kind = MCFixupKind(FIXUP);
+    if (MO.getExpr()->getKind() == MCExpr::Specifier)
+      Kind = getTargetFixup(MO.getExpr());
+
+    addFixup(Fixups, 0, MO.getExpr(), Kind);
+    return 0;
+  }
+
+  template <llvm::CSKY::Fixups FIXUP>
+  unsigned getConstpoolSymbolOpValue(const MCInst &MI, unsigned Idx,
+                                     SmallVectorImpl<MCFixup> &Fixups,
+                                     const MCSubtargetInfo &STI) const {
+    const MCOperand &MO = MI.getOperand(Idx);
+    assert(MO.isExpr() && "Unexpected MO type.");
+
+    MCFixupKind Kind = MCFixupKind(FIXUP);
+    if (MO.getExpr()->getKind() == MCExpr::Specifier)
+      Kind = getTargetFixup(MO.getExpr());
+
+    addFixup(Fixups, 0, MO.getExpr(), Kind);
+    return 0;
+  }
+
+  template <llvm::CSKY::Fixups FIXUP>
+  unsigned getDataSymbolOpValue(const MCInst &MI, unsigned Idx,
+                                SmallVectorImpl<MCFixup> &Fixups,
+                                const MCSubtargetInfo &STI) const {
+    const MCOperand &MO = MI.getOperand(Idx);
+    assert(MO.isExpr() && "Unexpected MO type.");
+
+    MCFixupKind Kind = MCFixupKind(FIXUP);
+    if (MO.getExpr()->getKind() == MCExpr::Specifier)
+      Kind = getTargetFixup(MO.getExpr());
+
+    addFixup(Fixups, 0, MO.getExpr(), Kind);
+    return 0;
+  }
+
+  unsigned getCallSymbolOpValue(const MCInst &MI, unsigned Idx,
+                                SmallVectorImpl<MCFixup> &Fixups,
+                                const MCSubtargetInfo &STI) const {
+    const MCOperand &MO = MI.getOperand(Idx);
+    assert(MO.isExpr() && "Unexpected MO type.");
+
+    MCFixupKind Kind = MCFixupKind(CSKY::fixup_csky_pcrel_imm26_scale2);
+    if (MO.getExpr()->getKind() == MCExpr::Specifier)
+      Kind = getTargetFixup(MO.getExpr());
+
+    addFixup(Fixups, 0, MO.getExpr(), Kind);
+    return 0;
+  }
+
+  unsigned getBareSymbolOpValue(const MCInst &MI, unsigned Idx,
+                                SmallVectorImpl<MCFixup> &Fixups,
+                                const MCSubtargetInfo &STI) const {
+    const MCOperand &MO = MI.getOperand(Idx);
+    assert(MO.isExpr() && "Unexpected MO type.");
+
+    MCFixupKind Kind = MCFixupKind(CSKY::fixup_csky_pcrel_imm18_scale2);
+    if (MO.getExpr()->getKind() == MCExpr::Specifier)
+      Kind = getTargetFixup(MO.getExpr());
+
+    addFixup(Fixups, 0, MO.getExpr(), Kind);
+    return 0;
+  }
+
+  void expandJBTF(const MCInst &MI, SmallVectorImpl<char> &CB,
+                  SmallVectorImpl<MCFixup> &Fixups,
+                  const MCSubtargetInfo &STI) const;
+  void expandNEG(const MCInst &MI, SmallVectorImpl<char> &CB,
+                 SmallVectorImpl<MCFixup> &Fixups,
+                 const MCSubtargetInfo &STI) const;
+  void expandRSUBI(const MCInst &MI, SmallVectorImpl<char> &CB,
+                   SmallVectorImpl<MCFixup> &Fixups,
+                   const MCSubtargetInfo &STI) const;
+};
+} // namespace
+
 unsigned CSKYMCCodeEmitter::getOImmOpValue(const MCInst &MI, unsigned Idx,
                                            SmallVectorImpl<MCFixup> &Fixups,
                                            const MCSubtargetInfo &STI) const {

diff  --git a/llvm/lib/Target/CSKY/MCTargetDesc/CSKYMCCodeEmitter.h b/llvm/lib/Target/CSKY/MCTargetDesc/CSKYMCCodeEmitter.h
deleted file mode 100644
index 3b9176d218099..0000000000000
--- a/llvm/lib/Target/CSKY/MCTargetDesc/CSKYMCCodeEmitter.h
+++ /dev/null
@@ -1,188 +0,0 @@
-//===-- CSKYMCCodeEmitter.cpp - CSKY Code Emitter interface ---------------===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-//
-// This file implements the CSKYMCCodeEmitter class.
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_LIB_TARGET_CSKY_MCTARGETDESC_CSKYMCCODEEMITTER_H
-#define LLVM_LIB_TARGET_CSKY_MCTARGETDESC_CSKYMCCODEEMITTER_H
-
-#include "MCTargetDesc/CSKYFixupKinds.h"
-#include "MCTargetDesc/CSKYMCAsmInfo.h"
-#include "llvm/MC/MCCodeEmitter.h"
-#include "llvm/MC/MCContext.h"
-
-namespace llvm {
-
-class MCInstrInfo;
-
-class CSKYMCCodeEmitter : public MCCodeEmitter {
-  MCContext &Ctx;
-  const MCInstrInfo &MII;
-
-public:
-  CSKYMCCodeEmitter(MCContext &Ctx, const MCInstrInfo &MII)
-      : Ctx(Ctx), MII(MII) {}
-
-  ~CSKYMCCodeEmitter() {}
-
-  void encodeInstruction(const MCInst &Inst, SmallVectorImpl<char> &CB,
-                         SmallVectorImpl<MCFixup> &Fixups,
-                         const MCSubtargetInfo &STI) const override;
-
-  // Generated by tablegen.
-  uint64_t getBinaryCodeForInstr(const MCInst &MI,
-                                 SmallVectorImpl<MCFixup> &Fixups,
-                                 const MCSubtargetInfo &STI) const;
-
-  // Default encoding method used by tablegen.
-  unsigned getMachineOpValue(const MCInst &MI, const MCOperand &MO,
-                             SmallVectorImpl<MCFixup> &Fixups,
-                             const MCSubtargetInfo &STI) const;
-
-  template <int shift = 0>
-  unsigned getImmOpValue(const MCInst &MI, unsigned Idx,
-                         SmallVectorImpl<MCFixup> &Fixups,
-                         const MCSubtargetInfo &STI) const {
-    const MCOperand &MO = MI.getOperand(Idx);
-    if (MO.isImm())
-      return (MO.getImm() >> shift);
-
-    assert(MO.isExpr() && "Unexpected MO type.");
-
-    MCFixupKind Kind = getTargetFixup(MO.getExpr());
-    Fixups.push_back(MCFixup::create(0, MO.getExpr(), Kind));
-    return 0;
-  }
-
-  unsigned getRegSeqImmOpValue(const MCInst &MI, unsigned Idx,
-                               SmallVectorImpl<MCFixup> &Fixups,
-                               const MCSubtargetInfo &STI) const;
-
-  unsigned getRegisterSeqOpValue(const MCInst &MI, unsigned Op,
-                                 SmallVectorImpl<MCFixup> &Fixups,
-                                 const MCSubtargetInfo &STI) const;
-
-  unsigned getOImmOpValue(const MCInst &MI, unsigned Idx,
-                          SmallVectorImpl<MCFixup> &Fixups,
-                          const MCSubtargetInfo &STI) const;
-
-  unsigned getImmOpValueIDLY(const MCInst &MI, unsigned Idx,
-                             SmallVectorImpl<MCFixup> &Fixups,
-                             const MCSubtargetInfo &STI) const;
-
-  unsigned getImmJMPIX(const MCInst &MI, unsigned Idx,
-                       SmallVectorImpl<MCFixup> &Fixups,
-                       const MCSubtargetInfo &STI) const;
-
-  unsigned getImmOpValueMSBSize(const MCInst &MI, unsigned Idx,
-                                SmallVectorImpl<MCFixup> &Fixups,
-                                const MCSubtargetInfo &STI) const;
-
-  unsigned getImmShiftOpValue(const MCInst &MI, unsigned Idx,
-                              SmallVectorImpl<MCFixup> &Fixups,
-                              const MCSubtargetInfo &STI) const {
-    const MCOperand &MO = MI.getOperand(Idx);
-    assert(MO.isImm() && "Unexpected MO type.");
-    return 1 << MO.getImm();
-  }
-
-  MCFixupKind getTargetFixup(const MCExpr *Expr) const;
-
-  template <llvm::CSKY::Fixups FIXUP>
-  unsigned getBranchSymbolOpValue(const MCInst &MI, unsigned Idx,
-                                  SmallVectorImpl<MCFixup> &Fixups,
-                                  const MCSubtargetInfo &STI) const {
-    const MCOperand &MO = MI.getOperand(Idx);
-
-    if (MO.isImm())
-      return MO.getImm() >> 1;
-
-    assert(MO.isExpr() && "Unexpected MO type.");
-
-    MCFixupKind Kind = MCFixupKind(FIXUP);
-    if (MO.getExpr()->getKind() == MCExpr::Specifier)
-      Kind = getTargetFixup(MO.getExpr());
-
-    Fixups.push_back(MCFixup::create(0, MO.getExpr(), Kind));
-    return 0;
-  }
-
-  template <llvm::CSKY::Fixups FIXUP>
-  unsigned getConstpoolSymbolOpValue(const MCInst &MI, unsigned Idx,
-                                     SmallVectorImpl<MCFixup> &Fixups,
-                                     const MCSubtargetInfo &STI) const {
-    const MCOperand &MO = MI.getOperand(Idx);
-    assert(MO.isExpr() && "Unexpected MO type.");
-
-    MCFixupKind Kind = MCFixupKind(FIXUP);
-    if (MO.getExpr()->getKind() == MCExpr::Specifier)
-      Kind = getTargetFixup(MO.getExpr());
-
-    Fixups.push_back(MCFixup::create(0, MO.getExpr(), Kind));
-    return 0;
-  }
-
-  template <llvm::CSKY::Fixups FIXUP>
-  unsigned getDataSymbolOpValue(const MCInst &MI, unsigned Idx,
-                                SmallVectorImpl<MCFixup> &Fixups,
-                                const MCSubtargetInfo &STI) const {
-    const MCOperand &MO = MI.getOperand(Idx);
-    assert(MO.isExpr() && "Unexpected MO type.");
-
-    MCFixupKind Kind = MCFixupKind(FIXUP);
-    if (MO.getExpr()->getKind() == MCExpr::Specifier)
-      Kind = getTargetFixup(MO.getExpr());
-
-    Fixups.push_back(MCFixup::create(0, MO.getExpr(), Kind));
-    return 0;
-  }
-
-  unsigned getCallSymbolOpValue(const MCInst &MI, unsigned Idx,
-                                SmallVectorImpl<MCFixup> &Fixups,
-                                const MCSubtargetInfo &STI) const {
-    const MCOperand &MO = MI.getOperand(Idx);
-    assert(MO.isExpr() && "Unexpected MO type.");
-
-    MCFixupKind Kind = MCFixupKind(CSKY::fixup_csky_pcrel_imm26_scale2);
-    if (MO.getExpr()->getKind() == MCExpr::Specifier)
-      Kind = getTargetFixup(MO.getExpr());
-
-    Fixups.push_back(MCFixup::create(0, MO.getExpr(), Kind));
-    return 0;
-  }
-
-  unsigned getBareSymbolOpValue(const MCInst &MI, unsigned Idx,
-                                SmallVectorImpl<MCFixup> &Fixups,
-                                const MCSubtargetInfo &STI) const {
-    const MCOperand &MO = MI.getOperand(Idx);
-    assert(MO.isExpr() && "Unexpected MO type.");
-
-    MCFixupKind Kind = MCFixupKind(CSKY::fixup_csky_pcrel_imm18_scale2);
-    if (MO.getExpr()->getKind() == MCExpr::Specifier)
-      Kind = getTargetFixup(MO.getExpr());
-
-    Fixups.push_back(MCFixup::create(0, MO.getExpr(), Kind));
-    return 0;
-  }
-
-  void expandJBTF(const MCInst &MI, SmallVectorImpl<char> &CB,
-                  SmallVectorImpl<MCFixup> &Fixups,
-                  const MCSubtargetInfo &STI) const;
-  void expandNEG(const MCInst &MI, SmallVectorImpl<char> &CB,
-                 SmallVectorImpl<MCFixup> &Fixups,
-                 const MCSubtargetInfo &STI) const;
-  void expandRSUBI(const MCInst &MI, SmallVectorImpl<char> &CB,
-                   SmallVectorImpl<MCFixup> &Fixups,
-                   const MCSubtargetInfo &STI) const;
-};
-
-} // namespace llvm
-
-#endif // LLVM_LIB_TARGET_CSKY_MCTARGETDESC_CSKYMCCODEEMITTER_H

diff  --git a/llvm/lib/Target/CSKY/MCTargetDesc/CSKYMCTargetDesc.cpp b/llvm/lib/Target/CSKY/MCTargetDesc/CSKYMCTargetDesc.cpp
index bb122f683644b..faaeeb8ed4dca 100644
--- a/llvm/lib/Target/CSKY/MCTargetDesc/CSKYMCTargetDesc.cpp
+++ b/llvm/lib/Target/CSKY/MCTargetDesc/CSKYMCTargetDesc.cpp
@@ -15,7 +15,6 @@
 #include "CSKYELFStreamer.h"
 #include "CSKYInstPrinter.h"
 #include "CSKYMCAsmInfo.h"
-#include "CSKYMCCodeEmitter.h"
 #include "CSKYTargetStreamer.h"
 #include "TargetInfo/CSKYTargetInfo.h"
 #include "llvm/MC/MCAssembler.h"


        


More information about the llvm-commits mailing list