From d7c59353bdd6936647ee52336d6d9e2f54111e02 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sun, 10 Jul 2011 05:53:24 +0000 Subject: [PATCH] enhance EmitLValueForFieldInitialization to do the proper pointer adjustment, allowing us to revert the other half of r134860. Now things are back to a relatively tidy state. llvm-svn: 134865 --- clang/lib/CodeGen/CGExpr.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp index 0a42a5f5190d..e1d93095bdfd 100644 --- a/clang/lib/CodeGen/CGExpr.cpp +++ b/clang/lib/CodeGen/CGExpr.cpp @@ -764,14 +764,6 @@ void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr, llvm::MDNode *TBAAInfo) { Value = EmitToMemory(Value, Ty); - // If this is a pointer r-value, make sure that it has the right scalar type. - if (isa(Value->getType())) { - llvm::Type *EltTy = - cast(Addr->getType())->getElementType(); - if (EltTy != Value->getType()) - Value = Builder.CreateBitCast(Value, EltTy); - } - llvm::StoreInst *Store = Builder.CreateStore(Value, Addr, Volatile); if (Alignment) Store->setAlignment(Alignment); @@ -1880,9 +1872,17 @@ CodeGenFunction::EmitLValueForFieldInitialization(llvm::Value *BaseValue, CGM.getTypes().getCGRecordLayout(Field->getParent()); unsigned idx = RL.getLLVMFieldNo(Field); llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp"); - assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs"); + + // Make sure that the address is pointing to the right type. This is critical + // 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 = ConvertTypeForMem(FieldType); + unsigned AS = cast(V->getType())->getAddressSpace(); + V = Builder.CreateBitCast(V, llvmType->getPointerTo(AS)); + unsigned Alignment = getContext().getDeclAlign(Field).getQuantity(); return MakeAddrLValue(V, FieldType, Alignment); }