[PATCH] D66621: [clang] Devirtualization for classes with destructors marked as 'final'

Logan Smith via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 22 14:21:05 PDT 2019


logan-5 created this revision.
logan-5 added a reviewer: rsmith.
logan-5 added a project: clang.
Herald added subscribers: cfe-commits, Prazek.

A class with a destructor marked `final` cannot be derived from, so it should afford the same devirtualization opportunities as marking the entire class `final`.


Repository:
  rC Clang

https://reviews.llvm.org/D66621

Files:
  clang/lib/AST/DeclCXX.cpp
  clang/test/CodeGenCXX/devirtualize-virtual-function-calls-final.cpp


Index: clang/test/CodeGenCXX/devirtualize-virtual-function-calls-final.cpp
===================================================================
--- clang/test/CodeGenCXX/devirtualize-virtual-function-calls-final.cpp
+++ clang/test/CodeGenCXX/devirtualize-virtual-function-calls-final.cpp
@@ -24,6 +24,20 @@
   }
 }
 
+namespace Test2a {
+  struct A {
+    virtual ~A() final {}
+    virtual int f();
+  };
+
+  // CHECK-LABEL: define i32 @_ZN6Test2a1fEPNS_1AE
+  int f(A *a) {
+    // CHECK: call i32 @_ZN6Test2a1A1fEv
+    return a->f();
+  }
+}
+
+
 namespace Test3 {
   struct A {
     virtual int f();
Index: clang/lib/AST/DeclCXX.cpp
===================================================================
--- clang/lib/AST/DeclCXX.cpp
+++ clang/lib/AST/DeclCXX.cpp
@@ -2067,9 +2067,11 @@
   if (DevirtualizedMethod->hasAttr<FinalAttr>())
     return DevirtualizedMethod;
 
-  // Similarly, if the class itself is marked 'final' it can't be overridden
-  // and we can therefore devirtualize the member function call.
-  if (BestDynamicDecl->hasAttr<FinalAttr>())
+  // Similarly, if the class itself or its destructor is marked 'final',
+  // the class can't be overridden and we can therefore devirtualize the 
+  // member function call.
+  if (BestDynamicDecl->hasAttr<FinalAttr>() ||
+      BestDynamicDecl->getDestructor()->hasAttr<FinalAttr>())
     return DevirtualizedMethod;
 
   if (const auto *DRE = dyn_cast<DeclRefExpr>(Base)) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D66621.216657.patch
Type: text/x-patch
Size: 1443 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190822/960b7cbb/attachment-0001.bin>


More information about the cfe-commits mailing list