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

Dávid Bolvanský via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Aug 31 11:51:47 PDT 2019


This revision was automatically updated to reflect the committed changes.
Closed by commit rL370597: [clang] Devirtualization for classes with destructors marked as 'final' (authored by xbolva00, committed by ).
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Changed prior to commit:
  https://reviews.llvm.org/D66621?vs=217009&id=218230#toc

Repository:
  rL LLVM

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D66621/new/

https://reviews.llvm.org/D66621

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


Index: cfe/trunk/lib/AST/DeclCXX.cpp
===================================================================
--- cfe/trunk/lib/AST/DeclCXX.cpp
+++ cfe/trunk/lib/AST/DeclCXX.cpp
@@ -2067,10 +2067,15 @@
   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.
+  // Similarly, if the class itself or its destructor is marked 'final',
+  // the class can't be derived from and we can therefore devirtualize the 
+  // member function call.
   if (BestDynamicDecl->hasAttr<FinalAttr>())
     return DevirtualizedMethod;
+  if (const auto *dtor = BestDynamicDecl->getDestructor()) {
+    if (dtor->hasAttr<FinalAttr>())
+      return DevirtualizedMethod;
+  }
 
   if (const auto *DRE = dyn_cast<DeclRefExpr>(Base)) {
     if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
Index: cfe/trunk/test/CodeGenCXX/devirtualize-virtual-function-calls-final.cpp
===================================================================
--- cfe/trunk/test/CodeGenCXX/devirtualize-virtual-function-calls-final.cpp
+++ cfe/trunk/test/CodeGenCXX/devirtualize-virtual-function-calls-final.cpp
@@ -24,11 +24,24 @@
   }
 }
 
-namespace Test3 {
+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();  };
+
   struct B final : A { };
 
   // CHECK-LABEL: define i32 @_ZN5Test31fEPNS_1BE


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D66621.218230.patch
Type: text/x-patch
Size: 1665 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190831/d418e25b/attachment.bin>


More information about the cfe-commits mailing list