diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp --- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -394,6 +394,8 @@ unsigned getEncodedSyncScopeID(SyncScope::ID SSID) { return unsigned(SSID); } + + unsigned getEncodedAlign(MaybeAlign Alignment) { return encode(Alignment); } }; /// Class to manage the bitcode writing for a combined index. @@ -1179,10 +1181,14 @@ // compute the maximum alignment value. std::map SectionMap; std::map GCMap; - unsigned MaxAlignment = 0; + MaybeAlign MaxAlignment; unsigned MaxGlobalType = 0; + const auto UpdateMaxAlignment = [&MaxAlignment](const MaybeAlign A) { + if (A) + MaxAlignment = !MaxAlignment ? *A : std::max(*MaxAlignment, *A); + }; for (const GlobalVariable &GV : M.globals()) { - MaxAlignment = std::max(MaxAlignment, GV.getAlignment()); + UpdateMaxAlignment(GV.getAlign()); MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV.getValueType())); if (GV.hasSection()) { // Give section names unique ID's. @@ -1195,7 +1201,7 @@ } } for (const Function &F : M) { - MaxAlignment = std::max(MaxAlignment, F.getAlignment()); + UpdateMaxAlignment(F.getAlign()); if (F.hasSection()) { // Give section names unique ID's. unsigned &Entry = SectionMap[std::string(F.getSection())]; @@ -1231,10 +1237,10 @@ //| constant Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // Linkage. - if (MaxAlignment == 0) // Alignment. + if (!MaxAlignment) // Alignment. Abbv->Add(BitCodeAbbrevOp(0)); else { - unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1; + unsigned MaxEncAlignment = getEncodedAlign(MaxAlignment); Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(MaxEncAlignment+1))); } @@ -1287,7 +1293,7 @@ Vals.push_back(GV.isDeclaration() ? 0 : (VE.getValueID(GV.getInitializer()) + 1)); Vals.push_back(getEncodedLinkage(GV)); - Vals.push_back(Log2_32(GV.getAlignment())+1); + Vals.push_back(getEncodedAlign(GV.getAlign())); Vals.push_back(GV.hasSection() ? SectionMap[std::string(GV.getSection())] : 0); if (GV.isThreadLocal() || @@ -1333,7 +1339,7 @@ Vals.push_back(F.isDeclaration()); Vals.push_back(getEncodedLinkage(F)); Vals.push_back(VE.getAttributeListID(F.getAttributes())); - Vals.push_back(Log2_32(F.getAlignment())+1); + Vals.push_back(getEncodedAlign(F.getAlign())); Vals.push_back(F.hasSection() ? SectionMap[std::string(F.getSection())] : 0); Vals.push_back(getEncodedVisibility(F)); @@ -2941,13 +2947,15 @@ Vals.push_back(VE.getTypeID(AI.getAllocatedType())); Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); Vals.push_back(VE.getValueID(I.getOperand(0))); // size. - unsigned AlignRecord = Log2_32(AI.getAlignment()) + 1; - assert(Log2_32(Value::MaximumAlignment) + 1 < 1 << 5 && - "not enough bits for maximum alignment"); - assert(AlignRecord < 1 << 5 && "alignment greater than 1 << 64"); - AlignRecord |= AI.isUsedWithInAlloca() << 5; - AlignRecord |= 1 << 6; - AlignRecord |= AI.isSwiftError() << 7; + using AlignField = Bitfield::Element; // bits : 0-4 + using UsedWithInAllocaField = Bitfield::Element; // bits : 5 + using UnknownField = Bitfield::Element; // bits : 6 + using SwiftErrorField = Bitfield::Element; // bits : 7 + unsigned AlignRecord = 0; + Bitfield::set(AlignRecord, getEncodedAlign(AI.getAlign())); + Bitfield::set(AlignRecord, AI.isUsedWithInAlloca()); + Bitfield::set(AlignRecord, true); + Bitfield::set(AlignRecord, AI.isSwiftError()); Vals.push_back(AlignRecord); break; } @@ -2962,7 +2970,7 @@ AbbrevToUse = FUNCTION_INST_LOAD_ABBREV; } Vals.push_back(VE.getTypeID(I.getType())); - Vals.push_back(Log2_32(cast(I).getAlignment())+1); + Vals.push_back(getEncodedAlign(cast(I).getAlign())); Vals.push_back(cast(I).isVolatile()); if (cast(I).isAtomic()) { Vals.push_back(getEncodedOrdering(cast(I).getOrdering())); @@ -2976,7 +2984,7 @@ Code = bitc::FUNC_CODE_INST_STORE; pushValueAndType(I.getOperand(1), InstID, Vals); // ptrty + ptr pushValueAndType(I.getOperand(0), InstID, Vals); // valty + val - Vals.push_back(Log2_32(cast(I).getAlignment())+1); + Vals.push_back(getEncodedAlign(cast(I).getAlign())); Vals.push_back(cast(I).isVolatile()); if (cast(I).isAtomic()) { Vals.push_back(getEncodedOrdering(cast(I).getOrdering()));