@@ -1147,8 +1147,15 @@ swift::tryDevirtualizeApply(ApplySite applySite, ClassHierarchyAnalysis *cha,
11471147
11481148 // Try to check if the exact dynamic type of the instance is statically
11491149 // 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+ }
11521159
11531160 if (auto exactTy = getExactDynamicType (cmi->getOperand (), cha)) {
11541161 if (exactTy == cmi->getOperand ()->getType ())
@@ -1207,8 +1214,11 @@ bool swift::canDevirtualizeApply(FullApplySite applySite,
12071214
12081215 // Try to check if the exact dynamic type of the instance is statically
12091216 // 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+ }
12121222
12131223 if (auto exactTy = getExactDynamicType (cmi->getOperand (), cha)) {
12141224 if (exactTy == cmi->getOperand ()->getType ())
0 commit comments