Skip to content

Commit f9bd89b

Browse files
committed
MCFixup: Add isRelocation/isRelocRelocation helpers
Add two helper functions to simplify checks for relocation types, replacing direct comparisons with FirstRelocationKind and FirstLiteralRelocationKind. Note: Some targets haven't utilized isRelocation yet. Also, update RelaxFixupKind to use 0 as the sentinel value.
1 parent 58a5c46 commit f9bd89b

File tree

16 files changed

+43
-28
lines changed

16 files changed

+43
-28
lines changed

llvm/include/llvm/MC/MCAsmBackend.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class raw_ostream;
4141
/// Generic interface to target specific assembler backends.
4242
class MCAsmBackend {
4343
protected: // Can only create subclasses.
44-
MCAsmBackend(llvm::endianness Endian, unsigned RelaxFixupKind = MaxFixupKind);
44+
MCAsmBackend(llvm::endianness Endian, unsigned RelaxFixupKind = 0);
4545

4646
public:
4747
MCAsmBackend(const MCAsmBackend &) = delete;
@@ -53,7 +53,7 @@ class MCAsmBackend {
5353
/// Fixup kind used for linker relaxation. Currently only used by RISC-V
5454
/// and LoongArch.
5555
const unsigned RelaxFixupKind;
56-
bool allowLinkerRelaxation() const { return RelaxFixupKind != MaxFixupKind; }
56+
bool allowLinkerRelaxation() const { return RelaxFixupKind != 0; }
5757

5858
/// Return true if this target might automatically pad instructions and thus
5959
/// need to emit padding enable/disable directives around sensative code.

llvm/include/llvm/MC/MCFixup.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,21 @@ class MCFixup {
122122
SMLoc getLoc() const { return Loc; }
123123
};
124124

125+
namespace mc {
126+
// Check if the fixup kind is a relocation type. Return false if the fixup can
127+
// be resolved without a relocation.
128+
inline bool isRelocation(MCFixupKind FixupKind) {
129+
return FixupKind >= FirstRelocationKind;
130+
}
131+
132+
// Check if the fixup kind represents a relocation type from a .reloc directive.
133+
// In ELF, this skips STT_SECTION adjustment and STT_TLS symbol type setting for
134+
// TLS relocations.
135+
inline bool isRelocRelocation(MCFixupKind FixupKind) {
136+
return FixupKind >= FirstLiteralRelocationKind;
137+
}
138+
} // namespace mc
139+
125140
} // End llvm namespace
126141

127142
#endif

llvm/lib/MC/ELFObjectWriter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,7 +1385,7 @@ void ELFObjectWriter::recordRelocation(MCAssembler &Asm,
13851385

13861386
auto EMachine = TargetObjectWriter->getEMachine();
13871387
unsigned Type;
1388-
if (Fixup.getKind() >= FirstLiteralRelocationKind)
1388+
if (mc::isRelocRelocation(Fixup.getKind()))
13891389
Type = Fixup.getKind() - FirstLiteralRelocationKind;
13901390
else
13911391
Type = TargetObjectWriter->getRelocType(Ctx, Target, Fixup, IsPCRel);
@@ -1396,7 +1396,7 @@ void ELFObjectWriter::recordRelocation(MCAssembler &Asm,
13961396
UseSectionSym = useSectionSymbol(Asm, Target, SymA, C, Type);
13971397

13981398
// Disable STT_SECTION adjustment for .reloc directives.
1399-
UseSectionSym &= Fixup.getKind() < FirstLiteralRelocationKind;
1399+
UseSectionSym &= !mc::isRelocRelocation(Fixup.getKind());
14001400
}
14011401

14021402
uint64_t Addend = UseSectionSym ? C + Asm.getSymbolOffset(*SymA) : C;

llvm/lib/MC/MCAsmStreamer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2403,7 +2403,7 @@ void MCAsmStreamer::AddEncodingComment(const MCInst &Inst,
24032403
<< "offset: " << F.getOffset() << ", value: ";
24042404
F.getValue()->print(OS, MAI);
24052405
auto Kind = F.getKind();
2406-
if (FirstRelocationKind <= Kind)
2406+
if (mc::isRelocation(Kind))
24072407
OS << ", relocation type: " << (Kind - FirstRelocationKind);
24082408
else
24092409
OS << ", kind: "

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class AArch64AsmBackend : public MCAsmBackend {
6969

7070
// Fixup kinds from raw relocation types and .reloc directives force
7171
// relocations and do not need these fields.
72-
if (Kind >= FirstRelocationKind)
72+
if (mc::isRelocation(Kind))
7373
return MCAsmBackend::getFixupKindInfo(FK_NONE);
7474

7575
if (Kind < FirstTargetFixupKind)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ void AMDGPUAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
135135
MutableArrayRef<char> Data, uint64_t Value,
136136
bool IsResolved,
137137
const MCSubtargetInfo *STI) const {
138-
if (Fixup.getKind() >= FirstLiteralRelocationKind)
138+
if (mc::isRelocation(Fixup.getKind()))
139139
return;
140140

141141
Value = adjustFixupValue(Fixup, Value, &Asm.getContext());
@@ -179,7 +179,7 @@ const MCFixupKindInfo &AMDGPUAsmBackend::getFixupKindInfo(
179179
{ "fixup_si_sopp_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
180180
};
181181

182-
if (Kind >= FirstLiteralRelocationKind)
182+
if (mc::isRelocation(Kind))
183183
return MCAsmBackend::getFixupKindInfo(FK_NONE);
184184

185185
if (Kind < FirstTargetFixupKind)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ const MCFixupKindInfo &ARMAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
193193

194194
// Fixup kinds from .reloc directive are like R_ARM_NONE. They do not require
195195
// any extra processing.
196-
if (Kind >= FirstLiteralRelocationKind)
196+
if (mc::isRelocation(Kind))
197197
return MCAsmBackend::getFixupKindInfo(FK_NONE);
198198

199199
if (Kind < FirstTargetFixupKind)
@@ -1146,8 +1146,8 @@ void ARMAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
11461146
MutableArrayRef<char> Data, uint64_t Value,
11471147
bool IsResolved,
11481148
const MCSubtargetInfo* STI) const {
1149-
unsigned Kind = Fixup.getKind();
1150-
if (Kind >= FirstLiteralRelocationKind)
1149+
auto Kind = Fixup.getKind();
1150+
if (mc::isRelocation(Kind))
11511151
return;
11521152
MCContext &Ctx = Asm.getContext();
11531153
Value = adjustFixupValue(Asm, Fixup, Target, Value, IsResolved, Ctx, STI);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ void AVRAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
373373
MutableArrayRef<char> Data, uint64_t Value,
374374
bool IsResolved,
375375
const MCSubtargetInfo *STI) const {
376-
if (Fixup.getKind() >= FirstLiteralRelocationKind)
376+
if (mc::isRelocation(Fixup.getKind()))
377377
return;
378378
adjustFixupValue(Fixup, Target, Value, &Asm.getContext());
379379
if (Value == 0)
@@ -475,7 +475,7 @@ MCFixupKindInfo const &AVRAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
475475

476476
// Fixup kinds from .reloc directive are like R_AVR_NONE. They do not require
477477
// any extra processing.
478-
if (Kind >= FirstLiteralRelocationKind)
478+
if (mc::isRelocation(Kind))
479479
return MCAsmBackend::getFixupKindInfo(FK_NONE);
480480

481481
if (Kind < FirstTargetFixupKind)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ void CSKYAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
201201
bool IsResolved,
202202
const MCSubtargetInfo *STI) const {
203203
MCFixupKind Kind = Fixup.getKind();
204-
if (Kind >= FirstLiteralRelocationKind)
204+
if (mc::isRelocation(Kind))
205205
return;
206206
MCContext &Ctx = Asm.getContext();
207207
MCFixupKindInfo Info = getFixupKindInfo(Kind);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ getFixupKindInfo(MCFixupKind Kind) const {
527527
static_assert(std::size(BigEndianInfos) == Mips::NumTargetFixupKinds,
528528
"Not all MIPS big endian fixup kinds added!");
529529

530-
if (Kind >= FirstLiteralRelocationKind)
530+
if (mc::isRelocation(Kind))
531531
return MCAsmBackend::getFixupKindInfo(FK_NONE);
532532
if (Kind < FirstTargetFixupKind)
533533
return MCAsmBackend::getFixupKindInfo(Kind);

llvm/lib/Target/PowerPC/MCTargetDesc/PPCAsmBackend.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class PPCAsmBackend : public MCAsmBackend {
120120

121121
// Fixup kinds from .reloc directive are like R_PPC_NONE/R_PPC64_NONE. They
122122
// do not require any extra processing.
123-
if (Kind >= FirstLiteralRelocationKind)
123+
if (mc::isRelocation(Kind))
124124
return MCAsmBackend::getFixupKindInfo(FK_NONE);
125125

126126
if (Kind < FirstTargetFixupKind)
@@ -138,7 +138,7 @@ class PPCAsmBackend : public MCAsmBackend {
138138
uint64_t Value, bool IsResolved,
139139
const MCSubtargetInfo *STI) const override {
140140
MCFixupKind Kind = Fixup.getKind();
141-
if (Kind >= FirstLiteralRelocationKind)
141+
if (mc::isRelocation(Kind))
142142
return;
143143
Value = adjustFixupValue(Kind, Value);
144144
if (!Value) return; // Doesn't change encoding.

llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ RISCVAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
9494
static_assert((std::size(Infos)) == RISCV::NumTargetFixupKinds,
9595
"Not all fixup kinds added to Infos array");
9696

97-
// Fixup kinds from raw relocation types and .reloc directive are like
98-
// R_RISCV_NONE. They do not require any extra processing.
99-
if (Kind >= FirstRelocationKind)
97+
// Fixup kinds from raw relocation types and .reloc directives force
98+
// relocations and do not use these fields.
99+
if (mc::isRelocation(Kind))
100100
return MCAsmBackend::getFixupKindInfo(FK_NONE);
101101

102102
if (Kind < FirstTargetFixupKind)

llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFObjectWriter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ unsigned RISCVELFObjectWriter::getRelocType(MCContext &Ctx,
7575

7676
// Extract the relocation type from the fixup kind, after applying STT_TLS as
7777
// needed.
78-
if (Kind >= FirstRelocationKind)
78+
if (mc::isRelocation(Fixup.getKind()))
7979
return Kind - FirstRelocationKind;
8080

8181
if (IsPCRel) {

llvm/lib/Target/Sparc/MCTargetDesc/SparcAsmBackend.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ namespace {
255255

256256
// Fixup kinds from .reloc directive are like R_SPARC_NONE. They do
257257
// not require any extra processing.
258-
if (Kind >= FirstLiteralRelocationKind)
258+
if (mc::isRelocation(Kind))
259259
return MCAsmBackend::getFixupKindInfo(FK_NONE);
260260

261261
if (Kind < FirstTargetFixupKind)
@@ -335,7 +335,7 @@ namespace {
335335
uint64_t Value, bool IsResolved,
336336
const MCSubtargetInfo *STI) const override {
337337

338-
if (Fixup.getKind() >= FirstLiteralRelocationKind)
338+
if (mc::isRelocation(Fixup.getKind()))
339339
return;
340340
Value = adjustFixupValue(Fixup.getKind(), Value);
341341
if (!Value) return; // Doesn't change encoding.

llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCAsmBackend.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ const MCFixupKindInfo &
142142
SystemZMCAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
143143
// Fixup kinds from .reloc directive are like R_390_NONE. They
144144
// do not require any extra processing.
145-
if (Kind >= FirstLiteralRelocationKind)
145+
if (mc::isRelocation(Kind))
146146
return MCAsmBackend::getFixupKindInfo(FK_NONE);
147147

148148
if (Kind < FirstTargetFixupKind)
@@ -160,7 +160,7 @@ void SystemZMCAsmBackend::applyFixup(const MCAssembler &Asm,
160160
bool IsResolved,
161161
const MCSubtargetInfo *STI) const {
162162
MCFixupKind Kind = Fixup.getKind();
163-
if (Kind >= FirstLiteralRelocationKind)
163+
if (mc::isRelocation(Kind))
164164
return;
165165
unsigned Offset = Fixup.getOffset();
166166
unsigned BitSize = getFixupKindInfo(Kind).TargetSize;

llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ const MCFixupKindInfo &X86AsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
639639

640640
// Fixup kinds from .reloc directive are like R_386_NONE/R_X86_64_NONE. They
641641
// do not require any extra processing.
642-
if (Kind >= FirstLiteralRelocationKind)
642+
if (mc::isRelocation(Kind))
643643
return MCAsmBackend::getFixupKindInfo(FK_NONE);
644644

645645
if (Kind < FirstTargetFixupKind)
@@ -691,8 +691,8 @@ void X86AsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
691691
const MCValue &, MutableArrayRef<char> Data,
692692
uint64_t Value, bool IsResolved,
693693
const MCSubtargetInfo *STI) const {
694-
unsigned Kind = Fixup.getKind();
695-
if (Kind >= FirstLiteralRelocationKind)
694+
auto Kind = Fixup.getKind();
695+
if (mc::isRelocation(Kind))
696696
return;
697697
unsigned Size = getFixupKindSize(Kind);
698698

0 commit comments

Comments
 (0)