mirror of
https://github.com/llvm/llvm-project.git
synced 2025-04-27 08:56:07 +00:00
If a virtual member function has the 'final' attribute, we can devirtualize calls to it.
llvm-svn: 117444
This commit is contained in:
parent
258b8e1dc8
commit
a7911fa3d7
@ -2039,6 +2039,7 @@
|
|||||||
isa = PBXProject;
|
isa = PBXProject;
|
||||||
buildConfigurationList = 1DEB923508733DC60010E9CD /* Build configuration list for PBXProject "clang" */;
|
buildConfigurationList = 1DEB923508733DC60010E9CD /* Build configuration list for PBXProject "clang" */;
|
||||||
compatibilityVersion = "Xcode 2.4";
|
compatibilityVersion = "Xcode 2.4";
|
||||||
|
developmentRegion = English;
|
||||||
hasScannedForEncodings = 1;
|
hasScannedForEncodings = 1;
|
||||||
knownRegions = (
|
knownRegions = (
|
||||||
English,
|
English,
|
||||||
|
@ -55,7 +55,14 @@ RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD,
|
|||||||
|
|
||||||
/// canDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given
|
/// canDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given
|
||||||
/// expr can be devirtualized.
|
/// 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 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) {
|
||||||
if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
|
if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
|
||||||
// This is a record decl. We know the type and can devirtualize it.
|
// This is a record decl. We know the type and can devirtualize it.
|
||||||
@ -76,7 +83,7 @@ static bool canDevirtualizeMemberFunctionCalls(const Expr *Base) {
|
|||||||
// Check if this is a call expr that returns a record type.
|
// Check if this is a call expr that returns a record type.
|
||||||
if (const CallExpr *CE = dyn_cast<CallExpr>(Base))
|
if (const CallExpr *CE = dyn_cast<CallExpr>(Base))
|
||||||
return CE->getCallReturnType()->isRecordType();
|
return CE->getCallReturnType()->isRecordType();
|
||||||
|
|
||||||
// We can't devirtualize the call.
|
// We can't devirtualize the call.
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -152,7 +159,7 @@ RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE,
|
|||||||
// We also don't emit a virtual call if the base expression has a record type
|
// We also don't emit a virtual call if the base expression has a record type
|
||||||
// because then we know what the type is.
|
// because then we know what the type is.
|
||||||
bool UseVirtualCall = MD->isVirtual() && !ME->hasQualifier()
|
bool UseVirtualCall = MD->isVirtual() && !ME->hasQualifier()
|
||||||
&& !canDevirtualizeMemberFunctionCalls(ME->getBase());
|
&& !canDevirtualizeMemberFunctionCalls(ME->getBase(), MD);
|
||||||
|
|
||||||
llvm::Value *Callee;
|
llvm::Value *Callee;
|
||||||
if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD)) {
|
if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD)) {
|
||||||
@ -267,7 +274,7 @@ CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
|
|||||||
This = LV.getAddress();
|
This = LV.getAddress();
|
||||||
|
|
||||||
llvm::Value *Callee;
|
llvm::Value *Callee;
|
||||||
if (MD->isVirtual() && !canDevirtualizeMemberFunctionCalls(E->getArg(0)))
|
if (MD->isVirtual() && !canDevirtualizeMemberFunctionCalls(E->getArg(0), MD))
|
||||||
Callee = BuildVirtualCall(MD, This, Ty);
|
Callee = BuildVirtualCall(MD, This, Ty);
|
||||||
else
|
else
|
||||||
Callee = CGM.GetAddrOfFunction(MD, Ty);
|
Callee = CGM.GetAddrOfFunction(MD, Ty);
|
||||||
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user