Skip to content

Commit 729c089

Browse files
authored
Merge pull request swiftlang#35769 from gottesmm/pr-24641c31dd4cd867a16902b73ef96df6882ffd1c
[sil-combine] When folding (unchecked_trivial_bit_cast (unchecked_ref_cast)), make sure to move the unchecked_trivial_bit_cast to before the unchecked_ref_cast.
2 parents c494586 + 7374516 commit 729c089

File tree

2 files changed

+54
-15
lines changed

2 files changed

+54
-15
lines changed

lib/SILOptimizer/SILCombiner/SILCombinerCastVisitors.cpp

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -747,28 +747,36 @@ SILCombiner::visitRawPointerToRefInst(RawPointerToRefInst *rawToRef) {
747747
return nullptr;
748748
}
749749

750-
SILInstruction *
751-
SILCombiner::
752-
visitUncheckedTrivialBitCastInst(UncheckedTrivialBitCastInst *UTBCI) {
750+
SILInstruction *SILCombiner::visitUncheckedTrivialBitCastInst(
751+
UncheckedTrivialBitCastInst *utbci) {
753752
// (unchecked_trivial_bit_cast Y->Z
754753
// (unchecked_trivial_bit_cast X->Y x))
755754
// ->
756755
// (unchecked_trivial_bit_cast X->Z x)
757-
SILValue Op = UTBCI->getOperand();
758-
if (auto *OtherUTBCI = dyn_cast<UncheckedTrivialBitCastInst>(Op)) {
759-
return Builder.createUncheckedTrivialBitCast(UTBCI->getLoc(),
760-
OtherUTBCI->getOperand(),
761-
UTBCI->getType());
756+
SILValue operand = utbci->getOperand();
757+
if (auto *otherUTBCI = dyn_cast<UncheckedTrivialBitCastInst>(operand)) {
758+
return Builder.createUncheckedTrivialBitCast(
759+
utbci->getLoc(), otherUTBCI->getOperand(), utbci->getType());
762760
}
763761

764-
// (unchecked_trivial_bit_cast Y->Z
765-
// (unchecked_ref_cast X->Y x))
762+
// %y = unchecked_ref_cast %x X->Y
763+
// ...
764+
// %z = unchecked_trivial_bit_cast %y Y->Z
765+
//
766766
// ->
767-
// (unchecked_trivial_bit_cast X->Z x)
768-
if (auto *URBCI = dyn_cast<UncheckedRefCastInst>(Op)) {
769-
return Builder.createUncheckedTrivialBitCast(UTBCI->getLoc(),
770-
URBCI->getOperand(),
771-
UTBCI->getType());
767+
//
768+
// %z = unchecked_trivial_bit_cast %x X->Z
769+
// %y = unchecked_ref_cast %x X->Y
770+
// ...
771+
if (auto *urbci = dyn_cast<UncheckedRefCastInst>(operand)) {
772+
// We just move the unchecked_trivial_bit_cast to before the
773+
// unchecked_ref_cast and then make its operand the unchecked_ref_cast
774+
// operand. Then we return the cast so we reprocess given that we changed
775+
// its operands.
776+
utbci->moveBefore(urbci);
777+
utbci->setDebugLocation(urbci->getDebugLocation());
778+
utbci->setOperand(urbci->getOperand());
779+
return utbci;
772780
}
773781

774782
return nullptr;

test/SILOptimizer/sil_combine_ossa.sil

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4769,3 +4769,34 @@ bb2(%6c : @guaranteed $__ContiguousArrayStorageBase):
47694769
%14 = struct $MyInt (%13 : $Builtin.Int32)
47704770
return %14 : $MyInt
47714771
}
4772+
4773+
// Make sure that we hoist the unchecked_trivial_bit_cast up the def-use chain
4774+
// to before copy1 and make its operand copy1.
4775+
//
4776+
// CHECK-LABEL: sil [ossa] @unchecked_trivial_bit_cast_hoist_up_def_use_chain : $@convention(thin) (@guaranteed AnyObject) -> UInt {
4777+
// CHECK: bb0([[ARG:%.*]] :
4778+
// CHECK: [[ARG_COPY:%.*]] = copy_value [[ARG]]
4779+
// CHECK: [[RESULT:%.*]] = unchecked_trivial_bit_cast [[ARG_COPY]]
4780+
// CHECK: destroy_value [[ARG_COPY]]
4781+
// CHECK: cond_br undef, [[BB_UNREACHABLE:bb[0-9]*]], [[BB_RESULT:bb[0-9]*]]
4782+
//
4783+
// CHECK: [[BB_UNREACHABLE]]:
4784+
// CHECK-NEXT: unreachable
4785+
//
4786+
// CHECK: [[BB_RESULT]]:
4787+
// CHECK-NEXT: return [[RESULT]]
4788+
// CHECK: } // end sil function 'unchecked_trivial_bit_cast_hoist_up_def_use_chain'
4789+
sil [ossa] @unchecked_trivial_bit_cast_hoist_up_def_use_chain : $@convention(thin) (@guaranteed AnyObject) -> UInt {
4790+
bb0(%0 : @guaranteed $AnyObject):
4791+
%copy1 = copy_value %0 : $AnyObject
4792+
%cast = unchecked_ref_cast %copy1 : $AnyObject to $Builtin.NativeObject
4793+
cond_br undef, bb3, bb4
4794+
4795+
bb3:
4796+
unreachable
4797+
4798+
bb4:
4799+
%2 = unchecked_trivial_bit_cast %cast : $Builtin.NativeObject to $UInt
4800+
destroy_value %cast : $Builtin.NativeObject
4801+
return %2 : $UInt
4802+
}

0 commit comments

Comments
 (0)