[PATCH] D63161: Devirtualize destructor of final class.

Hiroshi Yamauchi via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 11 13:12:42 PDT 2019


yamauchi created this revision.
yamauchi added a reviewer: davidxl.
Herald added a subscriber: Prazek.
Herald added a project: clang.

Take advantage of the final keyword to devirtualize destructor calls.

Fix https://bugs.llvm.org/show_bug.cgi?id=21368


Repository:
  rC Clang

https://reviews.llvm.org/D63161

Files:
  lib/CodeGen/CGExprCXX.cpp
  test/CodeGenCXX/devirtualize-dtor-final.cpp


Index: test/CodeGenCXX/devirtualize-dtor-final.cpp
===================================================================
--- /dev/null
+++ test/CodeGenCXX/devirtualize-dtor-final.cpp
@@ -0,0 +1,17 @@
+// RUN: %clang_cc1 -triple i386-unknown-unknown -std=c++11 %s -emit-llvm -o - | FileCheck %s
+
+namespace Test1 {
+  struct A { virtual ~A() {} };
+  struct B final : public A {};
+  struct C : public A { virtual ~C() final {} };
+  // CHECK-LABEL: define void @_ZN5Test13fooEPNS_1BE
+  void foo(B *b) {
+    // CHECK: call void @_ZN5Test11BD1Ev
+    delete b;
+  }
+  // CHECK-LABEL: define void @_ZN5Test14foo2EPNS_1CE
+  void foo2(C *c) {
+    // CHECK: call void @_ZN5Test11CD1Ev
+    delete c;
+  }
+}
Index: lib/CodeGen/CGExprCXX.cpp
===================================================================
--- lib/CodeGen/CGExprCXX.cpp
+++ lib/CodeGen/CGExprCXX.cpp
@@ -1864,7 +1864,8 @@
     if (RD->hasDefinition() && !RD->hasTrivialDestructor()) {
       Dtor = RD->getDestructor();
 
-      if (Dtor->isVirtual()) {
+      if (Dtor->isVirtual() &&
+          !(Dtor->hasAttr<FinalAttr>() || RD->hasAttr<FinalAttr>())) {
         CGF.CGM.getCXXABI().emitVirtualObjectDelete(CGF, DE, Ptr, ElementType,
                                                     Dtor);
         return;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D63161.204143.patch
Type: text/x-patch
Size: 1282 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190611/2ddca203/attachment.bin>


More information about the cfe-commits mailing list