|
| 1 | +// RUN: %target-sil-opt -enable-sil-verify-all %s -temp-lvalue-opt | %FileCheck %s |
| 2 | + |
| 3 | +import Swift |
| 4 | +import Builtin |
| 5 | + |
| 6 | +// CHECK-LABEL: sil [ossa] @test_enum_with_initialize |
| 7 | +// CHECK: bb0(%0 : $*Optional<Any>, %1 : $*Any): |
| 8 | +// CHECK-NEXT: [[E:%[0-9]+]] = init_enum_data_addr %0 |
| 9 | +// CHECK-NEXT: copy_addr [take] %1 to [initialization] [[E]] |
| 10 | +// CHECK-NEXT: inject_enum_addr %0 : $*Optional<Any>, #Optional.some!enumelt |
| 11 | +// CHECK-NOT: copy_addr |
| 12 | +// CHECK: } // end sil function 'test_enum_with_initialize' |
| 13 | +sil [ossa] @test_enum_with_initialize : $@convention(thin) (@in Any) -> @out Optional<Any> { |
| 14 | +bb0(%0 : $*Optional<Any>, %1 : $*Any): |
| 15 | + %2 = alloc_stack $Optional<Any> |
| 16 | + %3 = init_enum_data_addr %2 : $*Optional<Any>, #Optional.some!enumelt |
| 17 | + copy_addr [take] %1 to [initialization] %3 : $*Any |
| 18 | + inject_enum_addr %2 : $*Optional<Any>, #Optional.some!enumelt |
| 19 | + copy_addr [take] %2 to [initialization] %0 : $*Optional<Any> |
| 20 | + dealloc_stack %2 : $*Optional<Any> |
| 21 | + %6 = tuple () |
| 22 | + return %6 : $() |
| 23 | +} |
| 24 | + |
| 25 | +// CHECK-LABEL: sil [ossa] @test_enum_without_initialize |
| 26 | +// CHECK: bb0(%0 : $*Optional<Any>, %1 : $*Any): |
| 27 | +// CHECK-NEXT: destroy_addr %0 |
| 28 | +// CHECK-NEXT: [[E:%[0-9]+]] = init_enum_data_addr %0 |
| 29 | +// CHECK-NEXT: copy_addr [take] %1 to [initialization] [[E]] |
| 30 | +// CHECK-NEXT: inject_enum_addr %0 : $*Optional<Any>, #Optional.some!enumelt |
| 31 | +// CHECK-NOT: copy_addr |
| 32 | +// CHECK: } // end sil function 'test_enum_without_initialize' |
| 33 | +sil [ossa] @test_enum_without_initialize : $@convention(thin) (@inout Optional<Any>, @in Any) -> () { |
| 34 | +bb0(%0 : $*Optional<Any>, %1 : $*Any): |
| 35 | + %2 = alloc_stack $Optional<Any> |
| 36 | + %3 = init_enum_data_addr %2 : $*Optional<Any>, #Optional.some!enumelt |
| 37 | + copy_addr [take] %1 to [initialization] %3 : $*Any |
| 38 | + inject_enum_addr %2 : $*Optional<Any>, #Optional.some!enumelt |
| 39 | + copy_addr [take] %2 to %0 : $*Optional<Any> |
| 40 | + dealloc_stack %2 : $*Optional<Any> |
| 41 | + %6 = tuple () |
| 42 | + return %6 : $() |
| 43 | +} |
| 44 | + |
| 45 | +protocol Proto {} |
| 46 | + |
| 47 | +struct ConformingStruct : Proto { |
| 48 | + @_hasStorage let a: Any |
| 49 | +} |
| 50 | + |
| 51 | +// CHECK-LABEL: sil [ossa] @test_existential |
| 52 | +// CHECK: bb0(%0 : $*Proto, %1 : $*ConformingStruct): |
| 53 | +// CHECK-NEXT: [[E:%[0-9]+]] = init_existential_addr %0 |
| 54 | +// CHECK-NEXT: copy_addr [take] %1 to [initialization] [[E]] |
| 55 | +// CHECK-NOT: copy_addr |
| 56 | +// CHECK: } // end sil function 'test_existential' |
| 57 | +sil [ossa] @test_existential : $@convention(thin) (@in ConformingStruct) -> @out Proto { |
| 58 | +bb0(%0 : $*Proto, %1 : $*ConformingStruct): |
| 59 | + %2 = alloc_stack $Proto |
| 60 | + %3 = init_existential_addr %2 : $*Proto, $ConformingStruct |
| 61 | + copy_addr [take] %1 to [initialization] %3 : $*ConformingStruct |
| 62 | + copy_addr [take] %2 to [initialization] %0 : $*Proto |
| 63 | + dealloc_stack %2 : $*Proto |
| 64 | + %6 = tuple () |
| 65 | + return %6 : $() |
| 66 | +} |
| 67 | + |
| 68 | +enum Either<Left, Right> { |
| 69 | + case left(Left), right(Right) |
| 70 | +} |
| 71 | + |
| 72 | +public struct TestStruct { |
| 73 | + @_hasStorage var e: Either<AddressOnlyPayload, Int> |
| 74 | +} |
| 75 | + |
| 76 | +struct AddressOnlyPayload { |
| 77 | + @_hasStorage let a: Any |
| 78 | + @_hasStorage let i: Int |
| 79 | +} |
| 80 | + |
| 81 | +// There should only be a single copy_addr after optimization. |
| 82 | +// |
| 83 | +// CHECK-LABEL: sil [ossa] @test_initialize_struct |
| 84 | +// CHECK: bb0(%0 : $*TestStruct, %1 : $*Any, %2 : $Int): |
| 85 | +// CHECK-NEXT: [[E:%[0-9]+]] = struct_element_addr %0 |
| 86 | +// CHECK-NEXT: [[LEFT:%[0-9]+]] = init_enum_data_addr [[E]] |
| 87 | +// CHECK-NEXT: [[A:%[0-9]+]] = struct_element_addr [[LEFT]] |
| 88 | +// CHECK-NEXT: copy_addr [take] %1 to [initialization] [[A]] |
| 89 | +// CHECK-NEXT: [[I:%[0-9]+]] = struct_element_addr [[LEFT]] |
| 90 | +// CHECK-NEXT: store %2 to [trivial] [[I]] |
| 91 | +// CHECK-NEXT: inject_enum_addr [[E]] |
| 92 | +// CHECK-NOT: copy_addr |
| 93 | +// CHECK: } // end sil function 'test_initialize_struct' |
| 94 | +sil [ossa] @test_initialize_struct : $@convention(method) (@in Any, Int) -> @out TestStruct { |
| 95 | +bb0(%0 : $*TestStruct, %1 : $*Any, %2 : $Int): |
| 96 | + %3 = alloc_stack $TestStruct |
| 97 | + %4 = alloc_stack $Either<AddressOnlyPayload, Int> |
| 98 | + %5 = alloc_stack $AddressOnlyPayload |
| 99 | + %6 = struct_element_addr %5 : $*AddressOnlyPayload, #AddressOnlyPayload.a |
| 100 | + copy_addr [take] %1 to [initialization] %6 : $*Any |
| 101 | + %8 = struct_element_addr %5 : $*AddressOnlyPayload, #AddressOnlyPayload.i |
| 102 | + store %2 to [trivial] %8 : $*Int |
| 103 | + %10 = init_enum_data_addr %4 : $*Either<AddressOnlyPayload, Int>, #Either.left!enumelt |
| 104 | + copy_addr [take] %5 to [initialization] %10 : $*AddressOnlyPayload |
| 105 | + inject_enum_addr %4 : $*Either<AddressOnlyPayload, Int>, #Either.left!enumelt |
| 106 | + dealloc_stack %5 : $*AddressOnlyPayload |
| 107 | + %14 = struct_element_addr %3 : $*TestStruct, #TestStruct.e |
| 108 | + copy_addr [take] %4 to [initialization] %14 : $*Either<AddressOnlyPayload, Int> |
| 109 | + dealloc_stack %4 : $*Either<AddressOnlyPayload, Int> |
| 110 | + copy_addr %3 to [initialization] %0 : $*TestStruct |
| 111 | + destroy_addr %3 : $*TestStruct |
| 112 | + dealloc_stack %3 : $*TestStruct |
| 113 | + %20 = tuple () |
| 114 | + return %20 : $() |
| 115 | +} |
| 116 | + |
| 117 | +// CHECK-LABEL: sil [ossa] @bail_on_write_to_dest |
| 118 | +// CHECK: alloc_stack |
| 119 | +// CHECK: copy_addr |
| 120 | +// CHECK: copy_addr |
| 121 | +// CHECK: } // end sil function 'bail_on_write_to_dest' |
| 122 | +sil [ossa] @bail_on_write_to_dest : $@convention(thin) (@inout Optional<Any>, @in Any) -> () { |
| 123 | +bb0(%0 : $*Optional<Any>, %1 : $*Any): |
| 124 | + %2 = alloc_stack $Optional<Any> |
| 125 | + %3 = init_enum_data_addr %2 : $*Optional<Any>, #Optional.some!enumelt |
| 126 | + copy_addr [take] %1 to [initialization] %3 : $*Any |
| 127 | + inject_enum_addr %2 : $*Optional<Any>, #Optional.some!enumelt |
| 128 | + destroy_addr %0 : $*Optional<Any> |
| 129 | + copy_addr [take] %2 to [initialization] %0 : $*Optional<Any> |
| 130 | + dealloc_stack %2 : $*Optional<Any> |
| 131 | + %6 = tuple () |
| 132 | + return %6 : $() |
| 133 | +} |
| 134 | + |
| 135 | +// CHECK-LABEL: sil [ossa] @write_to_dest_ok_if_before_liferange |
| 136 | +// CHECK: bb0(%0 : $*Optional<Any>, %1 : $*Any): |
| 137 | +// CHECK-NEXT: destroy_addr |
| 138 | +// CHECK-NEXT: init_enum_data_addr |
| 139 | +// CHECK-NEXT: copy_addr |
| 140 | +// CHECK-NEXT: inject_enum_addr |
| 141 | +// CHECK-NOT: copy_addr |
| 142 | +// CHECK: } // end sil function 'write_to_dest_ok_if_before_liferange' |
| 143 | +sil [ossa] @write_to_dest_ok_if_before_liferange : $@convention(thin) (@inout Optional<Any>, @in Any) -> () { |
| 144 | +bb0(%0 : $*Optional<Any>, %1 : $*Any): |
| 145 | + %2 = alloc_stack $Optional<Any> |
| 146 | + destroy_addr %0 : $*Optional<Any> |
| 147 | + %3 = init_enum_data_addr %2 : $*Optional<Any>, #Optional.some!enumelt |
| 148 | + copy_addr [take] %1 to [initialization] %3 : $*Any |
| 149 | + inject_enum_addr %2 : $*Optional<Any>, #Optional.some!enumelt |
| 150 | + copy_addr [take] %2 to [initialization] %0 : $*Optional<Any> |
| 151 | + dealloc_stack %2 : $*Optional<Any> |
| 152 | + %6 = tuple () |
| 153 | + return %6 : $() |
| 154 | +} |
| 155 | + |
| 156 | +enum Enum { |
| 157 | + case A(Optional<Any>), B |
| 158 | +} |
| 159 | + |
| 160 | +struct StructWithEnum : Proto { |
| 161 | + @_hasStorage let e: Enum |
| 162 | +} |
| 163 | + |
| 164 | +// CHECK-LABEL: sil [ossa] @move_projections |
| 165 | +// CHECK: bb0(%0 : $*Proto, %1 : $*Any): |
| 166 | +// CHECK-NEXT: [[S:%[0-9]+]] = init_existential_addr %0 : $*Proto, $StructWithEnum |
| 167 | +// CHECK-NEXT: [[E:%[0-9]+]] = struct_element_addr [[S]] : $*StructWithEnum, #StructWithEnum.e |
| 168 | +// CHECK-NEXT: [[ENUMA:%[0-9]+]] = init_enum_data_addr [[E]] : $*Enum, #Enum.A!enumelt |
| 169 | +// CHECK-NEXT: [[OPTIONAL:%[0-9]+]] = init_enum_data_addr [[ENUMA]] : $*Optional<Any>, #Optional.some!enumelt |
| 170 | +// CHECK-NEXT: copy_addr [take] %1 to [initialization] [[OPTIONAL]] : $*Any |
| 171 | +// CHECK-NEXT: inject_enum_addr [[ENUMA]] : $*Optional<Any>, #Optional.some!enumelt |
| 172 | +// CHECK-NEXT: inject_enum_addr [[E]] : $*Enum, #Enum.A!enumelt |
| 173 | +// CHECK-NOT: copy_addr |
| 174 | +// CHECK: } // end sil function 'move_projections' |
| 175 | +sil [ossa] @move_projections : $@convention(thin) (@in Any) -> @out Proto { |
| 176 | +bb0(%0 : $*Proto, %1 : $*Any): |
| 177 | + %2 = alloc_stack $Optional<Any> |
| 178 | + %3 = init_enum_data_addr %2 : $*Optional<Any>, #Optional.some!enumelt |
| 179 | + copy_addr [take] %1 to [initialization] %3 : $*Any |
| 180 | + inject_enum_addr %2 : $*Optional<Any>, #Optional.some!enumelt |
| 181 | + %4 = init_existential_addr %0 : $*Proto, $StructWithEnum |
| 182 | + %5 = struct_element_addr %4 : $*StructWithEnum, #StructWithEnum.e |
| 183 | + %6 = init_enum_data_addr %5 : $*Enum, #Enum.A!enumelt |
| 184 | + copy_addr [take] %2 to [initialization] %6 : $*Optional<Any> |
| 185 | + inject_enum_addr %5 : $*Enum, #Enum.A!enumelt |
| 186 | + dealloc_stack %2 : $*Optional<Any> |
| 187 | + %10 = tuple () |
| 188 | + return %10 : $() |
| 189 | +} |
| 190 | + |
| 191 | +// CHECK-LABEL: sil [ossa] @cant_move_projections |
| 192 | +// CHECK: alloc_stack |
| 193 | +// CHECK: copy_addr |
| 194 | +// CHECK: load |
| 195 | +// CHECK: copy_addr |
| 196 | +// CHECK: } // end sil function 'cant_move_projections' |
| 197 | +sil [ossa] @cant_move_projections : $@convention(thin) (@in Any, @in_guaranteed Builtin.RawPointer) -> () { |
| 198 | +bb0(%0 : $*Any, %1 : $*Builtin.RawPointer): |
| 199 | + %2 = alloc_stack $Optional<Any> |
| 200 | + %3 = init_enum_data_addr %2 : $*Optional<Any>, #Optional.some!enumelt |
| 201 | + copy_addr [take] %0 to [initialization] %3 : $*Any |
| 202 | + inject_enum_addr %2 : $*Optional<Any>, #Optional.some!enumelt |
| 203 | + %4 = load [trivial] %1 : $*Builtin.RawPointer |
| 204 | + %5 = pointer_to_address %4 : $Builtin.RawPointer to $*Optional<Any> |
| 205 | + copy_addr [take] %2 to [initialization] %5 : $*Optional<Any> |
| 206 | + dealloc_stack %2 : $*Optional<Any> |
| 207 | + %10 = tuple () |
| 208 | + return %10 : $() |
| 209 | +} |
| 210 | + |
| 211 | +sil [ossa] @init_optional : $@convention(thin) () -> @out Optional<Any> |
| 212 | + |
| 213 | +// CHECK-LABEL: sil [ossa] @instructions_after_copy_addr |
| 214 | +// CHECK: alloc_stack |
| 215 | +// CHECK: copy_addr |
| 216 | +// CHECK: copy_addr |
| 217 | +// CHECK: apply |
| 218 | +// CHECK: } // end sil function 'instructions_after_copy_addr' |
| 219 | +sil [ossa] @instructions_after_copy_addr : $@convention(thin) (@in Any) -> @out Optional<Any> { |
| 220 | +bb0(%0 : $*Optional<Any>, %1 : $*Any): |
| 221 | + %2 = alloc_stack $Optional<Any> |
| 222 | + %3 = init_enum_data_addr %2 : $*Optional<Any>, #Optional.some!enumelt |
| 223 | + copy_addr [take] %1 to [initialization] %3 : $*Any |
| 224 | + inject_enum_addr %2 : $*Optional<Any>, #Optional.some!enumelt |
| 225 | + copy_addr [take] %2 to [initialization] %0 : $*Optional<Any> |
| 226 | + %4 = function_ref @init_optional : $@convention(thin) () -> @out Optional<Any> |
| 227 | + %5 = apply %4(%2) : $@convention(thin) () -> @out Optional<Any> |
| 228 | + destroy_addr %2 : $*Optional<Any> |
| 229 | + dealloc_stack %2 : $*Optional<Any> |
| 230 | + %6 = tuple () |
| 231 | + return %6 : $() |
| 232 | +} |
| 233 | + |
| 234 | +// CHECK-LABEL: sil [ossa] @dont_optimize_swap |
| 235 | +// CHECK: alloc_stack |
| 236 | +// CHECK: copy_addr |
| 237 | +// CHECK: copy_addr |
| 238 | +// CHECK: copy_addr |
| 239 | +// CHECK: dealloc_stack |
| 240 | +// CHECK: } // end sil function 'dont_optimize_swap' |
| 241 | +sil [ossa] @dont_optimize_swap : $@convention(thin) <T> (@inout T, @inout T) -> () { |
| 242 | +bb0(%0 : $*T, %1 : $*T): |
| 243 | + %2 = alloc_stack $T |
| 244 | + copy_addr [take] %0 to [initialization] %2 : $*T |
| 245 | + copy_addr [take] %1 to [initialization] %0 : $*T |
| 246 | + copy_addr [take] %2 to [initialization] %1 : $*T |
| 247 | + dealloc_stack %2 : $*T |
| 248 | + %78 = tuple () |
| 249 | + return %78 : $() |
| 250 | +} |
| 251 | + |
0 commit comments