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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
// RUN: %target-sil-opt -enable-sil-verify-all %s -compute-side-effects -cse | %FileCheck %s
// REQUIRES: swift_in_compiler
sil_stage canonical
import Builtin
import Swift
// "return (x + y) + (x + y)"
// make sure we are only calculating x+y once.
// CHECK-LABEL: simply_arith :
// CHECK: builtin
// CHECK: cond_fail
// CHECK: builtin
// CHECK: cond_fail
// CHECK-NOT: builtin
// CHECK-NOT: cond_fail
// CHECK-LABEL:} // end sil function 'simply_arith'
sil [ossa] @simply_arith : $@convention(thin) (Int32, Int32) -> Int32 {
bb0(%0 : $Int32, %1 : $Int32):
debug_value %0 : $Int32, let, name "x"
debug_value %1 : $Int32, let, name "y"
%5 = struct_extract %0 : $Int32, #Int32._value
%6 = struct_extract %1 : $Int32, #Int32._value
%7 = integer_literal $Builtin.Int1, -1
%8 = builtin "sadd_with_overflow_Int32"(%5 : $Builtin.Int32, %6 : $Builtin.Int32, %7 : $Builtin.Int1) : $(Builtin.Int32, Builtin.Int1)
%9 = tuple_extract %8 : $(Builtin.Int32, Builtin.Int1), 0
%10 = tuple_extract %8 : $(Builtin.Int32, Builtin.Int1), 1
cond_fail %10 : $Builtin.Int1
%12 = struct $Int32 (%9 : $Builtin.Int32)
%14 = struct_extract %0 : $Int32, #Int32._value
%15 = struct_extract %1 : $Int32, #Int32._value
%16 = integer_literal $Builtin.Int1, -1
%17 = builtin "sadd_with_overflow_Int32"(%14 : $Builtin.Int32, %15 : $Builtin.Int32, %16 : $Builtin.Int1) : $(Builtin.Int32, Builtin.Int1)
%18 = tuple_extract %17 : $(Builtin.Int32, Builtin.Int1), 0
%19 = tuple_extract %17 : $(Builtin.Int32, Builtin.Int1), 1
cond_fail %19 : $Builtin.Int1
%21 = struct $Int32 (%18 : $Builtin.Int32)
%23 = struct_extract %12 : $Int32, #Int32._value
%24 = struct_extract %21 : $Int32, #Int32._value
%25 = integer_literal $Builtin.Int1, -1
%26 = builtin "sadd_with_overflow_Int32"(%23 : $Builtin.Int32, %24 : $Builtin.Int32, %25 : $Builtin.Int1) : $(Builtin.Int32, Builtin.Int1)
%27 = tuple_extract %26 : $(Builtin.Int32, Builtin.Int1), 0
%28 = tuple_extract %26 : $(Builtin.Int32, Builtin.Int1), 1
cond_fail %28 : $Builtin.Int1
%30 = struct $Int32 (%27 : $Builtin.Int32)
return %30 : $Int32
}
sil [ossa] @readnone : $@convention(thin) (Int64, Int64) -> Int64 {
bb0(%0 : $Int64, %1 : $Int64):
return %0 : $Int64
}
sil_global @gg : $Int64
sil [ossa] @readsome : $@convention(thin) (Int64, Int64) -> Int64 {
bb0(%0 : $Int64, %1 : $Int64):
%g = global_addr @gg : $*Int64
%l = load [trivial] %g : $*Int64
return %l : $Int64
}
struct Storage {
var a: Int64
var b: Int64
var c: Int64
}
class XX {
}
sil_global @ggs : $Storage
sil [ossa] @retain_only : $@convention(thin) () -> @owned XX {
bb0:
%g = global_addr @ggs : $*Storage
%a = address_to_pointer %g : $*Storage to $Builtin.RawPointer
%r = raw_pointer_to_ref %a : $Builtin.RawPointer to $XX
%copy = copy_value %r : $XX
return %copy : $XX
}
// CHECK-LABEL: sil [ossa] @cse_readnone_apply :
// CHECK: [[A:%[0-9]+]] = apply
// CHECK: [[S:%[0-9]+]] = struct_extract [[A]]
// CHECK: builtin "sadd_with_overflow_Int64"([[S]] : $Builtin.Int64, [[S]] : $Builtin.Int64
// CHECK-LABEL:} // end sil function 'cse_readnone_apply'
sil [ossa] @cse_readnone_apply : $@convention(thin) (Int64) -> Int64 {
bb0(%0 : $Int64):
%2 = function_ref @readnone : $@convention(thin) (Int64, Int64) -> Int64
%3 = integer_literal $Builtin.Int64, 3
%4 = struct $Int64 (%3 : $Builtin.Int64)
%5 = apply %2(%0, %4) : $@convention(thin) (Int64, Int64) -> Int64
%6 = apply %2(%0, %4) : $@convention(thin) (Int64, Int64) -> Int64
%7 = struct_extract %5 : $Int64, #Int64._value
%8 = struct_extract %6 : $Int64, #Int64._value
%9 = integer_literal $Builtin.Int1, 0
%10 = builtin "sadd_with_overflow_Int64"(%7 : $Builtin.Int64, %8 : $Builtin.Int64, %9 : $Builtin.Int1) : $(Builtin.Int64, Builtin.Int1)
%11 = tuple_extract %10 : $(Builtin.Int64, Builtin.Int1), 0
%14 = struct $Int64 (%11 : $Builtin.Int64)
return %14 : $Int64
}
// CHECK-LABEL: sil [ossa] @dont_cse_readsome_apply :
// CHECK: %{{[0-9]+}} = apply
// CHECK: %{{[0-9]+}} = apply
// CHECK-LABEL:} // end sil function 'dont_cse_readsome_apply'
sil [ossa] @dont_cse_readsome_apply : $@convention(thin) (Int64) -> Int64 {
bb0(%0 : $Int64):
%2 = function_ref @readsome : $@convention(thin) (Int64, Int64) -> Int64
%3 = integer_literal $Builtin.Int64, 3
%4 = struct $Int64 (%3 : $Builtin.Int64)
%5 = apply %2(%0, %4) : $@convention(thin) (Int64, Int64) -> Int64
%6 = apply %2(%0, %4) : $@convention(thin) (Int64, Int64) -> Int64
%7 = struct_extract %5 : $Int64, #Int64._value
%8 = struct_extract %6 : $Int64, #Int64._value
%9 = integer_literal $Builtin.Int1, 0
%10 = builtin "sadd_with_overflow_Int64"(%7 : $Builtin.Int64, %8 : $Builtin.Int64, %9 : $Builtin.Int1) : $(Builtin.Int64, Builtin.Int1)
%11 = tuple_extract %10 : $(Builtin.Int64, Builtin.Int1), 0
%14 = struct $Int64 (%11 : $Builtin.Int64)
return %14 : $Int64
}
// CHECK-LABEL: sil [ossa] @cse_retain_only_apply :
// CHECK: apply
// CHECK-NOT: apply
// CHECK-LABEL:} // end sil function 'cse_retain_only_apply'
sil [ossa] @cse_retain_only_apply : $@convention(thin) () -> () {
bb0:
%f = function_ref @retain_only : $@convention(thin) () -> @owned XX
%a1 = apply %f() : $@convention(thin) () -> @owned XX
%a2 = apply %f() : $@convention(thin) () -> @owned XX
destroy_value %a1 : $XX
destroy_value %a2 : $XX
%r = tuple()
return %r : $()
}
class Klass {
}
sil [ossa] @identity_owned : $@convention(thin) (@owned Klass) -> @owned Klass {
bb0(%0 : @owned $Klass):
return %0 : $Klass
}
// CHECK-LABEL: sil [ossa] @apply_nontrivial_test1 :
// CHECK: apply
// CHECK-NOT: apply
// CHECK-LABEL:} // end sil function 'apply_nontrivial_test1'
sil [ossa] @apply_nontrivial_test1 : $@convention(thin) (@owned Klass) -> () {
bb0(%0 : @owned $Klass):
%copy0 = copy_value %0 : $Klass
%f = function_ref @identity_owned : $@convention(thin) (@owned Klass) -> @owned Klass
%a1 = apply %f(%0) : $@convention(thin) (@owned Klass) -> @owned Klass
%a2 = apply %f(%copy0) : $@convention(thin) (@owned Klass) -> @owned Klass
destroy_value %a1 : $Klass
destroy_value %a2 : $Klass
%r = tuple()
return %r : $()
}
sil [ossa] @identity_guaranteed : $@convention(thin) (@guaranteed Klass) -> @owned Klass {
bb0(%0 : @guaranteed $Klass):
%copy = copy_value %0 : $Klass
return %copy : $Klass
}
// CHECK-LABEL: sil [ossa] @apply_nontrivial_test2 :
// CHECK: apply
// CHECK-NOT: apply
// CHECK-LABEL:} // end sil function 'apply_nontrivial_test2'
sil [ossa] @apply_nontrivial_test2 : $@convention(thin) (@guaranteed Klass) -> () {
bb0(%0 : @guaranteed $Klass):
%f = function_ref @identity_guaranteed : $@convention(thin) (@guaranteed Klass) -> @owned Klass
%a1 = apply %f(%0) : $@convention(thin) (@guaranteed Klass) -> @owned Klass
%a2 = apply %f(%0) : $@convention(thin) (@guaranteed Klass) -> @owned Klass
destroy_value %a1 : $Klass
destroy_value %a2 : $Klass
%r = tuple()
return %r : $()
}
// CHECK-LABEL: sil [ossa] @apply_nontrivial_test3 :
// CHECK: apply
// CHECK-NOT: apply
// CHECK-LABEL:} // end sil function 'apply_nontrivial_test3'
sil [ossa] @apply_nontrivial_test3 : $@convention(thin) (@owned Klass) -> () {
bb0(%0 : @owned $Klass):
%copy0 = copy_value %0 : $Klass
%f = function_ref @identity_owned : $@convention(thin) (@owned Klass) -> @owned Klass
%a2 = apply %f(%copy0) : $@convention(thin) (@owned Klass) -> @owned Klass
%a1 = apply %f(%0) : $@convention(thin) (@owned Klass) -> @owned Klass
destroy_value %a1 : $Klass
destroy_value %a2 : $Klass
%r = tuple()
return %r : $()
}
|