10b57cec5SDimitry Andric// WebAssemblyInstrAtomics.td-WebAssembly Atomic codegen support-*- 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 Atomic operand code-gen constructs.
110b57cec5SDimitry Andric///
120b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric
140b57cec5SDimitry Andriclet UseNamedOperandTable = 1 in
150b57cec5SDimitry Andricmulticlass ATOMIC_I<dag oops_r, dag iops_r, dag oops_s, dag iops_s,
160b57cec5SDimitry Andric                    list<dag> pattern_r, string asmstr_r = "",
170b57cec5SDimitry Andric                    string asmstr_s = "", bits<32> atomic_op = -1> {
180b57cec5SDimitry Andric  defm "" : I<oops_r, iops_r, oops_s, iops_s, pattern_r, asmstr_r, asmstr_s,
190b57cec5SDimitry Andric              !or(0xfe00, !and(0xff, atomic_op))>,
200b57cec5SDimitry Andric            Requires<[HasAtomics]>;
210b57cec5SDimitry Andric}
220b57cec5SDimitry Andric
230b57cec5SDimitry Andricmulticlass ATOMIC_NRI<dag oops, dag iops, list<dag> pattern, string asmstr = "",
240b57cec5SDimitry Andric                      bits<32> atomic_op = -1> {
250b57cec5SDimitry Andric  defm "" : NRI<oops, iops, pattern, asmstr,
260b57cec5SDimitry Andric                !or(0xfe00, !and(0xff, atomic_op))>,
270b57cec5SDimitry Andric            Requires<[HasAtomics]>;
280b57cec5SDimitry Andric}
290b57cec5SDimitry Andric
300b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
310b57cec5SDimitry Andric// Atomic wait / notify
320b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
330b57cec5SDimitry Andric
340b57cec5SDimitry Andriclet hasSideEffects = 1 in {
350b57cec5SDimitry Andricdefm ATOMIC_NOTIFY :
360b57cec5SDimitry Andric  ATOMIC_I<(outs I32:$dst),
370b57cec5SDimitry Andric           (ins P2Align:$p2align, offset32_op:$off, I32:$addr, I32:$count),
380b57cec5SDimitry Andric           (outs), (ins P2Align:$p2align, offset32_op:$off), [],
390b57cec5SDimitry Andric           "atomic.notify \t$dst, ${off}(${addr})${p2align}, $count",
400b57cec5SDimitry Andric           "atomic.notify \t${off}${p2align}", 0x00>;
410b57cec5SDimitry Andriclet mayLoad = 1 in {
420b57cec5SDimitry Andricdefm ATOMIC_WAIT_I32 :
430b57cec5SDimitry Andric  ATOMIC_I<(outs I32:$dst),
440b57cec5SDimitry Andric           (ins P2Align:$p2align, offset32_op:$off, I32:$addr, I32:$exp,
450b57cec5SDimitry Andric                I64:$timeout),
460b57cec5SDimitry Andric           (outs), (ins P2Align:$p2align, offset32_op:$off), [],
470b57cec5SDimitry Andric           "i32.atomic.wait \t$dst, ${off}(${addr})${p2align}, $exp, $timeout",
480b57cec5SDimitry Andric           "i32.atomic.wait \t${off}${p2align}", 0x01>;
490b57cec5SDimitry Andricdefm ATOMIC_WAIT_I64 :
500b57cec5SDimitry Andric  ATOMIC_I<(outs I32:$dst),
510b57cec5SDimitry Andric           (ins P2Align:$p2align, offset32_op:$off, I32:$addr, I64:$exp,
520b57cec5SDimitry Andric                I64:$timeout),
530b57cec5SDimitry Andric           (outs), (ins P2Align:$p2align, offset32_op:$off), [],
540b57cec5SDimitry Andric           "i64.atomic.wait \t$dst, ${off}(${addr})${p2align}, $exp, $timeout",
550b57cec5SDimitry Andric           "i64.atomic.wait \t${off}${p2align}", 0x02>;
560b57cec5SDimitry Andric} // mayLoad = 1
570b57cec5SDimitry Andric} // hasSideEffects = 1
580b57cec5SDimitry Andric
590b57cec5SDimitry Andriclet Predicates = [HasAtomics] in {
600b57cec5SDimitry Andric// Select notifys with no constant offset.
610b57cec5SDimitry Andricdef NotifyPatNoOffset :
620b57cec5SDimitry Andric  Pat<(i32 (int_wasm_atomic_notify I32:$addr, I32:$count)),
630b57cec5SDimitry Andric      (ATOMIC_NOTIFY 0, 0, I32:$addr, I32:$count)>;
640b57cec5SDimitry Andric
650b57cec5SDimitry Andric// Select notifys with a constant offset.
660b57cec5SDimitry Andric
670b57cec5SDimitry Andric// Pattern with address + immediate offset
680b57cec5SDimitry Andricclass NotifyPatImmOff<PatFrag operand> :
690b57cec5SDimitry Andric  Pat<(i32 (int_wasm_atomic_notify (operand I32:$addr, imm:$off), I32:$count)),
700b57cec5SDimitry Andric      (ATOMIC_NOTIFY 0, imm:$off, I32:$addr, I32:$count)>;
710b57cec5SDimitry Andricdef : NotifyPatImmOff<regPlusImm>;
720b57cec5SDimitry Andricdef : NotifyPatImmOff<or_is_add>;
730b57cec5SDimitry Andric
740b57cec5SDimitry Andric// Select notifys with just a constant offset.
750b57cec5SDimitry Andricdef NotifyPatOffsetOnly :
760b57cec5SDimitry Andric  Pat<(i32 (int_wasm_atomic_notify imm:$off, I32:$count)),
770b57cec5SDimitry Andric      (ATOMIC_NOTIFY 0, imm:$off, (CONST_I32 0), I32:$count)>;
780b57cec5SDimitry Andric
790b57cec5SDimitry Andricdef NotifyPatGlobalAddrOffOnly :
800b57cec5SDimitry Andric  Pat<(i32 (int_wasm_atomic_notify (WebAssemblywrapper tglobaladdr:$off),
810b57cec5SDimitry Andric                                   I32:$count)),
820b57cec5SDimitry Andric      (ATOMIC_NOTIFY 0, tglobaladdr:$off, (CONST_I32 0), I32:$count)>;
830b57cec5SDimitry Andric
840b57cec5SDimitry Andric// Select waits with no constant offset.
850b57cec5SDimitry Andricclass WaitPatNoOffset<ValueType ty, Intrinsic kind, NI inst> :
860b57cec5SDimitry Andric  Pat<(i32 (kind I32:$addr, ty:$exp, I64:$timeout)),
870b57cec5SDimitry Andric      (inst 0, 0, I32:$addr, ty:$exp, I64:$timeout)>;
880b57cec5SDimitry Andricdef : WaitPatNoOffset<i32, int_wasm_atomic_wait_i32, ATOMIC_WAIT_I32>;
890b57cec5SDimitry Andricdef : WaitPatNoOffset<i64, int_wasm_atomic_wait_i64, ATOMIC_WAIT_I64>;
900b57cec5SDimitry Andric
910b57cec5SDimitry Andric// Select waits with a constant offset.
920b57cec5SDimitry Andric
930b57cec5SDimitry Andric// Pattern with address + immediate offset
940b57cec5SDimitry Andricclass WaitPatImmOff<ValueType ty, Intrinsic kind, PatFrag operand, NI inst> :
950b57cec5SDimitry Andric  Pat<(i32 (kind (operand I32:$addr, imm:$off), ty:$exp, I64:$timeout)),
960b57cec5SDimitry Andric      (inst 0, imm:$off, I32:$addr, ty:$exp, I64:$timeout)>;
970b57cec5SDimitry Andricdef : WaitPatImmOff<i32, int_wasm_atomic_wait_i32, regPlusImm, ATOMIC_WAIT_I32>;
980b57cec5SDimitry Andricdef : WaitPatImmOff<i32, int_wasm_atomic_wait_i32, or_is_add, ATOMIC_WAIT_I32>;
990b57cec5SDimitry Andricdef : WaitPatImmOff<i64, int_wasm_atomic_wait_i64, regPlusImm, ATOMIC_WAIT_I64>;
1000b57cec5SDimitry Andricdef : WaitPatImmOff<i64, int_wasm_atomic_wait_i64, or_is_add, ATOMIC_WAIT_I64>;
1010b57cec5SDimitry Andric
1020b57cec5SDimitry Andric// Select wait_i32, ATOMIC_WAIT_I32s with just a constant offset.
1030b57cec5SDimitry Andricclass WaitPatOffsetOnly<ValueType ty, Intrinsic kind, NI inst> :
1040b57cec5SDimitry Andric  Pat<(i32 (kind imm:$off, ty:$exp, I64:$timeout)),
1050b57cec5SDimitry Andric      (inst 0, imm:$off, (CONST_I32 0), ty:$exp, I64:$timeout)>;
1060b57cec5SDimitry Andricdef : WaitPatOffsetOnly<i32, int_wasm_atomic_wait_i32, ATOMIC_WAIT_I32>;
1070b57cec5SDimitry Andricdef : WaitPatOffsetOnly<i64, int_wasm_atomic_wait_i64, ATOMIC_WAIT_I64>;
1080b57cec5SDimitry Andric
1090b57cec5SDimitry Andricclass WaitPatGlobalAddrOffOnly<ValueType ty, Intrinsic kind, NI inst> :
1100b57cec5SDimitry Andric  Pat<(i32 (kind (WebAssemblywrapper tglobaladdr:$off), ty:$exp, I64:$timeout)),
1110b57cec5SDimitry Andric      (inst 0, tglobaladdr:$off, (CONST_I32 0), ty:$exp, I64:$timeout)>;
1120b57cec5SDimitry Andricdef : WaitPatGlobalAddrOffOnly<i32, int_wasm_atomic_wait_i32, ATOMIC_WAIT_I32>;
1130b57cec5SDimitry Andricdef : WaitPatGlobalAddrOffOnly<i64, int_wasm_atomic_wait_i64, ATOMIC_WAIT_I64>;
1140b57cec5SDimitry Andric} // Predicates = [HasAtomics]
1150b57cec5SDimitry Andric
1160b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
1178bcb0991SDimitry Andric// Atomic fences
1188bcb0991SDimitry Andric//===----------------------------------------------------------------------===//
1198bcb0991SDimitry Andric
1208bcb0991SDimitry Andric// A compiler fence instruction that prevents reordering of instructions.
1218bcb0991SDimitry Andriclet Defs = [ARGUMENTS] in {
1228bcb0991SDimitry Andriclet isPseudo = 1, hasSideEffects = 1 in
1238bcb0991SDimitry Andricdefm COMPILER_FENCE : ATOMIC_NRI<(outs), (ins), [], "compiler_fence">;
1248bcb0991SDimitry Andriclet hasSideEffects = 1 in
1258bcb0991SDimitry Andricdefm ATOMIC_FENCE : ATOMIC_NRI<(outs), (ins i8imm:$flags), [], "atomic.fence",
1268bcb0991SDimitry Andric                               0x03>;
1278bcb0991SDimitry Andric} // Defs = [ARGUMENTS]
1288bcb0991SDimitry Andric
1298bcb0991SDimitry Andric//===----------------------------------------------------------------------===//
1300b57cec5SDimitry Andric// Atomic loads
1310b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
1320b57cec5SDimitry Andric
1330b57cec5SDimitry Andricmulticlass AtomicLoad<WebAssemblyRegClass rc, string name, int atomic_op> {
1340b57cec5SDimitry Andric  defm "" : WebAssemblyLoad<rc, name, !or(0xfe00, !and(0xff, atomic_op))>,
1350b57cec5SDimitry Andric            Requires<[HasAtomics]>;
1360b57cec5SDimitry Andric}
1370b57cec5SDimitry Andric
1380b57cec5SDimitry Andricdefm ATOMIC_LOAD_I32 : AtomicLoad<I32, "i32.atomic.load", 0x10>;
1390b57cec5SDimitry Andricdefm ATOMIC_LOAD_I64 : AtomicLoad<I64, "i64.atomic.load", 0x11>;
1400b57cec5SDimitry Andric
1410b57cec5SDimitry Andric// Select loads with no constant offset.
1420b57cec5SDimitry Andriclet Predicates = [HasAtomics] in {
1430b57cec5SDimitry Andricdef : LoadPatNoOffset<i32, atomic_load_32, ATOMIC_LOAD_I32>;
1440b57cec5SDimitry Andricdef : LoadPatNoOffset<i64, atomic_load_64, ATOMIC_LOAD_I64>;
1450b57cec5SDimitry Andric
1460b57cec5SDimitry Andric// Select loads with a constant offset.
1470b57cec5SDimitry Andric
1480b57cec5SDimitry Andric// Pattern with address + immediate offset
1490b57cec5SDimitry Andricdef : LoadPatImmOff<i32, atomic_load_32, regPlusImm, ATOMIC_LOAD_I32>;
1500b57cec5SDimitry Andricdef : LoadPatImmOff<i64, atomic_load_64, regPlusImm, ATOMIC_LOAD_I64>;
1510b57cec5SDimitry Andricdef : LoadPatImmOff<i32, atomic_load_32, or_is_add, ATOMIC_LOAD_I32>;
1520b57cec5SDimitry Andricdef : LoadPatImmOff<i64, atomic_load_64, or_is_add, ATOMIC_LOAD_I64>;
1530b57cec5SDimitry Andric
1540b57cec5SDimitry Andric// Select loads with just a constant offset.
1550b57cec5SDimitry Andricdef : LoadPatOffsetOnly<i32, atomic_load_32, ATOMIC_LOAD_I32>;
1560b57cec5SDimitry Andricdef : LoadPatOffsetOnly<i64, atomic_load_64, ATOMIC_LOAD_I64>;
1570b57cec5SDimitry Andric
1580b57cec5SDimitry Andricdef : LoadPatGlobalAddrOffOnly<i32, atomic_load_32, ATOMIC_LOAD_I32>;
1590b57cec5SDimitry Andricdef : LoadPatGlobalAddrOffOnly<i64, atomic_load_64, ATOMIC_LOAD_I64>;
1600b57cec5SDimitry Andric
1610b57cec5SDimitry Andric} // Predicates = [HasAtomics]
1620b57cec5SDimitry Andric
1630b57cec5SDimitry Andric// Extending loads. Note that there are only zero-extending atomic loads, no
1640b57cec5SDimitry Andric// sign-extending loads.
1650b57cec5SDimitry Andricdefm ATOMIC_LOAD8_U_I32 : AtomicLoad<I32, "i32.atomic.load8_u", 0x12>;
1660b57cec5SDimitry Andricdefm ATOMIC_LOAD16_U_I32 : AtomicLoad<I32, "i32.atomic.load16_u", 0x13>;
1670b57cec5SDimitry Andricdefm ATOMIC_LOAD8_U_I64 : AtomicLoad<I64, "i64.atomic.load8_u", 0x14>;
1680b57cec5SDimitry Andricdefm ATOMIC_LOAD16_U_I64 : AtomicLoad<I64, "i64.atomic.load16_u", 0x15>;
1690b57cec5SDimitry Andricdefm ATOMIC_LOAD32_U_I64 : AtomicLoad<I64, "i64.atomic.load32_u", 0x16>;
1700b57cec5SDimitry Andric
1710b57cec5SDimitry Andric// Fragments for extending loads. These are different from regular loads because
1720b57cec5SDimitry Andric// the SDNodes are derived from AtomicSDNode rather than LoadSDNode and
1730b57cec5SDimitry Andric// therefore don't have the extension type field. So instead of matching that,
1740b57cec5SDimitry Andric// we match the patterns that the type legalizer expands them to.
1750b57cec5SDimitry Andric
1760b57cec5SDimitry Andric// We directly match zext patterns and select the zext atomic loads.
1770b57cec5SDimitry Andric// i32 (zext (i8 (atomic_load_8))) gets legalized to
1780b57cec5SDimitry Andric// i32 (and (i32 (atomic_load_8)), 255)
1790b57cec5SDimitry Andric// These can be selected to a single zero-extending atomic load instruction.
1800b57cec5SDimitry Andricdef zext_aload_8_32 :
1810b57cec5SDimitry Andric  PatFrag<(ops node:$addr), (and (i32 (atomic_load_8 node:$addr)), 255)>;
1820b57cec5SDimitry Andricdef zext_aload_16_32 :
1830b57cec5SDimitry Andric  PatFrag<(ops node:$addr), (and (i32 (atomic_load_16 node:$addr)), 65535)>;
1840b57cec5SDimitry Andric// Unlike regular loads, extension to i64 is handled differently than i32.
1850b57cec5SDimitry Andric// i64 (zext (i8 (atomic_load_8))) gets legalized to
1860b57cec5SDimitry Andric// i64 (and (i64 (anyext (i32 (atomic_load_8)))), 255)
1870b57cec5SDimitry Andricdef zext_aload_8_64 :
1880b57cec5SDimitry Andric  PatFrag<(ops node:$addr),
1890b57cec5SDimitry Andric          (and (i64 (anyext (i32 (atomic_load_8 node:$addr)))), 255)>;
1900b57cec5SDimitry Andricdef zext_aload_16_64 :
1910b57cec5SDimitry Andric  PatFrag<(ops node:$addr),
1920b57cec5SDimitry Andric          (and (i64 (anyext (i32 (atomic_load_16 node:$addr)))), 65535)>;
1930b57cec5SDimitry Andricdef zext_aload_32_64 :
1940b57cec5SDimitry Andric  PatFrag<(ops node:$addr),
1950b57cec5SDimitry Andric          (zext (i32 (atomic_load node:$addr)))>;
1960b57cec5SDimitry Andric
1970b57cec5SDimitry Andric// We don't have single sext atomic load instructions. So for sext loads, we
1980b57cec5SDimitry Andric// match bare subword loads (for 32-bit results) and anyext loads (for 64-bit
1990b57cec5SDimitry Andric// results) and select a zext load; the next instruction will be sext_inreg
2000b57cec5SDimitry Andric// which is selected by itself.
2010b57cec5SDimitry Andricdef sext_aload_8_64 :
2020b57cec5SDimitry Andric  PatFrag<(ops node:$addr), (anyext (i32 (atomic_load_8 node:$addr)))>;
2030b57cec5SDimitry Andricdef sext_aload_16_64 :
2040b57cec5SDimitry Andric  PatFrag<(ops node:$addr), (anyext (i32 (atomic_load_16 node:$addr)))>;
2050b57cec5SDimitry Andric
2060b57cec5SDimitry Andriclet Predicates = [HasAtomics] in {
2070b57cec5SDimitry Andric// Select zero-extending loads with no constant offset.
2080b57cec5SDimitry Andricdef : LoadPatNoOffset<i32, zext_aload_8_32, ATOMIC_LOAD8_U_I32>;
2090b57cec5SDimitry Andricdef : LoadPatNoOffset<i32, zext_aload_16_32, ATOMIC_LOAD16_U_I32>;
2100b57cec5SDimitry Andricdef : LoadPatNoOffset<i64, zext_aload_8_64, ATOMIC_LOAD8_U_I64>;
2110b57cec5SDimitry Andricdef : LoadPatNoOffset<i64, zext_aload_16_64, ATOMIC_LOAD16_U_I64>;
2120b57cec5SDimitry Andricdef : LoadPatNoOffset<i64, zext_aload_32_64, ATOMIC_LOAD32_U_I64>;
2130b57cec5SDimitry Andric
2140b57cec5SDimitry Andric// Select sign-extending loads with no constant offset
2150b57cec5SDimitry Andricdef : LoadPatNoOffset<i32, atomic_load_8, ATOMIC_LOAD8_U_I32>;
2160b57cec5SDimitry Andricdef : LoadPatNoOffset<i32, atomic_load_16, ATOMIC_LOAD16_U_I32>;
2170b57cec5SDimitry Andricdef : LoadPatNoOffset<i64, sext_aload_8_64, ATOMIC_LOAD8_U_I64>;
2180b57cec5SDimitry Andricdef : LoadPatNoOffset<i64, sext_aload_16_64, ATOMIC_LOAD16_U_I64>;
2190b57cec5SDimitry Andric// 32->64 sext load gets selected as i32.atomic.load, i64.extend_i32_s
2200b57cec5SDimitry Andric
2210b57cec5SDimitry Andric// Zero-extending loads with constant offset
2220b57cec5SDimitry Andricdef : LoadPatImmOff<i32, zext_aload_8_32, regPlusImm, ATOMIC_LOAD8_U_I32>;
2230b57cec5SDimitry Andricdef : LoadPatImmOff<i32, zext_aload_16_32, regPlusImm, ATOMIC_LOAD16_U_I32>;
2240b57cec5SDimitry Andricdef : LoadPatImmOff<i32, zext_aload_8_32, or_is_add, ATOMIC_LOAD8_U_I32>;
2250b57cec5SDimitry Andricdef : LoadPatImmOff<i32, zext_aload_16_32, or_is_add, ATOMIC_LOAD16_U_I32>;
2260b57cec5SDimitry Andricdef : LoadPatImmOff<i64, zext_aload_8_64, regPlusImm, ATOMIC_LOAD8_U_I64>;
2270b57cec5SDimitry Andricdef : LoadPatImmOff<i64, zext_aload_16_64, regPlusImm, ATOMIC_LOAD16_U_I64>;
2280b57cec5SDimitry Andricdef : LoadPatImmOff<i64, zext_aload_32_64, regPlusImm, ATOMIC_LOAD32_U_I64>;
2290b57cec5SDimitry Andricdef : LoadPatImmOff<i64, zext_aload_8_64, or_is_add, ATOMIC_LOAD8_U_I64>;
2300b57cec5SDimitry Andricdef : LoadPatImmOff<i64, zext_aload_16_64, or_is_add, ATOMIC_LOAD16_U_I64>;
2310b57cec5SDimitry Andricdef : LoadPatImmOff<i64, zext_aload_32_64, or_is_add, ATOMIC_LOAD32_U_I64>;
2320b57cec5SDimitry Andric
2330b57cec5SDimitry Andric// Sign-extending loads with constant offset
2340b57cec5SDimitry Andricdef : LoadPatImmOff<i32, atomic_load_8, regPlusImm, ATOMIC_LOAD8_U_I32>;
2350b57cec5SDimitry Andricdef : LoadPatImmOff<i32, atomic_load_16, regPlusImm, ATOMIC_LOAD16_U_I32>;
2360b57cec5SDimitry Andricdef : LoadPatImmOff<i32, atomic_load_8, or_is_add, ATOMIC_LOAD8_U_I32>;
2370b57cec5SDimitry Andricdef : LoadPatImmOff<i32, atomic_load_16, or_is_add, ATOMIC_LOAD16_U_I32>;
2380b57cec5SDimitry Andricdef : LoadPatImmOff<i64, sext_aload_8_64, regPlusImm, ATOMIC_LOAD8_U_I64>;
2390b57cec5SDimitry Andricdef : LoadPatImmOff<i64, sext_aload_16_64, regPlusImm, ATOMIC_LOAD16_U_I64>;
2400b57cec5SDimitry Andricdef : LoadPatImmOff<i64, sext_aload_8_64, or_is_add, ATOMIC_LOAD8_U_I64>;
2410b57cec5SDimitry Andricdef : LoadPatImmOff<i64, sext_aload_16_64, or_is_add, ATOMIC_LOAD16_U_I64>;
2420b57cec5SDimitry Andric// No 32->64 patterns, just use i32.atomic.load and i64.extend_s/i64
2430b57cec5SDimitry Andric
2440b57cec5SDimitry Andric// Extending loads with just a constant offset
2450b57cec5SDimitry Andricdef : LoadPatOffsetOnly<i32, zext_aload_8_32, ATOMIC_LOAD8_U_I32>;
2460b57cec5SDimitry Andricdef : LoadPatOffsetOnly<i32, zext_aload_16_32, ATOMIC_LOAD16_U_I32>;
2470b57cec5SDimitry Andricdef : LoadPatOffsetOnly<i64, zext_aload_8_64, ATOMIC_LOAD8_U_I64>;
2480b57cec5SDimitry Andricdef : LoadPatOffsetOnly<i64, zext_aload_16_64, ATOMIC_LOAD16_U_I64>;
2490b57cec5SDimitry Andricdef : LoadPatOffsetOnly<i64, zext_aload_32_64, ATOMIC_LOAD32_U_I64>;
2500b57cec5SDimitry Andricdef : LoadPatOffsetOnly<i32, atomic_load_8, ATOMIC_LOAD8_U_I32>;
2510b57cec5SDimitry Andricdef : LoadPatOffsetOnly<i32, atomic_load_16, ATOMIC_LOAD16_U_I32>;
2520b57cec5SDimitry Andricdef : LoadPatOffsetOnly<i64, sext_aload_8_64, ATOMIC_LOAD8_U_I64>;
2530b57cec5SDimitry Andricdef : LoadPatOffsetOnly<i64, sext_aload_16_64, ATOMIC_LOAD16_U_I64>;
2540b57cec5SDimitry Andric
2550b57cec5SDimitry Andricdef : LoadPatGlobalAddrOffOnly<i32, zext_aload_8_32, ATOMIC_LOAD8_U_I32>;
2560b57cec5SDimitry Andricdef : LoadPatGlobalAddrOffOnly<i32, zext_aload_16_32, ATOMIC_LOAD16_U_I32>;
2570b57cec5SDimitry Andricdef : LoadPatGlobalAddrOffOnly<i64, zext_aload_8_64, ATOMIC_LOAD8_U_I64>;
2580b57cec5SDimitry Andricdef : LoadPatGlobalAddrOffOnly<i64, zext_aload_16_64, ATOMIC_LOAD16_U_I64>;
2590b57cec5SDimitry Andricdef : LoadPatGlobalAddrOffOnly<i64, zext_aload_32_64, ATOMIC_LOAD32_U_I64>;
2600b57cec5SDimitry Andricdef : LoadPatGlobalAddrOffOnly<i32, atomic_load_8, ATOMIC_LOAD8_U_I32>;
2610b57cec5SDimitry Andricdef : LoadPatGlobalAddrOffOnly<i32, atomic_load_16, ATOMIC_LOAD16_U_I32>;
2620b57cec5SDimitry Andricdef : LoadPatGlobalAddrOffOnly<i64, sext_aload_8_64, ATOMIC_LOAD8_U_I64>;
2630b57cec5SDimitry Andricdef : LoadPatGlobalAddrOffOnly<i64, sext_aload_16_64, ATOMIC_LOAD16_U_I64>;
2640b57cec5SDimitry Andric
2650b57cec5SDimitry Andric} // Predicates = [HasAtomics]
2660b57cec5SDimitry Andric
2670b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
2680b57cec5SDimitry Andric// Atomic stores
2690b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
2700b57cec5SDimitry Andric
2710b57cec5SDimitry Andricmulticlass AtomicStore<WebAssemblyRegClass rc, string name, int atomic_op> {
2720b57cec5SDimitry Andric  defm "" : WebAssemblyStore<rc, name, !or(0xfe00, !and(0xff, atomic_op))>,
2730b57cec5SDimitry Andric            Requires<[HasAtomics]>;
2740b57cec5SDimitry Andric}
2750b57cec5SDimitry Andric
2760b57cec5SDimitry Andricdefm ATOMIC_STORE_I32 : AtomicStore<I32, "i32.atomic.store", 0x17>;
2770b57cec5SDimitry Andricdefm ATOMIC_STORE_I64 : AtomicStore<I64, "i64.atomic.store", 0x18>;
2780b57cec5SDimitry Andric
2790b57cec5SDimitry Andric// We need an 'atomic' version of store patterns because store and atomic_store
2800b57cec5SDimitry Andric// nodes have different operand orders:
2810b57cec5SDimitry Andric// store: (store $val, $ptr)
2820b57cec5SDimitry Andric// atomic_store: (store $ptr, $val)
2830b57cec5SDimitry Andric
2840b57cec5SDimitry Andriclet Predicates = [HasAtomics] in {
2850b57cec5SDimitry Andric
2860b57cec5SDimitry Andric// Select stores with no constant offset.
2870b57cec5SDimitry Andricclass AStorePatNoOffset<ValueType ty, PatFrag kind, NI inst> :
2880b57cec5SDimitry Andric  Pat<(kind I32:$addr, ty:$val), (inst 0, 0, I32:$addr, ty:$val)>;
2890b57cec5SDimitry Andricdef : AStorePatNoOffset<i32, atomic_store_32, ATOMIC_STORE_I32>;
2900b57cec5SDimitry Andricdef : AStorePatNoOffset<i64, atomic_store_64, ATOMIC_STORE_I64>;
2910b57cec5SDimitry Andric
2920b57cec5SDimitry Andric// Select stores with a constant offset.
2930b57cec5SDimitry Andric
2940b57cec5SDimitry Andric// Pattern with address + immediate offset
2950b57cec5SDimitry Andricclass AStorePatImmOff<ValueType ty, PatFrag kind, PatFrag operand, NI inst> :
2960b57cec5SDimitry Andric  Pat<(kind (operand I32:$addr, imm:$off), ty:$val),
2970b57cec5SDimitry Andric      (inst 0, imm:$off, I32:$addr, ty:$val)>;
2980b57cec5SDimitry Andricdef : AStorePatImmOff<i32, atomic_store_32, regPlusImm, ATOMIC_STORE_I32>;
2990b57cec5SDimitry Andricdef : AStorePatImmOff<i64, atomic_store_64, regPlusImm, ATOMIC_STORE_I64>;
3000b57cec5SDimitry Andricdef : AStorePatImmOff<i32, atomic_store_32, or_is_add, ATOMIC_STORE_I32>;
3010b57cec5SDimitry Andricdef : AStorePatImmOff<i64, atomic_store_64, or_is_add, ATOMIC_STORE_I64>;
3020b57cec5SDimitry Andric
3030b57cec5SDimitry Andric// Select stores with just a constant offset.
3040b57cec5SDimitry Andricclass AStorePatOffsetOnly<ValueType ty, PatFrag kind, NI inst> :
3050b57cec5SDimitry Andric  Pat<(kind imm:$off, ty:$val), (inst 0, imm:$off, (CONST_I32 0), ty:$val)>;
3060b57cec5SDimitry Andricdef : AStorePatOffsetOnly<i32, atomic_store_32, ATOMIC_STORE_I32>;
3070b57cec5SDimitry Andricdef : AStorePatOffsetOnly<i64, atomic_store_64, ATOMIC_STORE_I64>;
3080b57cec5SDimitry Andric
3090b57cec5SDimitry Andricclass AStorePatGlobalAddrOffOnly<ValueType ty, PatFrag kind, NI inst> :
3100b57cec5SDimitry Andric  Pat<(kind (WebAssemblywrapper tglobaladdr:$off), ty:$val),
3110b57cec5SDimitry Andric      (inst 0, tglobaladdr:$off, (CONST_I32 0), ty:$val)>;
3120b57cec5SDimitry Andricdef : AStorePatGlobalAddrOffOnly<i32, atomic_store_32, ATOMIC_STORE_I32>;
3130b57cec5SDimitry Andricdef : AStorePatGlobalAddrOffOnly<i64, atomic_store_64, ATOMIC_STORE_I64>;
3140b57cec5SDimitry Andric
3150b57cec5SDimitry Andric} // Predicates = [HasAtomics]
3160b57cec5SDimitry Andric
3170b57cec5SDimitry Andric// Truncating stores.
3180b57cec5SDimitry Andricdefm ATOMIC_STORE8_I32 : AtomicStore<I32, "i32.atomic.store8", 0x19>;
3190b57cec5SDimitry Andricdefm ATOMIC_STORE16_I32 : AtomicStore<I32, "i32.atomic.store16", 0x1a>;
3200b57cec5SDimitry Andricdefm ATOMIC_STORE8_I64 : AtomicStore<I64, "i64.atomic.store8", 0x1b>;
3210b57cec5SDimitry Andricdefm ATOMIC_STORE16_I64 : AtomicStore<I64, "i64.atomic.store16", 0x1c>;
3220b57cec5SDimitry Andricdefm ATOMIC_STORE32_I64 : AtomicStore<I64, "i64.atomic.store32", 0x1d>;
3230b57cec5SDimitry Andric
3240b57cec5SDimitry Andric// Fragments for truncating stores.
3250b57cec5SDimitry Andric
3260b57cec5SDimitry Andric// We don't have single truncating atomic store instructions. For 32-bit
3270b57cec5SDimitry Andric// instructions, we just need to match bare atomic stores. On the other hand,
3280b57cec5SDimitry Andric// truncating stores from i64 values are once truncated to i32 first.
3290b57cec5SDimitry Andricclass trunc_astore_64<PatFrag kind> :
3300b57cec5SDimitry Andric  PatFrag<(ops node:$addr, node:$val),
3310b57cec5SDimitry Andric          (kind node:$addr, (i32 (trunc (i64 node:$val))))>;
3320b57cec5SDimitry Andricdef trunc_astore_8_64 : trunc_astore_64<atomic_store_8>;
3330b57cec5SDimitry Andricdef trunc_astore_16_64 : trunc_astore_64<atomic_store_16>;
3340b57cec5SDimitry Andricdef trunc_astore_32_64 : trunc_astore_64<atomic_store_32>;
3350b57cec5SDimitry Andric
3360b57cec5SDimitry Andriclet Predicates = [HasAtomics] in {
3370b57cec5SDimitry Andric
3380b57cec5SDimitry Andric// Truncating stores with no constant offset
3390b57cec5SDimitry Andricdef : AStorePatNoOffset<i32, atomic_store_8, ATOMIC_STORE8_I32>;
3400b57cec5SDimitry Andricdef : AStorePatNoOffset<i32, atomic_store_16, ATOMIC_STORE16_I32>;
3410b57cec5SDimitry Andricdef : AStorePatNoOffset<i64, trunc_astore_8_64, ATOMIC_STORE8_I64>;
3420b57cec5SDimitry Andricdef : AStorePatNoOffset<i64, trunc_astore_16_64, ATOMIC_STORE16_I64>;
3430b57cec5SDimitry Andricdef : AStorePatNoOffset<i64, trunc_astore_32_64, ATOMIC_STORE32_I64>;
3440b57cec5SDimitry Andric
3450b57cec5SDimitry Andric// Truncating stores with a constant offset
3460b57cec5SDimitry Andricdef : AStorePatImmOff<i32, atomic_store_8, regPlusImm, ATOMIC_STORE8_I32>;
3470b57cec5SDimitry Andricdef : AStorePatImmOff<i32, atomic_store_16, regPlusImm, ATOMIC_STORE16_I32>;
3480b57cec5SDimitry Andricdef : AStorePatImmOff<i64, trunc_astore_8_64, regPlusImm, ATOMIC_STORE8_I64>;
3490b57cec5SDimitry Andricdef : AStorePatImmOff<i64, trunc_astore_16_64, regPlusImm, ATOMIC_STORE16_I64>;
3500b57cec5SDimitry Andricdef : AStorePatImmOff<i64, trunc_astore_32_64, regPlusImm, ATOMIC_STORE32_I64>;
3510b57cec5SDimitry Andricdef : AStorePatImmOff<i32, atomic_store_8, or_is_add, ATOMIC_STORE8_I32>;
3520b57cec5SDimitry Andricdef : AStorePatImmOff<i32, atomic_store_16, or_is_add, ATOMIC_STORE16_I32>;
3530b57cec5SDimitry Andricdef : AStorePatImmOff<i64, trunc_astore_8_64, or_is_add, ATOMIC_STORE8_I64>;
3540b57cec5SDimitry Andricdef : AStorePatImmOff<i64, trunc_astore_16_64, or_is_add, ATOMIC_STORE16_I64>;
3550b57cec5SDimitry Andricdef : AStorePatImmOff<i64, trunc_astore_32_64, or_is_add, ATOMIC_STORE32_I64>;
3560b57cec5SDimitry Andric
3570b57cec5SDimitry Andric// Truncating stores with just a constant offset
3580b57cec5SDimitry Andricdef : AStorePatOffsetOnly<i32, atomic_store_8, ATOMIC_STORE8_I32>;
3590b57cec5SDimitry Andricdef : AStorePatOffsetOnly<i32, atomic_store_16, ATOMIC_STORE16_I32>;
3600b57cec5SDimitry Andricdef : AStorePatOffsetOnly<i64, trunc_astore_8_64, ATOMIC_STORE8_I64>;
3610b57cec5SDimitry Andricdef : AStorePatOffsetOnly<i64, trunc_astore_16_64, ATOMIC_STORE16_I64>;
3620b57cec5SDimitry Andricdef : AStorePatOffsetOnly<i64, trunc_astore_32_64, ATOMIC_STORE32_I64>;
3630b57cec5SDimitry Andric
3640b57cec5SDimitry Andricdef : AStorePatGlobalAddrOffOnly<i32, atomic_store_8, ATOMIC_STORE8_I32>;
3650b57cec5SDimitry Andricdef : AStorePatGlobalAddrOffOnly<i32, atomic_store_16, ATOMIC_STORE16_I32>;
3660b57cec5SDimitry Andricdef : AStorePatGlobalAddrOffOnly<i64, trunc_astore_8_64, ATOMIC_STORE8_I64>;
3670b57cec5SDimitry Andricdef : AStorePatGlobalAddrOffOnly<i64, trunc_astore_16_64, ATOMIC_STORE16_I64>;
3680b57cec5SDimitry Andricdef : AStorePatGlobalAddrOffOnly<i64, trunc_astore_32_64, ATOMIC_STORE32_I64>;
3690b57cec5SDimitry Andric
3700b57cec5SDimitry Andric} // Predicates = [HasAtomics]
3710b57cec5SDimitry Andric
3720b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
3730b57cec5SDimitry Andric// Atomic binary read-modify-writes
3740b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
3750b57cec5SDimitry Andric
3760b57cec5SDimitry Andricmulticlass WebAssemblyBinRMW<WebAssemblyRegClass rc, string name,
3770b57cec5SDimitry Andric                             int atomic_op> {
3780b57cec5SDimitry Andric  defm "" :
3790b57cec5SDimitry Andric    ATOMIC_I<(outs rc:$dst),
3800b57cec5SDimitry Andric             (ins P2Align:$p2align, offset32_op:$off, I32:$addr, rc:$val),
3810b57cec5SDimitry Andric             (outs), (ins P2Align:$p2align, offset32_op:$off), [],
3820b57cec5SDimitry Andric             !strconcat(name, "\t$dst, ${off}(${addr})${p2align}, $val"),
3830b57cec5SDimitry Andric             !strconcat(name, "\t${off}${p2align}"), atomic_op>;
3840b57cec5SDimitry Andric}
3850b57cec5SDimitry Andric
3860b57cec5SDimitry Andricdefm ATOMIC_RMW_ADD_I32 : WebAssemblyBinRMW<I32, "i32.atomic.rmw.add", 0x1e>;
3870b57cec5SDimitry Andricdefm ATOMIC_RMW_ADD_I64 : WebAssemblyBinRMW<I64, "i64.atomic.rmw.add", 0x1f>;
3880b57cec5SDimitry Andricdefm ATOMIC_RMW8_U_ADD_I32 :
3890b57cec5SDimitry Andric  WebAssemblyBinRMW<I32, "i32.atomic.rmw8.add_u", 0x20>;
3900b57cec5SDimitry Andricdefm ATOMIC_RMW16_U_ADD_I32 :
3910b57cec5SDimitry Andric  WebAssemblyBinRMW<I32, "i32.atomic.rmw16.add_u", 0x21>;
3920b57cec5SDimitry Andricdefm ATOMIC_RMW8_U_ADD_I64 :
3930b57cec5SDimitry Andric  WebAssemblyBinRMW<I64, "i64.atomic.rmw8.add_u", 0x22>;
3940b57cec5SDimitry Andricdefm ATOMIC_RMW16_U_ADD_I64 :
3950b57cec5SDimitry Andric  WebAssemblyBinRMW<I64, "i64.atomic.rmw16.add_u", 0x23>;
3960b57cec5SDimitry Andricdefm ATOMIC_RMW32_U_ADD_I64 :
3970b57cec5SDimitry Andric  WebAssemblyBinRMW<I64, "i64.atomic.rmw32.add_u", 0x24>;
3980b57cec5SDimitry Andric
3990b57cec5SDimitry Andricdefm ATOMIC_RMW_SUB_I32 : WebAssemblyBinRMW<I32, "i32.atomic.rmw.sub", 0x25>;
4000b57cec5SDimitry Andricdefm ATOMIC_RMW_SUB_I64 : WebAssemblyBinRMW<I64, "i64.atomic.rmw.sub", 0x26>;
4010b57cec5SDimitry Andricdefm ATOMIC_RMW8_U_SUB_I32 :
4020b57cec5SDimitry Andric  WebAssemblyBinRMW<I32, "i32.atomic.rmw8.sub_u", 0x27>;
4030b57cec5SDimitry Andricdefm ATOMIC_RMW16_U_SUB_I32 :
4040b57cec5SDimitry Andric  WebAssemblyBinRMW<I32, "i32.atomic.rmw16.sub_u", 0x28>;
4050b57cec5SDimitry Andricdefm ATOMIC_RMW8_U_SUB_I64 :
4060b57cec5SDimitry Andric  WebAssemblyBinRMW<I64, "i64.atomic.rmw8.sub_u", 0x29>;
4070b57cec5SDimitry Andricdefm ATOMIC_RMW16_U_SUB_I64 :
4080b57cec5SDimitry Andric  WebAssemblyBinRMW<I64, "i64.atomic.rmw16.sub_u", 0x2a>;
4090b57cec5SDimitry Andricdefm ATOMIC_RMW32_U_SUB_I64 :
4100b57cec5SDimitry Andric  WebAssemblyBinRMW<I64, "i64.atomic.rmw32.sub_u", 0x2b>;
4110b57cec5SDimitry Andric
4120b57cec5SDimitry Andricdefm ATOMIC_RMW_AND_I32 : WebAssemblyBinRMW<I32, "i32.atomic.rmw.and", 0x2c>;
4130b57cec5SDimitry Andricdefm ATOMIC_RMW_AND_I64 : WebAssemblyBinRMW<I64, "i64.atomic.rmw.and", 0x2d>;
4140b57cec5SDimitry Andricdefm ATOMIC_RMW8_U_AND_I32 :
4150b57cec5SDimitry Andric  WebAssemblyBinRMW<I32, "i32.atomic.rmw8.and_u", 0x2e>;
4160b57cec5SDimitry Andricdefm ATOMIC_RMW16_U_AND_I32 :
4170b57cec5SDimitry Andric  WebAssemblyBinRMW<I32, "i32.atomic.rmw16.and_u", 0x2f>;
4180b57cec5SDimitry Andricdefm ATOMIC_RMW8_U_AND_I64 :
4190b57cec5SDimitry Andric  WebAssemblyBinRMW<I64, "i64.atomic.rmw8.and_u", 0x30>;
4200b57cec5SDimitry Andricdefm ATOMIC_RMW16_U_AND_I64 :
4210b57cec5SDimitry Andric  WebAssemblyBinRMW<I64, "i64.atomic.rmw16.and_u", 0x31>;
4220b57cec5SDimitry Andricdefm ATOMIC_RMW32_U_AND_I64 :
4230b57cec5SDimitry Andric  WebAssemblyBinRMW<I64, "i64.atomic.rmw32.and_u", 0x32>;
4240b57cec5SDimitry Andric
4250b57cec5SDimitry Andricdefm ATOMIC_RMW_OR_I32 : WebAssemblyBinRMW<I32, "i32.atomic.rmw.or", 0x33>;
4260b57cec5SDimitry Andricdefm ATOMIC_RMW_OR_I64 : WebAssemblyBinRMW<I64, "i64.atomic.rmw.or", 0x34>;
4270b57cec5SDimitry Andricdefm ATOMIC_RMW8_U_OR_I32 :
4280b57cec5SDimitry Andric  WebAssemblyBinRMW<I32, "i32.atomic.rmw8.or_u", 0x35>;
4290b57cec5SDimitry Andricdefm ATOMIC_RMW16_U_OR_I32 :
4300b57cec5SDimitry Andric  WebAssemblyBinRMW<I32, "i32.atomic.rmw16.or_u", 0x36>;
4310b57cec5SDimitry Andricdefm ATOMIC_RMW8_U_OR_I64 :
4320b57cec5SDimitry Andric  WebAssemblyBinRMW<I64, "i64.atomic.rmw8.or_u", 0x37>;
4330b57cec5SDimitry Andricdefm ATOMIC_RMW16_U_OR_I64 :
4340b57cec5SDimitry Andric  WebAssemblyBinRMW<I64, "i64.atomic.rmw16.or_u", 0x38>;
4350b57cec5SDimitry Andricdefm ATOMIC_RMW32_U_OR_I64 :
4360b57cec5SDimitry Andric  WebAssemblyBinRMW<I64, "i64.atomic.rmw32.or_u", 0x39>;
4370b57cec5SDimitry Andric
4380b57cec5SDimitry Andricdefm ATOMIC_RMW_XOR_I32 : WebAssemblyBinRMW<I32, "i32.atomic.rmw.xor", 0x3a>;
4390b57cec5SDimitry Andricdefm ATOMIC_RMW_XOR_I64 : WebAssemblyBinRMW<I64, "i64.atomic.rmw.xor", 0x3b>;
4400b57cec5SDimitry Andricdefm ATOMIC_RMW8_U_XOR_I32 :
4410b57cec5SDimitry Andric  WebAssemblyBinRMW<I32, "i32.atomic.rmw8.xor_u", 0x3c>;
4420b57cec5SDimitry Andricdefm ATOMIC_RMW16_U_XOR_I32 :
4430b57cec5SDimitry Andric  WebAssemblyBinRMW<I32, "i32.atomic.rmw16.xor_u", 0x3d>;
4440b57cec5SDimitry Andricdefm ATOMIC_RMW8_U_XOR_I64 :
4450b57cec5SDimitry Andric  WebAssemblyBinRMW<I64, "i64.atomic.rmw8.xor_u", 0x3e>;
4460b57cec5SDimitry Andricdefm ATOMIC_RMW16_U_XOR_I64 :
4470b57cec5SDimitry Andric  WebAssemblyBinRMW<I64, "i64.atomic.rmw16.xor_u", 0x3f>;
4480b57cec5SDimitry Andricdefm ATOMIC_RMW32_U_XOR_I64 :
4490b57cec5SDimitry Andric  WebAssemblyBinRMW<I64, "i64.atomic.rmw32.xor_u", 0x40>;
4500b57cec5SDimitry Andric
4510b57cec5SDimitry Andricdefm ATOMIC_RMW_XCHG_I32 :
4520b57cec5SDimitry Andric  WebAssemblyBinRMW<I32, "i32.atomic.rmw.xchg", 0x41>;
4530b57cec5SDimitry Andricdefm ATOMIC_RMW_XCHG_I64 :
4540b57cec5SDimitry Andric  WebAssemblyBinRMW<I64, "i64.atomic.rmw.xchg", 0x42>;
4550b57cec5SDimitry Andricdefm ATOMIC_RMW8_U_XCHG_I32 :
4560b57cec5SDimitry Andric  WebAssemblyBinRMW<I32, "i32.atomic.rmw8.xchg_u", 0x43>;
4570b57cec5SDimitry Andricdefm ATOMIC_RMW16_U_XCHG_I32 :
4580b57cec5SDimitry Andric  WebAssemblyBinRMW<I32, "i32.atomic.rmw16.xchg_u", 0x44>;
4590b57cec5SDimitry Andricdefm ATOMIC_RMW8_U_XCHG_I64 :
4600b57cec5SDimitry Andric  WebAssemblyBinRMW<I64, "i64.atomic.rmw8.xchg_u", 0x45>;
4610b57cec5SDimitry Andricdefm ATOMIC_RMW16_U_XCHG_I64 :
4620b57cec5SDimitry Andric  WebAssemblyBinRMW<I64, "i64.atomic.rmw16.xchg_u", 0x46>;
4630b57cec5SDimitry Andricdefm ATOMIC_RMW32_U_XCHG_I64 :
4640b57cec5SDimitry Andric  WebAssemblyBinRMW<I64, "i64.atomic.rmw32.xchg_u", 0x47>;
4650b57cec5SDimitry Andric
4660b57cec5SDimitry Andric// Select binary RMWs with no constant offset.
4670b57cec5SDimitry Andricclass BinRMWPatNoOffset<ValueType ty, PatFrag kind, NI inst> :
4680b57cec5SDimitry Andric  Pat<(ty (kind I32:$addr, ty:$val)), (inst 0, 0, I32:$addr, ty:$val)>;
4690b57cec5SDimitry Andric
4700b57cec5SDimitry Andric// Select binary RMWs with a constant offset.
4710b57cec5SDimitry Andric
4720b57cec5SDimitry Andric// Pattern with address + immediate offset
4730b57cec5SDimitry Andricclass BinRMWPatImmOff<ValueType ty, PatFrag kind, PatFrag operand, NI inst> :
4740b57cec5SDimitry Andric  Pat<(ty (kind (operand I32:$addr, imm:$off), ty:$val)),
4750b57cec5SDimitry Andric      (inst 0, imm:$off, I32:$addr, ty:$val)>;
4760b57cec5SDimitry Andric
4770b57cec5SDimitry Andric// Select binary RMWs with just a constant offset.
4780b57cec5SDimitry Andricclass BinRMWPatOffsetOnly<ValueType ty, PatFrag kind, NI inst> :
4790b57cec5SDimitry Andric  Pat<(ty (kind imm:$off, ty:$val)),
4800b57cec5SDimitry Andric      (inst 0, imm:$off, (CONST_I32 0), ty:$val)>;
4810b57cec5SDimitry Andric
4820b57cec5SDimitry Andricclass BinRMWPatGlobalAddrOffOnly<ValueType ty, PatFrag kind, NI inst> :
4830b57cec5SDimitry Andric  Pat<(ty (kind (WebAssemblywrapper tglobaladdr:$off), ty:$val)),
4840b57cec5SDimitry Andric      (inst 0, tglobaladdr:$off, (CONST_I32 0), ty:$val)>;
4850b57cec5SDimitry Andric
4860b57cec5SDimitry Andric// Patterns for various addressing modes.
4870b57cec5SDimitry Andricmulticlass BinRMWPattern<PatFrag rmw_32, PatFrag rmw_64, NI inst_32,
4880b57cec5SDimitry Andric                         NI inst_64> {
4890b57cec5SDimitry Andric  def : BinRMWPatNoOffset<i32, rmw_32, inst_32>;
4900b57cec5SDimitry Andric  def : BinRMWPatNoOffset<i64, rmw_64, inst_64>;
4910b57cec5SDimitry Andric
4920b57cec5SDimitry Andric  def : BinRMWPatImmOff<i32, rmw_32, regPlusImm, inst_32>;
4930b57cec5SDimitry Andric  def : BinRMWPatImmOff<i64, rmw_64, regPlusImm, inst_64>;
4940b57cec5SDimitry Andric  def : BinRMWPatImmOff<i32, rmw_32, or_is_add, inst_32>;
4950b57cec5SDimitry Andric  def : BinRMWPatImmOff<i64, rmw_64, or_is_add, inst_64>;
4960b57cec5SDimitry Andric
4970b57cec5SDimitry Andric  def : BinRMWPatOffsetOnly<i32, rmw_32, inst_32>;
4980b57cec5SDimitry Andric  def : BinRMWPatOffsetOnly<i64, rmw_64, inst_64>;
4990b57cec5SDimitry Andric
5000b57cec5SDimitry Andric  def : BinRMWPatGlobalAddrOffOnly<i32, rmw_32, inst_32>;
5010b57cec5SDimitry Andric  def : BinRMWPatGlobalAddrOffOnly<i64, rmw_64, inst_64>;
5020b57cec5SDimitry Andric}
5030b57cec5SDimitry Andric
5040b57cec5SDimitry Andriclet Predicates = [HasAtomics] in {
5050b57cec5SDimitry Andricdefm : BinRMWPattern<atomic_load_add_32, atomic_load_add_64, ATOMIC_RMW_ADD_I32,
5060b57cec5SDimitry Andric                     ATOMIC_RMW_ADD_I64>;
5070b57cec5SDimitry Andricdefm : BinRMWPattern<atomic_load_sub_32, atomic_load_sub_64, ATOMIC_RMW_SUB_I32,
5080b57cec5SDimitry Andric                     ATOMIC_RMW_SUB_I64>;
5090b57cec5SDimitry Andricdefm : BinRMWPattern<atomic_load_and_32, atomic_load_and_64, ATOMIC_RMW_AND_I32,
5100b57cec5SDimitry Andric                     ATOMIC_RMW_AND_I64>;
5110b57cec5SDimitry Andricdefm : BinRMWPattern<atomic_load_or_32, atomic_load_or_64, ATOMIC_RMW_OR_I32,
5120b57cec5SDimitry Andric                     ATOMIC_RMW_OR_I64>;
5130b57cec5SDimitry Andricdefm : BinRMWPattern<atomic_load_xor_32, atomic_load_xor_64, ATOMIC_RMW_XOR_I32,
5140b57cec5SDimitry Andric                     ATOMIC_RMW_XOR_I64>;
5150b57cec5SDimitry Andricdefm : BinRMWPattern<atomic_swap_32, atomic_swap_64, ATOMIC_RMW_XCHG_I32,
5160b57cec5SDimitry Andric                     ATOMIC_RMW_XCHG_I64>;
5170b57cec5SDimitry Andric} // Predicates = [HasAtomics]
5180b57cec5SDimitry Andric
5190b57cec5SDimitry Andric// Truncating & zero-extending binary RMW patterns.
5200b57cec5SDimitry Andric// These are combined patterns of truncating store patterns and zero-extending
5210b57cec5SDimitry Andric// load patterns above.
5220b57cec5SDimitry Andricclass zext_bin_rmw_8_32<PatFrag kind> :
5230b57cec5SDimitry Andric  PatFrag<(ops node:$addr, node:$val),
5240b57cec5SDimitry Andric          (and (i32 (kind node:$addr, node:$val)), 255)>;
5250b57cec5SDimitry Andricclass zext_bin_rmw_16_32<PatFrag kind> :
5260b57cec5SDimitry Andric  PatFrag<(ops node:$addr, node:$val),
5270b57cec5SDimitry Andric          (and (i32 (kind node:$addr, node:$val)), 65535)>;
5280b57cec5SDimitry Andricclass zext_bin_rmw_8_64<PatFrag kind> :
5290b57cec5SDimitry Andric  PatFrag<(ops node:$addr, node:$val),
5300b57cec5SDimitry Andric    (and (i64 (anyext (i32 (kind node:$addr,
5310b57cec5SDimitry Andric                                 (i32 (trunc (i64 node:$val))))))), 255)>;
5320b57cec5SDimitry Andricclass zext_bin_rmw_16_64<PatFrag kind> :
5330b57cec5SDimitry Andric  PatFrag<(ops node:$addr, node:$val),
5340b57cec5SDimitry Andric    (and (i64 (anyext (i32 (kind node:$addr,
5350b57cec5SDimitry Andric                                 (i32 (trunc (i64 node:$val))))))), 65535)>;
5360b57cec5SDimitry Andricclass zext_bin_rmw_32_64<PatFrag kind> :
5370b57cec5SDimitry Andric  PatFrag<(ops node:$addr, node:$val),
5380b57cec5SDimitry Andric          (zext (i32 (kind node:$addr, (i32 (trunc (i64 node:$val))))))>;
5390b57cec5SDimitry Andric
5400b57cec5SDimitry Andric// Truncating & sign-extending binary RMW patterns.
5410b57cec5SDimitry Andric// These are combined patterns of truncating store patterns and sign-extending
5420b57cec5SDimitry Andric// load patterns above. We match subword RMWs (for 32-bit) and anyext RMWs (for
5430b57cec5SDimitry Andric// 64-bit) and select a zext RMW; the next instruction will be sext_inreg which
5440b57cec5SDimitry Andric// is selected by itself.
5450b57cec5SDimitry Andricclass sext_bin_rmw_8_32<PatFrag kind> :
5460b57cec5SDimitry Andric  PatFrag<(ops node:$addr, node:$val), (kind node:$addr, node:$val)>;
5470b57cec5SDimitry Andricclass sext_bin_rmw_16_32<PatFrag kind> : sext_bin_rmw_8_32<kind>;
5480b57cec5SDimitry Andricclass sext_bin_rmw_8_64<PatFrag kind> :
5490b57cec5SDimitry Andric  PatFrag<(ops node:$addr, node:$val),
5500b57cec5SDimitry Andric          (anyext (i32 (kind node:$addr, (i32 (trunc (i64 node:$val))))))>;
5510b57cec5SDimitry Andricclass sext_bin_rmw_16_64<PatFrag kind> : sext_bin_rmw_8_64<kind>;
5520b57cec5SDimitry Andric// 32->64 sext RMW gets selected as i32.atomic.rmw.***, i64.extend_i32_s
5530b57cec5SDimitry Andric
5540b57cec5SDimitry Andric// Patterns for various addressing modes for truncating-extending binary RMWs.
5550b57cec5SDimitry Andricmulticlass BinRMWTruncExtPattern<
5560b57cec5SDimitry Andric  PatFrag rmw_8, PatFrag rmw_16, PatFrag rmw_32, PatFrag rmw_64,
5570b57cec5SDimitry Andric  NI inst8_32, NI inst16_32, NI inst8_64, NI inst16_64, NI inst32_64> {
5580b57cec5SDimitry Andric  // Truncating-extending binary RMWs with no constant offset
5590b57cec5SDimitry Andric  def : BinRMWPatNoOffset<i32, zext_bin_rmw_8_32<rmw_8>, inst8_32>;
5600b57cec5SDimitry Andric  def : BinRMWPatNoOffset<i32, zext_bin_rmw_16_32<rmw_16>, inst16_32>;
5610b57cec5SDimitry Andric  def : BinRMWPatNoOffset<i64, zext_bin_rmw_8_64<rmw_8>, inst8_64>;
5620b57cec5SDimitry Andric  def : BinRMWPatNoOffset<i64, zext_bin_rmw_16_64<rmw_16>, inst16_64>;
5630b57cec5SDimitry Andric  def : BinRMWPatNoOffset<i64, zext_bin_rmw_32_64<rmw_32>, inst32_64>;
5640b57cec5SDimitry Andric
5650b57cec5SDimitry Andric  def : BinRMWPatNoOffset<i32, sext_bin_rmw_8_32<rmw_8>, inst8_32>;
5660b57cec5SDimitry Andric  def : BinRMWPatNoOffset<i32, sext_bin_rmw_16_32<rmw_16>, inst16_32>;
5670b57cec5SDimitry Andric  def : BinRMWPatNoOffset<i64, sext_bin_rmw_8_64<rmw_8>, inst8_64>;
5680b57cec5SDimitry Andric  def : BinRMWPatNoOffset<i64, sext_bin_rmw_16_64<rmw_16>, inst16_64>;
5690b57cec5SDimitry Andric
5700b57cec5SDimitry Andric  // Truncating-extending binary RMWs with a constant offset
5710b57cec5SDimitry Andric  def : BinRMWPatImmOff<i32, zext_bin_rmw_8_32<rmw_8>, regPlusImm, inst8_32>;
5720b57cec5SDimitry Andric  def : BinRMWPatImmOff<i32, zext_bin_rmw_16_32<rmw_16>, regPlusImm, inst16_32>;
5730b57cec5SDimitry Andric  def : BinRMWPatImmOff<i64, zext_bin_rmw_8_64<rmw_8>, regPlusImm, inst8_64>;
5740b57cec5SDimitry Andric  def : BinRMWPatImmOff<i64, zext_bin_rmw_16_64<rmw_16>, regPlusImm, inst16_64>;
5750b57cec5SDimitry Andric  def : BinRMWPatImmOff<i64, zext_bin_rmw_32_64<rmw_32>, regPlusImm, inst32_64>;
5760b57cec5SDimitry Andric  def : BinRMWPatImmOff<i32, zext_bin_rmw_8_32<rmw_8>, or_is_add, inst8_32>;
5770b57cec5SDimitry Andric  def : BinRMWPatImmOff<i32, zext_bin_rmw_16_32<rmw_16>, or_is_add, inst16_32>;
5780b57cec5SDimitry Andric  def : BinRMWPatImmOff<i64, zext_bin_rmw_8_64<rmw_8>, or_is_add, inst8_64>;
5790b57cec5SDimitry Andric  def : BinRMWPatImmOff<i64, zext_bin_rmw_16_64<rmw_16>, or_is_add, inst16_64>;
5800b57cec5SDimitry Andric  def : BinRMWPatImmOff<i64, zext_bin_rmw_32_64<rmw_32>, or_is_add, inst32_64>;
5810b57cec5SDimitry Andric
5820b57cec5SDimitry Andric  def : BinRMWPatImmOff<i32, sext_bin_rmw_8_32<rmw_8>, regPlusImm, inst8_32>;
5830b57cec5SDimitry Andric  def : BinRMWPatImmOff<i32, sext_bin_rmw_16_32<rmw_16>, regPlusImm, inst16_32>;
5840b57cec5SDimitry Andric  def : BinRMWPatImmOff<i64, sext_bin_rmw_8_64<rmw_8>, regPlusImm, inst8_64>;
5850b57cec5SDimitry Andric  def : BinRMWPatImmOff<i64, sext_bin_rmw_16_64<rmw_16>, regPlusImm, inst16_64>;
5860b57cec5SDimitry Andric  def : BinRMWPatImmOff<i32, sext_bin_rmw_8_32<rmw_8>, or_is_add, inst8_32>;
5870b57cec5SDimitry Andric  def : BinRMWPatImmOff<i32, sext_bin_rmw_16_32<rmw_16>, or_is_add, inst16_32>;
5880b57cec5SDimitry Andric  def : BinRMWPatImmOff<i64, sext_bin_rmw_8_64<rmw_8>, or_is_add, inst8_64>;
5890b57cec5SDimitry Andric  def : BinRMWPatImmOff<i64, sext_bin_rmw_16_64<rmw_16>, or_is_add, inst16_64>;
5900b57cec5SDimitry Andric
5910b57cec5SDimitry Andric  // Truncating-extending binary RMWs with just a constant offset
5920b57cec5SDimitry Andric  def : BinRMWPatOffsetOnly<i32, zext_bin_rmw_8_32<rmw_8>, inst8_32>;
5930b57cec5SDimitry Andric  def : BinRMWPatOffsetOnly<i32, zext_bin_rmw_16_32<rmw_16>, inst16_32>;
5940b57cec5SDimitry Andric  def : BinRMWPatOffsetOnly<i64, zext_bin_rmw_8_64<rmw_8>, inst8_64>;
5950b57cec5SDimitry Andric  def : BinRMWPatOffsetOnly<i64, zext_bin_rmw_16_64<rmw_16>, inst16_64>;
5960b57cec5SDimitry Andric  def : BinRMWPatOffsetOnly<i64, zext_bin_rmw_32_64<rmw_32>, inst32_64>;
5970b57cec5SDimitry Andric
5980b57cec5SDimitry Andric  def : BinRMWPatOffsetOnly<i32, sext_bin_rmw_8_32<rmw_8>, inst8_32>;
5990b57cec5SDimitry Andric  def : BinRMWPatOffsetOnly<i32, sext_bin_rmw_16_32<rmw_16>, inst16_32>;
6000b57cec5SDimitry Andric  def : BinRMWPatOffsetOnly<i64, sext_bin_rmw_8_64<rmw_8>, inst8_64>;
6010b57cec5SDimitry Andric  def : BinRMWPatOffsetOnly<i64, sext_bin_rmw_16_64<rmw_16>, inst16_64>;
6020b57cec5SDimitry Andric
6030b57cec5SDimitry Andric  def : BinRMWPatGlobalAddrOffOnly<i32, zext_bin_rmw_8_32<rmw_8>, inst8_32>;
6040b57cec5SDimitry Andric  def : BinRMWPatGlobalAddrOffOnly<i32, zext_bin_rmw_16_32<rmw_16>, inst16_32>;
6050b57cec5SDimitry Andric  def : BinRMWPatGlobalAddrOffOnly<i64, zext_bin_rmw_8_64<rmw_8>, inst8_64>;
6060b57cec5SDimitry Andric  def : BinRMWPatGlobalAddrOffOnly<i64, zext_bin_rmw_16_64<rmw_16>, inst16_64>;
6070b57cec5SDimitry Andric  def : BinRMWPatGlobalAddrOffOnly<i64, zext_bin_rmw_32_64<rmw_32>, inst32_64>;
6080b57cec5SDimitry Andric
6090b57cec5SDimitry Andric  def : BinRMWPatGlobalAddrOffOnly<i32, sext_bin_rmw_8_32<rmw_8>, inst8_32>;
6100b57cec5SDimitry Andric  def : BinRMWPatGlobalAddrOffOnly<i32, sext_bin_rmw_16_32<rmw_16>, inst16_32>;
6110b57cec5SDimitry Andric  def : BinRMWPatGlobalAddrOffOnly<i64, sext_bin_rmw_8_64<rmw_8>, inst8_64>;
6120b57cec5SDimitry Andric  def : BinRMWPatGlobalAddrOffOnly<i64, sext_bin_rmw_16_64<rmw_16>, inst16_64>;
6130b57cec5SDimitry Andric}
6140b57cec5SDimitry Andric
6150b57cec5SDimitry Andriclet Predicates = [HasAtomics] in {
6160b57cec5SDimitry Andricdefm : BinRMWTruncExtPattern<
6170b57cec5SDimitry Andric  atomic_load_add_8, atomic_load_add_16, atomic_load_add_32, atomic_load_add_64,
6180b57cec5SDimitry Andric  ATOMIC_RMW8_U_ADD_I32, ATOMIC_RMW16_U_ADD_I32,
6190b57cec5SDimitry Andric  ATOMIC_RMW8_U_ADD_I64, ATOMIC_RMW16_U_ADD_I64, ATOMIC_RMW32_U_ADD_I64>;
6200b57cec5SDimitry Andricdefm : BinRMWTruncExtPattern<
6210b57cec5SDimitry Andric  atomic_load_sub_8, atomic_load_sub_16, atomic_load_sub_32, atomic_load_sub_64,
6220b57cec5SDimitry Andric  ATOMIC_RMW8_U_SUB_I32, ATOMIC_RMW16_U_SUB_I32,
6230b57cec5SDimitry Andric  ATOMIC_RMW8_U_SUB_I64, ATOMIC_RMW16_U_SUB_I64, ATOMIC_RMW32_U_SUB_I64>;
6240b57cec5SDimitry Andricdefm : BinRMWTruncExtPattern<
6250b57cec5SDimitry Andric  atomic_load_and_8, atomic_load_and_16, atomic_load_and_32, atomic_load_and_64,
6260b57cec5SDimitry Andric  ATOMIC_RMW8_U_AND_I32, ATOMIC_RMW16_U_AND_I32,
6270b57cec5SDimitry Andric  ATOMIC_RMW8_U_AND_I64, ATOMIC_RMW16_U_AND_I64, ATOMIC_RMW32_U_AND_I64>;
6280b57cec5SDimitry Andricdefm : BinRMWTruncExtPattern<
6290b57cec5SDimitry Andric  atomic_load_or_8, atomic_load_or_16, atomic_load_or_32, atomic_load_or_64,
6300b57cec5SDimitry Andric  ATOMIC_RMW8_U_OR_I32, ATOMIC_RMW16_U_OR_I32,
6310b57cec5SDimitry Andric  ATOMIC_RMW8_U_OR_I64, ATOMIC_RMW16_U_OR_I64, ATOMIC_RMW32_U_OR_I64>;
6320b57cec5SDimitry Andricdefm : BinRMWTruncExtPattern<
6330b57cec5SDimitry Andric  atomic_load_xor_8, atomic_load_xor_16, atomic_load_xor_32, atomic_load_xor_64,
6340b57cec5SDimitry Andric  ATOMIC_RMW8_U_XOR_I32, ATOMIC_RMW16_U_XOR_I32,
6350b57cec5SDimitry Andric  ATOMIC_RMW8_U_XOR_I64, ATOMIC_RMW16_U_XOR_I64, ATOMIC_RMW32_U_XOR_I64>;
6360b57cec5SDimitry Andricdefm : BinRMWTruncExtPattern<
6370b57cec5SDimitry Andric  atomic_swap_8, atomic_swap_16, atomic_swap_32, atomic_swap_64,
6380b57cec5SDimitry Andric  ATOMIC_RMW8_U_XCHG_I32, ATOMIC_RMW16_U_XCHG_I32,
6390b57cec5SDimitry Andric  ATOMIC_RMW8_U_XCHG_I64, ATOMIC_RMW16_U_XCHG_I64, ATOMIC_RMW32_U_XCHG_I64>;
6400b57cec5SDimitry Andric} // Predicates = [HasAtomics]
6410b57cec5SDimitry Andric
6420b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
6430b57cec5SDimitry Andric// Atomic ternary read-modify-writes
6440b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
6450b57cec5SDimitry Andric
6460b57cec5SDimitry Andric// TODO LLVM IR's cmpxchg instruction returns a pair of {loaded value, success
6470b57cec5SDimitry Andric// flag}. When we use the success flag or both values, we can't make use of i64
6480b57cec5SDimitry Andric// truncate/extend versions of instructions for now, which is suboptimal.
6490b57cec5SDimitry Andric// Consider adding a pass after instruction selection that optimizes this case
6500b57cec5SDimitry Andric// if it is frequent.
6510b57cec5SDimitry Andric
6520b57cec5SDimitry Andricmulticlass WebAssemblyTerRMW<WebAssemblyRegClass rc, string name,
6530b57cec5SDimitry Andric                             int atomic_op> {
6540b57cec5SDimitry Andric  defm "" :
6550b57cec5SDimitry Andric    ATOMIC_I<(outs rc:$dst),
6560b57cec5SDimitry Andric             (ins P2Align:$p2align, offset32_op:$off, I32:$addr, rc:$exp,
6570b57cec5SDimitry Andric                  rc:$new_),
6580b57cec5SDimitry Andric             (outs), (ins P2Align:$p2align, offset32_op:$off), [],
6590b57cec5SDimitry Andric             !strconcat(name, "\t$dst, ${off}(${addr})${p2align}, $exp, $new_"),
6600b57cec5SDimitry Andric             !strconcat(name, "\t${off}${p2align}"), atomic_op>;
6610b57cec5SDimitry Andric}
6620b57cec5SDimitry Andric
6630b57cec5SDimitry Andricdefm ATOMIC_RMW_CMPXCHG_I32 :
6640b57cec5SDimitry Andric  WebAssemblyTerRMW<I32, "i32.atomic.rmw.cmpxchg", 0x48>;
6650b57cec5SDimitry Andricdefm ATOMIC_RMW_CMPXCHG_I64 :
6660b57cec5SDimitry Andric  WebAssemblyTerRMW<I64, "i64.atomic.rmw.cmpxchg", 0x49>;
6670b57cec5SDimitry Andricdefm ATOMIC_RMW8_U_CMPXCHG_I32 :
6680b57cec5SDimitry Andric  WebAssemblyTerRMW<I32, "i32.atomic.rmw8.cmpxchg_u", 0x4a>;
6690b57cec5SDimitry Andricdefm ATOMIC_RMW16_U_CMPXCHG_I32 :
6700b57cec5SDimitry Andric  WebAssemblyTerRMW<I32, "i32.atomic.rmw16.cmpxchg_u", 0x4b>;
6710b57cec5SDimitry Andricdefm ATOMIC_RMW8_U_CMPXCHG_I64 :
6720b57cec5SDimitry Andric  WebAssemblyTerRMW<I64, "i64.atomic.rmw8.cmpxchg_u", 0x4c>;
6730b57cec5SDimitry Andricdefm ATOMIC_RMW16_U_CMPXCHG_I64 :
6740b57cec5SDimitry Andric  WebAssemblyTerRMW<I64, "i64.atomic.rmw16.cmpxchg_u", 0x4d>;
6750b57cec5SDimitry Andricdefm ATOMIC_RMW32_U_CMPXCHG_I64 :
6760b57cec5SDimitry Andric  WebAssemblyTerRMW<I64, "i64.atomic.rmw32.cmpxchg_u", 0x4e>;
6770b57cec5SDimitry Andric
6780b57cec5SDimitry Andric// Select ternary RMWs with no constant offset.
6790b57cec5SDimitry Andricclass TerRMWPatNoOffset<ValueType ty, PatFrag kind, NI inst> :
6800b57cec5SDimitry Andric  Pat<(ty (kind I32:$addr, ty:$exp, ty:$new)),
6810b57cec5SDimitry Andric      (inst 0, 0, I32:$addr, ty:$exp, ty:$new)>;
6820b57cec5SDimitry Andric
6830b57cec5SDimitry Andric// Select ternary RMWs with a constant offset.
6840b57cec5SDimitry Andric
6850b57cec5SDimitry Andric// Pattern with address + immediate offset
6860b57cec5SDimitry Andricclass TerRMWPatImmOff<ValueType ty, PatFrag kind, PatFrag operand, NI inst> :
6870b57cec5SDimitry Andric  Pat<(ty (kind (operand I32:$addr, imm:$off), ty:$exp, ty:$new)),
6880b57cec5SDimitry Andric      (inst 0, imm:$off, I32:$addr, ty:$exp, ty:$new)>;
6890b57cec5SDimitry Andric
6900b57cec5SDimitry Andric// Select ternary RMWs with just a constant offset.
6910b57cec5SDimitry Andricclass TerRMWPatOffsetOnly<ValueType ty, PatFrag kind, NI inst> :
6920b57cec5SDimitry Andric  Pat<(ty (kind imm:$off, ty:$exp, ty:$new)),
6930b57cec5SDimitry Andric      (inst 0, imm:$off, (CONST_I32 0), ty:$exp, ty:$new)>;
6940b57cec5SDimitry Andric
6950b57cec5SDimitry Andricclass TerRMWPatGlobalAddrOffOnly<ValueType ty, PatFrag kind, NI inst> :
6960b57cec5SDimitry Andric  Pat<(ty (kind (WebAssemblywrapper tglobaladdr:$off), ty:$exp, ty:$new)),
6970b57cec5SDimitry Andric      (inst 0, tglobaladdr:$off, (CONST_I32 0), ty:$exp, ty:$new)>;
6980b57cec5SDimitry Andric
6990b57cec5SDimitry Andric// Patterns for various addressing modes.
7000b57cec5SDimitry Andricmulticlass TerRMWPattern<PatFrag rmw_32, PatFrag rmw_64, NI inst_32,
7010b57cec5SDimitry Andric                         NI inst_64> {
7020b57cec5SDimitry Andric  def : TerRMWPatNoOffset<i32, rmw_32, inst_32>;
7030b57cec5SDimitry Andric  def : TerRMWPatNoOffset<i64, rmw_64, inst_64>;
7040b57cec5SDimitry Andric
7050b57cec5SDimitry Andric  def : TerRMWPatImmOff<i32, rmw_32, regPlusImm, inst_32>;
7060b57cec5SDimitry Andric  def : TerRMWPatImmOff<i64, rmw_64, regPlusImm, inst_64>;
7070b57cec5SDimitry Andric  def : TerRMWPatImmOff<i32, rmw_32, or_is_add, inst_32>;
7080b57cec5SDimitry Andric  def : TerRMWPatImmOff<i64, rmw_64, or_is_add, inst_64>;
7090b57cec5SDimitry Andric
7100b57cec5SDimitry Andric  def : TerRMWPatOffsetOnly<i32, rmw_32, inst_32>;
7110b57cec5SDimitry Andric  def : TerRMWPatOffsetOnly<i64, rmw_64, inst_64>;
7120b57cec5SDimitry Andric
7130b57cec5SDimitry Andric  def : TerRMWPatGlobalAddrOffOnly<i32, rmw_32, inst_32>;
7140b57cec5SDimitry Andric  def : TerRMWPatGlobalAddrOffOnly<i64, rmw_64, inst_64>;
7150b57cec5SDimitry Andric}
7160b57cec5SDimitry Andric
7170b57cec5SDimitry Andriclet Predicates = [HasAtomics] in
7180b57cec5SDimitry Andricdefm : TerRMWPattern<atomic_cmp_swap_32, atomic_cmp_swap_64,
7190b57cec5SDimitry Andric                     ATOMIC_RMW_CMPXCHG_I32, ATOMIC_RMW_CMPXCHG_I64>;
7200b57cec5SDimitry Andric
7210b57cec5SDimitry Andric// Truncating & zero-extending ternary RMW patterns.
7220b57cec5SDimitry Andric// DAG legalization & optimization before instruction selection may introduce
7230b57cec5SDimitry Andric// additional nodes such as anyext or assertzext depending on operand types.
7240b57cec5SDimitry Andricclass zext_ter_rmw_8_32<PatFrag kind> :
7250b57cec5SDimitry Andric  PatFrag<(ops node:$addr, node:$exp, node:$new),
7260b57cec5SDimitry Andric          (and (i32 (kind node:$addr, node:$exp, node:$new)), 255)>;
7270b57cec5SDimitry Andricclass zext_ter_rmw_16_32<PatFrag kind> :
7280b57cec5SDimitry Andric  PatFrag<(ops node:$addr, node:$exp, node:$new),
7290b57cec5SDimitry Andric          (and (i32 (kind node:$addr, node:$exp, node:$new)), 65535)>;
7300b57cec5SDimitry Andricclass zext_ter_rmw_8_64<PatFrag kind> :
7310b57cec5SDimitry Andric  PatFrag<(ops node:$addr, node:$exp, node:$new),
7320b57cec5SDimitry Andric          (zext (i32 (assertzext (i32 (kind node:$addr,
7330b57cec5SDimitry Andric                                            (i32 (trunc (i64 node:$exp))),
7340b57cec5SDimitry Andric                                            (i32 (trunc (i64 node:$new))))))))>;
7350b57cec5SDimitry Andricclass zext_ter_rmw_16_64<PatFrag kind> : zext_ter_rmw_8_64<kind>;
7360b57cec5SDimitry Andricclass zext_ter_rmw_32_64<PatFrag kind> :
7370b57cec5SDimitry Andric  PatFrag<(ops node:$addr, node:$exp, node:$new),
7380b57cec5SDimitry Andric          (zext (i32 (kind node:$addr,
7390b57cec5SDimitry Andric                           (i32 (trunc (i64 node:$exp))),
7400b57cec5SDimitry Andric                           (i32 (trunc (i64 node:$new))))))>;
7410b57cec5SDimitry Andric
7420b57cec5SDimitry Andric// Truncating & sign-extending ternary RMW patterns.
7430b57cec5SDimitry Andric// We match subword RMWs (for 32-bit) and anyext RMWs (for 64-bit) and select a
7440b57cec5SDimitry Andric// zext RMW; the next instruction will be sext_inreg which is selected by
7450b57cec5SDimitry Andric// itself.
7460b57cec5SDimitry Andricclass sext_ter_rmw_8_32<PatFrag kind> :
7470b57cec5SDimitry Andric  PatFrag<(ops node:$addr, node:$exp, node:$new),
7480b57cec5SDimitry Andric          (kind node:$addr, node:$exp, node:$new)>;
7490b57cec5SDimitry Andricclass sext_ter_rmw_16_32<PatFrag kind> : sext_ter_rmw_8_32<kind>;
7500b57cec5SDimitry Andricclass sext_ter_rmw_8_64<PatFrag kind> :
7510b57cec5SDimitry Andric  PatFrag<(ops node:$addr, node:$exp, node:$new),
7520b57cec5SDimitry Andric          (anyext (i32 (assertzext (i32
7530b57cec5SDimitry Andric            (kind node:$addr,
7540b57cec5SDimitry Andric                  (i32 (trunc (i64 node:$exp))),
7550b57cec5SDimitry Andric                  (i32 (trunc (i64 node:$new))))))))>;
7560b57cec5SDimitry Andricclass sext_ter_rmw_16_64<PatFrag kind> : sext_ter_rmw_8_64<kind>;
7570b57cec5SDimitry Andric// 32->64 sext RMW gets selected as i32.atomic.rmw.***, i64.extend_i32_s
7580b57cec5SDimitry Andric
7590b57cec5SDimitry Andric// Patterns for various addressing modes for truncating-extending ternary RMWs.
7600b57cec5SDimitry Andricmulticlass TerRMWTruncExtPattern<
7610b57cec5SDimitry Andric  PatFrag rmw_8, PatFrag rmw_16, PatFrag rmw_32, PatFrag rmw_64,
7620b57cec5SDimitry Andric  NI inst8_32, NI inst16_32, NI inst8_64, NI inst16_64, NI inst32_64> {
7630b57cec5SDimitry Andric  // Truncating-extending ternary RMWs with no constant offset
7640b57cec5SDimitry Andric  def : TerRMWPatNoOffset<i32, zext_ter_rmw_8_32<rmw_8>, inst8_32>;
7650b57cec5SDimitry Andric  def : TerRMWPatNoOffset<i32, zext_ter_rmw_16_32<rmw_16>, inst16_32>;
7660b57cec5SDimitry Andric  def : TerRMWPatNoOffset<i64, zext_ter_rmw_8_64<rmw_8>, inst8_64>;
7670b57cec5SDimitry Andric  def : TerRMWPatNoOffset<i64, zext_ter_rmw_16_64<rmw_16>, inst16_64>;
7680b57cec5SDimitry Andric  def : TerRMWPatNoOffset<i64, zext_ter_rmw_32_64<rmw_32>, inst32_64>;
7690b57cec5SDimitry Andric
7700b57cec5SDimitry Andric  def : TerRMWPatNoOffset<i32, sext_ter_rmw_8_32<rmw_8>, inst8_32>;
7710b57cec5SDimitry Andric  def : TerRMWPatNoOffset<i32, sext_ter_rmw_16_32<rmw_16>, inst16_32>;
7720b57cec5SDimitry Andric  def : TerRMWPatNoOffset<i64, sext_ter_rmw_8_64<rmw_8>, inst8_64>;
7730b57cec5SDimitry Andric  def : TerRMWPatNoOffset<i64, sext_ter_rmw_16_64<rmw_16>, inst16_64>;
7740b57cec5SDimitry Andric
7750b57cec5SDimitry Andric  // Truncating-extending ternary RMWs with a constant offset
7760b57cec5SDimitry Andric  def : TerRMWPatImmOff<i32, zext_ter_rmw_8_32<rmw_8>, regPlusImm, inst8_32>;
7770b57cec5SDimitry Andric  def : TerRMWPatImmOff<i32, zext_ter_rmw_16_32<rmw_16>, regPlusImm, inst16_32>;
7780b57cec5SDimitry Andric  def : TerRMWPatImmOff<i64, zext_ter_rmw_8_64<rmw_8>, regPlusImm, inst8_64>;
7790b57cec5SDimitry Andric  def : TerRMWPatImmOff<i64, zext_ter_rmw_16_64<rmw_16>, regPlusImm, inst16_64>;
7800b57cec5SDimitry Andric  def : TerRMWPatImmOff<i64, zext_ter_rmw_32_64<rmw_32>, regPlusImm, inst32_64>;
7810b57cec5SDimitry Andric  def : TerRMWPatImmOff<i32, zext_ter_rmw_8_32<rmw_8>, or_is_add, inst8_32>;
7820b57cec5SDimitry Andric  def : TerRMWPatImmOff<i32, zext_ter_rmw_16_32<rmw_16>, or_is_add, inst16_32>;
7830b57cec5SDimitry Andric  def : TerRMWPatImmOff<i64, zext_ter_rmw_8_64<rmw_8>, or_is_add, inst8_64>;
7840b57cec5SDimitry Andric  def : TerRMWPatImmOff<i64, zext_ter_rmw_16_64<rmw_16>, or_is_add, inst16_64>;
7850b57cec5SDimitry Andric  def : TerRMWPatImmOff<i64, zext_ter_rmw_32_64<rmw_32>, or_is_add, inst32_64>;
7860b57cec5SDimitry Andric
7870b57cec5SDimitry Andric  def : TerRMWPatImmOff<i32, sext_ter_rmw_8_32<rmw_8>, regPlusImm, inst8_32>;
7880b57cec5SDimitry Andric  def : TerRMWPatImmOff<i32, sext_ter_rmw_16_32<rmw_16>, regPlusImm, inst16_32>;
7890b57cec5SDimitry Andric  def : TerRMWPatImmOff<i64, sext_ter_rmw_8_64<rmw_8>, regPlusImm, inst8_64>;
7900b57cec5SDimitry Andric  def : TerRMWPatImmOff<i64, sext_ter_rmw_16_64<rmw_16>, regPlusImm, inst16_64>;
7910b57cec5SDimitry Andric  def : TerRMWPatImmOff<i32, sext_ter_rmw_8_32<rmw_8>, or_is_add, inst8_32>;
7920b57cec5SDimitry Andric  def : TerRMWPatImmOff<i32, sext_ter_rmw_16_32<rmw_16>, or_is_add, inst16_32>;
7930b57cec5SDimitry Andric  def : TerRMWPatImmOff<i64, sext_ter_rmw_8_64<rmw_8>, or_is_add, inst8_64>;
7940b57cec5SDimitry Andric  def : TerRMWPatImmOff<i64, sext_ter_rmw_16_64<rmw_16>, or_is_add, inst16_64>;
7950b57cec5SDimitry Andric
7960b57cec5SDimitry Andric  // Truncating-extending ternary RMWs with just a constant offset
7970b57cec5SDimitry Andric  def : TerRMWPatOffsetOnly<i32, zext_ter_rmw_8_32<rmw_8>, inst8_32>;
7980b57cec5SDimitry Andric  def : TerRMWPatOffsetOnly<i32, zext_ter_rmw_16_32<rmw_16>, inst16_32>;
7990b57cec5SDimitry Andric  def : TerRMWPatOffsetOnly<i64, zext_ter_rmw_8_64<rmw_8>, inst8_64>;
8000b57cec5SDimitry Andric  def : TerRMWPatOffsetOnly<i64, zext_ter_rmw_16_64<rmw_16>, inst16_64>;
8010b57cec5SDimitry Andric  def : TerRMWPatOffsetOnly<i64, zext_ter_rmw_32_64<rmw_32>, inst32_64>;
8020b57cec5SDimitry Andric
8030b57cec5SDimitry Andric  def : TerRMWPatOffsetOnly<i32, sext_ter_rmw_8_32<rmw_8>, inst8_32>;
8040b57cec5SDimitry Andric  def : TerRMWPatOffsetOnly<i32, sext_ter_rmw_16_32<rmw_16>, inst16_32>;
8050b57cec5SDimitry Andric  def : TerRMWPatOffsetOnly<i64, sext_ter_rmw_8_64<rmw_8>, inst8_64>;
8060b57cec5SDimitry Andric  def : TerRMWPatOffsetOnly<i64, sext_ter_rmw_16_64<rmw_16>, inst16_64>;
8070b57cec5SDimitry Andric
8080b57cec5SDimitry Andric  def : TerRMWPatGlobalAddrOffOnly<i32, zext_ter_rmw_8_32<rmw_8>, inst8_32>;
8090b57cec5SDimitry Andric  def : TerRMWPatGlobalAddrOffOnly<i32, zext_ter_rmw_16_32<rmw_16>, inst16_32>;
8100b57cec5SDimitry Andric  def : TerRMWPatGlobalAddrOffOnly<i64, zext_ter_rmw_8_64<rmw_8>, inst8_64>;
8110b57cec5SDimitry Andric  def : TerRMWPatGlobalAddrOffOnly<i64, zext_ter_rmw_16_64<rmw_16>, inst16_64>;
8120b57cec5SDimitry Andric  def : TerRMWPatGlobalAddrOffOnly<i64, zext_ter_rmw_32_64<rmw_32>, inst32_64>;
8130b57cec5SDimitry Andric
8140b57cec5SDimitry Andric  def : TerRMWPatGlobalAddrOffOnly<i32, sext_ter_rmw_8_32<rmw_8>, inst8_32>;
8150b57cec5SDimitry Andric  def : TerRMWPatGlobalAddrOffOnly<i32, sext_ter_rmw_16_32<rmw_16>, inst16_32>;
8160b57cec5SDimitry Andric  def : TerRMWPatGlobalAddrOffOnly<i64, sext_ter_rmw_8_64<rmw_8>, inst8_64>;
8170b57cec5SDimitry Andric  def : TerRMWPatGlobalAddrOffOnly<i64, sext_ter_rmw_16_64<rmw_16>, inst16_64>;
8180b57cec5SDimitry Andric}
8190b57cec5SDimitry Andric
8200b57cec5SDimitry Andriclet Predicates = [HasAtomics] in
8210b57cec5SDimitry Andricdefm : TerRMWTruncExtPattern<
8220b57cec5SDimitry Andric  atomic_cmp_swap_8, atomic_cmp_swap_16, atomic_cmp_swap_32, atomic_cmp_swap_64,
8230b57cec5SDimitry Andric  ATOMIC_RMW8_U_CMPXCHG_I32, ATOMIC_RMW16_U_CMPXCHG_I32,
8240b57cec5SDimitry Andric  ATOMIC_RMW8_U_CMPXCHG_I64, ATOMIC_RMW16_U_CMPXCHG_I64,
8250b57cec5SDimitry Andric  ATOMIC_RMW32_U_CMPXCHG_I64>;
826