// WebAssemblyInstrMemory.td-WebAssembly Memory 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 Memory operand code-gen constructs. /// //===----------------------------------------------------------------------===// // TODO: // - WebAssemblyTargetLowering having to do with atomics // - Each has optional alignment. // WebAssembly has i8/i16/i32/i64/f32/f64 memory types, but doesn't have i8/i16 // local types. These memory-only types instead zero- or sign-extend into local // types when loading, and truncate when storing. // WebAssembly constant offsets are performed as unsigned with infinite // precision, so we need to check for NoUnsignedWrap so that we don't fold an // offset for an add that needs wrapping. def regPlusImm : PatFrag<(ops node:$addr, node:$off), (add node:$addr, node:$off), [{ return N->getFlags().hasNoUnsignedWrap(); }]>; // Treat an 'or' node as an 'add' if the or'ed bits are known to be zero. def or_is_add : PatFrag<(ops node:$lhs, node:$rhs), (or node:$lhs, node:$rhs),[{ if (ConstantSDNode *CN = dyn_cast(N->getOperand(1))) return CurDAG->MaskedValueIsZero(N->getOperand(0), CN->getAPIntValue()); KnownBits Known0 = CurDAG->computeKnownBits(N->getOperand(0), 0); KnownBits Known1 = CurDAG->computeKnownBits(N->getOperand(1), 0); return (~Known0.Zero & ~Known1.Zero) == 0; }]>; // We don't need a regPlusES because external symbols never have constant // offsets folded into them, so we can just use add. // Defines atomic and non-atomic loads, regular and extending. multiclass WebAssemblyLoad reqs = []> { let mayLoad = 1, UseNamedOperandTable = 1 in { defm "_A32": I<(outs rc:$dst), (ins P2Align:$p2align, offset32_op:$off, I32:$addr), (outs), (ins P2Align:$p2align, offset32_op:$off), [], !strconcat(Name, "\t$dst, ${off}(${addr})${p2align}"), !strconcat(Name, "\t${off}${p2align}"), Opcode, "false">, Requires; defm "_A64": I<(outs rc:$dst), (ins P2Align:$p2align, offset64_op:$off, I64:$addr), (outs), (ins P2Align:$p2align, offset64_op:$off), [], !strconcat(Name, "\t$dst, ${off}(${addr})${p2align}"), !strconcat(Name, "\t${off}${p2align}"), Opcode, "true">, Requires; } } // Basic load. // FIXME: When we can break syntax compatibility, reorder the fields in the // asmstrings to match the binary encoding. defm LOAD_I32 : WebAssemblyLoad; defm LOAD_I64 : WebAssemblyLoad; defm LOAD_F32 : WebAssemblyLoad; defm LOAD_F64 : WebAssemblyLoad; // Select loads with no constant offset. multiclass LoadPatNoOffset { def : Pat<(ty (kind I32:$addr)), (!cast(inst # "_A32") 0, 0, I32:$addr)>, Requires<[HasAddr32]>; def : Pat<(ty (kind (i64 I64:$addr))), (!cast(inst # "_A64") 0, 0, I64:$addr)>, Requires<[HasAddr64]>; } defm : LoadPatNoOffset; defm : LoadPatNoOffset; defm : LoadPatNoOffset; defm : LoadPatNoOffset; // Select loads with a constant offset. // Pattern with address + immediate offset multiclass LoadPatImmOff { def : Pat<(ty (kind (operand I32:$addr, imm:$off))), (!cast(inst # "_A32") 0, imm:$off, I32:$addr)>, Requires<[HasAddr32]>; def : Pat<(ty (kind (operand I64:$addr, imm:$off))), (!cast(inst # "_A64") 0, imm:$off, I64:$addr)>, Requires<[HasAddr64]>; } defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; // Select loads with just a constant offset. multiclass LoadPatOffsetOnly { def : Pat<(ty (kind imm:$off)), (!cast(inst # "_A32") 0, imm:$off, (CONST_I32 0))>, Requires<[HasAddr32]>; def : Pat<(ty (kind imm:$off)), (!cast(inst # "_A64") 0, imm:$off, (CONST_I64 0))>, Requires<[HasAddr64]>; } defm : LoadPatOffsetOnly; defm : LoadPatOffsetOnly; defm : LoadPatOffsetOnly; defm : LoadPatOffsetOnly; multiclass LoadPatGlobalAddrOffOnly { def : Pat<(ty (kind (WebAssemblyWrapper tglobaladdr:$off))), (!cast(inst # "_A32") 0, tglobaladdr:$off, (CONST_I32 0))>, Requires<[IsNotPIC, HasAddr32]>; def : Pat<(ty (kind (WebAssemblyWrapper tglobaladdr:$off))), (!cast(inst # "_A64") 0, tglobaladdr:$off, (CONST_I64 0))>, Requires<[IsNotPIC, HasAddr64]>; } defm : LoadPatGlobalAddrOffOnly; defm : LoadPatGlobalAddrOffOnly; defm : LoadPatGlobalAddrOffOnly; defm : LoadPatGlobalAddrOffOnly; // Extending load. defm LOAD8_S_I32 : WebAssemblyLoad; defm LOAD8_U_I32 : WebAssemblyLoad; defm LOAD16_S_I32 : WebAssemblyLoad; defm LOAD16_U_I32 : WebAssemblyLoad; defm LOAD8_S_I64 : WebAssemblyLoad; defm LOAD8_U_I64 : WebAssemblyLoad; defm LOAD16_S_I64 : WebAssemblyLoad; defm LOAD16_U_I64 : WebAssemblyLoad; defm LOAD32_S_I64 : WebAssemblyLoad; defm LOAD32_U_I64 : WebAssemblyLoad; // Select extending loads with no constant offset. defm : LoadPatNoOffset; defm : LoadPatNoOffset; defm : LoadPatNoOffset; defm : LoadPatNoOffset; defm : LoadPatNoOffset; defm : LoadPatNoOffset; defm : LoadPatNoOffset; defm : LoadPatNoOffset; defm : LoadPatNoOffset; defm : LoadPatNoOffset; // Select extending loads with a constant offset. defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; // Select extending loads with just a constant offset. defm : LoadPatOffsetOnly; defm : LoadPatOffsetOnly; defm : LoadPatOffsetOnly; 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; defm : LoadPatGlobalAddrOffOnly; defm : LoadPatGlobalAddrOffOnly; defm : LoadPatGlobalAddrOffOnly; // Resolve "don't care" extending loads to zero-extending loads. This is // somewhat arbitrary, but zero-extending is conceptually simpler. // Select "don't care" extending loads with no constant offset. defm : LoadPatNoOffset; defm : LoadPatNoOffset; defm : LoadPatNoOffset; defm : LoadPatNoOffset; defm : LoadPatNoOffset; // Select "don't care" extending loads with a constant offset. defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; defm : LoadPatImmOff; // Select "don't care" extending loads with just a constant offset. defm : LoadPatOffsetOnly; defm : LoadPatOffsetOnly; defm : LoadPatOffsetOnly; defm : LoadPatOffsetOnly; defm : LoadPatOffsetOnly; defm : LoadPatGlobalAddrOffOnly; defm : LoadPatGlobalAddrOffOnly; defm : LoadPatGlobalAddrOffOnly; defm : LoadPatGlobalAddrOffOnly; defm : LoadPatGlobalAddrOffOnly; // Defines atomic and non-atomic stores, regular and truncating multiclass WebAssemblyStore reqs = []> { let mayStore = 1, UseNamedOperandTable = 1 in defm "_A32" : I<(outs), (ins P2Align:$p2align, offset32_op:$off, I32:$addr, rc:$val), (outs), (ins P2Align:$p2align, offset32_op:$off), [], !strconcat(Name, "\t${off}(${addr})${p2align}, $val"), !strconcat(Name, "\t${off}${p2align}"), Opcode, "false">, Requires; let mayStore = 1, UseNamedOperandTable = 1 in defm "_A64" : I<(outs), (ins P2Align:$p2align, offset64_op:$off, I64:$addr, rc:$val), (outs), (ins P2Align:$p2align, offset64_op:$off), [], !strconcat(Name, "\t${off}(${addr})${p2align}, $val"), !strconcat(Name, "\t${off}${p2align}"), Opcode, "true">, Requires; } // Basic store. // Note: WebAssembly inverts SelectionDAG's usual operand order. defm STORE_I32 : WebAssemblyStore; defm STORE_I64 : WebAssemblyStore; defm STORE_F32 : WebAssemblyStore; defm STORE_F64 : WebAssemblyStore; // Select stores with no constant offset. multiclass StorePatNoOffset { def : Pat<(node ty:$val, I32:$addr), (!cast(inst # "_A32") 0, 0, I32:$addr, ty:$val)>, Requires<[HasAddr32]>; def : Pat<(node ty:$val, I64:$addr), (!cast(inst # "_A64") 0, 0, I64:$addr, ty:$val)>, Requires<[HasAddr64]>; } defm : StorePatNoOffset; defm : StorePatNoOffset; defm : StorePatNoOffset; defm : StorePatNoOffset; // Select stores with a constant offset. multiclass StorePatImmOff { def : Pat<(kind ty:$val, (operand I32:$addr, imm:$off)), (!cast(inst # "_A32") 0, imm:$off, I32:$addr, ty:$val)>, Requires<[HasAddr32]>; def : Pat<(kind ty:$val, (operand I64:$addr, imm:$off)), (!cast(inst # "_A64") 0, imm:$off, I64:$addr, ty:$val)>, Requires<[HasAddr64]>; } defm : StorePatImmOff; defm : StorePatImmOff; defm : StorePatImmOff; defm : StorePatImmOff; defm : StorePatImmOff; defm : StorePatImmOff; defm : StorePatImmOff; defm : StorePatImmOff; // Select stores with just a constant offset. multiclass StorePatOffsetOnly { def : Pat<(kind ty:$val, imm:$off), (!cast(inst # "_A32") 0, imm:$off, (CONST_I32 0), ty:$val)>, Requires<[HasAddr32]>; def : Pat<(kind ty:$val, imm:$off), (!cast(inst # "_A64") 0, imm:$off, (CONST_I64 0), ty:$val)>, Requires<[HasAddr64]>; } defm : StorePatOffsetOnly; defm : StorePatOffsetOnly; defm : StorePatOffsetOnly; defm : StorePatOffsetOnly; multiclass StorePatGlobalAddrOffOnly { def : Pat<(kind ty:$val, (WebAssemblyWrapper tglobaladdr:$off)), (!cast(inst # "_A32") 0, tglobaladdr:$off, (CONST_I32 0), ty:$val)>, Requires<[IsNotPIC, HasAddr32]>; def : Pat<(kind ty:$val, (WebAssemblyWrapper tglobaladdr:$off)), (!cast(inst # "_A64") 0, tglobaladdr:$off, (CONST_I64 0), ty:$val)>, Requires<[IsNotPIC, HasAddr64]>; } defm : StorePatGlobalAddrOffOnly; defm : StorePatGlobalAddrOffOnly; defm : StorePatGlobalAddrOffOnly; defm : StorePatGlobalAddrOffOnly; // Truncating store. defm STORE8_I32 : WebAssemblyStore; defm STORE16_I32 : WebAssemblyStore; defm STORE8_I64 : WebAssemblyStore; defm STORE16_I64 : WebAssemblyStore; defm STORE32_I64 : WebAssemblyStore; // Select truncating stores with no constant offset. defm : StorePatNoOffset; defm : StorePatNoOffset; defm : StorePatNoOffset; defm : StorePatNoOffset; defm : StorePatNoOffset; // Select truncating stores with a constant offset. defm : StorePatImmOff; defm : StorePatImmOff; defm : StorePatImmOff; defm : StorePatImmOff; defm : StorePatImmOff; defm : StorePatImmOff; defm : StorePatImmOff; defm : StorePatImmOff; defm : StorePatImmOff; defm : StorePatImmOff; // Select truncating stores with just a constant offset. defm : StorePatOffsetOnly; defm : StorePatOffsetOnly; defm : StorePatOffsetOnly; defm : StorePatOffsetOnly; defm : StorePatOffsetOnly; defm : StorePatGlobalAddrOffOnly; defm : StorePatGlobalAddrOffOnly; defm : StorePatGlobalAddrOffOnly; defm : StorePatGlobalAddrOffOnly; defm : StorePatGlobalAddrOffOnly; multiclass MemoryOps { // Current memory size. defm MEMORY_SIZE_A#B : I<(outs rc:$dst), (ins i32imm:$flags), (outs), (ins i32imm:$flags), [(set rc:$dst, (int_wasm_memory_size (i32 imm:$flags)))], "memory.size\t$dst, $flags", "memory.size\t$flags", 0x3f>; // Grow memory. defm MEMORY_GROW_A#B : I<(outs rc:$dst), (ins i32imm:$flags, rc:$delta), (outs), (ins i32imm:$flags), [(set rc:$dst, (int_wasm_memory_grow (i32 imm:$flags), rc:$delta))], "memory.grow\t$dst, $flags, $delta", "memory.grow\t$flags", 0x40>; } defm : MemoryOps; defm : MemoryOps;