Skip to content

Commit 6b87566

Browse files
authored
Merge pull request #36369 from eeckstein/dead-cast-elimination
SILCombine: remove dead unconditional_checked_cast
2 parents 38efe16 + 74fe92f commit 6b87566

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

lib/SILOptimizer/Analysis/SimplifyInstruction.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -322,17 +322,6 @@ SILValue InstSimplifier::visitRefToRawPointerInst(RefToRawPointerInst *RefToRaw)
322322
return SILValue();
323323
}
324324

325-
SILValue
326-
InstSimplifier::
327-
visitUnconditionalCheckedCastInst(UnconditionalCheckedCastInst *UCCI) {
328-
// (UCCI downcast (upcast x #type1 to #type2) #type2 to #type1) -> x
329-
if (auto *upcast = dyn_cast<UpcastInst>(UCCI->getOperand()))
330-
if (UCCI->getType() == upcast->getOperand()->getType())
331-
return upcast->getOperand();
332-
333-
return SILValue();
334-
}
335-
336325
/// If the only use of a cast is a destroy, just destroy the cast operand.
337326
static SILValue simplifyDeadCast(SingleValueInstruction *Cast) {
338327
if (!Cast->hasUsesOfAnyResult())
@@ -356,6 +345,17 @@ static SILValue simplifyDeadCast(SingleValueInstruction *Cast) {
356345
return Cast->getOperand(0);
357346
}
358347

348+
SILValue
349+
InstSimplifier::
350+
visitUnconditionalCheckedCastInst(UnconditionalCheckedCastInst *UCCI) {
351+
// (UCCI downcast (upcast x #type1 to #type2) #type2 to #type1) -> x
352+
if (auto *upcast = dyn_cast<UpcastInst>(UCCI->getOperand()))
353+
if (UCCI->getType() == upcast->getOperand()->getType())
354+
return upcast->getOperand();
355+
356+
return simplifyDeadCast(UCCI);
357+
}
358+
359359
SILValue
360360
InstSimplifier::
361361
visitUncheckedRefCastInst(UncheckedRefCastInst *OPRI) {

test/SILOptimizer/sil_combine.sil

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,23 @@ bb0(%0 : $HeapBufferStorage<T_0_0, T_0_1>):
554554
return %3 : $HeapBufferStorage<T_0_0, T_0_1>
555555
}
556556

557+
class Derived : C {}
558+
559+
// CHECK-LABEL: sil @dead_unconditional_checked_cast :
560+
// CHECK-NOT: unconditional_checked_cast
561+
// CHECK: bb1:
562+
// CHECK: strong_release %0
563+
// CHECK: } // end sil function 'dead_unconditional_checked_cast'
564+
sil @dead_unconditional_checked_cast : $@convention(thin) (@owned C) -> () {
565+
bb0(%0 : $C):
566+
%1 = unconditional_checked_cast %0 : $C to Derived
567+
br bb1
568+
bb1:
569+
strong_release %1 : $Derived
570+
%r = tuple ()
571+
return %r : $()
572+
}
573+
557574
// CHECK-LABEL: sil @cond_fail_applied_to_zero_removal
558575
// CHECK: bb0
559576
// CHECK-NEXT: cond_fail

test/SILOptimizer/sil_combine_ossa.sil

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,20 @@ bb0(%0 : @owned $HeapBufferStorage<T_0_0, T_0_1>):
548548
return %3 : $HeapBufferStorage<T_0_0, T_0_1>
549549
}
550550

551+
class Derived : C {}
552+
553+
// CHECK-LABEL: sil [ossa] @dead_unconditional_checked_cast :
554+
// CHECK-NOT: unconditional_checked_cast
555+
// CHECK: destroy_value %0
556+
// CHECK: } // end sil function 'dead_unconditional_checked_cast'
557+
sil [ossa] @dead_unconditional_checked_cast : $@convention(thin) (@owned C) -> () {
558+
bb0(%0 : @owned $C):
559+
%1 = unconditional_checked_cast %0 : $C to Derived
560+
destroy_value %1 : $Derived
561+
%r = tuple ()
562+
return %r : $()
563+
}
564+
551565
// CHECK-LABEL: sil [ossa] @cond_fail_applied_to_zero_removal :
552566
// CHECK: bb0
553567
// CHECK-NEXT: cond_fail

0 commit comments

Comments
 (0)