[cfe-commits] r129253 - in /cfe/trunk: lib/CodeGen/CGExprCXX.cpp test/CodeGenCXX/devirtualize-virtual-function-calls.cpp

Anders Carlsson andersca at mac.com
Sun Apr 10 11:20:53 PDT 2011


Author: andersca
Date: Sun Apr 10 13:20:53 2011
New Revision: 129253

URL: http://llvm.org/viewvc/llvm-project?rev=129253&view=rev
Log:
Strip off parens and no-op casts when deciding if an expr can be devirtualized. Fixes the second half of PR9660.

Modified:
    cfe/trunk/lib/CodeGen/CGExprCXX.cpp
    cfe/trunk/test/CodeGenCXX/devirtualize-virtual-function-calls.cpp

Modified: cfe/trunk/lib/CodeGen/CGExprCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprCXX.cpp?rev=129253&r1=129252&r2=129253&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprCXX.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprCXX.cpp Sun Apr 10 13:20:53 2011
@@ -77,6 +77,31 @@
   return cast<CXXRecordDecl>(DerivedType->castAs<RecordType>()->getDecl());
 }
 
+// FIXME: Ideally Expr::IgnoreParenNoopCasts should do this, but it doesn't do
+// quite what we want.
+static const Expr *skipNoOpCastsAndParens(const Expr *E) {
+  while (true) {
+    if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) {
+      E = PE->getSubExpr();
+      continue;
+    }
+
+    if (const CastExpr *CE = dyn_cast<CastExpr>(E)) {
+      if (CE->getCastKind() == CK_NoOp) {
+        E = CE->getSubExpr();
+        continue;
+      }
+    }
+    if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
+      if (UO->getOpcode() == UO_Extension) {
+        E = UO->getSubExpr();
+        continue;
+      }
+    }
+    return E;
+  }
+}
+
 /// canDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given
 /// expr can be devirtualized.
 static bool canDevirtualizeMemberFunctionCalls(ASTContext &Context,
@@ -112,6 +137,7 @@
   if (MD->getParent()->hasAttr<FinalAttr>())
     return true;
 
+  Base = skipNoOpCastsAndParens(Base);
   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.

Modified: cfe/trunk/test/CodeGenCXX/devirtualize-virtual-function-calls.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/devirtualize-virtual-function-calls.cpp?rev=129253&r1=129252&r2=129253&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/devirtualize-virtual-function-calls.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/devirtualize-virtual-function-calls.cpp Sun Apr 10 13:20:53 2011
@@ -2,7 +2,8 @@
 
 struct A {
   virtual void f();
-  
+  virtual void f_const() const;
+
   A h();
 };
 
@@ -28,6 +29,12 @@
   
   // CHECK: call void @_ZN1A1fEv
   a.h().f();
+
+  // CHECK: call void @_ZNK1A7f_constEv
+  a.f_const();
+
+  // CHECK: call void @_ZN1A1fEv
+  (a).f();
 }
 
 struct B {
@@ -45,3 +52,4 @@
   // CHECK: call void @_ZN1B1fEv
   B().h().f();
 }
+





More information about the cfe-commits mailing list