1 //===-- RISCVInstrInfoD.td - RISC-V 'D' 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 'D',
10 // Double-Precision Floating-Point instruction set extension.
12 //===----------------------------------------------------------------------===//
14 //===----------------------------------------------------------------------===//
15 // RISC-V specific DAG Nodes.
16 //===----------------------------------------------------------------------===//
18 def SDT_RISCVBuildPairF64 : SDTypeProfile<1, 2, [SDTCisVT<0, f64>,
21 def SDT_RISCVSplitF64 : SDTypeProfile<2, 1, [SDTCisVT<0, i32>,
25 def RISCVBuildPairF64 : SDNode<"RISCVISD::BuildPairF64", SDT_RISCVBuildPairF64>;
26 def RISCVSplitF64 : SDNode<"RISCVISD::SplitF64", SDT_RISCVSplitF64>;
28 //===----------------------------------------------------------------------===//
29 // Instruction Class Templates
30 //===----------------------------------------------------------------------===//
32 let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
33 class FPFMAD_rrr_frm<RISCVOpcode opcode, string opcodestr>
34 : RVInstR4<0b01, opcode, (outs FPR64:$rd),
35 (ins FPR64:$rs1, FPR64:$rs2, FPR64:$rs3, frmarg:$funct3),
36 opcodestr, "$rd, $rs1, $rs2, $rs3, $funct3">;
38 class FPFMADDynFrmAlias<FPFMAD_rrr_frm Inst, string OpcodeStr>
39 : InstAlias<OpcodeStr#" $rd, $rs1, $rs2, $rs3",
40 (Inst FPR64:$rd, FPR64:$rs1, FPR64:$rs2, FPR64:$rs3, 0b111)>;
42 let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
43 class FPALUD_rr<bits<7> funct7, bits<3> funct3, string opcodestr>
44 : RVInstR<funct7, funct3, OPC_OP_FP, (outs FPR64:$rd),
45 (ins FPR64:$rs1, FPR64:$rs2), opcodestr, "$rd, $rs1, $rs2">;
47 let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
48 class FPALUD_rr_frm<bits<7> funct7, string opcodestr>
49 : RVInstRFrm<funct7, OPC_OP_FP, (outs FPR64:$rd),
50 (ins FPR64:$rs1, FPR64:$rs2, frmarg:$funct3), opcodestr,
51 "$rd, $rs1, $rs2, $funct3">;
53 class FPALUDDynFrmAlias<FPALUD_rr_frm Inst, string OpcodeStr>
54 : InstAlias<OpcodeStr#" $rd, $rs1, $rs2",
55 (Inst FPR64:$rd, FPR64:$rs1, FPR64:$rs2, 0b111)>;
57 let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
58 class FPCmpD_rr<bits<3> funct3, string opcodestr>
59 : RVInstR<0b1010001, funct3, OPC_OP_FP, (outs GPR:$rd),
60 (ins FPR64:$rs1, FPR64:$rs2), opcodestr, "$rd, $rs1, $rs2">;
62 //===----------------------------------------------------------------------===//
64 //===----------------------------------------------------------------------===//
66 let Predicates = [HasStdExtD] in {
68 let hasSideEffects = 0, mayLoad = 1, mayStore = 0 in
69 def FLD : RVInstI<0b011, OPC_LOAD_FP, (outs FPR64:$rd),
70 (ins GPR:$rs1, simm12:$imm12),
71 "fld", "$rd, ${imm12}(${rs1})">;
73 // Operands for stores are in the order srcreg, base, offset rather than
74 // reflecting the order these fields are specified in the instruction
76 let hasSideEffects = 0, mayLoad = 0, mayStore = 1 in
77 def FSD : RVInstS<0b011, OPC_STORE_FP, (outs),
78 (ins FPR64:$rs2, GPR:$rs1, simm12:$imm12),
79 "fsd", "$rs2, ${imm12}(${rs1})">;
81 def FMADD_D : FPFMAD_rrr_frm<OPC_MADD, "fmadd.d">;
82 def : FPFMADDynFrmAlias<FMADD_D, "fmadd.d">;
83 def FMSUB_D : FPFMAD_rrr_frm<OPC_MSUB, "fmsub.d">;
84 def : FPFMADDynFrmAlias<FMSUB_D, "fmsub.d">;
85 def FNMSUB_D : FPFMAD_rrr_frm<OPC_NMSUB, "fnmsub.d">;
86 def : FPFMADDynFrmAlias<FNMSUB_D, "fnmsub.d">;
87 def FNMADD_D : FPFMAD_rrr_frm<OPC_NMADD, "fnmadd.d">;
88 def : FPFMADDynFrmAlias<FNMADD_D, "fnmadd.d">;
90 def FADD_D : FPALUD_rr_frm<0b0000001, "fadd.d">;
91 def : FPALUDDynFrmAlias<FADD_D, "fadd.d">;
92 def FSUB_D : FPALUD_rr_frm<0b0000101, "fsub.d">;
93 def : FPALUDDynFrmAlias<FSUB_D, "fsub.d">;
94 def FMUL_D : FPALUD_rr_frm<0b0001001, "fmul.d">;
95 def : FPALUDDynFrmAlias<FMUL_D, "fmul.d">;
96 def FDIV_D : FPALUD_rr_frm<0b0001101, "fdiv.d">;
97 def : FPALUDDynFrmAlias<FDIV_D, "fdiv.d">;
99 def FSQRT_D : FPUnaryOp_r_frm<0b0101101, FPR64, FPR64, "fsqrt.d"> {
102 def : FPUnaryOpDynFrmAlias<FSQRT_D, "fsqrt.d", FPR64, FPR64>;
104 def FSGNJ_D : FPALUD_rr<0b0010001, 0b000, "fsgnj.d">;
105 def FSGNJN_D : FPALUD_rr<0b0010001, 0b001, "fsgnjn.d">;
106 def FSGNJX_D : FPALUD_rr<0b0010001, 0b010, "fsgnjx.d">;
107 def FMIN_D : FPALUD_rr<0b0010101, 0b000, "fmin.d">;
108 def FMAX_D : FPALUD_rr<0b0010101, 0b001, "fmax.d">;
110 def FCVT_S_D : FPUnaryOp_r_frm<0b0100000, FPR32, FPR64, "fcvt.s.d"> {
113 def : FPUnaryOpDynFrmAlias<FCVT_S_D, "fcvt.s.d", FPR32, FPR64>;
115 def FCVT_D_S : FPUnaryOp_r<0b0100001, 0b000, FPR64, FPR32, "fcvt.d.s"> {
119 def FEQ_D : FPCmpD_rr<0b010, "feq.d">;
120 def FLT_D : FPCmpD_rr<0b001, "flt.d">;
121 def FLE_D : FPCmpD_rr<0b000, "fle.d">;
123 def FCLASS_D : FPUnaryOp_r<0b1110001, 0b001, GPR, FPR64, "fclass.d"> {
127 def FCVT_W_D : FPUnaryOp_r_frm<0b1100001, GPR, FPR64, "fcvt.w.d"> {
130 def : FPUnaryOpDynFrmAlias<FCVT_W_D, "fcvt.w.d", GPR, FPR64>;
132 def FCVT_WU_D : FPUnaryOp_r_frm<0b1100001, GPR, FPR64, "fcvt.wu.d"> {
135 def : FPUnaryOpDynFrmAlias<FCVT_WU_D, "fcvt.wu.d", GPR, FPR64>;
137 def FCVT_D_W : FPUnaryOp_r<0b1101001, 0b000, FPR64, GPR, "fcvt.d.w"> {
141 def FCVT_D_WU : FPUnaryOp_r<0b1101001, 0b000, FPR64, GPR, "fcvt.d.wu"> {
144 } // Predicates = [HasStdExtD]
146 let Predicates = [HasStdExtD, IsRV64] in {
147 def FCVT_L_D : FPUnaryOp_r_frm<0b1100001, GPR, FPR64, "fcvt.l.d"> {
150 def : FPUnaryOpDynFrmAlias<FCVT_L_D, "fcvt.l.d", GPR, FPR64>;
152 def FCVT_LU_D : FPUnaryOp_r_frm<0b1100001, GPR, FPR64, "fcvt.lu.d"> {
155 def : FPUnaryOpDynFrmAlias<FCVT_LU_D, "fcvt.lu.d", GPR, FPR64>;
157 def FMV_X_D : FPUnaryOp_r<0b1110001, 0b000, GPR, FPR64, "fmv.x.d"> {
161 def FCVT_D_L : FPUnaryOp_r_frm<0b1101001, FPR64, GPR, "fcvt.d.l"> {
164 def : FPUnaryOpDynFrmAlias<FCVT_D_L, "fcvt.d.l", FPR64, GPR>;
166 def FCVT_D_LU : FPUnaryOp_r_frm<0b1101001, FPR64, GPR, "fcvt.d.lu"> {
169 def : FPUnaryOpDynFrmAlias<FCVT_D_LU, "fcvt.d.lu", FPR64, GPR>;
171 def FMV_D_X : FPUnaryOp_r<0b1111001, 0b000, FPR64, GPR, "fmv.d.x"> {
174 } // Predicates = [HasStdExtD, IsRV64]
176 //===----------------------------------------------------------------------===//
177 // Assembler Pseudo Instructions (User-Level ISA, Version 2.2, Chapter 20)
178 //===----------------------------------------------------------------------===//
180 let Predicates = [HasStdExtD] in {
181 def : InstAlias<"fld $rd, (${rs1})", (FLD FPR64:$rd, GPR:$rs1, 0), 0>;
182 def : InstAlias<"fsd $rs2, (${rs1})", (FSD FPR64:$rs2, GPR:$rs1, 0), 0>;
184 def : InstAlias<"fmv.d $rd, $rs", (FSGNJ_D FPR64:$rd, FPR64:$rs, FPR64:$rs)>;
185 def : InstAlias<"fabs.d $rd, $rs", (FSGNJX_D FPR64:$rd, FPR64:$rs, FPR64:$rs)>;
186 def : InstAlias<"fneg.d $rd, $rs", (FSGNJN_D FPR64:$rd, FPR64:$rs, FPR64:$rs)>;
188 // fgt.d/fge.d are recognised by the GNU assembler but the canonical
189 // flt.d/fle.d forms will always be printed. Therefore, set a zero weight.
190 def : InstAlias<"fgt.d $rd, $rs, $rt",
191 (FLT_D GPR:$rd, FPR64:$rt, FPR64:$rs), 0>;
192 def : InstAlias<"fge.d $rd, $rs, $rt",
193 (FLE_D GPR:$rd, FPR64:$rt, FPR64:$rs), 0>;
195 def PseudoFLD : PseudoFloatLoad<"fld", FPR64>;
196 def PseudoFSD : PseudoStore<"fsd", FPR64>;
197 } // Predicates = [HasStdExtD]
199 //===----------------------------------------------------------------------===//
200 // Pseudo-instructions and codegen patterns
201 //===----------------------------------------------------------------------===//
203 class PatFpr64Fpr64<SDPatternOperator OpNode, RVInstR Inst>
204 : Pat<(OpNode FPR64:$rs1, FPR64:$rs2), (Inst $rs1, $rs2)>;
206 class PatFpr64Fpr64DynFrm<SDPatternOperator OpNode, RVInstRFrm Inst>
207 : Pat<(OpNode FPR64:$rs1, FPR64:$rs2), (Inst $rs1, $rs2, 0b111)>;
209 let Predicates = [HasStdExtD] in {
211 /// Float conversion operations
213 // f64 -> f32, f32 -> f64
214 def : Pat<(fpround FPR64:$rs1), (FCVT_S_D FPR64:$rs1, 0b111)>;
215 def : Pat<(fpextend FPR32:$rs1), (FCVT_D_S FPR32:$rs1)>;
217 // [u]int<->double conversion patterns must be gated on IsRV32 or IsRV64, so
218 // are defined later.
220 /// Float arithmetic operations
222 def : PatFpr64Fpr64DynFrm<fadd, FADD_D>;
223 def : PatFpr64Fpr64DynFrm<fsub, FSUB_D>;
224 def : PatFpr64Fpr64DynFrm<fmul, FMUL_D>;
225 def : PatFpr64Fpr64DynFrm<fdiv, FDIV_D>;
227 def : Pat<(fsqrt FPR64:$rs1), (FSQRT_D FPR64:$rs1, 0b111)>;
229 def : Pat<(fneg FPR64:$rs1), (FSGNJN_D $rs1, $rs1)>;
230 def : Pat<(fabs FPR64:$rs1), (FSGNJX_D $rs1, $rs1)>;
232 def : PatFpr64Fpr64<fcopysign, FSGNJ_D>;
233 def : Pat<(fcopysign FPR64:$rs1, (fneg FPR64:$rs2)), (FSGNJN_D $rs1, $rs2)>;
235 // fmadd: rs1 * rs2 + rs3
236 def : Pat<(fma FPR64:$rs1, FPR64:$rs2, FPR64:$rs3),
237 (FMADD_D $rs1, $rs2, $rs3, 0b111)>;
239 // fmsub: rs1 * rs2 - rs3
240 def : Pat<(fma FPR64:$rs1, FPR64:$rs2, (fneg FPR64:$rs3)),
241 (FMSUB_D FPR64:$rs1, FPR64:$rs2, FPR64:$rs3, 0b111)>;
243 // fnmsub: -rs1 * rs2 + rs3
244 def : Pat<(fma (fneg FPR64:$rs1), FPR64:$rs2, FPR64:$rs3),
245 (FNMSUB_D FPR64:$rs1, FPR64:$rs2, FPR64:$rs3, 0b111)>;
247 // fnmadd: -rs1 * rs2 - rs3
248 def : Pat<(fma (fneg FPR64:$rs1), FPR64:$rs2, (fneg FPR64:$rs3)),
249 (FNMADD_D FPR64:$rs1, FPR64:$rs2, FPR64:$rs3, 0b111)>;
251 // The RISC-V 2.2 user-level ISA spec defines fmin and fmax as returning the
252 // canonical NaN when giving a signaling NaN. This doesn't match the LLVM
253 // behaviour (see https://bugs.llvm.org/show_bug.cgi?id=27363). However, the
254 // draft 2.3 ISA spec changes the definition of fmin and fmax in a way that
255 // matches LLVM's fminnum and fmaxnum
256 // <https://github.com/riscv/riscv-isa-manual/commit/cd20cee7efd9bac7c5aa127ec3b451749d2b3cce>.
257 def : PatFpr64Fpr64<fminnum, FMIN_D>;
258 def : PatFpr64Fpr64<fmaxnum, FMAX_D>;
262 def : PatFpr64Fpr64<seteq, FEQ_D>;
263 def : PatFpr64Fpr64<setoeq, FEQ_D>;
264 def : PatFpr64Fpr64<setlt, FLT_D>;
265 def : PatFpr64Fpr64<setolt, FLT_D>;
266 def : PatFpr64Fpr64<setle, FLE_D>;
267 def : PatFpr64Fpr64<setole, FLE_D>;
269 // Define pattern expansions for setcc operations which aren't directly
270 // handled by a RISC-V instruction and aren't expanded in the SelectionDAG
273 def : Pat<(seto FPR64:$rs1, FPR64:$rs2),
274 (AND (FEQ_D FPR64:$rs1, FPR64:$rs1),
275 (FEQ_D FPR64:$rs2, FPR64:$rs2))>;
277 def : Pat<(setuo FPR64:$rs1, FPR64:$rs2),
278 (SLTIU (AND (FEQ_D FPR64:$rs1, FPR64:$rs1),
279 (FEQ_D FPR64:$rs2, FPR64:$rs2)),
282 def Select_FPR64_Using_CC_GPR : SelectCC_rrirr<FPR64, GPR>;
286 defm : LdPat<load, FLD>;
290 defm : StPat<store, FSD, FPR64>;
292 /// Pseudo-instructions needed for the soft-float ABI with RV32D
294 // Moves two GPRs to an FPR.
295 let usesCustomInserter = 1 in
296 def BuildPairF64Pseudo
297 : Pseudo<(outs FPR64:$dst), (ins GPR:$src1, GPR:$src2),
298 [(set FPR64:$dst, (RISCVBuildPairF64 GPR:$src1, GPR:$src2))]>;
300 // Moves an FPR to two GPRs.
301 let usesCustomInserter = 1 in
303 : Pseudo<(outs GPR:$dst1, GPR:$dst2), (ins FPR64:$src),
304 [(set GPR:$dst1, GPR:$dst2, (RISCVSplitF64 FPR64:$src))]>;
306 } // Predicates = [HasStdExtD]
308 let Predicates = [HasStdExtD, IsRV32] in {
309 // double->[u]int. Round-to-zero must be used.
310 def : Pat<(fp_to_sint FPR64:$rs1), (FCVT_W_D FPR64:$rs1, 0b001)>;
311 def : Pat<(fp_to_uint FPR64:$rs1), (FCVT_WU_D FPR64:$rs1, 0b001)>;
314 def : Pat<(sint_to_fp GPR:$rs1), (FCVT_D_W GPR:$rs1)>;
315 def : Pat<(uint_to_fp GPR:$rs1), (FCVT_D_WU GPR:$rs1)>;
316 } // Predicates = [HasStdExtD, IsRV32]
318 let Predicates = [HasStdExtD, IsRV64] in {
319 def : Pat<(bitconvert GPR:$rs1), (FMV_D_X GPR:$rs1)>;
320 def : Pat<(bitconvert FPR64:$rs1), (FMV_X_D FPR64:$rs1)>;
322 // FP->[u]int32 is mostly handled by the FP->[u]int64 patterns. This is safe
323 // because fpto[u|s]i produce poison if the value can't fit into the target.
324 // We match the single case below because fcvt.wu.d sign-extends its result so
325 // is cheaper than fcvt.lu.d+sext.w.
326 def : Pat<(sext_inreg (zexti32 (fp_to_uint FPR64:$rs1)), i32),
327 (FCVT_WU_D $rs1, 0b001)>;
330 def : Pat<(sint_to_fp (sext_inreg GPR:$rs1, i32)), (FCVT_D_W $rs1)>;
331 def : Pat<(uint_to_fp (zexti32 GPR:$rs1)), (FCVT_D_WU $rs1)>;
333 def : Pat<(fp_to_sint FPR64:$rs1), (FCVT_L_D FPR64:$rs1, 0b001)>;
334 def : Pat<(fp_to_uint FPR64:$rs1), (FCVT_LU_D FPR64:$rs1, 0b001)>;
336 // [u]int64->fp. Match GCC and default to using dynamic rounding mode.
337 def : Pat<(sint_to_fp GPR:$rs1), (FCVT_D_L GPR:$rs1, 0b111)>;
338 def : Pat<(uint_to_fp GPR:$rs1), (FCVT_D_LU GPR:$rs1, 0b111)>;
339 } // Predicates = [HasStdExtD, IsRV64]