10b57cec5SDimitry Andric// WebAssemblyInstrInteger.td-WebAssembly Integer codegen -------*- tablegen -*- 20b57cec5SDimitry Andric// 30b57cec5SDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric// See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric// 70b57cec5SDimitry Andric//===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric/// 90b57cec5SDimitry Andric/// \file 100b57cec5SDimitry Andric/// WebAssembly Integer operand code-gen constructs. 110b57cec5SDimitry Andric/// 120b57cec5SDimitry Andric//===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric 140b57cec5SDimitry Andricmulticlass UnaryInt<SDNode node, string name, bits<32> i32Inst, 150b57cec5SDimitry Andric bits<32> i64Inst> { 160b57cec5SDimitry Andric defm _I32 : I<(outs I32:$dst), (ins I32:$src), (outs), (ins), 170b57cec5SDimitry Andric [(set I32:$dst, (node I32:$src))], 180b57cec5SDimitry Andric !strconcat("i32.", !strconcat(name, "\t$dst, $src")), 190b57cec5SDimitry Andric !strconcat("i32.", name), i32Inst>; 200b57cec5SDimitry Andric defm _I64 : I<(outs I64:$dst), (ins I64:$src), (outs), (ins), 210b57cec5SDimitry Andric [(set I64:$dst, (node I64:$src))], 220b57cec5SDimitry Andric !strconcat("i64.", !strconcat(name, "\t$dst, $src")), 230b57cec5SDimitry Andric !strconcat("i64.", name), i64Inst>; 240b57cec5SDimitry Andric} 250b57cec5SDimitry Andricmulticlass BinaryInt<SDNode node, string name, bits<32> i32Inst, 260b57cec5SDimitry Andric bits<32> i64Inst> { 270b57cec5SDimitry Andric defm _I32 : I<(outs I32:$dst), (ins I32:$lhs, I32:$rhs), (outs), (ins), 280b57cec5SDimitry Andric [(set I32:$dst, (node I32:$lhs, I32:$rhs))], 290b57cec5SDimitry Andric !strconcat("i32.", !strconcat(name, "\t$dst, $lhs, $rhs")), 300b57cec5SDimitry Andric !strconcat("i32.", name), i32Inst>; 310b57cec5SDimitry Andric defm _I64 : I<(outs I64:$dst), (ins I64:$lhs, I64:$rhs), (outs), (ins), 320b57cec5SDimitry Andric [(set I64:$dst, (node I64:$lhs, I64:$rhs))], 330b57cec5SDimitry Andric !strconcat("i64.", !strconcat(name, "\t$dst, $lhs, $rhs")), 340b57cec5SDimitry Andric !strconcat("i64.", name), i64Inst>; 350b57cec5SDimitry Andric} 360b57cec5SDimitry Andricmulticlass ComparisonInt<CondCode cond, string name, bits<32> i32Inst, bits<32> i64Inst> { 370b57cec5SDimitry Andric defm _I32 : I<(outs I32:$dst), (ins I32:$lhs, I32:$rhs), (outs), (ins), 380b57cec5SDimitry Andric [(set I32:$dst, (setcc I32:$lhs, I32:$rhs, cond))], 390b57cec5SDimitry Andric !strconcat("i32.", !strconcat(name, "\t$dst, $lhs, $rhs")), 400b57cec5SDimitry Andric !strconcat("i32.", name), i32Inst>; 410b57cec5SDimitry Andric defm _I64 : I<(outs I32:$dst), (ins I64:$lhs, I64:$rhs), (outs), (ins), 420b57cec5SDimitry Andric [(set I32:$dst, (setcc I64:$lhs, I64:$rhs, cond))], 430b57cec5SDimitry Andric !strconcat("i64.", !strconcat(name, "\t$dst, $lhs, $rhs")), 440b57cec5SDimitry Andric !strconcat("i64.", name), i64Inst>; 450b57cec5SDimitry Andric} 460b57cec5SDimitry Andric 470b57cec5SDimitry Andric// The spaces after the names are for aesthetic purposes only, to make 480b57cec5SDimitry Andric// operands line up vertically after tab expansion. 490b57cec5SDimitry Andriclet isCommutable = 1 in 500b57cec5SDimitry Andricdefm ADD : BinaryInt<add, "add ", 0x6a, 0x7c>; 510b57cec5SDimitry Andricdefm SUB : BinaryInt<sub, "sub ", 0x6b, 0x7d>; 520b57cec5SDimitry Andriclet isCommutable = 1 in 530b57cec5SDimitry Andricdefm MUL : BinaryInt<mul, "mul ", 0x6c, 0x7e>; 540b57cec5SDimitry Andric// Divide and remainder trap on a zero denominator. 550b57cec5SDimitry Andriclet hasSideEffects = 1 in { 560b57cec5SDimitry Andricdefm DIV_S : BinaryInt<sdiv, "div_s", 0x6d, 0x7f>; 570b57cec5SDimitry Andricdefm DIV_U : BinaryInt<udiv, "div_u", 0x6e, 0x80>; 580b57cec5SDimitry Andricdefm REM_S : BinaryInt<srem, "rem_s", 0x6f, 0x81>; 590b57cec5SDimitry Andricdefm REM_U : BinaryInt<urem, "rem_u", 0x70, 0x82>; 600b57cec5SDimitry Andric} // hasSideEffects = 1 610b57cec5SDimitry Andriclet isCommutable = 1 in { 620b57cec5SDimitry Andricdefm AND : BinaryInt<and, "and ", 0x71, 0x83>; 630b57cec5SDimitry Andricdefm OR : BinaryInt<or, "or ", 0x72, 0x84>; 640b57cec5SDimitry Andricdefm XOR : BinaryInt<xor, "xor ", 0x73, 0x85>; 650b57cec5SDimitry Andric} // isCommutable = 1 660b57cec5SDimitry Andricdefm SHL : BinaryInt<shl, "shl ", 0x74, 0x86>; 670b57cec5SDimitry Andricdefm SHR_S : BinaryInt<sra, "shr_s", 0x75, 0x87>; 680b57cec5SDimitry Andricdefm SHR_U : BinaryInt<srl, "shr_u", 0x76, 0x88>; 690b57cec5SDimitry Andricdefm ROTL : BinaryInt<rotl, "rotl", 0x77, 0x89>; 700b57cec5SDimitry Andricdefm ROTR : BinaryInt<rotr, "rotr", 0x78, 0x8a>; 710b57cec5SDimitry Andric 720b57cec5SDimitry Andriclet isCommutable = 1 in { 730b57cec5SDimitry Andricdefm EQ : ComparisonInt<SETEQ, "eq ", 0x46, 0x51>; 740b57cec5SDimitry Andricdefm NE : ComparisonInt<SETNE, "ne ", 0x47, 0x52>; 750b57cec5SDimitry Andric} // isCommutable = 1 760b57cec5SDimitry Andricdefm LT_S : ComparisonInt<SETLT, "lt_s", 0x48, 0x53>; 770b57cec5SDimitry Andricdefm LT_U : ComparisonInt<SETULT, "lt_u", 0x49, 0x54>; 780b57cec5SDimitry Andricdefm GT_S : ComparisonInt<SETGT, "gt_s", 0x4a, 0x55>; 790b57cec5SDimitry Andricdefm GT_U : ComparisonInt<SETUGT, "gt_u", 0x4b, 0x56>; 800b57cec5SDimitry Andricdefm LE_S : ComparisonInt<SETLE, "le_s", 0x4c, 0x57>; 810b57cec5SDimitry Andricdefm LE_U : ComparisonInt<SETULE, "le_u", 0x4d, 0x58>; 820b57cec5SDimitry Andricdefm GE_S : ComparisonInt<SETGE, "ge_s", 0x4e, 0x59>; 830b57cec5SDimitry Andricdefm GE_U : ComparisonInt<SETUGE, "ge_u", 0x4f, 0x5a>; 840b57cec5SDimitry Andric 850b57cec5SDimitry Andricdefm CLZ : UnaryInt<ctlz, "clz ", 0x67, 0x79>; 860b57cec5SDimitry Andricdefm CTZ : UnaryInt<cttz, "ctz ", 0x68, 0x7a>; 870b57cec5SDimitry Andricdefm POPCNT : UnaryInt<ctpop, "popcnt", 0x69, 0x7b>; 880b57cec5SDimitry Andric 890b57cec5SDimitry Andricdefm EQZ_I32 : I<(outs I32:$dst), (ins I32:$src), (outs), (ins), 900b57cec5SDimitry Andric [(set I32:$dst, (setcc I32:$src, 0, SETEQ))], 910b57cec5SDimitry Andric "i32.eqz \t$dst, $src", "i32.eqz", 0x45>; 920b57cec5SDimitry Andricdefm EQZ_I64 : I<(outs I32:$dst), (ins I64:$src), (outs), (ins), 930b57cec5SDimitry Andric [(set I32:$dst, (setcc I64:$src, 0, SETEQ))], 940b57cec5SDimitry Andric "i64.eqz \t$dst, $src", "i64.eqz", 0x50>; 950b57cec5SDimitry Andric 96*fe6060f1SDimitry Andric// Optimize away an explicit mask on a shift count. 97*fe6060f1SDimitry Andricdef : Pat<(shl I32:$lhs, (and I32:$rhs, 31)), (SHL_I32 I32:$lhs, I32:$rhs)>; 98*fe6060f1SDimitry Andricdef : Pat<(sra I32:$lhs, (and I32:$rhs, 31)), (SHR_S_I32 I32:$lhs, I32:$rhs)>; 99*fe6060f1SDimitry Andricdef : Pat<(srl I32:$lhs, (and I32:$rhs, 31)), (SHR_U_I32 I32:$lhs, I32:$rhs)>; 100*fe6060f1SDimitry Andricdef : Pat<(shl I64:$lhs, (and I64:$rhs, 63)), (SHL_I64 I64:$lhs, I64:$rhs)>; 101*fe6060f1SDimitry Andricdef : Pat<(sra I64:$lhs, (and I64:$rhs, 63)), (SHR_S_I64 I64:$lhs, I64:$rhs)>; 102*fe6060f1SDimitry Andricdef : Pat<(srl I64:$lhs, (and I64:$rhs, 63)), (SHR_U_I64 I64:$lhs, I64:$rhs)>; 103*fe6060f1SDimitry Andric 1040b57cec5SDimitry Andric// Optimize away an explicit mask on a rotate count. 1050b57cec5SDimitry Andricdef : Pat<(rotl I32:$lhs, (and I32:$rhs, 31)), (ROTL_I32 I32:$lhs, I32:$rhs)>; 1060b57cec5SDimitry Andricdef : Pat<(rotr I32:$lhs, (and I32:$rhs, 31)), (ROTR_I32 I32:$lhs, I32:$rhs)>; 1070b57cec5SDimitry Andricdef : Pat<(rotl I64:$lhs, (and I64:$rhs, 63)), (ROTL_I64 I64:$lhs, I64:$rhs)>; 1080b57cec5SDimitry Andricdef : Pat<(rotr I64:$lhs, (and I64:$rhs, 63)), (ROTR_I64 I64:$lhs, I64:$rhs)>; 1090b57cec5SDimitry Andric 1100b57cec5SDimitry Andricdefm SELECT_I32 : I<(outs I32:$dst), (ins I32:$lhs, I32:$rhs, I32:$cond), 1110b57cec5SDimitry Andric (outs), (ins), 1120b57cec5SDimitry Andric [(set I32:$dst, (select I32:$cond, I32:$lhs, I32:$rhs))], 1130b57cec5SDimitry Andric "i32.select\t$dst, $lhs, $rhs, $cond", "i32.select", 0x1b>; 1140b57cec5SDimitry Andricdefm SELECT_I64 : I<(outs I64:$dst), (ins I64:$lhs, I64:$rhs, I32:$cond), 1150b57cec5SDimitry Andric (outs), (ins), 1160b57cec5SDimitry Andric [(set I64:$dst, (select I32:$cond, I64:$lhs, I64:$rhs))], 1170b57cec5SDimitry Andric "i64.select\t$dst, $lhs, $rhs, $cond", "i64.select", 0x1b>; 1180b57cec5SDimitry Andric 1190b57cec5SDimitry Andric// ISD::SELECT requires its operand to conform to getBooleanContents, but 1200b57cec5SDimitry Andric// WebAssembly's select interprets any non-zero value as true, so we can fold 1210b57cec5SDimitry Andric// a setne with 0 into a select. 1220b57cec5SDimitry Andricdef : Pat<(select (i32 (setne I32:$cond, 0)), I32:$lhs, I32:$rhs), 1230b57cec5SDimitry Andric (SELECT_I32 I32:$lhs, I32:$rhs, I32:$cond)>; 1240b57cec5SDimitry Andricdef : Pat<(select (i32 (setne I32:$cond, 0)), I64:$lhs, I64:$rhs), 1250b57cec5SDimitry Andric (SELECT_I64 I64:$lhs, I64:$rhs, I32:$cond)>; 1260b57cec5SDimitry Andric 1270b57cec5SDimitry Andric// And again, this time with seteq instead of setne and the arms reversed. 1280b57cec5SDimitry Andricdef : Pat<(select (i32 (seteq I32:$cond, 0)), I32:$lhs, I32:$rhs), 1290b57cec5SDimitry Andric (SELECT_I32 I32:$rhs, I32:$lhs, I32:$cond)>; 1300b57cec5SDimitry Andricdef : Pat<(select (i32 (seteq I32:$cond, 0)), I64:$lhs, I64:$rhs), 1310b57cec5SDimitry Andric (SELECT_I64 I64:$rhs, I64:$lhs, I32:$cond)>; 132