Skip to content

Commit 0edf4a9

Browse files
committed
SIL: Remove SILBuilder::tryCreateUncheckedRefCast()
1 parent f447ed7 commit 0edf4a9

File tree

6 files changed

+20
-58
lines changed

6 files changed

+20
-58
lines changed

include/swift/SIL/SILBuilder.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1783,12 +1783,6 @@ class SILBuilder {
17831783
// Unchecked cast helpers
17841784
//===--------------------------------------------------------------------===//
17851785

1786-
// Create an UncheckedRefCast if the source and dest types are legal,
1787-
// otherwise return null.
1788-
// Unwrap or wrap optional types as needed.
1789-
SingleValueInstruction *tryCreateUncheckedRefCast(SILLocation Loc, SILValue Op,
1790-
SILType ResultTy);
1791-
17921786
// Create the appropriate cast instruction based on result type.
17931787
SingleValueInstruction *createUncheckedBitCast(SILLocation Loc, SILValue Op,
17941788
SILType Ty);

lib/SIL/SILBuilder.cpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,19 +110,6 @@ ProjectBoxInst *SILBuilder::createProjectBox(SILLocation Loc,
110110
getSILDebugLocation(Loc), boxOperand, index, fieldTy));
111111
}
112112

113-
// If legal, create an unchecked_ref_cast from the given operand and result
114-
// type, otherwise return null.
115-
SingleValueInstruction *
116-
SILBuilder::tryCreateUncheckedRefCast(SILLocation Loc, SILValue Op,
117-
SILType ResultTy) {
118-
if (!SILType::canRefCast(Op->getType(), ResultTy, getModule()))
119-
return nullptr;
120-
121-
return insert(UncheckedRefCastInst::create(getSILDebugLocation(Loc), Op,
122-
ResultTy, getFunction(),
123-
C.OpenedArchetypes));
124-
}
125-
126113
ClassifyBridgeObjectInst *
127114
SILBuilder::createClassifyBridgeObject(SILLocation Loc, SILValue value) {
128115
auto &ctx = getASTContext();
@@ -142,8 +129,8 @@ SILBuilder::createUncheckedBitCast(SILLocation Loc, SILValue Op, SILType Ty) {
142129
return insert(UncheckedTrivialBitCastInst::create(
143130
getSILDebugLocation(Loc), Op, Ty, getFunction(), C.OpenedArchetypes));
144131

145-
if (auto refCast = tryCreateUncheckedRefCast(Loc, Op, Ty))
146-
return refCast;
132+
if (SILType::canRefCast(Op->getType(), Ty, getModule()))
133+
return createUncheckedRefCast(Loc, Op, Ty);
147134

148135
// The destination type is nontrivial, and may be smaller than the source
149136
// type, so RC identity cannot be assumed.

lib/SILGen/SILGenBuilder.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -804,17 +804,6 @@ ManagedValue SILGenBuilder::createUncheckedAddrCast(SILLocation loc, ManagedValu
804804
return cloner.clone(cast);
805805
}
806806

807-
ManagedValue SILGenBuilder::tryCreateUncheckedRefCast(SILLocation loc,
808-
ManagedValue original,
809-
SILType type) {
810-
CleanupCloner cloner(*this, original);
811-
SILValue result = tryCreateUncheckedRefCast(loc, original.getValue(), type);
812-
if (!result)
813-
return ManagedValue();
814-
original.forward(SGF);
815-
return cloner.clone(result);
816-
}
817-
818807
ManagedValue SILGenBuilder::createUncheckedTrivialBitCast(SILLocation loc,
819808
ManagedValue original,
820809
SILType type) {

lib/SILGen/SILGenBuilder.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -256,11 +256,6 @@ class SILGenBuilder : public SILBuilder {
256256
ManagedValue createUpcast(SILLocation loc, ManagedValue original,
257257
SILType type);
258258

259-
using SILBuilder::tryCreateUncheckedRefCast;
260-
ManagedValue tryCreateUncheckedRefCast(SILLocation loc,
261-
ManagedValue op,
262-
SILType type);
263-
264259
using SILBuilder::createUncheckedTrivialBitCast;
265260
ManagedValue createUncheckedTrivialBitCast(SILLocation loc,
266261
ManagedValue original,

lib/SILGen/SILGenBuiltin.cpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -649,13 +649,14 @@ emitBuiltinCastReference(SILGenFunction &SGF,
649649
auto &toTL = SGF.getTypeLowering(toTy);
650650
assert(!fromTL.isTrivial() && !toTL.isTrivial() && "expected ref type");
651651

652+
auto arg = args[0];
653+
652654
// TODO: Fix this API.
653655
if (!fromTL.isAddress() || !toTL.isAddress()) {
654-
if (auto refCast = SGF.B.tryCreateUncheckedRefCast(loc, args[0],
655-
toTL.getLoweredType())) {
656+
if (SILType::canRefCast(arg.getType(), toTL.getLoweredType(), SGF.SGM.M)) {
656657
// Create a reference cast, forwarding the cleanup.
657658
// The cast takes the source reference.
658-
return refCast;
659+
return SGF.B.createUncheckedRefCast(loc, arg, toTL.getLoweredType());
659660
}
660661
}
661662

@@ -670,7 +671,7 @@ emitBuiltinCastReference(SILGenFunction &SGF,
670671
// TODO: For now, we leave invalid casts in address form so that the runtime
671672
// will trap. We could emit a noreturn call here instead which would provide
672673
// more information to the optimizer.
673-
SILValue srcVal = args[0].ensurePlusOne(SGF, loc).forward(SGF);
674+
SILValue srcVal = arg.ensurePlusOne(SGF, loc).forward(SGF);
674675
SILValue fromAddr;
675676
if (!fromTL.isAddress()) {
676677
// Move the loadable value into a "source temp". Since the source and
@@ -745,17 +746,9 @@ static ManagedValue emitBuiltinReinterpretCast(SILGenFunction &SGF,
745746
}
746747
// Create the appropriate bitcast based on the source and dest types.
747748
ManagedValue in = args[0];
748-
SILType resultTy = toTL.getLoweredType();
749-
if (resultTy.isTrivial(SGF.F))
750-
return SGF.B.createUncheckedTrivialBitCast(loc, in, resultTy);
751749

752-
// If we can perform a ref cast, just return.
753-
if (auto refCast = SGF.B.tryCreateUncheckedRefCast(loc, in, resultTy))
754-
return refCast;
755-
756-
// Otherwise leave the original cleanup and retain the cast value.
757-
SILValue out = SGF.B.createUncheckedBitwiseCast(loc, in.getValue(), resultTy);
758-
return SGF.emitManagedRetain(loc, out, toTL);
750+
SILType resultTy = toTL.getLoweredType();
751+
return SGF.B.createUncheckedBitCast(loc, in, resultTy);
759752
}
760753

761754
/// Specialized emitter for Builtin.castToBridgeObject.

lib/SILOptimizer/SILCombiner/SILCombinerCastVisitors.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,12 @@ SILCombiner::visitUncheckedRefCastAddrInst(UncheckedRefCastAddrInst *URCI) {
284284
Builder.setCurrentDebugScope(URCI->getDebugScope());
285285
LoadInst *load = Builder.createLoad(Loc, URCI->getSrc(),
286286
LoadOwnershipQualifier::Unqualified);
287-
auto *cast = Builder.tryCreateUncheckedRefCast(Loc, load,
288-
DestTy.getObjectType());
289-
assert(cast && "SILBuilder cannot handle reference-castable types");
287+
288+
assert(SILType::canRefCast(load->getType(), DestTy.getObjectType(),
289+
Builder.getModule()) &&
290+
"SILBuilder cannot handle reference-castable types");
291+
auto *cast = Builder.createUncheckedRefCast(Loc, load,
292+
DestTy.getObjectType());
290293
Builder.createStore(Loc, cast, URCI->getDest(),
291294
StoreOwnershipQualifier::Unqualified);
292295

@@ -391,11 +394,12 @@ visitUncheckedBitwiseCastInst(UncheckedBitwiseCastInst *UBCI) {
391394
UBCI->getOperand(),
392395
UBCI->getType());
393396

394-
if (auto refCast = Builder.tryCreateUncheckedRefCast(
395-
UBCI->getLoc(), UBCI->getOperand(), UBCI->getType()))
396-
return refCast;
397+
if (!SILType::canRefCast(UBCI->getOperand()->getType(), UBCI->getType(),
398+
Builder.getModule()))
399+
return nullptr;
397400

398-
return nullptr;
401+
return Builder.createUncheckedRefCast(UBCI->getLoc(), UBCI->getOperand(),
402+
UBCI->getType());
399403
}
400404

401405
SILInstruction *

0 commit comments

Comments
 (0)