|
| 1 | +// RUN: %target-sil-opt -enable-sil-verify-all %s -simplify-cfg -enable-ossa-simplify-cfg -sil-combine -simplify-cfg -sil-combine | %FileCheck %s |
| 2 | + |
| 3 | +// These tests require both SimplifyCFG and SILCombine |
| 4 | + |
| 5 | +sil_stage canonical |
| 6 | + |
| 7 | +import Builtin |
| 8 | +import Swift |
| 9 | + |
| 10 | +sil @external_f1 : $@convention(thin) () -> () |
| 11 | +sil @external_f2 : $@convention(thin) () -> () |
| 12 | +sil @external_f3 : $@convention(thin) () -> () |
| 13 | +sil @external_f4 : $@convention(thin) () -> () |
| 14 | + |
| 15 | +// CHECK-LABEL: sil [ossa] @select_enum_dominance_simplification : $@convention(thin) (Optional<Int32>) -> () { |
| 16 | +// CHECK-NOT: external_f2 |
| 17 | +// CHECK-NOT: external_f4 |
| 18 | +// CHECK: bb3: |
| 19 | +// CHECK-NEXT: tuple |
| 20 | +// CHECK-NEXT: return |
| 21 | +sil [ossa] @select_enum_dominance_simplification : $@convention(thin) (Optional<Int32>) -> () { |
| 22 | +bb0(%0 : $Optional<Int32>): |
| 23 | + %t = integer_literal $Builtin.Int1, 1 |
| 24 | + %f = integer_literal $Builtin.Int1, 0 |
| 25 | + %1 = select_enum %0 : $Optional<Int32>, case #Optional.some!enumelt: %t, case #Optional.none!enumelt: %f : $Builtin.Int1 |
| 26 | + cond_br %1, bb1, bb2 |
| 27 | + |
| 28 | +bb1: |
| 29 | + %2 = select_enum %0 : $Optional<Int32>, case #Optional.some!enumelt: %t, case #Optional.none!enumelt: %f : $Builtin.Int1 |
| 30 | + cond_br %2, bb3, bb4 |
| 31 | + |
| 32 | +bb2: |
| 33 | + %3 = select_enum %0 : $Optional<Int32>, case #Optional.some!enumelt: %f, case #Optional.none!enumelt: %t : $Builtin.Int1 |
| 34 | + cond_br %3, bb5, bb6 |
| 35 | + |
| 36 | +bb3: |
| 37 | + %f1 = function_ref @external_f1 : $@convention(thin) () -> () |
| 38 | + apply %f1() : $@convention(thin) () -> () |
| 39 | + br bb7 |
| 40 | + |
| 41 | +bb4: |
| 42 | + %f2 = function_ref @external_f2 : $@convention(thin) () -> () |
| 43 | + apply %f2() : $@convention(thin) () -> () |
| 44 | + br bb7 |
| 45 | + |
| 46 | +bb5: |
| 47 | + %f3 = function_ref @external_f3 : $@convention(thin) () -> () |
| 48 | + apply %f3() : $@convention(thin) () -> () |
| 49 | + br bb7 |
| 50 | + |
| 51 | +bb6: |
| 52 | + %f4 = function_ref @external_f4 : $@convention(thin) () -> () |
| 53 | + apply %f4() : $@convention(thin) () -> () |
| 54 | + br bb7 |
| 55 | + |
| 56 | +bb7: |
| 57 | + %9999 = tuple() |
| 58 | + return %9999 : $() |
| 59 | +} |
| 60 | + |
| 61 | +// CHECK-LABEL: sil [ossa] @switch_enum_dominates_select_enum : $@convention(thin) (Optional<Int32>) -> () { |
| 62 | +// CHECK-NOT: external_f2 |
| 63 | +// CHECK: bb3: |
| 64 | +// CHECK-NEXT: tuple |
| 65 | +// CHECK-NEXT: return |
| 66 | +sil [ossa] @switch_enum_dominates_select_enum : $@convention(thin) (Optional<Int32>) -> () { |
| 67 | +bb0(%0 : $Optional<Int32>): |
| 68 | + %t = integer_literal $Builtin.Int1, 1 |
| 69 | + %f = integer_literal $Builtin.Int1, 0 |
| 70 | + switch_enum %0 : $Optional<Int32>, case #Optional.none!enumelt: bb4, case #Optional.some!enumelt: bb1 |
| 71 | + |
| 72 | +bb1(%arg1 : $Int32): |
| 73 | + %c = select_enum %0 : $Optional<Int32>, case #Optional.none!enumelt: %f, case #Optional.some!enumelt: %t : $Builtin.Int1 |
| 74 | + cond_br %c, bb2, bb3 |
| 75 | + |
| 76 | +bb2: |
| 77 | + %f1 = function_ref @external_f1 : $@convention(thin) () -> () |
| 78 | + apply %f1() : $@convention(thin) () -> () |
| 79 | + br bb5 |
| 80 | + |
| 81 | +bb3: |
| 82 | + %f2 = function_ref @external_f2 : $@convention(thin) () -> () |
| 83 | + apply %f2() : $@convention(thin) () -> () |
| 84 | + br bb5 |
| 85 | + |
| 86 | +bb4: |
| 87 | + %f3 = function_ref @external_f3 : $@convention(thin) () -> () |
| 88 | + apply %f3() : $@convention(thin) () -> () |
| 89 | + br bb5 |
| 90 | + |
| 91 | +bb5: |
| 92 | + %9999 = tuple() |
| 93 | + return %9999 : $() |
| 94 | +} |
| 95 | + |
| 96 | +// CHECK-LABEL: sil [ossa] @switch_enum_dominates_select_enum2 : $@convention(thin) (Optional<Int32>) -> Builtin.Int32 { |
| 97 | +// CHECK-DAG: [[L2:%[0-9]+]] = integer_literal {{.*}}, 2 |
| 98 | +// CHECK-DAG: [[L1:%[0-9]+]] = integer_literal {{.*}}, 1 |
| 99 | +// CHECK: [[R:%[0-9]+]] = select_enum %0 : $Optional<Int32>, case #Optional.none!enumelt: [[L2]], case #Optional.some!enumelt: [[L1]] |
| 100 | +// CHECK-NEXT: return [[R]] |
| 101 | +sil [ossa] @switch_enum_dominates_select_enum2 : $@convention(thin) (Optional<Int32>) -> Builtin.Int32 { |
| 102 | +bb0(%0 : $Optional<Int32>): |
| 103 | + %i1 = integer_literal $Builtin.Int32, 1 |
| 104 | + %i0 = integer_literal $Builtin.Int32, 0 |
| 105 | + switch_enum %0 : $Optional<Int32>, case #Optional.none!enumelt: bb2, case #Optional.some!enumelt: bb1 |
| 106 | + |
| 107 | +bb1(%arg1 : $Int32): |
| 108 | + %c = select_enum %0 : $Optional<Int32>, case #Optional.none!enumelt: %i0, case #Optional.some!enumelt: %i1 : $Builtin.Int32 |
| 109 | + br bb3(%c : $Builtin.Int32) |
| 110 | + |
| 111 | +bb2: |
| 112 | + %i2 = integer_literal $Builtin.Int32, 2 |
| 113 | + br bb3(%i2 : $Builtin.Int32) |
| 114 | + |
| 115 | +bb3(%r : $Builtin.Int32): |
| 116 | + return %r : $Builtin.Int32 |
| 117 | +} |
| 118 | + |
| 119 | + |
| 120 | +// CHECK-LABEL: sil [ossa] @cond_br_dominates_cond_fail : $@convention(thin) (Builtin.Int1) -> () { |
| 121 | +// CHECK: bb0(%0 : $Builtin.Int1): |
| 122 | +// CHECK-NEXT: tuple |
| 123 | +// CHECK-NEXT: return |
| 124 | +sil [ossa] @cond_br_dominates_cond_fail : $@convention(thin) (Builtin.Int1) -> () { |
| 125 | +bb0(%0 : $Builtin.Int1): |
| 126 | + cond_br %0, bb2a, bb1 |
| 127 | + |
| 128 | +bb1: |
| 129 | + cond_fail %0 : $Builtin.Int1 |
| 130 | + br bb2 |
| 131 | + |
| 132 | +bb2a: |
| 133 | + br bb2 |
| 134 | + |
| 135 | +bb2: |
| 136 | + %r = tuple() |
| 137 | + return %r : $() |
| 138 | +} |
| 139 | + |
| 140 | +/// CHECK-LABEL: sil [ossa] @select_enum_dominates_switch_enum : $@convention(thin) (Int32) -> Int32 { |
| 141 | +/// The select_enum dominates the switch_enum and knows exactly which element will be |
| 142 | +/// selected. So this test ensures we can remove the switch_enum |
| 143 | +/// CHECK-NOT: switch_enum |
| 144 | +/// CHECK: return |
| 145 | +sil [ossa] @select_enum_dominates_switch_enum : $@convention(thin) (Int32) -> Int32 { |
| 146 | +bb0(%0 : $Int32): |
| 147 | + %1 = integer_literal $Builtin.Int32, 0 // users: %5, %9, %9 |
| 148 | + %2 = integer_literal $Builtin.Int1, -1 // users: %7, %37 |
| 149 | + %3 = struct_extract %0 : $Int32, #Int32._value // users: %5, %13 |
| 150 | + %5 = builtin "cmp_sle_Int32"(%1 : $Builtin.Int32, %3 : $Builtin.Int32) : $Builtin.Int1 // user: %7 |
| 151 | + %7 = builtin "xor_Int1"(%5 : $Builtin.Int1, %2 : $Builtin.Int1) : $Builtin.Int1 // user: %8 |
| 152 | + cond_fail %7 : $Builtin.Int1 // id: %8 |
| 153 | + br bb1(%1 : $Builtin.Int32, %1 : $Builtin.Int32) // id: %9 |
| 154 | + |
| 155 | +bb1(%10 : $Builtin.Int32, %11 : $Builtin.Int32): // Preds: bb0 bb6 |
| 156 | + %13 = builtin "cmp_eq_Int32"(%11 : $Builtin.Int32, %3 : $Builtin.Int32) : $Builtin.Int1 // user: %14 |
| 157 | + cond_br %13, bb2, bb4 // id: %14 |
| 158 | + |
| 159 | +bb2: // Preds: bb1 |
| 160 | + %15 = enum $Optional<Int32>, #Optional.none!enumelt // user: %16 |
| 161 | + br bb3(%11 : $Builtin.Int32, %15 : $Optional<Int32>) // id: %16 |
| 162 | + |
| 163 | +bb3(%17 : $Builtin.Int32, %18 : $Optional<Int32>): // Preds: bb2 bb4 |
| 164 | + %t = integer_literal $Builtin.Int1, 1 |
| 165 | + %f = integer_literal $Builtin.Int1, 0 |
| 166 | + %19 = select_enum %18 : $Optional<Int32>, case #Optional.some!enumelt: %t, case #Optional.none!enumelt: %f : $Builtin.Int1 |
| 167 | + cond_br %19, bb5, bb8 // id: %20 |
| 168 | + |
| 169 | +bb4: // Preds: bb1 |
| 170 | + %21 = integer_literal $Builtin.Int32, 1 // user: %24 |
| 171 | + %23 = integer_literal $Builtin.Int1, 0 // user: %24 |
| 172 | + %24 = builtin "sadd_with_overflow_Int32"(%11 : $Builtin.Int32, %21 : $Builtin.Int32, %23 : $Builtin.Int1) : $(Builtin.Int32, Builtin.Int1) // user: %25 |
| 173 | + %25 = tuple_extract %24 : $(Builtin.Int32, Builtin.Int1), 0 // user: %28 |
| 174 | + %26 = struct $Int32 (%11 : $Builtin.Int32) // user: %27 |
| 175 | + %27 = enum $Optional<Int32>, #Optional.some!enumelt, %26 : $Int32 // user: %28 |
| 176 | + br bb3(%25 : $Builtin.Int32, %27 : $Optional<Int32>) // id: %28 |
| 177 | + |
| 178 | +bb5: // Preds: bb3 |
| 179 | + switch_enum %18 : $Optional<Int32>, case #Optional.some!enumelt: bb6, case #Optional.none!enumelt: bb7 // id: %29 |
| 180 | + |
| 181 | +bb6(%arg6 : $Int32): // Preds: bb5 |
| 182 | + %30 = unchecked_enum_data %18 : $Optional<Int32>, #Optional.some!enumelt // user: %31 |
| 183 | + %31 = struct_extract %30 : $Int32, #Int32._value // user: %34 |
| 184 | + %33 = integer_literal $Builtin.Int1, 0 // user: %34 |
| 185 | + %34 = builtin "smul_with_overflow_Int32"(%10 : $Builtin.Int32, %31 : $Builtin.Int32, %33 : $Builtin.Int1) : $(Builtin.Int32, Builtin.Int1) // user: %35 |
| 186 | + %35 = tuple_extract %34 : $(Builtin.Int32, Builtin.Int1), 0 // user: %36 |
| 187 | + br bb1(%35 : $Builtin.Int32, %17 : $Builtin.Int32) // id: %36 |
| 188 | + |
| 189 | +bb7: // Preds: bb5 |
| 190 | + cond_fail %2 : $Builtin.Int1 // id: %37 |
| 191 | + unreachable // id: %38 |
| 192 | + |
| 193 | +bb8: // Preds: bb3 |
| 194 | + %39 = struct $Int32 (%10 : $Builtin.Int32) // user: %40 |
| 195 | + return %39 : $Int32 // id: %40 |
| 196 | +} |
| 197 | + |
| 198 | +/// CHECK-LABEL: sil [ossa] @select_enum_dominates_switch_enum2 : $@convention(thin) (Int32) -> Int32 { |
| 199 | +/// The select_enum dominates the switch_enum and knows exactly which element will be |
| 200 | +/// selected. |
| 201 | +/// In this case, the switch is reached when the select_enum is false. Given that the switch |
| 202 | +/// only has 2 elements, we know that the other element must be selected. |
| 203 | +/// CHECK-NOT: switch_enum |
| 204 | +/// CHECK: return |
| 205 | +sil [ossa] @select_enum_dominates_switch_enum2 : $@convention(thin) (Int32) -> Int32 { |
| 206 | +bb0(%0 : $Int32): |
| 207 | + %1 = integer_literal $Builtin.Int32, 0 // users: %5, %9, %9 |
| 208 | + %2 = integer_literal $Builtin.Int1, -1 // users: %7, %37 |
| 209 | + %3 = struct_extract %0 : $Int32, #Int32._value // users: %5, %13 |
| 210 | + %5 = builtin "cmp_sle_Int32"(%1 : $Builtin.Int32, %3 : $Builtin.Int32) : $Builtin.Int1 // user: %7 |
| 211 | + %7 = builtin "xor_Int1"(%5 : $Builtin.Int1, %2 : $Builtin.Int1) : $Builtin.Int1 // user: %8 |
| 212 | + cond_fail %7 : $Builtin.Int1 // id: %8 |
| 213 | + br bb1(%1 : $Builtin.Int32, %1 : $Builtin.Int32) // id: %9 |
| 214 | + |
| 215 | +bb1(%10 : $Builtin.Int32, %11 : $Builtin.Int32): // Preds: bb0 bb6 |
| 216 | + %13 = builtin "cmp_eq_Int32"(%11 : $Builtin.Int32, %3 : $Builtin.Int32) : $Builtin.Int1 // user: %14 |
| 217 | + cond_br %13, bb2, bb4 // id: %14 |
| 218 | + |
| 219 | +bb2: // Preds: bb1 |
| 220 | + %15 = enum $Optional<Int32>, #Optional.none!enumelt // user: %16 |
| 221 | + br bb3(%11 : $Builtin.Int32, %15 : $Optional<Int32>) // id: %16 |
| 222 | + |
| 223 | +bb3(%17 : $Builtin.Int32, %18 : $Optional<Int32>): // Preds: bb2 bb4 |
| 224 | + %t = integer_literal $Builtin.Int1, 1 |
| 225 | + %f = integer_literal $Builtin.Int1, 0 |
| 226 | + %19 = select_enum %18 : $Optional<Int32>, case #Optional.some!enumelt: %t, case #Optional.none!enumelt: %f : $Builtin.Int1 |
| 227 | + cond_br %19, bb8, bb5 // id: %20 |
| 228 | + |
| 229 | +bb4: // Preds: bb1 |
| 230 | + %21 = integer_literal $Builtin.Int32, 1 // user: %24 |
| 231 | + %23 = integer_literal $Builtin.Int1, 0 // user: %24 |
| 232 | + %24 = builtin "sadd_with_overflow_Int32"(%11 : $Builtin.Int32, %21 : $Builtin.Int32, %23 : $Builtin.Int1) : $(Builtin.Int32, Builtin.Int1) // user: %25 |
| 233 | + %25 = tuple_extract %24 : $(Builtin.Int32, Builtin.Int1), 0 // user: %28 |
| 234 | + %26 = struct $Int32 (%11 : $Builtin.Int32) // user: %27 |
| 235 | + %27 = enum $Optional<Int32>, #Optional.some!enumelt, %26 : $Int32 // user: %28 |
| 236 | + br bb3(%25 : $Builtin.Int32, %27 : $Optional<Int32>) // id: %28 |
| 237 | + |
| 238 | +bb5: // Preds: bb3 |
| 239 | + switch_enum %18 : $Optional<Int32>, case #Optional.some!enumelt: bb6, case #Optional.none!enumelt: bb7 // id: %29 |
| 240 | + |
| 241 | +bb6(%arg6 : $Int32): // Preds: bb5 |
| 242 | + %31 = struct_extract %arg6 : $Int32, #Int32._value // user: %34 |
| 243 | + %33 = integer_literal $Builtin.Int1, 0 // user: %34 |
| 244 | + %34 = builtin "smul_with_overflow_Int32"(%10 : $Builtin.Int32, %31 : $Builtin.Int32, %33 : $Builtin.Int1) : $(Builtin.Int32, Builtin.Int1) // user: %35 |
| 245 | + %35 = tuple_extract %34 : $(Builtin.Int32, Builtin.Int1), 0 // user: %36 |
| 246 | + br bb1(%35 : $Builtin.Int32, %17 : $Builtin.Int32) // id: %36 |
| 247 | + |
| 248 | +bb7: // Preds: bb5 |
| 249 | + cond_fail %2 : $Builtin.Int1 // id: %37 |
| 250 | + unreachable // id: %38 |
| 251 | + |
| 252 | +bb8: // Preds: bb3 |
| 253 | + %39 = struct $Int32 (%10 : $Builtin.Int32) // user: %40 |
| 254 | + return %39 : $Int32 // id: %40 |
| 255 | +} |
| 256 | + |
| 257 | +// CHECK-LABEL: @switch_enum_dominates_switch_enum_noarg |
| 258 | +// CHECK: bb0(%0 : $Optional<Builtin.Int32>): |
| 259 | +// CHECK-NEXT: %1 = integer_literal $Builtin.Int32, 1 |
| 260 | +// CHECK-NEXT: %2 = integer_literal $Builtin.Int32, 3 |
| 261 | +// CHECK-NEXT: %3 = select_enum %0 : $Optional<Builtin.Int32>, case #Optional.none!enumelt: %1, case #Optional.some!enumelt: %2 |
| 262 | +// CHECK-NEXT: return %3 |
| 263 | +sil @switch_enum_dominates_switch_enum_noarg : $@convention(thin) (Optional<Builtin.Int32>) -> Builtin.Int32 { |
| 264 | +bb0(%0 : $Optional<Builtin.Int32>): |
| 265 | + switch_enum %0 : $Optional<Builtin.Int32>, case #Optional.none!enumelt: bb1, case #Optional.some!enumelt: bb2 |
| 266 | + |
| 267 | +bb1: |
| 268 | + %i1 = integer_literal $Builtin.Int32, 1 |
| 269 | + br bb5(%i1 : $Builtin.Int32) |
| 270 | + |
| 271 | +bb2: |
| 272 | + switch_enum %0 : $Optional<Builtin.Int32>, case #Optional.none!enumelt: bb3, case #Optional.some!enumelt: bb4 |
| 273 | + |
| 274 | +bb3: |
| 275 | + %i2 = integer_literal $Builtin.Int32, 2 |
| 276 | + br bb5(%i2 : $Builtin.Int32) |
| 277 | + |
| 278 | +bb4: |
| 279 | + %i3 = integer_literal $Builtin.Int32, 3 |
| 280 | + br bb5(%i3 : $Builtin.Int32) |
| 281 | + |
| 282 | +bb5(%r : $Builtin.Int32): |
| 283 | + return %r : $Builtin.Int32 |
| 284 | +} |
| 285 | + |
0 commit comments