|
| 1 | +// RUN: %target-sil-opt -enable-sil-verify-all -sil-combine %s | %FileCheck %s |
| 2 | + |
| 3 | +// This file tests sil combine's canonicalization of memory. |
| 4 | + |
| 5 | +class Klass {} |
| 6 | + |
| 7 | +//////////////////////////// |
| 8 | +// Store Canonicalization // |
| 9 | +//////////////////////////// |
| 10 | + |
| 11 | +// We canonicalize stores of fields of single element structs into stores of the |
| 12 | +// struct itself. The two are equivalent. |
| 13 | + |
| 14 | +struct SingleEltStruct { |
| 15 | + var k: Klass |
| 16 | +} |
| 17 | + |
| 18 | +// CHECK-LABEL: sil [ossa] @promote_initialization_of_single_elt_struct : $@convention(thin) (@owned Klass) -> @owned SingleEltStruct { |
| 19 | +// CHECK: bb0([[ARG:%.*]] : @owned $Klass): |
| 20 | +// CHECK: [[STACK:%.*]] = alloc_stack $SingleEltStruct |
| 21 | +// CHECK: [[STRUCT_ARG:%.*]] = struct $SingleEltStruct ([[ARG]] : $Klass) |
| 22 | +// CHECK: store [[STRUCT_ARG]] to [init] [[STACK]] |
| 23 | +// CHECK: dealloc_stack [[STACK]] |
| 24 | +// CHECK: } // end sil function 'promote_initialization_of_single_elt_struct' |
| 25 | +sil [ossa] @promote_initialization_of_single_elt_struct : $@convention(thin) (@owned Klass) -> @owned SingleEltStruct { |
| 26 | +bb0(%0 : @owned $Klass): |
| 27 | + %1 = alloc_stack $SingleEltStruct |
| 28 | + %2 = struct_element_addr %1 : $*SingleEltStruct, #SingleEltStruct.k |
| 29 | + store %0 to [init] %2 : $*Klass |
| 30 | + %3 = load [take] %1 : $*SingleEltStruct |
| 31 | + dealloc_stack %1 : $*SingleEltStruct |
| 32 | + return %3 : $SingleEltStruct |
| 33 | +} |
| 34 | + |
| 35 | +struct RecursiveSingleEltStruct { |
| 36 | + var field: RecursiveSingleEltStructField |
| 37 | +} |
| 38 | + |
| 39 | +struct RecursiveSingleEltStructField { |
| 40 | + var k: Klass |
| 41 | +} |
| 42 | + |
| 43 | +// CHECK-LABEL: sil [ossa] @promote_initialization_of_recursive_single_elt_struct : $@convention(thin) (@owned Klass) -> () { |
| 44 | +// CHECK: bb0([[ARG:%.*]] : @owned $Klass): |
| 45 | +// CHECK: [[STACK:%.*]] = alloc_stack $RecursiveSingleEltStruct |
| 46 | +// CHECK: [[STRUCT_ARG_1:%.*]] = struct $RecursiveSingleEltStructField ([[ARG]] : $Klass) |
| 47 | +// CHECK: [[STRUCT_ARG_2:%.*]] = struct $RecursiveSingleEltStruct ([[STRUCT_ARG_1]] : $RecursiveSingleEltStructField) |
| 48 | +// CHECK: store [[STRUCT_ARG_2]] to [init] [[STACK]] |
| 49 | +// CHECK: dealloc_stack [[STACK]] |
| 50 | +// CHECK: } // end sil function 'promote_initialization_of_recursive_single_elt_struct' |
| 51 | +sil [ossa] @promote_initialization_of_recursive_single_elt_struct : $@convention(thin) (@owned Klass) -> () { |
| 52 | +bb0(%0 : @owned $Klass): |
| 53 | + %1 = alloc_stack $RecursiveSingleEltStruct |
| 54 | + %2 = struct_element_addr %1 : $*RecursiveSingleEltStruct, #RecursiveSingleEltStruct.field |
| 55 | + %3 = struct_element_addr %2 : $*RecursiveSingleEltStructField, #RecursiveSingleEltStructField.k |
| 56 | + store %0 to [init] %3 : $*Klass |
| 57 | + %4 = load [take] %1 : $*RecursiveSingleEltStruct |
| 58 | + destroy_value %4 : $RecursiveSingleEltStruct |
| 59 | + dealloc_stack %1 : $*RecursiveSingleEltStruct |
| 60 | + %9999 = tuple() |
| 61 | + return %9999 : $() |
| 62 | +} |
| 63 | + |
| 64 | +struct MultipleFieldStruct { |
| 65 | + var k: Klass |
| 66 | + var field: RecursiveSingleEltStructField |
| 67 | +} |
| 68 | + |
| 69 | +// CHECK-LABEL: sil [ossa] @only_promote_as_far_as_have_single_elts : $@convention(thin) (@owned Klass) -> () { |
| 70 | +// CHECK: bb0([[ARG:%.*]] : |
| 71 | +// CHECK: [[STACK:%.*]] = alloc_stack $MultipleFieldStruct |
| 72 | +// CHECK: [[MULTIPLE_FIELD_SEA:%.*]] = struct_element_addr [[STACK]] |
| 73 | +// CHECK: [[VALUE:%.*]] = struct $RecursiveSingleEltStructField ([[ARG]] : $Klass) |
| 74 | +// CHECK: store [[VALUE]] to [init] [[MULTIPLE_FIELD_SEA]] |
| 75 | +// CHECK: dealloc_stack [[STACK]] |
| 76 | +// CHECK: } // end sil function 'only_promote_as_far_as_have_single_elts' |
| 77 | +sil [ossa] @only_promote_as_far_as_have_single_elts : $@convention(thin) (@owned Klass) -> () { |
| 78 | +bb0(%0 : @owned $Klass): |
| 79 | + %1 = alloc_stack $MultipleFieldStruct |
| 80 | + %2 = struct_element_addr %1 : $*MultipleFieldStruct, #MultipleFieldStruct.field |
| 81 | + %2a = struct_element_addr %1 : $*MultipleFieldStruct, #MultipleFieldStruct.k |
| 82 | + %3 = struct_element_addr %2 : $*RecursiveSingleEltStructField, #RecursiveSingleEltStructField.k |
| 83 | + %0a = copy_value %0 : $Klass |
| 84 | + store %0a to [init] %2a : $*Klass |
| 85 | + store %0 to [init] %3 : $*Klass |
| 86 | + destroy_addr %1 : $*MultipleFieldStruct |
| 87 | + dealloc_stack %1 : $*MultipleFieldStruct |
| 88 | + %9999 = tuple() |
| 89 | + return %9999 : $() |
| 90 | +} |
| 91 | + |
| 92 | +// CHECK-LABEL: sil [ossa] @perform_no_work_if_multiple_fields : $@convention(thin) (@owned RecursiveSingleEltStructField, @owned Klass) -> () { |
| 93 | +// CHECK: bb0([[ARG:%[0-9][0-9]*]] : @owned $RecursiveSingleEltStructField, [[ARG2:%.*]] : @owned |
| 94 | +// CHECK: [[STACK:%.*]] = alloc_stack $MultipleFieldStruct |
| 95 | +// CHECK: [[MULTIPLE_FIELD_SEA:%.*]] = struct_element_addr [[STACK]] |
| 96 | +// CHECK: store [[ARG]] to [init] [[MULTIPLE_FIELD_SEA]] |
| 97 | +// CHECK: dealloc_stack [[STACK]] |
| 98 | +// CHECK: } // end sil function 'perform_no_work_if_multiple_fields' |
| 99 | +sil [ossa] @perform_no_work_if_multiple_fields : $@convention(thin) (@owned RecursiveSingleEltStructField, @owned Klass) -> () { |
| 100 | +bb0(%0 : @owned $RecursiveSingleEltStructField, %0a : @owned $Klass): |
| 101 | + %1 = alloc_stack $MultipleFieldStruct |
| 102 | + %2 = struct_element_addr %1 : $*MultipleFieldStruct, #MultipleFieldStruct.field |
| 103 | + %2a = struct_element_addr %1 : $*MultipleFieldStruct, #MultipleFieldStruct.k |
| 104 | + store %0a to [init] %2a : $*Klass |
| 105 | + store %0 to [init] %2 : $*RecursiveSingleEltStructField |
| 106 | + destroy_addr %1 : $*MultipleFieldStruct |
| 107 | + dealloc_stack %1 : $*MultipleFieldStruct |
| 108 | + %9999 = tuple() |
| 109 | + return %9999 : $() |
| 110 | +} |
| 111 | + |
| 112 | +// CHECK-LABEL: sil [ossa] @only_promote_while_we_have_sea : $@convention(thin) (@owned Klass) -> () { |
| 113 | +// CHECK: bb0([[ARG:%.*]] : |
| 114 | +// CHECK: [[STACK:%.*]] = alloc_stack $(Klass, RecursiveSingleEltStructField) |
| 115 | +// CHECK: [[TUPLE_ADDR:%.*]] = tuple_element_addr [[STACK]] |
| 116 | +// CHECK: [[VALUE:%.*]] = struct $RecursiveSingleEltStructField ([[ARG]] : $Klass) |
| 117 | +// CHECK: store [[VALUE]] to [init] [[TUPLE_ADDR]] |
| 118 | +// CHECK: dealloc_stack [[STACK]] |
| 119 | +// CHECK: } // end sil function 'only_promote_while_we_have_sea' |
| 120 | +sil [ossa] @only_promote_while_we_have_sea : $@convention(thin) (@owned Klass) -> () { |
| 121 | +bb0(%0 : @owned $Klass): |
| 122 | + %1 = alloc_stack $(Klass, RecursiveSingleEltStructField) |
| 123 | + %2 = tuple_element_addr %1 : $*(Klass, RecursiveSingleEltStructField), 1 |
| 124 | + %2a = tuple_element_addr %1 : $*(Klass, RecursiveSingleEltStructField), 0 |
| 125 | + %0a = copy_value %0 : $Klass |
| 126 | + store %0a to [init] %2a : $*Klass |
| 127 | + %3 = struct_element_addr %2 : $*RecursiveSingleEltStructField, #RecursiveSingleEltStructField.k |
| 128 | + store %0 to [init] %3 : $*Klass |
| 129 | + destroy_addr %1 : $*(Klass, RecursiveSingleEltStructField) |
| 130 | + dealloc_stack %1 : $*(Klass, RecursiveSingleEltStructField) |
| 131 | + %9999 = tuple() |
| 132 | + return %9999 : $() |
| 133 | +} |
0 commit comments