r315984 - [CodeGen] EmitPointerWithAlignment() to generate TBAA info along with LValue base info
Yung, Douglas via cfe-commits
cfe-commits at lists.llvm.org
Wed Oct 18 10:24:17 PDT 2017
Hi Ivan,
This change caused a compiler crash in one of our tests. I have put the details in PR34992, can you take a look?
Douglas Yung
> -----Original Message-----
> From: cfe-commits [mailto:cfe-commits-bounces at lists.llvm.org] On Behalf Of
> Ivan A. Kosarev via cfe-commits
> Sent: Tuesday, October 17, 2017 2:12
> To: cfe-commits at lists.llvm.org
> Subject: r315984 - [CodeGen] EmitPointerWithAlignment() to generate TBAA info
> along with LValue base info
>
> Author: kosarev
> Date: Tue Oct 17 02:12:13 2017
> New Revision: 315984
>
> URL: http://llvm.org/viewvc/llvm-project?rev=315984&view=rev
> Log:
> [CodeGen] EmitPointerWithAlignment() to generate TBAA info along with LValue
> base info
>
> Differential Revision: https://reviews.llvm.org/D38796
>
> Added:
> cfe/trunk/test/CodeGen/tbaa-cast.cpp
> Modified:
> cfe/trunk/lib/CodeGen/CGExpr.cpp
> cfe/trunk/lib/CodeGen/CodeGenFunction.cpp
> cfe/trunk/lib/CodeGen/CodeGenFunction.h
> cfe/trunk/lib/CodeGen/CodeGenModule.cpp
> cfe/trunk/lib/CodeGen/CodeGenModule.h
> cfe/trunk/lib/CodeGen/CodeGenTBAA.cpp
> cfe/trunk/lib/CodeGen/CodeGenTBAA.h
>
> Modified: cfe/trunk/lib/CodeGen/CGExpr.cpp
> URL: http://llvm.org/viewvc/llvm-
> project/cfe/trunk/lib/CodeGen/CGExpr.cpp?rev=315984&r1=315983&r2=315984&view=d
> iff
> ==============================================================================
> --- cfe/trunk/lib/CodeGen/CGExpr.cpp (original)
> +++ cfe/trunk/lib/CodeGen/CGExpr.cpp Tue Oct 17 02:12:13 2017
> @@ -916,7 +916,8 @@ void CodeGenModule::EmitExplicitCastExpr
> /// EmitPointerWithAlignment - Given an expression of pointer type, try to
> /// derive a more accurate bound on the alignment of the pointer.
> Address CodeGenFunction::EmitPointerWithAlignment(const Expr *E,
> - LValueBaseInfo *BaseInfo) {
> + LValueBaseInfo *BaseInfo,
> + TBAAAccessInfo
> + *TBAAInfo) {
> // We allow this with ObjC object pointers because of fragile ABIs.
> assert(E->getType()->isPointerType() ||
> E->getType()->isObjCObjectPointerType());
> @@ -936,20 +937,30 @@ Address CodeGenFunction::EmitPointerWith
> if (PtrTy->getPointeeType()->isVoidType())
> break;
>
> - LValueBaseInfo InnerInfo;
> - Address Addr = EmitPointerWithAlignment(CE->getSubExpr(),
> &InnerInfo);
> - if (BaseInfo) *BaseInfo = InnerInfo;
> -
> - // If this is an explicit bitcast, and the source l-value is
> - // opaque, honor the alignment of the casted-to type.
> - if (isa<ExplicitCastExpr>(CE) &&
> - InnerInfo.getAlignmentSource() != AlignmentSource::Decl) {
> - LValueBaseInfo ExpInfo;
> + LValueBaseInfo InnerBaseInfo;
> + TBAAAccessInfo InnerTBAAInfo;
> + Address Addr = EmitPointerWithAlignment(CE->getSubExpr(),
> + &InnerBaseInfo,
> + &InnerTBAAInfo);
> + if (BaseInfo) *BaseInfo = InnerBaseInfo;
> + if (TBAAInfo) *TBAAInfo = InnerTBAAInfo;
> +
> + if (isa<ExplicitCastExpr>(CE)) {
> + LValueBaseInfo TargetTypeBaseInfo;
> + TBAAAccessInfo TargetTypeTBAAInfo;
> CharUnits Align = getNaturalPointeeTypeAlignment(E->getType(),
> - &ExpInfo);
> - if (BaseInfo)
> - BaseInfo->mergeForCast(ExpInfo);
> - Addr = Address(Addr.getPointer(), Align);
> +
> &TargetTypeBaseInfo,
> +
> &TargetTypeTBAAInfo);
> + if (TBAAInfo)
> + *TBAAInfo = CGM.mergeTBAAInfoForCast(*TBAAInfo,
> + TargetTypeTBAAInfo);
> + // If the source l-value is opaque, honor the alignment of the
> + // casted-to type.
> + if (InnerBaseInfo.getAlignmentSource() != AlignmentSource::Decl) {
> + if (BaseInfo)
> + BaseInfo->mergeForCast(TargetTypeBaseInfo);
> + Addr = Address(Addr.getPointer(), Align);
> + }
> }
>
> if (SanOpts.has(SanitizerKind::CFIUnrelatedCast) && @@ -969,12
> +980,13 @@ Address CodeGenFunction::EmitPointerWith
>
> // Array-to-pointer decay.
> case CK_ArrayToPointerDecay:
> - return EmitArrayToPointerDecay(CE->getSubExpr(), BaseInfo);
> + return EmitArrayToPointerDecay(CE->getSubExpr(), BaseInfo,
> + TBAAInfo);
>
> // Derived-to-base conversions.
> case CK_UncheckedDerivedToBase:
> case CK_DerivedToBase: {
> - Address Addr = EmitPointerWithAlignment(CE->getSubExpr(), BaseInfo);
> + Address Addr = EmitPointerWithAlignment(CE->getSubExpr(), BaseInfo,
> + TBAAInfo);
> auto Derived = CE->getSubExpr()->getType()->getPointeeCXXRecordDecl();
> return GetAddressOfBaseClass(Addr, Derived,
> CE->path_begin(), CE->path_end(), @@ -
> 994,6 +1006,7 @@ Address CodeGenFunction::EmitPointerWith
> if (UO->getOpcode() == UO_AddrOf) {
> LValue LV = EmitLValue(UO->getSubExpr());
> if (BaseInfo) *BaseInfo = LV.getBaseInfo();
> + if (TBAAInfo) *TBAAInfo = LV.getTBAAInfo();
> return LV.getAddress();
> }
> }
> @@ -1001,7 +1014,8 @@ Address CodeGenFunction::EmitPointerWith
> // TODO: conditional operators, comma.
>
> // Otherwise, use the alignment of the type.
> - CharUnits Align = getNaturalPointeeTypeAlignment(E->getType(), BaseInfo);
> + CharUnits Align = getNaturalPointeeTypeAlignment(E->getType(), BaseInfo,
> + TBAAInfo);
> return Address(EmitScalarExpr(E), Align); }
>
> @@ -2447,8 +2461,10 @@ LValue CodeGenFunction::EmitUnaryOpLValu
> assert(!T.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal
> type");
>
> LValueBaseInfo BaseInfo;
> - Address Addr = EmitPointerWithAlignment(E->getSubExpr(), &BaseInfo);
> - LValue LV = MakeAddrLValue(Addr, T, BaseInfo, CGM.getTBAAAccessInfo(T));
> + TBAAAccessInfo TBAAInfo;
> + Address Addr = EmitPointerWithAlignment(E->getSubExpr(), &BaseInfo,
> + &TBAAInfo);
> + LValue LV = MakeAddrLValue(Addr, T, BaseInfo, TBAAInfo);
> LV.getQuals().setAddressSpace(ExprTy.getAddressSpace());
>
> // We should not generate __weak write barrier on indirect reference @@ -
> 3048,7 +3064,8 @@ llvm::CallInst *CodeGenFunction::EmitTra }
>
> Address CodeGenFunction::EmitArrayToPointerDecay(const Expr *E,
> - LValueBaseInfo *BaseInfo) {
> + LValueBaseInfo *BaseInfo,
> + TBAAAccessInfo
> + *TBAAInfo) {
> assert(E->getType()->isArrayType() &&
> "Array to pointer decay must have array source type!");
>
> @@ -3056,6 +3073,7 @@ Address CodeGenFunction::EmitArrayToPoin
> LValue LV = EmitLValue(E);
> Address Addr = LV.getAddress();
> if (BaseInfo) *BaseInfo = LV.getBaseInfo();
> + if (TBAAInfo) *TBAAInfo = LV.getTBAAInfo();
>
> // If the array type was an incomplete type, we need to make sure
> // the decay ends up being the right type.
> @@ -3216,13 +3234,14 @@ LValue CodeGenFunction::EmitArraySubscri
> }
>
> LValueBaseInfo BaseInfo;
> + TBAAAccessInfo TBAAInfo;
> Address Addr = Address::invalid();
> if (const VariableArrayType *vla =
> getContext().getAsVariableArrayType(E->getType())) {
> // The base must be a pointer, which is not an aggregate. Emit
> // it. It needs to be emitted first in case it's what captures
> // the VLA bounds.
> - Addr = EmitPointerWithAlignment(E->getBase(), &BaseInfo);
> + Addr = EmitPointerWithAlignment(E->getBase(), &BaseInfo,
> + &TBAAInfo);
> auto *Idx = EmitIdxAfterBase(/*Promote*/true);
>
> // The element count here is the total number of non-VLA elements.
> @@ -3246,7 +3265,7 @@ LValue CodeGenFunction::EmitArraySubscri
> // Indexing over an interface, as in "NSString *P; P[4];"
>
> // Emit the base pointer.
> - Addr = EmitPointerWithAlignment(E->getBase(), &BaseInfo);
> + Addr = EmitPointerWithAlignment(E->getBase(), &BaseInfo,
> + &TBAAInfo);
> auto *Idx = EmitIdxAfterBase(/*Promote*/true);
>
> CharUnits InterfaceSize = getContext().getTypeSizeInChars(OIT);
> @@ -3294,19 +3313,17 @@ LValue CodeGenFunction::EmitArraySubscri
> E->getType(), !getLangOpts().isSignedOverflowDefined(),
> SignedIndices,
> E->getExprLoc());
> BaseInfo = ArrayLV.getBaseInfo();
> + TBAAInfo = CGM.getTBAAAccessInfo(E->getType());
> } else {
> // The base must be a pointer; emit it with an estimate of its alignment.
> - Addr = EmitPointerWithAlignment(E->getBase(), &BaseInfo);
> + Addr = EmitPointerWithAlignment(E->getBase(), &BaseInfo,
> + &TBAAInfo);
> auto *Idx = EmitIdxAfterBase(/*Promote*/true);
> Addr = emitArraySubscriptGEP(*this, Addr, Idx, E->getType(),
> !getLangOpts().isSignedOverflowDefined(),
> SignedIndices, E->getExprLoc());
> }
>
> - LValue LV = MakeAddrLValue(Addr, E->getType(), BaseInfo,
> - CGM.getTBAAAccessInfo(E->getType()));
> -
> - // TODO: Preserve/extend path TBAA metadata?
> + LValue LV = MakeAddrLValue(Addr, E->getType(), BaseInfo, TBAAInfo);
>
> if (getLangOpts().ObjC1 &&
> getLangOpts().getGC() != LangOptions::NonGC) { @@ -3321,8 +3338,6 @@
> static Address emitOMPArraySectionBase(C
> TBAAAccessInfo &TBAAInfo,
> QualType BaseTy, QualType ElTy,
> bool IsLowerBound) {
> - TBAAInfo = CGF.CGM.getTBAAAccessInfo(ElTy);
> -
> LValue BaseLVal;
> if (auto *ASE = dyn_cast<OMPArraySectionExpr>(Base->IgnoreParenImpCasts()))
> {
> BaseLVal = CGF.EmitOMPArraySectionExpr(ASE, IsLowerBound); @@ -3352,7
> +3367,7 @@ static Address emitOMPArraySectionBase(C
> BaseInfo.mergeForCast(TypeInfo);
> return Address(CGF.Builder.CreateLoad(BaseLVal.getAddress()), Align);
> }
> - return CGF.EmitPointerWithAlignment(Base, &BaseInfo);
> + return CGF.EmitPointerWithAlignment(Base, &BaseInfo, &TBAAInfo);
> }
>
> LValue CodeGenFunction::EmitOMPArraySectionExpr(const OMPArraySectionExpr *E,
> @@ -3518,10 +3533,10 @@ EmitExtVectorElementExpr(const ExtVector
> // If it is a pointer to a vector, emit the address and form an lvalue
> with
> // it.
> LValueBaseInfo BaseInfo;
> - Address Ptr = EmitPointerWithAlignment(E->getBase(), &BaseInfo);
> + TBAAAccessInfo TBAAInfo;
> + Address Ptr = EmitPointerWithAlignment(E->getBase(), &BaseInfo,
> + &TBAAInfo);
> const PointerType *PT = E->getBase()->getType()->getAs<PointerType>();
> - Base = MakeAddrLValue(Ptr, PT->getPointeeType(), BaseInfo,
> - CGM.getTBAAAccessInfo(PT->getPointeeType()));
> + Base = MakeAddrLValue(Ptr, PT->getPointeeType(), BaseInfo,
> + TBAAInfo);
> Base.getQuals().removeObjCGCAttr();
> } else if (E->getBase()->isGLValue()) {
> // Otherwise, if the base is an lvalue ( as in the case of foo.x.x), @@ -
> 3577,7 +3592,8 @@ LValue CodeGenFunction::EmitMemberExpr(c
> LValue BaseLV;
> if (E->isArrow()) {
> LValueBaseInfo BaseInfo;
> - Address Addr = EmitPointerWithAlignment(BaseExpr, &BaseInfo);
> + TBAAAccessInfo TBAAInfo;
> + Address Addr = EmitPointerWithAlignment(BaseExpr, &BaseInfo,
> + &TBAAInfo);
> QualType PtrTy = BaseExpr->getType()->getPointeeType();
> SanitizerSet SkippedChecks;
> bool IsBaseCXXThis = IsWrappedCXXThis(BaseExpr); @@ -3587,8 +3603,7 @@
> LValue CodeGenFunction::EmitMemberExpr(c
> SkippedChecks.set(SanitizerKind::Null, true);
> EmitTypeCheck(TCK_MemberAccess, E->getExprLoc(), Addr.getPointer(),
> PtrTy,
> /*Alignment=*/CharUnits::Zero(), SkippedChecks);
> - BaseLV = MakeAddrLValue(Addr, PtrTy, BaseInfo,
> - CGM.getTBAAAccessInfo(PtrTy));
> + BaseLV = MakeAddrLValue(Addr, PtrTy, BaseInfo, TBAAInfo);
> } else
> BaseLV = EmitCheckedLValue(BaseExpr, TCK_MemberAccess);
>
>
> Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.cpp
> URL: http://llvm.org/viewvc/llvm-
> project/cfe/trunk/lib/CodeGen/CodeGenFunction.cpp?rev=315984&r1=315983&r2=3159
> 84&view=diff
> ==============================================================================
> --- cfe/trunk/lib/CodeGen/CodeGenFunction.cpp (original)
> +++ cfe/trunk/lib/CodeGen/CodeGenFunction.cpp Tue Oct 17 02:12:13 2017
> @@ -118,9 +118,9 @@ CodeGenFunction::~CodeGenFunction() { }
>
> CharUnits CodeGenFunction::getNaturalPointeeTypeAlignment(QualType T,
> - LValueBaseInfo *BaseInfo)
> {
> - return getNaturalTypeAlignment(T->getPointeeType(), BaseInfo,
> - /* TBAAInfo= */ nullptr,
> + LValueBaseInfo *BaseInfo,
> + TBAAAccessInfo
> + *TBAAInfo) { return getNaturalTypeAlignment(T->getPointeeType(),
> + BaseInfo, TBAAInfo,
> /* forPointeeType= */ true); }
>
>
> Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.h
> URL: http://llvm.org/viewvc/llvm-
> project/cfe/trunk/lib/CodeGen/CodeGenFunction.h?rev=315984&r1=315983&r2=315984
> &view=diff
> ==============================================================================
> --- cfe/trunk/lib/CodeGen/CodeGenFunction.h (original)
> +++ cfe/trunk/lib/CodeGen/CodeGenFunction.h Tue Oct 17 02:12:13 2017
> @@ -1949,7 +1949,8 @@ public:
> TBAAAccessInfo *TBAAInfo = nullptr,
> bool forPointeeType = false);
> CharUnits getNaturalPointeeTypeAlignment(QualType T,
> - LValueBaseInfo *BaseInfo =
> nullptr);
> + LValueBaseInfo *BaseInfo =
> nullptr,
> + TBAAAccessInfo *TBAAInfo =
> + nullptr);
>
> Address EmitLoadOfReference(Address Ref, const ReferenceType *RefTy,
> LValueBaseInfo *BaseInfo = nullptr, @@ -3198,7
> +3199,8 @@ public:
> RValue EmitRValueForField(LValue LV, const FieldDecl *FD, SourceLocation
> Loc);
>
> Address EmitArrayToPointerDecay(const Expr *Array,
> - LValueBaseInfo *BaseInfo = nullptr);
> + LValueBaseInfo *BaseInfo = nullptr,
> + TBAAAccessInfo *TBAAInfo = nullptr);
>
> class ConstantEmission {
> llvm::PointerIntPair<llvm::Constant*, 1, bool> ValueAndIsReference; @@ -
> 3920,7 +3922,8 @@ public:
> /// reasonable to just ignore the returned alignment when it isn't from an
> /// explicit source.
> Address EmitPointerWithAlignment(const Expr *Addr,
> - LValueBaseInfo *BaseInfo = nullptr);
> + LValueBaseInfo *BaseInfo = nullptr,
> + TBAAAccessInfo *TBAAInfo = nullptr);
>
> void EmitSanitizerStatReport(llvm::SanitizerStatKind SSK);
>
>
> Modified: cfe/trunk/lib/CodeGen/CodeGenModule.cpp
> URL: http://llvm.org/viewvc/llvm-
> project/cfe/trunk/lib/CodeGen/CodeGenModule.cpp?rev=315984&r1=315983&r2=315984
> &view=diff
> ==============================================================================
> --- cfe/trunk/lib/CodeGen/CodeGenModule.cpp (original)
> +++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp Tue Oct 17 02:12:13 2017
> @@ -612,6 +612,13 @@ TBAAAccessInfo CodeGenModule::getTBAAMay
> return TBAA->getMayAliasAccessInfo(); }
>
> +TBAAAccessInfo CodeGenModule::mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo,
> + TBAAAccessInfo
> +TargetInfo) {
> + if (!TBAA)
> + return TBAAAccessInfo();
> + return TBAA->mergeTBAAInfoForCast(SourceInfo, TargetInfo); }
> +
> void CodeGenModule::DecorateInstructionWithTBAA(llvm::Instruction *Inst,
> TBAAAccessInfo TBAAInfo) {
> if (llvm::MDNode *Tag = getTBAAAccessTagInfo(TBAAInfo))
>
> Modified: cfe/trunk/lib/CodeGen/CodeGenModule.h
> URL: http://llvm.org/viewvc/llvm-
> project/cfe/trunk/lib/CodeGen/CodeGenModule.h?rev=315984&r1=315983&r2=315984&v
> iew=diff
> ==============================================================================
> --- cfe/trunk/lib/CodeGen/CodeGenModule.h (original)
> +++ cfe/trunk/lib/CodeGen/CodeGenModule.h Tue Oct 17 02:12:13 2017
> @@ -677,6 +677,11 @@ public:
> /// may-alias accesses.
> TBAAAccessInfo getTBAAMayAliasAccessInfo();
>
> + /// mergeTBAAInfoForCast - Get merged TBAA information for the
> + purposes of /// type casts.
> + TBAAAccessInfo mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo,
> + TBAAAccessInfo TargetInfo);
> +
> bool isTypeConstant(QualType QTy, bool ExcludeCtorDtor);
>
> bool isPaddedAtomicType(QualType type);
>
> Modified: cfe/trunk/lib/CodeGen/CodeGenTBAA.cpp
> URL: http://llvm.org/viewvc/llvm-
> project/cfe/trunk/lib/CodeGen/CodeGenTBAA.cpp?rev=315984&r1=315983&r2=315984&v
> iew=diff
> ==============================================================================
> --- cfe/trunk/lib/CodeGen/CodeGenTBAA.cpp (original)
> +++ cfe/trunk/lib/CodeGen/CodeGenTBAA.cpp Tue Oct 17 02:12:13 2017
> @@ -309,3 +309,11 @@ llvm::MDNode *CodeGenTBAA::getAccessTagI TBAAAccessInfo
> CodeGenTBAA::getMayAliasAccessInfo() {
> return TBAAAccessInfo(getChar());
> }
> +
> +TBAAAccessInfo CodeGenTBAA::mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo,
> + TBAAAccessInfo
> +TargetInfo) {
> + TBAAAccessInfo MayAliasInfo = getMayAliasAccessInfo();
> + if (SourceInfo == MayAliasInfo || TargetInfo == MayAliasInfo)
> + return MayAliasInfo;
> + return TargetInfo;
> +}
>
> Modified: cfe/trunk/lib/CodeGen/CodeGenTBAA.h
> URL: http://llvm.org/viewvc/llvm-
> project/cfe/trunk/lib/CodeGen/CodeGenTBAA.h?rev=315984&r1=315983&r2=315984&vie
> w=diff
> ==============================================================================
> --- cfe/trunk/lib/CodeGen/CodeGenTBAA.h (original)
> +++ cfe/trunk/lib/CodeGen/CodeGenTBAA.h Tue Oct 17 02:12:13 2017
> @@ -47,6 +47,12 @@ struct TBAAAccessInfo {
> : TBAAAccessInfo(/* AccessType= */ nullptr)
> {}
>
> + bool operator==(const TBAAAccessInfo &Other) const {
> + return BaseType == Other.BaseType &&
> + AccessType == Other.AccessType &&
> + Offset == Other.Offset;
> + }
> +
> /// BaseType - The base/leading access type. May be null if this access
> /// descriptor represents an access that is not considered to be an access
> /// to an aggregate or union member.
> @@ -136,6 +142,11 @@ public:
> /// getMayAliasAccessInfo - Get TBAA information that represents may-alias
> /// accesses.
> TBAAAccessInfo getMayAliasAccessInfo();
> +
> + /// mergeTBAAInfoForCast - Get merged TBAA information for the
> + purpose of /// type casts.
> + TBAAAccessInfo mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo,
> + TBAAAccessInfo TargetInfo);
> };
>
> } // end namespace CodeGen
>
> Added: cfe/trunk/test/CodeGen/tbaa-cast.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/tbaa-
> cast.cpp?rev=315984&view=auto
> ==============================================================================
> --- cfe/trunk/test/CodeGen/tbaa-cast.cpp (added)
> +++ cfe/trunk/test/CodeGen/tbaa-cast.cpp Tue Oct 17 02:12:13 2017
> @@ -0,0 +1,23 @@
> +// RUN: %clang_cc1 -triple x86_64-linux -O1 -disable-llvm-passes %s \
> +// RUN: -emit-llvm -o - | FileCheck %s
> +//
> +// Check that we generate correct TBAA information for lvalues
> +constructed // with use of casts.
> +
> +struct V {
> + unsigned n;
> +};
> +
> +struct S {
> + char bytes[4];
> +};
> +
> +void foo(S *p) {
> +// CHECK-LABEL: _Z3fooP1S
> +// CHECK: store i32 5, {{.*}}, !tbaa [[TAG_V_n:!.*]]
> + ((V*)p->bytes)->n = 5;
> +}
> +
> +// CHECK-DAG: [[TAG_V_n]] = !{[[TYPE_V:!.*]], [[TYPE_int:!.*]], i64 0}
> +// CHECK-DAG: [[TYPE_V]] = !{!"_ZTS1V", !{{.*}}, i64 0} // CHECK-DAG:
> +[[TYPE_int]] = !{!"int", !{{.*}}, i64 0}
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
More information about the cfe-commits
mailing list