Skip to content

Commit 6a43c66

Browse files
authored
[TableGen][DecoderEmitter][RISCV] Always handle bits<0> (#159951)
Previously, `bits<0>` only had effect if `ignore-non-decodable-operands` wasn't specified. Handle it even if the option was specified. This should allow for a smoother transition to the option removed. The change revealed a couple of inaccuracies in RISCV compressed instruction definitions. * `C_ADDI4SPN` has `bits<5> rs1` field, but `rs1` is not encoded. It should be `bits<0>`. * `C_ADDI16SP` has `bits<5> rd` in the base class, but it is unused since `Inst{11-7}` is overwritten with constant bits. We should instead set `rd = 2` and `Inst{11-7} = rd`. There are a couple of alternative fixes, but this one is the shortest.
1 parent 19935ea commit 6a43c66

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

llvm/lib/Target/RISCV/Disassembler/RISCVDisassembler.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,14 @@ static DecodeStatus DecodeSPRegisterClass(MCInst &Inst,
206206
return MCDisassembler::Success;
207207
}
208208

209+
static DecodeStatus DecodeSPRegisterClass(MCInst &Inst, uint64_t RegNo,
210+
uint32_t Address,
211+
const MCDisassembler *Decoder) {
212+
assert(RegNo == 2);
213+
Inst.addOperand(MCOperand::createReg(RISCV::X2));
214+
return MCDisassembler::Success;
215+
}
216+
209217
static DecodeStatus DecodeGPRX5RegisterClass(MCInst &Inst,
210218
const MCDisassembler *Decoder) {
211219
Inst.addOperand(MCOperand::createReg(RISCV::X5));

llvm/lib/Target/RISCV/RISCVInstrInfoC.td

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ def C_ADDI4SPN : RVInst16CIW<0b000, 0b00, (outs GPRC:$rd),
298298
(ins SP:$rs1, uimm10_lsb00nonzero:$imm),
299299
"c.addi4spn", "$rd, $rs1, $imm">,
300300
Sched<[WriteIALU, ReadIALU]> {
301-
bits<5> rs1;
301+
bits<0> rs1;
302302
let Inst{12-11} = imm{5-4};
303303
let Inst{10-7} = imm{9-6};
304304
let Inst{6} = imm{2};
@@ -404,8 +404,8 @@ def C_ADDI16SP : RVInst16CI<0b011, 0b01, (outs SP:$rd_wb),
404404
"c.addi16sp", "$rd, $imm">,
405405
Sched<[WriteIALU, ReadIALU]> {
406406
let Constraints = "$rd = $rd_wb";
407+
let rd = 2;
407408
let Inst{12} = imm{9};
408-
let Inst{11-7} = 2;
409409
let Inst{6} = imm{4};
410410
let Inst{5} = imm{6};
411411
let Inst{4-3} = imm{8-7};
@@ -965,4 +965,3 @@ let Predicates = [HasStdExtCOrZcd, HasStdExtD] in {
965965
def : CompressPat<(FSD FPR64:$rs2, SPMem:$rs1, uimm9_lsb000:$imm),
966966
(C_FSDSP FPR64:$rs2, SPMem:$rs1, uimm9_lsb000:$imm)>;
967967
} // Predicates = [HasStdExtCOrZcd, HasStdExtD]
968-

llvm/utils/TableGen/Common/InstructionEncoding.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,14 @@ static void addOneOperandFields(const Record *EncodingDef,
316316
else
317317
OpInfo.addField(I, J - I, Offset);
318318
}
319+
320+
if (!OpInfo.InitValue && OpInfo.fields().empty()) {
321+
// We found a field in InstructionEncoding record that corresponds to the
322+
// named operand, but that field has no constant bits and doesn't contribute
323+
// to the Inst field. For now, treat that field as if it didn't exist.
324+
// TODO: Remove along with IgnoreNonDecodableOperands.
325+
OpInfo.HasNoEncoding = true;
326+
}
319327
}
320328

321329
void InstructionEncoding::parseFixedLenOperands(const BitsInit &Bits) {

llvm/utils/TableGen/DecoderEmitter.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -696,8 +696,6 @@ static void emitBinaryParser(raw_ostream &OS, indent Indent,
696696

697697
// Special case for 'bits<0>'.
698698
if (OpInfo.Fields.empty() && !OpInfo.InitValue) {
699-
if (IgnoreNonDecodableOperands)
700-
return;
701699
assert(!OpInfo.Decoder.empty());
702700
// The operand has no encoding, so the corresponding argument is omitted.
703701
// This avoids confusion and allows the function to be overloaded if the

0 commit comments

Comments
 (0)