summaryrefslogtreecommitdiff
path: root/test/lit/passes/simplify-globals-non-init.wast
diff options
context:
space:
mode:
authorAlon Zakai <azakai@google.com>2021-11-23 15:14:19 -0800
committerGitHub <noreply@github.com>2021-11-23 15:14:19 -0800
commita8dd9e83be8db755294f9e78b8f3466d1d03c1de (patch)
treed7931c6d0fc76501ec3afb842acc288bc73aa274 /test/lit/passes/simplify-globals-non-init.wast
parent7f24fce21a92f2aed4a11745d27d5181798ba6cd (diff)
downloadbinaryen-a8dd9e83be8db755294f9e78b8f3466d1d03c1de.tar.gz
binaryen-a8dd9e83be8db755294f9e78b8f3466d1d03c1de.tar.bz2
binaryen-a8dd9e83be8db755294f9e78b8f3466d1d03c1de.zip
SimplifyGlobals: If all writes write the initial value, they are unneeded (#4356)
Diffstat (limited to 'test/lit/passes/simplify-globals-non-init.wast')
-rw-r--r--test/lit/passes/simplify-globals-non-init.wast143
1 files changed, 143 insertions, 0 deletions
diff --git a/test/lit/passes/simplify-globals-non-init.wast b/test/lit/passes/simplify-globals-non-init.wast
new file mode 100644
index 000000000..9e3230319
--- /dev/null
+++ b/test/lit/passes/simplify-globals-non-init.wast
@@ -0,0 +1,143 @@
+;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited.
+;; NOTE: This test was ported using port_test.py and could be cleaned up.
+
+;; RUN: foreach %s %t wasm-opt --simplify-globals --enable-mutable-globals -S -o - | filecheck %s
+
+;; A global that is written its initial value in all subsequent writes can
+;; remove those writes.
+(module
+ ;; CHECK: (type $none_=>_none (func))
+
+ ;; CHECK: (global $global-0 i32 (i32.const 0))
+ (global $global-0 (mut i32) (i32.const 0))
+ ;; CHECK: (global $global-1 i32 (i32.const 1))
+ (global $global-1 (mut i32) (i32.const 1))
+
+ ;; CHECK: (func $sets
+ ;; CHECK-NEXT: (drop
+ ;; CHECK-NEXT: (i32.const 0)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (drop
+ ;; CHECK-NEXT: (i32.const 0)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (drop
+ ;; CHECK-NEXT: (i32.const 1)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (drop
+ ;; CHECK-NEXT: (i32.const 1)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ (func $sets
+ ;; All these writes can be turned into drops.
+
+ (global.set $global-0 (i32.const 0))
+ (global.set $global-0 (i32.const 0))
+
+ (global.set $global-1 (i32.const 1))
+ (global.set $global-1 (i32.const 1))
+ )
+
+ ;; CHECK: (func $gets
+ ;; CHECK-NEXT: (drop
+ ;; CHECK-NEXT: (i32.const 0)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (drop
+ ;; CHECK-NEXT: (i32.const 1)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ (func $gets
+ ;; Add gets to avoid other opts from removing the sets.
+ (drop (global.get $global-0))
+ (drop (global.get $global-1))
+ )
+)
+
+;; As above, but now we write other values.
+(module
+ ;; CHECK: (type $i32_=>_none (func (param i32)))
+
+ ;; CHECK: (type $none_=>_none (func))
+
+ ;; CHECK: (global $global-0 (mut i32) (i32.const 0))
+ (global $global-0 (mut i32) (i32.const 0))
+ ;; CHECK: (global $global-1 (mut i32) (i32.const 1))
+ (global $global-1 (mut i32) (i32.const 1))
+
+ ;; CHECK: (func $sets (param $unknown i32)
+ ;; CHECK-NEXT: (global.set $global-0
+ ;; CHECK-NEXT: (i32.const 0)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (global.set $global-0
+ ;; CHECK-NEXT: (i32.const 1)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (global.set $global-1
+ ;; CHECK-NEXT: (i32.const 1)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (global.set $global-1
+ ;; CHECK-NEXT: (local.get $unknown)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ (func $sets (param $unknown i32)
+ (global.set $global-0 (i32.const 0))
+ (global.set $global-0 (i32.const 1)) ;; a non-init value
+
+ (global.set $global-1 (i32.const 1))
+ (global.set $global-1 (local.get $unknown)) ;; a totally unknown value
+ )
+
+ ;; CHECK: (func $gets
+ ;; CHECK-NEXT: (drop
+ ;; CHECK-NEXT: (global.get $global-0)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (drop
+ ;; CHECK-NEXT: (global.get $global-1)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ (func $gets
+ (drop (global.get $global-0))
+ (drop (global.get $global-1))
+ )
+)
+
+;; Globals without constant initial values.
+(module
+ ;; An imported global.
+ ;; CHECK: (type $i32_=>_none (func (param i32)))
+
+ ;; CHECK: (type $none_=>_none (func))
+
+ ;; CHECK: (import "env" "import_global" (global $global-0 (mut i32)))
+ (import "env" "import_global" (global $global-0 (mut i32)))
+
+ ;; A global that initializes with another global.
+ ;; CHECK: (global $global-1 (mut i32) (global.get $global-0))
+ (global $global-1 (mut i32) (global.get $global-0))
+
+ ;; CHECK: (func $sets (param $unknown i32)
+ ;; CHECK-NEXT: (global.set $global-0
+ ;; CHECK-NEXT: (i32.const 0)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (global.set $global-1
+ ;; CHECK-NEXT: (i32.const 1)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ (func $sets (param $unknown i32)
+ (global.set $global-0 (i32.const 0))
+
+ (global.set $global-1 (i32.const 1))
+ )
+
+ ;; CHECK: (func $gets
+ ;; CHECK-NEXT: (drop
+ ;; CHECK-NEXT: (global.get $global-0)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (drop
+ ;; CHECK-NEXT: (global.get $global-1)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ (func $gets
+ ;; Add gets to avoid other opts from removing the sets.
+ (drop (global.get $global-0))
+ (drop (global.get $global-1))
+ )
+)