[cfe-commits] r83880 - in /cfe/trunk: lib/CodeGen/CGCXX.cpp test/CodeGenCXX/devirtualize-virtual-function-calls.cpp test/CodeGenCXX/virtual-function-calls.cpp
Anders Carlsson
andersca at mac.com
Mon Oct 12 12:41:04 PDT 2009
Author: andersca
Date: Mon Oct 12 14:41:04 2009
New Revision: 83880
URL: http://llvm.org/viewvc/llvm-project?rev=83880&view=rev
Log:
Factor out devirtualization checking into a separate function and make it handle references correctly.
Added:
cfe/trunk/test/CodeGenCXX/devirtualize-virtual-function-calls.cpp
Modified:
cfe/trunk/lib/CodeGen/CGCXX.cpp
cfe/trunk/test/CodeGenCXX/virtual-function-calls.cpp
Modified: cfe/trunk/lib/CodeGen/CGCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGCXX.cpp?rev=83880&r1=83879&r2=83880&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGCXX.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGCXX.cpp Mon Oct 12 14:41:04 2009
@@ -198,6 +198,20 @@
Callee, Args, MD);
}
+/// canDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given
+/// expr can be devirtualized.
+static bool canDevirtualizeMemberFunctionCalls(const Expr *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.
+ return VD->getType()->isRecordType();
+ }
+ }
+
+ // We can't devirtualize the call.
+ return false;
+}
+
RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE) {
if (isa<BinaryOperator>(CE->getCallee()))
return EmitCXXMemberPointerCallExpr(CE);
@@ -235,7 +249,7 @@
// because then we know what the type is.
llvm::Value *Callee;
if (MD->isVirtual() && !ME->hasQualifier() &&
- !ME->getBase()->getType()->isRecordType())
+ !canDevirtualizeMemberFunctionCalls(ME->getBase()))
Callee = BuildVirtualCall(MD, This, Ty);
else if (const CXXDestructorDecl *Destructor
= dyn_cast<CXXDestructorDecl>(MD))
Added: 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=83880&view=auto
==============================================================================
--- cfe/trunk/test/CodeGenCXX/devirtualize-virtual-function-calls.cpp (added)
+++ cfe/trunk/test/CodeGenCXX/devirtualize-virtual-function-calls.cpp Mon Oct 12 14:41:04 2009
@@ -0,0 +1,18 @@
+// RUN: clang-cc %s -emit-llvm -o - | FileCheck %s
+
+struct A {
+ virtual void f();
+};
+
+void f(A a, A *ap, A& ar) {
+ // This should not be a virtual function call.
+
+ // CHECK: call void @_ZN1A1fEv(%struct.A* %a)
+ a.f();
+
+ // CHECK: call void %
+ ap->f();
+
+ // CHECK: call void %
+ ar.f();
+}
Modified: cfe/trunk/test/CodeGenCXX/virtual-function-calls.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/virtual-function-calls.cpp?rev=83880&r1=83879&r2=83880&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/virtual-function-calls.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/virtual-function-calls.cpp Mon Oct 12 14:41:04 2009
@@ -8,10 +8,3 @@
void f(A *a) {
a->f('c');
}
-
-void f(A a) {
- // This should not be a virtual function call.
-
- // CHECK: call void @_ZN1A1fEc
- a.f('c');
-}
\ No newline at end of file
More information about the cfe-commits
mailing list