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 : 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"; 44} 45 46def byteselect : RISCVOp<i32>, TImmLeaf<i32, [{return isUInt<2>(Imm);}]> { 47 let ParserMatchClass = UImmAsmOperand<2>; 48 let DecoderMethod = "decodeUImmOperand<2>"; 49 let OperandType = "OPERAND_UIMM2"; 50} 51 52//===----------------------------------------------------------------------===// 53// Instruction class templates 54//===----------------------------------------------------------------------===// 55let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in 56class 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">; 59 60let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in 61class 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">{ 65 bits<2> bs; 66 let Inst{31-30} = bs; 67} 68 69let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in 70class 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"> { 73 bits<4> rnum; 74 let Inst{31-25} = funct7; 75 let Inst{24} = 0b1; 76 let Inst{23-20} = rnum; 77} 78 79//===----------------------------------------------------------------------===// 80// Instructions 81//===----------------------------------------------------------------------===// 82let Predicates = [HasStdExtZknd, IsRV32] in { 83def AES32DSI : RVKByteSelect<0b10101, "aes32dsi">; 84def AES32DSMI : RVKByteSelect<0b10111, "aes32dsmi">; 85} // Predicates = [HasStdExtZknd, IsRV32] 86 87let Predicates = [HasStdExtZknd, IsRV64] in { 88def AES64DS : ALU_rr<0b0011101, 0b000, "aes64ds">; 89def AES64DSM : ALU_rr<0b0011111, 0b000, "aes64dsm">; 90 91def AES64IM : RVKUnary<0b001100000000, 0b001, "aes64im">; 92} // Predicates = [HasStdExtZknd, IsRV64] 93 94let Predicates = [HasStdExtZkndOrZkne, IsRV64] in { 95def AES64KS2 : ALU_rr<0b0111111, 0b000, "aes64ks2">; 96 97def AES64KS1I : RVKUnary_rnum<0b0011000, 0b001, "aes64ks1i">; 98} // Predicates = [HasStdExtZkndOrZkne, IsRV64] 99 100let Predicates = [HasStdExtZkne, IsRV32] in { 101def AES32ESI : RVKByteSelect<0b10001, "aes32esi">; 102def AES32ESMI : RVKByteSelect<0b10011, "aes32esmi">; 103} // Predicates = [HasStdExtZkne, IsRV32] 104 105let Predicates = [HasStdExtZkne, IsRV64] in { 106def AES64ES : ALU_rr<0b0011001, 0b000, "aes64es">; 107def AES64ESM : ALU_rr<0b0011011, 0b000, "aes64esm">; 108} // Predicates = [HasStdExtZkne, IsRV64] 109 110let Predicates = [HasStdExtZknh], IsSignExtendingOpW = 1 in { 111def SHA256SIG0 : RVKUnary<0b000100000010, 0b001, "sha256sig0">; 112def SHA256SIG1 : RVKUnary<0b000100000011, 0b001, "sha256sig1">; 113def SHA256SUM0 : RVKUnary<0b000100000000, 0b001, "sha256sum0">; 114def SHA256SUM1 : RVKUnary<0b000100000001, 0b001, "sha256sum1">; 115} // Predicates = [HasStdExtZknh] 116 117let Predicates = [HasStdExtZknh, IsRV32] in { 118def SHA512SIG0H : ALU_rr<0b0101110, 0b000, "sha512sig0h">; 119def SHA512SIG0L : ALU_rr<0b0101010, 0b000, "sha512sig0l">; 120def SHA512SIG1H : ALU_rr<0b0101111, 0b000, "sha512sig1h">; 121def SHA512SIG1L : ALU_rr<0b0101011, 0b000, "sha512sig1l">; 122def SHA512SUM0R : ALU_rr<0b0101000, 0b000, "sha512sum0r">; 123def SHA512SUM1R : ALU_rr<0b0101001, 0b000, "sha512sum1r">; 124} // [HasStdExtZknh, IsRV32] 125 126let Predicates = [HasStdExtZknh, IsRV64] in { 127def SHA512SIG0 : RVKUnary<0b000100000110, 0b001, "sha512sig0">; 128def SHA512SIG1 : RVKUnary<0b000100000111, 0b001, "sha512sig1">; 129def SHA512SUM0 : RVKUnary<0b000100000100, 0b001, "sha512sum0">; 130def SHA512SUM1 : RVKUnary<0b000100000101, 0b001, "sha512sum1">; 131} // Predicates = [HasStdExtZknh, IsRV64] 132 133let Predicates = [HasStdExtZksed], IsSignExtendingOpW = 1 in { 134def SM4ED : RVKByteSelect<0b11000, "sm4ed">; 135def SM4KS : RVKByteSelect<0b11010, "sm4ks">; 136} // Predicates = [HasStdExtZksed] 137 138let Predicates = [HasStdExtZksh], IsSignExtendingOpW = 1 in { 139def SM3P0 : RVKUnary<0b000100001000, 0b001, "sm3p0">; 140def SM3P1 : RVKUnary<0b000100001001, 0b001, "sm3p1">; 141} // Predicates = [HasStdExtZksh] 142 143//===----------------------------------------------------------------------===// 144// Codegen patterns 145//===----------------------------------------------------------------------===// 146 147class 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)>; 150 151// Zknd 152let Predicates = [HasStdExtZknd, IsRV32] in { 153def : PatGprGprByteSelect<int_riscv_aes32dsi, AES32DSI>; 154def : PatGprGprByteSelect<int_riscv_aes32dsmi, AES32DSMI>; 155} // Predicates = [HasStdExtZknd, IsRV32] 156 157let Predicates = [HasStdExtZknd, IsRV64] in { 158def : PatGprGpr<int_riscv_aes64ds, AES64DS>; 159def : PatGprGpr<int_riscv_aes64dsm, AES64DSM>; 160def : PatGpr<int_riscv_aes64im, AES64IM>; 161} // Predicates = [HasStdExtZknd, IsRV64] 162 163let Predicates = [HasStdExtZkndOrZkne, IsRV64] in { 164def : PatGprGpr<int_riscv_aes64ks2, AES64KS2>; 165def : Pat<(int_riscv_aes64ks1i GPR:$rs1, rnum:$rnum), 166 (AES64KS1I GPR:$rs1, rnum:$rnum)>; 167} // Predicates = [HasStdExtZkndOrZkne, IsRV64] 168 169// Zkne 170let Predicates = [HasStdExtZkne, IsRV32] in { 171def : PatGprGprByteSelect<int_riscv_aes32esi, AES32ESI>; 172def : PatGprGprByteSelect<int_riscv_aes32esmi, AES32ESMI>; 173} // Predicates = [HasStdExtZkne, IsRV32] 174 175let Predicates = [HasStdExtZkne, IsRV64] in { 176def : PatGprGpr<int_riscv_aes64es, AES64ES>; 177def : PatGprGpr<int_riscv_aes64esm, AES64ESM>; 178} // Predicates = [HasStdExtZkne, IsRV64] 179 180// Zknh 181let Predicates = [HasStdExtZknh] in { 182def : PatGpr<riscv_sha256sig0, SHA256SIG0>; 183def : PatGpr<riscv_sha256sig1, SHA256SIG1>; 184def : PatGpr<riscv_sha256sum0, SHA256SUM0>; 185def : PatGpr<riscv_sha256sum1, SHA256SUM1>; 186} // Predicates = [HasStdExtZknh] 187 188let Predicates = [HasStdExtZknh, IsRV32] in { 189def : PatGprGpr<int_riscv_sha512sig0l, SHA512SIG0L>; 190def : PatGprGpr<int_riscv_sha512sig0h, SHA512SIG0H>; 191def : PatGprGpr<int_riscv_sha512sig1l, SHA512SIG1L>; 192def : PatGprGpr<int_riscv_sha512sig1h, SHA512SIG1H>; 193def : PatGprGpr<int_riscv_sha512sum0r, SHA512SUM0R>; 194def : PatGprGpr<int_riscv_sha512sum1r, SHA512SUM1R>; 195} // Predicates = [HasStdExtZknh, IsRV32] 196 197let Predicates = [HasStdExtZknh, IsRV64] in { 198def : PatGpr<int_riscv_sha512sig0, SHA512SIG0>; 199def : PatGpr<int_riscv_sha512sig1, SHA512SIG1>; 200def : PatGpr<int_riscv_sha512sum0, SHA512SUM0>; 201def : PatGpr<int_riscv_sha512sum1, SHA512SUM1>; 202} // Predicates = [HasStdExtZknh, IsRV64] 203 204// Zksed 205let Predicates = [HasStdExtZksed] in { 206def : PatGprGprByteSelect<riscv_sm4ks, SM4KS>; 207def : PatGprGprByteSelect<riscv_sm4ed, SM4ED>; 208} // Predicates = [HasStdExtZksed] 209 210// Zksh 211let Predicates = [HasStdExtZksh] in { 212def : PatGpr<riscv_sm3p0, SM3P0>; 213def : PatGpr<riscv_sm3p1, SM3P1>; 214} // Predicates = [HasStdExtZksh] 215