Index: lib/CodeGen/CGExprCXX.cpp =================================================================== --- lib/CodeGen/CGExprCXX.cpp +++ lib/CodeGen/CGExprCXX.cpp @@ -110,7 +110,15 @@ assert(DevirtualizedMethod); const CXXRecordDecl *DevirtualizedClass = DevirtualizedMethod->getParent(); const Expr *Inner = Base->ignoreParenBaseCasts(); - if (getCXXRecord(Inner) == DevirtualizedClass) + if (DevirtualizedMethod->getReturnType().getCanonicalType() != + MD->getReturnType().getCanonicalType()) + // If the return types are not the same, this might be a case where more + // code needs to run to compensate for it. For example, the derived + // method might return a type that inherits form from the return + // type of MD and has a prefix. + // For now we just avoid devirtualizing these covariant cases. + DevirtualizedMethod = nullptr; + else if (getCXXRecord(Inner) == DevirtualizedClass) // If the class of the Inner expression is where the dynamic method // is defined, build the this pointer from it. Base = Inner; @@ -121,15 +129,6 @@ // we don't have support for that yet, so do a virtual call. DevirtualizedMethod = nullptr; } - // If the return types are not the same, this might be a case where more - // code needs to run to compensate for it. For example, the derived - // method might return a type that inherits form from the return - // type of MD and has a prefix. - // For now we just avoid devirtualizing these covariant cases. - if (DevirtualizedMethod && - DevirtualizedMethod->getReturnType().getCanonicalType() != - MD->getReturnType().getCanonicalType()) - DevirtualizedMethod = nullptr; } llvm::Value *This; Index: test/CodeGenCXX/devirtualize-virtual-function-calls-final.cpp =================================================================== --- test/CodeGenCXX/devirtualize-virtual-function-calls-final.cpp +++ test/CodeGenCXX/devirtualize-virtual-function-calls-final.cpp @@ -178,9 +178,9 @@ A *f(RC *x) { // FIXME: It should be possible to devirtualize this case, but that is // not implemented yet. + // CHECK: bitcast // CHECK: getelementptr // CHECK-NEXT: %[[FUNC:.*]] = load - // CHECK-NEXT: bitcast // CHECK-NEXT: = call {{.*}} %[[FUNC]] return static_cast(x)->f(); }