Skip to content

Commit e833d11

Browse files
committed
[cast-opt] Change simplifyCheckedCast{,Value}BranchInst to use SILDynamicCastInst.
This is the last change needed to simplify the API of optimizeBridgedCasts to take SILDynamicCastInst.
1 parent 807b187 commit e833d11

File tree

2 files changed

+57
-65
lines changed

2 files changed

+57
-65
lines changed

include/swift/SIL/DynamicCasts.h

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,12 @@ struct SILDynamicCastInst {
184184
switch (getKind()) {
185185
case SILDynamicCastKind::CheckedCastAddrBranchInst:
186186
return cast<CheckedCastAddrBranchInst>(inst)->getConsumptionKind();
187+
// TODO: Bridged casts cannot be expressed by checked_cast_br or
188+
// checked_cast_value_br yet. Should we ever support it, please
189+
// review this code.
187190
case SILDynamicCastKind::CheckedCastBranchInst:
188191
case SILDynamicCastKind::CheckedCastValueBranchInst:
189-
llvm_unreachable("unsupported");
192+
return CastConsumptionKind::CopyOnSuccess;
190193
case SILDynamicCastKind::UnconditionalCheckedCastAddrInst:
191194
return CastConsumptionKind::TakeAlways;
192195
case SILDynamicCastKind::UnconditionalCheckedCastInst:
@@ -215,7 +218,7 @@ struct SILDynamicCastInst {
215218
case SILDynamicCastKind::CheckedCastBranchInst:
216219
return cast<CheckedCastBranchInst>(inst)->getSuccessBB();
217220
case SILDynamicCastKind::CheckedCastValueBranchInst:
218-
llvm_unreachable("unsupported");
221+
return cast<CheckedCastValueBranchInst>(inst)->getSuccessBB();
219222
case SILDynamicCastKind::UnconditionalCheckedCastAddrInst:
220223
case SILDynamicCastKind::UnconditionalCheckedCastInst:
221224
return nullptr;
@@ -251,7 +254,7 @@ struct SILDynamicCastInst {
251254
case SILDynamicCastKind::CheckedCastBranchInst:
252255
return cast<CheckedCastBranchInst>(inst)->getFailureBB();
253256
case SILDynamicCastKind::CheckedCastValueBranchInst:
254-
llvm_unreachable("unsupported");
257+
return cast<CheckedCastValueBranchInst>(inst)->getFailureBB();
255258
case SILDynamicCastKind::UnconditionalCheckedCastAddrInst:
256259
case SILDynamicCastKind::UnconditionalCheckedCastInst:
257260
return nullptr;
@@ -287,7 +290,7 @@ struct SILDynamicCastInst {
287290
case SILDynamicCastKind::CheckedCastBranchInst:
288291
return cast<CheckedCastBranchInst>(inst)->getOperand();
289292
case SILDynamicCastKind::CheckedCastValueBranchInst:
290-
llvm_unreachable("unsupported");
293+
return cast<CheckedCastValueBranchInst>(inst)->getOperand();
291294
case SILDynamicCastKind::UnconditionalCheckedCastAddrInst:
292295
return cast<UnconditionalCheckedCastAddrInst>(inst)->getSrc();
293296
case SILDynamicCastKind::UnconditionalCheckedCastInst:
@@ -304,7 +307,8 @@ struct SILDynamicCastInst {
304307
return cast<CheckedCastAddrBranchInst>(inst)->getDest();
305308
case SILDynamicCastKind::CheckedCastBranchInst:
306309
case SILDynamicCastKind::CheckedCastValueBranchInst:
307-
llvm_unreachable("unsupported");
310+
// TODO: Shouldn't this return getSuccessBlock()->getArgument(0)?
311+
return SILValue();
308312
case SILDynamicCastKind::UnconditionalCheckedCastAddrInst:
309313
return cast<UnconditionalCheckedCastAddrInst>(inst)->getDest();
310314
case SILDynamicCastKind::UnconditionalCheckedCastInst:
@@ -322,8 +326,9 @@ struct SILDynamicCastInst {
322326
case SILDynamicCastKind::CheckedCastAddrBranchInst:
323327
return cast<CheckedCastAddrBranchInst>(inst)->getSourceType();
324328
case SILDynamicCastKind::CheckedCastBranchInst:
329+
return cast<CheckedCastBranchInst>(inst)->getSourceType();
325330
case SILDynamicCastKind::CheckedCastValueBranchInst:
326-
llvm_unreachable("unsupported");
331+
return cast<CheckedCastValueBranchInst>(inst)->getSourceType();
327332
case SILDynamicCastKind::UnconditionalCheckedCastAddrInst:
328333
return cast<UnconditionalCheckedCastAddrInst>(inst)->getSourceType();
329334
case SILDynamicCastKind::UnconditionalCheckedCastInst:
@@ -355,8 +360,9 @@ struct SILDynamicCastInst {
355360
case SILDynamicCastKind::CheckedCastAddrBranchInst:
356361
return cast<CheckedCastAddrBranchInst>(inst)->getTargetType();
357362
case SILDynamicCastKind::CheckedCastBranchInst:
363+
return cast<CheckedCastBranchInst>(inst)->getTargetType();
358364
case SILDynamicCastKind::CheckedCastValueBranchInst:
359-
llvm_unreachable("unsupported");
365+
return cast<CheckedCastValueBranchInst>(inst)->getTargetType();
360366
case SILDynamicCastKind::UnconditionalCheckedCastAddrInst:
361367
return cast<UnconditionalCheckedCastAddrInst>(inst)->getTargetType();
362368
case SILDynamicCastKind::UnconditionalCheckedCastInst:
@@ -373,7 +379,7 @@ struct SILDynamicCastInst {
373379
case SILDynamicCastKind::CheckedCastBranchInst:
374380
return cast<CheckedCastBranchInst>(inst)->getCastType();
375381
case SILDynamicCastKind::CheckedCastValueBranchInst:
376-
llvm_unreachable("unsupported");
382+
return cast<CheckedCastValueBranchInst>(inst)->getCastType();
377383
case SILDynamicCastKind::UnconditionalCheckedCastAddrInst: {
378384
auto *uccai = dyn_cast<UnconditionalCheckedCastAddrInst>(inst);
379385
return uccai->getDest()->getType();
@@ -388,9 +394,8 @@ struct SILDynamicCastInst {
388394

389395
bool isSourceTypeExact() const {
390396
switch (getKind()) {
391-
case SILDynamicCastKind::CheckedCastBranchInst:
392397
case SILDynamicCastKind::CheckedCastValueBranchInst:
393-
llvm_unreachable("unsupported");
398+
case SILDynamicCastKind::CheckedCastBranchInst:
394399
case SILDynamicCastKind::CheckedCastAddrBranchInst:
395400
case SILDynamicCastKind::UnconditionalCheckedCastAddrInst:
396401
case SILDynamicCastKind::UnconditionalCheckedCastInst:
@@ -421,13 +426,18 @@ struct SILDynamicCastInst {
421426

422427
bool isConditional() const {
423428
switch (getKind()) {
424-
case SILDynamicCastKind::CheckedCastBranchInst:
425-
case SILDynamicCastKind::CheckedCastValueBranchInst:
426-
llvm_unreachable("unsupported");
427429
case SILDynamicCastKind::CheckedCastAddrBranchInst: {
428430
auto f = classifyFeasibility(true /*allow wmo*/);
429431
return f == DynamicCastFeasibility::MaySucceed;
430432
}
433+
case SILDynamicCastKind::CheckedCastBranchInst: {
434+
auto f = classifyFeasibility(false /*allow wmo*/);
435+
return f == DynamicCastFeasibility::MaySucceed;
436+
}
437+
case SILDynamicCastKind::CheckedCastValueBranchInst: {
438+
auto f = classifyFeasibility(false /*allow wmo opts*/);
439+
return f == DynamicCastFeasibility::MaySucceed;
440+
}
431441
case SILDynamicCastKind::UnconditionalCheckedCastAddrInst:
432442
case SILDynamicCastKind::UnconditionalCheckedCastInst:
433443
return false;

lib/SILOptimizer/Utils/CastOptimizer.cpp

Lines changed: 34 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -795,21 +795,21 @@ SILInstruction *CastOptimizer::simplifyCheckedCastAddrBranchInst(
795795
SILInstruction *
796796
CastOptimizer::simplifyCheckedCastBranchInst(CheckedCastBranchInst *Inst) {
797797
if (Inst->isExact()) {
798-
auto *ARI = dyn_cast<AllocRefInst>(stripUpCasts(Inst->getOperand()));
798+
SILDynamicCastInst dynamicCast(Inst);
799+
auto *ARI = dyn_cast<AllocRefInst>(stripUpCasts(dynamicCast.getSource()));
799800
if (!ARI)
800801
return nullptr;
801802

802803
// We know the dynamic type of the operand.
803804
SILBuilderWithScope Builder(Inst, BuilderContext);
804-
auto Loc = Inst->getLoc();
805-
auto *SuccessBB = Inst->getSuccessBB();
806-
auto *FailureBB = Inst->getFailureBB();
805+
auto Loc = dynamicCast.getLocation();
807806

808-
if (ARI->getType() == Inst->getCastType()) {
807+
if (ARI->getType() == dynamicCast.getLoweredTargetType()) {
809808
// This exact cast will succeed.
810809
SmallVector<SILValue, 1> Args;
811810
Args.push_back(ARI);
812-
auto *NewI = Builder.createBranch(Loc, SuccessBB, Args);
811+
auto *NewI =
812+
Builder.createBranch(Loc, dynamicCast.getSuccessBlock(), Args);
813813
EraseInstAction(Inst);
814814
WillSucceedAction();
815815
return NewI;
@@ -819,8 +819,8 @@ CastOptimizer::simplifyCheckedCastBranchInst(CheckedCastBranchInst *Inst) {
819819
// original casts value to the failure block.
820820
TinyPtrVector<SILValue> Args;
821821
if (Builder.hasOwnership())
822-
Args.push_back(Inst->getOperand());
823-
auto *NewI = Builder.createBranch(Loc, FailureBB, Args);
822+
Args.push_back(dynamicCast.getSource());
823+
auto *NewI = Builder.createBranch(Loc, dynamicCast.getFailureBlock(), Args);
824824
EraseInstAction(Inst);
825825
WillFailAction();
826826
return NewI;
@@ -832,26 +832,23 @@ CastOptimizer::simplifyCheckedCastBranchInst(CheckedCastBranchInst *Inst) {
832832
if (!Inst)
833833
return nullptr;
834834

835-
auto LoweredTargetType = Inst->getCastType();
836-
auto SourceType = Inst->getSourceType();
837-
auto TargetType = Inst->getTargetType();
838-
auto Loc = Inst->getLoc();
839-
auto *SuccessBB = Inst->getSuccessBB();
840-
auto *FailureBB = Inst->getFailureBB();
841-
auto Op = Inst->getOperand();
842-
auto &Mod = Inst->getModule();
843-
bool isSourceTypeExact = isa<MetatypeInst>(Op);
835+
SILDynamicCastInst dynamicCast(Inst);
836+
auto LoweredTargetType = dynamicCast.getLoweredTargetType();
837+
auto Loc = dynamicCast.getLocation();
838+
auto *SuccessBB = dynamicCast.getSuccessBlock();
839+
auto Op = dynamicCast.getSource();
840+
auto &Mod = dynamicCast.getModule();
844841

845842
// Check if we can statically predict the outcome of the cast.
846-
auto Feasibility = classifyDynamicCast(Mod.getSwiftModule(), SourceType,
847-
TargetType, isSourceTypeExact);
843+
auto Feasibility =
844+
dynamicCast.classifyFeasibility(false /*allow whole module*/);
848845

849846
SILBuilderWithScope Builder(Inst, BuilderContext);
850847
if (Feasibility == DynamicCastFeasibility::WillFail) {
851848
TinyPtrVector<SILValue> Args;
852849
if (Builder.hasOwnership())
853850
Args.push_back(Inst->getOperand());
854-
auto *NewI = Builder.createBranch(Loc, FailureBB, Args);
851+
auto *NewI = Builder.createBranch(Loc, dynamicCast.getFailureBlock(), Args);
855852
EraseInstAction(Inst);
856853
WillFailAction();
857854
return NewI;
@@ -860,15 +857,11 @@ CastOptimizer::simplifyCheckedCastBranchInst(CheckedCastBranchInst *Inst) {
860857
bool ResultNotUsed = SuccessBB->getArgument(0)->use_empty();
861858
SILValue CastedValue;
862859
if (Op->getType() != LoweredTargetType) {
863-
auto Src = Inst->getOperand();
864-
auto Dest = SILValue();
865860
// Apply the bridged cast optimizations.
861+
//
866862
// TODO: Bridged casts cannot be expressed by checked_cast_br yet.
867863
// Should we ever support it, please review this code.
868-
auto BridgedI = optimizeBridgedCasts(
869-
Inst, CastConsumptionKind::CopyOnSuccess,
870-
/* isConditional */ Feasibility == DynamicCastFeasibility::MaySucceed,
871-
Src, Dest, SourceType, TargetType, nullptr, nullptr);
864+
auto BridgedI = optimizeBridgedCasts(dynamicCast);
872865

873866
if (BridgedI) {
874867
llvm_unreachable(
@@ -886,12 +879,11 @@ CastOptimizer::simplifyCheckedCastBranchInst(CheckedCastBranchInst *Inst) {
886879
// The unconditional_cast can be skipped, if the result of a cast
887880
// is not used afterwards.
888881
if (!ResultNotUsed) {
889-
if (!canUseScalarCheckedCastInstructions(Mod, SourceType, TargetType))
882+
if (!dynamicCast.canUseScalarCheckedCastInstructions())
890883
return nullptr;
891884

892-
CastedValue = emitSuccessfulScalarUnconditionalCast(
893-
Builder, Mod.getSwiftModule(), Loc, Op, LoweredTargetType,
894-
SourceType, TargetType, Inst);
885+
CastedValue =
886+
emitSuccessfulScalarUnconditionalCast(Builder, Loc, dynamicCast);
895887
} else {
896888
CastedValue = SILUndef::get(LoweredTargetType, Mod);
897889
}
@@ -919,19 +911,16 @@ SILInstruction *CastOptimizer::simplifyCheckedCastValueBranchInst(
919911
if (!Inst)
920912
return nullptr;
921913

922-
auto LoweredTargetType = Inst->getCastType();
923-
auto SourceType = Inst->getSourceType();
924-
auto TargetType = Inst->getTargetType();
925-
auto Loc = Inst->getLoc();
926-
auto *SuccessBB = Inst->getSuccessBB();
927-
auto *FailureBB = Inst->getFailureBB();
928-
auto Op = Inst->getOperand();
929-
auto &Mod = Inst->getModule();
930-
bool isSourceTypeExact = isa<MetatypeInst>(Op);
914+
SILDynamicCastInst dynamicCast(Inst);
915+
auto LoweredTargetType = dynamicCast.getLoweredTargetType();
916+
auto Loc = dynamicCast.getLocation();
917+
auto *SuccessBB = dynamicCast.getSuccessBlock();
918+
auto *FailureBB = dynamicCast.getFailureBlock();
919+
auto Op = dynamicCast.getSource();
920+
auto &Mod = dynamicCast.getModule();
931921

932922
// Check if we can statically predict the outcome of the cast.
933-
auto Feasibility = classifyDynamicCast(Mod.getSwiftModule(), SourceType,
934-
TargetType, isSourceTypeExact);
923+
auto Feasibility = dynamicCast.classifyFeasibility(false /*allow wmo opts*/);
935924

936925
SILBuilderWithScope Builder(Inst, BuilderContext);
937926

@@ -947,17 +936,11 @@ SILInstruction *CastOptimizer::simplifyCheckedCastValueBranchInst(
947936
bool ResultNotUsed = SuccessBB->getArgument(0)->use_empty();
948937
SILValue CastedValue;
949938
if (Op->getType() != LoweredTargetType) {
950-
auto Src = Inst->getOperand();
951-
auto Dest = SILValue();
952939
// Apply the bridged cast optimizations.
953940
// TODO: Bridged casts cannot be expressed by checked_cast_value_br yet.
954941
// Once the support for opaque values has landed, please review this
955942
// code.
956-
auto BridgedI = optimizeBridgedCasts(
957-
Inst, CastConsumptionKind::CopyOnSuccess,
958-
/* isConditional */ Feasibility == DynamicCastFeasibility::MaySucceed,
959-
Src, Dest, SourceType, TargetType, nullptr, nullptr);
960-
943+
auto *BridgedI = optimizeBridgedCasts(dynamicCast);
961944
if (BridgedI) {
962945
llvm_unreachable(
963946
"Bridged casts cannot be expressed by checked_cast_value_br yet");
@@ -974,13 +957,12 @@ SILInstruction *CastOptimizer::simplifyCheckedCastValueBranchInst(
974957
// The unconditional_cast can be skipped, if the result of a cast
975958
// is not used afterwards.
976959

977-
if (!canUseScalarCheckedCastInstructions(Mod, SourceType, TargetType))
960+
if (!dynamicCast.canUseScalarCheckedCastInstructions())
978961
return nullptr;
979962

980963
if (!ResultNotUsed) {
981-
CastedValue = emitSuccessfulScalarUnconditionalCast(
982-
Builder, Mod.getSwiftModule(), Loc, Op, LoweredTargetType,
983-
SourceType, TargetType, Inst);
964+
CastedValue =
965+
emitSuccessfulScalarUnconditionalCast(Builder, Loc, dynamicCast);
984966
} else {
985967
CastedValue = SILUndef::get(LoweredTargetType, Mod);
986968
}

0 commit comments

Comments
 (0)