mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
RedundantPhiElimination eliminates block phi-arguments which have the same value as other arguments of the same block.
This also works with cycles, like two equivalent loop induction variables. Such patterns are generated e.g. when using stdlib's enumerated() on Array.
preheader:
br bb1(%initval, %initval)
header(%phi1, %phi2):
%next1 = builtin "add" (%phi1, %one)
%next2 = builtin "add" (%phi2, %one)
cond_br %loopcond, header(%next1, %next2), exit
exit:
is replaced with
preheader:
br bb1(%initval)
header(%phi1):
%next1 = builtin "add" (%phi1, %one)
%next2 = builtin "add" (%phi1, %one) // dead: will be cleaned-up later
cond_br %loopcond, header(%next1), exit
exit:
Any remaining dead or "trivially" equivalent instructions will then be cleaned-up by DCE and CSE, respectively.
rdar://problem/33438123
36 lines
1.1 KiB
Swift
36 lines
1.1 KiB
Swift
// RUN: %target-swift-frontend %s -O -module-name=test -emit-sil | %FileCheck %s
|
|
// REQUIRES: swift_stdlib_no_asserts,optimized_stdlib
|
|
|
|
var gg = 0
|
|
|
|
@inline(never)
|
|
func take(_ x: Int, _ y: Int) {
|
|
gg = x + y
|
|
}
|
|
|
|
// CHECK-LABEL: sil @$s4test23check_cond_fail_messageySiSaySiGF
|
|
// CHECK: cond_fail {{.*}} "Index out of range"
|
|
// CHECK: // end sil function '$s4test23check_cond_fail_messageySiSaySiGF'
|
|
public func check_cond_fail_message(_ array: [Int]) -> Int {
|
|
return array[2]
|
|
}
|
|
|
|
// CHECK-LABEL: sil @$s4test22eliminate_bounds_checkyySaySiGF
|
|
// CHECK-NOT: cond_fail {{.*}} "Index out of range"
|
|
// CHECK: // end sil function '$s4test22eliminate_bounds_checkyySaySiGF'
|
|
public func eliminate_bounds_check(_ array: [Int]) {
|
|
for (index, x) in array.enumerated() {
|
|
take(x, index)
|
|
}
|
|
}
|
|
|
|
// CHECK-LABEL: sil @$s4test27eliminate_two_bounds_checksyySaySiGF
|
|
// CHECK-NOT: cond_fail {{.*}} "Index out of range"
|
|
// CHECK: // end sil function '$s4test27eliminate_two_bounds_checksyySaySiGF'
|
|
public func eliminate_two_bounds_checks(_ array: [Int]) {
|
|
for (index, x) in array.enumerated() {
|
|
take(x, array[index])
|
|
}
|
|
}
|
|
|