[PATCH] D63161: Devirtualize destructor of final class.
Hiroshi Yamauchi via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Jun 14 14:50:41 PDT 2019
yamauchi updated this revision to Diff 204852.
yamauchi marked an inline comment as done.
yamauchi added a comment.
Addressed comment.
Repository:
rC Clang
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D63161/new/
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,34 @@
+// 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 : A {};
+ struct C : 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;
+ }
+}
+
+namespace Test2 {
+ struct SomethingElse { virtual void f(); };
+ struct A { virtual ~A() {} };
+ struct B final : SomethingElse, A {};
+ struct C : SomethingElse, A { virtual ~C() final {} };
+ // CHECK-LABEL: define void @_ZN5Test23fooEPNS_1BE
+ void foo(B *b) {
+ // CHECK: call void @_ZN5Test21BD1Ev
+ delete (A*)b;
+ }
+ // CHECK-LABEL: define void @_ZN5Test24foo2EPNS_1CE
+ void foo2(C *c) {
+ // CHECK: call void @_ZN5Test21CD1Ev
+ delete (A*)c;
+ }
+}
Index: lib/CodeGen/CGExprCXX.cpp
===================================================================
--- lib/CodeGen/CGExprCXX.cpp
+++ lib/CodeGen/CGExprCXX.cpp
@@ -1865,9 +1865,38 @@
Dtor = RD->getDestructor();
if (Dtor->isVirtual()) {
- CGF.CGM.getCXXABI().emitVirtualObjectDelete(CGF, DE, Ptr, ElementType,
- Dtor);
- return;
+ bool UseVirtualCall = true;
+ const Expr *Base = DE->getArgument();
+ if (auto *DevirtualizedDtor =
+ dyn_cast_or_null<const CXXDestructorDecl>(
+ Dtor->getDevirtualizedMethod(
+ Base, CGF.CGM.getLangOpts().AppleKext))) {
+ UseVirtualCall = false;
+ const CXXRecordDecl *DevirtualizedClass =
+ DevirtualizedDtor->getParent();
+ const Expr *Inner = Base->ignoreParenBaseCasts();
+ if (getCXXRecord(Base) == DevirtualizedClass) {
+ // Devirtualized to the class of the base type (the type of the
+ // whole expression).
+ Dtor = DevirtualizedDtor;
+ } else if (getCXXRecord(Inner) == DevirtualizedClass) {
+ // Devirtualized to the class of the derived type (the type of the
+ // expression inside derived-to-base casts). We need to adjust the
+ // this pointer to that type.
+ Dtor = DevirtualizedDtor;
+ Ptr = CGF.EmitPointerWithAlignment(Inner);
+ } else {
+ // Devirtualized to some other type. Would need to cast the this
+ // pointer to that type but we don't have support for that yet, so
+ // do a virtual call.
+ UseVirtualCall = true;
+ }
+ }
+ if (UseVirtualCall) {
+ CGF.CGM.getCXXABI().emitVirtualObjectDelete(CGF, DE, Ptr, ElementType,
+ Dtor);
+ return;
+ }
}
}
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D63161.204852.patch
Type: text/x-patch
Size: 3159 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190614/73e92ffb/attachment.bin>
More information about the cfe-commits
mailing list