@@ -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 ;
0 commit comments