PR34581: Don't remove an 'if (p)' guarding a call to 'operator delete(p)' under -Oz.
Summary:
This transformation is correct for a builtin call to 'free(p)', but not
for 'operator delete(p)'. There is no guarantee that a user replacement
'operator delete' has no effect when called on a null pointer.
However, the principle behind the transformation *is* correct, and can
be applied more broadly: a 'delete p' expression is permitted to
unconditionally call 'operator delete(p)'. So do that in Clang under
-Oz where possible. We do this whether or not 'p' has trivial
destruction, since the destruction might turn out to be trivial after
inlining, and even for a class-specific (but non-virtual,
non-destroying, non-array) 'operator delete'.
Reviewers: davide, dnsampaio, rjmccall
Reviewed By: dnsampaio
Subscribers: hiraditya, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D79378
2020-05-05 07:56:47 +08:00
|
|
|
// RUN: %clang_cc1 -triple x86_64-apple-darwin10 %s -emit-llvm -o - | FileCheck %s --check-prefixes=CHECK,CHECK-NOSIZE
|
|
|
|
// RUN: %clang_cc1 -triple x86_64-apple-darwin10 %s -emit-llvm -o - -Oz -disable-llvm-passes | FileCheck %s --check-prefixes=CHECK,CHECK-SIZE
|
2009-08-17 05:13:42 +08:00
|
|
|
|
|
|
|
void t1(int *a) {
|
|
|
|
delete a;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct S {
|
|
|
|
int a;
|
|
|
|
};
|
|
|
|
|
|
|
|
// POD types.
|
PR34581: Don't remove an 'if (p)' guarding a call to 'operator delete(p)' under -Oz.
Summary:
This transformation is correct for a builtin call to 'free(p)', but not
for 'operator delete(p)'. There is no guarantee that a user replacement
'operator delete' has no effect when called on a null pointer.
However, the principle behind the transformation *is* correct, and can
be applied more broadly: a 'delete p' expression is permitted to
unconditionally call 'operator delete(p)'. So do that in Clang under
-Oz where possible. We do this whether or not 'p' has trivial
destruction, since the destruction might turn out to be trivial after
inlining, and even for a class-specific (but non-virtual,
non-destroying, non-array) 'operator delete'.
Reviewers: davide, dnsampaio, rjmccall
Reviewed By: dnsampaio
Subscribers: hiraditya, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D79378
2020-05-05 07:56:47 +08:00
|
|
|
|
|
|
|
// CHECK-LABEL: define void @_Z2t3P1S
|
2009-08-17 05:13:42 +08:00
|
|
|
void t3(S *s) {
|
PR34581: Don't remove an 'if (p)' guarding a call to 'operator delete(p)' under -Oz.
Summary:
This transformation is correct for a builtin call to 'free(p)', but not
for 'operator delete(p)'. There is no guarantee that a user replacement
'operator delete' has no effect when called on a null pointer.
However, the principle behind the transformation *is* correct, and can
be applied more broadly: a 'delete p' expression is permitted to
unconditionally call 'operator delete(p)'. So do that in Clang under
-Oz where possible. We do this whether or not 'p' has trivial
destruction, since the destruction might turn out to be trivial after
inlining, and even for a class-specific (but non-virtual,
non-destroying, non-array) 'operator delete'.
Reviewers: davide, dnsampaio, rjmccall
Reviewed By: dnsampaio
Subscribers: hiraditya, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D79378
2020-05-05 07:56:47 +08:00
|
|
|
// CHECK: icmp {{.*}} null
|
|
|
|
// CHECK: br i1
|
|
|
|
|
|
|
|
// CHECK: bitcast
|
|
|
|
// CHECK-NEXT: call void @_ZdlPv
|
|
|
|
|
|
|
|
// Check the delete is inside the 'if !null' check unless we're optimizing
|
|
|
|
// for size. FIXME: We could omit the branch entirely in this case.
|
|
|
|
// CHECK-NOSIZE-NEXT: br
|
|
|
|
// CHECK-SIZE-NEXT: ret
|
2009-08-17 05:13:42 +08:00
|
|
|
delete s;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Non-POD
|
|
|
|
struct T {
|
|
|
|
~T();
|
|
|
|
int a;
|
|
|
|
};
|
|
|
|
|
2013-08-15 14:47:53 +08:00
|
|
|
// CHECK-LABEL: define void @_Z2t4P1T
|
2009-08-17 05:13:42 +08:00
|
|
|
void t4(T *t) {
|
2010-04-20 10:18:25 +08:00
|
|
|
// CHECK: call void @_ZN1TD1Ev
|
PR34581: Don't remove an 'if (p)' guarding a call to 'operator delete(p)' under -Oz.
Summary:
This transformation is correct for a builtin call to 'free(p)', but not
for 'operator delete(p)'. There is no guarantee that a user replacement
'operator delete' has no effect when called on a null pointer.
However, the principle behind the transformation *is* correct, and can
be applied more broadly: a 'delete p' expression is permitted to
unconditionally call 'operator delete(p)'. So do that in Clang under
-Oz where possible. We do this whether or not 'p' has trivial
destruction, since the destruction might turn out to be trivial after
inlining, and even for a class-specific (but non-virtual,
non-destroying, non-array) 'operator delete'.
Reviewers: davide, dnsampaio, rjmccall
Reviewed By: dnsampaio
Subscribers: hiraditya, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D79378
2020-05-05 07:56:47 +08:00
|
|
|
// CHECK-NOSIZE-NEXT: bitcast
|
|
|
|
// CHECK-SIZE-NEXT: br
|
|
|
|
// CHECK-SIZE: bitcast
|
2010-04-20 10:18:25 +08:00
|
|
|
// CHECK-NEXT: call void @_ZdlPv
|
2009-08-17 05:13:42 +08:00
|
|
|
delete t;
|
|
|
|
}
|
2009-10-01 13:49:51 +08:00
|
|
|
|
|
|
|
// PR5102
|
|
|
|
template <typename T>
|
|
|
|
class A {
|
2011-09-21 16:36:56 +08:00
|
|
|
public: operator T *() const;
|
2009-10-01 13:49:51 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
void f() {
|
|
|
|
A<char*> a;
|
|
|
|
|
|
|
|
delete a;
|
|
|
|
}
|
2010-04-20 10:18:25 +08:00
|
|
|
|
|
|
|
namespace test0 {
|
|
|
|
struct A {
|
|
|
|
void *operator new(__SIZE_TYPE__ sz);
|
|
|
|
void operator delete(void *p) { ::operator delete(p); }
|
|
|
|
~A() {}
|
|
|
|
};
|
|
|
|
|
2013-08-15 14:47:53 +08:00
|
|
|
// CHECK-LABEL: define void @_ZN5test04testEPNS_1AE(
|
2010-04-20 10:18:25 +08:00
|
|
|
void test(A *a) {
|
|
|
|
// CHECK: call void @_ZN5test01AD1Ev
|
PR34581: Don't remove an 'if (p)' guarding a call to 'operator delete(p)' under -Oz.
Summary:
This transformation is correct for a builtin call to 'free(p)', but not
for 'operator delete(p)'. There is no guarantee that a user replacement
'operator delete' has no effect when called on a null pointer.
However, the principle behind the transformation *is* correct, and can
be applied more broadly: a 'delete p' expression is permitted to
unconditionally call 'operator delete(p)'. So do that in Clang under
-Oz where possible. We do this whether or not 'p' has trivial
destruction, since the destruction might turn out to be trivial after
inlining, and even for a class-specific (but non-virtual,
non-destroying, non-array) 'operator delete'.
Reviewers: davide, dnsampaio, rjmccall
Reviewed By: dnsampaio
Subscribers: hiraditya, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D79378
2020-05-05 07:56:47 +08:00
|
|
|
// CHECK-NOSIZE-NEXT: bitcast
|
|
|
|
// CHECK-SIZE-NEXT: br
|
|
|
|
// CHECK-SIZE: bitcast
|
2010-04-20 10:18:25 +08:00
|
|
|
// CHECK-NEXT: call void @_ZN5test01AdlEPv
|
|
|
|
delete a;
|
|
|
|
}
|
|
|
|
|
2020-11-17 07:04:55 +08:00
|
|
|
// CHECK-LABEL: define linkonce_odr void @_ZN5test01AD1Ev(%"struct.test0::A"* {{[^,]*}} %this) unnamed_addr
|
2013-08-15 14:47:53 +08:00
|
|
|
// CHECK-LABEL: define linkonce_odr void @_ZN5test01AdlEPv
|
2010-04-20 10:18:25 +08:00
|
|
|
}
|
2010-09-02 17:58:18 +08:00
|
|
|
|
|
|
|
namespace test1 {
|
|
|
|
struct A {
|
|
|
|
int x;
|
|
|
|
~A();
|
|
|
|
};
|
|
|
|
|
2013-08-15 14:47:53 +08:00
|
|
|
// CHECK-LABEL: define void @_ZN5test14testEPA10_A20_NS_1AE(
|
2010-09-02 17:58:18 +08:00
|
|
|
void test(A (*arr)[10][20]) {
|
|
|
|
delete [] arr;
|
2011-07-13 09:41:37 +08:00
|
|
|
// CHECK: icmp eq [10 x [20 x [[A:%.*]]]]* [[PTR:%.*]], null
|
2010-09-02 17:58:18 +08:00
|
|
|
// CHECK-NEXT: br i1
|
|
|
|
|
2015-02-28 03:18:17 +08:00
|
|
|
// CHECK: [[BEGIN:%.*]] = getelementptr inbounds [10 x [20 x [[A]]]], [10 x [20 x [[A]]]]* [[PTR]], i32 0, i32 0, i32 0
|
2011-07-13 09:41:37 +08:00
|
|
|
// CHECK-NEXT: [[T0:%.*]] = bitcast [[A]]* [[BEGIN]] to i8*
|
2015-02-28 03:18:17 +08:00
|
|
|
// CHECK-NEXT: [[ALLOC:%.*]] = getelementptr inbounds i8, i8* [[T0]], i64 -8
|
2011-07-13 09:41:37 +08:00
|
|
|
// CHECK-NEXT: [[T1:%.*]] = bitcast i8* [[ALLOC]] to i64*
|
2015-02-28 05:19:58 +08:00
|
|
|
// CHECK-NEXT: [[COUNT:%.*]] = load i64, i64* [[T1]]
|
2015-02-28 03:18:17 +08:00
|
|
|
// CHECK: [[END:%.*]] = getelementptr inbounds [[A]], [[A]]* [[BEGIN]], i64 [[COUNT]]
|
2011-07-13 16:09:46 +08:00
|
|
|
// CHECK-NEXT: [[ISEMPTY:%.*]] = icmp eq [[A]]* [[BEGIN]], [[END]]
|
|
|
|
// CHECK-NEXT: br i1 [[ISEMPTY]],
|
2011-07-13 09:41:37 +08:00
|
|
|
// CHECK: [[PAST:%.*]] = phi [[A]]* [ [[END]], {{%.*}} ], [ [[CUR:%.*]], {{%.*}} ]
|
2015-02-28 03:18:17 +08:00
|
|
|
// CHECK-NEXT: [[CUR:%.*]] = getelementptr inbounds [[A]], [[A]]* [[PAST]], i64 -1
|
2020-11-17 07:04:55 +08:00
|
|
|
// CHECK-NEXT: call void @_ZN5test11AD1Ev([[A]]* {{[^,]*}} [[CUR]])
|
2011-07-13 09:41:37 +08:00
|
|
|
// CHECK-NEXT: [[ISDONE:%.*]] = icmp eq [[A]]* [[CUR]], [[BEGIN]]
|
|
|
|
// CHECK-NEXT: br i1 [[ISDONE]]
|
2010-09-02 17:58:18 +08:00
|
|
|
// CHECK: call void @_ZdaPv(i8* [[ALLOC]])
|
|
|
|
}
|
|
|
|
}
|
2010-09-02 23:34:35 +08:00
|
|
|
|
|
|
|
namespace test2 {
|
2013-08-15 14:47:53 +08:00
|
|
|
// CHECK-LABEL: define void @_ZN5test21fEPb
|
2010-09-02 23:34:35 +08:00
|
|
|
void f(bool *b) {
|
|
|
|
// CHECK: call void @_ZdlPv(i8*
|
|
|
|
delete b;
|
|
|
|
// CHECK: call void @_ZdaPv(i8*
|
|
|
|
delete [] b;
|
|
|
|
}
|
|
|
|
}
|
2010-09-14 04:15:54 +08:00
|
|
|
|
|
|
|
namespace test3 {
|
|
|
|
void f(int a[10][20]) {
|
|
|
|
// CHECK: call void @_ZdaPv(i8*
|
|
|
|
delete a;
|
|
|
|
}
|
|
|
|
}
|
2011-07-13 08:54:47 +08:00
|
|
|
|
|
|
|
namespace test4 {
|
|
|
|
// PR10341: ::delete with a virtual destructor
|
|
|
|
struct X {
|
|
|
|
virtual ~X();
|
|
|
|
void operator delete (void *);
|
|
|
|
};
|
|
|
|
|
2013-08-15 14:47:53 +08:00
|
|
|
// CHECK-LABEL: define void @_ZN5test421global_delete_virtualEPNS_1XE
|
2011-07-13 08:54:47 +08:00
|
|
|
void global_delete_virtual(X *xp) {
|
2012-09-25 18:10:39 +08:00
|
|
|
// Load the offset-to-top from the vtable and apply it.
|
|
|
|
// This has to be done first because the dtor can mess it up.
|
|
|
|
// CHECK: [[T0:%.*]] = bitcast [[X:%.*]]* [[XP:%.*]] to i64**
|
2015-02-28 05:19:58 +08:00
|
|
|
// CHECK-NEXT: [[VTABLE:%.*]] = load i64*, i64** [[T0]]
|
2015-02-28 03:18:17 +08:00
|
|
|
// CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds i64, i64* [[VTABLE]], i64 -2
|
2015-02-28 05:19:58 +08:00
|
|
|
// CHECK-NEXT: [[OFFSET:%.*]] = load i64, i64* [[T0]], align 8
|
2012-09-25 18:10:39 +08:00
|
|
|
// CHECK-NEXT: [[T0:%.*]] = bitcast [[X]]* [[XP]] to i8*
|
2015-02-28 03:18:17 +08:00
|
|
|
// CHECK-NEXT: [[ALLOCATED:%.*]] = getelementptr inbounds i8, i8* [[T0]], i64 [[OFFSET]]
|
2012-09-25 18:10:39 +08:00
|
|
|
// Load the complete-object destructor (not the deleting destructor)
|
|
|
|
// and call it.
|
|
|
|
// CHECK-NEXT: [[T0:%.*]] = bitcast [[X:%.*]]* [[XP:%.*]] to void ([[X]]*)***
|
2015-02-28 05:19:58 +08:00
|
|
|
// CHECK-NEXT: [[VTABLE:%.*]] = load void ([[X]]*)**, void ([[X]]*)*** [[T0]]
|
2015-02-28 03:18:17 +08:00
|
|
|
// CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds void ([[X]]*)*, void ([[X]]*)** [[VTABLE]], i64 0
|
2015-02-28 05:19:58 +08:00
|
|
|
// CHECK-NEXT: [[DTOR:%.*]] = load void ([[X]]*)*, void ([[X]]*)** [[T0]]
|
2020-11-17 07:04:55 +08:00
|
|
|
// CHECK-NEXT: call void [[DTOR]]([[X]]* {{[^,]*}} [[OBJ:%.*]])
|
2012-09-25 18:10:39 +08:00
|
|
|
// Call the global operator delete.
|
2013-02-22 17:10:20 +08:00
|
|
|
// CHECK-NEXT: call void @_ZdlPv(i8* [[ALLOCATED]]) [[NUW:#[0-9]+]]
|
2011-07-13 08:54:47 +08:00
|
|
|
::delete xp;
|
|
|
|
}
|
|
|
|
}
|
2011-07-28 02:54:57 +08:00
|
|
|
|
|
|
|
namespace test5 {
|
|
|
|
struct Incomplete;
|
2013-08-15 14:47:53 +08:00
|
|
|
// CHECK-LABEL: define void @_ZN5test523array_delete_incompleteEPNS_10IncompleteES1_
|
2011-08-03 02:05:30 +08:00
|
|
|
void array_delete_incomplete(Incomplete *p1, Incomplete *p2) {
|
|
|
|
// CHECK: call void @_ZdlPv
|
|
|
|
delete p1;
|
2011-07-28 04:09:36 +08:00
|
|
|
// CHECK: call void @_ZdaPv
|
2011-08-03 02:05:30 +08:00
|
|
|
delete [] p2;
|
2011-07-28 02:54:57 +08:00
|
|
|
}
|
|
|
|
}
|
2013-02-22 17:10:20 +08:00
|
|
|
|
2013-07-22 07:13:17 +08:00
|
|
|
// CHECK: attributes [[NUW]] = {{[{].*}} nounwind {{.*[}]}}
|