Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / CodeGenCXX / delete.cpp
blob1a418f48b65980c074475cf031ea14a557eff67d
1 // RUN: %clang_cc1 -triple x86_64-apple-darwin10 %s -emit-llvm -o - | FileCheck %s --check-prefixes=CHECK,CHECK-NOSIZE
2 // RUN: %clang_cc1 -triple x86_64-apple-darwin10 %s -emit-llvm -o - -Oz -disable-llvm-passes | FileCheck %s --check-prefixes=CHECK,CHECK-SIZE
4 void t1(int *a) {
5 delete a;
8 struct S {
9 int a;
12 // POD types.
14 // CHECK-LABEL: define{{.*}} void @_Z2t3P1S
15 void t3(S *s) {
16 // CHECK: icmp {{.*}} null
17 // CHECK: br i1
19 // CHECK: call void @_ZdlPv
21 // Check the delete is inside the 'if !null' check unless we're optimizing
22 // for size. FIXME: We could omit the branch entirely in this case.
23 // CHECK-NOSIZE-NEXT: br
24 // CHECK-SIZE-NEXT: ret
25 delete s;
28 // Non-POD
29 struct T {
30 ~T();
31 int a;
34 // CHECK-LABEL: define{{.*}} void @_Z2t4P1T
35 void t4(T *t) {
36 // CHECK: call void @_ZN1TD1Ev
37 // CHECK-SIZE-NEXT: br
38 // CHECK: call void @_ZdlPv
39 delete t;
42 // PR5102
43 template <typename T>
44 class A {
45 public: operator T *() const;
48 void f() {
49 A<char*> a;
51 delete a;
54 namespace test0 {
55 struct A {
56 void *operator new(__SIZE_TYPE__ sz);
57 void operator delete(void *p) { ::operator delete(p); }
58 ~A() {}
61 // CHECK-LABEL: define{{.*}} void @_ZN5test04testEPNS_1AE(
62 void test(A *a) {
63 // CHECK: call void @_ZN5test01AD1Ev
64 // CHECK-SIZE-NEXT: br
65 // CHECK: call void @_ZN5test01AdlEPv
66 delete a;
69 // CHECK-LABEL: define linkonce_odr void @_ZN5test01AD1Ev(ptr {{[^,]*}} %this) unnamed_addr
70 // CHECK-LABEL: define linkonce_odr void @_ZN5test01AdlEPv
73 namespace test1 {
74 struct A {
75 int x;
76 ~A();
79 // CHECK-LABEL: define{{.*}} void @_ZN5test14testEPA10_A20_NS_1AE(
80 void test(A (*arr)[10][20]) {
81 delete [] arr;
82 // CHECK: icmp eq ptr [[PTR:%.*]], null
83 // CHECK-NEXT: br i1
85 // CHECK: [[BEGIN:%.*]] = getelementptr inbounds [10 x [20 x [[A:%.*]]]], ptr [[PTR]], i32 0, i32 0, i32 0
86 // CHECK-NEXT: [[ALLOC:%.*]] = getelementptr inbounds i8, ptr [[BEGIN]], i64 -8
87 // CHECK-NEXT: [[COUNT:%.*]] = load i64, ptr [[ALLOC]]
88 // CHECK: [[END:%.*]] = getelementptr inbounds [[A]], ptr [[BEGIN]], i64 [[COUNT]]
89 // CHECK-NEXT: [[ISEMPTY:%.*]] = icmp eq ptr [[BEGIN]], [[END]]
90 // CHECK-NEXT: br i1 [[ISEMPTY]],
91 // CHECK: [[PAST:%.*]] = phi ptr [ [[END]], {{%.*}} ], [ [[CUR:%.*]], {{%.*}} ]
92 // CHECK-NEXT: [[CUR:%.*]] = getelementptr inbounds [[A]], ptr [[PAST]], i64 -1
93 // CHECK-NEXT: call void @_ZN5test11AD1Ev(ptr {{[^,]*}} [[CUR]])
94 // CHECK-NEXT: [[ISDONE:%.*]] = icmp eq ptr [[CUR]], [[BEGIN]]
95 // CHECK-NEXT: br i1 [[ISDONE]]
96 // CHECK: call void @_ZdaPv(ptr noundef [[ALLOC]])
100 namespace test2 {
101 // CHECK-LABEL: define{{.*}} void @_ZN5test21fEPb
102 void f(bool *b) {
103 // CHECK: call void @_ZdlPv(ptr
104 delete b;
105 // CHECK: call void @_ZdaPv(ptr
106 delete [] b;
110 namespace test3 {
111 void f(int a[10][20]) {
112 // CHECK: call void @_ZdaPv(ptr
113 delete a;
117 namespace test4 {
118 // PR10341: ::delete with a virtual destructor
119 struct X {
120 virtual ~X();
121 void operator delete (void *);
124 // CHECK-LABEL: define{{.*}} void @_ZN5test421global_delete_virtualEPNS_1XE
125 void global_delete_virtual(X *xp) {
126 // Load the offset-to-top from the vtable and apply it.
127 // This has to be done first because the dtor can mess it up.
128 // CHECK: [[XP:%.*]] = load ptr, ptr [[XP_ADDR:%.*]]
129 // CHECK: [[VTABLE:%.*]] = load ptr, ptr [[XP]]
130 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds i64, ptr [[VTABLE]], i64 -2
131 // CHECK-NEXT: [[OFFSET:%.*]] = load i64, ptr [[T0]], align 8
132 // CHECK-NEXT: [[ALLOCATED:%.*]] = getelementptr inbounds i8, ptr [[XP]], i64 [[OFFSET]]
133 // Load the complete-object destructor (not the deleting destructor)
134 // and call noundef it.
135 // CHECK-NEXT: [[VTABLE:%.*]] = load ptr, ptr [[XP:%.*]]
136 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds ptr, ptr [[VTABLE]], i64 0
137 // CHECK-NEXT: [[DTOR:%.*]] = load ptr, ptr [[T0]]
138 // CHECK-NEXT: call void [[DTOR]](ptr {{[^,]*}} [[OBJ:%.*]])
139 // Call the global operator delete.
140 // CHECK-NEXT: call void @_ZdlPv(ptr noundef [[ALLOCATED]]) [[NUW:#[0-9]+]]
141 ::delete xp;
145 namespace test5 {
146 struct Incomplete;
147 // CHECK-LABEL: define{{.*}} void @_ZN5test523array_delete_incompleteEPNS_10IncompleteES1_
148 void array_delete_incomplete(Incomplete *p1, Incomplete *p2) {
149 // CHECK: call void @_ZdlPv
150 delete p1;
151 // CHECK: call void @_ZdaPv
152 delete [] p2;
156 // CHECK: attributes [[NUW]] = {{[{].*}} nounwind {{.*[}]}}