summaryrefslogtreecommitdiff
path: root/deps/v8/test/mjsunit/wasm/type-based-optimizations.js
blob: 5ac4debefb11802614daf7d205bf43830d3627ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright 2022 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Flags: --experimental-wasm-gc --no-liftoff

d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");

// Test that we can eliminate type checks based on narrowed argument types
// (by inspecting the resulting graph).
(function WasmTypedOptimizationsTest() {
  let builder = new WasmModuleBuilder();
  let top = builder.addStruct([makeField(kWasmI32, true)]);
  let middle = builder.addStruct([makeField(kWasmI32, true),
                                  makeField(kWasmI64, false)],
                                 top);
  let bottom1 = builder.addStruct([makeField(kWasmI32, true),
                                   makeField(kWasmI64, false),
                                   makeField(kWasmI32, true)],
                                  middle);
  let bottom2 = builder.addStruct([makeField(kWasmI32, true),
                                   makeField(kWasmI64, false),
                                   makeField(kWasmI64, false)],
                                  middle);

  builder.addFunction("main", makeSig(
        [wasmRefType(bottom1), wasmRefType(bottom2)], [kWasmI32]))
    .addLocals(wasmRefNullType(top), 1)
    .addLocals(kWasmI32, 1)
    .addBody([
        // temp = x0;
        kExprLocalGet, 0, kExprLocalSet, 2,
        // while (true) {
        kExprLoop, kWasmVoid,
          // if (ref.test temp bottom1) {
          kExprLocalGet, 2, kGCPrefix, kExprRefTestDeprecated, bottom1,
          kExprIf, kWasmVoid,
            // counter += ((bottom1) temp).field_2;
            // Note: This cast should get optimized away with path-based type
            // tracking.
            kExprLocalGet, 2, kGCPrefix, kExprRefCast, bottom1,
            kGCPrefix, kExprStructGet, bottom1, 2,
            kExprLocalGet, 3, kExprI32Add, kExprLocalSet, 3,
            // temp = x1;
            kExprLocalGet, 1, kExprLocalSet, 2,
          // } else {
          kExprElse,
            // counter += (i32) ((middle) temp).field_1
            // Note: This cast should get optimized away, as temp only gets
            // assigned to {bottom1} and {bottom2}.
            kExprLocalGet, 2, kGCPrefix, kExprRefCast, middle,
            kGCPrefix, kExprStructGet, middle, 1, kExprI32ConvertI64,
            kExprLocalGet, 3, kExprI32Add, kExprLocalSet, 3,
            // temp = x0;
            kExprLocalGet, 0, kExprLocalSet, 2,
          // }
          kExprEnd,
          // if (counter < 100) continue; break;
          kExprLocalGet, 3, kExprI32Const, 100, kExprI32LtS,
          kExprBrIf, 0,
        // }
        kExprEnd,
        // return counter;
        kExprLocalGet, 3])
    .exportFunc();

  builder.instantiate({});
})();