1// WebAssemblyInstrMemory.td-WebAssembly Memory codegen support -*- tablegen -*-
2//
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
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// WebAssembly Memory operand code-gen constructs.
11///
12//===----------------------------------------------------------------------===//
13
14// TODO:
15//  - WebAssemblyTargetLowering having to do with atomics
16//  - Each has optional alignment.
17
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.
21
22// Address Operands
23
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.
27// For example,
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
33def AddrOps32 : ComplexPattern<i32, 2, "SelectAddrOperands32">;
34def AddrOps64 : ComplexPattern<i64, 2, "SelectAddrOperands64">;
35
36// Defines atomic and non-atomic loads, regular and extending.
37multiclass 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>,
45               Requires<reqs>;
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>,
51               Requires<reqs>;
52  }
53}
54
55// Basic load.
56// FIXME: When we can break syntax compatibility, reorder the fields in the
57// asmstrings to match the binary encoding.
58defm LOAD_I32 : WebAssemblyLoad<I32, "i32.load", 0x28, []>;
59defm LOAD_I64 : WebAssemblyLoad<I64, "i64.load", 0x29, []>;
60defm LOAD_F32 : WebAssemblyLoad<F32, "f32.load", 0x2a, []>;
61defm LOAD_F64 : WebAssemblyLoad<F64, "f64.load", 0x2b, []>;
62
63// Extending load.
64defm LOAD8_S_I32 : WebAssemblyLoad<I32, "i32.load8_s", 0x2c, []>;
65defm LOAD8_U_I32 : WebAssemblyLoad<I32, "i32.load8_u", 0x2d, []>;
66defm LOAD16_S_I32 : WebAssemblyLoad<I32, "i32.load16_s", 0x2e, []>;
67defm LOAD16_U_I32 : WebAssemblyLoad<I32, "i32.load16_u", 0x2f, []>;
68defm LOAD8_S_I64 : WebAssemblyLoad<I64, "i64.load8_s", 0x30, []>;
69defm LOAD8_U_I64 : WebAssemblyLoad<I64, "i64.load8_u", 0x31, []>;
70defm LOAD16_S_I64 : WebAssemblyLoad<I64, "i64.load16_s", 0x32, []>;
71defm LOAD16_U_I64 : WebAssemblyLoad<I64, "i64.load16_u", 0x33, []>;
72defm LOAD32_S_I64 : WebAssemblyLoad<I64, "i64.load32_s", 0x34, []>;
73defm LOAD32_U_I64 : WebAssemblyLoad<I64, "i64.load32_u", 0x35, []>;
74
75// Pattern matching
76
77multiclass LoadPat<ValueType ty, SDPatternOperator kind, string Name> {
78      def : Pat<(ty (kind (AddrOps32 offset32_op:$offset, I32:$addr))),
79           (!cast<NI>(Name # "_A32") 0,
80                                     offset32_op:$offset,
81                                     I32:$addr)>,
82                                     Requires<[HasAddr32]>;
83
84      def : Pat<(ty (kind (AddrOps64 offset64_op:$offset, I64:$addr))),
85           (!cast<NI>(Name # "_A64") 0,
86                                     offset64_op:$offset,
87                                     I64:$addr)>,
88                                     Requires<[HasAddr64]>;
89}
90
91defm : LoadPat<i32, load, "LOAD_I32">;
92defm : LoadPat<i64, load, "LOAD_I64">;
93defm : LoadPat<f32, load, "LOAD_F32">;
94defm : LoadPat<f64, load, "LOAD_F64">;
95
96defm : LoadPat<i32, sextloadi8, "LOAD8_S_I32">;
97defm : LoadPat<i32, sextloadi16, "LOAD16_S_I32">;
98defm : LoadPat<i64, sextloadi8, "LOAD8_S_I64">;
99defm : LoadPat<i64, sextloadi16, "LOAD16_S_I64">;
100defm : LoadPat<i64, sextloadi32, "LOAD32_S_I64">;
101
102defm : LoadPat<i32, zextloadi8, "LOAD8_U_I32">;
103defm : LoadPat<i32, zextloadi16, "LOAD16_U_I32">;
104defm : LoadPat<i64, zextloadi8, "LOAD8_U_I64">;
105defm : LoadPat<i64, zextloadi16, "LOAD16_U_I64">;
106defm : LoadPat<i64, zextloadi32, "LOAD32_U_I64">;
107
108defm : LoadPat<i32, extloadi8, "LOAD8_U_I32">;
109defm : LoadPat<i32, extloadi16, "LOAD16_U_I32">;
110defm : LoadPat<i64, extloadi8, "LOAD8_U_I64">;
111defm : LoadPat<i64, extloadi16, "LOAD16_U_I64">;
112defm : LoadPat<i64, extloadi32, "LOAD32_U_I64">;
113
114// Defines atomic and non-atomic stores, regular and truncating
115multiclass WebAssemblyStore<WebAssemblyRegClass rc, string Name, int Opcode,
116                            list<Predicate> reqs = []> {
117  let mayStore = 1, UseNamedOperandTable = 1 in
118  defm "_A32" : I<(outs),
119                  (ins P2Align:$p2align, offset32_op:$off, I32:$addr, rc:$val),
120                  (outs),
121                  (ins P2Align:$p2align, offset32_op:$off), [],
122                  !strconcat(Name, "\t${off}(${addr})${p2align}, $val"),
123                  !strconcat(Name, "\t${off}${p2align}"), Opcode, false>,
124                Requires<reqs>;
125  let mayStore = 1, UseNamedOperandTable = 1 in
126  defm "_A64" : I<(outs),
127                  (ins P2Align:$p2align, offset64_op:$off, I64:$addr, rc:$val),
128                  (outs),
129                  (ins P2Align:$p2align, offset64_op:$off), [],
130                  !strconcat(Name, "\t${off}(${addr})${p2align}, $val"),
131                  !strconcat(Name, "\t${off}${p2align}"), Opcode, true>,
132                Requires<reqs>;
133}
134
135// Basic store.
136// Note: WebAssembly inverts SelectionDAG's usual operand order.
137defm STORE_I32  : WebAssemblyStore<I32, "i32.store", 0x36>;
138defm STORE_I64  : WebAssemblyStore<I64, "i64.store", 0x37>;
139defm STORE_F32  : WebAssemblyStore<F32, "f32.store", 0x38>;
140defm STORE_F64  : WebAssemblyStore<F64, "f64.store", 0x39>;
141
142multiclass StorePat<ValueType ty, SDPatternOperator kind, string Name> {
143  def : Pat<(kind ty:$val, (AddrOps32 offset32_op:$offset, I32:$addr)),
144            (!cast<NI>(Name # "_A32") 0,
145                                      offset32_op:$offset,
146                                      I32:$addr,
147                                      ty:$val)>,
148            Requires<[HasAddr32]>;
149  def : Pat<(kind ty:$val, (AddrOps64 offset64_op:$offset, I64:$addr)),
150            (!cast<NI>(Name # "_A64") 0,
151                                      offset64_op:$offset,
152                                      I64:$addr,
153                                      ty:$val)>,
154            Requires<[HasAddr64]>;
155}
156
157defm : StorePat<i32, store, "STORE_I32">;
158defm : StorePat<i64, store, "STORE_I64">;
159defm : StorePat<f32, store, "STORE_F32">;
160defm : StorePat<f64, store, "STORE_F64">;
161
162// Truncating store.
163defm STORE8_I32 : WebAssemblyStore<I32, "i32.store8", 0x3a>;
164defm STORE16_I32 : WebAssemblyStore<I32, "i32.store16", 0x3b>;
165defm STORE8_I64 : WebAssemblyStore<I64, "i64.store8", 0x3c>;
166defm STORE16_I64 : WebAssemblyStore<I64, "i64.store16", 0x3d>;
167defm STORE32_I64 : WebAssemblyStore<I64, "i64.store32", 0x3e>;
168
169defm : StorePat<i32, truncstorei8, "STORE8_I32">;
170defm : StorePat<i32, truncstorei16, "STORE16_I32">;
171defm : StorePat<i64, truncstorei8, "STORE8_I64">;
172defm : StorePat<i64, truncstorei16, "STORE16_I64">;
173defm : StorePat<i64, truncstorei32, "STORE32_I64">;
174
175multiclass MemoryOps<WebAssemblyRegClass rc, string B> {
176// Current memory size.
177defm MEMORY_SIZE_A#B : I<(outs rc:$dst), (ins i32imm:$flags),
178                         (outs), (ins i32imm:$flags),
179                         [(set rc:$dst,
180                           (int_wasm_memory_size (i32 imm:$flags)))],
181                         "memory.size\t$dst, $flags", "memory.size\t$flags",
182                         0x3f>;
183
184// Grow memory.
185defm MEMORY_GROW_A#B : I<(outs rc:$dst), (ins i32imm:$flags, rc:$delta),
186                         (outs), (ins i32imm:$flags),
187                         [(set rc:$dst,
188                           (int_wasm_memory_grow (i32 imm:$flags),
189                             rc:$delta))],
190                         "memory.grow\t$dst, $flags, $delta",
191                         "memory.grow\t$flags", 0x40>;
192}
193
194defm : MemoryOps<I32, "32">;
195defm : MemoryOps<I64, "64">;
196