diff --git a/lib/SILOptimizer/Transforms/SimplifyCFG.cpp b/lib/SILOptimizer/Transforms/SimplifyCFG.cpp index 79f0c23048161..a64c91492f722 100644 --- a/lib/SILOptimizer/Transforms/SimplifyCFG.cpp +++ b/lib/SILOptimizer/Transforms/SimplifyCFG.cpp @@ -1793,7 +1793,11 @@ bool SimplifyCFG::simplifySwitchEnumUnreachableBlocks(SwitchEnumInst *SEI) { } if (Dest->args_empty()) { - SILBuilderWithScope(SEI).createBranch(SEI->getLoc(), Dest); + SILBuilderWithScope builder(SEI); + if (SEI->getOperand()->getOwnershipKind() == OwnershipKind::Owned) { + builder.createDestroyValue(SEI->getLoc(), SEI->getOperand()); + } + builder.createBranch(SEI->getLoc(), Dest); addToWorklist(SEI->getParent()); addToWorklist(Dest); diff --git a/test/SILOptimizer/simplify_cfg_ossa.sil b/test/SILOptimizer/simplify_cfg_ossa.sil index 60be68a61b632..917b5c9e0cce3 100644 --- a/test/SILOptimizer/simplify_cfg_ossa.sil +++ b/test/SILOptimizer/simplify_cfg_ossa.sil @@ -1944,6 +1944,24 @@ bb3: return %t : $() } +// CHECK-LABEL: sil [ossa] @insert_compensating_destroy_in_switch_enum_destination_block : +// CHECK: bb0(%0 : @owned $Optional): +// CHECK-NEXT: destroy_value %0 +// CHECK-NEXT: tuple +// CHECK: } // end sil function 'insert_compensating_destroy_in_switch_enum_destination_block' +sil [ossa] @insert_compensating_destroy_in_switch_enum_destination_block : $@convention(thin) (@owned Optional) -> () { +bb0(%0 : @owned $Optional): + switch_enum %0, case #Optional.none!enumelt: bb1, case #Optional.some!enumelt: bb2 + +bb1: + %15 = tuple () + return %15 + +bb2(%4 : @owned $AnyObject): + destroy_value %4 + unreachable +} + // CHECK-LABEL: sil [ossa] @replace_phi_arg_with_borrowed_from_use : // CHECK: bb3([[R:%.*]] : @reborrow $B): // CHECK: bb6([[G:%.*]] : @guaranteed $E):