1 //===-- RISCVInstrFormats.td - RISC-V 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 InstFormatCU : InstFormat<17>;
51 def InstFormatCLB : InstFormat<18>;
52 def InstFormatCLH : InstFormat<19>;
53 def InstFormatCSB : InstFormat<20>;
54 def InstFormatCSH : InstFormat<21>;
55 def InstFormatOther : InstFormat<22>;
57 class RISCVVConstraint<bits<3> val> {
60 def NoConstraint : RISCVVConstraint<0b000>;
61 def VS2Constraint : RISCVVConstraint<0b001>;
62 def VS1Constraint : RISCVVConstraint<0b010>;
63 def VMConstraint : RISCVVConstraint<0b100>;
65 // Illegal instructions:
67 // * The destination vector register group for a masked vector instruction
68 // cannot overlap the source mask register (v0), unless the destination vector
69 // register is being written with a mask value (e.g., comparisons) or the
70 // scalar result of a reduction.
72 // * Widening: The destination EEW is greater than the source EEW, the source
73 // EMUL is at least 1. The destination vector register group cannot overlap
74 // with the source vector register groups besides the highest-numbered part of
75 // the destination register group.
77 // * Narrowing: The destination EEW is smaller than the source EEW. The
78 // destination vector register group cannot overlap with the source vector
79 // register groups besides the lowest-numbered part of the source register
82 // * vmsbf.m/vmsif.m/vmsof.m: The destination register cannot overlap the
83 // source register and, if masked, cannot overlap the mask register ('v0').
85 // * viota: The destination register cannot overlap the source register and,
86 // if masked, cannot overlap the mask register ('v0').
88 // * v[f]slide[1]up: The destination vector register group for vslideup cannot
89 // overlap the source vector register group.
91 // * vrgather: The destination vector register group cannot overlap with the
92 // source vector register groups.
94 // * vcompress: The destination vector register group cannot overlap the
95 // source vector register group or the source mask register
96 def WidenV : RISCVVConstraint<!or(VS2Constraint.Value,
99 def WidenW : RISCVVConstraint<!or(VS1Constraint.Value,
100 VMConstraint.Value)>;
101 def WidenCvt : RISCVVConstraint<!or(VS2Constraint.Value,
102 VMConstraint.Value)>;
103 def Iota : RISCVVConstraint<!or(VS2Constraint.Value,
104 VMConstraint.Value)>;
105 def SlideUp : RISCVVConstraint<!or(VS2Constraint.Value,
106 VMConstraint.Value)>;
107 def Vrgather : RISCVVConstraint<!or(VS2Constraint.Value,
109 VMConstraint.Value)>;
110 def Vcompress : RISCVVConstraint<!or(VS2Constraint.Value,
111 VS1Constraint.Value)>;
113 // The following opcode names match those given in Table 19.1 in the
114 // RISC-V User-level ISA specification ("RISC-V base opcode map").
115 class RISCVOpcode<string name, bits<7> val> {
119 def RISCVOpcodesList : GenericTable {
120 let FilterClass = "RISCVOpcode";
124 let PrimaryKey = [ "Value" ];
125 let PrimaryKeyName = "lookupRISCVOpcodeByValue";
127 def lookupRISCVOpcodeByName : SearchIndex {
128 let Table = RISCVOpcodesList;
129 let Key = [ "Name" ];
131 def OPC_LOAD : RISCVOpcode<"LOAD", 0b0000011>;
132 def OPC_LOAD_FP : RISCVOpcode<"LOAD_FP", 0b0000111>;
133 def OPC_CUSTOM_0 : RISCVOpcode<"CUSTOM_0", 0b0001011>;
134 def OPC_MISC_MEM : RISCVOpcode<"MISC_MEM", 0b0001111>;
135 def OPC_OP_IMM : RISCVOpcode<"OP_IMM", 0b0010011>;
136 def OPC_AUIPC : RISCVOpcode<"AUIPC", 0b0010111>;
137 def OPC_OP_IMM_32 : RISCVOpcode<"OP_IMM_32", 0b0011011>;
138 def OPC_STORE : RISCVOpcode<"STORE", 0b0100011>;
139 def OPC_STORE_FP : RISCVOpcode<"STORE_FP", 0b0100111>;
140 def OPC_CUSTOM_1 : RISCVOpcode<"CUSTOM_1", 0b0101011>;
141 def OPC_AMO : RISCVOpcode<"AMO", 0b0101111>;
142 def OPC_OP : RISCVOpcode<"OP", 0b0110011>;
143 def OPC_LUI : RISCVOpcode<"LUI", 0b0110111>;
144 def OPC_OP_32 : RISCVOpcode<"OP_32", 0b0111011>;
145 def OPC_MADD : RISCVOpcode<"MADD", 0b1000011>;
146 def OPC_MSUB : RISCVOpcode<"MSUB", 0b1000111>;
147 def OPC_NMSUB : RISCVOpcode<"NMSUB", 0b1001011>;
148 def OPC_NMADD : RISCVOpcode<"NMADD", 0b1001111>;
149 def OPC_OP_FP : RISCVOpcode<"OP_FP", 0b1010011>;
150 def OPC_OP_V : RISCVOpcode<"OP_V", 0b1010111>;
151 def OPC_CUSTOM_2 : RISCVOpcode<"CUSTOM_2", 0b1011011>;
152 def OPC_BRANCH : RISCVOpcode<"BRANCH", 0b1100011>;
153 def OPC_JALR : RISCVOpcode<"JALR", 0b1100111>;
154 def OPC_JAL : RISCVOpcode<"JAL", 0b1101111>;
155 def OPC_SYSTEM : RISCVOpcode<"SYSTEM", 0b1110011>;
156 def OPC_OP_P : RISCVOpcode<"OP_P", 0b1110111>;
157 def OPC_CUSTOM_3 : RISCVOpcode<"CUSTOM_3", 0b1111011>;
159 class RVInstCommon<dag outs, dag ins, string opcodestr, string argstr,
160 list<dag> pattern, InstFormat format> : Instruction {
161 let Namespace = "RISCV";
163 dag OutOperandList = outs;
164 dag InOperandList = ins;
165 let AsmString = opcodestr # !if(!empty(argstr), "", "\t" # argstr);
166 let Pattern = pattern;
168 let TSFlags{4-0} = format.Value;
171 RISCVVConstraint RVVConstraint = NoConstraint;
172 let TSFlags{7-5} = RVVConstraint.Value;
175 let TSFlags{10-8} = VLMul;
177 bit ForceTailAgnostic = false;
178 let TSFlags{11} = ForceTailAgnostic;
180 bit IsTiedPseudo = 0;
181 let TSFlags{12} = IsTiedPseudo;
184 let TSFlags{13} = HasSEWOp;
187 let TSFlags{14} = HasVLOp;
189 bit HasVecPolicyOp = 0;
190 let TSFlags{15} = HasVecPolicyOp;
192 bit IsRVVWideningReduction = 0;
193 let TSFlags{16} = IsRVVWideningReduction;
195 bit UsesMaskPolicy = 0;
196 let TSFlags{17} = UsesMaskPolicy;
198 // Indicates that the result can be considered sign extended from bit 31. Some
199 // instructions with this flag aren't W instructions, but are either sign
200 // extended from a smaller size, always outputs a small integer, or put zeros
201 // in bits 63:31. Used by the SExtWRemoval pass.
202 bit IsSignExtendingOpW = 0;
203 let TSFlags{18} = IsSignExtendingOpW;
205 bit HasRoundModeOp = 0;
206 let TSFlags{19} = HasRoundModeOp;
208 // This is only valid when HasRoundModeOp is set to 1. HasRoundModeOp is set
209 // to 1 for vector fixed-point or floating-point intrinsics. This bit is
210 // processed under pass 'RISCVInsertReadWriteCSR' pass to distinguish between
211 // fixed-point / floating-point instructions and emit appropriate read/write
212 // to the correct CSR.
214 let TSFlags{20} = UsesVXRM;
216 // Indicates whther these instructions can partially overlap between source
217 // registers and destination registers according to the vector spec.
218 // 0 -> not a vector pseudo
219 // 1 -> default value for vector pseudos. not widening or narrowing.
220 // 2 -> narrowing case
221 // 3 -> widening case
222 bits<2> TargetOverlapConstraintType = 0;
223 let TSFlags{22-21} = TargetOverlapConstraintType;
226 class RVInst<dag outs, dag ins, string opcodestr, string argstr,
227 list<dag> pattern, InstFormat format>
228 : RVInstCommon<outs, ins, opcodestr, argstr, pattern, format> {
230 // SoftFail is a field the disassembler can use to provide a way for
231 // instructions to not match without killing the whole decode process. It is
232 // mainly used for ARM, but Tablegen expects this field to exist or it fails
233 // to build the decode table.
234 field bits<32> SoftFail = 0;
238 // Pseudo instructions
239 class Pseudo<dag outs, dag ins, list<dag> pattern, string opcodestr = "", string argstr = "">
240 : RVInst<outs, ins, opcodestr, argstr, pattern, InstFormatPseudo> {
242 let isCodeGenOnly = 1;
245 class PseudoQuietFCMP<DAGOperand Ty>
246 : Pseudo<(outs GPR:$rd), (ins Ty:$rs1, Ty:$rs2), []> {
247 let hasSideEffects = 1;
252 // Pseudo load instructions.
253 class PseudoLoad<string opcodestr>
254 : Pseudo<(outs GPR:$rd), (ins bare_symbol:$addr), [], opcodestr, "$rd, $addr"> {
255 let hasSideEffects = 0;
258 let isCodeGenOnly = 0;
259 let isAsmParserOnly = 1;
262 class PseudoFloatLoad<string opcodestr, RegisterClass rdty>
263 : Pseudo<(outs GPR:$tmp, rdty:$rd), (ins bare_symbol:$addr), [], opcodestr, "$rd, $addr, $tmp"> {
264 let hasSideEffects = 0;
267 let isCodeGenOnly = 0;
268 let isAsmParserOnly = 1;
271 // Pseudo store instructions.
272 class PseudoStore<string opcodestr, RegisterClass rsty = GPR>
273 : Pseudo<(outs GPR:$tmp), (ins rsty:$rs, bare_symbol:$addr), [], opcodestr, "$rs, $addr, $tmp"> {
274 let hasSideEffects = 0;
277 let isCodeGenOnly = 0;
278 let isAsmParserOnly = 1;
281 // Instruction formats are listed in the order they appear in the RISC-V
282 // instruction set manual (R, R4, I, S, B, U, J).
284 // Common base class for R format instructions. Bits {31-25} should be set by
286 class RVInstRBase<bits<3> funct3, RISCVOpcode opcode, dag outs,
287 dag ins, string opcodestr, string argstr>
288 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatR> {
293 let Inst{24-20} = rs2;
294 let Inst{19-15} = rs1;
295 let Inst{14-12} = funct3;
297 let Inst{6-0} = opcode.Value;
300 class RVInstR<bits<7> funct7, bits<3> funct3, RISCVOpcode opcode, dag outs,
301 dag ins, string opcodestr, string argstr>
302 : RVInstRBase<funct3, opcode, outs, ins, opcodestr, argstr> {
303 let Inst{31-25} = funct7;
306 class RVInstRAtomic<bits<5> funct5, bit aq, bit rl, bits<3> funct3,
307 RISCVOpcode opcode, dag outs, dag ins, string opcodestr,
309 : RVInstRBase<funct3, opcode, outs, ins, opcodestr, argstr> {
310 let Inst{31-27} = funct5;
315 class RVInstRFrm<bits<7> funct7, RISCVOpcode opcode, dag outs, dag ins,
316 string opcodestr, string argstr>
317 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatR> {
323 let Inst{31-25} = funct7;
324 let Inst{24-20} = rs2;
325 let Inst{19-15} = rs1;
326 let Inst{14-12} = frm;
328 let Inst{6-0} = opcode.Value;
331 class RVInstR4<bits<2> funct2, bits<3> funct3, RISCVOpcode opcode, dag outs,
332 dag ins, string opcodestr, string argstr>
333 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatR4> {
339 let Inst{31-27} = rs3;
340 let Inst{26-25} = funct2;
341 let Inst{24-20} = rs2;
342 let Inst{19-15} = rs1;
343 let Inst{14-12} = funct3;
345 let Inst{6-0} = opcode.Value;
348 class RVInstR4Frm<bits<2> funct2, RISCVOpcode opcode, dag outs, dag ins,
349 string opcodestr, string argstr>
350 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatR4> {
357 let Inst{31-27} = rs3;
358 let Inst{26-25} = funct2;
359 let Inst{24-20} = rs2;
360 let Inst{19-15} = rs1;
361 let Inst{14-12} = frm;
363 let Inst{6-0} = opcode.Value;
366 // Common base class for I format instructions. Bits {31-20} should be set by
368 class RVInstIBase<bits<3> funct3, RISCVOpcode opcode, dag outs, dag ins,
369 string opcodestr, string argstr>
370 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatI> {
374 let Inst{19-15} = rs1;
375 let Inst{14-12} = funct3;
377 let Inst{6-0} = opcode.Value;
380 class RVInstI<bits<3> funct3, RISCVOpcode opcode, dag outs, dag ins,
381 string opcodestr, string argstr>
382 : RVInstIBase<funct3, opcode, outs, ins, opcodestr, argstr> {
385 let Inst{31-20} = imm12;
388 class RVInstIShift<bits<5> imm11_7, bits<3> funct3, RISCVOpcode opcode,
389 dag outs, dag ins, string opcodestr, string argstr>
390 : RVInstIBase<funct3, opcode, outs, ins, opcodestr, argstr> {
393 let Inst{31-27} = imm11_7;
395 let Inst{25-20} = shamt;
398 class RVInstIShiftW<bits<7> imm11_5, bits<3> funct3, RISCVOpcode opcode,
399 dag outs, dag ins, string opcodestr, string argstr>
400 : RVInstIBase<funct3, opcode, outs, ins, opcodestr, argstr> {
403 let Inst{31-25} = imm11_5;
404 let Inst{24-20} = shamt;
407 class RVInstIUnary<bits<12> imm12, bits<3> funct3, RISCVOpcode opcode,
408 dag outs, dag ins, string opcodestr, string argstr>
409 : RVInstIBase<funct3, opcode, outs, ins, opcodestr, argstr> {
410 let Inst{31-20} = imm12;
413 class RVInstS<bits<3> funct3, RISCVOpcode opcode, dag outs, dag ins,
414 string opcodestr, string argstr>
415 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatS> {
420 let Inst{31-25} = imm12{11-5};
421 let Inst{24-20} = rs2;
422 let Inst{19-15} = rs1;
423 let Inst{14-12} = funct3;
424 let Inst{11-7} = imm12{4-0};
425 let Inst{6-0} = opcode.Value;
428 class RVInstB<bits<3> funct3, RISCVOpcode opcode, dag outs, dag ins,
429 string opcodestr, string argstr>
430 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatB> {
435 let Inst{31} = imm12{11};
436 let Inst{30-25} = imm12{9-4};
437 let Inst{24-20} = rs2;
438 let Inst{19-15} = rs1;
439 let Inst{14-12} = funct3;
440 let Inst{11-8} = imm12{3-0};
441 let Inst{7} = imm12{10};
442 let Inst{6-0} = opcode.Value;
445 class RVInstU<RISCVOpcode opcode, dag outs, dag ins, string opcodestr,
447 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatU> {
451 let Inst{31-12} = imm20;
453 let Inst{6-0} = opcode.Value;
456 class RVInstJ<RISCVOpcode opcode, dag outs, dag ins, string opcodestr,
458 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatJ> {
462 let Inst{31} = imm20{19};
463 let Inst{30-21} = imm20{9-0};
464 let Inst{20} = imm20{10};
465 let Inst{19-12} = imm20{18-11};
467 let Inst{6-0} = opcode.Value;
470 //===----------------------------------------------------------------------===//
471 // Instruction classes for .insn directives
472 //===----------------------------------------------------------------------===//
474 class DirectiveInsnR<dag outs, dag ins, string argstr>
475 : RVInst<outs, ins, "", "", [], InstFormatR> {
484 let Inst{31-25} = funct7;
485 let Inst{24-20} = rs2;
486 let Inst{19-15} = rs1;
487 let Inst{14-12} = funct3;
489 let Inst{6-0} = opcode;
491 let AsmString = ".insn r " # argstr;
494 class DirectiveInsnR4<dag outs, dag ins, string argstr>
495 : RVInst<outs, ins, "", "", [], InstFormatR4> {
505 let Inst{31-27} = rs3;
506 let Inst{26-25} = funct2;
507 let Inst{24-20} = rs2;
508 let Inst{19-15} = rs1;
509 let Inst{14-12} = funct3;
511 let Inst{6-0} = opcode;
513 let AsmString = ".insn r4 " # argstr;
516 class DirectiveInsnI<dag outs, dag ins, string argstr>
517 : RVInst<outs, ins, "", "", [], InstFormatI> {
525 let Inst{31-20} = imm12;
526 let Inst{19-15} = rs1;
527 let Inst{14-12} = funct3;
529 let Inst{6-0} = opcode;
531 let AsmString = ".insn i " # argstr;
534 class DirectiveInsnS<dag outs, dag ins, string argstr>
535 : RVInst<outs, ins, "", "", [], InstFormatS> {
543 let Inst{31-25} = imm12{11-5};
544 let Inst{24-20} = rs2;
545 let Inst{19-15} = rs1;
546 let Inst{14-12} = funct3;
547 let Inst{11-7} = imm12{4-0};
548 let Inst{6-0} = opcode;
550 let AsmString = ".insn s " # argstr;
553 class DirectiveInsnB<dag outs, dag ins, string argstr>
554 : RVInst<outs, ins, "", "", [], InstFormatB> {
562 let Inst{31} = imm12{11};
563 let Inst{30-25} = imm12{9-4};
564 let Inst{24-20} = rs2;
565 let Inst{19-15} = rs1;
566 let Inst{14-12} = funct3;
567 let Inst{11-8} = imm12{3-0};
568 let Inst{7} = imm12{10};
569 let Inst{6-0} = opcode;
571 let AsmString = ".insn b " # argstr;
574 class DirectiveInsnU<dag outs, dag ins, string argstr>
575 : RVInst<outs, ins, "", "", [], InstFormatU> {
581 let Inst{31-12} = imm20;
583 let Inst{6-0} = opcode;
585 let AsmString = ".insn u " # argstr;
588 class DirectiveInsnJ<dag outs, dag ins, string argstr>
589 : RVInst<outs, ins, "", "", [], InstFormatJ> {
595 let Inst{31-12} = imm20;
597 let Inst{6-0} = opcode;
599 let AsmString = ".insn j " # argstr;