Skip to content

Commit ee7c7ec

Browse files
committed
[RISCV] Prepare for the use of variable-sized register classes
While parameterising by XLen, also take the opportunity to clean up the formatting of the RISCV .td files. This commit unifies the in-tree code with my patchset at <https://github.com/lowrisc/riscv-llvm>. llvm-svn: 316159
1 parent 27c1f46 commit ee7c7ec

File tree

8 files changed

+276
-220
lines changed

8 files changed

+276
-220
lines changed

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ extern "C" void LLVMInitializeRISCVDisassembler() {
5656
}
5757

5858
static const unsigned GPRDecoderTable[] = {
59-
RISCV::X0_32, RISCV::X1_32, RISCV::X2_32, RISCV::X3_32,
60-
RISCV::X4_32, RISCV::X5_32, RISCV::X6_32, RISCV::X7_32,
61-
RISCV::X8_32, RISCV::X9_32, RISCV::X10_32, RISCV::X11_32,
62-
RISCV::X12_32, RISCV::X13_32, RISCV::X14_32, RISCV::X15_32,
63-
RISCV::X16_32, RISCV::X17_32, RISCV::X18_32, RISCV::X19_32,
64-
RISCV::X20_32, RISCV::X21_32, RISCV::X22_32, RISCV::X23_32,
65-
RISCV::X24_32, RISCV::X25_32, RISCV::X26_32, RISCV::X27_32,
66-
RISCV::X28_32, RISCV::X29_32, RISCV::X30_32, RISCV::X31_32
59+
RISCV::X0, RISCV::X1, RISCV::X2, RISCV::X3,
60+
RISCV::X4, RISCV::X5, RISCV::X6, RISCV::X7,
61+
RISCV::X8, RISCV::X9, RISCV::X10, RISCV::X11,
62+
RISCV::X12, RISCV::X13, RISCV::X14, RISCV::X15,
63+
RISCV::X16, RISCV::X17, RISCV::X18, RISCV::X19,
64+
RISCV::X20, RISCV::X21, RISCV::X22, RISCV::X23,
65+
RISCV::X24, RISCV::X25, RISCV::X26, RISCV::X27,
66+
RISCV::X28, RISCV::X29, RISCV::X30, RISCV::X31
6767
};
6868

6969
static DecodeStatus DecodeGPRRegisterClass(MCInst &Inst, uint64_t RegNo,

llvm/lib/Target/RISCV/MCTargetDesc/RISCVBaseInfo.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ enum {
2626
InstFormatR = 1,
2727
InstFormatI = 2,
2828
InstFormatS = 3,
29-
InstFormatSB = 4,
29+
InstFormatB = 4,
3030
InstFormatU = 5,
31-
InstFormatOther = 6,
31+
InstFormatJ = 6,
32+
InstFormatOther = 7,
3233

3334
InstFormatMask = 15
3435
};

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ unsigned RISCVMCCodeEmitter::getImmOpValue(const MCInst &MI, unsigned OpNo,
159159
cast<MCSymbolRefExpr>(Expr)->getKind() == MCSymbolRefExpr::VK_None) {
160160
if (Desc.getOpcode() == RISCV::JAL) {
161161
FixupKind = RISCV::fixup_riscv_jal;
162-
} else if (MIFrm == RISCVII::InstFormatSB) {
162+
} else if (MIFrm == RISCVII::InstFormatB) {
163163
FixupKind = RISCV::fixup_riscv_branch;
164164
}
165165
}

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static MCInstrInfo *createRISCVMCInstrInfo() {
4242

4343
static MCRegisterInfo *createRISCVMCRegisterInfo(const Triple &TT) {
4444
MCRegisterInfo *X = new MCRegisterInfo();
45-
InitRISCVMCRegisterInfo(X, RISCV::X1_32);
45+
InitRISCVMCRegisterInfo(X, RISCV::X1);
4646
return X;
4747
}
4848

@@ -51,6 +51,14 @@ static MCAsmInfo *createRISCVMCAsmInfo(const MCRegisterInfo &MRI,
5151
return new RISCVMCAsmInfo(TT);
5252
}
5353

54+
static MCSubtargetInfo *createRISCVMCSubtargetInfo(const Triple &TT,
55+
StringRef CPU, StringRef FS) {
56+
std::string CPUName = CPU;
57+
if (CPUName.empty())
58+
CPUName = TT.isArch64Bit() ? "generic-rv64" : "generic-rv32";
59+
return createRISCVMCSubtargetInfoImpl(TT, CPUName, FS);
60+
}
61+
5462
static MCInstPrinter *createRISCVMCInstPrinter(const Triple &T,
5563
unsigned SyntaxVariant,
5664
const MCAsmInfo &MAI,
@@ -67,6 +75,6 @@ extern "C" void LLVMInitializeRISCVTargetMC() {
6775
TargetRegistry::RegisterMCAsmBackend(*T, createRISCVAsmBackend);
6876
TargetRegistry::RegisterMCCodeEmitter(*T, createRISCVMCCodeEmitter);
6977
TargetRegistry::RegisterMCInstPrinter(*T, createRISCVMCInstPrinter);
70-
TargetRegistry::RegisterMCSubtargetInfo(*T, createRISCVMCSubtargetInfoImpl);
78+
TargetRegistry::RegisterMCSubtargetInfo(*T, createRISCVMCSubtargetInfo);
7179
}
7280
}

llvm/lib/Target/RISCV/RISCV.td

+23-5
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,37 @@
99

1010
include "llvm/Target/Target.td"
1111

12-
include "RISCVRegisterInfo.td"
13-
include "RISCVInstrInfo.td"
12+
//===----------------------------------------------------------------------===//
13+
// RISC-V subtarget features and instruction predicates.
14+
//===----------------------------------------------------------------------===//
1415

16+
def Feature64Bit : SubtargetFeature<"64bit", "HasRV64", "true",
17+
"Implements RV64">;
1518

16-
def RISCVInstrInfo : InstrInfo;
19+
def RV64 : HwMode<"+64bit">;
20+
def RV32 : HwMode<"-64bit">;
21+
22+
//===----------------------------------------------------------------------===//
23+
// Register file, instruction descriptions.
24+
//===----------------------------------------------------------------------===//
25+
26+
include "RISCVRegisterInfo.td"
27+
include "RISCVInstrInfo.td"
1728

18-
def Feature64Bit : SubtargetFeature<"64bit", "HasRV64", "true",
19-
"Implements RV64">;
29+
//===----------------------------------------------------------------------===//
30+
// RISC-V processors supported.
31+
//===----------------------------------------------------------------------===//
2032

2133
def : ProcessorModel<"generic-rv32", NoSchedModel, []>;
2234

2335
def : ProcessorModel<"generic-rv64", NoSchedModel, [Feature64Bit]>;
2436

37+
//===----------------------------------------------------------------------===//
38+
// Define the RISC-V target.
39+
//===----------------------------------------------------------------------===//
40+
41+
def RISCVInstrInfo : InstrInfo;
42+
2543
def RISCVAsmParser : AsmParser {
2644
let ShouldEmitMatchRegisterAltName = 1;
2745
}

llvm/lib/Target/RISCV/RISCVInstrFormats.td

+66-34
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,40 @@ def InstFormatPseudo : InstFormat<0>;
3535
def InstFormatR : InstFormat<1>;
3636
def InstFormatI : InstFormat<2>;
3737
def InstFormatS : InstFormat<3>;
38-
def InstFormatSB : InstFormat<4>;
38+
def InstFormatB : InstFormat<4>;
3939
def InstFormatU : InstFormat<5>;
40-
def InstFormatOther : InstFormat<6>;
40+
def InstFormatJ : InstFormat<6>;
41+
def InstFormatOther : InstFormat<7>;
4142

42-
class RISCVInst<dag outs, dag ins, string asmstr, list<dag> pattern,
43-
InstFormat format>
43+
// The following opcode names and match those given in Table 19.1 in the
44+
// RISC-V User-level ISA specification ("RISC-V base opcode map").
45+
class RISCVOpcode<bits<7> val> {
46+
bits<7> Value = val;
47+
}
48+
def OPC_LOAD : RISCVOpcode<0b0000011>;
49+
def OPC_LOAD_FP : RISCVOpcode<0b0000111>;
50+
def OPC_MISC_MEM : RISCVOpcode<0b0001111>;
51+
def OPC_OP_IMM : RISCVOpcode<0b0010011>;
52+
def OPC_AUIPC : RISCVOpcode<0b0010111>;
53+
def OPC_OP_IMM_32 : RISCVOpcode<0b0011011>;
54+
def OPC_STORE : RISCVOpcode<0b0100011>;
55+
def OPC_STORE_FP : RISCVOpcode<0b0100111>;
56+
def OPC_AMO : RISCVOpcode<0b0101111>;
57+
def OPC_OP : RISCVOpcode<0b0110011>;
58+
def OPC_LUI : RISCVOpcode<0b0110111>;
59+
def OPC_OP_32 : RISCVOpcode<0b0111011>;
60+
def OPC_MADD : RISCVOpcode<0b1000011>;
61+
def OPC_MSUB : RISCVOpcode<0b1000111>;
62+
def OPC_NMSUB : RISCVOpcode<0b1001011>;
63+
def OPC_NMADD : RISCVOpcode<0b1001111>;
64+
def OPC_OP_FP : RISCVOpcode<0b1010011>;
65+
def OPC_BRANCH : RISCVOpcode<0b1100011>;
66+
def OPC_JALR : RISCVOpcode<0b1100111>;
67+
def OPC_JAL : RISCVOpcode<0b1101111>;
68+
def OPC_SYSTEM : RISCVOpcode<0b1110011>;
69+
70+
class RVInst<dag outs, dag ins, string opcodestr, string argstr,
71+
list<dag> pattern, InstFormat format>
4472
: Instruction {
4573
field bits<32> Inst;
4674
// SoftFail is a field the disassembler can use to provide a way for
@@ -58,22 +86,26 @@ class RISCVInst<dag outs, dag ins, string asmstr, list<dag> pattern,
5886

5987
dag OutOperandList = outs;
6088
dag InOperandList = ins;
61-
let AsmString = asmstr;
89+
let AsmString = opcodestr # "\t" # argstr;
6290
let Pattern = pattern;
6391

6492
let TSFlags{3-0} = format.Value;
6593
}
6694

6795
// Pseudo instructions
6896
class Pseudo<dag outs, dag ins, list<dag> pattern>
69-
: RISCVInst<outs, ins, "", pattern, InstFormatPseudo> {
97+
: RVInst<outs, ins, "", "", pattern, InstFormatPseudo> {
7098
let isPseudo = 1;
7199
let isCodeGenOnly = 1;
72100
}
73101

74-
class FR<bits<7> funct7, bits<3> funct3, bits<7> opcode, dag outs, dag ins,
75-
string asmstr, list<dag> pattern> : RISCVInst<outs, ins, asmstr, pattern, InstFormatR>
76-
{
102+
// Instruction formats are listed in the order they appear in the RISC-V
103+
// instruction set manual (R, I, S, B, U, J) with sub-formats (e.g. RVInstR4,
104+
// RVInstRAtomic) sorted alphabetically.
105+
106+
class RVInstR<bits<7> funct7, bits<3> funct3, RISCVOpcode opcode, dag outs,
107+
dag ins, string opcodestr, string argstr>
108+
: RVInst<outs, ins, opcodestr, argstr, [], InstFormatR> {
77109
bits<5> rs2;
78110
bits<5> rs1;
79111
bits<5> rd;
@@ -83,12 +115,12 @@ class FR<bits<7> funct7, bits<3> funct3, bits<7> opcode, dag outs, dag ins,
83115
let Inst{19-15} = rs1;
84116
let Inst{14-12} = funct3;
85117
let Inst{11-7} = rd;
86-
let Opcode = opcode;
118+
let Opcode = opcode.Value;
87119
}
88120

89-
class FI<bits<3> funct3, bits<7> opcode, dag outs, dag ins, string asmstr, list<dag> pattern>
90-
: RISCVInst<outs, ins, asmstr, pattern, InstFormatI>
91-
{
121+
class RVInstI<bits<3> funct3, RISCVOpcode opcode, dag outs, dag ins,
122+
string opcodestr, string argstr>
123+
: RVInst<outs, ins, opcodestr, argstr, [], InstFormatI> {
92124
bits<12> imm12;
93125
bits<5> rs1;
94126
bits<5> rd;
@@ -97,12 +129,12 @@ class FI<bits<3> funct3, bits<7> opcode, dag outs, dag ins, string asmstr, list<
97129
let Inst{19-15} = rs1;
98130
let Inst{14-12} = funct3;
99131
let Inst{11-7} = rd;
100-
let Opcode = opcode;
132+
let Opcode = opcode.Value;
101133
}
102134

103-
class FI32Shift<bit arithshift, bits<3> funct3, bits<7> opcode, dag outs, dag ins, string asmstr, list<dag> pattern>
104-
: RISCVInst<outs, ins, asmstr, pattern, InstFormatI>
105-
{
135+
class RVInstIShift<bit arithshift, bits<3> funct3, RISCVOpcode opcode,
136+
dag outs, dag ins, string opcodestr, string argstr>
137+
: RVInst<outs, ins, opcodestr, argstr, [], InstFormatI> {
106138
bits<5> shamt;
107139
bits<5> rs1;
108140
bits<5> rd;
@@ -114,12 +146,12 @@ class FI32Shift<bit arithshift, bits<3> funct3, bits<7> opcode, dag outs, dag in
114146
let Inst{19-15} = rs1;
115147
let Inst{14-12} = funct3;
116148
let Inst{11-7} = rd;
117-
let Opcode = opcode;
149+
let Opcode = opcode.Value;
118150
}
119151

120-
class FS<bits<3> funct3, bits<7> opcode, dag outs, dag ins, string asmstr, list<dag> pattern>
121-
: RISCVInst<outs, ins, asmstr, pattern, InstFormatS>
122-
{
152+
class RVInstS<bits<3> funct3, RISCVOpcode opcode, dag outs, dag ins,
153+
string opcodestr, string argstr>
154+
: RVInst<outs, ins, opcodestr, argstr, [], InstFormatS> {
123155
bits<12> imm12;
124156
bits<5> rs2;
125157
bits<5> rs1;
@@ -129,12 +161,12 @@ class FS<bits<3> funct3, bits<7> opcode, dag outs, dag ins, string asmstr, list<
129161
let Inst{19-15} = rs1;
130162
let Inst{14-12} = funct3;
131163
let Inst{11-7} = imm12{4-0};
132-
let Opcode = opcode;
164+
let Opcode = opcode.Value;
133165
}
134166

135-
class FSB<bits<3> funct3, bits<7> opcode, dag outs, dag ins, string asmstr, list<dag> pattern>
136-
: RISCVInst<outs, ins, asmstr, pattern, InstFormatSB>
137-
{
167+
class RVInstB<bits<3> funct3, RISCVOpcode opcode, dag outs, dag ins,
168+
string opcodestr, string argstr>
169+
: RVInst<outs, ins, opcodestr, argstr, [], InstFormatB> {
138170
bits<12> imm12;
139171
bits<5> rs2;
140172
bits<5> rs1;
@@ -146,23 +178,23 @@ class FSB<bits<3> funct3, bits<7> opcode, dag outs, dag ins, string asmstr, list
146178
let Inst{14-12} = funct3;
147179
let Inst{11-8} = imm12{3-0};
148180
let Inst{7} = imm12{10};
149-
let Opcode = opcode;
181+
let Opcode = opcode.Value;
150182
}
151183

152-
class FU<bits<7> opcode, dag outs, dag ins, string asmstr, list<dag> pattern>
153-
: RISCVInst<outs, ins, asmstr, pattern, InstFormatU>
154-
{
184+
class RVInstU<RISCVOpcode opcode, dag outs, dag ins, string opcodestr,
185+
string argstr>
186+
: RVInst<outs, ins, opcodestr, argstr, [], InstFormatU> {
155187
bits<20> imm20;
156188
bits<5> rd;
157189

158190
let Inst{31-12} = imm20;
159191
let Inst{11-7} = rd;
160-
let Opcode = opcode;
192+
let Opcode = opcode.Value;
161193
}
162194

163-
class FUJ<bits<7> opcode, dag outs, dag ins, string asmstr, list<dag> pattern>
164-
: RISCVInst<outs, ins, asmstr, pattern, InstFormatU>
165-
{
195+
class RVInstJ<RISCVOpcode opcode, dag outs, dag ins, string opcodestr,
196+
string argstr>
197+
: RVInst<outs, ins, opcodestr, argstr, [], InstFormatJ> {
166198
bits<20> imm20;
167199
bits<5> rd;
168200

@@ -171,5 +203,5 @@ class FUJ<bits<7> opcode, dag outs, dag ins, string asmstr, list<dag> pattern>
171203
let Inst{20} = imm20{10};
172204
let Inst{19-12} = imm20{18-11};
173205
let Inst{11-7} = rd;
174-
let Opcode = opcode;
206+
let Opcode = opcode.Value;
175207
}

0 commit comments

Comments
 (0)