xref: /aosp_15_r20/external/llvm/lib/Target/WebAssembly/WebAssemblyInstrFloat.td (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1// WebAssemblyInstrFloat.td-WebAssembly Float codegen support ---*- tablegen -*-
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// \brief WebAssembly Floating-point operand code-gen constructs.
12///
13//===----------------------------------------------------------------------===//
14
15let Defs = [ARGUMENTS] in {
16
17let isCommutable = 1 in
18defm ADD : BinaryFP<fadd, "add ">;
19defm SUB : BinaryFP<fsub, "sub ">;
20let isCommutable = 1 in
21defm MUL : BinaryFP<fmul, "mul ">;
22defm DIV : BinaryFP<fdiv, "div ">;
23defm SQRT : UnaryFP<fsqrt, "sqrt">;
24
25defm ABS : UnaryFP<fabs, "abs ">;
26defm NEG : UnaryFP<fneg, "neg ">;
27defm COPYSIGN : BinaryFP<fcopysign, "copysign">;
28
29let isCommutable = 1 in {
30defm MIN : BinaryFP<fminnan, "min ">;
31defm MAX : BinaryFP<fmaxnan, "max ">;
32} // isCommutable = 1
33
34defm CEIL : UnaryFP<fceil, "ceil">;
35defm FLOOR : UnaryFP<ffloor, "floor">;
36defm TRUNC : UnaryFP<ftrunc, "trunc">;
37defm NEAREST : UnaryFP<fnearbyint, "nearest">;
38
39} // Defs = [ARGUMENTS]
40
41// DAGCombine oddly folds casts into the rhs of copysign. Unfold them.
42def : Pat<(fcopysign F64:$lhs, F32:$rhs),
43          (COPYSIGN_F64 F64:$lhs, (F64_PROMOTE_F32 F32:$rhs))>;
44def : Pat<(fcopysign F32:$lhs, F64:$rhs),
45          (COPYSIGN_F32 F32:$lhs, (F32_DEMOTE_F64 F64:$rhs))>;
46
47// WebAssembly doesn't expose inexact exceptions, so map frint to fnearbyint.
48def : Pat<(frint f32:$src), (NEAREST_F32 f32:$src)>;
49def : Pat<(frint f64:$src), (NEAREST_F64 f64:$src)>;
50
51let Defs = [ARGUMENTS] in {
52
53let isCommutable = 1 in {
54defm EQ : ComparisonFP<SETOEQ, "eq  ">;
55defm NE : ComparisonFP<SETUNE, "ne  ">;
56} // isCommutable = 1
57defm LT : ComparisonFP<SETOLT, "lt  ">;
58defm LE : ComparisonFP<SETOLE, "le  ">;
59defm GT : ComparisonFP<SETOGT, "gt  ">;
60defm GE : ComparisonFP<SETOGE, "ge  ">;
61
62} // Defs = [ARGUMENTS]
63
64// Don't care floating-point comparisons, supported via other comparisons.
65def : Pat<(seteq f32:$lhs, f32:$rhs), (EQ_F32 f32:$lhs, f32:$rhs)>;
66def : Pat<(setne f32:$lhs, f32:$rhs), (NE_F32 f32:$lhs, f32:$rhs)>;
67def : Pat<(setlt f32:$lhs, f32:$rhs), (LT_F32 f32:$lhs, f32:$rhs)>;
68def : Pat<(setle f32:$lhs, f32:$rhs), (LE_F32 f32:$lhs, f32:$rhs)>;
69def : Pat<(setgt f32:$lhs, f32:$rhs), (GT_F32 f32:$lhs, f32:$rhs)>;
70def : Pat<(setge f32:$lhs, f32:$rhs), (GE_F32 f32:$lhs, f32:$rhs)>;
71def : Pat<(seteq f64:$lhs, f64:$rhs), (EQ_F64 f64:$lhs, f64:$rhs)>;
72def : Pat<(setne f64:$lhs, f64:$rhs), (NE_F64 f64:$lhs, f64:$rhs)>;
73def : Pat<(setlt f64:$lhs, f64:$rhs), (LT_F64 f64:$lhs, f64:$rhs)>;
74def : Pat<(setle f64:$lhs, f64:$rhs), (LE_F64 f64:$lhs, f64:$rhs)>;
75def : Pat<(setgt f64:$lhs, f64:$rhs), (GT_F64 f64:$lhs, f64:$rhs)>;
76def : Pat<(setge f64:$lhs, f64:$rhs), (GE_F64 f64:$lhs, f64:$rhs)>;
77
78let Defs = [ARGUMENTS] in {
79
80def SELECT_F32 : I<(outs F32:$dst), (ins F32:$lhs, F32:$rhs, I32:$cond),
81                   [(set F32:$dst, (select I32:$cond, F32:$lhs, F32:$rhs))],
82                   "f32.select\t$dst, $lhs, $rhs, $cond">;
83def SELECT_F64 : I<(outs F64:$dst), (ins F64:$lhs, F64:$rhs, I32:$cond),
84                   [(set F64:$dst, (select I32:$cond, F64:$lhs, F64:$rhs))],
85                   "f64.select\t$dst, $lhs, $rhs, $cond">;
86
87} // Defs = [ARGUMENTS]
88
89// ISD::SELECT requires its operand to conform to getBooleanContents, but
90// WebAssembly's select interprets any non-zero value as true, so we can fold
91// a setne with 0 into a select.
92def : Pat<(select (i32 (setne I32:$cond, 0)), F32:$lhs, F32:$rhs),
93          (SELECT_F32 F32:$lhs, F32:$rhs, I32:$cond)>;
94def : Pat<(select (i32 (setne I32:$cond, 0)), F64:$lhs, F64:$rhs),
95          (SELECT_F64 F64:$lhs, F64:$rhs, I32:$cond)>;
96
97// And again, this time with seteq instead of setne and the arms reversed.
98def : Pat<(select (i32 (seteq I32:$cond, 0)), F32:$lhs, F32:$rhs),
99          (SELECT_F32 F32:$rhs, F32:$lhs, I32:$cond)>;
100def : Pat<(select (i32 (seteq I32:$cond, 0)), F64:$lhs, F64:$rhs),
101          (SELECT_F64 F64:$rhs, F64:$lhs, I32:$cond)>;
102