summaryrefslogtreecommitdiff
path: root/test/lit/passes/vacuum-gc.wast
blob: fcdeb8cd208cae796aef703b7d21c690598e2218 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited.
;; RUN: wasm-opt %s --vacuum -all -S -o - | filecheck %s

(module
  ;; CHECK:      (type ${} (struct ))

  ;; CHECK:      (import "binaryen-intrinsics" "call.without.effects" (func $call.without.effects (type $i32_i32_funcref_=>_anyref) (param i32 i32 funcref) (result anyref)))
  (import "binaryen-intrinsics" "call.without.effects" (func $call.without.effects (param i32 i32 funcref) (result (ref null any))))
  ;; CHECK:      (import "binaryen-intrinsics" "call.without.effects" (func $call.without.effects.non.null (type $i32_i32_funcref_=>_ref|any|) (param i32 i32 funcref) (result (ref any))))
  (import "binaryen-intrinsics" "call.without.effects" (func $call.without.effects.non.null (param i32 i32 funcref) (result (ref any))))

  (type ${} (struct))

  ;; CHECK:      (func $drop-ref-as (type $anyref_=>_none) (param $x anyref)
  ;; CHECK-NEXT:  (drop
  ;; CHECK-NEXT:   (ref.as_non_null
  ;; CHECK-NEXT:    (local.get $x)
  ;; CHECK-NEXT:   )
  ;; CHECK-NEXT:  )
  ;; CHECK-NEXT:  (drop
  ;; CHECK-NEXT:   (ref.cast null i31
  ;; CHECK-NEXT:    (local.get $x)
  ;; CHECK-NEXT:   )
  ;; CHECK-NEXT:  )
  ;; CHECK-NEXT: )
  (func $drop-ref-as (param $x anyref)
    ;; Without -tnh, we must assume all casts can have a trap effect, and so
    ;; we cannot remove anything here.
    (drop
      (ref.as_non_null
        (local.get $x)
      )
    )
    (drop
      (ref.cast null i31
        (local.get $x)
      )
    )
  )

  ;; CHECK:      (func $vacuum-nonnull (type $none_=>_none)
  ;; CHECK-NEXT:  (nop)
  ;; CHECK-NEXT: )
  (func $vacuum-nonnull
    (drop
      (if (result (ref ${}))
        (i32.const 1)
        ;; This block's result is not used. As a consequence vacuum will try to
        ;; generate a replacement zero for the block's fallthrough value. A
        ;; non-nullable reference is a problem for that, since we don't want to
        ;; synthesize and allocate a new struct value.  Vacuum should not error
        ;; on this case, though. Instead, the end result of this function should
        ;; simply be empty, as everything here can be vacuumed away.
        (block (result (ref ${}))
          (struct.new ${})
        )
        (unreachable)
      )
    )
  )

  ;; CHECK:      (func $drop-i31.get (type $i31ref_ref|i31|_=>_none) (param $ref i31ref) (param $ref-nn (ref i31))
  ;; CHECK-NEXT:  (drop
  ;; CHECK-NEXT:   (i31.get_s
  ;; CHECK-NEXT:    (local.get $ref)
  ;; CHECK-NEXT:   )
  ;; CHECK-NEXT:  )
  ;; CHECK-NEXT: )
  (func $drop-i31.get (param $ref i31ref) (param $ref-nn (ref i31))
    ;; A nullable get might trap, so only the second item can be removed.
    (drop
      (i31.get_s
        (local.get $ref)
      )
    )
    (drop
      (i31.get_s
        (local.get $ref-nn)
      )
    )
  )

  ;; CHECK:      (func $ref.cast.null.block (type $ref|${}|_=>_structref) (param $ref (ref ${})) (result structref)
  ;; CHECK-NEXT:  (ref.cast ${}
  ;; CHECK-NEXT:   (local.get $ref)
  ;; CHECK-NEXT:  )
  ;; CHECK-NEXT: )
  (func $ref.cast.null.block (param $ref (ref ${})) (result (ref null struct))
    ;; We can vacuum away the block, which will make this ref.cast null operate
    ;; on a non-nullable input. That is, we are refining the input to the cast.
    ;; The cast must be updated properly following that, to be a non-nullable
    ;; cast.
    (ref.cast null ${}
      (block (result (ref null ${}))
        (local.get $ref)
      )
    )
  )

  ;; CHECK:      (func $dropped-calls (type $none_=>_none)
  ;; CHECK-NEXT:  (block
  ;; CHECK-NEXT:   (drop
  ;; CHECK-NEXT:    (call $helper-i32)
  ;; CHECK-NEXT:   )
  ;; CHECK-NEXT:   (drop
  ;; CHECK-NEXT:    (call $helper-i32)
  ;; CHECK-NEXT:   )
  ;; CHECK-NEXT:  )
  ;; CHECK-NEXT:  (block
  ;; CHECK-NEXT:   (drop
  ;; CHECK-NEXT:    (call $helper-ref)
  ;; CHECK-NEXT:   )
  ;; CHECK-NEXT:   (drop
  ;; CHECK-NEXT:    (call $helper-ref)
  ;; CHECK-NEXT:   )
  ;; CHECK-NEXT:  )
  ;; CHECK-NEXT:  (block
  ;; CHECK-NEXT:   (drop
  ;; CHECK-NEXT:    (call $helper-i32)
  ;; CHECK-NEXT:   )
  ;; CHECK-NEXT:   (drop
  ;; CHECK-NEXT:    (call $helper-i32)
  ;; CHECK-NEXT:   )
  ;; CHECK-NEXT:  )
  ;; CHECK-NEXT:  (drop
  ;; CHECK-NEXT:   (call $call.without.effects.non.null
  ;; CHECK-NEXT:    (call $helper-i32)
  ;; CHECK-NEXT:    (call $helper-i32)
  ;; CHECK-NEXT:    (ref.func $helper-two-refs-non-null)
  ;; CHECK-NEXT:   )
  ;; CHECK-NEXT:  )
  ;; CHECK-NEXT: )
  (func $dropped-calls
    ;; The calls' outputs are used in a computation that itself has no effects,
    ;; and is dropped, so we don't need it. But we can't remove the calls
    ;; themselves, which should be all that remains, with drops of them (there
    ;; will also be blocks, which merge-blocks would remove).
    (drop
      (i32.add
        (call $helper-i32)
        (call $helper-i32)
      )
    )
    (drop
      (ref.eq
        (call $helper-ref)
        (call $helper-ref)
      )
    )
    ;; The call.without.effects can be removed, but not the two calls nested in
    ;; it.
    (drop
      (call $call.without.effects
        (call $helper-i32)
        (call $helper-i32)
        (ref.func $helper-two-refs)
      )
    )
    ;; The non-null case however is tricky, and we do not handle it atm. TODO
    (drop
      (call $call.without.effects.non.null
        (call $helper-i32)
        (call $helper-i32)
        (ref.func $helper-two-refs-non-null)
      )
    )
  )

  ;; CHECK:      (func $helper-i32 (type $none_=>_i32) (result i32)
  ;; CHECK-NEXT:  (i32.const 1)
  ;; CHECK-NEXT: )
  (func $helper-i32 (result i32)
    (i32.const 1)
  )

  ;; CHECK:      (func $helper-ref (type $none_=>_eqref) (result eqref)
  ;; CHECK-NEXT:  (unreachable)
  ;; CHECK-NEXT: )
  (func $helper-ref (result eqref)
    (unreachable)
  )

  ;; CHECK:      (func $helper-two-refs (type $i32_i32_=>_anyref) (param $0 i32) (param $1 i32) (result anyref)
  ;; CHECK-NEXT:  (unreachable)
  ;; CHECK-NEXT: )
  (func $helper-two-refs (param i32) (param i32) (result (ref null any))
    (unreachable)
  )

  ;; CHECK:      (func $helper-two-refs-non-null (type $i32_i32_=>_ref|any|) (param $0 i32) (param $1 i32) (result (ref any))
  ;; CHECK-NEXT:  (unreachable)
  ;; CHECK-NEXT: )
  (func $helper-two-refs-non-null (param i32) (param i32) (result (ref any))
    (unreachable)
  )
)