xref: /freebsd/contrib/llvm-project/llvm/lib/Target/RISCV/RISCVInstrInfoZk.td (revision 0ad011ececb978e22a9bff2acf76633b094f1ff6)
1//===- RISCVInstrInfoZk.td - RISC-V 'Zk' instructions ------*- tablegen -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file describes the RISC-V instructions from the standard 'Zk',
10// Scalar Cryptography Instructions extension, version 1.0.
11//
12//===----------------------------------------------------------------------===//
13
14//===----------------------------------------------------------------------===//
15// Operand and SDNode transformation definitions.
16//===----------------------------------------------------------------------===//
17
18def riscv_sha256sig0 : SDNode<"RISCVISD::SHA256SIG0", SDTIntUnaryOp>;
19def riscv_sha256sig1 : SDNode<"RISCVISD::SHA256SIG1", SDTIntUnaryOp>;
20def riscv_sha256sum0 : SDNode<"RISCVISD::SHA256SUM0", SDTIntUnaryOp>;
21def riscv_sha256sum1 : SDNode<"RISCVISD::SHA256SUM1", SDTIntUnaryOp>;
22
23def SDT_RISCVZkByteSelect : SDTypeProfile<1, 3, [SDTCisVT<0, XLenVT>,
24                                                 SDTCisVT<1, XLenVT>,
25                                                 SDTCisVT<2, XLenVT>,
26                                                 SDTCisVT<3, i32>]>;
27def riscv_sm4ks : SDNode<"RISCVISD::SM4KS", SDT_RISCVZkByteSelect>;
28def riscv_sm4ed : SDNode<"RISCVISD::SM4ED", SDT_RISCVZkByteSelect>;
29
30def riscv_sm3p0 : SDNode<"RISCVISD::SM3P0", SDTIntUnaryOp>;
31def riscv_sm3p1 : SDNode<"RISCVISD::SM3P1", SDTIntUnaryOp>;
32
33def RnumArg : AsmOperandClass {
34  let Name = "RnumArg";
35  let RenderMethod = "addImmOperands";
36  let DiagnosticType = "InvalidRnumArg";
37}
38
39def rnum : Operand<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";
44  let OperandNamespace = "RISCVOp";
45}
46
47def byteselect : Operand<i32>, TImmLeaf<i32, [{return isUInt<2>(Imm);}]> {
48  let ParserMatchClass = UImmAsmOperand<2>;
49  let DecoderMethod = "decodeUImmOperand<2>";
50  let OperandType = "OPERAND_UIMM2";
51  let OperandNamespace = "RISCVOp";
52}
53
54//===----------------------------------------------------------------------===//
55// Instruction class templates
56//===----------------------------------------------------------------------===//
57let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
58class RVKUnary<bits<12> imm12_in, bits<3> funct3, string opcodestr>
59    : RVInstI<funct3, OPC_OP_IMM, (outs GPR:$rd), (ins GPR:$rs1),
60              opcodestr, "$rd, $rs1">{
61  let imm12 = imm12_in;
62}
63
64let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
65class RVKByteSelect<bits<5> funct5, string opcodestr>
66    : RVInstR<{0b00, funct5}, 0b000, OPC_OP, (outs GPR:$rd),
67              (ins GPR:$rs1, GPR:$rs2, byteselect:$bs),
68              opcodestr, "$rd, $rs1, $rs2, $bs">{
69  bits<2> bs;
70  let Inst{31-30} = bs;
71}
72
73let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
74class RVKUnary_rnum<bits<7> funct7, bits<3> funct3, string opcodestr>
75    : RVInstI<funct3, OPC_OP_IMM, (outs GPR:$rd), (ins GPR:$rs1, rnum:$rnum),
76              opcodestr, "$rd, $rs1, $rnum">{
77    bits<4> rnum;
78    let Inst{31-25} = funct7;
79    let Inst{24} = 1;
80    let Inst{23-20} = rnum;
81}
82
83//===----------------------------------------------------------------------===//
84// Instructions
85//===----------------------------------------------------------------------===//
86let Predicates = [HasStdExtZknd, IsRV32] in {
87def AES32DSI  : RVKByteSelect<0b10101, "aes32dsi">;
88def AES32DSMI : RVKByteSelect<0b10111, "aes32dsmi">;
89} // Predicates = [HasStdExtZknd, IsRV32]
90
91let Predicates = [HasStdExtZknd, IsRV64] in {
92def AES64DS  : ALU_rr<0b0011101, 0b000, "aes64ds">;
93def AES64DSM : ALU_rr<0b0011111, 0b000, "aes64dsm">;
94
95def AES64IM  : RVKUnary<0b001100000000, 0b001, "aes64im">;
96} // Predicates = [HasStdExtZknd, IsRV64]
97
98let Predicates = [HasStdExtZkndOrZkne, IsRV64] in {
99def AES64KS2  : ALU_rr<0b0111111, 0b000, "aes64ks2">;
100
101def AES64KS1I : RVKUnary_rnum<0b0011000, 0b001, "aes64ks1i">;
102} // Predicates = [HasStdExtZkndOrZkne, IsRV64]
103
104let Predicates = [HasStdExtZkne, IsRV32] in {
105def AES32ESI  : RVKByteSelect<0b10001, "aes32esi">;
106def AES32ESMI : RVKByteSelect<0b10011, "aes32esmi">;
107} // Predicates = [HasStdExtZkne, IsRV32]
108
109let Predicates = [HasStdExtZkne, IsRV64] in {
110def AES64ES   : ALU_rr<0b0011001, 0b000, "aes64es">;
111def AES64ESM  : ALU_rr<0b0011011, 0b000, "aes64esm">;
112} // Predicates = [HasStdExtZkne, IsRV64]
113
114let Predicates = [HasStdExtZknh], IsSignExtendingOpW = 1 in {
115def SHA256SIG0 : RVKUnary<0b000100000010, 0b001, "sha256sig0">;
116def SHA256SIG1 : RVKUnary<0b000100000011, 0b001, "sha256sig1">;
117def SHA256SUM0 : RVKUnary<0b000100000000, 0b001, "sha256sum0">;
118def SHA256SUM1 : RVKUnary<0b000100000001, 0b001, "sha256sum1">;
119} // Predicates = [HasStdExtZknh]
120
121let Predicates = [HasStdExtZknh, IsRV32] in {
122def SHA512SIG0H : ALU_rr<0b0101110, 0b000, "sha512sig0h">;
123def SHA512SIG0L : ALU_rr<0b0101010, 0b000, "sha512sig0l">;
124def SHA512SIG1H : ALU_rr<0b0101111, 0b000, "sha512sig1h">;
125def SHA512SIG1L : ALU_rr<0b0101011, 0b000, "sha512sig1l">;
126def SHA512SUM0R : ALU_rr<0b0101000, 0b000, "sha512sum0r">;
127def SHA512SUM1R : ALU_rr<0b0101001, 0b000, "sha512sum1r">;
128} // [HasStdExtZknh, IsRV32]
129
130let Predicates = [HasStdExtZknh, IsRV64] in {
131def SHA512SIG0 : RVKUnary<0b000100000110, 0b001, "sha512sig0">;
132def SHA512SIG1 : RVKUnary<0b000100000111, 0b001, "sha512sig1">;
133def SHA512SUM0 : RVKUnary<0b000100000100, 0b001, "sha512sum0">;
134def SHA512SUM1 : RVKUnary<0b000100000101, 0b001, "sha512sum1">;
135} // Predicates = [HasStdExtZknh, IsRV64]
136
137let Predicates = [HasStdExtZksed], IsSignExtendingOpW = 1 in {
138def SM4ED : RVKByteSelect<0b11000, "sm4ed">;
139def SM4KS : RVKByteSelect<0b11010, "sm4ks">;
140} // Predicates = [HasStdExtZksed]
141
142let Predicates = [HasStdExtZksh], IsSignExtendingOpW = 1 in {
143def SM3P0 : RVKUnary<0b000100001000, 0b001, "sm3p0">;
144def SM3P1 : RVKUnary<0b000100001001, 0b001, "sm3p1">;
145} // Predicates = [HasStdExtZksh]
146
147//===----------------------------------------------------------------------===//
148// Codegen patterns
149//===----------------------------------------------------------------------===//
150
151class PatGprGprByteSelect<SDPatternOperator OpNode, RVInst Inst>
152    : Pat<(XLenVT (OpNode (XLenVT GPR:$rs1), (XLenVT GPR:$rs2), byteselect:$imm)),
153          (Inst GPR:$rs1, GPR:$rs2, byteselect:$imm)>;
154
155// Zknd
156let Predicates = [HasStdExtZknd, IsRV32] in {
157def : PatGprGprByteSelect<int_riscv_aes32dsi, AES32DSI>;
158def : PatGprGprByteSelect<int_riscv_aes32dsmi, AES32DSMI>;
159} // Predicates = [HasStdExtZknd, IsRV32]
160
161let Predicates = [HasStdExtZknd, IsRV64] in {
162def : PatGprGpr<int_riscv_aes64ds, AES64DS>;
163def : PatGprGpr<int_riscv_aes64dsm, AES64DSM>;
164def : PatGpr<int_riscv_aes64im, AES64IM>;
165} // Predicates = [HasStdExtZknd, IsRV64]
166
167let Predicates = [HasStdExtZkndOrZkne, IsRV64] in {
168def : PatGprGpr<int_riscv_aes64ks2, AES64KS2>;
169def : Pat<(int_riscv_aes64ks1i GPR:$rs1, rnum:$rnum),
170          (AES64KS1I GPR:$rs1, rnum:$rnum)>;
171} // Predicates = [HasStdExtZkndOrZkne, IsRV64]
172
173// Zkne
174let Predicates = [HasStdExtZkne, IsRV32] in {
175def : PatGprGprByteSelect<int_riscv_aes32esi, AES32ESI>;
176def : PatGprGprByteSelect<int_riscv_aes32esmi, AES32ESMI>;
177} // Predicates = [HasStdExtZkne, IsRV32]
178
179let Predicates = [HasStdExtZkne, IsRV64] in {
180def : PatGprGpr<int_riscv_aes64es, AES64ES>;
181def : PatGprGpr<int_riscv_aes64esm, AES64ESM>;
182} // Predicates = [HasStdExtZkne, IsRV64]
183
184// Zknh
185let Predicates = [HasStdExtZknh] in {
186def : PatGpr<riscv_sha256sig0, SHA256SIG0>;
187def : PatGpr<riscv_sha256sig1, SHA256SIG1>;
188def : PatGpr<riscv_sha256sum0, SHA256SUM0>;
189def : PatGpr<riscv_sha256sum1, SHA256SUM1>;
190} // Predicates = [HasStdExtZknh]
191
192let Predicates = [HasStdExtZknh, IsRV32] in {
193def : PatGprGpr<int_riscv_sha512sig0l, SHA512SIG0L>;
194def : PatGprGpr<int_riscv_sha512sig0h, SHA512SIG0H>;
195def : PatGprGpr<int_riscv_sha512sig1l, SHA512SIG1L>;
196def : PatGprGpr<int_riscv_sha512sig1h, SHA512SIG1H>;
197def : PatGprGpr<int_riscv_sha512sum0r, SHA512SUM0R>;
198def : PatGprGpr<int_riscv_sha512sum1r, SHA512SUM1R>;
199} // Predicates = [HasStdExtZknh, IsRV32]
200
201let Predicates = [HasStdExtZknh, IsRV64] in {
202def : PatGpr<int_riscv_sha512sig0, SHA512SIG0>;
203def : PatGpr<int_riscv_sha512sig1, SHA512SIG1>;
204def : PatGpr<int_riscv_sha512sum0, SHA512SUM0>;
205def : PatGpr<int_riscv_sha512sum1, SHA512SUM1>;
206} // Predicates = [HasStdExtZknh, IsRV64]
207
208// Zksed
209let Predicates = [HasStdExtZksed] in {
210def : PatGprGprByteSelect<riscv_sm4ks, SM4KS>;
211def : PatGprGprByteSelect<riscv_sm4ed, SM4ED>;
212} // Predicates = [HasStdExtZksed]
213
214// Zksh
215let Predicates = [HasStdExtZksh] in {
216def : PatGpr<riscv_sm3p0, SM3P0>;
217def : PatGpr<riscv_sm3p1, SM3P1>;
218} // Predicates = [HasStdExtZksh]
219