1286425Sdim// WebAssemblyInstrFloat.td-WebAssembly Float codegen support ---*- tablegen -*-
2286425Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6286425Sdim//
7286425Sdim//===----------------------------------------------------------------------===//
8286425Sdim///
9286425Sdim/// \file
10341825Sdim/// WebAssembly Floating-point operand code-gen constructs.
11286425Sdim///
12286425Sdim//===----------------------------------------------------------------------===//
13286425Sdim
14344779Sdimmulticlass UnaryFP<SDNode node, string name, bits<32> f32Inst,
15344779Sdim                   bits<32> f64Inst> {
16344779Sdim  defm _F32 : I<(outs F32:$dst), (ins F32:$src), (outs), (ins),
17344779Sdim                [(set F32:$dst, (node F32:$src))],
18344779Sdim                !strconcat("f32.", !strconcat(name, "\t$dst, $src")),
19344779Sdim                !strconcat("f32.", name), f32Inst>;
20344779Sdim  defm _F64 : I<(outs F64:$dst), (ins F64:$src), (outs), (ins),
21344779Sdim                [(set F64:$dst, (node F64:$src))],
22344779Sdim                !strconcat("f64.", !strconcat(name, "\t$dst, $src")),
23344779Sdim                !strconcat("f64.", name), f64Inst>;
24344779Sdim}
25344779Sdimmulticlass BinaryFP<SDNode node, string name, bits<32> f32Inst,
26344779Sdim                    bits<32> f64Inst> {
27344779Sdim  defm _F32 : I<(outs F32:$dst), (ins F32:$lhs, F32:$rhs), (outs), (ins),
28344779Sdim                [(set F32:$dst, (node F32:$lhs, F32:$rhs))],
29344779Sdim                !strconcat("f32.", !strconcat(name, "\t$dst, $lhs, $rhs")),
30344779Sdim                !strconcat("f32.", name), f32Inst>;
31344779Sdim  defm _F64 : I<(outs F64:$dst), (ins F64:$lhs, F64:$rhs), (outs), (ins),
32344779Sdim                [(set F64:$dst, (node F64:$lhs, F64:$rhs))],
33344779Sdim                !strconcat("f64.", !strconcat(name, "\t$dst, $lhs, $rhs")),
34344779Sdim                !strconcat("f64.", name), f64Inst>;
35344779Sdim}
36344779Sdimmulticlass ComparisonFP<CondCode cond, string name, bits<32> f32Inst, bits<32> f64Inst> {
37344779Sdim  defm _F32 : I<(outs I32:$dst), (ins F32:$lhs, F32:$rhs), (outs), (ins),
38344779Sdim                [(set I32:$dst, (setcc F32:$lhs, F32:$rhs, cond))],
39344779Sdim                !strconcat("f32.", !strconcat(name, "\t$dst, $lhs, $rhs")),
40344779Sdim                !strconcat("f32.", name), f32Inst>;
41344779Sdim  defm  _F64 : I<(outs I32:$dst), (ins F64:$lhs, F64:$rhs), (outs), (ins),
42344779Sdim                [(set I32:$dst, (setcc F64:$lhs, F64:$rhs, cond))],
43344779Sdim                !strconcat("f64.", !strconcat(name, "\t$dst, $lhs, $rhs")),
44344779Sdim                !strconcat("f64.", name), f64Inst>;
45344779Sdim}
46286425Sdim
47296417Sdimlet isCommutable = 1 in
48314564Sdimdefm ADD : BinaryFP<fadd, "add ", 0x92, 0xa0>;
49314564Sdimdefm SUB : BinaryFP<fsub, "sub ", 0x93, 0xa1>;
50296417Sdimlet isCommutable = 1 in
51314564Sdimdefm MUL : BinaryFP<fmul, "mul ", 0x94, 0xa2>;
52314564Sdimdefm DIV : BinaryFP<fdiv, "div ", 0x95, 0xa3>;
53314564Sdimdefm SQRT : UnaryFP<fsqrt, "sqrt", 0x91, 0x9f>;
54286425Sdim
55314564Sdimdefm ABS : UnaryFP<fabs, "abs ", 0x8b, 0x99>;
56314564Sdimdefm NEG : UnaryFP<fneg, "neg ", 0x8c, 0x9a>;
57314564Sdimdefm COPYSIGN : BinaryFP<fcopysign, "copysign", 0x98, 0xa6>;
58286425Sdim
59296417Sdimlet isCommutable = 1 in {
60344779Sdimdefm MIN : BinaryFP<fminimum, "min ", 0x96, 0xa4>;
61344779Sdimdefm MAX : BinaryFP<fmaximum, "max ", 0x97, 0xa5>;
62296417Sdim} // isCommutable = 1
63296417Sdim
64314564Sdimdefm CEIL : UnaryFP<fceil, "ceil", 0x8d, 0x9b>;
65314564Sdimdefm FLOOR : UnaryFP<ffloor, "floor", 0x8e, 0x9c>;
66314564Sdimdefm TRUNC : UnaryFP<ftrunc, "trunc", 0x8f, 0x9d>;
67314564Sdimdefm NEAREST : UnaryFP<fnearbyint, "nearest", 0x90, 0x9e>;
68296417Sdim
69296417Sdim// DAGCombine oddly folds casts into the rhs of copysign. Unfold them.
70296417Sdimdef : Pat<(fcopysign F64:$lhs, F32:$rhs),
71296417Sdim          (COPYSIGN_F64 F64:$lhs, (F64_PROMOTE_F32 F32:$rhs))>;
72296417Sdimdef : Pat<(fcopysign F32:$lhs, F64:$rhs),
73296417Sdim          (COPYSIGN_F32 F32:$lhs, (F32_DEMOTE_F64 F64:$rhs))>;
74296417Sdim
75296417Sdim// WebAssembly doesn't expose inexact exceptions, so map frint to fnearbyint.
76296417Sdimdef : Pat<(frint f32:$src), (NEAREST_F32 f32:$src)>;
77296417Sdimdef : Pat<(frint f64:$src), (NEAREST_F64 f64:$src)>;
78296417Sdim
79296417Sdimlet isCommutable = 1 in {
80314564Sdimdefm EQ : ComparisonFP<SETOEQ, "eq  ", 0x5b, 0x61>;
81314564Sdimdefm NE : ComparisonFP<SETUNE, "ne  ", 0x5c, 0x62>;
82296417Sdim} // isCommutable = 1
83314564Sdimdefm LT : ComparisonFP<SETOLT, "lt  ", 0x5d, 0x63>;
84321369Sdimdefm LE : ComparisonFP<SETOLE, "le  ", 0x5f, 0x65>;
85321369Sdimdefm GT : ComparisonFP<SETOGT, "gt  ", 0x5e, 0x64>;
86314564Sdimdefm GE : ComparisonFP<SETOGE, "ge  ", 0x60, 0x66>;
87296417Sdim
88296417Sdim// Don't care floating-point comparisons, supported via other comparisons.
89296417Sdimdef : Pat<(seteq f32:$lhs, f32:$rhs), (EQ_F32 f32:$lhs, f32:$rhs)>;
90296417Sdimdef : Pat<(setne f32:$lhs, f32:$rhs), (NE_F32 f32:$lhs, f32:$rhs)>;
91296417Sdimdef : Pat<(setlt f32:$lhs, f32:$rhs), (LT_F32 f32:$lhs, f32:$rhs)>;
92296417Sdimdef : Pat<(setle f32:$lhs, f32:$rhs), (LE_F32 f32:$lhs, f32:$rhs)>;
93296417Sdimdef : Pat<(setgt f32:$lhs, f32:$rhs), (GT_F32 f32:$lhs, f32:$rhs)>;
94296417Sdimdef : Pat<(setge f32:$lhs, f32:$rhs), (GE_F32 f32:$lhs, f32:$rhs)>;
95296417Sdimdef : Pat<(seteq f64:$lhs, f64:$rhs), (EQ_F64 f64:$lhs, f64:$rhs)>;
96296417Sdimdef : Pat<(setne f64:$lhs, f64:$rhs), (NE_F64 f64:$lhs, f64:$rhs)>;
97296417Sdimdef : Pat<(setlt f64:$lhs, f64:$rhs), (LT_F64 f64:$lhs, f64:$rhs)>;
98296417Sdimdef : Pat<(setle f64:$lhs, f64:$rhs), (LE_F64 f64:$lhs, f64:$rhs)>;
99296417Sdimdef : Pat<(setgt f64:$lhs, f64:$rhs), (GT_F64 f64:$lhs, f64:$rhs)>;
100296417Sdimdef : Pat<(setge f64:$lhs, f64:$rhs), (GE_F64 f64:$lhs, f64:$rhs)>;
101296417Sdim
102341825Sdimdefm SELECT_F32 : I<(outs F32:$dst), (ins F32:$lhs, F32:$rhs, I32:$cond),
103341825Sdim                    (outs), (ins),
104341825Sdim                    [(set F32:$dst, (select I32:$cond, F32:$lhs, F32:$rhs))],
105341825Sdim                    "f32.select\t$dst, $lhs, $rhs, $cond", "f32.select", 0x1b>;
106341825Sdimdefm SELECT_F64 : I<(outs F64:$dst), (ins F64:$lhs, F64:$rhs, I32:$cond),
107341825Sdim                    (outs), (ins),
108341825Sdim                    [(set F64:$dst, (select I32:$cond, F64:$lhs, F64:$rhs))],
109341825Sdim                    "f64.select\t$dst, $lhs, $rhs, $cond", "f64.select", 0x1b>;
110296417Sdim
111296417Sdim// ISD::SELECT requires its operand to conform to getBooleanContents, but
112296417Sdim// WebAssembly's select interprets any non-zero value as true, so we can fold
113296417Sdim// a setne with 0 into a select.
114296417Sdimdef : Pat<(select (i32 (setne I32:$cond, 0)), F32:$lhs, F32:$rhs),
115309124Sdim          (SELECT_F32 F32:$lhs, F32:$rhs, I32:$cond)>;
116296417Sdimdef : Pat<(select (i32 (setne I32:$cond, 0)), F64:$lhs, F64:$rhs),
117309124Sdim          (SELECT_F64 F64:$lhs, F64:$rhs, I32:$cond)>;
118296417Sdim
119296417Sdim// And again, this time with seteq instead of setne and the arms reversed.
120296417Sdimdef : Pat<(select (i32 (seteq I32:$cond, 0)), F32:$lhs, F32:$rhs),
121309124Sdim          (SELECT_F32 F32:$rhs, F32:$lhs, I32:$cond)>;
122296417Sdimdef : Pat<(select (i32 (seteq I32:$cond, 0)), F64:$lhs, F64:$rhs),
123309124Sdim          (SELECT_F64 F64:$rhs, F64:$lhs, I32:$cond)>;
124