WebAssemblyInstrFloat.td 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // WebAssemblyInstrFloat.td-WebAssembly Float codegen support ---*- 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. /// \file
  10. /// WebAssembly Floating-point operand code-gen constructs.
  11. ///
  12. //===----------------------------------------------------------------------===//
  13. multiclass UnaryFP<SDNode node, string name, bits<32> f32Inst,
  14. bits<32> f64Inst> {
  15. defm _F32 : I<(outs F32:$dst), (ins F32:$src), (outs), (ins),
  16. [(set F32:$dst, (node F32:$src))],
  17. !strconcat("f32.", !strconcat(name, "\t$dst, $src")),
  18. !strconcat("f32.", name), f32Inst>;
  19. defm _F64 : I<(outs F64:$dst), (ins F64:$src), (outs), (ins),
  20. [(set F64:$dst, (node F64:$src))],
  21. !strconcat("f64.", !strconcat(name, "\t$dst, $src")),
  22. !strconcat("f64.", name), f64Inst>;
  23. }
  24. multiclass BinaryFP<SDNode node, string name, bits<32> f32Inst,
  25. bits<32> f64Inst> {
  26. defm _F32 : I<(outs F32:$dst), (ins F32:$lhs, F32:$rhs), (outs), (ins),
  27. [(set F32:$dst, (node F32:$lhs, F32:$rhs))],
  28. !strconcat("f32.", !strconcat(name, "\t$dst, $lhs, $rhs")),
  29. !strconcat("f32.", name), f32Inst>;
  30. defm _F64 : I<(outs F64:$dst), (ins F64:$lhs, F64:$rhs), (outs), (ins),
  31. [(set F64:$dst, (node F64:$lhs, F64:$rhs))],
  32. !strconcat("f64.", !strconcat(name, "\t$dst, $lhs, $rhs")),
  33. !strconcat("f64.", name), f64Inst>;
  34. }
  35. multiclass ComparisonFP<CondCode cond, string name, bits<32> f32Inst, bits<32> f64Inst> {
  36. defm _F32 : I<(outs I32:$dst), (ins F32:$lhs, F32:$rhs), (outs), (ins),
  37. [(set I32:$dst, (setcc F32:$lhs, F32:$rhs, cond))],
  38. !strconcat("f32.", !strconcat(name, "\t$dst, $lhs, $rhs")),
  39. !strconcat("f32.", name), f32Inst>;
  40. defm _F64 : I<(outs I32:$dst), (ins F64:$lhs, F64:$rhs), (outs), (ins),
  41. [(set I32:$dst, (setcc F64:$lhs, F64:$rhs, cond))],
  42. !strconcat("f64.", !strconcat(name, "\t$dst, $lhs, $rhs")),
  43. !strconcat("f64.", name), f64Inst>;
  44. }
  45. let isCommutable = 1 in
  46. defm ADD : BinaryFP<fadd, "add ", 0x92, 0xa0>;
  47. defm SUB : BinaryFP<fsub, "sub ", 0x93, 0xa1>;
  48. let isCommutable = 1 in
  49. defm MUL : BinaryFP<fmul, "mul ", 0x94, 0xa2>;
  50. defm DIV : BinaryFP<fdiv, "div ", 0x95, 0xa3>;
  51. defm SQRT : UnaryFP<fsqrt, "sqrt", 0x91, 0x9f>;
  52. defm ABS : UnaryFP<fabs, "abs ", 0x8b, 0x99>;
  53. defm NEG : UnaryFP<fneg, "neg ", 0x8c, 0x9a>;
  54. defm COPYSIGN : BinaryFP<fcopysign, "copysign", 0x98, 0xa6>;
  55. let isCommutable = 1 in {
  56. defm MIN : BinaryFP<fminimum, "min ", 0x96, 0xa4>;
  57. defm MAX : BinaryFP<fmaximum, "max ", 0x97, 0xa5>;
  58. } // isCommutable = 1
  59. defm CEIL : UnaryFP<fceil, "ceil", 0x8d, 0x9b>;
  60. defm FLOOR : UnaryFP<ffloor, "floor", 0x8e, 0x9c>;
  61. defm TRUNC : UnaryFP<ftrunc, "trunc", 0x8f, 0x9d>;
  62. defm NEAREST : UnaryFP<fnearbyint, "nearest", 0x90, 0x9e>;
  63. // DAGCombine oddly folds casts into the rhs of copysign. Unfold them.
  64. def : Pat<(fcopysign F64:$lhs, F32:$rhs),
  65. (COPYSIGN_F64 F64:$lhs, (F64_PROMOTE_F32 F32:$rhs))>;
  66. def : Pat<(fcopysign F32:$lhs, F64:$rhs),
  67. (COPYSIGN_F32 F32:$lhs, (F32_DEMOTE_F64 F64:$rhs))>;
  68. // WebAssembly doesn't expose inexact exceptions, so map frint to fnearbyint.
  69. def : Pat<(frint f32:$src), (NEAREST_F32 f32:$src)>;
  70. def : Pat<(frint f64:$src), (NEAREST_F64 f64:$src)>;
  71. let isCommutable = 1 in {
  72. defm EQ : ComparisonFP<SETOEQ, "eq ", 0x5b, 0x61>;
  73. defm NE : ComparisonFP<SETUNE, "ne ", 0x5c, 0x62>;
  74. } // isCommutable = 1
  75. defm LT : ComparisonFP<SETOLT, "lt ", 0x5d, 0x63>;
  76. defm LE : ComparisonFP<SETOLE, "le ", 0x5f, 0x65>;
  77. defm GT : ComparisonFP<SETOGT, "gt ", 0x5e, 0x64>;
  78. defm GE : ComparisonFP<SETOGE, "ge ", 0x60, 0x66>;
  79. // Don't care floating-point comparisons, supported via other comparisons.
  80. def : Pat<(seteq f32:$lhs, f32:$rhs), (EQ_F32 f32:$lhs, f32:$rhs)>;
  81. def : Pat<(setne f32:$lhs, f32:$rhs), (NE_F32 f32:$lhs, f32:$rhs)>;
  82. def : Pat<(setlt f32:$lhs, f32:$rhs), (LT_F32 f32:$lhs, f32:$rhs)>;
  83. def : Pat<(setle f32:$lhs, f32:$rhs), (LE_F32 f32:$lhs, f32:$rhs)>;
  84. def : Pat<(setgt f32:$lhs, f32:$rhs), (GT_F32 f32:$lhs, f32:$rhs)>;
  85. def : Pat<(setge f32:$lhs, f32:$rhs), (GE_F32 f32:$lhs, f32:$rhs)>;
  86. def : Pat<(seteq f64:$lhs, f64:$rhs), (EQ_F64 f64:$lhs, f64:$rhs)>;
  87. def : Pat<(setne f64:$lhs, f64:$rhs), (NE_F64 f64:$lhs, f64:$rhs)>;
  88. def : Pat<(setlt f64:$lhs, f64:$rhs), (LT_F64 f64:$lhs, f64:$rhs)>;
  89. def : Pat<(setle f64:$lhs, f64:$rhs), (LE_F64 f64:$lhs, f64:$rhs)>;
  90. def : Pat<(setgt f64:$lhs, f64:$rhs), (GT_F64 f64:$lhs, f64:$rhs)>;
  91. def : Pat<(setge f64:$lhs, f64:$rhs), (GE_F64 f64:$lhs, f64:$rhs)>;
  92. defm SELECT_F32 : I<(outs F32:$dst), (ins F32:$lhs, F32:$rhs, I32:$cond),
  93. (outs), (ins),
  94. [(set F32:$dst, (select I32:$cond, F32:$lhs, F32:$rhs))],
  95. "f32.select\t$dst, $lhs, $rhs, $cond", "f32.select", 0x1b>;
  96. defm SELECT_F64 : I<(outs F64:$dst), (ins F64:$lhs, F64:$rhs, I32:$cond),
  97. (outs), (ins),
  98. [(set F64:$dst, (select I32:$cond, F64:$lhs, F64:$rhs))],
  99. "f64.select\t$dst, $lhs, $rhs, $cond", "f64.select", 0x1b>;
  100. // ISD::SELECT requires its operand to conform to getBooleanContents, but
  101. // WebAssembly's select interprets any non-zero value as true, so we can fold
  102. // a setne with 0 into a select.
  103. def : Pat<(select (i32 (setne I32:$cond, 0)), F32:$lhs, F32:$rhs),
  104. (SELECT_F32 F32:$lhs, F32:$rhs, I32:$cond)>;
  105. def : Pat<(select (i32 (setne I32:$cond, 0)), F64:$lhs, F64:$rhs),
  106. (SELECT_F64 F64:$lhs, F64:$rhs, I32:$cond)>;
  107. // And again, this time with seteq instead of setne and the arms reversed.
  108. def : Pat<(select (i32 (seteq I32:$cond, 0)), F32:$lhs, F32:$rhs),
  109. (SELECT_F32 F32:$rhs, F32:$lhs, I32:$cond)>;
  110. def : Pat<(select (i32 (seteq I32:$cond, 0)), F64:$lhs, F64:$rhs),
  111. (SELECT_F64 F64:$rhs, F64:$lhs, I32:$cond)>;