Properly model precise lifetime when given an incomplete dataflow sequence.
[oota-llvm.git] / test / Transforms / ObjCARC / basic.ll
1 ; RUN: opt -basicaa -objc-arc -S < %s | FileCheck %s
2
3 target datalayout = "e-p:64:64:64"
4
5 declare i8* @objc_retain(i8*)
6 declare i8* @objc_retainAutoreleasedReturnValue(i8*)
7 declare void @objc_release(i8*)
8 declare i8* @objc_autorelease(i8*)
9 declare i8* @objc_autoreleaseReturnValue(i8*)
10 declare void @objc_autoreleasePoolPop(i8*)
11 declare i8* @objc_autoreleasePoolPush()
12 declare i8* @objc_retainBlock(i8*)
13
14 declare i8* @objc_retainedObject(i8*)
15 declare i8* @objc_unretainedObject(i8*)
16 declare i8* @objc_unretainedPointer(i8*)
17
18 declare void @use_pointer(i8*)
19 declare void @callee()
20 declare void @callee_fnptr(void ()*)
21 declare void @invokee()
22 declare i8* @returner()
23 declare void @bar(i32 ()*)
24
25 declare void @llvm.dbg.value(metadata, i64, metadata)
26
27 declare i8* @objc_msgSend(i8*, i8*, ...)
28
29 ; Simple retain+release pair deletion, with some intervening control
30 ; flow and harmless instructions.
31
32 ; CHECK: define void @test0_precise(
33 ; CHECK: @objc_retain
34 ; CHECK: @objc_release
35 ; CHECK: }
36 define void @test0_precise(i32* %x, i1 %p) nounwind {
37 entry:
38   %a = bitcast i32* %x to i8*
39   %0 = call i8* @objc_retain(i8* %a) nounwind
40   br i1 %p, label %t, label %f
41
42 t:
43   store i8 3, i8* %a
44   %b = bitcast i32* %x to float*
45   store float 2.0, float* %b
46   br label %return
47
48 f:
49   store i32 7, i32* %x
50   br label %return
51
52 return:
53   %c = bitcast i32* %x to i8*
54   call void @objc_release(i8* %c) nounwind
55   ret void
56 }
57
58 ; CHECK: define void @test0_imprecise(
59 ; CHECK-NOT: @objc_
60 ; CHECK: }
61 define void @test0_imprecise(i32* %x, i1 %p) nounwind {
62 entry:
63   %a = bitcast i32* %x to i8*
64   %0 = call i8* @objc_retain(i8* %a) nounwind
65   br i1 %p, label %t, label %f
66
67 t:
68   store i8 3, i8* %a
69   %b = bitcast i32* %x to float*
70   store float 2.0, float* %b
71   br label %return
72
73 f:
74   store i32 7, i32* %x
75   br label %return
76
77 return:
78   %c = bitcast i32* %x to i8*
79   call void @objc_release(i8* %c) nounwind, !clang.imprecise_release !0
80   ret void
81 }
82
83 ; Like test0 but the release isn't always executed when the retain is,
84 ; so the optimization is not safe.
85
86 ; TODO: Make the objc_release's argument be %0.
87
88 ; CHECK: define void @test1_precise(
89 ; CHECK: @objc_retain(i8* %a)
90 ; CHECK: @objc_release
91 ; CHECK: }
92 define void @test1_precise(i32* %x, i1 %p, i1 %q) nounwind {
93 entry:
94   %a = bitcast i32* %x to i8*
95   %0 = call i8* @objc_retain(i8* %a) nounwind
96   br i1 %p, label %t, label %f
97
98 t:
99   store i8 3, i8* %a
100   %b = bitcast i32* %x to float*
101   store float 2.0, float* %b
102   br label %return
103
104 f:
105   store i32 7, i32* %x
106   call void @callee()
107   br i1 %q, label %return, label %alt_return
108
109 return:
110   %c = bitcast i32* %x to i8*
111   call void @objc_release(i8* %c) nounwind
112   ret void
113
114 alt_return:
115   ret void
116 }
117
118 ; CHECK: define void @test1_imprecise(
119 ; CHECK: @objc_retain(i8* %a)
120 ; CHECK: @objc_release
121 ; CHECK: }
122 define void @test1_imprecise(i32* %x, i1 %p, i1 %q) nounwind {
123 entry:
124   %a = bitcast i32* %x to i8*
125   %0 = call i8* @objc_retain(i8* %a) nounwind
126   br i1 %p, label %t, label %f
127
128 t:
129   store i8 3, i8* %a
130   %b = bitcast i32* %x to float*
131   store float 2.0, float* %b
132   br label %return
133
134 f:
135   store i32 7, i32* %x
136   call void @callee()
137   br i1 %q, label %return, label %alt_return
138
139 return:
140   %c = bitcast i32* %x to i8*
141   call void @objc_release(i8* %c) nounwind, !clang.imprecise_release !0
142   ret void
143
144 alt_return:
145   ret void
146 }
147
148
149 ; Don't do partial elimination into two different CFG diamonds.
150
151 ; CHECK: define void @test1b_precise(
152 ; CHECK: entry:
153 ; CHECK:   tail call i8* @objc_retain(i8* %x) [[NUW:#[0-9]+]]
154 ; CHECK-NOT: @objc_
155 ; CHECK: if.end5:
156 ; CHECK:   tail call void @objc_release(i8* %x) [[NUW]]
157 ; CHECK-NOT: @objc_
158 ; CHECK: }
159 define void @test1b_precise(i8* %x, i1 %p, i1 %q) {
160 entry:
161   tail call i8* @objc_retain(i8* %x) nounwind
162   br i1 %p, label %if.then, label %if.end
163
164 if.then:                                          ; preds = %entry
165   tail call void @callee()
166   br label %if.end
167
168 if.end:                                           ; preds = %if.then, %entry
169   br i1 %q, label %if.then3, label %if.end5
170
171 if.then3:                                         ; preds = %if.end
172   tail call void @use_pointer(i8* %x)
173   br label %if.end5
174
175 if.end5:                                          ; preds = %if.then3, %if.end
176   tail call void @objc_release(i8* %x) nounwind
177   ret void
178 }
179
180 ; CHECK: define void @test1b_imprecise(
181 ; CHECK: entry:
182 ; CHECK:   tail call i8* @objc_retain(i8* %x) [[NUW:#[0-9]+]]
183 ; CHECK-NOT: @objc_
184 ; CHECK: if.end5:
185 ; CHECK:   tail call void @objc_release(i8* %x) [[NUW]], !clang.imprecise_release !0
186 ; CHECK-NOT: @objc_
187 ; CHECK: }
188 define void @test1b_imprecise(i8* %x, i1 %p, i1 %q) {
189 entry:
190   tail call i8* @objc_retain(i8* %x) nounwind
191   br i1 %p, label %if.then, label %if.end
192
193 if.then:                                          ; preds = %entry
194   tail call void @callee()
195   br label %if.end
196
197 if.end:                                           ; preds = %if.then, %entry
198   br i1 %q, label %if.then3, label %if.end5
199
200 if.then3:                                         ; preds = %if.end
201   tail call void @use_pointer(i8* %x)
202   br label %if.end5
203
204 if.end5:                                          ; preds = %if.then3, %if.end
205   tail call void @objc_release(i8* %x) nounwind, !clang.imprecise_release !0
206   ret void
207 }
208
209
210 ; Like test0 but the pointer is passed to an intervening call,
211 ; so the optimization is not safe.
212
213 ; CHECK: define void @test2_precise(
214 ; CHECK: @objc_retain(i8* %a)
215 ; CHECK: @objc_release
216 ; CHECK: }
217 define void @test2_precise(i32* %x, i1 %p) nounwind {
218 entry:
219   %a = bitcast i32* %x to i8*
220   %0 = call i8* @objc_retain(i8* %a) nounwind
221   br i1 %p, label %t, label %f
222
223 t:
224   store i8 3, i8* %a
225   %b = bitcast i32* %x to float*
226   store float 2.0, float* %b
227   br label %return
228
229 f:
230   store i32 7, i32* %x
231   call void @use_pointer(i8* %0)
232   %d = bitcast i32* %x to float*
233   store float 3.0, float* %d
234   br label %return
235
236 return:
237   %c = bitcast i32* %x to i8*
238   call void @objc_release(i8* %c) nounwind
239   ret void
240 }
241
242 ; CHECK: define void @test2_imprecise(
243 ; CHECK: @objc_retain(i8* %a)
244 ; CHECK: @objc_release
245 ; CHECK: }
246 define void @test2_imprecise(i32* %x, i1 %p) nounwind {
247 entry:
248   %a = bitcast i32* %x to i8*
249   %0 = call i8* @objc_retain(i8* %a) nounwind
250   br i1 %p, label %t, label %f
251
252 t:
253   store i8 3, i8* %a
254   %b = bitcast i32* %x to float*
255   store float 2.0, float* %b
256   br label %return
257
258 f:
259   store i32 7, i32* %x
260   call void @use_pointer(i8* %0)
261   %d = bitcast i32* %x to float*
262   store float 3.0, float* %d
263   br label %return
264
265 return:
266   %c = bitcast i32* %x to i8*
267   call void @objc_release(i8* %c) nounwind, !clang.imprecise_release !0
268   ret void
269 }
270
271 ; Like test0 but the release is in a loop,
272 ; so the optimization is not safe.
273
274 ; TODO: For now, assume this can't happen.
275
276 ; CHECK: define void @test3_precise(
277 ; TODO: @objc_retain(i8* %a)
278 ; TODO: @objc_release
279 ; CHECK: }
280 define void @test3_precise(i32* %x, i1* %q) nounwind {
281 entry:
282   %a = bitcast i32* %x to i8*
283   %0 = call i8* @objc_retain(i8* %a) nounwind
284   br label %loop
285
286 loop:
287   %c = bitcast i32* %x to i8*
288   call void @objc_release(i8* %c) nounwind
289   %j = load volatile i1* %q
290   br i1 %j, label %loop, label %return
291
292 return:
293   ret void
294 }
295
296 ; CHECK: define void @test3_imprecise(
297 ; TODO: @objc_retain(i8* %a)
298 ; TODO: @objc_release
299 ; CHECK: }
300 define void @test3_imprecise(i32* %x, i1* %q) nounwind {
301 entry:
302   %a = bitcast i32* %x to i8*
303   %0 = call i8* @objc_retain(i8* %a) nounwind
304   br label %loop
305
306 loop:
307   %c = bitcast i32* %x to i8*
308   call void @objc_release(i8* %c) nounwind, !clang.imprecise_release !0
309   %j = load volatile i1* %q
310   br i1 %j, label %loop, label %return
311
312 return:
313   ret void
314 }
315
316
317 ; TODO: For now, assume this can't happen.
318
319 ; Like test0 but the retain is in a loop,
320 ; so the optimization is not safe.
321
322 ; CHECK: define void @test4_precise(
323 ; TODO: @objc_retain(i8* %a)
324 ; TODO: @objc_release
325 ; CHECK: }
326 define void @test4_precise(i32* %x, i1* %q) nounwind {
327 entry:
328   br label %loop
329
330 loop:
331   %a = bitcast i32* %x to i8*
332   %0 = call i8* @objc_retain(i8* %a) nounwind
333   %j = load volatile i1* %q
334   br i1 %j, label %loop, label %return
335
336 return:
337   %c = bitcast i32* %x to i8*
338   call void @objc_release(i8* %c) nounwind
339   ret void
340 }
341
342 ; CHECK: define void @test4_imprecise(
343 ; TODO: @objc_retain(i8* %a)
344 ; TODO: @objc_release
345 ; CHECK: }
346 define void @test4_imprecise(i32* %x, i1* %q) nounwind {
347 entry:
348   br label %loop
349
350 loop:
351   %a = bitcast i32* %x to i8*
352   %0 = call i8* @objc_retain(i8* %a) nounwind
353   %j = load volatile i1* %q
354   br i1 %j, label %loop, label %return
355
356 return:
357   %c = bitcast i32* %x to i8*
358   call void @objc_release(i8* %c) nounwind, !clang.imprecise_release !0
359   ret void
360 }
361
362
363 ; Like test0 but the pointer is conditionally passed to an intervening call,
364 ; so the optimization is not safe.
365
366 ; CHECK: define void @test5a(
367 ; CHECK: @objc_retain(i8*
368 ; CHECK: @objc_release
369 ; CHECK: }
370 define void @test5a(i32* %x, i1 %q, i8* %y) nounwind {
371 entry:
372   %a = bitcast i32* %x to i8*
373   %0 = call i8* @objc_retain(i8* %a) nounwind
374   %s = select i1 %q, i8* %y, i8* %0
375   call void @use_pointer(i8* %s)
376   store i32 7, i32* %x
377   %c = bitcast i32* %x to i8*
378   call void @objc_release(i8* %c) nounwind
379   ret void
380 }
381
382 ; CHECK: define void @test5b(
383 ; CHECK: @objc_retain(i8*
384 ; CHECK: @objc_release
385 ; CHECK: }
386 define void @test5b(i32* %x, i1 %q, i8* %y) nounwind {
387 entry:
388   %a = bitcast i32* %x to i8*
389   %0 = call i8* @objc_retain(i8* %a) nounwind
390   %s = select i1 %q, i8* %y, i8* %0
391   call void @use_pointer(i8* %s)
392   store i32 7, i32* %x
393   %c = bitcast i32* %x to i8*
394   call void @objc_release(i8* %c) nounwind, !clang.imprecise_release !0
395   ret void
396 }
397
398
399 ; retain+release pair deletion, where the release happens on two different
400 ; flow paths.
401
402 ; CHECK: define void @test6a(
403 ; CHECK: entry:
404 ; CHECK:   tail call i8* @objc_retain(
405 ; CHECK: t:
406 ; CHECK:   call void @objc_release(
407 ; CHECK: f:
408 ; CHECK:   call void @objc_release(
409 ; CHECK: return:
410 ; CHECK: }
411 define void @test6a(i32* %x, i1 %p) nounwind {
412 entry:
413   %a = bitcast i32* %x to i8*
414   %0 = call i8* @objc_retain(i8* %a) nounwind
415   br i1 %p, label %t, label %f
416
417 t:
418   store i8 3, i8* %a
419   %b = bitcast i32* %x to float*
420   store float 2.0, float* %b
421   %ct = bitcast i32* %x to i8*
422   call void @objc_release(i8* %ct) nounwind
423   br label %return
424
425 f:
426   store i32 7, i32* %x
427   call void @callee()
428   %cf = bitcast i32* %x to i8*
429   call void @objc_release(i8* %cf) nounwind
430   br label %return
431
432 return:
433   ret void
434 }
435
436 ; CHECK: define void @test6b(
437 ; CHECK-NOT: @objc_
438 ; CHECK: }
439 define void @test6b(i32* %x, i1 %p) nounwind {
440 entry:
441   %a = bitcast i32* %x to i8*
442   %0 = call i8* @objc_retain(i8* %a) nounwind
443   br i1 %p, label %t, label %f
444
445 t:
446   store i8 3, i8* %a
447   %b = bitcast i32* %x to float*
448   store float 2.0, float* %b
449   %ct = bitcast i32* %x to i8*
450   call void @objc_release(i8* %ct) nounwind, !clang.imprecise_release !0
451   br label %return
452
453 f:
454   store i32 7, i32* %x
455   call void @callee()
456   %cf = bitcast i32* %x to i8*
457   call void @objc_release(i8* %cf) nounwind, !clang.imprecise_release !0
458   br label %return
459
460 return:
461   ret void
462 }
463
464 ; CHECK: define void @test6c(
465 ; CHECK: entry:
466 ; CHECK:   tail call i8* @objc_retain(
467 ; CHECK: t:
468 ; CHECK:   call void @objc_release(
469 ; CHECK: f:
470 ; CHECK:   call void @objc_release(
471 ; CHECK: return:
472 ; CHECK: }
473 define void @test6c(i32* %x, i1 %p) nounwind {
474 entry:
475   %a = bitcast i32* %x to i8*
476   %0 = call i8* @objc_retain(i8* %a) nounwind
477   br i1 %p, label %t, label %f
478
479 t:
480   store i8 3, i8* %a
481   %b = bitcast i32* %x to float*
482   store float 2.0, float* %b
483   %ct = bitcast i32* %x to i8*
484   call void @objc_release(i8* %ct) nounwind
485   br label %return
486
487 f:
488   store i32 7, i32* %x
489   call void @callee()
490   %cf = bitcast i32* %x to i8*
491   call void @objc_release(i8* %cf) nounwind, !clang.imprecise_release !0
492   br label %return
493
494 return:
495   ret void
496 }
497
498 ; CHECK: define void @test6d(
499 ; CHECK: entry:
500 ; CHECK:   tail call i8* @objc_retain(
501 ; CHECK: t:
502 ; CHECK:   call void @objc_release(
503 ; CHECK: f:
504 ; CHECK:   call void @objc_release(
505 ; CHECK: return:
506 ; CHECK: }
507 define void @test6d(i32* %x, i1 %p) nounwind {
508 entry:
509   %a = bitcast i32* %x to i8*
510   %0 = call i8* @objc_retain(i8* %a) nounwind
511   br i1 %p, label %t, label %f
512
513 t:
514   store i8 3, i8* %a
515   %b = bitcast i32* %x to float*
516   store float 2.0, float* %b
517   %ct = bitcast i32* %x to i8*
518   call void @objc_release(i8* %ct) nounwind, !clang.imprecise_release !0
519   br label %return
520
521 f:
522   store i32 7, i32* %x
523   call void @callee()
524   %cf = bitcast i32* %x to i8*
525   call void @objc_release(i8* %cf) nounwind
526   br label %return
527
528 return:
529   ret void
530 }
531
532
533 ; retain+release pair deletion, where the retain happens on two different
534 ; flow paths.
535
536 ; CHECK:     define void @test7(
537 ; CHECK:     entry:
538 ; CHECK-NOT:   objc_
539 ; CHECK:     t:
540 ; CHECK:       call i8* @objc_retain
541 ; CHECK:     f:
542 ; CHECK:       call i8* @objc_retain
543 ; CHECK:     return:
544 ; CHECK:       call void @objc_release
545 ; CHECK: }
546 define void @test7(i32* %x, i1 %p) nounwind {
547 entry:
548   %a = bitcast i32* %x to i8*
549   br i1 %p, label %t, label %f
550
551 t:
552   %0 = call i8* @objc_retain(i8* %a) nounwind
553   store i8 3, i8* %a
554   %b = bitcast i32* %x to float*
555   store float 2.0, float* %b
556   br label %return
557
558 f:
559   %1 = call i8* @objc_retain(i8* %a) nounwind
560   store i32 7, i32* %x
561   call void @callee()
562   br label %return
563
564 return:
565   %c = bitcast i32* %x to i8*
566   call void @objc_release(i8* %c) nounwind
567   ret void
568 }
569
570 ; CHECK: define void @test7b(
571 ; CHECK-NOT: @objc_
572 ; CHECK: }
573 define void @test7b(i32* %x, i1 %p) nounwind {
574 entry:
575   %a = bitcast i32* %x to i8*
576   br i1 %p, label %t, label %f
577
578 t:
579   %0 = call i8* @objc_retain(i8* %a) nounwind
580   store i8 3, i8* %a
581   %b = bitcast i32* %x to float*
582   store float 2.0, float* %b
583   br label %return
584
585 f:
586   %1 = call i8* @objc_retain(i8* %a) nounwind
587   store i32 7, i32* %x
588   call void @callee()
589   br label %return
590
591 return:
592   %c = bitcast i32* %x to i8*
593   call void @objc_release(i8* %c) nounwind, !clang.imprecise_release !0
594   ret void
595 }
596
597 ; Like test7, but there's a retain/retainBlock mismatch. Don't delete!
598
599 ; CHECK: define void @test7c
600 ; CHECK: t:
601 ; CHECK:   call i8* @objc_retainBlock
602 ; CHECK: f:
603 ; CHECK:   call i8* @objc_retain
604 ; CHECK: return:
605 ; CHECK:   call void @objc_release
606 ; CHECK: }
607 define void @test7c(i32* %x, i1 %p) nounwind {
608 entry:
609   %a = bitcast i32* %x to i8*
610   br i1 %p, label %t, label %f
611
612 t:
613   %0 = call i8* @objc_retainBlock(i8* %a) nounwind
614   store i8 3, i8* %a
615   %b = bitcast i32* %x to float*
616   store float 2.0, float* %b
617   br label %return
618
619 f:
620   %1 = call i8* @objc_retain(i8* %a) nounwind
621   store i32 7, i32* %x
622   call void @callee()
623   br label %return
624
625 return:
626   %c = bitcast i32* %x to i8*
627   call void @objc_release(i8* %c) nounwind
628   ret void
629 }
630
631 ; retain+release pair deletion, where the retain and release both happen on
632 ; different flow paths. Wild!
633
634 ; CHECK: define void @test8a(
635 ; CHECK: entry:
636 ; CHECK: t:
637 ; CHECK:   @objc_retain
638 ; CHECK: f:
639 ; CHECK:   @objc_retain
640 ; CHECK: mid:
641 ; CHECK: u:
642 ; CHECK:   @objc_release
643 ; CHECK: g:
644 ; CHECK:   @objc_release
645 ; CHECK: return:
646 ; CHECK: }
647 define void @test8a(i32* %x, i1 %p, i1 %q) nounwind {
648 entry:
649   %a = bitcast i32* %x to i8*
650   br i1 %p, label %t, label %f
651
652 t:
653   %0 = call i8* @objc_retain(i8* %a) nounwind
654   store i8 3, i8* %a
655   %b = bitcast i32* %x to float*
656   store float 2.0, float* %b
657   br label %mid
658
659 f:
660   %1 = call i8* @objc_retain(i8* %a) nounwind
661   store i32 7, i32* %x
662   br label %mid
663
664 mid:
665   br i1 %q, label %u, label %g
666
667 u:
668   call void @callee()
669   %cu = bitcast i32* %x to i8*
670   call void @objc_release(i8* %cu) nounwind
671   br label %return
672
673 g:
674   %cg = bitcast i32* %x to i8*
675   call void @objc_release(i8* %cg) nounwind
676   br label %return
677
678 return:
679   ret void
680 }
681
682 ; CHECK: define void @test8b(
683 ; CHECK-NOT: @objc_
684 ; CHECK: }
685 define void @test8b(i32* %x, i1 %p, i1 %q) nounwind {
686 entry:
687   %a = bitcast i32* %x to i8*
688   br i1 %p, label %t, label %f
689
690 t:
691   %0 = call i8* @objc_retain(i8* %a) nounwind
692   store i8 3, i8* %a
693   %b = bitcast i32* %x to float*
694   store float 2.0, float* %b
695   br label %mid
696
697 f:
698   %1 = call i8* @objc_retain(i8* %a) nounwind
699   store i32 7, i32* %x
700   br label %mid
701
702 mid:
703   br i1 %q, label %u, label %g
704
705 u:
706   call void @callee()
707   %cu = bitcast i32* %x to i8*
708   call void @objc_release(i8* %cu) nounwind, !clang.imprecise_release !0
709   br label %return
710
711 g:
712   %cg = bitcast i32* %x to i8*
713   call void @objc_release(i8* %cg) nounwind, !clang.imprecise_release !0
714   br label %return
715
716 return:
717   ret void
718 }
719
720 ; CHECK: define void @test8c(
721 ; CHECK: entry:
722 ; CHECK: t:
723 ; CHECK:   @objc_retain
724 ; CHECK: f:
725 ; CHECK:   @objc_retain
726 ; CHECK: mid:
727 ; CHECK: u:
728 ; CHECK:   @objc_release
729 ; CHECK: g:
730 ; CHECK:   @objc_release
731 ; CHECK: return:
732 ; CHECK: }
733 define void @test8c(i32* %x, i1 %p, i1 %q) nounwind {
734 entry:
735   %a = bitcast i32* %x to i8*
736   br i1 %p, label %t, label %f
737
738 t:
739   %0 = call i8* @objc_retain(i8* %a) nounwind
740   store i8 3, i8* %a
741   %b = bitcast i32* %x to float*
742   store float 2.0, float* %b
743   br label %mid
744
745 f:
746   %1 = call i8* @objc_retain(i8* %a) nounwind
747   store i32 7, i32* %x
748   br label %mid
749
750 mid:
751   br i1 %q, label %u, label %g
752
753 u:
754   call void @callee()
755   %cu = bitcast i32* %x to i8*
756   call void @objc_release(i8* %cu) nounwind
757   br label %return
758
759 g:
760   %cg = bitcast i32* %x to i8*
761   call void @objc_release(i8* %cg) nounwind, !clang.imprecise_release !0
762   br label %return
763
764 return:
765   ret void
766 }
767
768 ; CHECK: define void @test8d(
769 ; CHECK: entry:
770 ; CHECK: t:
771 ; CHECK:   @objc_retain
772 ; CHECK: f:
773 ; CHECK:   @objc_retain
774 ; CHECK: mid:
775 ; CHECK: u:
776 ; CHECK:   @objc_release
777 ; CHECK: g:
778 ; CHECK:   @objc_release
779 ; CHECK: return:
780 ; CHECK: }
781 define void @test8d(i32* %x, i1 %p, i1 %q) nounwind {
782 entry:
783   %a = bitcast i32* %x to i8*
784   br i1 %p, label %t, label %f
785
786 t:
787   %0 = call i8* @objc_retain(i8* %a) nounwind
788   store i8 3, i8* %a
789   %b = bitcast i32* %x to float*
790   store float 2.0, float* %b
791   br label %mid
792
793 f:
794   %1 = call i8* @objc_retain(i8* %a) nounwind
795   store i32 7, i32* %x
796   br label %mid
797
798 mid:
799   br i1 %q, label %u, label %g
800
801 u:
802   call void @callee()
803   %cu = bitcast i32* %x to i8*
804   call void @objc_release(i8* %cu) nounwind, !clang.imprecise_release !0
805   br label %return
806
807 g:
808   %cg = bitcast i32* %x to i8*
809   call void @objc_release(i8* %cg) nounwind
810   br label %return
811
812 return:
813   ret void
814 }
815
816 ; Trivial retain+release pair deletion.
817
818 ; CHECK: define void @test9(
819 ; CHECK-NOT: @objc_
820 ; CHECK: }
821 define void @test9(i8* %x) nounwind {
822 entry:
823   %0 = call i8* @objc_retain(i8* %x) nounwind
824   call void @objc_release(i8* %0) nounwind
825   ret void
826 }
827
828 ; Retain+release pair, but on an unknown pointer relationship. Don't delete!
829
830 ; CHECK: define void @test9b
831 ; CHECK: @objc_retain(i8* %x)
832 ; CHECK: @objc_release(i8* %s)
833 ; CHECK: }
834 define void @test9b(i8* %x, i1 %j, i8* %p) nounwind {
835 entry:
836   %0 = call i8* @objc_retain(i8* %x) nounwind
837   %s = select i1 %j, i8* %x, i8* %p
838   call void @objc_release(i8* %s) nounwind
839   ret void
840 }
841
842 ; Trivial retain+release pair with intervening calls - don't delete!
843
844 ; CHECK: define void @test10(
845 ; CHECK: @objc_retain(i8* %x)
846 ; CHECK: @callee
847 ; CHECK: @use_pointer
848 ; CHECK: @objc_release
849 ; CHECK: }
850 define void @test10(i8* %x) nounwind {
851 entry:
852   %0 = call i8* @objc_retain(i8* %x) nounwind
853   call void @callee()
854   call void @use_pointer(i8* %x)
855   call void @objc_release(i8* %0) nounwind
856   ret void
857 }
858
859 ; Trivial retain+autoreleaserelease pair. Don't delete!
860 ; Also, add a tail keyword, since objc_retain can never be passed
861 ; a stack argument.
862
863 ; CHECK: define void @test11(
864 ; CHECK: tail call i8* @objc_retain(i8* %x) [[NUW]]
865 ; CHECK: call i8* @objc_autorelease(i8* %0) [[NUW]]
866 ; CHECK: }
867 define void @test11(i8* %x) nounwind {
868 entry:
869   %0 = call i8* @objc_retain(i8* %x) nounwind
870   call i8* @objc_autorelease(i8* %0) nounwind
871   call void @use_pointer(i8* %x)
872   ret void
873 }
874
875 ; Same as test11 but with no use_pointer call. Delete the pair!
876
877 ; CHECK: define void @test11a(
878 ; CHECK: entry:
879 ; CHECK-NEXT: ret void
880 ; CHECK: }
881 define void @test11a(i8* %x) nounwind {
882 entry:
883   %0 = call i8* @objc_retain(i8* %x) nounwind
884   call i8* @objc_autorelease(i8* %0) nounwind
885   ret void
886 }
887
888 ; Same as test11 but the value is returned. Do not perform an RV optimization
889 ; since if the frontend emitted code for an __autoreleasing variable, we may
890 ; want it to be in the autorelease pool.
891
892 ; CHECK: define i8* @test11b(
893 ; CHECK: tail call i8* @objc_retain(i8* %x) [[NUW]]
894 ; CHECK: call i8* @objc_autorelease(i8* %0) [[NUW]]
895 ; CHECK: }
896 define i8* @test11b(i8* %x) nounwind {
897 entry:
898   %0 = call i8* @objc_retain(i8* %x) nounwind
899   call i8* @objc_autorelease(i8* %0) nounwind
900   ret i8* %x
901 }
902
903 ; Trivial retain,release pair with intervening call, but it's dominated
904 ; by another retain - delete!
905
906 ; CHECK: define void @test12(
907 ; CHECK-NEXT: entry:
908 ; CHECK-NEXT: @objc_retain(i8* %x)
909 ; CHECK-NOT: @objc_
910 ; CHECK: }
911 define void @test12(i8* %x, i64 %n) {
912 entry:
913   call i8* @objc_retain(i8* %x) nounwind
914   call i8* @objc_retain(i8* %x) nounwind
915   call void @use_pointer(i8* %x)
916   call void @use_pointer(i8* %x)
917   call void @objc_release(i8* %x) nounwind
918   ret void
919 }
920
921 ; Trivial retain,autorelease pair. Don't delete!
922
923 ; CHECK: define void @test13(
924 ; CHECK: tail call i8* @objc_retain(i8* %x) [[NUW]]
925 ; CHECK: tail call i8* @objc_retain(i8* %x) [[NUW]]
926 ; CHECK: @use_pointer(i8* %x)
927 ; CHECK: call i8* @objc_autorelease(i8* %x) [[NUW]]
928 ; CHECK: }
929 define void @test13(i8* %x, i64 %n) {
930 entry:
931   call i8* @objc_retain(i8* %x) nounwind
932   call i8* @objc_retain(i8* %x) nounwind
933   call void @use_pointer(i8* %x)
934   call i8* @objc_autorelease(i8* %x) nounwind
935   ret void
936 }
937
938 ; Delete the retain+release pair.
939
940 ; CHECK: define void @test13b
941 ; CHECK-NEXT: entry:
942 ; CHECK-NEXT: @objc_retain(i8* %x)
943 ; CHECK-NEXT: @use_pointer
944 ; CHECK-NEXT: @use_pointer
945 ; CHECK-NEXT: ret void
946 define void @test13b(i8* %x, i64 %n) {
947 entry:
948   call i8* @objc_retain(i8* %x) nounwind
949   call i8* @objc_retain(i8* %x) nounwind
950   call void @use_pointer(i8* %x)
951   call void @use_pointer(i8* %x)
952   call void @objc_release(i8* %x) nounwind
953   ret void
954 }
955
956 ; Don't delete the retain+release pair because there's an
957 ; autoreleasePoolPop in the way.
958
959 ; CHECK: define void @test13c
960 ; CHECK: @objc_retain(i8* %x)
961 ; CHECK: @objc_autoreleasePoolPop
962 ; CHECK: @objc_retain(i8* %x)
963 ; CHECK: @use_pointer
964 ; CHECK: @objc_release
965 ; CHECK: }
966 define void @test13c(i8* %x, i64 %n) {
967 entry:
968   call i8* @objc_retain(i8* %x) nounwind
969   call void @objc_autoreleasePoolPop(i8* undef)
970   call i8* @objc_retain(i8* %x) nounwind
971   call void @use_pointer(i8* %x)
972   call void @use_pointer(i8* %x)
973   call void @objc_release(i8* %x) nounwind
974   ret void
975 }
976
977 ; Like test13c, but there's an autoreleasePoolPush in the way, but that
978 ; doesn't matter.
979
980 ; CHECK: define void @test13d
981 ; CHECK-NEXT: entry:
982 ; CHECK-NEXT: @objc_retain(i8* %x)
983 ; CHECK-NEXT: @objc_autoreleasePoolPush
984 ; CHECK-NEXT: @use_pointer
985 ; CHECK-NEXT: @use_pointer
986 ; CHECK-NEXT: ret void
987 define void @test13d(i8* %x, i64 %n) {
988 entry:
989   call i8* @objc_retain(i8* %x) nounwind
990   call i8* @objc_autoreleasePoolPush()
991   call i8* @objc_retain(i8* %x) nounwind
992   call void @use_pointer(i8* %x)
993   call void @use_pointer(i8* %x)
994   call void @objc_release(i8* %x) nounwind
995   ret void
996 }
997
998 ; Trivial retain,release pair with intervening call, but it's post-dominated
999 ; by another release - delete!
1000
1001 ; CHECK: define void @test14(
1002 ; CHECK-NEXT: entry:
1003 ; CHECK-NEXT: @use_pointer
1004 ; CHECK-NEXT: @use_pointer
1005 ; CHECK-NEXT: @objc_release
1006 ; CHECK-NEXT: ret void
1007 ; CHECK-NEXT: }
1008 define void @test14(i8* %x, i64 %n) {
1009 entry:
1010   call i8* @objc_retain(i8* %x) nounwind
1011   call void @use_pointer(i8* %x)
1012   call void @use_pointer(i8* %x)
1013   call void @objc_release(i8* %x) nounwind
1014   call void @objc_release(i8* %x) nounwind
1015   ret void
1016 }
1017
1018 ; Trivial retain,autorelease pair with intervening call, but it's post-dominated
1019 ; by another release. Don't delete anything.
1020
1021 ; CHECK: define void @test15(
1022 ; CHECK-NEXT: entry:
1023 ; CHECK-NEXT: @objc_retain(i8* %x)
1024 ; CHECK-NEXT: @use_pointer
1025 ; CHECK-NEXT: @objc_autorelease(i8* %x)
1026 ; CHECK-NEXT: @objc_release
1027 ; CHECK-NEXT: ret void
1028 ; CHECK-NEXT: }
1029 define void @test15(i8* %x, i64 %n) {
1030 entry:
1031   call i8* @objc_retain(i8* %x) nounwind
1032   call void @use_pointer(i8* %x)
1033   call i8* @objc_autorelease(i8* %x) nounwind
1034   call void @objc_release(i8* %x) nounwind
1035   ret void
1036 }
1037
1038 ; Trivial retain,autorelease pair, post-dominated
1039 ; by another release. Delete the retain and release.
1040
1041 ; CHECK: define void @test15b
1042 ; CHECK-NEXT: entry:
1043 ; CHECK-NEXT: @objc_retain
1044 ; CHECK-NEXT: @objc_autorelease
1045 ; CHECK-NEXT: @objc_release
1046 ; CHECK-NEXT: ret void
1047 ; CHECK-NEXT: }
1048 define void @test15b(i8* %x, i64 %n) {
1049 entry:
1050   call i8* @objc_retain(i8* %x) nounwind
1051   call i8* @objc_autorelease(i8* %x) nounwind
1052   call void @objc_release(i8* %x) nounwind
1053   ret void
1054 }
1055
1056 ; CHECK: define void @test15c
1057 ; CHECK-NEXT: entry:
1058 ; CHECK-NEXT: @objc_autorelease
1059 ; CHECK-NEXT: ret void
1060 ; CHECK-NEXT: }
1061 define void @test15c(i8* %x, i64 %n) {
1062 entry:
1063   call i8* @objc_retain(i8* %x) nounwind
1064   call i8* @objc_autorelease(i8* %x) nounwind
1065   call void @objc_release(i8* %x) nounwind, !clang.imprecise_release !0
1066   ret void
1067 }
1068
1069 ; Retain+release pairs in diamonds, all dominated by a retain.
1070
1071 ; CHECK: define void @test16a(
1072 ; CHECK: @objc_retain(i8* %x)
1073 ; CHECK-NOT: @objc
1074 ; CHECK: }
1075 define void @test16a(i1 %a, i1 %b, i8* %x) {
1076 entry:
1077   call i8* @objc_retain(i8* %x) nounwind
1078   br i1 %a, label %red, label %orange
1079
1080 red:
1081   call i8* @objc_retain(i8* %x) nounwind
1082   br label %yellow
1083
1084 orange:
1085   call i8* @objc_retain(i8* %x) nounwind
1086   br label %yellow
1087
1088 yellow:
1089   call void @use_pointer(i8* %x)
1090   call void @use_pointer(i8* %x)
1091   br i1 %b, label %green, label %blue
1092
1093 green:
1094   call void @objc_release(i8* %x) nounwind
1095   br label %purple
1096
1097 blue:
1098   call void @objc_release(i8* %x) nounwind
1099   br label %purple
1100
1101 purple:
1102   ret void
1103 }
1104
1105 ; CHECK: define void @test16b(
1106 ; CHECK: @objc_retain(i8* %x)
1107 ; CHECK-NOT: @objc
1108 ; CHECK: }
1109 define void @test16b(i1 %a, i1 %b, i8* %x) {
1110 entry:
1111   call i8* @objc_retain(i8* %x) nounwind
1112   br i1 %a, label %red, label %orange
1113
1114 red:
1115   call i8* @objc_retain(i8* %x) nounwind
1116   br label %yellow
1117
1118 orange:
1119   call i8* @objc_retain(i8* %x) nounwind
1120   br label %yellow
1121
1122 yellow:
1123   call void @use_pointer(i8* %x)
1124   call void @use_pointer(i8* %x)
1125   br i1 %b, label %green, label %blue
1126
1127 green:
1128   call void @objc_release(i8* %x) nounwind, !clang.imprecise_release !0
1129   br label %purple
1130
1131 blue:
1132   call void @objc_release(i8* %x) nounwind
1133   br label %purple
1134
1135 purple:
1136   ret void
1137 }
1138
1139 ; CHECK: define void @test16c(
1140 ; CHECK: @objc_retain(i8* %x)
1141 ; CHECK-NOT: @objc
1142 ; CHECK: }
1143 define void @test16c(i1 %a, i1 %b, i8* %x) {
1144 entry:
1145   call i8* @objc_retain(i8* %x) nounwind
1146   br i1 %a, label %red, label %orange
1147
1148 red:
1149   call i8* @objc_retain(i8* %x) nounwind
1150   br label %yellow
1151
1152 orange:
1153   call i8* @objc_retain(i8* %x) nounwind
1154   br label %yellow
1155
1156 yellow:
1157   call void @use_pointer(i8* %x)
1158   call void @use_pointer(i8* %x)
1159   br i1 %b, label %green, label %blue
1160
1161 green:
1162   call void @objc_release(i8* %x) nounwind, !clang.imprecise_release !0
1163   br label %purple
1164
1165 blue:
1166   call void @objc_release(i8* %x) nounwind, !clang.imprecise_release !0
1167   br label %purple
1168
1169 purple:
1170   ret void
1171 }
1172
1173 ; CHECK: define void @test16d(
1174 ; CHECK: @objc_retain(i8* %x)
1175 ; CHECK-NOT: @objc
1176 ; CHECK: }
1177 define void @test16d(i1 %a, i1 %b, i8* %x) {
1178 entry:
1179   call i8* @objc_retain(i8* %x) nounwind
1180   br i1 %a, label %red, label %orange
1181
1182 red:
1183   call i8* @objc_retain(i8* %x) nounwind
1184   br label %yellow
1185
1186 orange:
1187   call i8* @objc_retain(i8* %x) nounwind
1188   br label %yellow
1189
1190 yellow:
1191   call void @use_pointer(i8* %x)
1192   call void @use_pointer(i8* %x)
1193   br i1 %b, label %green, label %blue
1194
1195 green:
1196   call void @objc_release(i8* %x) nounwind
1197   br label %purple
1198
1199 blue:
1200   call void @objc_release(i8* %x) nounwind, !clang.imprecise_release !0
1201   br label %purple
1202
1203 purple:
1204   ret void
1205 }
1206
1207
1208 ; Retain+release pairs in diamonds, all post-dominated by a release.
1209
1210 ; CHECK: define void @test17(
1211 ; CHECK-NOT: @objc_
1212 ; CHECK: purple:
1213 ; CHECK: @objc_release
1214 ; CHECK: }
1215 define void @test17(i1 %a, i1 %b, i8* %x) {
1216 entry:
1217   br i1 %a, label %red, label %orange
1218
1219 red:
1220   call i8* @objc_retain(i8* %x) nounwind
1221   br label %yellow
1222
1223 orange:
1224   call i8* @objc_retain(i8* %x) nounwind
1225   br label %yellow
1226
1227 yellow:
1228   call void @use_pointer(i8* %x)
1229   call void @use_pointer(i8* %x)
1230   br i1 %b, label %green, label %blue
1231
1232 green:
1233   call void @objc_release(i8* %x) nounwind
1234   br label %purple
1235
1236 blue:
1237   call void @objc_release(i8* %x) nounwind
1238   br label %purple
1239
1240 purple:
1241   call void @objc_release(i8* %x) nounwind
1242   ret void
1243 }
1244
1245 ; Delete no-ops.
1246
1247 ; CHECK: define void @test18(
1248 ; CHECK-NOT: @objc_
1249 ; CHECK: }
1250 define void @test18() {
1251   call i8* @objc_retain(i8* null)
1252   call void @objc_release(i8* null)
1253   call i8* @objc_autorelease(i8* null)
1254   ret void
1255 }
1256
1257 ; Delete no-ops where undef can be assumed to be null.
1258
1259 ; CHECK: define void @test18b
1260 ; CHECK-NOT: @objc_
1261 ; CHECK: }
1262 define void @test18b() {
1263   call i8* @objc_retain(i8* undef)
1264   call void @objc_release(i8* undef)
1265   call i8* @objc_autorelease(i8* undef)
1266   ret void
1267 }
1268
1269 ; Replace uses of arguments with uses of return values, to reduce
1270 ; register pressure.
1271
1272 ; CHECK: define void @test19(i32* %y) {
1273 ; CHECK:   %z = bitcast i32* %y to i8*
1274 ; CHECK:   %0 = bitcast i32* %y to i8*
1275 ; CHECK:   %1 = tail call i8* @objc_retain(i8* %0)
1276 ; CHECK:   call void @use_pointer(i8* %z)
1277 ; CHECK:   call void @use_pointer(i8* %z)
1278 ; CHECK:   %2 = bitcast i32* %y to i8*
1279 ; CHECK:   call void @objc_release(i8* %2)
1280 ; CHECK:   ret void
1281 ; CHECK: }
1282 define void @test19(i32* %y) {
1283 entry:
1284   %x = bitcast i32* %y to i8*
1285   %0 = call i8* @objc_retain(i8* %x) nounwind
1286   %z = bitcast i32* %y to i8*
1287   call void @use_pointer(i8* %z)
1288   call void @use_pointer(i8* %z)
1289   call void @objc_release(i8* %x)
1290   ret void
1291 }
1292
1293 ; Bitcast insertion
1294
1295 ; CHECK: define void @test20(
1296 ; CHECK: %tmp1 = tail call i8* @objc_retain(i8* %tmp) [[NUW]]
1297 ; CHECK-NEXT: invoke
1298 define void @test20(double* %self) {
1299 if.then12:
1300   %tmp = bitcast double* %self to i8*
1301   %tmp1 = call i8* @objc_retain(i8* %tmp) nounwind
1302   invoke void @invokee()
1303           to label %invoke.cont23 unwind label %lpad20
1304
1305 invoke.cont23:                                    ; preds = %if.then12
1306   invoke void @invokee()
1307           to label %if.end unwind label %lpad20
1308
1309 lpad20:                                           ; preds = %invoke.cont23, %if.then12
1310   %tmp502 = phi double* [ undef, %invoke.cont23 ], [ %self, %if.then12 ]
1311   %exn = landingpad {i8*, i32} personality i32 (...)* @__gxx_personality_v0
1312            cleanup
1313   unreachable
1314
1315 if.end:                                           ; preds = %invoke.cont23
1316   ret void
1317 }
1318
1319 ; Delete a redundant retain,autorelease when forwaring a call result
1320 ; directly to a return value.
1321
1322 ; CHECK: define i8* @test21(
1323 ; CHECK: call i8* @returner()
1324 ; CHECK-NEXT: ret i8* %call
1325 define i8* @test21() {
1326 entry:
1327   %call = call i8* @returner()
1328   %0 = call i8* @objc_retain(i8* %call) nounwind
1329   %1 = call i8* @objc_autorelease(i8* %0) nounwind
1330   ret i8* %1
1331 }
1332
1333 ; Move an objc call up through a phi that has null operands.
1334
1335 ; CHECK: define void @test22(
1336 ; CHECK: B:
1337 ; CHECK:   %1 = bitcast double* %p to i8*
1338 ; CHECK:   call void @objc_release(i8* %1)
1339 ; CHECK:   br label %C
1340 ; CHECK: C:                                                ; preds = %B, %A
1341 ; CHECK-NOT: @objc_release
1342 ; CHECK: }
1343 define void @test22(double* %p, i1 %a) {
1344   br i1 %a, label %A, label %B
1345 A:
1346   br label %C
1347 B:
1348   br label %C
1349 C:
1350   %h = phi double* [ null, %A ], [ %p, %B ]
1351   %c = bitcast double* %h to i8*
1352   call void @objc_release(i8* %c)
1353   ret void
1354 }
1355
1356 ; Optimize objc_retainBlock.
1357
1358 ; CHECK: define void @test23(
1359 ; CHECK-NOT: @objc_
1360 ; CHECK: }
1361 %block0 = type { i64, i64, i8*, i8* }
1362 %block1 = type { i8**, i32, i32, i32 (%struct.__block_literal_1*)*, %block0* }
1363 %struct.__block_descriptor = type { i64, i64 }
1364 %struct.__block_literal_1 = type { i8**, i32, i32, i8**, %struct.__block_descriptor* }
1365 @__block_holder_tmp_1 = external constant %block1
1366 define void @test23() {
1367 entry:
1368   %0 = call i8* @objc_retainBlock(i8* bitcast (%block1* @__block_holder_tmp_1 to i8*)) nounwind, !clang.arc.copy_on_escape !0
1369   call void @bar(i32 ()* bitcast (%block1* @__block_holder_tmp_1 to i32 ()*))
1370   call void @bar(i32 ()* bitcast (%block1* @__block_holder_tmp_1 to i32 ()*))
1371   call void @objc_release(i8* bitcast (%block1* @__block_holder_tmp_1 to i8*)) nounwind
1372   ret void
1373 }
1374
1375 ; Don't optimize objc_retainBlock, but do strength reduce it.
1376
1377 ; CHECK: define void @test23b
1378 ; CHECK: @objc_retain
1379 ; CHECK: @objc_release
1380 ; CHECK: }
1381 define void @test23b(i8* %p) {
1382 entry:
1383   %0 = call i8* @objc_retainBlock(i8* %p) nounwind, !clang.arc.copy_on_escape !0
1384   call void @callee()
1385   call void @use_pointer(i8* %p)
1386   call void @objc_release(i8* %p) nounwind
1387   ret void
1388 }
1389
1390 ; Don't optimize objc_retainBlock, because there's no copy_on_escape metadata.
1391
1392 ; CHECK: define void @test23c(
1393 ; CHECK: @objc_retainBlock
1394 ; CHECK: @objc_release
1395 ; CHECK: }
1396 define void @test23c() {
1397 entry:
1398   %0 = call i8* @objc_retainBlock(i8* bitcast (%block1* @__block_holder_tmp_1 to i8*)) nounwind
1399   call void @bar(i32 ()* bitcast (%block1* @__block_holder_tmp_1 to i32 ()*))
1400   call void @bar(i32 ()* bitcast (%block1* @__block_holder_tmp_1 to i32 ()*))
1401   call void @objc_release(i8* bitcast (%block1* @__block_holder_tmp_1 to i8*)) nounwind
1402   ret void
1403 }
1404
1405 ; Any call can decrement a retain count.
1406
1407 ; CHECK: define void @test24(
1408 ; CHECK: @objc_retain(i8* %a)
1409 ; CHECK: @objc_release
1410 ; CHECK: }
1411 define void @test24(i8* %r, i8* %a) {
1412   call i8* @objc_retain(i8* %a)
1413   call void @use_pointer(i8* %r)
1414   %q = load i8* %a
1415   call void @objc_release(i8* %a)
1416   ret void
1417 }
1418
1419 ; Don't move a retain/release pair if the release can be moved
1420 ; but the retain can't be moved to balance it.
1421
1422 ; CHECK: define void @test25(
1423 ; CHECK: entry:
1424 ; CHECK:   call i8* @objc_retain(i8* %p)
1425 ; CHECK: true:
1426 ; CHECK: done:
1427 ; CHECK:   call void @objc_release(i8* %p)
1428 ; CHECK: }
1429 define void @test25(i8* %p, i1 %x) {
1430 entry:
1431   %f0 = call i8* @objc_retain(i8* %p)
1432   call void @callee()
1433   br i1 %x, label %true, label %done
1434
1435 true:
1436   store i8 0, i8* %p
1437   br label %done
1438
1439 done:
1440   call void @objc_release(i8* %p)
1441   ret void
1442 }
1443
1444 ; Don't move a retain/release pair if the retain can be moved
1445 ; but the release can't be moved to balance it.
1446
1447 ; CHECK: define void @test26(
1448 ; CHECK: entry:
1449 ; CHECK:   call i8* @objc_retain(i8* %p)
1450 ; CHECK: true:
1451 ; CHECK: done:
1452 ; CHECK:   call void @objc_release(i8* %p)
1453 ; CHECK: }
1454 define void @test26(i8* %p, i1 %x) {
1455 entry:
1456   %f0 = call i8* @objc_retain(i8* %p)
1457   br i1 %x, label %true, label %done
1458
1459 true:
1460   call void @callee()
1461   br label %done
1462
1463 done:
1464   store i8 0, i8* %p
1465   call void @objc_release(i8* %p)
1466   ret void
1467 }
1468
1469 ; Don't sink the retain,release into the loop.
1470
1471 ; CHECK: define void @test27(
1472 ; CHECK: entry:
1473 ; CHECK: call i8* @objc_retain(i8* %p)
1474 ; CHECK: loop:
1475 ; CHECK-NOT: @objc_
1476 ; CHECK: done:
1477 ; CHECK: call void @objc_release
1478 ; CHECK: }
1479 define void @test27(i8* %p, i1 %x, i1 %y) {
1480 entry: 
1481   %f0 = call i8* @objc_retain(i8* %p)
1482   br i1 %x, label %loop, label %done
1483
1484 loop:
1485   call void @callee()
1486   store i8 0, i8* %p
1487   br i1 %y, label %done, label %loop
1488   
1489 done: 
1490   call void @objc_release(i8* %p)
1491   ret void
1492 }
1493
1494 ; Trivial code motion case: Triangle.
1495
1496 ; CHECK: define void @test28(
1497 ; CHECK-NOT: @objc_
1498 ; CHECK: true:
1499 ; CHECK: call i8* @objc_retain(
1500 ; CHECK: call void @callee()
1501 ; CHECK: store
1502 ; CHECK: call void @objc_release
1503 ; CHECK: done:
1504 ; CHECK-NOT: @objc_
1505 ; CHECK: }
1506 define void @test28(i8* %p, i1 %x) {
1507 entry:
1508   %f0 = call i8* @objc_retain(i8* %p)
1509   br i1 %x, label %true, label %done
1510
1511 true:
1512   call void @callee()
1513   store i8 0, i8* %p
1514   br label %done
1515
1516 done:
1517   call void @objc_release(i8* %p), !clang.imprecise_release !0
1518   ret void
1519 }
1520
1521 ; Trivial code motion case: Triangle, but no metadata. Don't move past
1522 ; unrelated memory references!
1523
1524 ; CHECK: define void @test28b
1525 ; CHECK: call i8* @objc_retain(
1526 ; CHECK: true:
1527 ; CHECK-NOT: @objc_
1528 ; CHECK: call void @callee()
1529 ; CHECK-NOT: @objc_
1530 ; CHECK: store
1531 ; CHECK-NOT: @objc_
1532 ; CHECK: done:
1533 ; CHECK: @objc_release
1534 ; CHECK: }
1535 define void @test28b(i8* %p, i1 %x, i8* noalias %t) {
1536 entry:
1537   %f0 = call i8* @objc_retain(i8* %p)
1538   br i1 %x, label %true, label %done
1539
1540 true:
1541   call void @callee()
1542   store i8 0, i8* %p
1543   br label %done
1544
1545 done:
1546   store i8 0, i8* %t
1547   call void @objc_release(i8* %p)
1548   ret void
1549 }
1550
1551 ; Trivial code motion case: Triangle, with metadata. Do move past
1552 ; unrelated memory references! And preserve the metadata.
1553
1554 ; CHECK: define void @test28c
1555 ; CHECK-NOT: @objc_
1556 ; CHECK: true:
1557 ; CHECK: call i8* @objc_retain(
1558 ; CHECK: call void @callee()
1559 ; CHECK: store
1560 ; CHECK: call void @objc_release(i8* %p) [[NUW]], !clang.imprecise_release
1561 ; CHECK: done:
1562 ; CHECK-NOT: @objc_
1563 ; CHECK: }
1564 define void @test28c(i8* %p, i1 %x, i8* noalias %t) {
1565 entry:
1566   %f0 = call i8* @objc_retain(i8* %p)
1567   br i1 %x, label %true, label %done
1568
1569 true:
1570   call void @callee()
1571   store i8 0, i8* %p
1572   br label %done
1573
1574 done:
1575   store i8 0, i8* %t
1576   call void @objc_release(i8* %p), !clang.imprecise_release !0
1577   ret void
1578 }
1579
1580 ; Like test28. but with two releases.
1581
1582 ; CHECK: define void @test29(
1583 ; CHECK-NOT: @objc_
1584 ; CHECK: true:
1585 ; CHECK: call i8* @objc_retain(
1586 ; CHECK: call void @callee()
1587 ; CHECK: store
1588 ; CHECK: call void @objc_release
1589 ; CHECK-NOT: @objc_release
1590 ; CHECK: done:
1591 ; CHECK-NOT: @objc_
1592 ; CHECK: ohno:
1593 ; CHECK-NOT: @objc_
1594 ; CHECK: }
1595 define void @test29(i8* %p, i1 %x, i1 %y) {
1596 entry:
1597   %f0 = call i8* @objc_retain(i8* %p)
1598   br i1 %x, label %true, label %done
1599
1600 true:
1601   call void @callee()
1602   store i8 0, i8* %p
1603   br i1 %y, label %done, label %ohno
1604
1605 done:
1606   call void @objc_release(i8* %p)
1607   ret void
1608
1609 ohno:
1610   call void @objc_release(i8* %p)
1611   ret void
1612 }
1613
1614 ; Basic case with the use and call in a diamond
1615 ; with an extra release.
1616
1617 ; CHECK: define void @test30(
1618 ; CHECK-NOT: @objc_
1619 ; CHECK: true:
1620 ; CHECK: call i8* @objc_retain(
1621 ; CHECK: call void @callee()
1622 ; CHECK: store
1623 ; CHECK: call void @objc_release
1624 ; CHECK-NOT: @objc_release
1625 ; CHECK: false:
1626 ; CHECK-NOT: @objc_
1627 ; CHECK: done:
1628 ; CHECK-NOT: @objc_
1629 ; CHECK: ohno:
1630 ; CHECK-NOT: @objc_
1631 ; CHECK: }
1632 define void @test30(i8* %p, i1 %x, i1 %y, i1 %z) {
1633 entry:
1634   %f0 = call i8* @objc_retain(i8* %p)
1635   br i1 %x, label %true, label %false
1636
1637 true:
1638   call void @callee()
1639   store i8 0, i8* %p
1640   br i1 %y, label %done, label %ohno
1641
1642 false:
1643   br i1 %z, label %done, label %ohno
1644
1645 done:
1646   call void @objc_release(i8* %p)
1647   ret void
1648
1649 ohno:
1650   call void @objc_release(i8* %p)
1651   ret void
1652 }
1653
1654 ; Basic case with a mergeable release.
1655
1656 ; CHECK: define void @test31(
1657 ; CHECK: call i8* @objc_retain(i8* %p)
1658 ; CHECK: call void @callee()
1659 ; CHECK: store
1660 ; CHECK: call void @objc_release
1661 ; CHECK-NOT: @objc_release
1662 ; CHECK: true:
1663 ; CHECK-NOT: @objc_release
1664 ; CHECK: false:
1665 ; CHECK-NOT: @objc_release
1666 ; CHECK: ret void
1667 ; CHECK-NOT: @objc_release
1668 ; CHECK: }
1669 define void @test31(i8* %p, i1 %x) {
1670 entry:
1671   %f0 = call i8* @objc_retain(i8* %p)
1672   call void @callee()
1673   store i8 0, i8* %p
1674   br i1 %x, label %true, label %false
1675 true:
1676   call void @objc_release(i8* %p)
1677   ret void
1678 false:
1679   call void @objc_release(i8* %p)
1680   ret void
1681 }
1682
1683 ; Don't consider bitcasts or getelementptrs direct uses.
1684
1685 ; CHECK: define void @test32(
1686 ; CHECK-NOT: @objc_
1687 ; CHECK: true:
1688 ; CHECK: call i8* @objc_retain(
1689 ; CHECK: call void @callee()
1690 ; CHECK: store
1691 ; CHECK: call void @objc_release
1692 ; CHECK: done:
1693 ; CHECK-NOT: @objc_
1694 ; CHECK: }
1695 define void @test32(i8* %p, i1 %x) {
1696 entry:
1697   %f0 = call i8* @objc_retain(i8* %p)
1698   br i1 %x, label %true, label %done
1699
1700 true:
1701   call void @callee()
1702   store i8 0, i8* %p
1703   br label %done
1704
1705 done:
1706   %g = bitcast i8* %p to i8*
1707   %h = getelementptr i8* %g, i64 0
1708   call void @objc_release(i8* %g)
1709   ret void
1710 }
1711
1712 ; Do consider icmps to be direct uses.
1713
1714 ; CHECK: define void @test33(
1715 ; CHECK-NOT: @objc_
1716 ; CHECK: true:
1717 ; CHECK: call i8* @objc_retain(
1718 ; CHECK: call void @callee()
1719 ; CHECK: icmp
1720 ; CHECK: call void @objc_release
1721 ; CHECK: done:
1722 ; CHECK-NOT: @objc_
1723 ; CHECK: }
1724 define void @test33(i8* %p, i1 %x, i8* %y) {
1725 entry:
1726   %f0 = call i8* @objc_retain(i8* %p)
1727   br i1 %x, label %true, label %done
1728
1729 true:
1730   call void @callee()
1731   %v = icmp eq i8* %p, %y
1732   br label %done
1733
1734 done:
1735   %g = bitcast i8* %p to i8*
1736   %h = getelementptr i8* %g, i64 0
1737   call void @objc_release(i8* %g)
1738   ret void
1739 }
1740
1741 ; Delete retain,release if there's just a possible dec and we have imprecise
1742 ; releases.
1743
1744 ; CHECK: define void @test34a(
1745 ; CHECK:   call i8* @objc_retain
1746 ; CHECK: true:
1747 ; CHECK: done:
1748 ; CHECK: call void @objc_release
1749 ; CHECK: }
1750 define void @test34a(i8* %p, i1 %x, i8* %y) {
1751 entry:
1752   %f0 = call i8* @objc_retain(i8* %p)
1753   br i1 %x, label %true, label %done
1754
1755 true:
1756   call void @callee()
1757   br label %done
1758
1759 done:
1760   %g = bitcast i8* %p to i8*
1761   %h = getelementptr i8* %g, i64 0
1762   call void @objc_release(i8* %g)
1763   ret void
1764 }
1765
1766 ; CHECK: define void @test34b(
1767 ; CHECK-NOT: @objc_
1768 ; CHECK: }
1769 define void @test34b(i8* %p, i1 %x, i8* %y) {
1770 entry:
1771   %f0 = call i8* @objc_retain(i8* %p)
1772   br i1 %x, label %true, label %done
1773
1774 true:
1775   call void @callee()
1776   br label %done
1777
1778 done:
1779   %g = bitcast i8* %p to i8*
1780   %h = getelementptr i8* %g, i64 0
1781   call void @objc_release(i8* %g), !clang.imprecise_release !0
1782   ret void
1783 }
1784
1785
1786 ; Delete retain,release if there's just a use and we do not have a precise
1787 ; release.
1788
1789 ; Precise.
1790 ; CHECK: define void @test35a(
1791 ; CHECK: entry:
1792 ; CHECK:   call i8* @objc_retain
1793 ; CHECK: true:
1794 ; CHECK: done:
1795 ; CHECK:   call void @objc_release
1796 ; CHECK: }
1797 define void @test35a(i8* %p, i1 %x, i8* %y) {
1798 entry:
1799   %f0 = call i8* @objc_retain(i8* %p)
1800   br i1 %x, label %true, label %done
1801
1802 true:
1803   %v = icmp eq i8* %p, %y
1804   br label %done
1805
1806 done:
1807   %g = bitcast i8* %p to i8*
1808   %h = getelementptr i8* %g, i64 0
1809   call void @objc_release(i8* %g)
1810   ret void
1811 }
1812
1813 ; Imprecise.
1814 ; CHECK: define void @test35b(
1815 ; CHECK-NOT: @objc_
1816 ; CHECK: }
1817 define void @test35b(i8* %p, i1 %x, i8* %y) {
1818 entry:
1819   %f0 = call i8* @objc_retain(i8* %p)
1820   br i1 %x, label %true, label %done
1821
1822 true:
1823   %v = icmp eq i8* %p, %y
1824   br label %done
1825
1826 done:
1827   %g = bitcast i8* %p to i8*
1828   %h = getelementptr i8* %g, i64 0
1829   call void @objc_release(i8* %g), !clang.imprecise_release !0
1830   ret void
1831 }
1832
1833 ; Delete a retain,release if there's no actual use and we have precise release.
1834
1835 ; CHECK: define void @test36a(
1836 ; CHECK: @objc_retain
1837 ; CHECK: call void @callee()
1838 ; CHECK-NOT: @objc_
1839 ; CHECK: call void @callee()
1840 ; CHECK: @objc_release
1841 ; CHECK: }
1842 define void @test36a(i8* %p) {
1843 entry:
1844   call i8* @objc_retain(i8* %p)
1845   call void @callee()
1846   call void @callee()
1847   call void @objc_release(i8* %p)
1848   ret void
1849 }
1850
1851 ; Like test36, but with metadata.
1852
1853 ; CHECK: define void @test36b(
1854 ; CHECK-NOT: @objc_
1855 ; CHECK: }
1856 define void @test36b(i8* %p) {
1857 entry:
1858   call i8* @objc_retain(i8* %p)
1859   call void @callee()
1860   call void @callee()
1861   call void @objc_release(i8* %p), !clang.imprecise_release !0
1862   ret void
1863 }
1864
1865 ; Be aggressive about analyzing phis to eliminate possible uses.
1866
1867 ; CHECK: define void @test38(
1868 ; CHECK-NOT: @objc_
1869 ; CHECK: }
1870 define void @test38(i8* %p, i1 %u, i1 %m, i8* %z, i8* %y, i8* %x, i8* %w) {
1871 entry:
1872   call i8* @objc_retain(i8* %p)
1873   br i1 %u, label %true, label %false
1874 true:
1875   br i1 %m, label %a, label %b
1876 false:
1877   br i1 %m, label %c, label %d
1878 a:
1879   br label %e
1880 b:
1881   br label %e
1882 c:
1883   br label %f
1884 d:
1885   br label %f
1886 e:
1887   %j = phi i8* [ %z, %a ], [ %y, %b ]
1888   br label %g
1889 f:
1890   %k = phi i8* [ %w, %c ], [ %x, %d ]
1891   br label %g
1892 g:
1893   %h = phi i8* [ %j, %e ], [ %k, %f ]
1894   call void @use_pointer(i8* %h)
1895   call void @objc_release(i8* %p), !clang.imprecise_release !0
1896   ret void
1897 }
1898
1899 ; Delete retain,release pairs around loops.
1900
1901 ; CHECK: define void @test39(
1902 ; CHECK-NOT: @objc_
1903 ; CHECK: }
1904 define void @test39(i8* %p) {
1905 entry:
1906   %0 = call i8* @objc_retain(i8* %p)
1907   br label %loop
1908
1909 loop:                                             ; preds = %loop, %entry
1910   br i1 undef, label %loop, label %exit
1911
1912 exit:                                             ; preds = %loop
1913   call void @objc_release(i8* %0), !clang.imprecise_release !0
1914   ret void
1915 }
1916
1917 ; Delete retain,release pairs around loops containing uses.
1918
1919 ; CHECK: define void @test39b(
1920 ; CHECK-NOT: @objc_
1921 ; CHECK: }
1922 define void @test39b(i8* %p) {
1923 entry:
1924   %0 = call i8* @objc_retain(i8* %p)
1925   br label %loop
1926
1927 loop:                                             ; preds = %loop, %entry
1928   store i8 0, i8* %0
1929   br i1 undef, label %loop, label %exit
1930
1931 exit:                                             ; preds = %loop
1932   call void @objc_release(i8* %0), !clang.imprecise_release !0
1933   ret void
1934 }
1935
1936 ; Delete retain,release pairs around loops containing potential decrements.
1937
1938 ; CHECK: define void @test39c(
1939 ; CHECK-NOT: @objc_
1940 ; CHECK: }
1941 define void @test39c(i8* %p) {
1942 entry:
1943   %0 = call i8* @objc_retain(i8* %p)
1944   br label %loop
1945
1946 loop:                                             ; preds = %loop, %entry
1947   call void @use_pointer(i8* %0)
1948   br i1 undef, label %loop, label %exit
1949
1950 exit:                                             ; preds = %loop
1951   call void @objc_release(i8* %0), !clang.imprecise_release !0
1952   ret void
1953 }
1954
1955 ; Delete retain,release pairs around loops even if
1956 ; the successors are in a different order.
1957
1958 ; CHECK: define void @test40(
1959 ; CHECK-NOT: @objc_
1960 ; CHECK: }
1961 define void @test40(i8* %p) {
1962 entry:
1963   %0 = call i8* @objc_retain(i8* %p)
1964   br label %loop
1965
1966 loop:                                             ; preds = %loop, %entry
1967   call void @use_pointer(i8* %0)
1968   br i1 undef, label %exit, label %loop
1969
1970 exit:                                             ; preds = %loop
1971   call void @objc_release(i8* %0), !clang.imprecise_release !0
1972   ret void
1973 }
1974
1975 ; Do the known-incremented retain+release elimination even if the pointer
1976 ; is also autoreleased.
1977
1978 ; CHECK: define void @test42(
1979 ; CHECK-NEXT: entry:
1980 ; CHECK-NEXT: call i8* @objc_retain(i8* %p)
1981 ; CHECK-NEXT: call i8* @objc_autorelease(i8* %p)
1982 ; CHECK-NEXT: call void @use_pointer(i8* %p)
1983 ; CHECK-NEXT: call void @use_pointer(i8* %p)
1984 ; CHECK-NEXT: ret void
1985 ; CHECK-NEXT: }
1986 define void @test42(i8* %p) {
1987 entry:
1988   call i8* @objc_retain(i8* %p)
1989   call i8* @objc_autorelease(i8* %p)
1990   call i8* @objc_retain(i8* %p)
1991   call void @use_pointer(i8* %p)
1992   call void @use_pointer(i8* %p)
1993   call void @objc_release(i8* %p)
1994   ret void
1995 }
1996
1997 ; Don't the known-incremented retain+release elimination if the pointer is
1998 ; autoreleased and there's an autoreleasePoolPop.
1999
2000 ; CHECK: define void @test43(
2001 ; CHECK-NEXT: entry:
2002 ; CHECK-NEXT: call i8* @objc_retain(i8* %p)
2003 ; CHECK-NEXT: call i8* @objc_autorelease(i8* %p)
2004 ; CHECK-NEXT: call i8* @objc_retain
2005 ; CHECK-NEXT: call void @use_pointer(i8* %p)
2006 ; CHECK-NEXT: call void @use_pointer(i8* %p)
2007 ; CHECK-NEXT: call void @objc_autoreleasePoolPop(i8* undef)
2008 ; CHECK-NEXT: call void @objc_release
2009 ; CHECK-NEXT: ret void
2010 ; CHECK-NEXT: }
2011 define void @test43(i8* %p) {
2012 entry:
2013   call i8* @objc_retain(i8* %p)
2014   call i8* @objc_autorelease(i8* %p)
2015   call i8* @objc_retain(i8* %p)
2016   call void @use_pointer(i8* %p)
2017   call void @use_pointer(i8* %p)
2018   call void @objc_autoreleasePoolPop(i8* undef)
2019   call void @objc_release(i8* %p)
2020   ret void
2021 }
2022
2023 ; Do the known-incremented retain+release elimination if the pointer is
2024 ; autoreleased and there's an autoreleasePoolPush.
2025
2026 ; CHECK: define void @test43b
2027 ; CHECK-NEXT: entry:
2028 ; CHECK-NEXT: call i8* @objc_retain(i8* %p)
2029 ; CHECK-NEXT: call i8* @objc_autorelease(i8* %p)
2030 ; CHECK-NEXT: call void @use_pointer(i8* %p)
2031 ; CHECK-NEXT: call void @use_pointer(i8* %p)
2032 ; CHECK-NEXT: call i8* @objc_autoreleasePoolPush()
2033 ; CHECK-NEXT: ret void
2034 ; CHECK-NEXT: }
2035 define void @test43b(i8* %p) {
2036 entry:
2037   call i8* @objc_retain(i8* %p)
2038   call i8* @objc_autorelease(i8* %p)
2039   call i8* @objc_retain(i8* %p)
2040   call void @use_pointer(i8* %p)
2041   call void @use_pointer(i8* %p)
2042   call i8* @objc_autoreleasePoolPush()
2043   call void @objc_release(i8* %p)
2044   ret void
2045 }
2046
2047 ; Do retain+release elimination for non-provenance pointers.
2048
2049 ; CHECK: define void @test44(
2050 ; CHECK-NOT: objc_
2051 ; CHECK: }
2052 define void @test44(i8** %pp) {
2053   %p = load i8** %pp
2054   %q = call i8* @objc_retain(i8* %p)
2055   call void @objc_release(i8* %q)
2056   ret void
2057 }
2058
2059 ; Don't delete retain+release with an unknown-provenance
2060 ; may-alias objc_release between them.
2061
2062 ; CHECK: define void @test45(
2063 ; CHECK: call i8* @objc_retain(i8* %p)
2064 ; CHECK: call void @objc_release(i8* %q)
2065 ; CHECK: call void @use_pointer(i8* %p)
2066 ; CHECK: call void @objc_release(i8* %p)
2067 define void @test45(i8** %pp, i8** %qq) {
2068   %p = load i8** %pp
2069   %q = load i8** %qq
2070   call i8* @objc_retain(i8* %p)
2071   call void @objc_release(i8* %q)
2072   call void @use_pointer(i8* %p)
2073   call void @objc_release(i8* %p)
2074   ret void
2075 }
2076
2077 ; Don't delete retain and autorelease here.
2078
2079 ; CHECK: define void @test46(
2080 ; CHECK: tail call i8* @objc_retain(i8* %p) [[NUW]]
2081 ; CHECK: true:
2082 ; CHECK: call i8* @objc_autorelease(i8* %p) [[NUW]]
2083 define void @test46(i8* %p, i1 %a) {
2084 entry:
2085   call i8* @objc_retain(i8* %p)
2086   br i1 %a, label %true, label %false
2087
2088 true:
2089   call i8* @objc_autorelease(i8* %p)
2090   call void @use_pointer(i8* %p)
2091   ret void
2092
2093 false:
2094   ret void
2095 }
2096
2097 ; Delete no-op cast calls.
2098
2099 ; CHECK: define i8* @test47(
2100 ; CHECK-NOT: call
2101 ; CHECK: ret i8* %p
2102 define i8* @test47(i8* %p) nounwind {
2103   %x = call i8* @objc_retainedObject(i8* %p)
2104   ret i8* %x
2105 }
2106
2107 ; Delete no-op cast calls.
2108
2109 ; CHECK: define i8* @test48(
2110 ; CHECK-NOT: call
2111 ; CHECK: ret i8* %p
2112 define i8* @test48(i8* %p) nounwind {
2113   %x = call i8* @objc_unretainedObject(i8* %p)
2114   ret i8* %x
2115 }
2116
2117 ; Delete no-op cast calls.
2118
2119 ; CHECK: define i8* @test49(
2120 ; CHECK-NOT: call
2121 ; CHECK: ret i8* %p
2122 define i8* @test49(i8* %p) nounwind {
2123   %x = call i8* @objc_unretainedPointer(i8* %p)
2124   ret i8* %x
2125 }
2126
2127 ; Do delete retain+release with intervening stores of the address value if we
2128 ; have imprecise release attached to objc_release.
2129
2130 ; CHECK:      define void @test50a(
2131 ; CHECK-NEXT:   call i8* @objc_retain
2132 ; CHECK-NEXT:   call void @callee
2133 ; CHECK-NEXT:   store
2134 ; CHECK-NEXT:   call void @objc_release
2135 ; CHECK-NEXT:   ret void
2136 ; CHECK-NEXT: }
2137 define void @test50a(i8* %p, i8** %pp) {
2138   call i8* @objc_retain(i8* %p)
2139   call void @callee()
2140   store i8* %p, i8** %pp
2141   call void @objc_release(i8* %p)
2142   ret void
2143 }
2144
2145 ; CHECK: define void @test50b(
2146 ; CHECK-NOT: @objc_
2147 ; CHECK: }
2148 define void @test50b(i8* %p, i8** %pp) {
2149   call i8* @objc_retain(i8* %p)
2150   call void @callee()
2151   store i8* %p, i8** %pp
2152   call void @objc_release(i8* %p), !clang.imprecise_release !0
2153   ret void
2154 }
2155
2156
2157 ; Don't delete retain+release with intervening stores through the
2158 ; address value.
2159
2160 ; CHECK: define void @test51a(
2161 ; CHECK: call i8* @objc_retain(i8* %p)
2162 ; CHECK: call void @objc_release(i8* %p)
2163 ; CHECK: ret void
2164 ; CHECK: }
2165 define void @test51a(i8* %p) {
2166   call i8* @objc_retain(i8* %p)
2167   call void @callee()
2168   store i8 0, i8* %p
2169   call void @objc_release(i8* %p)
2170   ret void
2171 }
2172
2173 ; CHECK: define void @test51b(
2174 ; CHECK: call i8* @objc_retain(i8* %p)
2175 ; CHECK: call void @objc_release(i8* %p)
2176 ; CHECK: ret void
2177 ; CHECK: }
2178 define void @test51b(i8* %p) {
2179   call i8* @objc_retain(i8* %p)
2180   call void @callee()
2181   store i8 0, i8* %p
2182   call void @objc_release(i8* %p), !clang.imprecise_release !0
2183   ret void
2184 }
2185
2186 ; Don't delete retain+release with intervening use of a pointer of
2187 ; unknown provenance.
2188
2189 ; CHECK: define void @test52a(
2190 ; CHECK: call i8* @objc_retain
2191 ; CHECK: call void @callee()
2192 ; CHECK: call void @use_pointer(i8* %z)
2193 ; CHECK: call void @objc_release
2194 ; CHECK: ret void
2195 ; CHECK: }
2196 define void @test52a(i8** %zz, i8** %pp) {
2197   %p = load i8** %pp
2198   %1 = call i8* @objc_retain(i8* %p)
2199   call void @callee()
2200   %z = load i8** %zz
2201   call void @use_pointer(i8* %z)
2202   call void @objc_release(i8* %p)
2203   ret void
2204 }
2205
2206 ; CHECK: define void @test52b(
2207 ; CHECK: call i8* @objc_retain
2208 ; CHECK: call void @callee()
2209 ; CHECK: call void @use_pointer(i8* %z)
2210 ; CHECK: call void @objc_release
2211 ; CHECK: ret void
2212 ; CHECK: }
2213 define void @test52b(i8** %zz, i8** %pp) {
2214   %p = load i8** %pp
2215   %1 = call i8* @objc_retain(i8* %p)
2216   call void @callee()
2217   %z = load i8** %zz
2218   call void @use_pointer(i8* %z)
2219   call void @objc_release(i8* %p), !clang.imprecise_release !0
2220   ret void
2221 }
2222
2223 ; Like test52, but the pointer has function type, so it's assumed to
2224 ; be not reference counted.
2225 ; Oops. That's wrong. Clang sometimes uses function types gratuitously.
2226 ; See rdar://10551239.
2227
2228 ; CHECK: define void @test53(
2229 ; CHECK: @objc_
2230 ; CHECK: }
2231 define void @test53(void ()** %zz, i8** %pp) {
2232   %p = load i8** %pp
2233   %1 = call i8* @objc_retain(i8* %p)
2234   call void @callee()
2235   %z = load void ()** %zz
2236   call void @callee_fnptr(void ()* %z)
2237   call void @objc_release(i8* %p)
2238   ret void
2239 }
2240
2241 ; Convert autorelease to release if the value is unused.
2242
2243 ; CHECK: define void @test54(
2244 ; CHECK: call i8* @returner()
2245 ; CHECK-NEXT: call void @objc_release(i8* %t) [[NUW]], !clang.imprecise_release !0
2246 ; CHECK-NEXT: ret void
2247 define void @test54() {
2248   %t = call i8* @returner()
2249   call i8* @objc_autorelease(i8* %t)
2250   ret void
2251 }
2252
2253 ; Nested retain+release pairs. Delete them both.
2254
2255 ; CHECK: define void @test55(
2256 ; CHECK-NOT: @objc
2257 ; CHECK: }
2258 define void @test55(i8* %x) { 
2259 entry: 
2260   %0 = call i8* @objc_retain(i8* %x) nounwind 
2261   %1 = call i8* @objc_retain(i8* %x) nounwind 
2262   call void @objc_release(i8* %x) nounwind 
2263   call void @objc_release(i8* %x) nounwind 
2264   ret void 
2265 }
2266
2267 ; Nested retain+release pairs where the inner pair depends
2268 ; on the outer pair to be removed, and then the outer pair
2269 ; can be partially eliminated. Plus an extra outer pair to
2270 ; eliminate, for fun.
2271
2272 ; CHECK: define void @test56(
2273 ; CHECK-NOT: @objc
2274 ; CHECK: if.then:
2275 ; CHECK-NEXT: %0 = tail call i8* @objc_retain(i8* %x) [[NUW]]
2276 ; CHECK-NEXT: tail call void @use_pointer(i8* %x)
2277 ; CHECK-NEXT: tail call void @use_pointer(i8* %x)
2278 ; CHECK-NEXT: tail call void @objc_release(i8* %x) [[NUW]], !clang.imprecise_release !0
2279 ; CHECK-NEXT: br label %if.end
2280 ; CHECK-NOT: @objc
2281 ; CHECK: }
2282 define void @test56(i8* %x, i32 %n) {
2283 entry:
2284   %0 = tail call i8* @objc_retain(i8* %x) nounwind
2285   %1 = tail call i8* @objc_retain(i8* %0) nounwind
2286   %tobool = icmp eq i32 %n, 0
2287   br i1 %tobool, label %if.end, label %if.then
2288
2289 if.then:                                          ; preds = %entry
2290   %2 = tail call i8* @objc_retain(i8* %1) nounwind
2291   tail call void @use_pointer(i8* %2)
2292   tail call void @use_pointer(i8* %2)
2293   tail call void @objc_release(i8* %2) nounwind, !clang.imprecise_release !0
2294   br label %if.end
2295
2296 if.end:                                           ; preds = %entry, %if.then
2297   tail call void @objc_release(i8* %1) nounwind, !clang.imprecise_release !0
2298   tail call void @objc_release(i8* %0) nounwind, !clang.imprecise_release !0
2299   ret void
2300 }
2301
2302 ; When there are adjacent retain+release pairs, the first one is
2303 ; known unnecessary because the presence of the second one means that
2304 ; the first one won't be deleting the object.
2305
2306 ; CHECK:      define void @test57(
2307 ; CHECK-NEXT: entry:
2308 ; CHECK-NEXT:   call void @use_pointer(i8* %x)
2309 ; CHECK-NEXT:   call void @use_pointer(i8* %x)
2310 ; CHECK-NEXT:   %0 = tail call i8* @objc_retain(i8* %x) [[NUW]]
2311 ; CHECK-NEXT:   call void @use_pointer(i8* %x)
2312 ; CHECK-NEXT:   call void @use_pointer(i8* %x)
2313 ; CHECK-NEXT:   call void @objc_release(i8* %x) [[NUW]]
2314 ; CHECK-NEXT:   ret void
2315 ; CHECK-NEXT: }
2316 define void @test57(i8* %x) nounwind {
2317 entry:
2318   call i8* @objc_retain(i8* %x) nounwind
2319   call void @use_pointer(i8* %x)
2320   call void @use_pointer(i8* %x)
2321   call void @objc_release(i8* %x) nounwind
2322   call i8* @objc_retain(i8* %x) nounwind
2323   call void @use_pointer(i8* %x)
2324   call void @use_pointer(i8* %x)
2325   call void @objc_release(i8* %x) nounwind
2326   ret void
2327 }
2328
2329 ; An adjacent retain+release pair is sufficient even if it will be
2330 ; removed itself.
2331
2332 ; CHECK:      define void @test58(
2333 ; CHECK-NEXT: entry:
2334 ; CHECK-NEXT:   call void @use_pointer(i8* %x)
2335 ; CHECK-NEXT:   call void @use_pointer(i8* %x)
2336 ; CHECK-NEXT:   ret void
2337 ; CHECK-NEXT: }
2338 define void @test58(i8* %x) nounwind {
2339 entry:
2340   call i8* @objc_retain(i8* %x) nounwind
2341   call void @use_pointer(i8* %x)
2342   call void @use_pointer(i8* %x)
2343   call void @objc_release(i8* %x) nounwind
2344   call i8* @objc_retain(i8* %x) nounwind
2345   call void @objc_release(i8* %x) nounwind
2346   ret void
2347 }
2348
2349 ; Don't delete the second retain+release pair in an adjacent set.
2350
2351 ; CHECK:      define void @test59(
2352 ; CHECK-NEXT: entry:
2353 ; CHECK-NEXT:   %0 = tail call i8* @objc_retain(i8* %x) [[NUW]]
2354 ; CHECK-NEXT:   call void @use_pointer(i8* %x)
2355 ; CHECK-NEXT:   call void @use_pointer(i8* %x)
2356 ; CHECK-NEXT:   call void @objc_release(i8* %x) [[NUW]]
2357 ; CHECK-NEXT:   ret void
2358 ; CHECK-NEXT: }
2359 define void @test59(i8* %x) nounwind {
2360 entry:
2361   %a = call i8* @objc_retain(i8* %x) nounwind
2362   call void @objc_release(i8* %x) nounwind
2363   %b = call i8* @objc_retain(i8* %x) nounwind
2364   call void @use_pointer(i8* %x)
2365   call void @use_pointer(i8* %x)
2366   call void @objc_release(i8* %x) nounwind
2367   ret void
2368 }
2369
2370 ; Constant pointers to objects don't need reference counting.
2371
2372 @constptr = external constant i8*
2373 @something = external global i8*
2374
2375 ; We have a precise lifetime retain/release here. We can not remove them since
2376 ; @something is not constant.
2377
2378 ; CHECK: define void @test60a(
2379 ; CHECK: call i8* @objc_retain
2380 ; CHECK: call void @objc_release
2381 ; CHECK: }
2382 define void @test60a() {
2383   %t = load i8** @constptr
2384   %s = load i8** @something
2385   call i8* @objc_retain(i8* %s)
2386   call void @callee()
2387   call void @use_pointer(i8* %t)
2388   call void @objc_release(i8* %s)
2389   ret void
2390 }
2391
2392 ; CHECK: define void @test60b(
2393 ; CHECK: call i8* @objc_retain
2394 ; CHECK-NOT: call i8* @objc_retain
2395 ; CHECK-NOT: call i8* @objc_rrelease
2396 ; CHECK: }
2397 define void @test60b() {
2398   %t = load i8** @constptr
2399   %s = load i8** @something
2400   call i8* @objc_retain(i8* %s)
2401   call i8* @objc_retain(i8* %s)
2402   call void @callee()
2403   call void @use_pointer(i8* %t)
2404   call void @objc_release(i8* %s)
2405   ret void
2406 }
2407
2408 ; CHECK: define void @test60c(
2409 ; CHECK-NOT: @objc_
2410 ; CHECK: }
2411 define void @test60c() {
2412   %t = load i8** @constptr
2413   %s = load i8** @something
2414   call i8* @objc_retain(i8* %s)
2415   call void @callee()
2416   call void @use_pointer(i8* %t)
2417   call void @objc_release(i8* %s), !clang.imprecise_release !0
2418   ret void
2419 }
2420
2421 ; CHECK: define void @test60d(
2422 ; CHECK-NOT: @objc_
2423 ; CHECK: }
2424 define void @test60d() {
2425   %t = load i8** @constptr
2426   %s = load i8** @something
2427   call i8* @objc_retain(i8* %t)
2428   call void @callee()
2429   call void @use_pointer(i8* %s)
2430   call void @objc_release(i8* %t)
2431   ret void
2432 }
2433
2434 ; CHECK: define void @test60e(
2435 ; CHECK-NOT: @objc_
2436 ; CHECK: }
2437 define void @test60e() {
2438   %t = load i8** @constptr
2439   %s = load i8** @something
2440   call i8* @objc_retain(i8* %t)
2441   call void @callee()
2442   call void @use_pointer(i8* %s)
2443   call void @objc_release(i8* %t), !clang.imprecise_release !0
2444   ret void
2445 }
2446
2447 ; Constant pointers to objects don't need to be considered related to other
2448 ; pointers.
2449
2450 ; CHECK: define void @test61(
2451 ; CHECK-NOT: @objc_
2452 ; CHECK: }
2453 define void @test61() {
2454   %t = load i8** @constptr
2455   call i8* @objc_retain(i8* %t)
2456   call void @callee()
2457   call void @use_pointer(i8* %t)
2458   call void @objc_release(i8* %t)
2459   ret void
2460 }
2461
2462 ; Delete a retain matched by releases when one is inside the loop and the
2463 ; other is outside the loop.
2464
2465 ; CHECK: define void @test62(
2466 ; CHECK-NOT: @objc_
2467 ; CHECK: }
2468 define void @test62(i8* %x, i1* %p) nounwind {
2469 entry:
2470   br label %loop
2471
2472 loop:
2473   call i8* @objc_retain(i8* %x)
2474   %q = load i1* %p
2475   br i1 %q, label %loop.more, label %exit
2476
2477 loop.more:
2478   call void @objc_release(i8* %x)
2479   br label %loop
2480
2481 exit:
2482   call void @objc_release(i8* %x)
2483   ret void
2484 }
2485
2486 ; Like test62 but with no release in exit.
2487 ; Don't delete anything!
2488
2489 ; CHECK: define void @test63(
2490 ; CHECK: loop:
2491 ; CHECK:   tail call i8* @objc_retain(i8* %x)
2492 ; CHECK: loop.more:
2493 ; CHECK:   call void @objc_release(i8* %x)
2494 ; CHECK: }
2495 define void @test63(i8* %x, i1* %p) nounwind {
2496 entry:
2497   br label %loop
2498
2499 loop:
2500   call i8* @objc_retain(i8* %x)
2501   %q = load i1* %p
2502   br i1 %q, label %loop.more, label %exit
2503
2504 loop.more:
2505   call void @objc_release(i8* %x)
2506   br label %loop
2507
2508 exit:
2509   ret void
2510 }
2511
2512 ; Like test62 but with no release in loop.more.
2513 ; Don't delete anything!
2514
2515 ; CHECK: define void @test64(
2516 ; CHECK: loop:
2517 ; CHECK:   tail call i8* @objc_retain(i8* %x)
2518 ; CHECK: exit:
2519 ; CHECK:   call void @objc_release(i8* %x)
2520 ; CHECK: }
2521 define void @test64(i8* %x, i1* %p) nounwind {
2522 entry:
2523   br label %loop
2524
2525 loop:
2526   call i8* @objc_retain(i8* %x)
2527   %q = load i1* %p
2528   br i1 %q, label %loop.more, label %exit
2529
2530 loop.more:
2531   br label %loop
2532
2533 exit:
2534   call void @objc_release(i8* %x)
2535   ret void
2536 }
2537
2538 ; Move an autorelease past a phi with a null.
2539
2540 ; CHECK: define i8* @test65(
2541 ; CHECK: if.then:
2542 ; CHECK:   call i8* @objc_autorelease(
2543 ; CHECK: return:
2544 ; CHECK-NOT: @objc_autorelease
2545 ; CHECK: }
2546 define i8* @test65(i1 %x) {
2547 entry:
2548   br i1 %x, label %return, label %if.then
2549
2550 if.then:                                          ; preds = %entry
2551   %c = call i8* @returner()
2552   %s = call i8* @objc_retainAutoreleasedReturnValue(i8* %c) nounwind
2553   br label %return
2554
2555 return:                                           ; preds = %if.then, %entry
2556   %retval = phi i8* [ %s, %if.then ], [ null, %entry ]
2557   %q = call i8* @objc_autorelease(i8* %retval) nounwind
2558   ret i8* %retval
2559 }
2560
2561 ; Don't move an autorelease past an autorelease pool boundary.
2562
2563 ; CHECK: define i8* @test65b(
2564 ; CHECK: if.then:
2565 ; CHECK-NOT: @objc_autorelease
2566 ; CHECK: return:
2567 ; CHECK:   call i8* @objc_autorelease(
2568 ; CHECK: }
2569 define i8* @test65b(i1 %x) {
2570 entry:
2571   %t = call i8* @objc_autoreleasePoolPush()
2572   br i1 %x, label %return, label %if.then
2573
2574 if.then:                                          ; preds = %entry
2575   %c = call i8* @returner()
2576   %s = call i8* @objc_retainAutoreleasedReturnValue(i8* %c) nounwind
2577   br label %return
2578
2579 return:                                           ; preds = %if.then, %entry
2580   %retval = phi i8* [ %s, %if.then ], [ null, %entry ]
2581   call void @objc_autoreleasePoolPop(i8* %t)
2582   %q = call i8* @objc_autorelease(i8* %retval) nounwind
2583   ret i8* %retval
2584 }
2585
2586 ; Don't move an autoreleaseReuturnValue, which would break
2587 ; the RV optimization.
2588
2589 ; CHECK: define i8* @test65c(
2590 ; CHECK: if.then:
2591 ; CHECK-NOT: @objc_autorelease
2592 ; CHECK: return:
2593 ; CHECK:   call i8* @objc_autoreleaseReturnValue(
2594 ; CHECK: }
2595 define i8* @test65c(i1 %x) {
2596 entry:
2597   br i1 %x, label %return, label %if.then
2598
2599 if.then:                                          ; preds = %entry
2600   %c = call i8* @returner()
2601   %s = call i8* @objc_retainAutoreleasedReturnValue(i8* %c) nounwind
2602   br label %return
2603
2604 return:                                           ; preds = %if.then, %entry
2605   %retval = phi i8* [ %s, %if.then ], [ null, %entry ]
2606   %q = call i8* @objc_autoreleaseReturnValue(i8* %retval) nounwind
2607   ret i8* %retval
2608 }
2609
2610 ; A few real-world testcases.
2611
2612 @.str4 = private unnamed_addr constant [33 x i8] c"-[A z] = { %f, %f, { %f, %f } }\0A\00"
2613 @"OBJC_IVAR_$_A.myZ" = global i64 20, section "__DATA, __objc_const", align 8
2614 declare i32 @printf(i8* nocapture, ...) nounwind
2615 declare i32 @puts(i8* nocapture) nounwind
2616 @str = internal constant [16 x i8] c"-[ Top0 _getX ]\00"
2617
2618 ; CHECK: @"\01-[A z]"
2619 ; CHECK-NOT: @objc_
2620 ; CHECK: }
2621
2622 define {<2 x float>, <2 x float>} @"\01-[A z]"({}* %self, i8* nocapture %_cmd) nounwind {
2623 invoke.cont:
2624   %0 = bitcast {}* %self to i8*
2625   %1 = tail call i8* @objc_retain(i8* %0) nounwind
2626   tail call void @llvm.dbg.value(metadata !{{}* %self}, i64 0, metadata !0)
2627   tail call void @llvm.dbg.value(metadata !{{}* %self}, i64 0, metadata !0)
2628   %ivar = load i64* @"OBJC_IVAR_$_A.myZ", align 8
2629   %add.ptr = getelementptr i8* %0, i64 %ivar
2630   %tmp1 = bitcast i8* %add.ptr to float*
2631   %tmp2 = load float* %tmp1, align 4
2632   %conv = fpext float %tmp2 to double
2633   %add.ptr.sum = add i64 %ivar, 4
2634   %tmp6 = getelementptr inbounds i8* %0, i64 %add.ptr.sum
2635   %2 = bitcast i8* %tmp6 to float*
2636   %tmp7 = load float* %2, align 4
2637   %conv8 = fpext float %tmp7 to double
2638   %add.ptr.sum36 = add i64 %ivar, 8
2639   %tmp12 = getelementptr inbounds i8* %0, i64 %add.ptr.sum36
2640   %arrayidx = bitcast i8* %tmp12 to float*
2641   %tmp13 = load float* %arrayidx, align 4
2642   %conv14 = fpext float %tmp13 to double
2643   %tmp12.sum = add i64 %ivar, 12
2644   %arrayidx19 = getelementptr inbounds i8* %0, i64 %tmp12.sum
2645   %3 = bitcast i8* %arrayidx19 to float*
2646   %tmp20 = load float* %3, align 4
2647   %conv21 = fpext float %tmp20 to double
2648   %call = tail call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([33 x i8]* @.str4, i64 0, i64 0), double %conv, double %conv8, double %conv14, double %conv21)
2649   %ivar23 = load i64* @"OBJC_IVAR_$_A.myZ", align 8
2650   %add.ptr24 = getelementptr i8* %0, i64 %ivar23
2651   %4 = bitcast i8* %add.ptr24 to i128*
2652   %srcval = load i128* %4, align 4
2653   tail call void @objc_release(i8* %0) nounwind
2654   %tmp29 = trunc i128 %srcval to i64
2655   %tmp30 = bitcast i64 %tmp29 to <2 x float>
2656   %tmp31 = insertvalue {<2 x float>, <2 x float>} undef, <2 x float> %tmp30, 0
2657   %tmp32 = lshr i128 %srcval, 64
2658   %tmp33 = trunc i128 %tmp32 to i64
2659   %tmp34 = bitcast i64 %tmp33 to <2 x float>
2660   %tmp35 = insertvalue {<2 x float>, <2 x float>} %tmp31, <2 x float> %tmp34, 1
2661   ret {<2 x float>, <2 x float>} %tmp35
2662 }
2663
2664 ; CHECK: @"\01-[Top0 _getX]"
2665 ; CHECK-NOT: @objc_
2666 ; CHECK: }
2667
2668 define i32 @"\01-[Top0 _getX]"({}* %self, i8* nocapture %_cmd) nounwind {
2669 invoke.cont:
2670   %0 = bitcast {}* %self to i8*
2671   %1 = tail call i8* @objc_retain(i8* %0) nounwind
2672   %puts = tail call i32 @puts(i8* getelementptr inbounds ([16 x i8]* @str, i64 0, i64 0))
2673   tail call void @objc_release(i8* %0) nounwind
2674   ret i32 0
2675 }
2676
2677 @"\01L_OBJC_METH_VAR_NAME_" = internal global [5 x i8] c"frob\00", section "__TEXT,__cstring,cstring_literals", align 1@"\01L_OBJC_SELECTOR_REFERENCES_" = internal global i8* getelementptr inbounds ([5 x i8]* @"\01L_OBJC_METH_VAR_NAME_", i64 0, i64 0), section "__DATA, __objc_selrefs, literal_pointers, no_dead_strip"
2678 @"\01L_OBJC_IMAGE_INFO" = internal constant [2 x i32] [i32 0, i32 16], section "__DATA, __objc_imageinfo, regular, no_dead_strip"
2679 @llvm.used = appending global [3 x i8*] [i8* getelementptr inbounds ([5 x i8]* @"\01L_OBJC_METH_VAR_NAME_", i32 0, i32 0), i8* bitcast (i8** @"\01L_OBJC_SELECTOR_REFERENCES_" to i8*), i8* bitcast ([2 x i32]* @"\01L_OBJC_IMAGE_INFO" to i8*)], section "llvm.metadata"
2680
2681 ; A simple loop. Eliminate the retain and release inside of it!
2682
2683 ; CHECK: define void @loop
2684 ; CHECK: for.body:
2685 ; CHECK-NOT: @objc_
2686 ; CHECK: @objc_msgSend
2687 ; CHECK-NOT: @objc_
2688 ; CHECK: for.end:
2689 define void @loop(i8* %x, i64 %n) {
2690 entry:
2691   %0 = tail call i8* @objc_retain(i8* %x) nounwind
2692   %cmp9 = icmp sgt i64 %n, 0
2693   br i1 %cmp9, label %for.body, label %for.end
2694
2695 for.body:                                         ; preds = %entry, %for.body
2696   %i.010 = phi i64 [ %inc, %for.body ], [ 0, %entry ]
2697   %1 = tail call i8* @objc_retain(i8* %x) nounwind
2698   %tmp5 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_", align 8
2699   %call = tail call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %1, i8* %tmp5)
2700   tail call void @objc_release(i8* %1) nounwind, !clang.imprecise_release !0
2701   %inc = add nsw i64 %i.010, 1
2702   %exitcond = icmp eq i64 %inc, %n
2703   br i1 %exitcond, label %for.end, label %for.body
2704
2705 for.end:                                          ; preds = %for.body, %entry
2706   tail call void @objc_release(i8* %x) nounwind, !clang.imprecise_release !0
2707   ret void
2708 }
2709
2710 ; ObjCARCOpt can delete the retain,release on self.
2711
2712 ; CHECK: define void @TextEditTest
2713 ; CHECK-NOT: call i8* @objc_retain(i8* %tmp7)
2714 ; CHECK: }
2715
2716 %0 = type { i8* (i8*, %struct._message_ref_t*, ...)*, i8* }
2717 %1 = type opaque
2718 %2 = type opaque
2719 %3 = type opaque
2720 %4 = type opaque
2721 %5 = type opaque
2722 %struct.NSConstantString = type { i32*, i32, i8*, i64 }
2723 %struct._NSRange = type { i64, i64 }
2724 %struct.__CFString = type opaque
2725 %struct.__method_list_t = type { i32, i32, [0 x %struct._objc_method] }
2726 %struct._class_ro_t = type { i32, i32, i32, i8*, i8*, %struct.__method_list_t*, %struct._objc_protocol_list*, %struct._ivar_list_t*, i8*, %struct._prop_list_t* }
2727 %struct._class_t = type { %struct._class_t*, %struct._class_t*, %struct._objc_cache*, i8* (i8*, i8*)**, %struct._class_ro_t* }
2728 %struct._ivar_list_t = type { i32, i32, [0 x %struct._ivar_t] }
2729 %struct._ivar_t = type { i64*, i8*, i8*, i32, i32 }
2730 %struct._message_ref_t = type { i8*, i8* }
2731 %struct._objc_cache = type opaque
2732 %struct._objc_method = type { i8*, i8*, i8* }
2733 %struct._objc_protocol_list = type { i64, [0 x %struct._protocol_t*] }
2734 %struct._prop_list_t = type { i32, i32, [0 x %struct._message_ref_t] }
2735 %struct._protocol_t = type { i8*, i8*, %struct._objc_protocol_list*, %struct.__method_list_t*, %struct.__method_list_t*, %struct.__method_list_t*, %struct.__method_list_t*, %struct._prop_list_t*, i32, i32 }
2736
2737 @"\01L_OBJC_CLASSLIST_REFERENCES_$_17" = external hidden global %struct._class_t*, section "__DATA, __objc_classrefs, regular, no_dead_strip", align 8
2738 @kUTTypePlainText = external constant %struct.__CFString*
2739 @"\01L_OBJC_SELECTOR_REFERENCES_19" = external hidden global i8*, section "__DATA, __objc_selrefs, literal_pointers, no_dead_strip"
2740 @"\01L_OBJC_SELECTOR_REFERENCES_21" = external hidden global i8*, section "__DATA, __objc_selrefs, literal_pointers, no_dead_strip"
2741 @"\01L_OBJC_SELECTOR_REFERENCES_23" = external hidden global i8*, section "__DATA, __objc_selrefs, literal_pointers, no_dead_strip"
2742 @"\01L_OBJC_SELECTOR_REFERENCES_25" = external hidden global i8*, section "__DATA, __objc_selrefs, literal_pointers, no_dead_strip"
2743 @"\01L_OBJC_CLASSLIST_REFERENCES_$_26" = external hidden global %struct._class_t*, section "__DATA, __objc_classrefs, regular, no_dead_strip", align 8
2744 @"\01L_OBJC_SELECTOR_REFERENCES_28" = external hidden global i8*, section "__DATA, __objc_selrefs, literal_pointers, no_dead_strip"
2745 @"\01L_OBJC_CLASSLIST_REFERENCES_$_29" = external hidden global %struct._class_t*, section "__DATA, __objc_classrefs, regular, no_dead_strip", align 8
2746 @"\01L_OBJC_SELECTOR_REFERENCES_31" = external hidden global i8*, section "__DATA, __objc_selrefs, literal_pointers, no_dead_strip"
2747 @"\01L_OBJC_SELECTOR_REFERENCES_33" = external hidden global i8*, section "__DATA, __objc_selrefs, literal_pointers, no_dead_strip"
2748 @"\01L_OBJC_SELECTOR_REFERENCES_35" = external hidden global i8*, section "__DATA, __objc_selrefs, literal_pointers, no_dead_strip"
2749 @"\01L_OBJC_SELECTOR_REFERENCES_37" = external hidden global i8*, section "__DATA, __objc_selrefs, literal_pointers, no_dead_strip"
2750 @"\01L_OBJC_CLASSLIST_REFERENCES_$_38" = external hidden global %struct._class_t*, section "__DATA, __objc_classrefs, regular, no_dead_strip", align 8
2751 @"\01L_OBJC_SELECTOR_REFERENCES_40" = external hidden global i8*, section "__DATA, __objc_selrefs, literal_pointers, no_dead_strip"
2752 @"\01L_OBJC_SELECTOR_REFERENCES_42" = external hidden global i8*, section "__DATA, __objc_selrefs, literal_pointers, no_dead_strip"
2753 @_unnamed_cfstring_44 = external hidden constant %struct.NSConstantString, section "__DATA,__cfstring"
2754 @"\01L_OBJC_SELECTOR_REFERENCES_46" = external hidden global i8*, section "__DATA, __objc_selrefs, literal_pointers, no_dead_strip"
2755 @"\01L_OBJC_SELECTOR_REFERENCES_48" = external hidden global i8*, section "__DATA, __objc_selrefs, literal_pointers, no_dead_strip"
2756 @"\01l_objc_msgSend_fixup_isEqual_" = external hidden global %0, section "__DATA, __objc_msgrefs, coalesced", align 16
2757 @"\01L_OBJC_CLASSLIST_REFERENCES_$_50" = external hidden global %struct._class_t*, section "__DATA, __objc_classrefs, regular, no_dead_strip", align 8
2758 @NSCocoaErrorDomain = external constant %1*
2759 @"\01L_OBJC_CLASSLIST_REFERENCES_$_51" = external hidden global %struct._class_t*, section "__DATA, __objc_classrefs, regular, no_dead_strip", align 8
2760 @NSFilePathErrorKey = external constant %1*
2761 @"\01L_OBJC_SELECTOR_REFERENCES_53" = external hidden global i8*, section "__DATA, __objc_selrefs, literal_pointers, no_dead_strip"
2762 @"\01L_OBJC_SELECTOR_REFERENCES_55" = external hidden global i8*, section "__DATA, __objc_selrefs, literal_pointers, no_dead_strip"
2763 @"\01L_OBJC_CLASSLIST_REFERENCES_$_56" = external hidden global %struct._class_t*, section "__DATA, __objc_classrefs, regular, no_dead_strip", align 8
2764 @"\01L_OBJC_SELECTOR_REFERENCES_58" = external hidden global i8*, section "__DATA, __objc_selrefs, literal_pointers, no_dead_strip"
2765 @"\01L_OBJC_SELECTOR_REFERENCES_60" = external hidden global i8*, section "__DATA, __objc_selrefs, literal_pointers, no_dead_strip"
2766
2767 declare %1* @truncatedString(%1*, i64)
2768 define void @TextEditTest(%2* %self, %3* %pboard) {
2769 entry:
2770   %err = alloca %4*, align 8
2771   %tmp7 = bitcast %2* %self to i8*
2772   %tmp8 = call i8* @objc_retain(i8* %tmp7) nounwind
2773   store %4* null, %4** %err, align 8
2774   %tmp1 = load %struct._class_t** @"\01L_OBJC_CLASSLIST_REFERENCES_$_17", align 8
2775   %tmp2 = load %struct.__CFString** @kUTTypePlainText, align 8
2776   %tmp3 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_19", align 8
2777   %tmp4 = bitcast %struct._class_t* %tmp1 to i8*
2778   %call5 = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %tmp4, i8* %tmp3, %struct.__CFString* %tmp2)
2779   %tmp5 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_21", align 8
2780   %tmp6 = bitcast %3* %pboard to i8*
2781   %call76 = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %tmp6, i8* %tmp5, i8* %call5)
2782   %tmp9 = call i8* @objc_retain(i8* %call76) nounwind
2783   %tobool = icmp eq i8* %tmp9, null
2784   br i1 %tobool, label %end, label %land.lhs.true
2785
2786 land.lhs.true:                                    ; preds = %entry
2787   %tmp11 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_23", align 8
2788   %call137 = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %tmp6, i8* %tmp11, i8* %tmp9)
2789   %tmp = bitcast i8* %call137 to %1*
2790   %tmp10 = call i8* @objc_retain(i8* %call137) nounwind
2791   call void @objc_release(i8* null) nounwind
2792   %tmp12 = call i8* @objc_retain(i8* %call137) nounwind
2793   call void @objc_release(i8* null) nounwind
2794   %tobool16 = icmp eq i8* %call137, null
2795   br i1 %tobool16, label %end, label %if.then
2796
2797 if.then:                                          ; preds = %land.lhs.true
2798   %tmp19 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_25", align 8
2799   %call21 = call signext i8 bitcast (i8* (i8*, i8*, ...)* @objc_msgSend to i8 (i8*, i8*)*)(i8* %call137, i8* %tmp19)
2800   %tobool22 = icmp eq i8 %call21, 0
2801   br i1 %tobool22, label %if.then44, label %land.lhs.true23
2802
2803 land.lhs.true23:                                  ; preds = %if.then
2804   %tmp24 = load %struct._class_t** @"\01L_OBJC_CLASSLIST_REFERENCES_$_26", align 8
2805   %tmp26 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_28", align 8
2806   %tmp27 = bitcast %struct._class_t* %tmp24 to i8*
2807   %call2822 = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %tmp27, i8* %tmp26, i8* %call137)
2808   %tmp13 = bitcast i8* %call2822 to %5*
2809   %tmp14 = call i8* @objc_retain(i8* %call2822) nounwind
2810   call void @objc_release(i8* null) nounwind
2811   %tobool30 = icmp eq i8* %call2822, null
2812   br i1 %tobool30, label %if.then44, label %if.end
2813
2814 if.end:                                           ; preds = %land.lhs.true23
2815   %tmp32 = load %struct._class_t** @"\01L_OBJC_CLASSLIST_REFERENCES_$_29", align 8
2816   %tmp33 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_31", align 8
2817   %tmp34 = bitcast %struct._class_t* %tmp32 to i8*
2818   %call35 = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %tmp34, i8* %tmp33)
2819   %tmp37 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_33", align 8
2820   %call3923 = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %call35, i8* %tmp37, i8* %call2822, i32 signext 1, %4** %err)
2821   %cmp = icmp eq i8* %call3923, null
2822   br i1 %cmp, label %if.then44, label %end
2823
2824 if.then44:                                        ; preds = %if.end, %land.lhs.true23, %if.then
2825   %url.025 = phi %5* [ %tmp13, %if.end ], [ %tmp13, %land.lhs.true23 ], [ null, %if.then ]
2826   %tmp49 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_35", align 8
2827   %call51 = call %struct._NSRange bitcast (i8* (i8*, i8*, ...)* @objc_msgSend to %struct._NSRange (i8*, i8*, i64, i64)*)(i8* %call137, i8* %tmp49, i64 0, i64 0)
2828   %call513 = extractvalue %struct._NSRange %call51, 0
2829   %call514 = extractvalue %struct._NSRange %call51, 1
2830   %tmp52 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_37", align 8
2831   %call548 = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %call137, i8* %tmp52, i64 %call513, i64 %call514)
2832   %tmp55 = load %struct._class_t** @"\01L_OBJC_CLASSLIST_REFERENCES_$_38", align 8
2833   %tmp56 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_40", align 8
2834   %tmp57 = bitcast %struct._class_t* %tmp55 to i8*
2835   %call58 = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %tmp57, i8* %tmp56)
2836   %tmp59 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_42", align 8
2837   %call6110 = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %call548, i8* %tmp59, i8* %call58)
2838   %tmp15 = call i8* @objc_retain(i8* %call6110) nounwind
2839   call void @objc_release(i8* %call137) nounwind
2840   %tmp64 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_46", align 8
2841   %call66 = call signext i8 bitcast (i8* (i8*, i8*, ...)* @objc_msgSend to i8 (i8*, i8*, %1*)*)(i8* %call6110, i8* %tmp64, %1* bitcast (%struct.NSConstantString* @_unnamed_cfstring_44 to %1*))
2842   %tobool67 = icmp eq i8 %call66, 0
2843   br i1 %tobool67, label %if.end74, label %if.then68
2844
2845 if.then68:                                        ; preds = %if.then44
2846   %tmp70 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_48", align 8
2847   %call7220 = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %call6110, i8* %tmp70)
2848   %tmp16 = call i8* @objc_retain(i8* %call7220) nounwind
2849   call void @objc_release(i8* %call6110) nounwind
2850   br label %if.end74
2851
2852 if.end74:                                         ; preds = %if.then68, %if.then44
2853   %filename.0.in = phi i8* [ %call7220, %if.then68 ], [ %call6110, %if.then44 ]
2854   %filename.0 = bitcast i8* %filename.0.in to %1*
2855   %tmp17 = load i8** bitcast (%0* @"\01l_objc_msgSend_fixup_isEqual_" to i8**), align 16
2856   %tmp18 = bitcast i8* %tmp17 to i8 (i8*, %struct._message_ref_t*, i8*, ...)*
2857   %call78 = call signext i8 (i8*, %struct._message_ref_t*, i8*, ...)* %tmp18(i8* %call137, %struct._message_ref_t* bitcast (%0* @"\01l_objc_msgSend_fixup_isEqual_" to %struct._message_ref_t*), i8* %filename.0.in)
2858   %tobool79 = icmp eq i8 %call78, 0
2859   br i1 %tobool79, label %land.lhs.true80, label %if.then109
2860
2861 land.lhs.true80:                                  ; preds = %if.end74
2862   %tmp82 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_25", align 8
2863   %call84 = call signext i8 bitcast (i8* (i8*, i8*, ...)* @objc_msgSend to i8 (i8*, i8*)*)(i8* %filename.0.in, i8* %tmp82)
2864   %tobool86 = icmp eq i8 %call84, 0
2865   br i1 %tobool86, label %if.then109, label %if.end106
2866
2867 if.end106:                                        ; preds = %land.lhs.true80
2868   %tmp88 = load %struct._class_t** @"\01L_OBJC_CLASSLIST_REFERENCES_$_26", align 8
2869   %tmp90 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_28", align 8
2870   %tmp91 = bitcast %struct._class_t* %tmp88 to i8*
2871   %call9218 = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %tmp91, i8* %tmp90, i8* %filename.0.in)
2872   %tmp20 = bitcast i8* %call9218 to %5*
2873   %tmp21 = call i8* @objc_retain(i8* %call9218) nounwind
2874   %tmp22 = bitcast %5* %url.025 to i8*
2875   call void @objc_release(i8* %tmp22) nounwind
2876   %tmp94 = load %struct._class_t** @"\01L_OBJC_CLASSLIST_REFERENCES_$_29", align 8
2877   %tmp95 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_31", align 8
2878   %tmp96 = bitcast %struct._class_t* %tmp94 to i8*
2879   %call97 = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %tmp96, i8* %tmp95)
2880   %tmp99 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_33", align 8
2881   %call10119 = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %call97, i8* %tmp99, i8* %call9218, i32 signext 1, %4** %err)
2882   %phitmp = icmp eq i8* %call10119, null
2883   br i1 %phitmp, label %if.then109, label %end
2884
2885 if.then109:                                       ; preds = %if.end106, %land.lhs.true80, %if.end74
2886   %url.129 = phi %5* [ %tmp20, %if.end106 ], [ %url.025, %if.end74 ], [ %url.025, %land.lhs.true80 ]
2887   %tmp110 = load %4** %err, align 8
2888   %tobool111 = icmp eq %4* %tmp110, null
2889   br i1 %tobool111, label %if.then112, label %if.end125
2890
2891 if.then112:                                       ; preds = %if.then109
2892   %tmp113 = load %struct._class_t** @"\01L_OBJC_CLASSLIST_REFERENCES_$_50", align 8
2893   %tmp114 = load %1** @NSCocoaErrorDomain, align 8
2894   %tmp115 = load %struct._class_t** @"\01L_OBJC_CLASSLIST_REFERENCES_$_51", align 8
2895   %call117 = call %1* @truncatedString(%1* %filename.0, i64 1034)
2896   %tmp118 = load %1** @NSFilePathErrorKey, align 8
2897   %tmp119 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_53", align 8
2898   %tmp120 = bitcast %struct._class_t* %tmp115 to i8*
2899   %call12113 = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %tmp120, i8* %tmp119, %1* %call117, %1* %tmp118, i8* null)
2900   %tmp122 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_55", align 8
2901   %tmp123 = bitcast %struct._class_t* %tmp113 to i8*
2902   %call12414 = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %tmp123, i8* %tmp122, %1* %tmp114, i64 258, i8* %call12113)
2903   %tmp23 = call i8* @objc_retain(i8* %call12414) nounwind
2904   %tmp25 = call i8* @objc_autorelease(i8* %tmp23) nounwind
2905   %tmp28 = bitcast i8* %tmp25 to %4*
2906   store %4* %tmp28, %4** %err, align 8
2907   br label %if.end125
2908
2909 if.end125:                                        ; preds = %if.then112, %if.then109
2910   %tmp127 = phi %4* [ %tmp110, %if.then109 ], [ %tmp28, %if.then112 ]
2911   %tmp126 = load %struct._class_t** @"\01L_OBJC_CLASSLIST_REFERENCES_$_56", align 8
2912   %tmp128 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_58", align 8
2913   %tmp129 = bitcast %struct._class_t* %tmp126 to i8*
2914   %call13015 = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %tmp129, i8* %tmp128, %4* %tmp127)
2915   %tmp131 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_60", align 8
2916   %call13317 = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %call13015, i8* %tmp131)
2917   br label %end
2918
2919 end:                                              ; preds = %if.end125, %if.end106, %if.end, %land.lhs.true, %entry
2920   %filename.2 = phi %1* [ %filename.0, %if.end106 ], [ %filename.0, %if.end125 ], [ %tmp, %land.lhs.true ], [ null, %entry ], [ %tmp, %if.end ]
2921   %origFilename.0 = phi %1* [ %tmp, %if.end106 ], [ %tmp, %if.end125 ], [ %tmp, %land.lhs.true ], [ null, %entry ], [ %tmp, %if.end ]
2922   %url.2 = phi %5* [ %tmp20, %if.end106 ], [ %url.129, %if.end125 ], [ null, %land.lhs.true ], [ null, %entry ], [ %tmp13, %if.end ]
2923   call void @objc_release(i8* %tmp9) nounwind, !clang.imprecise_release !0
2924   %tmp29 = bitcast %5* %url.2 to i8*
2925   call void @objc_release(i8* %tmp29) nounwind, !clang.imprecise_release !0
2926   %tmp30 = bitcast %1* %origFilename.0 to i8*
2927   call void @objc_release(i8* %tmp30) nounwind, !clang.imprecise_release !0
2928   %tmp31 = bitcast %1* %filename.2 to i8*
2929   call void @objc_release(i8* %tmp31) nounwind, !clang.imprecise_release !0
2930   call void @objc_release(i8* %tmp7) nounwind, !clang.imprecise_release !0
2931   ret void
2932 }
2933
2934 !0 = metadata !{}
2935
2936 declare i32 @__gxx_personality_v0(...)
2937
2938 ; CHECK: attributes #0 = { nounwind readnone }
2939 ; CHECK: attributes [[NUW]] = { nounwind }