diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h --- a/clang/include/clang/AST/ASTContext.h +++ b/clang/include/clang/AST/ASTContext.h @@ -164,24 +164,43 @@ template class AbstractTypeReader; } // namespace serialization +enum class AlignRequirementKind { + /// The alignment was not explicit in code. + None, + + /// The alignment comes from an alignment attribute on a typedef. + RequiredByTypedef, + + /// The alignment comes from an alignment attribute on a record type. + RequiredByRecord, +}; + struct TypeInfo { uint64_t Width = 0; unsigned Align = 0; - bool AlignIsRequired : 1; + AlignRequirementKind AlignRequirement; - TypeInfo() : AlignIsRequired(false) {} - TypeInfo(uint64_t Width, unsigned Align, bool AlignIsRequired) - : Width(Width), Align(Align), AlignIsRequired(AlignIsRequired) {} + TypeInfo() : AlignRequirement(AlignRequirementKind::None) {} + TypeInfo(uint64_t Width, unsigned Align, + AlignRequirementKind AlignRequirement) + : Width(Width), Align(Align), AlignRequirement(AlignRequirement) {} + bool isAlignRequired() { + return AlignRequirement != AlignRequirementKind::None; + } }; struct TypeInfoChars { CharUnits Width; CharUnits Align; - bool AlignIsRequired : 1; + AlignRequirementKind AlignRequirement; - TypeInfoChars() : AlignIsRequired(false) {} - TypeInfoChars(CharUnits Width, CharUnits Align, bool AlignIsRequired) - : Width(Width), Align(Align), AlignIsRequired(AlignIsRequired) {} + TypeInfoChars() : AlignRequirement(AlignRequirementKind::None) {} + TypeInfoChars(CharUnits Width, CharUnits Align, + AlignRequirementKind AlignRequirement) + : Width(Width), Align(Align), AlignRequirement(AlignRequirement) {} + bool isAlignRequired() { + return AlignRequirement != AlignRequirementKind::None; + } }; /// Holds long-lived AST nodes (such as types and decls) that can be diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp --- a/clang/lib/AST/ASTContext.cpp +++ b/clang/lib/AST/ASTContext.cpp @@ -1858,7 +1858,7 @@ Width = llvm::alignTo(Width, Align); return TypeInfoChars(CharUnits::fromQuantity(Width), CharUnits::fromQuantity(Align), - EltInfo.AlignIsRequired); + EltInfo.AlignRequirement); } TypeInfoChars ASTContext::getTypeInfoInChars(const Type *T) const { @@ -1866,8 +1866,7 @@ return getConstantArrayInfoInChars(*this, CAT); TypeInfo Info = getTypeInfo(T); return TypeInfoChars(toCharUnitsFromBits(Info.Width), - toCharUnitsFromBits(Info.Align), - Info.AlignIsRequired); + toCharUnitsFromBits(Info.Align), Info.AlignRequirement); } TypeInfoChars ASTContext::getTypeInfoInChars(QualType T) const { @@ -1875,7 +1874,7 @@ } bool ASTContext::isAlignmentRequired(const Type *T) const { - return getTypeInfo(T).AlignIsRequired; + return getTypeInfo(T).AlignRequirement != AlignRequirementKind::None; } bool ASTContext::isAlignmentRequired(QualType T) const { @@ -1927,7 +1926,7 @@ TypeInfo ASTContext::getTypeInfoImpl(const Type *T) const { uint64_t Width = 0; unsigned Align = 8; - bool AlignIsRequired = false; + AlignRequirementKind AlignRequirement = AlignRequirementKind::None; unsigned AS = 0; switch (T->getTypeClass()) { #define TYPE(Class, Base) @@ -1961,7 +1960,7 @@ "Overflow in array type bit size evaluation"); Width = EltInfo.Width * Size; Align = EltInfo.Align; - AlignIsRequired = EltInfo.AlignIsRequired; + AlignRequirement = EltInfo.AlignRequirement; if (!getTargetInfo().getCXXABI().isMicrosoft() || getTargetInfo().getPointerWidth(0) == 64) Width = llvm::alignTo(Width, Align); @@ -2298,7 +2297,7 @@ getTypeInfo(ED->getIntegerType()->getUnqualifiedDesugaredType()); if (unsigned AttrAlign = ED->getMaxAlignment()) { Info.Align = AttrAlign; - Info.AlignIsRequired = true; + Info.AlignRequirement = AlignRequirementKind::RequiredByRecord; } return Info; } @@ -2308,7 +2307,9 @@ const ASTRecordLayout &Layout = getASTRecordLayout(RD); Width = toBits(Layout.getSize()); Align = toBits(Layout.getAlignment()); - AlignIsRequired = RD->hasAttr(); + AlignRequirement = RD->hasAttr() + ? AlignRequirementKind::RequiredByRecord + : AlignRequirementKind::None; break; } @@ -2342,10 +2343,10 @@ // attribute(aligned) can only round up) but matches its implementation. if (unsigned AttrAlign = Typedef->getMaxAlignment()) { Align = AttrAlign; - AlignIsRequired = true; + AlignRequirement = AlignRequirementKind::RequiredByTypedef; } else { Align = Info.Align; - AlignIsRequired = Info.AlignIsRequired; + AlignRequirement = Info.AlignRequirement; } Width = Info.Width; break; @@ -2391,7 +2392,7 @@ } assert(llvm::isPowerOf2_32(Align) && "Alignment must be power of 2"); - return TypeInfo(Width, Align, AlignIsRequired); + return TypeInfo(Width, Align, AlignRequirement); } unsigned ASTContext::getTypeUnadjustedAlign(const Type *T) const { @@ -2481,7 +2482,7 @@ // When used as part of a typedef, or together with a 'packed' attribute, // the 'aligned' attribute can be used to decrease alignment. - if ((TI.AlignIsRequired && T->getAs() != nullptr) || + if ((TI.isAlignRequired() && T->getAs() != nullptr) || RD->isInvalidDecl()) return ABIAlign; @@ -2506,7 +2507,7 @@ Target->defaultsToAIXPowerAlignment())) // Don't increase the alignment if an alignment attribute was specified on a // typedef declaration. - if (!TI.AlignIsRequired) + if (!TI.isAlignRequired()) return std::max(ABIAlign, (unsigned)getTypeSize(T)); return ABIAlign; diff --git a/clang/lib/AST/RecordLayoutBuilder.cpp b/clang/lib/AST/RecordLayoutBuilder.cpp --- a/clang/lib/AST/RecordLayoutBuilder.cpp +++ b/clang/lib/AST/RecordLayoutBuilder.cpp @@ -1538,7 +1538,7 @@ TypeInfo FieldInfo = Context.getTypeInfo(D->getType()); uint64_t StorageUnitSize = FieldInfo.Width; unsigned FieldAlign = FieldInfo.Align; - bool AlignIsRequired = FieldInfo.AlignIsRequired; + bool AlignIsRequired = FieldInfo.isAlignRequired(); // UnfilledBitsInLastUnit is the difference between the end of the // last allocated bitfield (i.e. the first bit offset available for @@ -1889,7 +1889,7 @@ bool FieldPacked = Packed || D->hasAttr(); - bool AlignIsRequired = false; + AlignRequirementKind AlignRequirement = AlignRequirementKind::None; CharUnits FieldSize; CharUnits FieldAlign; // The amount of this class's dsize occupied by the field. @@ -1904,7 +1904,7 @@ // aligned appropriately for their element type. EffectiveFieldSize = FieldSize = IsIncompleteArrayType ? CharUnits::Zero() : TI.Width; - AlignIsRequired = TI.AlignIsRequired; + AlignRequirement = TI.AlignRequirement; }; if (D->getType()->isIncompleteArrayType()) { diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp --- a/clang/lib/CodeGen/CGDebugInfo.cpp +++ b/clang/lib/CodeGen/CGDebugInfo.cpp @@ -52,7 +52,7 @@ static uint32_t getTypeAlignIfRequired(const Type *Ty, const ASTContext &Ctx) { auto TI = Ctx.getTypeInfo(Ty); - return TI.AlignIsRequired ? TI.Align : 0; + return TI.isAlignRequired() ? TI.Align : 0; } static uint32_t getTypeAlignIfRequired(QualType Ty, const ASTContext &Ctx) { @@ -4676,7 +4676,7 @@ llvm::DIType *fieldType; if (capture->isByRef()) { TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy); - auto Align = PtrInfo.AlignIsRequired ? PtrInfo.Align : 0; + auto Align = PtrInfo.isAlignRequired() ? PtrInfo.Align : 0; // FIXME: This recomputes the layout of the BlockByRefWrapper. uint64_t xoffset; fieldType = diff --git a/clang/lib/CodeGen/MicrosoftCXXABI.cpp b/clang/lib/CodeGen/MicrosoftCXXABI.cpp --- a/clang/lib/CodeGen/MicrosoftCXXABI.cpp +++ b/clang/lib/CodeGen/MicrosoftCXXABI.cpp @@ -847,7 +847,7 @@ // arguments was not supported and resulted in a compiler error. In 19.14 // and later versions, such arguments are now passed indirectly. TypeInfo Info = getContext().getTypeInfo(RD->getTypeForDecl()); - if (Info.AlignIsRequired && Info.Align > 4) + if (Info.isAlignRequired() && Info.Align > 4) return RAA_Indirect; // If C++ prohibits us from making a copy, construct the arguments directly diff --git a/clang/lib/CodeGen/TargetInfo.cpp b/clang/lib/CodeGen/TargetInfo.cpp --- a/clang/lib/CodeGen/TargetInfo.cpp +++ b/clang/lib/CodeGen/TargetInfo.cpp @@ -1839,7 +1839,7 @@ // Pass over-aligned aggregates on Windows indirectly. This behavior was // added in MSVC 2015. - if (IsWin32StructABI && TI.AlignIsRequired && TI.Align > 32) + if (IsWin32StructABI && TI.isAlignRequired() && TI.Align > 32) return getIndirectResult(Ty, /*ByVal=*/false, State); // Expand small (<= 128-bit) record types when we know that the stack layout @@ -6992,7 +6992,7 @@ TyAlignForABI = CharUnits::fromQuantity(4); } - TypeInfoChars TyInfo(TySize, TyAlignForABI, false); + TypeInfoChars TyInfo(TySize, TyAlignForABI, AlignRequirementKind::None); return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo, SlotSize, /*AllowHigherAlign*/ true); }