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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
|
;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited.
;; RUN: wasm-opt %s --optimize-instructions --traps-never-happen -all --nominal -S -o - | filecheck %s --check-prefix TNH
;; RUN: wasm-opt %s --optimize-instructions -all --nominal -S -o - | filecheck %s --check-prefix NO_TNH
(module
;; TNH: (type $struct (struct (field (mut i32))))
;; NO_TNH: (type $struct (struct (field (mut i32))))
(type $struct (struct_subtype (field (mut i32)) data))
;; TNH: (type $void (func))
;; NO_TNH: (type $void (func))
(type $void (func))
;; TNH: (func $ref.eq (type $eqref_eqref_=>_i32) (param $a eqref) (param $b eqref) (result i32)
;; TNH-NEXT: (ref.eq
;; TNH-NEXT: (local.get $a)
;; TNH-NEXT: (local.get $b)
;; TNH-NEXT: )
;; TNH-NEXT: )
;; NO_TNH: (func $ref.eq (type $eqref_eqref_=>_i32) (param $a eqref) (param $b eqref) (result i32)
;; NO_TNH-NEXT: (ref.eq
;; NO_TNH-NEXT: (ref.cast $struct
;; NO_TNH-NEXT: (local.get $a)
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: (ref.cast struct
;; NO_TNH-NEXT: (local.get $b)
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: )
(func $ref.eq (param $a (ref null eq)) (param $b (ref null eq)) (result i32)
;; When traps never happen we can remove all the casts here, since they do
;; not affect the comparison of the references.
(ref.eq
;; When traps can happen we can still improve this by removing and
;; combining redundant casts.
(ref.cast struct
(ref.as_non_null
(ref.cast null $struct
(local.get $a)
)
)
)
;; Note that we can remove the non-null casts here in both modes, as the
;; ref.cast struct also checks for null.
(ref.cast struct
(ref.as_non_null
(ref.as_non_null
(local.get $b)
)
)
)
)
)
;; TNH: (func $ref.eq-no (type $eqref_eqref_anyref_=>_none) (param $a eqref) (param $b eqref) (param $any anyref)
;; TNH-NEXT: (drop
;; TNH-NEXT: (i32.const 1)
;; TNH-NEXT: )
;; TNH-NEXT: )
;; NO_TNH: (func $ref.eq-no (type $eqref_eqref_anyref_=>_none) (param $a eqref) (param $b eqref) (param $any anyref)
;; NO_TNH-NEXT: (drop
;; NO_TNH-NEXT: (ref.eq
;; NO_TNH-NEXT: (ref.cast null $struct
;; NO_TNH-NEXT: (local.get $any)
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: (ref.cast struct
;; NO_TNH-NEXT: (local.get $any)
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: )
(func $ref.eq-no (param $a (ref null eq)) (param $b (ref null eq)) (param $any anyref)
;; We must leave the inputs to ref.eq of type eqref or a subtype.
(drop
(ref.eq
(ref.cast null $struct
(local.get $any) ;; *Not* an eqref!
)
(ref.as_non_null
(ref.cast struct
(ref.as_non_null
(local.get $any) ;; *Not* an eqref!
)
)
)
)
)
)
;; TNH: (func $ref.is (type $eqref_=>_i32) (param $a eqref) (result i32)
;; TNH-NEXT: (drop
;; TNH-NEXT: (ref.cast $struct
;; TNH-NEXT: (local.get $a)
;; TNH-NEXT: )
;; TNH-NEXT: )
;; TNH-NEXT: (i32.const 0)
;; TNH-NEXT: )
;; NO_TNH: (func $ref.is (type $eqref_=>_i32) (param $a eqref) (result i32)
;; NO_TNH-NEXT: (drop
;; NO_TNH-NEXT: (ref.cast $struct
;; NO_TNH-NEXT: (local.get $a)
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: (i32.const 0)
;; NO_TNH-NEXT: )
(func $ref.is (param $a (ref null eq)) (result i32)
;; In this case non-nullability is enough to tell that the ref.is will
;; return 0. TNH does not help here.
(ref.is_null
(ref.cast $struct
(ref.as_non_null
(ref.cast struct
(local.get $a)
)
)
)
)
)
;; TNH: (func $ref.is_b (type $eqref_funcref_=>_i32) (param $a eqref) (param $f funcref) (result i32)
;; TNH-NEXT: (drop
;; TNH-NEXT: (ref.is_null
;; TNH-NEXT: (local.get $a)
;; TNH-NEXT: )
;; TNH-NEXT: )
;; TNH-NEXT: (ref.is_null
;; TNH-NEXT: (local.get $f)
;; TNH-NEXT: )
;; TNH-NEXT: )
;; NO_TNH: (func $ref.is_b (type $eqref_funcref_=>_i32) (param $a eqref) (param $f funcref) (result i32)
;; NO_TNH-NEXT: (drop
;; NO_TNH-NEXT: (ref.is_null
;; NO_TNH-NEXT: (ref.cast null $struct
;; NO_TNH-NEXT: (local.get $a)
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: (ref.is_null
;; NO_TNH-NEXT: (ref.cast null $void
;; NO_TNH-NEXT: (local.get $f)
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: )
(func $ref.is_b (param $a eqref) (param $f funcref) (result i32)
;; Here we only have a cast, and no ref.as operations that force the value
;; to be non-nullable. That means we cannot remove the ref.is, but we can
;; remove the cast in TNH.
(drop
(ref.is_null
(ref.cast null $struct
(local.get $a)
)
)
)
;; It works on func references, too.
(ref.is_null
(ref.cast null $void
(local.get $f)
)
)
)
;; TNH: (func $ref.is_func (type $funcref_=>_i32) (param $a funcref) (result i32)
;; TNH-NEXT: (drop
;; TNH-NEXT: (ref.as_non_null
;; TNH-NEXT: (local.get $a)
;; TNH-NEXT: )
;; TNH-NEXT: )
;; TNH-NEXT: (i32.const 1)
;; TNH-NEXT: )
;; NO_TNH: (func $ref.is_func (type $funcref_=>_i32) (param $a funcref) (result i32)
;; NO_TNH-NEXT: (drop
;; NO_TNH-NEXT: (ref.as_non_null
;; NO_TNH-NEXT: (local.get $a)
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: (i32.const 1)
;; NO_TNH-NEXT: )
(func $ref.is_func (param $a funcref) (result i32)
;; The check must succeed. We can return 1 here, and drop the rest, with or
;; without TNH (in particular, TNH should not just remove the cast but not
;; return a 1).
(ref.is_func
(ref.as_func
(local.get $a)
)
)
)
;; TNH: (func $if.arm.null (type $i32_ref|$struct|_=>_none) (param $x i32) (param $ref (ref $struct))
;; TNH-NEXT: (struct.set $struct 0
;; TNH-NEXT: (block (result (ref $struct))
;; TNH-NEXT: (drop
;; TNH-NEXT: (local.get $x)
;; TNH-NEXT: )
;; TNH-NEXT: (local.get $ref)
;; TNH-NEXT: )
;; TNH-NEXT: (i32.const 1)
;; TNH-NEXT: )
;; TNH-NEXT: (struct.set $struct 0
;; TNH-NEXT: (block (result (ref $struct))
;; TNH-NEXT: (drop
;; TNH-NEXT: (local.get $x)
;; TNH-NEXT: )
;; TNH-NEXT: (local.get $ref)
;; TNH-NEXT: )
;; TNH-NEXT: (i32.const 2)
;; TNH-NEXT: )
;; TNH-NEXT: )
;; NO_TNH: (func $if.arm.null (type $i32_ref|$struct|_=>_none) (param $x i32) (param $ref (ref $struct))
;; NO_TNH-NEXT: (struct.set $struct 0
;; NO_TNH-NEXT: (if (result (ref null $struct))
;; NO_TNH-NEXT: (local.get $x)
;; NO_TNH-NEXT: (local.get $ref)
;; NO_TNH-NEXT: (ref.null none)
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: (i32.const 1)
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: (struct.set $struct 0
;; NO_TNH-NEXT: (if (result (ref null $struct))
;; NO_TNH-NEXT: (local.get $x)
;; NO_TNH-NEXT: (ref.null none)
;; NO_TNH-NEXT: (local.get $ref)
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: (i32.const 2)
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: )
(func $if.arm.null (param $x i32) (param $ref (ref $struct))
;; A set will trap on a null, so in tnh mode we know the null arm is not
;; executed, and the other one is.
(struct.set $struct 0
(if (result (ref null $struct))
(local.get $x)
(local.get $ref)
(ref.null none)
)
(i32.const 1)
)
(struct.set $struct 0
(if (result (ref null $struct))
(local.get $x)
(ref.null none)
(local.get $ref)
)
(i32.const 2)
)
)
;; TNH: (func $select.arm.null (type $i32_ref|$struct|_=>_none) (param $x i32) (param $ref (ref $struct))
;; TNH-NEXT: (struct.set $struct 0
;; TNH-NEXT: (block (result (ref $struct))
;; TNH-NEXT: (block
;; TNH-NEXT: (drop
;; TNH-NEXT: (ref.null none)
;; TNH-NEXT: )
;; TNH-NEXT: (drop
;; TNH-NEXT: (local.get $x)
;; TNH-NEXT: )
;; TNH-NEXT: )
;; TNH-NEXT: (local.get $ref)
;; TNH-NEXT: )
;; TNH-NEXT: (i32.const 1)
;; TNH-NEXT: )
;; TNH-NEXT: (struct.set $struct 0
;; TNH-NEXT: (block (result (ref $struct))
;; TNH-NEXT: (drop
;; TNH-NEXT: (ref.null none)
;; TNH-NEXT: )
;; TNH-NEXT: (block (result (ref $struct))
;; TNH-NEXT: (drop
;; TNH-NEXT: (local.get $x)
;; TNH-NEXT: )
;; TNH-NEXT: (local.get $ref)
;; TNH-NEXT: )
;; TNH-NEXT: )
;; TNH-NEXT: (i32.const 2)
;; TNH-NEXT: )
;; TNH-NEXT: )
;; NO_TNH: (func $select.arm.null (type $i32_ref|$struct|_=>_none) (param $x i32) (param $ref (ref $struct))
;; NO_TNH-NEXT: (struct.set $struct 0
;; NO_TNH-NEXT: (select (result (ref null $struct))
;; NO_TNH-NEXT: (local.get $ref)
;; NO_TNH-NEXT: (ref.null none)
;; NO_TNH-NEXT: (local.get $x)
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: (i32.const 1)
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: (struct.set $struct 0
;; NO_TNH-NEXT: (select (result (ref null $struct))
;; NO_TNH-NEXT: (ref.null none)
;; NO_TNH-NEXT: (local.get $ref)
;; NO_TNH-NEXT: (local.get $x)
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: (i32.const 2)
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: )
(func $select.arm.null (param $x i32) (param $ref (ref $struct))
;; As above but with a select.
(struct.set $struct 0
(select (result (ref null $struct))
(local.get $ref)
(ref.null none)
(local.get $x)
)
(i32.const 1)
)
(struct.set $struct 0
(select (result (ref null $struct))
(ref.null none)
(local.get $ref)
(local.get $x)
)
(i32.const 2)
)
)
;; TNH: (func $select.arm.null.effects (type $void)
;; TNH-NEXT: (local $0 (ref $struct))
;; TNH-NEXT: (local $1 (ref $struct))
;; TNH-NEXT: (struct.set $struct 0
;; TNH-NEXT: (block (result (ref $struct))
;; TNH-NEXT: (local.set $0
;; TNH-NEXT: (call $get-ref)
;; TNH-NEXT: )
;; TNH-NEXT: (block
;; TNH-NEXT: (drop
;; TNH-NEXT: (call $get-null)
;; TNH-NEXT: )
;; TNH-NEXT: (drop
;; TNH-NEXT: (call $get-i32)
;; TNH-NEXT: )
;; TNH-NEXT: )
;; TNH-NEXT: (local.get $0)
;; TNH-NEXT: )
;; TNH-NEXT: (i32.const 1)
;; TNH-NEXT: )
;; TNH-NEXT: (struct.set $struct 0
;; TNH-NEXT: (block (result (ref $struct))
;; TNH-NEXT: (drop
;; TNH-NEXT: (call $get-null)
;; TNH-NEXT: )
;; TNH-NEXT: (block (result (ref $struct))
;; TNH-NEXT: (local.set $1
;; TNH-NEXT: (call $get-ref)
;; TNH-NEXT: )
;; TNH-NEXT: (drop
;; TNH-NEXT: (call $get-i32)
;; TNH-NEXT: )
;; TNH-NEXT: (local.get $1)
;; TNH-NEXT: )
;; TNH-NEXT: )
;; TNH-NEXT: (i32.const 2)
;; TNH-NEXT: )
;; TNH-NEXT: )
;; NO_TNH: (func $select.arm.null.effects (type $void)
;; NO_TNH-NEXT: (struct.set $struct 0
;; NO_TNH-NEXT: (select (result (ref null $struct))
;; NO_TNH-NEXT: (call $get-ref)
;; NO_TNH-NEXT: (call $get-null)
;; NO_TNH-NEXT: (call $get-i32)
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: (i32.const 1)
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: (struct.set $struct 0
;; NO_TNH-NEXT: (select (result (ref null $struct))
;; NO_TNH-NEXT: (call $get-null)
;; NO_TNH-NEXT: (call $get-ref)
;; NO_TNH-NEXT: (call $get-i32)
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: (i32.const 2)
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: )
(func $select.arm.null.effects
;; As above but there are conflicting effects and we must add a local when
;; we optimize.
(struct.set $struct 0
(select (result (ref null $struct))
(call $get-ref)
(call $get-null)
(call $get-i32)
)
(i32.const 1)
)
(struct.set $struct 0
(select (result (ref null $struct))
(call $get-null)
(call $get-ref)
(call $get-i32)
)
(i32.const 2)
)
)
;; TNH: (func $null.arm.null.effects (type $void)
;; TNH-NEXT: (block ;; (replaces something unreachable we can't emit)
;; TNH-NEXT: (drop
;; TNH-NEXT: (block (result nullref)
;; TNH-NEXT: (drop
;; TNH-NEXT: (ref.as_non_null
;; TNH-NEXT: (ref.null none)
;; TNH-NEXT: )
;; TNH-NEXT: )
;; TNH-NEXT: (block (result nullref)
;; TNH-NEXT: (drop
;; TNH-NEXT: (call $get-i32)
;; TNH-NEXT: )
;; TNH-NEXT: (ref.null none)
;; TNH-NEXT: )
;; TNH-NEXT: )
;; TNH-NEXT: )
;; TNH-NEXT: (drop
;; TNH-NEXT: (i32.const 1)
;; TNH-NEXT: )
;; TNH-NEXT: (unreachable)
;; TNH-NEXT: )
;; TNH-NEXT: )
;; NO_TNH: (func $null.arm.null.effects (type $void)
;; NO_TNH-NEXT: (struct.set $struct 0
;; NO_TNH-NEXT: (select (result (ref null $struct))
;; NO_TNH-NEXT: (ref.as_non_null
;; NO_TNH-NEXT: (ref.null none)
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: (ref.null none)
;; NO_TNH-NEXT: (call $get-i32)
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: (i32.const 1)
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: )
(func $null.arm.null.effects
;; Verify we do not error on a null reference in a select, even if cast to
;; non-null.
(struct.set $struct 0
(select (result (ref null $struct))
(ref.as_non_null
(ref.null none)
)
(ref.null none)
(call $get-i32)
)
(i32.const 1)
)
)
;; TNH: (func $set-get-cast (type $structref_=>_none) (param $ref structref)
;; TNH-NEXT: (drop
;; TNH-NEXT: (struct.get $struct 0
;; TNH-NEXT: (ref.cast $struct
;; TNH-NEXT: (local.get $ref)
;; TNH-NEXT: )
;; TNH-NEXT: )
;; TNH-NEXT: )
;; TNH-NEXT: (struct.set $struct 0
;; TNH-NEXT: (ref.cast $struct
;; TNH-NEXT: (local.get $ref)
;; TNH-NEXT: )
;; TNH-NEXT: (i32.const 1)
;; TNH-NEXT: )
;; TNH-NEXT: (struct.set $struct 0
;; TNH-NEXT: (ref.cast null $struct
;; TNH-NEXT: (local.get $ref)
;; TNH-NEXT: )
;; TNH-NEXT: (block (result i32)
;; TNH-NEXT: (return)
;; TNH-NEXT: (i32.const 1)
;; TNH-NEXT: )
;; TNH-NEXT: )
;; TNH-NEXT: )
;; NO_TNH: (func $set-get-cast (type $structref_=>_none) (param $ref structref)
;; NO_TNH-NEXT: (drop
;; NO_TNH-NEXT: (struct.get $struct 0
;; NO_TNH-NEXT: (ref.cast $struct
;; NO_TNH-NEXT: (local.get $ref)
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: (struct.set $struct 0
;; NO_TNH-NEXT: (ref.cast null $struct
;; NO_TNH-NEXT: (local.get $ref)
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: (i32.const 1)
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: (struct.set $struct 0
;; NO_TNH-NEXT: (ref.cast null $struct
;; NO_TNH-NEXT: (local.get $ref)
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: (block (result i32)
;; NO_TNH-NEXT: (return)
;; NO_TNH-NEXT: (i32.const 1)
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: )
(func $set-get-cast (param $ref (ref null struct))
;; A nullable cast flowing into a place that traps on null can become a
;; non-nullable cast.
(drop
(struct.get $struct 0
(ref.cast null $struct
(local.get $ref)
)
)
)
;; Ditto for a set, at least in traps-happen mode.
;; TODO handle non-TNH as well, but we need to be careful of effects in
;; other children.
(struct.set $struct 0
(ref.cast null $struct
(local.get $ref)
)
(i32.const 1)
)
;; Even in TNH mode, a child with an effect of control flow transfer
;; prevents us from optimizing - if the parent is not necessarily reached,
;; we cannot infer the child won't trap.
(struct.set $struct 0
(ref.cast null $struct
(local.get $ref)
)
(block (result i32)
;; This block has type i32, to check that we don't just look for
;; unreachable. We must scan for any transfer of control flow in the
;; child of the struct.set.
(return)
(i32.const 1)
)
)
)
;; TNH: (func $cast-if-null (type $none_=>_ref|$struct|) (result (ref $struct))
;; TNH-NEXT: (block ;; (replaces something unreachable we can't emit)
;; TNH-NEXT: (drop
;; TNH-NEXT: (block
;; TNH-NEXT: (drop
;; TNH-NEXT: (i32.const 1)
;; TNH-NEXT: )
;; TNH-NEXT: (unreachable)
;; TNH-NEXT: )
;; TNH-NEXT: )
;; TNH-NEXT: (unreachable)
;; TNH-NEXT: )
;; TNH-NEXT: )
;; NO_TNH: (func $cast-if-null (type $none_=>_ref|$struct|) (result (ref $struct))
;; NO_TNH-NEXT: (drop
;; NO_TNH-NEXT: (if (result (ref none))
;; NO_TNH-NEXT: (i32.const 1)
;; NO_TNH-NEXT: (unreachable)
;; NO_TNH-NEXT: (ref.as_non_null
;; NO_TNH-NEXT: (ref.null none)
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: (unreachable)
;; NO_TNH-NEXT: )
(func $cast-if-null (result (ref $struct))
;; We can remove the unreachable arm of the if here in TNH mode. While doing
;; so we must refinalize properly or else we'll hit an error in pass-debug
;; mode.
(ref.cast $struct
(if (result (ref none))
(i32.const 1)
(unreachable)
(ref.as_non_null
(ref.null none)
)
)
)
)
;; TNH: (func $cast-if-null-flip (type $none_=>_ref|$struct|) (result (ref $struct))
;; TNH-NEXT: (block ;; (replaces something unreachable we can't emit)
;; TNH-NEXT: (drop
;; TNH-NEXT: (block
;; TNH-NEXT: (drop
;; TNH-NEXT: (i32.const 1)
;; TNH-NEXT: )
;; TNH-NEXT: (unreachable)
;; TNH-NEXT: )
;; TNH-NEXT: )
;; TNH-NEXT: (unreachable)
;; TNH-NEXT: )
;; TNH-NEXT: )
;; NO_TNH: (func $cast-if-null-flip (type $none_=>_ref|$struct|) (result (ref $struct))
;; NO_TNH-NEXT: (drop
;; NO_TNH-NEXT: (if (result (ref none))
;; NO_TNH-NEXT: (i32.const 1)
;; NO_TNH-NEXT: (ref.as_non_null
;; NO_TNH-NEXT: (ref.null none)
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: (unreachable)
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: )
;; NO_TNH-NEXT: (unreachable)
;; NO_TNH-NEXT: )
(func $cast-if-null-flip (result (ref $struct))
;; As above but with arms flipped.
(ref.cast $struct
(if (result (ref none))
(i32.const 1)
(ref.as_non_null
(ref.null none)
)
(unreachable)
)
)
)
;; Helper functions.
;; TNH: (func $get-i32 (type $none_=>_i32) (result i32)
;; TNH-NEXT: (unreachable)
;; TNH-NEXT: )
;; NO_TNH: (func $get-i32 (type $none_=>_i32) (result i32)
;; NO_TNH-NEXT: (unreachable)
;; NO_TNH-NEXT: )
(func $get-i32 (result i32)
(unreachable)
)
;; TNH: (func $get-ref (type $none_=>_ref|$struct|) (result (ref $struct))
;; TNH-NEXT: (unreachable)
;; TNH-NEXT: )
;; NO_TNH: (func $get-ref (type $none_=>_ref|$struct|) (result (ref $struct))
;; NO_TNH-NEXT: (unreachable)
;; NO_TNH-NEXT: )
(func $get-ref (result (ref $struct))
(unreachable)
)
;; TNH: (func $get-null (type $none_=>_nullref) (result nullref)
;; TNH-NEXT: (unreachable)
;; TNH-NEXT: )
;; NO_TNH: (func $get-null (type $none_=>_nullref) (result nullref)
;; NO_TNH-NEXT: (unreachable)
;; NO_TNH-NEXT: )
(func $get-null (result (ref null none))
(unreachable)
)
)
|