Skip to content

Commit 505dbae

Browse files
authored
Merge pull request swiftlang#19899 from eeckstein/fold-bridge-object-casts
2 parents 7461bfa + 54399f9 commit 505dbae

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

lib/SILOptimizer/SILCombiner/SILCombinerBuiltinVisitors.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,18 @@ SILInstruction *SILCombiner::optimizeBuiltinZextOrBitCast(BuiltinInst *I) {
161161
replaceInstUsesWith(*I, NI);
162162
return eraseInstFromFunction(*I);
163163
}
164+
// Optimize a sequence of conversion of an builtin integer to and from
165+
// BridgeObject. This sequence appears in the String implementation.
166+
if (auto *BO2W = dyn_cast<BridgeObjectToWordInst>(Op)) {
167+
if (auto *V2BO = dyn_cast<ValueToBridgeObjectInst>(BO2W->getOperand())) {
168+
if (auto *SI = dyn_cast<StructInst>(V2BO->getOperand())) {
169+
if (SI->getNumOperands() == 1 && SI->getOperand(0)->getType() == I->getType()) {
170+
replaceInstUsesWith(*I, SI->getOperand(0));
171+
return eraseInstFromFunction(*I);
172+
}
173+
}
174+
}
175+
}
164176
return nullptr;
165177
}
166178

test/SILOptimizer/sil_combine.sil

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3637,3 +3637,15 @@ bb0:
36373637
return %36 : $Builtin.Int64
36383638
}
36393639

3640+
// CHECK-LABEL: sil @conversion_to_and_from_bridge_object
3641+
// CHECK: bb0(%0 : $Builtin.Int64):
3642+
// CHECK-NEXT: return %0
3643+
sil @conversion_to_and_from_bridge_object : $@convention(thin) (Builtin.Int64) -> (Builtin.Int64) {
3644+
bb0(%0 : $Builtin.Int64):
3645+
%1 = struct $UInt64 (%0 : $Builtin.Int64)
3646+
%2 = value_to_bridge_object %1 : $UInt64
3647+
%3 = bridge_object_to_word %2 : $Builtin.BridgeObject to $Builtin.Word
3648+
%4 = builtin "zextOrBitCast_Word_Int64"(%3 : $Builtin.Word) : $Builtin.Int64
3649+
return %4 : $Builtin.Int64
3650+
}
3651+

0 commit comments

Comments
 (0)