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 | 149b77526e93b1ebe3cead46e3e459923660b750 (patch) | |
tree | 9ab13a126d00439397e9275bc36914f57c02567c /lib/CodeGen/CGClass.cpp | |
parent | e02feb3964e30644b8e3156807ac431fe244c9c7 (diff) |
[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
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@284766 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/CodeGen/CGClass.cpp')
-rw-r--r-- | lib/CodeGen/CGClass.cpp | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/lib/CodeGen/CGClass.cpp b/lib/CodeGen/CGClass.cpp index 4d69c3ff5b..2d70e7a071 100644 --- a/lib/CodeGen/CGClass.cpp +++ b/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. |