1// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fobjc-arc -emit-llvm %s -o - | FileCheck %s
2
3// rdar://problem/10290317
4@interface Test0
5- (void) setValue: (id) x;
6@end
7void test0(Test0 *t0, id value) {
8  t0.value = value;
9}
10// CHECK-LABEL: define void @test0(
11// CHECK: call void @objc_storeStrong
12// CHECK: call void @objc_storeStrong
13// CHECK: @objc_msgSend
14// CHECK: call void @objc_storeStrong(
15// CHECK: call void @objc_storeStrong(
16
17struct S1 { Class isa; };
18@interface Test1
19@property (nonatomic, strong) __attribute__((NSObject)) struct S1 *pointer;
20@end
21@implementation Test1
22@synthesize pointer;
23@end
24//   The getter should be a simple load.
25// CHECK:    define internal [[S1:%.*]]* @"\01-[Test1 pointer]"(
26// CHECK:      [[OFFSET:%.*]] = load i64, i64* @"OBJC_IVAR_$_Test1.pointer"
27// CHECK-NEXT: [[T0:%.*]] = bitcast [[TEST1:%.*]]* {{%.*}} to i8*
28// CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds i8, i8* [[T0]], i64 [[OFFSET]]
29// CHECK-NEXT: [[T2:%.*]] = bitcast i8* [[T1]] to [[S1]]**
30// CHECK-NEXT: [[T3:%.*]] = load [[S1]]*, [[S1]]** [[T2]], align 8
31// CHECK-NEXT: ret [[S1]]* [[T3]]
32
33//   The setter should be using objc_setProperty.
34// CHECK:    define internal void @"\01-[Test1 setPointer:]"(
35// CHECK:      [[T0:%.*]] = bitcast [[TEST1]]* {{%.*}} to i8*
36// CHECK-NEXT: [[OFFSET:%.*]] = load i64, i64* @"OBJC_IVAR_$_Test1.pointer"
37// CHECK-NEXT: [[T1:%.*]] = load [[S1]]*, [[S1]]** {{%.*}}
38// CHECK-NEXT: [[T2:%.*]] = bitcast [[S1]]* [[T1]] to i8*
39// CHECK-NEXT: call void @objc_setProperty(i8* [[T0]], i8* {{%.*}}, i64 [[OFFSET]], i8* [[T2]], i1 zeroext false, i1 zeroext false)
40// CHECK-NEXT: ret void
41
42
43// rdar://problem/12039404
44@interface Test2 {
45@private
46  Class _theClass;
47}
48@property (copy) Class theClass;
49@end
50
51static Class theGlobalClass;
52@implementation Test2
53@synthesize theClass = _theClass;
54- (void) test {
55  _theClass = theGlobalClass;
56}
57@end
58// CHECK:    define internal void @"\01-[Test2 test]"(
59// CHECK:      [[T0:%.*]] = load i8*, i8** @theGlobalClass, align 8
60// CHECK-NEXT: [[T1:%.*]] = load [[TEST2:%.*]]*, [[TEST2:%.*]]**
61// CHECK-NEXT: [[OFFSET:%.*]] = load i64, i64* @"OBJC_IVAR_$_Test2._theClass"
62// CHECK-NEXT: [[T2:%.*]] = bitcast [[TEST2]]* [[T1]] to i8*
63// CHECK-NEXT: [[T3:%.*]] = getelementptr inbounds i8, i8* [[T2]], i64 [[OFFSET]]
64// CHECK-NEXT: [[T4:%.*]] = bitcast i8* [[T3]] to i8**
65// CHECK-NEXT: call void @objc_storeStrong(i8** [[T4]], i8* [[T0]]) [[NUW:#[0-9]+]]
66// CHECK-NEXT: ret void
67
68// CHECK:    define internal i8* @"\01-[Test2 theClass]"(
69// CHECK:      [[OFFSET:%.*]] = load i64, i64* @"OBJC_IVAR_$_Test2._theClass"
70// CHECK-NEXT: [[T0:%.*]] = tail call i8* @objc_getProperty(i8* {{.*}}, i8* {{.*}}, i64 [[OFFSET]], i1 zeroext true)
71// CHECK-NEXT: ret i8* [[T0]]
72
73// CHECK:    define internal void @"\01-[Test2 setTheClass:]"(
74// CHECK:      [[T0:%.*]] = bitcast [[TEST2]]* {{%.*}} to i8*
75// CHECK-NEXT: [[OFFSET:%.*]] = load i64, i64* @"OBJC_IVAR_$_Test2._theClass"
76// CHECK-NEXT: [[T1:%.*]] = load i8*, i8** {{%.*}}
77// CHECK-NEXT: call void @objc_setProperty(i8* [[T0]], i8* {{%.*}}, i64 [[OFFSET]], i8* [[T1]], i1 zeroext true, i1 zeroext true)
78// CHECK-NEXT: ret void
79
80// CHECK:    define internal void @"\01-[Test2 .cxx_destruct]"(
81// CHECK:      [[T0:%.*]] = load [[TEST2]]*, [[TEST2]]**
82// CHECK-NEXT: [[OFFSET:%.*]] = load i64, i64* @"OBJC_IVAR_$_Test2._theClass"
83// CHECK-NEXT: [[T1:%.*]] = bitcast [[TEST2]]* [[T0]] to i8*
84// CHECK-NEXT: [[T2:%.*]] = getelementptr inbounds i8, i8* [[T1]], i64 [[OFFSET]]
85// CHECK-NEXT: [[T3:%.*]] = bitcast i8* [[T2]] to i8**
86// CHECK-NEXT: call void @objc_storeStrong(i8** [[T3]], i8* null) [[NUW]]
87// CHECK-NEXT: ret void
88
89// rdar://13115896
90@interface Test3
91@property id copyMachine;
92@end
93
94void test3(Test3 *t) {
95  id x = t.copyMachine;
96  x = [t copyMachine];
97}
98// CHECK:    define void @test3([[TEST3:%.*]]*
99//   Prologue.
100// CHECK:      [[T:%.*]] = alloca [[TEST3]]*,
101// CHECK-NEXT: [[X:%.*]] = alloca i8*,
102//   Property access.
103// CHECK:      [[T0:%.*]] = load [[TEST3]]*, [[TEST3]]** [[T]],
104// CHECK-NEXT: [[SEL:%.*]] = load i8*, i8** @OBJC_SELECTOR_REFERENCES
105// CHECK-NEXT: [[T1:%.*]] = bitcast [[TEST3]]* [[T0]] to i8*
106// CHECK-NEXT: [[T2:%.*]] = call i8* bitcast ({{.*}} @objc_msgSend to {{.*}})(i8* [[T1]], i8* [[SEL]])
107// CHECK-NEXT: store i8* [[T2]], i8** [[X]],
108//   Message send.
109// CHECK-NEXT: [[T0:%.*]] = load [[TEST3]]*, [[TEST3]]** [[T]],
110// CHECK-NEXT: [[SEL:%.*]] = load i8*, i8** @OBJC_SELECTOR_REFERENCES
111// CHECK-NEXT: [[T1:%.*]] = bitcast [[TEST3]]* [[T0]] to i8*
112// CHECK-NEXT: [[T2:%.*]] = call i8* bitcast ({{.*}} @objc_msgSend to {{.*}})(i8* [[T1]], i8* [[SEL]])
113// CHECK-NEXT: [[T3:%.*]] = load i8*, i8** [[X]],
114// CHECK-NEXT: store i8* [[T2]], i8** [[X]],
115// CHECK-NEXT: call void @objc_release(i8* [[T3]])
116//   Epilogue.
117// CHECK-NEXT: call void @objc_storeStrong(i8** [[X]], i8* null)
118// CHECK-NEXT: [[T0:%.*]] = bitcast [[TEST3]]** [[T]] to i8**
119// CHECK-NEXT: call void @objc_storeStrong(i8** [[T0]], i8* null)
120// CHECK-NEXT: ret void
121
122@implementation Test3
123- (id) copyMachine {
124  extern id test3_helper(void);
125  return test3_helper();
126}
127// CHECK:    define internal i8* @"\01-[Test3 copyMachine]"(
128// CHECK:      [[T0:%.*]] = call i8* @test3_helper()
129// CHECK-NEXT: [[T1:%.*]] = call i8* @objc_retainAutoreleasedReturnValue(i8* [[T0]])
130// CHECK-NEXT: ret i8* [[T1]]
131- (void) setCopyMachine: (id) x {}
132@end
133
134// CHECK: attributes [[NUW]] = { nounwind }
135