• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -emit-llvm -fobjc-nonfragile-abi -fobjc-runtime-has-weak -fblocks -fobjc-arc -O2 -disable-llvm-optzns -o - %s | FileCheck %s
2
3// rdar://problem/9315552
4// The analogous ObjC testcase test46 in arr.m.
5void test0(__weak id *wp, __weak volatile id *wvp) {
6  extern id test0_helper(void);
7
8  // TODO: this is sub-optimal, we should retain at the actual call site.
9  // TODO: in the non-volatile case, we do not need to be reloading.
10
11  // CHECK:      [[T0:%.*]] = call i8* @_Z12test0_helperv()
12  // CHECK-NEXT: [[T1:%.*]] = call i8* @objc_retainAutoreleasedReturnValue(i8* [[T0]])
13  // CHECK-NEXT: [[T2:%.*]] = load i8*** {{%.*}}, align 8
14  // CHECK-NEXT: [[T3:%.*]] = call i8* @objc_storeWeak(i8** [[T2]], i8* [[T1]])
15  // CHECK-NEXT: [[T4:%.*]] = call i8* @objc_retain(i8* [[T3]])
16  // CHECK-NEXT: store i8* [[T4]], i8**
17  // CHECK-NEXT: call void @objc_release(i8* [[T1]])
18  id x = *wp = test0_helper();
19
20  // CHECK:      [[T0:%.*]] = call i8* @_Z12test0_helperv()
21  // CHECK-NEXT: [[T1:%.*]] = call i8* @objc_retainAutoreleasedReturnValue(i8* [[T0]])
22  // CHECK-NEXT: [[T2:%.*]] = load i8*** {{%.*}}, align 8
23  // CHECK-NEXT: [[T3:%.*]] = call i8* @objc_storeWeak(i8** [[T2]], i8* [[T1]])
24  // CHECK-NEXT: [[T4:%.*]] = call i8* @objc_loadWeakRetained(i8** [[T2]])
25  // CHECK-NEXT: store i8* [[T4]], i8**
26  // CHECK-NEXT: call void @objc_release(i8* [[T1]])
27  id y = *wvp = test0_helper();
28}
29
30// rdar://problem/9320648
31struct Test1_helper { Test1_helper(); };
32@interface Test1 @end
33@implementation Test1 { Test1_helper x; } @end
34// CHECK: define internal i8* @"\01-[Test1 .cxx_construct]"(
35// CHECK:      call void @_ZN12Test1_helperC1Ev(
36// CHECK-NEXT: load
37// CHECK-NEXT: bitcast
38// CHECK-NEXT: ret i8*
39
40void test34(int cond) {
41  __strong id strong;
42  __weak id weak;
43  extern void test34_sink(id *);
44  test34_sink(cond ? &strong : 0);
45  test34_sink(cond ? &weak : 0);
46
47  // CHECK:    define void @_Z6test34i(
48  // CHECK:      [[COND:%.*]] = alloca i32
49  // CHECK-NEXT: [[STRONG:%.*]] = alloca i8*
50  // CHECK-NEXT: [[WEAK:%.*]] = alloca i8*
51  // CHECK-NEXT: [[TEMP1:%.*]] = alloca i8*
52  // CHECK-NEXT: [[TEMP2:%.*]] = alloca i8*
53  // CHECK-NEXT: store i32
54  // CHECK-NEXT: store i8* null, i8** [[STRONG]]
55  // CHECK-NEXT: call i8* @objc_initWeak(i8** [[WEAK]], i8* null)
56
57  // CHECK-NEXT: [[T0:%.*]] = load i32* [[COND]]
58  // CHECK-NEXT: [[T1:%.*]] = icmp ne i32 [[T0]], 0
59  // CHECK:      [[ARG:%.*]] = phi i8**
60  // CHECK-NEXT: [[T0:%.*]] = icmp eq i8** [[ARG]], null
61  // CHECK-NEXT: [[T1:%.*]] = select i1 [[T0]], i8** null, i8** [[TEMP1]]
62  // CHECK-NEXT: br i1 [[T0]],
63  // CHECK:      [[T0:%.*]] = load i8** [[ARG]]
64  // CHECK-NEXT: store i8* [[T0]], i8** [[TEMP1]]
65  // CHECK-NEXT: br label
66  // CHECK:      call void @_Z11test34_sinkPU15__autoreleasingP11objc_object(i8** [[T1]])
67  // CHECK-NEXT: [[T0:%.*]] = icmp eq i8** [[ARG]], null
68  // CHECK-NEXT: br i1 [[T0]],
69  // CHECK:      [[T0:%.*]] = load i8** [[TEMP1]]
70  // CHECK-NEXT: [[T1:%.*]] = call i8* @objc_retain(i8* [[T0]])
71  // CHECK-NEXT: [[T2:%.*]] = load i8** [[ARG]]
72  // CHECK-NEXT: store i8* [[T1]], i8** [[ARG]]
73  // CHECK-NEXT: call void @objc_release(i8* [[T2]])
74  // CHECK-NEXT: br label
75
76  // CHECK:      [[T0:%.*]] = load i32* [[COND]]
77  // CHECK-NEXT: [[T1:%.*]] = icmp ne i32 [[T0]], 0
78  // CHECK:      [[ARG:%.*]] = phi i8**
79  // CHECK-NEXT: [[T0:%.*]] = icmp eq i8** [[ARG]], null
80  // CHECK-NEXT: [[T1:%.*]] = select i1 [[T0]], i8** null, i8** [[TEMP2]]
81  // CHECK-NEXT: br i1 [[T0]],
82  // CHECK:      [[T0:%.*]] = call i8* @objc_loadWeak(i8** [[ARG]])
83  // CHECK-NEXT: store i8* [[T0]], i8** [[TEMP2]]
84  // CHECK-NEXT: br label
85  // CHECK:      call void @_Z11test34_sinkPU15__autoreleasingP11objc_object(i8** [[T1]])
86  // CHECK-NEXT: [[T0:%.*]] = icmp eq i8** [[ARG]], null
87  // CHECK-NEXT: br i1 [[T0]],
88  // CHECK:      [[T0:%.*]] = load i8** [[TEMP2]]
89  // CHECK-NEXT: call i8* @objc_storeWeak(i8** [[ARG]], i8* [[T0]])
90  // CHECK-NEXT: br label
91
92  // CHECK:      call void @objc_destroyWeak(i8** [[WEAK]])
93  // CHECK:      ret void
94}
95
96struct Test35_Helper {
97  static id makeObject1() __attribute__((ns_returns_retained));
98  id makeObject2() __attribute__((ns_returns_retained));
99  static id makeObject3();
100  id makeObject4();
101};
102
103// CHECK: define void @_Z6test3513Test35_HelperPS_
104void test35(Test35_Helper x0, Test35_Helper *x0p) {
105  // CHECK: call i8* @_ZN13Test35_Helper11makeObject1Ev
106  // CHECK-NOT: call i8* @objc_retain
107  id obj1 = Test35_Helper::makeObject1();
108  // CHECK: call i8* @_ZN13Test35_Helper11makeObject2Ev
109  // CHECK-NOT: call i8* @objc_retain
110  id obj2 = x0.makeObject2();
111  // CHECK: call i8* @_ZN13Test35_Helper11makeObject2Ev
112  // CHECK-NOT: call i8* @objc_retain
113  id obj3 = x0p->makeObject2();
114  id (Test35_Helper::*pmf)() __attribute__((ns_returns_retained))
115    = &Test35_Helper::makeObject2;
116  // CHECK: call i8* %
117  // CHECK-NOT: call i8* @objc_retain
118  id obj4 = (x0.*pmf)();
119  // CHECK: call i8* %
120  // CHECK-NOT: call i8* @objc_retain
121  id obj5 = (x0p->*pmf)();
122
123  // CHECK: call void @objc_release
124  // CHECK: call void @objc_release
125  // CHECK: call void @objc_release
126  // CHECK: call void @objc_release
127  // CHECK: call void @objc_release
128  // CHECK-NEXT: ret void
129}
130
131// CHECK: define void @_Z7test35b13Test35_HelperPS_
132void test35b(Test35_Helper x0, Test35_Helper *x0p) {
133  // CHECK: call i8* @_ZN13Test35_Helper11makeObject3Ev
134  // CHECK: call i8* @objc_retain
135  id obj1 = Test35_Helper::makeObject3();
136  // CHECK: call i8* @_ZN13Test35_Helper11makeObject4Ev
137  // CHECK: call i8* @objc_retain
138  id obj2 = x0.makeObject4();
139  // CHECK: call i8* @_ZN13Test35_Helper11makeObject4Ev
140  // CHECK: call i8* @objc_retain
141  id obj3 = x0p->makeObject4();
142  id (Test35_Helper::*pmf)() = &Test35_Helper::makeObject4;
143  // CHECK: call i8* %
144  // CHECK: call i8* @objc_retain
145  id obj4 = (x0.*pmf)();
146  // CHECK: call i8* %
147  // CHECK: call i8* @objc_retain
148  id obj5 = (x0p->*pmf)();
149
150  // CHECK: call void @objc_release
151  // CHECK: call void @objc_release
152  // CHECK: call void @objc_release
153  // CHECK: call void @objc_release
154  // CHECK: call void @objc_release
155  // CHECK-NEXT: ret void
156}
157
158// rdar://problem/9603128
159// CHECK: define i8* @_Z6test36P11objc_object(
160id test36(id z) {
161  // CHECK: objc_retain
162  // CHECK: objc_retain
163  // CHECK: objc_release
164  // CHECK: objc_autoreleaseReturnValue
165  return z;
166}
167