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