;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited.
;; RUN: wasm-opt %s -all --disable-gc --dae2 --closed-world -S -o - | filecheck %s

(module
  ;; CHECK:      (type $f (func (param i32)))
  (type $f (func (param i32)))
  ;; CHECK:      (type $1 (func))

  ;; CHECK:      (type $2 (func (param i32 i64)))

  ;; CHECK:      (type $no-funcs (func (param i64)))
  (type $no-funcs (func (param i64)))

  ;; CHECK:      (table $t 1 1 funcref)
  (table $t funcref (elem $referenced))

  ;; CHECK:      (elem $implicit-elem (i32.const 0) $referenced)

  ;; CHECK:      (func $referenced (param $unused i32)
  ;; CHECK-NEXT:  (nop)
  ;; CHECK-NEXT: )
  (func $referenced (type $f) (param $unused i32)
    ;; Even though the parameter is not used in any use of this function type,
    ;; we cannot optimize indirect calls without GC enabled.
    (nop)
  )

  ;; CHECK:      (func $not-referenced
  ;; CHECK-NEXT:  (local $unused i32)
  ;; CHECK-NEXT:  (nop)
  ;; CHECK-NEXT: )
  (func $not-referenced (type $f) (param $unused i32)
    ;; Non-referenced functions with the same types can still be optimized.
    (nop)
  )

  ;; CHECK:      (func $caller (param $i32 i32) (param $i64 i64)
  ;; CHECK-NEXT:  (call_indirect $t (type $f)
  ;; CHECK-NEXT:   (local.get $i32)
  ;; CHECK-NEXT:   (i32.const 0)
  ;; CHECK-NEXT:  )
  ;; CHECK-NEXT:  (call_indirect $t (type $no-funcs)
  ;; CHECK-NEXT:   (local.get $i64)
  ;; CHECK-NEXT:   (i32.const 1)
  ;; CHECK-NEXT:  )
  ;; CHECK-NEXT: )
  (func $caller (param $i32 i32) (param $i64 i64)
    ;; Since the param remains used, we should not optimize indirect calls and
    ;; we should not optimize out the forwarded params.
    (call_indirect (type $f)
      (local.get $i32)
      (i32.const 0)
    )
    ;; Even function types with no functions cannot be optimized because in
    ;; general this would require being able to generate different function
    ;; types with the same signature, which we cannot do without GC.
    (call_indirect (type $no-funcs)
      (local.get $i64)
      (i32.const 1)
    )
  )
)
