Skip to content

Commit 1bf172a

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, remove the MCAssembler argument, which is also only required by ARM. Additionally, make applyReloc non-const. Its arguments now fully cover addReloc's functionality.
1 parent 84f06b8 commit 1bf172a

File tree

32 files changed

+129
-157
lines changed

32 files changed

+129
-157
lines changed

llvm/include/llvm/MC/MCAsmBackend.h

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

139136
/// @}
140137

@@ -222,6 +219,10 @@ class MCAsmBackend {
222219

223220
bool isDarwinCanonicalPersonality(const MCSymbol *Sym) const;
224221

222+
// Return STI for fragments of type MCRelaxableFragment and MCDataFragment
223+
// with hasInstructions() == true.
224+
static const MCSubtargetInfo *getSubtargetInfo(const MCFragment &F);
225+
225226
private:
226227
const bool LinkerRelaxation;
227228
};

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
@@ -145,3 +145,24 @@ bool MCAsmBackend::isDarwinCanonicalPersonality(const MCSymbol *Sym) const {
145145
// Reserving an empty slot for it seems silly.
146146
return name == "___gxx_personality_v0" || name == "___objc_personality_v0";
147147
}
148+
149+
const MCSubtargetInfo *MCAsmBackend::getSubtargetInfo(const MCFragment &F) {
150+
const MCSubtargetInfo *STI = nullptr;
151+
switch (F.getKind()) {
152+
case MCFragment::FT_Data: {
153+
auto &DF = cast<MCDataFragment>(F);
154+
STI = DF.getSubtargetInfo();
155+
assert(!DF.hasInstructions() || STI != nullptr);
156+
break;
157+
}
158+
case MCFragment::FT_Relaxable: {
159+
auto &RF = cast<MCRelaxableFragment>(F);
160+
STI = RF.getSubtargetInfo();
161+
assert(!RF.hasInstructions() || STI != nullptr);
162+
break;
163+
}
164+
default:
165+
break;
166+
}
167+
return STI;
168+
}

llvm/lib/MC/MCAssembler.cpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,9 @@ bool MCAssembler::isThumbFunc(const MCSymbol *Symbol) const {
139139
return true;
140140
}
141141

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

@@ -203,8 +203,7 @@ bool MCAssembler::evaluateFixup(const MCFixup &Fixup, const MCFragment *DF,
203203
IsResolved = false;
204204
IsResolved = getBackend().addReloc(const_cast<MCAssembler &>(*this), *DF,
205205
Fixup, Target, Value, IsResolved);
206-
getBackend().applyFixup(*this, Fixup, Target, Contents, Value, IsResolved,
207-
STI);
206+
getBackend().applyFixup(*DF, Fixup, Target, Contents, Value, IsResolved);
208207
return true;
209208
}
210209

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

914912
// Process MCAlignFragment and MCEncodedFragmentWithFixups here.
915913
switch (Frag.getKind()) {
@@ -927,16 +925,12 @@ void MCAssembler::layout() {
927925
MCDataFragment &DF = cast<MCDataFragment>(Frag);
928926
Fixups = DF.getFixups();
929927
Contents = DF.getContents();
930-
STI = DF.getSubtargetInfo();
931-
assert(!DF.hasInstructions() || STI != nullptr);
932928
break;
933929
}
934930
case MCFragment::FT_Relaxable: {
935931
MCRelaxableFragment &RF = cast<MCRelaxableFragment>(Frag);
936932
Fixups = RF.getFixups();
937933
Contents = RF.getContents();
938-
STI = RF.getSubtargetInfo();
939-
assert(!RF.hasInstructions() || STI != nullptr);
940934
break;
941935
}
942936
case MCFragment::FT_CVDefRange: {
@@ -973,7 +967,7 @@ void MCAssembler::layout() {
973967
for (const MCFixup &Fixup : Fixups) {
974968
uint64_t FixedValue;
975969
MCValue Target;
976-
evaluateFixup(Fixup, &Frag, Target, STI, FixedValue,
970+
evaluateFixup(&Frag, Fixup, Target, FixedValue,
977971
/*RecordReloc=*/true, Contents);
978972
}
979973
}
@@ -994,9 +988,8 @@ bool MCAssembler::fixupNeedsRelaxation(const MCFixup &Fixup,
994988
assert(getBackendPtr() && "Expected assembler backend");
995989
MCValue Target;
996990
uint64_t Value;
997-
bool Resolved =
998-
evaluateFixup(const_cast<MCFixup &>(Fixup), DF, Target,
999-
DF->getSubtargetInfo(), Value, /*RecordReloc=*/false, {});
991+
bool Resolved = evaluateFixup(DF, const_cast<MCFixup &>(Fixup), Target, Value,
992+
/*RecordReloc=*/false, {});
1000993
return getBackend().fixupNeedsRelaxationAdvanced(*this, Fixup, Target, Value,
1001994
Resolved);
1002995
}

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,
85-
const MCValue &Target, MutableArrayRef<char> Data,
86-
uint64_t Value, bool IsResolved,
87-
const MCSubtargetInfo *STI) const override;
84+
void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,
85+
MutableArrayRef<char> Data, uint64_t Value,
86+
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,
417+
void AArch64AsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
419418
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,
37-
const MCValue &Target, MutableArrayRef<char> Data,
38-
uint64_t Value, bool IsResolved,
39-
const MCSubtargetInfo *STI) const override;
35+
void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,
36+
MutableArrayRef<char> Data, uint64_t Value,
37+
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,
134+
void AMDGPUAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
137135
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: 4 additions & 4 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,
1130+
void ARMAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
11311131
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 = 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: 3 additions & 4 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,
42-
const MCValue &Target, MutableArrayRef<char> Data,
43-
uint64_t Value, bool IsResolved,
44-
const MCSubtargetInfo *STI) const override;
41+
void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,
42+
MutableArrayRef<char> Data, uint64_t Value,
43+
bool IsResolved) override;
4544

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

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

Lines changed: 2 additions & 3 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,
387+
void AVRAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
388388
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, &getContext());

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

Lines changed: 3 additions & 4 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,
45-
const MCValue &Target, MutableArrayRef<char> Data,
46-
uint64_t Value, bool IsResolved,
47-
const MCSubtargetInfo *STI) const override;
44+
void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,
45+
MutableArrayRef<char> Data, uint64_t Value,
46+
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,
31-
const MCValue &Target, MutableArrayRef<char> Data,
32-
uint64_t Value, bool IsResolved,
33-
const MCSubtargetInfo *STI) const override;
30+
void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,
31+
MutableArrayRef<char> Data, uint64_t Value,
32+
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,
69+
void BPFAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
7170
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: 2 additions & 3 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,
192+
void CSKYAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
193193
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: 3 additions & 5 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,
27-
const MCValue &Target, MutableArrayRef<char> Data,
28-
uint64_t Value, bool IsResolved,
29-
const MCSubtargetInfo *STI) const override;
25+
void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,
26+
MutableArrayRef<char> Data, uint64_t Value,
27+
bool IsResolved) override;
3028

3129
MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const override;
3230

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

Lines changed: 3 additions & 4 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,
81-
const MCValue &Target, MutableArrayRef<char> Data,
82-
uint64_t Value, bool IsResolved,
83-
const MCSubtargetInfo *STI) const override {}
80+
void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,
81+
MutableArrayRef<char> Data, uint64_t Value,
82+
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(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,
51-
const MCValue &Target, MutableArrayRef<char> Data,
52-
uint64_t Value, bool IsResolved,
53-
const MCSubtargetInfo *STI) const override;
50+
void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,
51+
MutableArrayRef<char> Data, uint64_t Value,
52+
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,
74+
void LanaiAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
7675
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 & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,10 @@ 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,
144-
const MCFixup &Fixup,
143+
void LoongArchAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
145144
const MCValue &Target,
146145
MutableArrayRef<char> Data, uint64_t Value,
147-
bool IsResolved,
148-
const MCSubtargetInfo *STI) const {
146+
bool IsResolved) {
149147
if (!Value)
150148
return; // Doesn't change encoding.
151149

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

Lines changed: 3 additions & 4 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,
43-
const MCValue &Target, MutableArrayRef<char> Data,
44-
uint64_t Value, bool IsResolved,
45-
const MCSubtargetInfo *STI) const override;
42+
void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,
43+
MutableArrayRef<char> Data, uint64_t Value,
44+
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)