@@ -546,31 +546,6 @@ bool swift::canDevirtualizeClassMethod(FullApplySite AI,
546
546
return false ;
547
547
}
548
548
549
- // Type of the actual function to be called.
550
- CanSILFunctionType GenCalleeType = F->getLoweredFunctionType ();
551
-
552
- // Type of the actual function to be called with substitutions applied.
553
- CanSILFunctionType SubstCalleeType = GenCalleeType;
554
-
555
- // For polymorphic functions, bail if the number of substitutions is
556
- // not the same as the number of expected generic parameters.
557
- if (GenCalleeType->isPolymorphic ()) {
558
- // First, find proper list of substitutions for the concrete
559
- // method to be called.
560
- SmallVector<Substitution, 4 > Subs;
561
- getSubstitutionsForCallee (Mod, GenCalleeType,
562
- ClassOrMetatypeType.getSwiftRValueType (),
563
- AI, Subs);
564
- SubstCalleeType = GenCalleeType->substGenericArgs (Mod, Subs);
565
- }
566
-
567
- // Check if the optimizer knows how to cast the return type.
568
- SILType ReturnType =
569
- SILFunctionConventions (SubstCalleeType, Mod).getSILResultType ();
570
-
571
- if (!canCastValueToABICompatibleType (Mod, ReturnType, AI.getType ()))
572
- return false ;
573
-
574
549
return true ;
575
550
}
576
551
@@ -612,8 +587,7 @@ DevirtualizationResult swift::devirtualizeClassMethod(FullApplySite AI,
612
587
for (auto ResultTy : substConv.getIndirectSILResultTypes ()) {
613
588
NewArgs.push_back (
614
589
castValueToABICompatibleType (&B, AI.getLoc (), *IndirectResultArgIter,
615
- IndirectResultArgIter->getType (), ResultTy)
616
- .getValue ());
590
+ IndirectResultArgIter->getType (), ResultTy));
617
591
++IndirectResultArgIter;
618
592
}
619
593
@@ -623,8 +597,7 @@ DevirtualizationResult swift::devirtualizeClassMethod(FullApplySite AI,
623
597
auto paramType = substConv.getSILType (param);
624
598
NewArgs.push_back (
625
599
castValueToABICompatibleType (&B, AI.getLoc (), *ParamArgIter,
626
- ParamArgIter->getType (), paramType)
627
- .getValue ());
600
+ ParamArgIter->getType (), paramType));
628
601
++ParamArgIter;
629
602
}
630
603
@@ -634,7 +607,7 @@ DevirtualizationResult swift::devirtualizeClassMethod(FullApplySite AI,
634
607
NewArgs.push_back (castValueToABICompatibleType (&B, AI.getLoc (),
635
608
ClassOrMetatype,
636
609
ClassOrMetatypeType,
637
- SelfParamTy). getValue () );
610
+ SelfParamTy));
638
611
639
612
SILType ResultTy = substConv.getSILResultType ();
640
613
@@ -707,7 +680,7 @@ DevirtualizationResult swift::devirtualizeClassMethod(FullApplySite AI,
707
680
708
681
// Check if any casting is required for the return value.
709
682
ResultValue = castValueToABICompatibleType (&B, NewAI.getLoc (), ResultValue,
710
- ResultTy, AI.getType ()). getValue () ;
683
+ ResultTy, AI.getType ());
711
684
712
685
DEBUG (llvm::dbgs () << " SUCCESS: " << F->getName () << " \n " );
713
686
NumClassDevirt++;
@@ -871,75 +844,6 @@ static void getWitnessMethodSubstitutions(ApplySite AI, SILFunction *F,
871
844
origSubs, isDefaultWitness, NewSubs);
872
845
}
873
846
874
- // / Check if an upcast is legal.
875
- // / The logic in this function is heavily based on the checks in
876
- // / the SILVerifier.
877
- bool swift::isLegalUpcast (SILType FromTy, SILType ToTy) {
878
- if (ToTy.is <MetatypeType>()) {
879
- CanType InstTy (ToTy.castTo <MetatypeType>()->getInstanceType ());
880
- if (!FromTy.is <MetatypeType>())
881
- return false ;
882
- CanType OpInstTy (FromTy.castTo <MetatypeType>()->getInstanceType ());
883
- auto InstClass = InstTy->getClassOrBoundGenericClass ();
884
- if (!InstClass)
885
- return false ;
886
-
887
- bool CanBeUpcasted =
888
- InstClass->usesObjCGenericsModel ()
889
- ? InstClass->getDeclaredTypeInContext ()->isBindableToSuperclassOf (
890
- OpInstTy, nullptr )
891
- : InstTy->isExactSuperclassOf (OpInstTy, nullptr );
892
-
893
- return CanBeUpcasted;
894
- }
895
-
896
- // Upcast from Optional<B> to Optional<A> is legal as long as B is a
897
- // subclass of A.
898
- if (ToTy.getSwiftRValueType ().getAnyOptionalObjectType () &&
899
- FromTy.getSwiftRValueType ().getAnyOptionalObjectType ()) {
900
- ToTy = SILType::getPrimitiveObjectType (
901
- ToTy.getSwiftRValueType ().getAnyOptionalObjectType ());
902
- FromTy = SILType::getPrimitiveObjectType (
903
- FromTy.getSwiftRValueType ().getAnyOptionalObjectType ());
904
- }
905
-
906
- auto ToClass = ToTy.getClassOrBoundGenericClass ();
907
- if (!ToClass)
908
- return false ;
909
- bool CanBeUpcasted =
910
- ToClass->usesObjCGenericsModel ()
911
- ? ToClass->getDeclaredTypeInContext ()->isBindableToSuperclassOf (
912
- FromTy.getSwiftRValueType (), nullptr )
913
- : ToTy.isExactSuperclassOf (FromTy);
914
-
915
- return CanBeUpcasted;
916
- }
917
-
918
- // / Check if we can pass/convert all arguments of the original apply
919
- // / as required by the found devirtualized method.
920
- // / FIXME: This method was introduced as a workaround. We need to
921
- // / revisit it and check if it is still needed.
922
- static bool
923
- canPassOrConvertAllArguments (ApplySite AI,
924
- CanSILFunctionType SubstCalleeCanType) {
925
- SILFunctionConventions substConv (SubstCalleeCanType, AI.getModule ());
926
- unsigned substArgIdx = AI.getCalleeArgIndexOfFirstAppliedArg ();
927
- for (auto arg : AI.getArguments ()) {
928
- // Check if we can cast the provided argument into the required
929
- // parameter type.
930
- auto FromTy = arg->getType ();
931
- auto ToTy = substConv.getSILArgumentType (substArgIdx++);
932
- // If types are the same, no conversion will be required.
933
- if (FromTy == ToTy)
934
- continue ;
935
- // Otherwise, it should be possible to upcast the arguments.
936
- if (!isLegalUpcast (FromTy, ToTy))
937
- return false ;
938
- }
939
- assert (substArgIdx == substConv.getNumSILArguments ());
940
- return true ;
941
- }
942
-
943
847
// / Generate a new apply of a function_ref to replace an apply of a
944
848
// / witness_method when we've determined the actual function we'll end
945
849
// / up calling.
@@ -963,11 +867,6 @@ static ApplySite devirtualizeWitnessMethod(ApplySite AI, SILFunction *F,
963
867
auto CalleeCanType = F->getLoweredFunctionType ();
964
868
auto SubstCalleeCanType = CalleeCanType->substGenericArgs (Module, NewSubs);
965
869
966
- // Bail if some of the arguments cannot be converted into
967
- // types required by the found devirtualized method.
968
- if (!canPassOrConvertAllArguments (AI, SubstCalleeCanType))
969
- return ApplySite ();
970
-
971
870
// Collect arguments from the apply instruction.
972
871
auto Arguments = SmallVector<SILValue, 4 >();
973
872
@@ -979,7 +878,8 @@ static ApplySite devirtualizeWitnessMethod(ApplySite AI, SILFunction *F,
979
878
for (auto arg : AI.getArguments ()) {
980
879
auto paramType = substConv.getSILArgumentType (substArgIdx++);
981
880
if (arg->getType () != paramType)
982
- arg = B.createUpcast (AI.getLoc (), arg, paramType);
881
+ arg = castValueToABICompatibleType (&B, AI.getLoc (), arg,
882
+ arg->getType (), paramType);
983
883
Arguments.push_back (arg);
984
884
}
985
885
assert (substArgIdx == substConv.getNumSILArguments ());
@@ -1030,26 +930,6 @@ static bool canDevirtualizeWitnessMethod(ApplySite AI) {
1030
930
return false ;
1031
931
}
1032
932
1033
- // Collect all the required substitutions.
1034
- //
1035
- // The complete set of substitutions may be different, e.g. because the found
1036
- // witness thunk F may have been created by a specialization pass and have
1037
- // additional generic parameters.
1038
- SmallVector<Substitution, 4 > NewSubs;
1039
-
1040
- getWitnessMethodSubstitutions (AI, F, WMI->getConformance (), NewSubs);
1041
-
1042
- // Figure out the exact bound type of the function to be called by
1043
- // applying all substitutions.
1044
- auto &Module = AI.getModule ();
1045
- auto CalleeCanType = F->getLoweredFunctionType ();
1046
- auto SubstCalleeCanType = CalleeCanType->substGenericArgs (Module, NewSubs);
1047
-
1048
- // Bail if some of the arguments cannot be converted into
1049
- // types required by the found devirtualized method.
1050
- if (!canPassOrConvertAllArguments (AI, SubstCalleeCanType))
1051
- return false ;
1052
-
1053
933
return true ;
1054
934
}
1055
935
0 commit comments