Skip to content

Commit 221df61

Browse files
committed
SILOptimizer: Clean up SubstitutionMap usage in devirtualizer
1 parent 3130c3c commit 221df61

File tree

1 file changed

+27
-31
lines changed

1 file changed

+27
-31
lines changed

lib/SILOptimizer/Utils/Devirtualize.cpp

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -778,23 +778,21 @@ getSubstitutionsForProtocolConformance(ProtocolConformanceRef CRef) {
778778
/// \param requirementSig The generic signature of the requirement
779779
/// \param witnessThunkSig The generic signature of the witness method
780780
/// \param origSubs The substitutions from the call instruction
781-
/// \param newSubs New substitutions are stored here
782-
static void getWitnessMethodSubstitutions(
783-
SILModule &M,
781+
static SubstitutionMap
782+
getWitnessMethodSubstitutions(
784783
ProtocolConformanceRef conformanceRef,
785784
GenericSignature *requirementSig,
786785
GenericSignature *witnessThunkSig,
787786
SubstitutionList origSubs,
788-
bool isDefaultWitness,
789-
SmallVectorImpl<Substitution> &newSubs) {
787+
bool isDefaultWitness) {
790788

791789
if (witnessThunkSig == nullptr)
792-
return;
790+
return SubstitutionMap();
793791

794-
if (isDefaultWitness) {
795-
newSubs.append(origSubs.begin(), origSubs.end());
796-
return;
797-
}
792+
auto origSubMap = requirementSig->getSubstitutionMap(origSubs);
793+
794+
if (isDefaultWitness)
795+
return origSubMap;
798796

799797
assert(!conformanceRef.isAbstract());
800798
auto conformance = conformanceRef.getConcrete();
@@ -809,24 +807,19 @@ static void getWitnessMethodSubstitutions(
809807
baseDepth = witnessSig->getGenericParams().back()->getDepth() + 1;
810808

811809
auto origDepth = 1;
812-
auto origSubMap = requirementSig->getSubstitutionMap(origSubs);
813810

814-
auto subMap =
815-
SubstitutionMap::combineSubstitutionMaps(baseSubMap,
816-
origSubMap,
817-
CombineSubstitutionMaps::AtDepth,
818-
baseDepth,
819-
origDepth,
820-
witnessThunkSig);
821-
822-
witnessThunkSig->getSubstitutions(subMap, newSubs);
811+
return SubstitutionMap::combineSubstitutionMaps(
812+
baseSubMap,
813+
origSubMap,
814+
CombineSubstitutionMaps::AtDepth,
815+
baseDepth,
816+
origDepth,
817+
witnessThunkSig);
823818
}
824819

825-
static void getWitnessMethodSubstitutions(ApplySite AI, SILFunction *F,
826-
ProtocolConformanceRef CRef,
827-
SmallVectorImpl<Substitution> &NewSubs) {
828-
auto &Module = AI.getModule();
829-
820+
static SubstitutionMap
821+
getWitnessMethodSubstitutions(SILModule &Module, ApplySite AI, SILFunction *F,
822+
ProtocolConformanceRef CRef) {
830823
auto requirementSig = AI.getOrigCalleeType()->getGenericSignature();
831824
auto witnessThunkSig = F->getLoweredFunctionType()->getGenericSignature();
832825

@@ -839,8 +832,9 @@ static void getWitnessMethodSubstitutions(ApplySite AI, SILFunction *F,
839832
*Module.getSwiftModule())
840833
== CRef.getRequirement();
841834

842-
getWitnessMethodSubstitutions(Module, CRef, requirementSig, witnessThunkSig,
843-
origSubs, isDefaultWitness, NewSubs);
835+
return getWitnessMethodSubstitutions(
836+
CRef, requirementSig, witnessThunkSig,
837+
origSubs, isDefaultWitness);
844838
}
845839

846840
/// Generate a new apply of a function_ref to replace an apply of a
@@ -858,14 +852,12 @@ devirtualizeWitnessMethod(ApplySite AI, SILFunction *F,
858852
// The complete set of substitutions may be different, e.g. because the found
859853
// witness thunk F may have been created by a specialization pass and have
860854
// additional generic parameters.
861-
SmallVector<Substitution, 4> NewSubs;
862-
863-
getWitnessMethodSubstitutions(AI, F, C, NewSubs);
855+
auto SubMap = getWitnessMethodSubstitutions(Module, AI, F, C);
864856

865857
// Figure out the exact bound type of the function to be called by
866858
// applying all substitutions.
867859
auto CalleeCanType = F->getLoweredFunctionType();
868-
auto SubstCalleeCanType = CalleeCanType->substGenericArgs(Module, NewSubs);
860+
auto SubstCalleeCanType = CalleeCanType->substGenericArgs(Module, SubMap);
869861

870862
// Collect arguments from the apply instruction.
871863
auto Arguments = SmallVector<SILValue, 4>();
@@ -894,6 +886,10 @@ devirtualizeWitnessMethod(ApplySite AI, SILFunction *F,
894886
auto ResultSILType = substConv.getSILResultType();
895887
ApplySite SAI;
896888

889+
SmallVector<Substitution, 4> NewSubs;
890+
if (auto GenericSig = CalleeCanType->getGenericSignature())
891+
GenericSig->getSubstitutions(SubMap, NewSubs);
892+
897893
SILValue ResultValue;
898894
if (auto *A = dyn_cast<ApplyInst>(AI)) {
899895
auto *NewAI =

0 commit comments

Comments
 (0)