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( + 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; + } } } } 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; + } +}