@@ -1147,8 +1147,15 @@ swift::tryDevirtualizeApply(ApplySite applySite, ClassHierarchyAnalysis *cha,
1147
1147
1148
1148
// Try to check if the exact dynamic type of the instance is statically
1149
1149
// known.
1150
- if (auto instance = getInstanceWithExactDynamicType (cmi->getOperand (), cha))
1151
- return tryDevirtualizeClassMethod (fas, instance, cd, ore);
1150
+ if (auto instance = getInstanceWithExactDynamicType (cmi->getOperand (), cha)) {
1151
+ // Update the classDecl, because we are stripping casts more aggressively
1152
+ // in getInstanceWithExactDynamicType than in stripUpCasts.
1153
+ CanType classType = getSelfInstanceType (instance->getType ().getASTType ());
1154
+ // This should never be null - make the check just to be on the safe side.
1155
+ if (ClassDecl *cd = classType.getClassOrBoundGenericClass ())
1156
+ return tryDevirtualizeClassMethod (fas, instance, cd, ore);
1157
+ return {ApplySite (), false };
1158
+ }
1152
1159
1153
1160
if (auto exactTy = getExactDynamicType (cmi->getOperand (), cha)) {
1154
1161
if (exactTy == cmi->getOperand ()->getType ())
@@ -1207,8 +1214,11 @@ bool swift::canDevirtualizeApply(FullApplySite applySite,
1207
1214
1208
1215
// Try to check if the exact dynamic type of the instance is statically
1209
1216
// known.
1210
- if (auto instance = getInstanceWithExactDynamicType (cmi->getOperand (), cha))
1211
- return canDevirtualizeClassMethod (applySite, cd);
1217
+ if (auto instance = getInstanceWithExactDynamicType (cmi->getOperand (), cha)) {
1218
+ CanType classType = getSelfInstanceType (instance->getType ().getASTType ());
1219
+ ClassDecl *cd = classType.getClassOrBoundGenericClass ();
1220
+ return cd && canDevirtualizeClassMethod (applySite, cd);
1221
+ }
1212
1222
1213
1223
if (auto exactTy = getExactDynamicType (cmi->getOperand (), cha)) {
1214
1224
if (exactTy == cmi->getOperand ()->getType ())
0 commit comments