@@ -38,10 +38,6 @@ namespace {
3838
3939class VTableSpecializer : public SILModuleTransform {
4040 bool specializeVTables (SILModule &module );
41- bool specializeVTableFor (SILType classTy, SILModule &module );
42- SILFunction *specializeVTableMethod (SILFunction *origMethod,
43- SubstitutionMap subs, SILModule &module );
44- bool specializeClassMethodInst (ClassMethodInst *cm);
4541
4642 // / The entry point to the transformation.
4743 void run () override {
@@ -57,25 +53,41 @@ class VTableSpecializer : public SILModuleTransform {
5753
5854} // end anonymous namespace
5955
60- bool VTableSpecializer::specializeVTables (SILModule &module ) {
56+ static SILFunction *specializeVTableMethod (SILFunction *origMethod,
57+ SubstitutionMap subs,
58+ SILModule &module ,
59+ SILTransform *transform);
60+
61+ static bool specializeVTablesInFunction (SILFunction &func, SILModule &module ,
62+ SILTransform *transform) {
6163 bool changed = false ;
62- for (SILFunction &func : module ) {
63- if (func.getLoweredFunctionType ()->isPolymorphic ()) continue ;
64-
65- for (SILBasicBlock &block : func) {
66- for (SILInstruction &inst : block) {
67- if (auto *allocRef = dyn_cast<AllocRefInst>(&inst)) {
68- changed |= specializeVTableFor (allocRef->getType (), module );
69- } else if (auto *metatype = dyn_cast<MetatypeInst>(&inst)) {
70- changed |= specializeVTableFor (
71- metatype->getType ().getInstanceTypeOfMetatype (&func), module );
72- } else if (auto *cm = dyn_cast<ClassMethodInst>(&inst)) {
73- changed |= specializeClassMethodInst (cm);
74- }
64+ if (func.getLoweredFunctionType ()->isPolymorphic ())
65+ return changed;
66+
67+ for (SILBasicBlock &block : func) {
68+ for (SILInstruction &inst : block) {
69+ if (auto *allocRef = dyn_cast<AllocRefInst>(&inst)) {
70+ changed |= (specializeVTableForType (allocRef->getType (), module ,
71+ transform) != nullptr );
72+ } else if (auto *metatype = dyn_cast<MetatypeInst>(&inst)) {
73+ changed |= (specializeVTableForType (
74+ metatype->getType ().getInstanceTypeOfMetatype (&func),
75+ module , transform) != nullptr );
76+ } else if (auto *cm = dyn_cast<ClassMethodInst>(&inst)) {
77+ changed |= specializeClassMethodInst (cm);
7578 }
7679 }
7780 }
7881
82+ return changed;
83+ }
84+
85+ bool VTableSpecializer::specializeVTables (SILModule &module ) {
86+ bool changed = false ;
87+ for (SILFunction &func : module ) {
88+ specializeVTablesInFunction (func, module , this );
89+ }
90+
7991 for (SILVTable *vtable : module .getVTables ()) {
8092 if (vtable->getClass ()->isGenericContext ()) continue ;
8193
@@ -92,13 +104,13 @@ bool VTableSpecializer::specializeVTables(SILModule &module) {
92104 return changed;
93105}
94106
95- bool VTableSpecializer::specializeVTableFor (SILType classTy,
96- SILModule & module ) {
107+ SILVTable * swift::specializeVTableForType (SILType classTy, SILModule & module ,
108+ SILTransform *transform ) {
97109 CanType astType = classTy.getASTType ();
98110 BoundGenericClassType *genClassTy = dyn_cast<BoundGenericClassType>(astType);
99- if (!genClassTy) return false ;
111+ if (!genClassTy) return nullptr ;
100112
101- if (module .lookUpSpecializedVTable (classTy)) return false ;
113+ if (module .lookUpSpecializedVTable (classTy)) return nullptr ;
102114
103115 LLVM_DEBUG (llvm::errs () << " specializeVTableFor "
104116 << genClassTy->getDecl ()->getName () << ' '
@@ -120,19 +132,21 @@ bool VTableSpecializer::specializeVTableFor(SILType classTy,
120132 for (const SILVTableEntry &entry : origVtable->getEntries ()) {
121133 SILFunction *origMethod = entry.getImplementation ();
122134 SILFunction *specializedMethod =
123- specializeVTableMethod (origMethod, subs, module );
135+ specializeVTableMethod (origMethod, subs, module , transform );
124136 newEntries.push_back (SILVTableEntry (entry.getMethod (), specializedMethod,
125137 entry.getKind (),
126138 entry.isNonOverridden ()));
127139 }
128140
129- SILVTable::create (module , classDecl, classTy, IsNotSerialized, newEntries);
130- return true ;
141+ SILVTable *vtable = SILVTable::create (module , classDecl, classTy,
142+ IsNotSerialized, newEntries);
143+ return vtable;
131144}
132145
133- SILFunction *VTableSpecializer::specializeVTableMethod (SILFunction *origMethod,
134- SubstitutionMap subs,
135- SILModule &module ) {
146+ static SILFunction *specializeVTableMethod (SILFunction *origMethod,
147+ SubstitutionMap subs,
148+ SILModule &module ,
149+ SILTransform *transform) {
136150 LLVM_DEBUG (llvm::errs () << " specializeVTableMethod " << origMethod->getName ()
137151 << ' \n ' );
138152
@@ -149,7 +163,7 @@ SILFunction *VTableSpecializer::specializeVTableMethod(SILFunction *origMethod,
149163 llvm::report_fatal_error (" cannot specialize vtable method" );
150164 }
151165
152- SILOptFunctionBuilder FunctionBuilder (*this );
166+ SILOptFunctionBuilder FunctionBuilder (*transform );
153167
154168 GenericFuncSpecializer FuncSpecializer (FunctionBuilder, origMethod, subs,
155169 ReInfo, /* isMandatory=*/ true );
@@ -172,7 +186,10 @@ SILFunction *VTableSpecializer::specializeVTableMethod(SILFunction *origMethod,
172186 return SpecializedF;
173187}
174188
175- bool VTableSpecializer::specializeClassMethodInst (ClassMethodInst *cm) {
189+ bool swift::specializeClassMethodInst (ClassMethodInst *cm) {
190+ SILFunction *f = cm->getFunction ();
191+ SILModule &m = f->getModule ();
192+
176193 SILValue instance = cm->getOperand ();
177194 SILType classTy = instance->getType ();
178195 CanType astType = classTy.getASTType ();
@@ -184,9 +201,6 @@ bool VTableSpecializer::specializeClassMethodInst(ClassMethodInst *cm) {
184201 classDecl->getParentModule (), classDecl);
185202
186203 SILType funcTy = cm->getType ();
187-
188- SILFunction *f = cm->getFunction ();
189- SILModule &m = f->getModule ();
190204 SILType substitutedType =
191205 funcTy.substGenericArgs (m, subs, TypeExpansionContext::minimal ());
192206
0 commit comments