Skip to content

MCAsmBackend: Simplify applyFixup #141333

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 1 commit into from
May 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions llvm/include/llvm/MC/MCAsmBackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,9 @@ class MCAsmBackend {
/// the offset specified by the fixup and following the fixup kind as
/// appropriate. Errors (such as an out of range fixup value) should be
/// reported via \p Ctx.
/// The \p STI is present only for fragments of type MCRelaxableFragment and
/// MCDataFragment with hasInstructions() == true.
virtual void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
virtual void applyFixup(const MCFragment &, const MCFixup &,
const MCValue &Target, MutableArrayRef<char> Data,
uint64_t Value, bool IsResolved,
const MCSubtargetInfo *STI) const = 0;
uint64_t Value, bool IsResolved) = 0;

/// @}

Expand Down Expand Up @@ -222,6 +219,10 @@ class MCAsmBackend {

bool isDarwinCanonicalPersonality(const MCSymbol *Sym) const;

// Return STI for fragments of type MCRelaxableFragment and MCDataFragment
// with hasInstructions() == true.
static const MCSubtargetInfo *getSubtargetInfo(const MCFragment &F);

private:
const bool LinkerRelaxation;
};
Expand Down
5 changes: 2 additions & 3 deletions llvm/include/llvm/MC/MCAssembler.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,15 @@ class MCAssembler {
/// Evaluate a fixup to a relocatable expression and the value which should be
/// placed into the fixup.
///
/// \param F The fragment the fixup is inside.
/// \param Fixup The fixup to evaluate.
/// \param DF The fragment the fixup is inside.
/// \param Target [out] On return, the relocatable expression the fixup
/// evaluates to.
/// \param Value [out] On return, the value of the fixup as currently laid
/// out.
/// \param RecordReloc Record relocation if needed.
/// relocation.
bool evaluateFixup(const MCFixup &Fixup, const MCFragment *DF,
MCValue &Target, const MCSubtargetInfo *STI,
bool evaluateFixup(const MCFragment *F, const MCFixup &Fixup, MCValue &Target,
uint64_t &Value, bool RecordReloc,
MutableArrayRef<char> Contents) const;

Expand Down
21 changes: 21 additions & 0 deletions llvm/lib/MC/MCAsmBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,24 @@ bool MCAsmBackend::isDarwinCanonicalPersonality(const MCSymbol *Sym) const {
// Reserving an empty slot for it seems silly.
return name == "___gxx_personality_v0" || name == "___objc_personality_v0";
}

const MCSubtargetInfo *MCAsmBackend::getSubtargetInfo(const MCFragment &F) {
const MCSubtargetInfo *STI = nullptr;
switch (F.getKind()) {
case MCFragment::FT_Data: {
auto &DF = cast<MCDataFragment>(F);
STI = DF.getSubtargetInfo();
assert(!DF.hasInstructions() || STI != nullptr);
break;
}
case MCFragment::FT_Relaxable: {
auto &RF = cast<MCRelaxableFragment>(F);
STI = RF.getSubtargetInfo();
assert(!RF.hasInstructions() || STI != nullptr);
break;
}
default:
break;
}
return STI;
}
21 changes: 7 additions & 14 deletions llvm/lib/MC/MCAssembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ bool MCAssembler::isThumbFunc(const MCSymbol *Symbol) const {
return true;
}

bool MCAssembler::evaluateFixup(const MCFixup &Fixup, const MCFragment *DF,
MCValue &Target, const MCSubtargetInfo *STI,
uint64_t &Value, bool RecordReloc,
bool MCAssembler::evaluateFixup(const MCFragment *DF, const MCFixup &Fixup,
MCValue &Target, uint64_t &Value,
bool RecordReloc,
MutableArrayRef<char> Contents) const {
++stats::evaluateFixup;

Expand Down Expand Up @@ -203,8 +203,7 @@ bool MCAssembler::evaluateFixup(const MCFixup &Fixup, const MCFragment *DF,
IsResolved = false;
IsResolved = getBackend().addReloc(const_cast<MCAssembler &>(*this), *DF,
Fixup, Target, Value, IsResolved);
getBackend().applyFixup(*this, Fixup, Target, Contents, Value, IsResolved,
STI);
getBackend().applyFixup(*DF, Fixup, Target, Contents, Value, IsResolved);
return true;
}

Expand Down Expand Up @@ -909,7 +908,6 @@ void MCAssembler::layout() {
for (MCFragment &Frag : Sec) {
MutableArrayRef<MCFixup> Fixups;
MutableArrayRef<char> Contents;
const MCSubtargetInfo *STI = nullptr;

// Process MCAlignFragment and MCEncodedFragmentWithFixups here.
switch (Frag.getKind()) {
Expand All @@ -927,16 +925,12 @@ void MCAssembler::layout() {
MCDataFragment &DF = cast<MCDataFragment>(Frag);
Fixups = DF.getFixups();
Contents = DF.getContents();
STI = DF.getSubtargetInfo();
assert(!DF.hasInstructions() || STI != nullptr);
break;
}
case MCFragment::FT_Relaxable: {
MCRelaxableFragment &RF = cast<MCRelaxableFragment>(Frag);
Fixups = RF.getFixups();
Contents = RF.getContents();
STI = RF.getSubtargetInfo();
assert(!RF.hasInstructions() || STI != nullptr);
break;
}
case MCFragment::FT_CVDefRange: {
Expand Down Expand Up @@ -973,7 +967,7 @@ void MCAssembler::layout() {
for (const MCFixup &Fixup : Fixups) {
uint64_t FixedValue;
MCValue Target;
evaluateFixup(Fixup, &Frag, Target, STI, FixedValue,
evaluateFixup(&Frag, Fixup, Target, FixedValue,
/*RecordReloc=*/true, Contents);
}
}
Expand All @@ -994,9 +988,8 @@ bool MCAssembler::fixupNeedsRelaxation(const MCFixup &Fixup,
assert(getBackendPtr() && "Expected assembler backend");
MCValue Target;
uint64_t Value;
bool Resolved =
evaluateFixup(const_cast<MCFixup &>(Fixup), DF, Target,
DF->getSubtargetInfo(), Value, /*RecordReloc=*/false, {});
bool Resolved = evaluateFixup(DF, const_cast<MCFixup &>(Fixup), Target, Value,
/*RecordReloc=*/false, {});
return getBackend().fixupNeedsRelaxationAdvanced(*this, Fixup, Target, Value,
Resolved);
}
Expand Down
12 changes: 5 additions & 7 deletions llvm/lib/Target/AArch64/MCTargetDesc/AArch64AsmBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,9 @@ class AArch64AsmBackend : public MCAsmBackend {
return Infos[Kind - FirstTargetFixupKind];
}

void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
const MCValue &Target, MutableArrayRef<char> Data,
uint64_t Value, bool IsResolved,
const MCSubtargetInfo *STI) const override;
void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
bool IsResolved) override;

bool fixupNeedsRelaxation(const MCFixup &Fixup,
uint64_t Value) const override;
Expand Down Expand Up @@ -415,11 +414,10 @@ unsigned AArch64AsmBackend::getFixupKindContainereSizeInBytes(unsigned Kind) con
}
}

void AArch64AsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
void AArch64AsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
bool IsResolved,
const MCSubtargetInfo *STI) const {
bool IsResolved) {
MCFixupKind Kind = Fixup.getKind();
if (mc::isRelocation(Kind))
return;
Expand Down
13 changes: 5 additions & 8 deletions llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUAsmBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,9 @@ class AMDGPUAsmBackend : public MCAsmBackend {
public:
AMDGPUAsmBackend(const Target &T) : MCAsmBackend(llvm::endianness::little) {}


void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
const MCValue &Target, MutableArrayRef<char> Data,
uint64_t Value, bool IsResolved,
const MCSubtargetInfo *STI) const override;
void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
bool IsResolved) override;
bool fixupNeedsRelaxation(const MCFixup &Fixup,
uint64_t Value) const override;

Expand Down Expand Up @@ -133,11 +131,10 @@ static uint64_t adjustFixupValue(const MCFixup &Fixup, uint64_t Value,
}
}

void AMDGPUAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
void AMDGPUAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
bool IsResolved,
const MCSubtargetInfo *STI) const {
bool IsResolved) {
if (mc::isRelocation(Fixup.getKind()))
return;

Expand Down
8 changes: 4 additions & 4 deletions llvm/lib/Target/ARM/MCTargetDesc/ARMAsmBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1127,16 +1127,16 @@ static unsigned getFixupKindContainerSizeBytes(unsigned Kind) {
}
}

void ARMAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
void ARMAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
bool IsResolved,
const MCSubtargetInfo* STI) const {
bool IsResolved) {
auto Kind = Fixup.getKind();
if (mc::isRelocation(Kind))
return;
MCContext &Ctx = getContext();
Value = adjustFixupValue(Asm, Fixup, Target, Value, IsResolved, Ctx, STI);
Value = adjustFixupValue(*Asm, Fixup, Target, Value, IsResolved, Ctx,
getSubtargetInfo(F));
if (!Value)
return; // Doesn't change encoding.
const unsigned NumBytes = getFixupKindNumBytes(Kind);
Expand Down
7 changes: 3 additions & 4 deletions llvm/lib/Target/ARM/MCTargetDesc/ARMAsmBackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ class ARMAsmBackend : public MCAsmBackend {
bool IsResolved, MCContext &Ctx,
const MCSubtargetInfo *STI) const;

void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
const MCValue &Target, MutableArrayRef<char> Data,
uint64_t Value, bool IsResolved,
const MCSubtargetInfo *STI) const override;
void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
bool IsResolved) override;

unsigned getRelaxedOpcode(unsigned Op, const MCSubtargetInfo &STI) const;

Expand Down
5 changes: 2 additions & 3 deletions llvm/lib/Target/AVR/MCTargetDesc/AVRAsmBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,11 +384,10 @@ bool AVRAsmBackend::addReloc(MCAssembler &Asm, const MCFragment &F,
return IsResolved;
}

void AVRAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
void AVRAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
bool IsResolved,
const MCSubtargetInfo *STI) const {
bool IsResolved) {
if (mc::isRelocation(Fixup.getKind()))
return;
adjustFixupValue(Fixup, Target, Value, &getContext());
Expand Down
7 changes: 3 additions & 4 deletions llvm/lib/Target/AVR/MCTargetDesc/AVRAsmBackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@ class AVRAsmBackend : public MCAsmBackend {
const MCValue &Target, uint64_t &FixedValue,
bool IsResolved) override;

void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
const MCValue &Target, MutableArrayRef<char> Data,
uint64_t Value, bool IsResolved,
const MCSubtargetInfo *STI) const override;
void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
bool IsResolved) override;

std::optional<MCFixupKind> getFixupKind(StringRef Name) const override;
MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const override;
Expand Down
12 changes: 5 additions & 7 deletions llvm/lib/Target/BPF/MCTargetDesc/BPFAsmBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ class BPFAsmBackend : public MCAsmBackend {
BPFAsmBackend(llvm::endianness Endian) : MCAsmBackend(Endian) {}
~BPFAsmBackend() override = default;

void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
const MCValue &Target, MutableArrayRef<char> Data,
uint64_t Value, bool IsResolved,
const MCSubtargetInfo *STI) const override;
void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
bool IsResolved) override;

std::unique_ptr<MCObjectTargetWriter>
createObjectTargetWriter() const override;
Expand Down Expand Up @@ -67,11 +66,10 @@ bool BPFAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
return true;
}

void BPFAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
void BPFAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
bool IsResolved,
const MCSubtargetInfo *STI) const {
bool IsResolved) {
if (Fixup.getKind() == FK_SecRel_8) {
// The Value is 0 for global variables, and the in-section offset
// for static variables. Write to the immediate field of the inst.
Expand Down
5 changes: 2 additions & 3 deletions llvm/lib/Target/CSKY/MCTargetDesc/CSKYAsmBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,10 @@ bool CSKYAsmBackend::fixupNeedsRelaxationAdvanced(const MCAssembler &,
}
}

void CSKYAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
void CSKYAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
bool IsResolved,
const MCSubtargetInfo *STI) const {
bool IsResolved) {
MCFixupKind Kind = Fixup.getKind();
if (mc::isRelocation(Kind))
return;
Expand Down
8 changes: 3 additions & 5 deletions llvm/lib/Target/CSKY/MCTargetDesc/CSKYAsmBackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ class CSKYAsmBackend : public MCAsmBackend {
CSKYAsmBackend(const MCSubtargetInfo &STI, const MCTargetOptions &OP)
: MCAsmBackend(llvm::endianness::little) {}


void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
const MCValue &Target, MutableArrayRef<char> Data,
uint64_t Value, bool IsResolved,
const MCSubtargetInfo *STI) const override;
void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
bool IsResolved) override;

MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const override;

Expand Down
7 changes: 3 additions & 4 deletions llvm/lib/Target/DirectX/MCTargetDesc/DirectXMCTargetDesc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,9 @@ class DXILAsmBackend : public MCAsmBackend {
: MCAsmBackend(llvm::endianness::little) {}
~DXILAsmBackend() override = default;

void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
const MCValue &Target, MutableArrayRef<char> Data,
uint64_t Value, bool IsResolved,
const MCSubtargetInfo *STI) const override {}
void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
bool IsResolved) override {}

std::unique_ptr<MCObjectTargetWriter>
createObjectTargetWriter() const override {
Expand Down
5 changes: 2 additions & 3 deletions llvm/lib/Target/Hexagon/MCTargetDesc/HexagonAsmBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,10 +407,9 @@ class HexagonAsmBackend : public MCAsmBackend {
/// ApplyFixup - Apply the \arg Value for given \arg Fixup into the provided
/// data fragment, at the offset specified by the fixup and following the
/// fixup kind as appropriate.
void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
void applyFixup(const MCFragment &, const MCFixup &Fixup,
const MCValue &Target, MutableArrayRef<char> Data,
uint64_t FixupValue, bool IsResolved,
const MCSubtargetInfo *STI) const override {
uint64_t FixupValue, bool IsResolved) override {

// When FixupValue is 0 the relocation is external and there
// is nothing for us to do.
Expand Down
12 changes: 5 additions & 7 deletions llvm/lib/Target/Lanai/MCTargetDesc/LanaiAsmBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,9 @@ class LanaiAsmBackend : public MCAsmBackend {
LanaiAsmBackend(const Target &T, Triple::OSType OST)
: MCAsmBackend(llvm::endianness::big), OSType(OST) {}

void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
const MCValue &Target, MutableArrayRef<char> Data,
uint64_t Value, bool IsResolved,
const MCSubtargetInfo *STI) const override;
void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
bool IsResolved) override;

std::unique_ptr<MCObjectTargetWriter>
createObjectTargetWriter() const override;
Expand All @@ -72,11 +71,10 @@ bool LanaiAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
return true;
}

void LanaiAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
void LanaiAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
bool /*IsResolved*/,
const MCSubtargetInfo * /*STI*/) const {
bool) {
MCFixupKind Kind = Fixup.getKind();
Value = adjustFixupValue(static_cast<unsigned>(Kind), Value);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,10 @@ static void fixupLeb128(MCContext &Ctx, const MCFixup &Fixup,
Ctx.reportError(Fixup.getLoc(), "Invalid uleb128 value!");
}

void LoongArchAsmBackend::applyFixup(const MCAssembler &Asm,
const MCFixup &Fixup,
void LoongArchAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
bool IsResolved,
const MCSubtargetInfo *STI) const {
bool IsResolved) {
if (!Value)
return; // Doesn't change encoding.

Expand Down
7 changes: 3 additions & 4 deletions llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ class LoongArchAsmBackend : public MCAsmBackend {
const MCValue &Target, uint64_t &FixedValue,
bool IsResolved) override;

void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
const MCValue &Target, MutableArrayRef<char> Data,
uint64_t Value, bool IsResolved,
const MCSubtargetInfo *STI) const override;
void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
bool IsResolved) override;

// Return Size with extra Nop Bytes for alignment directive in code section.
bool shouldInsertExtraNopBytesForCodeAlign(const MCAlignFragment &AF,
Expand Down
Loading
Loading