Home | History | Annotate | Download | only in CodeGenObjC
      1 // RUN: %clang_cc1 -fblocks -fobjc-arc -fobjc-runtime-has-weak -triple x86_64-apple-darwin -O0 -emit-llvm %s -o %t-64.s
      2 // RUN: FileCheck -check-prefix LP64 --input-file=%t-64.s %s
      3 // rdar://9503326
      4 // rdar://9606600
      5 
      6 extern void use(id);
      7 extern void use_block(void (^)(void));
      8 
      9 struct NSFastEnumerationState;
     10 @interface NSArray
     11 - (unsigned long) countByEnumeratingWithState: (struct NSFastEnumerationState*) state
     12                   objects: (id*) buffer
     13                   count: (unsigned long) bufferSize;
     14 @end;
     15 
     16 void test0(NSArray *array) {
     17   // 'x' should be initialized without a retain.
     18   // We should actually do a non-constant capture, and that
     19   // capture should require a retain.
     20   for (id x in array) {
     21     use_block(^{ use(x); });
     22   }
     23 }
     24 
     25 // CHECK-LP64:    define void @test0(
     26 // CHECK-LP64:      [[ARRAY:%.*]] = alloca [[ARRAY_T:%.*]]*,
     27 // CHECK-LP64-NEXT: [[X:%.*]] = alloca i8*,
     28 // CHECK-LP64-NEXT: [[STATE:%.*]] = alloca [[STATE_T:%.*]],
     29 // CHECK-LP64-NEXT: [[BUFFER:%.*]] = alloca [16 x i8*], align 8
     30 // CHECK-LP64-NEXT: [[BLOCK:%.*]] = alloca [[BLOCK_T:<{.*}>]],
     31 
     32 // Initialize 'array'.
     33 // CHECK-LP64-NEXT: store [[ARRAY_T]]* null, [[ARRAY_T]]** [[ARRAY]]
     34 // CHECK-LP64-NEXT: [[ZERO:%.*]] = bitcast [[ARRAY_T]]** [[ARRAY]] to i8**
     35 // CHECK-LP64-NEXT: [[ONE:%.*]] = bitcast [[ARRAY_T]]* {{%.*}} to i8*
     36 // CHECK-LP64-NEXT: call void @objc_storeStrong(i8** [[ZERO]], i8* [[ONE]]) [[NUW:#[0-9]+]]
     37 
     38 // Initialize the fast enumaration state.
     39 // CHECK-LP64-NEXT: [[T0:%.*]] = bitcast [[STATE_T]]* [[STATE]] to i8*
     40 // CHECK-LP64-NEXT: call void @llvm.memset.p0i8.i64(i8* [[T0]], i8 0, i64 64, i32 8, i1 false)
     41 
     42 // Evaluate the collection expression and retain.
     43 // CHECK-LP64-NEXT: [[T0:%.*]] = load [[ARRAY_T]]** [[ARRAY]], align 8
     44 // CHECK-LP64-NEXT: [[T1:%.*]] = bitcast [[ARRAY_T]]* [[T0]] to i8*
     45 // CHECK-LP64-NEXT: [[T2:%.*]] = call i8* @objc_retain(i8* [[T1]])
     46 // CHECK-LP64-NEXT: [[SAVED_ARRAY:%.*]] = bitcast i8* [[T2]] to [[ARRAY_T]]*
     47 
     48 // Call the enumeration method.
     49 // CHECK-LP64-NEXT: [[T0:%.*]] = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_
     50 // CHECK-LP64-NEXT: [[T1:%.*]] = bitcast [[ARRAY_T]]* [[SAVED_ARRAY]] to i8*
     51 // CHECK-LP64-NEXT: [[SIZE:%.*]] = call i64 bitcast (i8* (i8*, i8*, ...)* @objc_msgSend to i64 (i8*, i8*, [[STATE_T]]*, [16 x i8*]*, i64)*)(i8* [[T1]], i8* [[T0]], [[STATE_T]]* [[STATE]], [16 x i8*]* [[BUFFER]], i64 16)
     52 
     53 // Check for a nonzero result.
     54 // CHECK-LP64-NEXT: [[T0:%.*]] = icmp eq i64 [[SIZE]], 0
     55 // CHECK-LP64-NEXT: br i1 [[T0]]
     56 
     57 // CHECK-LP64:      [[T0:%.*]] = getelementptr inbounds [[STATE_T]]* [[STATE]], i32 0, i32 1
     58 // CHECK-LP64-NEXT: [[T1:%.*]] = load i8*** [[T0]]
     59 // CHECK-LP64-NEXT: [[T2:%.*]] = getelementptr i8** [[T1]], i64
     60 // CHECK-LP64-NEXT: [[T3:%.*]] = load i8** [[T2]]
     61 // CHECK-LP64-NEXT: store i8* [[T3]], i8** [[X]]
     62 
     63 // CHECK-LP64:      [[D0:%.*]] = getelementptr inbounds [[BLOCK_T]]* [[BLOCK]], i32 0, i32 5
     64 // CHECK-LP64:      [[T0:%.*]] = getelementptr inbounds [[BLOCK_T]]* [[BLOCK]], i32 0, i32 5
     65 // CHECK-LP64-NEXT: [[T1:%.*]] = load i8** [[X]]
     66 // CHECK-LP64-NEXT: [[T2:%.*]] = call i8* @objc_retain(i8* [[T1]])
     67 // CHECK-LP64-NEXT: store i8* [[T2]], i8** [[T0]]
     68 // CHECK-LP64-NEXT: [[T1:%.*]] = bitcast [[BLOCK_T]]* [[BLOCK]] 
     69 // CHECK-LP64: call void @use_block(
     70 // CHECK-LP64-NEXT: call void @objc_storeStrong(i8** [[D0]], i8* null)
     71 
     72 // CHECK-LP64:      [[T0:%.*]] = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_
     73 // CHECK-LP64-NEXT: [[T1:%.*]] = bitcast [[ARRAY_T]]* [[SAVED_ARRAY]] to i8*
     74 // CHECK-LP64-NEXT: [[SIZE:%.*]] = call i64 bitcast (i8* (i8*, i8*, ...)* @objc_msgSend to i64 (i8*, i8*, [[STATE_T]]*, [16 x i8*]*, i64)*)(i8* [[T1]], i8* [[T0]], [[STATE_T]]* [[STATE]], [16 x i8*]* [[BUFFER]], i64 16)
     75 
     76 // Release the array.
     77 // CHECK-LP64:      [[T0:%.*]] = bitcast [[ARRAY_T]]* [[SAVED_ARRAY]] to i8*
     78 // CHECK-LP64-NEXT: call void @objc_release(i8* [[T0]])
     79 
     80 // Destroy 'array'.
     81 // CHECK-LP64:      [[T0:%.*]] = bitcast [[ARRAY_T]]** [[ARRAY]] to i8**
     82 // CHECK-LP64-NEXT: call void @objc_storeStrong(i8** [[T0]], i8* null)
     83 // CHECK-LP64-NEXT: ret void
     84 
     85 // CHECK-LP64:    define internal void @__test0_block_invoke
     86 // CHECK-LP64:      [[BLOCK:%.*]] = bitcast i8* {{%.*}} to [[BLOCK_T]]*
     87 // CHECK-LP64-NOT:  ret
     88 // CHECK-LP64:      [[T0:%.*]] = getelementptr inbounds [[BLOCK_T]]* [[BLOCK]], i32 0, i32 5
     89 // CHECK-LP64-NEXT: [[T2:%.*]] = load i8** [[T0]], align 8 
     90 // CHECK-LP64-NEXT: call void @use(i8* [[T2]])
     91 
     92 void test1(NSArray *array) {
     93   for (__weak id x in array) {
     94     use_block(^{ use(x); });
     95   }
     96 }
     97 
     98 // CHECK-LP64:    define void @test1(
     99 // CHECK-LP64:      alloca [[ARRAY_T:%.*]]*,
    100 // CHECK-LP64-NEXT: [[X:%.*]] = alloca i8*,
    101 // CHECK-LP64-NEXT: [[STATE:%.*]] = alloca [[STATE_T:%.*]],
    102 // CHECK-LP64-NEXT: alloca [16 x i8*], align 8
    103 // CHECK-LP64-NEXT: [[BLOCK:%.*]] = alloca [[BLOCK_T:<{.*}>]],
    104 
    105 // CHECK-LP64:      [[T0:%.*]] = getelementptr inbounds [[STATE_T]]* [[STATE]], i32 0, i32 1
    106 // CHECK-LP64-NEXT: [[T1:%.*]] = load i8*** [[T0]]
    107 // CHECK-LP64-NEXT: [[T2:%.*]] = getelementptr i8** [[T1]], i64
    108 // CHECK-LP64-NEXT: [[T3:%.*]] = load i8** [[T2]]
    109 // CHECK-LP64-NEXT: call i8* @objc_initWeak(i8** [[X]], i8* [[T3]])
    110 
    111 // CHECK-LP64:      [[D0:%.*]] = getelementptr inbounds [[BLOCK_T]]* [[BLOCK]], i32 0, i32 5
    112 // CHECK-LP64:      [[T0:%.*]] = getelementptr inbounds [[BLOCK_T]]* [[BLOCK]], i32 0, i32 5
    113 // CHECK-LP64-NEXT: [[T1:%.*]] = call i8* @objc_loadWeakRetained(i8** [[X]])
    114 // CHECK-LP64-NEXT: call i8* @objc_initWeak(i8** [[T0]], i8* [[T1]])
    115 // CHECK-LP64-NEXT: call void @objc_release(i8* [[T1]]) 
    116 // CHECK-LP64-NEXT: [[T1:%.*]] = bitcast [[BLOCK_T]]* [[BLOCK]] to
    117 // CHECK-LP64: call void @use_block
    118 // CHECK-LP64-NEXT: call void @objc_destroyWeak(i8** [[D0]])
    119 // CHECK-LP64-NEXT: call void @objc_destroyWeak(i8** [[X]])
    120 
    121 // rdar://problem/9817306
    122 @interface Test2
    123 - (NSArray *) array;
    124 @end
    125 void test2(Test2 *a) {
    126   for (id x in a.array) {
    127     use(x);
    128   }
    129 }
    130 
    131 // CHECK-LP64:    define void @test2(
    132 // CHECK-LP64:      [[T0:%.*]] = call [[ARRAY_T]]* bitcast (i8* (i8*, i8*, ...)* @objc_msgSend to [[ARRAY_T]]* (i8*, i8*)*)(
    133 // CHECK-LP64-NEXT: [[T1:%.*]] = bitcast [[ARRAY_T]]* [[T0]] to i8*
    134 // CHECK-LP64-NEXT: [[T2:%.*]] = call i8* @objc_retainAutoreleasedReturnValue(i8* [[T1]])
    135 // CHECK-LP64-NEXT: [[COLL:%.*]] = bitcast i8* [[T2]] to [[ARRAY_T]]*
    136 
    137 // Make sure it's not immediately released before starting the iteration.
    138 // CHECK-LP64-NEXT: load i8** @"\01L_OBJC_SELECTOR_REFERENCES_
    139 // CHECK-LP64-NEXT: [[T0:%.*]] = bitcast [[ARRAY_T]]* [[COLL]] to i8*
    140 // CHECK-LP64-NEXT: @objc_msgSend
    141 
    142 // This bitcast is for the mutation check.
    143 // CHECK-LP64:      [[T0:%.*]] = bitcast [[ARRAY_T]]* [[COLL]] to i8*
    144 // CHECK-LP64-NEXT: @objc_enumerationMutation
    145 
    146 // This bitcast is for the 'next' message send.
    147 // CHECK-LP64:      [[T0:%.*]] = bitcast [[ARRAY_T]]* [[COLL]] to i8*
    148 // CHECK-LP64-NEXT: @objc_msgSend
    149 
    150 // This bitcast is for the final release.
    151 // CHECK-LP64:      [[T0:%.*]] = bitcast [[ARRAY_T]]* [[COLL]] to i8*
    152 // CHECK-LP64-NEXT: call void @objc_release(i8* [[T0]])
    153 
    154 
    155 // Check that the 'continue' label is positioned appropriately
    156 // relative to the collection clenaup.
    157 void test3(NSArray *array) {
    158   for (id x in array) {
    159     if (!x) continue;
    160     use(x);
    161   }
    162 
    163   // CHECK-LP64:    define void @test3(
    164   // CHECK-LP64:      [[ARRAY:%.*]] = alloca [[ARRAY_T]]*, align 8
    165   // CHECK-LP64-NEXT: [[X:%.*]] = alloca i8*, align 8
    166   // CHECK-LP64:      [[T0:%.*]] = load i8** [[X]], align 8
    167   // CHECK-LP64-NEXT: [[T1:%.*]] = icmp ne i8* [[T0]], null
    168   // CHECK-LP64-NEXT: br i1 [[T1]],
    169   // CHECK-LP64:      br label [[L:%[^ ]+]]
    170   // CHECK-LP64:      [[T0:%.*]] = load i8** [[X]], align 8
    171   // CHECK-LP64-NEXT: call void @use(i8* [[T0]])
    172   // CHECK-LP64-NEXT: br label [[L]]
    173 }
    174 
    175 // CHECK: attributes [[NUW]] = { nounwind }
    176