Skip to content

Commit b9257a3

Browse files
committed
RISCVMCCodeEmitter: Set PCRel at fixup creation
Avoid reliance on the MCAssembler::evaluateFixup workaround that checks MCFixupKindInfo::FKF_IsPCRel. Additionally, standardize how fixups are appended. This helper will facilitate future fixup data structure optimizations.
1 parent 7d500b1 commit b9257a3

File tree

2 files changed

+42
-26
lines changed

2 files changed

+42
-26
lines changed

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

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,25 +69,23 @@ MCFixupKindInfo RISCVAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
6969
{"fixup_riscv_lo12_i", 20, 12, 0},
7070
{"fixup_riscv_12_i", 20, 12, 0},
7171
{"fixup_riscv_lo12_s", 0, 32, 0},
72-
{"fixup_riscv_pcrel_hi20", 12, 20, MCFixupKindInfo::FKF_IsPCRel},
73-
{"fixup_riscv_pcrel_lo12_i", 20, 12,
74-
MCFixupKindInfo::FKF_IsPCRel | MCFixupKindInfo::FKF_IsTarget},
75-
{"fixup_riscv_pcrel_lo12_s", 0, 32,
76-
MCFixupKindInfo::FKF_IsPCRel | MCFixupKindInfo::FKF_IsTarget},
77-
{"fixup_riscv_jal", 12, 20, MCFixupKindInfo::FKF_IsPCRel},
78-
{"fixup_riscv_branch", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
79-
{"fixup_riscv_rvc_jump", 2, 11, MCFixupKindInfo::FKF_IsPCRel},
80-
{"fixup_riscv_rvc_branch", 0, 16, MCFixupKindInfo::FKF_IsPCRel},
81-
{"fixup_riscv_call", 0, 64, MCFixupKindInfo::FKF_IsPCRel},
82-
{"fixup_riscv_call_plt", 0, 64, MCFixupKindInfo::FKF_IsPCRel},
83-
84-
{"fixup_riscv_qc_e_branch", 0, 48, MCFixupKindInfo::FKF_IsPCRel},
72+
{"fixup_riscv_pcrel_hi20", 12, 20, 0},
73+
{"fixup_riscv_pcrel_lo12_i", 20, 12, MCFixupKindInfo::FKF_IsTarget},
74+
{"fixup_riscv_pcrel_lo12_s", 0, 32, MCFixupKindInfo::FKF_IsTarget},
75+
{"fixup_riscv_jal", 12, 20, 0},
76+
{"fixup_riscv_branch", 0, 32, 0},
77+
{"fixup_riscv_rvc_jump", 2, 11, 0},
78+
{"fixup_riscv_rvc_branch", 0, 16, 0},
79+
{"fixup_riscv_call", 0, 64, 0},
80+
{"fixup_riscv_call_plt", 0, 64, 0},
81+
82+
{"fixup_riscv_qc_e_branch", 0, 48, 0},
8583
{"fixup_riscv_qc_e_32", 16, 32, 0},
8684
{"fixup_riscv_qc_abs20_u", 12, 20, 0},
87-
{"fixup_riscv_qc_e_call_plt", 0, 48, MCFixupKindInfo::FKF_IsPCRel},
85+
{"fixup_riscv_qc_e_call_plt", 0, 48, 0},
8886

8987
// Andes fixups
90-
{"fixup_riscv_nds_branch_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
88+
{"fixup_riscv_nds_branch_10", 0, 32, 0},
9189
};
9290
static_assert((std::size(Infos)) == RISCV::NumTargetFixupKinds,
9391
"Not all fixup kinds added to Infos array");

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

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,28 @@ MCCodeEmitter *llvm::createRISCVMCCodeEmitter(const MCInstrInfo &MCII,
120120
return new RISCVMCCodeEmitter(Ctx, MCII);
121121
}
122122

123+
static void addFixup(SmallVectorImpl<MCFixup> &Fixups, uint32_t Offset,
124+
const MCExpr *Value, uint16_t Kind) {
125+
bool PCRel = false;
126+
switch (Kind) {
127+
case ELF::R_RISCV_CALL_PLT:
128+
case RISCV::fixup_riscv_pcrel_hi20:
129+
case RISCV::fixup_riscv_pcrel_lo12_i:
130+
case RISCV::fixup_riscv_pcrel_lo12_s:
131+
case RISCV::fixup_riscv_jal:
132+
case RISCV::fixup_riscv_branch:
133+
case RISCV::fixup_riscv_rvc_jump:
134+
case RISCV::fixup_riscv_rvc_branch:
135+
case RISCV::fixup_riscv_call:
136+
case RISCV::fixup_riscv_call_plt:
137+
case RISCV::fixup_riscv_qc_e_branch:
138+
case RISCV::fixup_riscv_qc_e_call_plt:
139+
case RISCV::fixup_riscv_nds_branch_10:
140+
PCRel = true;
141+
}
142+
Fixups.push_back(MCFixup::create(Offset, Value, Kind, PCRel));
143+
}
144+
123145
// Expand PseudoCALL(Reg), PseudoTAIL and PseudoJump to AUIPC and JALR with
124146
// relocation types. We expand those pseudo-instructions while encoding them,
125147
// meaning AUIPC and JALR won't go through RISC-V MC to MC compressed
@@ -181,7 +203,7 @@ void RISCVMCCodeEmitter::expandTLSDESCCall(const MCInst &MI,
181203
MCRegister Link = MI.getOperand(0).getReg();
182204
MCRegister Dest = MI.getOperand(1).getReg();
183205
int64_t Imm = MI.getOperand(2).getImm();
184-
Fixups.push_back(MCFixup::create(0, Expr, ELF::R_RISCV_TLSDESC_CALL));
206+
addFixup(Fixups, 0, Expr, ELF::R_RISCV_TLSDESC_CALL);
185207
MCInst Call =
186208
MCInstBuilder(RISCV::JALR).addReg(Link).addReg(Dest).addImm(Imm);
187209

@@ -208,7 +230,7 @@ void RISCVMCCodeEmitter::expandAddTPRel(const MCInst &MI,
208230
assert(Expr && Expr->getSpecifier() == ELF::R_RISCV_TPREL_ADD &&
209231
"Expected tprel_add relocation on TP-relative symbol");
210232

211-
Fixups.push_back(MCFixup::create(0, Expr, ELF::R_RISCV_TPREL_ADD));
233+
addFixup(Fixups, 0, Expr, ELF::R_RISCV_TPREL_ADD);
212234
if (STI.hasFeature(RISCV::FeatureRelax))
213235
Fixups.back().setLinkerRelaxable();
214236

@@ -318,10 +340,8 @@ void RISCVMCCodeEmitter::expandLongCondBr(const MCInst &MI,
318340
// Drop any fixup added so we can add the correct one.
319341
Fixups.resize(FixupStartIndex);
320342

321-
if (SrcSymbol.isExpr()) {
322-
Fixups.push_back(
323-
MCFixup::create(Offset, SrcSymbol.getExpr(), RISCV::fixup_riscv_jal));
324-
}
343+
if (SrcSymbol.isExpr())
344+
addFixup(Fixups, Offset, SrcSymbol.getExpr(), RISCV::fixup_riscv_jal);
325345
}
326346

327347
// Expand PseudoLongQC_(E_)Bxxx to an inverted conditional branch and an
@@ -368,10 +388,8 @@ void RISCVMCCodeEmitter::expandQCLongCondBrImm(const MCInst &MI,
368388
support::endian::write(CB, JBinary, llvm::endianness::little);
369389
// Drop any fixup added so we can add the correct one.
370390
Fixups.resize(FixupStartIndex);
371-
if (SrcSymbol.isExpr()) {
372-
Fixups.push_back(
373-
MCFixup::create(Offset, SrcSymbol.getExpr(), RISCV::fixup_riscv_jal));
374-
}
391+
if (SrcSymbol.isExpr())
392+
addFixup(Fixups, Offset, SrcSymbol.getExpr(), RISCV::fixup_riscv_jal);
375393
}
376394

377395
void RISCVMCCodeEmitter::encodeInstruction(const MCInst &MI,
@@ -649,7 +667,7 @@ uint64_t RISCVMCCodeEmitter::getImmOpValue(const MCInst &MI, unsigned OpNo,
649667

650668
assert(FixupKind != RISCV::fixup_riscv_invalid && "Unhandled expression!");
651669

652-
Fixups.push_back(MCFixup::create(0, Expr, FixupKind));
670+
addFixup(Fixups, 0, Expr, FixupKind);
653671
// If linker relaxation is enabled and supported by this relocation, set
654672
// a bit so that if fixup is unresolved, a R_RISCV_RELAX relocation will be
655673
// appended.

0 commit comments

Comments
 (0)