Remove an optimization where we were changing an objc_autorelease into an objc_autore...
[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
24 declare void @llvm.dbg.value(metadata, i64, metadata)
25
26 declare i8* @objc_msgSend(i8*, i8*, ...)
27
28 ; Simple retain+release pair deletion, with some intervening control
29 ; flow and harmless instructions.
30
31 ; CHECK: define void @test0(
32 ; CHECK-NOT: @objc_
33 ; CHECK: }
34 define void @test0(i32* %x, i1 %p) nounwind {
35 entry:
36   %a = bitcast i32* %x to i8*
37   %0 = call i8* @objc_retain(i8* %a) nounwind
38   br i1 %p, label %t, label %f
39
40 t:
41   store i8 3, i8* %a
42   %b = bitcast i32* %x to float*
43   store float 2.0, float* %b
44   br label %return
45
46 f:
47   store i32 7, i32* %x
48   br label %return
49
50 return:
51   %c = bitcast i32* %x to i8*
52   call void @objc_release(i8* %c) nounwind
53   ret void
54 }
55
56 ; Like test0 but the release isn't always executed when the retain is,
57 ; so the optimization is not safe.
58
59 ; TODO: Make the objc_release's argument be %0.
60
61 ; CHECK: define void @test1(
62 ; CHECK: @objc_retain(i8* %a)
63 ; CHECK: @objc_release
64 ; CHECK: }
65 define void @test1(i32* %x, i1 %p, i1 %q) nounwind {
66 entry:
67   %a = bitcast i32* %x to i8*
68   %0 = call i8* @objc_retain(i8* %a) nounwind
69   br i1 %p, label %t, label %f
70
71 t:
72   store i8 3, i8* %a
73   %b = bitcast i32* %x to float*
74   store float 2.0, float* %b
75   br label %return
76
77 f:
78   store i32 7, i32* %x
79   call void @callee()
80   br i1 %q, label %return, label %alt_return
81
82 return:
83   %c = bitcast i32* %x to i8*
84   call void @objc_release(i8* %c) nounwind
85   ret void
86
87 alt_return:
88   ret void
89 }
90
91 ; Don't do partial elimination into two different CFG diamonds.
92
93 ; CHECK: define void @test1b(
94 ; CHECK: entry:
95 ; CHECK:   tail call i8* @objc_retain(i8* %x) [[NUW:#[0-9]+]]
96 ; CHECK-NOT: @objc_
97 ; CHECK: if.end5:
98 ; CHECK:   tail call void @objc_release(i8* %x) [[NUW]], !clang.imprecise_release !0
99 ; CHECK-NOT: @objc_
100 ; CHECK: }
101 define void @test1b(i8* %x, i1 %p, i1 %q) {
102 entry:
103   tail call i8* @objc_retain(i8* %x) nounwind
104   br i1 %p, label %if.then, label %if.end
105
106 if.then:                                          ; preds = %entry
107   tail call void @callee()
108   br label %if.end
109
110 if.end:                                           ; preds = %if.then, %entry
111   br i1 %q, label %if.then3, label %if.end5
112
113 if.then3:                                         ; preds = %if.end
114   tail call void @use_pointer(i8* %x)
115   br label %if.end5
116
117 if.end5:                                          ; preds = %if.then3, %if.end
118   tail call void @objc_release(i8* %x) nounwind, !clang.imprecise_release !0
119   ret void
120 }
121
122 ; Like test0 but the pointer is passed to an intervening call,
123 ; so the optimization is not safe.
124
125 ; CHECK: define void @test2(
126 ; CHECK: @objc_retain(i8* %a)
127 ; CHECK: @objc_release
128 ; CHECK: }
129 define void @test2(i32* %x, i1 %p) nounwind {
130 entry:
131   %a = bitcast i32* %x to i8*
132   %0 = call i8* @objc_retain(i8* %a) nounwind
133   br i1 %p, label %t, label %f
134
135 t:
136   store i8 3, i8* %a
137   %b = bitcast i32* %x to float*
138   store float 2.0, float* %b
139   br label %return
140
141 f:
142   store i32 7, i32* %x
143   call void @use_pointer(i8* %0)
144   %d = bitcast i32* %x to float*
145   store float 3.0, float* %d
146   br label %return
147
148 return:
149   %c = bitcast i32* %x to i8*
150   call void @objc_release(i8* %c) nounwind
151   ret void
152 }
153
154 ; Like test0 but the release is in a loop,
155 ; so the optimization is not safe.
156
157 ; TODO: For now, assume this can't happen.
158
159 ; CHECK: define void @test3(
160 ; TODO: @objc_retain(i8* %a)
161 ; TODO: @objc_release
162 ; CHECK: }
163 define void @test3(i32* %x, i1* %q) nounwind {
164 entry:
165   %a = bitcast i32* %x to i8*
166   %0 = call i8* @objc_retain(i8* %a) nounwind
167   br label %loop
168
169 loop:
170   %c = bitcast i32* %x to i8*
171   call void @objc_release(i8* %c) nounwind
172   %j = load volatile i1* %q
173   br i1 %j, label %loop, label %return
174
175 return:
176   ret void
177 }
178
179 ; TODO: For now, assume this can't happen.
180
181 ; Like test0 but the retain is in a loop,
182 ; so the optimization is not safe.
183
184 ; CHECK: define void @test4(
185 ; TODO: @objc_retain(i8* %a)
186 ; TODO: @objc_release
187 ; CHECK: }
188 define void @test4(i32* %x, i1* %q) nounwind {
189 entry:
190   br label %loop
191
192 loop:
193   %a = bitcast i32* %x to i8*
194   %0 = call i8* @objc_retain(i8* %a) nounwind
195   %j = load volatile i1* %q
196   br i1 %j, label %loop, label %return
197
198 return:
199   %c = bitcast i32* %x to i8*
200   call void @objc_release(i8* %c) nounwind
201   ret void
202 }
203
204 ; Like test0 but the pointer is conditionally passed to an intervening call,
205 ; so the optimization is not safe.
206
207 ; CHECK: define void @test5(
208 ; CHECK: @objc_retain(i8*
209 ; CHECK: @objc_release
210 ; CHECK: }
211 define void @test5(i32* %x, i1 %q, i8* %y) nounwind {
212 entry:
213   %a = bitcast i32* %x to i8*
214   %0 = call i8* @objc_retain(i8* %a) nounwind
215   %s = select i1 %q, i8* %y, i8* %0
216   call void @use_pointer(i8* %s)
217   store i32 7, i32* %x
218   %c = bitcast i32* %x to i8*
219   call void @objc_release(i8* %c) nounwind
220   ret void
221 }
222
223 ; retain+release pair deletion, where the release happens on two different
224 ; flow paths.
225
226 ; CHECK: define void @test6(
227 ; CHECK-NOT: @objc_
228 ; CHECK: }
229 define void @test6(i32* %x, i1 %p) nounwind {
230 entry:
231   %a = bitcast i32* %x to i8*
232   %0 = call i8* @objc_retain(i8* %a) nounwind
233   br i1 %p, label %t, label %f
234
235 t:
236   store i8 3, i8* %a
237   %b = bitcast i32* %x to float*
238   store float 2.0, float* %b
239   %ct = bitcast i32* %x to i8*
240   call void @objc_release(i8* %ct) nounwind
241   br label %return
242
243 f:
244   store i32 7, i32* %x
245   call void @callee()
246   %cf = bitcast i32* %x to i8*
247   call void @objc_release(i8* %cf) nounwind
248   br label %return
249
250 return:
251   ret void
252 }
253
254 ; retain+release pair deletion, where the retain happens on two different
255 ; flow paths.
256
257 ; CHECK: define void @test7(
258 ; CHECK-NOT: @objc_
259 ; CHECK: }
260 define void @test7(i32* %x, i1 %p) nounwind {
261 entry:
262   %a = bitcast i32* %x to i8*
263   br i1 %p, label %t, label %f
264
265 t:
266   %0 = call i8* @objc_retain(i8* %a) nounwind
267   store i8 3, i8* %a
268   %b = bitcast i32* %x to float*
269   store float 2.0, float* %b
270   br label %return
271
272 f:
273   %1 = call i8* @objc_retain(i8* %a) nounwind
274   store i32 7, i32* %x
275   call void @callee()
276   br label %return
277
278 return:
279   %c = bitcast i32* %x to i8*
280   call void @objc_release(i8* %c) nounwind
281   ret void
282 }
283
284 ; Like test7, but there's a retain/retainBlock mismatch. Don't delete!
285
286 ; CHECK: define void @test7b
287 ; CHECK: t:
288 ; CHECK: call i8* @objc_retainBlock
289 ; CHECK: f:
290 ; CHECK: call i8* @objc_retain
291 ; CHECK: return:
292 ; CHECK: call void @objc_release
293 ; CHECK: }
294 define void @test7b(i32* %x, i1 %p) nounwind {
295 entry:
296   %a = bitcast i32* %x to i8*
297   br i1 %p, label %t, label %f
298
299 t:
300   %0 = call i8* @objc_retainBlock(i8* %a) nounwind
301   store i8 3, i8* %a
302   %b = bitcast i32* %x to float*
303   store float 2.0, float* %b
304   br label %return
305
306 f:
307   %1 = call i8* @objc_retain(i8* %a) nounwind
308   store i32 7, i32* %x
309   call void @callee()
310   br label %return
311
312 return:
313   %c = bitcast i32* %x to i8*
314   call void @objc_release(i8* %c) nounwind
315   ret void
316 }
317
318 ; retain+release pair deletion, where the retain and release both happen on
319 ; different flow paths. Wild!
320
321 ; CHECK: define void @test8(
322 ; CHECK-NOT: @objc_
323 ; CHECK: }
324 define void @test8(i32* %x, i1 %p, i1 %q) nounwind {
325 entry:
326   %a = bitcast i32* %x to i8*
327   br i1 %p, label %t, label %f
328
329 t:
330   %0 = call i8* @objc_retain(i8* %a) nounwind
331   store i8 3, i8* %a
332   %b = bitcast i32* %x to float*
333   store float 2.0, float* %b
334   br label %mid
335
336 f:
337   %1 = call i8* @objc_retain(i8* %a) nounwind
338   store i32 7, i32* %x
339   br label %mid
340
341 mid:
342   br i1 %q, label %u, label %g
343
344 u:
345   call void @callee()
346   %cu = bitcast i32* %x to i8*
347   call void @objc_release(i8* %cu) nounwind
348   br label %return
349
350 g:
351   %cg = bitcast i32* %x to i8*
352   call void @objc_release(i8* %cg) nounwind
353   br label %return
354
355 return:
356   ret void
357 }
358
359 ; Trivial retain+release pair deletion.
360
361 ; CHECK: define void @test9(
362 ; CHECK-NOT: @objc_
363 ; CHECK: }
364 define void @test9(i8* %x) nounwind {
365 entry:
366   %0 = call i8* @objc_retain(i8* %x) nounwind
367   call void @objc_release(i8* %0) nounwind
368   ret void
369 }
370
371 ; Retain+release pair, but on an unknown pointer relationship. Don't delete!
372
373 ; CHECK: define void @test9b
374 ; CHECK: @objc_retain(i8* %x)
375 ; CHECK: @objc_release(i8* %s)
376 ; CHECK: }
377 define void @test9b(i8* %x, i1 %j, i8* %p) nounwind {
378 entry:
379   %0 = call i8* @objc_retain(i8* %x) nounwind
380   %s = select i1 %j, i8* %x, i8* %p
381   call void @objc_release(i8* %s) nounwind
382   ret void
383 }
384
385 ; Trivial retain+release pair with intervening calls - don't delete!
386
387 ; CHECK: define void @test10(
388 ; CHECK: @objc_retain(i8* %x)
389 ; CHECK: @callee
390 ; CHECK: @use_pointer
391 ; CHECK: @objc_release
392 ; CHECK: }
393 define void @test10(i8* %x) nounwind {
394 entry:
395   %0 = call i8* @objc_retain(i8* %x) nounwind
396   call void @callee()
397   call void @use_pointer(i8* %x)
398   call void @objc_release(i8* %0) nounwind
399   ret void
400 }
401
402 ; Trivial retain+autoreleaserelease pair. Don't delete!
403 ; Also, add a tail keyword, since objc_retain can never be passed
404 ; a stack argument.
405
406 ; CHECK: define void @test11(
407 ; CHECK: tail call i8* @objc_retain(i8* %x) [[NUW]]
408 ; CHECK: call i8* @objc_autorelease(i8* %0) [[NUW]]
409 ; CHECK: }
410 define void @test11(i8* %x) nounwind {
411 entry:
412   %0 = call i8* @objc_retain(i8* %x) nounwind
413   call i8* @objc_autorelease(i8* %0) nounwind
414   call void @use_pointer(i8* %x)
415   ret void
416 }
417
418 ; Same as test11 but with no use_pointer call. Delete the pair!
419
420 ; CHECK: define void @test11a(
421 ; CHECK: entry:
422 ; CHECK-NEXT: ret void
423 ; CHECK: }
424 define void @test11a(i8* %x) nounwind {
425 entry:
426   %0 = call i8* @objc_retain(i8* %x) nounwind
427   call i8* @objc_autorelease(i8* %0) nounwind
428   ret void
429 }
430
431 ; Same as test11 but the value is returned. Do not perform an RV optimization
432 ; since if the frontend emitted code for an __autoreleasing variable, we may
433 ; want it to be in the autorelease pool.
434
435 ; CHECK: define i8* @test11b(
436 ; CHECK: tail call i8* @objc_retain(i8* %x) [[NUW]]
437 ; CHECK: call i8* @objc_autorelease(i8* %0) [[NUW]]
438 ; CHECK: }
439 define i8* @test11b(i8* %x) nounwind {
440 entry:
441   %0 = call i8* @objc_retain(i8* %x) nounwind
442   call i8* @objc_autorelease(i8* %0) nounwind
443   ret i8* %x
444 }
445
446 ; Trivial retain,release pair with intervening call, but it's dominated
447 ; by another retain - delete!
448
449 ; CHECK: define void @test12(
450 ; CHECK-NEXT: entry:
451 ; CHECK-NEXT: @objc_retain(i8* %x)
452 ; CHECK-NOT: @objc_
453 ; CHECK: }
454 define void @test12(i8* %x, i64 %n) {
455 entry:
456   call i8* @objc_retain(i8* %x) nounwind
457   call i8* @objc_retain(i8* %x) nounwind
458   call void @use_pointer(i8* %x)
459   call void @use_pointer(i8* %x)
460   call void @objc_release(i8* %x) nounwind
461   ret void
462 }
463
464 ; Trivial retain,autorelease pair. Don't delete!
465
466 ; CHECK: define void @test13(
467 ; CHECK: tail call i8* @objc_retain(i8* %x) [[NUW]]
468 ; CHECK: tail call i8* @objc_retain(i8* %x) [[NUW]]
469 ; CHECK: @use_pointer(i8* %x)
470 ; CHECK: call i8* @objc_autorelease(i8* %x) [[NUW]]
471 ; CHECK: }
472 define void @test13(i8* %x, i64 %n) {
473 entry:
474   call i8* @objc_retain(i8* %x) nounwind
475   call i8* @objc_retain(i8* %x) nounwind
476   call void @use_pointer(i8* %x)
477   call i8* @objc_autorelease(i8* %x) nounwind
478   ret void
479 }
480
481 ; Delete the retain+release pair.
482
483 ; CHECK: define void @test13b
484 ; CHECK-NEXT: entry:
485 ; CHECK-NEXT: @objc_retain(i8* %x)
486 ; CHECK-NEXT: @use_pointer
487 ; CHECK-NEXT: @use_pointer
488 ; CHECK-NEXT: ret void
489 define void @test13b(i8* %x, i64 %n) {
490 entry:
491   call i8* @objc_retain(i8* %x) nounwind
492   call i8* @objc_retain(i8* %x) nounwind
493   call void @use_pointer(i8* %x)
494   call void @use_pointer(i8* %x)
495   call void @objc_release(i8* %x) nounwind
496   ret void
497 }
498
499 ; Don't delete the retain+release pair because there's an
500 ; autoreleasePoolPop in the way.
501
502 ; CHECK: define void @test13c
503 ; CHECK: @objc_retain(i8* %x)
504 ; CHECK: @objc_autoreleasePoolPop
505 ; CHECK: @objc_retain(i8* %x)
506 ; CHECK: @use_pointer
507 ; CHECK: @objc_release
508 ; CHECK: }
509 define void @test13c(i8* %x, i64 %n) {
510 entry:
511   call i8* @objc_retain(i8* %x) nounwind
512   call void @objc_autoreleasePoolPop(i8* undef)
513   call i8* @objc_retain(i8* %x) nounwind
514   call void @use_pointer(i8* %x)
515   call void @use_pointer(i8* %x)
516   call void @objc_release(i8* %x) nounwind
517   ret void
518 }
519
520 ; Like test13c, but there's an autoreleasePoolPush in the way, but that
521 ; doesn't matter.
522
523 ; CHECK: define void @test13d
524 ; CHECK-NEXT: entry:
525 ; CHECK-NEXT: @objc_retain(i8* %x)
526 ; CHECK-NEXT: @objc_autoreleasePoolPush
527 ; CHECK-NEXT: @use_pointer
528 ; CHECK-NEXT: @use_pointer
529 ; CHECK-NEXT: ret void
530 define void @test13d(i8* %x, i64 %n) {
531 entry:
532   call i8* @objc_retain(i8* %x) nounwind
533   call i8* @objc_autoreleasePoolPush()
534   call i8* @objc_retain(i8* %x) nounwind
535   call void @use_pointer(i8* %x)
536   call void @use_pointer(i8* %x)
537   call void @objc_release(i8* %x) nounwind
538   ret void
539 }
540
541 ; Trivial retain,release pair with intervening call, but it's post-dominated
542 ; by another release - delete!
543
544 ; CHECK: define void @test14(
545 ; CHECK-NEXT: entry:
546 ; CHECK-NEXT: @use_pointer
547 ; CHECK-NEXT: @use_pointer
548 ; CHECK-NEXT: @objc_release
549 ; CHECK-NEXT: ret void
550 ; CHECK-NEXT: }
551 define void @test14(i8* %x, i64 %n) {
552 entry:
553   call i8* @objc_retain(i8* %x) nounwind
554   call void @use_pointer(i8* %x)
555   call void @use_pointer(i8* %x)
556   call void @objc_release(i8* %x) nounwind
557   call void @objc_release(i8* %x) nounwind
558   ret void
559 }
560
561 ; Trivial retain,autorelease pair with intervening call, but it's post-dominated
562 ; by another release. Don't delete anything.
563
564 ; CHECK: define void @test15(
565 ; CHECK-NEXT: entry:
566 ; CHECK-NEXT: @objc_retain(i8* %x)
567 ; CHECK-NEXT: @use_pointer
568 ; CHECK-NEXT: @objc_autorelease(i8* %x)
569 ; CHECK-NEXT: @objc_release
570 ; CHECK-NEXT: ret void
571 ; CHECK-NEXT: }
572 define void @test15(i8* %x, i64 %n) {
573 entry:
574   call i8* @objc_retain(i8* %x) nounwind
575   call void @use_pointer(i8* %x)
576   call i8* @objc_autorelease(i8* %x) nounwind
577   call void @objc_release(i8* %x) nounwind
578   ret void
579 }
580
581 ; Trivial retain,autorelease pair, post-dominated
582 ; by another release. Delete the retain and release.
583
584 ; CHECK: define void @test15b
585 ; CHECK-NEXT: entry:
586 ; CHECK-NEXT: @objc_autorelease
587 ; CHECK-NEXT: ret void
588 ; CHECK-NEXT: }
589 define void @test15b(i8* %x, i64 %n) {
590 entry:
591   call i8* @objc_retain(i8* %x) nounwind
592   call i8* @objc_autorelease(i8* %x) nounwind
593   call void @objc_release(i8* %x) nounwind
594   ret void
595 }
596
597 ; Retain+release pairs in diamonds, all dominated by a retain.
598
599 ; CHECK: define void @test16(
600 ; CHECK: @objc_retain(i8* %x)
601 ; CHECK-NOT: @objc
602 ; CHECK: }
603 define void @test16(i1 %a, i1 %b, i8* %x) {
604 entry:
605   call i8* @objc_retain(i8* %x) nounwind
606   br i1 %a, label %red, label %orange
607
608 red:
609   call i8* @objc_retain(i8* %x) nounwind
610   br label %yellow
611
612 orange:
613   call i8* @objc_retain(i8* %x) nounwind
614   br label %yellow
615
616 yellow:
617   call void @use_pointer(i8* %x)
618   call void @use_pointer(i8* %x)
619   br i1 %b, label %green, label %blue
620
621 green:
622   call void @objc_release(i8* %x) nounwind
623   br label %purple
624
625 blue:
626   call void @objc_release(i8* %x) nounwind
627   br label %purple
628
629 purple:
630   ret void
631 }
632
633 ; Retain+release pairs in diamonds, all post-dominated by a release.
634
635 ; CHECK: define void @test17(
636 ; CHECK-NOT: @objc_
637 ; CHECK: purple:
638 ; CHECK: @objc_release
639 ; CHECK: }
640 define void @test17(i1 %a, i1 %b, i8* %x) {
641 entry:
642   br i1 %a, label %red, label %orange
643
644 red:
645   call i8* @objc_retain(i8* %x) nounwind
646   br label %yellow
647
648 orange:
649   call i8* @objc_retain(i8* %x) nounwind
650   br label %yellow
651
652 yellow:
653   call void @use_pointer(i8* %x)
654   call void @use_pointer(i8* %x)
655   br i1 %b, label %green, label %blue
656
657 green:
658   call void @objc_release(i8* %x) nounwind
659   br label %purple
660
661 blue:
662   call void @objc_release(i8* %x) nounwind
663   br label %purple
664
665 purple:
666   call void @objc_release(i8* %x) nounwind
667   ret void
668 }
669
670 ; Delete no-ops.
671
672 ; CHECK: define void @test18(
673 ; CHECK-NOT: @objc_
674 ; CHECK: }
675 define void @test18() {
676   call i8* @objc_retain(i8* null)
677   call void @objc_release(i8* null)
678   call i8* @objc_autorelease(i8* null)
679   ret void
680 }
681
682 ; Delete no-ops where undef can be assumed to be null.
683
684 ; CHECK: define void @test18b
685 ; CHECK-NOT: @objc_
686 ; CHECK: }
687 define void @test18b() {
688   call i8* @objc_retain(i8* undef)
689   call void @objc_release(i8* undef)
690   call i8* @objc_autorelease(i8* undef)
691   ret void
692 }
693
694 ; Replace uses of arguments with uses of return values, to reduce
695 ; register pressure.
696
697 ; CHECK: define void @test19(i32* %y) {
698 ; CHECK:   %z = bitcast i32* %y to i8*
699 ; CHECK:   %0 = bitcast i32* %y to i8*
700 ; CHECK:   %1 = tail call i8* @objc_retain(i8* %0)
701 ; CHECK:   call void @use_pointer(i8* %z)
702 ; CHECK:   call void @use_pointer(i8* %z)
703 ; CHECK:   %2 = bitcast i32* %y to i8*
704 ; CHECK:   call void @objc_release(i8* %2)
705 ; CHECK:   ret void
706 ; CHECK: }
707 define void @test19(i32* %y) {
708 entry:
709   %x = bitcast i32* %y to i8*
710   %0 = call i8* @objc_retain(i8* %x) nounwind
711   %z = bitcast i32* %y to i8*
712   call void @use_pointer(i8* %z)
713   call void @use_pointer(i8* %z)
714   call void @objc_release(i8* %x)
715   ret void
716 }
717
718 ; Bitcast insertion
719
720 ; CHECK: define void @test20(
721 ; CHECK: %tmp1 = tail call i8* @objc_retain(i8* %tmp) [[NUW]]
722 ; CHECK-NEXT: invoke
723 define void @test20(double* %self) {
724 if.then12:
725   %tmp = bitcast double* %self to i8*
726   %tmp1 = call i8* @objc_retain(i8* %tmp) nounwind
727   invoke void @invokee()
728           to label %invoke.cont23 unwind label %lpad20
729
730 invoke.cont23:                                    ; preds = %if.then12
731   invoke void @invokee()
732           to label %if.end unwind label %lpad20
733
734 lpad20:                                           ; preds = %invoke.cont23, %if.then12
735   %tmp502 = phi double* [ undef, %invoke.cont23 ], [ %self, %if.then12 ]
736   %exn = landingpad {i8*, i32} personality i32 (...)* @__gxx_personality_v0
737            cleanup
738   unreachable
739
740 if.end:                                           ; preds = %invoke.cont23
741   ret void
742 }
743
744 ; Delete a redundant retain,autorelease when forwaring a call result
745 ; directly to a return value.
746
747 ; CHECK: define i8* @test21(
748 ; CHECK: call i8* @returner()
749 ; CHECK-NEXT: ret i8* %call
750 define i8* @test21() {
751 entry:
752   %call = call i8* @returner()
753   %0 = call i8* @objc_retain(i8* %call) nounwind
754   %1 = call i8* @objc_autorelease(i8* %0) nounwind
755   ret i8* %1
756 }
757
758 ; Move an objc call up through a phi that has null operands.
759
760 ; CHECK: define void @test22(
761 ; CHECK: B:
762 ; CHECK:   %1 = bitcast double* %p to i8*
763 ; CHECK:   call void @objc_release(i8* %1)
764 ; CHECK:   br label %C
765 ; CHECK: C:                                                ; preds = %B, %A
766 ; CHECK-NOT: @objc_release
767 ; CHECK: }
768 define void @test22(double* %p, i1 %a) {
769   br i1 %a, label %A, label %B
770 A:
771   br label %C
772 B:
773   br label %C
774 C:
775   %h = phi double* [ null, %A ], [ %p, %B ]
776   %c = bitcast double* %h to i8*
777   call void @objc_release(i8* %c)
778   ret void
779 }
780
781 ; Optimize objc_retainBlock.
782
783 ; CHECK: define void @test23(
784 ; CHECK-NOT: @objc_
785 ; CHECK: }
786 %block0 = type { i64, i64, i8*, i8* }
787 %block1 = type { i8**, i32, i32, i32 (%struct.__block_literal_1*)*, %block0* }
788 %struct.__block_descriptor = type { i64, i64 }
789 %struct.__block_literal_1 = type { i8**, i32, i32, i8**, %struct.__block_descriptor* }
790 @__block_holder_tmp_1 = external constant %block1
791 define void @test23() {
792 entry:
793   %0 = call i8* @objc_retainBlock(i8* bitcast (%block1* @__block_holder_tmp_1 to i8*)) nounwind, !clang.arc.copy_on_escape !0
794   call void @bar(i32 ()* bitcast (%block1* @__block_holder_tmp_1 to i32 ()*))
795   call void @bar(i32 ()* bitcast (%block1* @__block_holder_tmp_1 to i32 ()*))
796   call void @objc_release(i8* bitcast (%block1* @__block_holder_tmp_1 to i8*)) nounwind
797   ret void
798 }
799
800 ; Don't optimize objc_retainBlock, but do strength reduce it.
801
802 ; CHECK: define void @test23b
803 ; CHECK: @objc_retain
804 ; CHECK: @objc_release
805 ; CHECK: }
806 define void @test23b(i8* %p) {
807 entry:
808   %0 = call i8* @objc_retainBlock(i8* %p) nounwind, !clang.arc.copy_on_escape !0
809   call void @callee()
810   call void @use_pointer(i8* %p)
811   call void @objc_release(i8* %p) nounwind
812   ret void
813 }
814
815 ; Don't optimize objc_retainBlock, because there's no copy_on_escape metadata.
816
817 ; CHECK: define void @test23c(
818 ; CHECK: @objc_retainBlock
819 ; CHECK: @objc_release
820 ; CHECK: }
821 define void @test23c() {
822 entry:
823   %0 = call i8* @objc_retainBlock(i8* bitcast (%block1* @__block_holder_tmp_1 to i8*)) nounwind
824   call void @bar(i32 ()* bitcast (%block1* @__block_holder_tmp_1 to i32 ()*))
825   call void @bar(i32 ()* bitcast (%block1* @__block_holder_tmp_1 to i32 ()*))
826   call void @objc_release(i8* bitcast (%block1* @__block_holder_tmp_1 to i8*)) nounwind
827   ret void
828 }
829
830 ; Any call can decrement a retain count.
831
832 ; CHECK: define void @test24(
833 ; CHECK: @objc_retain(i8* %a)
834 ; CHECK: @objc_release
835 ; CHECK: }
836 define void @test24(i8* %r, i8* %a) {
837   call i8* @objc_retain(i8* %a)
838   call void @use_pointer(i8* %r)
839   %q = load i8* %a
840   call void @objc_release(i8* %a)
841   ret void
842 }
843
844 ; Don't move a retain/release pair if the release can be moved
845 ; but the retain can't be moved to balance it.
846
847 ; CHECK: define void @test25(
848 ; CHECK: entry:
849 ; CHECK:   call i8* @objc_retain(i8* %p)
850 ; CHECK: true:
851 ; CHECK: done:
852 ; CHECK:   call void @objc_release(i8* %p)
853 ; CHECK: }
854 define void @test25(i8* %p, i1 %x) {
855 entry:
856   %f0 = call i8* @objc_retain(i8* %p)
857   call void @callee()
858   br i1 %x, label %true, label %done
859
860 true:
861   store i8 0, i8* %p
862   br label %done
863
864 done:
865   call void @objc_release(i8* %p)
866   ret void
867 }
868
869 ; Don't move a retain/release pair if the retain can be moved
870 ; but the release can't be moved to balance it.
871
872 ; CHECK: define void @test26(
873 ; CHECK: entry:
874 ; CHECK:   call i8* @objc_retain(i8* %p)
875 ; CHECK: true:
876 ; CHECK: done:
877 ; CHECK:   call void @objc_release(i8* %p)
878 ; CHECK: }
879 define void @test26(i8* %p, i1 %x) {
880 entry:
881   %f0 = call i8* @objc_retain(i8* %p)
882   br i1 %x, label %true, label %done
883
884 true:
885   call void @callee()
886   br label %done
887
888 done:
889   store i8 0, i8* %p
890   call void @objc_release(i8* %p)
891   ret void
892 }
893
894 ; Don't sink the retain,release into the loop.
895
896 ; CHECK: define void @test27(
897 ; CHECK: entry:
898 ; CHECK: call i8* @objc_retain(i8* %p)
899 ; CHECK: loop:
900 ; CHECK-NOT: @objc_
901 ; CHECK: done:
902 ; CHECK: call void @objc_release
903 ; CHECK: }
904 define void @test27(i8* %p, i1 %x, i1 %y) {
905 entry: 
906   %f0 = call i8* @objc_retain(i8* %p)
907   br i1 %x, label %loop, label %done
908
909 loop:
910   call void @callee()
911   store i8 0, i8* %p
912   br i1 %y, label %done, label %loop
913   
914 done: 
915   call void @objc_release(i8* %p)
916   ret void
917 }
918
919 ; Trivial code motion case: Triangle.
920
921 ; CHECK: define void @test28(
922 ; CHECK-NOT: @objc_
923 ; CHECK: true:
924 ; CHECK: call i8* @objc_retain(
925 ; CHECK: call void @callee()
926 ; CHECK: store
927 ; CHECK: call void @objc_release
928 ; CHECK: done:
929 ; CHECK-NOT: @objc_
930 ; CHECK: }
931 define void @test28(i8* %p, i1 %x) {
932 entry:
933   %f0 = call i8* @objc_retain(i8* %p)
934   br i1 %x, label %true, label %done
935
936 true:
937   call void @callee()
938   store i8 0, i8* %p
939   br label %done
940
941 done:
942   call void @objc_release(i8* %p), !clang.imprecise_release !0
943   ret void
944 }
945
946 ; Trivial code motion case: Triangle, but no metadata. Don't move past
947 ; unrelated memory references!
948
949 ; CHECK: define void @test28b
950 ; CHECK: call i8* @objc_retain(
951 ; CHECK: true:
952 ; CHECK-NOT: @objc_
953 ; CHECK: call void @callee()
954 ; CHECK-NOT: @objc_
955 ; CHECK: store
956 ; CHECK-NOT: @objc_
957 ; CHECK: done:
958 ; CHECK: @objc_release
959 ; CHECK: }
960 define void @test28b(i8* %p, i1 %x, i8* noalias %t) {
961 entry:
962   %f0 = call i8* @objc_retain(i8* %p)
963   br i1 %x, label %true, label %done
964
965 true:
966   call void @callee()
967   store i8 0, i8* %p
968   br label %done
969
970 done:
971   store i8 0, i8* %t
972   call void @objc_release(i8* %p)
973   ret void
974 }
975
976 ; Trivial code motion case: Triangle, with metadata. Do move past
977 ; unrelated memory references! And preserve the metadata.
978
979 ; CHECK: define void @test28c
980 ; CHECK-NOT: @objc_
981 ; CHECK: true:
982 ; CHECK: call i8* @objc_retain(
983 ; CHECK: call void @callee()
984 ; CHECK: store
985 ; CHECK: call void @objc_release(i8* %p) [[NUW]], !clang.imprecise_release
986 ; CHECK: done:
987 ; CHECK-NOT: @objc_
988 ; CHECK: }
989 define void @test28c(i8* %p, i1 %x, i8* noalias %t) {
990 entry:
991   %f0 = call i8* @objc_retain(i8* %p)
992   br i1 %x, label %true, label %done
993
994 true:
995   call void @callee()
996   store i8 0, i8* %p
997   br label %done
998
999 done:
1000   store i8 0, i8* %t
1001   call void @objc_release(i8* %p), !clang.imprecise_release !0
1002   ret void
1003 }
1004
1005 ; Like test28. but with two releases.
1006
1007 ; CHECK: define void @test29(
1008 ; CHECK-NOT: @objc_
1009 ; CHECK: true:
1010 ; CHECK: call i8* @objc_retain(
1011 ; CHECK: call void @callee()
1012 ; CHECK: store
1013 ; CHECK: call void @objc_release
1014 ; CHECK-NOT: @objc_release
1015 ; CHECK: done:
1016 ; CHECK-NOT: @objc_
1017 ; CHECK: ohno:
1018 ; CHECK-NOT: @objc_
1019 ; CHECK: }
1020 define void @test29(i8* %p, i1 %x, i1 %y) {
1021 entry:
1022   %f0 = call i8* @objc_retain(i8* %p)
1023   br i1 %x, label %true, label %done
1024
1025 true:
1026   call void @callee()
1027   store i8 0, i8* %p
1028   br i1 %y, label %done, label %ohno
1029
1030 done:
1031   call void @objc_release(i8* %p)
1032   ret void
1033
1034 ohno:
1035   call void @objc_release(i8* %p)
1036   ret void
1037 }
1038
1039 ; Basic case with the use and call in a diamond
1040 ; with an extra release.
1041
1042 ; CHECK: define void @test30(
1043 ; CHECK-NOT: @objc_
1044 ; CHECK: true:
1045 ; CHECK: call i8* @objc_retain(
1046 ; CHECK: call void @callee()
1047 ; CHECK: store
1048 ; CHECK: call void @objc_release
1049 ; CHECK-NOT: @objc_release
1050 ; CHECK: false:
1051 ; CHECK-NOT: @objc_
1052 ; CHECK: done:
1053 ; CHECK-NOT: @objc_
1054 ; CHECK: ohno:
1055 ; CHECK-NOT: @objc_
1056 ; CHECK: }
1057 define void @test30(i8* %p, i1 %x, i1 %y, i1 %z) {
1058 entry:
1059   %f0 = call i8* @objc_retain(i8* %p)
1060   br i1 %x, label %true, label %false
1061
1062 true:
1063   call void @callee()
1064   store i8 0, i8* %p
1065   br i1 %y, label %done, label %ohno
1066
1067 false:
1068   br i1 %z, label %done, label %ohno
1069
1070 done:
1071   call void @objc_release(i8* %p)
1072   ret void
1073
1074 ohno:
1075   call void @objc_release(i8* %p)
1076   ret void
1077 }
1078
1079 ; Basic case with a mergeable release.
1080
1081 ; CHECK: define void @test31(
1082 ; CHECK: call i8* @objc_retain(i8* %p)
1083 ; CHECK: call void @callee()
1084 ; CHECK: store
1085 ; CHECK: call void @objc_release
1086 ; CHECK-NOT: @objc_release
1087 ; CHECK: true:
1088 ; CHECK-NOT: @objc_release
1089 ; CHECK: false:
1090 ; CHECK-NOT: @objc_release
1091 ; CHECK: ret void
1092 ; CHECK-NOT: @objc_release
1093 ; CHECK: }
1094 define void @test31(i8* %p, i1 %x) {
1095 entry:
1096   %f0 = call i8* @objc_retain(i8* %p)
1097   call void @callee()
1098   store i8 0, i8* %p
1099   br i1 %x, label %true, label %false
1100 true:
1101   call void @objc_release(i8* %p)
1102   ret void
1103 false:
1104   call void @objc_release(i8* %p)
1105   ret void
1106 }
1107
1108 ; Don't consider bitcasts or getelementptrs direct uses.
1109
1110 ; CHECK: define void @test32(
1111 ; CHECK-NOT: @objc_
1112 ; CHECK: true:
1113 ; CHECK: call i8* @objc_retain(
1114 ; CHECK: call void @callee()
1115 ; CHECK: store
1116 ; CHECK: call void @objc_release
1117 ; CHECK: done:
1118 ; CHECK-NOT: @objc_
1119 ; CHECK: }
1120 define void @test32(i8* %p, i1 %x) {
1121 entry:
1122   %f0 = call i8* @objc_retain(i8* %p)
1123   br i1 %x, label %true, label %done
1124
1125 true:
1126   call void @callee()
1127   store i8 0, i8* %p
1128   br label %done
1129
1130 done:
1131   %g = bitcast i8* %p to i8*
1132   %h = getelementptr i8* %g, i64 0
1133   call void @objc_release(i8* %g)
1134   ret void
1135 }
1136
1137 ; Do consider icmps to be direct uses.
1138
1139 ; CHECK: define void @test33(
1140 ; CHECK-NOT: @objc_
1141 ; CHECK: true:
1142 ; CHECK: call i8* @objc_retain(
1143 ; CHECK: call void @callee()
1144 ; CHECK: icmp
1145 ; CHECK: call void @objc_release
1146 ; CHECK: done:
1147 ; CHECK-NOT: @objc_
1148 ; CHECK: }
1149 define void @test33(i8* %p, i1 %x, i8* %y) {
1150 entry:
1151   %f0 = call i8* @objc_retain(i8* %p)
1152   br i1 %x, label %true, label %done
1153
1154 true:
1155   call void @callee()
1156   %v = icmp eq i8* %p, %y
1157   br label %done
1158
1159 done:
1160   %g = bitcast i8* %p to i8*
1161   %h = getelementptr i8* %g, i64 0
1162   call void @objc_release(i8* %g)
1163   ret void
1164 }
1165
1166 ; Delete retain,release if there's just a possible dec.
1167
1168 ; CHECK: define void @test34(
1169 ; CHECK-NOT: @objc_
1170 ; CHECK: }
1171 define void @test34(i8* %p, i1 %x, i8* %y) {
1172 entry:
1173   %f0 = call i8* @objc_retain(i8* %p)
1174   br i1 %x, label %true, label %done
1175
1176 true:
1177   call void @callee()
1178   br label %done
1179
1180 done:
1181   %g = bitcast i8* %p to i8*
1182   %h = getelementptr i8* %g, i64 0
1183   call void @objc_release(i8* %g)
1184   ret void
1185 }
1186
1187 ; Delete retain,release if there's just a use.
1188
1189 ; CHECK: define void @test35(
1190 ; CHECK-NOT: @objc_
1191 ; CHECK: }
1192 define void @test35(i8* %p, i1 %x, i8* %y) {
1193 entry:
1194   %f0 = call i8* @objc_retain(i8* %p)
1195   br i1 %x, label %true, label %done
1196
1197 true:
1198   %v = icmp eq i8* %p, %y
1199   br label %done
1200
1201 done:
1202   %g = bitcast i8* %p to i8*
1203   %h = getelementptr i8* %g, i64 0
1204   call void @objc_release(i8* %g)
1205   ret void
1206 }
1207
1208 ; Delete a retain,release if there's no actual use.
1209
1210 ; CHECK: define void @test36(
1211 ; CHECK-NOT: @objc_
1212 ; CHECK: call void @callee()
1213 ; CHECK-NOT: @objc_
1214 ; CHECK: call void @callee()
1215 ; CHECK-NOT: @objc_
1216 ; CHECK: }
1217 define void @test36(i8* %p) {
1218 entry:
1219   call i8* @objc_retain(i8* %p)
1220   call void @callee()
1221   call void @callee()
1222   call void @objc_release(i8* %p)
1223   ret void
1224 }
1225
1226 ; Like test36, but with metadata.
1227
1228 ; CHECK: define void @test37(
1229 ; CHECK-NOT: @objc_
1230 ; CHECK: }
1231 define void @test37(i8* %p) {
1232 entry:
1233   call i8* @objc_retain(i8* %p)
1234   call void @callee()
1235   call void @callee()
1236   call void @objc_release(i8* %p), !clang.imprecise_release !0
1237   ret void
1238 }
1239
1240 ; Be aggressive about analyzing phis to eliminate possible uses.
1241
1242 ; CHECK: define void @test38(
1243 ; CHECK-NOT: @objc_
1244 ; CHECK: }
1245 define void @test38(i8* %p, i1 %u, i1 %m, i8* %z, i8* %y, i8* %x, i8* %w) {
1246 entry:
1247   call i8* @objc_retain(i8* %p)
1248   br i1 %u, label %true, label %false
1249 true:
1250   br i1 %m, label %a, label %b
1251 false:
1252   br i1 %m, label %c, label %d
1253 a:
1254   br label %e
1255 b:
1256   br label %e
1257 c:
1258   br label %f
1259 d:
1260   br label %f
1261 e:
1262   %j = phi i8* [ %z, %a ], [ %y, %b ]
1263   br label %g
1264 f:
1265   %k = phi i8* [ %w, %c ], [ %x, %d ]
1266   br label %g
1267 g:
1268   %h = phi i8* [ %j, %e ], [ %k, %f ]
1269   call void @use_pointer(i8* %h)
1270   call void @objc_release(i8* %p), !clang.imprecise_release !0
1271   ret void
1272 }
1273
1274 ; Delete retain,release pairs around loops.
1275
1276 ; CHECK: define void @test39(
1277 ; CHECK-NOT: @objc_
1278 ; CHECK: }
1279 define void @test39(i8* %p) {
1280 entry:
1281   %0 = call i8* @objc_retain(i8* %p)
1282   br label %loop
1283
1284 loop:                                             ; preds = %loop, %entry
1285   br i1 undef, label %loop, label %exit
1286
1287 exit:                                             ; preds = %loop
1288   call void @objc_release(i8* %0), !clang.imprecise_release !0
1289   ret void
1290 }
1291
1292 ; Delete retain,release pairs around loops containing uses.
1293
1294 ; CHECK: define void @test39b(
1295 ; CHECK-NOT: @objc_
1296 ; CHECK: }
1297 define void @test39b(i8* %p) {
1298 entry:
1299   %0 = call i8* @objc_retain(i8* %p)
1300   br label %loop
1301
1302 loop:                                             ; preds = %loop, %entry
1303   store i8 0, i8* %0
1304   br i1 undef, label %loop, label %exit
1305
1306 exit:                                             ; preds = %loop
1307   call void @objc_release(i8* %0), !clang.imprecise_release !0
1308   ret void
1309 }
1310
1311 ; Delete retain,release pairs around loops containing potential decrements.
1312
1313 ; CHECK: define void @test39c(
1314 ; CHECK-NOT: @objc_
1315 ; CHECK: }
1316 define void @test39c(i8* %p) {
1317 entry:
1318   %0 = call i8* @objc_retain(i8* %p)
1319   br label %loop
1320
1321 loop:                                             ; preds = %loop, %entry
1322   call void @use_pointer(i8* %0)
1323   br i1 undef, label %loop, label %exit
1324
1325 exit:                                             ; preds = %loop
1326   call void @objc_release(i8* %0), !clang.imprecise_release !0
1327   ret void
1328 }
1329
1330 ; Delete retain,release pairs around loops even if
1331 ; the successors are in a different order.
1332
1333 ; CHECK: define void @test40(
1334 ; CHECK-NOT: @objc_
1335 ; CHECK: }
1336 define void @test40(i8* %p) {
1337 entry:
1338   %0 = call i8* @objc_retain(i8* %p)
1339   br label %loop
1340
1341 loop:                                             ; preds = %loop, %entry
1342   call void @use_pointer(i8* %0)
1343   br i1 undef, label %exit, label %loop
1344
1345 exit:                                             ; preds = %loop
1346   call void @objc_release(i8* %0), !clang.imprecise_release !0
1347   ret void
1348 }
1349
1350 ; Do the known-incremented retain+release elimination even if the pointer
1351 ; is also autoreleased.
1352
1353 ; CHECK: define void @test42(
1354 ; CHECK-NEXT: entry:
1355 ; CHECK-NEXT: call i8* @objc_retain(i8* %p)
1356 ; CHECK-NEXT: call i8* @objc_autorelease(i8* %p)
1357 ; CHECK-NEXT: call void @use_pointer(i8* %p)
1358 ; CHECK-NEXT: call void @use_pointer(i8* %p)
1359 ; CHECK-NEXT: ret void
1360 ; CHECK-NEXT: }
1361 define void @test42(i8* %p) {
1362 entry:
1363   call i8* @objc_retain(i8* %p)
1364   call i8* @objc_autorelease(i8* %p)
1365   call i8* @objc_retain(i8* %p)
1366   call void @use_pointer(i8* %p)
1367   call void @use_pointer(i8* %p)
1368   call void @objc_release(i8* %p)
1369   ret void
1370 }
1371
1372 ; Don't the known-incremented retain+release elimination if the pointer is
1373 ; autoreleased and there's an autoreleasePoolPop.
1374
1375 ; CHECK: define void @test43(
1376 ; CHECK-NEXT: entry:
1377 ; CHECK-NEXT: call i8* @objc_retain(i8* %p)
1378 ; CHECK-NEXT: call i8* @objc_autorelease(i8* %p)
1379 ; CHECK-NEXT: call i8* @objc_retain
1380 ; CHECK-NEXT: call void @use_pointer(i8* %p)
1381 ; CHECK-NEXT: call void @use_pointer(i8* %p)
1382 ; CHECK-NEXT: call void @objc_autoreleasePoolPop(i8* undef)
1383 ; CHECK-NEXT: call void @objc_release
1384 ; CHECK-NEXT: ret void
1385 ; CHECK-NEXT: }
1386 define void @test43(i8* %p) {
1387 entry:
1388   call i8* @objc_retain(i8* %p)
1389   call i8* @objc_autorelease(i8* %p)
1390   call i8* @objc_retain(i8* %p)
1391   call void @use_pointer(i8* %p)
1392   call void @use_pointer(i8* %p)
1393   call void @objc_autoreleasePoolPop(i8* undef)
1394   call void @objc_release(i8* %p)
1395   ret void
1396 }
1397
1398 ; Do the known-incremented retain+release elimination if the pointer is
1399 ; autoreleased and there's an autoreleasePoolPush.
1400
1401 ; CHECK: define void @test43b
1402 ; CHECK-NEXT: entry:
1403 ; CHECK-NEXT: call i8* @objc_retain(i8* %p)
1404 ; CHECK-NEXT: call i8* @objc_autorelease(i8* %p)
1405 ; CHECK-NEXT: call void @use_pointer(i8* %p)
1406 ; CHECK-NEXT: call void @use_pointer(i8* %p)
1407 ; CHECK-NEXT: call i8* @objc_autoreleasePoolPush()
1408 ; CHECK-NEXT: ret void
1409 ; CHECK-NEXT: }
1410 define void @test43b(i8* %p) {
1411 entry:
1412   call i8* @objc_retain(i8* %p)
1413   call i8* @objc_autorelease(i8* %p)
1414   call i8* @objc_retain(i8* %p)
1415   call void @use_pointer(i8* %p)
1416   call void @use_pointer(i8* %p)
1417   call i8* @objc_autoreleasePoolPush()
1418   call void @objc_release(i8* %p)
1419   ret void
1420 }
1421
1422 ; Do retain+release elimination for non-provenance pointers.
1423
1424 ; CHECK: define void @test44(
1425 ; CHECK-NOT: objc_
1426 ; CHECK: }
1427 define void @test44(i8** %pp) {
1428   %p = load i8** %pp
1429   %q = call i8* @objc_retain(i8* %p)
1430   call void @objc_release(i8* %q)
1431   ret void
1432 }
1433
1434 ; Don't delete retain+release with an unknown-provenance
1435 ; may-alias objc_release between them.
1436
1437 ; CHECK: define void @test45(
1438 ; CHECK: call i8* @objc_retain(i8* %p)
1439 ; CHECK: call void @objc_release(i8* %q)
1440 ; CHECK: call void @use_pointer(i8* %p)
1441 ; CHECK: call void @objc_release(i8* %p)
1442 define void @test45(i8** %pp, i8** %qq) {
1443   %p = load i8** %pp
1444   %q = load i8** %qq
1445   call i8* @objc_retain(i8* %p)
1446   call void @objc_release(i8* %q)
1447   call void @use_pointer(i8* %p)
1448   call void @objc_release(i8* %p)
1449   ret void
1450 }
1451
1452 ; Don't delete retain and autorelease here.
1453
1454 ; CHECK: define void @test46(
1455 ; CHECK: tail call i8* @objc_retain(i8* %p) [[NUW]]
1456 ; CHECK: true:
1457 ; CHECK: call i8* @objc_autorelease(i8* %p) [[NUW]]
1458 define void @test46(i8* %p, i1 %a) {
1459 entry:
1460   call i8* @objc_retain(i8* %p)
1461   br i1 %a, label %true, label %false
1462
1463 true:
1464   call i8* @objc_autorelease(i8* %p)
1465   call void @use_pointer(i8* %p)
1466   ret void
1467
1468 false:
1469   ret void
1470 }
1471
1472 ; Delete no-op cast calls.
1473
1474 ; CHECK: define i8* @test47(
1475 ; CHECK-NOT: call
1476 ; CHECK: ret i8* %p
1477 define i8* @test47(i8* %p) nounwind {
1478   %x = call i8* @objc_retainedObject(i8* %p)
1479   ret i8* %x
1480 }
1481
1482 ; Delete no-op cast calls.
1483
1484 ; CHECK: define i8* @test48(
1485 ; CHECK-NOT: call
1486 ; CHECK: ret i8* %p
1487 define i8* @test48(i8* %p) nounwind {
1488   %x = call i8* @objc_unretainedObject(i8* %p)
1489   ret i8* %x
1490 }
1491
1492 ; Delete no-op cast calls.
1493
1494 ; CHECK: define i8* @test49(
1495 ; CHECK-NOT: call
1496 ; CHECK: ret i8* %p
1497 define i8* @test49(i8* %p) nounwind {
1498   %x = call i8* @objc_unretainedPointer(i8* %p)
1499   ret i8* %x
1500 }
1501
1502 ; Do delete retain+release with intervening stores of the
1503 ; address value.
1504
1505 ; CHECK: define void @test50(
1506 ; CHECK-NOT: @objc_
1507 ; CHECK: }
1508 define void @test50(i8* %p, i8** %pp) {
1509   call i8* @objc_retain(i8* %p)
1510   call void @callee()
1511   store i8* %p, i8** %pp
1512   call void @objc_release(i8* %p)
1513   ret void
1514 }
1515
1516 ; Don't delete retain+release with intervening stores through the
1517 ; address value.
1518
1519 ; CHECK: define void @test51(
1520 ; CHECK: call i8* @objc_retain(i8* %p)
1521 ; CHECK: call void @objc_release(i8* %p)
1522 define void @test51(i8* %p) {
1523   call i8* @objc_retain(i8* %p)
1524   call void @callee()
1525   store i8 0, i8* %p
1526   call void @objc_release(i8* %p)
1527   ret void
1528 }
1529
1530 ; Don't delete retain+release with intervening use of a pointer of
1531 ; unknown provenance.
1532
1533 ; CHECK: define void @test52(
1534 ; CHECK: call i8* @objc_retain
1535 ; CHECK: call void @callee()
1536 ; CHECK: call void @use_pointer(i8* %z)
1537 ; CHECK: call void @objc_release
1538 define void @test52(i8** %zz, i8** %pp) {
1539   %p = load i8** %pp
1540   %1 = call i8* @objc_retain(i8* %p)
1541   call void @callee()
1542   %z = load i8** %zz
1543   call void @use_pointer(i8* %z)
1544   call void @objc_release(i8* %p)
1545   ret void
1546 }
1547
1548 ; Like test52, but the pointer has function type, so it's assumed to
1549 ; be not reference counted.
1550 ; Oops. That's wrong. Clang sometimes uses function types gratuitously.
1551 ; See rdar://10551239.
1552
1553 ; CHECK: define void @test53(
1554 ; CHECK: @objc_
1555 ; CHECK: }
1556 define void @test53(void ()** %zz, i8** %pp) {
1557   %p = load i8** %pp
1558   %1 = call i8* @objc_retain(i8* %p)
1559   call void @callee()
1560   %z = load void ()** %zz
1561   call void @callee_fnptr(void ()* %z)
1562   call void @objc_release(i8* %p)
1563   ret void
1564 }
1565
1566 ; Convert autorelease to release if the value is unused.
1567
1568 ; CHECK: define void @test54(
1569 ; CHECK: call i8* @returner()
1570 ; CHECK-NEXT: call void @objc_release(i8* %t) [[NUW]], !clang.imprecise_release !0
1571 ; CHECK-NEXT: ret void
1572 define void @test54() {
1573   %t = call i8* @returner()
1574   call i8* @objc_autorelease(i8* %t)
1575   ret void
1576 }
1577
1578 ; Nested retain+release pairs. Delete them both.
1579
1580 ; CHECK: define void @test55(
1581 ; CHECK-NOT: @objc
1582 ; CHECK: }
1583 define void @test55(i8* %x) { 
1584 entry: 
1585   %0 = call i8* @objc_retain(i8* %x) nounwind 
1586   %1 = call i8* @objc_retain(i8* %x) nounwind 
1587   call void @objc_release(i8* %x) nounwind 
1588   call void @objc_release(i8* %x) nounwind 
1589   ret void 
1590 }
1591
1592 ; Nested retain+release pairs where the inner pair depends
1593 ; on the outer pair to be removed, and then the outer pair
1594 ; can be partially eliminated. Plus an extra outer pair to
1595 ; eliminate, for fun.
1596
1597 ; CHECK: define void @test56(
1598 ; CHECK-NOT: @objc
1599 ; CHECK: if.then:
1600 ; CHECK-NEXT: %0 = tail call i8* @objc_retain(i8* %x) [[NUW]]
1601 ; CHECK-NEXT: tail call void @use_pointer(i8* %x)
1602 ; CHECK-NEXT: tail call void @use_pointer(i8* %x)
1603 ; CHECK-NEXT: tail call void @objc_release(i8* %x) [[NUW]], !clang.imprecise_release !0
1604 ; CHECK-NEXT: br label %if.end
1605 ; CHECK-NOT: @objc
1606 ; CHECK: }
1607 define void @test56(i8* %x, i32 %n) {
1608 entry:
1609   %0 = tail call i8* @objc_retain(i8* %x) nounwind
1610   %1 = tail call i8* @objc_retain(i8* %0) nounwind
1611   %tobool = icmp eq i32 %n, 0
1612   br i1 %tobool, label %if.end, label %if.then
1613
1614 if.then:                                          ; preds = %entry
1615   %2 = tail call i8* @objc_retain(i8* %1) nounwind
1616   tail call void @use_pointer(i8* %2)
1617   tail call void @use_pointer(i8* %2)
1618   tail call void @objc_release(i8* %2) nounwind, !clang.imprecise_release !0
1619   br label %if.end
1620
1621 if.end:                                           ; preds = %entry, %if.then
1622   tail call void @objc_release(i8* %1) nounwind, !clang.imprecise_release !0
1623   tail call void @objc_release(i8* %0) nounwind, !clang.imprecise_release !0
1624   ret void
1625 }
1626
1627 ; When there are adjacent retain+release pairs, the first one is
1628 ; known unnecessary because the presence of the second one means that
1629 ; the first one won't be deleting the object.
1630
1631 ; CHECK:      define void @test57(
1632 ; CHECK-NEXT: entry:
1633 ; CHECK-NEXT:   call void @use_pointer(i8* %x)
1634 ; CHECK-NEXT:   call void @use_pointer(i8* %x)
1635 ; CHECK-NEXT:   %0 = tail call i8* @objc_retain(i8* %x) [[NUW]]
1636 ; CHECK-NEXT:   call void @use_pointer(i8* %x)
1637 ; CHECK-NEXT:   call void @use_pointer(i8* %x)
1638 ; CHECK-NEXT:   call void @objc_release(i8* %x) [[NUW]]
1639 ; CHECK-NEXT:   ret void
1640 ; CHECK-NEXT: }
1641 define void @test57(i8* %x) nounwind {
1642 entry:
1643   call i8* @objc_retain(i8* %x) nounwind
1644   call void @use_pointer(i8* %x)
1645   call void @use_pointer(i8* %x)
1646   call void @objc_release(i8* %x) nounwind
1647   call i8* @objc_retain(i8* %x) nounwind
1648   call void @use_pointer(i8* %x)
1649   call void @use_pointer(i8* %x)
1650   call void @objc_release(i8* %x) nounwind
1651   ret void
1652 }
1653
1654 ; An adjacent retain+release pair is sufficient even if it will be
1655 ; removed itself.
1656
1657 ; CHECK:      define void @test58(
1658 ; CHECK-NEXT: entry:
1659 ; CHECK-NEXT:   call void @use_pointer(i8* %x)
1660 ; CHECK-NEXT:   call void @use_pointer(i8* %x)
1661 ; CHECK-NEXT:   ret void
1662 ; CHECK-NEXT: }
1663 define void @test58(i8* %x) nounwind {
1664 entry:
1665   call i8* @objc_retain(i8* %x) nounwind
1666   call void @use_pointer(i8* %x)
1667   call void @use_pointer(i8* %x)
1668   call void @objc_release(i8* %x) nounwind
1669   call i8* @objc_retain(i8* %x) nounwind
1670   call void @objc_release(i8* %x) nounwind
1671   ret void
1672 }
1673
1674 ; Don't delete the second retain+release pair in an adjacent set.
1675
1676 ; CHECK:      define void @test59(
1677 ; CHECK-NEXT: entry:
1678 ; CHECK-NEXT:   %0 = tail call i8* @objc_retain(i8* %x) [[NUW]]
1679 ; CHECK-NEXT:   call void @use_pointer(i8* %x)
1680 ; CHECK-NEXT:   call void @use_pointer(i8* %x)
1681 ; CHECK-NEXT:   call void @objc_release(i8* %x) [[NUW]]
1682 ; CHECK-NEXT:   ret void
1683 ; CHECK-NEXT: }
1684 define void @test59(i8* %x) nounwind {
1685 entry:
1686   %a = call i8* @objc_retain(i8* %x) nounwind
1687   call void @objc_release(i8* %x) nounwind
1688   %b = call i8* @objc_retain(i8* %x) nounwind
1689   call void @use_pointer(i8* %x)
1690   call void @use_pointer(i8* %x)
1691   call void @objc_release(i8* %x) nounwind
1692   ret void
1693 }
1694
1695 ; Constant pointers to objects don't need reference counting.
1696
1697 @constptr = external constant i8*
1698 @something = external global i8*
1699
1700 ; CHECK: define void @test60(
1701 ; CHECK-NOT: @objc_
1702 ; CHECK: }
1703 define void @test60() {
1704   %t = load i8** @constptr
1705   %s = load i8** @something
1706   call i8* @objc_retain(i8* %s)
1707   call void @callee()
1708   call void @use_pointer(i8* %t)
1709   call void @objc_release(i8* %s)
1710   ret void
1711 }
1712
1713 ; Constant pointers to objects don't need to be considered related to other
1714 ; pointers.
1715
1716 ; CHECK: define void @test61(
1717 ; CHECK-NOT: @objc_
1718 ; CHECK: }
1719 define void @test61() {
1720   %t = load i8** @constptr
1721   call i8* @objc_retain(i8* %t)
1722   call void @callee()
1723   call void @use_pointer(i8* %t)
1724   call void @objc_release(i8* %t)
1725   ret void
1726 }
1727
1728 ; Delete a retain matched by releases when one is inside the loop and the
1729 ; other is outside the loop.
1730
1731 ; CHECK: define void @test62(
1732 ; CHECK-NOT: @objc_
1733 ; CHECK: }
1734 define void @test62(i8* %x, i1* %p) nounwind {
1735 entry:
1736   br label %loop
1737
1738 loop:
1739   call i8* @objc_retain(i8* %x)
1740   %q = load i1* %p
1741   br i1 %q, label %loop.more, label %exit
1742
1743 loop.more:
1744   call void @objc_release(i8* %x)
1745   br label %loop
1746
1747 exit:
1748   call void @objc_release(i8* %x)
1749   ret void
1750 }
1751
1752 ; Like test62 but with no release in exit.
1753 ; Don't delete anything!
1754
1755 ; CHECK: define void @test63(
1756 ; CHECK: loop:
1757 ; CHECK:   tail call i8* @objc_retain(i8* %x)
1758 ; CHECK: loop.more:
1759 ; CHECK:   call void @objc_release(i8* %x)
1760 ; CHECK: }
1761 define void @test63(i8* %x, i1* %p) nounwind {
1762 entry:
1763   br label %loop
1764
1765 loop:
1766   call i8* @objc_retain(i8* %x)
1767   %q = load i1* %p
1768   br i1 %q, label %loop.more, label %exit
1769
1770 loop.more:
1771   call void @objc_release(i8* %x)
1772   br label %loop
1773
1774 exit:
1775   ret void
1776 }
1777
1778 ; Like test62 but with no release in loop.more.
1779 ; Don't delete anything!
1780
1781 ; CHECK: define void @test64(
1782 ; CHECK: loop:
1783 ; CHECK:   tail call i8* @objc_retain(i8* %x)
1784 ; CHECK: exit:
1785 ; CHECK:   call void @objc_release(i8* %x)
1786 ; CHECK: }
1787 define void @test64(i8* %x, i1* %p) nounwind {
1788 entry:
1789   br label %loop
1790
1791 loop:
1792   call i8* @objc_retain(i8* %x)
1793   %q = load i1* %p
1794   br i1 %q, label %loop.more, label %exit
1795
1796 loop.more:
1797   br label %loop
1798
1799 exit:
1800   call void @objc_release(i8* %x)
1801   ret void
1802 }
1803
1804 ; Move an autorelease past a phi with a null.
1805
1806 ; CHECK: define i8* @test65(
1807 ; CHECK: if.then:
1808 ; CHECK:   call i8* @objc_autorelease(
1809 ; CHECK: return:
1810 ; CHECK-NOT: @objc_autorelease
1811 ; CHECK: }
1812 define i8* @test65(i1 %x) {
1813 entry:
1814   br i1 %x, label %return, label %if.then
1815
1816 if.then:                                          ; preds = %entry
1817   %c = call i8* @returner()
1818   %s = call i8* @objc_retainAutoreleasedReturnValue(i8* %c) nounwind
1819   br label %return
1820
1821 return:                                           ; preds = %if.then, %entry
1822   %retval = phi i8* [ %s, %if.then ], [ null, %entry ]
1823   %q = call i8* @objc_autorelease(i8* %retval) nounwind
1824   ret i8* %retval
1825 }
1826
1827 ; Don't move an autorelease past an autorelease pool boundary.
1828
1829 ; CHECK: define i8* @test65b(
1830 ; CHECK: if.then:
1831 ; CHECK-NOT: @objc_autorelease
1832 ; CHECK: return:
1833 ; CHECK:   call i8* @objc_autorelease(
1834 ; CHECK: }
1835 define i8* @test65b(i1 %x) {
1836 entry:
1837   %t = call i8* @objc_autoreleasePoolPush()
1838   br i1 %x, label %return, label %if.then
1839
1840 if.then:                                          ; preds = %entry
1841   %c = call i8* @returner()
1842   %s = call i8* @objc_retainAutoreleasedReturnValue(i8* %c) nounwind
1843   br label %return
1844
1845 return:                                           ; preds = %if.then, %entry
1846   %retval = phi i8* [ %s, %if.then ], [ null, %entry ]
1847   call void @objc_autoreleasePoolPop(i8* %t)
1848   %q = call i8* @objc_autorelease(i8* %retval) nounwind
1849   ret i8* %retval
1850 }
1851
1852 ; Don't move an autoreleaseReuturnValue, which would break
1853 ; the RV optimization.
1854
1855 ; CHECK: define i8* @test65c(
1856 ; CHECK: if.then:
1857 ; CHECK-NOT: @objc_autorelease
1858 ; CHECK: return:
1859 ; CHECK:   call i8* @objc_autoreleaseReturnValue(
1860 ; CHECK: }
1861 define i8* @test65c(i1 %x) {
1862 entry:
1863   br i1 %x, label %return, label %if.then
1864
1865 if.then:                                          ; preds = %entry
1866   %c = call i8* @returner()
1867   %s = call i8* @objc_retainAutoreleasedReturnValue(i8* %c) nounwind
1868   br label %return
1869
1870 return:                                           ; preds = %if.then, %entry
1871   %retval = phi i8* [ %s, %if.then ], [ null, %entry ]
1872   %q = call i8* @objc_autoreleaseReturnValue(i8* %retval) nounwind
1873   ret i8* %retval
1874 }
1875
1876 ; An objc_retain can serve as a may-use for a different pointer.
1877 ; rdar://11931823
1878
1879 ; CHECK: define void @test66(
1880 ; CHECK:   %tmp7 = tail call i8* @objc_retain(i8* %cond) [[NUW]]
1881 ; CHECK:   tail call void @objc_release(i8* %cond) [[NUW]]
1882 ; CHECK: }
1883 define void @test66(i8* %tmp5, i8* %bar, i1 %tobool, i1 %tobool1, i8* %call) {
1884 entry:
1885   br i1 %tobool, label %cond.true, label %cond.end
1886
1887 cond.true:
1888   br label %cond.end
1889
1890 cond.end:                                         ; preds = %cond.true, %entry
1891   %cond = phi i8* [ %tmp5, %cond.true ], [ %call, %entry ]
1892   %tmp7 = tail call i8* @objc_retain(i8* %cond) nounwind
1893   tail call void @objc_release(i8* %call) nounwind
1894   %tmp8 = select i1 %tobool1, i8* %cond, i8* %bar
1895   %tmp9 = tail call i8* @objc_retain(i8* %tmp8) nounwind
1896   tail call void @objc_release(i8* %cond) nounwind
1897   ret void
1898 }
1899
1900 declare void @bar(i32 ()*)
1901
1902 ; A few real-world testcases.
1903
1904 @.str4 = private unnamed_addr constant [33 x i8] c"-[A z] = { %f, %f, { %f, %f } }\0A\00"
1905 @"OBJC_IVAR_$_A.myZ" = global i64 20, section "__DATA, __objc_const", align 8
1906 declare i32 @printf(i8* nocapture, ...) nounwind
1907 declare i32 @puts(i8* nocapture) nounwind
1908 @str = internal constant [16 x i8] c"-[ Top0 _getX ]\00"
1909
1910 ; CHECK: @"\01-[A z]"
1911 ; CHECK-NOT: @objc_
1912 ; CHECK: }
1913
1914 define {<2 x float>, <2 x float>} @"\01-[A z]"({}* %self, i8* nocapture %_cmd) nounwind {
1915 invoke.cont:
1916   %0 = bitcast {}* %self to i8*
1917   %1 = tail call i8* @objc_retain(i8* %0) nounwind
1918   tail call void @llvm.dbg.value(metadata !{{}* %self}, i64 0, metadata !0)
1919   tail call void @llvm.dbg.value(metadata !{{}* %self}, i64 0, metadata !0)
1920   %ivar = load i64* @"OBJC_IVAR_$_A.myZ", align 8
1921   %add.ptr = getelementptr i8* %0, i64 %ivar
1922   %tmp1 = bitcast i8* %add.ptr to float*
1923   %tmp2 = load float* %tmp1, align 4
1924   %conv = fpext float %tmp2 to double
1925   %add.ptr.sum = add i64 %ivar, 4
1926   %tmp6 = getelementptr inbounds i8* %0, i64 %add.ptr.sum
1927   %2 = bitcast i8* %tmp6 to float*
1928   %tmp7 = load float* %2, align 4
1929   %conv8 = fpext float %tmp7 to double
1930   %add.ptr.sum36 = add i64 %ivar, 8
1931   %tmp12 = getelementptr inbounds i8* %0, i64 %add.ptr.sum36
1932   %arrayidx = bitcast i8* %tmp12 to float*
1933   %tmp13 = load float* %arrayidx, align 4
1934   %conv14 = fpext float %tmp13 to double
1935   %tmp12.sum = add i64 %ivar, 12
1936   %arrayidx19 = getelementptr inbounds i8* %0, i64 %tmp12.sum
1937   %3 = bitcast i8* %arrayidx19 to float*
1938   %tmp20 = load float* %3, align 4
1939   %conv21 = fpext float %tmp20 to double
1940   %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)
1941   %ivar23 = load i64* @"OBJC_IVAR_$_A.myZ", align 8
1942   %add.ptr24 = getelementptr i8* %0, i64 %ivar23
1943   %4 = bitcast i8* %add.ptr24 to i128*
1944   %srcval = load i128* %4, align 4
1945   tail call void @objc_release(i8* %0) nounwind
1946   %tmp29 = trunc i128 %srcval to i64
1947   %tmp30 = bitcast i64 %tmp29 to <2 x float>
1948   %tmp31 = insertvalue {<2 x float>, <2 x float>} undef, <2 x float> %tmp30, 0
1949   %tmp32 = lshr i128 %srcval, 64
1950   %tmp33 = trunc i128 %tmp32 to i64
1951   %tmp34 = bitcast i64 %tmp33 to <2 x float>
1952   %tmp35 = insertvalue {<2 x float>, <2 x float>} %tmp31, <2 x float> %tmp34, 1
1953   ret {<2 x float>, <2 x float>} %tmp35
1954 }
1955
1956 ; CHECK: @"\01-[Top0 _getX]"
1957 ; CHECK-NOT: @objc_
1958 ; CHECK: }
1959
1960 define i32 @"\01-[Top0 _getX]"({}* %self, i8* nocapture %_cmd) nounwind {
1961 invoke.cont:
1962   %0 = bitcast {}* %self to i8*
1963   %1 = tail call i8* @objc_retain(i8* %0) nounwind
1964   %puts = tail call i32 @puts(i8* getelementptr inbounds ([16 x i8]* @str, i64 0, i64 0))
1965   tail call void @objc_release(i8* %0) nounwind
1966   ret i32 0
1967 }
1968
1969 @"\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"
1970 @"\01L_OBJC_IMAGE_INFO" = internal constant [2 x i32] [i32 0, i32 16], section "__DATA, __objc_imageinfo, regular, no_dead_strip"
1971 @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"
1972
1973 ; A simple loop. Eliminate the retain and release inside of it!
1974
1975 ; CHECK: define void @loop
1976 ; CHECK: for.body:
1977 ; CHECK-NOT: @objc_
1978 ; CHECK: @objc_msgSend
1979 ; CHECK-NOT: @objc_
1980 ; CHECK: for.end:
1981 define void @loop(i8* %x, i64 %n) {
1982 entry:
1983   %0 = tail call i8* @objc_retain(i8* %x) nounwind
1984   %cmp9 = icmp sgt i64 %n, 0
1985   br i1 %cmp9, label %for.body, label %for.end
1986
1987 for.body:                                         ; preds = %entry, %for.body
1988   %i.010 = phi i64 [ %inc, %for.body ], [ 0, %entry ]
1989   %1 = tail call i8* @objc_retain(i8* %x) nounwind
1990   %tmp5 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_", align 8
1991   %call = tail call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %1, i8* %tmp5)
1992   tail call void @objc_release(i8* %1) nounwind, !clang.imprecise_release !0
1993   %inc = add nsw i64 %i.010, 1
1994   %exitcond = icmp eq i64 %inc, %n
1995   br i1 %exitcond, label %for.end, label %for.body
1996
1997 for.end:                                          ; preds = %for.body, %entry
1998   tail call void @objc_release(i8* %x) nounwind, !clang.imprecise_release !0
1999   ret void
2000 }
2001
2002 ; ObjCARCOpt can delete the retain,release on self.
2003
2004 ; CHECK: define void @TextEditTest
2005 ; CHECK-NOT: call i8* @objc_retain(i8* %tmp7)
2006 ; CHECK: }
2007
2008 %0 = type { i8* (i8*, %struct._message_ref_t*, ...)*, i8* }
2009 %1 = type opaque
2010 %2 = type opaque
2011 %3 = type opaque
2012 %4 = type opaque
2013 %5 = type opaque
2014 %struct.NSConstantString = type { i32*, i32, i8*, i64 }
2015 %struct._NSRange = type { i64, i64 }
2016 %struct.__CFString = type opaque
2017 %struct.__method_list_t = type { i32, i32, [0 x %struct._objc_method] }
2018 %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* }
2019 %struct._class_t = type { %struct._class_t*, %struct._class_t*, %struct._objc_cache*, i8* (i8*, i8*)**, %struct._class_ro_t* }
2020 %struct._ivar_list_t = type { i32, i32, [0 x %struct._ivar_t] }
2021 %struct._ivar_t = type { i64*, i8*, i8*, i32, i32 }
2022 %struct._message_ref_t = type { i8*, i8* }
2023 %struct._objc_cache = type opaque
2024 %struct._objc_method = type { i8*, i8*, i8* }
2025 %struct._objc_protocol_list = type { i64, [0 x %struct._protocol_t*] }
2026 %struct._prop_list_t = type { i32, i32, [0 x %struct._message_ref_t] }
2027 %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 }
2028
2029 @"\01L_OBJC_CLASSLIST_REFERENCES_$_17" = external hidden global %struct._class_t*, section "__DATA, __objc_classrefs, regular, no_dead_strip", align 8
2030 @kUTTypePlainText = external constant %struct.__CFString*
2031 @"\01L_OBJC_SELECTOR_REFERENCES_19" = external hidden global i8*, section "__DATA, __objc_selrefs, literal_pointers, no_dead_strip"
2032 @"\01L_OBJC_SELECTOR_REFERENCES_21" = external hidden global i8*, section "__DATA, __objc_selrefs, literal_pointers, no_dead_strip"
2033 @"\01L_OBJC_SELECTOR_REFERENCES_23" = external hidden global i8*, section "__DATA, __objc_selrefs, literal_pointers, no_dead_strip"
2034 @"\01L_OBJC_SELECTOR_REFERENCES_25" = external hidden global i8*, section "__DATA, __objc_selrefs, literal_pointers, no_dead_strip"
2035 @"\01L_OBJC_CLASSLIST_REFERENCES_$_26" = external hidden global %struct._class_t*, section "__DATA, __objc_classrefs, regular, no_dead_strip", align 8
2036 @"\01L_OBJC_SELECTOR_REFERENCES_28" = external hidden global i8*, section "__DATA, __objc_selrefs, literal_pointers, no_dead_strip"
2037 @"\01L_OBJC_CLASSLIST_REFERENCES_$_29" = external hidden global %struct._class_t*, section "__DATA, __objc_classrefs, regular, no_dead_strip", align 8
2038 @"\01L_OBJC_SELECTOR_REFERENCES_31" = external hidden global i8*, section "__DATA, __objc_selrefs, literal_pointers, no_dead_strip"
2039 @"\01L_OBJC_SELECTOR_REFERENCES_33" = external hidden global i8*, section "__DATA, __objc_selrefs, literal_pointers, no_dead_strip"
2040 @"\01L_OBJC_SELECTOR_REFERENCES_35" = external hidden global i8*, section "__DATA, __objc_selrefs, literal_pointers, no_dead_strip"
2041 @"\01L_OBJC_SELECTOR_REFERENCES_37" = external hidden global i8*, section "__DATA, __objc_selrefs, literal_pointers, no_dead_strip"
2042 @"\01L_OBJC_CLASSLIST_REFERENCES_$_38" = external hidden global %struct._class_t*, section "__DATA, __objc_classrefs, regular, no_dead_strip", align 8
2043 @"\01L_OBJC_SELECTOR_REFERENCES_40" = external hidden global i8*, section "__DATA, __objc_selrefs, literal_pointers, no_dead_strip"
2044 @"\01L_OBJC_SELECTOR_REFERENCES_42" = external hidden global i8*, section "__DATA, __objc_selrefs, literal_pointers, no_dead_strip"
2045 @_unnamed_cfstring_44 = external hidden constant %struct.NSConstantString, section "__DATA,__cfstring"
2046 @"\01L_OBJC_SELECTOR_REFERENCES_46" = external hidden global i8*, section "__DATA, __objc_selrefs, literal_pointers, no_dead_strip"
2047 @"\01L_OBJC_SELECTOR_REFERENCES_48" = external hidden global i8*, section "__DATA, __objc_selrefs, literal_pointers, no_dead_strip"
2048 @"\01l_objc_msgSend_fixup_isEqual_" = external hidden global %0, section "__DATA, __objc_msgrefs, coalesced", align 16
2049 @"\01L_OBJC_CLASSLIST_REFERENCES_$_50" = external hidden global %struct._class_t*, section "__DATA, __objc_classrefs, regular, no_dead_strip", align 8
2050 @NSCocoaErrorDomain = external constant %1*
2051 @"\01L_OBJC_CLASSLIST_REFERENCES_$_51" = external hidden global %struct._class_t*, section "__DATA, __objc_classrefs, regular, no_dead_strip", align 8
2052 @NSFilePathErrorKey = external constant %1*
2053 @"\01L_OBJC_SELECTOR_REFERENCES_53" = external hidden global i8*, section "__DATA, __objc_selrefs, literal_pointers, no_dead_strip"
2054 @"\01L_OBJC_SELECTOR_REFERENCES_55" = external hidden global i8*, section "__DATA, __objc_selrefs, literal_pointers, no_dead_strip"
2055 @"\01L_OBJC_CLASSLIST_REFERENCES_$_56" = external hidden global %struct._class_t*, section "__DATA, __objc_classrefs, regular, no_dead_strip", align 8
2056 @"\01L_OBJC_SELECTOR_REFERENCES_58" = external hidden global i8*, section "__DATA, __objc_selrefs, literal_pointers, no_dead_strip"
2057 @"\01L_OBJC_SELECTOR_REFERENCES_60" = external hidden global i8*, section "__DATA, __objc_selrefs, literal_pointers, no_dead_strip"
2058
2059 declare %1* @truncatedString(%1*, i64)
2060 define void @TextEditTest(%2* %self, %3* %pboard) {
2061 entry:
2062   %err = alloca %4*, align 8
2063   %tmp7 = bitcast %2* %self to i8*
2064   %tmp8 = call i8* @objc_retain(i8* %tmp7) nounwind
2065   store %4* null, %4** %err, align 8
2066   %tmp1 = load %struct._class_t** @"\01L_OBJC_CLASSLIST_REFERENCES_$_17", align 8
2067   %tmp2 = load %struct.__CFString** @kUTTypePlainText, align 8
2068   %tmp3 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_19", align 8
2069   %tmp4 = bitcast %struct._class_t* %tmp1 to i8*
2070   %call5 = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %tmp4, i8* %tmp3, %struct.__CFString* %tmp2)
2071   %tmp5 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_21", align 8
2072   %tmp6 = bitcast %3* %pboard to i8*
2073   %call76 = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %tmp6, i8* %tmp5, i8* %call5)
2074   %tmp9 = call i8* @objc_retain(i8* %call76) nounwind
2075   %tobool = icmp eq i8* %tmp9, null
2076   br i1 %tobool, label %end, label %land.lhs.true
2077
2078 land.lhs.true:                                    ; preds = %entry
2079   %tmp11 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_23", align 8
2080   %call137 = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %tmp6, i8* %tmp11, i8* %tmp9)
2081   %tmp = bitcast i8* %call137 to %1*
2082   %tmp10 = call i8* @objc_retain(i8* %call137) nounwind
2083   call void @objc_release(i8* null) nounwind
2084   %tmp12 = call i8* @objc_retain(i8* %call137) nounwind
2085   call void @objc_release(i8* null) nounwind
2086   %tobool16 = icmp eq i8* %call137, null
2087   br i1 %tobool16, label %end, label %if.then
2088
2089 if.then:                                          ; preds = %land.lhs.true
2090   %tmp19 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_25", align 8
2091   %call21 = call signext i8 bitcast (i8* (i8*, i8*, ...)* @objc_msgSend to i8 (i8*, i8*)*)(i8* %call137, i8* %tmp19)
2092   %tobool22 = icmp eq i8 %call21, 0
2093   br i1 %tobool22, label %if.then44, label %land.lhs.true23
2094
2095 land.lhs.true23:                                  ; preds = %if.then
2096   %tmp24 = load %struct._class_t** @"\01L_OBJC_CLASSLIST_REFERENCES_$_26", align 8
2097   %tmp26 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_28", align 8
2098   %tmp27 = bitcast %struct._class_t* %tmp24 to i8*
2099   %call2822 = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %tmp27, i8* %tmp26, i8* %call137)
2100   %tmp13 = bitcast i8* %call2822 to %5*
2101   %tmp14 = call i8* @objc_retain(i8* %call2822) nounwind
2102   call void @objc_release(i8* null) nounwind
2103   %tobool30 = icmp eq i8* %call2822, null
2104   br i1 %tobool30, label %if.then44, label %if.end
2105
2106 if.end:                                           ; preds = %land.lhs.true23
2107   %tmp32 = load %struct._class_t** @"\01L_OBJC_CLASSLIST_REFERENCES_$_29", align 8
2108   %tmp33 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_31", align 8
2109   %tmp34 = bitcast %struct._class_t* %tmp32 to i8*
2110   %call35 = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %tmp34, i8* %tmp33)
2111   %tmp37 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_33", align 8
2112   %call3923 = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %call35, i8* %tmp37, i8* %call2822, i32 signext 1, %4** %err)
2113   %cmp = icmp eq i8* %call3923, null
2114   br i1 %cmp, label %if.then44, label %end
2115
2116 if.then44:                                        ; preds = %if.end, %land.lhs.true23, %if.then
2117   %url.025 = phi %5* [ %tmp13, %if.end ], [ %tmp13, %land.lhs.true23 ], [ null, %if.then ]
2118   %tmp49 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_35", align 8
2119   %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)
2120   %call513 = extractvalue %struct._NSRange %call51, 0
2121   %call514 = extractvalue %struct._NSRange %call51, 1
2122   %tmp52 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_37", align 8
2123   %call548 = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %call137, i8* %tmp52, i64 %call513, i64 %call514)
2124   %tmp55 = load %struct._class_t** @"\01L_OBJC_CLASSLIST_REFERENCES_$_38", align 8
2125   %tmp56 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_40", align 8
2126   %tmp57 = bitcast %struct._class_t* %tmp55 to i8*
2127   %call58 = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %tmp57, i8* %tmp56)
2128   %tmp59 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_42", align 8
2129   %call6110 = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %call548, i8* %tmp59, i8* %call58)
2130   %tmp15 = call i8* @objc_retain(i8* %call6110) nounwind
2131   call void @objc_release(i8* %call137) nounwind
2132   %tmp64 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_46", align 8
2133   %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*))
2134   %tobool67 = icmp eq i8 %call66, 0
2135   br i1 %tobool67, label %if.end74, label %if.then68
2136
2137 if.then68:                                        ; preds = %if.then44
2138   %tmp70 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_48", align 8
2139   %call7220 = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %call6110, i8* %tmp70)
2140   %tmp16 = call i8* @objc_retain(i8* %call7220) nounwind
2141   call void @objc_release(i8* %call6110) nounwind
2142   br label %if.end74
2143
2144 if.end74:                                         ; preds = %if.then68, %if.then44
2145   %filename.0.in = phi i8* [ %call7220, %if.then68 ], [ %call6110, %if.then44 ]
2146   %filename.0 = bitcast i8* %filename.0.in to %1*
2147   %tmp17 = load i8** bitcast (%0* @"\01l_objc_msgSend_fixup_isEqual_" to i8**), align 16
2148   %tmp18 = bitcast i8* %tmp17 to i8 (i8*, %struct._message_ref_t*, i8*, ...)*
2149   %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)
2150   %tobool79 = icmp eq i8 %call78, 0
2151   br i1 %tobool79, label %land.lhs.true80, label %if.then109
2152
2153 land.lhs.true80:                                  ; preds = %if.end74
2154   %tmp82 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_25", align 8
2155   %call84 = call signext i8 bitcast (i8* (i8*, i8*, ...)* @objc_msgSend to i8 (i8*, i8*)*)(i8* %filename.0.in, i8* %tmp82)
2156   %tobool86 = icmp eq i8 %call84, 0
2157   br i1 %tobool86, label %if.then109, label %if.end106
2158
2159 if.end106:                                        ; preds = %land.lhs.true80
2160   %tmp88 = load %struct._class_t** @"\01L_OBJC_CLASSLIST_REFERENCES_$_26", align 8
2161   %tmp90 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_28", align 8
2162   %tmp91 = bitcast %struct._class_t* %tmp88 to i8*
2163   %call9218 = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %tmp91, i8* %tmp90, i8* %filename.0.in)
2164   %tmp20 = bitcast i8* %call9218 to %5*
2165   %tmp21 = call i8* @objc_retain(i8* %call9218) nounwind
2166   %tmp22 = bitcast %5* %url.025 to i8*
2167   call void @objc_release(i8* %tmp22) nounwind
2168   %tmp94 = load %struct._class_t** @"\01L_OBJC_CLASSLIST_REFERENCES_$_29", align 8
2169   %tmp95 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_31", align 8
2170   %tmp96 = bitcast %struct._class_t* %tmp94 to i8*
2171   %call97 = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %tmp96, i8* %tmp95)
2172   %tmp99 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_33", align 8
2173   %call10119 = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %call97, i8* %tmp99, i8* %call9218, i32 signext 1, %4** %err)
2174   %phitmp = icmp eq i8* %call10119, null
2175   br i1 %phitmp, label %if.then109, label %end
2176
2177 if.then109:                                       ; preds = %if.end106, %land.lhs.true80, %if.end74
2178   %url.129 = phi %5* [ %tmp20, %if.end106 ], [ %url.025, %if.end74 ], [ %url.025, %land.lhs.true80 ]
2179   %tmp110 = load %4** %err, align 8
2180   %tobool111 = icmp eq %4* %tmp110, null
2181   br i1 %tobool111, label %if.then112, label %if.end125
2182
2183 if.then112:                                       ; preds = %if.then109
2184   %tmp113 = load %struct._class_t** @"\01L_OBJC_CLASSLIST_REFERENCES_$_50", align 8
2185   %tmp114 = load %1** @NSCocoaErrorDomain, align 8
2186   %tmp115 = load %struct._class_t** @"\01L_OBJC_CLASSLIST_REFERENCES_$_51", align 8
2187   %call117 = call %1* @truncatedString(%1* %filename.0, i64 1034)
2188   %tmp118 = load %1** @NSFilePathErrorKey, align 8
2189   %tmp119 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_53", align 8
2190   %tmp120 = bitcast %struct._class_t* %tmp115 to i8*
2191   %call12113 = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %tmp120, i8* %tmp119, %1* %call117, %1* %tmp118, i8* null)
2192   %tmp122 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_55", align 8
2193   %tmp123 = bitcast %struct._class_t* %tmp113 to i8*
2194   %call12414 = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %tmp123, i8* %tmp122, %1* %tmp114, i64 258, i8* %call12113)
2195   %tmp23 = call i8* @objc_retain(i8* %call12414) nounwind
2196   %tmp25 = call i8* @objc_autorelease(i8* %tmp23) nounwind
2197   %tmp28 = bitcast i8* %tmp25 to %4*
2198   store %4* %tmp28, %4** %err, align 8
2199   br label %if.end125
2200
2201 if.end125:                                        ; preds = %if.then112, %if.then109
2202   %tmp127 = phi %4* [ %tmp110, %if.then109 ], [ %tmp28, %if.then112 ]
2203   %tmp126 = load %struct._class_t** @"\01L_OBJC_CLASSLIST_REFERENCES_$_56", align 8
2204   %tmp128 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_58", align 8
2205   %tmp129 = bitcast %struct._class_t* %tmp126 to i8*
2206   %call13015 = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %tmp129, i8* %tmp128, %4* %tmp127)
2207   %tmp131 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_60", align 8
2208   %call13317 = call i8* (i8*, i8*, ...)* @objc_msgSend(i8* %call13015, i8* %tmp131)
2209   br label %end
2210
2211 end:                                              ; preds = %if.end125, %if.end106, %if.end, %land.lhs.true, %entry
2212   %filename.2 = phi %1* [ %filename.0, %if.end106 ], [ %filename.0, %if.end125 ], [ %tmp, %land.lhs.true ], [ null, %entry ], [ %tmp, %if.end ]
2213   %origFilename.0 = phi %1* [ %tmp, %if.end106 ], [ %tmp, %if.end125 ], [ %tmp, %land.lhs.true ], [ null, %entry ], [ %tmp, %if.end ]
2214   %url.2 = phi %5* [ %tmp20, %if.end106 ], [ %url.129, %if.end125 ], [ null, %land.lhs.true ], [ null, %entry ], [ %tmp13, %if.end ]
2215   call void @objc_release(i8* %tmp9) nounwind, !clang.imprecise_release !0
2216   %tmp29 = bitcast %5* %url.2 to i8*
2217   call void @objc_release(i8* %tmp29) nounwind, !clang.imprecise_release !0
2218   %tmp30 = bitcast %1* %origFilename.0 to i8*
2219   call void @objc_release(i8* %tmp30) nounwind, !clang.imprecise_release !0
2220   %tmp31 = bitcast %1* %filename.2 to i8*
2221   call void @objc_release(i8* %tmp31) nounwind, !clang.imprecise_release !0
2222   call void @objc_release(i8* %tmp7) nounwind, !clang.imprecise_release !0
2223   ret void
2224 }
2225
2226 !0 = metadata !{}
2227
2228 declare i32 @__gxx_personality_v0(...)
2229
2230 ; CHECK: attributes #0 = { nounwind readnone }
2231 ; CHECK: attributes [[NUW]] = { nounwind }