Skip to content

Commit dd28915

Browse files
authored
MCAsmBackend: Merge addReloc into applyFixup (#146820)
Follow-up to #141333. Relocation generation called both addReloc and applyFixup, with the default addReloc invoking shouldForceRelocation, resulting in three virtual calls. This approach was also inflexible, as targets needing additional data required extending `shouldForceRelocation` (see #73721, resolved by #141311). This change integrates relocation handling into applyFixup, eliminating two virtual calls. The prior default addReloc is renamed to maybeAddReloc. Targets overriding addReloc now call their customized addReloc implementation.
1 parent 119705e commit dd28915

File tree

29 files changed

+115
-115
lines changed

29 files changed

+115
-115
lines changed

llvm/include/llvm/MC/MCAsmBackend.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,6 @@ class LLVM_ABI MCAsmBackend {
9191
/// Get information on a fixup kind.
9292
virtual MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const;
9393

94-
// Hook used by the default `addReloc` to check if a relocation is needed.
95-
virtual bool shouldForceRelocation(const MCFixup &, const MCValue &) {
96-
return false;
97-
}
98-
9994
/// Hook to check if extra nop bytes must be inserted for alignment directive.
10095
/// For some targets this may be necessary in order to support linker
10196
/// relaxation. The number of bytes to insert are returned in Size.
@@ -116,9 +111,10 @@ class LLVM_ABI MCAsmBackend {
116111
llvm_unreachable("Need to implement hook if target has custom fixups");
117112
}
118113

119-
virtual bool addReloc(const MCFragment &, const MCFixup &, const MCValue &,
120-
uint64_t &FixedValue, bool IsResolved);
114+
void maybeAddReloc(const MCFragment &, const MCFixup &, const MCValue &,
115+
uint64_t &Value, bool IsResolved);
121116

117+
/// Determine if a relocation is required. In addition,
122118
/// Apply the \p Value for given \p Fixup into the provided data fragment, at
123119
/// the offset specified by the fixup and following the fixup kind as
124120
/// appropriate. Errors (such as an out of range fixup value) should be

llvm/lib/MC/MCAsmBackend.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,11 @@ bool MCAsmBackend::fixupNeedsRelaxationAdvanced(const MCFixup &Fixup,
116116
return fixupNeedsRelaxation(Fixup, Value);
117117
}
118118

119-
bool MCAsmBackend::addReloc(const MCFragment &F, const MCFixup &Fixup,
120-
const MCValue &Target, uint64_t &FixedValue,
121-
bool IsResolved) {
122-
if (IsResolved && shouldForceRelocation(Fixup, Target))
123-
IsResolved = false;
119+
void MCAsmBackend::maybeAddReloc(const MCFragment &F, const MCFixup &Fixup,
120+
const MCValue &Target, uint64_t &Value,
121+
bool IsResolved) {
124122
if (!IsResolved)
125-
Asm->getWriter().recordRelocation(F, Fixup, Target, FixedValue);
126-
return IsResolved;
123+
Asm->getWriter().recordRelocation(F, Fixup, Target, Value);
127124
}
128125

129126
bool MCAsmBackend::isDarwinCanonicalPersonality(const MCSymbol *Sym) const {

llvm/lib/MC/MCAssembler.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,6 @@ bool MCAssembler::evaluateFixup(const MCFragment &F, const MCFixup &Fixup,
202202

203203
if (IsResolved && mc::isRelocRelocation(Fixup.getKind()))
204204
IsResolved = false;
205-
IsResolved = getBackend().addReloc(F, Fixup, Target, Value, IsResolved);
206205
getBackend().applyFixup(F, Fixup, Target, Contents, Value, IsResolved);
207206
return true;
208207
}

llvm/lib/Target/AArch64/MCTargetDesc/AArch64AsmBackend.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ class AArch64AsmBackend : public MCAsmBackend {
9494

9595
unsigned getFixupKindContainereSizeInBytes(unsigned Kind) const;
9696

97-
bool shouldForceRelocation(const MCFixup &Fixup,
98-
const MCValue &Target) override;
97+
bool shouldForceRelocation(const MCFixup &Fixup, const MCValue &Target);
9998
};
10099

101100
} // end anonymous namespace
@@ -412,10 +411,13 @@ unsigned AArch64AsmBackend::getFixupKindContainereSizeInBytes(unsigned Kind) con
412411
}
413412
}
414413

415-
void AArch64AsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
414+
void AArch64AsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
416415
const MCValue &Target,
417416
MutableArrayRef<char> Data, uint64_t Value,
418417
bool IsResolved) {
418+
if (IsResolved && shouldForceRelocation(Fixup, Target))
419+
IsResolved = false;
420+
maybeAddReloc(F, Fixup, Target, Value, IsResolved);
419421
MCFixupKind Kind = Fixup.getKind();
420422
if (mc::isRelocation(Kind))
421423
return;

llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUAsmBackend.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class AMDGPUAsmBackend : public MCAsmBackend {
5050

5151
std::optional<MCFixupKind> getFixupKind(StringRef Name) const override;
5252
MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const override;
53-
bool shouldForceRelocation(const MCFixup &, const MCValue &) override;
53+
bool shouldForceRelocation(const MCFixup &, const MCValue &);
5454
};
5555

5656
} //End anonymous namespace
@@ -130,10 +130,13 @@ static uint64_t adjustFixupValue(const MCFixup &Fixup, uint64_t Value,
130130
}
131131
}
132132

133-
void AMDGPUAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
133+
void AMDGPUAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
134134
const MCValue &Target,
135135
MutableArrayRef<char> Data, uint64_t Value,
136136
bool IsResolved) {
137+
if (IsResolved && shouldForceRelocation(Fixup, Target))
138+
IsResolved = false;
139+
maybeAddReloc(F, Fixup, Target, Value, IsResolved);
137140
if (mc::isRelocation(Fixup.getKind()))
138141
return;
139142

llvm/lib/Target/ARM/MCTargetDesc/ARMAsmBackend.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,9 @@ void ARMAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
11281128
const MCValue &Target,
11291129
MutableArrayRef<char> Data, uint64_t Value,
11301130
bool IsResolved) {
1131+
if (IsResolved && shouldForceRelocation(Fixup, Target))
1132+
IsResolved = false;
1133+
maybeAddReloc(F, Fixup, Target, Value, IsResolved);
11311134
auto Kind = Fixup.getKind();
11321135
if (mc::isRelocation(Kind))
11331136
return;

llvm/lib/Target/ARM/MCTargetDesc/ARMAsmBackend.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ class ARMAsmBackend : public MCAsmBackend {
3030

3131
MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const override;
3232

33-
bool shouldForceRelocation(const MCFixup &Fixup,
34-
const MCValue &Target) override;
33+
bool shouldForceRelocation(const MCFixup &Fixup, const MCValue &Target);
3534

3635
unsigned adjustFixupValue(const MCAssembler &Asm, const MCFixup &Fixup,
3736
const MCValue &Target, uint64_t Value,

llvm/lib/Target/AVR/MCTargetDesc/AVRAsmBackend.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -368,26 +368,21 @@ AVRAsmBackend::createObjectTargetWriter() const {
368368
return createAVRELFObjectWriter(MCELFObjectTargetWriter::getOSABI(OSType));
369369
}
370370

371-
bool AVRAsmBackend::addReloc(const MCFragment &F, const MCFixup &Fixup,
372-
const MCValue &Target, uint64_t &FixedValue,
373-
bool IsResolved) {
371+
void AVRAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
372+
const MCValue &Target,
373+
MutableArrayRef<char> Data, uint64_t Value,
374+
bool IsResolved) {
374375
// AVR sets the fixup value to bypass the assembly time overflow with a
375376
// relocation.
376377
if (IsResolved) {
377-
auto TargetVal = MCValue::get(Target.getAddSym(), Target.getSubSym(),
378-
FixedValue, Target.getSpecifier());
378+
auto TargetVal = MCValue::get(Target.getAddSym(), Target.getSubSym(), Value,
379+
Target.getSpecifier());
379380
if (forceRelocation(F, Fixup, TargetVal))
380381
IsResolved = false;
381382
}
382383
if (!IsResolved)
383-
Asm->getWriter().recordRelocation(F, Fixup, Target, FixedValue);
384-
return IsResolved;
385-
}
384+
Asm->getWriter().recordRelocation(F, Fixup, Target, Value);
386385

387-
void AVRAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
388-
const MCValue &Target,
389-
MutableArrayRef<char> Data, uint64_t Value,
390-
bool IsResolved) {
391386
if (mc::isRelocation(Fixup.getKind()))
392387
return;
393388
adjustFixupValue(Fixup, Target, Value, &getContext());

llvm/lib/Target/AVR/MCTargetDesc/AVRAsmBackend.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ class AVRAsmBackend : public MCAsmBackend {
3737
std::unique_ptr<MCObjectTargetWriter>
3838
createObjectTargetWriter() const override;
3939

40-
bool addReloc(const MCFragment &, const MCFixup &, const MCValue &,
41-
uint64_t &FixedValue, bool IsResolved) override;
42-
4340
void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,
4441
MutableArrayRef<char> Data, uint64_t Value,
4542
bool IsResolved) override;

llvm/lib/Target/BPF/MCTargetDesc/BPFAsmBackend.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,11 @@ bool BPFAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
6666
return true;
6767
}
6868

69-
void BPFAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
69+
void BPFAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
7070
const MCValue &Target,
7171
MutableArrayRef<char> Data, uint64_t Value,
7272
bool IsResolved) {
73+
maybeAddReloc(F, Fixup, Target, Value, IsResolved);
7374
if (Fixup.getKind() == FK_SecRel_8) {
7475
// The Value is 0 for global variables, and the in-section offset
7576
// for static variables. Write to the immediate field of the inst.

llvm/lib/Target/CSKY/MCTargetDesc/CSKYAsmBackend.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,14 @@ bool CSKYAsmBackend::fixupNeedsRelaxationAdvanced(const MCFixup &Fixup,
188188
}
189189
}
190190

191-
void CSKYAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
191+
void CSKYAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
192192
const MCValue &Target,
193193
MutableArrayRef<char> Data, uint64_t Value,
194194
bool IsResolved) {
195+
if (IsResolved && shouldForceRelocation(Fixup, Target))
196+
IsResolved = false;
197+
maybeAddReloc(F, Fixup, Target, Value, IsResolved);
198+
195199
MCFixupKind Kind = Fixup.getKind();
196200
if (mc::isRelocation(Kind))
197201
return;

llvm/lib/Target/CSKY/MCTargetDesc/CSKYAsmBackend.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ class CSKYAsmBackend : public MCAsmBackend {
4343
bool writeNopData(raw_ostream &OS, uint64_t Count,
4444
const MCSubtargetInfo *STI) const override;
4545

46-
bool shouldForceRelocation(const MCFixup &Fixup,
47-
const MCValue &Target) override;
46+
bool shouldForceRelocation(const MCFixup &Fixup, const MCValue &Target);
4847

4948
std::unique_ptr<MCObjectTargetWriter>
5049
createObjectTargetWriter() const override;

llvm/lib/Target/Hexagon/MCTargetDesc/HexagonAsmBackend.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,7 @@ class HexagonAsmBackend : public MCAsmBackend {
198198
return Infos[Kind - FirstTargetFixupKind];
199199
}
200200

201-
bool shouldForceRelocation(const MCFixup &Fixup,
202-
const MCValue &Target) override {
201+
bool shouldForceRelocation(const MCFixup &Fixup, const MCValue &Target) {
203202
switch(Fixup.getTargetKind()) {
204203
default:
205204
llvm_unreachable("Unknown Fixup Kind!");
@@ -653,10 +652,13 @@ class HexagonAsmBackend : public MCAsmBackend {
653652

654653
} // namespace
655654

656-
void HexagonAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
655+
void HexagonAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
657656
const MCValue &Target,
658657
MutableArrayRef<char> Data,
659658
uint64_t FixupValue, bool IsResolved) {
659+
if (IsResolved && shouldForceRelocation(Fixup, Target))
660+
IsResolved = false;
661+
maybeAddReloc(F, Fixup, Target, FixupValue, IsResolved);
660662
// When FixupValue is 0 the relocation is external and there
661663
// is nothing for us to do.
662664
if (!FixupValue)

llvm/lib/Target/Lanai/MCTargetDesc/LanaiAsmBackend.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "llvm/MC/MCFixupKindInfo.h"
1515
#include "llvm/MC/MCObjectWriter.h"
1616
#include "llvm/MC/MCSubtargetInfo.h"
17+
#include "llvm/MC/MCValue.h"
1718
#include "llvm/Support/ErrorHandling.h"
1819
#include "llvm/Support/raw_ostream.h"
1920

@@ -71,13 +72,15 @@ bool LanaiAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
7172
return true;
7273
}
7374

74-
void LanaiAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
75+
void LanaiAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
7576
const MCValue &Target,
7677
MutableArrayRef<char> Data, uint64_t Value,
77-
bool) {
78+
bool IsResolved) {
79+
if (!IsResolved)
80+
Asm->getWriter().recordRelocation(F, Fixup, Target, Value);
81+
7882
MCFixupKind Kind = Fixup.getKind();
7983
Value = adjustFixupValue(static_cast<unsigned>(Kind), Value);
80-
8184
if (!Value)
8285
return; // This value doesn't change the encoding
8386

llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,13 @@ static void fixupLeb128(MCContext &Ctx, const MCFixup &Fixup,
140140
Ctx.reportError(Fixup.getLoc(), "Invalid uleb128 value!");
141141
}
142142

143-
void LoongArchAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
143+
void LoongArchAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
144144
const MCValue &Target,
145145
MutableArrayRef<char> Data, uint64_t Value,
146146
bool IsResolved) {
147+
if (IsResolved && shouldForceRelocation(Fixup, Target))
148+
IsResolved = false;
149+
IsResolved = addReloc(F, Fixup, Target, Value, IsResolved);
147150
if (!Value)
148151
return; // Doesn't change encoding.
149152

@@ -453,7 +456,8 @@ bool LoongArchAsmBackend::addReloc(const MCFragment &F, const MCFixup &Fixup,
453456
const MCValue &Target, uint64_t &FixedValue,
454457
bool IsResolved) {
455458
auto Fallback = [&]() {
456-
return MCAsmBackend::addReloc(F, Fixup, Target, FixedValue, IsResolved);
459+
MCAsmBackend::maybeAddReloc(F, Fixup, Target, FixedValue, IsResolved);
460+
return true;
457461
};
458462
uint64_t FixedValueA, FixedValueB;
459463
if (Target.getSubSym()) {

llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class LoongArchAsmBackend : public MCAsmBackend {
4040
const MCTargetOptions &Options);
4141

4242
bool addReloc(const MCFragment &, const MCFixup &, const MCValue &,
43-
uint64_t &FixedValue, bool IsResolved) override;
43+
uint64_t &FixedValue, bool IsResolved);
4444

4545
void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,
4646
MutableArrayRef<char> Data, uint64_t Value,
@@ -54,8 +54,7 @@ class LoongArchAsmBackend : public MCAsmBackend {
5454
bool shouldInsertFixupForCodeAlign(MCAssembler &Asm,
5555
MCAlignFragment &AF) override;
5656

57-
bool shouldForceRelocation(const MCFixup &Fixup,
58-
const MCValue &Target) override;
57+
bool shouldForceRelocation(const MCFixup &Fixup, const MCValue &Target);
5958

6059
std::optional<MCFixupKind> getFixupKind(StringRef Name) const override;
6160

llvm/lib/Target/M68k/MCTargetDesc/M68kAsmBackend.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "llvm/BinaryFormat/ELF.h"
1919
#include "llvm/BinaryFormat/MachO.h"
2020
#include "llvm/MC/MCAsmBackend.h"
21+
#include "llvm/MC/MCAssembler.h"
2122
#include "llvm/MC/MCELFObjectWriter.h"
2223
#include "llvm/MC/MCExpr.h"
2324
#include "llvm/MC/MCFixupKindInfo.h"
@@ -77,11 +78,14 @@ class M68kAsmBackend : public MCAsmBackend {
7778
};
7879
} // end anonymous namespace
7980

80-
void M68kAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
81-
const MCValue &, MutableArrayRef<char> Data,
82-
uint64_t Value, bool) {
83-
unsigned Size = 1 << getFixupKindLog2Size(Fixup.getKind());
81+
void M68kAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
82+
const MCValue &Target,
83+
MutableArrayRef<char> Data, uint64_t Value,
84+
bool IsResolved) {
85+
if (!IsResolved)
86+
Asm->getWriter().recordRelocation(F, Fixup, Target, Value);
8487

88+
unsigned Size = 1 << getFixupKindLog2Size(Fixup.getKind());
8589
assert(Fixup.getOffset() + Size <= Data.size() && "Invalid fixup offset!");
8690
// Check that uppper bits are either all zeros or all ones.
8791
// Specifically ignore overflow/underflow as long as the leakage is

llvm/lib/Target/MSP430/MCTargetDesc/MSP430AsmBackend.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,11 @@ uint64_t MSP430AsmBackend::adjustFixupValue(const MCFixup &Fixup,
103103
}
104104
}
105105

106-
void MSP430AsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
106+
void MSP430AsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
107107
const MCValue &Target,
108108
MutableArrayRef<char> Data, uint64_t Value,
109109
bool IsResolved) {
110+
maybeAddReloc(F, Fixup, Target, Value, IsResolved);
110111
Value = adjustFixupValue(Fixup, Value, getContext());
111112
MCFixupKindInfo Info = getFixupKindInfo(Fixup.getKind());
112113
if (!Value)

llvm/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,13 @@ static unsigned calculateMMLEIndex(unsigned i) {
242242
/// ApplyFixup - Apply the \p Value for given \p Fixup into the provided
243243
/// data fragment, at the offset specified by the fixup and following the
244244
/// fixup kind as appropriate.
245-
void MipsAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
245+
void MipsAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
246246
const MCValue &Target,
247247
MutableArrayRef<char> Data, uint64_t Value,
248248
bool IsResolved) {
249+
if (IsResolved && shouldForceRelocation(Fixup, Target))
250+
IsResolved = false;
251+
maybeAddReloc(F, Fixup, Target, Value, IsResolved);
249252
MCFixupKind Kind = Fixup.getKind();
250253
MCContext &Ctx = getContext();
251254
Value = adjustFixupValue(Fixup, Value, Ctx);

llvm/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ class MipsAsmBackend : public MCAsmBackend {
4949
bool writeNopData(raw_ostream &OS, uint64_t Count,
5050
const MCSubtargetInfo *STI) const override;
5151

52-
bool shouldForceRelocation(const MCFixup &Fixup,
53-
const MCValue &Target) override;
52+
bool shouldForceRelocation(const MCFixup &Fixup, const MCValue &Target);
5453
}; // class MipsAsmBackend
5554

5655
} // namespace

0 commit comments

Comments
 (0)