1286425Sdim// WebAssemblyInstrInteger.td-WebAssembly Integer codegen -------*- tablegen -*-
2286425Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6286425Sdim//
7286425Sdim//===----------------------------------------------------------------------===//
8286425Sdim///
9286425Sdim/// \file
10341825Sdim/// WebAssembly Integer operand code-gen constructs.
11286425Sdim///
12286425Sdim//===----------------------------------------------------------------------===//
13286425Sdim
14344779Sdimmulticlass UnaryInt<SDNode node, string name, bits<32> i32Inst,
15344779Sdim                    bits<32> i64Inst> {
16344779Sdim  defm _I32 : I<(outs I32:$dst), (ins I32:$src), (outs), (ins),
17344779Sdim                [(set I32:$dst, (node I32:$src))],
18344779Sdim                !strconcat("i32.", !strconcat(name, "\t$dst, $src")),
19344779Sdim                !strconcat("i32.", name), i32Inst>;
20344779Sdim  defm _I64 : I<(outs I64:$dst), (ins I64:$src), (outs), (ins),
21344779Sdim                [(set I64:$dst, (node I64:$src))],
22344779Sdim                !strconcat("i64.", !strconcat(name, "\t$dst, $src")),
23344779Sdim                !strconcat("i64.", name), i64Inst>;
24344779Sdim}
25344779Sdimmulticlass BinaryInt<SDNode node, string name, bits<32> i32Inst,
26344779Sdim                     bits<32> i64Inst> {
27344779Sdim  defm _I32 : I<(outs I32:$dst), (ins I32:$lhs, I32:$rhs), (outs), (ins),
28344779Sdim                [(set I32:$dst, (node I32:$lhs, I32:$rhs))],
29344779Sdim                !strconcat("i32.", !strconcat(name, "\t$dst, $lhs, $rhs")),
30344779Sdim                !strconcat("i32.", name), i32Inst>;
31344779Sdim  defm _I64 : I<(outs I64:$dst), (ins I64:$lhs, I64:$rhs), (outs), (ins),
32344779Sdim                [(set I64:$dst, (node I64:$lhs, I64:$rhs))],
33344779Sdim                !strconcat("i64.", !strconcat(name, "\t$dst, $lhs, $rhs")),
34344779Sdim                !strconcat("i64.", name), i64Inst>;
35344779Sdim}
36344779Sdimmulticlass ComparisonInt<CondCode cond, string name, bits<32> i32Inst, bits<32> i64Inst> {
37344779Sdim  defm _I32 : I<(outs I32:$dst), (ins I32:$lhs, I32:$rhs), (outs), (ins),
38344779Sdim                [(set I32:$dst, (setcc I32:$lhs, I32:$rhs, cond))],
39344779Sdim                !strconcat("i32.", !strconcat(name, "\t$dst, $lhs, $rhs")),
40344779Sdim                !strconcat("i32.", name), i32Inst>;
41344779Sdim  defm _I64 : I<(outs I32:$dst), (ins I64:$lhs, I64:$rhs), (outs), (ins),
42344779Sdim                [(set I32:$dst, (setcc I64:$lhs, I64:$rhs, cond))],
43344779Sdim                !strconcat("i64.", !strconcat(name, "\t$dst, $lhs, $rhs")),
44344779Sdim                !strconcat("i64.", name), i64Inst>;
45344779Sdim}
46286425Sdim
47296417Sdim// The spaces after the names are for aesthetic purposes only, to make
48296417Sdim// operands line up vertically after tab expansion.
49296417Sdimlet isCommutable = 1 in
50314564Sdimdefm ADD : BinaryInt<add, "add ", 0x6a, 0x7c>;
51314564Sdimdefm SUB : BinaryInt<sub, "sub ", 0x6b, 0x7d>;
52296417Sdimlet isCommutable = 1 in
53314564Sdimdefm MUL : BinaryInt<mul, "mul ", 0x6c, 0x7e>;
54296417Sdim// Divide and remainder trap on a zero denominator.
55296417Sdimlet hasSideEffects = 1 in {
56314564Sdimdefm DIV_S : BinaryInt<sdiv, "div_s", 0x6d, 0x7f>;
57314564Sdimdefm DIV_U : BinaryInt<udiv, "div_u", 0x6e, 0x80>;
58314564Sdimdefm REM_S : BinaryInt<srem, "rem_s", 0x6f, 0x81>;
59314564Sdimdefm REM_U : BinaryInt<urem, "rem_u", 0x70, 0x82>;
60296417Sdim} // hasSideEffects = 1
61296417Sdimlet isCommutable = 1 in {
62314564Sdimdefm AND : BinaryInt<and, "and ", 0x71, 0x83>;
63314564Sdimdefm OR : BinaryInt<or, "or  ", 0x72, 0x84>;
64314564Sdimdefm XOR : BinaryInt<xor, "xor ", 0x73, 0x85>;
65296417Sdim} // isCommutable = 1
66314564Sdimdefm SHL : BinaryInt<shl, "shl ", 0x74, 0x86>;
67314564Sdimdefm SHR_S : BinaryInt<sra, "shr_s", 0x75, 0x87>;
68314564Sdimdefm SHR_U : BinaryInt<srl, "shr_u", 0x76, 0x88>;
69314564Sdimdefm ROTL : BinaryInt<rotl, "rotl", 0x77, 0x89>;
70314564Sdimdefm ROTR : BinaryInt<rotr, "rotr", 0x78, 0x8a>;
71286425Sdim
72296417Sdimlet isCommutable = 1 in {
73314564Sdimdefm EQ : ComparisonInt<SETEQ, "eq  ", 0x46, 0x51>;
74314564Sdimdefm NE : ComparisonInt<SETNE, "ne  ", 0x47, 0x52>;
75296417Sdim} // isCommutable = 1
76314564Sdimdefm LT_S : ComparisonInt<SETLT,  "lt_s", 0x48, 0x53>;
77314564Sdimdefm LT_U : ComparisonInt<SETULT, "lt_u", 0x49, 0x54>;
78314564Sdimdefm GT_S : ComparisonInt<SETGT,  "gt_s", 0x4a, 0x55>;
79314564Sdimdefm GT_U : ComparisonInt<SETUGT, "gt_u", 0x4b, 0x56>;
80314564Sdimdefm LE_S : ComparisonInt<SETLE,  "le_s", 0x4c, 0x57>;
81314564Sdimdefm LE_U : ComparisonInt<SETULE, "le_u", 0x4d, 0x58>;
82314564Sdimdefm GE_S : ComparisonInt<SETGE,  "ge_s", 0x4e, 0x59>;
83314564Sdimdefm GE_U : ComparisonInt<SETUGE, "ge_u", 0x4f, 0x5a>;
84296417Sdim
85314564Sdimdefm CLZ : UnaryInt<ctlz, "clz ", 0x67, 0x79>;
86314564Sdimdefm CTZ : UnaryInt<cttz, "ctz ", 0x68, 0x7a>;
87314564Sdimdefm POPCNT : UnaryInt<ctpop, "popcnt", 0x69, 0x7b>;
88296417Sdim
89341825Sdimdefm EQZ_I32 : I<(outs I32:$dst), (ins I32:$src), (outs), (ins),
90341825Sdim                 [(set I32:$dst, (setcc I32:$src, 0, SETEQ))],
91341825Sdim                 "i32.eqz \t$dst, $src", "i32.eqz", 0x45>;
92341825Sdimdefm EQZ_I64 : I<(outs I32:$dst), (ins I64:$src), (outs), (ins),
93341825Sdim                 [(set I32:$dst, (setcc I64:$src, 0, SETEQ))],
94341825Sdim                 "i64.eqz \t$dst, $src", "i64.eqz", 0x50>;
95309124Sdim
96309124Sdim// Optimize away an explicit mask on a rotate count.
97309124Sdimdef : Pat<(rotl I32:$lhs, (and I32:$rhs, 31)), (ROTL_I32 I32:$lhs, I32:$rhs)>;
98309124Sdimdef : Pat<(rotr I32:$lhs, (and I32:$rhs, 31)), (ROTR_I32 I32:$lhs, I32:$rhs)>;
99309124Sdimdef : Pat<(rotl I64:$lhs, (and I64:$rhs, 63)), (ROTL_I64 I64:$lhs, I64:$rhs)>;
100309124Sdimdef : Pat<(rotr I64:$lhs, (and I64:$rhs, 63)), (ROTR_I64 I64:$lhs, I64:$rhs)>;
101296417Sdim
102341825Sdimdefm SELECT_I32 : I<(outs I32:$dst), (ins I32:$lhs, I32:$rhs, I32:$cond),
103341825Sdim                    (outs), (ins),
104341825Sdim                    [(set I32:$dst, (select I32:$cond, I32:$lhs, I32:$rhs))],
105341825Sdim                    "i32.select\t$dst, $lhs, $rhs, $cond", "i32.select", 0x1b>;
106341825Sdimdefm SELECT_I64 : I<(outs I64:$dst), (ins I64:$lhs, I64:$rhs, I32:$cond),
107341825Sdim                    (outs), (ins),
108341825Sdim                    [(set I64:$dst, (select I32:$cond, I64:$lhs, I64:$rhs))],
109341825Sdim                    "i64.select\t$dst, $lhs, $rhs, $cond", "i64.select", 0x1b>;
110296417Sdim
111296417Sdim// ISD::SELECT requires its operand to conform to getBooleanContents, but
112296417Sdim// WebAssembly's select interprets any non-zero value as true, so we can fold
113296417Sdim// a setne with 0 into a select.
114296417Sdimdef : Pat<(select (i32 (setne I32:$cond, 0)), I32:$lhs, I32:$rhs),
115309124Sdim          (SELECT_I32 I32:$lhs, I32:$rhs, I32:$cond)>;
116296417Sdimdef : Pat<(select (i32 (setne I32:$cond, 0)), I64:$lhs, I64:$rhs),
117309124Sdim          (SELECT_I64 I64:$lhs, I64:$rhs, I32:$cond)>;
118296417Sdim
119296417Sdim// And again, this time with seteq instead of setne and the arms reversed.
120296417Sdimdef : Pat<(select (i32 (seteq I32:$cond, 0)), I32:$lhs, I32:$rhs),
121309124Sdim          (SELECT_I32 I32:$rhs, I32:$lhs, I32:$cond)>;
122296417Sdimdef : Pat<(select (i32 (seteq I32:$cond, 0)), I64:$lhs, I64:$rhs),
123309124Sdim          (SELECT_I64 I64:$rhs, I64:$lhs, I32:$cond)>;
124