1 //===-- RISCVInstrInfoF.td - RISC-V 'F' 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 'F',
10 // Single-Precision Floating-Point instruction set extension.
12 //===----------------------------------------------------------------------===//
14 //===----------------------------------------------------------------------===//
15 // RISC-V specific DAG Nodes.
16 //===----------------------------------------------------------------------===//
18 def SDT_RISCVFMV_W_X_RV64
19 : SDTypeProfile<1, 1, [SDTCisVT<0, f32>, SDTCisVT<1, i64>]>;
20 def SDT_RISCVFMV_X_ANYEXTW_RV64
21 : SDTypeProfile<1, 1, [SDTCisVT<0, i64>, SDTCisVT<1, f32>]>;
23 def riscv_fmv_w_x_rv64
24 : SDNode<"RISCVISD::FMV_W_X_RV64", SDT_RISCVFMV_W_X_RV64>;
25 def riscv_fmv_x_anyextw_rv64
26 : SDNode<"RISCVISD::FMV_X_ANYEXTW_RV64", SDT_RISCVFMV_X_ANYEXTW_RV64>;
28 //===----------------------------------------------------------------------===//
29 // Operand and SDNode transformation definitions.
30 //===----------------------------------------------------------------------===//
32 // Floating-point rounding mode
34 def FRMArg : AsmOperandClass {
36 let RenderMethod = "addFRMArgOperands";
37 let DiagnosticType = "InvalidFRMArg";
40 def frmarg : Operand<XLenVT> {
41 let ParserMatchClass = FRMArg;
42 let PrintMethod = "printFRMArg";
43 let DecoderMethod = "decodeFRMArg";
46 //===----------------------------------------------------------------------===//
47 // Instruction class templates
48 //===----------------------------------------------------------------------===//
50 let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
51 class FPFMAS_rrr_frm<RISCVOpcode opcode, string opcodestr>
52 : RVInstR4<0b00, opcode, (outs FPR32:$rd),
53 (ins FPR32:$rs1, FPR32:$rs2, FPR32:$rs3, frmarg:$funct3),
54 opcodestr, "$rd, $rs1, $rs2, $rs3, $funct3">;
56 class FPFMASDynFrmAlias<FPFMAS_rrr_frm Inst, string OpcodeStr>
57 : InstAlias<OpcodeStr#" $rd, $rs1, $rs2, $rs3",
58 (Inst FPR32:$rd, FPR32:$rs1, FPR32:$rs2, FPR32:$rs3, 0b111)>;
60 let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
61 class FPALUS_rr<bits<7> funct7, bits<3> funct3, string opcodestr>
62 : RVInstR<funct7, funct3, OPC_OP_FP, (outs FPR32:$rd),
63 (ins FPR32:$rs1, FPR32:$rs2), opcodestr, "$rd, $rs1, $rs2">;
65 let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
66 class FPALUS_rr_frm<bits<7> funct7, string opcodestr>
67 : RVInstRFrm<funct7, OPC_OP_FP, (outs FPR32:$rd),
68 (ins FPR32:$rs1, FPR32:$rs2, frmarg:$funct3), opcodestr,
69 "$rd, $rs1, $rs2, $funct3">;
71 class FPALUSDynFrmAlias<FPALUS_rr_frm Inst, string OpcodeStr>
72 : InstAlias<OpcodeStr#" $rd, $rs1, $rs2",
73 (Inst FPR32:$rd, FPR32:$rs1, FPR32:$rs2, 0b111)>;
75 let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
76 class FPUnaryOp_r<bits<7> funct7, bits<3> funct3, RegisterClass rdty,
77 RegisterClass rs1ty, string opcodestr>
78 : RVInstR<funct7, funct3, OPC_OP_FP, (outs rdty:$rd), (ins rs1ty:$rs1),
79 opcodestr, "$rd, $rs1">;
81 let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
82 class FPUnaryOp_r_frm<bits<7> funct7, RegisterClass rdty, RegisterClass rs1ty,
84 : RVInstRFrm<funct7, OPC_OP_FP, (outs rdty:$rd),
85 (ins rs1ty:$rs1, frmarg:$funct3), opcodestr,
86 "$rd, $rs1, $funct3">;
88 class FPUnaryOpDynFrmAlias<FPUnaryOp_r_frm Inst, string OpcodeStr,
89 RegisterClass rdty, RegisterClass rs1ty>
90 : InstAlias<OpcodeStr#" $rd, $rs1",
91 (Inst rdty:$rd, rs1ty:$rs1, 0b111)>;
93 let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
94 class FPCmpS_rr<bits<3> funct3, string opcodestr>
95 : RVInstR<0b1010000, funct3, OPC_OP_FP, (outs GPR:$rd),
96 (ins FPR32:$rs1, FPR32:$rs2), opcodestr, "$rd, $rs1, $rs2">;
98 //===----------------------------------------------------------------------===//
100 //===----------------------------------------------------------------------===//
102 let Predicates = [HasStdExtF] in {
103 let hasSideEffects = 0, mayLoad = 1, mayStore = 0 in
104 def FLW : RVInstI<0b010, OPC_LOAD_FP, (outs FPR32:$rd),
105 (ins GPR:$rs1, simm12:$imm12),
106 "flw", "$rd, ${imm12}(${rs1})">;
108 // Operands for stores are in the order srcreg, base, offset rather than
109 // reflecting the order these fields are specified in the instruction
111 let hasSideEffects = 0, mayLoad = 0, mayStore = 1 in
112 def FSW : RVInstS<0b010, OPC_STORE_FP, (outs),
113 (ins FPR32:$rs2, GPR:$rs1, simm12:$imm12),
114 "fsw", "$rs2, ${imm12}(${rs1})">;
116 def FMADD_S : FPFMAS_rrr_frm<OPC_MADD, "fmadd.s">;
117 def : FPFMASDynFrmAlias<FMADD_S, "fmadd.s">;
118 def FMSUB_S : FPFMAS_rrr_frm<OPC_MSUB, "fmsub.s">;
119 def : FPFMASDynFrmAlias<FMSUB_S, "fmsub.s">;
120 def FNMSUB_S : FPFMAS_rrr_frm<OPC_NMSUB, "fnmsub.s">;
121 def : FPFMASDynFrmAlias<FNMSUB_S, "fnmsub.s">;
122 def FNMADD_S : FPFMAS_rrr_frm<OPC_NMADD, "fnmadd.s">;
123 def : FPFMASDynFrmAlias<FNMADD_S, "fnmadd.s">;
125 def FADD_S : FPALUS_rr_frm<0b0000000, "fadd.s">;
126 def : FPALUSDynFrmAlias<FADD_S, "fadd.s">;
127 def FSUB_S : FPALUS_rr_frm<0b0000100, "fsub.s">;
128 def : FPALUSDynFrmAlias<FSUB_S, "fsub.s">;
129 def FMUL_S : FPALUS_rr_frm<0b0001000, "fmul.s">;
130 def : FPALUSDynFrmAlias<FMUL_S, "fmul.s">;
131 def FDIV_S : FPALUS_rr_frm<0b0001100, "fdiv.s">;
132 def : FPALUSDynFrmAlias<FDIV_S, "fdiv.s">;
134 def FSQRT_S : FPUnaryOp_r_frm<0b0101100, FPR32, FPR32, "fsqrt.s"> {
137 def : FPUnaryOpDynFrmAlias<FSQRT_S, "fsqrt.s", FPR32, FPR32>;
139 def FSGNJ_S : FPALUS_rr<0b0010000, 0b000, "fsgnj.s">;
140 def FSGNJN_S : FPALUS_rr<0b0010000, 0b001, "fsgnjn.s">;
141 def FSGNJX_S : FPALUS_rr<0b0010000, 0b010, "fsgnjx.s">;
142 def FMIN_S : FPALUS_rr<0b0010100, 0b000, "fmin.s">;
143 def FMAX_S : FPALUS_rr<0b0010100, 0b001, "fmax.s">;
145 def FCVT_W_S : FPUnaryOp_r_frm<0b1100000, GPR, FPR32, "fcvt.w.s"> {
148 def : FPUnaryOpDynFrmAlias<FCVT_W_S, "fcvt.w.s", GPR, FPR32>;
150 def FCVT_WU_S : FPUnaryOp_r_frm<0b1100000, GPR, FPR32, "fcvt.wu.s"> {
153 def : FPUnaryOpDynFrmAlias<FCVT_WU_S, "fcvt.wu.s", GPR, FPR32>;
155 def FMV_X_W : FPUnaryOp_r<0b1110000, 0b000, GPR, FPR32, "fmv.x.w"> {
159 def FEQ_S : FPCmpS_rr<0b010, "feq.s">;
160 def FLT_S : FPCmpS_rr<0b001, "flt.s">;
161 def FLE_S : FPCmpS_rr<0b000, "fle.s">;
163 def FCLASS_S : FPUnaryOp_r<0b1110000, 0b001, GPR, FPR32, "fclass.s"> {
167 def FCVT_S_W : FPUnaryOp_r_frm<0b1101000, FPR32, GPR, "fcvt.s.w"> {
170 def : FPUnaryOpDynFrmAlias<FCVT_S_W, "fcvt.s.w", FPR32, GPR>;
172 def FCVT_S_WU : FPUnaryOp_r_frm<0b1101000, FPR32, GPR, "fcvt.s.wu"> {
175 def : FPUnaryOpDynFrmAlias<FCVT_S_WU, "fcvt.s.wu", FPR32, GPR>;
177 def FMV_W_X : FPUnaryOp_r<0b1111000, 0b000, FPR32, GPR, "fmv.w.x"> {
180 } // Predicates = [HasStdExtF]
182 let Predicates = [HasStdExtF, IsRV64] in {
183 def FCVT_L_S : FPUnaryOp_r_frm<0b1100000, GPR, FPR32, "fcvt.l.s"> {
186 def : FPUnaryOpDynFrmAlias<FCVT_L_S, "fcvt.l.s", GPR, FPR32>;
188 def FCVT_LU_S : FPUnaryOp_r_frm<0b1100000, GPR, FPR32, "fcvt.lu.s"> {
191 def : FPUnaryOpDynFrmAlias<FCVT_LU_S, "fcvt.lu.s", GPR, FPR32>;
193 def FCVT_S_L : FPUnaryOp_r_frm<0b1101000, FPR32, GPR, "fcvt.s.l"> {
196 def : FPUnaryOpDynFrmAlias<FCVT_S_L, "fcvt.s.l", FPR32, GPR>;
198 def FCVT_S_LU : FPUnaryOp_r_frm<0b1101000, FPR32, GPR, "fcvt.s.lu"> {
201 def : FPUnaryOpDynFrmAlias<FCVT_S_LU, "fcvt.s.lu", FPR32, GPR>;
202 } // Predicates = [HasStdExtF, IsRV64]
204 //===----------------------------------------------------------------------===//
205 // Assembler Pseudo Instructions (User-Level ISA, Version 2.2, Chapter 20)
206 //===----------------------------------------------------------------------===//
208 let Predicates = [HasStdExtF] in {
209 def : InstAlias<"flw $rd, (${rs1})", (FLW FPR32:$rd, GPR:$rs1, 0), 0>;
210 def : InstAlias<"fsw $rs2, (${rs1})", (FSW FPR32:$rs2, GPR:$rs1, 0), 0>;
212 def : InstAlias<"fmv.s $rd, $rs", (FSGNJ_S FPR32:$rd, FPR32:$rs, FPR32:$rs)>;
213 def : InstAlias<"fabs.s $rd, $rs", (FSGNJX_S FPR32:$rd, FPR32:$rs, FPR32:$rs)>;
214 def : InstAlias<"fneg.s $rd, $rs", (FSGNJN_S FPR32:$rd, FPR32:$rs, FPR32:$rs)>;
216 // fgt.s/fge.s are recognised by the GNU assembler but the canonical
217 // flt.s/fle.s forms will always be printed. Therefore, set a zero weight.
218 def : InstAlias<"fgt.s $rd, $rs, $rt",
219 (FLT_S GPR:$rd, FPR32:$rt, FPR32:$rs), 0>;
220 def : InstAlias<"fge.s $rd, $rs, $rt",
221 (FLE_S GPR:$rd, FPR32:$rt, FPR32:$rs), 0>;
223 // The following csr instructions actually alias instructions from the base ISA.
224 // However, it only makes sense to support them when the F extension is enabled.
225 // NOTE: "frcsr", "frrm", and "frflags" are more specialized version of "csrr".
226 def : InstAlias<"frcsr $rd", (CSRRS GPR:$rd, FCSR.Encoding, X0), 2>;
227 def : InstAlias<"fscsr $rd, $rs", (CSRRW GPR:$rd, FCSR.Encoding, GPR:$rs)>;
228 def : InstAlias<"fscsr $rs", (CSRRW X0, FCSR.Encoding, GPR:$rs), 2>;
230 def : InstAlias<"frrm $rd", (CSRRS GPR:$rd, FRM.Encoding, X0), 2>;
231 def : InstAlias<"fsrm $rd, $rs", (CSRRW GPR:$rd, FRM.Encoding, GPR:$rs)>;
232 def : InstAlias<"fsrm $rs", (CSRRW X0, FRM.Encoding, GPR:$rs), 2>;
233 def : InstAlias<"fsrmi $rd, $imm", (CSRRWI GPR:$rd, FRM.Encoding, uimm5:$imm)>;
234 def : InstAlias<"fsrmi $imm", (CSRRWI X0, FRM.Encoding, uimm5:$imm), 2>;
236 def : InstAlias<"frflags $rd", (CSRRS GPR:$rd, FFLAGS.Encoding, X0), 2>;
237 def : InstAlias<"fsflags $rd, $rs", (CSRRW GPR:$rd, FFLAGS.Encoding, GPR:$rs)>;
238 def : InstAlias<"fsflags $rs", (CSRRW X0, FFLAGS.Encoding, GPR:$rs), 2>;
239 def : InstAlias<"fsflagsi $rd, $imm", (CSRRWI GPR:$rd, FFLAGS.Encoding, uimm5:$imm)>;
240 def : InstAlias<"fsflagsi $imm", (CSRRWI X0, FFLAGS.Encoding, uimm5:$imm), 2>;
242 // fmv.w.x and fmv.x.w were previously known as fmv.s.x and fmv.x.s. Both
243 // spellings should be supported by standard tools.
244 def : MnemonicAlias<"fmv.s.x", "fmv.w.x">;
245 def : MnemonicAlias<"fmv.x.s", "fmv.x.w">;
247 def PseudoFLW : PseudoFloatLoad<"flw", FPR32>;
248 def PseudoFSW : PseudoStore<"fsw", FPR32>;
249 } // Predicates = [HasStdExtF]
251 //===----------------------------------------------------------------------===//
252 // Pseudo-instructions and codegen patterns
253 //===----------------------------------------------------------------------===//
255 /// Generic pattern classes
256 class PatFpr32Fpr32<SDPatternOperator OpNode, RVInstR Inst>
257 : Pat<(OpNode FPR32:$rs1, FPR32:$rs2), (Inst $rs1, $rs2)>;
259 class PatFpr32Fpr32DynFrm<SDPatternOperator OpNode, RVInstRFrm Inst>
260 : Pat<(OpNode FPR32:$rs1, FPR32:$rs2), (Inst $rs1, $rs2, 0b111)>;
262 let Predicates = [HasStdExtF] in {
264 /// Float conversion operations
266 // Moves (no conversion)
267 def : Pat<(bitconvert GPR:$rs1), (FMV_W_X GPR:$rs1)>;
268 def : Pat<(bitconvert FPR32:$rs1), (FMV_X_W FPR32:$rs1)>;
270 // [u]int32<->float conversion patterns must be gated on IsRV32 or IsRV64, so
271 // are defined later.
273 /// Float arithmetic operations
275 def : PatFpr32Fpr32DynFrm<fadd, FADD_S>;
276 def : PatFpr32Fpr32DynFrm<fsub, FSUB_S>;
277 def : PatFpr32Fpr32DynFrm<fmul, FMUL_S>;
278 def : PatFpr32Fpr32DynFrm<fdiv, FDIV_S>;
280 def : Pat<(fsqrt FPR32:$rs1), (FSQRT_S FPR32:$rs1, 0b111)>;
282 def : Pat<(fneg FPR32:$rs1), (FSGNJN_S $rs1, $rs1)>;
283 def : Pat<(fabs FPR32:$rs1), (FSGNJX_S $rs1, $rs1)>;
285 def : PatFpr32Fpr32<fcopysign, FSGNJ_S>;
286 def : Pat<(fcopysign FPR32:$rs1, (fneg FPR32:$rs2)), (FSGNJN_S $rs1, $rs2)>;
288 // fmadd: rs1 * rs2 + rs3
289 def : Pat<(fma FPR32:$rs1, FPR32:$rs2, FPR32:$rs3),
290 (FMADD_S $rs1, $rs2, $rs3, 0b111)>;
292 // fmsub: rs1 * rs2 - rs3
293 def : Pat<(fma FPR32:$rs1, FPR32:$rs2, (fneg FPR32:$rs3)),
294 (FMSUB_S FPR32:$rs1, FPR32:$rs2, FPR32:$rs3, 0b111)>;
296 // fnmsub: -rs1 * rs2 + rs3
297 def : Pat<(fma (fneg FPR32:$rs1), FPR32:$rs2, FPR32:$rs3),
298 (FNMSUB_S FPR32:$rs1, FPR32:$rs2, FPR32:$rs3, 0b111)>;
300 // fnmadd: -rs1 * rs2 - rs3
301 def : Pat<(fma (fneg FPR32:$rs1), FPR32:$rs2, (fneg FPR32:$rs3)),
302 (FNMADD_S FPR32:$rs1, FPR32:$rs2, FPR32:$rs3, 0b111)>;
304 // The RISC-V 2.2 user-level ISA spec defines fmin and fmax as returning the
305 // canonical NaN when given a signaling NaN. This doesn't match the LLVM
306 // behaviour (see https://bugs.llvm.org/show_bug.cgi?id=27363). However, the
307 // draft 2.3 ISA spec changes the definition of fmin and fmax in a way that
308 // matches LLVM's fminnum and fmaxnum
309 // <https://github.com/riscv/riscv-isa-manual/commit/cd20cee7efd9bac7c5aa127ec3b451749d2b3cce>.
310 def : PatFpr32Fpr32<fminnum, FMIN_S>;
311 def : PatFpr32Fpr32<fmaxnum, FMAX_S>;
315 def : PatFpr32Fpr32<seteq, FEQ_S>;
316 def : PatFpr32Fpr32<setoeq, FEQ_S>;
317 def : PatFpr32Fpr32<setlt, FLT_S>;
318 def : PatFpr32Fpr32<setolt, FLT_S>;
319 def : PatFpr32Fpr32<setle, FLE_S>;
320 def : PatFpr32Fpr32<setole, FLE_S>;
322 // Define pattern expansions for setcc operations which aren't directly
323 // handled by a RISC-V instruction and aren't expanded in the SelectionDAG
326 def : Pat<(seto FPR32:$rs1, FPR32:$rs2),
327 (AND (FEQ_S FPR32:$rs1, FPR32:$rs1),
328 (FEQ_S FPR32:$rs2, FPR32:$rs2))>;
330 def : Pat<(setuo FPR32:$rs1, FPR32:$rs2),
331 (SLTIU (AND (FEQ_S FPR32:$rs1, FPR32:$rs1),
332 (FEQ_S FPR32:$rs2, FPR32:$rs2)),
335 def Select_FPR32_Using_CC_GPR : SelectCC_rrirr<FPR32, GPR>;
339 defm : LdPat<load, FLW>;
343 defm : StPat<store, FSW, FPR32>;
345 } // Predicates = [HasStdExtF]
347 let Predicates = [HasStdExtF, IsRV32] in {
348 // float->[u]int. Round-to-zero must be used.
349 def : Pat<(fp_to_sint FPR32:$rs1), (FCVT_W_S $rs1, 0b001)>;
350 def : Pat<(fp_to_uint FPR32:$rs1), (FCVT_WU_S $rs1, 0b001)>;
352 // [u]int->float. Match GCC and default to using dynamic rounding mode.
353 def : Pat<(sint_to_fp GPR:$rs1), (FCVT_S_W $rs1, 0b111)>;
354 def : Pat<(uint_to_fp GPR:$rs1), (FCVT_S_WU $rs1, 0b111)>;
355 } // Predicates = [HasStdExtF, IsRV32]
357 let Predicates = [HasStdExtF, IsRV32] in {
358 // FP->[u]int. Round-to-zero must be used
359 def : Pat<(fp_to_sint FPR32:$rs1), (FCVT_W_S $rs1, 0b001)>;
360 def : Pat<(fp_to_uint FPR32:$rs1), (FCVT_WU_S $rs1, 0b001)>;
362 // [u]int->fp. Match GCC and default to using dynamic rounding mode.
363 def : Pat<(sint_to_fp GPR:$rs1), (FCVT_S_W $rs1, 0b111)>;
364 def : Pat<(uint_to_fp GPR:$rs1), (FCVT_S_WU $rs1, 0b111)>;
365 } // Predicates = [HasStdExtF, IsRV32]
367 let Predicates = [HasStdExtF, IsRV64] in {
368 def : Pat<(riscv_fmv_w_x_rv64 GPR:$src), (FMV_W_X GPR:$src)>;
369 def : Pat<(riscv_fmv_x_anyextw_rv64 FPR32:$src), (FMV_X_W FPR32:$src)>;
370 def : Pat<(sexti32 (riscv_fmv_x_anyextw_rv64 FPR32:$src)),
371 (FMV_X_W FPR32:$src)>;
373 // FP->[u]int32 is mostly handled by the FP->[u]int64 patterns. This is safe
374 // because fpto[u|s]i produces poison if the value can't fit into the target.
375 // We match the single case below because fcvt.wu.s sign-extends its result so
376 // is cheaper than fcvt.lu.s+sext.w.
377 def : Pat<(sext_inreg (assertzexti32 (fp_to_uint FPR32:$rs1)), i32),
378 (FCVT_WU_S $rs1, 0b001)>;
381 def : Pat<(fp_to_sint FPR32:$rs1), (FCVT_L_S $rs1, 0b001)>;
382 def : Pat<(fp_to_uint FPR32:$rs1), (FCVT_LU_S $rs1, 0b001)>;
384 // [u]int->fp. Match GCC and default to using dynamic rounding mode.
385 def : Pat<(sint_to_fp (sext_inreg GPR:$rs1, i32)), (FCVT_S_W $rs1, 0b111)>;
386 def : Pat<(uint_to_fp (zexti32 GPR:$rs1)), (FCVT_S_WU $rs1, 0b111)>;
387 def : Pat<(sint_to_fp GPR:$rs1), (FCVT_S_L $rs1, 0b111)>;
388 def : Pat<(uint_to_fp GPR:$rs1), (FCVT_S_LU $rs1, 0b111)>;
389 } // Predicates = [HasStdExtF, IsRV64]