1// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -emit-llvm -fblocks -fobjc-arc -fobjc-runtime-has-weak -O2 -disable-llvm-passes -o - %s | FileCheck %s 2 3id g0, g1; 4 5void test0(_Bool cond) { 6 id test0_helper(void) __attribute__((ns_returns_retained)); 7 8 // CHECK-LABEL: define void @test0( 9 // CHECK: [[COND:%.*]] = alloca i8, 10 // CHECK-NEXT: [[X:%.*]] = alloca i8*, 11 // CHECK-NEXT: [[RELVAL:%.*]] = alloca i8* 12 // CHECK-NEXT: [[RELCOND:%.*]] = alloca i1 13 // CHECK-NEXT: zext 14 // CHECK-NEXT: store 15 // CHECK-NEXT: [[XPTR1:%.*]] = bitcast i8** [[X]] to i8* 16 // CHECK-NEXT: call void @llvm.lifetime.start.p0i8(i64 8, i8* [[XPTR1]]) 17 // CHECK-NEXT: [[T0:%.*]] = load i8, i8* [[COND]] 18 // CHECK-NEXT: [[T1:%.*]] = trunc i8 [[T0]] to i1 19 // CHECK-NEXT: store i1 false, i1* [[RELCOND]] 20 // CHECK-NEXT: br i1 [[T1]], 21 // CHECK: br label 22 // CHECK: [[CALL:%.*]] = call i8* @test0_helper() 23 // CHECK-NEXT: store i8* [[CALL]], i8** [[RELVAL]] 24 // CHECK-NEXT: store i1 true, i1* [[RELCOND]] 25 // CHECK-NEXT: br label 26 // CHECK: [[T0:%.*]] = phi i8* [ null, {{%.*}} ], [ [[CALL]], {{%.*}} ] 27 // CHECK-NEXT: [[T1:%.*]] = call i8* @llvm.objc.retain(i8* [[T0]]) [[NUW:#[0-9]+]] 28 // CHECK-NEXT: store i8* [[T1]], i8** [[X]], 29 // CHECK-NEXT: [[REL:%.*]] = load i1, i1* [[RELCOND]] 30 // CHECK-NEXT: br i1 [[REL]], 31 // CHECK: [[T0:%.*]] = load i8*, i8** [[RELVAL]] 32 // CHECK-NEXT: call void @llvm.objc.release(i8* [[T0]]) [[NUW]] 33 // CHECK-NEXT: br label 34 // CHECK: [[T0:%.*]] = load i8*, i8** [[X]] 35 // CHECK-NEXT: call void @llvm.objc.release(i8* [[T0]]) [[NUW]] 36 // CHECK-NEXT: [[XPTR2:%.*]] = bitcast i8** [[X]] to i8* 37 // CHECK-NEXT: call void @llvm.lifetime.end.p0i8(i64 8, i8* [[XPTR2]]) 38 // CHECK-NEXT: ret void 39 id x = (cond ? 0 : test0_helper()); 40} 41 42void test1(int cond) { 43 __strong id strong; 44 __weak id weak; 45 extern void test1_sink(id *); 46 test1_sink(cond ? &strong : 0); 47 test1_sink(cond ? &weak : 0); 48 49 // CHECK-LABEL: define void @test1( 50 // CHECK: [[COND:%.*]] = alloca i32 51 // CHECK-NEXT: [[STRONG:%.*]] = alloca i8* 52 // CHECK-NEXT: [[WEAK:%.*]] = alloca i8* 53 // CHECK-NEXT: [[TEMP1:%.*]] = alloca i8* 54 // CHECK-NEXT: [[TEMP2:%.*]] = alloca i8* 55 // CHECK-NEXT: [[CONDCLEANUPSAVE:%.*]] = alloca i8* 56 // CHECK-NEXT: [[CONDCLEANUP:%.*]] = alloca i1 57 // CHECK-NEXT: store i32 58 // CHECK-NEXT: [[STRONGPTR1:%.*]] = bitcast i8** [[STRONG]] to i8* 59 // CHECK-NEXT: call void @llvm.lifetime.start.p0i8(i64 8, i8* [[STRONGPTR1]]) 60 // CHECK-NEXT: store i8* null, i8** [[STRONG]] 61 // CHECK-NEXT: [[WEAKPTR1:%.*]] = bitcast i8** [[WEAK]] to i8* 62 // CHECK-NEXT: call void @llvm.lifetime.start.p0i8(i64 8, i8* [[WEAKPTR1]]) 63 // CHECK-NEXT: call i8* @llvm.objc.initWeak(i8** [[WEAK]], i8* null) 64 65 // CHECK-NEXT: [[T0:%.*]] = load i32, i32* [[COND]] 66 // CHECK-NEXT: [[T1:%.*]] = icmp ne i32 [[T0]], 0 67 // CHECK: [[ARG:%.*]] = phi i8** 68 // CHECK-NEXT: [[T0:%.*]] = icmp eq i8** [[ARG]], null 69 // CHECK-NEXT: [[T1:%.*]] = select i1 [[T0]], i8** null, i8** [[TEMP1]] 70 // CHECK-NEXT: br i1 [[T0]], 71 // CHECK: [[T0:%.*]] = load i8*, i8** [[ARG]] 72 // CHECK-NEXT: store i8* [[T0]], i8** [[TEMP1]] 73 // CHECK-NEXT: br label 74 // CHECK: [[W:%.*]] = phi i8* [ [[T0]], {{%.*}} ], [ undef, {{%.*}} ] 75 // CHECK-NEXT: call void @test1_sink(i8** [[T1]]) 76 // CHECK-NEXT: [[T0:%.*]] = icmp eq i8** [[ARG]], null 77 // CHECK-NEXT: br i1 [[T0]], 78 // CHECK: [[T0:%.*]] = load i8*, i8** [[TEMP1]] 79 // CHECK-NEXT: [[T1:%.*]] = call i8* @llvm.objc.retain(i8* [[T0]]) 80 // CHECK-NEXT: call void (...) @llvm.objc.clang.arc.use(i8* [[W]]) [[NUW]] 81 // CHECK-NEXT: [[T2:%.*]] = load i8*, i8** [[ARG]] 82 // CHECK-NEXT: store i8* [[T1]], i8** [[ARG]] 83 // CHECK-NEXT: call void @llvm.objc.release(i8* [[T2]]) 84 // CHECK-NEXT: br label 85 86 // CHECK: [[T0:%.*]] = load i32, i32* [[COND]] 87 // CHECK-NEXT: [[T1:%.*]] = icmp ne i32 [[T0]], 0 88 // CHECK: [[ARG:%.*]] = phi i8** 89 // CHECK-NEXT: [[T0:%.*]] = icmp eq i8** [[ARG]], null 90 // CHECK-NEXT: [[T1:%.*]] = select i1 [[T0]], i8** null, i8** [[TEMP2]] 91 // CHECK-NEXT: store i1 false, i1* [[CONDCLEANUP]] 92 // CHECK-NEXT: br i1 [[T0]], 93 // CHECK: [[T0:%.*]] = call i8* @llvm.objc.loadWeakRetained(i8** [[ARG]]) 94 // CHECK-NEXT: store i8* [[T0]], i8** [[CONDCLEANUPSAVE]] 95 // CHECK-NEXT: store i1 true, i1* [[CONDCLEANUP]] 96 // CHECK-NEXT: store i8* [[T0]], i8** [[TEMP2]] 97 // CHECK-NEXT: br label 98 // CHECK: call void @test1_sink(i8** [[T1]]) 99 // CHECK-NEXT: [[T0:%.*]] = icmp eq i8** [[ARG]], null 100 // CHECK-NEXT: br i1 [[T0]], 101 // CHECK: [[T0:%.*]] = load i8*, i8** [[TEMP2]] 102 // CHECK-NEXT: call i8* @llvm.objc.storeWeak(i8** [[ARG]], i8* [[T0]]) 103 // CHECK-NEXT: br label 104 105 // CHECK: call void @llvm.objc.destroyWeak(i8** [[WEAK]]) 106 // CHECK: [[WEAKPTR2:%.*]] = bitcast i8** [[WEAK]] to i8* 107 // CHECK: call void @llvm.lifetime.end.p0i8(i64 8, i8* [[WEAKPTR2]]) 108 // CHECK: [[STRONGPTR2:%.*]] = bitcast i8** [[STRONG]] to i8* 109 // CHECK: call void @llvm.lifetime.end.p0i8(i64 8, i8* [[STRONGPTR2]]) 110 // CHECK: ret void 111} 112 113// rdar://13113981 114// Test that, when emitting an expression at +1 that we can't peephole, 115// we emit the retain inside the full-expression. If we ever peephole 116// +1s of conditional expressions (which we probably ought to), we'll 117// need to find another example of something we need to do this for. 118void test2(int cond) { 119 extern id test2_producer(void); 120 for (id obj in cond ? test2_producer() : (void*) 0) { 121 } 122 123 // CHECK-LABEL: define void @test2( 124 // CHECK: [[COND:%.*]] = alloca i32, 125 // CHECK: alloca i8* 126 // CHECK: [[CLEANUP_SAVE:%.*]] = alloca i8* 127 // CHECK: [[RUN_CLEANUP:%.*]] = alloca i1 128 // Evaluate condition; cleanup disabled by default. 129 // CHECK: [[T0:%.*]] = load i32, i32* [[COND]], 130 // CHECK-NEXT: icmp ne i32 [[T0]], 0 131 // CHECK-NEXT: store i1 false, i1* [[RUN_CLEANUP]] 132 // CHECK-NEXT: br i1 133 // Within true branch, cleanup enabled. 134 // CHECK: [[T0:%.*]] = call i8* @test2_producer() 135 // CHECK-NEXT: [[T1:%.*]] = notail call i8* @llvm.objc.retainAutoreleasedReturnValue(i8* [[T0]]) 136 // CHECK-NEXT: store i8* [[T1]], i8** [[CLEANUP_SAVE]] 137 // CHECK-NEXT: store i1 true, i1* [[RUN_CLEANUP]] 138 // CHECK-NEXT: br label 139 // Join point for conditional operator; retain immediately. 140 // CHECK: [[T0:%.*]] = phi i8* [ [[T1]], {{%.*}} ], [ null, {{%.*}} ] 141 // CHECK-NEXT: [[RESULT:%.*]] = call i8* @llvm.objc.retain(i8* [[T0]]) 142 // Leaving full-expression; run conditional cleanup. 143 // CHECK-NEXT: [[T0:%.*]] = load i1, i1* [[RUN_CLEANUP]] 144 // CHECK-NEXT: br i1 [[T0]] 145 // CHECK: [[T0:%.*]] = load i8*, i8** [[CLEANUP_SAVE]] 146 // CHECK-NEXT: call void @llvm.objc.release(i8* [[T0]]) 147 // CHECK-NEXT: br label 148 // And way down at the end of the loop: 149 // CHECK: call void @llvm.objc.release(i8* [[RESULT]]) 150} 151 152void test3(int cond) { 153 __strong id *p = cond ? (__strong id[]){g0, g1} : (__strong id[]){g1, g0}; 154 test2(cond); 155 156 // CHECK: define void @test3( 157 // CHECK: %[[P:.*]] = alloca i8**, align 8 158 // CHECK: %[[_COMPOUNDLITERAL:.*]] = alloca [2 x i8*], align 8 159 // CHECK: %[[CLEANUP_COND:.*]] = alloca i1, align 1 160 // CHECK: %[[_COMPOUNDLITERAL1:.*]] = alloca [2 x i8*], align 8 161 // CHECK: %[[CLEANUP_COND4:.*]] = alloca i1, align 1 162 163 // CHECK: %[[ARRAYINIT_BEGIN:.*]] = getelementptr inbounds [2 x i8*], [2 x i8*]* %[[_COMPOUNDLITERAL]], i64 0, i64 0 164 // CHECK: %[[V2:.*]] = load i8*, i8** @g0, align 8 165 // CHECK: %[[V3:.*]] = call i8* @llvm.objc.retain(i8* %[[V2]]) 166 // CHECK: store i8* %[[V3]], i8** %[[ARRAYINIT_BEGIN]], align 8 167 // CHECK: %[[ARRAYINIT_ELEMENT:.*]] = getelementptr inbounds i8*, i8** %[[ARRAYINIT_BEGIN]], i64 1 168 // CHECK: %[[V4:.*]] = load i8*, i8** @g1, align 8 169 // CHECK: %[[V5:.*]] = call i8* @llvm.objc.retain(i8* %[[V4]]) 170 // CHECK: store i8* %[[V5]], i8** %[[ARRAYINIT_ELEMENT]], align 8 171 // CHECK: store i1 true, i1* %[[CLEANUP_COND]], align 1 172 // CHECK: %[[ARRAYDECAY:.*]] = getelementptr inbounds [2 x i8*], [2 x i8*]* %[[_COMPOUNDLITERAL]], i64 0, i64 0 173 174 // CHECK: %[[ARRAYINIT_BEGIN2:.*]] = getelementptr inbounds [2 x i8*], [2 x i8*]* %[[_COMPOUNDLITERAL1]], i64 0, i64 0 175 // CHECK: %[[V6:.*]] = load i8*, i8** @g1, align 8 176 // CHECK: %[[V7:.*]] = call i8* @llvm.objc.retain(i8* %[[V6]]) 177 // CHECK: store i8* %[[V7]], i8** %[[ARRAYINIT_BEGIN2]], align 8 178 // CHECK: %[[ARRAYINIT_ELEMENT3:.*]] = getelementptr inbounds i8*, i8** %[[ARRAYINIT_BEGIN2]], i64 1 179 // CHECK: %[[V8:.*]] = load i8*, i8** @g0, align 8 180 // CHECK: %[[V9:.*]] = call i8* @llvm.objc.retain(i8* %[[V8]]) 181 // CHECK: store i8* %[[V9]], i8** %[[ARRAYINIT_ELEMENT3]], align 8 182 // CHECK: store i1 true, i1* %[[CLEANUP_COND4]], align 1 183 // CHECK: %[[ARRAYDECAY5:.*]] = getelementptr inbounds [2 x i8*], [2 x i8*]* %[[_COMPOUNDLITERAL1]], i64 0, i64 0 184 185 // CHECK: %[[COND6:.*]] = phi i8** [ %[[ARRAYDECAY]], %{{.*}} ], [ %[[ARRAYDECAY5]], %{{.*}} ] 186 // CHECK: store i8** %[[COND6]], i8*** %[[P]], align 8 187 // CHECK: call void @test2( 188 189 // CHECK: %[[ARRAY_BEGIN:.*]] = getelementptr inbounds [2 x i8*], [2 x i8*]* %[[_COMPOUNDLITERAL1]], i32 0, i32 0 190 // CHECK: %[[V11:.*]] = getelementptr inbounds i8*, i8** %[[ARRAY_BEGIN]], i64 2 191 192 // CHECK: %[[ARRAYDESTROY_ELEMENTPAST:.*]] = phi i8** [ %[[V11]], %{{.*}} ], [ %[[ARRAYDESTROY_ELEMENT:.*]], %{{.*}} ] 193 // CHECK: %[[ARRAYDESTROY_ELEMENT]] = getelementptr inbounds i8*, i8** %[[ARRAYDESTROY_ELEMENTPAST]], i64 -1 194 // CHECK: %[[V12:.*]] = load i8*, i8** %[[ARRAYDESTROY_ELEMENT]], align 8 195 // CHECK: call void @llvm.objc.release(i8* %[[V12]]) 196 197 // CHECK: %[[ARRAY_BEGIN10:.*]] = getelementptr inbounds [2 x i8*], [2 x i8*]* %[[_COMPOUNDLITERAL]], i32 0, i32 0 198 // CHECK: %[[V13:.*]] = getelementptr inbounds i8*, i8** %[[ARRAY_BEGIN10]], i64 2 199 200 // CHECK: %[[ARRAYDESTROY_ELEMENTPAST12:.*]] = phi i8** [ %[[V13]], %{{.*}} ], [ %[[ARRAYDESTROY_ELEMENT13:.*]], %{{.*}} ] 201 // CHECK: %[[ARRAYDESTROY_ELEMENT13]] = getelementptr inbounds i8*, i8** %[[ARRAYDESTROY_ELEMENTPAST12]], i64 -1 202 // CHECK: %[[V14:.*]] = load i8*, i8** %[[ARRAYDESTROY_ELEMENT13]], align 8 203 // CHECK: call void @llvm.objc.release(i8* %[[V14]]) 204} 205 206// CHECK: attributes [[NUW]] = { nounwind } 207