Raw File
canonicalize_switch_enum.sil
// RUN: %target-sil-opt -enable-sil-verify-all %s -simplify-cfg | FileCheck %s

import Builtin
import Swift
import SwiftShims

enum E {
	case A
	case B
	case C
}

enum GenE<T> {
	case A
	case B
	case C(T)
}

sil @external_f : $@convention(thin) () -> ()

// CHECK-LABEL: sil @test_switch_enum
sil @test_switch_enum : $@convention(thin) (E) -> Int32 {
bb0(%0 : $E):
  // CHECK: switch_enum %0 : $E, case #E.A!enumelt: bb1, case #E.B!enumelt: bb2, case #E.C!enumelt: bb3
  switch_enum %0 : $E, case #E.A!enumelt: bb1, case #E.B!enumelt: bb2, default bb3

bb1:
  %2 = integer_literal $Builtin.Int32, 10
  br bb4(%2 : $Builtin.Int32)

bb2:
  %21 = function_ref @external_f : $@convention(thin) () -> ()
  %22 = apply %21() : $@convention(thin) () -> ()
  %23 = integer_literal $Builtin.Int32, 20
  br bb4(%23 : $Builtin.Int32)

bb3:
  %6 = integer_literal $Builtin.Int32, 30
  br bb4(%6 : $Builtin.Int32)

bb4(%8 : $Builtin.Int32):
  %9 = struct $Int32 (%8 : $Builtin.Int32)
  return %9 : $Int32
}

// CHECK-LABEL: sil @test_switch_enum_addr
sil @test_switch_enum_addr : $@convention(thin) <T> (@in GenE<T>) -> Int32 {
bb0(%0 : $*GenE<T>):
  %1 = alloc_stack $GenE<T>
  copy_addr %0 to [initialization] %1 : $*GenE<T>
  // CHECK: switch_enum_addr %1 : $*GenE<T>, case #GenE.A!enumelt: bb1, case #GenE.B!enumelt: bb2, case #GenE.C!enumelt.1: bb3
  switch_enum_addr %1 : $*GenE<T>, case #GenE.A!enumelt: bb1, case #GenE.B!enumelt: bb2, default bb3

bb1:
  %4 = integer_literal $Builtin.Int32, 10
  br bb4(%4 : $Builtin.Int32)

bb2:
  %6 = integer_literal $Builtin.Int32, 20
  br bb4(%6 : $Builtin.Int32)

bb3:
  destroy_addr %1 : $*GenE<T>
  %9 = integer_literal $Builtin.Int32, 30
  br bb4(%9 : $Builtin.Int32)

bb4(%11 : $Builtin.Int32):
  %12 = struct $Int32 (%11 : $Builtin.Int32)
  dealloc_stack %1 : $*GenE<T>
  destroy_addr %0 : $*GenE<T>
  return %12 : $Int32
}

// CHECK-LABEL: sil @test_no_replace
sil @test_no_replace : $@convention(thin) (@inout E) -> Int32 {
bb0(%0 : $*E):
  %1 = load %0 : $*E
  // CHECK: switch_enum %1 : $E, case #E.A!enumelt: bb1, default bb2
  switch_enum %1 : $E, case #E.A!enumelt: bb1, default bb2

bb1:
  %3 = integer_literal $Builtin.Int32, 10
  br bb3(%3 : $Builtin.Int32)

bb2:
  %21 = function_ref @external_f : $@convention(thin) () -> ()
  %22 = apply %21() : $@convention(thin) () -> ()
  %5 = integer_literal $Builtin.Int32, 30
  br bb3(%5 : $Builtin.Int32)

bb3(%7 : $Builtin.Int32):
  %8 = struct $Int32 (%7 : $Builtin.Int32)
  return %8 : $Int32
}

back to top