1// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -emit-llvm -fblocks -fobjc-arc -fobjc-runtime-has-weak -o - %s | FileCheck %s
2// rdar://10849570
3
4@interface NSObject @end
5
6@interface SomeClass : NSObject
7- (id) init;
8@end
9
10@implementation SomeClass
11- (void)foo {
12}
13- (id) init {
14    return 0;
15}
16+ alloc { return 0; }
17@end
18
19int main (int argc, const char * argv[]) {
20    @autoreleasepool {
21        SomeClass *objPtr1 = [[SomeClass alloc] init];
22        __weak SomeClass *weakRef = objPtr1;
23
24        [weakRef foo];
25
26        objPtr1 = (void *)0;
27        return 0;
28    }
29}
30
31// CHECK: [[SIXTEEN:%.*]]  = call i8* @objc_loadWeakRetained(i8** {{%.*}})
32// CHECK-NEXT:  [[SEVENTEEN:%.*]] = bitcast i8* [[SIXTEEN]] to {{%.*}}
33// CHECK-NEXT:  [[EIGHTEEN:%.*]] = load i8*, i8** @OBJC_SELECTOR_REFERENCES_6
34// CHECK-NEXT:  [[NINETEEN:%.*]] = bitcast %0* [[SEVENTEEN]] to i8*
35// CHECK-NEXT:  call void bitcast (i8* (i8*, i8*, ...)* @objc_msgSend
36// CHECK-NEXT:  [[TWENTY:%.*]] = bitcast %0* [[SEVENTEEN]] to i8*
37// CHECK-NEXT:  call void @objc_release(i8* [[TWENTY]])
38
39void test1(int cond) {
40  extern void test34_sink(id *);
41  __weak id weak;
42  test34_sink(cond ? &weak : 0);
43}
44
45// CHECK-LABEL: define void @test1(
46// CHECK: [[CONDADDR:%.*]] = alloca i32
47// CHECK-NEXT: [[WEAK:%.*]] = alloca i8*
48// CHECK-NEXT: [[INCRTEMP:%.*]] = alloca i8*
49// CHECK-NEXT: [[CONDCLEANUPSAVE:%.*]] = alloca i8*
50// CHECK-NEXT: [[CONDCLEANUP:%.*]] = alloca i1
51// CHECK-NEXT: store i32
52// CHECK-NEXT: store i8* null, i8** [[WEAK]]
53// CHECK:  [[COND1:%.*]] = phi i8**
54// CHECK-NEXT: [[ICRISNULL:%.*]] = icmp eq i8** [[COND1]], null
55// CHECK-NEXT: [[ICRARGUMENT:%.*]] = select i1 [[ICRISNULL]], i8** null, i8** [[INCRTEMP]]
56// CHECK-NEXT: store i1 false, i1* [[CONDCLEANUP]]
57// CHECK-NEXT: br i1 [[ICRISNULL]], label [[ICRCONT:%.*]], label [[ICRCOPY:%.*]]
58// CHECK:  [[ONE:%.*]] = call i8* @objc_loadWeakRetained(
59// CHECK-NEXT: store i8* [[ONE]], i8** [[CONDCLEANUPSAVE]]
60// CHECK-NEXT: store i1 true, i1* [[CONDCLEANUP]]
61// CHECK-NEXT: store i8* [[ONE]], i8** [[INCRTEMP]]
62// CHECK-NEXT: br label
63
64// CHECK: call void @test34_sink(
65// CHECK-NEXT: [[ICRISNULL1:%.*]] = icmp eq i8** [[COND1]], null
66// CHECK-NEXT: br i1 [[ICRISNULL1]], label [[ICRDONE:%.*]], label [[ICRWRITEBACK:%.*]]
67// CHECK:  [[TWO:%.*]] = load i8*, i8** [[INCRTEMP]]
68// CHECK-NEXT:  [[THREE:%.*]] = call i8* @objc_storeWeak(
69// CHECK-NEXT  br label [[ICRDONE]]
70// CHECK:  [[CLEANUPISACTIVE:%.*]] = load i1, i1* [[CONDCLEANUP]]
71// CHECK-NEXT:  br i1 [[CLEANUPISACTIVE]], label [[CLEASNUPACTION:%.*]], label [[CLEANUPDONE:%.*]]
72
73// CHECK: [[FOUR:%.*]] = load i8*, i8** [[CONDCLEANUPSAVE]]
74// CHECK-NEXT: call void @objc_release(i8* [[FOUR]])
75// CHECK-NEXT:  br label
76// CHECK:  call void @objc_destroyWeak(i8** [[WEAK]])
77// CHECK-NEXT: ret void
78