| JF Bastien | 5ca0bac | 2015-07-10 18:23:10 +0000 | [diff] [blame] | 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 | 
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 11 | /// WebAssembly Floating-point operand code-gen constructs. | 
| JF Bastien | 5ca0bac | 2015-07-10 18:23:10 +0000 | [diff] [blame] | 12 | /// | 
|  | 13 | //===----------------------------------------------------------------------===// | 
|  | 14 |  | 
| Thomas Lively | 914f0f2 | 2018-08-23 00:36:43 +0000 | [diff] [blame] | 15 | multiclass 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 | } | 
|  | 26 | multiclass 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 | } | 
|  | 37 | multiclass 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 |  | 
| Dan Gohman | 174b2d8 | 2015-11-29 22:59:19 +0000 | [diff] [blame] | 48 | let isCommutable = 1 in | 
| Dan Gohman | c968297 | 2016-10-24 20:21:49 +0000 | [diff] [blame] | 49 | defm ADD : BinaryFP<fadd, "add ", 0x92, 0xa0>; | 
|  | 50 | defm SUB : BinaryFP<fsub, "sub ", 0x93, 0xa1>; | 
| Dan Gohman | 174b2d8 | 2015-11-29 22:59:19 +0000 | [diff] [blame] | 51 | let isCommutable = 1 in | 
| Dan Gohman | c968297 | 2016-10-24 20:21:49 +0000 | [diff] [blame] | 52 | defm MUL : BinaryFP<fmul, "mul ", 0x94, 0xa2>; | 
|  | 53 | defm DIV : BinaryFP<fdiv, "div ", 0x95, 0xa3>; | 
|  | 54 | defm SQRT : UnaryFP<fsqrt, "sqrt", 0x91, 0x9f>; | 
| Dan Gohman | 896e53f | 2015-08-24 18:23:13 +0000 | [diff] [blame] | 55 |  | 
| Dan Gohman | c968297 | 2016-10-24 20:21:49 +0000 | [diff] [blame] | 56 | defm ABS : UnaryFP<fabs, "abs ", 0x8b, 0x99>; | 
|  | 57 | defm NEG : UnaryFP<fneg, "neg ", 0x8c, 0x9a>; | 
|  | 58 | defm COPYSIGN : BinaryFP<fcopysign, "copysign", 0x98, 0xa6>; | 
| JF Bastien | ef172fc | 2015-08-11 02:45:15 +0000 | [diff] [blame] | 59 |  | 
| Dan Gohman | 174b2d8 | 2015-11-29 22:59:19 +0000 | [diff] [blame] | 60 | let isCommutable = 1 in { | 
| Thomas Lively | 30f1d69 | 2018-10-24 22:49:55 +0000 | [diff] [blame] | 61 | defm MIN : BinaryFP<fminimum, "min ", 0x96, 0xa4>; | 
|  | 62 | defm MAX : BinaryFP<fmaximum, "max ", 0x97, 0xa5>; | 
| Dan Gohman | 174b2d8 | 2015-11-29 22:59:19 +0000 | [diff] [blame] | 63 | } // isCommutable = 1 | 
| Dan Gohman | b84ae9b | 2015-11-10 21:40:21 +0000 | [diff] [blame] | 64 |  | 
| Dan Gohman | c968297 | 2016-10-24 20:21:49 +0000 | [diff] [blame] | 65 | defm CEIL : UnaryFP<fceil, "ceil", 0x8d, 0x9b>; | 
|  | 66 | defm FLOOR : UnaryFP<ffloor, "floor", 0x8e, 0x9c>; | 
|  | 67 | defm TRUNC : UnaryFP<ftrunc, "trunc", 0x8f, 0x9d>; | 
|  | 68 | defm NEAREST : UnaryFP<fnearbyint, "nearest", 0x90, 0x9e>; | 
| Dan Gohman | 896e53f | 2015-08-24 18:23:13 +0000 | [diff] [blame] | 69 |  | 
| Dan Gohman | f170ba0 | 2015-12-10 04:55:31 +0000 | [diff] [blame] | 70 | // DAGCombine oddly folds casts into the rhs of copysign. Unfold them. | 
|  | 71 | def : Pat<(fcopysign F64:$lhs, F32:$rhs), | 
|  | 72 | (COPYSIGN_F64 F64:$lhs, (F64_PROMOTE_F32 F32:$rhs))>; | 
|  | 73 | def : Pat<(fcopysign F32:$lhs, F64:$rhs), | 
|  | 74 | (COPYSIGN_F32 F32:$lhs, (F32_DEMOTE_F64 F64:$rhs))>; | 
|  | 75 |  | 
| Dan Gohman | 896e53f | 2015-08-24 18:23:13 +0000 | [diff] [blame] | 76 | // WebAssembly doesn't expose inexact exceptions, so map frint to fnearbyint. | 
| Dan Gohman | d0bf981 | 2015-09-26 01:09:44 +0000 | [diff] [blame] | 77 | def : Pat<(frint f32:$src), (NEAREST_F32 f32:$src)>; | 
|  | 78 | def : Pat<(frint f64:$src), (NEAREST_F64 f64:$src)>; | 
| JF Bastien | d9767a3 | 2015-07-14 21:13:29 +0000 | [diff] [blame] | 79 |  | 
| Dan Gohman | 174b2d8 | 2015-11-29 22:59:19 +0000 | [diff] [blame] | 80 | let isCommutable = 1 in { | 
| Dan Gohman | c968297 | 2016-10-24 20:21:49 +0000 | [diff] [blame] | 81 | defm EQ : ComparisonFP<SETOEQ, "eq  ", 0x5b, 0x61>; | 
|  | 82 | defm NE : ComparisonFP<SETUNE, "ne  ", 0x5c, 0x62>; | 
| Dan Gohman | 174b2d8 | 2015-11-29 22:59:19 +0000 | [diff] [blame] | 83 | } // isCommutable = 1 | 
| Dan Gohman | c968297 | 2016-10-24 20:21:49 +0000 | [diff] [blame] | 84 | defm LT : ComparisonFP<SETOLT, "lt  ", 0x5d, 0x63>; | 
| Dan Gohman | 3a74cfe | 2017-03-09 23:08:21 +0000 | [diff] [blame] | 85 | defm LE : ComparisonFP<SETOLE, "le  ", 0x5f, 0x65>; | 
|  | 86 | defm GT : ComparisonFP<SETOGT, "gt  ", 0x5e, 0x64>; | 
| Dan Gohman | c968297 | 2016-10-24 20:21:49 +0000 | [diff] [blame] | 87 | defm GE : ComparisonFP<SETOGE, "ge  ", 0x60, 0x66>; | 
| JF Bastien | d9767a3 | 2015-07-14 21:13:29 +0000 | [diff] [blame] | 88 |  | 
| JF Bastien | 71d29ac | 2015-08-12 17:53:29 +0000 | [diff] [blame] | 89 | // Don't care floating-point comparisons, supported via other comparisons. | 
|  | 90 | def : Pat<(seteq f32:$lhs, f32:$rhs), (EQ_F32 f32:$lhs, f32:$rhs)>; | 
|  | 91 | def : Pat<(setne f32:$lhs, f32:$rhs), (NE_F32 f32:$lhs, f32:$rhs)>; | 
|  | 92 | def : Pat<(setlt f32:$lhs, f32:$rhs), (LT_F32 f32:$lhs, f32:$rhs)>; | 
|  | 93 | def : Pat<(setle f32:$lhs, f32:$rhs), (LE_F32 f32:$lhs, f32:$rhs)>; | 
|  | 94 | def : Pat<(setgt f32:$lhs, f32:$rhs), (GT_F32 f32:$lhs, f32:$rhs)>; | 
|  | 95 | def : Pat<(setge f32:$lhs, f32:$rhs), (GE_F32 f32:$lhs, f32:$rhs)>; | 
|  | 96 | def : Pat<(seteq f64:$lhs, f64:$rhs), (EQ_F64 f64:$lhs, f64:$rhs)>; | 
|  | 97 | def : Pat<(setne f64:$lhs, f64:$rhs), (NE_F64 f64:$lhs, f64:$rhs)>; | 
|  | 98 | def : Pat<(setlt f64:$lhs, f64:$rhs), (LT_F64 f64:$lhs, f64:$rhs)>; | 
|  | 99 | def : Pat<(setle f64:$lhs, f64:$rhs), (LE_F64 f64:$lhs, f64:$rhs)>; | 
|  | 100 | def : Pat<(setgt f64:$lhs, f64:$rhs), (GT_F64 f64:$lhs, f64:$rhs)>; | 
|  | 101 | def : Pat<(setge f64:$lhs, f64:$rhs), (GE_F64 f64:$lhs, f64:$rhs)>; | 
|  | 102 |  | 
| Wouter van Oortmerssen | 48dac31 | 2018-06-18 21:22:44 +0000 | [diff] [blame] | 103 | defm SELECT_F32 : I<(outs F32:$dst), (ins F32:$lhs, F32:$rhs, I32:$cond), | 
|  | 104 | (outs), (ins), | 
|  | 105 | [(set F32:$dst, (select I32:$cond, F32:$lhs, F32:$rhs))], | 
|  | 106 | "f32.select\t$dst, $lhs, $rhs, $cond", "f32.select", 0x1b>; | 
|  | 107 | defm SELECT_F64 : I<(outs F64:$dst), (ins F64:$lhs, F64:$rhs, I32:$cond), | 
|  | 108 | (outs), (ins), | 
|  | 109 | [(set F64:$dst, (select I32:$cond, F64:$lhs, F64:$rhs))], | 
|  | 110 | "f64.select\t$dst, $lhs, $rhs, $cond", "f64.select", 0x1b>; | 
| Dan Gohman | fb3e059 | 2015-11-25 19:36:19 +0000 | [diff] [blame] | 111 |  | 
| Dan Gohman | d9b4218 | 2015-11-25 22:13:48 +0000 | [diff] [blame] | 112 | // ISD::SELECT requires its operand to conform to getBooleanContents, but | 
|  | 113 | // WebAssembly's select interprets any non-zero value as true, so we can fold | 
|  | 114 | // a setne with 0 into a select. | 
|  | 115 | def : Pat<(select (i32 (setne I32:$cond, 0)), F32:$lhs, F32:$rhs), | 
| Dan Gohman | d46b092 | 2016-02-05 17:14:59 +0000 | [diff] [blame] | 116 | (SELECT_F32 F32:$lhs, F32:$rhs, I32:$cond)>; | 
| Dan Gohman | d9b4218 | 2015-11-25 22:13:48 +0000 | [diff] [blame] | 117 | def : Pat<(select (i32 (setne I32:$cond, 0)), F64:$lhs, F64:$rhs), | 
| Dan Gohman | d46b092 | 2016-02-05 17:14:59 +0000 | [diff] [blame] | 118 | (SELECT_F64 F64:$lhs, F64:$rhs, I32:$cond)>; | 
| Dan Gohman | d9b4218 | 2015-11-25 22:13:48 +0000 | [diff] [blame] | 119 |  | 
|  | 120 | // And again, this time with seteq instead of setne and the arms reversed. | 
|  | 121 | def : Pat<(select (i32 (seteq I32:$cond, 0)), F32:$lhs, F32:$rhs), | 
| Dan Gohman | d46b092 | 2016-02-05 17:14:59 +0000 | [diff] [blame] | 122 | (SELECT_F32 F32:$rhs, F32:$lhs, I32:$cond)>; | 
| Dan Gohman | d9b4218 | 2015-11-25 22:13:48 +0000 | [diff] [blame] | 123 | def : Pat<(select (i32 (seteq I32:$cond, 0)), F64:$lhs, F64:$rhs), | 
| Dan Gohman | d46b092 | 2016-02-05 17:14:59 +0000 | [diff] [blame] | 124 | (SELECT_F64 F64:$rhs, F64:$lhs, I32:$cond)>; | 
| Thomas Lively | eb15d00 | 2018-10-29 18:38:12 +0000 | [diff] [blame] | 125 |  | 
|  | 126 | // The legalizer inserts an unnecessary `and 1` to make input conform | 
|  | 127 | // to getBooleanContents, which we can lower away. | 
|  | 128 | def : Pat<(select (i32 (and I32:$cond, 1)), F32:$lhs, F32:$rhs), | 
|  | 129 | (SELECT_F32 F32:$lhs, F32:$rhs, I32:$cond)>; | 
|  | 130 | def : Pat<(select (i32 (and I32:$cond, 1)), F64:$lhs, F64:$rhs), | 
|  | 131 | (SELECT_F64 F64:$lhs, F64:$rhs, I32:$cond)>; |