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 StackAdjAsmOperand : AsmOperandClass {
45 let Name = "StackAdj";
46 let ParserMethod = "parseZcmpStackAdj";
47 let DiagnosticType = "InvalidStackAdj";
48 let PredicateMethod = "isSpimm";
49 let RenderMethod = "addSpimmOperands";
52 def NegStackAdjAsmOperand : AsmOperandClass {
53 let Name = "NegStackAdj";
54 let ParserMethod = "parseZcmpNegStackAdj";
55 let DiagnosticType = "InvalidStackAdj";
56 let PredicateMethod = "isSpimm";
57 let RenderMethod = "addSpimmOperands";
60 def rlist : Operand<OtherVT> {
61 let ParserMatchClass = RlistAsmOperand;
62 let PrintMethod = "printRlist";
63 let DecoderMethod = "decodeZcmpRlist";
64 let EncoderMethod = "getRlistOpValue";
65 let MCOperandPredicate = [{
67 if (!MCOp.evaluateAsConstantImm(Imm))
69 // 0~3 Reserved for EABI
70 return isUInt<4>(Imm) && Imm >= 4;
74 def stackadj : RISCVOp<OtherVT> {
75 let ParserMatchClass = StackAdjAsmOperand;
76 let PrintMethod = "printStackAdj";
77 let DecoderMethod = "decodeZcmpSpimm";
78 let OperandType = "OPERAND_SPIMM";
79 let MCOperandPredicate = [{
81 if (!MCOp.evaluateAsConstantImm(Imm))
83 return isShiftedUInt<2, 4>(Imm);
87 def negstackadj : RISCVOp<OtherVT> {
88 let ParserMatchClass = NegStackAdjAsmOperand;
89 let PrintMethod = "printNegStackAdj";
90 let DecoderMethod = "decodeZcmpSpimm";
91 let OperandType = "OPERAND_SPIMM";
92 let MCOperandPredicate = [{
94 if (!MCOp.evaluateAsConstantImm(Imm))
96 return isShiftedUInt<2, 4>(Imm);
100 //===----------------------------------------------------------------------===//
101 // Instruction Class Templates
102 //===----------------------------------------------------------------------===//
104 let hasSideEffects = 0, mayLoad = 1, mayStore = 0 in
105 class CLoadB_ri<bits<6> funct6, string OpcodeStr>
106 : RVInst16CLB<funct6, 0b00, (outs GPRC:$rd),
107 (ins GPRCMem:$rs1, uimm2:$imm),
108 OpcodeStr, "$rd, ${imm}(${rs1})"> {
111 let Inst{6-5} = imm{0,1};
114 let hasSideEffects = 0, mayLoad = 1, mayStore = 0 in
115 class CLoadH_ri<bits<6> funct6, bit funct1, string OpcodeStr,
116 DAGOperand rty = GPRC>
117 : RVInst16CLH<funct6, funct1, 0b00, (outs rty:$rd),
118 (ins GPRCMem:$rs1, uimm2_lsb0:$imm),
119 OpcodeStr, "$rd, ${imm}(${rs1})"> {
122 let Inst{5} = imm{1};
125 let hasSideEffects = 0, mayLoad = 0, mayStore = 1 in
126 class CStoreB_rri<bits<6> funct6, string OpcodeStr>
127 : RVInst16CSB<funct6, 0b00, (outs),
128 (ins GPRC:$rs2, GPRCMem:$rs1, uimm2:$imm),
129 OpcodeStr, "$rs2, ${imm}(${rs1})"> {
132 let Inst{6-5} = imm{0,1};
135 let hasSideEffects = 0, mayLoad = 0, mayStore = 1 in
136 class CStoreH_rri<bits<6> funct6, bit funct1, string OpcodeStr,
137 DAGOperand rty = GPRC>
138 : RVInst16CSH<funct6, funct1, 0b00, (outs),
139 (ins rty:$rs2, GPRCMem:$rs1, uimm2_lsb0:$imm),
140 OpcodeStr, "$rs2, ${imm}(${rs1})"> {
143 let Inst{5} = imm{1};
146 let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
147 class RVZcArith_r<bits<5> funct5, string OpcodeStr> :
148 RVInst16CU<0b100111, funct5, 0b01, (outs GPRC:$rd_wb), (ins GPRC:$rd),
150 let Constraints = "$rd = $rd_wb";
153 class RVInstZcCPPP<bits<5> funct5, string opcodestr,
154 DAGOperand immtype = stackadj>
155 : RVInst16<(outs), (ins rlist:$rlist, immtype:$stackadj),
156 opcodestr, "$rlist, $stackadj", [], InstFormatOther> {
160 let Inst{1-0} = 0b10;
161 let Inst{3-2} = stackadj{5-4};
162 let Inst{7-4} = rlist;
163 let Inst{12-8} = funct5;
164 let Inst{15-13} = 0b101;
167 //===----------------------------------------------------------------------===//
169 //===----------------------------------------------------------------------===//
171 let Predicates = [HasStdExtZcb, HasStdExtZba, IsRV64] in
172 def C_ZEXT_W : RVZcArith_r<0b11100 , "c.zext.w">,
173 Sched<[WriteIALU32, ReadIALU32, ReadIALU32]>;
175 let Predicates = [HasStdExtZcb, HasStdExtZbb] in {
176 def C_ZEXT_H : RVZcArith_r<0b11010 , "c.zext.h">,
177 Sched<[WriteIALU, ReadIALU]>;
178 def C_SEXT_B : RVZcArith_r<0b11001 , "c.sext.b">,
179 Sched<[WriteIALU, ReadIALU]>;
180 def C_SEXT_H : RVZcArith_r<0b11011 , "c.sext.h">,
181 Sched<[WriteIALU, ReadIALU]>;
184 let Predicates = [HasStdExtZcb] in
185 def C_ZEXT_B : RVZcArith_r<0b11000 , "c.zext.b">,
186 Sched<[WriteIALU, ReadIALU]>;
188 let Predicates = [HasStdExtZcb, HasStdExtZmmul] in
189 def C_MUL : CA_ALU<0b100111, 0b10, "c.mul">,
190 Sched<[WriteIMul, ReadIMul, ReadIMul]>;
192 let Predicates = [HasStdExtZcb] in {
193 def C_NOT : RVZcArith_r<0b11101 , "c.not">,
194 Sched<[WriteIALU, ReadIALU]>;
196 def C_LBU : CLoadB_ri<0b100000, "c.lbu">,
197 Sched<[WriteLDB, ReadMemBase]>;
198 def C_LHU : CLoadH_ri<0b100001, 0b0, "c.lhu">,
199 Sched<[WriteLDH, ReadMemBase]>;
200 def C_LH : CLoadH_ri<0b100001, 0b1, "c.lh">,
201 Sched<[WriteLDH, ReadMemBase]>;
203 def C_SB : CStoreB_rri<0b100010, "c.sb">,
204 Sched<[WriteSTB, ReadStoreData, ReadMemBase]>;
205 def C_SH : CStoreH_rri<0b100011, 0b0, "c.sh">,
206 Sched<[WriteSTH, ReadStoreData, ReadMemBase]>;
208 // Compressed versions of Zhinx load/store.
209 let isCodeGenOnly = 1 in {
210 def C_LH_INX : CLoadH_ri<0b100001, 0b1, "c.lh", GPRF16C>,
211 Sched<[WriteLDH, ReadMemBase]>;
212 def C_SH_INX : CStoreH_rri<0b100011, 0b0, "c.sh", GPRF16C>,
213 Sched<[WriteSTH, ReadStoreData, ReadMemBase]>;
215 } // Predicates = [HasStdExtZcb]
218 let DecoderNamespace = "RVZcmp", Predicates = [HasStdExtZcmp],
219 hasSideEffects = 0, mayLoad = 0, mayStore = 0 in {
220 let Defs = [X10, X11] in
221 def CM_MVA01S : RVInst16CA<0b101011, 0b11, 0b10, (outs),
222 (ins SR07:$rs1, SR07:$rs2), "cm.mva01s", "$rs1, $rs2">,
223 Sched<[WriteIALU, WriteIALU, ReadIALU, ReadIALU]>;
225 let Uses = [X10, X11] in
226 def CM_MVSA01 : RVInst16CA<0b101011, 0b01, 0b10, (outs SR07:$rs1, SR07:$rs2),
227 (ins), "cm.mvsa01", "$rs1, $rs2">,
228 Sched<[WriteIALU, WriteIALU, ReadIALU, ReadIALU]>;
229 } // DecoderNamespace = "RVZcmp", Predicates = [HasStdExtZcmp]...
231 let DecoderNamespace = "RVZcmp", Predicates = [HasStdExtZcmp] in {
232 let hasSideEffects = 0, mayLoad = 0, mayStore = 1, Uses = [X2], Defs = [X2] in
233 def CM_PUSH : RVInstZcCPPP<0b11000, "cm.push", negstackadj>,
234 Sched<[WriteIALU, ReadIALU, ReadStoreData, ReadStoreData,
235 ReadStoreData, ReadStoreData, ReadStoreData, ReadStoreData,
236 ReadStoreData, ReadStoreData, ReadStoreData, ReadStoreData,
237 ReadStoreData, ReadStoreData, ReadStoreData]>;
239 let hasSideEffects = 0, mayLoad = 1, mayStore = 0, isReturn = 1,
240 Uses = [X2], Defs = [X2] in
241 def CM_POPRET : RVInstZcCPPP<0b11110, "cm.popret">,
242 Sched<[WriteIALU, WriteLDW, WriteLDW, WriteLDW, WriteLDW,
243 WriteLDW, WriteLDW, WriteLDW, WriteLDW, WriteLDW,
244 WriteLDW, WriteLDW, WriteLDW, WriteLDW, ReadIALU]>;
246 let hasSideEffects = 0, mayLoad = 1, mayStore = 0, isReturn = 1,
247 Uses = [X2], Defs = [X2, X10] in
248 def CM_POPRETZ : RVInstZcCPPP<0b11100, "cm.popretz">,
249 Sched<[WriteIALU, WriteIALU, WriteLDW, WriteLDW, WriteLDW,
250 WriteLDW, WriteLDW, WriteLDW, WriteLDW, WriteLDW,
251 WriteLDW, WriteLDW, WriteLDW, WriteLDW, WriteLDW,
254 let hasSideEffects = 0, mayLoad = 1, mayStore = 0,
255 Uses = [X2], Defs = [X2] in
256 def CM_POP : RVInstZcCPPP<0b11010, "cm.pop">,
257 Sched<[WriteIALU, WriteLDW, WriteLDW, WriteLDW, WriteLDW,
258 WriteLDW, WriteLDW, WriteLDW, WriteLDW, WriteLDW, WriteLDW,
259 WriteLDW, WriteLDW, WriteLDW, ReadIALU]>;
260 } // DecoderNamespace = "RVZcmp", Predicates = [HasStdExtZcmp]...
262 let DecoderNamespace = "RVZcmt", Predicates = [HasStdExtZcmt],
263 hasSideEffects = 0, mayLoad = 0, mayStore = 0 in {
264 def CM_JT : RVInst16CJ<0b101, 0b10, (outs), (ins uimm5:$index),
268 let Inst{12-7} = 0b000000;
269 let Inst{6-2} = index;
273 def CM_JALT : RVInst16CJ<0b101, 0b10, (outs), (ins uimm8ge32:$index),
274 "cm.jalt", "$index">{
277 let Inst{12-10} = 0b000;
278 let Inst{9-2} = index;
280 } // DecoderNamespace = "RVZcmt", Predicates = [HasStdExtZcmt]...
283 let Predicates = [HasStdExtZcb, HasStdExtZmmul] in{
284 def : CompressPat<(MUL GPRC:$rs1, GPRC:$rs1, GPRC:$rs2),
285 (C_MUL GPRC:$rs1, GPRC:$rs2)>;
286 let isCompressOnly = true in
287 def : CompressPat<(MUL GPRC:$rs1, GPRC:$rs2, GPRC:$rs1),
288 (C_MUL GPRC:$rs1, GPRC:$rs2)>;
289 } // Predicates = [HasStdExtZcb, HasStdExtZmmul]
291 let Predicates = [HasStdExtZcb, HasStdExtZbb] in{
292 def : CompressPat<(SEXT_B GPRC:$rs1, GPRC:$rs1),
293 (C_SEXT_B GPRC:$rs1, GPRC:$rs1)>;
294 def : CompressPat<(SEXT_H GPRC:$rs1, GPRC:$rs1),
295 (C_SEXT_H GPRC:$rs1, GPRC:$rs1)>;
296 } // Predicates = [HasStdExtZcb, HasStdExtZbb]
298 let Predicates = [HasStdExtZcb, HasStdExtZbb] in{
299 def : CompressPat<(ZEXT_H_RV32 GPRC:$rs1, GPRC:$rs1),
300 (C_ZEXT_H GPRC:$rs1, GPRC:$rs1)>;
301 def : CompressPat<(ZEXT_H_RV64 GPRC:$rs1, GPRC:$rs1),
302 (C_ZEXT_H GPRC:$rs1, GPRC:$rs1)>;
303 } // Predicates = [HasStdExtZcb, HasStdExtZbb]
305 let Predicates = [HasStdExtZcb] in{
306 def : CompressPat<(ANDI GPRC:$rs1, GPRC:$rs1, 255),
307 (C_ZEXT_B GPRC:$rs1, GPRC:$rs1)>;
308 } // Predicates = [HasStdExtZcb]
310 let Predicates = [HasStdExtZcb, HasStdExtZba, IsRV64] in{
311 def : CompressPat<(ADD_UW GPRC:$rs1, GPRC:$rs1, X0),
312 (C_ZEXT_W GPRC:$rs1, GPRC:$rs1)>;
313 } // Predicates = [HasStdExtZcb, HasStdExtZba, IsRV64]
315 let Predicates = [HasStdExtZcb] in{
316 def : CompressPat<(XORI GPRC:$rs1, GPRC:$rs1, -1),
317 (C_NOT GPRC:$rs1, GPRC:$rs1)>;
320 let Predicates = [HasStdExtZcb] in{
321 def : CompressPat<(LBU GPRC:$rd, GPRCMem:$rs1, uimm2:$imm),
322 (C_LBU GPRC:$rd, GPRCMem:$rs1, uimm2:$imm)>;
323 def : CompressPat<(LHU GPRC:$rd, GPRCMem:$rs1, uimm2_lsb0:$imm),
324 (C_LHU GPRC:$rd, GPRCMem:$rs1, uimm2_lsb0:$imm)>;
325 def : CompressPat<(LH GPRC:$rd, GPRCMem:$rs1, uimm2_lsb0:$imm),
326 (C_LH GPRC:$rd, GPRCMem:$rs1, uimm2_lsb0:$imm)>;
327 def : CompressPat<(SB GPRC:$rs2, GPRCMem:$rs1, uimm2:$imm),
328 (C_SB GPRC:$rs2, GPRCMem:$rs1, uimm2:$imm)>;
329 def : CompressPat<(SH GPRC:$rs2, GPRCMem:$rs1, uimm2_lsb0:$imm),
330 (C_SH GPRC:$rs2, GPRCMem:$rs1, uimm2_lsb0:$imm)>;
332 let isCompressOnly = true in {
333 def : CompressPat<(LH_INX GPRF16C:$rd, GPRCMem:$rs1, uimm2_lsb0:$imm),
334 (C_LH_INX GPRF16C:$rd, GPRCMem:$rs1, uimm2_lsb0:$imm)>;
335 def : CompressPat<(SH_INX GPRF16C:$rs2, GPRCMem:$rs1, uimm2_lsb0:$imm),
336 (C_SH_INX GPRF16C:$rs2, GPRCMem:$rs1, uimm2_lsb0:$imm)>;
338 }// Predicates = [HasStdExtZcb]
341 //===----------------------------------------------------------------------===//
342 // Pseudo Instructions
343 //===----------------------------------------------------------------------===//
345 let Predicates = [HasStdExtZcb] in {
346 def : InstAlias<"c.lbu $rd, (${rs1})",(C_LBU GPRC:$rd, GPRC:$rs1, 0), 0>;
347 def : InstAlias<"c.lhu $rd, (${rs1})",(C_LHU GPRC:$rd, GPRC:$rs1, 0), 0>;
348 def : InstAlias<"c.lh $rd, (${rs1})", (C_LH GPRC:$rd, GPRC:$rs1, 0), 0>;
349 def : InstAlias<"c.sb $rd, (${rs1})", (C_SB GPRC:$rd, GPRC:$rs1, 0), 0>;
350 def : InstAlias<"c.sh $rd, (${rs1})", (C_SH GPRC:$rd, GPRC:$rs1, 0), 0>;