blob: 12cd17bad6869d6f8b4485ae2dc3a20228a3d59b [file] [log] [blame]
// RUN: %target-sil-opt -enable-sil-verify-all %s -mem2reg | %FileCheck %s
import Builtin
import Swift
// We cannot use unchecked_value_cast for conversions to trivial type.
// Since it is forwarding, the ownersip of the src forwards, but we cannot destroy the dst because it is trivial
// CHECK-LABEL: sil [ossa] @casttotrivial :
// CHECK: [[CAST:%.*]] = unchecked_bitwise_cast %0 : $AnyObject to $UInt8
// CHECK-NEXT: destroy_value %0 : $AnyObject
// CHECK-NEXT: return [[CAST]] : $UInt8
// CHECK-LABEL: } // end sil function 'casttotrivial'
sil [ossa] @casttotrivial : $@convention(thin) (@owned AnyObject) -> @owned UInt8 {
bb0(%0 : @owned $AnyObject):
%1 = alloc_stack $AnyObject
store %0 to [init] %1 : $*AnyObject
%2 = unchecked_addr_cast %1 : $*AnyObject to $*UInt8
%3 = load [trivial] %2 : $*UInt8
%4 = load [take] %1 : $*AnyObject
destroy_value %4 : $AnyObject
dealloc_stack %1 : $*AnyObject
return %3 : $UInt8
}
// We cannot use unchecked_value_cast, because it is forwarding, it forwards the src, and the src can no longer be used as a RunningVal
// To get rid of this issue we need to use a copy_value of the src, and make sure we don't generate copy_value in case of a load [copy].
// To avoid all this spl handling, just use bitwise cast
// CHECK-LABEL: sil [ossa] @casttonontrivial :
// CHECK: [[CAST:%.*]] = unchecked_bitwise_cast %0 : $AnyObject to $String
// CHECK: [[COPY:%.*]] = copy_value [[CAST]] : $String
// CHECK-NEXT: destroy_value %0 : $AnyObject
// CHECK-NEXT: return [[COPY]] : $String
// CHECK-LABEL: } // end sil function 'casttonontrivial'
sil [ossa] @casttonontrivial : $@convention(thin) (@owned AnyObject) -> @owned String {
bb0(%0 : @owned $AnyObject):
%1 = alloc_stack $AnyObject
store %0 to [init] %1 : $*AnyObject
%2 = unchecked_addr_cast %1 : $*AnyObject to $*String
%3 = load [copy] %2 : $*String
%4 = load [take] %1 : $*AnyObject
destroy_value %4 : $AnyObject
dealloc_stack %1 : $*AnyObject
return %3 : $String
}
struct Pair { var lhs: AnyObject; var rhs: AnyObject }
// CHECK-LABEL: sil [ossa] @shorteningcast :
// CHECK: [[CAST:%.*]] = unchecked_bitwise_cast %0 : $Pair to $AnyObject
// CHECK: [[COPY:%.*]] = copy_value [[CAST]] : $AnyObject
// CHECK-NEXT: destroy_value %0 : $Pair
// CHECK-NEXT: return [[COPY]] : $AnyObject
// CHECK-LABEL: } // end sil function 'shorteningcast'
sil [ossa] @shorteningcast : $@convention(thin) (@owned Pair) -> @owned AnyObject {
bb0(%0 : @owned $Pair):
%1 = alloc_stack $Pair
store %0 to [init] %1 : $*Pair
%2 = unchecked_addr_cast %1 : $*Pair to $*AnyObject
%3 = load [copy] %2 : $*AnyObject
%4 = load [take] %1 : $*Pair
destroy_value %4 : $Pair
dealloc_stack %1 : $*Pair
return %3 : $AnyObject
}
// CHECK-LABEL: sil [ossa] @deadcast :
// CHECK-LABEL: bb0
// CHECK-NEXT: destroy_value %0 : $AnyObject
// CHECK-LABEL: } // end sil function 'deadcast'
sil [ossa] @deadcast : $@convention(thin) (@owned AnyObject) -> () {
bb0(%0 : @owned $AnyObject):
%1 = alloc_stack $AnyObject
store %0 to [init] %1 : $*AnyObject
%2 = unchecked_addr_cast %1 : $*AnyObject to $*String
destroy_addr %1 : $*AnyObject
dealloc_stack %1 : $*AnyObject
%4 = tuple()
return %4 : $()
}