15ca0bacaSJF Bastien// WebAssemblyInstrInteger.td-WebAssembly Integer codegen -------*- tablegen -*-
25ca0bacaSJF Bastien//
32946cd70SChandler Carruth// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth// See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65ca0bacaSJF Bastien//
75ca0bacaSJF Bastien//===----------------------------------------------------------------------===//
85ca0bacaSJF Bastien///
95ca0bacaSJF Bastien/// \file
105f8f34e4SAdrian Prantl/// WebAssembly Integer operand code-gen constructs.
115ca0bacaSJF Bastien///
125ca0bacaSJF Bastien//===----------------------------------------------------------------------===//
135ca0bacaSJF Bastien
14914f0f20SThomas Livelymulticlass UnaryInt<SDNode node, string name, bits<32> i32Inst,
15914f0f20SThomas Lively                    bits<32> i64Inst> {
16914f0f20SThomas Lively  defm _I32 : I<(outs I32:$dst), (ins I32:$src), (outs), (ins),
17914f0f20SThomas Lively                [(set I32:$dst, (node I32:$src))],
18914f0f20SThomas Lively                !strconcat("i32.", !strconcat(name, "\t$dst, $src")),
19914f0f20SThomas Lively                !strconcat("i32.", name), i32Inst>;
20914f0f20SThomas Lively  defm _I64 : I<(outs I64:$dst), (ins I64:$src), (outs), (ins),
21914f0f20SThomas Lively                [(set I64:$dst, (node I64:$src))],
22914f0f20SThomas Lively                !strconcat("i64.", !strconcat(name, "\t$dst, $src")),
23914f0f20SThomas Lively                !strconcat("i64.", name), i64Inst>;
24914f0f20SThomas Lively}
25914f0f20SThomas Livelymulticlass BinaryInt<SDNode node, string name, bits<32> i32Inst,
26914f0f20SThomas Lively                     bits<32> i64Inst> {
27914f0f20SThomas Lively  defm _I32 : I<(outs I32:$dst), (ins I32:$lhs, I32:$rhs), (outs), (ins),
28914f0f20SThomas Lively                [(set I32:$dst, (node I32:$lhs, I32:$rhs))],
29914f0f20SThomas Lively                !strconcat("i32.", !strconcat(name, "\t$dst, $lhs, $rhs")),
30914f0f20SThomas Lively                !strconcat("i32.", name), i32Inst>;
31914f0f20SThomas Lively  defm _I64 : I<(outs I64:$dst), (ins I64:$lhs, I64:$rhs), (outs), (ins),
32914f0f20SThomas Lively                [(set I64:$dst, (node I64:$lhs, I64:$rhs))],
33914f0f20SThomas Lively                !strconcat("i64.", !strconcat(name, "\t$dst, $lhs, $rhs")),
34914f0f20SThomas Lively                !strconcat("i64.", name), i64Inst>;
35914f0f20SThomas Lively}
36914f0f20SThomas Livelymulticlass ComparisonInt<CondCode cond, string name, bits<32> i32Inst, bits<32> i64Inst> {
37914f0f20SThomas Lively  defm _I32 : I<(outs I32:$dst), (ins I32:$lhs, I32:$rhs), (outs), (ins),
38914f0f20SThomas Lively                [(set I32:$dst, (setcc I32:$lhs, I32:$rhs, cond))],
39914f0f20SThomas Lively                !strconcat("i32.", !strconcat(name, "\t$dst, $lhs, $rhs")),
40914f0f20SThomas Lively                !strconcat("i32.", name), i32Inst>;
41914f0f20SThomas Lively  defm _I64 : I<(outs I32:$dst), (ins I64:$lhs, I64:$rhs), (outs), (ins),
42914f0f20SThomas Lively                [(set I32:$dst, (setcc I64:$lhs, I64:$rhs, cond))],
43914f0f20SThomas Lively                !strconcat("i64.", !strconcat(name, "\t$dst, $lhs, $rhs")),
44914f0f20SThomas Lively                !strconcat("i64.", name), i64Inst>;
45914f0f20SThomas Lively}
46914f0f20SThomas Lively
471f29c680SDan Gohman// The spaces after the names are for aesthetic purposes only, to make
481f29c680SDan Gohman// operands line up vertically after tab expansion.
49174b2d83SDan Gohmanlet isCommutable = 1 in
50c968297bSDan Gohmandefm ADD : BinaryInt<add, "add ", 0x6a, 0x7c>;
51c968297bSDan Gohmandefm SUB : BinaryInt<sub, "sub ", 0x6b, 0x7d>;
52174b2d83SDan Gohmanlet isCommutable = 1 in
53c968297bSDan Gohmandefm MUL : BinaryInt<mul, "mul ", 0x6c, 0x7e>;
54174b2d83SDan Gohman// Divide and remainder trap on a zero denominator.
55174b2d83SDan Gohmanlet hasSideEffects = 1 in {
56c968297bSDan Gohmandefm DIV_S : BinaryInt<sdiv, "div_s", 0x6d, 0x7f>;
57c968297bSDan Gohmandefm DIV_U : BinaryInt<udiv, "div_u", 0x6e, 0x80>;
58c968297bSDan Gohmandefm REM_S : BinaryInt<srem, "rem_s", 0x6f, 0x81>;
59c968297bSDan Gohmandefm REM_U : BinaryInt<urem, "rem_u", 0x70, 0x82>;
60174b2d83SDan Gohman} // hasSideEffects = 1
61174b2d83SDan Gohmanlet isCommutable = 1 in {
62c968297bSDan Gohmandefm AND : BinaryInt<and, "and ", 0x71, 0x83>;
63c968297bSDan Gohmandefm OR : BinaryInt<or, "or  ", 0x72, 0x84>;
64c968297bSDan Gohmandefm XOR : BinaryInt<xor, "xor ", 0x73, 0x85>;
65174b2d83SDan Gohman} // isCommutable = 1
66c968297bSDan Gohmandefm SHL : BinaryInt<shl, "shl ", 0x74, 0x86>;
67c968297bSDan Gohmandefm SHR_S : BinaryInt<sra, "shr_s", 0x75, 0x87>;
68c968297bSDan Gohmandefm SHR_U : BinaryInt<srl, "shr_u", 0x76, 0x88>;
69c968297bSDan Gohmandefm ROTL : BinaryInt<rotl, "rotl", 0x77, 0x89>;
70a2b9b349SDan Gohmandefm ROTR : BinaryInt<rotr, "rotr", 0x78, 0x8a>;
71d9767a36SJF Bastien
72174b2d83SDan Gohmanlet isCommutable = 1 in {
732aae5dc7SDan Gohmandefm EQ : ComparisonInt<SETEQ, "eq  ", 0x46, 0x51>;
742aae5dc7SDan Gohmandefm NE : ComparisonInt<SETNE, "ne  ", 0x47, 0x52>;
75174b2d83SDan Gohman} // isCommutable = 1
76c968297bSDan Gohmandefm LT_S : ComparisonInt<SETLT,  "lt_s", 0x48, 0x53>;
77c968297bSDan Gohmandefm LT_U : ComparisonInt<SETULT, "lt_u", 0x49, 0x54>;
78c968297bSDan Gohmandefm GT_S : ComparisonInt<SETGT,  "gt_s", 0x4a, 0x55>;
79c968297bSDan Gohmandefm GT_U : ComparisonInt<SETUGT, "gt_u", 0x4b, 0x56>;
80c968297bSDan Gohmandefm LE_S : ComparisonInt<SETLE,  "le_s", 0x4c, 0x57>;
81c968297bSDan Gohmandefm LE_U : ComparisonInt<SETULE, "le_u", 0x4d, 0x58>;
82c968297bSDan Gohmandefm GE_S : ComparisonInt<SETGE,  "ge_s", 0x4e, 0x59>;
83c968297bSDan Gohmandefm GE_U : ComparisonInt<SETUGE, "ge_u", 0x4f, 0x5a>;
84d9767a36SJF Bastien
85c968297bSDan Gohmandefm CLZ : UnaryInt<ctlz, "clz ", 0x67, 0x79>;
86c968297bSDan Gohmandefm CTZ : UnaryInt<cttz, "ctz ", 0x68, 0x7a>;
87c968297bSDan Gohmandefm POPCNT : UnaryInt<ctpop, "popcnt", 0x69, 0x7b>;
8808fc966dSDan Gohman
8948dac310SWouter van Oortmerssendefm EQZ_I32 : I<(outs I32:$dst), (ins I32:$src), (outs), (ins),
90c8d7f145SDan Gohman                 [(set I32:$dst, (setcc I32:$src, 0, SETEQ))],
9148dac310SWouter van Oortmerssen                 "i32.eqz \t$dst, $src", "i32.eqz", 0x45>;
9248dac310SWouter van Oortmerssendefm EQZ_I64 : I<(outs I32:$dst), (ins I64:$src), (outs), (ins),
93c8d7f145SDan Gohman                 [(set I32:$dst, (setcc I64:$src, 0, SETEQ))],
9448dac310SWouter van Oortmerssen                 "i64.eqz \t$dst, $src", "i64.eqz", 0x50>;
95c8d7f145SDan Gohman
96*f8c5a4c6SThomas Lively// Optimize away an explicit mask on a shift count.
97*f8c5a4c6SThomas Livelydef : Pat<(shl I32:$lhs, (and I32:$rhs, 31)), (SHL_I32 I32:$lhs, I32:$rhs)>;
98*f8c5a4c6SThomas Livelydef : Pat<(sra I32:$lhs, (and I32:$rhs, 31)), (SHR_S_I32 I32:$lhs, I32:$rhs)>;
99*f8c5a4c6SThomas Livelydef : Pat<(srl I32:$lhs, (and I32:$rhs, 31)), (SHR_U_I32 I32:$lhs, I32:$rhs)>;
100*f8c5a4c6SThomas Livelydef : Pat<(shl I64:$lhs, (and I64:$rhs, 63)), (SHL_I64 I64:$lhs, I64:$rhs)>;
101*f8c5a4c6SThomas Livelydef : Pat<(sra I64:$lhs, (and I64:$rhs, 63)), (SHR_S_I64 I64:$lhs, I64:$rhs)>;
102*f8c5a4c6SThomas Livelydef : Pat<(srl I64:$lhs, (and I64:$rhs, 63)), (SHR_U_I64 I64:$lhs, I64:$rhs)>;
103*f8c5a4c6SThomas Lively
104665d7e38SDan Gohman// Optimize away an explicit mask on a rotate count.
105665d7e38SDan Gohmandef : Pat<(rotl I32:$lhs, (and I32:$rhs, 31)), (ROTL_I32 I32:$lhs, I32:$rhs)>;
106665d7e38SDan Gohmandef : Pat<(rotr I32:$lhs, (and I32:$rhs, 31)), (ROTR_I32 I32:$lhs, I32:$rhs)>;
107665d7e38SDan Gohmandef : Pat<(rotl I64:$lhs, (and I64:$rhs, 63)), (ROTL_I64 I64:$lhs, I64:$rhs)>;
108665d7e38SDan Gohmandef : Pat<(rotr I64:$lhs, (and I64:$rhs, 63)), (ROTR_I64 I64:$lhs, I64:$rhs)>;
109665d7e38SDan Gohman
11048dac310SWouter van Oortmerssendefm SELECT_I32 : I<(outs I32:$dst), (ins I32:$lhs, I32:$rhs, I32:$cond),
11148dac310SWouter van Oortmerssen                    (outs), (ins),
112af29bd4fSDan Gohman                    [(set I32:$dst, (select I32:$cond, I32:$lhs, I32:$rhs))],
11348dac310SWouter van Oortmerssen                    "i32.select\t$dst, $lhs, $rhs, $cond", "i32.select", 0x1b>;
11448dac310SWouter van Oortmerssendefm SELECT_I64 : I<(outs I64:$dst), (ins I64:$lhs, I64:$rhs, I32:$cond),
11548dac310SWouter van Oortmerssen                    (outs), (ins),
116af29bd4fSDan Gohman                    [(set I64:$dst, (select I32:$cond, I64:$lhs, I64:$rhs))],
11748dac310SWouter van Oortmerssen                    "i64.select\t$dst, $lhs, $rhs, $cond", "i64.select", 0x1b>;
118fb3e0594SDan Gohman
119d9b42188SDan Gohman// ISD::SELECT requires its operand to conform to getBooleanContents, but
120d9b42188SDan Gohman// WebAssembly's select interprets any non-zero value as true, so we can fold
121d9b42188SDan Gohman// a setne with 0 into a select.
122d9b42188SDan Gohmandef : Pat<(select (i32 (setne I32:$cond, 0)), I32:$lhs, I32:$rhs),
123d46b0926SDan Gohman          (SELECT_I32 I32:$lhs, I32:$rhs, I32:$cond)>;
124d9b42188SDan Gohmandef : Pat<(select (i32 (setne I32:$cond, 0)), I64:$lhs, I64:$rhs),
125d46b0926SDan Gohman          (SELECT_I64 I64:$lhs, I64:$rhs, I32:$cond)>;
126d9b42188SDan Gohman
127d9b42188SDan Gohman// And again, this time with seteq instead of setne and the arms reversed.
128d9b42188SDan Gohmandef : Pat<(select (i32 (seteq I32:$cond, 0)), I32:$lhs, I32:$rhs),
129d46b0926SDan Gohman          (SELECT_I32 I32:$rhs, I32:$lhs, I32:$cond)>;
130d9b42188SDan Gohmandef : Pat<(select (i32 (seteq I32:$cond, 0)), I64:$lhs, I64:$rhs),
131d46b0926SDan Gohman          (SELECT_I64 I64:$rhs, I64:$lhs, I32:$cond)>;
132