Simplify some code and make it easier to understand.

CSE some stripUpCasts() calls and do a little renaming to aid in
understandability. NFC.

Swift SVN r25941
This commit is contained in:
Mark Lacey
2015-03-10 19:27:38 +00:00
parent d1349f8424
commit dd055c3496

View File

@@ -341,17 +341,15 @@ static bool insertInlineCaches(ApplyInst *AI, ClassHierarchyAnalysis *CHA) {
return false;
SILValue ClassInstance = CMI->getOperand();
// The static type used by the class_method instruction
// is the class which a given method belongs to.
// Is either the class of the instance itself or one of its superclasses.
// Therefore, strip all upcasts to get the real static type
// of the instance.
// Specifically, only the upcast to the static class which method belongs to
// should be stripped.
SILType InstanceType = ClassInstance.stripUpCasts().getType();
ClassDecl *CD = InstanceType.getClassOrBoundGenericClass();
if (auto *VMTI = dyn_cast<ValueMetatypeInst>(ClassInstance.stripUpCasts())) {
// Strip any upcasts off of our 'self' value, potentially leaving us
// with a value whose type is closer (in the class hierarchy) to the
// actual dynamic type.
auto SubTypeValue = ClassInstance.stripUpCasts();
SILType SubType = SubTypeValue.getType();
ClassDecl *CD = SubType.getClassOrBoundGenericClass();
if (auto *VMTI = dyn_cast<ValueMetatypeInst>(SubTypeValue)) {
CanType InstTy = VMTI->getType().castTo<MetatypeType>().getInstanceType();
CD = InstTy.getClassOrBoundGenericClass();
}
@@ -360,27 +358,27 @@ static bool insertInlineCaches(ApplyInst *AI, ClassHierarchyAnalysis *CHA) {
if (!CD)
return false;
if (ClassInstance.getType() != InstanceType) {
if (ClassInstance != SubTypeValue) {
// The implementation of a method to be invoked may actually
// be defined by one of the superclasses.
if (ClassInstance.getType().getAs<MetatypeType>()) {
auto &Module = AI->getModule();
if (!ClassInstance.getType().getMetatypeInstanceType(Module).
isSuperclassOf(InstanceType.getMetatypeInstanceType(Module)))
isSuperclassOf(SubType.getMetatypeInstanceType(Module)))
return false;
} else {
if (!ClassInstance.getType().isSuperclassOf(InstanceType))
if (!ClassInstance.getType().isSuperclassOf(SubType))
return false;
}
// ClassInstance and InstanceType should match for
// ClassInstance and SubType should match for
// devirtualizeClassMethod to work.
ClassInstance = ClassInstance.stripUpCasts();
ClassInstance = SubTypeValue;
}
// Bail if any generic types parameters of the class instance type are
// unbound.
// We cannot devirtualize unbound generic calls yet.
if (isClassWithUnboundGenericParameters(InstanceType, AI->getModule()))
if (isClassWithUnboundGenericParameters(SubType, AI->getModule()))
return false;
if (!CHA->hasKnownDirectSubclasses(CD)) {
@@ -394,7 +392,7 @@ static bool insertInlineCaches(ApplyInst *AI, ClassHierarchyAnalysis *CHA) {
DEBUG(llvm::dbgs() << "Inserting monomorphic inline caches for class " <<
CD->getName() << "\n");
return insertMonomorphicInlineCaches(AI, InstanceType);
return insertMonomorphicInlineCaches(AI, SubType);
}
// Collect the direct subclasses for the class.
@@ -482,7 +480,7 @@ static bool insertInlineCaches(ApplyInst *AI, ClassHierarchyAnalysis *CHA) {
//
// But we can still try to devirtualize the static class of instance
// if it is possible.
return insertMonomorphicInlineCaches(AI, InstanceType);
return insertMonomorphicInlineCaches(AI, SubType);
}
// At this point it is known that there is only one remaining method