From 3a2b657088de9413714a51bff153a59565adb3ef Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Tue, 12 Jul 2011 06:52:18 +0000 Subject: Fix a problem Eli ran into where we now reject incomplete arrays of uncompleted struct types. We now do what llvm-gcc does and compile them into [i8 x 0]. If the type is later completed, we make sure that it is appropriately cast. We compile the terrible example to something like this now: %struct.A = type { i32, i32, i32 } @g = external global [0 x i8] define void @_Z1fv() nounwind { entry: call void @_Z3fooP1A(%struct.A* bitcast ([0 x i8]* @g to %struct.A*)) ret void } declare void @_Z3fooP1A(%struct.A*) define %struct.A* @_Z2f2v() nounwind { entry: ret %struct.A* getelementptr inbounds ([0 x %struct.A]* bitcast ([0 x i8]* @g to [0 x %struct.A]*), i32 0, i64 1) } git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@134972 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGExpr.cpp | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'lib/CodeGen/CGExpr.cpp') diff --git a/lib/CodeGen/CGExpr.cpp b/lib/CodeGen/CGExpr.cpp index e1d93095bd..6055fa759b 100644 --- a/lib/CodeGen/CGExpr.cpp +++ b/lib/CodeGen/CGExpr.cpp @@ -1274,6 +1274,14 @@ static void setObjCGCLValueClass(const ASTContext &Ctx, const Expr *E, } } +static llvm::Value * +EmitBitCastOfLValueToProperType(llvm::IRBuilder<> &Builder, + llvm::Value *V, llvm::Type *IRType, + llvm::StringRef Name = llvm::StringRef()) { + unsigned AS = cast(V->getType())->getAddressSpace(); + return Builder.CreateBitCast(V, IRType->getPointerTo(AS), Name); +} + static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF, const Expr *E, const VarDecl *VD) { assert((VD->hasExternalStorage() || VD->isFileVarDecl()) && @@ -1282,8 +1290,11 @@ static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF, llvm::Value *V = CGF.CGM.GetAddrOfGlobalVar(VD); if (VD->getType()->isReferenceType()) V = CGF.Builder.CreateLoad(V, "tmp"); - unsigned Alignment = CGF.getContext().getDeclAlign(VD).getQuantity(); + V = EmitBitCastOfLValueToProperType(CGF.Builder, V, + CGF.getTypes().ConvertTypeForMem(E->getType())); + + unsigned Alignment = CGF.getContext().getDeclAlign(VD).getQuantity(); LValue LV = CGF.MakeAddrLValue(V, E->getType(), Alignment); setObjCGCLValueClass(CGF.getContext(), E, LV); return LV; @@ -1339,6 +1350,9 @@ LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) { if (VD->getType()->isReferenceType()) V = Builder.CreateLoad(V, "tmp"); + V = EmitBitCastOfLValueToProperType(Builder, V, + getTypes().ConvertTypeForMem(E->getType())); + LValue LV = MakeAddrLValue(V, E->getType(), Alignment); if (NonGCable) { LV.getQuals().removeObjCGCAttr(); @@ -1836,11 +1850,9 @@ LValue CodeGenFunction::EmitLValueForField(llvm::Value *baseAddr, // for both unions and structs. A union needs a bitcast, a struct element // will need a bitcast if the LLVM type laid out doesn't match the desired // type. - const llvm::Type *llvmType = CGM.getTypes().ConvertTypeForMem(type); - unsigned AS = cast(baseAddr->getType())->getAddressSpace(); - addr = Builder.CreateBitCast(addr, llvmType->getPointerTo(AS), - field->getName()); - + addr = EmitBitCastOfLValueToProperType(Builder, addr, + CGM.getTypes().ConvertTypeForMem(type), + field->getName()); unsigned alignment = getContext().getDeclAlign(field).getQuantity(); LValue LV = MakeAddrLValue(addr, type, alignment); -- cgit v1.2.3