aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnders Carlsson <andersca@mac.com>2010-10-27 13:34:43 +0000
committerAnders Carlsson <andersca@mac.com>2010-10-27 13:34:43 +0000
commitd66f42856539bf1c764c13fba41758228fc4cb9b (patch)
treefbbca264cfa13eeab2bfcff9624325bc7a86457d
parentbd2bfae2a6e5dc264e1f13183ad2ac11095766bb (diff)
Also devirtualize calls to a member functions where the containing class has been marked final.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@117445 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--lib/CodeGen/CGExprCXX.cpp9
-rw-r--r--test/CodeGenCXX/attr-final-devirtualize-virtual-function-calls.cpp14
2 files changed, 20 insertions, 3 deletions
diff --git a/lib/CodeGen/CGExprCXX.cpp b/lib/CodeGen/CGExprCXX.cpp
index e3b5f71b27..a03a1fe362 100644
--- a/lib/CodeGen/CGExprCXX.cpp
+++ b/lib/CodeGen/CGExprCXX.cpp
@@ -59,10 +59,15 @@ static bool canDevirtualizeMemberFunctionCalls(const Expr *Base,
const CXXMethodDecl *MD) {
// If the member function has the "final" attribute, we know that it can't be
- // overridden and can therefor devirtualize it.
+ // overridden and can therefore devirtualize it.
if (MD->hasAttr<FinalAttr>())
return true;
-
+
+ // Similarly, if the class itself has the "final" attribute it can't be
+ // overridden and we can therefore devirtualize the member function call.
+ if (MD->getParent()->hasAttr<FinalAttr>())
+ return true;
+
if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) {
if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
// This is a record decl. We know the type and can devirtualize it.
diff --git a/test/CodeGenCXX/attr-final-devirtualize-virtual-function-calls.cpp b/test/CodeGenCXX/attr-final-devirtualize-virtual-function-calls.cpp
index b96cf04e6d..a45078055d 100644
--- a/test/CodeGenCXX/attr-final-devirtualize-virtual-function-calls.cpp
+++ b/test/CodeGenCXX/attr-final-devirtualize-virtual-function-calls.cpp
@@ -6,7 +6,19 @@ namespace Test1 {
};
// CHECK: define i32 @_ZN5Test11fEPNS_1AE
- int f(A* a) {
+ int f(A *a) {
+ // CHECK: ret i32 1
+ return a->f();
+ }
+}
+
+namespace Test2 {
+ struct __attribute__((final)) A {
+ virtual int f() { return 1; }
+ };
+
+ // CHECK: define i32 @_ZN5Test21fEPNS_1AE
+ int f(A *a) {
// CHECK: ret i32 1
return a->f();
}