1 // WebAssemblyInstrBulkMemory.td - bulk 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 bulk memory codegen constructs.
12 //===----------------------------------------------------------------------===//
14 // Instruction requiring HasBulkMemoryOpt and the bulk memory prefix byte
15 multiclass BULK_I<dag oops_r, dag iops_r, dag oops_s, dag iops_s,
16 list<dag> pattern_r, string asmstr_r = "",
17 string asmstr_s = "", bits<32> simdop = -1> {
18 defm "" : I<oops_r, iops_r, oops_s, iops_s, pattern_r, asmstr_r, asmstr_s,
19 !or(0xfc00, !and(0xff, simdop))>,
20 Requires<[HasBulkMemoryOpt]>;
23 // Bespoke types and nodes for bulk memory ops
25 def wasm_memcpy_t : SDTypeProfile<0, 5,
26 [SDTCisInt<0>, SDTCisInt<1>, SDTCisPtrTy<2>, SDTCisPtrTy<3>, SDTCisInt<4>]
28 def wasm_memset_t : SDTypeProfile<0, 4,
29 [SDTCisInt<0>, SDTCisPtrTy<1>, SDTCisInt<2>, SDTCisInt<3>]
32 // memory.copy with a branch to avoid trapping in the case of out-of-bounds
33 // pointers with empty ranges.
34 def wasm_memcpy : SDNode<"WebAssemblyISD::MEMCPY", wasm_memcpy_t,
35 [SDNPHasChain, SDNPMayLoad, SDNPMayStore]>;
37 // memory.fill with a branch to avoid trapping in the case of out-of-bounds
38 // pointers with empty ranges.
39 def wasm_memset : SDNode<"WebAssemblyISD::MEMSET", wasm_memset_t,
40 [SDNPHasChain, SDNPMayStore]>;
42 // A multiclass for defining Wasm's raw bulk-memory `memory.*` instructions.
43 // `memory.copy` and `memory.fill` have Wasm's behavior rather than
44 // `memcpy`/`memset` behavior.
45 multiclass BulkMemoryOps<WebAssemblyRegClass rc, string B> {
47 let mayStore = 1, hasSideEffects = 1 in
50 (ins i32imm_op:$seg, i32imm_op:$idx, rc:$dest,
51 I32:$offset, I32:$size),
52 (outs), (ins i32imm_op:$seg, i32imm_op:$idx),
54 "memory.init\t$seg, $idx, $dest, $offset, $size",
55 "memory.init\t$seg, $idx", 0x08>;
57 let mayLoad = 1, mayStore = 1 in
59 BULK_I<(outs), (ins i32imm_op:$src_idx, i32imm_op:$dst_idx,
60 rc:$dst, rc:$src, rc:$len),
61 (outs), (ins i32imm_op:$src_idx, i32imm_op:$dst_idx),
63 "memory.copy\t$src_idx, $dst_idx, $dst, $src, $len",
64 "memory.copy\t$src_idx, $dst_idx", 0x0a>;
68 BULK_I<(outs), (ins i32imm_op:$idx, rc:$dst, I32:$value, rc:$size),
69 (outs), (ins i32imm_op:$idx),
71 "memory.fill\t$idx, $dst, $value, $size",
72 "memory.fill\t$idx", 0x0b>;
75 defm MEMORY_ : BulkMemoryOps<I32, "32">;
76 defm MEMORY_ : BulkMemoryOps<I64, "64">;
78 // A multiclass for defining `memcpy`/`memset` pseudo instructions. These have
79 // the behavior the rest of LLVM CodeGen expects, and we lower them into code
80 // sequences that include the Wasm `memory.fill` and `memory.copy` instructions
81 // using custom inserters, because they introduce new control flow.
82 multiclass BulkMemOps<WebAssemblyRegClass rc, string B> {
84 let usesCustomInserter = 1, isCodeGenOnly = 1, mayLoad = 1, mayStore = 1 in
85 defm CPY_A#B : I<(outs), (ins i32imm_op:$src_idx, i32imm_op:$dst_idx,
86 rc:$dst, rc:$src, rc:$len),
87 (outs), (ins i32imm_op:$src_idx, i32imm_op:$dst_idx),
88 [(wasm_memcpy (i32 imm:$src_idx), (i32 imm:$dst_idx),
89 rc:$dst, rc:$src, rc:$len
92 Requires<[HasBulkMemoryOpt]>;
94 let usesCustomInserter = 1, isCodeGenOnly = 1, mayStore = 1 in
95 defm SET_A#B : I<(outs), (ins i32imm_op:$idx, rc:$dst, I32:$value, rc:$size),
96 (outs), (ins i32imm_op:$idx),
97 [(wasm_memset (i32 imm:$idx), rc:$dst, I32:$value, rc:$size)],
99 Requires<[HasBulkMemoryOpt]>;
103 defm MEM : BulkMemOps<I32, "32">;
104 defm MEM : BulkMemOps<I64, "64">;
106 let hasSideEffects = 1 in
108 BULK_I<(outs), (ins i32imm_op:$seg), (outs), (ins i32imm_op:$seg),
110 "data.drop\t$seg", "data.drop\t$seg", 0x09>;