@@ -795,21 +795,21 @@ SILInstruction *CastOptimizer::simplifyCheckedCastAddrBranchInst(
795
795
SILInstruction *
796
796
CastOptimizer::simplifyCheckedCastBranchInst (CheckedCastBranchInst *Inst) {
797
797
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 ()));
799
800
if (!ARI)
800
801
return nullptr ;
801
802
802
803
// We know the dynamic type of the operand.
803
804
SILBuilderWithScope Builder (Inst, BuilderContext);
804
- auto Loc = Inst->getLoc ();
805
- auto *SuccessBB = Inst->getSuccessBB ();
806
- auto *FailureBB = Inst->getFailureBB ();
805
+ auto Loc = dynamicCast.getLocation ();
807
806
808
- if (ARI->getType () == Inst-> getCastType ()) {
807
+ if (ARI->getType () == dynamicCast. getLoweredTargetType ()) {
809
808
// This exact cast will succeed.
810
809
SmallVector<SILValue, 1 > Args;
811
810
Args.push_back (ARI);
812
- auto *NewI = Builder.createBranch (Loc, SuccessBB, Args);
811
+ auto *NewI =
812
+ Builder.createBranch (Loc, dynamicCast.getSuccessBlock (), Args);
813
813
EraseInstAction (Inst);
814
814
WillSucceedAction ();
815
815
return NewI;
@@ -819,8 +819,8 @@ CastOptimizer::simplifyCheckedCastBranchInst(CheckedCastBranchInst *Inst) {
819
819
// original casts value to the failure block.
820
820
TinyPtrVector<SILValue> Args;
821
821
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);
824
824
EraseInstAction (Inst);
825
825
WillFailAction ();
826
826
return NewI;
@@ -832,26 +832,23 @@ CastOptimizer::simplifyCheckedCastBranchInst(CheckedCastBranchInst *Inst) {
832
832
if (!Inst)
833
833
return nullptr ;
834
834
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 ();
844
841
845
842
// 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 */ );
848
845
849
846
SILBuilderWithScope Builder (Inst, BuilderContext);
850
847
if (Feasibility == DynamicCastFeasibility::WillFail) {
851
848
TinyPtrVector<SILValue> Args;
852
849
if (Builder.hasOwnership ())
853
850
Args.push_back (Inst->getOperand ());
854
- auto *NewI = Builder.createBranch (Loc, FailureBB , Args);
851
+ auto *NewI = Builder.createBranch (Loc, dynamicCast. getFailureBlock () , Args);
855
852
EraseInstAction (Inst);
856
853
WillFailAction ();
857
854
return NewI;
@@ -860,15 +857,11 @@ CastOptimizer::simplifyCheckedCastBranchInst(CheckedCastBranchInst *Inst) {
860
857
bool ResultNotUsed = SuccessBB->getArgument (0 )->use_empty ();
861
858
SILValue CastedValue;
862
859
if (Op->getType () != LoweredTargetType) {
863
- auto Src = Inst->getOperand ();
864
- auto Dest = SILValue ();
865
860
// Apply the bridged cast optimizations.
861
+ //
866
862
// TODO: Bridged casts cannot be expressed by checked_cast_br yet.
867
863
// 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);
872
865
873
866
if (BridgedI) {
874
867
llvm_unreachable (
@@ -886,12 +879,11 @@ CastOptimizer::simplifyCheckedCastBranchInst(CheckedCastBranchInst *Inst) {
886
879
// The unconditional_cast can be skipped, if the result of a cast
887
880
// is not used afterwards.
888
881
if (!ResultNotUsed) {
889
- if (!canUseScalarCheckedCastInstructions (Mod, SourceType, TargetType ))
882
+ if (!dynamicCast. canUseScalarCheckedCastInstructions ())
890
883
return nullptr ;
891
884
892
- CastedValue = emitSuccessfulScalarUnconditionalCast (
893
- Builder, Mod.getSwiftModule (), Loc, Op, LoweredTargetType,
894
- SourceType, TargetType, Inst);
885
+ CastedValue =
886
+ emitSuccessfulScalarUnconditionalCast (Builder, Loc, dynamicCast);
895
887
} else {
896
888
CastedValue = SILUndef::get (LoweredTargetType, Mod);
897
889
}
@@ -919,19 +911,16 @@ SILInstruction *CastOptimizer::simplifyCheckedCastValueBranchInst(
919
911
if (!Inst)
920
912
return nullptr ;
921
913
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 ();
931
921
932
922
// 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*/ );
935
924
936
925
SILBuilderWithScope Builder (Inst, BuilderContext);
937
926
@@ -947,17 +936,11 @@ SILInstruction *CastOptimizer::simplifyCheckedCastValueBranchInst(
947
936
bool ResultNotUsed = SuccessBB->getArgument (0 )->use_empty ();
948
937
SILValue CastedValue;
949
938
if (Op->getType () != LoweredTargetType) {
950
- auto Src = Inst->getOperand ();
951
- auto Dest = SILValue ();
952
939
// Apply the bridged cast optimizations.
953
940
// TODO: Bridged casts cannot be expressed by checked_cast_value_br yet.
954
941
// Once the support for opaque values has landed, please review this
955
942
// 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);
961
944
if (BridgedI) {
962
945
llvm_unreachable (
963
946
" Bridged casts cannot be expressed by checked_cast_value_br yet" );
@@ -974,13 +957,12 @@ SILInstruction *CastOptimizer::simplifyCheckedCastValueBranchInst(
974
957
// The unconditional_cast can be skipped, if the result of a cast
975
958
// is not used afterwards.
976
959
977
- if (!canUseScalarCheckedCastInstructions (Mod, SourceType, TargetType ))
960
+ if (!dynamicCast. canUseScalarCheckedCastInstructions ())
978
961
return nullptr ;
979
962
980
963
if (!ResultNotUsed) {
981
- CastedValue = emitSuccessfulScalarUnconditionalCast (
982
- Builder, Mod.getSwiftModule (), Loc, Op, LoweredTargetType,
983
- SourceType, TargetType, Inst);
964
+ CastedValue =
965
+ emitSuccessfulScalarUnconditionalCast (Builder, Loc, dynamicCast);
984
966
} else {
985
967
CastedValue = SILUndef::get (LoweredTargetType, Mod);
986
968
}
0 commit comments