[WebAssembly] Factor out a TypeToString function, since we need it in multiple places.
[oota-llvm.git] / lib / Target / WebAssembly / WebAssemblyInstrFloat.td
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
15 let Defs = [ARGUMENTS] in {
16
17 let isCommutable = 1 in
18 defm ADD : BinaryFP<fadd, "add ">;
19 defm SUB : BinaryFP<fsub, "sub ">;
20 let isCommutable = 1 in
21 defm MUL : BinaryFP<fmul, "mul ">;
22 defm DIV : BinaryFP<fdiv, "div ">;
23 defm SQRT : UnaryFP<fsqrt, "sqrt">;
24
25 defm ABS : UnaryFP<fabs, "abs ">;
26 defm NEG : UnaryFP<fneg, "neg ">;
27 defm COPYSIGN : BinaryFP<fcopysign, "copysign">;
28
29 let isCommutable = 1 in {
30 defm MIN : BinaryFP<fminnan, "min ">;
31 defm MAX : BinaryFP<fmaxnan, "max ">;
32 } // isCommutable = 1
33
34 defm CEIL : UnaryFP<fceil, "ceil">;
35 defm FLOOR : UnaryFP<ffloor, "floor">;
36 defm TRUNC : UnaryFP<ftrunc, "trunc">;
37 defm NEAREST : UnaryFP<fnearbyint, "nearest">;
38
39 } // Defs = [ARGUMENTS]
40
41 // WebAssembly doesn't expose inexact exceptions, so map frint to fnearbyint.
42 def : Pat<(frint f32:$src), (NEAREST_F32 f32:$src)>;
43 def : Pat<(frint f64:$src), (NEAREST_F64 f64:$src)>;
44
45 let Defs = [ARGUMENTS] in {
46
47 let isCommutable = 1 in {
48 defm EQ : ComparisonFP<SETOEQ, "eq  ">;
49 defm NE : ComparisonFP<SETUNE, "ne  ">;
50 } // isCommutable = 1
51 defm LT : ComparisonFP<SETOLT, "lt  ">;
52 defm LE : ComparisonFP<SETOLE, "le  ">;
53 defm GT : ComparisonFP<SETOGT, "gt  ">;
54 defm GE : ComparisonFP<SETOGE, "ge  ">;
55
56 } // Defs = [ARGUMENTS]
57
58 // Don't care floating-point comparisons, supported via other comparisons.
59 def : Pat<(seteq f32:$lhs, f32:$rhs), (EQ_F32 f32:$lhs, f32:$rhs)>;
60 def : Pat<(setne f32:$lhs, f32:$rhs), (NE_F32 f32:$lhs, f32:$rhs)>;
61 def : Pat<(setlt f32:$lhs, f32:$rhs), (LT_F32 f32:$lhs, f32:$rhs)>;
62 def : Pat<(setle f32:$lhs, f32:$rhs), (LE_F32 f32:$lhs, f32:$rhs)>;
63 def : Pat<(setgt f32:$lhs, f32:$rhs), (GT_F32 f32:$lhs, f32:$rhs)>;
64 def : Pat<(setge f32:$lhs, f32:$rhs), (GE_F32 f32:$lhs, f32:$rhs)>;
65 def : Pat<(seteq f64:$lhs, f64:$rhs), (EQ_F64 f64:$lhs, f64:$rhs)>;
66 def : Pat<(setne f64:$lhs, f64:$rhs), (NE_F64 f64:$lhs, f64:$rhs)>;
67 def : Pat<(setlt f64:$lhs, f64:$rhs), (LT_F64 f64:$lhs, f64:$rhs)>;
68 def : Pat<(setle f64:$lhs, f64:$rhs), (LE_F64 f64:$lhs, f64:$rhs)>;
69 def : Pat<(setgt f64:$lhs, f64:$rhs), (GT_F64 f64:$lhs, f64:$rhs)>;
70 def : Pat<(setge f64:$lhs, f64:$rhs), (GE_F64 f64:$lhs, f64:$rhs)>;
71
72 let Defs = [ARGUMENTS] in {
73
74 def SELECT_F32 : I<(outs F32:$dst), (ins I32:$cond, F32:$lhs, F32:$rhs),
75                    [(set F32:$dst, (select I32:$cond, F32:$lhs, F32:$rhs))],
76                    "f32.select\t$dst, $cond, $lhs, $rhs">;
77 def SELECT_F64 : I<(outs F64:$dst), (ins I32:$cond, F64:$lhs, F64:$rhs),
78                    [(set F64:$dst, (select I32:$cond, F64:$lhs, F64:$rhs))],
79                    "f64.select\t$dst, $cond, $lhs, $rhs">;
80
81 } // Defs = [ARGUMENTS]
82
83 // ISD::SELECT requires its operand to conform to getBooleanContents, but
84 // WebAssembly's select interprets any non-zero value as true, so we can fold
85 // a setne with 0 into a select.
86 def : Pat<(select (i32 (setne I32:$cond, 0)), F32:$lhs, F32:$rhs),
87           (SELECT_F32 I32:$cond, F32:$lhs, F32:$rhs)>;
88 def : Pat<(select (i32 (setne I32:$cond, 0)), F64:$lhs, F64:$rhs),
89           (SELECT_F64 I32:$cond, F64:$lhs, F64:$rhs)>;
90
91 // And again, this time with seteq instead of setne and the arms reversed.
92 def : Pat<(select (i32 (seteq I32:$cond, 0)), F32:$lhs, F32:$rhs),
93           (SELECT_F32 I32:$cond, F32:$rhs, F32:$lhs)>;
94 def : Pat<(select (i32 (seteq I32:$cond, 0)), F64:$lhs, F64:$rhs),
95           (SELECT_F64 I32:$cond, F64:$rhs, F64:$lhs)>;