@@ -38,10 +38,6 @@ namespace {
38
38
39
39
class VTableSpecializer : public SILModuleTransform {
40
40
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);
45
41
46
42
// / The entry point to the transformation.
47
43
void run () override {
@@ -57,25 +53,41 @@ class VTableSpecializer : public SILModuleTransform {
57
53
58
54
} // end anonymous namespace
59
55
60
- bool VTableSpecializer::specializeVTables (SILModule &module ) {
56
+ static bool specializeVTableFor (SILType classTy, SILModule &module ,
57
+ SILTransform *transform);
58
+ static SILFunction *specializeVTableMethod (SILFunction *origMethod,
59
+ SubstitutionMap subs,
60
+ SILModule &module ,
61
+ SILTransform *transform);
62
+ static bool specializeClassMethodInst (ClassMethodInst *cm);
63
+
64
+ static bool specializeVTablesInFunction (SILFunction &func, SILModule &module ,
65
+ SILTransform *transform) {
61
66
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
- }
67
+ if (func.getLoweredFunctionType ()->isPolymorphic ()) return changed;
68
+
69
+ for (SILBasicBlock &block : func) {
70
+ for (SILInstruction &inst : block) {
71
+ if (auto *allocRef = dyn_cast<AllocRefInst>(&inst)) {
72
+ changed |= specializeVTableFor (allocRef->getType (), module , transform);
73
+ } else if (auto *metatype = dyn_cast<MetatypeInst>(&inst)) {
74
+ changed |= specializeVTableFor (
75
+ metatype->getType ().getInstanceTypeOfMetatype (&func), module , transform);
76
+ } else if (auto *cm = dyn_cast<ClassMethodInst>(&inst)) {
77
+ changed |= specializeClassMethodInst (cm);
75
78
}
76
79
}
77
80
}
78
81
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
+
79
91
for (SILVTable *vtable : module .getVTables ()) {
80
92
if (vtable->getClass ()->isGenericContext ()) continue ;
81
93
@@ -92,8 +104,8 @@ bool VTableSpecializer::specializeVTables(SILModule &module) {
92
104
return changed;
93
105
}
94
106
95
- bool VTableSpecializer:: specializeVTableFor (SILType classTy,
96
- SILModule & module ) {
107
+ static bool specializeVTableFor (SILType classTy, SILModule & module ,
108
+ SILTransform *transform ) {
97
109
CanType astType = classTy.getASTType ();
98
110
BoundGenericClassType *genClassTy = dyn_cast<BoundGenericClassType>(astType);
99
111
if (!genClassTy) return false ;
@@ -120,7 +132,7 @@ bool VTableSpecializer::specializeVTableFor(SILType classTy,
120
132
for (const SILVTableEntry &entry : origVtable->getEntries ()) {
121
133
SILFunction *origMethod = entry.getImplementation ();
122
134
SILFunction *specializedMethod =
123
- specializeVTableMethod (origMethod, subs, module );
135
+ specializeVTableMethod (origMethod, subs, module , transform );
124
136
newEntries.push_back (SILVTableEntry (entry.getMethod (), specializedMethod,
125
137
entry.getKind (),
126
138
entry.isNonOverridden ()));
@@ -130,9 +142,10 @@ bool VTableSpecializer::specializeVTableFor(SILType classTy,
130
142
return true ;
131
143
}
132
144
133
- SILFunction *VTableSpecializer::specializeVTableMethod (SILFunction *origMethod,
134
- SubstitutionMap subs,
135
- SILModule &module ) {
145
+ static SILFunction *specializeVTableMethod (SILFunction *origMethod,
146
+ SubstitutionMap subs,
147
+ SILModule &module ,
148
+ SILTransform *transform) {
136
149
LLVM_DEBUG (llvm::errs () << " specializeVTableMethod " << origMethod->getName ()
137
150
<< ' \n ' );
138
151
@@ -149,7 +162,7 @@ SILFunction *VTableSpecializer::specializeVTableMethod(SILFunction *origMethod,
149
162
llvm::report_fatal_error (" cannot specialize vtable method" );
150
163
}
151
164
152
- SILOptFunctionBuilder FunctionBuilder (*this );
165
+ SILOptFunctionBuilder FunctionBuilder (*transform );
153
166
154
167
GenericFuncSpecializer FuncSpecializer (FunctionBuilder, origMethod, subs,
155
168
ReInfo, /* isMandatory=*/ true );
@@ -172,7 +185,7 @@ SILFunction *VTableSpecializer::specializeVTableMethod(SILFunction *origMethod,
172
185
return SpecializedF;
173
186
}
174
187
175
- bool VTableSpecializer:: specializeClassMethodInst (ClassMethodInst *cm) {
188
+ static bool specializeClassMethodInst (ClassMethodInst *cm) {
176
189
SILValue instance = cm->getOperand ();
177
190
SILType classTy = instance->getType ();
178
191
CanType astType = classTy.getASTType ();
@@ -217,6 +230,12 @@ bool VTableSpecializer::specializeClassMethodInst(ClassMethodInst *cm) {
217
230
return true ;
218
231
}
219
232
233
+ bool swift::specializeVTables (SILFunction *fn, SILTransform *transform) {
234
+ if (!fn->getModule .getASTContext ().LangOpts .hasFeature (Feature::Embedded))
235
+ return false ;
236
+ return specializeVTablesInFunction (*fn, fn->getModule (), transform);
237
+ }
238
+
220
239
SILTransform *swift::createVTableSpecializer () {
221
240
return new VTableSpecializer ();
222
241
}
0 commit comments