diff options
author | Vedant Kumar <vsk@apple.com> | 2016-10-20 18:44:14 +0000 |
---|---|---|
committer | Vedant Kumar <vsk@apple.com> | 2016-10-20 18:44:14 +0000 |
commit | 2d38ae6c415c92207239ce3be11c244dd3ddba66 (patch) | |
tree | 9e3e0385850556e4d8e5c3dcf3fda8319b55d8d8 /clang/lib/CodeGen/CGClass.cpp | |
parent | b625d17db804ff389c0be469ac646f7edc0452ff (diff) | |
download | bcm5719-llvm-2d38ae6c415c92207239ce3be11c244dd3ddba66.tar.gz bcm5719-llvm-2d38ae6c415c92207239ce3be11c244dd3ddba66.zip |
[CodeGen] Devirtualize calls to methods marked final in a derived class
If we see a virtual method call to Base::foo() but can infer that the
object is an instance of Derived, and that 'foo' is marked 'final' in
Derived, we can devirtualize the call to Derived::foo().
Differential Revision: https://reviews.llvm.org/D25813
llvm-svn: 284766
Diffstat (limited to 'clang/lib/CodeGen/CGClass.cpp')
-rw-r--r-- | clang/lib/CodeGen/CGClass.cpp | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/clang/lib/CodeGen/CGClass.cpp b/clang/lib/CodeGen/CGClass.cpp index 4d69c3ff5be..2d70e7a0718 100644 --- a/clang/lib/CodeGen/CGClass.cpp +++ b/clang/lib/CodeGen/CGClass.cpp @@ -2873,6 +2873,11 @@ CodeGenFunction::CanDevirtualizeMemberFunctionCall(const Expr *Base, if (getLangOpts().AppleKext) return false; + // If the member function is marked 'final', we know that it can't be + // overridden and can therefore devirtualize it. + if (MD->hasAttr<FinalAttr>()) + return true; + // If the most derived class is marked final, we know that no subclass can // override this member function and so we can devirtualize it. For example: // @@ -2883,14 +2888,17 @@ CodeGenFunction::CanDevirtualizeMemberFunctionCall(const Expr *Base, // b->f(); // } // - const CXXRecordDecl *MostDerivedClassDecl = Base->getBestDynamicClassType(); - if (MostDerivedClassDecl->hasAttr<FinalAttr>()) - return true; + if (const CXXRecordDecl *BestDynamicDecl = Base->getBestDynamicClassType()) { + if (BestDynamicDecl->hasAttr<FinalAttr>()) + return true; - // If the member function is marked 'final', we know that it can't be - // overridden and can therefore devirtualize it. - if (MD->hasAttr<FinalAttr>()) - return true; + // There may be a method corresponding to MD in a derived class. If that + // method is marked final, we can devirtualize it. + const CXXMethodDecl *DevirtualizedMethod = + MD->getCorrespondingMethodInClass(BestDynamicDecl); + if (DevirtualizedMethod->hasAttr<FinalAttr>()) + return true; + } // Similarly, if the class itself is marked 'final' it can't be overridden // and we can therefore devirtualize the member function call. |