diff options
Diffstat (limited to 'lib/Bitcode')
-rw-r--r-- | lib/Bitcode/Reader/BitReader.cpp | 21 | ||||
-rw-r--r-- | lib/Bitcode/Reader/BitcodeReader.cpp | 1363 | ||||
-rw-r--r-- | lib/Bitcode/Reader/BitcodeReader.h | 58 | ||||
-rw-r--r-- | lib/Bitcode/Reader/BitstreamReader.cpp | 8 | ||||
-rw-r--r-- | lib/Bitcode/Reader/CMakeLists.txt | 3 | ||||
-rw-r--r-- | lib/Bitcode/Writer/BitcodeWriter.cpp | 658 | ||||
-rw-r--r-- | lib/Bitcode/Writer/BitcodeWriterPass.cpp | 4 | ||||
-rw-r--r-- | lib/Bitcode/Writer/ValueEnumerator.cpp | 215 | ||||
-rw-r--r-- | lib/Bitcode/Writer/ValueEnumerator.h | 41 |
9 files changed, 1732 insertions, 639 deletions
diff --git a/lib/Bitcode/Reader/BitReader.cpp b/lib/Bitcode/Reader/BitReader.cpp index 9b3acb5..868fbf0 100644 --- a/lib/Bitcode/Reader/BitReader.cpp +++ b/lib/Bitcode/Reader/BitReader.cpp @@ -9,9 +9,11 @@ #include "llvm-c/BitReader.h" #include "llvm/Bitcode/ReaderWriter.h" +#include "llvm/IR/DiagnosticPrinter.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/Module.h" #include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/raw_ostream.h" #include <cstring> #include <string> @@ -30,11 +32,20 @@ LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef, LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutModule, char **OutMessage) { - ErrorOr<Module *> ModuleOrErr = - parseBitcodeFile(unwrap(MemBuf)->getMemBufferRef(), *unwrap(ContextRef)); - if (std::error_code EC = ModuleOrErr.getError()) { - if (OutMessage) - *OutMessage = strdup(EC.message().c_str()); + MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef(); + LLVMContext &Ctx = *unwrap(ContextRef); + + std::string Message; + raw_string_ostream Stream(Message); + DiagnosticPrinterRawOStream DP(Stream); + + ErrorOr<Module *> ModuleOrErr = parseBitcodeFile( + Buf, Ctx, [&](const DiagnosticInfo &DI) { DI.print(DP); }); + if (ModuleOrErr.getError()) { + if (OutMessage) { + Stream.flush(); + *OutMessage = strdup(Message.c_str()); + } *OutModule = wrap((Module*)nullptr); return 1; } diff --git a/lib/Bitcode/Reader/BitcodeReader.cpp b/lib/Bitcode/Reader/BitcodeReader.cpp index b2ca22c..92af0f8 100644 --- a/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/lib/Bitcode/Reader/BitcodeReader.cpp @@ -11,10 +11,13 @@ #include "BitcodeReader.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/Triple.h" #include "llvm/Bitcode/LLVMBitCodes.h" #include "llvm/IR/AutoUpgrade.h" #include "llvm/IR/Constants.h" +#include "llvm/IR/DebugInfoMetadata.h" #include "llvm/IR/DerivedTypes.h" +#include "llvm/IR/DiagnosticPrinter.h" #include "llvm/IR/InlineAsm.h" #include "llvm/IR/IntrinsicInst.h" #include "llvm/IR/LLVMContext.h" @@ -22,10 +25,10 @@ #include "llvm/IR/OperandTraits.h" #include "llvm/IR/Operator.h" #include "llvm/Support/DataStream.h" +#include "llvm/Support/ManagedStatic.h" #include "llvm/Support/MathExtras.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/raw_ostream.h" -#include "llvm/Support/ManagedStatic.h" using namespace llvm; @@ -33,6 +36,61 @@ enum { SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex }; +BitcodeDiagnosticInfo::BitcodeDiagnosticInfo(std::error_code EC, + DiagnosticSeverity Severity, + const Twine &Msg) + : DiagnosticInfo(DK_Bitcode, Severity), Msg(Msg), EC(EC) {} + +void BitcodeDiagnosticInfo::print(DiagnosticPrinter &DP) const { DP << Msg; } + +static std::error_code Error(DiagnosticHandlerFunction DiagnosticHandler, + std::error_code EC, const Twine &Message) { + BitcodeDiagnosticInfo DI(EC, DS_Error, Message); + DiagnosticHandler(DI); + return EC; +} + +static std::error_code Error(DiagnosticHandlerFunction DiagnosticHandler, + std::error_code EC) { + return Error(DiagnosticHandler, EC, EC.message()); +} + +std::error_code BitcodeReader::Error(BitcodeError E, const Twine &Message) { + return ::Error(DiagnosticHandler, make_error_code(E), Message); +} + +std::error_code BitcodeReader::Error(const Twine &Message) { + return ::Error(DiagnosticHandler, + make_error_code(BitcodeError::CorruptedBitcode), Message); +} + +std::error_code BitcodeReader::Error(BitcodeError E) { + return ::Error(DiagnosticHandler, make_error_code(E)); +} + +static DiagnosticHandlerFunction getDiagHandler(DiagnosticHandlerFunction F, + LLVMContext &C) { + if (F) + return F; + return [&C](const DiagnosticInfo &DI) { C.diagnose(DI); }; +} + +BitcodeReader::BitcodeReader(MemoryBuffer *buffer, LLVMContext &C, + DiagnosticHandlerFunction DiagnosticHandler) + : Context(C), DiagnosticHandler(getDiagHandler(DiagnosticHandler, C)), + TheModule(nullptr), Buffer(buffer), LazyStreamer(nullptr), + NextUnreadBit(0), SeenValueSymbolTable(false), ValueList(C), + MDValueList(C), SeenFirstFunctionBody(false), UseRelativeIDs(false), + WillMaterializeAllForwardRefs(false) {} + +BitcodeReader::BitcodeReader(DataStreamer *streamer, LLVMContext &C, + DiagnosticHandlerFunction DiagnosticHandler) + : Context(C), DiagnosticHandler(getDiagHandler(DiagnosticHandler, C)), + TheModule(nullptr), Buffer(nullptr), LazyStreamer(streamer), + NextUnreadBit(0), SeenValueSymbolTable(false), ValueList(C), + MDValueList(C), SeenFirstFunctionBody(false), UseRelativeIDs(false), + WillMaterializeAllForwardRefs(false) {} + std::error_code BitcodeReader::materializeForwardReferencedFunctions() { if (WillMaterializeAllForwardRefs) return std::error_code(); @@ -53,7 +111,7 @@ std::error_code BitcodeReader::materializeForwardReferencedFunctions() { // isn't a trivial way to check if a function will have a body without a // linear search through FunctionsWithBodies, so just check it here. if (!F->isMaterializable()) - return Error(BitcodeError::NeverResolvedFunctionFromBlockAddress); + return Error("Never resolved function from blockaddress"); // Try to materialize F. if (std::error_code EC = materialize(F)) @@ -100,26 +158,57 @@ static bool ConvertToString(ArrayRef<uint64_t> Record, unsigned Idx, return false; } -static GlobalValue::LinkageTypes GetDecodedLinkage(unsigned Val) { +static bool hasImplicitComdat(size_t Val) { + switch (Val) { + default: + return false; + case 1: // Old WeakAnyLinkage + case 4: // Old LinkOnceAnyLinkage + case 10: // Old WeakODRLinkage + case 11: // Old LinkOnceODRLinkage + return true; + } +} + +static GlobalValue::LinkageTypes getDecodedLinkage(unsigned Val) { switch (Val) { default: // Map unknown/new linkages to external - case 0: return GlobalValue::ExternalLinkage; - case 1: return GlobalValue::WeakAnyLinkage; - case 2: return GlobalValue::AppendingLinkage; - case 3: return GlobalValue::InternalLinkage; - case 4: return GlobalValue::LinkOnceAnyLinkage; - case 5: return GlobalValue::ExternalLinkage; // Obsolete DLLImportLinkage - case 6: return GlobalValue::ExternalLinkage; // Obsolete DLLExportLinkage - case 7: return GlobalValue::ExternalWeakLinkage; - case 8: return GlobalValue::CommonLinkage; - case 9: return GlobalValue::PrivateLinkage; - case 10: return GlobalValue::WeakODRLinkage; - case 11: return GlobalValue::LinkOnceODRLinkage; - case 12: return GlobalValue::AvailableExternallyLinkage; + case 0: + return GlobalValue::ExternalLinkage; + case 2: + return GlobalValue::AppendingLinkage; + case 3: + return GlobalValue::InternalLinkage; + case 5: + return GlobalValue::ExternalLinkage; // Obsolete DLLImportLinkage + case 6: + return GlobalValue::ExternalLinkage; // Obsolete DLLExportLinkage + case 7: + return GlobalValue::ExternalWeakLinkage; + case 8: + return GlobalValue::CommonLinkage; + case 9: + return GlobalValue::PrivateLinkage; + case 12: + return GlobalValue::AvailableExternallyLinkage; case 13: return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateLinkage case 14: return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateWeakLinkage + case 15: + return GlobalValue::ExternalLinkage; // Obsolete LinkOnceODRAutoHideLinkage + case 1: // Old value with implicit comdat. + case 16: + return GlobalValue::WeakAnyLinkage; + case 10: // Old value with implicit comdat. + case 17: + return GlobalValue::WeakODRLinkage; + case 4: // Old value with implicit comdat. + case 18: + return GlobalValue::LinkOnceAnyLinkage; + case 11: // Old value with implicit comdat. + case 19: + return GlobalValue::LinkOnceODRLinkage; } } @@ -261,7 +350,7 @@ namespace { /// @brief A class for maintaining the slot number definition /// as a placeholder for the actual definition for forward constants defs. class ConstantPlaceHolder : public ConstantExpr { - void operator=(const ConstantPlaceHolder &) LLVM_DELETED_FUNCTION; + void operator=(const ConstantPlaceHolder &) = delete; public: // allocate space for exactly one operand void *operator new(size_t s) { @@ -280,7 +369,7 @@ namespace { /// Provide fast operand accessors - //DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); + DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); }; } @@ -289,6 +378,7 @@ template <> struct OperandTraits<ConstantPlaceHolder> : public FixedNumOperandTraits<ConstantPlaceHolder, 1> { }; +DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantPlaceHolder, Value) } @@ -437,43 +527,72 @@ void BitcodeReaderValueList::ResolveConstantForwardRefs() { } } -void BitcodeReaderMDValueList::AssignValue(Value *V, unsigned Idx) { +void BitcodeReaderMDValueList::AssignValue(Metadata *MD, unsigned Idx) { if (Idx == size()) { - push_back(V); + push_back(MD); return; } if (Idx >= size()) resize(Idx+1); - WeakVH &OldV = MDValuePtrs[Idx]; - if (!OldV) { - OldV = V; + TrackingMDRef &OldMD = MDValuePtrs[Idx]; + if (!OldMD) { + OldMD.reset(MD); return; } // If there was a forward reference to this value, replace it. - MDNode *PrevVal = cast<MDNode>(OldV); - OldV->replaceAllUsesWith(V); - MDNode::deleteTemporary(PrevVal); - // Deleting PrevVal sets Idx value in MDValuePtrs to null. Set new - // value for Idx. - MDValuePtrs[Idx] = V; + TempMDTuple PrevMD(cast<MDTuple>(OldMD.get())); + PrevMD->replaceAllUsesWith(MD); + --NumFwdRefs; } -Value *BitcodeReaderMDValueList::getValueFwdRef(unsigned Idx) { +Metadata *BitcodeReaderMDValueList::getValueFwdRef(unsigned Idx) { if (Idx >= size()) resize(Idx + 1); - if (Value *V = MDValuePtrs[Idx]) { - assert(V->getType()->isMetadataTy() && "Type mismatch in value table!"); - return V; + if (Metadata *MD = MDValuePtrs[Idx]) + return MD; + + // Track forward refs to be resolved later. + if (AnyFwdRefs) { + MinFwdRef = std::min(MinFwdRef, Idx); + MaxFwdRef = std::max(MaxFwdRef, Idx); + } else { + AnyFwdRefs = true; + MinFwdRef = MaxFwdRef = Idx; } + ++NumFwdRefs; // Create and return a placeholder, which will later be RAUW'd. - Value *V = MDNode::getTemporary(Context, None); - MDValuePtrs[Idx] = V; - return V; + Metadata *MD = MDNode::getTemporary(Context, None).release(); + MDValuePtrs[Idx].reset(MD); + return MD; +} + +void BitcodeReaderMDValueList::tryToResolveCycles() { + if (!AnyFwdRefs) + // Nothing to do. + return; + + if (NumFwdRefs) + // Still forward references... can't resolve cycles. + return; + + // Resolve any cycles. + for (unsigned I = MinFwdRef, E = MaxFwdRef + 1; I != E; ++I) { + auto &MD = MDValuePtrs[I]; + auto *N = dyn_cast_or_null<MDNode>(MD); + if (!N) + continue; + + assert(!N->isTemporary() && "Unexpected forward reference"); + N->resolveCycles(); + } + + // Make sure we return early again until there's another forward ref. + AnyFwdRefs = false; } Type *BitcodeReader::getTypeByID(unsigned ID) { @@ -486,7 +605,20 @@ Type *BitcodeReader::getTypeByID(unsigned ID) { // If we have a forward reference, the only possible case is when it is to a // named struct. Just create a placeholder for now. - return TypeList[ID] = StructType::create(Context); + return TypeList[ID] = createIdentifiedStructType(Context); +} + +StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context, + StringRef Name) { + auto *Ret = StructType::create(Context, Name); + IdentifiedStructTypes.push_back(Ret); + return Ret; +} + +StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context) { + auto *Ret = StructType::create(Context); + IdentifiedStructTypes.push_back(Ret); + return Ret; } @@ -516,10 +648,10 @@ static void decodeLLVMAttributesForBitcode(AttrBuilder &B, std::error_code BitcodeReader::ParseAttributeBlock() { if (Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); if (!MAttributes.empty()) - return Error(BitcodeError::InvalidMultipleBlocks); + return Error("Invalid multiple blocks"); SmallVector<uint64_t, 64> Record; @@ -532,7 +664,7 @@ std::error_code BitcodeReader::ParseAttributeBlock() { switch (Entry.Kind) { case BitstreamEntry::SubBlock: // Handled for us already. case BitstreamEntry::Error: - return Error(BitcodeError::MalformedBlock); + return Error("Malformed block"); case BitstreamEntry::EndBlock: return std::error_code(); case BitstreamEntry::Record: @@ -548,7 +680,7 @@ std::error_code BitcodeReader::ParseAttributeBlock() { case bitc::PARAMATTR_CODE_ENTRY_OLD: { // ENTRY: [paramidx0, attr0, ...] // FIXME: Remove in 4.0. if (Record.size() & 1) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); for (unsigned i = 0, e = Record.size(); i != e; i += 2) { AttrBuilder B; @@ -662,20 +794,31 @@ static Attribute::AttrKind GetAttrFromCode(uint64_t Code) { } } +std::error_code BitcodeReader::parseAlignmentValue(uint64_t Exponent, + unsigned &Alignment) { + // Note: Alignment in bitcode files is incremented by 1, so that zero + // can be used for default alignment. + if (Exponent > Value::MaxAlignmentExponent + 1) + return Error("Invalid alignment value"); + Alignment = (1 << static_cast<unsigned>(Exponent)) >> 1; + return std::error_code(); +} + std::error_code BitcodeReader::ParseAttrKind(uint64_t Code, Attribute::AttrKind *Kind) { *Kind = GetAttrFromCode(Code); if (*Kind == Attribute::None) - return Error(BitcodeError::InvalidValue); + return Error(BitcodeError::CorruptedBitcode, + "Unknown attribute kind (" + Twine(Code) + ")"); return std::error_code(); } std::error_code BitcodeReader::ParseAttributeGroupBlock() { if (Stream.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); if (!MAttributeGroups.empty()) - return Error(BitcodeError::InvalidMultipleBlocks); + return Error("Invalid multiple blocks"); SmallVector<uint64_t, 64> Record; @@ -686,7 +829,7 @@ std::error_code BitcodeReader::ParseAttributeGroupBlock() { switch (Entry.Kind) { case BitstreamEntry::SubBlock: // Handled for us already. case BitstreamEntry::Error: - return Error(BitcodeError::MalformedBlock); + return Error("Malformed block"); case BitstreamEntry::EndBlock: return std::error_code(); case BitstreamEntry::Record: @@ -701,7 +844,7 @@ std::error_code BitcodeReader::ParseAttributeGroupBlock() { break; case bitc::PARAMATTR_GRP_CODE_ENTRY: { // ENTRY: [grpid, idx, a0, a1, ...] if (Record.size() < 3) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); uint64_t GrpID = Record[0]; uint64_t Idx = Record[1]; // Index of the object this attribute refers to. @@ -756,14 +899,14 @@ std::error_code BitcodeReader::ParseAttributeGroupBlock() { std::error_code BitcodeReader::ParseTypeTable() { if (Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); return ParseTypeTableBody(); } std::error_code BitcodeReader::ParseTypeTableBody() { if (!TypeList.empty()) - return Error(BitcodeError::InvalidMultipleBlocks); + return Error("Invalid multiple blocks"); SmallVector<uint64_t, 64> Record; unsigned NumRecords = 0; @@ -777,10 +920,10 @@ std::error_code BitcodeReader::ParseTypeTableBody() { switch (Entry.Kind) { case BitstreamEntry::SubBlock: // Handled for us already. case BitstreamEntry::Error: - return Error(BitcodeError::MalformedBlock); + return Error("Malformed block"); case BitstreamEntry::EndBlock: if (NumRecords != TypeList.size()) - return Error(BitcodeError::MalformedBlock); + return Error("Malformed block"); return std::error_code(); case BitstreamEntry::Record: // The interesting case. @@ -792,12 +935,12 @@ std::error_code BitcodeReader::ParseTypeTableBody() { Type *ResultTy = nullptr; switch (Stream.readRecord(Entry.ID, Record)) { default: - return Error(BitcodeError::InvalidValue); + return Error("Invalid value"); case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries] // TYPE_CODE_NUMENTRY contains a count of the number of types in the // type list. This allows us to reserve space. if (Record.size() < 1) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); TypeList.resize(Record[0]); continue; case bitc::TYPE_CODE_VOID: // VOID @@ -830,22 +973,27 @@ std::error_code BitcodeReader::ParseTypeTableBody() { case bitc::TYPE_CODE_X86_MMX: // X86_MMX ResultTy = Type::getX86_MMXTy(Context); break; - case bitc::TYPE_CODE_INTEGER: // INTEGER: [width] + case bitc::TYPE_CODE_INTEGER: { // INTEGER: [width] if (Record.size() < 1) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); - ResultTy = IntegerType::get(Context, Record[0]); + uint64_t NumBits = Record[0]; + if (NumBits < IntegerType::MIN_INT_BITS || + NumBits > IntegerType::MAX_INT_BITS) + return Error("Bitwidth for integer type out of range"); + ResultTy = IntegerType::get(Context, NumBits); break; + } case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or // [pointee type, address space] if (Record.size() < 1) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); unsigned AddressSpace = 0; if (Record.size() == 2) AddressSpace = Record[1]; ResultTy = getTypeByID(Record[0]); if (!ResultTy) - return Error(BitcodeError::InvalidType); + return Error("Invalid type"); ResultTy = PointerType::get(ResultTy, AddressSpace); break; } @@ -853,7 +1001,7 @@ std::error_code BitcodeReader::ParseTypeTableBody() { // FIXME: attrid is dead, remove it in LLVM 4.0 // FUNCTION: [vararg, attrid, retty, paramty x N] if (Record.size() < 3) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); SmallVector<Type*, 8> ArgTys; for (unsigned i = 3, e = Record.size(); i != e; ++i) { if (Type *T = getTypeByID(Record[i])) @@ -864,7 +1012,7 @@ std::error_code BitcodeReader::ParseTypeTableBody() { ResultTy = getTypeByID(Record[2]); if (!ResultTy || ArgTys.size() < Record.size()-3) - return Error(BitcodeError::InvalidType); + return Error("Invalid type"); ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]); break; @@ -872,7 +1020,7 @@ std::error_code BitcodeReader::ParseTypeTableBody() { case bitc::TYPE_CODE_FUNCTION: { // FUNCTION: [vararg, retty, paramty x N] if (Record.size() < 2) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); SmallVector<Type*, 8> ArgTys; for (unsigned i = 2, e = Record.size(); i != e; ++i) { if (Type *T = getTypeByID(Record[i])) @@ -883,14 +1031,14 @@ std::error_code BitcodeReader::ParseTypeTableBody() { ResultTy = getTypeByID(Record[1]); if (!ResultTy || ArgTys.size() < Record.size()-2) - return Error(BitcodeError::InvalidType); + return Error("Invalid type"); ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]); break; } case bitc::TYPE_CODE_STRUCT_ANON: { // STRUCT: [ispacked, eltty x N] if (Record.size() < 1) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); SmallVector<Type*, 8> EltTys; for (unsigned i = 1, e = Record.size(); i != e; ++i) { if (Type *T = getTypeByID(Record[i])) @@ -899,21 +1047,21 @@ std::error_code BitcodeReader::ParseTypeTableBody() { break; } if (EltTys.size() != Record.size()-1) - return Error(BitcodeError::InvalidType); + return Error("Invalid type"); ResultTy = StructType::get(Context, EltTys, Record[0]); break; } case bitc::TYPE_CODE_STRUCT_NAME: // STRUCT_NAME: [strchr x N] if (ConvertToString(Record, 0, TypeName)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); continue; case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N] if (Record.size() < 1) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); if (NumRecords >= TypeList.size()) - return Error(BitcodeError::InvalidTYPETable); + return Error("Invalid TYPE table"); // Check to see if this was forward referenced, if so fill in the temp. StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]); @@ -921,7 +1069,7 @@ std::error_code BitcodeReader::ParseTypeTableBody() { Res->setName(TypeName); TypeList[NumRecords] = nullptr; } else // Otherwise, create a new struct. - Res = StructType::create(Context, TypeName); + Res = createIdentifiedStructType(Context, TypeName); TypeName.clear(); SmallVector<Type*, 8> EltTys; @@ -932,17 +1080,17 @@ std::error_code BitcodeReader::ParseTypeTableBody() { break; } if (EltTys.size() != Record.size()-1) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); Res->setBody(EltTys, Record[0]); ResultTy = Res; break; } case bitc::TYPE_CODE_OPAQUE: { // OPAQUE: [] if (Record.size() != 1) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); if (NumRecords >= TypeList.size()) - return Error(BitcodeError::InvalidTYPETable); + return Error("Invalid TYPE table"); // Check to see if this was forward referenced, if so fill in the temp. StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]); @@ -950,43 +1098,47 @@ std::error_code BitcodeReader::ParseTypeTableBody() { Res->setName(TypeName); TypeList[NumRecords] = nullptr; } else // Otherwise, create a new struct with no body. - Res = StructType::create(Context, TypeName); + Res = createIdentifiedStructType(Context, TypeName); TypeName.clear(); ResultTy = Res; break; } case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty] if (Record.size() < 2) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); if ((ResultTy = getTypeByID(Record[1]))) ResultTy = ArrayType::get(ResultTy, Record[0]); else - return Error(BitcodeError::InvalidType); + return Error("Invalid type"); break; case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty] if (Record.size() < 2) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); if ((ResultTy = getTypeByID(Record[1]))) ResultTy = VectorType::get(ResultTy, Record[0]); else - return Error(BitcodeError::InvalidType); + return Error("Invalid type"); break; } if (NumRecords >= TypeList.size()) - return Error(BitcodeError::InvalidTYPETable); + return Error("Invalid TYPE table"); + if (TypeList[NumRecords]) + return Error( + "Invalid TYPE table: Only named structs can be forward referenced"); assert(ResultTy && "Didn't read a type?"); - assert(!TypeList[NumRecords] && "Already read type?"); TypeList[NumRecords++] = ResultTy; } } std::error_code BitcodeReader::ParseValueSymbolTable() { if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); SmallVector<uint64_t, 64> Record; + Triple TT(TheModule->getTargetTriple()); + // Read all the records for this value table. SmallString<128> ValueName; while (1) { @@ -995,7 +1147,7 @@ std::error_code BitcodeReader::ParseValueSymbolTable() { switch (Entry.Kind) { case BitstreamEntry::SubBlock: // Handled for us already. case BitstreamEntry::Error: - return Error(BitcodeError::MalformedBlock); + return Error("Malformed block"); case BitstreamEntry::EndBlock: return std::error_code(); case BitstreamEntry::Record: @@ -1010,22 +1162,30 @@ std::error_code BitcodeReader::ParseValueSymbolTable() { break; case bitc::VST_CODE_ENTRY: { // VST_ENTRY: [valueid, namechar x N] if (ConvertToString(Record, 1, ValueName)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); unsigned ValueID = Record[0]; if (ValueID >= ValueList.size() || !ValueList[ValueID]) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); Value *V = ValueList[ValueID]; V->setName(StringRef(ValueName.data(), ValueName.size())); + if (auto *GO = dyn_cast<GlobalObject>(V)) { + if (GO->getComdat() == reinterpret_cast<Comdat *>(1)) { + if (TT.isOSBinFormatMachO()) + GO->setComdat(nullptr); + else + GO->setComdat(TheModule->getOrInsertComdat(V->getName())); + } + } ValueName.clear(); break; } case bitc::VST_CODE_BBENTRY: { if (ConvertToString(Record, 1, ValueName)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); BasicBlock *BB = getBasicBlock(Record[0]); if (!BB) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); BB->setName(StringRef(ValueName.data(), ValueName.size())); ValueName.clear(); @@ -1035,14 +1195,32 @@ std::error_code BitcodeReader::ParseValueSymbolTable() { } } +static int64_t unrotateSign(uint64_t U) { return U & 1 ? ~(U >> 1) : U >> 1; } + std::error_code BitcodeReader::ParseMetadata() { unsigned NextMDValueNo = MDValueList.size(); if (Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); SmallVector<uint64_t, 64> Record; + auto getMD = + [&](unsigned ID) -> Metadata *{ return MDValueList.getValueFwdRef(ID); }; + auto getMDOrNull = [&](unsigned ID) -> Metadata *{ + if (ID) + return getMD(ID - 1); + return nullptr; + }; + auto getMDString = [&](unsigned ID) -> MDString *{ + // This requires that the ID is not really a forward reference. In + // particular, the MDString must already have been resolved. + return cast_or_null<MDString>(getMDOrNull(ID)); + }; + +#define GET_OR_DISTINCT(CLASS, DISTINCT, ARGS) \ + (DISTINCT ? CLASS::getDistinct ARGS : CLASS::get ARGS) + // Read all the records. while (1) { BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); @@ -1050,18 +1228,19 @@ std::error_code BitcodeReader::ParseMetadata() { switch (Entry.Kind) { case BitstreamEntry::SubBlock: // Handled for us already. case BitstreamEntry::Error: - return Error(BitcodeError::MalformedBlock); + return Error("Malformed block"); case BitstreamEntry::EndBlock: + MDValueList.tryToResolveCycles(); return std::error_code(); case BitstreamEntry::Record: // The interesting case. break; } - bool IsFunctionLocal = false; // Read a record. Record.clear(); unsigned Code = Stream.readRecord(Entry.ID, Record); + bool IsDistinct = false; switch (Code) { default: // Default behavior: ignore. break; @@ -1081,57 +1260,377 @@ std::error_code BitcodeReader::ParseMetadata() { for (unsigned i = 0; i != Size; ++i) { MDNode *MD = dyn_cast_or_null<MDNode>(MDValueList.getValueFwdRef(Record[i])); if (!MD) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); NMD->addOperand(MD); } break; } - case bitc::METADATA_FN_NODE: - IsFunctionLocal = true; - // fall-through - case bitc::METADATA_NODE: { + case bitc::METADATA_OLD_FN_NODE: { + // FIXME: Remove in 4.0. + // This is a LocalAsMetadata record, the only type of function-local + // metadata. if (Record.size() % 2 == 1) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); + + // If this isn't a LocalAsMetadata record, we're dropping it. This used + // to be legal, but there's no upgrade path. + auto dropRecord = [&] { + MDValueList.AssignValue(MDNode::get(Context, None), NextMDValueNo++); + }; + if (Record.size() != 2) { + dropRecord(); + break; + } + + Type *Ty = getTypeByID(Record[0]); + if (Ty->isMetadataTy() || Ty->isVoidTy()) { + dropRecord(); + break; + } + + MDValueList.AssignValue( + LocalAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)), + NextMDValueNo++); + break; + } + case bitc::METADATA_OLD_NODE: { + // FIXME: Remove in 4.0. + if (Record.size() % 2 == 1) + return Error("Invalid record"); unsigned Size = Record.size(); - SmallVector<Value*, 8> Elts; + SmallVector<Metadata *, 8> Elts; for (unsigned i = 0; i != Size; i += 2) { Type *Ty = getTypeByID(Record[i]); if (!Ty) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); if (Ty->isMetadataTy()) Elts.push_back(MDValueList.getValueFwdRef(Record[i+1])); - else if (!Ty->isVoidTy()) - Elts.push_back(ValueList.getValueFwdRef(Record[i+1], Ty)); - else + else if (!Ty->isVoidTy()) { + auto *MD = + ValueAsMetadata::get(ValueList.getValueFwdRef(Record[i + 1], Ty)); + assert(isa<ConstantAsMetadata>(MD) && + "Expected non-function-local metadata"); + Elts.push_back(MD); + } else Elts.push_back(nullptr); } - Value *V = MDNode::getWhenValsUnresolved(Context, Elts, IsFunctionLocal); - IsFunctionLocal = false; - MDValueList.AssignValue(V, NextMDValueNo++); + MDValueList.AssignValue(MDNode::get(Context, Elts), NextMDValueNo++); + break; + } + case bitc::METADATA_VALUE: { + if (Record.size() != 2) + return Error("Invalid record"); + + Type *Ty = getTypeByID(Record[0]); + if (Ty->isMetadataTy() || Ty->isVoidTy()) + return Error("Invalid record"); + + MDValueList.AssignValue( + ValueAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)), + NextMDValueNo++); + break; + } + case bitc::METADATA_DISTINCT_NODE: + IsDistinct = true; + // fallthrough... + case bitc::METADATA_NODE: { + SmallVector<Metadata *, 8> Elts; + Elts.reserve(Record.size()); + for (unsigned ID : Record) + Elts.push_back(ID ? MDValueList.getValueFwdRef(ID - 1) : nullptr); + MDValueList.AssignValue(IsDistinct ? MDNode::getDistinct(Context, Elts) + : MDNode::get(Context, Elts), + NextMDValueNo++); + break; + } + case bitc::METADATA_LOCATION: { + if (Record.size() != 5) + return Error("Invalid record"); + + auto get = Record[0] ? MDLocation::getDistinct : MDLocation::get; + unsigned Line = Record[1]; + unsigned Column = Record[2]; + MDNode *Scope = cast<MDNode>(MDValueList.getValueFwdRef(Record[3])); + Metadata *InlinedAt = + Record[4] ? MDValueList.getValueFwdRef(Record[4] - 1) : nullptr; + MDValueList.AssignValue(get(Context, Line, Column, Scope, InlinedAt), + NextMDValueNo++); + break; + } + case bitc::METADATA_GENERIC_DEBUG: { + if (Record.size() < 4) + return Error("Invalid record"); + + unsigned Tag = Record[1]; + unsigned Version = Record[2]; + + if (Tag >= 1u << 16 || Version != 0) + return Error("Invalid record"); + + auto *Header = getMDString(Record[3]); + SmallVector<Metadata *, 8> DwarfOps; + for (unsigned I = 4, E = Record.size(); I != E; ++I) + DwarfOps.push_back(Record[I] ? MDValueList.getValueFwdRef(Record[I] - 1) + : nullptr); + MDValueList.AssignValue(GET_OR_DISTINCT(GenericDebugNode, Record[0], + (Context, Tag, Header, DwarfOps)), + NextMDValueNo++); + break; + } + case bitc::METADATA_SUBRANGE: { + if (Record.size() != 3) + return Error("Invalid record"); + + MDValueList.AssignValue( + GET_OR_DISTINCT(MDSubrange, Record[0], + (Context, Record[1], unrotateSign(Record[2]))), + NextMDValueNo++); + break; + } + case bitc::METADATA_ENUMERATOR: { + if (Record.size() != 3) + return Error("Invalid record"); + + MDValueList.AssignValue(GET_OR_DISTINCT(MDEnumerator, Record[0], + (Context, unrotateSign(Record[1]), + getMDString(Record[2]))), + NextMDValueNo++); + break; + } + case bitc::METADATA_BASIC_TYPE: { + if (Record.size() != 6) + return Error("Invalid record"); + + MDValueList.AssignValue( + GET_OR_DISTINCT(MDBasicType, Record[0], + (Context, Record[1], getMDString(Record[2]), + Record[3], Record[4], Record[5])), + NextMDValueNo++); + break; + } + case bitc::METADATA_DERIVED_TYPE: { + if (Record.size() != 12) + return Error("Invalid record"); + + MDValueList.AssignValue( + GET_OR_DISTINCT(MDDerivedType, Record[0], + (Context, Record[1], getMDString(Record[2]), + getMDOrNull(Record[3]), Record[4], + getMDOrNull(Record[5]), getMDOrNull(Record[6]), + Record[7], Record[8], Record[9], Record[10], + getMDOrNull(Record[11]))), + NextMDValueNo++); + break; + } + case bitc::METADATA_COMPOSITE_TYPE: { + if (Record.size() != 16) + return Error("Invalid record"); + + MDValueList.AssignValue( + GET_OR_DISTINCT(MDCompositeType, Record[0], + (Context, Record[1], getMDString(Record[2]), + getMDOrNull(Record[3]), Record[4], + getMDOrNull(Record[5]), getMDOrNull(Record[6]), + Record[7], Record[8], Record[9], Record[10], + getMDOrNull(Record[11]), Record[12], + getMDOrNull(Record[13]), getMDOrNull(Record[14]), + getMDString(Record[15]))), + NextMDValueNo++); + break; + } + case bitc::METADATA_SUBROUTINE_TYPE: { + if (Record.size() != 3) + return Error("Invalid record"); + + MDValueList.AssignValue( + GET_OR_DISTINCT(MDSubroutineType, Record[0], + (Context, Record[1], getMDOrNull(Record[2]))), + NextMDValueNo++); + break; + } + case bitc::METADATA_FILE: { + if (Record.size() != 3) + return Error("Invalid record"); + + MDValueList.AssignValue( + GET_OR_DISTINCT(MDFile, Record[0], (Context, getMDString(Record[1]), + getMDString(Record[2]))), + NextMDValueNo++); + break; + } + case bitc::METADATA_COMPILE_UNIT: { + if (Record.size() != 14) + return Error("Invalid record"); + + MDValueList.AssignValue( + GET_OR_DISTINCT(MDCompileUnit, Record[0], + (Context, Record[1], getMDOrNull(Record[2]), + getMDString(Record[3]), Record[4], + getMDString(Record[5]), Record[6], + getMDString(Record[7]), Record[8], + getMDOrNull(Record[9]), getMDOrNull(Record[10]), + getMDOrNull(Record[11]), getMDOrNull(Record[12]), + getMDOrNull(Record[13]))), + NextMDValueNo++); + break; + } + case bitc::METADATA_SUBPROGRAM: { + if (Record.size() != 19) + return Error("Invalid record"); + + MDValueList.AssignValue( + GET_OR_DISTINCT( + MDSubprogram, Record[0], + (Context, getMDOrNull(Record[1]), getMDString(Record[2]), + getMDString(Record[3]), getMDOrNull(Record[4]), Record[5], + getMDOrNull(Record[6]), Record[7], Record[8], Record[9], + getMDOrNull(Record[10]), Record[11], Record[12], Record[13], + Record[14], getMDOrNull(Record[15]), getMDOrNull(Record[16]), + getMDOrNull(Record[17]), getMDOrNull(Record[18]))), + NextMDValueNo++); + break; + } + case bitc::METADATA_LEXICAL_BLOCK: { + if (Record.size() != 5) + return Error("Invalid record"); + + MDValueList.AssignValue( + GET_OR_DISTINCT(MDLexicalBlock, Record[0], + (Context, getMDOrNull(Record[1]), + getMDOrNull(Record[2]), Record[3], Record[4])), + NextMDValueNo++); + break; + } + case bitc::METADATA_LEXICAL_BLOCK_FILE: { + if (Record.size() != 4) + return Error("Invalid record"); + + MDValueList.AssignValue( + GET_OR_DISTINCT(MDLexicalBlockFile, Record[0], + (Context, getMDOrNull(Record[1]), + getMDOrNull(Record[2]), Record[3])), + NextMDValueNo++); + break; + } + case bitc::METADATA_NAMESPACE: { + if (Record.size() != 5) + return Error("Invalid record"); + + MDValueList.AssignValue( + GET_OR_DISTINCT(MDNamespace, Record[0], + (Context, getMDOrNull(Record[1]), + getMDOrNull(Record[2]), getMDString(Record[3]), + Record[4])), + NextMDValueNo++); + break; + } + case bitc::METADATA_TEMPLATE_TYPE: { + if (Record.size() != 3) + return Error("Invalid record"); + + MDValueList.AssignValue(GET_OR_DISTINCT(MDTemplateTypeParameter, + Record[0], + (Context, getMDString(Record[1]), + getMDOrNull(Record[2]))), + NextMDValueNo++); + break; + } + case bitc::METADATA_TEMPLATE_VALUE: { + if (Record.size() != 5) + return Error("Invalid record"); + + MDValueList.AssignValue( + GET_OR_DISTINCT(MDTemplateValueParameter, Record[0], + (Context, Record[1], getMDString(Record[2]), + getMDOrNull(Record[3]), getMDOrNull(Record[4]))), + NextMDValueNo++); + break; + } + case bitc::METADATA_GLOBAL_VAR: { + if (Record.size() != 11) + return Error("Invalid record"); + + MDValueList.AssignValue( + GET_OR_DISTINCT(MDGlobalVariable, Record[0], + (Context, getMDOrNull(Record[1]), + getMDString(Record[2]), getMDString(Record[3]), + getMDOrNull(Record[4]), Record[5], + getMDOrNull(Record[6]), Record[7], Record[8], + getMDOrNull(Record[9]), getMDOrNull(Record[10]))), + NextMDValueNo++); + break; + } + case bitc::METADATA_LOCAL_VAR: { + if (Record.size() != 10) + return Error("Invalid record"); + + MDValueList.AssignValue( + GET_OR_DISTINCT(MDLocalVariable, Record[0], + (Context, Record[1], getMDOrNull(Record[2]), + getMDString(Record[3]), getMDOrNull(Record[4]), + Record[5], getMDOrNull(Record[6]), Record[7], + Record[8], getMDOrNull(Record[9]))), + NextMDValueNo++); + break; + } + case bitc::METADATA_EXPRESSION: { + if (Record.size() < 1) + return Error("Invalid record"); + + MDValueList.AssignValue( + GET_OR_DISTINCT(MDExpression, Record[0], + (Context, makeArrayRef(Record).slice(1))), + NextMDValueNo++); + break; + } + case bitc::METADATA_OBJC_PROPERTY: { + if (Record.size() != 8) + return Error("Invalid record"); + + MDValueList.AssignValue( + GET_OR_DISTINCT(MDObjCProperty, Record[0], + (Context, getMDString(Record[1]), + getMDOrNull(Record[2]), Record[3], + getMDString(Record[4]), getMDString(Record[5]), + Record[6], getMDOrNull(Record[7]))), + NextMDValueNo++); + break; + } + case bitc::METADATA_IMPORTED_ENTITY: { + if (Record.size() != 6) + return Error("Invalid record"); + + MDValueList.AssignValue( + GET_OR_DISTINCT(MDImportedEntity, Record[0], + (Context, Record[1], getMDOrNull(Record[2]), + getMDOrNull(Record[3]), Record[4], + getMDString(Record[5]))), + NextMDValueNo++); break; } case bitc::METADATA_STRING: { std::string String(Record.begin(), Record.end()); llvm::UpgradeMDStringConstant(String); - Value *V = MDString::get(Context, String); - MDValueList.AssignValue(V, NextMDValueNo++); + Metadata *MD = MDString::get(Context, String); + MDValueList.AssignValue(MD, NextMDValueNo++); break; } case bitc::METADATA_KIND: { if (Record.size() < 2) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); unsigned Kind = Record[0]; SmallString<8> Name(Record.begin()+1, Record.end()); unsigned NewKind = TheModule->getMDKindID(Name.str()); if (!MDKindMap.insert(std::make_pair(Kind, NewKind)).second) - return Error(BitcodeError::ConflictingMETADATA_KINDRecords); + return Error("Conflicting METADATA_KIND records"); break; } } } +#undef GET_OR_DISTINCT } /// decodeSignRotatedValue - Decode a signed value stored with the sign bit in @@ -1151,10 +1650,12 @@ std::error_code BitcodeReader::ResolveGlobalAndAliasInits() { std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInitWorklist; std::vector<std::pair<GlobalAlias*, unsigned> > AliasInitWorklist; std::vector<std::pair<Function*, unsigned> > FunctionPrefixWorklist; + std::vector<std::pair<Function*, unsigned> > FunctionPrologueWorklist; GlobalInitWorklist.swap(GlobalInits); AliasInitWorklist.swap(AliasInits); FunctionPrefixWorklist.swap(FunctionPrefixes); + FunctionPrologueWorklist.swap(FunctionPrologues); while (!GlobalInitWorklist.empty()) { unsigned ValID = GlobalInitWorklist.back().second; @@ -1165,7 +1666,7 @@ std::error_code BitcodeReader::ResolveGlobalAndAliasInits() { if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) GlobalInitWorklist.back().first->setInitializer(C); else - return Error(BitcodeError::ExpectedConstant); + return Error("Expected a constant"); } GlobalInitWorklist.pop_back(); } @@ -1178,7 +1679,7 @@ std::error_code BitcodeReader::ResolveGlobalAndAliasInits() { if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) AliasInitWorklist.back().first->setAliasee(C); else - return Error(BitcodeError::ExpectedConstant); + return Error("Expected a constant"); } AliasInitWorklist.pop_back(); } @@ -1191,11 +1692,24 @@ std::error_code BitcodeReader::ResolveGlobalAndAliasInits() { if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) FunctionPrefixWorklist.back().first->setPrefixData(C); else - return Error(BitcodeError::ExpectedConstant); + return Error("Expected a constant"); } FunctionPrefixWorklist.pop_back(); } + while (!FunctionPrologueWorklist.empty()) { + unsigned ValID = FunctionPrologueWorklist.back().second; + if (ValID >= ValueList.size()) { + FunctionPrologues.push_back(FunctionPrologueWorklist.back()); + } else { + if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID])) + FunctionPrologueWorklist.back().first->setPrologueData(C); + else + return Error("Expected a constant"); + } + FunctionPrologueWorklist.pop_back(); + } + return std::error_code(); } @@ -1209,7 +1723,7 @@ static APInt ReadWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) { std::error_code BitcodeReader::ParseConstants() { if (Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); SmallVector<uint64_t, 64> Record; @@ -1222,10 +1736,10 @@ std::error_code BitcodeReader::ParseConstants() { switch (Entry.Kind) { case BitstreamEntry::SubBlock: // Handled for us already. case BitstreamEntry::Error: - return Error(BitcodeError::MalformedBlock); + return Error("Malformed block"); case BitstreamEntry::EndBlock: if (NextCstNo != ValueList.size()) - return Error(BitcodeError::InvalidConstantReference); + return Error("Invalid ronstant reference"); // Once all the constants have been read, go through and resolve forward // references. @@ -1247,9 +1761,9 @@ std::error_code BitcodeReader::ParseConstants() { break; case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid] if (Record.empty()) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); if (Record[0] >= TypeList.size() || !TypeList[Record[0]]) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); CurTy = TypeList[Record[0]]; continue; // Skip the ValueList manipulation. case bitc::CST_CODE_NULL: // NULL @@ -1257,12 +1771,12 @@ std::error_code BitcodeReader::ParseConstants() { break; case bitc::CST_CODE_INTEGER: // INTEGER: [intval] if (!CurTy->isIntegerTy() || Record.empty()) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); V = ConstantInt::get(CurTy, decodeSignRotatedValue(Record[0])); break; case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval] if (!CurTy->isIntegerTy() || Record.empty()) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); APInt VInt = ReadWideAPInt(Record, cast<IntegerType>(CurTy)->getBitWidth()); @@ -1272,7 +1786,7 @@ std::error_code BitcodeReader::ParseConstants() { } case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval] if (Record.empty()) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); if (CurTy->isHalfTy()) V = ConstantFP::get(Context, APFloat(APFloat::IEEEhalf, APInt(16, (uint16_t)Record[0]))); @@ -1302,7 +1816,7 @@ std::error_code BitcodeReader::ParseConstants() { case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number] if (Record.empty()) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); unsigned Size = Record.size(); SmallVector<Constant*, 16> Elts; @@ -1330,7 +1844,7 @@ std::error_code BitcodeReader::ParseConstants() { case bitc::CST_CODE_STRING: // STRING: [values] case bitc::CST_CODE_CSTRING: { // CSTRING: [values] if (Record.empty()) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); SmallString<16> Elts(Record.begin(), Record.end()); V = ConstantDataArray::getString(Context, Elts, @@ -1339,7 +1853,7 @@ std::error_code BitcodeReader::ParseConstants() { } case bitc::CST_CODE_DATA: {// DATA: [n x value] if (Record.empty()) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); Type *EltTy = cast<SequentialType>(CurTy)->getElementType(); unsigned Size = Record.size(); @@ -1384,14 +1898,14 @@ std::error_code BitcodeReader::ParseConstants() { else V = ConstantDataArray::get(Context, Elts); } else { - return Error(BitcodeError::InvalidTypeForValue); + return Error("Invalid type for value"); } break; } case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval] if (Record.size() < 3) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); int Opc = GetDecodedBinaryOpcode(Record[0], CurTy); if (Opc < 0) { V = UndefValue::get(CurTy); // Unknown binop. @@ -1422,14 +1936,14 @@ std::error_code BitcodeReader::ParseConstants() { } case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval] if (Record.size() < 3) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); int Opc = GetDecodedCastOpcode(Record[0]); if (Opc < 0) { V = UndefValue::get(CurTy); // Unknown cast. } else { Type *OpTy = getTypeByID(Record[1]); if (!OpTy) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy); V = UpgradeBitCastExpr(Opc, Op, CurTy); if (!V) V = ConstantExpr::getCast(Opc, Op, CurTy); @@ -1439,12 +1953,12 @@ std::error_code BitcodeReader::ParseConstants() { case bitc::CST_CODE_CE_INBOUNDS_GEP: case bitc::CST_CODE_CE_GEP: { // CE_GEP: [n x operands] if (Record.size() & 1) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); SmallVector<Constant*, 16> Elts; for (unsigned i = 0, e = Record.size(); i != e; i += 2) { Type *ElTy = getTypeByID(Record[i]); if (!ElTy) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); Elts.push_back(ValueList.getConstantFwdRef(Record[i+1], ElTy)); } ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end()); @@ -1455,7 +1969,7 @@ std::error_code BitcodeReader::ParseConstants() { } case bitc::CST_CODE_CE_SELECT: { // CE_SELECT: [opval#, opval#, opval#] if (Record.size() < 3) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); Type *SelectorTy = Type::getInt1Ty(Context); @@ -1474,22 +1988,22 @@ std::error_code BitcodeReader::ParseConstants() { case bitc::CST_CODE_CE_EXTRACTELT : { // CE_EXTRACTELT: [opty, opval, opty, opval] if (Record.size() < 3) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); VectorType *OpTy = dyn_cast_or_null<VectorType>(getTypeByID(Record[0])); if (!OpTy) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); Constant *Op1 = nullptr; if (Record.size() == 4) { Type *IdxTy = getTypeByID(Record[2]); if (!IdxTy) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); Op1 = ValueList.getConstantFwdRef(Record[3], IdxTy); } else // TODO: Remove with llvm 4.0 Op1 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context)); if (!Op1) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); V = ConstantExpr::getExtractElement(Op0, Op1); break; } @@ -1497,7 +2011,7 @@ std::error_code BitcodeReader::ParseConstants() { : { // CE_INSERTELT: [opval, opval, opty, opval] VectorType *OpTy = dyn_cast<VectorType>(CurTy); if (Record.size() < 3 || !OpTy) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy); Constant *Op1 = ValueList.getConstantFwdRef(Record[1], OpTy->getElementType()); @@ -1505,19 +2019,19 @@ std::error_code BitcodeReader::ParseConstants() { if (Record.size() == 4) { Type *IdxTy = getTypeByID(Record[2]); if (!IdxTy) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); Op2 = ValueList.getConstantFwdRef(Record[3], IdxTy); } else // TODO: Remove with llvm 4.0 Op2 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context)); if (!Op2) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); V = ConstantExpr::getInsertElement(Op0, Op1, Op2); break; } case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval] VectorType *OpTy = dyn_cast<VectorType>(CurTy); if (Record.size() < 3 || !OpTy) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy); Constant *Op1 = ValueList.getConstantFwdRef(Record[1], OpTy); Type *ShufTy = VectorType::get(Type::getInt32Ty(Context), @@ -1531,7 +2045,7 @@ std::error_code BitcodeReader::ParseConstants() { VectorType *OpTy = dyn_cast_or_null<VectorType>(getTypeByID(Record[0])); if (Record.size() < 4 || !RTy || !OpTy) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy); Type *ShufTy = VectorType::get(Type::getInt32Ty(Context), @@ -1542,10 +2056,10 @@ std::error_code BitcodeReader::ParseConstants() { } case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred] if (Record.size() < 4) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); Type *OpTy = getTypeByID(Record[0]); if (!OpTy) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy); @@ -1559,16 +2073,16 @@ std::error_code BitcodeReader::ParseConstants() { // FIXME: Remove with the 4.0 release. case bitc::CST_CODE_INLINEASM_OLD: { if (Record.size() < 2) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); std::string AsmStr, ConstrStr; bool HasSideEffects = Record[0] & 1; bool IsAlignStack = Record[0] >> 1; unsigned AsmStrSize = Record[1]; if (2+AsmStrSize >= Record.size()) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); unsigned ConstStrSize = Record[2+AsmStrSize]; if (3+AsmStrSize+ConstStrSize > Record.size()) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); for (unsigned i = 0; i != AsmStrSize; ++i) AsmStr += (char)Record[2+i]; @@ -1583,17 +2097,17 @@ std::error_code BitcodeReader::ParseConstants() { // inteldialect). case bitc::CST_CODE_INLINEASM: { if (Record.size() < 2) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); std::string AsmStr, ConstrStr; bool HasSideEffects = Record[0] & 1; bool IsAlignStack = (Record[0] >> 1) & 1; unsigned AsmDialect = Record[0] >> 2; unsigned AsmStrSize = Record[1]; if (2+AsmStrSize >= Record.size()) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); unsigned ConstStrSize = Record[2+AsmStrSize]; if (3+AsmStrSize+ConstStrSize > Record.size()) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); for (unsigned i = 0; i != AsmStrSize; ++i) AsmStr += (char)Record[2+i]; @@ -1607,14 +2121,14 @@ std::error_code BitcodeReader::ParseConstants() { } case bitc::CST_CODE_BLOCKADDRESS:{ if (Record.size() < 3) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); Type *FnTy = getTypeByID(Record[0]); if (!FnTy) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); Function *Fn = dyn_cast_or_null<Function>(ValueList.getConstantFwdRef(Record[1],FnTy)); if (!Fn) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); // Don't let Fn get dematerialized. BlockAddressesTaken.insert(Fn); @@ -1625,12 +2139,12 @@ std::error_code BitcodeReader::ParseConstants() { unsigned BBID = Record[2]; if (!BBID) // Invalid reference to entry block. - return Error(BitcodeError::InvalidID); + return Error("Invalid ID"); if (!Fn->empty()) { Function::iterator BBI = Fn->begin(), BBE = Fn->end(); for (size_t I = 0, E = BBID; I != E; ++I) { if (BBI == BBE) - return Error(BitcodeError::InvalidID); + return Error("Invalid ID"); ++BBI; } BB = BBI; @@ -1658,7 +2172,7 @@ std::error_code BitcodeReader::ParseConstants() { std::error_code BitcodeReader::ParseUseLists() { if (Stream.EnterSubBlock(bitc::USELIST_BLOCK_ID)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); // Read all the records. SmallVector<uint64_t, 64> Record; @@ -1668,7 +2182,7 @@ std::error_code BitcodeReader::ParseUseLists() { switch (Entry.Kind) { case BitstreamEntry::SubBlock: // Handled for us already. case BitstreamEntry::Error: - return Error(BitcodeError::MalformedBlock); + return Error("Malformed block"); case BitstreamEntry::EndBlock: return std::error_code(); case BitstreamEntry::Record: @@ -1689,7 +2203,7 @@ std::error_code BitcodeReader::ParseUseLists() { unsigned RecordLength = Record.size(); if (RecordLength < 3) // Records should have at least an ID and two indexes. - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); unsigned ID = Record.back(); Record.pop_back(); @@ -1726,7 +2240,7 @@ std::error_code BitcodeReader::ParseUseLists() { std::error_code BitcodeReader::RememberAndSkipFunctionBody() { // Get the function we are talking about. if (FunctionsWithBodies.empty()) - return Error(BitcodeError::InsufficientFunctionProtos); + return Error("Insufficient function protos"); Function *Fn = FunctionsWithBodies.back(); FunctionsWithBodies.pop_back(); @@ -1737,7 +2251,7 @@ std::error_code BitcodeReader::RememberAndSkipFunctionBody() { // Skip over the function block for now. if (Stream.SkipBlock()) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); return std::error_code(); } @@ -1745,7 +2259,7 @@ std::error_code BitcodeReader::GlobalCleanup() { // Patch the initializers for globals and aliases up. ResolveGlobalAndAliasInits(); if (!GlobalInits.empty() || !AliasInits.empty()) - return Error(BitcodeError::MalformedGlobalInitializerSet); + return Error("Malformed global initializer set"); // Look for intrinsic functions which need to be upgraded at some point for (Module::iterator FI = TheModule->begin(), FE = TheModule->end(); @@ -1774,7 +2288,7 @@ std::error_code BitcodeReader::ParseModule(bool Resume) { if (Resume) Stream.JumpToBit(NextUnreadBit); else if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); SmallVector<uint64_t, 64> Record; std::vector<std::string> SectionTable; @@ -1786,7 +2300,7 @@ std::error_code BitcodeReader::ParseModule(bool Resume) { switch (Entry.Kind) { case BitstreamEntry::Error: - return Error(BitcodeError::MalformedBlock); + return Error("Malformed block"); case BitstreamEntry::EndBlock: return GlobalCleanup(); @@ -1794,11 +2308,11 @@ std::error_code BitcodeReader::ParseModule(bool Resume) { switch (Entry.ID) { default: // Skip unknown content. if (Stream.SkipBlock()) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); break; case bitc::BLOCKINFO_BLOCK_ID: if (Stream.ReadBlockInfoBlock()) - return Error(BitcodeError::MalformedBlock); + return Error("Malformed block"); break; case bitc::PARAMATTR_BLOCK_ID: if (std::error_code EC = ParseAttributeBlock()) @@ -1868,12 +2382,12 @@ std::error_code BitcodeReader::ParseModule(bool Resume) { default: break; // Default behavior, ignore unknown content. case bitc::MODULE_CODE_VERSION: { // VERSION: [version#] if (Record.size() < 1) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); // Only version #0 and #1 are supported so far. unsigned module_version = Record[0]; switch (module_version) { default: - return Error(BitcodeError::InvalidValue); + return Error("Invalid value"); case 0: UseRelativeIDs = false; break; @@ -1886,21 +2400,21 @@ std::error_code BitcodeReader::ParseModule(bool Resume) { case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N] std::string S; if (ConvertToString(Record, 0, S)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); TheModule->setTargetTriple(S); break; } case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N] std::string S; if (ConvertToString(Record, 0, S)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); TheModule->setDataLayout(S); break; } case bitc::MODULE_CODE_ASM: { // ASM: [strchr x N] std::string S; if (ConvertToString(Record, 0, S)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); TheModule->setModuleInlineAsm(S); break; } @@ -1908,27 +2422,27 @@ std::error_code BitcodeReader::ParseModule(bool Resume) { // FIXME: Remove in 4.0. std::string S; if (ConvertToString(Record, 0, S)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); // Ignore value. break; } case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N] std::string S; if (ConvertToString(Record, 0, S)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); SectionTable.push_back(S); break; } case bitc::MODULE_CODE_GCNAME: { // SECTIONNAME: [strchr x N] std::string S; if (ConvertToString(Record, 0, S)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); GCTable.push_back(S); break; } case bitc::MODULE_CODE_COMDAT: { // COMDAT: [selection_kind, name] if (Record.size() < 2) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); Comdat::SelectionKind SK = getDecodedComdatSelectionKind(Record[0]); unsigned ComdatNameSize = Record[1]; std::string ComdatName; @@ -1942,25 +2456,29 @@ std::error_code BitcodeReader::ParseModule(bool Resume) { } // GLOBALVAR: [pointer type, isconst, initid, // linkage, alignment, section, visibility, threadlocal, - // unnamed_addr, dllstorageclass] + // unnamed_addr, externally_initialized, dllstorageclass, + // comdat] case bitc::MODULE_CODE_GLOBALVAR: { if (Record.size() < 6) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); Type *Ty = getTypeByID(Record[0]); if (!Ty) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); if (!Ty->isPointerTy()) - return Error(BitcodeError::InvalidTypeForValue); + return Error("Invalid type for value"); unsigned AddressSpace = cast<PointerType>(Ty)->getAddressSpace(); Ty = cast<PointerType>(Ty)->getElementType(); bool isConstant = Record[1]; - GlobalValue::LinkageTypes Linkage = GetDecodedLinkage(Record[3]); - unsigned Alignment = (1 << Record[4]) >> 1; + uint64_t RawLinkage = Record[3]; + GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage); + unsigned Alignment; + if (std::error_code EC = parseAlignmentValue(Record[4], Alignment)) + return EC; std::string Section; if (Record[5]) { if (Record[5]-1 >= SectionTable.size()) - return Error(BitcodeError::InvalidID); + return Error("Invalid ID"); Section = SectionTable[Record[5]-1]; } GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility; @@ -1993,7 +2511,7 @@ std::error_code BitcodeReader::ParseModule(bool Resume) { if (Record.size() > 10) NewGV->setDLLStorageClass(GetDecodedDLLStorageClass(Record[10])); else - UpgradeDLLImportExportLinkage(NewGV, Record[3]); + UpgradeDLLImportExportLinkage(NewGV, RawLinkage); ValueList.push_back(NewGV); @@ -2001,41 +2519,48 @@ std::error_code BitcodeReader::ParseModule(bool Resume) { if (unsigned InitID = Record[2]) GlobalInits.push_back(std::make_pair(NewGV, InitID-1)); - if (Record.size() > 11) + if (Record.size() > 11) { if (unsigned ComdatID = Record[11]) { assert(ComdatID <= ComdatList.size()); NewGV->setComdat(ComdatList[ComdatID - 1]); } + } else if (hasImplicitComdat(RawLinkage)) { + NewGV->setComdat(reinterpret_cast<Comdat *>(1)); + } break; } // FUNCTION: [type, callingconv, isproto, linkage, paramattr, // alignment, section, visibility, gc, unnamed_addr, - // dllstorageclass] + // prologuedata, dllstorageclass, comdat, prefixdata] case bitc::MODULE_CODE_FUNCTION: { if (Record.size() < 8) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); Type *Ty = getTypeByID(Record[0]); if (!Ty) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); if (!Ty->isPointerTy()) - return Error(BitcodeError::InvalidTypeForValue); + return Error("Invalid type for value"); FunctionType *FTy = dyn_cast<FunctionType>(cast<PointerType>(Ty)->getElementType()); if (!FTy) - return Error(BitcodeError::InvalidTypeForValue); + return Error("Invalid type for value"); Function *Func = Function::Create(FTy, GlobalValue::ExternalLinkage, "", TheModule); Func->setCallingConv(static_cast<CallingConv::ID>(Record[1])); bool isProto = Record[2]; - Func->setLinkage(GetDecodedLinkage(Record[3])); + uint64_t RawLinkage = Record[3]; + Func->setLinkage(getDecodedLinkage(RawLinkage)); Func->setAttributes(getAttributes(Record[4])); - Func->setAlignment((1 << Record[5]) >> 1); + unsigned Alignment; + if (std::error_code EC = parseAlignmentValue(Record[5], Alignment)) + return EC; + Func->setAlignment(Alignment); if (Record[6]) { if (Record[6]-1 >= SectionTable.size()) - return Error(BitcodeError::InvalidID); + return Error("Invalid ID"); Func->setSection(SectionTable[Record[6]-1]); } // Local linkage must have default visibility. @@ -2044,7 +2569,7 @@ std::error_code BitcodeReader::ParseModule(bool Resume) { Func->setVisibility(GetDecodedVisibility(Record[7])); if (Record.size() > 8 && Record[8]) { if (Record[8]-1 > GCTable.size()) - return Error(BitcodeError::InvalidID); + return Error("Invalid ID"); Func->setGC(GCTable[Record[8]-1].c_str()); } bool UnnamedAddr = false; @@ -2052,18 +2577,24 @@ std::error_code BitcodeReader::ParseModule(bool Resume) { UnnamedAddr = Record[9]; Func->setUnnamedAddr(UnnamedAddr); if (Record.size() > 10 && Record[10] != 0) - FunctionPrefixes.push_back(std::make_pair(Func, Record[10]-1)); + FunctionPrologues.push_back(std::make_pair(Func, Record[10]-1)); if (Record.size() > 11) Func->setDLLStorageClass(GetDecodedDLLStorageClass(Record[11])); else - UpgradeDLLImportExportLinkage(Func, Record[3]); + UpgradeDLLImportExportLinkage(Func, RawLinkage); - if (Record.size() > 12) + if (Record.size() > 12) { if (unsigned ComdatID = Record[12]) { assert(ComdatID <= ComdatList.size()); Func->setComdat(ComdatList[ComdatID - 1]); } + } else if (hasImplicitComdat(RawLinkage)) { + Func->setComdat(reinterpret_cast<Comdat *>(1)); + } + + if (Record.size() > 13 && Record[13] != 0) + FunctionPrefixes.push_back(std::make_pair(Func, Record[13]-1)); ValueList.push_back(Func); @@ -2081,17 +2612,17 @@ std::error_code BitcodeReader::ParseModule(bool Resume) { // ALIAS: [alias type, aliasee val#, linkage, visibility, dllstorageclass] case bitc::MODULE_CODE_ALIAS: { if (Record.size() < 3) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); Type *Ty = getTypeByID(Record[0]); if (!Ty) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); auto *PTy = dyn_cast<PointerType>(Ty); if (!PTy) - return Error(BitcodeError::InvalidTypeForValue); + return Error("Invalid type for value"); auto *NewGA = GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(), - GetDecodedLinkage(Record[2]), "", TheModule); + getDecodedLinkage(Record[2]), "", TheModule); // Old bitcode files didn't have visibility field. // Local linkage must have default visibility. if (Record.size() > 3 && !NewGA->hasLocalLinkage()) @@ -2113,7 +2644,7 @@ std::error_code BitcodeReader::ParseModule(bool Resume) { case bitc::MODULE_CODE_PURGEVALS: // Trim down the value list to the specified size. if (Record.size() < 1 || Record[0] > ValueList.size()) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); ValueList.shrinkTo(Record[0]); break; } @@ -2134,7 +2665,7 @@ std::error_code BitcodeReader::ParseBitcodeInto(Module *M) { Stream.Read(4) != 0xC || Stream.Read(4) != 0xE || Stream.Read(4) != 0xD) - return Error(BitcodeError::InvalidBitcodeSignature); + return Error("Invalid bitcode signature"); // We expect a number of well-defined blocks, though we don't necessarily // need to understand them all. @@ -2147,7 +2678,7 @@ std::error_code BitcodeReader::ParseBitcodeInto(Module *M) { switch (Entry.Kind) { case BitstreamEntry::Error: - return Error(BitcodeError::MalformedBlock); + return Error("Malformed block"); case BitstreamEntry::EndBlock: return std::error_code(); @@ -2155,12 +2686,12 @@ std::error_code BitcodeReader::ParseBitcodeInto(Module *M) { switch (Entry.ID) { case bitc::BLOCKINFO_BLOCK_ID: if (Stream.ReadBlockInfoBlock()) - return Error(BitcodeError::MalformedBlock); + return Error("Malformed block"); break; case bitc::MODULE_BLOCK_ID: // Reject multiple MODULE_BLOCK's in a single bitstream. if (TheModule) - return Error(BitcodeError::InvalidMultipleBlocks); + return Error("Invalid multiple blocks"); TheModule = M; if (std::error_code EC = ParseModule(false)) return EC; @@ -2169,7 +2700,7 @@ std::error_code BitcodeReader::ParseBitcodeInto(Module *M) { break; default: if (Stream.SkipBlock()) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); break; } continue; @@ -2184,14 +2715,14 @@ std::error_code BitcodeReader::ParseBitcodeInto(Module *M) { Stream.AtEndOfStream()) return std::error_code(); - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); } } } ErrorOr<std::string> BitcodeReader::parseModuleTriple() { if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); SmallVector<uint64_t, 64> Record; @@ -2203,7 +2734,7 @@ ErrorOr<std::string> BitcodeReader::parseModuleTriple() { switch (Entry.Kind) { case BitstreamEntry::SubBlock: // Handled for us already. case BitstreamEntry::Error: - return Error(BitcodeError::MalformedBlock); + return Error("Malformed block"); case BitstreamEntry::EndBlock: return Triple; case BitstreamEntry::Record: @@ -2217,7 +2748,7 @@ ErrorOr<std::string> BitcodeReader::parseModuleTriple() { case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N] std::string S; if (ConvertToString(Record, 0, S)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); Triple = S; break; } @@ -2238,7 +2769,7 @@ ErrorOr<std::string> BitcodeReader::parseTriple() { Stream.Read(4) != 0xC || Stream.Read(4) != 0xE || Stream.Read(4) != 0xD) - return Error(BitcodeError::InvalidBitcodeSignature); + return Error("Invalid bitcode signature"); // We expect a number of well-defined blocks, though we don't necessarily // need to understand them all. @@ -2247,7 +2778,7 @@ ErrorOr<std::string> BitcodeReader::parseTriple() { switch (Entry.Kind) { case BitstreamEntry::Error: - return Error(BitcodeError::MalformedBlock); + return Error("Malformed block"); case BitstreamEntry::EndBlock: return std::error_code(); @@ -2257,7 +2788,7 @@ ErrorOr<std::string> BitcodeReader::parseTriple() { // Ignore other sub-blocks. if (Stream.SkipBlock()) - return Error(BitcodeError::MalformedBlock); + return Error("Malformed block"); continue; case BitstreamEntry::Record: @@ -2270,7 +2801,7 @@ ErrorOr<std::string> BitcodeReader::parseTriple() { /// ParseMetadataAttachment - Parse metadata attachments. std::error_code BitcodeReader::ParseMetadataAttachment() { if (Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); SmallVector<uint64_t, 64> Record; while (1) { @@ -2279,7 +2810,7 @@ std::error_code BitcodeReader::ParseMetadataAttachment() { switch (Entry.Kind) { case BitstreamEntry::SubBlock: // Handled for us already. case BitstreamEntry::Error: - return Error(BitcodeError::MalformedBlock); + return Error("Malformed block"); case BitstreamEntry::EndBlock: return std::error_code(); case BitstreamEntry::Record: @@ -2295,15 +2826,19 @@ std::error_code BitcodeReader::ParseMetadataAttachment() { case bitc::METADATA_ATTACHMENT: { unsigned RecordLength = Record.size(); if (Record.empty() || (RecordLength - 1) % 2 == 1) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); Instruction *Inst = InstructionList[Record[0]]; for (unsigned i = 1; i != RecordLength; i = i+2) { unsigned Kind = Record[i]; DenseMap<unsigned, unsigned>::iterator I = MDKindMap.find(Kind); if (I == MDKindMap.end()) - return Error(BitcodeError::InvalidID); - Value *Node = MDValueList.getValueFwdRef(Record[i+1]); + return Error("Invalid ID"); + Metadata *Node = MDValueList.getValueFwdRef(Record[i + 1]); + if (isa<LocalAsMetadata>(Node)) + // Drop the attachment. This used to be legal, but there's no + // upgrade path. + break; Inst->setMetadata(I->second, cast<MDNode>(Node)); if (I->second == LLVMContext::MD_tbaa) InstsWithTBAATag.push_back(Inst); @@ -2317,7 +2852,7 @@ std::error_code BitcodeReader::ParseMetadataAttachment() { /// ParseFunctionBody - Lazily parse the specified function body block. std::error_code BitcodeReader::ParseFunctionBody(Function *F) { if (Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); InstructionList.clear(); unsigned ModuleValueListSize = ValueList.size(); @@ -2332,6 +2867,14 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { unsigned CurBBNo = 0; DebugLoc LastLoc; + auto getLastInstruction = [&]() -> Instruction * { + if (CurBB && !CurBB->empty()) + return &CurBB->back(); + else if (CurBBNo && FunctionBBs[CurBBNo - 1] && + !FunctionBBs[CurBBNo - 1]->empty()) + return &FunctionBBs[CurBBNo - 1]->back(); + return nullptr; + }; // Read all the records. SmallVector<uint64_t, 64> Record; @@ -2340,7 +2883,7 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { switch (Entry.Kind) { case BitstreamEntry::Error: - return Error(BitcodeError::MalformedBlock); + return Error("Malformed block"); case BitstreamEntry::EndBlock: goto OutOfRecordLoop; @@ -2348,7 +2891,7 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { switch (Entry.ID) { default: // Skip unknown content. if (Stream.SkipBlock()) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); break; case bitc::CONSTANTS_BLOCK_ID: if (std::error_code EC = ParseConstants()) @@ -2385,10 +2928,10 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { unsigned BitCode = Stream.readRecord(Entry.ID, Record); switch (BitCode) { default: // Default behavior: reject - return Error(BitcodeError::InvalidValue); + return Error("Invalid value"); case bitc::FUNC_CODE_DECLAREBLOCKS: { // DECLAREBLOCKS: [nblocks] if (Record.size() < 1 || Record[0] == 0) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); // Create all the basic blocks for the function. FunctionBBs.resize(Record[0]); @@ -2401,7 +2944,7 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { auto &BBRefs = BBFRI->second; // Check for invalid basic block references. if (BBRefs.size() > FunctionBBs.size()) - return Error(BitcodeError::InvalidID); + return Error("Invalid ID"); assert(!BBRefs.empty() && "Unexpected empty array"); assert(!BBRefs.front() && "Invalid reference to entry block"); for (unsigned I = 0, E = FunctionBBs.size(), RE = BBRefs.size(); I != E; @@ -2424,30 +2967,18 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: // DEBUG_LOC_AGAIN // This record indicates that the last instruction is at the same // location as the previous instruction with a location. - I = nullptr; - - // Get the last instruction emitted. - if (CurBB && !CurBB->empty()) - I = &CurBB->back(); - else if (CurBBNo && FunctionBBs[CurBBNo-1] && - !FunctionBBs[CurBBNo-1]->empty()) - I = &FunctionBBs[CurBBNo-1]->back(); + I = getLastInstruction(); if (!I) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); I->setDebugLoc(LastLoc); I = nullptr; continue; case bitc::FUNC_CODE_DEBUG_LOC: { // DEBUG_LOC: [line, col, scope, ia] - I = nullptr; // Get the last instruction emitted. - if (CurBB && !CurBB->empty()) - I = &CurBB->back(); - else if (CurBBNo && FunctionBBs[CurBBNo-1] && - !FunctionBBs[CurBBNo-1]->empty()) - I = &FunctionBBs[CurBBNo-1]->back(); + I = getLastInstruction(); if (!I || Record.size() < 4) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); unsigned Line = Record[0], Col = Record[1]; unsigned ScopeID = Record[2], IAID = Record[3]; @@ -2467,11 +2998,11 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { if (getValueTypePair(Record, OpNum, NextValueNo, LHS) || popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) || OpNum+1 > Record.size()) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); int Opc = GetDecodedBinaryOpcode(Record[OpNum++], LHS->getType()); if (Opc == -1) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); InstructionList.push_back(I); if (OpNum < Record.size()) { @@ -2513,12 +3044,12 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { Value *Op; if (getValueTypePair(Record, OpNum, NextValueNo, Op) || OpNum+2 != Record.size()) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); Type *ResTy = getTypeByID(Record[OpNum]); int Opc = GetDecodedCastOpcode(Record[OpNum+1]); if (Opc == -1 || !ResTy) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); Instruction *Temp = nullptr; if ((I = UpgradeBitCastInst(Opc, Op, ResTy, Temp))) { if (Temp) { @@ -2531,24 +3062,38 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { InstructionList.push_back(I); break; } - case bitc::FUNC_CODE_INST_INBOUNDS_GEP: - case bitc::FUNC_CODE_INST_GEP: { // GEP: [n x operands] + case bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD: + case bitc::FUNC_CODE_INST_GEP_OLD: + case bitc::FUNC_CODE_INST_GEP: { // GEP: type, [n x operands] unsigned OpNum = 0; + + Type *Ty; + bool InBounds; + + if (BitCode == bitc::FUNC_CODE_INST_GEP) { + InBounds = Record[OpNum++]; + Ty = getTypeByID(Record[OpNum++]); + } else { + InBounds = BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD; + Ty = nullptr; + } + Value *BasePtr; if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); SmallVector<Value*, 16> GEPIdx; while (OpNum != Record.size()) { Value *Op; if (getValueTypePair(Record, OpNum, NextValueNo, Op)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); GEPIdx.push_back(Op); } I = GetElementPtrInst::Create(BasePtr, GEPIdx); + assert(!Ty || Ty == cast<GetElementPtrInst>(I)->getSourceElementType()); InstructionList.push_back(I); - if (BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP) + if (InBounds) cast<GetElementPtrInst>(I)->setIsInBounds(true); break; } @@ -2558,15 +3103,30 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { unsigned OpNum = 0; Value *Agg; if (getValueTypePair(Record, OpNum, NextValueNo, Agg)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); SmallVector<unsigned, 4> EXTRACTVALIdx; + Type *CurTy = Agg->getType(); for (unsigned RecSize = Record.size(); OpNum != RecSize; ++OpNum) { + bool IsArray = CurTy->isArrayTy(); + bool IsStruct = CurTy->isStructTy(); uint64_t Index = Record[OpNum]; + + if (!IsStruct && !IsArray) + return Error("EXTRACTVAL: Invalid type"); if ((unsigned)Index != Index) - return Error(BitcodeError::InvalidValue); + return Error("Invalid value"); + if (IsStruct && Index >= CurTy->subtypes().size()) + return Error("EXTRACTVAL: Invalid struct index"); + if (IsArray && Index >= CurTy->getArrayNumElements()) + return Error("EXTRACTVAL: Invalid array index"); EXTRACTVALIdx.push_back((unsigned)Index); + + if (IsStruct) + CurTy = CurTy->subtypes()[Index]; + else + CurTy = CurTy->subtypes()[0]; } I = ExtractValueInst::Create(Agg, EXTRACTVALIdx); @@ -2579,18 +3139,35 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { unsigned OpNum = 0; Value *Agg; if (getValueTypePair(Record, OpNum, NextValueNo, Agg)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); Value *Val; if (getValueTypePair(Record, OpNum, NextValueNo, Val)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); SmallVector<unsigned, 4> INSERTVALIdx; + Type *CurTy = Agg->getType(); for (unsigned RecSize = Record.size(); OpNum != RecSize; ++OpNum) { + bool IsArray = CurTy->isArrayTy(); + bool IsStruct = CurTy->isStructTy(); uint64_t Index = Record[OpNum]; + + if (!IsStruct && !IsArray) + return Error("INSERTVAL: Invalid type"); + if (!CurTy->isStructTy() && !CurTy->isArrayTy()) + return Error("Invalid type"); if ((unsigned)Index != Index) - return Error(BitcodeError::InvalidValue); + return Error("Invalid value"); + if (IsStruct && Index >= CurTy->subtypes().size()) + return Error("INSERTVAL: Invalid struct index"); + if (IsArray && Index >= CurTy->getArrayNumElements()) + return Error("INSERTVAL: Invalid array index"); + INSERTVALIdx.push_back((unsigned)Index); + if (IsStruct) + CurTy = CurTy->subtypes()[Index]; + else + CurTy = CurTy->subtypes()[0]; } I = InsertValueInst::Create(Agg, Val, INSERTVALIdx); @@ -2606,7 +3183,7 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) || popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) || popValue(Record, OpNum, NextValueNo, Type::getInt1Ty(Context), Cond)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); I = SelectInst::Create(Cond, TrueVal, FalseVal); InstructionList.push_back(I); @@ -2621,18 +3198,18 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) || popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) || getValueTypePair(Record, OpNum, NextValueNo, Cond)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); // select condition can be either i1 or [N x i1] if (VectorType* vector_type = dyn_cast<VectorType>(Cond->getType())) { // expect <n x i1> if (vector_type->getElementType() != Type::getInt1Ty(Context)) - return Error(BitcodeError::InvalidTypeForValue); + return Error("Invalid type for value"); } else { // expect i1 if (Cond->getType() != Type::getInt1Ty(Context)) - return Error(BitcodeError::InvalidTypeForValue); + return Error("Invalid type for value"); } I = SelectInst::Create(Cond, TrueVal, FalseVal); @@ -2645,7 +3222,7 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { Value *Vec, *Idx; if (getValueTypePair(Record, OpNum, NextValueNo, Vec) || getValueTypePair(Record, OpNum, NextValueNo, Idx)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); I = ExtractElementInst::Create(Vec, Idx); InstructionList.push_back(I); break; @@ -2658,7 +3235,7 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { popValue(Record, OpNum, NextValueNo, cast<VectorType>(Vec->getType())->getElementType(), Elt) || getValueTypePair(Record, OpNum, NextValueNo, Idx)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); I = InsertElementInst::Create(Vec, Elt, Idx); InstructionList.push_back(I); break; @@ -2669,10 +3246,10 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { Value *Vec1, *Vec2, *Mask; if (getValueTypePair(Record, OpNum, NextValueNo, Vec1) || popValue(Record, OpNum, NextValueNo, Vec1->getType(), Vec2)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); if (getValueTypePair(Record, OpNum, NextValueNo, Mask)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); I = new ShuffleVectorInst(Vec1, Vec2, Mask); InstructionList.push_back(I); break; @@ -2690,7 +3267,7 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { if (getValueTypePair(Record, OpNum, NextValueNo, LHS) || popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) || OpNum+1 != Record.size()) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); if (LHS->getType()->isFPOrFPVectorTy()) I = new FCmpInst((FCmpInst::Predicate)Record[OpNum], LHS, RHS); @@ -2712,9 +3289,9 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { unsigned OpNum = 0; Value *Op = nullptr; if (getValueTypePair(Record, OpNum, NextValueNo, Op)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); if (OpNum != Record.size()) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); I = ReturnInst::Create(Context, Op); InstructionList.push_back(I); @@ -2722,10 +3299,10 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { } case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#] if (Record.size() != 1 && Record.size() != 3) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); BasicBlock *TrueDest = getBasicBlock(Record[0]); if (!TrueDest) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); if (Record.size() == 1) { I = BranchInst::Create(TrueDest); @@ -2736,7 +3313,7 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { Value *Cond = getValue(Record, 2, NextValueNo, Type::getInt1Ty(Context)); if (!FalseDest || !Cond) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); I = BranchInst::Create(TrueDest, FalseDest, Cond); InstructionList.push_back(I); } @@ -2756,7 +3333,7 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { Value *Cond = getValue(Record, 2, NextValueNo, OpTy); BasicBlock *Default = getBasicBlock(Record[3]); if (!OpTy || !Cond || !Default) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); unsigned NumCases = Record[4]; @@ -2808,12 +3385,12 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { // Old SwitchInst format without case ranges. if (Record.size() < 3 || (Record.size() & 1) == 0) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); Type *OpTy = getTypeByID(Record[0]); Value *Cond = getValue(Record, 1, NextValueNo, OpTy); BasicBlock *Default = getBasicBlock(Record[2]); if (!OpTy || !Cond || !Default) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); unsigned NumCases = (Record.size()-3)/2; SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases); InstructionList.push_back(SI); @@ -2823,7 +3400,7 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]); if (!CaseVal || !DestBB) { delete SI; - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); } SI->addCase(CaseVal, DestBB); } @@ -2832,11 +3409,11 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { } case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...] if (Record.size() < 2) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); Type *OpTy = getTypeByID(Record[0]); Value *Address = getValue(Record, 1, NextValueNo, OpTy); if (!OpTy || !Address) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); unsigned NumDests = Record.size()-2; IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests); InstructionList.push_back(IBI); @@ -2845,7 +3422,7 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { IBI->addDestination(DestBB); } else { delete IBI; - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); } } I = IBI; @@ -2855,7 +3432,7 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { case bitc::FUNC_CODE_INST_INVOKE: { // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...] if (Record.size() < 4) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); AttributeSet PAL = getAttributes(Record[0]); unsigned CCInfo = Record[1]; BasicBlock *NormalBB = getBasicBlock(Record[2]); @@ -2864,7 +3441,7 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { unsigned OpNum = 4; Value *Callee; if (getValueTypePair(Record, OpNum, NextValueNo, Callee)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType()); FunctionType *FTy = !CalleeTy ? nullptr : @@ -2873,25 +3450,25 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { // Check that the right number of fixed parameters are here. if (!FTy || !NormalBB || !UnwindBB || Record.size() < OpNum+FTy->getNumParams()) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); SmallVector<Value*, 16> Ops; for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) { Ops.push_back(getValue(Record, OpNum, NextValueNo, FTy->getParamType(i))); if (!Ops.back()) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); } if (!FTy->isVarArg()) { if (Record.size() != OpNum) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); } else { // Read type/value pairs for varargs params. while (OpNum != Record.size()) { Value *Op; if (getValueTypePair(Record, OpNum, NextValueNo, Op)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); Ops.push_back(Op); } } @@ -2907,7 +3484,7 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { unsigned Idx = 0; Value *Val = nullptr; if (getValueTypePair(Record, Idx, NextValueNo, Val)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); I = ResumeInst::Create(Val); InstructionList.push_back(I); break; @@ -2918,10 +3495,10 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { break; case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...] if (Record.size() < 1 || ((Record.size()-1)&1)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); Type *Ty = getTypeByID(Record[0]); if (!Ty) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); PHINode *PN = PHINode::Create(Ty, (Record.size()-1)/2); InstructionList.push_back(PN); @@ -2937,7 +3514,7 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { V = getValue(Record, 1+i, NextValueNo, Ty); BasicBlock *BB = getBasicBlock(Record[2+i]); if (!V || !BB) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); PN->addIncoming(V, BB); } I = PN; @@ -2948,13 +3525,13 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?] unsigned Idx = 0; if (Record.size() < 4) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); Type *Ty = getTypeByID(Record[Idx++]); if (!Ty) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); Value *PersFn = nullptr; if (getValueTypePair(Record, Idx, NextValueNo, PersFn)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); bool IsCleanup = !!Record[Idx++]; unsigned NumClauses = Record[Idx++]; @@ -2967,7 +3544,7 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { if (getValueTypePair(Record, Idx, NextValueNo, Val)) { delete LP; - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); } assert((CT != LandingPadInst::Catch || @@ -2986,17 +3563,22 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align] if (Record.size() != 4) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); PointerType *Ty = dyn_cast_or_null<PointerType>(getTypeByID(Record[0])); Type *OpTy = getTypeByID(Record[1]); Value *Size = getFnValueByID(Record[2], OpTy); - unsigned AlignRecord = Record[3]; - bool InAlloca = AlignRecord & (1 << 5); - unsigned Align = AlignRecord & ((1 << 5) - 1); + uint64_t AlignRecord = Record[3]; + const uint64_t InAllocaMask = uint64_t(1) << 5; + bool InAlloca = AlignRecord & InAllocaMask; + unsigned Align; + if (std::error_code EC = + parseAlignmentValue(AlignRecord & ~InAllocaMask, Align)) { + return EC; + } if (!Ty || !Size) - return Error(BitcodeError::InvalidRecord); - AllocaInst *AI = new AllocaInst(Ty->getElementType(), Size, (1 << Align) >> 1); + return Error("Invalid record"); + AllocaInst *AI = new AllocaInst(Ty->getElementType(), Size, Align); AI->setUsedWithInAlloca(InAlloca); I = AI; InstructionList.push_back(I); @@ -3006,10 +3588,21 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { unsigned OpNum = 0; Value *Op; if (getValueTypePair(Record, OpNum, NextValueNo, Op) || - OpNum+2 != Record.size()) - return Error(BitcodeError::InvalidRecord); + (OpNum + 2 != Record.size() && OpNum + 3 != Record.size())) + return Error("Invalid record"); + + Type *Ty = nullptr; + if (OpNum + 3 == Record.size()) + Ty = getTypeByID(Record[OpNum++]); + + unsigned Align; + if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align)) + return EC; + I = new LoadInst(Op, "", Record[OpNum+1], Align); + + assert((!Ty || Ty == I->getType()) && + "Explicit type doesn't match pointee type of the first operand"); - I = new LoadInst(Op, "", Record[OpNum+1], (1 << Record[OpNum]) >> 1); InstructionList.push_back(I); break; } @@ -3018,19 +3611,29 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { unsigned OpNum = 0; Value *Op; if (getValueTypePair(Record, OpNum, NextValueNo, Op) || - OpNum+4 != Record.size()) - return Error(BitcodeError::InvalidRecord); + (OpNum + 4 != Record.size() && OpNum + 5 != Record.size())) + return Error("Invalid record"); + + Type *Ty = nullptr; + if (OpNum + 5 == Record.size()) + Ty = getTypeByID(Record[OpNum++]); AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]); if (Ordering == NotAtomic || Ordering == Release || Ordering == AcquireRelease) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); if (Ordering != NotAtomic && Record[OpNum] == 0) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]); - I = new LoadInst(Op, "", Record[OpNum+1], (1 << Record[OpNum]) >> 1, - Ordering, SynchScope); + unsigned Align; + if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align)) + return EC; + I = new LoadInst(Op, "", Record[OpNum+1], Align, Ordering, SynchScope); + + assert((!Ty || Ty == I->getType()) && + "Explicit type doesn't match pointee type of the first operand"); + InstructionList.push_back(I); break; } @@ -3041,9 +3644,11 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { popValue(Record, OpNum, NextValueNo, cast<PointerType>(Ptr->getType())->getElementType(), Val) || OpNum+2 != Record.size()) - return Error(BitcodeError::InvalidRecord); - - I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1); + return Error("Invalid record"); + unsigned Align; + if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align)) + return EC; + I = new StoreInst(Val, Ptr, Record[OpNum+1], Align); InstructionList.push_back(I); break; } @@ -3055,18 +3660,20 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { popValue(Record, OpNum, NextValueNo, cast<PointerType>(Ptr->getType())->getElementType(), Val) || OpNum+4 != Record.size()) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]); if (Ordering == NotAtomic || Ordering == Acquire || Ordering == AcquireRelease) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]); if (Ordering != NotAtomic && Record[OpNum] == 0) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); - I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1, - Ordering, SynchScope); + unsigned Align; + if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align)) + return EC; + I = new StoreInst(Val, Ptr, Record[OpNum+1], Align, Ordering, SynchScope); InstructionList.push_back(I); break; } @@ -3081,10 +3688,10 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { popValue(Record, OpNum, NextValueNo, cast<PointerType>(Ptr->getType())->getElementType(), New) || (Record.size() < OpNum + 3 || Record.size() > OpNum + 5)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); AtomicOrdering SuccessOrdering = GetDecodedOrdering(Record[OpNum+1]); if (SuccessOrdering == NotAtomic || SuccessOrdering == Unordered) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+2]); AtomicOrdering FailureOrdering; @@ -3119,14 +3726,14 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { popValue(Record, OpNum, NextValueNo, cast<PointerType>(Ptr->getType())->getElementType(), Val) || OpNum+4 != Record.size()) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); AtomicRMWInst::BinOp Operation = GetDecodedRMWOperation(Record[OpNum]); if (Operation < AtomicRMWInst::FIRST_BINOP || Operation > AtomicRMWInst::LAST_BINOP) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); AtomicOrdering Ordering = GetDecodedOrdering(Record[OpNum+2]); if (Ordering == NotAtomic || Ordering == Unordered) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); SynchronizationScope SynchScope = GetDecodedSynchScope(Record[OpNum+3]); I = new AtomicRMWInst(Operation, Ptr, Val, Ordering, SynchScope); cast<AtomicRMWInst>(I)->setVolatile(Record[OpNum+1]); @@ -3135,11 +3742,11 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { } case bitc::FUNC_CODE_INST_FENCE: { // FENCE:[ordering, synchscope] if (2 != Record.size()) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); AtomicOrdering Ordering = GetDecodedOrdering(Record[0]); if (Ordering == NotAtomic || Ordering == Unordered || Ordering == Monotonic) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); SynchronizationScope SynchScope = GetDecodedSynchScope(Record[1]); I = new FenceInst(Context, Ordering, SynchScope); InstructionList.push_back(I); @@ -3148,7 +3755,7 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { case bitc::FUNC_CODE_INST_CALL: { // CALL: [paramattrs, cc, fnty, fnid, arg0, arg1...] if (Record.size() < 3) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); AttributeSet PAL = getAttributes(Record[0]); unsigned CCInfo = Record[1]; @@ -3156,13 +3763,13 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { unsigned OpNum = 2; Value *Callee; if (getValueTypePair(Record, OpNum, NextValueNo, Callee)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); PointerType *OpTy = dyn_cast<PointerType>(Callee->getType()); FunctionType *FTy = nullptr; if (OpTy) FTy = dyn_cast<FunctionType>(OpTy->getElementType()); if (!FTy || Record.size() < FTy->getNumParams()+OpNum) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); SmallVector<Value*, 16> Args; // Read the fixed params. @@ -3173,18 +3780,18 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { Args.push_back(getValue(Record, OpNum, NextValueNo, FTy->getParamType(i))); if (!Args.back()) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); } // Read type/value pairs for varargs params. if (!FTy->isVarArg()) { if (OpNum != Record.size()) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); } else { while (OpNum != Record.size()) { Value *Op; if (getValueTypePair(Record, OpNum, NextValueNo, Op)) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); Args.push_back(Op); } } @@ -3204,12 +3811,12 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { } case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty] if (Record.size() < 3) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); Type *OpTy = getTypeByID(Record[0]); Value *Op = getValue(Record, 1, NextValueNo, OpTy); Type *ResTy = getTypeByID(Record[2]); if (!OpTy || !Op || !ResTy) - return Error(BitcodeError::InvalidRecord); + return Error("Invalid record"); I = new VAArgInst(Op, ResTy); InstructionList.push_back(I); break; @@ -3220,7 +3827,7 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) { // this file. if (!CurBB) { delete I; - return Error(BitcodeError::InvalidInstructionWithNoBB); + return Error("Invalid instruction with no BB"); } CurBB->getInstList().push_back(I); @@ -3247,7 +3854,7 @@ OutOfRecordLoop: delete A; } } - return Error(BitcodeError::NeverResolvedValueFoundInFunction); + return Error("Never resolved value found in function"); } } @@ -3267,7 +3874,7 @@ std::error_code BitcodeReader::FindFunctionInStream( DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator) { while (DeferredFunctionInfoIterator->second == 0) { if (Stream.AtEndOfStream()) - return Error(BitcodeError::CouldNotFindFunctionInStream); + return Error("Could not find function in stream"); // ParseModule will parse the next body in the stream and set its // position in the DeferredFunctionInfo map. if (std::error_code EC = ParseModule(true)) @@ -3369,7 +3976,7 @@ std::error_code BitcodeReader::MaterializeModule(Module *M) { // Check that all block address forward references got resolved (as we // promised above). if (!BasicBlockFwdRefs.empty()) - return Error(BitcodeError::NeverResolvedFunctionFromBlockAddress); + return Error("Never resolved function from blockaddress"); // Upgrade any intrinsic calls that slipped through (should not happen!) and // delete the old functions to clean up. We can't do this unless the entire @@ -3397,6 +4004,10 @@ std::error_code BitcodeReader::MaterializeModule(Module *M) { return std::error_code(); } +std::vector<StructType *> BitcodeReader::getIdentifiedStructTypes() const { + return IdentifiedStructTypes; +} + std::error_code BitcodeReader::InitStream() { if (LazyStreamer) return InitLazyStream(); @@ -3408,13 +4019,13 @@ std::error_code BitcodeReader::InitStreamFromBuffer() { const unsigned char *BufEnd = BufPtr+Buffer->getBufferSize(); if (Buffer->getBufferSize() & 3) - return Error(BitcodeError::InvalidBitcodeSignature); + return Error("Invalid bitcode signature"); // If we have a wrapper header, parse it and ignore the non-bc file contents. // The magic number is 0x0B17C0DE stored in little endian. if (isBitcodeWrapper(BufPtr, BufEnd)) if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true)) - return Error(BitcodeError::InvalidBitcodeWrapperHeader); + return Error("Invalid bitcode wrapper header"); StreamFile.reset(new BitstreamReader(BufPtr, BufEnd)); Stream.init(&*StreamFile); @@ -3425,23 +4036,24 @@ std::error_code BitcodeReader::InitStreamFromBuffer() { std::error_code BitcodeReader::InitLazyStream() { // Check and strip off the bitcode wrapper; BitstreamReader expects never to // see it. - StreamingMemoryObject *Bytes = new StreamingMemoryObject(LazyStreamer); - StreamFile.reset(new BitstreamReader(Bytes)); + auto OwnedBytes = llvm::make_unique<StreamingMemoryObject>(LazyStreamer); + StreamingMemoryObject &Bytes = *OwnedBytes; + StreamFile = llvm::make_unique<BitstreamReader>(std::move(OwnedBytes)); Stream.init(&*StreamFile); unsigned char buf[16]; - if (Bytes->readBytes(buf, 16, 0) != 16) - return Error(BitcodeError::InvalidBitcodeSignature); + if (Bytes.readBytes(buf, 16, 0) != 16) + return Error("Invalid bitcode signature"); if (!isBitcode(buf, buf + 16)) - return Error(BitcodeError::InvalidBitcodeSignature); + return Error("Invalid bitcode signature"); if (isBitcodeWrapper(buf, buf + 4)) { const unsigned char *bitcodeStart = buf; const unsigned char *bitcodeEnd = buf + 16; SkipBitcodeWrapperHeader(bitcodeStart, bitcodeEnd, false); - Bytes->dropLeadingBytes(bitcodeStart - buf); - Bytes->setKnownObjectSize(bitcodeEnd - bitcodeStart); + Bytes.dropLeadingBytes(bitcodeStart - buf); + Bytes.setKnownObjectSize(bitcodeEnd - bitcodeStart); } return std::error_code(); } @@ -3454,44 +4066,10 @@ class BitcodeErrorCategoryType : public std::error_category { std::string message(int IE) const override { BitcodeError E = static_cast<BitcodeError>(IE); switch (E) { - case BitcodeError::ConflictingMETADATA_KINDRecords: - return "Conflicting METADATA_KIND records"; - case BitcodeError::CouldNotFindFunctionInStream: - return "Could not find function in stream"; - case BitcodeError::ExpectedConstant: - return "Expected a constant"; - case BitcodeError::InsufficientFunctionProtos: - return "Insufficient function protos"; case BitcodeError::InvalidBitcodeSignature: return "Invalid bitcode signature"; - case BitcodeError::InvalidBitcodeWrapperHeader: - return "Invalid bitcode wrapper header"; - case BitcodeError::InvalidConstantReference: - return "Invalid ronstant reference"; - case BitcodeError::InvalidID: - return "Invalid ID"; - case BitcodeError::InvalidInstructionWithNoBB: - return "Invalid instruction with no BB"; - case BitcodeError::InvalidRecord: - return "Invalid record"; - case BitcodeError::InvalidTypeForValue: - return "Invalid type for value"; - case BitcodeError::InvalidTYPETable: - return "Invalid TYPE table"; - case BitcodeError::InvalidType: - return "Invalid type"; - case BitcodeError::MalformedBlock: - return "Malformed block"; - case BitcodeError::MalformedGlobalInitializerSet: - return "Malformed global initializer set"; - case BitcodeError::InvalidMultipleBlocks: - return "Invalid multiple blocks"; - case BitcodeError::NeverResolvedValueFoundInFunction: - return "Never resolved value found in function"; - case BitcodeError::NeverResolvedFunctionFromBlockAddress: - return "Never resolved function from blockaddress"; - case BitcodeError::InvalidValue: - return "Invalid value"; + case BitcodeError::CorruptedBitcode: + return "Corrupted bitcode"; } llvm_unreachable("Unknown error type!"); } @@ -3518,9 +4096,11 @@ const std::error_category &llvm::BitcodeErrorCategory() { /// materialize everything -- in particular, if this isn't truly lazy. static ErrorOr<Module *> getLazyBitcodeModuleImpl(std::unique_ptr<MemoryBuffer> &&Buffer, - LLVMContext &Context, bool WillMaterializeAll) { + LLVMContext &Context, bool WillMaterializeAll, + DiagnosticHandlerFunction DiagnosticHandler) { Module *M = new Module(Buffer->getBufferIdentifier(), Context); - BitcodeReader *R = new BitcodeReader(Buffer.get(), Context); + BitcodeReader *R = + new BitcodeReader(Buffer.get(), Context, DiagnosticHandler); M->setMaterializer(R); auto cleanupOnError = [&](std::error_code EC) { @@ -3543,31 +4123,30 @@ getLazyBitcodeModuleImpl(std::unique_ptr<MemoryBuffer> &&Buffer, ErrorOr<Module *> llvm::getLazyBitcodeModule(std::unique_ptr<MemoryBuffer> &&Buffer, - LLVMContext &Context) { - return getLazyBitcodeModuleImpl(std::move(Buffer), Context, false); + LLVMContext &Context, + DiagnosticHandlerFunction DiagnosticHandler) { + return getLazyBitcodeModuleImpl(std::move(Buffer), Context, false, + DiagnosticHandler); } -Module *llvm::getStreamedBitcodeModule(const std::string &name, - DataStreamer *streamer, - LLVMContext &Context, - std::string *ErrMsg) { - Module *M = new Module(name, Context); - BitcodeReader *R = new BitcodeReader(streamer, Context); +ErrorOr<std::unique_ptr<Module>> +llvm::getStreamedBitcodeModule(StringRef Name, DataStreamer *Streamer, + LLVMContext &Context, + DiagnosticHandlerFunction DiagnosticHandler) { + std::unique_ptr<Module> M = make_unique<Module>(Name, Context); + BitcodeReader *R = new BitcodeReader(Streamer, Context, DiagnosticHandler); M->setMaterializer(R); - if (std::error_code EC = R->ParseBitcodeInto(M)) { - if (ErrMsg) - *ErrMsg = EC.message(); - delete M; // Also deletes R. - return nullptr; - } - return M; + if (std::error_code EC = R->ParseBitcodeInto(M.get())) + return EC; + return std::move(M); } -ErrorOr<Module *> llvm::parseBitcodeFile(MemoryBufferRef Buffer, - LLVMContext &Context) { +ErrorOr<Module *> +llvm::parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context, + DiagnosticHandlerFunction DiagnosticHandler) { std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false); - ErrorOr<Module *> ModuleOrErr = - getLazyBitcodeModuleImpl(std::move(Buf), Context, true); + ErrorOr<Module *> ModuleOrErr = getLazyBitcodeModuleImpl( + std::move(Buf), Context, true, DiagnosticHandler); if (!ModuleOrErr) return ModuleOrErr; Module *M = ModuleOrErr.get(); @@ -3583,10 +4162,12 @@ ErrorOr<Module *> llvm::parseBitcodeFile(MemoryBufferRef Buffer, return M; } -std::string llvm::getBitcodeTargetTriple(MemoryBufferRef Buffer, - LLVMContext &Context) { +std::string +llvm::getBitcodeTargetTriple(MemoryBufferRef Buffer, LLVMContext &Context, + DiagnosticHandlerFunction DiagnosticHandler) { std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false); - auto R = llvm::make_unique<BitcodeReader>(Buf.release(), Context); + auto R = llvm::make_unique<BitcodeReader>(Buf.release(), Context, + DiagnosticHandler); ErrorOr<std::string> Triple = R->parseTriple(); if (Triple.getError()) return ""; diff --git a/lib/Bitcode/Reader/BitcodeReader.h b/lib/Bitcode/Reader/BitcodeReader.h index 047fef8..9803e78 100644 --- a/lib/Bitcode/Reader/BitcodeReader.h +++ b/lib/Bitcode/Reader/BitcodeReader.h @@ -19,7 +19,9 @@ #include "llvm/Bitcode/LLVMBitCodes.h" #include "llvm/IR/Attributes.h" #include "llvm/IR/GVMaterializer.h" +#include "llvm/IR/Metadata.h" #include "llvm/IR/OperandTraits.h" +#include "llvm/IR/TrackingMDRef.h" #include "llvm/IR/Type.h" #include "llvm/IR/ValueHandle.h" #include <deque> @@ -95,22 +97,27 @@ public: //===----------------------------------------------------------------------===// class BitcodeReaderMDValueList { - std::vector<WeakVH> MDValuePtrs; + unsigned NumFwdRefs; + bool AnyFwdRefs; + unsigned MinFwdRef; + unsigned MaxFwdRef; + std::vector<TrackingMDRef> MDValuePtrs; LLVMContext &Context; public: - BitcodeReaderMDValueList(LLVMContext& C) : Context(C) {} + BitcodeReaderMDValueList(LLVMContext &C) + : NumFwdRefs(0), AnyFwdRefs(false), Context(C) {} // vector compatibility methods unsigned size() const { return MDValuePtrs.size(); } void resize(unsigned N) { MDValuePtrs.resize(N); } - void push_back(Value *V) { MDValuePtrs.push_back(V); } + void push_back(Metadata *MD) { MDValuePtrs.emplace_back(MD); } void clear() { MDValuePtrs.clear(); } - Value *back() const { return MDValuePtrs.back(); } + Metadata *back() const { return MDValuePtrs.back(); } void pop_back() { MDValuePtrs.pop_back(); } bool empty() const { return MDValuePtrs.empty(); } - Value *operator[](unsigned i) const { + Metadata *operator[](unsigned i) const { assert(i < MDValuePtrs.size()); return MDValuePtrs[i]; } @@ -120,12 +127,14 @@ public: MDValuePtrs.resize(N); } - Value *getValueFwdRef(unsigned Idx); - void AssignValue(Value *V, unsigned Idx); + Metadata *getValueFwdRef(unsigned Idx); + void AssignValue(Metadata *MD, unsigned Idx); + void tryToResolveCycles(); }; class BitcodeReader : public GVMaterializer { LLVMContext &Context; + DiagnosticHandlerFunction DiagnosticHandler; Module *TheModule; std::unique_ptr<MemoryBuffer> Buffer; std::unique_ptr<BitstreamReader> StreamFile; @@ -143,6 +152,7 @@ class BitcodeReader : public GVMaterializer { std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits; std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits; std::vector<std::pair<Function*, unsigned> > FunctionPrefixes; + std::vector<std::pair<Function*, unsigned> > FunctionPrologues; SmallVector<Instruction*, 64> InstsWithTBAATag; @@ -203,18 +213,14 @@ class BitcodeReader : public GVMaterializer { SmallPtrSet<const Function *, 4> BlockAddressesTaken; public: - std::error_code Error(BitcodeError E) { return make_error_code(E); } - - explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext &C) - : Context(C), TheModule(nullptr), Buffer(buffer), LazyStreamer(nullptr), - NextUnreadBit(0), SeenValueSymbolTable(false), ValueList(C), - MDValueList(C), SeenFirstFunctionBody(false), UseRelativeIDs(false), - WillMaterializeAllForwardRefs(false) {} - explicit BitcodeReader(DataStreamer *streamer, LLVMContext &C) - : Context(C), TheModule(nullptr), Buffer(nullptr), LazyStreamer(streamer), - NextUnreadBit(0), SeenValueSymbolTable(false), ValueList(C), - MDValueList(C), SeenFirstFunctionBody(false), UseRelativeIDs(false), - WillMaterializeAllForwardRefs(false) {} + std::error_code Error(BitcodeError E, const Twine &Message); + std::error_code Error(BitcodeError E); + std::error_code Error(const Twine &Message); + + explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext &C, + DiagnosticHandlerFunction DiagnosticHandler); + explicit BitcodeReader(DataStreamer *streamer, LLVMContext &C, + DiagnosticHandlerFunction DiagnosticHandler); ~BitcodeReader() { FreeState(); } std::error_code materializeForwardReferencedFunctions(); @@ -226,6 +232,7 @@ public: bool isDematerializable(const GlobalValue *GV) const override; std::error_code materialize(GlobalValue *GV) override; std::error_code MaterializeModule(Module *M) override; + std::vector<StructType *> getIdentifiedStructTypes() const override; void Dematerialize(GlobalValue *GV) override; /// @brief Main interface to parsing a bitcode buffer. @@ -239,12 +246,19 @@ public: static uint64_t decodeSignRotatedValue(uint64_t V); private: + std::vector<StructType *> IdentifiedStructTypes; + StructType *createIdentifiedStructType(LLVMContext &Context, StringRef Name); + StructType *createIdentifiedStructType(LLVMContext &Context); + Type *getTypeByID(unsigned ID); Value *getFnValueByID(unsigned ID, Type *Ty) { if (Ty && Ty->isMetadataTy()) - return MDValueList.getValueFwdRef(ID); + return MetadataAsValue::get(Ty->getContext(), getFnMetadataByID(ID)); return ValueList.getValueFwdRef(ID, Ty); } + Metadata *getFnMetadataByID(unsigned ID) { + return MDValueList.getValueFwdRef(ID); + } BasicBlock *getBasicBlock(unsigned ID) const { if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID return FunctionBBs[ID]; @@ -321,6 +335,10 @@ private: return getFnValueByID(ValNo, Ty); } + /// Converts alignment exponent (i.e. power of two (or zero)) to the + /// corresponding alignment to use. If alignment is too large, returns + /// a corresponding error code. + std::error_code parseAlignmentValue(uint64_t Exponent, unsigned &Alignment); std::error_code ParseAttrKind(uint64_t Code, Attribute::AttrKind *Kind); std::error_code ParseModule(bool Resume); std::error_code ParseAttributeBlock(); diff --git a/lib/Bitcode/Reader/BitstreamReader.cpp b/lib/Bitcode/Reader/BitstreamReader.cpp index 5e3232e..ca68257 100644 --- a/lib/Bitcode/Reader/BitstreamReader.cpp +++ b/lib/Bitcode/Reader/BitstreamReader.cpp @@ -170,8 +170,12 @@ unsigned BitstreamCursor::readRecord(unsigned AbbrevID, unsigned Code; if (CodeOp.isLiteral()) Code = CodeOp.getLiteralValue(); - else + else { + if (CodeOp.getEncoding() == BitCodeAbbrevOp::Array || + CodeOp.getEncoding() == BitCodeAbbrevOp::Blob) + report_fatal_error("Abbreviation starts with an Array or a Blob"); Code = readAbbreviatedField(*this, CodeOp); + } for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i != e; ++i) { const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i); @@ -249,7 +253,7 @@ void BitstreamCursor::ReadAbbrevRecord() { BitCodeAbbrevOp::Encoding E = (BitCodeAbbrevOp::Encoding)Read(3); if (BitCodeAbbrevOp::hasEncodingData(E)) { - unsigned Data = ReadVBR64(5); + uint64_t Data = ReadVBR64(5); // As a special case, handle fixed(0) (i.e., a fixed field with zero bits) // and vbr(0) as a literal zero. This is decoded the same way, and avoids diff --git a/lib/Bitcode/Reader/CMakeLists.txt b/lib/Bitcode/Reader/CMakeLists.txt index f614c9f..62954f2 100644 --- a/lib/Bitcode/Reader/CMakeLists.txt +++ b/lib/Bitcode/Reader/CMakeLists.txt @@ -2,6 +2,9 @@ add_llvm_library(LLVMBitReader BitReader.cpp BitcodeReader.cpp BitstreamReader.cpp + + ADDITIONAL_HEADER_DIRS + ${LLVM_MAIN_INCLUDE_DIR}/llvm/Bitcode ) add_dependencies(LLVMBitReader intrinsics_gen) diff --git a/lib/Bitcode/Writer/BitcodeWriter.cpp b/lib/Bitcode/Writer/BitcodeWriter.cpp index 6cfc357..ecb6f7c 100644 --- a/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -17,6 +17,7 @@ #include "llvm/Bitcode/BitstreamWriter.h" #include "llvm/Bitcode/LLVMBitCodes.h" #include "llvm/IR/Constants.h" +#include "llvm/IR/DebugInfoMetadata.h" #include "llvm/IR/DerivedTypes.h" #include "llvm/IR/InlineAsm.h" #include "llvm/IR/Instructions.h" @@ -55,7 +56,8 @@ enum { FUNCTION_INST_CAST_ABBREV, FUNCTION_INST_RET_VOID_ABBREV, FUNCTION_INST_RET_VAL_ABBREV, - FUNCTION_INST_UNREACHABLE_ABBREV + FUNCTION_INST_UNREACHABLE_ABBREV, + FUNCTION_INST_GEP_ABBREV, }; static unsigned GetEncodedCastOpcode(unsigned Opcode) { @@ -322,7 +324,7 @@ static void WriteTypeTable(const ValueEnumerator &VE, BitstreamWriter &Stream) { Stream.EnterSubblock(bitc::TYPE_BLOCK_ID_NEW, 4 /*count from # abbrevs */); SmallVector<uint64_t, 64> TypeVals; - uint64_t NumBits = Log2_32_Ceil(VE.getTypes().size()+1); + uint64_t NumBits = VE.computeBitsRequiredForTypeIndicies(); // Abbrev for TYPE_CODE_POINTER. BitCodeAbbrev *Abbv = new BitCodeAbbrev(); @@ -477,17 +479,28 @@ static void WriteTypeTable(const ValueEnumerator &VE, BitstreamWriter &Stream) { static unsigned getEncodedLinkage(const GlobalValue &GV) { switch (GV.getLinkage()) { - case GlobalValue::ExternalLinkage: return 0; - case GlobalValue::WeakAnyLinkage: return 1; - case GlobalValue::AppendingLinkage: return 2; - case GlobalValue::InternalLinkage: return 3; - case GlobalValue::LinkOnceAnyLinkage: return 4; - case GlobalValue::ExternalWeakLinkage: return 7; - case GlobalValue::CommonLinkage: return 8; - case GlobalValue::PrivateLinkage: return 9; - case GlobalValue::WeakODRLinkage: return 10; - case GlobalValue::LinkOnceODRLinkage: return 11; - case GlobalValue::AvailableExternallyLinkage: return 12; + case GlobalValue::ExternalLinkage: + return 0; + case GlobalValue::WeakAnyLinkage: + return 16; + case GlobalValue::AppendingLinkage: + return 2; + case GlobalValue::InternalLinkage: + return 3; + case GlobalValue::LinkOnceAnyLinkage: + return 18; + case GlobalValue::ExternalWeakLinkage: + return 7; + case GlobalValue::CommonLinkage: + return 8; + case GlobalValue::PrivateLinkage: + return 9; + case GlobalValue::WeakODRLinkage: + return 17; + case GlobalValue::LinkOnceODRLinkage: + return 19; + case GlobalValue::AvailableExternallyLinkage: + return 12; } llvm_unreachable("Invalid linkage"); } @@ -538,11 +551,13 @@ static unsigned getEncodedComdatSelectionKind(const Comdat &C) { } static void writeComdats(const ValueEnumerator &VE, BitstreamWriter &Stream) { - SmallVector<uint8_t, 64> Vals; + SmallVector<uint16_t, 64> Vals; for (const Comdat *C : VE.getComdats()) { // COMDAT: [selection_kind, name] Vals.push_back(getEncodedComdatSelectionKind(*C)); - Vals.push_back(C->getName().size()); + size_t Size = C->getName().size(); + assert(isUInt<16>(Size)); + Vals.push_back(Size); for (char Chr : C->getName()) Vals.push_back((unsigned char)Chr); Stream.EmitRecord(bitc::MODULE_CODE_COMDAT, Vals, /*AbbrevToUse=*/0); @@ -616,7 +631,7 @@ static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE, Log2_32_Ceil(MaxGlobalType+1))); Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Constant. Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer. - Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // Linkage. + Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // Linkage. if (MaxAlignment == 0) // Alignment. Abbv->Add(BitCodeAbbrevOp(0)); else { @@ -640,7 +655,8 @@ static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE, // GLOBALVAR: [type, isconst, initid, // linkage, alignment, section, visibility, threadlocal, - // unnamed_addr, externally_initialized, dllstorageclass] + // unnamed_addr, externally_initialized, dllstorageclass, + // comdat] Vals.push_back(VE.getTypeID(GV.getType())); Vals.push_back(GV.isConstant()); Vals.push_back(GV.isDeclaration() ? 0 : @@ -670,7 +686,8 @@ static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE, // Emit the function proto information. for (const Function &F : *M) { // FUNCTION: [type, callingconv, isproto, linkage, paramattrs, alignment, - // section, visibility, gc, unnamed_addr, prefix] + // section, visibility, gc, unnamed_addr, prologuedata, + // dllstorageclass, comdat, prefixdata] Vals.push_back(VE.getTypeID(F.getType())); Vals.push_back(F.getCallingConv()); Vals.push_back(F.isDeclaration()); @@ -681,10 +698,12 @@ static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE, Vals.push_back(getEncodedVisibility(F)); Vals.push_back(F.hasGC() ? GCMap[F.getGC()] : 0); Vals.push_back(F.hasUnnamedAddr()); - Vals.push_back(F.hasPrefixData() ? (VE.getValueID(F.getPrefixData()) + 1) - : 0); + Vals.push_back(F.hasPrologueData() ? (VE.getValueID(F.getPrologueData()) + 1) + : 0); Vals.push_back(getEncodedDLLStorageClass(F)); Vals.push_back(F.hasComdat() ? VE.getComdatID(F.getComdat()) : 0); + Vals.push_back(F.hasPrefixData() ? (VE.getValueID(F.getPrefixData()) + 1) + : 0); unsigned AbbrevToUse = 0; Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse); @@ -734,89 +753,497 @@ static uint64_t GetOptimizationFlags(const Value *V) { return Flags; } -static void WriteMDNode(const MDNode *N, - const ValueEnumerator &VE, - BitstreamWriter &Stream, - SmallVectorImpl<uint64_t> &Record) { +static void WriteValueAsMetadata(const ValueAsMetadata *MD, + const ValueEnumerator &VE, + BitstreamWriter &Stream, + SmallVectorImpl<uint64_t> &Record) { + // Mimic an MDNode with a value as one operand. + Value *V = MD->getValue(); + Record.push_back(VE.getTypeID(V->getType())); + Record.push_back(VE.getValueID(V)); + Stream.EmitRecord(bitc::METADATA_VALUE, Record, 0); + Record.clear(); +} + +static void WriteMDTuple(const MDTuple *N, const ValueEnumerator &VE, + BitstreamWriter &Stream, + SmallVectorImpl<uint64_t> &Record, unsigned Abbrev) { for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { - if (N->getOperand(i)) { - Record.push_back(VE.getTypeID(N->getOperand(i)->getType())); - Record.push_back(VE.getValueID(N->getOperand(i))); - } else { - Record.push_back(VE.getTypeID(Type::getVoidTy(N->getContext()))); - Record.push_back(0); - } + Metadata *MD = N->getOperand(i); + assert(!(MD && isa<LocalAsMetadata>(MD)) && + "Unexpected function-local metadata"); + Record.push_back(VE.getMetadataOrNullID(MD)); } - unsigned MDCode = N->isFunctionLocal() ? bitc::METADATA_FN_NODE : - bitc::METADATA_NODE; - Stream.EmitRecord(MDCode, Record, 0); + Stream.EmitRecord(N->isDistinct() ? bitc::METADATA_DISTINCT_NODE + : bitc::METADATA_NODE, + Record, Abbrev); + Record.clear(); +} + +static void WriteMDLocation(const MDLocation *N, const ValueEnumerator &VE, + BitstreamWriter &Stream, + SmallVectorImpl<uint64_t> &Record, + unsigned Abbrev) { + Record.push_back(N->isDistinct()); + Record.push_back(N->getLine()); + Record.push_back(N->getColumn()); + Record.push_back(VE.getMetadataID(N->getScope())); + Record.push_back(VE.getMetadataOrNullID(N->getInlinedAt())); + + Stream.EmitRecord(bitc::METADATA_LOCATION, Record, Abbrev); + Record.clear(); +} + +static void WriteGenericDebugNode(const GenericDebugNode *N, + const ValueEnumerator &VE, + BitstreamWriter &Stream, + SmallVectorImpl<uint64_t> &Record, + unsigned Abbrev) { + Record.push_back(N->isDistinct()); + Record.push_back(N->getTag()); + Record.push_back(0); // Per-tag version field; unused for now. + + for (auto &I : N->operands()) + Record.push_back(VE.getMetadataOrNullID(I)); + + Stream.EmitRecord(bitc::METADATA_GENERIC_DEBUG, Record, Abbrev); + Record.clear(); +} + +static uint64_t rotateSign(int64_t I) { + uint64_t U = I; + return I < 0 ? ~(U << 1) : U << 1; +} + +static void WriteMDSubrange(const MDSubrange *N, const ValueEnumerator &, + BitstreamWriter &Stream, + SmallVectorImpl<uint64_t> &Record, + unsigned Abbrev) { + Record.push_back(N->isDistinct()); + Record.push_back(N->getCount()); + Record.push_back(rotateSign(N->getLo())); + + Stream.EmitRecord(bitc::METADATA_SUBRANGE, Record, Abbrev); + Record.clear(); +} + +static void WriteMDEnumerator(const MDEnumerator *N, const ValueEnumerator &VE, + BitstreamWriter &Stream, + SmallVectorImpl<uint64_t> &Record, + unsigned Abbrev) { + Record.push_back(N->isDistinct()); + Record.push_back(rotateSign(N->getValue())); + Record.push_back(VE.getMetadataOrNullID(N->getRawName())); + + Stream.EmitRecord(bitc::METADATA_ENUMERATOR, Record, Abbrev); + Record.clear(); +} + +static void WriteMDBasicType(const MDBasicType *N, const ValueEnumerator &VE, + BitstreamWriter &Stream, + SmallVectorImpl<uint64_t> &Record, + unsigned Abbrev) { + Record.push_back(N->isDistinct()); + Record.push_back(N->getTag()); + Record.push_back(VE.getMetadataOrNullID(N->getRawName())); + Record.push_back(N->getSizeInBits()); + Record.push_back(N->getAlignInBits()); + Record.push_back(N->getEncoding()); + + Stream.EmitRecord(bitc::METADATA_BASIC_TYPE, Record, Abbrev); + Record.clear(); +} + +static void WriteMDDerivedType(const MDDerivedType *N, + const ValueEnumerator &VE, + BitstreamWriter &Stream, + SmallVectorImpl<uint64_t> &Record, + unsigned Abbrev) { + Record.push_back(N->isDistinct()); + Record.push_back(N->getTag()); + Record.push_back(VE.getMetadataOrNullID(N->getRawName())); + Record.push_back(VE.getMetadataOrNullID(N->getFile())); + Record.push_back(N->getLine()); + Record.push_back(VE.getMetadataOrNullID(N->getScope())); + Record.push_back(VE.getMetadataOrNullID(N->getBaseType())); + Record.push_back(N->getSizeInBits()); + Record.push_back(N->getAlignInBits()); + Record.push_back(N->getOffsetInBits()); + Record.push_back(N->getFlags()); + Record.push_back(VE.getMetadataOrNullID(N->getExtraData())); + + Stream.EmitRecord(bitc::METADATA_DERIVED_TYPE, Record, Abbrev); + Record.clear(); +} + +static void WriteMDCompositeType(const MDCompositeType *N, + const ValueEnumerator &VE, + BitstreamWriter &Stream, + SmallVectorImpl<uint64_t> &Record, + unsigned Abbrev) { + Record.push_back(N->isDistinct()); + Record.push_back(N->getTag()); + Record.push_back(VE.getMetadataOrNullID(N->getRawName())); + Record.push_back(VE.getMetadataOrNullID(N->getFile())); + Record.push_back(N->getLine()); + Record.push_back(VE.getMetadataOrNullID(N->getScope())); + Record.push_back(VE.getMetadataOrNullID(N->getBaseType())); + Record.push_back(N->getSizeInBits()); + Record.push_back(N->getAlignInBits()); + Record.push_back(N->getOffsetInBits()); + Record.push_back(N->getFlags()); + Record.push_back(VE.getMetadataOrNullID(N->getElements())); + Record.push_back(N->getRuntimeLang()); + Record.push_back(VE.getMetadataOrNullID(N->getVTableHolder())); + Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams())); + Record.push_back(VE.getMetadataOrNullID(N->getRawIdentifier())); + + Stream.EmitRecord(bitc::METADATA_COMPOSITE_TYPE, Record, Abbrev); + Record.clear(); +} + +static void WriteMDSubroutineType(const MDSubroutineType *N, + const ValueEnumerator &VE, + BitstreamWriter &Stream, + SmallVectorImpl<uint64_t> &Record, + unsigned Abbrev) { + Record.push_back(N->isDistinct()); + Record.push_back(N->getFlags()); + Record.push_back(VE.getMetadataOrNullID(N->getTypeArray())); + + Stream.EmitRecord(bitc::METADATA_SUBROUTINE_TYPE, Record, Abbrev); + Record.clear(); +} + +static void WriteMDFile(const MDFile *N, const ValueEnumerator &VE, + BitstreamWriter &Stream, + SmallVectorImpl<uint64_t> &Record, unsigned Abbrev) { + Record.push_back(N->isDistinct()); + Record.push_back(VE.getMetadataOrNullID(N->getRawFilename())); + Record.push_back(VE.getMetadataOrNullID(N->getRawDirectory())); + + Stream.EmitRecord(bitc::METADATA_FILE, Record, Abbrev); + Record.clear(); +} + +static void WriteMDCompileUnit(const MDCompileUnit *N, + const ValueEnumerator &VE, + BitstreamWriter &Stream, + SmallVectorImpl<uint64_t> &Record, + unsigned Abbrev) { + Record.push_back(N->isDistinct()); + Record.push_back(N->getSourceLanguage()); + Record.push_back(VE.getMetadataOrNullID(N->getFile())); + Record.push_back(VE.getMetadataOrNullID(N->getRawProducer())); + Record.push_back(N->isOptimized()); + Record.push_back(VE.getMetadataOrNullID(N->getRawFlags())); + Record.push_back(N->getRuntimeVersion()); + Record.push_back(VE.getMetadataOrNullID(N->getRawSplitDebugFilename())); + Record.push_back(N->getEmissionKind()); + Record.push_back(VE.getMetadataOrNullID(N->getEnumTypes())); + Record.push_back(VE.getMetadataOrNullID(N->getRetainedTypes())); + Record.push_back(VE.getMetadataOrNullID(N->getSubprograms())); + Record.push_back(VE.getMetadataOrNullID(N->getGlobalVariables())); + Record.push_back(VE.getMetadataOrNullID(N->getImportedEntities())); + + Stream.EmitRecord(bitc::METADATA_COMPILE_UNIT, Record, Abbrev); + Record.clear(); +} + +static void WriteMDSubprogram(const MDSubprogram *N, + const ValueEnumerator &VE, + BitstreamWriter &Stream, + SmallVectorImpl<uint64_t> &Record, + unsigned Abbrev) { + Record.push_back(N->isDistinct()); + Record.push_back(VE.getMetadataOrNullID(N->getScope())); + Record.push_back(VE.getMetadataOrNullID(N->getRawName())); + Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName())); + Record.push_back(VE.getMetadataOrNullID(N->getFile())); + Record.push_back(N->getLine()); + Record.push_back(VE.getMetadataOrNullID(N->getType())); + Record.push_back(N->isLocalToUnit()); + Record.push_back(N->isDefinition()); + Record.push_back(N->getScopeLine()); + Record.push_back(VE.getMetadataOrNullID(N->getContainingType())); + Record.push_back(N->getVirtuality()); + Record.push_back(N->getVirtualIndex()); + Record.push_back(N->getFlags()); + Record.push_back(N->isOptimized()); + Record.push_back(VE.getMetadataOrNullID(N->getFunction())); + Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams())); + Record.push_back(VE.getMetadataOrNullID(N->getDeclaration())); + Record.push_back(VE.getMetadataOrNullID(N->getVariables())); + + Stream.EmitRecord(bitc::METADATA_SUBPROGRAM, Record, Abbrev); + Record.clear(); +} + +static void WriteMDLexicalBlock(const MDLexicalBlock *N, + const ValueEnumerator &VE, + BitstreamWriter &Stream, + SmallVectorImpl<uint64_t> &Record, + unsigned Abbrev) { + Record.push_back(N->isDistinct()); + Record.push_back(VE.getMetadataOrNullID(N->getScope())); + Record.push_back(VE.getMetadataOrNullID(N->getFile())); + Record.push_back(N->getLine()); + Record.push_back(N->getColumn()); + + Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK, Record, Abbrev); + Record.clear(); +} + +static void WriteMDLexicalBlockFile(const MDLexicalBlockFile *N, + const ValueEnumerator &VE, + BitstreamWriter &Stream, + SmallVectorImpl<uint64_t> &Record, + unsigned Abbrev) { + Record.push_back(N->isDistinct()); + Record.push_back(VE.getMetadataOrNullID(N->getScope())); + Record.push_back(VE.getMetadataOrNullID(N->getFile())); + Record.push_back(N->getDiscriminator()); + + Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK_FILE, Record, Abbrev); + Record.clear(); +} + +static void WriteMDNamespace(const MDNamespace *N, const ValueEnumerator &VE, + BitstreamWriter &Stream, + SmallVectorImpl<uint64_t> &Record, + unsigned Abbrev) { + Record.push_back(N->isDistinct()); + Record.push_back(VE.getMetadataOrNullID(N->getScope())); + Record.push_back(VE.getMetadataOrNullID(N->getFile())); + Record.push_back(VE.getMetadataOrNullID(N->getRawName())); + Record.push_back(N->getLine()); + + Stream.EmitRecord(bitc::METADATA_NAMESPACE, Record, Abbrev); + Record.clear(); +} + +static void WriteMDTemplateTypeParameter(const MDTemplateTypeParameter *N, + const ValueEnumerator &VE, + BitstreamWriter &Stream, + SmallVectorImpl<uint64_t> &Record, + unsigned Abbrev) { + Record.push_back(N->isDistinct()); + Record.push_back(VE.getMetadataOrNullID(N->getRawName())); + Record.push_back(VE.getMetadataOrNullID(N->getType())); + + Stream.EmitRecord(bitc::METADATA_TEMPLATE_TYPE, Record, Abbrev); + Record.clear(); +} + +static void WriteMDTemplateValueParameter(const MDTemplateValueParameter *N, + const ValueEnumerator &VE, + BitstreamWriter &Stream, + SmallVectorImpl<uint64_t> &Record, + unsigned Abbrev) { + Record.push_back(N->isDistinct()); + Record.push_back(N->getTag()); + Record.push_back(VE.getMetadataOrNullID(N->getRawName())); + Record.push_back(VE.getMetadataOrNullID(N->getType())); + Record.push_back(VE.getMetadataOrNullID(N->getValue())); + + Stream.EmitRecord(bitc::METADATA_TEMPLATE_VALUE, Record, Abbrev); + Record.clear(); +} + +static void WriteMDGlobalVariable(const MDGlobalVariable *N, + const ValueEnumerator &VE, + BitstreamWriter &Stream, + SmallVectorImpl<uint64_t> &Record, + unsigned Abbrev) { + Record.push_back(N->isDistinct()); + Record.push_back(VE.getMetadataOrNullID(N->getScope())); + Record.push_back(VE.getMetadataOrNullID(N->getRawName())); + Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName())); + Record.push_back(VE.getMetadataOrNullID(N->getFile())); + Record.push_back(N->getLine()); + Record.push_back(VE.getMetadataOrNullID(N->getType())); + Record.push_back(N->isLocalToUnit()); + Record.push_back(N->isDefinition()); + Record.push_back(VE.getMetadataOrNullID(N->getVariable())); + Record.push_back(VE.getMetadataOrNullID(N->getStaticDataMemberDeclaration())); + + Stream.EmitRecord(bitc::METADATA_GLOBAL_VAR, Record, Abbrev); + Record.clear(); +} + +static void WriteMDLocalVariable(const MDLocalVariable *N, + const ValueEnumerator &VE, + BitstreamWriter &Stream, + SmallVectorImpl<uint64_t> &Record, + unsigned Abbrev) { + Record.push_back(N->isDistinct()); + Record.push_back(N->getTag()); + Record.push_back(VE.getMetadataOrNullID(N->getScope())); + Record.push_back(VE.getMetadataOrNullID(N->getRawName())); + Record.push_back(VE.getMetadataOrNullID(N->getFile())); + Record.push_back(N->getLine()); + Record.push_back(VE.getMetadataOrNullID(N->getType())); + Record.push_back(N->getArg()); + Record.push_back(N->getFlags()); + Record.push_back(VE.getMetadataOrNullID(N->getInlinedAt())); + + Stream.EmitRecord(bitc::METADATA_LOCAL_VAR, Record, Abbrev); + Record.clear(); +} + +static void WriteMDExpression(const MDExpression *N, const ValueEnumerator &, + BitstreamWriter &Stream, + SmallVectorImpl<uint64_t> &Record, + unsigned Abbrev) { + Record.reserve(N->getElements().size() + 1); + + Record.push_back(N->isDistinct()); + Record.append(N->elements_begin(), N->elements_end()); + + Stream.EmitRecord(bitc::METADATA_EXPRESSION, Record, Abbrev); + Record.clear(); +} + +static void WriteMDObjCProperty(const MDObjCProperty *N, + const ValueEnumerator &VE, + BitstreamWriter &Stream, + SmallVectorImpl<uint64_t> &Record, + unsigned Abbrev) { + Record.push_back(N->isDistinct()); + Record.push_back(VE.getMetadataOrNullID(N->getRawName())); + Record.push_back(VE.getMetadataOrNullID(N->getFile())); + Record.push_back(N->getLine()); + Record.push_back(VE.getMetadataOrNullID(N->getRawSetterName())); + Record.push_back(VE.getMetadataOrNullID(N->getRawGetterName())); + Record.push_back(N->getAttributes()); + Record.push_back(VE.getMetadataOrNullID(N->getType())); + + Stream.EmitRecord(bitc::METADATA_OBJC_PROPERTY, Record, Abbrev); + Record.clear(); +} + +static void WriteMDImportedEntity(const MDImportedEntity *N, + const ValueEnumerator &VE, + BitstreamWriter &Stream, + SmallVectorImpl<uint64_t> &Record, + unsigned Abbrev) { + Record.push_back(N->isDistinct()); + Record.push_back(N->getTag()); + Record.push_back(VE.getMetadataOrNullID(N->getScope())); + Record.push_back(VE.getMetadataOrNullID(N->getEntity())); + Record.push_back(N->getLine()); + Record.push_back(VE.getMetadataOrNullID(N->getRawName())); + + Stream.EmitRecord(bitc::METADATA_IMPORTED_ENTITY, Record, Abbrev); Record.clear(); } static void WriteModuleMetadata(const Module *M, const ValueEnumerator &VE, BitstreamWriter &Stream) { - const auto &Vals = VE.getMDValues(); - bool StartedMetadataBlock = false; + const auto &MDs = VE.getMDs(); + if (MDs.empty() && M->named_metadata_empty()) + return; + + Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); + unsigned MDSAbbrev = 0; - SmallVector<uint64_t, 64> Record; - for (unsigned i = 0, e = Vals.size(); i != e; ++i) { + if (VE.hasMDString()) { + // Abbrev for METADATA_STRING. + BitCodeAbbrev *Abbv = new BitCodeAbbrev(); + Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRING)); + Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); + Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); + MDSAbbrev = Stream.EmitAbbrev(Abbv); + } - if (const MDNode *N = dyn_cast<MDNode>(Vals[i])) { - if (!N->isFunctionLocal() || !N->getFunction()) { - if (!StartedMetadataBlock) { - Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); - StartedMetadataBlock = true; - } - WriteMDNode(N, VE, Stream, Record); - } - } else if (const MDString *MDS = dyn_cast<MDString>(Vals[i])) { - if (!StartedMetadataBlock) { - Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); - - // Abbrev for METADATA_STRING. - BitCodeAbbrev *Abbv = new BitCodeAbbrev(); - Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRING)); - Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); - Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); - MDSAbbrev = Stream.EmitAbbrev(Abbv); - StartedMetadataBlock = true; - } + // Initialize MDNode abbreviations. +#define HANDLE_MDNODE_LEAF(CLASS) unsigned CLASS##Abbrev = 0; +#include "llvm/IR/Metadata.def" - // Code: [strchar x N] - Record.append(MDS->begin(), MDS->end()); + if (VE.hasMDLocation()) { + // Abbrev for METADATA_LOCATION. + // + // Assume the column is usually under 128, and always output the inlined-at + // location (it's never more expensive than building an array size 1). + BitCodeAbbrev *Abbv = new BitCodeAbbrev(); + Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_LOCATION)); + Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); + Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); + Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); + Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); + Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); + MDLocationAbbrev = Stream.EmitAbbrev(Abbv); + } - // Emit the finished record. - Stream.EmitRecord(bitc::METADATA_STRING, Record, MDSAbbrev); - Record.clear(); - } + if (VE.hasGenericDebugNode()) { + // Abbrev for METADATA_GENERIC_DEBUG. + // + // Assume the column is usually under 128, and always output the inlined-at + // location (it's never more expensive than building an array size 1). + BitCodeAbbrev *Abbv = new BitCodeAbbrev(); + Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_GENERIC_DEBUG)); + Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); + Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); + Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); + Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); + Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); + Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); + GenericDebugNodeAbbrev = Stream.EmitAbbrev(Abbv); } - // Write named metadata. - for (Module::const_named_metadata_iterator I = M->named_metadata_begin(), - E = M->named_metadata_end(); I != E; ++I) { - const NamedMDNode *NMD = I; - if (!StartedMetadataBlock) { - Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); - StartedMetadataBlock = true; + unsigned NameAbbrev = 0; + if (!M->named_metadata_empty()) { + // Abbrev for METADATA_NAME. + BitCodeAbbrev *Abbv = new BitCodeAbbrev(); + Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_NAME)); + Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); + Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); + NameAbbrev = Stream.EmitAbbrev(Abbv); + } + + SmallVector<uint64_t, 64> Record; + for (const Metadata *MD : MDs) { + if (const MDNode *N = dyn_cast<MDNode>(MD)) { + switch (N->getMetadataID()) { + default: + llvm_unreachable("Invalid MDNode subclass"); +#define HANDLE_MDNODE_LEAF(CLASS) \ + case Metadata::CLASS##Kind: \ + Write##CLASS(cast<CLASS>(N), VE, Stream, Record, CLASS##Abbrev); \ + continue; +#include "llvm/IR/Metadata.def" + } } + if (const auto *MDC = dyn_cast<ConstantAsMetadata>(MD)) { + WriteValueAsMetadata(MDC, VE, Stream, Record); + continue; + } + const MDString *MDS = cast<MDString>(MD); + // Code: [strchar x N] + Record.append(MDS->bytes_begin(), MDS->bytes_end()); + // Emit the finished record. + Stream.EmitRecord(bitc::METADATA_STRING, Record, MDSAbbrev); + Record.clear(); + } + + // Write named metadata. + for (const NamedMDNode &NMD : M->named_metadata()) { // Write name. - StringRef Str = NMD->getName(); - for (unsigned i = 0, e = Str.size(); i != e; ++i) - Record.push_back(Str[i]); - Stream.EmitRecord(bitc::METADATA_NAME, Record, 0/*TODO*/); + StringRef Str = NMD.getName(); + Record.append(Str.bytes_begin(), Str.bytes_end()); + Stream.EmitRecord(bitc::METADATA_NAME, Record, NameAbbrev); Record.clear(); // Write named metadata operands. - for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) - Record.push_back(VE.getValueID(NMD->getOperand(i))); + for (const MDNode *N : NMD.operands()) + Record.push_back(VE.getMetadataID(N)); Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0); Record.clear(); } - if (StartedMetadataBlock) - Stream.ExitBlock(); + Stream.ExitBlock(); } static void WriteFunctionLocalMetadata(const Function &F, @@ -824,16 +1251,16 @@ static void WriteFunctionLocalMetadata(const Function &F, BitstreamWriter &Stream) { bool StartedMetadataBlock = false; SmallVector<uint64_t, 64> Record; - const SmallVectorImpl<const MDNode *> &Vals = VE.getFunctionLocalMDValues(); - for (unsigned i = 0, e = Vals.size(); i != e; ++i) - if (const MDNode *N = Vals[i]) - if (N->isFunctionLocal() && N->getFunction() == &F) { - if (!StartedMetadataBlock) { - Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); - StartedMetadataBlock = true; - } - WriteMDNode(N, VE, Stream, Record); - } + const SmallVectorImpl<const LocalAsMetadata *> &MDs = + VE.getFunctionLocalMDs(); + for (unsigned i = 0, e = MDs.size(); i != e; ++i) { + assert(MDs[i] && "Expected valid function-local metadata"); + if (!StartedMetadataBlock) { + Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); + StartedMetadataBlock = true; + } + WriteValueAsMetadata(MDs[i], VE, Stream, Record); + } if (StartedMetadataBlock) Stream.ExitBlock(); @@ -863,7 +1290,7 @@ static void WriteMetadataAttachment(const Function &F, for (unsigned i = 0, e = MDs.size(); i != e; ++i) { Record.push_back(MDs[i].first); - Record.push_back(VE.getValueID(MDs[i].second)); + Record.push_back(VE.getMetadataID(MDs[i].second)); } Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0); Record.clear(); @@ -966,14 +1393,12 @@ static void WriteConstants(unsigned FirstVal, unsigned LastVal, // Add the asm string. const std::string &AsmStr = IA->getAsmString(); Record.push_back(AsmStr.size()); - for (unsigned i = 0, e = AsmStr.size(); i != e; ++i) - Record.push_back(AsmStr[i]); + Record.append(AsmStr.begin(), AsmStr.end()); // Add the constraint string. const std::string &ConstraintStr = IA->getConstraintString(); Record.push_back(ConstraintStr.size()); - for (unsigned i = 0, e = ConstraintStr.size(); i != e; ++i) - Record.push_back(ConstraintStr[i]); + Record.append(ConstraintStr.begin(), ConstraintStr.end()); Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record); Record.clear(); continue; @@ -1251,19 +1676,21 @@ static void WriteInstruction(const Instruction &I, unsigned InstID, } break; - case Instruction::GetElementPtr: + case Instruction::GetElementPtr: { Code = bitc::FUNC_CODE_INST_GEP; - if (cast<GEPOperator>(&I)->isInBounds()) - Code = bitc::FUNC_CODE_INST_INBOUNDS_GEP; + AbbrevToUse = FUNCTION_INST_GEP_ABBREV; + auto &GEPInst = cast<GetElementPtrInst>(I); + Vals.push_back(GEPInst.isInBounds()); + Vals.push_back(VE.getTypeID(GEPInst.getSourceElementType())); for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) PushValueAndType(I.getOperand(i), InstID, Vals, VE); break; + } case Instruction::ExtractValue: { Code = bitc::FUNC_CODE_INST_EXTRACTVAL; PushValueAndType(I.getOperand(0), InstID, Vals, VE); const ExtractValueInst *EVI = cast<ExtractValueInst>(&I); - for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i) - Vals.push_back(*i); + Vals.append(EVI->idx_begin(), EVI->idx_end()); break; } case Instruction::InsertValue: { @@ -1271,8 +1698,7 @@ static void WriteInstruction(const Instruction &I, unsigned InstID, PushValueAndType(I.getOperand(0), InstID, Vals, VE); PushValueAndType(I.getOperand(1), InstID, Vals, VE); const InsertValueInst *IVI = cast<InsertValueInst>(&I); - for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i) - Vals.push_back(*i); + Vals.append(IVI->idx_begin(), IVI->idx_end()); break; } case Instruction::Select: @@ -1449,6 +1875,7 @@ static void WriteInstruction(const Instruction &I, unsigned InstID, if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) // ptr AbbrevToUse = FUNCTION_INST_LOAD_ABBREV; } + Vals.push_back(VE.getTypeID(I.getType())); Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1); Vals.push_back(cast<LoadInst>(I).isVolatile()); if (cast<LoadInst>(I).isAtomic()) { @@ -1608,9 +2035,7 @@ static void WriteUseList(ValueEnumerator &VE, UseListOrder &&Order, else Code = bitc::USELIST_CODE_DEFAULT; - SmallVector<uint64_t, 64> Record; - for (unsigned I : Order.Shuffle) - Record.push_back(I); + SmallVector<uint64_t, 64> Record(Order.Shuffle.begin(), Order.Shuffle.end()); Record.push_back(VE.getValueID(Order.V)); Stream.EmitRecord(Code, Record); } @@ -1683,11 +2108,12 @@ static void WriteFunction(const Function &F, ValueEnumerator &VE, } else { MDNode *Scope, *IA; DL.getScopeAndInlinedAt(Scope, IA, I->getContext()); + assert(Scope && "Expected valid scope"); Vals.push_back(DL.getLine()); Vals.push_back(DL.getCol()); - Vals.push_back(Scope ? VE.getValueID(Scope)+1 : 0); - Vals.push_back(IA ? VE.getValueID(IA)+1 : 0); + Vals.push_back(VE.getMetadataOrNullID(Scope)); + Vals.push_back(VE.getMetadataOrNullID(IA)); Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC, Vals); Vals.clear(); @@ -1761,7 +2187,7 @@ static void WriteBlockInfo(const ValueEnumerator &VE, BitstreamWriter &Stream) { BitCodeAbbrev *Abbv = new BitCodeAbbrev(); Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE)); Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, - Log2_32_Ceil(VE.getTypes().size()+1))); + VE.computeBitsRequiredForTypeIndicies())); if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) != CONSTANTS_SETTYPE_ABBREV) llvm_unreachable("Unexpected abbrev ordering!"); @@ -1781,7 +2207,7 @@ static void WriteBlockInfo(const ValueEnumerator &VE, BitstreamWriter &Stream) { Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST)); Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid - Log2_32_Ceil(VE.getTypes().size()+1))); + VE.computeBitsRequiredForTypeIndicies())); Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, @@ -1802,6 +2228,8 @@ static void WriteBlockInfo(const ValueEnumerator &VE, BitstreamWriter &Stream) { BitCodeAbbrev *Abbv = new BitCodeAbbrev(); Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD)); Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr + Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty + VE.computeBitsRequiredForTypeIndicies())); Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, @@ -1834,7 +2262,7 @@ static void WriteBlockInfo(const ValueEnumerator &VE, BitstreamWriter &Stream) { Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST)); Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty - Log2_32_Ceil(VE.getTypes().size()+1))); + VE.computeBitsRequiredForTypeIndicies())); Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) != FUNCTION_INST_CAST_ABBREV) @@ -1863,6 +2291,18 @@ static void WriteBlockInfo(const ValueEnumerator &VE, BitstreamWriter &Stream) { Abbv) != FUNCTION_INST_UNREACHABLE_ABBREV) llvm_unreachable("Unexpected abbrev ordering!"); } + { + BitCodeAbbrev *Abbv = new BitCodeAbbrev(); + Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_GEP)); + Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); + Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty + Log2_32_Ceil(VE.getTypes().size() + 1))); + Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); + Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); + if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) != + FUNCTION_INST_GEP_ABBREV) + llvm_unreachable("Unexpected abbrev ordering!"); + } Stream.ExitBlock(); } diff --git a/lib/Bitcode/Writer/BitcodeWriterPass.cpp b/lib/Bitcode/Writer/BitcodeWriterPass.cpp index 4167f6d..25456a4 100644 --- a/lib/Bitcode/Writer/BitcodeWriterPass.cpp +++ b/lib/Bitcode/Writer/BitcodeWriterPass.cpp @@ -18,8 +18,8 @@ #include "llvm/Pass.h" using namespace llvm; -PreservedAnalyses BitcodeWriterPass::run(Module *M) { - WriteBitcodeToFile(M, OS); +PreservedAnalyses BitcodeWriterPass::run(Module &M) { + WriteBitcodeToFile(&M, OS); return PreservedAnalyses::all(); } diff --git a/lib/Bitcode/Writer/ValueEnumerator.cpp b/lib/Bitcode/Writer/ValueEnumerator.cpp index f065c83..549e94f 100644 --- a/lib/Bitcode/Writer/ValueEnumerator.cpp +++ b/lib/Bitcode/Writer/ValueEnumerator.cpp @@ -15,6 +15,7 @@ #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallPtrSet.h" #include "llvm/IR/Constants.h" +#include "llvm/IR/DebugInfoMetadata.h" #include "llvm/IR/DerivedTypes.h" #include "llvm/IR/Instructions.h" #include "llvm/IR/Module.h" @@ -85,10 +86,14 @@ static OrderMap orderModule(const Module &M) { for (const GlobalAlias &A : M.aliases()) if (!isa<GlobalValue>(A.getAliasee())) orderValue(A.getAliasee(), OM); - for (const Function &F : M) + for (const Function &F : M) { if (F.hasPrefixData()) if (!isa<GlobalValue>(F.getPrefixData())) orderValue(F.getPrefixData(), OM); + if (F.hasPrologueData()) + if (!isa<GlobalValue>(F.getPrologueData())) + orderValue(F.getPrologueData(), OM); + } OM.LastGlobalConstantID = OM.size(); // Initializers of GlobalValues are processed in @@ -264,9 +269,12 @@ static UseListOrderStack predictUseListOrder(const Module &M) { predictValueUseListOrder(G.getInitializer(), nullptr, OM, Stack); for (const GlobalAlias &A : M.aliases()) predictValueUseListOrder(A.getAliasee(), nullptr, OM, Stack); - for (const Function &F : M) + for (const Function &F : M) { if (F.hasPrefixData()) predictValueUseListOrder(F.getPrefixData(), nullptr, OM, Stack); + if (F.hasPrologueData()) + predictValueUseListOrder(F.getPrologueData(), nullptr, OM, Stack); + } return Stack; } @@ -275,7 +283,8 @@ static bool isIntOrIntVectorValue(const std::pair<const Value*, unsigned> &V) { return V.first->getType()->isIntOrIntVectorTy(); } -ValueEnumerator::ValueEnumerator(const Module &M) { +ValueEnumerator::ValueEnumerator(const Module &M) + : HasMDString(false), HasMDLocation(false), HasGenericDebugNode(false) { if (shouldPreserveBitcodeUseListOrder()) UseListOrders = predictUseListOrder(M); @@ -314,6 +323,17 @@ ValueEnumerator::ValueEnumerator(const Module &M) { if (I->hasPrefixData()) EnumerateValue(I->getPrefixData()); + // Enumerate the prologue data constants. + for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) + if (I->hasPrologueData()) + EnumerateValue(I->getPrologueData()); + + // Enumerate the metadata type. + // + // TODO: Move this to ValueEnumerator::EnumerateOperandType() once bitcode + // only encodes the metadata type when it's used as a value. + EnumerateType(Type::getMetadataTy(M.getContext())); + // Insert constants and metadata that are named at module level into the slot // pool so that the module symbol table can refer to them... EnumerateValueSymbolTable(M.getValueSymbolTable()); @@ -329,11 +349,17 @@ ValueEnumerator::ValueEnumerator(const Module &M) { for (const BasicBlock &BB : F) for (const Instruction &I : BB) { for (const Use &Op : I.operands()) { - if (MDNode *MD = dyn_cast<MDNode>(&Op)) - if (MD->isFunctionLocal() && MD->getFunction()) - // These will get enumerated during function-incorporation. - continue; - EnumerateOperandType(Op); + auto *MD = dyn_cast<MetadataAsValue>(&Op); + if (!MD) { + EnumerateOperandType(Op); + continue; + } + + // Local metadata is enumerated during function-incorporation. + if (isa<LocalAsMetadata>(MD->getMetadata())) + continue; + + EnumerateMetadata(MD->getMetadata()); } EnumerateType(I.getType()); if (const CallInst *CI = dyn_cast<CallInst>(&I)) @@ -377,11 +403,8 @@ void ValueEnumerator::setInstructionID(const Instruction *I) { } unsigned ValueEnumerator::getValueID(const Value *V) const { - if (isa<MDNode>(V) || isa<MDString>(V)) { - ValueMapType::const_iterator I = MDValueMap.find(V); - assert(I != MDValueMap.end() && "Value not in slotcalculator!"); - return I->second-1; - } + if (auto *MD = dyn_cast<MetadataAsValue>(V)) + return getMetadataID(MD->getMetadata()); ValueMapType::const_iterator I = ValueMap.find(V); assert(I != ValueMap.end() && "Value not in slotcalculator!"); @@ -424,6 +447,18 @@ void ValueEnumerator::print(raw_ostream &OS, const ValueMapType &Map, } } +void ValueEnumerator::print(raw_ostream &OS, const MetadataMapType &Map, + const char *Name) const { + + OS << "Map Name: " << Name << "\n"; + OS << "Size: " << Map.size() << "\n"; + for (auto I = Map.begin(), E = Map.end(); I != E; ++I) { + const Metadata *MD = I->first; + OS << "Metadata: slot = " << I->second << "\n"; + MD->print(OS); + } +} + /// OptimizeConstants - Reorder constant pool for denser encoding. void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) { if (CstStart == CstEnd || CstStart+1 == CstEnd) return; @@ -481,25 +516,18 @@ void ValueEnumerator::EnumerateNamedMDNode(const NamedMDNode *MD) { /// and types referenced by the given MDNode. void ValueEnumerator::EnumerateMDNodeOperands(const MDNode *N) { for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { - if (Value *V = N->getOperand(i)) { - if (isa<MDNode>(V) || isa<MDString>(V)) - EnumerateMetadata(V); - else if (!isa<Instruction>(V) && !isa<Argument>(V)) - EnumerateValue(V); - } else - EnumerateType(Type::getVoidTy(N->getContext())); + Metadata *MD = N->getOperand(i); + if (!MD) + continue; + assert(!isa<LocalAsMetadata>(MD) && "MDNodes cannot be function-local"); + EnumerateMetadata(MD); } } -void ValueEnumerator::EnumerateMetadata(const Value *MD) { - assert((isa<MDNode>(MD) || isa<MDString>(MD)) && "Invalid metadata kind"); - - // Skip function-local nodes themselves, but walk their operands. - const MDNode *N = dyn_cast<MDNode>(MD); - if (N && N->isFunctionLocal() && N->getFunction()) { - EnumerateMDNodeOperands(N); - return; - } +void ValueEnumerator::EnumerateMetadata(const Metadata *MD) { + assert( + (isa<MDNode>(MD) || isa<MDString>(MD) || isa<ConstantAsMetadata>(MD)) && + "Invalid metadata kind"); // Insert a dummy ID to block the co-recursive call to // EnumerateMDNodeOperands() from re-visiting MD in a cyclic graph. @@ -508,55 +536,43 @@ void ValueEnumerator::EnumerateMetadata(const Value *MD) { if (!MDValueMap.insert(std::make_pair(MD, 0)).second) return; - // Enumerate the type of this value. - EnumerateType(MD->getType()); - // Visit operands first to minimize RAUW. - if (N) + if (auto *N = dyn_cast<MDNode>(MD)) EnumerateMDNodeOperands(N); + else if (auto *C = dyn_cast<ConstantAsMetadata>(MD)) + EnumerateValue(C->getValue()); + + HasMDString |= isa<MDString>(MD); + HasMDLocation |= isa<MDLocation>(MD); + HasGenericDebugNode |= isa<GenericDebugNode>(MD); // Replace the dummy ID inserted above with the correct one. MDValueMap may // have changed by inserting operands, so we need a fresh lookup here. - MDValues.push_back(MD); - MDValueMap[MD] = MDValues.size(); + MDs.push_back(MD); + MDValueMap[MD] = MDs.size(); } /// EnumerateFunctionLocalMetadataa - Incorporate function-local metadata -/// information reachable from the given MDNode. -void ValueEnumerator::EnumerateFunctionLocalMetadata(const MDNode *N) { - assert(N->isFunctionLocal() && N->getFunction() && - "EnumerateFunctionLocalMetadata called on non-function-local mdnode!"); - - // Enumerate the type of this value. - EnumerateType(N->getType()); - +/// information reachable from the metadata. +void ValueEnumerator::EnumerateFunctionLocalMetadata( + const LocalAsMetadata *Local) { // Check to see if it's already in! - unsigned &MDValueID = MDValueMap[N]; + unsigned &MDValueID = MDValueMap[Local]; if (MDValueID) return; - MDValues.push_back(N); - MDValueID = MDValues.size(); - - // To incoroporate function-local information visit all function-local - // MDNodes and all function-local values they reference. - for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) - if (Value *V = N->getOperand(i)) { - if (MDNode *O = dyn_cast<MDNode>(V)) { - if (O->isFunctionLocal() && O->getFunction()) - EnumerateFunctionLocalMetadata(O); - } else if (isa<Instruction>(V) || isa<Argument>(V)) - EnumerateValue(V); - } + MDs.push_back(Local); + MDValueID = MDs.size(); + + EnumerateValue(Local->getValue()); - // Also, collect all function-local MDNodes for easy access. - FunctionLocalMDs.push_back(N); + // Also, collect all function-local metadata for easy access. + FunctionLocalMDs.push_back(Local); } void ValueEnumerator::EnumerateValue(const Value *V) { assert(!V->getType()->isVoidTy() && "Can't insert void values!"); - assert(!isa<MDNode>(V) && !isa<MDString>(V) && - "EnumerateValue doesn't handle Metadata!"); + assert(!isa<MetadataAsValue>(V) && "EnumerateValue doesn't handle Metadata!"); // Check to see if it's already in! unsigned &ValueID = ValueMap[V]; @@ -620,9 +636,8 @@ void ValueEnumerator::EnumerateType(Type *Ty) { // Enumerate all of the subtypes before we enumerate this type. This ensures // that the type will be enumerated in an order that can be directly built. - for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end(); - I != E; ++I) - EnumerateType(*I); + for (Type *SubTy : Ty->subtypes()) + EnumerateType(SubTy); // Refresh the TypeID pointer in case the table rehashed. TypeID = &TypeMap[Ty]; @@ -646,30 +661,35 @@ void ValueEnumerator::EnumerateType(Type *Ty) { void ValueEnumerator::EnumerateOperandType(const Value *V) { EnumerateType(V->getType()); - if (const Constant *C = dyn_cast<Constant>(V)) { - // If this constant is already enumerated, ignore it, we know its type must - // be enumerated. - if (ValueMap.count(V)) return; + if (auto *MD = dyn_cast<MetadataAsValue>(V)) { + assert(!isa<LocalAsMetadata>(MD->getMetadata()) && + "Function-local metadata should be left for later"); + + EnumerateMetadata(MD->getMetadata()); + return; + } - // This constant may have operands, make sure to enumerate the types in - // them. - for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) { - const Value *Op = C->getOperand(i); + const Constant *C = dyn_cast<Constant>(V); + if (!C) + return; - // Don't enumerate basic blocks here, this happens as operands to - // blockaddress. - if (isa<BasicBlock>(Op)) continue; + // If this constant is already enumerated, ignore it, we know its type must + // be enumerated. + if (ValueMap.count(C)) + return; - EnumerateOperandType(Op); - } + // This constant may have operands, make sure to enumerate the types in + // them. + for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) { + const Value *Op = C->getOperand(i); - if (const MDNode *N = dyn_cast<MDNode>(V)) { - for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) - if (Value *Elem = N->getOperand(i)) - EnumerateOperandType(Elem); - } - } else if (isa<MDString>(V) || isa<MDNode>(V)) - EnumerateMetadata(V); + // Don't enumerate basic blocks here, this happens as operands to + // blockaddress. + if (isa<BasicBlock>(Op)) + continue; + + EnumerateOperandType(Op); + } } void ValueEnumerator::EnumerateAttributes(AttributeSet PAL) { @@ -697,7 +717,7 @@ void ValueEnumerator::EnumerateAttributes(AttributeSet PAL) { void ValueEnumerator::incorporateFunction(const Function &F) { InstructionCount = 0; NumModuleValues = Values.size(); - NumModuleMDValues = MDValues.size(); + NumModuleMDs = MDs.size(); // Adding function arguments to the value table. for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); @@ -728,24 +748,16 @@ void ValueEnumerator::incorporateFunction(const Function &F) { FirstInstID = Values.size(); - SmallVector<MDNode *, 8> FnLocalMDVector; + SmallVector<LocalAsMetadata *, 8> FnLocalMDVector; // Add all of the instructions. for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) { for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) { for (User::const_op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI) { - if (MDNode *MD = dyn_cast<MDNode>(*OI)) - if (MD->isFunctionLocal() && MD->getFunction()) + if (auto *MD = dyn_cast<MetadataAsValue>(&*OI)) + if (auto *Local = dyn_cast<LocalAsMetadata>(MD->getMetadata())) // Enumerate metadata after the instructions they might refer to. - FnLocalMDVector.push_back(MD); - } - - SmallVector<std::pair<unsigned, MDNode *>, 8> MDs; - I->getAllMetadataOtherThanDebugLoc(MDs); - for (unsigned i = 0, e = MDs.size(); i != e; ++i) { - MDNode *N = MDs[i].second; - if (N->isFunctionLocal() && N->getFunction()) - FnLocalMDVector.push_back(N); + FnLocalMDVector.push_back(Local); } if (!I->getType()->isVoidTy()) @@ -762,13 +774,13 @@ void ValueEnumerator::purgeFunction() { /// Remove purged values from the ValueMap. for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i) ValueMap.erase(Values[i].first); - for (unsigned i = NumModuleMDValues, e = MDValues.size(); i != e; ++i) - MDValueMap.erase(MDValues[i]); + for (unsigned i = NumModuleMDs, e = MDs.size(); i != e; ++i) + MDValueMap.erase(MDs[i]); for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i) ValueMap.erase(BasicBlocks[i]); Values.resize(NumModuleValues); - MDValues.resize(NumModuleMDValues); + MDs.resize(NumModuleMDs); BasicBlocks.clear(); FunctionLocalMDs.clear(); } @@ -792,3 +804,6 @@ unsigned ValueEnumerator::getGlobalBasicBlockID(const BasicBlock *BB) const { return getGlobalBasicBlockID(BB); } +uint64_t ValueEnumerator::computeBitsRequiredForTypeIndicies() const { + return Log2_32_Ceil(getTypes().size() + 1); +} diff --git a/lib/Bitcode/Writer/ValueEnumerator.h b/lib/Bitcode/Writer/ValueEnumerator.h index 563c214..b94c370 100644 --- a/lib/Bitcode/Writer/ValueEnumerator.h +++ b/lib/Bitcode/Writer/ValueEnumerator.h @@ -30,6 +30,8 @@ class BasicBlock; class Comdat; class Function; class Module; +class Metadata; +class LocalAsMetadata; class MDNode; class NamedMDNode; class AttributeSet; @@ -58,9 +60,13 @@ private: typedef UniqueVector<const Comdat *> ComdatSetType; ComdatSetType Comdats; - std::vector<const Value *> MDValues; - SmallVector<const MDNode *, 8> FunctionLocalMDs; - ValueMapType MDValueMap; + std::vector<const Metadata *> MDs; + SmallVector<const LocalAsMetadata *, 8> FunctionLocalMDs; + typedef DenseMap<const Metadata *, unsigned> MetadataMapType; + MetadataMapType MDValueMap; + bool HasMDString; + bool HasMDLocation; + bool HasGenericDebugNode; typedef DenseMap<AttributeSet, unsigned> AttributeGroupMapType; AttributeGroupMapType AttributeGroupMap; @@ -88,20 +94,34 @@ private: /// When a function is incorporated, this is the size of the MDValues list /// before incorporation. - unsigned NumModuleMDValues; + unsigned NumModuleMDs; unsigned FirstFuncConstantID; unsigned FirstInstID; - ValueEnumerator(const ValueEnumerator &) LLVM_DELETED_FUNCTION; - void operator=(const ValueEnumerator &) LLVM_DELETED_FUNCTION; + ValueEnumerator(const ValueEnumerator &) = delete; + void operator=(const ValueEnumerator &) = delete; public: ValueEnumerator(const Module &M); void dump() const; void print(raw_ostream &OS, const ValueMapType &Map, const char *Name) const; + void print(raw_ostream &OS, const MetadataMapType &Map, + const char *Name) const; unsigned getValueID(const Value *V) const; + unsigned getMetadataID(const Metadata *MD) const { + auto ID = getMetadataOrNullID(MD); + assert(ID != 0 && "Metadata not in slotcalculator!"); + return ID - 1; + } + unsigned getMetadataOrNullID(const Metadata *MD) const { + return MDValueMap.lookup(MD); + } + + bool hasMDString() const { return HasMDString; } + bool hasMDLocation() const { return HasMDLocation; } + bool hasGenericDebugNode() const { return HasGenericDebugNode; } unsigned getTypeID(Type *T) const { TypeMapType::const_iterator I = TypeMap.find(T); @@ -134,8 +154,8 @@ public: } const ValueList &getValues() const { return Values; } - const std::vector<const Value *> &getMDValues() const { return MDValues; } - const SmallVectorImpl<const MDNode *> &getFunctionLocalMDValues() const { + const std::vector<const Metadata *> &getMDs() const { return MDs; } + const SmallVectorImpl<const LocalAsMetadata *> &getFunctionLocalMDs() const { return FunctionLocalMDs; } const TypeList &getTypes() const { return Types; } @@ -162,13 +182,14 @@ public: /// void incorporateFunction(const Function &F); void purgeFunction(); + uint64_t computeBitsRequiredForTypeIndicies() const; private: void OptimizeConstants(unsigned CstStart, unsigned CstEnd); void EnumerateMDNodeOperands(const MDNode *N); - void EnumerateMetadata(const Value *MD); - void EnumerateFunctionLocalMetadata(const MDNode *N); + void EnumerateMetadata(const Metadata *MD); + void EnumerateFunctionLocalMetadata(const LocalAsMetadata *Local); void EnumerateNamedMDNode(const NamedMDNode *NMD); void EnumerateValue(const Value *V); void EnumerateType(Type *T); |