From: Eli Friedman Date: Sat, 3 Dec 2011 04:14:32 +0000 (+0000) Subject: Switch LValue so that it exposes alignment in CharUnits. (No functional change.) X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6da2c716017d5c8530ec99779524491ebc5dadb8;p=clang Switch LValue so that it exposes alignment in CharUnits. (No functional change.) git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@145753 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/CodeGen/CGBlocks.cpp b/lib/CodeGen/CGBlocks.cpp index e713492cc0..8fc4344201 100644 --- a/lib/CodeGen/CGBlocks.cpp +++ b/lib/CodeGen/CGBlocks.cpp @@ -745,8 +745,7 @@ llvm::Value *CodeGenFunction::EmitBlockLiteral(const CGBlockInfo &blockInfo) { declRef, VK_RValue); EmitExprAsInit(&l2r, &blockFieldPseudoVar, MakeAddrLValue(blockField, type, - getContext().getDeclAlign(variable) - .getQuantity()), + getContext().getDeclAlign(variable)), /*captured by init*/ false); } diff --git a/lib/CodeGen/CGCall.cpp b/lib/CodeGen/CGCall.cpp index 93bc1753ce..3457b5bc14 100644 --- a/lib/CodeGen/CGCall.cpp +++ b/lib/CodeGen/CGCall.cpp @@ -1060,8 +1060,9 @@ void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI, // we need to create a temporary and reconstruct it from the // arguments. llvm::AllocaInst *Alloca = CreateMemTemp(Ty); - Alloca->setAlignment(getContext().getDeclAlign(Arg).getQuantity()); - LValue LV = MakeAddrLValue(Alloca, Ty, Alloca->getAlignment()); + CharUnits Align = getContext().getDeclAlign(Arg); + Alloca->setAlignment(Align.getQuantity()); + LValue LV = MakeAddrLValue(Alloca, Ty, Align); llvm::Function::arg_iterator End = ExpandTypeFromArgs(Ty, LV, AI); EmitParmDecl(*Arg, Alloca, ArgNo); diff --git a/lib/CodeGen/CGClass.cpp b/lib/CodeGen/CGClass.cpp index eabc201cc4..8a305fde2d 100644 --- a/lib/CodeGen/CGClass.cpp +++ b/lib/CodeGen/CGClass.cpp @@ -434,7 +434,7 @@ static void EmitAggMemberInitializer(CodeGenFunction &CGF, // Update the LValue. LV.setAddress(Dest); - unsigned Align = CGF.getContext().getTypeAlignInChars(T).getQuantity(); + CharUnits Align = CGF.getContext().getTypeAlignInChars(T); LV.setAlignment(std::min(Align, LV.getAlignment())); } diff --git a/lib/CodeGen/CGDecl.cpp b/lib/CodeGen/CGDecl.cpp index c7bc87ba1e..ff85fc5688 100644 --- a/lib/CodeGen/CGDecl.cpp +++ b/lib/CodeGen/CGDecl.cpp @@ -972,7 +972,7 @@ void CodeGenFunction::EmitAutoVarInit(const AutoVarEmission &emission) { capturedByInit ? emission.Address : emission.getObjectAddress(*this); if (!emission.IsConstantAggregate) { - LValue lv = MakeAddrLValue(Loc, type, alignment.getQuantity()); + LValue lv = MakeAddrLValue(Loc, type, alignment); lv.setNonGC(true); return EmitExprAsInit(Init, &D, lv, capturedByInit); } @@ -1505,7 +1505,7 @@ void CodeGenFunction::EmitParmDecl(const VarDecl &D, llvm::Value *Arg, // Store the initial value into the alloca. if (doStore) { LValue lv = MakeAddrLValue(DeclPtr, Ty, - getContext().getDeclAlign(&D).getQuantity()); + getContext().getDeclAlign(&D)); EmitStoreOfScalar(Arg, lv); } } diff --git a/lib/CodeGen/CGDeclCXX.cpp b/lib/CodeGen/CGDeclCXX.cpp index 3b8f830278..2f32381c5e 100644 --- a/lib/CodeGen/CGDeclCXX.cpp +++ b/lib/CodeGen/CGDeclCXX.cpp @@ -28,7 +28,7 @@ static void EmitDeclInit(CodeGenFunction &CGF, const VarDecl &D, ASTContext &Context = CGF.getContext(); - unsigned alignment = Context.getDeclAlign(&D).getQuantity(); + CharUnits alignment = Context.getDeclAlign(&D); QualType type = D.getType(); LValue lv = CGF.MakeAddrLValue(DeclPtr, type, alignment); diff --git a/lib/CodeGen/CGExpr.cpp b/lib/CodeGen/CGExpr.cpp index a8c134b2ae..2ed7b1ad81 100644 --- a/lib/CodeGen/CGExpr.cpp +++ b/lib/CodeGen/CGExpr.cpp @@ -750,8 +750,8 @@ LValue CodeGenFunction::EmitLValue(const Expr *E) { llvm::Value *CodeGenFunction::EmitLoadOfScalar(LValue lvalue) { return EmitLoadOfScalar(lvalue.getAddress(), lvalue.isVolatile(), - lvalue.getAlignment(), lvalue.getType(), - lvalue.getTBAAInfo()); + lvalue.getAlignment().getQuantity(), + lvalue.getType(), lvalue.getTBAAInfo()); } llvm::Value *CodeGenFunction::EmitLoadOfScalar(llvm::Value *Addr, bool Volatile, @@ -812,7 +812,7 @@ void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr, void CodeGenFunction::EmitStoreOfScalar(llvm::Value *value, LValue lvalue) { EmitStoreOfScalar(value, lvalue.getAddress(), lvalue.isVolatile(), - lvalue.getAlignment(), lvalue.getType(), + lvalue.getAlignment().getQuantity(), lvalue.getType(), lvalue.getTBAAInfo()); } @@ -1335,12 +1335,12 @@ static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF, llvm::Value *V = CGF.CGM.GetAddrOfGlobalVar(VD); llvm::Type *RealVarTy = CGF.getTypes().ConvertTypeForMem(VD->getType()); V = EmitBitCastOfLValueToProperType(CGF, V, RealVarTy); - unsigned Alignment = CGF.getContext().getDeclAlign(VD).getQuantity(); + CharUnits Alignment = CGF.getContext().getDeclAlign(VD); QualType T = E->getType(); LValue LV; if (VD->getType()->isReferenceType()) { llvm::LoadInst *LI = CGF.Builder.CreateLoad(V); - LI->setAlignment(Alignment); + LI->setAlignment(Alignment.getQuantity()); V = LI; LV = CGF.MakeNaturalAlignAddrLValue(V, T); } else { @@ -1365,13 +1365,13 @@ static LValue EmitFunctionDeclLValue(CodeGenFunction &CGF, V = CGF.Builder.CreateBitCast(V, CGF.ConvertType(NoProtoType)); } } - unsigned Alignment = CGF.getContext().getDeclAlign(FD).getQuantity(); + CharUnits Alignment = CGF.getContext().getDeclAlign(FD); return CGF.MakeAddrLValue(V, E->getType(), Alignment); } LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) { const NamedDecl *ND = E->getDecl(); - unsigned Alignment = getContext().getDeclAlign(ND).getQuantity(); + CharUnits Alignment = getContext().getDeclAlign(ND); QualType T = E->getType(); if (ND->hasAttr()) { @@ -1401,7 +1401,7 @@ LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) { LValue LV; if (VD->getType()->isReferenceType()) { llvm::LoadInst *LI = Builder.CreateLoad(V); - LI->setAlignment(Alignment); + LI->setAlignment(Alignment.getQuantity()); V = LI; LV = MakeNaturalAlignAddrLValue(V, T); } else { @@ -1427,8 +1427,7 @@ LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) { } LValue CodeGenFunction::EmitBlockDeclRefLValue(const BlockDeclRefExpr *E) { - unsigned Alignment = - getContext().getDeclAlign(E->getDecl()).getQuantity(); + CharUnits Alignment = getContext().getDeclAlign(E->getDecl()); return MakeAddrLValue(GetAddrOfBlockDecl(E), E->getType(), Alignment); } @@ -1636,7 +1635,7 @@ LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) { // We know that the pointer points to a type of the correct size, unless the // size is a VLA or Objective-C interface. llvm::Value *Address = 0; - unsigned ArrayAlignment = 0; + CharUnits ArrayAlignment; if (const VariableArrayType *vla = getContext().getAsVariableArrayType(E->getType())) { // The base must be a pointer, which is not an aggregate. Emit @@ -1704,8 +1703,8 @@ LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) { "CodeGenFunction::EmitArraySubscriptExpr(): Illegal base type"); // Limit the alignment to that of the result type. - if (ArrayAlignment) { - unsigned Align = getContext().getTypeAlignInChars(T).getQuantity(); + if (!ArrayAlignment.isZero()) { + CharUnits Align = getContext().getTypeAlignInChars(T); ArrayAlignment = std::min(Align, ArrayAlignment); } @@ -1866,7 +1865,7 @@ LValue CodeGenFunction::EmitLValueForField(llvm::Value *baseAddr, const RecordDecl *rec = field->getParent(); QualType type = field->getType(); - unsigned alignment = getContext().getDeclAlign(field).getQuantity(); + CharUnits alignment = getContext().getDeclAlign(field); bool mayAlias = rec->hasAttr(); @@ -1883,7 +1882,7 @@ LValue CodeGenFunction::EmitLValueForField(llvm::Value *baseAddr, if (const ReferenceType *refType = type->getAs()) { llvm::LoadInst *load = Builder.CreateLoad(addr, "ref"); if (cvr & Qualifiers::Volatile) load->setVolatile(true); - load->setAlignment(alignment); + load->setAlignment(alignment.getQuantity()); if (CGM.shouldUseTBAA()) { llvm::MDNode *tbaa; @@ -1898,9 +1897,9 @@ LValue CodeGenFunction::EmitLValueForField(llvm::Value *baseAddr, mayAlias = false; type = refType->getPointeeType(); if (type->isIncompleteType()) - alignment = 0; + alignment = CharUnits(); else - alignment = getContext().getTypeAlignInChars(type).getQuantity(); + alignment = getContext().getTypeAlignInChars(type); cvr = 0; // qualifiers don't recursively apply to referencee } } @@ -1956,7 +1955,7 @@ CodeGenFunction::EmitLValueForFieldInitialization(llvm::Value *BaseValue, unsigned AS = cast(V->getType())->getAddressSpace(); V = Builder.CreateBitCast(V, llvmType->getPointerTo(AS)); - unsigned Alignment = getContext().getDeclAlign(Field).getQuantity(); + CharUnits Alignment = getContext().getDeclAlign(Field); return MakeAddrLValue(V, FieldType, Alignment); } diff --git a/lib/CodeGen/CGExprCXX.cpp b/lib/CodeGen/CGExprCXX.cpp index da1ca2ccf3..ed7605e34b 100644 --- a/lib/CodeGen/CGExprCXX.cpp +++ b/lib/CodeGen/CGExprCXX.cpp @@ -753,7 +753,7 @@ static void StoreAnyExprIntoOneUnit(CodeGenFunction &CGF, const CXXNewExpr *E, CharUnits Alignment = CGF.getContext().getTypeAlignInChars(AllocType); if (!CGF.hasAggregateLLVMType(AllocType)) CGF.EmitScalarInit(Init, 0, CGF.MakeAddrLValue(NewPtr, AllocType, - Alignment.getQuantity()), + Alignment), false); else if (AllocType->isAnyComplexType()) CGF.EmitComplexExprIntoAddr(Init, NewPtr, diff --git a/lib/CodeGen/CGObjC.cpp b/lib/CodeGen/CGObjC.cpp index 686f0e6833..4e5915a3ae 100644 --- a/lib/CodeGen/CGObjC.cpp +++ b/lib/CodeGen/CGObjC.cpp @@ -1700,7 +1700,8 @@ llvm::Value *CodeGenFunction::EmitARCStoreStrong(LValue dst, // lvalue is inadequately aligned. if (shouldUseFusedARCCalls() && !isBlock && - !(dst.getAlignment() && dst.getAlignment() < PointerAlignInBytes)) { + (dst.getAlignment().isZero() || + dst.getAlignment() >= CharUnits::fromQuantity(PointerAlignInBytes))) { return EmitARCStoreStrongCall(dst.getAddress(), newValue, ignored); } @@ -2416,12 +2417,8 @@ CodeGenFunction::EmitARCStoreStrong(const BinaryOperator *e, // If the RHS was emitted retained, expand this. if (hasImmediateRetain) { llvm::Value *oldValue = - EmitLoadOfScalar(lvalue.getAddress(), lvalue.isVolatileQualified(), - lvalue.getAlignment(), e->getType(), - lvalue.getTBAAInfo()); - EmitStoreOfScalar(value, lvalue.getAddress(), - lvalue.isVolatileQualified(), lvalue.getAlignment(), - e->getType(), lvalue.getTBAAInfo()); + EmitLoadOfScalar(lvalue); + EmitStoreOfScalar(value, lvalue); EmitARCRelease(oldValue, /*precise*/ false); } else { value = EmitARCStoreStrong(lvalue, value, ignored); @@ -2435,9 +2432,7 @@ CodeGenFunction::EmitARCStoreAutoreleasing(const BinaryOperator *e) { llvm::Value *value = EmitARCRetainAutoreleaseScalarExpr(e->getRHS()); LValue lvalue = EmitLValue(e->getLHS()); - EmitStoreOfScalar(value, lvalue.getAddress(), - lvalue.isVolatileQualified(), lvalue.getAlignment(), - e->getType(), lvalue.getTBAAInfo()); + EmitStoreOfScalar(value, lvalue); return std::pair(lvalue, value); } diff --git a/lib/CodeGen/CGValue.h b/lib/CodeGen/CGValue.h index fd85fb2489..b8fe2a25b4 100644 --- a/lib/CodeGen/CGValue.h +++ b/lib/CodeGen/CGValue.h @@ -151,12 +151,14 @@ class LValue { llvm::MDNode *TBAAInfo; private: - void Initialize(QualType Type, Qualifiers Quals, unsigned Alignment = 0, + void Initialize(QualType Type, Qualifiers Quals, + CharUnits Alignment = CharUnits(), llvm::MDNode *TBAAInfo = 0) { this->Type = Type; this->Quals = Quals; - this->Alignment = Alignment; - assert(this->Alignment == Alignment && "Alignment exceeds allowed max!"); + this->Alignment = Alignment.getQuantity(); + assert(this->Alignment == Alignment.getQuantity() && + "Alignment exceeds allowed max!"); // Initialize Objective-C flags. this->Ivar = this->ObjIsArray = this->NonGC = this->GlobalObjCRef = false; @@ -220,8 +222,8 @@ public: unsigned getAddressSpace() const { return Quals.getAddressSpace(); } - unsigned getAlignment() const { return Alignment; } - void setAlignment(unsigned A) { Alignment = A; } + CharUnits getAlignment() const { return CharUnits::fromQuantity(Alignment); } + void setAlignment(CharUnits A) { Alignment = A.getQuantity(); } // simple lvalue llvm::Value *getAddress() const { assert(isSimple()); return V; } @@ -252,7 +254,7 @@ public: } static LValue MakeAddr(llvm::Value *address, QualType type, - unsigned alignment, ASTContext &Context, + CharUnits alignment, ASTContext &Context, llvm::MDNode *TBAAInfo = 0) { Qualifiers qs = type.getQualifiers(); qs.setObjCGCAttr(Context.getObjCGCAttrKind(type)); @@ -393,7 +395,7 @@ public: NeedsGCBarriers_t needsGC, IsAliased_t isAliased, IsZeroed_t isZeroed = IsNotZeroed) { - return forAddr(LV.getAddress(), CharUnits::fromQuantity(LV.getAlignment()), + return forAddr(LV.getAddress(), LV.getAlignment(), LV.getQuals(), isDestructed, needsGC, isAliased, isZeroed); } diff --git a/lib/CodeGen/CodeGenFunction.h b/lib/CodeGen/CodeGenFunction.h index 74f48c3d80..d611996bd1 100644 --- a/lib/CodeGen/CodeGenFunction.h +++ b/lib/CodeGen/CodeGenFunction.h @@ -1537,16 +1537,15 @@ public: // Helpers //===--------------------------------------------------------------------===// - LValue MakeAddrLValue(llvm::Value *V, QualType T, unsigned Alignment = 0) { + LValue MakeAddrLValue(llvm::Value *V, QualType T, + CharUnits Alignment = CharUnits()) { return LValue::MakeAddr(V, T, Alignment, getContext(), CGM.getTBAAInfo(T)); } LValue MakeNaturalAlignAddrLValue(llvm::Value *V, QualType T) { - unsigned Alignment; - if (T->isIncompleteType()) - Alignment = 0; - else - Alignment = getContext().getTypeAlignInChars(T).getQuantity(); + CharUnits Alignment; + if (!T->isIncompleteType()) + Alignment = getContext().getTypeAlignInChars(T); return LValue::MakeAddr(V, T, Alignment, getContext(), CGM.getTBAAInfo(T)); }