Skip to content

Commit c6525fd

Browse files
committed
[ownership] end_borrow/destroy_value with an operand that has OwnershipKind::None are trivially dead, so make isInstructionTriviallyDead return true for those cases!
1 parent 04a1a72 commit c6525fd

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

lib/SILOptimizer/Utils/InstOptUtils.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@ swift::createDecrementBefore(SILValue ptr, SILInstruction *insertPt) {
138138
return builder.createReleaseValue(loc, ptr, builder.getDefaultAtomicity());
139139
}
140140

141+
static bool isOSSAEndScopeWithNoneOperand(SILInstruction *i) {
142+
if (!isa<EndBorrowInst>(i) && !isa<DestroyValueInst>(i))
143+
return false;
144+
return i->getOperand(0).getOwnershipKind() == OwnershipKind::None;
145+
}
146+
141147
/// Perform a fast local check to see if the instruction is dead.
142148
///
143149
/// This routine only examines the state of the instruction at hand.
@@ -178,6 +184,15 @@ bool swift::isInstructionTriviallyDead(SILInstruction *inst) {
178184
if (isa<UncheckedTakeEnumDataAddrInst>(inst))
179185
return true;
180186

187+
// An ossa end scope instruction is trivially dead if its operand has
188+
// OwnershipKind::None. This can occur after CFG simplification in the
189+
// presence of non-payloaded or trivial payload cases of non-trivial enums.
190+
//
191+
// Examples of ossa end_scope instructions: end_borrow, destroy_value.
192+
if (inst->getFunction()->hasOwnership() &&
193+
isOSSAEndScopeWithNoneOperand(inst))
194+
return true;
195+
181196
if (!inst->mayHaveSideEffects())
182197
return true;
183198

test/SILOptimizer/mandatory_combiner.sil

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ sil @myint_from_myint_and_proto : $@convention(thin) (MyInt, @guaranteed { var P
7575

7676
sil @myint_from_proto_and_myint : $@convention(thin) (@guaranteed { var Proto }, MyInt) -> MyInt
7777

78+
// Optional support
79+
enum FakeOptional<T> {
80+
case none
81+
case some(T)
82+
}
83+
7884
///////////
7985
// Tests //
8086
///////////
@@ -922,11 +928,15 @@ bb0(%0 : @guaranteed $Klass, %1 : @guaranteed $Klass, %2 : @guaranteed $Klass):
922928
// CHECK-LABEL: sil [ossa] @eliminate_simple_arc_traffic : $@convention(thin) (@guaranteed Klass) -> () {
923929
// CHECK-NOT: copy_value
924930
// CHECK-NOT: destroy_value
931+
// CHECK-NOT: enum
932+
// CHECK-NOT: end_borrow
925933
// CHECK: } // end sil function 'eliminate_simple_arc_traffic'
926934
sil [ossa] @eliminate_simple_arc_traffic : $@convention(thin) (@guaranteed Klass) -> () {
927935
bb0(%0 : @guaranteed $Klass):
928936
%1 = copy_value %0 : $Klass
929937
destroy_value %1 : $Klass
938+
%2 = enum $FakeOptional<Klass>, #FakeOptional.none!enumelt
939+
end_borrow %2 : $FakeOptional<Klass>
930940
%9999 = tuple()
931941
return %9999 : $()
932942
}

0 commit comments

Comments
 (0)