1 //===-- RISCVInstrFormats.td - RISCV Instruction Formats ---*- tablegen -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 //===----------------------------------------------------------------------===//
11 // These instruction format definitions are structured to match the
12 // description in the RISC-V User-Level ISA specification as closely as
13 // possible. For instance, the specification describes instructions with the
14 // MSB (31st bit) on the left and the LSB (0th bit) on the right. This is
15 // reflected in the order of parameters to each instruction class.
17 // One area of divergence is in the description of immediates. The
18 // specification describes immediate encoding in terms of bit-slicing
19 // operations on the logical value represented. The immediate argument to
20 // these instruction formats instead represents the bit sequence that will be
21 // inserted into the instruction. e.g. although JAL's immediate is logically
22 // a 21-bit value (where the LSB is always zero), we describe it as an imm20
23 // to match how it is encoded.
25 //===----------------------------------------------------------------------===//
27 // Format specifies the encoding used by the instruction. This is used by
28 // RISCVMCCodeEmitter to determine which form of fixup to use. These
29 // definitions must be kept in-sync with RISCVBaseInfo.h.
30 class InstFormat<bits<5> val> {
33 def InstFormatPseudo : InstFormat<0>;
34 def InstFormatR : InstFormat<1>;
35 def InstFormatR4 : InstFormat<2>;
36 def InstFormatI : InstFormat<3>;
37 def InstFormatS : InstFormat<4>;
38 def InstFormatB : InstFormat<5>;
39 def InstFormatU : InstFormat<6>;
40 def InstFormatJ : InstFormat<7>;
41 def InstFormatCR : InstFormat<8>;
42 def InstFormatCI : InstFormat<9>;
43 def InstFormatCSS : InstFormat<10>;
44 def InstFormatCIW : InstFormat<11>;
45 def InstFormatCL : InstFormat<12>;
46 def InstFormatCS : InstFormat<13>;
47 def InstFormatCA : InstFormat<14>;
48 def InstFormatCB : InstFormat<15>;
49 def InstFormatCJ : InstFormat<16>;
50 def InstFormatOther : InstFormat<17>;
52 class RISCVVConstraint<bits<3> val> {
55 def NoConstraint : RISCVVConstraint<0b000>;
56 def VS2Constraint : RISCVVConstraint<0b001>;
57 def VS1Constraint : RISCVVConstraint<0b010>;
58 def VMConstraint : RISCVVConstraint<0b100>;
60 // Illegal instructions:
62 // * The destination vector register group for a masked vector instruction
63 // cannot overlap the source mask register (v0), unless the destination vector
64 // register is being written with a mask value (e.g., comparisons) or the
65 // scalar result of a reduction.
67 // * Widening: The destination EEW is greater than the source EEW, the source
68 // EMUL is at least 1. The destination vector register group cannot overlap
69 // with the source vector register groups besides the highest-numbered part of
70 // the destination register group.
72 // * Narrowing: The destination EEW is smaller than the source EEW. The
73 // destination vector register group cannot overlap with the source vector
74 // register groups besides the lowest-numbered part of the source register
77 // * vmsbf.m/vmsif.m/vmsof.m: The destination register cannot overlap the
78 // source register and, if masked, cannot overlap the mask register ('v0').
80 // * viota: The destination register cannot overlap the source register and,
81 // if masked, cannot overlap the mask register ('v0').
83 // * v[f]slide[1]up: The destination vector register group for vslideup cannot
84 // overlap the source vector register group.
86 // * vrgather: The destination vector register group cannot overlap with the
87 // source vector register groups.
89 // * vcompress: The destination vector register group cannot overlap the
90 // source vector register group or the source mask register
91 def WidenV : RISCVVConstraint<!or(VS2Constraint.Value,
94 def WidenW : RISCVVConstraint<!or(VS1Constraint.Value,
96 def WidenCvt : RISCVVConstraint<!or(VS2Constraint.Value,
98 def Iota : RISCVVConstraint<!or(VS2Constraint.Value,
100 def SlideUp : RISCVVConstraint<!or(VS2Constraint.Value,
101 VMConstraint.Value)>;
102 def Vrgather : RISCVVConstraint<!or(VS2Constraint.Value,
104 VMConstraint.Value)>;
105 def Vcompress : RISCVVConstraint<!or(VS2Constraint.Value,
106 VS1Constraint.Value)>;
108 // The following opcode names match those given in Table 19.1 in the
109 // RISC-V User-level ISA specification ("RISC-V base opcode map").
110 class RISCVOpcode<bits<7> val> {
113 def OPC_LOAD : RISCVOpcode<0b0000011>;
114 def OPC_LOAD_FP : RISCVOpcode<0b0000111>;
115 def OPC_MISC_MEM : RISCVOpcode<0b0001111>;
116 def OPC_OP_IMM : RISCVOpcode<0b0010011>;
117 def OPC_AUIPC : RISCVOpcode<0b0010111>;
118 def OPC_OP_IMM_32 : RISCVOpcode<0b0011011>;
119 def OPC_STORE : RISCVOpcode<0b0100011>;
120 def OPC_STORE_FP : RISCVOpcode<0b0100111>;
121 def OPC_AMO : RISCVOpcode<0b0101111>;
122 def OPC_OP : RISCVOpcode<0b0110011>;
123 def OPC_LUI : RISCVOpcode<0b0110111>;
124 def OPC_OP_32 : RISCVOpcode<0b0111011>;
125 def OPC_MADD : RISCVOpcode<0b1000011>;
126 def OPC_MSUB : RISCVOpcode<0b1000111>;
127 def OPC_NMSUB : RISCVOpcode<0b1001011>;
128 def OPC_NMADD : RISCVOpcode<0b1001111>;
129 def OPC_OP_FP : RISCVOpcode<0b1010011>;
130 def OPC_OP_V : RISCVOpcode<0b1010111>;
131 def OPC_BRANCH : RISCVOpcode<0b1100011>;
132 def OPC_JALR : RISCVOpcode<0b1100111>;
133 def OPC_JAL : RISCVOpcode<0b1101111>;
134 def OPC_SYSTEM : RISCVOpcode<0b1110011>;
136 class RVInst<dag outs, dag ins, string opcodestr, string argstr,
137 list<dag> pattern, InstFormat format>
140 // SoftFail is a field the disassembler can use to provide a way for
141 // instructions to not match without killing the whole decode process. It is
142 // mainly used for ARM, but Tablegen expects this field to exist or it fails
143 // to build the decode table.
144 field bits<32> SoftFail = 0;
149 let Inst{6-0} = Opcode;
151 let Namespace = "RISCV";
153 dag OutOperandList = outs;
154 dag InOperandList = ins;
155 let AsmString = opcodestr # "\t" # argstr;
156 let Pattern = pattern;
158 let TSFlags{4-0} = format.Value;
161 RISCVVConstraint RVVConstraint = NoConstraint;
162 let TSFlags{7-5} = RVVConstraint.Value;
165 let TSFlags{10-8} = VLMul;
167 bit HasDummyMask = 0;
168 let TSFlags{11} = HasDummyMask;
170 bit ForceTailAgnostic = false;
171 let TSFlags{12} = ForceTailAgnostic;
174 let TSFlags{13} = HasMergeOp;
177 let TSFlags{14} = HasSEWOp;
180 let TSFlags{15} = HasVLOp;
182 bit HasVecPolicyOp = 0;
183 let TSFlags{16} = HasVecPolicyOp;
186 // Pseudo instructions
187 class Pseudo<dag outs, dag ins, list<dag> pattern, string opcodestr = "", string argstr = "">
188 : RVInst<outs, ins, opcodestr, argstr, pattern, InstFormatPseudo>,
191 let isCodeGenOnly = 1;
194 // Pseudo load instructions.
195 class PseudoLoad<string opcodestr, RegisterClass rdty = GPR>
196 : Pseudo<(outs rdty:$rd), (ins bare_symbol:$addr), [], opcodestr, "$rd, $addr"> {
197 let hasSideEffects = 0;
200 let isCodeGenOnly = 0;
201 let isAsmParserOnly = 1;
204 class PseudoFloatLoad<string opcodestr, RegisterClass rdty = GPR>
205 : Pseudo<(outs GPR:$tmp, rdty:$rd), (ins bare_symbol:$addr), [], opcodestr, "$rd, $addr, $tmp"> {
206 let hasSideEffects = 0;
209 let isCodeGenOnly = 0;
210 let isAsmParserOnly = 1;
213 // Pseudo store instructions.
214 class PseudoStore<string opcodestr, RegisterClass rsty = GPR>
215 : Pseudo<(outs GPR:$tmp), (ins rsty:$rs, bare_symbol:$addr), [], opcodestr, "$rs, $addr, $tmp"> {
216 let hasSideEffects = 0;
219 let isCodeGenOnly = 0;
220 let isAsmParserOnly = 1;
223 // Instruction formats are listed in the order they appear in the RISC-V
224 // instruction set manual (R, I, S, B, U, J) with sub-formats (e.g. RVInstR4,
225 // RVInstRAtomic) sorted alphabetically.
227 class RVInstR<bits<7> funct7, bits<3> funct3, RISCVOpcode opcode, dag outs,
228 dag ins, string opcodestr, string argstr>
229 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatR> {
234 let Inst{31-25} = funct7;
235 let Inst{24-20} = rs2;
236 let Inst{19-15} = rs1;
237 let Inst{14-12} = funct3;
239 let Opcode = opcode.Value;
242 class RVInstR4<bits<2> funct2, bits<3> funct3, RISCVOpcode opcode, dag outs,
243 dag ins, string opcodestr, string argstr>
244 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatR4> {
250 let Inst{31-27} = rs3;
251 let Inst{26-25} = funct2;
252 let Inst{24-20} = rs2;
253 let Inst{19-15} = rs1;
254 let Inst{14-12} = funct3;
256 let Opcode = opcode.Value;
259 class RVInstR4Frm<bits<2> funct2, RISCVOpcode opcode, dag outs, dag ins,
260 string opcodestr, string argstr>
261 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatR4> {
268 let Inst{31-27} = rs3;
269 let Inst{26-25} = funct2;
270 let Inst{24-20} = rs2;
271 let Inst{19-15} = rs1;
272 let Inst{14-12} = funct3;
274 let Opcode = opcode.Value;
277 class RVInstRAtomic<bits<5> funct5, bit aq, bit rl, bits<3> funct3,
278 RISCVOpcode opcode, dag outs, dag ins, string opcodestr,
280 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatR> {
285 let Inst{31-27} = funct5;
288 let Inst{24-20} = rs2;
289 let Inst{19-15} = rs1;
290 let Inst{14-12} = funct3;
292 let Opcode = opcode.Value;
295 class RVInstRFrm<bits<7> funct7, RISCVOpcode opcode, dag outs, dag ins,
296 string opcodestr, string argstr>
297 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatR> {
303 let Inst{31-25} = funct7;
304 let Inst{24-20} = rs2;
305 let Inst{19-15} = rs1;
306 let Inst{14-12} = funct3;
308 let Opcode = opcode.Value;
311 class RVInstI<bits<3> funct3, RISCVOpcode opcode, dag outs, dag ins,
312 string opcodestr, string argstr>
313 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatI> {
318 let Inst{31-20} = imm12;
319 let Inst{19-15} = rs1;
320 let Inst{14-12} = funct3;
322 let Opcode = opcode.Value;
325 class RVInstIShift<bits<5> imm11_7, bits<3> funct3, RISCVOpcode opcode,
326 dag outs, dag ins, string opcodestr, string argstr>
327 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatI> {
332 let Inst{31-27} = imm11_7;
334 let Inst{25-20} = shamt;
335 let Inst{19-15} = rs1;
336 let Inst{14-12} = funct3;
338 let Opcode = opcode.Value;
341 class RVInstIShiftW<bits<7> imm11_5, bits<3> funct3, RISCVOpcode opcode,
342 dag outs, dag ins, string opcodestr, string argstr>
343 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatI> {
348 let Inst{31-25} = imm11_5;
349 let Inst{24-20} = shamt;
350 let Inst{19-15} = rs1;
351 let Inst{14-12} = funct3;
353 let Opcode = opcode.Value;
356 class RVInstS<bits<3> funct3, RISCVOpcode opcode, dag outs, dag ins,
357 string opcodestr, string argstr>
358 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatS> {
363 let Inst{31-25} = imm12{11-5};
364 let Inst{24-20} = rs2;
365 let Inst{19-15} = rs1;
366 let Inst{14-12} = funct3;
367 let Inst{11-7} = imm12{4-0};
368 let Opcode = opcode.Value;
371 class RVInstB<bits<3> funct3, RISCVOpcode opcode, dag outs, dag ins,
372 string opcodestr, string argstr>
373 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatB> {
378 let Inst{31} = imm12{11};
379 let Inst{30-25} = imm12{9-4};
380 let Inst{24-20} = rs2;
381 let Inst{19-15} = rs1;
382 let Inst{14-12} = funct3;
383 let Inst{11-8} = imm12{3-0};
384 let Inst{7} = imm12{10};
385 let Opcode = opcode.Value;
388 class RVInstU<RISCVOpcode opcode, dag outs, dag ins, string opcodestr,
390 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatU> {
394 let Inst{31-12} = imm20;
396 let Opcode = opcode.Value;
399 class RVInstJ<RISCVOpcode opcode, dag outs, dag ins, string opcodestr,
401 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatJ> {
405 let Inst{31} = imm20{19};
406 let Inst{30-21} = imm20{9-0};
407 let Inst{20} = imm20{10};
408 let Inst{19-12} = imm20{18-11};
410 let Opcode = opcode.Value;