1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
// RUN: %target-sil-opt -enable-sil-verify-all -enable-lexical-lifetimes %s -mem2reg | %FileCheck %s
import Builtin
import Swift
// We cannot use unchecked_value_cast for conversions to trivial type.
// Since it is forwarding, the ownership of the src forwards, but we cannot destroy the dst because it is trivial
// CHECK-LABEL: sil [ossa] @casttotrivial :
// CHECK: [[LIFETIME_OWNED:%[^,]+]] = move_value [lexical] %0
// CHECK: [[CAST:%.*]] = unchecked_bitwise_cast [[LIFETIME_OWNED]] : $AnyObject to $UInt8
// CHECK-NEXT: destroy_value [[LIFETIME_OWNED]]
// CHECK-NEXT: return [[CAST]]
// CHECK-LABEL: } // end sil function 'casttotrivial'
sil [ossa] @casttotrivial : $@convention(thin) (@owned AnyObject) -> @owned UInt8 {
bb0(%0 : @owned $AnyObject):
%1 = alloc_stack [lexical] $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: [[LIFETIME_OWNED:%[^,]+]] = move_value [lexical] %0
// CHECK: [[CAST:%.*]] = unchecked_bitwise_cast [[LIFETIME_OWNED]] : $AnyObject to $String
// CHECK: [[COPY:%.*]] = copy_value [[CAST]]
// CHECK-NEXT: destroy_value [[LIFETIME_OWNED]]
// CHECK-NEXT: return [[COPY]]
// CHECK-LABEL: } // end sil function 'casttonontrivial'
sil [ossa] @casttonontrivial : $@convention(thin) (@owned AnyObject) -> @owned String {
bb0(%0 : @owned $AnyObject):
%1 = alloc_stack [lexical] $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: [[LIFETIME_OWNED:%[^,]+]] = move_value [lexical] %0
// CHECK: [[CAST:%.*]] = unchecked_bitwise_cast [[LIFETIME_OWNED]] : $Pair to $AnyObject
// CHECK: [[COPY:%.*]] = copy_value [[CAST]]
// CHECK-NEXT: destroy_value [[LIFETIME_OWNED]]
// CHECK-NEXT: return [[COPY]]
// CHECK-LABEL: } // end sil function 'shorteningcast'
sil [ossa] @shorteningcast : $@convention(thin) (@owned Pair) -> @owned AnyObject {
bb0(%0 : @owned $Pair):
%1 = alloc_stack [lexical] $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: [[LIFETIME_OWNED:%[^,]+]] = move_value [lexical] %0
// CHECK-NEXT: destroy_value [[LIFETIME_OWNED]]
// CHECK-LABEL: } // end sil function 'deadcast'
sil [ossa] @deadcast : $@convention(thin) (@owned AnyObject) -> () {
bb0(%0 : @owned $AnyObject):
%1 = alloc_stack [lexical] $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 : $()
}
|