xref: /llvm-project/llvm/lib/Target/WebAssembly/WebAssemblyInstrBulkMemory.td (revision c3536b263f253a69fb336fb0617ee33a01a5c5dd)
1// WebAssemblyInstrBulkMemory.td - bulk 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 bulk memory codegen constructs.
11///
12//===----------------------------------------------------------------------===//
13
14// Instruction requiring HasBulkMemoryOpt and the bulk memory prefix byte
15multiclass 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]>;
21}
22
23// Bespoke types and nodes for bulk memory ops
24
25def wasm_memcpy_t : SDTypeProfile<0, 5,
26  [SDTCisInt<0>, SDTCisInt<1>, SDTCisPtrTy<2>, SDTCisPtrTy<3>, SDTCisInt<4>]
27>;
28def wasm_memset_t : SDTypeProfile<0, 4,
29  [SDTCisInt<0>, SDTCisPtrTy<1>, SDTCisInt<2>, SDTCisInt<3>]
30>;
31
32// memory.copy with a branch to avoid trapping in the case of out-of-bounds
33// pointers with empty ranges.
34def wasm_memcpy : SDNode<"WebAssemblyISD::MEMCPY", wasm_memcpy_t,
35                         [SDNPHasChain, SDNPMayLoad, SDNPMayStore]>;
36
37// memory.fill with a branch to avoid trapping in the case of out-of-bounds
38// pointers with empty ranges.
39def wasm_memset : SDNode<"WebAssemblyISD::MEMSET", wasm_memset_t,
40                         [SDNPHasChain, SDNPMayStore]>;
41
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.
45multiclass BulkMemoryOps<WebAssemblyRegClass rc, string B> {
46
47let mayStore = 1, hasSideEffects = 1 in
48defm INIT_A#B :
49  BULK_I<(outs),
50         (ins i32imm_op:$seg, i32imm_op:$idx, rc:$dest,
51              I32:$offset, I32:$size),
52         (outs), (ins i32imm_op:$seg, i32imm_op:$idx),
53         [],
54         "memory.init\t$seg, $idx, $dest, $offset, $size",
55         "memory.init\t$seg, $idx", 0x08>;
56
57let mayLoad = 1, mayStore = 1 in
58defm COPY_A#B :
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),
62         [],
63         "memory.copy\t$src_idx, $dst_idx, $dst, $src, $len",
64         "memory.copy\t$src_idx, $dst_idx", 0x0a>;
65
66let mayStore = 1 in
67defm FILL_A#B :
68  BULK_I<(outs), (ins i32imm_op:$idx, rc:$dst, I32:$value, rc:$size),
69         (outs), (ins i32imm_op:$idx),
70         [],
71         "memory.fill\t$idx, $dst, $value, $size",
72         "memory.fill\t$idx", 0x0b>;
73}
74
75defm MEMORY_ : BulkMemoryOps<I32, "32">;
76defm MEMORY_ : BulkMemoryOps<I64, "64">;
77
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.
82multiclass BulkMemOps<WebAssemblyRegClass rc, string B> {
83
84let usesCustomInserter = 1, isCodeGenOnly = 1, mayLoad = 1, mayStore = 1 in
85defm 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
90                 )],
91                 "", "", 0>,
92                  Requires<[HasBulkMemoryOpt]>;
93
94let usesCustomInserter = 1, isCodeGenOnly = 1, mayStore = 1 in
95defm 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)],
98                 "", "", 0>,
99                 Requires<[HasBulkMemoryOpt]>;
100
101}
102
103defm MEM : BulkMemOps<I32, "32">;
104defm MEM : BulkMemOps<I64, "64">;
105
106let hasSideEffects = 1 in
107defm DATA_DROP :
108  BULK_I<(outs), (ins i32imm_op:$seg), (outs), (ins i32imm_op:$seg),
109         [],
110         "data.drop\t$seg", "data.drop\t$seg", 0x09>;
111