15ca0bacaSJF Bastien// WebAssemblyInstrFloat.td-WebAssembly Float codegen support ---*- tablegen -*-
25ca0bacaSJF Bastien//
3*2946cd70SChandler Carruth// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*2946cd70SChandler Carruth// See https://llvm.org/LICENSE.txt for license information.
5*2946cd70SChandler Carruth// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65ca0bacaSJF Bastien//
75ca0bacaSJF Bastien//===----------------------------------------------------------------------===//
85ca0bacaSJF Bastien///
95ca0bacaSJF Bastien/// \file
105f8f34e4SAdrian Prantl/// WebAssembly Floating-point operand code-gen constructs.
115ca0bacaSJF Bastien///
125ca0bacaSJF Bastien//===----------------------------------------------------------------------===//
135ca0bacaSJF Bastien
14914f0f20SThomas Livelymulticlass UnaryFP<SDNode node, string name, bits<32> f32Inst,
15914f0f20SThomas Lively                   bits<32> f64Inst> {
16914f0f20SThomas Lively  defm _F32 : I<(outs F32:$dst), (ins F32:$src), (outs), (ins),
17914f0f20SThomas Lively                [(set F32:$dst, (node F32:$src))],
18914f0f20SThomas Lively                !strconcat("f32.", !strconcat(name, "\t$dst, $src")),
19914f0f20SThomas Lively                !strconcat("f32.", name), f32Inst>;
20914f0f20SThomas Lively  defm _F64 : I<(outs F64:$dst), (ins F64:$src), (outs), (ins),
21914f0f20SThomas Lively                [(set F64:$dst, (node F64:$src))],
22914f0f20SThomas Lively                !strconcat("f64.", !strconcat(name, "\t$dst, $src")),
23914f0f20SThomas Lively                !strconcat("f64.", name), f64Inst>;
24914f0f20SThomas Lively}
25914f0f20SThomas Livelymulticlass BinaryFP<SDNode node, string name, bits<32> f32Inst,
26914f0f20SThomas Lively                    bits<32> f64Inst> {
27914f0f20SThomas Lively  defm _F32 : I<(outs F32:$dst), (ins F32:$lhs, F32:$rhs), (outs), (ins),
28914f0f20SThomas Lively                [(set F32:$dst, (node F32:$lhs, F32:$rhs))],
29914f0f20SThomas Lively                !strconcat("f32.", !strconcat(name, "\t$dst, $lhs, $rhs")),
30914f0f20SThomas Lively                !strconcat("f32.", name), f32Inst>;
31914f0f20SThomas Lively  defm _F64 : I<(outs F64:$dst), (ins F64:$lhs, F64:$rhs), (outs), (ins),
32914f0f20SThomas Lively                [(set F64:$dst, (node F64:$lhs, F64:$rhs))],
33914f0f20SThomas Lively                !strconcat("f64.", !strconcat(name, "\t$dst, $lhs, $rhs")),
34914f0f20SThomas Lively                !strconcat("f64.", name), f64Inst>;
35914f0f20SThomas Lively}
36914f0f20SThomas Livelymulticlass ComparisonFP<CondCode cond, string name, bits<32> f32Inst, bits<32> f64Inst> {
37914f0f20SThomas Lively  defm _F32 : I<(outs I32:$dst), (ins F32:$lhs, F32:$rhs), (outs), (ins),
38914f0f20SThomas Lively                [(set I32:$dst, (setcc F32:$lhs, F32:$rhs, cond))],
39914f0f20SThomas Lively                !strconcat("f32.", !strconcat(name, "\t$dst, $lhs, $rhs")),
40914f0f20SThomas Lively                !strconcat("f32.", name), f32Inst>;
41914f0f20SThomas Lively  defm  _F64 : I<(outs I32:$dst), (ins F64:$lhs, F64:$rhs), (outs), (ins),
42914f0f20SThomas Lively                [(set I32:$dst, (setcc F64:$lhs, F64:$rhs, cond))],
43914f0f20SThomas Lively                !strconcat("f64.", !strconcat(name, "\t$dst, $lhs, $rhs")),
44914f0f20SThomas Lively                !strconcat("f64.", name), f64Inst>;
45914f0f20SThomas Lively}
46914f0f20SThomas Lively
47174b2d83SDan Gohmanlet isCommutable = 1 in
48c968297bSDan Gohmandefm ADD : BinaryFP<fadd, "add ", 0x92, 0xa0>;
49c968297bSDan Gohmandefm SUB : BinaryFP<fsub, "sub ", 0x93, 0xa1>;
50174b2d83SDan Gohmanlet isCommutable = 1 in
51c968297bSDan Gohmandefm MUL : BinaryFP<fmul, "mul ", 0x94, 0xa2>;
52c968297bSDan Gohmandefm DIV : BinaryFP<fdiv, "div ", 0x95, 0xa3>;
53c968297bSDan Gohmandefm SQRT : UnaryFP<fsqrt, "sqrt", 0x91, 0x9f>;
54896e53faSDan Gohman
55c968297bSDan Gohmandefm ABS : UnaryFP<fabs, "abs ", 0x8b, 0x99>;
56c968297bSDan Gohmandefm NEG : UnaryFP<fneg, "neg ", 0x8c, 0x9a>;
57c968297bSDan Gohmandefm COPYSIGN : BinaryFP<fcopysign, "copysign", 0x98, 0xa6>;
58ef172fc9SJF Bastien
59174b2d83SDan Gohmanlet isCommutable = 1 in {
6030f1d691SThomas Livelydefm MIN : BinaryFP<fminimum, "min ", 0x96, 0xa4>;
6130f1d691SThomas Livelydefm MAX : BinaryFP<fmaximum, "max ", 0x97, 0xa5>;
62174b2d83SDan Gohman} // isCommutable = 1
63b84ae9bbSDan Gohman
64c968297bSDan Gohmandefm CEIL : UnaryFP<fceil, "ceil", 0x8d, 0x9b>;
65c968297bSDan Gohmandefm FLOOR : UnaryFP<ffloor, "floor", 0x8e, 0x9c>;
66c968297bSDan Gohmandefm TRUNC : UnaryFP<ftrunc, "trunc", 0x8f, 0x9d>;
67c968297bSDan Gohmandefm NEAREST : UnaryFP<fnearbyint, "nearest", 0x90, 0x9e>;
68896e53faSDan Gohman
69f170ba08SDan Gohman// DAGCombine oddly folds casts into the rhs of copysign. Unfold them.
70f170ba08SDan Gohmandef : Pat<(fcopysign F64:$lhs, F32:$rhs),
71f170ba08SDan Gohman          (COPYSIGN_F64 F64:$lhs, (F64_PROMOTE_F32 F32:$rhs))>;
72f170ba08SDan Gohmandef : Pat<(fcopysign F32:$lhs, F64:$rhs),
73f170ba08SDan Gohman          (COPYSIGN_F32 F32:$lhs, (F32_DEMOTE_F64 F64:$rhs))>;
74f170ba08SDan Gohman
75896e53faSDan Gohman// WebAssembly doesn't expose inexact exceptions, so map frint to fnearbyint.
76d0bf9812SDan Gohmandef : Pat<(frint f32:$src), (NEAREST_F32 f32:$src)>;
77d0bf9812SDan Gohmandef : Pat<(frint f64:$src), (NEAREST_F64 f64:$src)>;
78d9767a36SJF Bastien
79174b2d83SDan Gohmanlet isCommutable = 1 in {
80c968297bSDan Gohmandefm EQ : ComparisonFP<SETOEQ, "eq  ", 0x5b, 0x61>;
81c968297bSDan Gohmandefm NE : ComparisonFP<SETUNE, "ne  ", 0x5c, 0x62>;
82174b2d83SDan Gohman} // isCommutable = 1
83c968297bSDan Gohmandefm LT : ComparisonFP<SETOLT, "lt  ", 0x5d, 0x63>;
843a74cfecSDan Gohmandefm LE : ComparisonFP<SETOLE, "le  ", 0x5f, 0x65>;
853a74cfecSDan Gohmandefm GT : ComparisonFP<SETOGT, "gt  ", 0x5e, 0x64>;
86c968297bSDan Gohmandefm GE : ComparisonFP<SETOGE, "ge  ", 0x60, 0x66>;
87d9767a36SJF Bastien
8871d29aceSJF Bastien// Don't care floating-point comparisons, supported via other comparisons.
8971d29aceSJF Bastiendef : Pat<(seteq f32:$lhs, f32:$rhs), (EQ_F32 f32:$lhs, f32:$rhs)>;
9071d29aceSJF Bastiendef : Pat<(setne f32:$lhs, f32:$rhs), (NE_F32 f32:$lhs, f32:$rhs)>;
9171d29aceSJF Bastiendef : Pat<(setlt f32:$lhs, f32:$rhs), (LT_F32 f32:$lhs, f32:$rhs)>;
9271d29aceSJF Bastiendef : Pat<(setle f32:$lhs, f32:$rhs), (LE_F32 f32:$lhs, f32:$rhs)>;
9371d29aceSJF Bastiendef : Pat<(setgt f32:$lhs, f32:$rhs), (GT_F32 f32:$lhs, f32:$rhs)>;
9471d29aceSJF Bastiendef : Pat<(setge f32:$lhs, f32:$rhs), (GE_F32 f32:$lhs, f32:$rhs)>;
9571d29aceSJF Bastiendef : Pat<(seteq f64:$lhs, f64:$rhs), (EQ_F64 f64:$lhs, f64:$rhs)>;
9671d29aceSJF Bastiendef : Pat<(setne f64:$lhs, f64:$rhs), (NE_F64 f64:$lhs, f64:$rhs)>;
9771d29aceSJF Bastiendef : Pat<(setlt f64:$lhs, f64:$rhs), (LT_F64 f64:$lhs, f64:$rhs)>;
9871d29aceSJF Bastiendef : Pat<(setle f64:$lhs, f64:$rhs), (LE_F64 f64:$lhs, f64:$rhs)>;
9971d29aceSJF Bastiendef : Pat<(setgt f64:$lhs, f64:$rhs), (GT_F64 f64:$lhs, f64:$rhs)>;
10071d29aceSJF Bastiendef : Pat<(setge f64:$lhs, f64:$rhs), (GE_F64 f64:$lhs, f64:$rhs)>;
10171d29aceSJF Bastien
10248dac310SWouter van Oortmerssendefm SELECT_F32 : I<(outs F32:$dst), (ins F32:$lhs, F32:$rhs, I32:$cond),
10348dac310SWouter van Oortmerssen                    (outs), (ins),
104af29bd4fSDan Gohman                    [(set F32:$dst, (select I32:$cond, F32:$lhs, F32:$rhs))],
10548dac310SWouter van Oortmerssen                    "f32.select\t$dst, $lhs, $rhs, $cond", "f32.select", 0x1b>;
10648dac310SWouter van Oortmerssendefm SELECT_F64 : I<(outs F64:$dst), (ins F64:$lhs, F64:$rhs, I32:$cond),
10748dac310SWouter van Oortmerssen                    (outs), (ins),
108af29bd4fSDan Gohman                    [(set F64:$dst, (select I32:$cond, F64:$lhs, F64:$rhs))],
10948dac310SWouter van Oortmerssen                    "f64.select\t$dst, $lhs, $rhs, $cond", "f64.select", 0x1b>;
110fb3e0594SDan Gohman
111d9b42188SDan Gohman// ISD::SELECT requires its operand to conform to getBooleanContents, but
112d9b42188SDan Gohman// WebAssembly's select interprets any non-zero value as true, so we can fold
113d9b42188SDan Gohman// a setne with 0 into a select.
114d9b42188SDan Gohmandef : Pat<(select (i32 (setne I32:$cond, 0)), F32:$lhs, F32:$rhs),
115d46b0926SDan Gohman          (SELECT_F32 F32:$lhs, F32:$rhs, I32:$cond)>;
116d9b42188SDan Gohmandef : Pat<(select (i32 (setne I32:$cond, 0)), F64:$lhs, F64:$rhs),
117d46b0926SDan Gohman          (SELECT_F64 F64:$lhs, F64:$rhs, I32:$cond)>;
118d9b42188SDan Gohman
119d9b42188SDan Gohman// And again, this time with seteq instead of setne and the arms reversed.
120d9b42188SDan Gohmandef : Pat<(select (i32 (seteq I32:$cond, 0)), F32:$lhs, F32:$rhs),
121d46b0926SDan Gohman          (SELECT_F32 F32:$rhs, F32:$lhs, I32:$cond)>;
122d9b42188SDan Gohmandef : Pat<(select (i32 (seteq I32:$cond, 0)), F64:$lhs, F64:$rhs),
123d46b0926SDan Gohman          (SELECT_F64 F64:$rhs, F64:$lhs, I32:$cond)>;
124