1 // WebAssemblyInstrMemory.td-WebAssembly Memory codegen support -*- tablegen -*-
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 /// WebAssembly Memory operand code-gen constructs.
12 //===----------------------------------------------------------------------===//
15 // - WebAssemblyTargetLowering having to do with atomics
16 // - Each has optional alignment.
18 // WebAssembly has i8/i16/i32/i64/f32/f64 memory types, but doesn't have i8/i16
19 // local types. These memory-only types instead zero- or sign-extend into local
20 // types when loading, and truncate when storing.
24 // These patterns match the static (offset) and dynamic (address stack operand)
25 // operands for loads and stores, based on a combination of target global
26 // addresses and constants.
28 // (load (add tga, x)) -> load offset=tga, addr=x
29 // (store v, tga) -> store v, offset=tga, addr=0
30 // (load (add const, x)) -> load offset=const, addr=x
31 // (store v, const) -> store v, offset=const, addr=0
32 // (load x) -> load offset=0, addr=x
33 def AddrOps32 : ComplexPattern<i32, 2, "SelectAddrOperands32">;
34 def AddrOps64 : ComplexPattern<i64, 2, "SelectAddrOperands64">;
36 // Defines atomic and non-atomic loads, regular and extending.
37 multiclass WebAssemblyLoad<WebAssemblyRegClass rc, string Name, int Opcode,
38 list<Predicate> reqs = []> {
39 let mayLoad = 1, UseNamedOperandTable = 1 in {
40 defm "_A32": I<(outs rc:$dst),
41 (ins P2Align:$p2align, offset32_op:$off, I32:$addr),
42 (outs), (ins P2Align:$p2align, offset32_op:$off),
43 [], !strconcat(Name, "\t$dst, ${off}(${addr})${p2align}"),
44 !strconcat(Name, "\t${off}${p2align}"), Opcode, false>,
46 defm "_A64": I<(outs rc:$dst),
47 (ins P2Align:$p2align, offset64_op:$off, I64:$addr),
48 (outs), (ins P2Align:$p2align, offset64_op:$off),
49 [], !strconcat(Name, "\t$dst, ${off}(${addr})${p2align}"),
50 !strconcat(Name, "\t${off}${p2align}"), Opcode, true>,
56 // FIXME: When we can break syntax compatibility, reorder the fields in the
57 // asmstrings to match the binary encoding.
58 defm LOAD_I32 : WebAssemblyLoad<I32, "i32.load", 0x28, []>;
59 defm LOAD_I64 : WebAssemblyLoad<I64, "i64.load", 0x29, []>;
60 defm LOAD_F32 : WebAssemblyLoad<F32, "f32.load", 0x2a, []>;
61 defm LOAD_F64 : WebAssemblyLoad<F64, "f64.load", 0x2b, []>;
64 defm LOAD8_S_I32 : WebAssemblyLoad<I32, "i32.load8_s", 0x2c, []>;
65 defm LOAD8_U_I32 : WebAssemblyLoad<I32, "i32.load8_u", 0x2d, []>;
66 defm LOAD16_S_I32 : WebAssemblyLoad<I32, "i32.load16_s", 0x2e, []>;
67 defm LOAD16_U_I32 : WebAssemblyLoad<I32, "i32.load16_u", 0x2f, []>;
68 defm LOAD8_S_I64 : WebAssemblyLoad<I64, "i64.load8_s", 0x30, []>;
69 defm LOAD8_U_I64 : WebAssemblyLoad<I64, "i64.load8_u", 0x31, []>;
70 defm LOAD16_S_I64 : WebAssemblyLoad<I64, "i64.load16_s", 0x32, []>;
71 defm LOAD16_U_I64 : WebAssemblyLoad<I64, "i64.load16_u", 0x33, []>;
72 defm LOAD32_S_I64 : WebAssemblyLoad<I64, "i64.load32_s", 0x34, []>;
73 defm LOAD32_U_I64 : WebAssemblyLoad<I64, "i64.load32_u", 0x35, []>;
75 // Half-precision load.
77 WebAssemblyLoad<F32, "f32.load_f16", 0xfc30, [HasFP16]>;
81 multiclass LoadPat<ValueType ty, SDPatternOperator kind, string Name> {
82 def : Pat<(ty (kind (AddrOps32 offset32_op:$offset, I32:$addr))),
83 (!cast<NI>(Name # "_A32") 0,
86 Requires<[HasAddr32]>;
88 def : Pat<(ty (kind (AddrOps64 offset64_op:$offset, I64:$addr))),
89 (!cast<NI>(Name # "_A64") 0,
92 Requires<[HasAddr64]>;
95 defm : LoadPat<i32, load, "LOAD_I32">;
96 defm : LoadPat<i64, load, "LOAD_I64">;
97 defm : LoadPat<f32, load, "LOAD_F32">;
98 defm : LoadPat<f64, load, "LOAD_F64">;
100 defm : LoadPat<i32, sextloadi8, "LOAD8_S_I32">;
101 defm : LoadPat<i32, sextloadi16, "LOAD16_S_I32">;
102 defm : LoadPat<i64, sextloadi8, "LOAD8_S_I64">;
103 defm : LoadPat<i64, sextloadi16, "LOAD16_S_I64">;
104 defm : LoadPat<i64, sextloadi32, "LOAD32_S_I64">;
106 defm : LoadPat<i32, zextloadi8, "LOAD8_U_I32">;
107 defm : LoadPat<i32, zextloadi16, "LOAD16_U_I32">;
108 defm : LoadPat<i64, zextloadi8, "LOAD8_U_I64">;
109 defm : LoadPat<i64, zextloadi16, "LOAD16_U_I64">;
110 defm : LoadPat<i64, zextloadi32, "LOAD32_U_I64">;
112 defm : LoadPat<i32, extloadi8, "LOAD8_U_I32">;
113 defm : LoadPat<i32, extloadi16, "LOAD16_U_I32">;
114 defm : LoadPat<i64, extloadi8, "LOAD8_U_I64">;
115 defm : LoadPat<i64, extloadi16, "LOAD16_U_I64">;
116 defm : LoadPat<i64, extloadi32, "LOAD32_U_I64">;
118 defm : LoadPat<f32, int_wasm_loadf16_f32, "LOAD_F16_F32">;
120 // Defines atomic and non-atomic stores, regular and truncating
121 multiclass WebAssemblyStore<WebAssemblyRegClass rc, string Name, int Opcode,
122 list<Predicate> reqs = []> {
123 let mayStore = 1, UseNamedOperandTable = 1 in
124 defm "_A32" : I<(outs),
125 (ins P2Align:$p2align, offset32_op:$off, I32:$addr, rc:$val),
127 (ins P2Align:$p2align, offset32_op:$off), [],
128 !strconcat(Name, "\t${off}(${addr})${p2align}, $val"),
129 !strconcat(Name, "\t${off}${p2align}"), Opcode, false>,
131 let mayStore = 1, UseNamedOperandTable = 1 in
132 defm "_A64" : I<(outs),
133 (ins P2Align:$p2align, offset64_op:$off, I64:$addr, rc:$val),
135 (ins P2Align:$p2align, offset64_op:$off), [],
136 !strconcat(Name, "\t${off}(${addr})${p2align}, $val"),
137 !strconcat(Name, "\t${off}${p2align}"), Opcode, true>,
142 // Note: WebAssembly inverts SelectionDAG's usual operand order.
143 defm STORE_I32 : WebAssemblyStore<I32, "i32.store", 0x36>;
144 defm STORE_I64 : WebAssemblyStore<I64, "i64.store", 0x37>;
145 defm STORE_F32 : WebAssemblyStore<F32, "f32.store", 0x38>;
146 defm STORE_F64 : WebAssemblyStore<F64, "f64.store", 0x39>;
148 multiclass StorePat<ValueType ty, SDPatternOperator kind, string Name> {
149 def : Pat<(kind ty:$val, (AddrOps32 offset32_op:$offset, I32:$addr)),
150 (!cast<NI>(Name # "_A32") 0,
154 Requires<[HasAddr32]>;
155 def : Pat<(kind ty:$val, (AddrOps64 offset64_op:$offset, I64:$addr)),
156 (!cast<NI>(Name # "_A64") 0,
160 Requires<[HasAddr64]>;
163 defm : StorePat<i32, store, "STORE_I32">;
164 defm : StorePat<i64, store, "STORE_I64">;
165 defm : StorePat<f32, store, "STORE_F32">;
166 defm : StorePat<f64, store, "STORE_F64">;
169 defm STORE8_I32 : WebAssemblyStore<I32, "i32.store8", 0x3a>;
170 defm STORE16_I32 : WebAssemblyStore<I32, "i32.store16", 0x3b>;
171 defm STORE8_I64 : WebAssemblyStore<I64, "i64.store8", 0x3c>;
172 defm STORE16_I64 : WebAssemblyStore<I64, "i64.store16", 0x3d>;
173 defm STORE32_I64 : WebAssemblyStore<I64, "i64.store32", 0x3e>;
175 // Half-precision store.
177 WebAssemblyStore<F32, "f32.store_f16", 0xfc31, [HasFP16]>;
179 defm : StorePat<i32, truncstorei8, "STORE8_I32">;
180 defm : StorePat<i32, truncstorei16, "STORE16_I32">;
181 defm : StorePat<i64, truncstorei8, "STORE8_I64">;
182 defm : StorePat<i64, truncstorei16, "STORE16_I64">;
183 defm : StorePat<i64, truncstorei32, "STORE32_I64">;
185 defm : StorePat<f32, int_wasm_storef16_f32, "STORE_F16_F32">;
187 multiclass MemoryOps<WebAssemblyRegClass rc, string B> {
188 // Current memory size.
189 defm MEMORY_SIZE_A#B : I<(outs rc:$dst), (ins i32imm:$flags),
190 (outs), (ins i32imm:$flags),
192 (int_wasm_memory_size (i32 imm:$flags)))],
193 "memory.size\t$dst, $flags", "memory.size\t$flags",
197 defm MEMORY_GROW_A#B : I<(outs rc:$dst), (ins i32imm:$flags, rc:$delta),
198 (outs), (ins i32imm:$flags),
200 (int_wasm_memory_grow (i32 imm:$flags),
202 "memory.grow\t$dst, $flags, $delta",
203 "memory.grow\t$flags", 0x40>;
206 defm : MemoryOps<I32, "32">;
207 defm : MemoryOps<I64, "64">;