Skip to content

Commit a2b0e8f

Browse files
committed
SILCombine: make the value_to_bridge_object peephole less conservative.
We can always eliminate ARC operations on the result of a value_to_bridge_object instruction. There is no need to restrict the operand to a specific SIL pattern.
1 parent 217a317 commit a2b0e8f

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
lines changed

lib/SILOptimizer/SILCombiner/SILCombinerMiscVisitors.cpp

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -686,20 +686,6 @@ SILInstruction *SILCombiner::visitCondFailInst(CondFailInst *CFI) {
686686
return nullptr;
687687
}
688688

689-
static bool isValueToBridgeObjectremovable(ValueToBridgeObjectInst *VTBOI) {
690-
SILValue operand = VTBOI->getOperand();
691-
// If operand is a struct $UInt (% : $Builtin.), fetch the real source
692-
if (auto *SI = dyn_cast<StructInst>(operand)) {
693-
assert(SI->SILInstruction::getAllOperands().size() == 1 &&
694-
"Expected a single operand");
695-
operand = SI->getOperand(0);
696-
}
697-
if (auto *BI = dyn_cast<BuiltinInst>(operand)) {
698-
return (BI->getBuiltinInfo().ID == BuiltinValueKind::StringObjectOr);
699-
}
700-
return false;
701-
}
702-
703689
SILInstruction *SILCombiner::visitStrongRetainInst(StrongRetainInst *SRI) {
704690
// Retain of ThinToThickFunction is a no-op.
705691
SILValue funcOper = SRI->getOperand();
@@ -718,8 +704,7 @@ SILInstruction *SILCombiner::visitStrongRetainInst(StrongRetainInst *SRI) {
718704
// builtin "stringObjectOr_Int64" (or to tag the string)
719705
// value_to_bridge_object (cast the UInt to bridge object)
720706
if (auto *VTBOI = dyn_cast<ValueToBridgeObjectInst>(SRI->getOperand())) {
721-
if (isValueToBridgeObjectremovable(VTBOI))
722-
return eraseInstFromFunction(*SRI);
707+
return eraseInstFromFunction(*SRI);
723708
}
724709

725710
// Sometimes in the stdlib due to hand offs, we will see code like:
@@ -1169,8 +1154,7 @@ SILInstruction *SILCombiner::visitStrongReleaseInst(StrongReleaseInst *SRI) {
11691154
// builtin "stringObjectOr_Int64" (or to tag the string)
11701155
// value_to_bridge_object (cast the UInt to bridge object)
11711156
if (auto *VTBOI = dyn_cast<ValueToBridgeObjectInst>(SRI->getOperand())) {
1172-
if (isValueToBridgeObjectremovable(VTBOI))
1173-
return eraseInstFromFunction(*SRI);
1157+
return eraseInstFromFunction(*SRI);
11741158
}
11751159

11761160
// Release of a classbound existential converted from a class is just a

test/SILOptimizer/sil_combine.sil

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3505,3 +3505,16 @@ bb0:
35053505
%4 = apply %2() : $@noescape @callee_guaranteed () -> Builtin.Int32
35063506
return %4 : $Builtin.Int32
35073507
}
3508+
3509+
// CHECK-LABEL: sil @optimize_arc_with_value_to_bridge_object
3510+
// CHECK: bb0(%0 : $Builtin.Int64):
3511+
// CHECK-NEXT: struct $UInt64
3512+
// CHECK-NEXT: value_to_bridge_object
3513+
// CHECK-NEXT: return
3514+
sil @optimize_arc_with_value_to_bridge_object : $@convention(thin) (Builtin.Int64) -> Builtin.BridgeObject {
3515+
bb0(%0 : $Builtin.Int64):
3516+
%1 = struct $UInt64 (%0 : $Builtin.Int64)
3517+
%2 = value_to_bridge_object %1 : $UInt64
3518+
strong_retain %2 : $Builtin.BridgeObject
3519+
return %2 : $Builtin.BridgeObject
3520+
}

0 commit comments

Comments
 (0)