1 //===-- RISCVInstrInfoZc.td - RISC-V 'Zc*' instructions ----*- 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 /// This file describes the RISC-V instructions from the 'Zc*' compressed
10 /// instruction extensions, version 1.0.3.
12 //===----------------------------------------------------------------------===//
14 //===----------------------------------------------------------------------===//
15 // Operand and SDNode transformation definitions.
16 //===----------------------------------------------------------------------===//
18 def uimm2_lsb0 : RISCVOp,
19 ImmLeaf<XLenVT, [{return isShiftedUInt<1, 1>(Imm);}]> {
20 let ParserMatchClass = UImmAsmOperand<2, "Lsb0">;
21 let EncoderMethod = "getImmOpValue";
22 let DecoderMethod = "decodeUImmOperand<2>";
23 let OperandType = "OPERAND_UIMM2_LSB0";
24 let MCOperandPredicate = [{
26 if (!MCOp.evaluateAsConstantImm(Imm))
28 return isShiftedUInt<1, 1>(Imm);
32 def uimm8ge32 : RISCVOp {
33 let ParserMatchClass = UImmAsmOperand<8, "GE32">;
34 let DecoderMethod = "decodeUImmOperand<8>";
35 let OperandType = "OPERAND_UIMM8_GE32";
38 def RlistAsmOperand : AsmOperandClass {
40 let ParserMethod = "parseReglist";
41 let DiagnosticType = "InvalidRlist";
44 def SpimmAsmOperand : AsmOperandClass {
46 let ParserMethod = "parseZcmpSpimm";
47 let DiagnosticType = "InvalidSpimm";
50 def rlist : Operand<OtherVT> {
51 let ParserMatchClass = RlistAsmOperand;
52 let PrintMethod = "printRlist";
53 let DecoderMethod = "decodeZcmpRlist";
54 let EncoderMethod = "getRlistOpValue";
55 let MCOperandPredicate = [{
57 if (!MCOp.evaluateAsConstantImm(Imm))
59 // 0~3 Reserved for EABI
60 return isUInt<4>(Imm) && Imm >= 4;
64 def spimm : Operand<OtherVT> {
65 let ParserMatchClass = SpimmAsmOperand;
66 let PrintMethod = "printSpimm";
67 let DecoderMethod = "decodeZcmpSpimm";
68 let MCOperandPredicate = [{
70 if (!MCOp.evaluateAsConstantImm(Imm))
72 return isShiftedUInt<2, 4>(Imm);
76 //===----------------------------------------------------------------------===//
77 // Instruction Class Templates
78 //===----------------------------------------------------------------------===//
80 let hasSideEffects = 0, mayLoad = 1, mayStore = 0 in
81 class CLoadB_ri<bits<6> funct6, string OpcodeStr>
82 : RVInst16CLB<funct6, 0b00, (outs GPRC:$rd),
83 (ins GPRCMem:$rs1, uimm2:$imm),
84 OpcodeStr, "$rd, ${imm}(${rs1})"> {
87 let Inst{6-5} = imm{0,1};
90 let hasSideEffects = 0, mayLoad = 1, mayStore = 0 in
91 class CLoadH_ri<bits<6> funct6, bit funct1, string OpcodeStr>
92 : RVInst16CLH<funct6, funct1, 0b00, (outs GPRC:$rd),
93 (ins GPRCMem:$rs1, uimm2_lsb0:$imm),
94 OpcodeStr, "$rd, ${imm}(${rs1})"> {
100 let hasSideEffects = 0, mayLoad = 0, mayStore = 1 in
101 class CStoreB_rri<bits<6> funct6, string OpcodeStr>
102 : RVInst16CSB<funct6, 0b00, (outs),
103 (ins GPRC:$rs2, GPRCMem:$rs1, uimm2:$imm),
104 OpcodeStr, "$rs2, ${imm}(${rs1})"> {
107 let Inst{6-5} = imm{0,1};
110 let hasSideEffects = 0, mayLoad = 0, mayStore = 1 in
111 class CStoreH_rri<bits<6> funct6, bit funct1, string OpcodeStr>
112 : RVInst16CSH<funct6, funct1, 0b00, (outs),
113 (ins GPRC:$rs2, GPRCMem:$rs1, uimm2_lsb0:$imm),
114 OpcodeStr, "$rs2, ${imm}(${rs1})"> {
117 let Inst{5} = imm{1};
120 let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
121 class RVZcArith_r<bits<5> funct5, string OpcodeStr> :
122 RVInst16CU<0b100111, funct5, 0b01, (outs GPRC:$rd_wb), (ins GPRC:$rd),
124 let Constraints = "$rd = $rd_wb";
127 class RVInstZcCPPP<bits<5> funct5, string opcodestr>
128 : RVInst16<(outs), (ins rlist:$rlist, spimm:$spimm),
129 opcodestr, "$rlist, $spimm", [], InstFormatOther> {
133 let Inst{1-0} = 0b10;
134 let Inst{3-2} = spimm{5-4};
135 let Inst{7-4} = rlist;
136 let Inst{12-8} = funct5;
137 let Inst{15-13} = 0b101;
140 //===----------------------------------------------------------------------===//
142 //===----------------------------------------------------------------------===//
144 let Predicates = [HasStdExtZcb, HasStdExtZba, IsRV64] in
145 def C_ZEXT_W : RVZcArith_r<0b11100 , "c.zext.w">,
146 Sched<[WriteIALU32, ReadIALU32, ReadIALU32]>;
148 let Predicates = [HasStdExtZcb, HasStdExtZbb] in {
149 def C_ZEXT_H : RVZcArith_r<0b11010 , "c.zext.h">,
150 Sched<[WriteIALU, ReadIALU]>;
151 def C_SEXT_B : RVZcArith_r<0b11001 , "c.sext.b">,
152 Sched<[WriteIALU, ReadIALU]>;
153 def C_SEXT_H : RVZcArith_r<0b11011 , "c.sext.h">,
154 Sched<[WriteIALU, ReadIALU]>;
157 let Predicates = [HasStdExtZcb] in
158 def C_ZEXT_B : RVZcArith_r<0b11000 , "c.zext.b">,
159 Sched<[WriteIALU, ReadIALU]>;
161 let Predicates = [HasStdExtZcb, HasStdExtMOrZmmul] in
162 def C_MUL : CA_ALU<0b100111, 0b10, "c.mul", GPRC>,
163 Sched<[WriteIMul, ReadIMul, ReadIMul]>;
165 let Predicates = [HasStdExtZcb] in {
166 def C_NOT : RVZcArith_r<0b11101 , "c.not">,
167 Sched<[WriteIALU, ReadIALU]>;
169 def C_LBU : CLoadB_ri<0b100000, "c.lbu">,
170 Sched<[WriteLDB, ReadMemBase]>;
171 def C_LHU : CLoadH_ri<0b100001, 0b0, "c.lhu">,
172 Sched<[WriteLDH, ReadMemBase]>;
173 def C_LH : CLoadH_ri<0b100001, 0b1, "c.lh">,
174 Sched<[WriteLDH, ReadMemBase]>;
176 def C_SB : CStoreB_rri<0b100010, "c.sb">,
177 Sched<[WriteSTB, ReadStoreData, ReadMemBase]>;
178 def C_SH : CStoreH_rri<0b100011, 0b0, "c.sh">,
179 Sched<[WriteSTH, ReadStoreData, ReadMemBase]>;
183 let DecoderNamespace = "RVZcmp", Predicates = [HasStdExtZcmp],
184 Defs = [X10, X11], hasSideEffects = 0, mayLoad = 0, mayStore = 0 in {
185 def CM_MVA01S : RVInst16CA<0b101011, 0b11, 0b10, (outs),
186 (ins SR07:$rs1, SR07:$rs2), "cm.mva01s", "$rs1, $rs2">;
188 def CM_MVSA01 : RVInst16CA<0b101011, 0b01, 0b10, (outs SR07:$rs1, SR07:$rs2),
189 (ins), "cm.mvsa01", "$rs1, $rs2">;
190 } // DecoderNamespace = "RVZcmp", Predicates = [HasStdExtZcmp]...
192 let DecoderNamespace = "RVZcmp", Predicates = [HasStdExtZcmp] in {
193 let hasSideEffects = 0, mayLoad = 0, mayStore = 1 in
194 def CM_PUSH : RVInstZcCPPP<0b11000, "cm.push">;
196 let hasSideEffects = 0, mayLoad = 1, mayStore = 0 in
197 def CM_POPRET : RVInstZcCPPP<0b11110, "cm.popret">;
199 let hasSideEffects = 0, mayLoad = 1, mayStore = 0 in
200 def CM_POPRETZ : RVInstZcCPPP<0b11100, "cm.popretz">;
202 let hasSideEffects = 0, mayLoad = 1, mayStore = 0 in
203 def CM_POP : RVInstZcCPPP<0b11010, "cm.pop">;
204 } // DecoderNamespace = "RVZcmp", Predicates = [HasStdExtZcmp]...
206 let DecoderNamespace = "RVZcmt", Predicates = [HasStdExtZcmt],
207 hasSideEffects = 0, mayLoad = 0, mayStore = 0 in {
208 def CM_JT : RVInst16CJ<0b101, 0b10, (outs), (ins uimm5:$index),
212 let Inst{12-7} = 0b000000;
213 let Inst{6-2} = index;
217 def CM_JALT : RVInst16CJ<0b101, 0b10, (outs), (ins uimm8ge32:$index),
218 "cm.jalt", "$index">{
221 let Inst{12-10} = 0b000;
222 let Inst{9-2} = index;
224 } // DecoderNamespace = "RVZcmt", Predicates = [HasStdExtZcmt]...
227 let Predicates = [HasStdExtZcb, HasStdExtMOrZmmul] in{
228 def : CompressPat<(MUL GPRC:$rs1, GPRC:$rs1, GPRC:$rs2),
229 (C_MUL GPRC:$rs1, GPRC:$rs2)>;
230 let isCompressOnly = true in
231 def : CompressPat<(MUL GPRC:$rs1, GPRC:$rs2, GPRC:$rs1),
232 (C_MUL GPRC:$rs1, GPRC:$rs2)>;
233 } // Predicates = [HasStdExtZcb, HasStdExtMOrZmmul]
235 let Predicates = [HasStdExtZcb, HasStdExtZbb] in{
236 def : CompressPat<(SEXT_B GPRC:$rs1, GPRC:$rs1),
237 (C_SEXT_B GPRC:$rs1, GPRC:$rs1)>;
238 def : CompressPat<(SEXT_H GPRC:$rs1, GPRC:$rs1),
239 (C_SEXT_H GPRC:$rs1, GPRC:$rs1)>;
240 } // Predicates = [HasStdExtZcb, HasStdExtZbb]
242 let Predicates = [HasStdExtZcb, HasStdExtZbb] in{
243 def : CompressPat<(ZEXT_H_RV32 GPRC:$rs1, GPRC:$rs1),
244 (C_ZEXT_H GPRC:$rs1, GPRC:$rs1)>;
245 def : CompressPat<(ZEXT_H_RV64 GPRC:$rs1, GPRC:$rs1),
246 (C_ZEXT_H GPRC:$rs1, GPRC:$rs1)>;
247 } // Predicates = [HasStdExtZcb, HasStdExtZbb]
249 let Predicates = [HasStdExtZcb] in{
250 def : CompressPat<(ANDI GPRC:$rs1, GPRC:$rs1, 255),
251 (C_ZEXT_B GPRC:$rs1, GPRC:$rs1)>;
252 } // Predicates = [HasStdExtZcb]
254 let Predicates = [HasStdExtZcb, HasStdExtZba, IsRV64] in{
255 def : CompressPat<(ADD_UW GPRC:$rs1, GPRC:$rs1, X0),
256 (C_ZEXT_W GPRC:$rs1, GPRC:$rs1)>;
257 } // Predicates = [HasStdExtZcb, HasStdExtZba, IsRV64]
259 let Predicates = [HasStdExtZcb] in{
260 def : CompressPat<(XORI GPRC:$rs1, GPRC:$rs1, -1),
261 (C_NOT GPRC:$rs1, GPRC:$rs1)>;
264 let Predicates = [HasStdExtZcb] in{
265 def : CompressPat<(LBU GPRC:$rd, GPRCMem:$rs1, uimm2:$imm),
266 (C_LBU GPRC:$rd, GPRCMem:$rs1, uimm2:$imm)>;
267 def : CompressPat<(LHU GPRC:$rd, GPRCMem:$rs1, uimm2_lsb0:$imm),
268 (C_LHU GPRC:$rd, GPRCMem:$rs1, uimm2_lsb0:$imm)>;
269 def : CompressPat<(LH GPRC:$rd, GPRCMem:$rs1, uimm2_lsb0:$imm),
270 (C_LH GPRC:$rd, GPRCMem:$rs1, uimm2_lsb0:$imm)>;
271 def : CompressPat<(SB GPRC:$rs2, GPRCMem:$rs1, uimm2:$imm),
272 (C_SB GPRC:$rs2, GPRCMem:$rs1, uimm2:$imm)>;
273 def : CompressPat<(SH GPRC:$rs2, GPRCMem:$rs1, uimm2_lsb0:$imm),
274 (C_SH GPRC:$rs2, GPRCMem:$rs1, uimm2_lsb0:$imm)>;
275 }// Predicates = [HasStdExtZcb]
278 //===----------------------------------------------------------------------===//
279 // Pseudo Instructions
280 //===----------------------------------------------------------------------===//
282 let Predicates = [HasStdExtZcb] in {
283 def : InstAlias<"c.lbu $rd, (${rs1})",(C_LBU GPRC:$rd, GPRC:$rs1, 0), 0>;
284 def : InstAlias<"c.lhu $rd, (${rs1})",(C_LHU GPRC:$rd, GPRC:$rs1, 0), 0>;
285 def : InstAlias<"c.lh $rd, (${rs1})", (C_LH GPRC:$rd, GPRC:$rs1, 0), 0>;
286 def : InstAlias<"c.sb $rd, (${rs1})", (C_SB GPRC:$rd, GPRC:$rs1, 0), 0>;
287 def : InstAlias<"c.sh $rd, (${rs1})", (C_SH GPRC:$rd, GPRC:$rs1, 0), 0>;