// WebAssemblyInstrAtomics.td-WebAssembly Atomic codegen support-*- tablegen -*- // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// /// /// \file /// WebAssembly Atomic operand code-gen constructs. /// //===----------------------------------------------------------------------===// let UseNamedOperandTable = 1 in multiclass ATOMIC_I pattern_r, string asmstr_r, string asmstr_s, bits<32> atomic_op, string is64 = "false"> { defm "" : I, Requires<[HasAtomics]>; } multiclass ATOMIC_NRI pattern, string asmstr = "", bits<32> atomic_op = -1> { defm "" : NRI, Requires<[HasAtomics]>; } //===----------------------------------------------------------------------===// // Atomic wait / notify //===----------------------------------------------------------------------===// let hasSideEffects = 1 in { defm MEMORY_ATOMIC_NOTIFY_A32 : ATOMIC_I<(outs I32:$dst), (ins P2Align:$p2align, offset32_op:$off, I32:$addr, I32:$count), (outs), (ins P2Align:$p2align, offset32_op:$off), [], "memory.atomic.notify \t$dst, ${off}(${addr})${p2align}, $count", "memory.atomic.notify \t${off}${p2align}", 0x00, "false">; defm MEMORY_ATOMIC_NOTIFY_A64 : ATOMIC_I<(outs I32:$dst), (ins P2Align:$p2align, offset64_op:$off, I64:$addr, I32:$count), (outs), (ins P2Align:$p2align, offset64_op:$off), [], "memory.atomic.notify \t$dst, ${off}(${addr})${p2align}, $count", "memory.atomic.notify \t${off}${p2align}", 0x00, "true">; let mayLoad = 1 in { defm MEMORY_ATOMIC_WAIT32_A32 : ATOMIC_I<(outs I32:$dst), (ins P2Align:$p2align, offset32_op:$off, I32:$addr, I32:$exp, I64:$timeout), (outs), (ins P2Align:$p2align, offset32_op:$off), [], "memory.atomic.wait32 \t$dst, ${off}(${addr})${p2align}, $exp, $timeout", "memory.atomic.wait32 \t${off}${p2align}", 0x01, "false">; defm MEMORY_ATOMIC_WAIT32_A64 : ATOMIC_I<(outs I32:$dst), (ins P2Align:$p2align, offset64_op:$off, I64:$addr, I32:$exp, I64:$timeout), (outs), (ins P2Align:$p2align, offset64_op:$off), [], "memory.atomic.wait32 \t$dst, ${off}(${addr})${p2align}, $exp, $timeout", "memory.atomic.wait32 \t${off}${p2align}", 0x01, "true">; defm MEMORY_ATOMIC_WAIT64_A32 : ATOMIC_I<(outs I32:$dst), (ins P2Align:$p2align, offset32_op:$off, I32:$addr, I64:$exp, I64:$timeout), (outs), (ins P2Align:$p2align, offset32_op:$off), [], "memory.atomic.wait64 \t$dst, ${off}(${addr})${p2align}, $exp, $timeout", "memory.atomic.wait64 \t${off}${p2align}", 0x02, "false">; defm MEMORY_ATOMIC_WAIT64_A64 : ATOMIC_I<(outs I32:$dst), (ins P2Align:$p2align, offset64_op:$off, I64:$addr, I64:$exp, I64:$timeout), (outs), (ins P2Align:$p2align, offset64_op:$off), [], "memory.atomic.wait64 \t$dst, ${off}(${addr})${p2align}, $exp, $timeout", "memory.atomic.wait64 \t${off}${p2align}", 0x02, "true">; } // mayLoad = 1 } // hasSideEffects = 1 // Select notifys with no constant offset. def NotifyPatNoOffset_A32 : Pat<(i32 (int_wasm_memory_atomic_notify I32:$addr, I32:$count)), (MEMORY_ATOMIC_NOTIFY_A32 0, 0, I32:$addr, I32:$count)>, Requires<[HasAddr32, HasAtomics]>; def NotifyPatNoOffset_A64 : Pat<(i32 (int_wasm_memory_atomic_notify I64:$addr, I32:$count)), (MEMORY_ATOMIC_NOTIFY_A64 0, 0, I64:$addr, I32:$count)>, Requires<[HasAddr64, HasAtomics]>; // Select notifys with a constant offset. // Pattern with address + immediate offset multiclass NotifyPatImmOff { def : Pat<(i32 (int_wasm_memory_atomic_notify (operand I32:$addr, imm:$off), I32:$count)), (!cast(inst#_A32) 0, imm:$off, I32:$addr, I32:$count)>, Requires<[HasAddr32, HasAtomics]>; def : Pat<(i32 (int_wasm_memory_atomic_notify (operand I64:$addr, imm:$off), I32:$count)), (!cast(inst#_A64) 0, imm:$off, I64:$addr, I32:$count)>, Requires<[HasAddr64, HasAtomics]>; } defm : NotifyPatImmOff; defm : NotifyPatImmOff; // Select notifys with just a constant offset. def NotifyPatOffsetOnly_A32 : Pat<(i32 (int_wasm_memory_atomic_notify imm:$off, I32:$count)), (MEMORY_ATOMIC_NOTIFY_A32 0, imm:$off, (CONST_I32 0), I32:$count)>, Requires<[HasAddr32, HasAtomics]>; def NotifyPatOffsetOnly_A64 : Pat<(i32 (int_wasm_memory_atomic_notify imm:$off, I32:$count)), (MEMORY_ATOMIC_NOTIFY_A64 0, imm:$off, (CONST_I64 0), I32:$count)>, Requires<[HasAddr64, HasAtomics]>; def NotifyPatGlobalAddrOffOnly_A32 : Pat<(i32 (int_wasm_memory_atomic_notify (WebAssemblywrapper tglobaladdr:$off), I32:$count)), (MEMORY_ATOMIC_NOTIFY_A32 0, tglobaladdr:$off, (CONST_I32 0), I32:$count) >, Requires<[HasAddr32, HasAtomics, IsNotPIC]>; def NotifyPatGlobalAddrOffOnly_A64 : Pat<(i32 (int_wasm_memory_atomic_notify (WebAssemblywrapper tglobaladdr:$off), I32:$count)), (MEMORY_ATOMIC_NOTIFY_A64 0, tglobaladdr:$off, (CONST_I64 0), I32:$count) >, Requires<[HasAddr64, HasAtomics, IsNotPIC]>; // Select waits with no constant offset. multiclass WaitPatNoOffset { def : Pat<(i32 (kind I32:$addr, ty:$exp, I64:$timeout)), (!cast(inst#_A32) 0, 0, I32:$addr, ty:$exp, I64:$timeout)>, Requires<[HasAddr32, HasAtomics]>; def : Pat<(i32 (kind I64:$addr, ty:$exp, I64:$timeout)), (!cast(inst#_A64) 0, 0, I64:$addr, ty:$exp, I64:$timeout)>, Requires<[HasAddr64, HasAtomics]>; } defm : WaitPatNoOffset; defm : WaitPatNoOffset; defm : WaitPatNoOffset; defm : WaitPatNoOffset; // Select waits with a constant offset. // Pattern with address + immediate offset multiclass WaitPatImmOff { def : Pat<(i32 (kind (operand I32:$addr, imm:$off), ty:$exp, I64:$timeout)), (!cast(inst#_A32) 0, imm:$off, I32:$addr, ty:$exp, I64:$timeout)>, Requires<[HasAddr32, HasAtomics]>; def : Pat<(i32 (kind (operand I64:$addr, imm:$off), ty:$exp, I64:$timeout)), (!cast(inst#_A64) 0, imm:$off, I64:$addr, ty:$exp, I64:$timeout)>, Requires<[HasAddr64, HasAtomics]>; } defm : WaitPatImmOff; defm : WaitPatImmOff; defm : WaitPatImmOff; defm : WaitPatImmOff; // Select waits with just a constant offset. multiclass WaitPatOffsetOnly { def : Pat<(i32 (kind imm:$off, ty:$exp, I64:$timeout)), (!cast(inst#_A32) 0, imm:$off, (CONST_I32 0), ty:$exp, I64:$timeout)>, Requires<[HasAddr32, HasAtomics]>; def : Pat<(i32 (kind imm:$off, ty:$exp, I64:$timeout)), (!cast(inst#_A64) 0, imm:$off, (CONST_I64 0), ty:$exp, I64:$timeout)>, Requires<[HasAddr64, HasAtomics]>; } defm : WaitPatOffsetOnly; defm : WaitPatOffsetOnly; multiclass WaitPatGlobalAddrOffOnly { def : Pat<(i32 (kind (WebAssemblywrapper tglobaladdr:$off), ty:$exp, I64:$timeout)), (!cast(inst#_A32) 0, tglobaladdr:$off, (CONST_I32 0), ty:$exp, I64:$timeout)>, Requires<[HasAddr32, HasAtomics, IsNotPIC]>; def : Pat<(i32 (kind (WebAssemblywrapper tglobaladdr:$off), ty:$exp, I64:$timeout)), (!cast(inst#_A64) 0, tglobaladdr:$off, (CONST_I64 0), ty:$exp, I64:$timeout)>, Requires<[HasAddr64, HasAtomics, IsNotPIC]>; } defm : WaitPatGlobalAddrOffOnly; defm : WaitPatGlobalAddrOffOnly; //===----------------------------------------------------------------------===// // Atomic fences //===----------------------------------------------------------------------===// // A compiler fence instruction that prevents reordering of instructions. let Defs = [ARGUMENTS] in { let isPseudo = 1, hasSideEffects = 1 in defm COMPILER_FENCE : ATOMIC_NRI<(outs), (ins), [], "compiler_fence">; let hasSideEffects = 1 in defm ATOMIC_FENCE : ATOMIC_NRI<(outs), (ins i8imm:$flags), [], "atomic.fence", 0x03>; } // Defs = [ARGUMENTS] //===----------------------------------------------------------------------===// // Atomic loads //===----------------------------------------------------------------------===// multiclass AtomicLoad { defm "" : WebAssemblyLoad; } defm ATOMIC_LOAD_I32 : AtomicLoad; defm ATOMIC_LOAD_I64 : AtomicLoad; // Select loads with no constant offset. defm : LoadPatNoOffset; defm : LoadPatNoOffset; // Select loads with a constant offset. // Pattern with address + immediate offset defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; // Select loads with just a constant offset. defm : LoadPatOffsetOnly; defm : LoadPatOffsetOnly; defm : LoadPatGlobalAddrOffOnly; defm : LoadPatGlobalAddrOffOnly; // Extending loads. Note that there are only zero-extending atomic loads, no // sign-extending loads. defm ATOMIC_LOAD8_U_I32 : AtomicLoad; defm ATOMIC_LOAD16_U_I32 : AtomicLoad; defm ATOMIC_LOAD8_U_I64 : AtomicLoad; defm ATOMIC_LOAD16_U_I64 : AtomicLoad; defm ATOMIC_LOAD32_U_I64 : AtomicLoad; // Fragments for extending loads. These are different from regular loads because // the SDNodes are derived from AtomicSDNode rather than LoadSDNode and // therefore don't have the extension type field. So instead of matching that, // we match the patterns that the type legalizer expands them to. // Unlike regular loads, extension to i64 is handled differently than i32. // i64 (zext (i8 (atomic_load_8))) gets legalized to // i64 (and (i64 (anyext (i32 (atomic_load_8)))), 255) // Extension to i32 is elided by SelectionDAG as our atomic loads are // zero-extending. def zext_aload_8_64 : PatFrag<(ops node:$addr), (i64 (zext (i32 (atomic_load_8 node:$addr))))>; def zext_aload_16_64 : PatFrag<(ops node:$addr), (i64 (zext (i32 (atomic_load_16 node:$addr))))>; def zext_aload_32_64 : PatFrag<(ops node:$addr), (i64 (zext (i32 (atomic_load_32 node:$addr))))>; // We don't have single sext atomic load instructions. So for sext loads, we // match bare subword loads (for 32-bit results) and anyext loads (for 64-bit // results) and select a zext load; the next instruction will be sext_inreg // which is selected by itself. def sext_aload_8_64 : PatFrag<(ops node:$addr), (anyext (i32 (atomic_load_8 node:$addr)))>; def sext_aload_16_64 : PatFrag<(ops node:$addr), (anyext (i32 (atomic_load_16 node:$addr)))>; // Select zero-extending loads with no constant offset. defm : LoadPatNoOffset; defm : LoadPatNoOffset; defm : LoadPatNoOffset; // Select sign-extending loads with no constant offset defm : LoadPatNoOffset; defm : LoadPatNoOffset; defm : LoadPatNoOffset; defm : LoadPatNoOffset; // 32->64 sext load gets selected as i32.atomic.load, i64.extend_i32_s // Zero-extending loads with constant offset defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; // Sign-extending loads with constant offset defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; // No 32->64 patterns, just use i32.atomic.load and i64.extend_s/i64 // Extending loads with just a constant offset defm : LoadPatOffsetOnly; defm : LoadPatOffsetOnly; defm : LoadPatOffsetOnly; defm : LoadPatOffsetOnly; defm : LoadPatOffsetOnly; defm : LoadPatOffsetOnly; defm : LoadPatOffsetOnly; defm : LoadPatGlobalAddrOffOnly; defm : LoadPatGlobalAddrOffOnly; defm : LoadPatGlobalAddrOffOnly; defm : LoadPatGlobalAddrOffOnly; defm : LoadPatGlobalAddrOffOnly; defm : LoadPatGlobalAddrOffOnly; defm : LoadPatGlobalAddrOffOnly; //===----------------------------------------------------------------------===// // Atomic stores //===----------------------------------------------------------------------===// multiclass AtomicStore { defm "" : WebAssemblyStore; } defm ATOMIC_STORE_I32 : AtomicStore; defm ATOMIC_STORE_I64 : AtomicStore; // We need an 'atomic' version of store patterns because store and atomic_store // nodes have different operand orders: // store: (store $val, $ptr) // atomic_store: (store $ptr, $val) // Select stores with no constant offset. multiclass AStorePatNoOffset { def : Pat<(kind I32:$addr, ty:$val), (!cast(inst#_A32) 0, 0, I32:$addr, ty:$val)>, Requires<[HasAddr32, HasAtomics]>; def : Pat<(kind I64:$addr, ty:$val), (!cast(inst#_A64) 0, 0, I64:$addr, ty:$val)>, Requires<[HasAddr64, HasAtomics]>; } defm : AStorePatNoOffset; defm : AStorePatNoOffset; // Select stores with a constant offset. // Pattern with address + immediate offset multiclass AStorePatImmOff { def : Pat<(kind (operand I32:$addr, imm:$off), ty:$val), (!cast(inst#_A32) 0, imm:$off, I32:$addr, ty:$val)>, Requires<[HasAddr32, HasAtomics]>; def : Pat<(kind (operand I64:$addr, imm:$off), ty:$val), (!cast(inst#_A64) 0, imm:$off, I64:$addr, ty:$val)>, Requires<[HasAddr64, HasAtomics]>; } defm : AStorePatImmOff; defm : AStorePatImmOff; // Select stores with just a constant offset. multiclass AStorePatOffsetOnly { def : Pat<(kind imm:$off, ty:$val), (!cast(inst#_A32) 0, imm:$off, (CONST_I32 0), ty:$val)>, Requires<[HasAddr32, HasAtomics]>; def : Pat<(kind imm:$off, ty:$val), (!cast(inst#_A64) 0, imm:$off, (CONST_I64 0), ty:$val)>, Requires<[HasAddr64, HasAtomics]>; } defm : AStorePatOffsetOnly; defm : AStorePatOffsetOnly; multiclass AStorePatGlobalAddrOffOnly { def : Pat<(kind (WebAssemblywrapper tglobaladdr:$off), ty:$val), (!cast(inst#_A32) 0, tglobaladdr:$off, (CONST_I32 0), ty:$val)>, Requires<[HasAddr32, HasAtomics, IsNotPIC]>; def : Pat<(kind (WebAssemblywrapper tglobaladdr:$off), ty:$val), (!cast(inst#_A64) 0, tglobaladdr:$off, (CONST_I64 0), ty:$val)>, Requires<[HasAddr64, HasAtomics, IsNotPIC]>; } defm : AStorePatGlobalAddrOffOnly; defm : AStorePatGlobalAddrOffOnly; // Truncating stores. defm ATOMIC_STORE8_I32 : AtomicStore; defm ATOMIC_STORE16_I32 : AtomicStore; defm ATOMIC_STORE8_I64 : AtomicStore; defm ATOMIC_STORE16_I64 : AtomicStore; defm ATOMIC_STORE32_I64 : AtomicStore; // Fragments for truncating stores. // We don't have single truncating atomic store instructions. For 32-bit // instructions, we just need to match bare atomic stores. On the other hand, // truncating stores from i64 values are once truncated to i32 first. class trunc_astore_64 : PatFrag<(ops node:$addr, node:$val), (kind node:$addr, (i32 (trunc (i64 node:$val))))>; def trunc_astore_8_64 : trunc_astore_64; def trunc_astore_16_64 : trunc_astore_64; def trunc_astore_32_64 : trunc_astore_64; // Truncating stores with no constant offset defm : AStorePatNoOffset; defm : AStorePatNoOffset; defm : AStorePatNoOffset; defm : AStorePatNoOffset; defm : AStorePatNoOffset; // Truncating stores with a constant offset defm : AStorePatImmOff; defm : AStorePatImmOff; defm : AStorePatImmOff; defm : AStorePatImmOff; defm : AStorePatImmOff; defm : AStorePatImmOff; defm : AStorePatImmOff; defm : AStorePatImmOff; defm : AStorePatImmOff; defm : AStorePatImmOff; // Truncating stores with just a constant offset defm : AStorePatOffsetOnly; defm : AStorePatOffsetOnly; defm : AStorePatOffsetOnly; defm : AStorePatOffsetOnly; defm : AStorePatOffsetOnly; defm : AStorePatGlobalAddrOffOnly; defm : AStorePatGlobalAddrOffOnly; defm : AStorePatGlobalAddrOffOnly; defm : AStorePatGlobalAddrOffOnly; defm : AStorePatGlobalAddrOffOnly; //===----------------------------------------------------------------------===// // Atomic binary read-modify-writes //===----------------------------------------------------------------------===// multiclass WebAssemblyBinRMW { defm "_A32" : ATOMIC_I<(outs rc:$dst), (ins P2Align:$p2align, offset32_op:$off, I32:$addr, rc:$val), (outs), (ins P2Align:$p2align, offset32_op:$off), [], !strconcat(name, "\t$dst, ${off}(${addr})${p2align}, $val"), !strconcat(name, "\t${off}${p2align}"), atomic_op, "false">; defm "_A64" : ATOMIC_I<(outs rc:$dst), (ins P2Align:$p2align, offset64_op:$off, I64:$addr, rc:$val), (outs), (ins P2Align:$p2align, offset64_op:$off), [], !strconcat(name, "\t$dst, ${off}(${addr})${p2align}, $val"), !strconcat(name, "\t${off}${p2align}"), atomic_op, "true">; } defm ATOMIC_RMW_ADD_I32 : WebAssemblyBinRMW; defm ATOMIC_RMW_ADD_I64 : WebAssemblyBinRMW; defm ATOMIC_RMW8_U_ADD_I32 : WebAssemblyBinRMW; defm ATOMIC_RMW16_U_ADD_I32 : WebAssemblyBinRMW; defm ATOMIC_RMW8_U_ADD_I64 : WebAssemblyBinRMW; defm ATOMIC_RMW16_U_ADD_I64 : WebAssemblyBinRMW; defm ATOMIC_RMW32_U_ADD_I64 : WebAssemblyBinRMW; defm ATOMIC_RMW_SUB_I32 : WebAssemblyBinRMW; defm ATOMIC_RMW_SUB_I64 : WebAssemblyBinRMW; defm ATOMIC_RMW8_U_SUB_I32 : WebAssemblyBinRMW; defm ATOMIC_RMW16_U_SUB_I32 : WebAssemblyBinRMW; defm ATOMIC_RMW8_U_SUB_I64 : WebAssemblyBinRMW; defm ATOMIC_RMW16_U_SUB_I64 : WebAssemblyBinRMW; defm ATOMIC_RMW32_U_SUB_I64 : WebAssemblyBinRMW; defm ATOMIC_RMW_AND_I32 : WebAssemblyBinRMW; defm ATOMIC_RMW_AND_I64 : WebAssemblyBinRMW; defm ATOMIC_RMW8_U_AND_I32 : WebAssemblyBinRMW; defm ATOMIC_RMW16_U_AND_I32 : WebAssemblyBinRMW; defm ATOMIC_RMW8_U_AND_I64 : WebAssemblyBinRMW; defm ATOMIC_RMW16_U_AND_I64 : WebAssemblyBinRMW; defm ATOMIC_RMW32_U_AND_I64 : WebAssemblyBinRMW; defm ATOMIC_RMW_OR_I32 : WebAssemblyBinRMW; defm ATOMIC_RMW_OR_I64 : WebAssemblyBinRMW; defm ATOMIC_RMW8_U_OR_I32 : WebAssemblyBinRMW; defm ATOMIC_RMW16_U_OR_I32 : WebAssemblyBinRMW; defm ATOMIC_RMW8_U_OR_I64 : WebAssemblyBinRMW; defm ATOMIC_RMW16_U_OR_I64 : WebAssemblyBinRMW; defm ATOMIC_RMW32_U_OR_I64 : WebAssemblyBinRMW; defm ATOMIC_RMW_XOR_I32 : WebAssemblyBinRMW; defm ATOMIC_RMW_XOR_I64 : WebAssemblyBinRMW; defm ATOMIC_RMW8_U_XOR_I32 : WebAssemblyBinRMW; defm ATOMIC_RMW16_U_XOR_I32 : WebAssemblyBinRMW; defm ATOMIC_RMW8_U_XOR_I64 : WebAssemblyBinRMW; defm ATOMIC_RMW16_U_XOR_I64 : WebAssemblyBinRMW; defm ATOMIC_RMW32_U_XOR_I64 : WebAssemblyBinRMW; defm ATOMIC_RMW_XCHG_I32 : WebAssemblyBinRMW; defm ATOMIC_RMW_XCHG_I64 : WebAssemblyBinRMW; defm ATOMIC_RMW8_U_XCHG_I32 : WebAssemblyBinRMW; defm ATOMIC_RMW16_U_XCHG_I32 : WebAssemblyBinRMW; defm ATOMIC_RMW8_U_XCHG_I64 : WebAssemblyBinRMW; defm ATOMIC_RMW16_U_XCHG_I64 : WebAssemblyBinRMW; defm ATOMIC_RMW32_U_XCHG_I64 : WebAssemblyBinRMW; // Select binary RMWs with no constant offset. multiclass BinRMWPatNoOffset { def : Pat<(ty (kind I32:$addr, ty:$val)), (!cast(inst#_A32) 0, 0, I32:$addr, ty:$val)>, Requires<[HasAddr32, HasAtomics]>; def : Pat<(ty (kind I64:$addr, ty:$val)), (!cast(inst#_A64) 0, 0, I64:$addr, ty:$val)>, Requires<[HasAddr64, HasAtomics]>; } // Select binary RMWs with a constant offset. // Pattern with address + immediate offset multiclass BinRMWPatImmOff { def : Pat<(ty (kind (operand I32:$addr, imm:$off), ty:$val)), (!cast(inst#_A32) 0, imm:$off, I32:$addr, ty:$val)>, Requires<[HasAddr32, HasAtomics]>; def : Pat<(ty (kind (operand I64:$addr, imm:$off), ty:$val)), (!cast(inst#_A64) 0, imm:$off, I64:$addr, ty:$val)>, Requires<[HasAddr64, HasAtomics]>; } // Select binary RMWs with just a constant offset. multiclass BinRMWPatOffsetOnly { def : Pat<(ty (kind imm:$off, ty:$val)), (!cast(inst#_A32) 0, imm:$off, (CONST_I32 0), ty:$val)>, Requires<[HasAddr32, HasAtomics]>; def : Pat<(ty (kind imm:$off, ty:$val)), (!cast(inst#_A64) 0, imm:$off, (CONST_I64 0), ty:$val)>, Requires<[HasAddr64, HasAtomics]>; } multiclass BinRMWPatGlobalAddrOffOnly { def : Pat<(ty (kind (WebAssemblywrapper tglobaladdr:$off), ty:$val)), (!cast(inst#_A32) 0, tglobaladdr:$off, (CONST_I32 0), ty:$val)>, Requires<[HasAddr32, HasAtomics, IsNotPIC]>; def : Pat<(ty (kind (WebAssemblywrapper tglobaladdr:$off), ty:$val)), (!cast(inst#_A64) 0, tglobaladdr:$off, (CONST_I64 0), ty:$val)>, Requires<[HasAddr64, HasAtomics, IsNotPIC]>; } // Patterns for various addressing modes. multiclass BinRMWPattern { defm : BinRMWPatNoOffset; defm : BinRMWPatNoOffset; defm : BinRMWPatImmOff; defm : BinRMWPatImmOff; defm : BinRMWPatImmOff; defm : BinRMWPatImmOff; defm : BinRMWPatOffsetOnly; defm : BinRMWPatOffsetOnly; defm : BinRMWPatGlobalAddrOffOnly; defm : BinRMWPatGlobalAddrOffOnly; } defm : BinRMWPattern; defm : BinRMWPattern; defm : BinRMWPattern; defm : BinRMWPattern; defm : BinRMWPattern; defm : BinRMWPattern; // Truncating & zero-extending binary RMW patterns. // These are combined patterns of truncating store patterns and zero-extending // load patterns above. class zext_bin_rmw_8_32 : PatFrag<(ops node:$addr, node:$val), (i32 (kind node:$addr, node:$val))>; class zext_bin_rmw_16_32 : zext_bin_rmw_8_32; class zext_bin_rmw_8_64 : PatFrag<(ops node:$addr, node:$val), (zext (i32 (kind node:$addr, (i32 (trunc (i64 node:$val))))))>; class zext_bin_rmw_16_64 : zext_bin_rmw_8_64; class zext_bin_rmw_32_64 : zext_bin_rmw_8_64; // Truncating & sign-extending binary RMW patterns. // These are combined patterns of truncating store patterns and sign-extending // load patterns above. We match subword RMWs (for 32-bit) and anyext RMWs (for // 64-bit) and select a zext RMW; the next instruction will be sext_inreg which // is selected by itself. class sext_bin_rmw_8_32 : PatFrag<(ops node:$addr, node:$val), (kind node:$addr, node:$val)>; class sext_bin_rmw_16_32 : sext_bin_rmw_8_32; class sext_bin_rmw_8_64 : PatFrag<(ops node:$addr, node:$val), (anyext (i32 (kind node:$addr, (i32 (trunc (i64 node:$val))))))>; class sext_bin_rmw_16_64 : sext_bin_rmw_8_64; // 32->64 sext RMW gets selected as i32.atomic.rmw.***, i64.extend_i32_s // Patterns for various addressing modes for truncating-extending binary RMWs. multiclass BinRMWTruncExtPattern< PatFrag rmw_8, PatFrag rmw_16, PatFrag rmw_32, PatFrag rmw_64, string inst8_32, string inst16_32, string inst8_64, string inst16_64, string inst32_64> { // Truncating-extending binary RMWs with no constant offset defm : BinRMWPatNoOffset, inst8_32>; defm : BinRMWPatNoOffset, inst16_32>; defm : BinRMWPatNoOffset, inst8_64>; defm : BinRMWPatNoOffset, inst16_64>; defm : BinRMWPatNoOffset, inst32_64>; defm : BinRMWPatNoOffset, inst8_32>; defm : BinRMWPatNoOffset, inst16_32>; defm : BinRMWPatNoOffset, inst8_64>; defm : BinRMWPatNoOffset, inst16_64>; // Truncating-extending binary RMWs with a constant offset defm : BinRMWPatImmOff, regPlusImm, inst8_32>; defm : BinRMWPatImmOff, regPlusImm, inst16_32>; defm : BinRMWPatImmOff, regPlusImm, inst8_64>; defm : BinRMWPatImmOff, regPlusImm, inst16_64>; defm : BinRMWPatImmOff, regPlusImm, inst32_64>; defm : BinRMWPatImmOff, or_is_add, inst8_32>; defm : BinRMWPatImmOff, or_is_add, inst16_32>; defm : BinRMWPatImmOff, or_is_add, inst8_64>; defm : BinRMWPatImmOff, or_is_add, inst16_64>; defm : BinRMWPatImmOff, or_is_add, inst32_64>; defm : BinRMWPatImmOff, regPlusImm, inst8_32>; defm : BinRMWPatImmOff, regPlusImm, inst16_32>; defm : BinRMWPatImmOff, regPlusImm, inst8_64>; defm : BinRMWPatImmOff, regPlusImm, inst16_64>; defm : BinRMWPatImmOff, or_is_add, inst8_32>; defm : BinRMWPatImmOff, or_is_add, inst16_32>; defm : BinRMWPatImmOff, or_is_add, inst8_64>; defm : BinRMWPatImmOff, or_is_add, inst16_64>; // Truncating-extending binary RMWs with just a constant offset defm : BinRMWPatOffsetOnly, inst8_32>; defm : BinRMWPatOffsetOnly, inst16_32>; defm : BinRMWPatOffsetOnly, inst8_64>; defm : BinRMWPatOffsetOnly, inst16_64>; defm : BinRMWPatOffsetOnly, inst32_64>; defm : BinRMWPatOffsetOnly, inst8_32>; defm : BinRMWPatOffsetOnly, inst16_32>; defm : BinRMWPatOffsetOnly, inst8_64>; defm : BinRMWPatOffsetOnly, inst16_64>; defm : BinRMWPatGlobalAddrOffOnly, inst8_32>; defm : BinRMWPatGlobalAddrOffOnly, inst16_32>; defm : BinRMWPatGlobalAddrOffOnly, inst8_64>; defm : BinRMWPatGlobalAddrOffOnly, inst16_64>; defm : BinRMWPatGlobalAddrOffOnly, inst32_64>; defm : BinRMWPatGlobalAddrOffOnly, inst8_32>; defm : BinRMWPatGlobalAddrOffOnly, inst16_32>; defm : BinRMWPatGlobalAddrOffOnly, inst8_64>; defm : BinRMWPatGlobalAddrOffOnly, inst16_64>; } defm : BinRMWTruncExtPattern< atomic_load_add_8, atomic_load_add_16, atomic_load_add_32, atomic_load_add_64, "ATOMIC_RMW8_U_ADD_I32", "ATOMIC_RMW16_U_ADD_I32", "ATOMIC_RMW8_U_ADD_I64", "ATOMIC_RMW16_U_ADD_I64", "ATOMIC_RMW32_U_ADD_I64">; defm : BinRMWTruncExtPattern< atomic_load_sub_8, atomic_load_sub_16, atomic_load_sub_32, atomic_load_sub_64, "ATOMIC_RMW8_U_SUB_I32", "ATOMIC_RMW16_U_SUB_I32", "ATOMIC_RMW8_U_SUB_I64", "ATOMIC_RMW16_U_SUB_I64", "ATOMIC_RMW32_U_SUB_I64">; defm : BinRMWTruncExtPattern< atomic_load_and_8, atomic_load_and_16, atomic_load_and_32, atomic_load_and_64, "ATOMIC_RMW8_U_AND_I32", "ATOMIC_RMW16_U_AND_I32", "ATOMIC_RMW8_U_AND_I64", "ATOMIC_RMW16_U_AND_I64", "ATOMIC_RMW32_U_AND_I64">; defm : BinRMWTruncExtPattern< atomic_load_or_8, atomic_load_or_16, atomic_load_or_32, atomic_load_or_64, "ATOMIC_RMW8_U_OR_I32", "ATOMIC_RMW16_U_OR_I32", "ATOMIC_RMW8_U_OR_I64", "ATOMIC_RMW16_U_OR_I64", "ATOMIC_RMW32_U_OR_I64">; defm : BinRMWTruncExtPattern< atomic_load_xor_8, atomic_load_xor_16, atomic_load_xor_32, atomic_load_xor_64, "ATOMIC_RMW8_U_XOR_I32", "ATOMIC_RMW16_U_XOR_I32", "ATOMIC_RMW8_U_XOR_I64", "ATOMIC_RMW16_U_XOR_I64", "ATOMIC_RMW32_U_XOR_I64">; defm : BinRMWTruncExtPattern< atomic_swap_8, atomic_swap_16, atomic_swap_32, atomic_swap_64, "ATOMIC_RMW8_U_XCHG_I32", "ATOMIC_RMW16_U_XCHG_I32", "ATOMIC_RMW8_U_XCHG_I64", "ATOMIC_RMW16_U_XCHG_I64", "ATOMIC_RMW32_U_XCHG_I64">; //===----------------------------------------------------------------------===// // Atomic ternary read-modify-writes //===----------------------------------------------------------------------===// // TODO LLVM IR's cmpxchg instruction returns a pair of {loaded value, success // flag}. When we use the success flag or both values, we can't make use of i64 // truncate/extend versions of instructions for now, which is suboptimal. // Consider adding a pass after instruction selection that optimizes this case // if it is frequent. multiclass WebAssemblyTerRMW { defm "_A32" : ATOMIC_I<(outs rc:$dst), (ins P2Align:$p2align, offset32_op:$off, I32:$addr, rc:$exp, rc:$new_), (outs), (ins P2Align:$p2align, offset32_op:$off), [], !strconcat(name, "\t$dst, ${off}(${addr})${p2align}, $exp, $new_"), !strconcat(name, "\t${off}${p2align}"), atomic_op, "false">; defm "_A64" : ATOMIC_I<(outs rc:$dst), (ins P2Align:$p2align, offset64_op:$off, I64:$addr, rc:$exp, rc:$new_), (outs), (ins P2Align:$p2align, offset64_op:$off), [], !strconcat(name, "\t$dst, ${off}(${addr})${p2align}, $exp, $new_"), !strconcat(name, "\t${off}${p2align}"), atomic_op, "true">; } defm ATOMIC_RMW_CMPXCHG_I32 : WebAssemblyTerRMW; defm ATOMIC_RMW_CMPXCHG_I64 : WebAssemblyTerRMW; defm ATOMIC_RMW8_U_CMPXCHG_I32 : WebAssemblyTerRMW; defm ATOMIC_RMW16_U_CMPXCHG_I32 : WebAssemblyTerRMW; defm ATOMIC_RMW8_U_CMPXCHG_I64 : WebAssemblyTerRMW; defm ATOMIC_RMW16_U_CMPXCHG_I64 : WebAssemblyTerRMW; defm ATOMIC_RMW32_U_CMPXCHG_I64 : WebAssemblyTerRMW; // Select ternary RMWs with no constant offset. multiclass TerRMWPatNoOffset { def : Pat<(ty (kind I32:$addr, ty:$exp, ty:$new)), (!cast(inst#_A32) 0, 0, I32:$addr, ty:$exp, ty:$new)>, Requires<[HasAddr32, HasAtomics]>; def : Pat<(ty (kind I64:$addr, ty:$exp, ty:$new)), (!cast(inst#_A64) 0, 0, I64:$addr, ty:$exp, ty:$new)>, Requires<[HasAddr64, HasAtomics]>; } // Select ternary RMWs with a constant offset. // Pattern with address + immediate offset multiclass TerRMWPatImmOff { def : Pat<(ty (kind (operand I32:$addr, imm:$off), ty:$exp, ty:$new)), (!cast(inst#_A32) 0, imm:$off, I32:$addr, ty:$exp, ty:$new)>, Requires<[HasAddr32, HasAtomics]>; def : Pat<(ty (kind (operand I64:$addr, imm:$off), ty:$exp, ty:$new)), (!cast(inst#_A64) 0, imm:$off, I64:$addr, ty:$exp, ty:$new)>, Requires<[HasAddr64, HasAtomics]>; } // Select ternary RMWs with just a constant offset. multiclass TerRMWPatOffsetOnly { def : Pat<(ty (kind imm:$off, ty:$exp, ty:$new)), (!cast(inst#_A32) 0, imm:$off, (CONST_I32 0), ty:$exp, ty:$new)>; def : Pat<(ty (kind imm:$off, ty:$exp, ty:$new)), (!cast(inst#_A64) 0, imm:$off, (CONST_I64 0), ty:$exp, ty:$new)>; } multiclass TerRMWPatGlobalAddrOffOnly { def : Pat<(ty (kind (WebAssemblywrapper tglobaladdr:$off), ty:$exp, ty:$new)), (!cast(inst#_A32) 0, tglobaladdr:$off, (CONST_I32 0), ty:$exp, ty:$new)>, Requires<[HasAddr32, HasAtomics, IsNotPIC]>; def : Pat<(ty (kind (WebAssemblywrapper tglobaladdr:$off), ty:$exp, ty:$new)), (!cast(inst#_A64) 0, tglobaladdr:$off, (CONST_I64 0), ty:$exp, ty:$new)>, Requires<[HasAddr64, HasAtomics, IsNotPIC]>; } // Patterns for various addressing modes. multiclass TerRMWPattern { defm : TerRMWPatNoOffset; defm : TerRMWPatNoOffset; defm : TerRMWPatImmOff; defm : TerRMWPatImmOff; defm : TerRMWPatImmOff; defm : TerRMWPatImmOff; defm : TerRMWPatOffsetOnly; defm : TerRMWPatOffsetOnly; defm : TerRMWPatGlobalAddrOffOnly; defm : TerRMWPatGlobalAddrOffOnly; } defm : TerRMWPattern; // Truncating & zero-extending ternary RMW patterns. // DAG legalization & optimization before instruction selection may introduce // additional nodes such as anyext or assertzext depending on operand types. class zext_ter_rmw_8_32 : PatFrag<(ops node:$addr, node:$exp, node:$new), (i32 (kind node:$addr, node:$exp, node:$new))>; class zext_ter_rmw_16_32 : zext_ter_rmw_8_32; class zext_ter_rmw_8_64 : PatFrag<(ops node:$addr, node:$exp, node:$new), (zext (i32 (assertzext (i32 (kind node:$addr, (i32 (trunc (i64 node:$exp))), (i32 (trunc (i64 node:$new))))))))>; class zext_ter_rmw_16_64 : zext_ter_rmw_8_64; class zext_ter_rmw_32_64 : PatFrag<(ops node:$addr, node:$exp, node:$new), (zext (i32 (kind node:$addr, (i32 (trunc (i64 node:$exp))), (i32 (trunc (i64 node:$new))))))>; // Truncating & sign-extending ternary RMW patterns. // We match subword RMWs (for 32-bit) and anyext RMWs (for 64-bit) and select a // zext RMW; the next instruction will be sext_inreg which is selected by // itself. class sext_ter_rmw_8_32 : PatFrag<(ops node:$addr, node:$exp, node:$new), (kind node:$addr, node:$exp, node:$new)>; class sext_ter_rmw_16_32 : sext_ter_rmw_8_32; class sext_ter_rmw_8_64 : PatFrag<(ops node:$addr, node:$exp, node:$new), (anyext (i32 (assertzext (i32 (kind node:$addr, (i32 (trunc (i64 node:$exp))), (i32 (trunc (i64 node:$new))))))))>; class sext_ter_rmw_16_64 : sext_ter_rmw_8_64; // 32->64 sext RMW gets selected as i32.atomic.rmw.***, i64.extend_i32_s // Patterns for various addressing modes for truncating-extending ternary RMWs. multiclass TerRMWTruncExtPattern< PatFrag rmw_8, PatFrag rmw_16, PatFrag rmw_32, PatFrag rmw_64, string inst8_32, string inst16_32, string inst8_64, string inst16_64, string inst32_64> { // Truncating-extending ternary RMWs with no constant offset defm : TerRMWPatNoOffset, inst8_32>; defm : TerRMWPatNoOffset, inst16_32>; defm : TerRMWPatNoOffset, inst8_64>; defm : TerRMWPatNoOffset, inst16_64>; defm : TerRMWPatNoOffset, inst32_64>; defm : TerRMWPatNoOffset, inst8_32>; defm : TerRMWPatNoOffset, inst16_32>; defm : TerRMWPatNoOffset, inst8_64>; defm : TerRMWPatNoOffset, inst16_64>; // Truncating-extending ternary RMWs with a constant offset defm : TerRMWPatImmOff, regPlusImm, inst8_32>; defm : TerRMWPatImmOff, regPlusImm, inst16_32>; defm : TerRMWPatImmOff, regPlusImm, inst8_64>; defm : TerRMWPatImmOff, regPlusImm, inst16_64>; defm : TerRMWPatImmOff, regPlusImm, inst32_64>; defm : TerRMWPatImmOff, or_is_add, inst8_32>; defm : TerRMWPatImmOff, or_is_add, inst16_32>; defm : TerRMWPatImmOff, or_is_add, inst8_64>; defm : TerRMWPatImmOff, or_is_add, inst16_64>; defm : TerRMWPatImmOff, or_is_add, inst32_64>; defm : TerRMWPatImmOff, regPlusImm, inst8_32>; defm : TerRMWPatImmOff, regPlusImm, inst16_32>; defm : TerRMWPatImmOff, regPlusImm, inst8_64>; defm : TerRMWPatImmOff, regPlusImm, inst16_64>; defm : TerRMWPatImmOff, or_is_add, inst8_32>; defm : TerRMWPatImmOff, or_is_add, inst16_32>; defm : TerRMWPatImmOff, or_is_add, inst8_64>; defm : TerRMWPatImmOff, or_is_add, inst16_64>; // Truncating-extending ternary RMWs with just a constant offset defm : TerRMWPatOffsetOnly, inst8_32>; defm : TerRMWPatOffsetOnly, inst16_32>; defm : TerRMWPatOffsetOnly, inst8_64>; defm : TerRMWPatOffsetOnly, inst16_64>; defm : TerRMWPatOffsetOnly, inst32_64>; defm : TerRMWPatOffsetOnly, inst8_32>; defm : TerRMWPatOffsetOnly, inst16_32>; defm : TerRMWPatOffsetOnly, inst8_64>; defm : TerRMWPatOffsetOnly, inst16_64>; defm : TerRMWPatGlobalAddrOffOnly, inst8_32>; defm : TerRMWPatGlobalAddrOffOnly, inst16_32>; defm : TerRMWPatGlobalAddrOffOnly, inst8_64>; defm : TerRMWPatGlobalAddrOffOnly, inst16_64>; defm : TerRMWPatGlobalAddrOffOnly, inst32_64>; defm : TerRMWPatGlobalAddrOffOnly, inst8_32>; defm : TerRMWPatGlobalAddrOffOnly, inst16_32>; defm : TerRMWPatGlobalAddrOffOnly, inst8_64>; defm : TerRMWPatGlobalAddrOffOnly, inst16_64>; } defm : TerRMWTruncExtPattern< atomic_cmp_swap_8, atomic_cmp_swap_16, atomic_cmp_swap_32, atomic_cmp_swap_64, "ATOMIC_RMW8_U_CMPXCHG_I32", "ATOMIC_RMW16_U_CMPXCHG_I32", "ATOMIC_RMW8_U_CMPXCHG_I64", "ATOMIC_RMW16_U_CMPXCHG_I64", "ATOMIC_RMW32_U_CMPXCHG_I64">;