[cfe-commits] r117444 - in /cfe/trunk: clang.xcodeproj/project.pbxproj lib/CodeGen/CGExprCXX.cpp test/CodeGenCXX/attr-final-devirtualize-virtual-function-calls.cpp

Anders Carlsson andersca at mac.com
Wed Oct 27 06:28:47 PDT 2010


Author: andersca
Date: Wed Oct 27 08:28:46 2010
New Revision: 117444

URL: http://llvm.org/viewvc/llvm-project?rev=117444&view=rev
Log:
If a virtual member function has the 'final' attribute, we can devirtualize calls to it.

Added:
    cfe/trunk/test/CodeGenCXX/attr-final-devirtualize-virtual-function-calls.cpp
Modified:
    cfe/trunk/clang.xcodeproj/project.pbxproj
    cfe/trunk/lib/CodeGen/CGExprCXX.cpp

Modified: cfe/trunk/clang.xcodeproj/project.pbxproj
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/clang.xcodeproj/project.pbxproj?rev=117444&r1=117443&r2=117444&view=diff
==============================================================================
--- cfe/trunk/clang.xcodeproj/project.pbxproj (original)
+++ cfe/trunk/clang.xcodeproj/project.pbxproj Wed Oct 27 08:28:46 2010
@@ -2039,6 +2039,7 @@
 			isa = PBXProject;
 			buildConfigurationList = 1DEB923508733DC60010E9CD /* Build configuration list for PBXProject "clang" */;
 			compatibilityVersion = "Xcode 2.4";
+			developmentRegion = English;
 			hasScannedForEncodings = 1;
 			knownRegions = (
 				English,

Modified: cfe/trunk/lib/CodeGen/CGExprCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprCXX.cpp?rev=117444&r1=117443&r2=117444&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprCXX.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprCXX.cpp Wed Oct 27 08:28:46 2010
@@ -55,7 +55,14 @@
 
 /// canDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given
 /// expr can be devirtualized.
-static bool canDevirtualizeMemberFunctionCalls(const Expr *Base) {
+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.
+  if (MD->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.
@@ -76,7 +83,7 @@
   // Check if this is a call expr that returns a record type.
   if (const CallExpr *CE = dyn_cast<CallExpr>(Base))
     return CE->getCallReturnType()->isRecordType();
-  
+
   // We can't devirtualize the call.
   return false;
 }
@@ -152,7 +159,7 @@
   // We also don't emit a virtual call if the base expression has a record type
   // because then we know what the type is.
   bool UseVirtualCall = MD->isVirtual() && !ME->hasQualifier()
-                     && !canDevirtualizeMemberFunctionCalls(ME->getBase());
+                     && !canDevirtualizeMemberFunctionCalls(ME->getBase(), MD);
 
   llvm::Value *Callee;
   if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD)) {
@@ -267,7 +274,7 @@
     This = LV.getAddress();
 
   llvm::Value *Callee;
-  if (MD->isVirtual() && !canDevirtualizeMemberFunctionCalls(E->getArg(0)))
+  if (MD->isVirtual() && !canDevirtualizeMemberFunctionCalls(E->getArg(0), MD))
     Callee = BuildVirtualCall(MD, This, Ty);
   else
     Callee = CGM.GetAddrOfFunction(MD, Ty);

Added: cfe/trunk/test/CodeGenCXX/attr-final-devirtualize-virtual-function-calls.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/attr-final-devirtualize-virtual-function-calls.cpp?rev=117444&view=auto
==============================================================================
--- cfe/trunk/test/CodeGenCXX/attr-final-devirtualize-virtual-function-calls.cpp (added)
+++ cfe/trunk/test/CodeGenCXX/attr-final-devirtualize-virtual-function-calls.cpp Wed Oct 27 08:28:46 2010
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 %s -O3 -emit-llvm -o - | FileCheck %s
+
+namespace Test1 {
+  struct A {
+    virtual int f() __attribute__((final)) { return 1; }
+  };
+
+  // CHECK: define i32 @_ZN5Test11fEPNS_1AE
+  int f(A* a) {
+    // CHECK: ret i32 1
+    return a->f();
+  }
+}





More information about the cfe-commits mailing list