1 ; RUN: opt -passes=objc-arc-contract -S < %s | FileCheck %s
3 target datalayout = "e-p:64:64:64"
5 declare ptr @llvm.objc.retain(ptr)
6 declare void @llvm.objc.release(ptr)
7 declare void @use_pointer(ptr)
9 @x = external global ptr
11 ; CHECK-LABEL: define void @test0(
13 ; CHECK-NEXT: tail call void @llvm.objc.storeStrong(ptr @x, ptr %p) [[NUW:#[0-9]+]]
14 ; CHECK-NEXT: ret void
16 define void @test0(ptr %p) {
18 %0 = tail call ptr @llvm.objc.retain(ptr %p) nounwind
19 %tmp = load ptr, ptr @x, align 8
20 store ptr %0, ptr @x, align 8
21 tail call void @llvm.objc.release(ptr %tmp) nounwind
25 ; Don't do this if the load is volatile.
27 ; CHECK-LABEL: define void @test1(ptr %p) {
29 ; CHECK-NEXT: %0 = tail call ptr @llvm.objc.retain(ptr %p) [[NUW]]
30 ; CHECK-NEXT: %tmp = load volatile ptr, ptr @x, align 8
31 ; CHECK-NEXT: store ptr %0, ptr @x, align 8
32 ; CHECK-NEXT: tail call void @llvm.objc.release(ptr %tmp) [[NUW]]
33 ; CHECK-NEXT: ret void
35 define void @test1(ptr %p) {
37 %0 = tail call ptr @llvm.objc.retain(ptr %p) nounwind
38 %tmp = load volatile ptr, ptr @x, align 8
39 store ptr %0, ptr @x, align 8
40 tail call void @llvm.objc.release(ptr %tmp) nounwind
44 ; Don't do this if the store is volatile.
46 ; CHECK-LABEL: define void @test2(ptr %p) {
48 ; CHECK-NEXT: %0 = tail call ptr @llvm.objc.retain(ptr %p) [[NUW]]
49 ; CHECK-NEXT: %tmp = load ptr, ptr @x, align 8
50 ; CHECK-NEXT: store volatile ptr %0, ptr @x, align 8
51 ; CHECK-NEXT: tail call void @llvm.objc.release(ptr %tmp) [[NUW]]
52 ; CHECK-NEXT: ret void
54 define void @test2(ptr %p) {
56 %0 = tail call ptr @llvm.objc.retain(ptr %p) nounwind
57 %tmp = load ptr, ptr @x, align 8
58 store volatile ptr %0, ptr @x, align 8
59 tail call void @llvm.objc.release(ptr %tmp) nounwind
63 ; Don't do this if there's a use of the old pointer value between the store
66 ; CHECK-LABEL: define void @test3(ptr %newValue) {
68 ; CHECK-NEXT: %x0 = tail call ptr @llvm.objc.retain(ptr %newValue) [[NUW]]
69 ; CHECK-NEXT: %x1 = load ptr, ptr @x, align 8
70 ; CHECK-NEXT: store ptr %x0, ptr @x, align 8
71 ; CHECK-NEXT: tail call void @use_pointer(ptr %x1), !clang.arc.no_objc_arc_exceptions !0
72 ; CHECK-NEXT: tail call void @llvm.objc.release(ptr %x1) [[NUW]], !clang.imprecise_release !0
73 ; CHECK-NEXT: ret void
75 define void @test3(ptr %newValue) {
77 %x0 = tail call ptr @llvm.objc.retain(ptr %newValue) nounwind
78 %x1 = load ptr, ptr @x, align 8
79 store ptr %newValue, ptr @x, align 8
80 tail call void @use_pointer(ptr %x1), !clang.arc.no_objc_arc_exceptions !0
81 tail call void @llvm.objc.release(ptr %x1) nounwind, !clang.imprecise_release !0
85 ; Like test3, but with an icmp use instead of a call, for good measure.
87 ; CHECK-LABEL: define i1 @test4(ptr %newValue, ptr %foo) {
89 ; CHECK-NEXT: %x0 = tail call ptr @llvm.objc.retain(ptr %newValue) [[NUW]]
90 ; CHECK-NEXT: %x1 = load ptr, ptr @x, align 8
91 ; CHECK-NEXT: store ptr %x0, ptr @x, align 8
92 ; CHECK-NEXT: %t = icmp eq ptr %x1, %foo
93 ; CHECK-NEXT: tail call void @llvm.objc.release(ptr %x1) [[NUW]], !clang.imprecise_release !0
94 ; CHECK-NEXT: ret i1 %t
96 define i1 @test4(ptr %newValue, ptr %foo) {
98 %x0 = tail call ptr @llvm.objc.retain(ptr %newValue) nounwind
99 %x1 = load ptr, ptr @x, align 8
100 store ptr %newValue, ptr @x, align 8
101 %t = icmp eq ptr %x1, %foo
102 tail call void @llvm.objc.release(ptr %x1) nounwind, !clang.imprecise_release !0
106 ; Do form an llvm.objc.storeStrong here, because the use is before the store.
108 ; CHECK-LABEL: define i1 @test5(ptr %newValue, ptr %foo) {
109 ; CHECK: %t = icmp eq ptr %x1, %foo
110 ; CHECK: tail call void @llvm.objc.storeStrong(ptr @x, ptr %newValue) [[NUW]]
112 define i1 @test5(ptr %newValue, ptr %foo) {
114 %x0 = tail call ptr @llvm.objc.retain(ptr %newValue) nounwind
115 %x1 = load ptr, ptr @x, align 8
116 %t = icmp eq ptr %x1, %foo
117 store ptr %newValue, ptr @x, align 8
118 tail call void @llvm.objc.release(ptr %x1) nounwind, !clang.imprecise_release !0
122 ; Like test5, but the release is before the store.
124 ; CHECK-LABEL: define i1 @test6(ptr %newValue, ptr %foo) {
125 ; CHECK: %t = icmp eq ptr %x1, %foo
126 ; CHECK: tail call void @llvm.objc.storeStrong(ptr @x, ptr %newValue) [[NUW]]
128 define i1 @test6(ptr %newValue, ptr %foo) {
130 %x0 = tail call ptr @llvm.objc.retain(ptr %newValue) nounwind
131 %x1 = load ptr, ptr @x, align 8
132 tail call void @llvm.objc.release(ptr %x1) nounwind, !clang.imprecise_release !0
133 %t = icmp eq ptr %x1, %foo
134 store ptr %newValue, ptr @x, align 8
138 ; Like test0, but there's no store, so don't form an llvm.objc.storeStrong.
140 ; CHECK-LABEL: define void @test7(
142 ; CHECK-NEXT: %0 = tail call ptr @llvm.objc.retain(ptr %p) [[NUW]]
143 ; CHECK-NEXT: %tmp = load ptr, ptr @x, align 8
144 ; CHECK-NEXT: tail call void @llvm.objc.release(ptr %tmp) [[NUW]]
145 ; CHECK-NEXT: ret void
147 define void @test7(ptr %p) {
149 %0 = tail call ptr @llvm.objc.retain(ptr %p) nounwind
150 %tmp = load ptr, ptr @x, align 8
151 tail call void @llvm.objc.release(ptr %tmp) nounwind
155 ; Like test0, but there's no retain, so don't form an llvm.objc.storeStrong.
157 ; CHECK-LABEL: define void @test8(
159 ; CHECK-NEXT: %tmp = load ptr, ptr @x, align 8
160 ; CHECK-NEXT: store ptr %p, ptr @x, align 8
161 ; CHECK-NEXT: tail call void @llvm.objc.release(ptr %tmp) [[NUW]]
162 ; CHECK-NEXT: ret void
164 define void @test8(ptr %p) {
166 %tmp = load ptr, ptr @x, align 8
167 store ptr %p, ptr @x, align 8
168 tail call void @llvm.objc.release(ptr %tmp) nounwind
172 ; Make sure that we properly handle release that *may* release our new
173 ; value in between the retain and the store. We need to be sure that
174 ; this we can safely move the retain to the store. This specific test
175 ; makes sure that we properly handled a release of an unrelated
178 ; CHECK-LABEL: define i1 @test9(ptr %newValue, ptr %foo, ptr %unrelated_ptr) {
179 ; CHECK-NOT: llvm.objc.storeStrong
180 define i1 @test9(ptr %newValue, ptr %foo, ptr %unrelated_ptr) {
182 %x0 = tail call ptr @llvm.objc.retain(ptr %newValue) nounwind
183 tail call void @llvm.objc.release(ptr %unrelated_ptr) nounwind, !clang.imprecise_release !0
184 %x1 = load ptr, ptr @x, align 8
185 tail call void @llvm.objc.release(ptr %x1) nounwind, !clang.imprecise_release !0
186 %t = icmp eq ptr %x1, %foo
187 store ptr %newValue, ptr @x, align 8
191 ; Make sure that we don't perform the optimization when we just have a call.
193 ; CHECK-LABEL: define i1 @test10(ptr %newValue, ptr %foo, ptr %unrelated_ptr) {
194 ; CHECK-NOT: llvm.objc.storeStrong
195 define i1 @test10(ptr %newValue, ptr %foo, ptr %unrelated_ptr) {
197 %x0 = tail call ptr @llvm.objc.retain(ptr %newValue) nounwind
198 call void @use_pointer(ptr %unrelated_ptr)
199 %x1 = load ptr, ptr @x, align 8
200 tail call void @llvm.objc.release(ptr %x1) nounwind, !clang.imprecise_release !0
201 %t = icmp eq ptr %x1, %foo
202 store ptr %newValue, ptr @x, align 8
206 ; Make sure we form the store strong if the use in between the retain
207 ; and the store does not touch reference counts.
208 ; CHECK-LABEL: define i1 @test11(ptr %newValue, ptr %foo, ptr %unrelated_ptr) {
209 ; CHECK: llvm.objc.storeStrong
210 define i1 @test11(ptr %newValue, ptr %foo, ptr %unrelated_ptr) {
212 %x0 = tail call ptr @llvm.objc.retain(ptr %newValue) nounwind
213 %t = icmp eq ptr %newValue, %foo
214 %x1 = load ptr, ptr @x, align 8
215 tail call void @llvm.objc.release(ptr %x1) nounwind, !clang.imprecise_release !0
216 store ptr %newValue, ptr @x, align 8
220 ; Make sure that we form the store strong even if there are bitcasts on
222 ; CHECK-LABEL: define void @test12(
224 ; CHECK-NEXT: %p16 = bitcast ptr @x to ptr
225 ; CHECK-NEXT: %tmp16 = load ptr, ptr %p16, align 8
226 ; CHECK-NEXT: %tmp8 = bitcast ptr %tmp16 to ptr
227 ; CHECK-NEXT: %p32 = bitcast ptr @x to ptr
228 ; CHECK-NEXT: %v32 = bitcast ptr %p to ptr
229 ; CHECK-NEXT: tail call void @llvm.objc.storeStrong(ptr %p16, ptr %p)
230 ; CHECK-NEXT: ret void
232 define void @test12(ptr %p) {
234 %retain = tail call ptr @llvm.objc.retain(ptr %p) nounwind
235 %p16 = bitcast ptr @x to ptr
236 %tmp16 = load ptr, ptr %p16, align 8
237 %tmp8 = bitcast ptr %tmp16 to ptr
238 %p32 = bitcast ptr @x to ptr
239 %v32 = bitcast ptr %retain to ptr
240 store ptr %v32, ptr %p32, align 8
241 tail call void @llvm.objc.release(ptr %tmp8) nounwind
245 ; This used to crash.
246 ; CHECK-LABEL: define ptr @test13(
247 ; CHECK: tail call void @llvm.objc.storeStrong(ptr %{{.*}}, ptr %[[NEW:.*]])
248 ; CHECK-NEXT: ret ptr %[[NEW]]
250 define ptr @test13(ptr %a0, ptr %a1, ptr %addr, ptr %new) {
251 %old = load ptr, ptr %addr, align 8
252 call void @llvm.objc.release(ptr %old)
253 %retained = call ptr @llvm.objc.retain(ptr %new)
254 store ptr %retained, ptr %addr, align 8
258 ; Cannot form a storeStrong call because it's unsafe to move the release call to
261 ; CHECK-LABEL: define void @test14(
262 ; CHECK: %[[V0:.*]] = load ptr, ptr %a
263 ; CHECK: %[[V1:.*]] = call ptr @llvm.objc.retain(ptr %p)
264 ; CHECK: store ptr %[[V1]], ptr %a
265 ; CHECK: %[[V2:.*]] = call ptr @llvm.objc.retain(ptr %[[V0]])
266 ; CHECK: call void @llvm.objc.release(ptr %[[V2]])
268 define void @test14(ptr %a, ptr %p) {
269 %v0 = load ptr, ptr %a, align 8
270 %v1 = call ptr @llvm.objc.retain(ptr %p)
271 store ptr %p, ptr %a, align 8
272 %v2 = call ptr @llvm.objc.retain(ptr %v0)
273 call void @llvm.objc.release(ptr %v0)
279 ; CHECK: attributes [[NUW]] = { nounwind }