diff --git a/llvm/include/llvm/Bitstream/BitcodeCommon.h b/llvm/include/llvm/Bitstream/BitcodeCommon.h new file mode 100644 --- /dev/null +++ b/llvm/include/llvm/Bitstream/BitcodeCommon.h @@ -0,0 +1,30 @@ +//===- BitcodeCommon.h - Common code for encode/decode --------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This header defines common code to be used by BitcodeWriter and +// BitcodeReader. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_BITSTREAM_BITCODECOMMON_H +#define LLVM_BITSTREAM_BITCODECOMMON_H + +#include "llvm/ADT/Bitfields.h" + +namespace llvm { + +struct AllocaPackedValues { + using Align = Bitfield::Element; + using UsedWithInAlloca = Bitfield::Element; + using ExplicitType = Bitfield::Element; + using SwiftError = Bitfield::Element; +}; + +} // namespace llvm + +#endif // LLVM_BITSTREAM_BITCODECOMMON_H diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp --- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp @@ -20,8 +20,9 @@ #include "llvm/ADT/StringRef.h" #include "llvm/ADT/Triple.h" #include "llvm/ADT/Twine.h" -#include "llvm/Bitstream/BitstreamReader.h" #include "llvm/Bitcode/LLVMBitCodes.h" +#include "llvm/Bitstream/BitcodeCommon.h" +#include "llvm/Bitstream/BitstreamReader.h" #include "llvm/Config/llvm-config.h" #include "llvm/IR/Argument.h" #include "llvm/IR/Attributes.h" @@ -4806,17 +4807,13 @@ case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align] if (Record.size() != 4) return error("Invalid record"); - uint64_t AlignRecord = Record[3]; - const uint64_t InAllocaMask = uint64_t(1) << 5; - const uint64_t ExplicitTypeMask = uint64_t(1) << 6; - const uint64_t SwiftErrorMask = uint64_t(1) << 7; - const uint64_t FlagMask = InAllocaMask | ExplicitTypeMask | - SwiftErrorMask; - bool InAlloca = AlignRecord & InAllocaMask; - bool SwiftError = AlignRecord & SwiftErrorMask; + using APV = AllocaPackedValues; + const uint64_t Rec = Record[3]; + const bool InAlloca = Bitfield::get(Rec); + const bool SwiftError = Bitfield::get(Rec); FullTy = getFullyStructuredTypeByID(Record[0]); Type *Ty = flattenPointerTypes(FullTy); - if ((AlignRecord & ExplicitTypeMask) == 0) { + if (!Bitfield::get(Rec)) { auto *PTy = dyn_cast_or_null(Ty); if (!PTy) return error("Old-style alloca with a non-pointer type"); @@ -4825,7 +4822,8 @@ Type *OpTy = getTypeByID(Record[1]); Value *Size = getFnValueByID(Record[2], OpTy); MaybeAlign Align; - if (Error Err = parseAlignmentValue(AlignRecord & ~FlagMask, Align)) { + if (Error Err = + parseAlignmentValue(Bitfield::get(Rec), Align)) { return Err; } if (!Ty || !Size) 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 @@ -27,6 +27,7 @@ #include "llvm/Bitcode/BitcodeReader.h" #include "llvm/Bitcode/LLVMBitCodes.h" #include "llvm/Bitstream/BitCodes.h" +#include "llvm/Bitstream/BitcodeCommon.h" #include "llvm/Bitstream/BitstreamWriter.h" #include "llvm/Config/llvm-config.h" #include "llvm/IR/Attributes.h" @@ -394,6 +395,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 +1182,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 +1202,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 +1238,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 +1294,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 +1340,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,14 +2948,13 @@ 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; - Vals.push_back(AlignRecord); + using APV = AllocaPackedValues; + unsigned Record = 0; + Bitfield::set(Record, getEncodedAlign(AI.getAlign())); + Bitfield::set(Record, AI.isUsedWithInAlloca()); + Bitfield::set(Record, true); + Bitfield::set(Record, AI.isSwiftError()); + Vals.push_back(Record); break; } @@ -2962,7 +2968,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 +2982,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()));