Skip to content

Commit a3f1728

Browse files
committed
[sil-combine] Enable classify_bridge_object folding when ownership is enabled.
From an ownership perspective, this is just eliminating a non-lifetime ending use. The new instructions we create are not connecting to the underlying object anymore.
1 parent 364cee8 commit a3f1728

File tree

2 files changed

+18
-22
lines changed

2 files changed

+18
-22
lines changed

lib/SILOptimizer/SILCombiner/SILCombinerMiscVisitors.cpp

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2027,26 +2027,20 @@ SILInstruction *SILCombiner::visitMarkDependenceInst(MarkDependenceInst *mdi) {
20272027
return nullptr;
20282028
}
20292029

2030-
2031-
SILInstruction *SILCombiner::
2032-
visitClassifyBridgeObjectInst(ClassifyBridgeObjectInst *CBOI) {
2033-
if (CBOI->getFunction()->hasOwnership())
2034-
return nullptr;
2035-
2036-
auto *URC = dyn_cast<UncheckedRefCastInst>(CBOI->getOperand());
2037-
if (!URC)
2030+
SILInstruction *
2031+
SILCombiner::visitClassifyBridgeObjectInst(ClassifyBridgeObjectInst *cboi) {
2032+
auto *urc = dyn_cast<UncheckedRefCastInst>(cboi->getOperand());
2033+
if (!urc)
20382034
return nullptr;
20392035

2040-
auto type = URC->getOperand()->getType().getASTType();
2036+
auto type = urc->getOperand()->getType().getASTType();
20412037
if (ClassDecl *cd = type->getClassOrBoundGenericClass()) {
20422038
if (!cd->isObjC()) {
20432039
auto int1Ty = SILType::getBuiltinIntegerType(1, Builder.getASTContext());
2044-
SILValue zero = Builder.createIntegerLiteral(CBOI->getLoc(),
2045-
int1Ty, 0);
2046-
return Builder.createTuple(CBOI->getLoc(), { zero, zero });
2040+
SILValue zero = Builder.createIntegerLiteral(cboi->getLoc(), int1Ty, 0);
2041+
return Builder.createTuple(cboi->getLoc(), {zero, zero});
20472042
}
20482043
}
20492044

20502045
return nullptr;
20512046
}
2052-

test/SILOptimizer/sil_combine_ossa.sil

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4080,11 +4080,12 @@ bb0(%0 : $*Builtin.Int64, %1 : @owned $B):
40804080

40814081
// Test to see if we can constant fold a classify_bridge_object on a Swift class
40824082
// to false.
4083-
// CHECK-LABEL: sil [ossa] @test_classify_fold
4084-
// XHECK-NOT: classify_bridge_object
4085-
// XHECK: integer_literal $Builtin.Int1, 0
4086-
// XHECK-NEXT: destroy_value
4087-
// XHECK-NEXT: return
4083+
// CHECK-LABEL: sil [ossa] @test_classify_fold :
4084+
// CHECK-NOT: classify_bridge_object
4085+
// CHECK: destroy_value
4086+
// CHECK: [[LITERAL:%.*]] = integer_literal $Builtin.Int1, 0
4087+
// CHECK: return [[LITERAL]]
4088+
// CHECK: } // end sil function 'test_classify_fold'
40884089
sil [ossa] @test_classify_fold : $@convention(thin) (@owned _ContiguousArrayStorage<B>) -> Builtin.Int1 {
40894090
bb0(%0 : @owned $_ContiguousArrayStorage<B>):
40904091
%1 = unchecked_ref_cast %0 : $_ContiguousArrayStorage<B> to $Builtin.BridgeObject
@@ -4094,10 +4095,11 @@ bb0(%0 : @owned $_ContiguousArrayStorage<B>):
40944095
return %3 : $Builtin.Int1
40954096
}
40964097

4097-
// CHECK-LABEL: sil [ossa] @bridge_cast_noop_fold
4098-
// XHECK: bb0(%0
4099-
// XHECK-NEXT: %1 = upcast %0
4100-
// XHECK-NEXT: return %1
4098+
// CHECK-LABEL: sil [ossa] @bridge_cast_noop_fold :
4099+
// CHECK: bb0(%0
4100+
// CHECK-NEXT: %1 = upcast %0
4101+
// CHECK-NEXT: return %1
4102+
// CHECK: } // end sil function 'bridge_cast_noop_fold'
41014103
sil [ossa] @bridge_cast_noop_fold : $@convention(thin) (@owned _ContiguousArrayStorage<B>) -> @owned __ContiguousArrayStorageBase {
41024104
bb0(%0 : @owned $_ContiguousArrayStorage<B>):
41034105
%1 = unchecked_ref_cast %0 : $_ContiguousArrayStorage<B> to $Builtin.BridgeObject

0 commit comments

Comments
 (0)