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