xref: /freebsd/contrib/llvm-project/llvm/lib/Target/RISCV/RISCVInstrInfoZfh.td (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
1//===-- RISCVInstrInfoZfh.td - RISC-V 'Zfh' 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 'Zfh'
10// half-precision floating-point extension, version 1.0.
11//
12//===----------------------------------------------------------------------===//
13
14//===----------------------------------------------------------------------===//
15// RISC-V specific DAG Nodes.
16//===----------------------------------------------------------------------===//
17
18def SDT_RISCVFMV_H_X
19    : SDTypeProfile<1, 1, [SDTCisFP<0>, SDTCisVT<1, XLenVT>]>;
20def SDT_RISCVFMV_X_EXTH
21    : SDTypeProfile<1, 1, [SDTCisVT<0, XLenVT>, SDTCisFP<1>]>;
22
23def riscv_fmv_h_x
24    : SDNode<"RISCVISD::FMV_H_X", SDT_RISCVFMV_H_X>;
25def riscv_fmv_x_anyexth
26    : SDNode<"RISCVISD::FMV_X_ANYEXTH", SDT_RISCVFMV_X_EXTH>;
27def riscv_fmv_x_signexth
28    : SDNode<"RISCVISD::FMV_X_SIGNEXTH", SDT_RISCVFMV_X_EXTH>;
29
30//===----------------------------------------------------------------------===//
31// Operand and SDNode transformation definitions.
32//===----------------------------------------------------------------------===//
33
34// Zhinxmin and Zhinx
35
36def FPR16INX : RegisterOperand<GPRF16> {
37  let ParserMatchClass = GPRAsFPR;
38  let DecoderMethod = "DecodeGPRRegisterClass";
39}
40
41def ZfhExt     : ExtInfo<"", "", [HasStdExtZfh],
42                         f16, FPR16, FPR32, ?, FPR16>;
43def ZfhminExt  : ExtInfo<"", "", [HasStdExtZfhOrZfhmin],
44                         f16, FPR16, FPR32, ?, FPR16>;
45def ZfhDExt    : ExtInfo<"", "", [HasStdExtZfh, HasStdExtD],
46                         ?, ?, FPR32, FPR64, FPR16>;
47def ZfhminDExt : ExtInfo<"", "", [HasStdExtZfhOrZfhmin, HasStdExtD],
48                         ?, ?, FPR32, FPR64, FPR16>;
49
50def ZhinxExt            : ExtInfo<"_INX", "RVZfinx",
51                                  [HasStdExtZhinx],
52                                  f16, FPR16INX, FPR32INX, ?, FPR16INX>;
53def ZhinxminExt         : ExtInfo<"_INX", "RVZfinx",
54                                  [HasStdExtZhinxOrZhinxmin],
55                                  f16, FPR16INX, FPR32INX, ?, FPR16INX>;
56def ZhinxZdinxExt       : ExtInfo<"_INX", "RVZfinx",
57                                  [HasStdExtZhinx, HasStdExtZdinx, IsRV64],
58                                  ?, ?, FPR32INX, FPR64INX, FPR16INX>;
59def ZhinxminZdinxExt    : ExtInfo<"_INX", "RVZfinx",
60                                  [HasStdExtZhinxOrZhinxmin, HasStdExtZdinx, IsRV64],
61                                  ?, ?, FPR32INX, FPR64INX, FPR16INX>;
62def ZhinxZdinx32Ext     : ExtInfo<"_IN32X", "RV32Zdinx",
63                                  [HasStdExtZhinx, HasStdExtZdinx, IsRV32],
64                                  ?, ?, FPR32INX, FPR64IN32X, FPR16INX >;
65def ZhinxminZdinx32Ext  : ExtInfo<"_IN32X", "RV32Zdinx",
66                                  [HasStdExtZhinxOrZhinxmin, HasStdExtZdinx, IsRV32],
67                                  ?, ?, FPR32INX, FPR64IN32X, FPR16INX>;
68
69defvar ZfhExts = [ZfhExt, ZhinxExt];
70defvar ZfhminExts = [ZfhminExt, ZhinxminExt];
71defvar ZfhDExts = [ZfhDExt, ZhinxZdinxExt, ZhinxZdinx32Ext];
72defvar ZfhminDExts = [ZfhminDExt, ZhinxminZdinxExt, ZhinxminZdinx32Ext];
73
74//===----------------------------------------------------------------------===//
75// Instructions
76//===----------------------------------------------------------------------===//
77
78let Predicates = [HasHalfFPLoadStoreMove] in {
79def FLH : FPLoad_r<0b001, "flh", FPR16, WriteFLD16>;
80
81// Operands for stores are in the order srcreg, base, offset rather than
82// reflecting the order these fields are specified in the instruction
83// encoding.
84def FSH : FPStore_r<0b001, "fsh", FPR16, WriteFST16>;
85} // Predicates = [HasHalfFPLoadStoreMove]
86
87foreach Ext = ZfhExts in {
88  let SchedRW = [WriteFMA16, ReadFMA16, ReadFMA16, ReadFMA16] in {
89    defm FMADD_H  : FPFMA_rrr_frm_m<OPC_MADD,  0b10, "fmadd.h",  Ext>;
90    defm FMSUB_H  : FPFMA_rrr_frm_m<OPC_MSUB,  0b10, "fmsub.h",  Ext>;
91    defm FNMSUB_H : FPFMA_rrr_frm_m<OPC_NMSUB, 0b10, "fnmsub.h", Ext>;
92    defm FNMADD_H : FPFMA_rrr_frm_m<OPC_NMADD, 0b10, "fnmadd.h", Ext>;
93  }
94
95  let SchedRW = [WriteFAdd16, ReadFAdd16, ReadFAdd16] in {
96    defm FADD_H : FPALU_rr_frm_m<0b0000010, "fadd.h", Ext, Commutable=1>;
97    defm FSUB_H : FPALU_rr_frm_m<0b0000110, "fsub.h", Ext>;
98  }
99  let SchedRW = [WriteFMul16, ReadFMul16, ReadFMul16] in
100  defm FMUL_H : FPALU_rr_frm_m<0b0001010, "fmul.h", Ext, Commutable=1>;
101
102  let SchedRW = [WriteFDiv16, ReadFDiv16, ReadFDiv16] in
103  defm FDIV_H : FPALU_rr_frm_m<0b0001110, "fdiv.h", Ext>;
104
105  defm FSQRT_H : FPUnaryOp_r_frm_m<0b0101110, 0b00000, Ext, Ext.PrimaryTy,
106                                   Ext.PrimaryTy, "fsqrt.h">,
107                 Sched<[WriteFSqrt16, ReadFSqrt16]>;
108
109  let SchedRW = [WriteFSGNJ16, ReadFSGNJ16, ReadFSGNJ16],
110      mayRaiseFPException = 0 in {
111    defm FSGNJ_H  : FPALU_rr_m<0b0010010, 0b000, "fsgnj.h",  Ext>;
112    defm FSGNJN_H : FPALU_rr_m<0b0010010, 0b001, "fsgnjn.h", Ext>;
113    defm FSGNJX_H : FPALU_rr_m<0b0010010, 0b010, "fsgnjx.h", Ext>;
114  }
115
116  let SchedRW = [WriteFMinMax16, ReadFMinMax16, ReadFMinMax16] in {
117    defm FMIN_H   : FPALU_rr_m<0b0010110, 0b000, "fmin.h", Ext, Commutable=1>;
118    defm FMAX_H   : FPALU_rr_m<0b0010110, 0b001, "fmax.h", Ext, Commutable=1>;
119  }
120
121  let IsSignExtendingOpW = 1 in
122  defm FCVT_W_H : FPUnaryOp_r_frm_m<0b1100010, 0b00000, Ext, GPR, Ext.PrimaryTy,
123                                    "fcvt.w.h">,
124                  Sched<[WriteFCvtF16ToI32, ReadFCvtF16ToI32]>;
125
126  let IsSignExtendingOpW = 1 in
127  defm FCVT_WU_H : FPUnaryOp_r_frm_m<0b1100010, 0b00001, Ext, GPR, Ext.PrimaryTy,
128                                     "fcvt.wu.h">,
129                   Sched<[WriteFCvtF16ToI32, ReadFCvtF16ToI32]>;
130
131  defm FCVT_H_W : FPUnaryOp_r_frm_m<0b1101010, 0b00000, Ext, Ext.PrimaryTy, GPR,
132                                    "fcvt.h.w">,
133                  Sched<[WriteFCvtI32ToF16, ReadFCvtI32ToF16]>;
134
135  defm FCVT_H_WU : FPUnaryOp_r_frm_m<0b1101010, 0b00001, Ext, Ext.PrimaryTy, GPR,
136                                     "fcvt.h.wu">,
137                   Sched<[WriteFCvtI32ToF16, ReadFCvtI32ToF16]>;
138} // foreach Ext = ZfhExts
139
140foreach Ext = ZfhminExts in {
141  defm FCVT_H_S : FPUnaryOp_r_frm_m<0b0100010, 0b00000, Ext, Ext.PrimaryTy,
142                                    Ext.F32Ty, "fcvt.h.s">,
143                  Sched<[WriteFCvtF32ToF16, ReadFCvtF32ToF16]>;
144
145  defm FCVT_S_H : FPUnaryOp_r_m<0b0100000, 0b00010, 0b000, Ext, Ext.F32Ty,
146                                Ext.PrimaryTy, "fcvt.s.h">,
147                 Sched<[WriteFCvtF16ToF32, ReadFCvtF16ToF32]>;
148} // foreach Ext = ZfhminExts
149
150let Predicates = [HasHalfFPLoadStoreMove] in {
151let mayRaiseFPException = 0, IsSignExtendingOpW = 1 in
152def FMV_X_H : FPUnaryOp_r<0b1110010, 0b00000, 0b000, GPR, FPR16, "fmv.x.h">,
153              Sched<[WriteFMovF16ToI16, ReadFMovF16ToI16]>;
154
155let mayRaiseFPException = 0 in
156def FMV_H_X : FPUnaryOp_r<0b1111010, 0b00000, 0b000, FPR16, GPR, "fmv.h.x">,
157              Sched<[WriteFMovI16ToF16, ReadFMovI16ToF16]>;
158} // Predicates = [HasHalfFPLoadStoreMove]
159
160foreach Ext = ZfhExts in {
161  let SchedRW = [WriteFCmp16, ReadFCmp16, ReadFCmp16] in {
162    defm FEQ_H : FPCmp_rr_m<0b1010010, 0b010, "feq.h", Ext, Commutable=1>;
163    defm FLT_H : FPCmp_rr_m<0b1010010, 0b001, "flt.h", Ext>;
164    defm FLE_H : FPCmp_rr_m<0b1010010, 0b000, "fle.h", Ext>;
165  }
166
167  let mayRaiseFPException = 0 in
168  defm FCLASS_H : FPUnaryOp_r_m<0b1110010, 0b00000, 0b001, Ext, GPR, Ext.PrimaryTy,
169                                "fclass.h">,
170                  Sched<[WriteFClass16, ReadFClass16]>;
171
172  defm FCVT_L_H  : FPUnaryOp_r_frm_m<0b1100010, 0b00010, Ext, GPR, Ext.PrimaryTy,
173                                     "fcvt.l.h", [IsRV64]>,
174                   Sched<[WriteFCvtF16ToI64, ReadFCvtF16ToI64]>;
175
176  defm FCVT_LU_H  : FPUnaryOp_r_frm_m<0b1100010, 0b00011, Ext, GPR, Ext.PrimaryTy,
177                                      "fcvt.lu.h", [IsRV64]>,
178                    Sched<[WriteFCvtF16ToI64, ReadFCvtF16ToI64]>;
179
180  defm FCVT_H_L : FPUnaryOp_r_frm_m<0b1101010, 0b00010, Ext, Ext.PrimaryTy, GPR,
181                                    "fcvt.h.l", [IsRV64]>,
182                  Sched<[WriteFCvtI64ToF16, ReadFCvtI64ToF16]>;
183
184  defm FCVT_H_LU : FPUnaryOp_r_frm_m<0b1101010, 0b00011, Ext, Ext.PrimaryTy, GPR,
185                                     "fcvt.h.lu", [IsRV64]>,
186                   Sched<[WriteFCvtI64ToF16, ReadFCvtI64ToF16]>;
187} // foreach Ext = ZfhExts
188
189foreach Ext = ZfhminDExts in {
190  defm FCVT_H_D : FPUnaryOp_r_frm_m<0b0100010, 0b00001, Ext, Ext.F16Ty,
191                                   Ext.F64Ty, "fcvt.h.d">,
192                  Sched<[WriteFCvtF64ToF16, ReadFCvtF64ToF16]>;
193
194  defm FCVT_D_H : FPUnaryOp_r_m<0b0100001, 0b00010, 0b000, Ext, Ext.F64Ty,
195                                Ext.F16Ty, "fcvt.d.h">,
196                  Sched<[WriteFCvtF16ToF64, ReadFCvtF16ToF64]>;
197} // foreach Ext = ZfhminDExts
198
199//===----------------------------------------------------------------------===//
200// Assembler Pseudo Instructions (User-Level ISA, Version 2.2, Chapter 20)
201//===----------------------------------------------------------------------===//
202
203let Predicates = [HasStdExtZfhOrZfhmin] in {
204def : InstAlias<"flh $rd, (${rs1})",  (FLH FPR16:$rd,  GPR:$rs1, 0), 0>;
205def : InstAlias<"fsh $rs2, (${rs1})", (FSH FPR16:$rs2, GPR:$rs1, 0), 0>;
206} // Predicates = [HasStdExtZfhOrZfhmin]
207
208let Predicates = [HasStdExtZfh] in {
209def : InstAlias<"fmv.h $rd, $rs",  (FSGNJ_H  FPR16:$rd, FPR16:$rs, FPR16:$rs)>;
210def : InstAlias<"fabs.h $rd, $rs", (FSGNJX_H FPR16:$rd, FPR16:$rs, FPR16:$rs)>;
211def : InstAlias<"fneg.h $rd, $rs", (FSGNJN_H FPR16:$rd, FPR16:$rs, FPR16:$rs)>;
212
213// fgt.h/fge.h are recognised by the GNU assembler but the canonical
214// flt.h/fle.h forms will always be printed. Therefore, set a zero weight.
215def : InstAlias<"fgt.h $rd, $rs, $rt",
216                (FLT_H GPR:$rd, FPR16:$rt, FPR16:$rs), 0>;
217def : InstAlias<"fge.h $rd, $rs, $rt",
218                (FLE_H GPR:$rd, FPR16:$rt, FPR16:$rs), 0>;
219
220let usesCustomInserter = 1 in {
221def PseudoQuietFLE_H : PseudoQuietFCMP<FPR16>;
222def PseudoQuietFLT_H : PseudoQuietFCMP<FPR16>;
223}
224} // Predicates = [HasStdExtZfh]
225
226let Predicates = [HasStdExtZfhOrZfhmin] in {
227def PseudoFLH  : PseudoFloatLoad<"flh", FPR16>;
228def PseudoFSH  : PseudoStore<"fsh", FPR16>;
229} // Predicates = [HasStdExtZfhOrZfhmin]
230
231let Predicates = [HasStdExtZhinx] in {
232def : InstAlias<"fmv.h $rd, $rs",  (FSGNJ_H_INX  FPR16INX:$rd, FPR16INX:$rs, FPR16INX:$rs)>;
233def : InstAlias<"fabs.h $rd, $rs", (FSGNJX_H_INX FPR16INX:$rd, FPR16INX:$rs, FPR16INX:$rs)>;
234def : InstAlias<"fneg.h $rd, $rs", (FSGNJN_H_INX FPR16INX:$rd, FPR16INX:$rs, FPR16INX:$rs)>;
235
236def : InstAlias<"fgt.h $rd, $rs, $rt",
237                (FLT_H_INX GPR:$rd, FPR16INX:$rt, FPR16INX:$rs), 0>;
238def : InstAlias<"fge.h $rd, $rs, $rt",
239                (FLE_H_INX GPR:$rd, FPR16INX:$rt, FPR16INX:$rs), 0>;
240
241let usesCustomInserter = 1 in {
242def PseudoQuietFLE_H_INX : PseudoQuietFCMP<FPR16INX>;
243def PseudoQuietFLT_H_INX : PseudoQuietFCMP<FPR16INX>;
244}
245} // Predicates = [HasStdExtZhinxOrZhinxmin]
246
247//===----------------------------------------------------------------------===//
248// Pseudo-instructions and codegen patterns
249//===----------------------------------------------------------------------===//
250
251let Predicates = [HasStdExtZfh] in {
252
253/// Float conversion operations
254
255// [u]int32<->float conversion patterns must be gated on IsRV32 or IsRV64, so
256// are defined later.
257
258/// Float arithmetic operations
259
260def : PatFprFprDynFrm<any_fadd, FADD_H, FPR16, f16>;
261def : PatFprFprDynFrm<any_fsub, FSUB_H, FPR16, f16>;
262def : PatFprFprDynFrm<any_fmul, FMUL_H, FPR16, f16>;
263def : PatFprFprDynFrm<any_fdiv, FDIV_H, FPR16, f16>;
264
265def : Pat<(f16 (any_fsqrt FPR16:$rs1)), (FSQRT_H FPR16:$rs1, FRM_DYN)>;
266
267def : Pat<(f16 (fneg FPR16:$rs1)), (FSGNJN_H $rs1, $rs1)>;
268def : Pat<(f16 (fabs FPR16:$rs1)), (FSGNJX_H $rs1, $rs1)>;
269
270def : Pat<(riscv_fpclass (f16 FPR16:$rs1)), (FCLASS_H $rs1)>;
271
272def : PatFprFpr<fcopysign, FSGNJ_H, FPR16, f16>;
273def : Pat<(f16 (fcopysign FPR16:$rs1, (f16 (fneg FPR16:$rs2)))), (FSGNJN_H $rs1, $rs2)>;
274def : Pat<(f16 (fcopysign FPR16:$rs1, FPR32:$rs2)),
275          (FSGNJ_H $rs1, (FCVT_H_S $rs2, FRM_DYN))>;
276
277// fmadd: rs1 * rs2 + rs3
278def : Pat<(f16 (any_fma FPR16:$rs1, FPR16:$rs2, FPR16:$rs3)),
279          (FMADD_H $rs1, $rs2, $rs3, FRM_DYN)>;
280
281// fmsub: rs1 * rs2 - rs3
282def : Pat<(f16 (any_fma FPR16:$rs1, FPR16:$rs2, (fneg FPR16:$rs3))),
283          (FMSUB_H FPR16:$rs1, FPR16:$rs2, FPR16:$rs3, FRM_DYN)>;
284
285// fnmsub: -rs1 * rs2 + rs3
286def : Pat<(f16 (any_fma (fneg FPR16:$rs1), FPR16:$rs2, FPR16:$rs3)),
287          (FNMSUB_H FPR16:$rs1, FPR16:$rs2, FPR16:$rs3, FRM_DYN)>;
288
289// fnmadd: -rs1 * rs2 - rs3
290def : Pat<(f16 (any_fma (fneg FPR16:$rs1), FPR16:$rs2, (fneg FPR16:$rs3))),
291          (FNMADD_H FPR16:$rs1, FPR16:$rs2, FPR16:$rs3, FRM_DYN)>;
292
293// fnmadd: -(rs1 * rs2 + rs3) (the nsz flag on the FMA)
294def : Pat<(f16 (fneg (any_fma_nsz FPR16:$rs1, FPR16:$rs2, FPR16:$rs3))),
295          (FNMADD_H FPR16:$rs1, FPR16:$rs2, FPR16:$rs3, FRM_DYN)>;
296} // Predicates = [HasStdExtZfh]
297
298let Predicates = [HasStdExtZhinx] in {
299
300/// Float conversion operations
301
302// [u]int32<->float conversion patterns must be gated on IsRV32 or IsRV64, so
303// are defined later.
304
305/// Float arithmetic operations
306
307def : PatFprFprDynFrm<any_fadd, FADD_H_INX, FPR16INX, f16>;
308def : PatFprFprDynFrm<any_fsub, FSUB_H_INX, FPR16INX, f16>;
309def : PatFprFprDynFrm<any_fmul, FMUL_H_INX, FPR16INX, f16>;
310def : PatFprFprDynFrm<any_fdiv, FDIV_H_INX, FPR16INX, f16>;
311
312def : Pat<(any_fsqrt FPR16INX:$rs1), (FSQRT_H_INX FPR16INX:$rs1, FRM_DYN)>;
313
314def : Pat<(fneg FPR16INX:$rs1), (FSGNJN_H_INX $rs1, $rs1)>;
315def : Pat<(fabs FPR16INX:$rs1), (FSGNJX_H_INX $rs1, $rs1)>;
316
317def : Pat<(riscv_fpclass FPR16INX:$rs1), (FCLASS_H_INX $rs1)>;
318
319def : PatFprFpr<fcopysign, FSGNJ_H_INX, FPR16INX, f16>;
320def : Pat<(fcopysign FPR16INX:$rs1, (fneg FPR16INX:$rs2)), (FSGNJN_H_INX $rs1, $rs2)>;
321def : Pat<(fcopysign FPR16INX:$rs1, FPR32INX:$rs2),
322          (FSGNJ_H_INX $rs1, (FCVT_H_S_INX $rs2, FRM_DYN))>;
323
324// fmadd: rs1 * rs2 + rs3
325def : Pat<(any_fma FPR16INX:$rs1, FPR16INX:$rs2, FPR16INX:$rs3),
326          (FMADD_H_INX $rs1, $rs2, $rs3, FRM_DYN)>;
327
328// fmsub: rs1 * rs2 - rs3
329def : Pat<(any_fma FPR16INX:$rs1, FPR16INX:$rs2, (fneg FPR16INX:$rs3)),
330          (FMSUB_H_INX FPR16INX:$rs1, FPR16INX:$rs2, FPR16INX:$rs3, FRM_DYN)>;
331
332// fnmsub: -rs1 * rs2 + rs3
333def : Pat<(any_fma (fneg FPR16INX:$rs1), FPR16INX:$rs2, FPR16INX:$rs3),
334          (FNMSUB_H_INX FPR16INX:$rs1, FPR16INX:$rs2, FPR16INX:$rs3, FRM_DYN)>;
335
336// fnmadd: -rs1 * rs2 - rs3
337def : Pat<(any_fma (fneg FPR16INX:$rs1), FPR16INX:$rs2, (fneg FPR16INX:$rs3)),
338          (FNMADD_H_INX FPR16INX:$rs1, FPR16INX:$rs2, FPR16INX:$rs3, FRM_DYN)>;
339
340// fnmadd: -(rs1 * rs2 + rs3) (the nsz flag on the FMA)
341def : Pat<(fneg (any_fma_nsz FPR16INX:$rs1, FPR16INX:$rs2, FPR16INX:$rs3)),
342          (FNMADD_H_INX FPR16INX:$rs1, FPR16INX:$rs2, FPR16INX:$rs3, FRM_DYN)>;
343} // Predicates = [HasStdExtZhinx]
344
345// The ratified 20191213 ISA spec defines fmin and fmax in a way that matches
346// LLVM's fminnum and fmaxnum
347// <https://github.com/riscv/riscv-isa-manual/commit/cd20cee7efd9bac7c5aa127ec3b451749d2b3cce>.
348foreach Ext = ZfhExts in {
349  defm : PatFprFpr_m<fminnum, FMIN_H, Ext>;
350  defm : PatFprFpr_m<fmaxnum, FMAX_H, Ext>;
351  defm : PatFprFpr_m<riscv_fmin, FMIN_H, Ext>;
352  defm : PatFprFpr_m<riscv_fmax, FMAX_H, Ext>;
353}
354
355/// Setcc
356// FIXME: SETEQ/SETLT/SETLE imply nonans, can we pick better instructions for
357// strict versions of those.
358
359// Match non-signaling FEQ_D
360foreach Ext = ZfhExts in {
361  defm : PatSetCC_m<any_fsetcc,    SETEQ,  FEQ_H,            Ext, f16>;
362  defm : PatSetCC_m<any_fsetcc,    SETOEQ, FEQ_H,            Ext, f16>;
363  defm : PatSetCC_m<strict_fsetcc, SETLT,  PseudoQuietFLT_H, Ext, f16>;
364  defm : PatSetCC_m<strict_fsetcc, SETOLT, PseudoQuietFLT_H, Ext, f16>;
365  defm : PatSetCC_m<strict_fsetcc, SETLE,  PseudoQuietFLE_H, Ext, f16>;
366  defm : PatSetCC_m<strict_fsetcc, SETOLE, PseudoQuietFLE_H, Ext, f16>;
367}
368
369let Predicates = [HasStdExtZfh] in {
370// Match signaling FEQ_H
371def : Pat<(XLenVT (strict_fsetccs (f16 FPR16:$rs1), FPR16:$rs2, SETEQ)),
372          (AND (FLE_H $rs1, $rs2),
373               (FLE_H $rs2, $rs1))>;
374def : Pat<(XLenVT (strict_fsetccs (f16 FPR16:$rs1), FPR16:$rs2, SETOEQ)),
375          (AND (FLE_H $rs1, $rs2),
376               (FLE_H $rs2, $rs1))>;
377// If both operands are the same, use a single FLE.
378def : Pat<(XLenVT (strict_fsetccs (f16 FPR16:$rs1), (f16 FPR16:$rs1), SETEQ)),
379          (FLE_H $rs1, $rs1)>;
380def : Pat<(XLenVT (strict_fsetccs (f16 FPR16:$rs1), (f16 FPR16:$rs1), SETOEQ)),
381          (FLE_H $rs1, $rs1)>;
382} // Predicates = [HasStdExtZfh]
383
384let Predicates = [HasStdExtZhinx] in {
385// Match signaling FEQ_H
386def : Pat<(XLenVT (strict_fsetccs FPR16INX:$rs1, FPR16INX:$rs2, SETEQ)),
387          (AND (FLE_H_INX $rs1, $rs2),
388               (FLE_H_INX $rs2, $rs1))>;
389def : Pat<(XLenVT (strict_fsetccs FPR16INX:$rs1, FPR16INX:$rs2, SETOEQ)),
390          (AND (FLE_H_INX $rs1, $rs2),
391               (FLE_H_INX $rs2, $rs1))>;
392// If both operands are the same, use a single FLE.
393def : Pat<(XLenVT (strict_fsetccs FPR16INX:$rs1, FPR16INX:$rs1, SETEQ)),
394          (FLE_H_INX $rs1, $rs1)>;
395def : Pat<(XLenVT (strict_fsetccs FPR16INX:$rs1, FPR16INX:$rs1, SETOEQ)),
396          (FLE_H_INX $rs1, $rs1)>;
397} // Predicates = [HasStdExtZhinx]
398
399foreach Ext = ZfhExts in {
400  defm : PatSetCC_m<any_fsetccs, SETLT,  FLT_H, Ext, f16>;
401  defm : PatSetCC_m<any_fsetccs, SETOLT, FLT_H, Ext, f16>;
402  defm : PatSetCC_m<any_fsetccs, SETLE,  FLE_H, Ext, f16>;
403  defm : PatSetCC_m<any_fsetccs, SETOLE, FLE_H, Ext, f16>;
404}
405
406let Predicates = [HasStdExtZfh] in {
407defm Select_FPR16 : SelectCC_GPR_rrirr<FPR16, f16>;
408
409def PseudoFROUND_H : PseudoFROUND<FPR16, f16>;
410} // Predicates = [HasStdExtZfh]
411
412let Predicates = [HasStdExtZhinx] in {
413defm Select_FPR16INX : SelectCC_GPR_rrirr<FPR16INX, f16>;
414
415def PseudoFROUND_H_INX : PseudoFROUND<FPR16INX, f16>;
416} // Predicates = [HasStdExtZhinx]
417
418let Predicates = [HasStdExtZfhOrZfhmin] in {
419/// Loads
420def : LdPat<load, FLH, f16>;
421
422/// Stores
423def : StPat<store, FSH, FPR16, f16>;
424} // Predicates = [HasStdExtZfhOrZfhmin]
425
426let Predicates = [HasStdExtZhinxOrZhinxmin] in {
427/// Loads
428def : Pat<(f16 (load GPR:$rs1)), (COPY_TO_REGCLASS (LH GPR:$rs1, 0), GPRF16)>;
429
430/// Stores
431def : Pat<(store (f16 FPR16INX:$rs2), GPR:$rs1),
432          (SH (COPY_TO_REGCLASS FPR16INX:$rs2, GPR), GPR:$rs1, 0)>;
433} // Predicates = [HasStdExtZhinxOrZhinxmin]
434
435let Predicates = [HasStdExtZfhOrZfhmin] in {
436/// Float conversion operations
437
438// f32 -> f16, f16 -> f32
439def : Pat<(f16 (any_fpround FPR32:$rs1)), (FCVT_H_S FPR32:$rs1, FRM_DYN)>;
440def : Pat<(any_fpextend (f16 FPR16:$rs1)), (FCVT_S_H FPR16:$rs1)>;
441
442// Moves (no conversion)
443def : Pat<(f16 (riscv_fmv_h_x GPR:$src)), (FMV_H_X GPR:$src)>;
444def : Pat<(riscv_fmv_x_anyexth (f16 FPR16:$src)), (FMV_X_H FPR16:$src)>;
445def : Pat<(riscv_fmv_x_signexth (f16 FPR16:$src)), (FMV_X_H FPR16:$src)>;
446
447def : Pat<(fcopysign FPR32:$rs1, (f16 FPR16:$rs2)), (FSGNJ_S $rs1, (FCVT_S_H $rs2))>;
448} // Predicates = [HasStdExtZfhOrZfhmin]
449
450let Predicates = [HasStdExtZhinxOrZhinxmin] in {
451/// Float conversion operations
452
453// f32 -> f16, f16 -> f32
454def : Pat<(any_fpround FPR32INX:$rs1), (FCVT_H_S_INX FPR32INX:$rs1, FRM_DYN)>;
455def : Pat<(any_fpextend FPR16INX:$rs1), (FCVT_S_H_INX FPR16INX:$rs1)>;
456
457// Moves (no conversion)
458def : Pat<(f16 (riscv_fmv_h_x GPR:$src)), (COPY_TO_REGCLASS GPR:$src, GPR)>;
459def : Pat<(riscv_fmv_x_anyexth FPR16INX:$src), (COPY_TO_REGCLASS FPR16INX:$src, GPR)>;
460def : Pat<(riscv_fmv_x_signexth FPR16INX:$src), (COPY_TO_REGCLASS FPR16INX:$src, GPR)>;
461
462def : Pat<(fcopysign FPR32INX:$rs1, FPR16INX:$rs2), (FSGNJ_S_INX $rs1, (FCVT_S_H_INX $rs2))>;
463} // Predicates = [HasStdExtZhinxOrZhinxmin]
464
465let Predicates = [HasStdExtZfh, IsRV32] in {
466// half->[u]int. Round-to-zero must be used.
467def : Pat<(i32 (any_fp_to_sint (f16 FPR16:$rs1))), (FCVT_W_H $rs1, 0b001)>;
468def : Pat<(i32 (any_fp_to_uint (f16 FPR16:$rs1))), (FCVT_WU_H $rs1, 0b001)>;
469
470// Saturating half->[u]int32.
471def : Pat<(i32 (riscv_fcvt_x (f16 FPR16:$rs1), timm:$frm)), (FCVT_W_H $rs1, timm:$frm)>;
472def : Pat<(i32 (riscv_fcvt_xu (f16 FPR16:$rs1), timm:$frm)), (FCVT_WU_H $rs1, timm:$frm)>;
473
474// half->int32 with current rounding mode.
475def : Pat<(i32 (any_lrint (f16 FPR16:$rs1))), (FCVT_W_H $rs1, FRM_DYN)>;
476
477// half->int32 rounded to nearest with ties rounded away from zero.
478def : Pat<(i32 (any_lround (f16 FPR16:$rs1))), (FCVT_W_H $rs1, FRM_RMM)>;
479
480// [u]int->half. Match GCC and default to using dynamic rounding mode.
481def : Pat<(f16 (any_sint_to_fp (i32 GPR:$rs1))), (FCVT_H_W $rs1, FRM_DYN)>;
482def : Pat<(f16 (any_uint_to_fp (i32 GPR:$rs1))), (FCVT_H_WU $rs1, FRM_DYN)>;
483} // Predicates = [HasStdExtZfh, IsRV32]
484
485let Predicates = [HasStdExtZhinx, IsRV32] in {
486// half->[u]int. Round-to-zero must be used.
487def : Pat<(i32 (any_fp_to_sint FPR16INX:$rs1)), (FCVT_W_H_INX $rs1, 0b001)>;
488def : Pat<(i32 (any_fp_to_uint FPR16INX:$rs1)), (FCVT_WU_H_INX $rs1, 0b001)>;
489
490// Saturating float->[u]int32.
491def : Pat<(i32 (riscv_fcvt_x FPR16INX:$rs1, timm:$frm)), (FCVT_W_H_INX $rs1, timm:$frm)>;
492def : Pat<(i32 (riscv_fcvt_xu FPR16INX:$rs1, timm:$frm)), (FCVT_WU_H_INX $rs1, timm:$frm)>;
493
494// half->int32 with current rounding mode.
495def : Pat<(i32 (any_lrint FPR16INX:$rs1)), (FCVT_W_H_INX $rs1, FRM_DYN)>;
496
497// half->int32 rounded to nearest with ties rounded away from zero.
498def : Pat<(i32 (any_lround FPR16INX:$rs1)), (FCVT_W_H_INX $rs1, FRM_RMM)>;
499
500// [u]int->half. Match GCC and default to using dynamic rounding mode.
501def : Pat<(any_sint_to_fp (i32 GPR:$rs1)), (FCVT_H_W_INX $rs1, FRM_DYN)>;
502def : Pat<(any_uint_to_fp (i32 GPR:$rs1)), (FCVT_H_WU_INX $rs1, FRM_DYN)>;
503} // Predicates = [HasStdExtZhinx, IsRV32]
504
505let Predicates = [HasStdExtZfh, IsRV64] in {
506// Use target specific isd nodes to help us remember the result is sign
507// extended. Matching sext_inreg+fptoui/fptosi may cause the conversion to be
508// duplicated if it has another user that didn't need the sign_extend.
509def : Pat<(riscv_any_fcvt_w_rv64 (f16 FPR16:$rs1), timm:$frm),  (FCVT_W_H $rs1, timm:$frm)>;
510def : Pat<(riscv_any_fcvt_wu_rv64 (f16 FPR16:$rs1), timm:$frm), (FCVT_WU_H $rs1, timm:$frm)>;
511
512// half->[u]int64. Round-to-zero must be used.
513def : Pat<(i64 (any_fp_to_sint (f16 FPR16:$rs1))), (FCVT_L_H $rs1, 0b001)>;
514def : Pat<(i64 (any_fp_to_uint (f16 FPR16:$rs1))), (FCVT_LU_H $rs1, 0b001)>;
515
516// Saturating half->[u]int64.
517def : Pat<(i64 (riscv_fcvt_x (f16 FPR16:$rs1), timm:$frm)), (FCVT_L_H $rs1, timm:$frm)>;
518def : Pat<(i64 (riscv_fcvt_xu (f16 FPR16:$rs1), timm:$frm)), (FCVT_LU_H $rs1, timm:$frm)>;
519
520// half->int64 with current rounding mode.
521def : Pat<(i64 (any_lrint (f16 FPR16:$rs1))), (FCVT_L_H $rs1, FRM_DYN)>;
522def : Pat<(i64 (any_llrint (f16 FPR16:$rs1))), (FCVT_L_H $rs1, FRM_DYN)>;
523
524// half->int64 rounded to nearest with ties rounded away from zero.
525def : Pat<(i64 (any_lround (f16 FPR16:$rs1))), (FCVT_L_H $rs1, FRM_RMM)>;
526def : Pat<(i64 (any_llround (f16 FPR16:$rs1))), (FCVT_L_H $rs1, FRM_RMM)>;
527
528// [u]int->fp. Match GCC and default to using dynamic rounding mode.
529def : Pat<(f16 (any_sint_to_fp (i64 (sexti32 (i64 GPR:$rs1))))), (FCVT_H_W $rs1, FRM_DYN)>;
530def : Pat<(f16 (any_uint_to_fp (i64 (zexti32 (i64 GPR:$rs1))))), (FCVT_H_WU $rs1, FRM_DYN)>;
531def : Pat<(f16 (any_sint_to_fp (i64 GPR:$rs1))), (FCVT_H_L $rs1, FRM_DYN)>;
532def : Pat<(f16 (any_uint_to_fp (i64 GPR:$rs1))), (FCVT_H_LU $rs1, FRM_DYN)>;
533} // Predicates = [HasStdExtZfh, IsRV64]
534
535let Predicates = [HasStdExtZhinx, IsRV64] in {
536// Use target specific isd nodes to help us remember the result is sign
537// extended. Matching sext_inreg+fptoui/fptosi may cause the conversion to be
538// duplicated if it has another user that didn't need the sign_extend.
539def : Pat<(riscv_any_fcvt_w_rv64 FPR16INX:$rs1, timm:$frm),  (FCVT_W_H_INX $rs1, timm:$frm)>;
540def : Pat<(riscv_any_fcvt_wu_rv64 FPR16INX:$rs1, timm:$frm), (FCVT_WU_H_INX $rs1, timm:$frm)>;
541
542// half->[u]int64. Round-to-zero must be used.
543def : Pat<(i64 (any_fp_to_sint FPR16INX:$rs1)), (FCVT_L_H_INX $rs1, 0b001)>;
544def : Pat<(i64 (any_fp_to_uint FPR16INX:$rs1)), (FCVT_LU_H_INX $rs1, 0b001)>;
545
546// Saturating float->[u]int64.
547def : Pat<(i64 (riscv_fcvt_x FPR16INX:$rs1, timm:$frm)), (FCVT_L_H_INX $rs1, timm:$frm)>;
548def : Pat<(i64 (riscv_fcvt_xu FPR16INX:$rs1, timm:$frm)), (FCVT_LU_H_INX $rs1, timm:$frm)>;
549
550// half->int64 with current rounding mode.
551def : Pat<(i64 (any_lrint FPR16INX:$rs1)), (FCVT_L_H_INX $rs1, FRM_DYN)>;
552def : Pat<(i64 (any_llrint FPR16INX:$rs1)), (FCVT_L_H_INX $rs1, FRM_DYN)>;
553
554// half->int64 rounded to nearest with ties rounded away from zero.
555def : Pat<(i64 (any_lround FPR16INX:$rs1)), (FCVT_L_H_INX $rs1, FRM_RMM)>;
556def : Pat<(i64 (any_llround FPR16INX:$rs1)), (FCVT_L_H_INX $rs1, FRM_RMM)>;
557
558// [u]int->fp. Match GCC and default to using dynamic rounding mode.
559def : Pat<(any_sint_to_fp (i64 (sexti32 (i64 GPR:$rs1)))), (FCVT_H_W_INX $rs1, FRM_DYN)>;
560def : Pat<(any_uint_to_fp (i64 (zexti32 (i64 GPR:$rs1)))), (FCVT_H_WU_INX $rs1, FRM_DYN)>;
561def : Pat<(any_sint_to_fp (i64 GPR:$rs1)), (FCVT_H_L_INX $rs1, FRM_DYN)>;
562def : Pat<(any_uint_to_fp (i64 GPR:$rs1)), (FCVT_H_LU_INX $rs1, FRM_DYN)>;
563} // Predicates = [HasStdExtZhinx, IsRV64]
564
565let Predicates = [HasStdExtZfhOrZfhmin, HasStdExtD] in {
566/// Float conversion operations
567// f64 -> f16, f16 -> f64
568def : Pat<(f16 (any_fpround FPR64:$rs1)), (FCVT_H_D FPR64:$rs1, FRM_DYN)>;
569def : Pat<(any_fpextend (f16 FPR16:$rs1)), (FCVT_D_H FPR16:$rs1)>;
570
571/// Float arithmetic operations
572def : Pat<(f16 (fcopysign FPR16:$rs1, FPR64:$rs2)),
573          (FSGNJ_H $rs1, (FCVT_H_D $rs2, FRM_DYN))>;
574def : Pat<(fcopysign FPR64:$rs1, (f16 FPR16:$rs2)), (FSGNJ_D $rs1, (FCVT_D_H $rs2))>;
575} // Predicates = [HasStdExtZfhOrZfhmin, HasStdExtD]
576
577let Predicates = [HasStdExtZhinxOrZhinxmin, HasStdExtZdinx, IsRV32] in {
578/// Float conversion operations
579// f64 -> f16, f16 -> f64
580def : Pat<(any_fpround FPR64IN32X:$rs1), (FCVT_H_D_IN32X FPR64IN32X:$rs1, FRM_DYN)>;
581def : Pat<(any_fpextend FPR16INX:$rs1), (FCVT_D_H_IN32X FPR16INX:$rs1)>;
582
583/// Float arithmetic operations
584def : Pat<(fcopysign FPR16INX:$rs1, FPR64IN32X:$rs2),
585          (FSGNJ_H_INX $rs1, (FCVT_H_D_IN32X $rs2, 0b111))>;
586def : Pat<(fcopysign FPR64IN32X:$rs1, FPR16INX:$rs2), (FSGNJ_D_IN32X $rs1, (FCVT_D_H_IN32X $rs2))>;
587} // Predicates = [HasStdExtZhinxOrZhinxmin, HasStdExtZdinx, IsRV32]
588
589let Predicates = [HasStdExtZhinxOrZhinxmin, HasStdExtZdinx, IsRV64] in {
590/// Float conversion operations
591// f64 -> f16, f16 -> f64
592def : Pat<(any_fpround FPR64INX:$rs1), (FCVT_H_D_INX FPR64INX:$rs1, FRM_DYN)>;
593def : Pat<(any_fpextend FPR16INX:$rs1), (FCVT_D_H_INX FPR16INX:$rs1)>;
594
595/// Float arithmetic operations
596def : Pat<(fcopysign FPR16INX:$rs1, FPR64INX:$rs2),
597          (FSGNJ_H_INX $rs1, (FCVT_H_D_INX $rs2, 0b111))>;
598def : Pat<(fcopysign FPR64INX:$rs1, FPR16INX:$rs2), (FSGNJ_D_INX $rs1, (FCVT_D_H_INX $rs2))>;
599} // Predicates = [HasStdExtZhinxOrZhinxmin, HasStdExtZdinx, IsRV64]
600
601let Predicates = [HasStdExtZfhmin, NoStdExtZfh, IsRV32] in {
602// half->[u]int. Round-to-zero must be used.
603def : Pat<(i32 (any_fp_to_sint (f16 FPR16:$rs1))), (FCVT_W_S (FCVT_S_H $rs1), FRM_RTZ)>;
604def : Pat<(i32 (any_fp_to_uint (f16 FPR16:$rs1))), (FCVT_WU_S (FCVT_S_H $rs1), FRM_RTZ)>;
605
606// half->int32 with current rounding mode.
607def : Pat<(i32 (any_lrint (f16 FPR16:$rs1))), (FCVT_W_S (FCVT_S_H $rs1), FRM_DYN)>;
608
609// half->int32 rounded to nearest with ties rounded away from zero.
610def : Pat<(i32 (any_lround (f16 FPR16:$rs1))), (FCVT_W_S (FCVT_S_H $rs1), FRM_RMM)>;
611
612// [u]int->half. Match GCC and default to using dynamic rounding mode.
613def : Pat<(f16 (any_sint_to_fp (i32 GPR:$rs1))), (FCVT_H_S (FCVT_S_W $rs1, FRM_DYN), FRM_DYN)>;
614def : Pat<(f16 (any_uint_to_fp (i32 GPR:$rs1))), (FCVT_H_S (FCVT_S_WU $rs1, FRM_DYN), FRM_DYN)>;
615} // Predicates = [HasStdExtZfhmin, NoStdExtZfh, IsRV32]
616
617let Predicates = [HasStdExtZhinxmin, NoStdExtZhinx, IsRV32] in {
618// half->[u]int. Round-to-zero must be used.
619def : Pat<(i32 (any_fp_to_sint FPR16INX:$rs1)), (FCVT_W_S_INX (FCVT_S_H_INX $rs1), FRM_RTZ)>;
620def : Pat<(i32 (any_fp_to_uint FPR16INX:$rs1)), (FCVT_WU_S_INX (FCVT_S_H_INX $rs1), FRM_RTZ)>;
621
622// half->int32 with current rounding mode.
623def : Pat<(i32 (any_lrint FPR16INX:$rs1)), (FCVT_W_S_INX (FCVT_S_H_INX $rs1), FRM_DYN)>;
624
625// half->int32 rounded to nearest with ties rounded away from zero.
626def : Pat<(i32 (any_lround FPR16INX:$rs1)), (FCVT_W_S_INX (FCVT_S_H_INX $rs1), FRM_RMM)>;
627
628// [u]int->half. Match GCC and default to using dynamic rounding mode.
629def : Pat<(any_sint_to_fp (i32 GPR:$rs1)), (FCVT_H_S_INX (FCVT_S_W_INX $rs1, FRM_DYN), FRM_DYN)>;
630def : Pat<(any_uint_to_fp (i32 GPR:$rs1)), (FCVT_H_S_INX (FCVT_S_WU_INX $rs1, FRM_DYN), FRM_DYN)>;
631} // Predicates = [HasStdExtZhinxmin, NoStdExtZhinx, IsRV32]
632
633let Predicates = [HasStdExtZfhmin, NoStdExtZfh, IsRV64] in {
634// half->[u]int64. Round-to-zero must be used.
635def : Pat<(i64 (any_fp_to_sint (f16 FPR16:$rs1))), (FCVT_L_S (FCVT_S_H $rs1), FRM_RTZ)>;
636def : Pat<(i64 (any_fp_to_uint (f16 FPR16:$rs1))), (FCVT_LU_S (FCVT_S_H $rs1), FRM_RTZ)>;
637
638// half->int64 with current rounding mode.
639def : Pat<(i64 (any_lrint (f16 FPR16:$rs1))), (FCVT_L_S (FCVT_S_H $rs1), FRM_DYN)>;
640def : Pat<(i64 (any_llrint (f16 FPR16:$rs1))), (FCVT_L_S (FCVT_S_H $rs1), FRM_DYN)>;
641
642// half->int64 rounded to nearest with ties rounded away from zero.
643def : Pat<(i64 (any_lround (f16 FPR16:$rs1))), (FCVT_L_S (FCVT_S_H $rs1), FRM_RMM)>;
644def : Pat<(i64 (any_llround (f16 FPR16:$rs1))), (FCVT_L_S (FCVT_S_H $rs1), FRM_RMM)>;
645
646// [u]int->fp. Match GCC and default to using dynamic rounding mode.
647def : Pat<(f16 (any_sint_to_fp (i64 GPR:$rs1))), (FCVT_H_S (FCVT_S_L $rs1, FRM_DYN), FRM_DYN)>;
648def : Pat<(f16 (any_uint_to_fp (i64 GPR:$rs1))), (FCVT_H_S (FCVT_S_LU $rs1, FRM_DYN), FRM_DYN)>;
649} // Predicates = [HasStdExtZfhmin, NoStdExtZfh, IsRV64]
650
651let Predicates = [HasStdExtZhinxmin, NoStdExtZhinx, IsRV64] in {
652// half->[u]int64. Round-to-zero must be used.
653def : Pat<(i64 (any_fp_to_sint FPR16INX:$rs1)), (FCVT_L_S_INX (FCVT_S_H_INX $rs1), FRM_RTZ)>;
654def : Pat<(i64 (any_fp_to_uint FPR16INX:$rs1)), (FCVT_LU_S_INX (FCVT_S_H_INX $rs1), FRM_RTZ)>;
655
656// half->int64 with current rounding mode.
657def : Pat<(i64 (any_lrint FPR16INX:$rs1)), (FCVT_L_S_INX (FCVT_S_H_INX $rs1), FRM_DYN)>;
658def : Pat<(i64 (any_llrint FPR16INX:$rs1)), (FCVT_L_S_INX (FCVT_S_H_INX $rs1), FRM_DYN)>;
659
660// half->int64 rounded to nearest with ties rounded away from zero.
661def : Pat<(i64 (any_lround FPR16INX:$rs1)), (FCVT_L_S_INX (FCVT_S_H_INX $rs1), FRM_RMM)>;
662def : Pat<(i64 (any_llround FPR16INX:$rs1)), (FCVT_L_S_INX (FCVT_S_H_INX $rs1), FRM_RMM)>;
663
664// [u]int->fp. Match GCC and default to using dynamic rounding mode.
665def : Pat<(any_sint_to_fp (i64 GPR:$rs1)), (FCVT_H_S_INX (FCVT_S_L_INX $rs1, FRM_DYN), FRM_DYN)>;
666def : Pat<(any_uint_to_fp (i64 GPR:$rs1)), (FCVT_H_S_INX (FCVT_S_LU_INX $rs1, FRM_DYN), FRM_DYN)>;
667} // Predicates = [HasStdExtZhinxmin, NoStdExtZhinx, IsRV64]
668