1*0b57cec5SDimitry Andric// WebAssemblyInstrInteger.td-WebAssembly Integer codegen -------*- tablegen -*- 2*0b57cec5SDimitry Andric// 3*0b57cec5SDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric// See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric// 7*0b57cec5SDimitry Andric//===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric/// 9*0b57cec5SDimitry Andric/// \file 10*0b57cec5SDimitry Andric/// WebAssembly Integer operand code-gen constructs. 11*0b57cec5SDimitry Andric/// 12*0b57cec5SDimitry Andric//===----------------------------------------------------------------------===// 13*0b57cec5SDimitry Andric 14*0b57cec5SDimitry Andricmulticlass UnaryInt<SDNode node, string name, bits<32> i32Inst, 15*0b57cec5SDimitry Andric bits<32> i64Inst> { 16*0b57cec5SDimitry Andric defm _I32 : I<(outs I32:$dst), (ins I32:$src), (outs), (ins), 17*0b57cec5SDimitry Andric [(set I32:$dst, (node I32:$src))], 18*0b57cec5SDimitry Andric !strconcat("i32.", !strconcat(name, "\t$dst, $src")), 19*0b57cec5SDimitry Andric !strconcat("i32.", name), i32Inst>; 20*0b57cec5SDimitry Andric defm _I64 : I<(outs I64:$dst), (ins I64:$src), (outs), (ins), 21*0b57cec5SDimitry Andric [(set I64:$dst, (node I64:$src))], 22*0b57cec5SDimitry Andric !strconcat("i64.", !strconcat(name, "\t$dst, $src")), 23*0b57cec5SDimitry Andric !strconcat("i64.", name), i64Inst>; 24*0b57cec5SDimitry Andric} 25*0b57cec5SDimitry Andricmulticlass BinaryInt<SDNode node, string name, bits<32> i32Inst, 26*0b57cec5SDimitry Andric bits<32> i64Inst> { 27*0b57cec5SDimitry Andric defm _I32 : I<(outs I32:$dst), (ins I32:$lhs, I32:$rhs), (outs), (ins), 28*0b57cec5SDimitry Andric [(set I32:$dst, (node I32:$lhs, I32:$rhs))], 29*0b57cec5SDimitry Andric !strconcat("i32.", !strconcat(name, "\t$dst, $lhs, $rhs")), 30*0b57cec5SDimitry Andric !strconcat("i32.", name), i32Inst>; 31*0b57cec5SDimitry Andric defm _I64 : I<(outs I64:$dst), (ins I64:$lhs, I64:$rhs), (outs), (ins), 32*0b57cec5SDimitry Andric [(set I64:$dst, (node I64:$lhs, I64:$rhs))], 33*0b57cec5SDimitry Andric !strconcat("i64.", !strconcat(name, "\t$dst, $lhs, $rhs")), 34*0b57cec5SDimitry Andric !strconcat("i64.", name), i64Inst>; 35*0b57cec5SDimitry Andric} 36*0b57cec5SDimitry Andricmulticlass ComparisonInt<CondCode cond, string name, bits<32> i32Inst, bits<32> i64Inst> { 37*0b57cec5SDimitry Andric defm _I32 : I<(outs I32:$dst), (ins I32:$lhs, I32:$rhs), (outs), (ins), 38*0b57cec5SDimitry Andric [(set I32:$dst, (setcc I32:$lhs, I32:$rhs, cond))], 39*0b57cec5SDimitry Andric !strconcat("i32.", !strconcat(name, "\t$dst, $lhs, $rhs")), 40*0b57cec5SDimitry Andric !strconcat("i32.", name), i32Inst>; 41*0b57cec5SDimitry Andric defm _I64 : I<(outs I32:$dst), (ins I64:$lhs, I64:$rhs), (outs), (ins), 42*0b57cec5SDimitry Andric [(set I32:$dst, (setcc I64:$lhs, I64:$rhs, cond))], 43*0b57cec5SDimitry Andric !strconcat("i64.", !strconcat(name, "\t$dst, $lhs, $rhs")), 44*0b57cec5SDimitry Andric !strconcat("i64.", name), i64Inst>; 45*0b57cec5SDimitry Andric} 46*0b57cec5SDimitry Andric 47*0b57cec5SDimitry Andric// The spaces after the names are for aesthetic purposes only, to make 48*0b57cec5SDimitry Andric// operands line up vertically after tab expansion. 49*0b57cec5SDimitry Andriclet isCommutable = 1 in 50*0b57cec5SDimitry Andricdefm ADD : BinaryInt<add, "add ", 0x6a, 0x7c>; 51*0b57cec5SDimitry Andricdefm SUB : BinaryInt<sub, "sub ", 0x6b, 0x7d>; 52*0b57cec5SDimitry Andriclet isCommutable = 1 in 53*0b57cec5SDimitry Andricdefm MUL : BinaryInt<mul, "mul ", 0x6c, 0x7e>; 54*0b57cec5SDimitry Andric// Divide and remainder trap on a zero denominator. 55*0b57cec5SDimitry Andriclet hasSideEffects = 1 in { 56*0b57cec5SDimitry Andricdefm DIV_S : BinaryInt<sdiv, "div_s", 0x6d, 0x7f>; 57*0b57cec5SDimitry Andricdefm DIV_U : BinaryInt<udiv, "div_u", 0x6e, 0x80>; 58*0b57cec5SDimitry Andricdefm REM_S : BinaryInt<srem, "rem_s", 0x6f, 0x81>; 59*0b57cec5SDimitry Andricdefm REM_U : BinaryInt<urem, "rem_u", 0x70, 0x82>; 60*0b57cec5SDimitry Andric} // hasSideEffects = 1 61*0b57cec5SDimitry Andriclet isCommutable = 1 in { 62*0b57cec5SDimitry Andricdefm AND : BinaryInt<and, "and ", 0x71, 0x83>; 63*0b57cec5SDimitry Andricdefm OR : BinaryInt<or, "or ", 0x72, 0x84>; 64*0b57cec5SDimitry Andricdefm XOR : BinaryInt<xor, "xor ", 0x73, 0x85>; 65*0b57cec5SDimitry Andric} // isCommutable = 1 66*0b57cec5SDimitry Andricdefm SHL : BinaryInt<shl, "shl ", 0x74, 0x86>; 67*0b57cec5SDimitry Andricdefm SHR_S : BinaryInt<sra, "shr_s", 0x75, 0x87>; 68*0b57cec5SDimitry Andricdefm SHR_U : BinaryInt<srl, "shr_u", 0x76, 0x88>; 69*0b57cec5SDimitry Andricdefm ROTL : BinaryInt<rotl, "rotl", 0x77, 0x89>; 70*0b57cec5SDimitry Andricdefm ROTR : BinaryInt<rotr, "rotr", 0x78, 0x8a>; 71*0b57cec5SDimitry Andric 72*0b57cec5SDimitry Andriclet isCommutable = 1 in { 73*0b57cec5SDimitry Andricdefm EQ : ComparisonInt<SETEQ, "eq ", 0x46, 0x51>; 74*0b57cec5SDimitry Andricdefm NE : ComparisonInt<SETNE, "ne ", 0x47, 0x52>; 75*0b57cec5SDimitry Andric} // isCommutable = 1 76*0b57cec5SDimitry Andricdefm LT_S : ComparisonInt<SETLT, "lt_s", 0x48, 0x53>; 77*0b57cec5SDimitry Andricdefm LT_U : ComparisonInt<SETULT, "lt_u", 0x49, 0x54>; 78*0b57cec5SDimitry Andricdefm GT_S : ComparisonInt<SETGT, "gt_s", 0x4a, 0x55>; 79*0b57cec5SDimitry Andricdefm GT_U : ComparisonInt<SETUGT, "gt_u", 0x4b, 0x56>; 80*0b57cec5SDimitry Andricdefm LE_S : ComparisonInt<SETLE, "le_s", 0x4c, 0x57>; 81*0b57cec5SDimitry Andricdefm LE_U : ComparisonInt<SETULE, "le_u", 0x4d, 0x58>; 82*0b57cec5SDimitry Andricdefm GE_S : ComparisonInt<SETGE, "ge_s", 0x4e, 0x59>; 83*0b57cec5SDimitry Andricdefm GE_U : ComparisonInt<SETUGE, "ge_u", 0x4f, 0x5a>; 84*0b57cec5SDimitry Andric 85*0b57cec5SDimitry Andricdefm CLZ : UnaryInt<ctlz, "clz ", 0x67, 0x79>; 86*0b57cec5SDimitry Andricdefm CTZ : UnaryInt<cttz, "ctz ", 0x68, 0x7a>; 87*0b57cec5SDimitry Andricdefm POPCNT : UnaryInt<ctpop, "popcnt", 0x69, 0x7b>; 88*0b57cec5SDimitry Andric 89*0b57cec5SDimitry Andricdefm EQZ_I32 : I<(outs I32:$dst), (ins I32:$src), (outs), (ins), 90*0b57cec5SDimitry Andric [(set I32:$dst, (setcc I32:$src, 0, SETEQ))], 91*0b57cec5SDimitry Andric "i32.eqz \t$dst, $src", "i32.eqz", 0x45>; 92*0b57cec5SDimitry Andricdefm EQZ_I64 : I<(outs I32:$dst), (ins I64:$src), (outs), (ins), 93*0b57cec5SDimitry Andric [(set I32:$dst, (setcc I64:$src, 0, SETEQ))], 94*0b57cec5SDimitry Andric "i64.eqz \t$dst, $src", "i64.eqz", 0x50>; 95*0b57cec5SDimitry Andric 96*0b57cec5SDimitry Andric// Optimize away an explicit mask on a rotate count. 97*0b57cec5SDimitry Andricdef : Pat<(rotl I32:$lhs, (and I32:$rhs, 31)), (ROTL_I32 I32:$lhs, I32:$rhs)>; 98*0b57cec5SDimitry Andricdef : Pat<(rotr I32:$lhs, (and I32:$rhs, 31)), (ROTR_I32 I32:$lhs, I32:$rhs)>; 99*0b57cec5SDimitry Andricdef : Pat<(rotl I64:$lhs, (and I64:$rhs, 63)), (ROTL_I64 I64:$lhs, I64:$rhs)>; 100*0b57cec5SDimitry Andricdef : Pat<(rotr I64:$lhs, (and I64:$rhs, 63)), (ROTR_I64 I64:$lhs, I64:$rhs)>; 101*0b57cec5SDimitry Andric 102*0b57cec5SDimitry Andricdefm SELECT_I32 : I<(outs I32:$dst), (ins I32:$lhs, I32:$rhs, I32:$cond), 103*0b57cec5SDimitry Andric (outs), (ins), 104*0b57cec5SDimitry Andric [(set I32:$dst, (select I32:$cond, I32:$lhs, I32:$rhs))], 105*0b57cec5SDimitry Andric "i32.select\t$dst, $lhs, $rhs, $cond", "i32.select", 0x1b>; 106*0b57cec5SDimitry Andricdefm SELECT_I64 : I<(outs I64:$dst), (ins I64:$lhs, I64:$rhs, I32:$cond), 107*0b57cec5SDimitry Andric (outs), (ins), 108*0b57cec5SDimitry Andric [(set I64:$dst, (select I32:$cond, I64:$lhs, I64:$rhs))], 109*0b57cec5SDimitry Andric "i64.select\t$dst, $lhs, $rhs, $cond", "i64.select", 0x1b>; 110*0b57cec5SDimitry Andric 111*0b57cec5SDimitry Andric// ISD::SELECT requires its operand to conform to getBooleanContents, but 112*0b57cec5SDimitry Andric// WebAssembly's select interprets any non-zero value as true, so we can fold 113*0b57cec5SDimitry Andric// a setne with 0 into a select. 114*0b57cec5SDimitry Andricdef : Pat<(select (i32 (setne I32:$cond, 0)), I32:$lhs, I32:$rhs), 115*0b57cec5SDimitry Andric (SELECT_I32 I32:$lhs, I32:$rhs, I32:$cond)>; 116*0b57cec5SDimitry Andricdef : Pat<(select (i32 (setne I32:$cond, 0)), I64:$lhs, I64:$rhs), 117*0b57cec5SDimitry Andric (SELECT_I64 I64:$lhs, I64:$rhs, I32:$cond)>; 118*0b57cec5SDimitry Andric 119*0b57cec5SDimitry Andric// And again, this time with seteq instead of setne and the arms reversed. 120*0b57cec5SDimitry Andricdef : Pat<(select (i32 (seteq I32:$cond, 0)), I32:$lhs, I32:$rhs), 121*0b57cec5SDimitry Andric (SELECT_I32 I32:$rhs, I32:$lhs, I32:$cond)>; 122*0b57cec5SDimitry Andricdef : Pat<(select (i32 (seteq I32:$cond, 0)), I64:$lhs, I64:$rhs), 123*0b57cec5SDimitry Andric (SELECT_I64 I64:$rhs, I64:$lhs, I32:$cond)>; 124