;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited.

;; RUN: foreach %s %t wasm-opt --simplify-globals -all -S -o - | filecheck %s

(module
  ;; CHECK:      (type $0 (func))

  ;; CHECK:      (global $global (mut i32) (i32.const 0))
  (global $global (mut i32) (i32.const 0))

  ;; CHECK:      (func $test (type $0)
  ;; CHECK-NEXT:  (global.set $global
  ;; CHECK-NEXT:   (i32.const 10)
  ;; CHECK-NEXT:  )
  ;; CHECK-NEXT:  (if
  ;; CHECK-NEXT:   (i32.const 0)
  ;; CHECK-NEXT:   (then
  ;; CHECK-NEXT:    (drop
  ;; CHECK-NEXT:     (i32.const 10)
  ;; CHECK-NEXT:    )
  ;; CHECK-NEXT:    (call $test)
  ;; CHECK-NEXT:    (drop
  ;; CHECK-NEXT:     (global.get $global)
  ;; CHECK-NEXT:    )
  ;; CHECK-NEXT:   )
  ;; CHECK-NEXT:   (else
  ;; CHECK-NEXT:    (drop
  ;; CHECK-NEXT:     (global.get $global)
  ;; CHECK-NEXT:    )
  ;; CHECK-NEXT:   )
  ;; CHECK-NEXT:  )
  ;; CHECK-NEXT: )
  (func $test
    (global.set $global
      (i32.const 10)
    )
    (if
      (i32.const 0)
      (then
        (block
          ;; This is dominated by the set, so we can apply 10 here.
          (drop
            (global.get $global)
          )
          (call $test)
          ;; This is after a call, so we do nothing (we are still dominated by the
          ;; global.set, but the call might set the global to another value).
          (drop
            (global.get $global)
          )
        )
      )
      ;; This is dominated by the set, but we do not optimize it yet. TODO
      (else
        (drop
          (global.get $global)
        )
      )
    )
  )
)
