[llvm] b59763a - MCAsmBackend: Simplify shouldForceRelocation overrides
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 2 23:30:42 PDT 2025
Author: Fangrui Song
Date: 2025-07-02T23:30:36-07:00
New Revision: b59763a7db7fc502b4e1d8aaa4da319d85006066
URL: https://github.com/llvm/llvm-project/commit/b59763a7db7fc502b4e1d8aaa4da319d85006066
DIFF: https://github.com/llvm/llvm-project/commit/b59763a7db7fc502b4e1d8aaa4da319d85006066.diff
LOG: MCAsmBackend: Simplify shouldForceRelocation overrides
Added:
Modified:
llvm/lib/Target/AArch64/MCTargetDesc/AArch64AsmBackend.cpp
llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUAsmBackend.cpp
llvm/lib/Target/Hexagon/MCTargetDesc/HexagonAsmBackend.cpp
llvm/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.cpp
llvm/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.h
llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCAsmBackend.cpp
llvm/lib/Target/VE/MCTargetDesc/VEAsmBackend.cpp
llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Target/AArch64/MCTargetDesc/AArch64AsmBackend.cpp b/llvm/lib/Target/AArch64/MCTargetDesc/AArch64AsmBackend.cpp
index 128adcd77de3d..23d38178c9f04 100644
--- a/llvm/lib/Target/AArch64/MCTargetDesc/AArch64AsmBackend.cpp
+++ b/llvm/lib/Target/AArch64/MCTargetDesc/AArch64AsmBackend.cpp
@@ -93,8 +93,6 @@ class AArch64AsmBackend : public MCAsmBackend {
const MCSubtargetInfo *STI) const override;
unsigned getFixupKindContainereSizeInBytes(unsigned Kind) const;
-
- bool shouldForceRelocation(const MCFixup &Fixup, const MCValue &Target);
};
} // end anonymous namespace
@@ -411,11 +409,27 @@ unsigned AArch64AsmBackend::getFixupKindContainereSizeInBytes(unsigned Kind) con
}
}
+static bool shouldForceRelocation(const MCFixup &Fixup) {
+ // The ADRP instruction adds some multiple of 0x1000 to the current PC &
+ // ~0xfff. This means that the required offset to reach a symbol can vary by
+ // up to one step depending on where the ADRP is in memory. For example:
+ //
+ // ADRP x0, there
+ // there:
+ //
+ // If the ADRP occurs at address 0xffc then "there" will be at 0x1000 and
+ // we'll need that as an offset. At any other address "there" will be in the
+ // same page as the ADRP and the instruction should encode 0x0. Assuming the
+ // section isn't 0x1000-aligned, we therefore need to delegate this decision
+ // to the linker -- a relocation!
+ return Fixup.getTargetKind() == AArch64::fixup_aarch64_pcrel_adrp_imm21;
+}
+
void AArch64AsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
bool IsResolved) {
- if (IsResolved && shouldForceRelocation(Fixup, Target))
+ if (shouldForceRelocation(Fixup))
IsResolved = false;
maybeAddReloc(F, Fixup, Target, Value, IsResolved);
MCFixupKind Kind = Fixup.getKind();
@@ -517,23 +531,6 @@ bool AArch64AsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
return true;
}
-bool AArch64AsmBackend::shouldForceRelocation(const MCFixup &Fixup,
- const MCValue &Target) {
- // The ADRP instruction adds some multiple of 0x1000 to the current PC &
- // ~0xfff. This means that the required offset to reach a symbol can vary by
- // up to one step depending on where the ADRP is in memory. For example:
- //
- // ADRP x0, there
- // there:
- //
- // If the ADRP occurs at address 0xffc then "there" will be at 0x1000 and
- // we'll need that as an offset. At any other address "there" will be in the
- // same page as the ADRP and the instruction should encode 0x0. Assuming the
- // section isn't 0x1000-aligned, we therefore need to delegate this decision
- // to the linker -- a relocation!
- return Fixup.getTargetKind() == AArch64::fixup_aarch64_pcrel_adrp_imm21;
-}
-
namespace {
namespace CU {
diff --git a/llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUAsmBackend.cpp b/llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUAsmBackend.cpp
index 545308c734ae6..dd4f8b759ab01 100644
--- a/llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUAsmBackend.cpp
+++ b/llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUAsmBackend.cpp
@@ -50,7 +50,6 @@ class AMDGPUAsmBackend : public MCAsmBackend {
std::optional<MCFixupKind> getFixupKind(StringRef Name) const override;
MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const override;
- bool shouldForceRelocation(const MCFixup &, const MCValue &);
};
} //End anonymous namespace
@@ -134,7 +133,7 @@ void AMDGPUAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
bool IsResolved) {
- if (IsResolved && shouldForceRelocation(Fixup, Target))
+ if (Target.getSpecifier())
IsResolved = false;
maybeAddReloc(F, Fixup, Target, Value, IsResolved);
if (mc::isRelocation(Fixup.getKind()))
@@ -191,11 +190,6 @@ MCFixupKindInfo AMDGPUAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
return Infos[Kind - FirstTargetFixupKind];
}
-bool AMDGPUAsmBackend::shouldForceRelocation(const MCFixup &,
- const MCValue &Target) {
- return Target.getSpecifier();
-}
-
unsigned AMDGPUAsmBackend::getMinimumNopSize() const {
return 4;
}
diff --git a/llvm/lib/Target/Hexagon/MCTargetDesc/HexagonAsmBackend.cpp b/llvm/lib/Target/Hexagon/MCTargetDesc/HexagonAsmBackend.cpp
index 65f27327f2a15..1c31257a9e2ac 100644
--- a/llvm/lib/Target/Hexagon/MCTargetDesc/HexagonAsmBackend.cpp
+++ b/llvm/lib/Target/Hexagon/MCTargetDesc/HexagonAsmBackend.cpp
@@ -198,7 +198,7 @@ class HexagonAsmBackend : public MCAsmBackend {
return Infos[Kind - FirstTargetFixupKind];
}
- bool shouldForceRelocation(const MCFixup &Fixup, const MCValue &Target) {
+ bool shouldForceRelocation(const MCFixup &Fixup) {
switch(Fixup.getTargetKind()) {
default:
llvm_unreachable("Unknown Fixup Kind!");
@@ -656,7 +656,7 @@ void HexagonAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
const MCValue &Target,
MutableArrayRef<char> Data,
uint64_t FixupValue, bool IsResolved) {
- if (IsResolved && shouldForceRelocation(Fixup, Target))
+ if (IsResolved && shouldForceRelocation(Fixup))
IsResolved = false;
maybeAddReloc(F, Fixup, Target, FixupValue, IsResolved);
// When FixupValue is 0 the relocation is external and there
diff --git a/llvm/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.cpp b/llvm/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.cpp
index 5b4bc9cc569e4..dad4e705a8d45 100644
--- a/llvm/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.cpp
+++ b/llvm/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.cpp
@@ -239,6 +239,47 @@ static unsigned calculateMMLEIndex(unsigned i) {
return (1 - i / 2) * 2 + i % 2;
}
+static bool shouldForceRelocation(const MCFixup &Fixup) {
+ const unsigned FixupKind = Fixup.getKind();
+ switch (FixupKind) {
+ default:
+ return false;
+ // All these relocations require special processing
+ // at linking time. Delegate this work to a linker.
+ case Mips::fixup_Mips_CALL_HI16:
+ case Mips::fixup_Mips_CALL_LO16:
+ case Mips::fixup_Mips_CALL16:
+ case Mips::fixup_Mips_GOT:
+ case Mips::fixup_Mips_GOT_PAGE:
+ case Mips::fixup_Mips_GOT_OFST:
+ case Mips::fixup_Mips_GOT_DISP:
+ case Mips::fixup_Mips_GOT_HI16:
+ case Mips::fixup_Mips_GOT_LO16:
+ case Mips::fixup_Mips_GOTTPREL:
+ case Mips::fixup_Mips_DTPREL_HI:
+ case Mips::fixup_Mips_DTPREL_LO:
+ case Mips::fixup_Mips_TLSGD:
+ case Mips::fixup_Mips_TLSLDM:
+ case Mips::fixup_Mips_TPREL_HI:
+ case Mips::fixup_Mips_TPREL_LO:
+ case Mips::fixup_Mips_JALR:
+ case Mips::fixup_MICROMIPS_CALL16:
+ case Mips::fixup_MICROMIPS_GOT_DISP:
+ case Mips::fixup_MICROMIPS_GOT_PAGE:
+ case Mips::fixup_MICROMIPS_GOT_OFST:
+ case Mips::fixup_MICROMIPS_GOT16:
+ case Mips::fixup_MICROMIPS_GOTTPREL:
+ case Mips::fixup_MICROMIPS_TLS_DTPREL_HI16:
+ case Mips::fixup_MICROMIPS_TLS_DTPREL_LO16:
+ case Mips::fixup_MICROMIPS_TLS_GD:
+ case Mips::fixup_MICROMIPS_TLS_LDM:
+ case Mips::fixup_MICROMIPS_TLS_TPREL_HI16:
+ case Mips::fixup_MICROMIPS_TLS_TPREL_LO16:
+ case Mips::fixup_MICROMIPS_JALR:
+ return true;
+ }
+}
+
/// ApplyFixup - Apply the \p Value for given \p Fixup into the provided
/// data fragment, at the offset specified by the fixup and following the
/// fixup kind as appropriate.
@@ -246,7 +287,7 @@ void MipsAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
bool IsResolved) {
- if (IsResolved && shouldForceRelocation(Fixup, Target))
+ if (shouldForceRelocation(Fixup))
IsResolved = false;
maybeAddReloc(F, Fixup, Target, Value, IsResolved);
MCFixupKind Kind = Fixup.getKind();
@@ -559,48 +600,6 @@ bool MipsAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
return true;
}
-bool MipsAsmBackend::shouldForceRelocation(const MCFixup &Fixup,
- const MCValue &Target) {
- const unsigned FixupKind = Fixup.getKind();
- switch (FixupKind) {
- default:
- return false;
- // All these relocations require special processing
- // at linking time. Delegate this work to a linker.
- case Mips::fixup_Mips_CALL_HI16:
- case Mips::fixup_Mips_CALL_LO16:
- case Mips::fixup_Mips_CALL16:
- case Mips::fixup_Mips_GOT:
- case Mips::fixup_Mips_GOT_PAGE:
- case Mips::fixup_Mips_GOT_OFST:
- case Mips::fixup_Mips_GOT_DISP:
- case Mips::fixup_Mips_GOT_HI16:
- case Mips::fixup_Mips_GOT_LO16:
- case Mips::fixup_Mips_GOTTPREL:
- case Mips::fixup_Mips_DTPREL_HI:
- case Mips::fixup_Mips_DTPREL_LO:
- case Mips::fixup_Mips_TLSGD:
- case Mips::fixup_Mips_TLSLDM:
- case Mips::fixup_Mips_TPREL_HI:
- case Mips::fixup_Mips_TPREL_LO:
- case Mips::fixup_Mips_JALR:
- case Mips::fixup_MICROMIPS_CALL16:
- case Mips::fixup_MICROMIPS_GOT_DISP:
- case Mips::fixup_MICROMIPS_GOT_PAGE:
- case Mips::fixup_MICROMIPS_GOT_OFST:
- case Mips::fixup_MICROMIPS_GOT16:
- case Mips::fixup_MICROMIPS_GOTTPREL:
- case Mips::fixup_MICROMIPS_TLS_DTPREL_HI16:
- case Mips::fixup_MICROMIPS_TLS_DTPREL_LO16:
- case Mips::fixup_MICROMIPS_TLS_GD:
- case Mips::fixup_MICROMIPS_TLS_LDM:
- case Mips::fixup_MICROMIPS_TLS_TPREL_HI16:
- case Mips::fixup_MICROMIPS_TLS_TPREL_LO16:
- case Mips::fixup_MICROMIPS_JALR:
- return true;
- }
-}
-
namespace {
class WindowsMipsAsmBackend : public MipsAsmBackend {
diff --git a/llvm/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.h b/llvm/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.h
index d6bae15dea2c6..816626da723af 100644
--- a/llvm/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.h
+++ b/llvm/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.h
@@ -48,8 +48,6 @@ class MipsAsmBackend : public MCAsmBackend {
bool writeNopData(raw_ostream &OS, uint64_t Count,
const MCSubtargetInfo *STI) const override;
-
- bool shouldForceRelocation(const MCFixup &Fixup, const MCValue &Target);
}; // class MipsAsmBackend
} // namespace
diff --git a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCAsmBackend.cpp b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCAsmBackend.cpp
index d0d0052c9be3d..1db5819492d3a 100644
--- a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCAsmBackend.cpp
+++ b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCAsmBackend.cpp
@@ -113,7 +113,6 @@ class SystemZMCAsmBackend : public MCAsmBackend {
// Override MCAsmBackend
std::optional<MCFixupKind> getFixupKind(StringRef Name) const override;
MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const override;
- bool shouldForceRelocation(const MCFixup &, const MCValue &);
void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
bool IsResolved) override;
@@ -153,16 +152,11 @@ MCFixupKindInfo SystemZMCAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
return SystemZ::MCFixupKindInfos[Kind - FirstTargetFixupKind];
}
-bool SystemZMCAsmBackend::shouldForceRelocation(const MCFixup &,
- const MCValue &Target) {
- return Target.getSpecifier();
-}
-
void SystemZMCAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
bool IsResolved) {
- if (IsResolved && shouldForceRelocation(Fixup, Target))
+ if (Target.getSpecifier())
IsResolved = false;
maybeAddReloc(F, Fixup, Target, Value, IsResolved);
MCFixupKind Kind = Fixup.getKind();
diff --git a/llvm/lib/Target/VE/MCTargetDesc/VEAsmBackend.cpp b/llvm/lib/Target/VE/MCTargetDesc/VEAsmBackend.cpp
index b8380618db681..ad7b92f8cf389 100644
--- a/llvm/lib/Target/VE/MCTargetDesc/VEAsmBackend.cpp
+++ b/llvm/lib/Target/VE/MCTargetDesc/VEAsmBackend.cpp
@@ -122,18 +122,6 @@ class VEAsmBackend : public MCAsmBackend {
return Infos[Kind - FirstTargetFixupKind];
}
- bool shouldForceRelocation(const MCFixup &Fixup, const MCValue &Target) {
- switch ((VE::Fixups)Fixup.getKind()) {
- default:
- return false;
- case VE::fixup_ve_tls_gd_hi32:
- case VE::fixup_ve_tls_gd_lo32:
- case VE::fixup_ve_tpoff_hi32:
- case VE::fixup_ve_tpoff_lo32:
- return true;
- }
- }
-
void applyFixup(const MCFragment &, const MCFixup &, const MCValue &,
MutableArrayRef<char>, uint64_t Value,
bool IsResolved) override;
@@ -183,8 +171,14 @@ class ELFVEAsmBackend : public VEAsmBackend {
void VEAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
const MCValue &Target, MutableArrayRef<char> Data,
uint64_t Value, bool IsResolved) {
- if (IsResolved && shouldForceRelocation(Fixup, Target))
+ switch (Fixup.getTargetKind()) {
+ case VE::fixup_ve_tls_gd_hi32:
+ case VE::fixup_ve_tls_gd_lo32:
+ case VE::fixup_ve_tpoff_hi32:
+ case VE::fixup_ve_tpoff_lo32:
IsResolved = false;
+ break;
+ }
maybeAddReloc(F, Fixup, Target, Value, IsResolved);
Value = adjustFixupValue(Fixup.getKind(), Value);
if (!Value)
diff --git a/llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp b/llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp
index 964954e60c12e..1213e1cf91ece 100644
--- a/llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp
+++ b/llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp
@@ -687,20 +687,16 @@ static unsigned getFixupKindSize(unsigned Kind) {
}
}
-// Force relocation when there is a specifier. This might be too conservative -
-// GAS doesn't emit a relocation for call local at plt; local:.
-bool X86AsmBackend::shouldForceRelocation(const MCFixup &,
- const MCValue &Target) {
- return Target.getSpecifier();
-}
-
void X86AsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
bool IsResolved) {
- if (IsResolved && shouldForceRelocation(Fixup, Target))
+ // Force relocation when there is a specifier. This might be too conservative
+ // - GAS doesn't emit a relocation for call local at plt; local:.
+ if (Target.getSpecifier())
IsResolved = false;
maybeAddReloc(F, Fixup, Target, Value, IsResolved);
+
auto Kind = Fixup.getKind();
if (mc::isRelocation(Kind))
return;
More information about the llvm-commits
mailing list