diff --git a/lib/SILOptimizer/PassManager/PassPipeline.cpp b/lib/SILOptimizer/PassManager/PassPipeline.cpp index b46ab9c304325..a1a04e038ec06 100644 --- a/lib/SILOptimizer/PassManager/PassPipeline.cpp +++ b/lib/SILOptimizer/PassManager/PassPipeline.cpp @@ -760,6 +760,9 @@ static void addLowLevelPassPipeline(SILPassPipelinePlan &P) { P.addObjectOutliner(); P.addDeadStoreElimination(); + // dead-store-elimination can expose opportunities for dead object elimination. + P.addDeadObjectElimination(); + // We've done a lot of optimizations on this function, attempt to FSO. P.addFunctionSignatureOpts(); P.addComputeEscapeEffects(); diff --git a/test/SILOptimizer/dead_alloc.swift b/test/SILOptimizer/dead_alloc.swift new file mode 100644 index 0000000000000..3c435ec9e58b2 --- /dev/null +++ b/test/SILOptimizer/dead_alloc.swift @@ -0,0 +1,47 @@ +// RUN: %target-swift-frontend -O -emit-sil -parse-as-library %s | %FileCheck %s + +// REQUIRES: swift_stdlib_no_asserts,optimized_stdlib +// REQUIRES: swift_in_compiler + +protocol E { + func f() -> Bool +} + +protocol P { + associatedtype A = Int +} + +public struct X : P, E { + func f() -> Bool { return true } +} + +func g(_ x : T) -> Bool { + if let y = x as? E { return y.f() } + return false +} + +// Check that this function can be completely constant folded and no alloc_stack remains. + +// CHECK-LABEL: sil @$s10dead_alloc0A10AllocStackySbAA1XVF : +// CHECK: bb0({{.*}}): +// CHECK-NEXT: debug_value +// CHECK-NEXT: integer_literal +// CHECK-NEXT: struct +// CHECK-NEXT: return +// CHECK-NEXT: } // end sil function '$s10dead_alloc0A10AllocStackySbAA1XVF' +public func deadAllocStack(_ x: X) -> Bool { + return g(x) +} + +public class C { + let x: String = "123" +} + +// CHECK-LABEL: sil @$s10dead_alloc0A13ClassInstanceyyF : +// CHECK: bb0: +// CHECK-NEXT: tuple +// CHECK-NEXT: return +// CHECK-NEXT: } // end sil function '$s10dead_alloc0A13ClassInstanceyyF' +public func deadClassInstance() { + let _ = C() +} diff --git a/test/SILOptimizer/dead_alloc_stack.swift b/test/SILOptimizer/dead_alloc_stack.swift deleted file mode 100644 index 457253f8a3a34..0000000000000 --- a/test/SILOptimizer/dead_alloc_stack.swift +++ /dev/null @@ -1,32 +0,0 @@ -// RUN: %target-swift-frontend -O -emit-sil -parse-as-library %s | %FileCheck %s - -protocol E { - func f() -> Bool -} - -protocol P { - associatedtype A = Int -} - -public struct X : P, E { - func f() -> Bool { return true } -} - -func g(_ x : T) -> Bool { - if let y = x as? E { return y.f() } - return false -} - -// Check that this function can be completely constant folded and no alloc_stack remains. - -// CHECK-LABEL: sil @$s16dead_alloc_stack6testitySbAA1XVF -// CHECK: bb0({{.*}}): -// CHECK-NEXT: debug_value -// CHECK-NEXT: integer_literal -// CHECK-NEXT: struct -// CHECK-NEXT: return -// CHECK-NEXT: } // end sil function '$s16dead_alloc_stack6testitySbAA1XVF' -public func testit(_ x: X) -> Bool { - return g(x) -} -