Skip to content

Commit 40789ce

Browse files
committed
MCFixup: Move relocation values before FK_NONE
Simplify the process of encoding a raw relocation type using MCFixupKind. Currently, FirstRelocationkind is utilized by AArch64, LoongArch, and RISCV.
1 parent 0c0b5ab commit 40789ce

12 files changed

+63
-80
lines changed

llvm/include/llvm/MC/MCFixup.h

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,17 @@ namespace llvm {
1818
class MCExpr;
1919

2020
/// Extensible enumeration to represent the type of a fixup.
21-
enum MCFixupKind {
22-
FK_NONE = 0, ///< A no-op fixup.
21+
enum MCFixupKind : uint16_t {
22+
// [0, FirstLiteralRelocationKind) encodes raw relocation types.
23+
24+
// [FirstLiteralRelocationKind, FK_NONE) encodes raw relocation types coming
25+
// from .reloc directives. Fixup kind
26+
// FirstLiteralRelocationKind+t encodes relocation type t.
27+
FirstLiteralRelocationKind = 2000,
28+
29+
// Other kinds indicate the fixup may resolve to a constant, allowing the
30+
// assembler to update the instruction or data directly without a relocation.
31+
FK_NONE = 4000, ///< A no-op fixup.
2332
FK_Data_1, ///< A one-byte fixup.
2433
FK_Data_2, ///< A two-byte fixup.
2534
FK_Data_4, ///< A four-byte fixup.
@@ -34,19 +43,7 @@ enum MCFixupKind {
3443
FK_SecRel_4, ///< A four-byte section relative fixup.
3544
FK_SecRel_8, ///< A eight-byte section relative fixup.
3645

37-
FirstTargetFixupKind = 128,
38-
39-
/// Targets can use FirstRelocationKind+t to encode relocation type t.
40-
FirstRelocationKind = 256,
41-
42-
/// The range [FirstLiteralRelocationKind, MaxTargetFixupKind) is used for
43-
/// relocations coming from .reloc directive. Fixup kind
44-
/// FirstLiteralRelocationKind+V represents the relocation type with number V.
45-
FirstLiteralRelocationKind = 256 + 1032 + 32,
46-
47-
/// Set limit to accommodate the highest reloc type in use for all Targets,
48-
/// currently R_AARCH64_IRELATIVE at 1032, including room for expansion.
49-
MaxFixupKind = FirstLiteralRelocationKind + 1032 + 32,
46+
FirstTargetFixupKind,
5047
};
5148

5249
/// Encode information on a single operation to perform on a byte
@@ -81,7 +78,6 @@ class MCFixup {
8178
public:
8279
static MCFixup create(uint32_t Offset, const MCExpr *Value,
8380
MCFixupKind Kind, SMLoc Loc = SMLoc()) {
84-
assert(Kind <= MaxFixupKind && "Kind out of range!");
8581
MCFixup FI;
8682
FI.Value = Value;
8783
FI.Offset = Offset;
@@ -125,15 +121,13 @@ class MCFixup {
125121
namespace mc {
126122
// Check if the fixup kind is a relocation type. Return false if the fixup can
127123
// be resolved without a relocation.
128-
inline bool isRelocation(MCFixupKind FixupKind) {
129-
return FixupKind >= FirstRelocationKind;
130-
}
124+
inline bool isRelocation(MCFixupKind FixupKind) { return FixupKind < FK_NONE; }
131125

132126
// Check if the fixup kind represents a relocation type from a .reloc directive.
133127
// In ELF, this skips STT_SECTION adjustment and STT_TLS symbol type setting for
134128
// TLS relocations.
135129
inline bool isRelocRelocation(MCFixupKind FixupKind) {
136-
return FixupKind >= FirstLiteralRelocationKind;
130+
return FirstLiteralRelocationKind <= FixupKind && FixupKind < FK_NONE;
137131
}
138132
} // namespace mc
139133

llvm/lib/MC/MCAsmBackend.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ const MCFixupKindInfo &MCAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
105105
{"FK_SecRel_8", 0, 64, 0},
106106
};
107107

108-
assert((size_t)Kind <= std::size(Builtins) && "Unknown fixup kind");
109-
return Builtins[Kind];
108+
assert(size_t(Kind - FK_NONE) < std::size(Builtins) && "Unknown fixup kind");
109+
return Builtins[Kind - FK_NONE];
110110
}
111111

112112
bool MCAsmBackend::shouldForceRelocation(const MCAssembler &, const MCFixup &,

llvm/lib/MC/MCAsmStreamer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2404,7 +2404,7 @@ void MCAsmStreamer::AddEncodingComment(const MCInst &Inst,
24042404
F.getValue()->print(OS, MAI);
24052405
auto Kind = F.getKind();
24062406
if (mc::isRelocation(Kind))
2407-
OS << ", relocation type: " << (Kind - FirstRelocationKind);
2407+
OS << ", relocation type: " << Kind;
24082408
else
24092409
OS << ", kind: "
24102410
<< getAssembler().getBackend().getFixupKindInfo(Kind).Name;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ unsigned AArch64ELFObjectWriter::getRelocType(MCContext &Ctx,
130130
// Extract the relocation type from the fixup kind, after applying STT_TLS as
131131
// needed.
132132
if (mc::isRelocation(Fixup.getKind()))
133-
return Kind - FirstRelocationKind;
133+
return Kind;
134134

135135
if (IsPCRel) {
136136
switch (Kind) {

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -749,8 +749,7 @@ void AArch64MCCodeEmitter::encodeInstruction(const MCInst &MI,
749749
auto Reloc = STI.getTargetTriple().getEnvironment() == Triple::GNUILP32
750750
? ELF::R_AARCH64_P32_TLSDESC_CALL
751751
: ELF::R_AARCH64_TLSDESC_CALL;
752-
Fixups.push_back(MCFixup::create(0, MI.getOperand(0).getExpr(),
753-
FirstRelocationKind + Reloc));
752+
Fixups.push_back(MCFixup::create(0, MI.getOperand(0).getExpr(), Reloc));
754753
return;
755754
}
756755

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

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -271,27 +271,23 @@ getRelocPairForSize(unsigned Size) {
271271
default:
272272
llvm_unreachable("unsupported fixup size");
273273
case 6:
274-
return std::make_pair(MCFixupKind(FirstRelocationKind + ELF::R_LARCH_ADD6),
275-
MCFixupKind(FirstRelocationKind + ELF::R_LARCH_SUB6));
274+
return std::make_pair(MCFixupKind(ELF::R_LARCH_ADD6),
275+
MCFixupKind(ELF::R_LARCH_SUB6));
276276
case 8:
277-
return std::make_pair(MCFixupKind(FirstRelocationKind + ELF::R_LARCH_ADD8),
278-
MCFixupKind(FirstRelocationKind + ELF::R_LARCH_SUB8));
277+
return std::make_pair(MCFixupKind(ELF::R_LARCH_ADD8),
278+
MCFixupKind(ELF::R_LARCH_SUB8));
279279
case 16:
280-
return std::make_pair(
281-
MCFixupKind(FirstRelocationKind + ELF::R_LARCH_ADD16),
282-
MCFixupKind(FirstRelocationKind + ELF::R_LARCH_SUB16));
280+
return std::make_pair(MCFixupKind(ELF::R_LARCH_ADD16),
281+
MCFixupKind(ELF::R_LARCH_SUB16));
283282
case 32:
284-
return std::make_pair(
285-
MCFixupKind(FirstRelocationKind + ELF::R_LARCH_ADD32),
286-
MCFixupKind(FirstRelocationKind + ELF::R_LARCH_SUB32));
283+
return std::make_pair(MCFixupKind(ELF::R_LARCH_ADD32),
284+
MCFixupKind(ELF::R_LARCH_SUB32));
287285
case 64:
288-
return std::make_pair(
289-
MCFixupKind(FirstRelocationKind + ELF::R_LARCH_ADD64),
290-
MCFixupKind(FirstRelocationKind + ELF::R_LARCH_SUB64));
286+
return std::make_pair(MCFixupKind(ELF::R_LARCH_ADD64),
287+
MCFixupKind(ELF::R_LARCH_SUB64));
291288
case 128:
292-
return std::make_pair(
293-
MCFixupKind(FirstRelocationKind + ELF::R_LARCH_ADD_ULEB128),
294-
MCFixupKind(FirstRelocationKind + ELF::R_LARCH_SUB_ULEB128));
289+
return std::make_pair(MCFixupKind(ELF::R_LARCH_ADD_ULEB128),
290+
MCFixupKind(ELF::R_LARCH_SUB_ULEB128));
295291
}
296292
}
297293

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ unsigned LoongArchELFObjectWriter::getRelocType(MCContext &Ctx,
7373

7474
unsigned Kind = Fixup.getTargetKind();
7575
if (mc::isRelocation(Fixup.getKind()))
76-
return Kind - FirstRelocationKind;
76+
return Kind;
7777
switch (Kind) {
7878
default:
7979
Ctx.reportError(Fixup.getLoc(), "Unsupported relocation type");

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ enum Fixups {
5555
// Define fixups for force relocation as FirstRelocationKind+V
5656
// represents the relocation type with number V.
5757
// 20-bit fixup corresponding to %pc_hi20(foo) for instruction pcalau12i.
58-
fixup_loongarch_pcala_hi20 = FirstRelocationKind + ELF::R_LARCH_PCALA_HI20,
58+
fixup_loongarch_pcala_hi20 = ELF::R_LARCH_PCALA_HI20,
5959
// 12-bit fixup corresponding to %pc_lo12(foo) for instructions like addi.w/d.
6060
fixup_loongarch_pcala_lo12,
6161
// 20-bit fixup corresponding to %pc64_lo20(foo) for instruction lu32i.d.
@@ -81,8 +81,7 @@ enum Fixups {
8181
fixup_loongarch_got64_hi12,
8282
// Skip R_LARCH_TLS_LE_*.
8383
// 20-bit fixup corresponding to %ie_pc_hi20(foo) for instruction pcalau12i.
84-
fixup_loongarch_tls_ie_pc_hi20 =
85-
FirstRelocationKind + ELF::R_LARCH_TLS_IE_PC_HI20,
84+
fixup_loongarch_tls_ie_pc_hi20 = ELF::R_LARCH_TLS_IE_PC_HI20,
8685
// 12-bit fixup corresponding to %ie_pc_lo12(foo) for instructions
8786
// ld.w/ld.d/add.d.
8887
fixup_loongarch_tls_ie_pc_lo12,
@@ -107,17 +106,16 @@ enum Fixups {
107106
// 20-bit fixup corresponding to %gd_hi20(foo) for instruction lu12i.w.
108107
fixup_loongarch_tls_gd_hi20,
109108
// Generate an R_LARCH_RELAX which indicates the linker may relax here.
110-
fixup_loongarch_relax = FirstRelocationKind + ELF::R_LARCH_RELAX,
109+
fixup_loongarch_relax = ELF::R_LARCH_RELAX,
111110
// Generate an R_LARCH_ALIGN which indicates the linker may fixup align here.
112-
fixup_loongarch_align = FirstRelocationKind + ELF::R_LARCH_ALIGN,
111+
fixup_loongarch_align = ELF::R_LARCH_ALIGN,
113112
// 20-bit fixup corresponding to %pcrel_20(foo) for instruction pcaddi.
114113
fixup_loongarch_pcrel20_s2,
115114
// 36-bit fixup corresponding to %call36(foo) for a pair instructions:
116115
// pcaddu18i+jirl.
117-
fixup_loongarch_call36 = FirstRelocationKind + ELF::R_LARCH_CALL36,
116+
fixup_loongarch_call36 = ELF::R_LARCH_CALL36,
118117
// 20-bit fixup corresponding to %desc_pc_hi20(foo) for instruction pcalau12i.
119-
fixup_loongarch_tls_desc_pc_hi20 =
120-
FirstRelocationKind + ELF::R_LARCH_TLS_DESC_PC_HI20,
118+
fixup_loongarch_tls_desc_pc_hi20 = ELF::R_LARCH_TLS_DESC_PC_HI20,
121119
// 12-bit fixup corresponding to %desc_pc_lo12(foo) for instructions like
122120
// addi.w/d.
123121
fixup_loongarch_tls_desc_pc_lo12,

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ static cl::opt<bool> ULEB128Reloc(
3636

3737
RISCVAsmBackend::RISCVAsmBackend(const MCSubtargetInfo &STI, uint8_t OSABI,
3838
bool Is64Bit, const MCTargetOptions &Options)
39-
: MCAsmBackend(llvm::endianness::little,
40-
FirstRelocationKind + ELF::R_RISCV_RELAX),
41-
STI(STI), OSABI(OSABI), Is64Bit(Is64Bit), TargetOptions(Options) {
39+
: MCAsmBackend(llvm::endianness::little, ELF::R_RISCV_RELAX), STI(STI),
40+
OSABI(OSABI), Is64Bit(Is64Bit), TargetOptions(Options) {
4241
RISCVFeatures::validate(STI.getTargetTriple(), STI.getFeatureBits());
4342
}
4443

@@ -659,10 +658,8 @@ bool RISCVAsmBackend::handleAddSubRelocations(const MCAssembler &Asm,
659658
}
660659
MCValue A = MCValue::get(Target.getAddSym(), nullptr, Target.getConstant());
661660
MCValue B = MCValue::get(Target.getSubSym());
662-
auto FA =
663-
MCFixup::create(Fixup.getOffset(), nullptr, FirstRelocationKind + TA);
664-
auto FB =
665-
MCFixup::create(Fixup.getOffset(), nullptr, FirstRelocationKind + TB);
661+
auto FA = MCFixup::create(Fixup.getOffset(), nullptr, TA);
662+
auto FB = MCFixup::create(Fixup.getOffset(), nullptr, TB);
666663
auto &Assembler = const_cast<MCAssembler &>(Asm);
667664
Asm.getWriter().recordRelocation(Assembler, &F, FA, A, FixedValueA);
668665
Asm.getWriter().recordRelocation(Assembler, &F, FB, B, FixedValueB);
@@ -744,8 +741,7 @@ bool RISCVAsmBackend::shouldInsertFixupForCodeAlign(MCAssembler &Asm,
744741
MCContext &Ctx = Asm.getContext();
745742
const MCExpr *Dummy = MCConstantExpr::create(0, Ctx);
746743
// Create fixup_riscv_align fixup.
747-
MCFixup Fixup = MCFixup::create(
748-
0, Dummy, FirstRelocationKind + ELF::R_RISCV_ALIGN, SMLoc());
744+
MCFixup Fixup = MCFixup::create(0, Dummy, ELF::R_RISCV_ALIGN, SMLoc());
749745

750746
uint64_t FixedValue = 0;
751747
MCValue NopBytes = MCValue::get(Count);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ unsigned RISCVELFObjectWriter::getRelocType(MCContext &Ctx,
7676
// Extract the relocation type from the fixup kind, after applying STT_TLS as
7777
// needed.
7878
if (mc::isRelocation(Fixup.getKind()))
79-
return Kind - FirstRelocationKind;
79+
return Kind;
8080

8181
if (IsPCRel) {
8282
switch (Kind) {

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

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ void RISCVMCCodeEmitter::expandTLSDESCCall(const MCInst &MI,
180180
MCRegister Link = MI.getOperand(0).getReg();
181181
MCRegister Dest = MI.getOperand(1).getReg();
182182
int64_t Imm = MI.getOperand(2).getImm();
183-
Fixups.push_back(MCFixup::create(
184-
0, Expr, FirstRelocationKind + ELF::R_RISCV_TLSDESC_CALL, MI.getLoc()));
183+
Fixups.push_back(
184+
MCFixup::create(0, Expr, ELF::R_RISCV_TLSDESC_CALL, MI.getLoc()));
185185
MCInst Call =
186186
MCInstBuilder(RISCV::JALR).addReg(Link).addReg(Dest).addImm(Imm);
187187

@@ -209,14 +209,14 @@ void RISCVMCCodeEmitter::expandAddTPRel(const MCInst &MI,
209209
"Expected tprel_add relocation on TP-relative symbol");
210210

211211
// Emit the correct tprel_add relocation for the symbol.
212-
Fixups.push_back(MCFixup::create(
213-
0, Expr, FirstRelocationKind + ELF::R_RISCV_TPREL_ADD, MI.getLoc()));
212+
Fixups.push_back(
213+
MCFixup::create(0, Expr, ELF::R_RISCV_TPREL_ADD, MI.getLoc()));
214214

215215
// Emit R_RISCV_RELAX for tprel_add where the relax feature is enabled.
216216
if (STI.hasFeature(RISCV::FeatureRelax)) {
217217
const MCConstantExpr *Dummy = MCConstantExpr::create(0, Ctx);
218-
Fixups.push_back(MCFixup::create(
219-
0, Dummy, FirstRelocationKind + ELF::R_RISCV_RELAX, MI.getLoc()));
218+
Fixups.push_back(
219+
MCFixup::create(0, Dummy, ELF::R_RISCV_RELAX, MI.getLoc()));
220220
}
221221

222222
// Emit a normal ADD instruction with the given operands.
@@ -612,26 +612,26 @@ uint64_t RISCVMCCodeEmitter::getImmOpValue(const MCInst &MI, unsigned OpNo,
612612
RelaxCandidate = true;
613613
break;
614614
case RISCVMCExpr::VK_GOT_HI:
615-
FixupKind = FirstRelocationKind + ELF::R_RISCV_GOT_HI20;
615+
FixupKind = ELF::R_RISCV_GOT_HI20;
616616
break;
617617
case RISCVMCExpr::VK_TPREL_LO:
618618
if (MIFrm == RISCVII::InstFormatI)
619-
FixupKind = FirstRelocationKind + ELF::R_RISCV_TPREL_LO12_I;
619+
FixupKind = ELF::R_RISCV_TPREL_LO12_I;
620620
else if (MIFrm == RISCVII::InstFormatS)
621-
FixupKind = FirstRelocationKind + ELF::R_RISCV_TPREL_LO12_S;
621+
FixupKind = ELF::R_RISCV_TPREL_LO12_S;
622622
else
623623
llvm_unreachable("VK_TPREL_LO used with unexpected instruction format");
624624
RelaxCandidate = true;
625625
break;
626626
case RISCVMCExpr::VK_TPREL_HI:
627-
FixupKind = FirstRelocationKind + ELF::R_RISCV_TPREL_HI20;
627+
FixupKind = ELF::R_RISCV_TPREL_HI20;
628628
RelaxCandidate = true;
629629
break;
630630
case RISCVMCExpr::VK_TLS_GOT_HI:
631-
FixupKind = FirstRelocationKind + ELF::R_RISCV_TLS_GOT_HI20;
631+
FixupKind = ELF::R_RISCV_TLS_GOT_HI20;
632632
break;
633633
case RISCVMCExpr::VK_TLS_GD_HI:
634-
FixupKind = FirstRelocationKind + ELF::R_RISCV_TLS_GD_HI20;
634+
FixupKind = ELF::R_RISCV_TLS_GD_HI20;
635635
break;
636636
case RISCVMCExpr::VK_CALL:
637637
FixupKind = RISCV::fixup_riscv_call;
@@ -642,16 +642,16 @@ uint64_t RISCVMCCodeEmitter::getImmOpValue(const MCInst &MI, unsigned OpNo,
642642
RelaxCandidate = true;
643643
break;
644644
case RISCVMCExpr::VK_TLSDESC_HI:
645-
FixupKind = FirstRelocationKind + ELF::R_RISCV_TLSDESC_HI20;
645+
FixupKind = ELF::R_RISCV_TLSDESC_HI20;
646646
break;
647647
case RISCVMCExpr::VK_TLSDESC_LOAD_LO:
648-
FixupKind = FirstRelocationKind + ELF::R_RISCV_TLSDESC_LOAD_LO12;
648+
FixupKind = ELF::R_RISCV_TLSDESC_LOAD_LO12;
649649
break;
650650
case RISCVMCExpr::VK_TLSDESC_ADD_LO:
651-
FixupKind = FirstRelocationKind + ELF::R_RISCV_TLSDESC_ADD_LO12;
651+
FixupKind = ELF::R_RISCV_TLSDESC_ADD_LO12;
652652
break;
653653
case RISCVMCExpr::VK_TLSDESC_CALL:
654-
FixupKind = FirstRelocationKind + ELF::R_RISCV_TLSDESC_CALL;
654+
FixupKind = ELF::R_RISCV_TLSDESC_CALL;
655655
break;
656656
case RISCVMCExpr::VK_QC_ABS20:
657657
FixupKind = RISCV::fixup_riscv_qc_abs20_u;
@@ -689,8 +689,8 @@ uint64_t RISCVMCCodeEmitter::getImmOpValue(const MCInst &MI, unsigned OpNo,
689689
// relaxed.
690690
if (EnableRelax && RelaxCandidate) {
691691
const MCConstantExpr *Dummy = MCConstantExpr::create(0, Ctx);
692-
Fixups.push_back(MCFixup::create(
693-
0, Dummy, FirstRelocationKind + ELF::R_RISCV_RELAX, MI.getLoc()));
692+
Fixups.push_back(
693+
MCFixup::create(0, Dummy, ELF::R_RISCV_RELAX, MI.getLoc()));
694694
++MCNumFixups;
695695
}
696696

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ const MCFixup *RISCVMCExpr::getPCRelHiFixup(const MCFragment **DFOut) const {
7575
}
7676
break;
7777
}
78-
switch (Kind - FirstRelocationKind) {
78+
switch (Kind) {
7979
case ELF::R_RISCV_GOT_HI20:
8080
case ELF::R_RISCV_TLS_GOT_HI20:
8181
case ELF::R_RISCV_TLS_GD_HI20:

0 commit comments

Comments
 (0)