Skip to content

Commit 9b6feeb

Browse files
committed
MCAsmBackend: Simplify applyFixup
Remove the MCSubtargetInfo argument from applyFixup, introduced in https://reviews.llvm.org/D45962 , as it's only required by ARM. Instead, add const MCFragment & so that ARMAsmBackend can retrieve MCSubtargetInfo via a static member function. Additionally, make applyReloc non-const. Its arguments now fully cover addReloc's functionality.
1 parent f0ff2be commit 9b6feeb

File tree

32 files changed

+128
-149
lines changed

32 files changed

+128
-149
lines changed

llvm/include/llvm/MC/MCAsmBackend.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,9 @@ class MCAsmBackend {
123123
/// the offset specified by the fixup and following the fixup kind as
124124
/// appropriate. Errors (such as an out of range fixup value) should be
125125
/// reported via \p Ctx.
126-
/// The \p STI is present only for fragments of type MCRelaxableFragment and
127-
/// MCDataFragment with hasInstructions() == true.
128-
virtual void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
126+
virtual void applyFixup(MCAssembler &, const MCFragment &, const MCFixup &,
129127
const MCValue &Target, MutableArrayRef<char> Data,
130-
uint64_t Value, bool IsResolved,
131-
const MCSubtargetInfo *STI) const = 0;
128+
uint64_t Value, bool IsResolved) = 0;
132129

133130
/// @}
134131

@@ -216,6 +213,10 @@ class MCAsmBackend {
216213

217214
bool isDarwinCanonicalPersonality(const MCSymbol *Sym) const;
218215

216+
// Return STI for fragments of type MCRelaxableFragment and MCDataFragment
217+
// with hasInstructions() == true.
218+
static const MCSubtargetInfo *getSubtargetInfo(const MCFragment &F);
219+
219220
private:
220221
const bool LinkerRelaxation;
221222
};

llvm/include/llvm/MC/MCAssembler.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,15 @@ class MCAssembler {
9090
/// Evaluate a fixup to a relocatable expression and the value which should be
9191
/// placed into the fixup.
9292
///
93+
/// \param F The fragment the fixup is inside.
9394
/// \param Fixup The fixup to evaluate.
94-
/// \param DF The fragment the fixup is inside.
9595
/// \param Target [out] On return, the relocatable expression the fixup
9696
/// evaluates to.
9797
/// \param Value [out] On return, the value of the fixup as currently laid
9898
/// out.
9999
/// \param RecordReloc Record relocation if needed.
100100
/// relocation.
101-
bool evaluateFixup(const MCFixup &Fixup, const MCFragment *DF,
102-
MCValue &Target, const MCSubtargetInfo *STI,
101+
bool evaluateFixup(const MCFragment *F, const MCFixup &Fixup, MCValue &Target,
103102
uint64_t &Value, bool RecordReloc,
104103
MutableArrayRef<char> Contents) const;
105104

llvm/lib/MC/MCAsmBackend.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,24 @@ bool MCAsmBackend::isDarwinCanonicalPersonality(const MCSymbol *Sym) const {
143143
// Reserving an empty slot for it seems silly.
144144
return name == "___gxx_personality_v0" || name == "___objc_personality_v0";
145145
}
146+
147+
const MCSubtargetInfo *MCAsmBackend::getSubtargetInfo(const MCFragment &F) {
148+
const MCSubtargetInfo *STI = nullptr;
149+
switch (F.getKind()) {
150+
case MCFragment::FT_Data: {
151+
auto &DF = cast<MCDataFragment>(F);
152+
STI = DF.getSubtargetInfo();
153+
assert(!DF.hasInstructions() || STI != nullptr);
154+
break;
155+
}
156+
case MCFragment::FT_Relaxable: {
157+
auto &RF = cast<MCRelaxableFragment>(F);
158+
STI = RF.getSubtargetInfo();
159+
assert(!RF.hasInstructions() || STI != nullptr);
160+
break;
161+
}
162+
default:
163+
break;
164+
}
165+
return STI;
166+
}

llvm/lib/MC/MCAssembler.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ bool MCAssembler::isThumbFunc(const MCSymbol *Symbol) const {
136136
return true;
137137
}
138138

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

@@ -200,8 +200,8 @@ bool MCAssembler::evaluateFixup(const MCFixup &Fixup, const MCFragment *DF,
200200
IsResolved = false;
201201
IsResolved = getBackend().addReloc(const_cast<MCAssembler &>(*this), *DF,
202202
Fixup, Target, Value, IsResolved);
203-
getBackend().applyFixup(*this, Fixup, Target, Contents, Value, IsResolved,
204-
STI);
203+
getBackend().applyFixup(const_cast<MCAssembler &>(*this), *DF, Fixup, Target,
204+
Contents, Value, IsResolved);
205205
return true;
206206
}
207207

@@ -906,7 +906,6 @@ void MCAssembler::layout() {
906906
for (MCFragment &Frag : Sec) {
907907
MutableArrayRef<MCFixup> Fixups;
908908
MutableArrayRef<char> Contents;
909-
const MCSubtargetInfo *STI = nullptr;
910909

911910
// Process MCAlignFragment and MCEncodedFragmentWithFixups here.
912911
switch (Frag.getKind()) {
@@ -924,16 +923,12 @@ void MCAssembler::layout() {
924923
MCDataFragment &DF = cast<MCDataFragment>(Frag);
925924
Fixups = DF.getFixups();
926925
Contents = DF.getContents();
927-
STI = DF.getSubtargetInfo();
928-
assert(!DF.hasInstructions() || STI != nullptr);
929926
break;
930927
}
931928
case MCFragment::FT_Relaxable: {
932929
MCRelaxableFragment &RF = cast<MCRelaxableFragment>(Frag);
933930
Fixups = RF.getFixups();
934931
Contents = RF.getContents();
935-
STI = RF.getSubtargetInfo();
936-
assert(!RF.hasInstructions() || STI != nullptr);
937932
break;
938933
}
939934
case MCFragment::FT_CVDefRange: {
@@ -970,7 +965,7 @@ void MCAssembler::layout() {
970965
for (const MCFixup &Fixup : Fixups) {
971966
uint64_t FixedValue;
972967
MCValue Target;
973-
evaluateFixup(Fixup, &Frag, Target, STI, FixedValue,
968+
evaluateFixup(&Frag, Fixup, Target, FixedValue,
974969
/*RecordReloc=*/true, Contents);
975970
}
976971
}
@@ -991,9 +986,8 @@ bool MCAssembler::fixupNeedsRelaxation(const MCFixup &Fixup,
991986
assert(getBackendPtr() && "Expected assembler backend");
992987
MCValue Target;
993988
uint64_t Value;
994-
bool Resolved =
995-
evaluateFixup(const_cast<MCFixup &>(Fixup), DF, Target,
996-
DF->getSubtargetInfo(), Value, /*RecordReloc=*/false, {});
989+
bool Resolved = evaluateFixup(DF, const_cast<MCFixup &>(Fixup), Target, Value,
990+
/*RecordReloc=*/false, {});
997991
return getBackend().fixupNeedsRelaxationAdvanced(*this, Fixup, Target, Value,
998992
Resolved);
999993
}

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,9 @@ class AArch64AsmBackend : public MCAsmBackend {
8181
return Infos[Kind - FirstTargetFixupKind];
8282
}
8383

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

8988
bool fixupNeedsRelaxation(const MCFixup &Fixup,
9089
uint64_t Value) const override;
@@ -415,11 +414,10 @@ unsigned AArch64AsmBackend::getFixupKindContainereSizeInBytes(unsigned Kind) con
415414
}
416415
}
417416

418-
void AArch64AsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
419-
const MCValue &Target,
417+
void AArch64AsmBackend::applyFixup(MCAssembler &Asm, const MCFragment &,
418+
const MCFixup &Fixup, const MCValue &Target,
420419
MutableArrayRef<char> Data, uint64_t Value,
421-
bool IsResolved,
422-
const MCSubtargetInfo *STI) const {
420+
bool IsResolved) {
423421
MCFixupKind Kind = Fixup.getKind();
424422
if (mc::isRelocation(Kind))
425423
return;

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,9 @@ class AMDGPUAsmBackend : public MCAsmBackend {
3232
public:
3333
AMDGPUAsmBackend(const Target &T) : MCAsmBackend(llvm::endianness::little) {}
3434

35-
36-
void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
35+
void applyFixup(MCAssembler &, const MCFragment &, const MCFixup &,
3736
const MCValue &Target, MutableArrayRef<char> Data,
38-
uint64_t Value, bool IsResolved,
39-
const MCSubtargetInfo *STI) const override;
37+
uint64_t Value, bool IsResolved) override;
4038
bool fixupNeedsRelaxation(const MCFixup &Fixup,
4139
uint64_t Value) const override;
4240

@@ -133,11 +131,10 @@ static uint64_t adjustFixupValue(const MCFixup &Fixup, uint64_t Value,
133131
}
134132
}
135133

136-
void AMDGPUAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
137-
const MCValue &Target,
134+
void AMDGPUAsmBackend::applyFixup(MCAssembler &Asm, const MCFragment &,
135+
const MCFixup &Fixup, const MCValue &Target,
138136
MutableArrayRef<char> Data, uint64_t Value,
139-
bool IsResolved,
140-
const MCSubtargetInfo *STI) const {
137+
bool IsResolved) {
141138
if (mc::isRelocation(Fixup.getKind()))
142139
return;
143140

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,16 +1127,16 @@ static unsigned getFixupKindContainerSizeBytes(unsigned Kind) {
11271127
}
11281128
}
11291129

1130-
void ARMAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
1131-
const MCValue &Target,
1130+
void ARMAsmBackend::applyFixup(MCAssembler &Asm, const MCFragment &F,
1131+
const MCFixup &Fixup, const MCValue &Target,
11321132
MutableArrayRef<char> Data, uint64_t Value,
1133-
bool IsResolved,
1134-
const MCSubtargetInfo* STI) const {
1133+
bool IsResolved) {
11351134
auto Kind = Fixup.getKind();
11361135
if (mc::isRelocation(Kind))
11371136
return;
11381137
MCContext &Ctx = Asm.getContext();
1139-
Value = adjustFixupValue(Asm, Fixup, Target, Value, IsResolved, Ctx, STI);
1138+
Value = adjustFixupValue(Asm, Fixup, Target, Value, IsResolved, Ctx,
1139+
getSubtargetInfo(F));
11401140
if (!Value)
11411141
return; // Doesn't change encoding.
11421142
const unsigned NumBytes = getFixupKindNumBytes(Kind);

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,9 @@ class ARMAsmBackend : public MCAsmBackend {
3838
bool IsResolved, MCContext &Ctx,
3939
const MCSubtargetInfo *STI) const;
4040

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

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

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -384,11 +384,10 @@ bool AVRAsmBackend::addReloc(MCAssembler &Asm, const MCFragment &F,
384384
return IsResolved;
385385
}
386386

387-
void AVRAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
388-
const MCValue &Target,
387+
void AVRAsmBackend::applyFixup(MCAssembler &Asm, const MCFragment &,
388+
const MCFixup &Fixup, const MCValue &Target,
389389
MutableArrayRef<char> Data, uint64_t Value,
390-
bool IsResolved,
391-
const MCSubtargetInfo *STI) const {
390+
bool IsResolved) {
392391
if (mc::isRelocation(Fixup.getKind()))
393392
return;
394393
adjustFixupValue(Fixup, Target, Value, &Asm.getContext());

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,9 @@ class AVRAsmBackend : public MCAsmBackend {
4141
const MCValue &Target, uint64_t &FixedValue,
4242
bool IsResolved) override;
4343

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

4948
std::optional<MCFixupKind> getFixupKind(StringRef Name) const override;
5049
MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const override;

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ class BPFAsmBackend : public MCAsmBackend {
2727
BPFAsmBackend(llvm::endianness Endian) : MCAsmBackend(Endian) {}
2828
~BPFAsmBackend() override = default;
2929

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

3534
std::unique_ptr<MCObjectTargetWriter>
3635
createObjectTargetWriter() const override;
@@ -67,11 +66,10 @@ bool BPFAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
6766
return true;
6867
}
6968

70-
void BPFAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
71-
const MCValue &Target,
69+
void BPFAsmBackend::applyFixup(MCAssembler &Asm, const MCFragment &,
70+
const MCFixup &Fixup, const MCValue &Target,
7271
MutableArrayRef<char> Data, uint64_t Value,
73-
bool IsResolved,
74-
const MCSubtargetInfo *STI) const {
72+
bool IsResolved) {
7573
if (Fixup.getKind() == FK_SecRel_8) {
7674
// The Value is 0 for global variables, and the in-section offset
7775
// for static variables. Write to the immediate field of the inst.

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,10 @@ bool CSKYAsmBackend::fixupNeedsRelaxationAdvanced(const MCAssembler &,
189189
}
190190
}
191191

192-
void CSKYAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
193-
const MCValue &Target,
192+
void CSKYAsmBackend::applyFixup(MCAssembler &Asm, const MCFragment &,
193+
const MCFixup &Fixup, const MCValue &Target,
194194
MutableArrayRef<char> Data, uint64_t Value,
195-
bool IsResolved,
196-
const MCSubtargetInfo *STI) const {
195+
bool IsResolved) {
197196
MCFixupKind Kind = Fixup.getKind();
198197
if (mc::isRelocation(Kind))
199198
return;

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,9 @@ class CSKYAsmBackend : public MCAsmBackend {
2222
CSKYAsmBackend(const MCSubtargetInfo &STI, const MCTargetOptions &OP)
2323
: MCAsmBackend(llvm::endianness::little) {}
2424

25-
26-
void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
25+
void applyFixup(MCAssembler &, const MCFragment &, const MCFixup &,
2726
const MCValue &Target, MutableArrayRef<char> Data,
28-
uint64_t Value, bool IsResolved,
29-
const MCSubtargetInfo *STI) const override;
27+
uint64_t Value, bool IsResolved) override;
3028

3129
MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const override;
3230

llvm/lib/Target/DirectX/MCTargetDesc/DirectXMCTargetDesc.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,9 @@ class DXILAsmBackend : public MCAsmBackend {
7777
: MCAsmBackend(llvm::endianness::little) {}
7878
~DXILAsmBackend() override = default;
7979

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

8584
std::unique_ptr<MCObjectTargetWriter>
8685
createObjectTargetWriter() const override {

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,10 +407,9 @@ class HexagonAsmBackend : public MCAsmBackend {
407407
/// ApplyFixup - Apply the \arg Value for given \arg Fixup into the provided
408408
/// data fragment, at the offset specified by the fixup and following the
409409
/// fixup kind as appropriate.
410-
void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
410+
void applyFixup(MCAssembler &Asm, const MCFragment &, const MCFixup &Fixup,
411411
const MCValue &Target, MutableArrayRef<char> Data,
412-
uint64_t FixupValue, bool IsResolved,
413-
const MCSubtargetInfo *STI) const override {
412+
uint64_t FixupValue, bool IsResolved) override {
414413

415414
// When FixupValue is 0 the relocation is external and there
416415
// is nothing for us to do.

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,9 @@ class LanaiAsmBackend : public MCAsmBackend {
4747
LanaiAsmBackend(const Target &T, Triple::OSType OST)
4848
: MCAsmBackend(llvm::endianness::big), OSType(OST) {}
4949

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

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

75-
void LanaiAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
76-
const MCValue &Target,
74+
void LanaiAsmBackend::applyFixup(MCAssembler &Asm, const MCFragment &,
75+
const MCFixup &Fixup, const MCValue &Target,
7776
MutableArrayRef<char> Data, uint64_t Value,
78-
bool /*IsResolved*/,
79-
const MCSubtargetInfo * /*STI*/) const {
77+
bool) {
8078
MCFixupKind Kind = Fixup.getKind();
8179
Value = adjustFixupValue(static_cast<unsigned>(Kind), Value);
8280

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

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

143-
void LoongArchAsmBackend::applyFixup(const MCAssembler &Asm,
143+
void LoongArchAsmBackend::applyFixup(MCAssembler &Asm, const MCFragment &,
144144
const MCFixup &Fixup,
145145
const MCValue &Target,
146146
MutableArrayRef<char> Data, uint64_t Value,
147-
bool IsResolved,
148-
const MCSubtargetInfo *STI) const {
147+
bool IsResolved) {
149148
if (!Value)
150149
return; // Doesn't change encoding.
151150

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,9 @@ class LoongArchAsmBackend : public MCAsmBackend {
3939
const MCValue &Target, uint64_t &FixedValue,
4040
bool IsResolved) override;
4141

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

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

0 commit comments

Comments
 (0)