Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions lib/SILOptimizer/Utils/Devirtualize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1147,8 +1147,15 @@ swift::tryDevirtualizeApply(ApplySite applySite, ClassHierarchyAnalysis *cha,

// Try to check if the exact dynamic type of the instance is statically
// known.
if (auto instance = getInstanceWithExactDynamicType(cmi->getOperand(), cha))
return tryDevirtualizeClassMethod(fas, instance, cd, ore);
if (auto instance = getInstanceWithExactDynamicType(cmi->getOperand(), cha)) {
// Update the classDecl, because we are stripping casts more aggressively
// in getInstanceWithExactDynamicType than in stripUpCasts.
CanType classType = getSelfInstanceType(instance->getType().getASTType());
// This should never be null - make the check just to be on the safe side.
if (ClassDecl *cd = classType.getClassOrBoundGenericClass())
return tryDevirtualizeClassMethod(fas, instance, cd, ore);
return {ApplySite(), false};
}

if (auto exactTy = getExactDynamicType(cmi->getOperand(), cha)) {
if (exactTy == cmi->getOperand()->getType())
Expand Down Expand Up @@ -1207,8 +1214,11 @@ bool swift::canDevirtualizeApply(FullApplySite applySite,

// Try to check if the exact dynamic type of the instance is statically
// known.
if (auto instance = getInstanceWithExactDynamicType(cmi->getOperand(), cha))
return canDevirtualizeClassMethod(applySite, cd);
if (auto instance = getInstanceWithExactDynamicType(cmi->getOperand(), cha)) {
CanType classType = getSelfInstanceType(instance->getType().getASTType());
ClassDecl *cd = classType.getClassOrBoundGenericClass();
return cd && canDevirtualizeClassMethod(applySite, cd);
}

if (auto exactTy = getExactDynamicType(cmi->getOperand(), cha)) {
if (exactTy == cmi->getOperand()->getType())
Expand Down
37 changes: 37 additions & 0 deletions test/SILOptimizer/devirtualize_class_method.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// RUN: %empty-directory(%t)
// RUN: %target-build-swift -O -module-name=test %s -o %t/a.out
// RUN: %target-run %t/a.out | %FileCheck %s
// REQUIRES: executable_test

class Base {
required init() { }

class func instance() -> Base {
return self.init()
}
}

class Middle: Base {
override class func instance() -> Middle {
return self.init()
}
}

class Derived: Middle {
required init() {
super.init()
print("init Derived")
}
}

struct Maker<C: Base> {
@inline(never)
static func create() -> Base {
return C.instance()
}
}

// CHECK: init Derived
// CHECK: test.Derived
print(Maker<Derived>.create())