WebAssemblyInstrAtomics.td 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. // WebAssemblyInstrAtomics.td-WebAssembly Atomic 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 Atomic operand code-gen constructs.
  11. ///
  12. //===----------------------------------------------------------------------===//
  13. let UseNamedOperandTable = 1 in
  14. multiclass ATOMIC_I<dag oops_r, dag iops_r, dag oops_s, dag iops_s,
  15. list<dag> pattern_r, string asmstr_r,
  16. string asmstr_s, bits<32> atomic_op,
  17. bit is64 = false> {
  18. defm "" : I<oops_r, iops_r, oops_s, iops_s, pattern_r, asmstr_r, asmstr_s,
  19. !or(0xfe00, !and(0xff, atomic_op)), is64>,
  20. Requires<[HasAtomics]>;
  21. }
  22. multiclass ATOMIC_NRI<dag oops, dag iops, list<dag> pattern, string asmstr = "",
  23. bits<32> atomic_op = -1> {
  24. defm "" : NRI<oops, iops, pattern, asmstr,
  25. !or(0xfe00, !and(0xff, atomic_op))>,
  26. Requires<[HasAtomics]>;
  27. }
  28. //===----------------------------------------------------------------------===//
  29. // Atomic wait / notify
  30. //===----------------------------------------------------------------------===//
  31. let hasSideEffects = 1 in {
  32. defm MEMORY_ATOMIC_NOTIFY_A32 :
  33. ATOMIC_I<(outs I32:$dst),
  34. (ins P2Align:$p2align, offset32_op:$off, I32:$addr, I32:$count),
  35. (outs), (ins P2Align:$p2align, offset32_op:$off), [],
  36. "memory.atomic.notify \t$dst, ${off}(${addr})${p2align}, $count",
  37. "memory.atomic.notify \t${off}${p2align}", 0x00, false>;
  38. defm MEMORY_ATOMIC_NOTIFY_A64 :
  39. ATOMIC_I<(outs I32:$dst),
  40. (ins P2Align:$p2align, offset64_op:$off, I64:$addr, I32:$count),
  41. (outs), (ins P2Align:$p2align, offset64_op:$off), [],
  42. "memory.atomic.notify \t$dst, ${off}(${addr})${p2align}, $count",
  43. "memory.atomic.notify \t${off}${p2align}", 0x00, true>;
  44. let mayLoad = 1 in {
  45. defm MEMORY_ATOMIC_WAIT32_A32 :
  46. ATOMIC_I<(outs I32:$dst),
  47. (ins P2Align:$p2align, offset32_op:$off, I32:$addr, I32:$exp,
  48. I64:$timeout),
  49. (outs), (ins P2Align:$p2align, offset32_op:$off), [],
  50. "memory.atomic.wait32 \t$dst, ${off}(${addr})${p2align}, $exp, $timeout",
  51. "memory.atomic.wait32 \t${off}${p2align}", 0x01, false>;
  52. defm MEMORY_ATOMIC_WAIT32_A64 :
  53. ATOMIC_I<(outs I32:$dst),
  54. (ins P2Align:$p2align, offset64_op:$off, I64:$addr, I32:$exp,
  55. I64:$timeout),
  56. (outs), (ins P2Align:$p2align, offset64_op:$off), [],
  57. "memory.atomic.wait32 \t$dst, ${off}(${addr})${p2align}, $exp, $timeout",
  58. "memory.atomic.wait32 \t${off}${p2align}", 0x01, true>;
  59. defm MEMORY_ATOMIC_WAIT64_A32 :
  60. ATOMIC_I<(outs I32:$dst),
  61. (ins P2Align:$p2align, offset32_op:$off, I32:$addr, I64:$exp,
  62. I64:$timeout),
  63. (outs), (ins P2Align:$p2align, offset32_op:$off), [],
  64. "memory.atomic.wait64 \t$dst, ${off}(${addr})${p2align}, $exp, $timeout",
  65. "memory.atomic.wait64 \t${off}${p2align}", 0x02, false>;
  66. defm MEMORY_ATOMIC_WAIT64_A64 :
  67. ATOMIC_I<(outs I32:$dst),
  68. (ins P2Align:$p2align, offset64_op:$off, I64:$addr, I64:$exp,
  69. I64:$timeout),
  70. (outs), (ins P2Align:$p2align, offset64_op:$off), [],
  71. "memory.atomic.wait64 \t$dst, ${off}(${addr})${p2align}, $exp, $timeout",
  72. "memory.atomic.wait64 \t${off}${p2align}", 0x02, true>;
  73. } // mayLoad = 1
  74. } // hasSideEffects = 1
  75. def NotifyPat_A32 :
  76. Pat<(i32 (int_wasm_memory_atomic_notify (AddrOps32 offset32_op:$offset, I32:$addr), I32:$count)),
  77. (MEMORY_ATOMIC_NOTIFY_A32 0, $offset, $addr, $count)>,
  78. Requires<[HasAddr32, HasAtomics]>;
  79. def NotifyPat_A64 :
  80. Pat<(i32 (int_wasm_memory_atomic_notify (AddrOps64 offset64_op:$offset, I64:$addr), I32:$count)),
  81. (MEMORY_ATOMIC_NOTIFY_A64 0, $offset, $addr, $count)>,
  82. Requires<[HasAddr64, HasAtomics]>;
  83. multiclass WaitPat<ValueType ty, Intrinsic kind, string inst> {
  84. def WaitPat_A32 :
  85. Pat<(i32 (kind (AddrOps32 offset32_op:$offset, I32:$addr), ty:$exp, I64:$timeout)),
  86. (!cast<NI>(inst#_A32) 0, $offset, $addr, $exp, $timeout)>,
  87. Requires<[HasAddr32, HasAtomics]>;
  88. def WaitPat_A64 :
  89. Pat<(i32 (kind (AddrOps64 offset64_op:$offset, I64:$addr), ty:$exp, I64:$timeout)),
  90. (!cast<NI>(inst#_A64) 0, $offset, $addr, $exp, $timeout)>,
  91. Requires<[HasAddr64, HasAtomics]>;
  92. }
  93. defm : WaitPat<i32, int_wasm_memory_atomic_wait32, "MEMORY_ATOMIC_WAIT32">;
  94. defm : WaitPat<i64, int_wasm_memory_atomic_wait64, "MEMORY_ATOMIC_WAIT64">;
  95. //===----------------------------------------------------------------------===//
  96. // Atomic fences
  97. //===----------------------------------------------------------------------===//
  98. // A compiler fence instruction that prevents reordering of instructions.
  99. let Defs = [ARGUMENTS] in {
  100. let isPseudo = 1, hasSideEffects = 1 in
  101. defm COMPILER_FENCE : ATOMIC_NRI<(outs), (ins), [], "compiler_fence">;
  102. let hasSideEffects = 1 in
  103. defm ATOMIC_FENCE : ATOMIC_NRI<(outs), (ins i8imm:$flags), [], "atomic.fence",
  104. 0x03>;
  105. } // Defs = [ARGUMENTS]
  106. //===----------------------------------------------------------------------===//
  107. // Atomic loads
  108. //===----------------------------------------------------------------------===//
  109. multiclass AtomicLoad<WebAssemblyRegClass rc, string name, int atomic_op> {
  110. defm "" : WebAssemblyLoad<rc, name, !or(0xfe00, !and(0xff, atomic_op)),
  111. [HasAtomics]>;
  112. }
  113. defm ATOMIC_LOAD_I32 : AtomicLoad<I32, "i32.atomic.load", 0x10>;
  114. defm ATOMIC_LOAD_I64 : AtomicLoad<I64, "i64.atomic.load", 0x11>;
  115. // Select loads
  116. defm : LoadPat<i32, atomic_load_32, "ATOMIC_LOAD_I32">;
  117. defm : LoadPat<i64, atomic_load_64, "ATOMIC_LOAD_I64">;
  118. // Extending loads. Note that there are only zero-extending atomic loads, no
  119. // sign-extending loads.
  120. defm ATOMIC_LOAD8_U_I32 : AtomicLoad<I32, "i32.atomic.load8_u", 0x12>;
  121. defm ATOMIC_LOAD16_U_I32 : AtomicLoad<I32, "i32.atomic.load16_u", 0x13>;
  122. defm ATOMIC_LOAD8_U_I64 : AtomicLoad<I64, "i64.atomic.load8_u", 0x14>;
  123. defm ATOMIC_LOAD16_U_I64 : AtomicLoad<I64, "i64.atomic.load16_u", 0x15>;
  124. defm ATOMIC_LOAD32_U_I64 : AtomicLoad<I64, "i64.atomic.load32_u", 0x16>;
  125. // Fragments for extending loads. These are different from regular loads because
  126. // the SDNodes are derived from AtomicSDNode rather than LoadSDNode and
  127. // therefore don't have the extension type field. So instead of matching that,
  128. // we match the patterns that the type legalizer expands them to.
  129. // Unlike regular loads, extension to i64 is handled differently than i32.
  130. // i64 (zext (i8 (atomic_load_8))) gets legalized to
  131. // i64 (and (i64 (anyext (i32 (atomic_load_8)))), 255)
  132. // Extension to i32 is elided by SelectionDAG as our atomic loads are
  133. // zero-extending.
  134. def zext_aload_8_64 :
  135. PatFrag<(ops node:$addr),
  136. (i64 (zext (i32 (atomic_load_8 node:$addr))))>;
  137. def zext_aload_16_64 :
  138. PatFrag<(ops node:$addr),
  139. (i64 (zext (i32 (atomic_load_16 node:$addr))))>;
  140. def zext_aload_32_64 :
  141. PatFrag<(ops node:$addr),
  142. (i64 (zext (i32 (atomic_load_32 node:$addr))))>;
  143. // We don't have single sext atomic load instructions. So for sext loads, we
  144. // match bare subword loads (for 32-bit results) and anyext loads (for 64-bit
  145. // results) and select a zext load; the next instruction will be sext_inreg
  146. // which is selected by itself.
  147. def sext_aload_8_64 :
  148. PatFrag<(ops node:$addr), (anyext (i32 (atomic_load_8 node:$addr)))>;
  149. def sext_aload_16_64 :
  150. PatFrag<(ops node:$addr), (anyext (i32 (atomic_load_16 node:$addr)))>;
  151. // Select zero-extending loads
  152. defm : LoadPat<i64, zext_aload_8_64, "ATOMIC_LOAD8_U_I64">;
  153. defm : LoadPat<i64, zext_aload_16_64, "ATOMIC_LOAD16_U_I64">;
  154. defm : LoadPat<i64, zext_aload_32_64, "ATOMIC_LOAD32_U_I64">;
  155. // Select sign-extending loads
  156. defm : LoadPat<i32, atomic_load_8, "ATOMIC_LOAD8_U_I32">;
  157. defm : LoadPat<i32, atomic_load_16, "ATOMIC_LOAD16_U_I32">;
  158. defm : LoadPat<i64, sext_aload_8_64, "ATOMIC_LOAD8_U_I64">;
  159. defm : LoadPat<i64, sext_aload_16_64, "ATOMIC_LOAD16_U_I64">;
  160. // 32->64 sext load gets selected as i32.atomic.load, i64.extend_i32_s
  161. //===----------------------------------------------------------------------===//
  162. // Atomic stores
  163. //===----------------------------------------------------------------------===//
  164. multiclass AtomicStore<WebAssemblyRegClass rc, string name, int atomic_op> {
  165. defm "" : WebAssemblyStore<rc, name, !or(0xfe00, !and(0xff, atomic_op)),
  166. [HasAtomics]>;
  167. }
  168. defm ATOMIC_STORE_I32 : AtomicStore<I32, "i32.atomic.store", 0x17>;
  169. defm ATOMIC_STORE_I64 : AtomicStore<I64, "i64.atomic.store", 0x18>;
  170. // We need an 'atomic' version of store patterns because store and atomic_store
  171. // nodes have different operand orders:
  172. // store: (store $val, $ptr)
  173. // atomic_store: (store $ptr, $val)
  174. multiclass AStorePat<ValueType ty, PatFrag kind, string inst> {
  175. def : Pat<(kind (AddrOps32 offset32_op:$offset, I32:$addr), ty:$val),
  176. (!cast<NI>(inst#_A32) 0, $offset, $addr, $val)>,
  177. Requires<[HasAddr32, HasAtomics]>;
  178. def : Pat<(kind (AddrOps64 offset64_op:$offset, I64:$addr), ty:$val),
  179. (!cast<NI>(inst#_A64) 0, $offset, $addr, $val)>,
  180. Requires<[HasAddr64, HasAtomics]>;
  181. }
  182. defm : AStorePat<i32, atomic_store_32, "ATOMIC_STORE_I32">;
  183. defm : AStorePat<i64, atomic_store_64, "ATOMIC_STORE_I64">;
  184. // Truncating stores.
  185. defm ATOMIC_STORE8_I32 : AtomicStore<I32, "i32.atomic.store8", 0x19>;
  186. defm ATOMIC_STORE16_I32 : AtomicStore<I32, "i32.atomic.store16", 0x1a>;
  187. defm ATOMIC_STORE8_I64 : AtomicStore<I64, "i64.atomic.store8", 0x1b>;
  188. defm ATOMIC_STORE16_I64 : AtomicStore<I64, "i64.atomic.store16", 0x1c>;
  189. defm ATOMIC_STORE32_I64 : AtomicStore<I64, "i64.atomic.store32", 0x1d>;
  190. // Fragments for truncating stores.
  191. // We don't have single truncating atomic store instructions. For 32-bit
  192. // instructions, we just need to match bare atomic stores. On the other hand,
  193. // truncating stores from i64 values are once truncated to i32 first.
  194. class trunc_astore_64<PatFrag kind> :
  195. PatFrag<(ops node:$addr, node:$val),
  196. (kind node:$addr, (i32 (trunc (i64 node:$val))))>;
  197. def trunc_astore_8_64 : trunc_astore_64<atomic_store_8>;
  198. def trunc_astore_16_64 : trunc_astore_64<atomic_store_16>;
  199. def trunc_astore_32_64 : trunc_astore_64<atomic_store_32>;
  200. // Truncating stores with no constant offset
  201. defm : AStorePat<i32, atomic_store_8, "ATOMIC_STORE8_I32">;
  202. defm : AStorePat<i32, atomic_store_16, "ATOMIC_STORE16_I32">;
  203. defm : AStorePat<i64, trunc_astore_8_64, "ATOMIC_STORE8_I64">;
  204. defm : AStorePat<i64, trunc_astore_16_64, "ATOMIC_STORE16_I64">;
  205. defm : AStorePat<i64, trunc_astore_32_64, "ATOMIC_STORE32_I64">;
  206. //===----------------------------------------------------------------------===//
  207. // Atomic binary read-modify-writes
  208. //===----------------------------------------------------------------------===//
  209. multiclass WebAssemblyBinRMW<WebAssemblyRegClass rc, string name,
  210. int atomic_op> {
  211. defm "_A32" :
  212. ATOMIC_I<(outs rc:$dst),
  213. (ins P2Align:$p2align, offset32_op:$off, I32:$addr, rc:$val),
  214. (outs), (ins P2Align:$p2align, offset32_op:$off), [],
  215. !strconcat(name, "\t$dst, ${off}(${addr})${p2align}, $val"),
  216. !strconcat(name, "\t${off}${p2align}"), atomic_op, false>;
  217. defm "_A64" :
  218. ATOMIC_I<(outs rc:$dst),
  219. (ins P2Align:$p2align, offset64_op:$off, I64:$addr, rc:$val),
  220. (outs), (ins P2Align:$p2align, offset64_op:$off), [],
  221. !strconcat(name, "\t$dst, ${off}(${addr})${p2align}, $val"),
  222. !strconcat(name, "\t${off}${p2align}"), atomic_op, true>;
  223. }
  224. defm ATOMIC_RMW_ADD_I32 : WebAssemblyBinRMW<I32, "i32.atomic.rmw.add", 0x1e>;
  225. defm ATOMIC_RMW_ADD_I64 : WebAssemblyBinRMW<I64, "i64.atomic.rmw.add", 0x1f>;
  226. defm ATOMIC_RMW8_U_ADD_I32 :
  227. WebAssemblyBinRMW<I32, "i32.atomic.rmw8.add_u", 0x20>;
  228. defm ATOMIC_RMW16_U_ADD_I32 :
  229. WebAssemblyBinRMW<I32, "i32.atomic.rmw16.add_u", 0x21>;
  230. defm ATOMIC_RMW8_U_ADD_I64 :
  231. WebAssemblyBinRMW<I64, "i64.atomic.rmw8.add_u", 0x22>;
  232. defm ATOMIC_RMW16_U_ADD_I64 :
  233. WebAssemblyBinRMW<I64, "i64.atomic.rmw16.add_u", 0x23>;
  234. defm ATOMIC_RMW32_U_ADD_I64 :
  235. WebAssemblyBinRMW<I64, "i64.atomic.rmw32.add_u", 0x24>;
  236. defm ATOMIC_RMW_SUB_I32 : WebAssemblyBinRMW<I32, "i32.atomic.rmw.sub", 0x25>;
  237. defm ATOMIC_RMW_SUB_I64 : WebAssemblyBinRMW<I64, "i64.atomic.rmw.sub", 0x26>;
  238. defm ATOMIC_RMW8_U_SUB_I32 :
  239. WebAssemblyBinRMW<I32, "i32.atomic.rmw8.sub_u", 0x27>;
  240. defm ATOMIC_RMW16_U_SUB_I32 :
  241. WebAssemblyBinRMW<I32, "i32.atomic.rmw16.sub_u", 0x28>;
  242. defm ATOMIC_RMW8_U_SUB_I64 :
  243. WebAssemblyBinRMW<I64, "i64.atomic.rmw8.sub_u", 0x29>;
  244. defm ATOMIC_RMW16_U_SUB_I64 :
  245. WebAssemblyBinRMW<I64, "i64.atomic.rmw16.sub_u", 0x2a>;
  246. defm ATOMIC_RMW32_U_SUB_I64 :
  247. WebAssemblyBinRMW<I64, "i64.atomic.rmw32.sub_u", 0x2b>;
  248. defm ATOMIC_RMW_AND_I32 : WebAssemblyBinRMW<I32, "i32.atomic.rmw.and", 0x2c>;
  249. defm ATOMIC_RMW_AND_I64 : WebAssemblyBinRMW<I64, "i64.atomic.rmw.and", 0x2d>;
  250. defm ATOMIC_RMW8_U_AND_I32 :
  251. WebAssemblyBinRMW<I32, "i32.atomic.rmw8.and_u", 0x2e>;
  252. defm ATOMIC_RMW16_U_AND_I32 :
  253. WebAssemblyBinRMW<I32, "i32.atomic.rmw16.and_u", 0x2f>;
  254. defm ATOMIC_RMW8_U_AND_I64 :
  255. WebAssemblyBinRMW<I64, "i64.atomic.rmw8.and_u", 0x30>;
  256. defm ATOMIC_RMW16_U_AND_I64 :
  257. WebAssemblyBinRMW<I64, "i64.atomic.rmw16.and_u", 0x31>;
  258. defm ATOMIC_RMW32_U_AND_I64 :
  259. WebAssemblyBinRMW<I64, "i64.atomic.rmw32.and_u", 0x32>;
  260. defm ATOMIC_RMW_OR_I32 : WebAssemblyBinRMW<I32, "i32.atomic.rmw.or", 0x33>;
  261. defm ATOMIC_RMW_OR_I64 : WebAssemblyBinRMW<I64, "i64.atomic.rmw.or", 0x34>;
  262. defm ATOMIC_RMW8_U_OR_I32 :
  263. WebAssemblyBinRMW<I32, "i32.atomic.rmw8.or_u", 0x35>;
  264. defm ATOMIC_RMW16_U_OR_I32 :
  265. WebAssemblyBinRMW<I32, "i32.atomic.rmw16.or_u", 0x36>;
  266. defm ATOMIC_RMW8_U_OR_I64 :
  267. WebAssemblyBinRMW<I64, "i64.atomic.rmw8.or_u", 0x37>;
  268. defm ATOMIC_RMW16_U_OR_I64 :
  269. WebAssemblyBinRMW<I64, "i64.atomic.rmw16.or_u", 0x38>;
  270. defm ATOMIC_RMW32_U_OR_I64 :
  271. WebAssemblyBinRMW<I64, "i64.atomic.rmw32.or_u", 0x39>;
  272. defm ATOMIC_RMW_XOR_I32 : WebAssemblyBinRMW<I32, "i32.atomic.rmw.xor", 0x3a>;
  273. defm ATOMIC_RMW_XOR_I64 : WebAssemblyBinRMW<I64, "i64.atomic.rmw.xor", 0x3b>;
  274. defm ATOMIC_RMW8_U_XOR_I32 :
  275. WebAssemblyBinRMW<I32, "i32.atomic.rmw8.xor_u", 0x3c>;
  276. defm ATOMIC_RMW16_U_XOR_I32 :
  277. WebAssemblyBinRMW<I32, "i32.atomic.rmw16.xor_u", 0x3d>;
  278. defm ATOMIC_RMW8_U_XOR_I64 :
  279. WebAssemblyBinRMW<I64, "i64.atomic.rmw8.xor_u", 0x3e>;
  280. defm ATOMIC_RMW16_U_XOR_I64 :
  281. WebAssemblyBinRMW<I64, "i64.atomic.rmw16.xor_u", 0x3f>;
  282. defm ATOMIC_RMW32_U_XOR_I64 :
  283. WebAssemblyBinRMW<I64, "i64.atomic.rmw32.xor_u", 0x40>;
  284. defm ATOMIC_RMW_XCHG_I32 :
  285. WebAssemblyBinRMW<I32, "i32.atomic.rmw.xchg", 0x41>;
  286. defm ATOMIC_RMW_XCHG_I64 :
  287. WebAssemblyBinRMW<I64, "i64.atomic.rmw.xchg", 0x42>;
  288. defm ATOMIC_RMW8_U_XCHG_I32 :
  289. WebAssemblyBinRMW<I32, "i32.atomic.rmw8.xchg_u", 0x43>;
  290. defm ATOMIC_RMW16_U_XCHG_I32 :
  291. WebAssemblyBinRMW<I32, "i32.atomic.rmw16.xchg_u", 0x44>;
  292. defm ATOMIC_RMW8_U_XCHG_I64 :
  293. WebAssemblyBinRMW<I64, "i64.atomic.rmw8.xchg_u", 0x45>;
  294. defm ATOMIC_RMW16_U_XCHG_I64 :
  295. WebAssemblyBinRMW<I64, "i64.atomic.rmw16.xchg_u", 0x46>;
  296. defm ATOMIC_RMW32_U_XCHG_I64 :
  297. WebAssemblyBinRMW<I64, "i64.atomic.rmw32.xchg_u", 0x47>;
  298. multiclass BinRMWPat<ValueType ty, PatFrag kind, string inst> {
  299. def : Pat<(ty (kind (AddrOps32 offset32_op:$offset, I32:$addr), ty:$val)),
  300. (!cast<NI>(inst#_A32) 0, $offset, $addr, $val)>,
  301. Requires<[HasAddr32, HasAtomics]>;
  302. def : Pat<(ty (kind (AddrOps64 offset64_op:$offset, I64:$addr), ty:$val)),
  303. (!cast<NI>(inst#_A64) 0, $offset, $addr, $val)>,
  304. Requires<[HasAddr64, HasAtomics]>;
  305. }
  306. // Patterns for various addressing modes.
  307. multiclass BinRMWPattern<PatFrag rmw_32, PatFrag rmw_64, string inst_32,
  308. string inst_64> {
  309. defm : BinRMWPat<i32, rmw_32, inst_32>;
  310. defm : BinRMWPat<i64, rmw_64, inst_64>;
  311. }
  312. defm : BinRMWPattern<atomic_load_add_32, atomic_load_add_64,
  313. "ATOMIC_RMW_ADD_I32", "ATOMIC_RMW_ADD_I64">;
  314. defm : BinRMWPattern<atomic_load_sub_32, atomic_load_sub_64,
  315. "ATOMIC_RMW_SUB_I32", "ATOMIC_RMW_SUB_I64">;
  316. defm : BinRMWPattern<atomic_load_and_32, atomic_load_and_64,
  317. "ATOMIC_RMW_AND_I32", "ATOMIC_RMW_AND_I64">;
  318. defm : BinRMWPattern<atomic_load_or_32, atomic_load_or_64,
  319. "ATOMIC_RMW_OR_I32", "ATOMIC_RMW_OR_I64">;
  320. defm : BinRMWPattern<atomic_load_xor_32, atomic_load_xor_64,
  321. "ATOMIC_RMW_XOR_I32", "ATOMIC_RMW_XOR_I64">;
  322. defm : BinRMWPattern<atomic_swap_32, atomic_swap_64,
  323. "ATOMIC_RMW_XCHG_I32", "ATOMIC_RMW_XCHG_I64">;
  324. // Truncating & zero-extending binary RMW patterns.
  325. // These are combined patterns of truncating store patterns and zero-extending
  326. // load patterns above.
  327. class zext_bin_rmw_8_32<PatFrag kind> :
  328. PatFrag<(ops node:$addr, node:$val), (i32 (kind node:$addr, node:$val))>;
  329. class zext_bin_rmw_16_32<PatFrag kind> : zext_bin_rmw_8_32<kind>;
  330. class zext_bin_rmw_8_64<PatFrag kind> :
  331. PatFrag<(ops node:$addr, node:$val),
  332. (zext (i32 (kind node:$addr, (i32 (trunc (i64 node:$val))))))>;
  333. class zext_bin_rmw_16_64<PatFrag kind> : zext_bin_rmw_8_64<kind>;
  334. class zext_bin_rmw_32_64<PatFrag kind> : zext_bin_rmw_8_64<kind>;
  335. // Truncating & sign-extending binary RMW patterns.
  336. // These are combined patterns of truncating store patterns and sign-extending
  337. // load patterns above. We match subword RMWs (for 32-bit) and anyext RMWs (for
  338. // 64-bit) and select a zext RMW; the next instruction will be sext_inreg which
  339. // is selected by itself.
  340. class sext_bin_rmw_8_32<PatFrag kind> :
  341. PatFrag<(ops node:$addr, node:$val), (kind node:$addr, node:$val)>;
  342. class sext_bin_rmw_16_32<PatFrag kind> : sext_bin_rmw_8_32<kind>;
  343. class sext_bin_rmw_8_64<PatFrag kind> :
  344. PatFrag<(ops node:$addr, node:$val),
  345. (anyext (i32 (kind node:$addr, (i32 (trunc (i64 node:$val))))))>;
  346. class sext_bin_rmw_16_64<PatFrag kind> : sext_bin_rmw_8_64<kind>;
  347. // 32->64 sext RMW gets selected as i32.atomic.rmw.***, i64.extend_i32_s
  348. // Patterns for various addressing modes for truncating-extending binary RMWs.
  349. multiclass BinRMWTruncExtPattern<
  350. PatFrag rmw_8, PatFrag rmw_16, PatFrag rmw_32,
  351. string inst8_32, string inst16_32, string inst8_64, string inst16_64, string inst32_64> {
  352. // Truncating-extending binary RMWs
  353. defm : BinRMWPat<i32, zext_bin_rmw_8_32<rmw_8>, inst8_32>;
  354. defm : BinRMWPat<i32, zext_bin_rmw_16_32<rmw_16>, inst16_32>;
  355. defm : BinRMWPat<i64, zext_bin_rmw_8_64<rmw_8>, inst8_64>;
  356. defm : BinRMWPat<i64, zext_bin_rmw_16_64<rmw_16>, inst16_64>;
  357. defm : BinRMWPat<i64, zext_bin_rmw_32_64<rmw_32>, inst32_64>;
  358. defm : BinRMWPat<i32, sext_bin_rmw_8_32<rmw_8>, inst8_32>;
  359. defm : BinRMWPat<i32, sext_bin_rmw_16_32<rmw_16>, inst16_32>;
  360. defm : BinRMWPat<i64, sext_bin_rmw_8_64<rmw_8>, inst8_64>;
  361. defm : BinRMWPat<i64, sext_bin_rmw_16_64<rmw_16>, inst16_64>;
  362. }
  363. defm : BinRMWTruncExtPattern<
  364. atomic_load_add_8, atomic_load_add_16, atomic_load_add_32,
  365. "ATOMIC_RMW8_U_ADD_I32", "ATOMIC_RMW16_U_ADD_I32",
  366. "ATOMIC_RMW8_U_ADD_I64", "ATOMIC_RMW16_U_ADD_I64", "ATOMIC_RMW32_U_ADD_I64">;
  367. defm : BinRMWTruncExtPattern<
  368. atomic_load_sub_8, atomic_load_sub_16, atomic_load_sub_32,
  369. "ATOMIC_RMW8_U_SUB_I32", "ATOMIC_RMW16_U_SUB_I32",
  370. "ATOMIC_RMW8_U_SUB_I64", "ATOMIC_RMW16_U_SUB_I64", "ATOMIC_RMW32_U_SUB_I64">;
  371. defm : BinRMWTruncExtPattern<
  372. atomic_load_and_8, atomic_load_and_16, atomic_load_and_32,
  373. "ATOMIC_RMW8_U_AND_I32", "ATOMIC_RMW16_U_AND_I32",
  374. "ATOMIC_RMW8_U_AND_I64", "ATOMIC_RMW16_U_AND_I64", "ATOMIC_RMW32_U_AND_I64">;
  375. defm : BinRMWTruncExtPattern<
  376. atomic_load_or_8, atomic_load_or_16, atomic_load_or_32,
  377. "ATOMIC_RMW8_U_OR_I32", "ATOMIC_RMW16_U_OR_I32",
  378. "ATOMIC_RMW8_U_OR_I64", "ATOMIC_RMW16_U_OR_I64", "ATOMIC_RMW32_U_OR_I64">;
  379. defm : BinRMWTruncExtPattern<
  380. atomic_load_xor_8, atomic_load_xor_16, atomic_load_xor_32,
  381. "ATOMIC_RMW8_U_XOR_I32", "ATOMIC_RMW16_U_XOR_I32",
  382. "ATOMIC_RMW8_U_XOR_I64", "ATOMIC_RMW16_U_XOR_I64", "ATOMIC_RMW32_U_XOR_I64">;
  383. defm : BinRMWTruncExtPattern<
  384. atomic_swap_8, atomic_swap_16, atomic_swap_32,
  385. "ATOMIC_RMW8_U_XCHG_I32", "ATOMIC_RMW16_U_XCHG_I32",
  386. "ATOMIC_RMW8_U_XCHG_I64", "ATOMIC_RMW16_U_XCHG_I64",
  387. "ATOMIC_RMW32_U_XCHG_I64">;
  388. //===----------------------------------------------------------------------===//
  389. // Atomic ternary read-modify-writes
  390. //===----------------------------------------------------------------------===//
  391. // TODO LLVM IR's cmpxchg instruction returns a pair of {loaded value, success
  392. // flag}. When we use the success flag or both values, we can't make use of i64
  393. // truncate/extend versions of instructions for now, which is suboptimal.
  394. // Consider adding a pass after instruction selection that optimizes this case
  395. // if it is frequent.
  396. multiclass WebAssemblyTerRMW<WebAssemblyRegClass rc, string name,
  397. int atomic_op> {
  398. defm "_A32" :
  399. ATOMIC_I<(outs rc:$dst),
  400. (ins P2Align:$p2align, offset32_op:$off, I32:$addr, rc:$exp,
  401. rc:$new_),
  402. (outs), (ins P2Align:$p2align, offset32_op:$off), [],
  403. !strconcat(name, "\t$dst, ${off}(${addr})${p2align}, $exp, $new_"),
  404. !strconcat(name, "\t${off}${p2align}"), atomic_op, false>;
  405. defm "_A64" :
  406. ATOMIC_I<(outs rc:$dst),
  407. (ins P2Align:$p2align, offset64_op:$off, I64:$addr, rc:$exp,
  408. rc:$new_),
  409. (outs), (ins P2Align:$p2align, offset64_op:$off), [],
  410. !strconcat(name, "\t$dst, ${off}(${addr})${p2align}, $exp, $new_"),
  411. !strconcat(name, "\t${off}${p2align}"), atomic_op, true>;
  412. }
  413. defm ATOMIC_RMW_CMPXCHG_I32 :
  414. WebAssemblyTerRMW<I32, "i32.atomic.rmw.cmpxchg", 0x48>;
  415. defm ATOMIC_RMW_CMPXCHG_I64 :
  416. WebAssemblyTerRMW<I64, "i64.atomic.rmw.cmpxchg", 0x49>;
  417. defm ATOMIC_RMW8_U_CMPXCHG_I32 :
  418. WebAssemblyTerRMW<I32, "i32.atomic.rmw8.cmpxchg_u", 0x4a>;
  419. defm ATOMIC_RMW16_U_CMPXCHG_I32 :
  420. WebAssemblyTerRMW<I32, "i32.atomic.rmw16.cmpxchg_u", 0x4b>;
  421. defm ATOMIC_RMW8_U_CMPXCHG_I64 :
  422. WebAssemblyTerRMW<I64, "i64.atomic.rmw8.cmpxchg_u", 0x4c>;
  423. defm ATOMIC_RMW16_U_CMPXCHG_I64 :
  424. WebAssemblyTerRMW<I64, "i64.atomic.rmw16.cmpxchg_u", 0x4d>;
  425. defm ATOMIC_RMW32_U_CMPXCHG_I64 :
  426. WebAssemblyTerRMW<I64, "i64.atomic.rmw32.cmpxchg_u", 0x4e>;
  427. multiclass TerRMWPat<ValueType ty, PatFrag kind, string inst> {
  428. def : Pat<(ty (kind (AddrOps32 offset32_op:$offset, I32:$addr), ty:$exp, ty:$new)),
  429. (!cast<NI>(inst#_A32) 0, $offset, $addr, $exp, $new)>,
  430. Requires<[HasAddr32, HasAtomics]>;
  431. def : Pat<(ty (kind (AddrOps64 offset64_op:$offset, I64:$addr), ty:$exp, ty:$new)),
  432. (!cast<NI>(inst#_A64) 0, $offset, $addr, $exp, $new)>,
  433. Requires<[HasAddr64, HasAtomics]>;
  434. }
  435. defm : TerRMWPat<i32, atomic_cmp_swap_32, "ATOMIC_RMW_CMPXCHG_I32">;
  436. defm : TerRMWPat<i64, atomic_cmp_swap_64, "ATOMIC_RMW_CMPXCHG_I64">;
  437. // Truncating & zero-extending ternary RMW patterns.
  438. // DAG legalization & optimization before instruction selection may introduce
  439. // additional nodes such as anyext or assertzext depending on operand types.
  440. class zext_ter_rmw_8_32<PatFrag kind> :
  441. PatFrag<(ops node:$addr, node:$exp, node:$new),
  442. (i32 (kind node:$addr, node:$exp, node:$new))>;
  443. class zext_ter_rmw_16_32<PatFrag kind> : zext_ter_rmw_8_32<kind>;
  444. class zext_ter_rmw_8_64<PatFrag kind> :
  445. PatFrag<(ops node:$addr, node:$exp, node:$new),
  446. (zext (i32 (assertzext (i32 (kind node:$addr,
  447. (i32 (trunc (i64 node:$exp))),
  448. (i32 (trunc (i64 node:$new))))))))>;
  449. class zext_ter_rmw_16_64<PatFrag kind> : zext_ter_rmw_8_64<kind>;
  450. class zext_ter_rmw_32_64<PatFrag kind> :
  451. PatFrag<(ops node:$addr, node:$exp, node:$new),
  452. (zext (i32 (kind node:$addr,
  453. (i32 (trunc (i64 node:$exp))),
  454. (i32 (trunc (i64 node:$new))))))>;
  455. // Truncating & sign-extending ternary RMW patterns.
  456. // We match subword RMWs (for 32-bit) and anyext RMWs (for 64-bit) and select a
  457. // zext RMW; the next instruction will be sext_inreg which is selected by
  458. // itself.
  459. class sext_ter_rmw_8_32<PatFrag kind> :
  460. PatFrag<(ops node:$addr, node:$exp, node:$new),
  461. (kind node:$addr, node:$exp, node:$new)>;
  462. class sext_ter_rmw_16_32<PatFrag kind> : sext_ter_rmw_8_32<kind>;
  463. class sext_ter_rmw_8_64<PatFrag kind> :
  464. PatFrag<(ops node:$addr, node:$exp, node:$new),
  465. (anyext (i32 (assertzext (i32
  466. (kind node:$addr,
  467. (i32 (trunc (i64 node:$exp))),
  468. (i32 (trunc (i64 node:$new))))))))>;
  469. class sext_ter_rmw_16_64<PatFrag kind> : sext_ter_rmw_8_64<kind>;
  470. // 32->64 sext RMW gets selected as i32.atomic.rmw.***, i64.extend_i32_s
  471. defm : TerRMWPat<i32, zext_ter_rmw_8_32<atomic_cmp_swap_8>, "ATOMIC_RMW8_U_CMPXCHG_I32">;
  472. defm : TerRMWPat<i32, zext_ter_rmw_16_32<atomic_cmp_swap_16>, "ATOMIC_RMW16_U_CMPXCHG_I32">;
  473. defm : TerRMWPat<i64, zext_ter_rmw_8_64<atomic_cmp_swap_8>, "ATOMIC_RMW8_U_CMPXCHG_I64">;
  474. defm : TerRMWPat<i64, zext_ter_rmw_16_64<atomic_cmp_swap_16>, "ATOMIC_RMW16_U_CMPXCHG_I64">;
  475. defm : TerRMWPat<i64, zext_ter_rmw_32_64<atomic_cmp_swap_32>, "ATOMIC_RMW32_U_CMPXCHG_I64">;
  476. defm : TerRMWPat<i32, sext_ter_rmw_8_32<atomic_cmp_swap_8>, "ATOMIC_RMW8_U_CMPXCHG_I32">;
  477. defm : TerRMWPat<i32, sext_ter_rmw_16_32<atomic_cmp_swap_16>, "ATOMIC_RMW16_U_CMPXCHG_I32">;
  478. defm : TerRMWPat<i64, sext_ter_rmw_8_64<atomic_cmp_swap_8>, "ATOMIC_RMW8_U_CMPXCHG_I64">;
  479. defm : TerRMWPat<i64, sext_ter_rmw_16_64<atomic_cmp_swap_16>, "ATOMIC_RMW16_U_CMPXCHG_I64">;