From cd0b32e73a66a20a8dab7a7f0ce963dc669f7c0a Mon Sep 17 00:00:00 2001 From: Anders Carlsson Date: Sun, 10 Apr 2011 18:20:53 +0000 Subject: [PATCH] Strip off parens and no-op casts when deciding if an expr can be devirtualized. Fixes the second half of PR9660. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@129253 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGExprCXX.cpp | 26 +++++++++++++++++++ .../devirtualize-virtual-function-calls.cpp | 10 ++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/CodeGen/CGExprCXX.cpp b/lib/CodeGen/CGExprCXX.cpp index 777036bc3b..075fdc30a8 100644 --- a/lib/CodeGen/CGExprCXX.cpp +++ b/lib/CodeGen/CGExprCXX.cpp @@ -77,6 +77,31 @@ static const CXXRecordDecl *getMostDerivedClassDecl(const Expr *Base) { return cast(DerivedType->castAs()->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(E)) { + E = PE->getSubExpr(); + continue; + } + + if (const CastExpr *CE = dyn_cast(E)) { + if (CE->getCastKind() == CK_NoOp) { + E = CE->getSubExpr(); + continue; + } + } + if (const UnaryOperator *UO = dyn_cast(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 @@ static bool canDevirtualizeMemberFunctionCalls(ASTContext &Context, if (MD->getParent()->hasAttr()) return true; + Base = skipNoOpCastsAndParens(Base); if (const DeclRefExpr *DRE = dyn_cast(Base)) { if (const VarDecl *VD = dyn_cast(DRE->getDecl())) { // This is a record decl. We know the type and can devirtualize it. diff --git a/test/CodeGenCXX/devirtualize-virtual-function-calls.cpp b/test/CodeGenCXX/devirtualize-virtual-function-calls.cpp index 74795b5dfb..5eede66cd7 100644 --- a/test/CodeGenCXX/devirtualize-virtual-function-calls.cpp +++ b/test/CodeGenCXX/devirtualize-virtual-function-calls.cpp @@ -2,7 +2,8 @@ struct A { virtual void f(); - + virtual void f_const() const; + A h(); }; @@ -28,6 +29,12 @@ void f(A a, A *ap, A& ar) { // 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 @@ void f() { // CHECK: call void @_ZN1B1fEv B().h().f(); } + -- 2.40.0