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)>;
112 def Sha2Constraint : RISCVVConstraint<!or(VS2Constraint.Value,
113 VS1Constraint.Value)>;
115 // The following opcode names match those given in Table 19.1 in the
116 // RISC-V User-level ISA specification ("RISC-V base opcode map").
117 class RISCVOpcode<string name, bits<7> val> {
121 def RISCVOpcodesList : GenericTable {
122 let FilterClass = "RISCVOpcode";
126 let PrimaryKey = [ "Value" ];
127 let PrimaryKeyName = "lookupRISCVOpcodeByValue";
129 def lookupRISCVOpcodeByName : SearchIndex {
130 let Table = RISCVOpcodesList;
131 let Key = [ "Name" ];
133 def OPC_LOAD : RISCVOpcode<"LOAD", 0b0000011>;
134 def OPC_LOAD_FP : RISCVOpcode<"LOAD_FP", 0b0000111>;
135 def OPC_CUSTOM_0 : RISCVOpcode<"CUSTOM_0", 0b0001011>;
136 def OPC_MISC_MEM : RISCVOpcode<"MISC_MEM", 0b0001111>;
137 def OPC_OP_IMM : RISCVOpcode<"OP_IMM", 0b0010011>;
138 def OPC_AUIPC : RISCVOpcode<"AUIPC", 0b0010111>;
139 def OPC_OP_IMM_32 : RISCVOpcode<"OP_IMM_32", 0b0011011>;
140 def OPC_STORE : RISCVOpcode<"STORE", 0b0100011>;
141 def OPC_STORE_FP : RISCVOpcode<"STORE_FP", 0b0100111>;
142 def OPC_CUSTOM_1 : RISCVOpcode<"CUSTOM_1", 0b0101011>;
143 def OPC_AMO : RISCVOpcode<"AMO", 0b0101111>;
144 def OPC_OP : RISCVOpcode<"OP", 0b0110011>;
145 def OPC_LUI : RISCVOpcode<"LUI", 0b0110111>;
146 def OPC_OP_32 : RISCVOpcode<"OP_32", 0b0111011>;
147 def OPC_MADD : RISCVOpcode<"MADD", 0b1000011>;
148 def OPC_MSUB : RISCVOpcode<"MSUB", 0b1000111>;
149 def OPC_NMSUB : RISCVOpcode<"NMSUB", 0b1001011>;
150 def OPC_NMADD : RISCVOpcode<"NMADD", 0b1001111>;
151 def OPC_OP_FP : RISCVOpcode<"OP_FP", 0b1010011>;
152 def OPC_OP_V : RISCVOpcode<"OP_V", 0b1010111>;
153 def OPC_CUSTOM_2 : RISCVOpcode<"CUSTOM_2", 0b1011011>;
154 def OPC_BRANCH : RISCVOpcode<"BRANCH", 0b1100011>;
155 def OPC_JALR : RISCVOpcode<"JALR", 0b1100111>;
156 def OPC_JAL : RISCVOpcode<"JAL", 0b1101111>;
157 def OPC_SYSTEM : RISCVOpcode<"SYSTEM", 0b1110011>;
158 def OPC_OP_VE : RISCVOpcode<"OP_VE", 0b1110111>;
159 def OPC_CUSTOM_3 : RISCVOpcode<"CUSTOM_3", 0b1111011>;
161 class EltDeps<bit vl, bit mask> {
166 def EltDepsNone : EltDeps<vl=0, mask=0>;
167 def EltDepsVL : EltDeps<vl=1, mask=0>;
168 def EltDepsVLMask : EltDeps<vl=1, mask=1>;
170 class EEW <bits<2> val> {
174 def EEWSEWx1 : EEW<1>;
175 def EEWSEWx2 : EEW<2>;
176 def EEWSEWx4 : EEW<3>;
178 class RVInstCommon<dag outs, dag ins, string opcodestr, string argstr,
179 list<dag> pattern, InstFormat format> : Instruction {
180 let Namespace = "RISCV";
182 dag OutOperandList = outs;
183 dag InOperandList = ins;
184 let AsmString = opcodestr # !if(!empty(argstr), "", "\t" # argstr);
185 let Pattern = pattern;
187 let TSFlags{4-0} = format.Value;
190 RISCVVConstraint RVVConstraint = NoConstraint;
191 let TSFlags{7-5} = RVVConstraint.Value;
194 let TSFlags{10-8} = VLMul;
196 bit ForceTailAgnostic = false;
197 let TSFlags{11} = ForceTailAgnostic;
199 bit IsTiedPseudo = 0;
200 let TSFlags{12} = IsTiedPseudo;
203 let TSFlags{13} = HasSEWOp;
206 let TSFlags{14} = HasVLOp;
208 bit HasVecPolicyOp = 0;
209 let TSFlags{15} = HasVecPolicyOp;
211 bit IsRVVWideningReduction = 0;
212 let TSFlags{16} = IsRVVWideningReduction;
214 bit UsesMaskPolicy = 0;
215 let TSFlags{17} = UsesMaskPolicy;
217 // Indicates that the result can be considered sign extended from bit 31. Some
218 // instructions with this flag aren't W instructions, but are either sign
219 // extended from a smaller size, always outputs a small integer, or put zeros
220 // in bits 63:31. Used by the SExtWRemoval pass.
221 bit IsSignExtendingOpW = 0;
222 let TSFlags{18} = IsSignExtendingOpW;
224 bit HasRoundModeOp = 0;
225 let TSFlags{19} = HasRoundModeOp;
227 // This is only valid when HasRoundModeOp is set to 1. HasRoundModeOp is set
228 // to 1 for vector fixed-point or floating-point intrinsics. This bit is
229 // processed under pass 'RISCVInsertReadWriteCSR' pass to distinguish between
230 // fixed-point / floating-point instructions and emit appropriate read/write
231 // to the correct CSR.
233 let TSFlags{20} = UsesVXRM;
235 // Indicates whther these instructions can partially overlap between source
236 // registers and destination registers according to the vector spec.
237 // 0 -> not a vector pseudo
238 // 1 -> default value for vector pseudos. not widening or narrowing.
239 // 2 -> narrowing case
240 // 3 -> widening case
241 bits<2> TargetOverlapConstraintType = 0;
242 let TSFlags{22-21} = TargetOverlapConstraintType;
244 // Most vector instructions are elementwise, but some may depend on the value
245 // of VL (e.g. vslide1down.vx), and others may depend on the VL and mask
246 // (e.g. vredsum.vs, viota.m). Mark these instructions so that peepholes avoid
247 // changing their VL and/or mask.
248 EltDeps ElementsDependOn = EltDepsNone;
249 let TSFlags{23} = ElementsDependOn.VL;
250 let TSFlags{24} = ElementsDependOn.Mask;
252 // Indicates the EEW of a vector instruction's destination operand.
253 EEW DestEEW = EEWSEWx1;
254 let TSFlags{26-25} = DestEEW.Value;
257 class RVInst<dag outs, dag ins, string opcodestr, string argstr,
258 list<dag> pattern, InstFormat format>
259 : RVInstCommon<outs, ins, opcodestr, argstr, pattern, format> {
261 // SoftFail is a field the disassembler can use to provide a way for
262 // instructions to not match without killing the whole decode process. It is
263 // mainly used for ARM, but Tablegen expects this field to exist or it fails
264 // to build the decode table.
265 field bits<32> SoftFail = 0;
269 class RVInst48<dag outs, dag ins, string opcodestr, string argstr,
270 list<dag> pattern, InstFormat format>
271 : RVInstCommon<outs, ins, opcodestr, argstr, pattern, format> {
273 field bits<48> SoftFail = 0;
277 class RVInst64<dag outs, dag ins, string opcodestr, string argstr,
278 list<dag> pattern, InstFormat format>
279 : RVInstCommon<outs, ins, opcodestr, argstr, pattern, format> {
281 field bits<64> SoftFail = 0;
285 // Pseudo instructions
286 class Pseudo<dag outs, dag ins, list<dag> pattern, string opcodestr = "", string argstr = "">
287 : RVInst<outs, ins, opcodestr, argstr, pattern, InstFormatPseudo> {
289 let isCodeGenOnly = 1;
292 class PseudoQuietFCMP<DAGOperand Ty>
293 : Pseudo<(outs GPR:$rd), (ins Ty:$rs1, Ty:$rs2), []> {
294 let hasSideEffects = 1;
299 // Pseudo load instructions.
300 class PseudoLoad<string opcodestr>
301 : Pseudo<(outs GPR:$rd), (ins bare_symbol:$addr), [], opcodestr, "$rd, $addr"> {
302 let hasSideEffects = 0;
305 let isCodeGenOnly = 0;
306 let isAsmParserOnly = 1;
309 class PseudoFloatLoad<string opcodestr, RegisterClass rdty>
310 : Pseudo<(outs GPR:$tmp, rdty:$rd), (ins bare_symbol:$addr), [], opcodestr, "$rd, $addr, $tmp"> {
311 let hasSideEffects = 0;
314 let isCodeGenOnly = 0;
315 let isAsmParserOnly = 1;
318 // Pseudo store instructions.
319 class PseudoStore<string opcodestr, RegisterClass rsty = GPR>
320 : Pseudo<(outs GPR:$tmp), (ins rsty:$rs, bare_symbol:$addr), [], opcodestr, "$rs, $addr, $tmp"> {
321 let hasSideEffects = 0;
324 let isCodeGenOnly = 0;
325 let isAsmParserOnly = 1;
328 // Instruction formats are listed in the order they appear in the RISC-V
329 // instruction set manual (R, R4, I, S, B, U, J).
331 // Common base class for R format instructions. Bits {31-25} should be set by
333 class RVInstRBase<bits<3> funct3, RISCVOpcode opcode, dag outs,
334 dag ins, string opcodestr, string argstr>
335 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatR> {
340 let Inst{24-20} = rs2;
341 let Inst{19-15} = rs1;
342 let Inst{14-12} = funct3;
344 let Inst{6-0} = opcode.Value;
347 class RVInstR<bits<7> funct7, bits<3> funct3, RISCVOpcode opcode, dag outs,
348 dag ins, string opcodestr, string argstr>
349 : RVInstRBase<funct3, opcode, outs, ins, opcodestr, argstr> {
350 let Inst{31-25} = funct7;
353 class RVInstRAtomic<bits<5> funct5, bit aq, bit rl, bits<3> funct3,
354 RISCVOpcode opcode, dag outs, dag ins, string opcodestr,
356 : RVInstRBase<funct3, opcode, outs, ins, opcodestr, argstr> {
357 let Inst{31-27} = funct5;
362 class RVInstRFrm<bits<7> funct7, RISCVOpcode opcode, dag outs, dag ins,
363 string opcodestr, string argstr>
364 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatR> {
370 let Inst{31-25} = funct7;
371 let Inst{24-20} = rs2;
372 let Inst{19-15} = rs1;
373 let Inst{14-12} = frm;
375 let Inst{6-0} = opcode.Value;
378 class RVInstR4<bits<2> funct2, bits<3> funct3, RISCVOpcode opcode, dag outs,
379 dag ins, string opcodestr, string argstr>
380 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatR4> {
386 let Inst{31-27} = rs3;
387 let Inst{26-25} = funct2;
388 let Inst{24-20} = rs2;
389 let Inst{19-15} = rs1;
390 let Inst{14-12} = funct3;
392 let Inst{6-0} = opcode.Value;
395 class RVInstR4Frm<bits<2> funct2, RISCVOpcode opcode, dag outs, dag ins,
396 string opcodestr, string argstr>
397 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatR4> {
404 let Inst{31-27} = rs3;
405 let Inst{26-25} = funct2;
406 let Inst{24-20} = rs2;
407 let Inst{19-15} = rs1;
408 let Inst{14-12} = frm;
410 let Inst{6-0} = opcode.Value;
413 // Common base class for I format instructions. Bits {31-20} should be set by
415 class RVInstIBase<bits<3> funct3, RISCVOpcode opcode, dag outs, dag ins,
416 string opcodestr, string argstr>
417 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatI> {
421 let Inst{19-15} = rs1;
422 let Inst{14-12} = funct3;
424 let Inst{6-0} = opcode.Value;
427 class RVInstI<bits<3> funct3, RISCVOpcode opcode, dag outs, dag ins,
428 string opcodestr, string argstr>
429 : RVInstIBase<funct3, opcode, outs, ins, opcodestr, argstr> {
432 let Inst{31-20} = imm12;
435 class RVInstIShift<bits<5> imm11_7, bits<3> funct3, RISCVOpcode opcode,
436 dag outs, dag ins, string opcodestr, string argstr>
437 : RVInstIBase<funct3, opcode, outs, ins, opcodestr, argstr> {
440 let Inst{31-27} = imm11_7;
442 let Inst{25-20} = shamt;
445 class RVInstIShiftW<bits<7> imm11_5, bits<3> funct3, RISCVOpcode opcode,
446 dag outs, dag ins, string opcodestr, string argstr>
447 : RVInstIBase<funct3, opcode, outs, ins, opcodestr, argstr> {
450 let Inst{31-25} = imm11_5;
451 let Inst{24-20} = shamt;
454 class RVInstIUnary<bits<12> imm12, bits<3> funct3, RISCVOpcode opcode,
455 dag outs, dag ins, string opcodestr, string argstr>
456 : RVInstIBase<funct3, opcode, outs, ins, opcodestr, argstr> {
457 let Inst{31-20} = imm12;
460 class RVInstS<bits<3> funct3, RISCVOpcode opcode, dag outs, dag ins,
461 string opcodestr, string argstr>
462 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatS> {
467 let Inst{31-25} = imm12{11-5};
468 let Inst{24-20} = rs2;
469 let Inst{19-15} = rs1;
470 let Inst{14-12} = funct3;
471 let Inst{11-7} = imm12{4-0};
472 let Inst{6-0} = opcode.Value;
475 class RVInstB<bits<3> funct3, RISCVOpcode opcode, dag outs, dag ins,
476 string opcodestr, string argstr>
477 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatB> {
482 let Inst{31} = imm12{11};
483 let Inst{30-25} = imm12{9-4};
484 let Inst{24-20} = rs2;
485 let Inst{19-15} = rs1;
486 let Inst{14-12} = funct3;
487 let Inst{11-8} = imm12{3-0};
488 let Inst{7} = imm12{10};
489 let Inst{6-0} = opcode.Value;
492 class RVInstU<RISCVOpcode opcode, dag outs, dag ins, string opcodestr,
494 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatU> {
498 let Inst{31-12} = imm20;
500 let Inst{6-0} = opcode.Value;
503 class RVInstJ<RISCVOpcode opcode, dag outs, dag ins, string opcodestr,
505 : RVInst<outs, ins, opcodestr, argstr, [], InstFormatJ> {
509 let Inst{31} = imm20{19};
510 let Inst{30-21} = imm20{9-0};
511 let Inst{20} = imm20{10};
512 let Inst{19-12} = imm20{18-11};
514 let Inst{6-0} = opcode.Value;
517 //===----------------------------------------------------------------------===//
518 // Instruction classes for .insn directives
519 //===----------------------------------------------------------------------===//
521 class DirectiveInsnR<dag outs, dag ins, string argstr>
522 : RVInst<outs, ins, "", "", [], InstFormatR> {
531 let Inst{31-25} = funct7;
532 let Inst{24-20} = rs2;
533 let Inst{19-15} = rs1;
534 let Inst{14-12} = funct3;
536 let Inst{6-0} = opcode;
538 let AsmString = ".insn r " # argstr;
541 class DirectiveInsnR4<dag outs, dag ins, string argstr>
542 : RVInst<outs, ins, "", "", [], InstFormatR4> {
552 let Inst{31-27} = rs3;
553 let Inst{26-25} = funct2;
554 let Inst{24-20} = rs2;
555 let Inst{19-15} = rs1;
556 let Inst{14-12} = funct3;
558 let Inst{6-0} = opcode;
560 let AsmString = ".insn r4 " # argstr;
563 class DirectiveInsnI<dag outs, dag ins, string argstr>
564 : RVInst<outs, ins, "", "", [], InstFormatI> {
572 let Inst{31-20} = imm12;
573 let Inst{19-15} = rs1;
574 let Inst{14-12} = funct3;
576 let Inst{6-0} = opcode;
578 let AsmString = ".insn i " # argstr;
581 class DirectiveInsnS<dag outs, dag ins, string argstr>
582 : RVInst<outs, ins, "", "", [], InstFormatS> {
590 let Inst{31-25} = imm12{11-5};
591 let Inst{24-20} = rs2;
592 let Inst{19-15} = rs1;
593 let Inst{14-12} = funct3;
594 let Inst{11-7} = imm12{4-0};
595 let Inst{6-0} = opcode;
597 let AsmString = ".insn s " # argstr;
600 class DirectiveInsnB<dag outs, dag ins, string argstr>
601 : RVInst<outs, ins, "", "", [], InstFormatB> {
609 let Inst{31} = imm12{11};
610 let Inst{30-25} = imm12{9-4};
611 let Inst{24-20} = rs2;
612 let Inst{19-15} = rs1;
613 let Inst{14-12} = funct3;
614 let Inst{11-8} = imm12{3-0};
615 let Inst{7} = imm12{10};
616 let Inst{6-0} = opcode;
618 let AsmString = ".insn b " # argstr;
621 class DirectiveInsnU<dag outs, dag ins, string argstr>
622 : RVInst<outs, ins, "", "", [], InstFormatU> {
628 let Inst{31-12} = imm20;
630 let Inst{6-0} = opcode;
632 let AsmString = ".insn u " # argstr;
635 class DirectiveInsnJ<dag outs, dag ins, string argstr>
636 : RVInst<outs, ins, "", "", [], InstFormatJ> {
642 let Inst{31-12} = imm20;
644 let Inst{6-0} = opcode;
646 let AsmString = ".insn j " # argstr;