Skip to content

Commit e18beda

Browse files
committed
CastOptimizer: pass the SIL function to the cast-classify APIs, instead of the module decl
NFC
1 parent 9df1039 commit e18beda

File tree

4 files changed

+22
-24
lines changed

4 files changed

+22
-24
lines changed

include/swift/SIL/DynamicCasts.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ atBest(DynamicCastFeasibility feasibility, DynamicCastFeasibility bestCase) {
6464
/// Classify the feasibility of a dynamic cast. The source and target
6565
/// types should be unlowered formal types.
6666
DynamicCastFeasibility classifyDynamicCast(
67-
ModuleDecl *context,
67+
SILFunction *function,
6868
CanType sourceType, CanType targetType,
6969
bool isSourceTypeExact = false,
7070
bool isWholeModuleOpts = false);
@@ -390,7 +390,7 @@ struct SILDynamicCastInst {
390390

391391
DynamicCastFeasibility classifyFeasibility(bool allowWholeModule) const {
392392
return swift::classifyDynamicCast(
393-
getModule().getSwiftModule(),
393+
getFunction(),
394394
getSourceFormalType(), getTargetFormalType(),
395395
isSourceTypeExact(), allowWholeModule && getModule().isWholeModule());
396396
}

lib/SIL/Utils/DynamicCasts.cpp

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ static CanType unwrapExistential(CanType e) {
9393
/// into an existential type by performing a static check
9494
/// of protocol conformances if it is possible.
9595
static DynamicCastFeasibility
96-
classifyDynamicCastToProtocol(CanType source, CanType target,
96+
classifyDynamicCastToProtocol(SILFunction *function, CanType source, CanType target,
9797
bool isWholeModuleOpts) {
9898
assert(target.isExistentialType() &&
9999
"target should be an existential type");
@@ -468,7 +468,7 @@ static bool isCFBridgingConversion(CanType sourceFormalType,
468468

469469
/// Try to classify the dynamic-cast relationship between two types.
470470
DynamicCastFeasibility
471-
swift::classifyDynamicCast(ModuleDecl *M,
471+
swift::classifyDynamicCast(SILFunction *function,
472472
CanType source,
473473
CanType target,
474474
bool isSourceTypeExact,
@@ -481,19 +481,20 @@ swift::classifyDynamicCast(ModuleDecl *M,
481481

482482
auto sourceObject = source.getOptionalObjectType();
483483
auto targetObject = target.getOptionalObjectType();
484+
ModuleDecl *M = function->getModule().getSwiftModule();
484485

485486
// A common level of optionality doesn't affect the feasibility,
486487
// except that we can't fold things to failure because nil inhabits
487488
// both types.
488489
if (sourceObject && targetObject) {
489-
return atWorst(classifyDynamicCast(M, sourceObject, targetObject),
490+
return atWorst(classifyDynamicCast(function, sourceObject, targetObject),
490491
DynamicCastFeasibility::MaySucceed);
491492

492493
// Casting to a more optional type follows the same rule unless we
493494
// know that the source cannot dynamically be an optional value,
494495
// in which case we'll always just cast and inject into an optional.
495496
} else if (targetObject) {
496-
auto result = classifyDynamicCast(M, source, targetObject,
497+
auto result = classifyDynamicCast(function, source, targetObject,
497498
/* isSourceTypeExact */ false,
498499
isWholeModuleOpts);
499500
if (canDynamicallyStoreOptional(source))
@@ -502,12 +503,12 @@ swift::classifyDynamicCast(ModuleDecl *M,
502503

503504
// Casting to a less-optional type can always fail.
504505
} else if (sourceObject) {
505-
auto result = atBest(classifyDynamicCast(M, sourceObject, target,
506+
auto result = atBest(classifyDynamicCast(function, sourceObject, target,
506507
/* isSourceTypeExact */ false,
507508
isWholeModuleOpts),
508509
DynamicCastFeasibility::MaySucceed);
509510
if (target.isExistentialType()) {
510-
result = atWorst(result, classifyDynamicCastToProtocol(
511+
result = atWorst(result, classifyDynamicCastToProtocol(function,
511512
source, target, isWholeModuleOpts));
512513
}
513514
return result;
@@ -522,7 +523,7 @@ swift::classifyDynamicCast(ModuleDecl *M,
522523
// Check conversions from non-protocol types into protocol types.
523524
if (!source.isExistentialType() &&
524525
target.isExistentialType())
525-
return classifyDynamicCastToProtocol(source, target,
526+
return classifyDynamicCastToProtocol(function, source, target,
526527
isWholeModuleOpts);
527528

528529
// Check conversions from protocol types to non-protocol types.
@@ -552,7 +553,7 @@ swift::classifyDynamicCast(ModuleDecl *M,
552553
// Hashable is not actually a legal existential type right now, but
553554
// the check doesn't care about that.
554555
if (auto hashable = getHashableExistentialType(M)) {
555-
return classifyDynamicCastToProtocol(source, hashable,
556+
return classifyDynamicCastToProtocol(function, source, hashable,
556557
isWholeModuleOpts);
557558
}
558559
}
@@ -589,7 +590,7 @@ swift::classifyDynamicCast(ModuleDecl *M,
589590

590591
if (targetMetatype.isAnyExistentialType() && target->isExistentialType()) {
591592
auto Feasibility =
592-
classifyDynamicCastToProtocol(source, target, isWholeModuleOpts);
593+
classifyDynamicCastToProtocol(function, source, target, isWholeModuleOpts);
593594
// Cast from existential metatype to existential metatype may still
594595
// succeed, even if we cannot prove anything statically.
595596
if (Feasibility != DynamicCastFeasibility::WillFail ||
@@ -699,7 +700,7 @@ swift::classifyDynamicCast(ModuleDecl *M,
699700

700701
// Combine the result of prior elements with this element type.
701702
result = std::max(result,
702-
classifyDynamicCast(M,
703+
classifyDynamicCast(function,
703704
sourceElt.getType()->getCanonicalType(),
704705
targetElt.getType()->getCanonicalType(),
705706
isSourceTypeExact,
@@ -758,7 +759,7 @@ swift::classifyDynamicCast(ModuleDecl *M,
758759
// question there.
759760
if (bridgedSource) source = bridgedSource;
760761
if (bridgedTarget) target = bridgedTarget;
761-
return classifyDynamicCast(M, source, target, false, isWholeModuleOpts);
762+
return classifyDynamicCast(function, source, target, false, isWholeModuleOpts);
762763
}
763764

764765
// Casts from a class into a non-class can never succeed if the target must
@@ -833,7 +834,7 @@ swift::classifyDynamicCast(ModuleDecl *M,
833834
if (Type ObjCTy = M->getASTContext().getBridgedToObjC(M, source)) {
834835
// If the bridged ObjC type is known, check if
835836
// this type can be cast into target type.
836-
return classifyDynamicCast(M,
837+
return classifyDynamicCast(function,
837838
ObjCTy->getCanonicalType(),
838839
target,
839840
/* isSourceTypeExact */ false, isWholeModuleOpts);
@@ -861,16 +862,16 @@ swift::classifyDynamicCast(ModuleDecl *M,
861862
// Arrays and sets.
862863
if (sourceStruct->isArray() || sourceStruct->isSet()) {
863864
auto valueFeasibility =
864-
classifyDynamicCast(M, sourceArgs[0], targetArgs[0]);
865+
classifyDynamicCast(function, sourceArgs[0], targetArgs[0]);
865866
return atWorst(valueFeasibility,
866867
DynamicCastFeasibility::MaySucceed);
867868

868869
// Dictionaries.
869870
} else if (sourceStruct->isDictionary()) {
870871
auto keyFeasibility =
871-
classifyDynamicCast(M, sourceArgs[0], targetArgs[0]);
872+
classifyDynamicCast(function, sourceArgs[0], targetArgs[0]);
872873
auto valueFeasibility =
873-
classifyDynamicCast(M, sourceArgs[1], targetArgs[1]);
874+
classifyDynamicCast(function, sourceArgs[1], targetArgs[1]);
874875
return atWorst(atBest(keyFeasibility, valueFeasibility),
875876
DynamicCastFeasibility::MaySucceed);
876877
}
@@ -1240,7 +1241,7 @@ swift::emitSuccessfulScalarUnconditionalCast(SILBuilder &B, ModuleDecl *M,
12401241
CanType sourceFormalType,
12411242
CanType targetFormalType,
12421243
SILInstruction *existingCast) {
1243-
assert(classifyDynamicCast(M, sourceFormalType, targetFormalType)
1244+
assert(classifyDynamicCast(&B.getFunction(), sourceFormalType, targetFormalType)
12441245
== DynamicCastFeasibility::WillSucceed);
12451246

12461247
// Casts to/from existential types cannot be further improved.
@@ -1277,7 +1278,7 @@ bool swift::emitSuccessfulIndirectUnconditionalCast(
12771278
SILBuilder &B, ModuleDecl *M, SILLocation loc, SILValue src,
12781279
CanType sourceFormalType, SILValue dest, CanType targetFormalType,
12791280
SILInstruction *existingCast) {
1280-
assert(classifyDynamicCast(M, sourceFormalType, targetFormalType)
1281+
assert(classifyDynamicCast(&B.getFunction(), sourceFormalType, targetFormalType)
12811282
== DynamicCastFeasibility::WillSucceed);
12821283

12831284
assert(src->getType().isAddress());

lib/SILOptimizer/Utils/ConstExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1874,7 +1874,7 @@ ConstExprFunctionState::evaluateInstructionAndGetNext(
18741874
CanType targetType = substituteGenericParamsAndSimplify(
18751875
checkedCastInst->getTargetFormalType());
18761876
DynamicCastFeasibility castResult = classifyDynamicCast(
1877-
inst->getModule().getSwiftModule(), sourceType, targetType);
1877+
inst->getFunction(), sourceType, targetType);
18781878
if (castResult == DynamicCastFeasibility::MaySucceed) {
18791879
return {std::nullopt, getUnknown(evaluator, inst->asSILNode(),
18801880
UnknownReason::UnknownCastResult)};

lib/SILOptimizer/Utils/OptimizerBridging.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -530,10 +530,7 @@ BridgedDynamicCastResult classifyDynamicCastBridged(BridgedCanType sourceTy, Bri
530530
static_assert((int)DynamicCastFeasibility::WillFail == (int)BridgedDynamicCastResult::willFail);
531531

532532
return static_cast<BridgedDynamicCastResult>(
533-
classifyDynamicCast(function.getFunction()->getModule().getSwiftModule(),
534-
sourceTy.unbridged(),
535-
destTy.unbridged(),
536-
sourceTypeIsExact));
533+
classifyDynamicCast(function.getFunction(), sourceTy.unbridged(), destTy.unbridged(), sourceTypeIsExact));
537534
}
538535

539536
BridgedDynamicCastResult classifyDynamicCastBridged(BridgedInstruction inst) {

0 commit comments

Comments
 (0)