1 //===- RISCVInstrInfoZk.td - RISC-V 'Zk' 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 standard 'Zk',
10 // Scalar Cryptography Instructions extension, version 1.0.
12 //===----------------------------------------------------------------------===//
14 //===----------------------------------------------------------------------===//
15 // Operand and SDNode transformation definitions.
16 //===----------------------------------------------------------------------===//
18 def riscv_sha256sig0 : SDNode<"RISCVISD::SHA256SIG0", SDTIntUnaryOp>;
19 def riscv_sha256sig1 : SDNode<"RISCVISD::SHA256SIG1", SDTIntUnaryOp>;
20 def riscv_sha256sum0 : SDNode<"RISCVISD::SHA256SUM0", SDTIntUnaryOp>;
21 def riscv_sha256sum1 : SDNode<"RISCVISD::SHA256SUM1", SDTIntUnaryOp>;
23 def SDT_RISCVZkByteSelect : SDTypeProfile<1, 3, [SDTCisVT<0, XLenVT>,
27 def riscv_sm4ks : SDNode<"RISCVISD::SM4KS", SDT_RISCVZkByteSelect>;
28 def riscv_sm4ed : SDNode<"RISCVISD::SM4ED", SDT_RISCVZkByteSelect>;
30 def riscv_sm3p0 : SDNode<"RISCVISD::SM3P0", SDTIntUnaryOp>;
31 def riscv_sm3p1 : SDNode<"RISCVISD::SM3P1", SDTIntUnaryOp>;
33 def RnumArg : AsmOperandClass {
35 let RenderMethod = "addImmOperands";
36 let DiagnosticType = "InvalidRnumArg";
39 def rnum : RISCVOp<i32>, TImmLeaf<i32, [{return (Imm >= 0 && Imm <= 10);}]> {
40 let ParserMatchClass = RnumArg;
41 let EncoderMethod = "getImmOpValue";
42 let DecoderMethod = "decodeUImmOperand<4>";
43 let OperandType = "OPERAND_RVKRNUM";
46 def byteselect : RISCVOp<i32>, TImmLeaf<i32, [{return isUInt<2>(Imm);}]> {
47 let ParserMatchClass = UImmAsmOperand<2>;
48 let DecoderMethod = "decodeUImmOperand<2>";
49 let OperandType = "OPERAND_UIMM2";
52 //===----------------------------------------------------------------------===//
53 // Instruction class templates
54 //===----------------------------------------------------------------------===//
55 let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
56 class RVKUnary<bits<12> imm12, bits<3> funct3, string opcodestr>
57 : RVInstIUnary<imm12, funct3, OPC_OP_IMM, (outs GPR:$rd), (ins GPR:$rs1),
58 opcodestr, "$rd, $rs1">;
60 let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
61 class RVKByteSelect<bits<5> funct5, string opcodestr>
62 : RVInstR<{0b00, funct5}, 0b000, OPC_OP, (outs GPR:$rd),
63 (ins GPR:$rs1, GPR:$rs2, byteselect:$bs),
64 opcodestr, "$rd, $rs1, $rs2, $bs">{
69 let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
70 class RVKUnary_rnum<bits<7> funct7, bits<3> funct3, string opcodestr>
71 : RVInstIBase<funct3, OPC_OP_IMM, (outs GPR:$rd),
72 (ins GPR:$rs1, rnum:$rnum), opcodestr, "$rd, $rs1, $rnum"> {
74 let Inst{31-25} = funct7;
76 let Inst{23-20} = rnum;
79 //===----------------------------------------------------------------------===//
81 //===----------------------------------------------------------------------===//
82 let Predicates = [HasStdExtZknd, IsRV32] in {
83 def AES32DSI : RVKByteSelect<0b10101, "aes32dsi">;
84 def AES32DSMI : RVKByteSelect<0b10111, "aes32dsmi">;
85 } // Predicates = [HasStdExtZknd, IsRV32]
87 let Predicates = [HasStdExtZknd, IsRV64] in {
88 def AES64DS : ALU_rr<0b0011101, 0b000, "aes64ds">;
89 def AES64DSM : ALU_rr<0b0011111, 0b000, "aes64dsm">;
91 def AES64IM : RVKUnary<0b001100000000, 0b001, "aes64im">;
92 } // Predicates = [HasStdExtZknd, IsRV64]
94 let Predicates = [HasStdExtZkndOrZkne, IsRV64] in {
95 def AES64KS2 : ALU_rr<0b0111111, 0b000, "aes64ks2">;
97 def AES64KS1I : RVKUnary_rnum<0b0011000, 0b001, "aes64ks1i">;
98 } // Predicates = [HasStdExtZkndOrZkne, IsRV64]
100 let Predicates = [HasStdExtZkne, IsRV32] in {
101 def AES32ESI : RVKByteSelect<0b10001, "aes32esi">;
102 def AES32ESMI : RVKByteSelect<0b10011, "aes32esmi">;
103 } // Predicates = [HasStdExtZkne, IsRV32]
105 let Predicates = [HasStdExtZkne, IsRV64] in {
106 def AES64ES : ALU_rr<0b0011001, 0b000, "aes64es">;
107 def AES64ESM : ALU_rr<0b0011011, 0b000, "aes64esm">;
108 } // Predicates = [HasStdExtZkne, IsRV64]
110 let Predicates = [HasStdExtZknh], IsSignExtendingOpW = 1 in {
111 def SHA256SIG0 : RVKUnary<0b000100000010, 0b001, "sha256sig0">;
112 def SHA256SIG1 : RVKUnary<0b000100000011, 0b001, "sha256sig1">;
113 def SHA256SUM0 : RVKUnary<0b000100000000, 0b001, "sha256sum0">;
114 def SHA256SUM1 : RVKUnary<0b000100000001, 0b001, "sha256sum1">;
115 } // Predicates = [HasStdExtZknh]
117 let Predicates = [HasStdExtZknh, IsRV32] in {
118 def SHA512SIG0H : ALU_rr<0b0101110, 0b000, "sha512sig0h">;
119 def SHA512SIG0L : ALU_rr<0b0101010, 0b000, "sha512sig0l">;
120 def SHA512SIG1H : ALU_rr<0b0101111, 0b000, "sha512sig1h">;
121 def SHA512SIG1L : ALU_rr<0b0101011, 0b000, "sha512sig1l">;
122 def SHA512SUM0R : ALU_rr<0b0101000, 0b000, "sha512sum0r">;
123 def SHA512SUM1R : ALU_rr<0b0101001, 0b000, "sha512sum1r">;
124 } // [HasStdExtZknh, IsRV32]
126 let Predicates = [HasStdExtZknh, IsRV64] in {
127 def SHA512SIG0 : RVKUnary<0b000100000110, 0b001, "sha512sig0">;
128 def SHA512SIG1 : RVKUnary<0b000100000111, 0b001, "sha512sig1">;
129 def SHA512SUM0 : RVKUnary<0b000100000100, 0b001, "sha512sum0">;
130 def SHA512SUM1 : RVKUnary<0b000100000101, 0b001, "sha512sum1">;
131 } // Predicates = [HasStdExtZknh, IsRV64]
133 let Predicates = [HasStdExtZksed], IsSignExtendingOpW = 1 in {
134 def SM4ED : RVKByteSelect<0b11000, "sm4ed">;
135 def SM4KS : RVKByteSelect<0b11010, "sm4ks">;
136 } // Predicates = [HasStdExtZksed]
138 let Predicates = [HasStdExtZksh], IsSignExtendingOpW = 1 in {
139 def SM3P0 : RVKUnary<0b000100001000, 0b001, "sm3p0">;
140 def SM3P1 : RVKUnary<0b000100001001, 0b001, "sm3p1">;
141 } // Predicates = [HasStdExtZksh]
143 //===----------------------------------------------------------------------===//
145 //===----------------------------------------------------------------------===//
147 class PatGprGprByteSelect<SDPatternOperator OpNode, RVInst Inst>
148 : Pat<(XLenVT (OpNode (XLenVT GPR:$rs1), (XLenVT GPR:$rs2), byteselect:$imm)),
149 (Inst GPR:$rs1, GPR:$rs2, byteselect:$imm)>;
152 let Predicates = [HasStdExtZknd, IsRV32] in {
153 def : PatGprGprByteSelect<int_riscv_aes32dsi, AES32DSI>;
154 def : PatGprGprByteSelect<int_riscv_aes32dsmi, AES32DSMI>;
155 } // Predicates = [HasStdExtZknd, IsRV32]
157 let Predicates = [HasStdExtZknd, IsRV64] in {
158 def : PatGprGpr<int_riscv_aes64ds, AES64DS>;
159 def : PatGprGpr<int_riscv_aes64dsm, AES64DSM>;
160 def : PatGpr<int_riscv_aes64im, AES64IM>;
161 } // Predicates = [HasStdExtZknd, IsRV64]
163 let Predicates = [HasStdExtZkndOrZkne, IsRV64] in {
164 def : PatGprGpr<int_riscv_aes64ks2, AES64KS2>;
165 def : Pat<(int_riscv_aes64ks1i GPR:$rs1, rnum:$rnum),
166 (AES64KS1I GPR:$rs1, rnum:$rnum)>;
167 } // Predicates = [HasStdExtZkndOrZkne, IsRV64]
170 let Predicates = [HasStdExtZkne, IsRV32] in {
171 def : PatGprGprByteSelect<int_riscv_aes32esi, AES32ESI>;
172 def : PatGprGprByteSelect<int_riscv_aes32esmi, AES32ESMI>;
173 } // Predicates = [HasStdExtZkne, IsRV32]
175 let Predicates = [HasStdExtZkne, IsRV64] in {
176 def : PatGprGpr<int_riscv_aes64es, AES64ES>;
177 def : PatGprGpr<int_riscv_aes64esm, AES64ESM>;
178 } // Predicates = [HasStdExtZkne, IsRV64]
181 let Predicates = [HasStdExtZknh] in {
182 def : PatGpr<riscv_sha256sig0, SHA256SIG0>;
183 def : PatGpr<riscv_sha256sig1, SHA256SIG1>;
184 def : PatGpr<riscv_sha256sum0, SHA256SUM0>;
185 def : PatGpr<riscv_sha256sum1, SHA256SUM1>;
186 } // Predicates = [HasStdExtZknh]
188 let Predicates = [HasStdExtZknh, IsRV32] in {
189 def : PatGprGpr<int_riscv_sha512sig0l, SHA512SIG0L>;
190 def : PatGprGpr<int_riscv_sha512sig0h, SHA512SIG0H>;
191 def : PatGprGpr<int_riscv_sha512sig1l, SHA512SIG1L>;
192 def : PatGprGpr<int_riscv_sha512sig1h, SHA512SIG1H>;
193 def : PatGprGpr<int_riscv_sha512sum0r, SHA512SUM0R>;
194 def : PatGprGpr<int_riscv_sha512sum1r, SHA512SUM1R>;
195 } // Predicates = [HasStdExtZknh, IsRV32]
197 let Predicates = [HasStdExtZknh, IsRV64] in {
198 def : PatGpr<int_riscv_sha512sig0, SHA512SIG0>;
199 def : PatGpr<int_riscv_sha512sig1, SHA512SIG1>;
200 def : PatGpr<int_riscv_sha512sum0, SHA512SUM0>;
201 def : PatGpr<int_riscv_sha512sum1, SHA512SUM1>;
202 } // Predicates = [HasStdExtZknh, IsRV64]
205 let Predicates = [HasStdExtZksed] in {
206 def : PatGprGprByteSelect<riscv_sm4ks, SM4KS>;
207 def : PatGprGprByteSelect<riscv_sm4ed, SM4ED>;
208 } // Predicates = [HasStdExtZksed]
211 let Predicates = [HasStdExtZksh] in {
212 def : PatGpr<riscv_sm3p0, SM3P0>;
213 def : PatGpr<riscv_sm3p1, SM3P1>;
214 } // Predicates = [HasStdExtZksh]