WebAssemblyInstrMemory.td 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. // TODO:
  14. // - WebAssemblyTargetLowering having to do with atomics
  15. // - Each has optional alignment.
  16. // WebAssembly has i8/i16/i32/i64/f32/f64 memory types, but doesn't have i8/i16
  17. // local types. These memory-only types instead zero- or sign-extend into local
  18. // types when loading, and truncate when storing.
  19. // Address Operands
  20. // These patterns match the static (offset) and dynamic (address stack operand)
  21. // operands for loads and stores, based on a combination of target global
  22. // addresses and constants.
  23. // For example,
  24. // (load (add tga, x)) -> load offset=tga, addr=x
  25. // (store v, tga) -> store v, offset=tga, addr=0
  26. // (load (add const, x)) -> load offset=const, addr=x
  27. // (store v, const) -> store v, offset=const, addr=0
  28. // (load x) -> load offset=0, addr=x
  29. def AddrOps32 : ComplexPattern<i32, 2, "SelectAddrOperands32">;
  30. def AddrOps64 : ComplexPattern<i64, 2, "SelectAddrOperands64">;
  31. // Defines atomic and non-atomic loads, regular and extending.
  32. multiclass WebAssemblyLoad<WebAssemblyRegClass rc, string Name, int Opcode,
  33. list<Predicate> reqs = []> {
  34. let mayLoad = 1, UseNamedOperandTable = 1 in {
  35. defm "_A32": I<(outs rc:$dst),
  36. (ins P2Align:$p2align, offset32_op:$off, I32:$addr),
  37. (outs), (ins P2Align:$p2align, offset32_op:$off),
  38. [], !strconcat(Name, "\t$dst, ${off}(${addr})${p2align}"),
  39. !strconcat(Name, "\t${off}${p2align}"), Opcode, false>,
  40. Requires<reqs>;
  41. defm "_A64": I<(outs rc:$dst),
  42. (ins P2Align:$p2align, offset64_op:$off, I64:$addr),
  43. (outs), (ins P2Align:$p2align, offset64_op:$off),
  44. [], !strconcat(Name, "\t$dst, ${off}(${addr})${p2align}"),
  45. !strconcat(Name, "\t${off}${p2align}"), Opcode, true>,
  46. Requires<reqs>;
  47. }
  48. }
  49. // Basic load.
  50. // FIXME: When we can break syntax compatibility, reorder the fields in the
  51. // asmstrings to match the binary encoding.
  52. defm LOAD_I32 : WebAssemblyLoad<I32, "i32.load", 0x28, []>;
  53. defm LOAD_I64 : WebAssemblyLoad<I64, "i64.load", 0x29, []>;
  54. defm LOAD_F32 : WebAssemblyLoad<F32, "f32.load", 0x2a, []>;
  55. defm LOAD_F64 : WebAssemblyLoad<F64, "f64.load", 0x2b, []>;
  56. // Extending load.
  57. defm LOAD8_S_I32 : WebAssemblyLoad<I32, "i32.load8_s", 0x2c, []>;
  58. defm LOAD8_U_I32 : WebAssemblyLoad<I32, "i32.load8_u", 0x2d, []>;
  59. defm LOAD16_S_I32 : WebAssemblyLoad<I32, "i32.load16_s", 0x2e, []>;
  60. defm LOAD16_U_I32 : WebAssemblyLoad<I32, "i32.load16_u", 0x2f, []>;
  61. defm LOAD8_S_I64 : WebAssemblyLoad<I64, "i64.load8_s", 0x30, []>;
  62. defm LOAD8_U_I64 : WebAssemblyLoad<I64, "i64.load8_u", 0x31, []>;
  63. defm LOAD16_S_I64 : WebAssemblyLoad<I64, "i64.load16_s", 0x32, []>;
  64. defm LOAD16_U_I64 : WebAssemblyLoad<I64, "i64.load16_u", 0x33, []>;
  65. defm LOAD32_S_I64 : WebAssemblyLoad<I64, "i64.load32_s", 0x34, []>;
  66. defm LOAD32_U_I64 : WebAssemblyLoad<I64, "i64.load32_u", 0x35, []>;
  67. // Pattern matching
  68. multiclass LoadPat<ValueType ty, SDPatternOperator kind, string Name> {
  69. def : Pat<(ty (kind (AddrOps32 offset32_op:$offset, I32:$addr))),
  70. (!cast<NI>(Name # "_A32") 0,
  71. offset32_op:$offset,
  72. I32:$addr)>,
  73. Requires<[HasAddr32]>;
  74. def : Pat<(ty (kind (AddrOps64 offset64_op:$offset, I64:$addr))),
  75. (!cast<NI>(Name # "_A64") 0,
  76. offset64_op:$offset,
  77. I64:$addr)>,
  78. Requires<[HasAddr64]>;
  79. }
  80. defm : LoadPat<i32, load, "LOAD_I32">;
  81. defm : LoadPat<i64, load, "LOAD_I64">;
  82. defm : LoadPat<f32, load, "LOAD_F32">;
  83. defm : LoadPat<f64, load, "LOAD_F64">;
  84. defm : LoadPat<i32, sextloadi8, "LOAD8_S_I32">;
  85. defm : LoadPat<i32, sextloadi16, "LOAD16_S_I32">;
  86. defm : LoadPat<i64, sextloadi8, "LOAD8_S_I64">;
  87. defm : LoadPat<i64, sextloadi16, "LOAD16_S_I64">;
  88. defm : LoadPat<i64, sextloadi32, "LOAD32_S_I64">;
  89. defm : LoadPat<i32, zextloadi8, "LOAD8_U_I32">;
  90. defm : LoadPat<i32, zextloadi16, "LOAD16_U_I32">;
  91. defm : LoadPat<i64, zextloadi8, "LOAD8_U_I64">;
  92. defm : LoadPat<i64, zextloadi16, "LOAD16_U_I64">;
  93. defm : LoadPat<i64, zextloadi32, "LOAD32_U_I64">;
  94. defm : LoadPat<i32, extloadi8, "LOAD8_U_I32">;
  95. defm : LoadPat<i32, extloadi16, "LOAD16_U_I32">;
  96. defm : LoadPat<i64, extloadi8, "LOAD8_U_I64">;
  97. defm : LoadPat<i64, extloadi16, "LOAD16_U_I64">;
  98. defm : LoadPat<i64, extloadi32, "LOAD32_U_I64">;
  99. // Defines atomic and non-atomic stores, regular and truncating
  100. multiclass WebAssemblyStore<WebAssemblyRegClass rc, string Name, int Opcode,
  101. list<Predicate> reqs = []> {
  102. let mayStore = 1, UseNamedOperandTable = 1 in
  103. defm "_A32" : I<(outs),
  104. (ins P2Align:$p2align, offset32_op:$off, I32:$addr, rc:$val),
  105. (outs),
  106. (ins P2Align:$p2align, offset32_op:$off), [],
  107. !strconcat(Name, "\t${off}(${addr})${p2align}, $val"),
  108. !strconcat(Name, "\t${off}${p2align}"), Opcode, false>,
  109. Requires<reqs>;
  110. let mayStore = 1, UseNamedOperandTable = 1 in
  111. defm "_A64" : I<(outs),
  112. (ins P2Align:$p2align, offset64_op:$off, I64:$addr, rc:$val),
  113. (outs),
  114. (ins P2Align:$p2align, offset64_op:$off), [],
  115. !strconcat(Name, "\t${off}(${addr})${p2align}, $val"),
  116. !strconcat(Name, "\t${off}${p2align}"), Opcode, true>,
  117. Requires<reqs>;
  118. }
  119. // Basic store.
  120. // Note: WebAssembly inverts SelectionDAG's usual operand order.
  121. defm STORE_I32 : WebAssemblyStore<I32, "i32.store", 0x36>;
  122. defm STORE_I64 : WebAssemblyStore<I64, "i64.store", 0x37>;
  123. defm STORE_F32 : WebAssemblyStore<F32, "f32.store", 0x38>;
  124. defm STORE_F64 : WebAssemblyStore<F64, "f64.store", 0x39>;
  125. multiclass StorePat<ValueType ty, SDPatternOperator kind, string Name> {
  126. def : Pat<(kind ty:$val, (AddrOps32 offset32_op:$offset, I32:$addr)),
  127. (!cast<NI>(Name # "_A32") 0,
  128. offset32_op:$offset,
  129. I32:$addr,
  130. ty:$val)>,
  131. Requires<[HasAddr32]>;
  132. def : Pat<(kind ty:$val, (AddrOps64 offset64_op:$offset, I64:$addr)),
  133. (!cast<NI>(Name # "_A64") 0,
  134. offset64_op:$offset,
  135. I64:$addr,
  136. ty:$val)>,
  137. Requires<[HasAddr64]>;
  138. }
  139. defm : StorePat<i32, store, "STORE_I32">;
  140. defm : StorePat<i64, store, "STORE_I64">;
  141. defm : StorePat<f32, store, "STORE_F32">;
  142. defm : StorePat<f64, store, "STORE_F64">;
  143. // Truncating store.
  144. defm STORE8_I32 : WebAssemblyStore<I32, "i32.store8", 0x3a>;
  145. defm STORE16_I32 : WebAssemblyStore<I32, "i32.store16", 0x3b>;
  146. defm STORE8_I64 : WebAssemblyStore<I64, "i64.store8", 0x3c>;
  147. defm STORE16_I64 : WebAssemblyStore<I64, "i64.store16", 0x3d>;
  148. defm STORE32_I64 : WebAssemblyStore<I64, "i64.store32", 0x3e>;
  149. defm : StorePat<i32, truncstorei8, "STORE8_I32">;
  150. defm : StorePat<i32, truncstorei16, "STORE16_I32">;
  151. defm : StorePat<i64, truncstorei8, "STORE8_I64">;
  152. defm : StorePat<i64, truncstorei16, "STORE16_I64">;
  153. defm : StorePat<i64, truncstorei32, "STORE32_I64">;
  154. multiclass MemoryOps<WebAssemblyRegClass rc, string B> {
  155. // Current memory size.
  156. defm MEMORY_SIZE_A#B : I<(outs rc:$dst), (ins i32imm:$flags),
  157. (outs), (ins i32imm:$flags),
  158. [(set rc:$dst,
  159. (int_wasm_memory_size (i32 imm:$flags)))],
  160. "memory.size\t$dst, $flags", "memory.size\t$flags",
  161. 0x3f>;
  162. // Grow memory.
  163. defm MEMORY_GROW_A#B : I<(outs rc:$dst), (ins i32imm:$flags, rc:$delta),
  164. (outs), (ins i32imm:$flags),
  165. [(set rc:$dst,
  166. (int_wasm_memory_grow (i32 imm:$flags),
  167. rc:$delta))],
  168. "memory.grow\t$dst, $flags, $delta",
  169. "memory.grow\t$flags", 0x40>;
  170. }
  171. defm : MemoryOps<I32, "32">;
  172. defm : MemoryOps<I64, "64">;