From 527842f97b684831de2b1a455649131ed381ece4 Mon Sep 17 00:00:00 2001 From: John McCall Date: Thu, 4 Apr 2013 00:20:38 +0000 Subject: [PATCH] Protect the values of array and dictionary literals from the ARC optimizer while they're held in local unsafe buffers. Based on a patch by Jesse Rusak! rdar://13573224 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@178721 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGObjC.cpp | 39 +++++++++++--- test/CodeGenObjC/arc-literals.m | 91 ++++++++++++++++++++++++--------- 2 files changed, 100 insertions(+), 30 deletions(-) diff --git a/lib/CodeGen/CGObjC.cpp b/lib/CodeGen/CGObjC.cpp index 2e3bd5f7f1..79d97b99b4 100644 --- a/lib/CodeGen/CGObjC.cpp +++ b/lib/CodeGen/CGObjC.cpp @@ -109,32 +109,50 @@ llvm::Value *CodeGenFunction::EmitObjCCollectionLiteral(const Expr *E, if (DLE) Keys = CreateMemTemp(ElementArrayType, "keys"); + // In ARC, we may need to do extra work to keep all the keys and + // values alive until after the call. + SmallVector NeededObjects; + bool TrackNeededObjects = + (getLangOpts().ObjCAutoRefCount && + CGM.getCodeGenOpts().OptimizationLevel != 0); + // Perform the actual initialialization of the array(s). for (uint64_t i = 0; i < NumElements; i++) { if (ALE) { - // Emit the initializer. + // Emit the element and store it to the appropriate array slot. const Expr *Rhs = ALE->getElement(i); LValue LV = LValue::MakeAddr(Builder.CreateStructGEP(Objects, i), ElementType, Context.getTypeAlignInChars(Rhs->getType()), Context); - EmitScalarInit(Rhs, /*D=*/0, LV, /*capturedByInit=*/false); + + llvm::Value *value = EmitScalarExpr(Rhs); + EmitStoreThroughLValue(RValue::get(value), LV, true); + if (TrackNeededObjects) { + NeededObjects.push_back(value); + } } else { - // Emit the key initializer. + // Emit the key and store it to the appropriate array slot. const Expr *Key = DLE->getKeyValueElement(i).Key; LValue KeyLV = LValue::MakeAddr(Builder.CreateStructGEP(Keys, i), ElementType, Context.getTypeAlignInChars(Key->getType()), Context); - EmitScalarInit(Key, /*D=*/0, KeyLV, /*capturedByInit=*/false); + llvm::Value *keyValue = EmitScalarExpr(Key); + EmitStoreThroughLValue(RValue::get(keyValue), KeyLV, /*isInit=*/true); - // Emit the value initializer. + // Emit the value and store it to the appropriate array slot. const Expr *Value = DLE->getKeyValueElement(i).Value; LValue ValueLV = LValue::MakeAddr(Builder.CreateStructGEP(Objects, i), ElementType, Context.getTypeAlignInChars(Value->getType()), Context); - EmitScalarInit(Value, /*D=*/0, ValueLV, /*capturedByInit=*/false); + llvm::Value *valueValue = EmitScalarExpr(Value); + EmitStoreThroughLValue(RValue::get(valueValue), ValueLV, /*isInit=*/true); + if (TrackNeededObjects) { + NeededObjects.push_back(keyValue); + NeededObjects.push_back(valueValue); + } } } @@ -172,6 +190,15 @@ llvm::Value *CodeGenFunction::EmitObjCCollectionLiteral(const Expr *E, Sel, Receiver, Args, Class, MethodWithObjects); + + // The above message send needs these objects, but in ARC they are + // passed in a buffer that is essentially __unsafe_unretained. + // Therefore we must prevent the optimizer from releasing them until + // after the call. + if (TrackNeededObjects) { + EmitARCIntrinsicUse(NeededObjects); + } + return Builder.CreateBitCast(result.getScalarVal(), ConvertType(E->getType())); } diff --git a/test/CodeGenObjC/arc-literals.m b/test/CodeGenObjC/arc-literals.m index 203c2ad1ee..78c5d9d237 100644 --- a/test/CodeGenObjC/arc-literals.m +++ b/test/CodeGenObjC/arc-literals.m @@ -35,18 +35,28 @@ void test_numeric() { // CHECK: define void @test_array void test_array(id a, id b) { + // CHECK: [[A:%.*]] = alloca i8*, + // CHECK: [[B:%.*]] = alloca i8*, + // Retaining parameters // CHECK: call i8* @objc_retain(i8* // CHECK: call i8* @objc_retain(i8* // Constructing the array - // CHECK: getelementptr inbounds [2 x i8*]* [[OBJECTS:%[A-Za-z0-9]+]], i32 0, i32 0 - // CHECK: store i8* - // CHECK: getelementptr inbounds [2 x i8*]* [[OBJECTS]], i32 0, i32 1 - // CHECK: store i8* - - // CHECK: {{call i8*.*objc_msgSend.*i64 2}} - // CHECK: call i8* @objc_retainAutoreleasedReturnValue + // CHECK: [[T0:%.*]] = getelementptr inbounds [2 x i8*]* [[OBJECTS:%[A-Za-z0-9]+]], i32 0, i32 0 + // CHECK-NEXT: [[V0:%.*]] = load i8** [[A]], + // CHECK-NEXT: store i8* [[V0]], i8** [[T0]] + // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds [2 x i8*]* [[OBJECTS]], i32 0, i32 1 + // CHECK-NEXT: [[V1:%.*]] = load i8** [[B]], + // CHECK-NEXT: store i8* [[V1]], i8** [[T0]] + + // CHECK-NEXT: [[T0:%.*]] = load [[CLASS_T:%.*]]** @"\01L_OBJC_CLASSLIST + // CHECK-NEXT: [[SEL:%.*]] = load i8** @"\01L_OBJC_SELECTOR_REFERENCES + // CHECK-NEXT: [[T1:%.*]] = bitcast [[CLASS_T]]* [[T0]] to i8* + // CHECK-NEXT: [[T2:%.*]] = bitcast [2 x i8*]* [[OBJECTS]] to i8** + // CHECK-NEXT: [[T3:%.*]] = call i8* bitcast ({{.*@objc_msgSend.*}})(i8* [[T1]], i8* [[SEL]], i8** [[T2]], i64 2) + // CHECK-NEXT: [[T4:%.*]] = call i8* @objc_retainAutoreleasedReturnValue(i8* [[T3]]) + // CHECK: call void (...)* @clang.arc.use(i8* [[V0]], i8* [[V1]]) id arr = @[a, b]; // CHECK: call void @objc_release @@ -57,6 +67,11 @@ void test_array(id a, id b) { // CHECK: define void @test_dictionary void test_dictionary(id k1, id o1, id k2, id o2) { + // CHECK: [[K1:%.*]] = alloca i8*, + // CHECK: [[O1:%.*]] = alloca i8*, + // CHECK: [[K2:%.*]] = alloca i8*, + // CHECK: [[O2:%.*]] = alloca i8*, + // Retaining parameters // CHECK: call i8* @objc_retain(i8* // CHECK: call i8* @objc_retain(i8* @@ -64,18 +79,29 @@ void test_dictionary(id k1, id o1, id k2, id o2) { // CHECK: call i8* @objc_retain(i8* // Constructing the arrays - // CHECK: getelementptr inbounds [2 x i8*]* [[KEYS:%[A-Za-z0-9]+]], i32 0, i32 0 - // CHECK: store i8* - // CHECK: getelementptr inbounds [2 x i8*]* [[OBJECTS:%[A-Za-z0-9]+]], i32 0, i32 0 - // CHECK: store i8* - // CHECK: getelementptr inbounds [2 x i8*]* [[KEYS]], i32 0, i32 1 - // CHECK: store i8* - // CHECK: getelementptr inbounds [2 x i8*]* [[OBJECTS]], i32 0, i32 1 - // CHECK: store i8* + // CHECK: [[T0:%.*]] = getelementptr inbounds [2 x i8*]* [[KEYS:%[A-Za-z0-9]+]], i32 0, i32 0 + // CHECK-NEXT: [[V0:%.*]] = load i8** [[K1]], + // CHECK-NEXT: store i8* [[V0]], i8** [[T0]] + // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds [2 x i8*]* [[OBJECTS:%[A-Za-z0-9]+]], i32 0, i32 0 + // CHECK-NEXT: [[V1:%.*]] = load i8** [[O1]], + // CHECK-NEXT: store i8* [[V1]], i8** [[T0]] + // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds [2 x i8*]* [[KEYS]], i32 0, i32 1 + // CHECK-NEXT: [[V2:%.*]] = load i8** [[K2]], + // CHECK-NEXT: store i8* [[V2]], i8** [[T0]] + // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds [2 x i8*]* [[OBJECTS]], i32 0, i32 1 + // CHECK-NEXT: [[V3:%.*]] = load i8** [[O2]], + // CHECK-NEXT: store i8* [[V3]], i8** [[T0]] // Constructing the dictionary - // CHECK: {{call i8.*@objc_msgSend}} - // CHECK: call i8* @objc_retainAutoreleasedReturnValue + // CHECK-NEXT: [[T0:%.*]] = load [[CLASS_T:%.*]]** @"\01L_OBJC_CLASSLIST + // CHECK-NEXT: [[SEL:%.*]] = load i8** @"\01L_OBJC_SELECTOR_REFERENCES + // CHECK-NEXT: [[T1:%.*]] = bitcast [[CLASS_T]]* [[T0]] to i8* + // CHECK-NEXT: [[T2:%.*]] = bitcast [2 x i8*]* [[OBJECTS]] to i8** + // CHECK-NEXT: [[T3:%.*]] = bitcast [2 x i8*]* [[KEYS]] to i8** + // CHECK-NEXT: [[T4:%.*]] = call i8* bitcast ({{.*@objc_msgSend.*}})(i8* [[T1]], i8* [[SEL]], i8** [[T2]], i8** [[T3]], i64 2) + // CHECK-NEXT: [[T5:%.*]] = call i8* @objc_retainAutoreleasedReturnValue(i8* [[T4]]) + // CHECK-NEXT: call void (...)* @clang.arc.use(i8* [[V0]], i8* [[V1]], i8* [[V2]], i8* [[V3]]) + id dict = @{ k1 : o1, k2 : o2 }; // CHECK: call void @objc_release @@ -98,19 +124,36 @@ void test_property(B *b) { // Retain parameter // CHECK: call i8* @objc_retain + // CHECK: [[T0:%.*]] = getelementptr inbounds [1 x i8*]* [[OBJECTS:%.*]], i32 0, i32 0 + // Invoke 'prop' - // CHECK: load i8** @"\01L_OBJC_SELECTOR_REFERENCES - // CHECK: {{call.*@objc_msgSend}} - // CHECK: call i8* @objc_retainAutoreleasedReturnValue + // CHECK: [[SEL:%.*]] = load i8** @"\01L_OBJC_SELECTOR_REFERENCES + // CHECK-NEXT: [[T1:%.*]] = bitcast + // CHECK-NEXT: [[T2:%.*]] = call [[B:%.*]]* bitcast ({{.*}} @objc_msgSend to {{.*}})(i8* [[T1]], i8* [[SEL]]) + // CHECK-NEXT: [[T3:%.*]] = bitcast [[B]]* [[T2]] to i8* + // CHECK-NEXT: [[T4:%.*]] = call i8* @objc_retainAutoreleasedReturnValue(i8* [[T3]]) + // CHECK-NEXT: [[V0:%.*]] = bitcast i8* [[T4]] to [[B]]* + // CHECK-NEXT: [[V1:%.*]] = bitcast [[B]]* [[V0]] to i8* + + // Store to array. + // CHECK-NEXT: store i8* [[V1]], i8** [[T0]] // Invoke arrayWithObjects:count: - // CHECK: load i8** @"\01L_OBJC_SELECTOR_REFERENCES - // CHECK: {{call.*objc_msgSend}} - // CHECK: call i8* @objc_retainAutoreleasedReturnValue + // CHECK-NEXT: [[T0:%.*]] = load [[CLASS_T]]** @"\01L_OBJC_CLASSLIST + // CHECK-NEXT: [[SEL:%.*]] = load i8** @"\01L_OBJC_SELECTOR_REFERENCES + // CHECK-NEXT: [[T1:%.*]] = bitcast [[CLASS_T]]* [[T0]] to i8* + // CHECK-NEXT: [[T2:%.*]] = bitcast [1 x i8*]* [[OBJECTS]] to i8** + // CHECK-NEXT: [[T3:%.*]] = call i8* bitcast ({{.*}} @objc_msgSend to {{.*}}(i8* [[T1]], i8* [[SEL]], i8** [[T2]], i64 1) + // CHECK-NEXT: call i8* @objc_retainAutoreleasedReturnValue(i8* [[T3]]) + // CHECK-NEXT: call void (...)* @clang.arc.use(i8* [[V1]]) + // CHECK-NEXT: bitcast + // CHECK-NEXT: bitcast + // CHECK-NEXT: store id arr = @[ b.prop ]; // Release b.prop - // CHECK: call void @objc_release + // CHECK-NEXT: [[T0:%.*]] = bitcast [[B]]* [[V0]] to i8* + // CHECK-NEXT: call void @objc_release(i8* [[T0]]) // Destroy arr // CHECK: call void @objc_release -- 2.40.0