From 52a80e19ad688091723a52ad53337767bb0ac684 Mon Sep 17 00:00:00 2001 From: Fariborz Jahanian Date: Wed, 26 Jan 2011 23:08:27 +0000 Subject: [PATCH] Fixes an IRgen bug where __block variable is referenced in the block-literal initializer of that variable. // rdar://8893785 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@124332 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGDecl.cpp | 30 +++++++++++++++++++++++++++--- lib/CodeGen/CGExpr.cpp | 9 +++------ lib/CodeGen/CodeGenFunction.h | 6 +++++- test/CodeGenObjC/block-6.m | 12 ++++++++++++ 4 files changed, 47 insertions(+), 10 deletions(-) create mode 100644 test/CodeGenObjC/block-6.m diff --git a/lib/CodeGen/CGDecl.cpp b/lib/CodeGen/CGDecl.cpp index 8270e84cb5..f12be98510 100644 --- a/lib/CodeGen/CGDecl.cpp +++ b/lib/CodeGen/CGDecl.cpp @@ -305,6 +305,15 @@ unsigned CodeGenFunction::getByRefValueLLVMField(const ValueDecl *VD) const { return ByRefValueInfo.find(VD)->second.second; } +llvm::Value *CodeGenFunction::BuildBlockByrefAddress(llvm::Value *BaseAddr, + const VarDecl *V) { + llvm::Value *Loc = Builder.CreateStructGEP(BaseAddr, 1, "forwarding"); + Loc = Builder.CreateLoad(Loc); + Loc = Builder.CreateStructGEP(Loc, getByRefValueLLVMField(V), + V->getNameAsString()); + return Loc; +} + /// BuildByRefType - This routine changes a __block variable declared as T x /// into: /// @@ -795,9 +804,6 @@ void CodeGenFunction::EmitAutoVarDecl(const VarDecl &D, SpecialInit(*this, D, DeclPtr); } else if (Init) { llvm::Value *Loc = DeclPtr; - if (isByRef) - Loc = Builder.CreateStructGEP(DeclPtr, getByRefValueLLVMField(&D), - D.getNameAsString()); bool isVolatile = getContext().getCanonicalType(Ty).isVolatileQualified(); @@ -844,13 +850,31 @@ void CodeGenFunction::EmitAutoVarDecl(const VarDecl &D, } } else if (Ty->isReferenceType()) { RValue RV = EmitReferenceBindingToExpr(Init, &D); + if (isByRef) + Loc = Builder.CreateStructGEP(DeclPtr, getByRefValueLLVMField(&D), + D.getNameAsString()); EmitStoreOfScalar(RV.getScalarVal(), Loc, false, Alignment, Ty); } else if (!hasAggregateLLVMType(Init->getType())) { llvm::Value *V = EmitScalarExpr(Init); + if (isByRef) { + // When RHS has side-effect, must go through "forwarding' field + // to get to the address of the __block variable descriptor. + if (Init->HasSideEffects(getContext())) + Loc = BuildBlockByrefAddress(DeclPtr, &D); + else + Loc = Builder.CreateStructGEP(DeclPtr, getByRefValueLLVMField(&D), + D.getNameAsString()); + } EmitStoreOfScalar(V, Loc, isVolatile, Alignment, Ty); } else if (Init->getType()->isAnyComplexType()) { + if (isByRef) + Loc = Builder.CreateStructGEP(DeclPtr, getByRefValueLLVMField(&D), + D.getNameAsString()); EmitComplexExprIntoAddr(Init, Loc, isVolatile); } else { + if (isByRef) + Loc = Builder.CreateStructGEP(DeclPtr, getByRefValueLLVMField(&D), + D.getNameAsString()); EmitAggExpr(Init, AggValueSlot::forAddr(Loc, isVolatile, true, false)); } } diff --git a/lib/CodeGen/CGExpr.cpp b/lib/CodeGen/CGExpr.cpp index afc1e0709e..ca80d3dd38 100644 --- a/lib/CodeGen/CGExpr.cpp +++ b/lib/CodeGen/CGExpr.cpp @@ -1164,12 +1164,9 @@ LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) { V = CGM.getStaticLocalDeclAddress(VD); assert(V && "DeclRefExpr not entered in LocalDeclMap?"); - if (VD->hasAttr()) { - V = Builder.CreateStructGEP(V, 1, "forwarding"); - V = Builder.CreateLoad(V); - V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD), - VD->getNameAsString()); - } + if (VD->hasAttr()) + V = BuildBlockByrefAddress(V, VD); + if (VD->getType()->isReferenceType()) V = Builder.CreateLoad(V, "tmp"); diff --git a/lib/CodeGen/CodeGenFunction.h b/lib/CodeGen/CodeGenFunction.h index dcd6173763..834a1cfa2f 100644 --- a/lib/CodeGen/CodeGenFunction.h +++ b/lib/CodeGen/CodeGenFunction.h @@ -811,7 +811,11 @@ public: /// getByrefValueFieldNumber - Given a declaration, returns the LLVM field /// number that holds the value. unsigned getByRefValueLLVMField(const ValueDecl *VD) const; - + + /// BuildBlockByrefAddress - Computes address location of the + /// variable which is declared as __block. + llvm::Value *BuildBlockByrefAddress(llvm::Value *BaseAddr, + const VarDecl *V); private: CGDebugInfo *DebugInfo; diff --git a/test/CodeGenObjC/block-6.m b/test/CodeGenObjC/block-6.m new file mode 100644 index 0000000000..3720a87b9e --- /dev/null +++ b/test/CodeGenObjC/block-6.m @@ -0,0 +1,12 @@ +// RUN: %clang_cc1 %s -emit-llvm -o - -fblocks -triple x86_64-apple-darwin10 | FileCheck %s +// rdar://8893785 + +void MYFUNC() { +// CHECK: [[T1:%.*]] = bitcast i8* ()* +// CHECK-NEXT: [[FORWARDING:%.*]] = getelementptr inbounds [[N_T:%.*]]* [[N:%.*]], i32 0, i32 1 +// CHECK-NEXT: [[T0:%.*]] = load [[N_T]]** [[FORWARDING]] +// CHECK-NEXT: [[OBSERVER:%.*]] = getelementptr inbounds [[N_T]]* [[T0]], i32 0, i32 6 +// CHECK-NEXT: store i8* [[T1]], i8** [[OBSERVER]] + __block id observer = ^{ return observer; }; +} + -- 2.40.0