Skip to content

Commit 2ff6fdf

Browse files
committed
[sil-combine] Update ownership for bridge_object_to_ref.
This just uses the infrastructure in place from fixing upcast/unchecked_ref_cast.
1 parent 7ecee99 commit 2ff6fdf

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

lib/SILOptimizer/SILCombiner/SILCombinerCastVisitors.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -499,15 +499,24 @@ SILInstruction *SILCombiner::visitEndCOWMutationInst(EndCOWMutationInst *ECM) {
499499
}
500500

501501
SILInstruction *
502-
SILCombiner::visitBridgeObjectToRefInst(BridgeObjectToRefInst *BORI) {
503-
if (BORI->getFunction()->hasOwnership())
504-
return nullptr;
502+
SILCombiner::visitBridgeObjectToRefInst(BridgeObjectToRefInst *bori) {
505503
// Fold noop casts through Builtin.BridgeObject.
504+
//
506505
// (bridge_object_to_ref (unchecked-ref-cast x BridgeObject) y)
507506
// -> (unchecked-ref-cast x y)
508-
if (auto URC = dyn_cast<UncheckedRefCastInst>(BORI->getOperand()))
509-
return Builder.createUncheckedRefCast(BORI->getLoc(), URC->getOperand(),
510-
BORI->getType());
507+
if (auto *urc = dyn_cast<UncheckedRefCastInst>(bori->getOperand())) {
508+
if (SILValue(urc).getOwnershipKind() != OwnershipKind::Owned) {
509+
return Builder.createUncheckedRefCast(
510+
bori->getLoc(), urc->getOperand(), bori->getType());
511+
}
512+
SingleBlockOwnedForwardingInstFolder folder(*this, bori);
513+
if (folder.add(urc)) {
514+
auto *newValue = Builder.createUncheckedRefCast(
515+
bori->getLoc(), urc->getOperand(), bori->getType());
516+
return std::move(folder).optimizeWithReplacement(newValue);
517+
}
518+
}
519+
511520
return nullptr;
512521
}
513522

0 commit comments

Comments
 (0)