@@ -795,21 +795,21 @@ SILInstruction *CastOptimizer::simplifyCheckedCastAddrBranchInst(
795795SILInstruction *
796796CastOptimizer::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