RISCVInstrInfoZfh.td 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. // RISC-V specific DAG Nodes.
  15. //===----------------------------------------------------------------------===//
  16. def SDT_RISCVFMV_H_X
  17. : SDTypeProfile<1, 1, [SDTCisVT<0, f16>, SDTCisVT<1, XLenVT>]>;
  18. def SDT_RISCVFMV_X_EXTH
  19. : SDTypeProfile<1, 1, [SDTCisVT<0, XLenVT>, SDTCisVT<1, f16>]>;
  20. def riscv_fmv_h_x
  21. : SDNode<"RISCVISD::FMV_H_X", SDT_RISCVFMV_H_X>;
  22. def riscv_fmv_x_anyexth
  23. : SDNode<"RISCVISD::FMV_X_ANYEXTH", SDT_RISCVFMV_X_EXTH>;
  24. def riscv_fmv_x_signexth
  25. : SDNode<"RISCVISD::FMV_X_SIGNEXTH", SDT_RISCVFMV_X_EXTH>;
  26. //===----------------------------------------------------------------------===//
  27. // Operand and SDNode transformation definitions.
  28. //===----------------------------------------------------------------------===//
  29. // Zhinxmin and Zhinx
  30. def FPR16INX : RegisterOperand<GPRF16> {
  31. let ParserMatchClass = GPRAsFPR;
  32. let DecoderMethod = "DecodeGPRRegisterClass";
  33. }
  34. def ZfhExt : ExtInfo<0, [HasStdExtZfh]>;
  35. def Zfh64Ext : ExtInfo<0, [HasStdExtZfh, IsRV64]>;
  36. def ZfhminExt : ExtInfo<0, [HasStdExtZfhOrZfhmin]>;
  37. def ZhinxExt : ExtInfo<1, [HasStdExtZhinx]>;
  38. def ZhinxminExt : ExtInfo<1, [HasStdExtZhinxOrZhinxmin]>;
  39. def Zhinx64Ext : ExtInfo<1, [HasStdExtZhinx, IsRV64]>;
  40. def ZfhminDExt : ExtInfo<0, [HasStdExtZfhOrZfhmin, HasStdExtD]>;
  41. def ZhinxminZdinxExt : ExtInfo<1, [HasStdExtZhinxOrZhinxmin, HasStdExtZdinx]>;
  42. def H : ExtInfo_r<ZfhExt, FPR16>;
  43. def H_INX : ExtInfo_r<ZhinxExt, FPR16INX>;
  44. def HH : ExtInfo_rr<ZfhExt, FPR16, FPR16>;
  45. def HH_INX : ExtInfo_rr<ZhinxExt, FPR16INX, FPR16INX>;
  46. def XH : ExtInfo_rr<ZfhExt, GPR, FPR16>;
  47. def XH_INX : ExtInfo_rr<ZhinxExt, GPR, FPR16INX>;
  48. def HX : ExtInfo_rr<ZfhExt, FPR16, GPR>;
  49. def HX_INX : ExtInfo_rr<ZhinxExt, FPR16INX, GPR>;
  50. def XH_64 : ExtInfo_rr<Zfh64Ext, GPR, FPR16>;
  51. def HX_64 : ExtInfo_rr<Zfh64Ext, FPR16, GPR>;
  52. def XH_INX_64 : ExtInfo_rr<Zhinx64Ext, GPR, FPR16INX>;
  53. def HX_INX_64 : ExtInfo_rr<Zhinx64Ext, FPR16INX, GPR>;
  54. def HFmin : ExtInfo_rr<ZfhminExt, FPR16, FPR32>;
  55. def HF_INXmin : ExtInfo_rr<ZhinxminExt, FPR16INX, FPR32INX>;
  56. def HF_INX : ExtInfo_rr<ZhinxExt, FPR16INX, FPR32INX>;
  57. def FHmin : ExtInfo_rr<ZfhminExt, FPR32, FPR16>;
  58. def FH_INXmin : ExtInfo_rr<ZhinxminExt, FPR32INX, FPR16INX>;
  59. def FH_INX : ExtInfo_rr<ZhinxExt, FPR32INX, FPR16INX>;
  60. def DHmin : ExtInfo_rr<ZfhminDExt, FPR64, FPR16>;
  61. def DH_INXmin : ExtInfo_rr<ZhinxminZdinxExt, FPR64INX, FPR16INX>;
  62. def HDmin : ExtInfo_rr<ZfhminDExt, FPR16, FPR64>;
  63. def HD_INXmin : ExtInfo_rr<ZhinxminZdinxExt, FPR16INX, FPR64INX>;
  64. defvar HINX = [H, H_INX];
  65. defvar HHINX = [HH, HH_INX];
  66. defvar XHINX = [XH, XH_INX];
  67. defvar HXINX = [HX, HX_INX];
  68. defvar XHIN64X = [XH_64, XH_INX_64];
  69. defvar HXIN64X = [HX_64, HX_INX_64];
  70. defvar HFINXmin = [HFmin, HF_INXmin];
  71. defvar FHINXmin = [FHmin, FH_INXmin];
  72. defvar DHINXmin = [DHmin, DH_INXmin];
  73. defvar HDINXmin = [HDmin, HD_INXmin];
  74. //===----------------------------------------------------------------------===//
  75. // Instructions
  76. //===----------------------------------------------------------------------===//
  77. let Predicates = [HasStdExtZfhOrZfhmin] in {
  78. def FLH : FPLoad_r<0b001, "flh", FPR16, WriteFLD16>;
  79. // Operands for stores are in the order srcreg, base, offset rather than
  80. // reflecting the order these fields are specified in the instruction
  81. // encoding.
  82. def FSH : FPStore_r<0b001, "fsh", FPR16, WriteFST16>;
  83. } // Predicates = [HasStdExtZfhOrZfhmin]
  84. let SchedRW = [WriteFMA16, ReadFMA16, ReadFMA16, ReadFMA16] in {
  85. defm FMADD_H : FPFMA_rrr_frm_m<OPC_MADD, 0b10, "fmadd.h", HINX>;
  86. defm FMSUB_H : FPFMA_rrr_frm_m<OPC_MSUB, 0b10, "fmsub.h", HINX>;
  87. defm FNMSUB_H : FPFMA_rrr_frm_m<OPC_NMSUB, 0b10, "fnmsub.h", HINX>;
  88. defm FNMADD_H : FPFMA_rrr_frm_m<OPC_NMADD, 0b10, "fnmadd.h", HINX>;
  89. }
  90. defm : FPFMADynFrmAlias_m<FMADD_H, "fmadd.h", HINX>;
  91. defm : FPFMADynFrmAlias_m<FMSUB_H, "fmsub.h", HINX>;
  92. defm : FPFMADynFrmAlias_m<FNMSUB_H, "fnmsub.h", HINX>;
  93. defm : FPFMADynFrmAlias_m<FNMADD_H, "fnmadd.h", HINX>;
  94. let SchedRW = [WriteFAdd16, ReadFAdd16, ReadFAdd16] in {
  95. defm FADD_H : FPALU_rr_frm_m<0b0000010, "fadd.h", HINX, /*Commutable*/1>;
  96. defm FSUB_H : FPALU_rr_frm_m<0b0000110, "fsub.h", HINX>;
  97. }
  98. let SchedRW = [WriteFMul16, ReadFMul16, ReadFMul16] in
  99. defm FMUL_H : FPALU_rr_frm_m<0b0001010, "fmul.h", HINX, /*Commutable*/1>;
  100. let SchedRW = [WriteFDiv16, ReadFDiv16, ReadFDiv16] in
  101. defm FDIV_H : FPALU_rr_frm_m<0b0001110, "fdiv.h", HINX>;
  102. defm : FPALUDynFrmAlias_m<FADD_H, "fadd.h", HINX>;
  103. defm : FPALUDynFrmAlias_m<FSUB_H, "fsub.h", HINX>;
  104. defm : FPALUDynFrmAlias_m<FMUL_H, "fmul.h", HINX>;
  105. defm : FPALUDynFrmAlias_m<FDIV_H, "fdiv.h", HINX>;
  106. defm FSQRT_H : FPUnaryOp_r_frm_m<0b0101110, 0b00000, HHINX, "fsqrt.h">,
  107. Sched<[WriteFSqrt16, ReadFSqrt16]>;
  108. defm : FPUnaryOpDynFrmAlias_m<FSQRT_H, "fsqrt.h", HHINX>;
  109. let SchedRW = [WriteFSGNJ16, ReadFSGNJ16, ReadFSGNJ16],
  110. mayRaiseFPException = 0 in {
  111. defm FSGNJ_H : FPALU_rr_m<0b0010010, 0b000, "fsgnj.h", HINX>;
  112. defm FSGNJN_H : FPALU_rr_m<0b0010010, 0b001, "fsgnjn.h", HINX>;
  113. defm FSGNJX_H : FPALU_rr_m<0b0010010, 0b010, "fsgnjx.h", HINX>;
  114. }
  115. let SchedRW = [WriteFMinMax16, ReadFMinMax16, ReadFMinMax16] in {
  116. defm FMIN_H : FPALU_rr_m<0b0010110, 0b000, "fmin.h", HINX, /*Commutable*/1>;
  117. defm FMAX_H : FPALU_rr_m<0b0010110, 0b001, "fmax.h", HINX, /*Commutable*/1>;
  118. }
  119. let IsSignExtendingOpW = 1 in
  120. defm FCVT_W_H : FPUnaryOp_r_frm_m<0b1100010, 0b00000, XHINX, "fcvt.w.h">,
  121. Sched<[WriteFCvtF16ToI32, ReadFCvtF16ToI32]>;
  122. defm : FPUnaryOpDynFrmAlias_m<FCVT_W_H, "fcvt.w.h", XHINX>;
  123. let IsSignExtendingOpW = 1 in
  124. defm FCVT_WU_H : FPUnaryOp_r_frm_m<0b1100010, 0b00001, XHINX, "fcvt.wu.h">,
  125. Sched<[WriteFCvtF16ToI32, ReadFCvtF16ToI32]>;
  126. defm : FPUnaryOpDynFrmAlias_m<FCVT_WU_H, "fcvt.wu.h", XHINX>;
  127. defm FCVT_H_W : FPUnaryOp_r_frm_m<0b1101010, 0b00000, HXINX, "fcvt.h.w">,
  128. Sched<[WriteFCvtI32ToF16, ReadFCvtI32ToF16]>;
  129. defm : FPUnaryOpDynFrmAlias_m<FCVT_H_W, "fcvt.h.w", HXINX>;
  130. defm FCVT_H_WU : FPUnaryOp_r_frm_m<0b1101010, 0b00001, HXINX, "fcvt.h.wu">,
  131. Sched<[WriteFCvtI32ToF16, ReadFCvtI32ToF16]>;
  132. defm : FPUnaryOpDynFrmAlias_m<FCVT_H_WU, "fcvt.h.wu", HXINX>;
  133. defm FCVT_H_S : FPUnaryOp_r_frm_m<0b0100010, 0b00000, HFINXmin, "fcvt.h.s">,
  134. Sched<[WriteFCvtF32ToF16, ReadFCvtF32ToF16]>;
  135. defm : FPUnaryOpDynFrmAlias_m<FCVT_H_S, "fcvt.h.s", HFINXmin>;
  136. defm FCVT_S_H : FPUnaryOp_r_m<0b0100000, 0b00010, 0b000, FHINXmin, "fcvt.s.h">,
  137. Sched<[WriteFCvtF16ToF32, ReadFCvtF16ToF32]>;
  138. let Predicates = [HasStdExtZfhOrZfhmin] in {
  139. let mayRaiseFPException = 0, IsSignExtendingOpW = 1 in
  140. def FMV_X_H : FPUnaryOp_r<0b1110010, 0b00000, 0b000, GPR, FPR16, "fmv.x.h">,
  141. Sched<[WriteFMovF16ToI16, ReadFMovF16ToI16]>;
  142. let mayRaiseFPException = 0 in
  143. def FMV_H_X : FPUnaryOp_r<0b1111010, 0b00000, 0b000, FPR16, GPR, "fmv.h.x">,
  144. Sched<[WriteFMovI16ToF16, ReadFMovI16ToF16]>;
  145. } // Predicates = [HasStdExtZfhOrZfhmin]
  146. let SchedRW = [WriteFCmp16, ReadFCmp16, ReadFCmp16] in {
  147. defm FEQ_H : FPCmp_rr_m<0b1010010, 0b010, "feq.h", HINX, /*Commutable*/1>;
  148. defm FLT_H : FPCmp_rr_m<0b1010010, 0b001, "flt.h", HINX>;
  149. defm FLE_H : FPCmp_rr_m<0b1010010, 0b000, "fle.h", HINX>;
  150. }
  151. let mayRaiseFPException = 0 in
  152. defm FCLASS_H : FPUnaryOp_r_m<0b1110010, 0b00000, 0b001, XHINX, "fclass.h">,
  153. Sched<[WriteFClass16, ReadFClass16]>;
  154. defm FCVT_L_H : FPUnaryOp_r_frm_m<0b1100010, 0b00010, XHIN64X, "fcvt.l.h">,
  155. Sched<[WriteFCvtF16ToI64, ReadFCvtF16ToI64]>;
  156. defm : FPUnaryOpDynFrmAlias_m<FCVT_L_H, "fcvt.l.h", XHIN64X>;
  157. defm FCVT_LU_H : FPUnaryOp_r_frm_m<0b1100010, 0b00011, XHIN64X, "fcvt.lu.h">,
  158. Sched<[WriteFCvtF16ToI64, ReadFCvtF16ToI64]>;
  159. defm : FPUnaryOpDynFrmAlias_m<FCVT_LU_H, "fcvt.lu.h", XHIN64X>;
  160. defm FCVT_H_L : FPUnaryOp_r_frm_m<0b1101010, 0b00010, HXIN64X, "fcvt.h.l">,
  161. Sched<[WriteFCvtI64ToF16, ReadFCvtI64ToF16]>;
  162. defm : FPUnaryOpDynFrmAlias_m<FCVT_H_L, "fcvt.h.l", HXIN64X>;
  163. defm FCVT_H_LU : FPUnaryOp_r_frm_m<0b1101010, 0b00011, HXIN64X, "fcvt.h.lu">,
  164. Sched<[WriteFCvtI64ToF16, ReadFCvtI64ToF16]>;
  165. defm : FPUnaryOpDynFrmAlias_m<FCVT_H_LU, "fcvt.h.lu", HXIN64X>;
  166. defm FCVT_H_D : FPUnaryOp_r_frm_m<0b0100010, 0b00001, HDINXmin, "fcvt.h.d">,
  167. Sched<[WriteFCvtF64ToF16, ReadFCvtF64ToF16]>;
  168. defm : FPUnaryOpDynFrmAlias_m<FCVT_H_D, "fcvt.h.d", HDINXmin>;
  169. defm FCVT_D_H : FPUnaryOp_r_m<0b0100001, 0b00010, 0b000, DHINXmin, "fcvt.d.h">,
  170. Sched<[WriteFCvtF16ToF64, ReadFCvtF16ToF64]>;
  171. //===----------------------------------------------------------------------===//
  172. // Assembler Pseudo Instructions (User-Level ISA, Version 2.2, Chapter 20)
  173. //===----------------------------------------------------------------------===//
  174. let Predicates = [HasStdExtZfhOrZfhmin] in {
  175. def : InstAlias<"flh $rd, (${rs1})", (FLH FPR16:$rd, GPR:$rs1, 0), 0>;
  176. def : InstAlias<"fsh $rs2, (${rs1})", (FSH FPR16:$rs2, GPR:$rs1, 0), 0>;
  177. } // Predicates = [HasStdExtZfhOrZfhmin]
  178. let Predicates = [HasStdExtZfh] in {
  179. def : InstAlias<"fmv.h $rd, $rs", (FSGNJ_H FPR16:$rd, FPR16:$rs, FPR16:$rs)>;
  180. def : InstAlias<"fabs.h $rd, $rs", (FSGNJX_H FPR16:$rd, FPR16:$rs, FPR16:$rs)>;
  181. def : InstAlias<"fneg.h $rd, $rs", (FSGNJN_H FPR16:$rd, FPR16:$rs, FPR16:$rs)>;
  182. // fgt.h/fge.h are recognised by the GNU assembler but the canonical
  183. // flt.h/fle.h forms will always be printed. Therefore, set a zero weight.
  184. def : InstAlias<"fgt.h $rd, $rs, $rt",
  185. (FLT_H GPR:$rd, FPR16:$rt, FPR16:$rs), 0>;
  186. def : InstAlias<"fge.h $rd, $rs, $rt",
  187. (FLE_H GPR:$rd, FPR16:$rt, FPR16:$rs), 0>;
  188. } // Predicates = [HasStdExtZfh]
  189. let Predicates = [HasStdExtZfhOrZfhmin] in {
  190. def PseudoFLH : PseudoFloatLoad<"flh", FPR16>;
  191. def PseudoFSH : PseudoStore<"fsh", FPR16>;
  192. let usesCustomInserter = 1 in {
  193. def PseudoQuietFLE_H : PseudoQuietFCMP<FPR16>;
  194. def PseudoQuietFLT_H : PseudoQuietFCMP<FPR16>;
  195. }
  196. } // Predicates = [HasStdExtZfhOrZfhmin]
  197. let Predicates = [HasStdExtZhinx] in {
  198. def : InstAlias<"fmv.h $rd, $rs", (FSGNJ_H_INX FPR16INX:$rd, FPR16INX:$rs, FPR16INX:$rs)>;
  199. def : InstAlias<"fabs.h $rd, $rs", (FSGNJX_H_INX FPR16INX:$rd, FPR16INX:$rs, FPR16INX:$rs)>;
  200. def : InstAlias<"fneg.h $rd, $rs", (FSGNJN_H_INX FPR16INX:$rd, FPR16INX:$rs, FPR16INX:$rs)>;
  201. def : InstAlias<"fgt.h $rd, $rs, $rt",
  202. (FLT_H_INX GPR:$rd, FPR16INX:$rt, FPR16INX:$rs), 0>;
  203. def : InstAlias<"fge.h $rd, $rs, $rt",
  204. (FLE_H_INX GPR:$rd, FPR16INX:$rt, FPR16INX:$rs), 0>;
  205. } // Predicates = [HasStdExtZhinx]
  206. //===----------------------------------------------------------------------===//
  207. // Pseudo-instructions and codegen patterns
  208. //===----------------------------------------------------------------------===//
  209. let Predicates = [HasStdExtZfh] in {
  210. // Floating point constant -0.0
  211. def : Pat<(f16 (fpimmneg0)), (FSGNJN_H (FMV_H_X X0), (FMV_H_X X0))>;
  212. /// Float conversion operations
  213. // [u]int32<->float conversion patterns must be gated on IsRV32 or IsRV64, so
  214. // are defined later.
  215. /// Float arithmetic operations
  216. def : PatFprFprDynFrm<any_fadd, FADD_H, FPR16>;
  217. def : PatFprFprDynFrm<any_fsub, FSUB_H, FPR16>;
  218. def : PatFprFprDynFrm<any_fmul, FMUL_H, FPR16>;
  219. def : PatFprFprDynFrm<any_fdiv, FDIV_H, FPR16>;
  220. def : Pat<(any_fsqrt FPR16:$rs1), (FSQRT_H FPR16:$rs1, 0b111)>;
  221. def : Pat<(fneg FPR16:$rs1), (FSGNJN_H $rs1, $rs1)>;
  222. def : Pat<(fabs FPR16:$rs1), (FSGNJX_H $rs1, $rs1)>;
  223. def : PatFprFpr<fcopysign, FSGNJ_H, FPR16>;
  224. def : Pat<(fcopysign FPR16:$rs1, (fneg FPR16:$rs2)), (FSGNJN_H $rs1, $rs2)>;
  225. def : Pat<(fcopysign FPR16:$rs1, FPR32:$rs2),
  226. (FSGNJ_H $rs1, (FCVT_H_S $rs2, 0b111))>;
  227. // fmadd: rs1 * rs2 + rs3
  228. def : Pat<(any_fma FPR16:$rs1, FPR16:$rs2, FPR16:$rs3),
  229. (FMADD_H $rs1, $rs2, $rs3, 0b111)>;
  230. // fmsub: rs1 * rs2 - rs3
  231. def : Pat<(any_fma FPR16:$rs1, FPR16:$rs2, (fneg FPR16:$rs3)),
  232. (FMSUB_H FPR16:$rs1, FPR16:$rs2, FPR16:$rs3, 0b111)>;
  233. // fnmsub: -rs1 * rs2 + rs3
  234. def : Pat<(any_fma (fneg FPR16:$rs1), FPR16:$rs2, FPR16:$rs3),
  235. (FNMSUB_H FPR16:$rs1, FPR16:$rs2, FPR16:$rs3, 0b111)>;
  236. // fnmadd: -rs1 * rs2 - rs3
  237. def : Pat<(any_fma (fneg FPR16:$rs1), FPR16:$rs2, (fneg FPR16:$rs3)),
  238. (FNMADD_H FPR16:$rs1, FPR16:$rs2, FPR16:$rs3, 0b111)>;
  239. // fnmadd: -(rs1 * rs2 + rs3) (the nsz flag on the FMA)
  240. def : Pat<(fneg (any_fma_nsz FPR16:$rs1, FPR16:$rs2, FPR16:$rs3)),
  241. (FNMADD_H FPR16:$rs1, FPR16:$rs2, FPR16:$rs3, 0b111)>;
  242. // The ratified 20191213 ISA spec defines fmin and fmax in a way that matches
  243. // LLVM's fminnum and fmaxnum
  244. // <https://github.com/riscv/riscv-isa-manual/commit/cd20cee7efd9bac7c5aa127ec3b451749d2b3cce>.
  245. def : PatFprFpr<fminnum, FMIN_H, FPR16>;
  246. def : PatFprFpr<fmaxnum, FMAX_H, FPR16>;
  247. /// Setcc
  248. // FIXME: SETEQ/SETLT/SETLE imply nonans, can we pick better instructions for
  249. // strict versions of those.
  250. // Match non-signaling FEQ_D
  251. def : PatSetCC<FPR16, any_fsetcc, SETEQ, FEQ_H>;
  252. def : PatSetCC<FPR16, any_fsetcc, SETOEQ, FEQ_H>;
  253. def : PatSetCC<FPR16, strict_fsetcc, SETLT, PseudoQuietFLT_H>;
  254. def : PatSetCC<FPR16, strict_fsetcc, SETOLT, PseudoQuietFLT_H>;
  255. def : PatSetCC<FPR16, strict_fsetcc, SETLE, PseudoQuietFLE_H>;
  256. def : PatSetCC<FPR16, strict_fsetcc, SETOLE, PseudoQuietFLE_H>;
  257. // Match signaling FEQ_H
  258. def : Pat<(strict_fsetccs FPR16:$rs1, FPR16:$rs2, SETEQ),
  259. (AND (FLE_H $rs1, $rs2),
  260. (FLE_H $rs2, $rs1))>;
  261. def : Pat<(strict_fsetccs FPR16:$rs1, FPR16:$rs2, SETOEQ),
  262. (AND (FLE_H $rs1, $rs2),
  263. (FLE_H $rs2, $rs1))>;
  264. // If both operands are the same, use a single FLE.
  265. def : Pat<(strict_fsetccs FPR16:$rs1, FPR16:$rs1, SETEQ),
  266. (FLE_H $rs1, $rs1)>;
  267. def : Pat<(strict_fsetccs FPR16:$rs1, FPR16:$rs1, SETOEQ),
  268. (FLE_H $rs1, $rs1)>;
  269. def : PatSetCC<FPR16, any_fsetccs, SETLT, FLT_H>;
  270. def : PatSetCC<FPR16, any_fsetccs, SETOLT, FLT_H>;
  271. def : PatSetCC<FPR16, any_fsetccs, SETLE, FLE_H>;
  272. def : PatSetCC<FPR16, any_fsetccs, SETOLE, FLE_H>;
  273. defm Select_FPR16 : SelectCC_GPR_rrirr<FPR16>;
  274. def PseudoFROUND_H : PseudoFROUND<FPR16>;
  275. } // Predicates = [HasStdExtZfh]
  276. let Predicates = [HasStdExtZfhOrZfhmin] in {
  277. /// Loads
  278. defm : LdPat<load, FLH, f16>;
  279. /// Stores
  280. defm : StPat<store, FSH, FPR16, f16>;
  281. /// Floating point constant +0.0
  282. def : Pat<(f16 (fpimm0)), (FMV_H_X X0)>;
  283. /// Float conversion operations
  284. // f32 -> f16, f16 -> f32
  285. def : Pat<(any_fpround FPR32:$rs1), (FCVT_H_S FPR32:$rs1, 0b111)>;
  286. def : Pat<(any_fpextend FPR16:$rs1), (FCVT_S_H FPR16:$rs1)>;
  287. // Moves (no conversion)
  288. def : Pat<(riscv_fmv_h_x GPR:$src), (FMV_H_X GPR:$src)>;
  289. def : Pat<(riscv_fmv_x_anyexth FPR16:$src), (FMV_X_H FPR16:$src)>;
  290. def : Pat<(riscv_fmv_x_signexth FPR16:$src), (FMV_X_H FPR16:$src)>;
  291. def : Pat<(fcopysign FPR32:$rs1, FPR16:$rs2), (FSGNJ_S $rs1, (FCVT_S_H $rs2))>;
  292. } // Predicates = [HasStdExtZfhOrZfhmin]
  293. let Predicates = [HasStdExtZfh, IsRV32] in {
  294. // half->[u]int. Round-to-zero must be used.
  295. def : Pat<(i32 (any_fp_to_sint FPR16:$rs1)), (FCVT_W_H $rs1, 0b001)>;
  296. def : Pat<(i32 (any_fp_to_uint FPR16:$rs1)), (FCVT_WU_H $rs1, 0b001)>;
  297. // Saturating half->[u]int32.
  298. def : Pat<(i32 (riscv_fcvt_x FPR16:$rs1, timm:$frm)), (FCVT_W_H $rs1, timm:$frm)>;
  299. def : Pat<(i32 (riscv_fcvt_xu FPR16:$rs1, timm:$frm)), (FCVT_WU_H $rs1, timm:$frm)>;
  300. // half->int32 with current rounding mode.
  301. def : Pat<(i32 (any_lrint FPR16:$rs1)), (FCVT_W_H $rs1, 0b111)>;
  302. // half->int32 rounded to nearest with ties rounded away from zero.
  303. def : Pat<(i32 (any_lround FPR16:$rs1)), (FCVT_W_H $rs1, 0b100)>;
  304. // [u]int->half. Match GCC and default to using dynamic rounding mode.
  305. def : Pat<(any_sint_to_fp (i32 GPR:$rs1)), (FCVT_H_W $rs1, 0b111)>;
  306. def : Pat<(any_uint_to_fp (i32 GPR:$rs1)), (FCVT_H_WU $rs1, 0b111)>;
  307. } // Predicates = [HasStdExtZfh, IsRV32]
  308. let Predicates = [HasStdExtZfh, IsRV64] in {
  309. // Use target specific isd nodes to help us remember the result is sign
  310. // extended. Matching sext_inreg+fptoui/fptosi may cause the conversion to be
  311. // duplicated if it has another user that didn't need the sign_extend.
  312. def : Pat<(riscv_any_fcvt_w_rv64 FPR16:$rs1, timm:$frm), (FCVT_W_H $rs1, timm:$frm)>;
  313. def : Pat<(riscv_any_fcvt_wu_rv64 FPR16:$rs1, timm:$frm), (FCVT_WU_H $rs1, timm:$frm)>;
  314. // half->[u]int64. Round-to-zero must be used.
  315. def : Pat<(i64 (any_fp_to_sint FPR16:$rs1)), (FCVT_L_H $rs1, 0b001)>;
  316. def : Pat<(i64 (any_fp_to_uint FPR16:$rs1)), (FCVT_LU_H $rs1, 0b001)>;
  317. // Saturating half->[u]int64.
  318. def : Pat<(i64 (riscv_fcvt_x FPR16:$rs1, timm:$frm)), (FCVT_L_H $rs1, timm:$frm)>;
  319. def : Pat<(i64 (riscv_fcvt_xu FPR16:$rs1, timm:$frm)), (FCVT_LU_H $rs1, timm:$frm)>;
  320. // half->int64 with current rounding mode.
  321. def : Pat<(i64 (any_lrint FPR16:$rs1)), (FCVT_L_H $rs1, 0b111)>;
  322. def : Pat<(i64 (any_llrint FPR16:$rs1)), (FCVT_L_H $rs1, 0b111)>;
  323. // half->int64 rounded to nearest with ties rounded away from zero.
  324. def : Pat<(i64 (any_lround FPR16:$rs1)), (FCVT_L_H $rs1, 0b100)>;
  325. def : Pat<(i64 (any_llround FPR16:$rs1)), (FCVT_L_H $rs1, 0b100)>;
  326. // [u]int->fp. Match GCC and default to using dynamic rounding mode.
  327. def : Pat<(any_sint_to_fp (i64 (sexti32 (i64 GPR:$rs1)))), (FCVT_H_W $rs1, 0b111)>;
  328. def : Pat<(any_uint_to_fp (i64 (zexti32 (i64 GPR:$rs1)))), (FCVT_H_WU $rs1, 0b111)>;
  329. def : Pat<(any_sint_to_fp (i64 GPR:$rs1)), (FCVT_H_L $rs1, 0b111)>;
  330. def : Pat<(any_uint_to_fp (i64 GPR:$rs1)), (FCVT_H_LU $rs1, 0b111)>;
  331. } // Predicates = [HasStdExtZfh, IsRV64]
  332. let Predicates = [HasStdExtZfhOrZfhmin, HasStdExtD] in {
  333. /// Float conversion operations
  334. // f64 -> f16, f16 -> f64
  335. def : Pat<(any_fpround FPR64:$rs1), (FCVT_H_D FPR64:$rs1, 0b111)>;
  336. def : Pat<(any_fpextend FPR16:$rs1), (FCVT_D_H FPR16:$rs1)>;
  337. /// Float arithmetic operations
  338. def : Pat<(fcopysign FPR16:$rs1, FPR64:$rs2),
  339. (FSGNJ_H $rs1, (FCVT_H_D $rs2, 0b111))>;
  340. def : Pat<(fcopysign FPR64:$rs1, FPR16:$rs2), (FSGNJ_D $rs1, (FCVT_D_H $rs2))>;
  341. } // Predicates = [HasStdExtZfhOrZfhmin, HasStdExtD]
  342. let Predicates = [HasStdExtZfhmin, NoStdExtZfh] in {
  343. // Floating point constant -0.0
  344. def : Pat<(f16 (fpimmneg0)), (FCVT_H_S (FSGNJN_S (FMV_W_X X0), (FMV_W_X X0)), 0b111)>;
  345. } // Predicates = [HasStdExtZfhmin, NoStdExtZfh]
  346. let Predicates = [HasStdExtZfhmin, NoStdExtZfh, IsRV32] in {
  347. // half->[u]int. Round-to-zero must be used.
  348. def : Pat<(i32 (any_fp_to_sint FPR16:$rs1)), (FCVT_W_S (FCVT_S_H $rs1), 0b001)>;
  349. def : Pat<(i32 (any_fp_to_uint FPR16:$rs1)), (FCVT_WU_S (FCVT_S_H $rs1), 0b001)>;
  350. // half->int32 with current rounding mode.
  351. def : Pat<(i32 (any_lrint FPR16:$rs1)), (FCVT_W_S (FCVT_S_H $rs1), 0b111)>;
  352. // half->int32 rounded to nearest with ties rounded away from zero.
  353. def : Pat<(i32 (any_lround FPR16:$rs1)), (FCVT_W_S (FCVT_S_H $rs1), 0b100)>;
  354. // [u]int->half. Match GCC and default to using dynamic rounding mode.
  355. def : Pat<(any_sint_to_fp (i32 GPR:$rs1)), (FCVT_H_S (FCVT_S_W $rs1, 0b111), 0b111)>;
  356. def : Pat<(any_uint_to_fp (i32 GPR:$rs1)), (FCVT_H_S (FCVT_S_WU $rs1, 0b111), 0b111)>;
  357. } // Predicates = [HasStdExtZfhmin, NoStdExtZfh, IsRV32]
  358. let Predicates = [HasStdExtZfhmin, NoStdExtZfh, IsRV64] in {
  359. // half->[u]int64. Round-to-zero must be used.
  360. def : Pat<(i64 (any_fp_to_sint FPR16:$rs1)), (FCVT_L_S (FCVT_S_H $rs1), 0b001)>;
  361. def : Pat<(i64 (any_fp_to_uint FPR16:$rs1)), (FCVT_LU_S (FCVT_S_H $rs1), 0b001)>;
  362. // half->int64 with current rounding mode.
  363. def : Pat<(i64 (any_lrint FPR16:$rs1)), (FCVT_L_S (FCVT_S_H $rs1), 0b111)>;
  364. def : Pat<(i64 (any_llrint FPR16:$rs1)), (FCVT_L_S (FCVT_S_H $rs1), 0b111)>;
  365. // half->int64 rounded to nearest with ties rounded away from zero.
  366. def : Pat<(i64 (any_lround FPR16:$rs1)), (FCVT_L_S (FCVT_S_H $rs1), 0b100)>;
  367. def : Pat<(i64 (any_llround FPR16:$rs1)), (FCVT_L_S (FCVT_S_H $rs1), 0b100)>;
  368. // [u]int->fp. Match GCC and default to using dynamic rounding mode.
  369. def : Pat<(any_sint_to_fp (i64 GPR:$rs1)), (FCVT_H_S (FCVT_S_L $rs1, 0b111), 0b111)>;
  370. def : Pat<(any_uint_to_fp (i64 GPR:$rs1)), (FCVT_H_S (FCVT_S_LU $rs1, 0b111), 0b111)>;
  371. } // Predicates = [HasStdExtZfhmin, NoStdExtZfh, IsRV64]