1 //===- BitcodeReader.cpp - Internal BitcodeReader implementation ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Bitcode/ReaderWriter.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/SmallVector.h"
14 #include "llvm/ADT/Triple.h"
15 #include "llvm/Bitcode/BitstreamReader.h"
16 #include "llvm/Bitcode/LLVMBitCodes.h"
17 #include "llvm/IR/AutoUpgrade.h"
18 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/DebugInfo.h"
20 #include "llvm/IR/DebugInfoMetadata.h"
21 #include "llvm/IR/DerivedTypes.h"
22 #include "llvm/IR/DiagnosticPrinter.h"
23 #include "llvm/IR/GVMaterializer.h"
24 #include "llvm/IR/InlineAsm.h"
25 #include "llvm/IR/IntrinsicInst.h"
26 #include "llvm/IR/LLVMContext.h"
27 #include "llvm/IR/Module.h"
28 #include "llvm/IR/OperandTraits.h"
29 #include "llvm/IR/Operator.h"
30 #include "llvm/IR/FunctionInfo.h"
31 #include "llvm/IR/ValueHandle.h"
32 #include "llvm/Support/DataStream.h"
33 #include "llvm/Support/ManagedStatic.h"
34 #include "llvm/Support/MathExtras.h"
35 #include "llvm/Support/MemoryBuffer.h"
36 #include "llvm/Support/raw_ostream.h"
42 SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex
45 /// Indicates which operator an operand allows (for the few operands that may
46 /// only reference a certain operator).
47 enum OperatorConstraint {
48 OC_None = 0, // No constraint
49 OC_CatchPad, // Must be CatchPadInst
50 OC_CleanupPad // Must be CleanupPadInst
53 class BitcodeReaderValueList {
54 std::vector<WeakVH> ValuePtrs;
56 /// As we resolve forward-referenced constants, we add information about them
57 /// to this vector. This allows us to resolve them in bulk instead of
58 /// resolving each reference at a time. See the code in
59 /// ResolveConstantForwardRefs for more information about this.
61 /// The key of this vector is the placeholder constant, the value is the slot
62 /// number that holds the resolved value.
63 typedef std::vector<std::pair<Constant*, unsigned> > ResolveConstantsTy;
64 ResolveConstantsTy ResolveConstants;
67 BitcodeReaderValueList(LLVMContext &C) : Context(C) {}
68 ~BitcodeReaderValueList() {
69 assert(ResolveConstants.empty() && "Constants not resolved?");
72 // vector compatibility methods
73 unsigned size() const { return ValuePtrs.size(); }
74 void resize(unsigned N) { ValuePtrs.resize(N); }
75 void push_back(Value *V) { ValuePtrs.emplace_back(V); }
78 assert(ResolveConstants.empty() && "Constants not resolved?");
82 Value *operator[](unsigned i) const {
83 assert(i < ValuePtrs.size());
87 Value *back() const { return ValuePtrs.back(); }
88 void pop_back() { ValuePtrs.pop_back(); }
89 bool empty() const { return ValuePtrs.empty(); }
90 void shrinkTo(unsigned N) {
91 assert(N <= size() && "Invalid shrinkTo request!");
95 Constant *getConstantFwdRef(unsigned Idx, Type *Ty);
96 Value *getValueFwdRef(unsigned Idx, Type *Ty,
97 OperatorConstraint OC = OC_None);
99 bool assignValue(Value *V, unsigned Idx);
101 /// Once all constants are read, this method bulk resolves any forward
103 void resolveConstantForwardRefs();
106 class BitcodeReaderMDValueList {
111 std::vector<TrackingMDRef> MDValuePtrs;
113 LLVMContext &Context;
115 BitcodeReaderMDValueList(LLVMContext &C)
116 : NumFwdRefs(0), AnyFwdRefs(false), Context(C) {}
118 // vector compatibility methods
119 unsigned size() const { return MDValuePtrs.size(); }
120 void resize(unsigned N) { MDValuePtrs.resize(N); }
121 void push_back(Metadata *MD) { MDValuePtrs.emplace_back(MD); }
122 void clear() { MDValuePtrs.clear(); }
123 Metadata *back() const { return MDValuePtrs.back(); }
124 void pop_back() { MDValuePtrs.pop_back(); }
125 bool empty() const { return MDValuePtrs.empty(); }
127 Metadata *operator[](unsigned i) const {
128 assert(i < MDValuePtrs.size());
129 return MDValuePtrs[i];
132 void shrinkTo(unsigned N) {
133 assert(N <= size() && "Invalid shrinkTo request!");
134 MDValuePtrs.resize(N);
137 Metadata *getValueFwdRef(unsigned Idx);
138 void assignValue(Metadata *MD, unsigned Idx);
139 void tryToResolveCycles();
142 class BitcodeReader : public GVMaterializer {
143 LLVMContext &Context;
144 DiagnosticHandlerFunction DiagnosticHandler;
145 Module *TheModule = nullptr;
146 std::unique_ptr<MemoryBuffer> Buffer;
147 std::unique_ptr<BitstreamReader> StreamFile;
148 BitstreamCursor Stream;
149 // Next offset to start scanning for lazy parsing of function bodies.
150 uint64_t NextUnreadBit = 0;
151 // Last function offset found in the VST.
152 uint64_t LastFunctionBlockBit = 0;
153 bool SeenValueSymbolTable = false;
154 uint64_t VSTOffset = 0;
155 // Contains an arbitrary and optional string identifying the bitcode producer
156 std::string ProducerIdentification;
157 // Number of module level metadata records specified by the
158 // MODULE_CODE_METADATA_VALUES record.
159 unsigned NumModuleMDs = 0;
160 // Support older bitcode without the MODULE_CODE_METADATA_VALUES record.
161 bool SeenModuleValuesRecord = false;
163 std::vector<Type*> TypeList;
164 BitcodeReaderValueList ValueList;
165 BitcodeReaderMDValueList MDValueList;
166 std::vector<Comdat *> ComdatList;
167 SmallVector<Instruction *, 64> InstructionList;
169 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
170 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
171 std::vector<std::pair<Function*, unsigned> > FunctionPrefixes;
172 std::vector<std::pair<Function*, unsigned> > FunctionPrologues;
173 std::vector<std::pair<Function*, unsigned> > FunctionPersonalityFns;
175 SmallVector<Instruction*, 64> InstsWithTBAATag;
177 /// The set of attributes by index. Index zero in the file is for null, and
178 /// is thus not represented here. As such all indices are off by one.
179 std::vector<AttributeSet> MAttributes;
181 /// The set of attribute groups.
182 std::map<unsigned, AttributeSet> MAttributeGroups;
184 /// While parsing a function body, this is a list of the basic blocks for the
186 std::vector<BasicBlock*> FunctionBBs;
188 // When reading the module header, this list is populated with functions that
189 // have bodies later in the file.
190 std::vector<Function*> FunctionsWithBodies;
192 // When intrinsic functions are encountered which require upgrading they are
193 // stored here with their replacement function.
194 typedef DenseMap<Function*, Function*> UpgradedIntrinsicMap;
195 UpgradedIntrinsicMap UpgradedIntrinsics;
197 // Map the bitcode's custom MDKind ID to the Module's MDKind ID.
198 DenseMap<unsigned, unsigned> MDKindMap;
200 // Several operations happen after the module header has been read, but
201 // before function bodies are processed. This keeps track of whether
202 // we've done this yet.
203 bool SeenFirstFunctionBody = false;
205 /// When function bodies are initially scanned, this map contains info about
206 /// where to find deferred function body in the stream.
207 DenseMap<Function*, uint64_t> DeferredFunctionInfo;
209 /// When Metadata block is initially scanned when parsing the module, we may
210 /// choose to defer parsing of the metadata. This vector contains info about
211 /// which Metadata blocks are deferred.
212 std::vector<uint64_t> DeferredMetadataInfo;
214 /// These are basic blocks forward-referenced by block addresses. They are
215 /// inserted lazily into functions when they're loaded. The basic block ID is
216 /// its index into the vector.
217 DenseMap<Function *, std::vector<BasicBlock *>> BasicBlockFwdRefs;
218 std::deque<Function *> BasicBlockFwdRefQueue;
220 /// Indicates that we are using a new encoding for instruction operands where
221 /// most operands in the current FUNCTION_BLOCK are encoded relative to the
222 /// instruction number, for a more compact encoding. Some instruction
223 /// operands are not relative to the instruction ID: basic block numbers, and
224 /// types. Once the old style function blocks have been phased out, we would
225 /// not need this flag.
226 bool UseRelativeIDs = false;
228 /// True if all functions will be materialized, negating the need to process
229 /// (e.g.) blockaddress forward references.
230 bool WillMaterializeAllForwardRefs = false;
232 /// Functions that have block addresses taken. This is usually empty.
233 SmallPtrSet<const Function *, 4> BlockAddressesTaken;
235 /// True if any Metadata block has been materialized.
236 bool IsMetadataMaterialized = false;
238 bool StripDebugInfo = false;
240 /// Functions that need to be matched with subprograms when upgrading old
242 SmallDenseMap<Function *, DISubprogram *, 16> FunctionsWithSPs;
244 std::vector<std::string> BundleTags;
247 std::error_code error(BitcodeError E, const Twine &Message);
248 std::error_code error(BitcodeError E);
249 std::error_code error(const Twine &Message);
251 BitcodeReader(MemoryBuffer *Buffer, LLVMContext &Context,
252 DiagnosticHandlerFunction DiagnosticHandler);
253 BitcodeReader(LLVMContext &Context,
254 DiagnosticHandlerFunction DiagnosticHandler);
255 ~BitcodeReader() override { freeState(); }
257 std::error_code materializeForwardReferencedFunctions();
261 void releaseBuffer();
263 bool isDematerializable(const GlobalValue *GV) const override;
264 std::error_code materialize(GlobalValue *GV) override;
265 std::error_code materializeModule(Module *M) override;
266 std::vector<StructType *> getIdentifiedStructTypes() const override;
267 void dematerialize(GlobalValue *GV) override;
269 /// \brief Main interface to parsing a bitcode buffer.
270 /// \returns true if an error occurred.
271 std::error_code parseBitcodeInto(std::unique_ptr<DataStreamer> Streamer,
273 bool ShouldLazyLoadMetadata = false);
275 /// \brief Cheap mechanism to just extract module triple
276 /// \returns true if an error occurred.
277 ErrorOr<std::string> parseTriple();
279 /// Cheap mechanism to just extract the identification block out of bitcode.
280 ErrorOr<std::string> parseIdentificationBlock();
282 static uint64_t decodeSignRotatedValue(uint64_t V);
284 /// Materialize any deferred Metadata block.
285 std::error_code materializeMetadata() override;
287 void setStripDebugInfo() override;
290 /// Parse the "IDENTIFICATION_BLOCK_ID" block, populate the
291 // ProducerIdentification data member, and do some basic enforcement on the
292 // "epoch" encoded in the bitcode.
293 std::error_code parseBitcodeVersion();
295 std::vector<StructType *> IdentifiedStructTypes;
296 StructType *createIdentifiedStructType(LLVMContext &Context, StringRef Name);
297 StructType *createIdentifiedStructType(LLVMContext &Context);
299 Type *getTypeByID(unsigned ID);
300 Value *getFnValueByID(unsigned ID, Type *Ty,
301 OperatorConstraint OC = OC_None) {
302 if (Ty && Ty->isMetadataTy())
303 return MetadataAsValue::get(Ty->getContext(), getFnMetadataByID(ID));
304 return ValueList.getValueFwdRef(ID, Ty, OC);
306 Metadata *getFnMetadataByID(unsigned ID) {
307 return MDValueList.getValueFwdRef(ID);
309 BasicBlock *getBasicBlock(unsigned ID) const {
310 if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID
311 return FunctionBBs[ID];
313 AttributeSet getAttributes(unsigned i) const {
314 if (i-1 < MAttributes.size())
315 return MAttributes[i-1];
316 return AttributeSet();
319 /// Read a value/type pair out of the specified record from slot 'Slot'.
320 /// Increment Slot past the number of slots used in the record. Return true on
322 bool getValueTypePair(SmallVectorImpl<uint64_t> &Record, unsigned &Slot,
323 unsigned InstNum, Value *&ResVal) {
324 if (Slot == Record.size()) return true;
325 unsigned ValNo = (unsigned)Record[Slot++];
326 // Adjust the ValNo, if it was encoded relative to the InstNum.
328 ValNo = InstNum - ValNo;
329 if (ValNo < InstNum) {
330 // If this is not a forward reference, just return the value we already
332 ResVal = getFnValueByID(ValNo, nullptr);
333 return ResVal == nullptr;
335 if (Slot == Record.size())
338 unsigned TypeNo = (unsigned)Record[Slot++];
339 ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
340 return ResVal == nullptr;
343 /// Read a value out of the specified record from slot 'Slot'. Increment Slot
344 /// past the number of slots used by the value in the record. Return true if
345 /// there is an error.
346 bool popValue(SmallVectorImpl<uint64_t> &Record, unsigned &Slot,
347 unsigned InstNum, Type *Ty, Value *&ResVal,
348 OperatorConstraint OC = OC_None) {
349 if (getValue(Record, Slot, InstNum, Ty, ResVal, OC))
351 // All values currently take a single record slot.
356 /// Like popValue, but does not increment the Slot number.
357 bool getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
358 unsigned InstNum, Type *Ty, Value *&ResVal,
359 OperatorConstraint OC = OC_None) {
360 ResVal = getValue(Record, Slot, InstNum, Ty, OC);
361 return ResVal == nullptr;
364 /// Version of getValue that returns ResVal directly, or 0 if there is an
366 Value *getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
367 unsigned InstNum, Type *Ty, OperatorConstraint OC = OC_None) {
368 if (Slot == Record.size()) return nullptr;
369 unsigned ValNo = (unsigned)Record[Slot];
370 // Adjust the ValNo, if it was encoded relative to the InstNum.
372 ValNo = InstNum - ValNo;
373 return getFnValueByID(ValNo, Ty, OC);
376 /// Like getValue, but decodes signed VBRs.
377 Value *getValueSigned(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
378 unsigned InstNum, Type *Ty,
379 OperatorConstraint OC = OC_None) {
380 if (Slot == Record.size()) return nullptr;
381 unsigned ValNo = (unsigned)decodeSignRotatedValue(Record[Slot]);
382 // Adjust the ValNo, if it was encoded relative to the InstNum.
384 ValNo = InstNum - ValNo;
385 return getFnValueByID(ValNo, Ty, OC);
388 /// Converts alignment exponent (i.e. power of two (or zero)) to the
389 /// corresponding alignment to use. If alignment is too large, returns
390 /// a corresponding error code.
391 std::error_code parseAlignmentValue(uint64_t Exponent, unsigned &Alignment);
392 std::error_code parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind);
393 std::error_code parseModule(uint64_t ResumeBit,
394 bool ShouldLazyLoadMetadata = false);
395 std::error_code parseAttributeBlock();
396 std::error_code parseAttributeGroupBlock();
397 std::error_code parseTypeTable();
398 std::error_code parseTypeTableBody();
399 std::error_code parseOperandBundleTags();
401 ErrorOr<Value *> recordValue(SmallVectorImpl<uint64_t> &Record,
402 unsigned NameIndex, Triple &TT);
403 std::error_code parseValueSymbolTable(uint64_t Offset = 0);
404 std::error_code parseConstants();
405 std::error_code rememberAndSkipFunctionBodies();
406 std::error_code rememberAndSkipFunctionBody();
407 /// Save the positions of the Metadata blocks and skip parsing the blocks.
408 std::error_code rememberAndSkipMetadata();
409 std::error_code parseFunctionBody(Function *F);
410 std::error_code globalCleanup();
411 std::error_code resolveGlobalAndAliasInits();
412 std::error_code parseMetadata(bool ModuleLevel = false);
413 std::error_code parseMetadataKinds();
414 std::error_code parseMetadataKindRecord(SmallVectorImpl<uint64_t> &Record);
415 std::error_code parseMetadataAttachment(Function &F);
416 ErrorOr<std::string> parseModuleTriple();
417 std::error_code parseUseLists();
418 std::error_code initStream(std::unique_ptr<DataStreamer> Streamer);
419 std::error_code initStreamFromBuffer();
420 std::error_code initLazyStream(std::unique_ptr<DataStreamer> Streamer);
421 std::error_code findFunctionInStream(
423 DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator);
426 /// Class to manage reading and parsing function summary index bitcode
428 class FunctionIndexBitcodeReader {
429 DiagnosticHandlerFunction DiagnosticHandler;
431 /// Eventually points to the function index built during parsing.
432 FunctionInfoIndex *TheIndex = nullptr;
434 std::unique_ptr<MemoryBuffer> Buffer;
435 std::unique_ptr<BitstreamReader> StreamFile;
436 BitstreamCursor Stream;
438 /// \brief Used to indicate whether we are doing lazy parsing of summary data.
440 /// If false, the summary section is fully parsed into the index during
441 /// the initial parse. Otherwise, if true, the caller is expected to
442 /// invoke \a readFunctionSummary for each summary needed, and the summary
443 /// section is thus parsed lazily.
446 /// Used to indicate whether caller only wants to check for the presence
447 /// of the function summary bitcode section. All blocks are skipped,
448 /// but the SeenFuncSummary boolean is set.
449 bool CheckFuncSummaryPresenceOnly = false;
451 /// Indicates whether we have encountered a function summary section
452 /// yet during parsing, used when checking if file contains function
454 bool SeenFuncSummary = false;
456 /// \brief Map populated during function summary section parsing, and
457 /// consumed during ValueSymbolTable parsing.
459 /// Used to correlate summary records with VST entries. For the per-module
460 /// index this maps the ValueID to the parsed function summary, and
461 /// for the combined index this maps the summary record's bitcode
462 /// offset to the function summary (since in the combined index the
463 /// VST records do not hold value IDs but rather hold the function
464 /// summary record offset).
465 DenseMap<uint64_t, std::unique_ptr<FunctionSummary>> SummaryMap;
467 /// Map populated during module path string table parsing, from the
468 /// module ID to a string reference owned by the index's module
469 /// path string table, used to correlate with combined index function
471 DenseMap<uint64_t, StringRef> ModuleIdMap;
474 std::error_code error(BitcodeError E, const Twine &Message);
475 std::error_code error(BitcodeError E);
476 std::error_code error(const Twine &Message);
478 FunctionIndexBitcodeReader(MemoryBuffer *Buffer,
479 DiagnosticHandlerFunction DiagnosticHandler,
481 bool CheckFuncSummaryPresenceOnly = false);
482 FunctionIndexBitcodeReader(DiagnosticHandlerFunction DiagnosticHandler,
484 bool CheckFuncSummaryPresenceOnly = false);
485 ~FunctionIndexBitcodeReader() { freeState(); }
489 void releaseBuffer();
491 /// Check if the parser has encountered a function summary section.
492 bool foundFuncSummary() { return SeenFuncSummary; }
494 /// \brief Main interface to parsing a bitcode buffer.
495 /// \returns true if an error occurred.
496 std::error_code parseSummaryIndexInto(std::unique_ptr<DataStreamer> Streamer,
497 FunctionInfoIndex *I);
499 /// \brief Interface for parsing a function summary lazily.
500 std::error_code parseFunctionSummary(std::unique_ptr<DataStreamer> Streamer,
501 FunctionInfoIndex *I,
502 size_t FunctionSummaryOffset);
505 std::error_code parseModule();
506 std::error_code parseValueSymbolTable();
507 std::error_code parseEntireSummary();
508 std::error_code parseModuleStringTable();
509 std::error_code initStream(std::unique_ptr<DataStreamer> Streamer);
510 std::error_code initStreamFromBuffer();
511 std::error_code initLazyStream(std::unique_ptr<DataStreamer> Streamer);
515 BitcodeDiagnosticInfo::BitcodeDiagnosticInfo(std::error_code EC,
516 DiagnosticSeverity Severity,
518 : DiagnosticInfo(DK_Bitcode, Severity), Msg(Msg), EC(EC) {}
520 void BitcodeDiagnosticInfo::print(DiagnosticPrinter &DP) const { DP << Msg; }
522 static std::error_code error(DiagnosticHandlerFunction DiagnosticHandler,
523 std::error_code EC, const Twine &Message) {
524 BitcodeDiagnosticInfo DI(EC, DS_Error, Message);
525 DiagnosticHandler(DI);
529 static std::error_code error(DiagnosticHandlerFunction DiagnosticHandler,
530 std::error_code EC) {
531 return error(DiagnosticHandler, EC, EC.message());
534 static std::error_code error(DiagnosticHandlerFunction DiagnosticHandler,
535 const Twine &Message) {
536 return error(DiagnosticHandler,
537 make_error_code(BitcodeError::CorruptedBitcode), Message);
540 std::error_code BitcodeReader::error(BitcodeError E, const Twine &Message) {
541 if (!ProducerIdentification.empty()) {
542 return ::error(DiagnosticHandler, make_error_code(E),
543 Message + " (Producer: '" + ProducerIdentification +
544 "' Reader: 'LLVM " + LLVM_VERSION_STRING "')");
546 return ::error(DiagnosticHandler, make_error_code(E), Message);
549 std::error_code BitcodeReader::error(const Twine &Message) {
550 if (!ProducerIdentification.empty()) {
551 return ::error(DiagnosticHandler,
552 make_error_code(BitcodeError::CorruptedBitcode),
553 Message + " (Producer: '" + ProducerIdentification +
554 "' Reader: 'LLVM " + LLVM_VERSION_STRING "')");
556 return ::error(DiagnosticHandler,
557 make_error_code(BitcodeError::CorruptedBitcode), Message);
560 std::error_code BitcodeReader::error(BitcodeError E) {
561 return ::error(DiagnosticHandler, make_error_code(E));
564 static DiagnosticHandlerFunction getDiagHandler(DiagnosticHandlerFunction F,
568 return [&C](const DiagnosticInfo &DI) { C.diagnose(DI); };
571 BitcodeReader::BitcodeReader(MemoryBuffer *Buffer, LLVMContext &Context,
572 DiagnosticHandlerFunction DiagnosticHandler)
574 DiagnosticHandler(getDiagHandler(DiagnosticHandler, Context)),
575 Buffer(Buffer), ValueList(Context), MDValueList(Context) {}
577 BitcodeReader::BitcodeReader(LLVMContext &Context,
578 DiagnosticHandlerFunction DiagnosticHandler)
580 DiagnosticHandler(getDiagHandler(DiagnosticHandler, Context)),
581 Buffer(nullptr), ValueList(Context), MDValueList(Context) {}
583 std::error_code BitcodeReader::materializeForwardReferencedFunctions() {
584 if (WillMaterializeAllForwardRefs)
585 return std::error_code();
587 // Prevent recursion.
588 WillMaterializeAllForwardRefs = true;
590 while (!BasicBlockFwdRefQueue.empty()) {
591 Function *F = BasicBlockFwdRefQueue.front();
592 BasicBlockFwdRefQueue.pop_front();
593 assert(F && "Expected valid function");
594 if (!BasicBlockFwdRefs.count(F))
595 // Already materialized.
598 // Check for a function that isn't materializable to prevent an infinite
599 // loop. When parsing a blockaddress stored in a global variable, there
600 // isn't a trivial way to check if a function will have a body without a
601 // linear search through FunctionsWithBodies, so just check it here.
602 if (!F->isMaterializable())
603 return error("Never resolved function from blockaddress");
605 // Try to materialize F.
606 if (std::error_code EC = materialize(F))
609 assert(BasicBlockFwdRefs.empty() && "Function missing from queue");
612 WillMaterializeAllForwardRefs = false;
613 return std::error_code();
616 void BitcodeReader::freeState() {
618 std::vector<Type*>().swap(TypeList);
621 std::vector<Comdat *>().swap(ComdatList);
623 std::vector<AttributeSet>().swap(MAttributes);
624 std::vector<BasicBlock*>().swap(FunctionBBs);
625 std::vector<Function*>().swap(FunctionsWithBodies);
626 DeferredFunctionInfo.clear();
627 DeferredMetadataInfo.clear();
630 assert(BasicBlockFwdRefs.empty() && "Unresolved blockaddress fwd references");
631 BasicBlockFwdRefQueue.clear();
634 //===----------------------------------------------------------------------===//
635 // Helper functions to implement forward reference resolution, etc.
636 //===----------------------------------------------------------------------===//
638 /// Convert a string from a record into an std::string, return true on failure.
639 template <typename StrTy>
640 static bool convertToString(ArrayRef<uint64_t> Record, unsigned Idx,
642 if (Idx > Record.size())
645 for (unsigned i = Idx, e = Record.size(); i != e; ++i)
646 Result += (char)Record[i];
650 static bool hasImplicitComdat(size_t Val) {
654 case 1: // Old WeakAnyLinkage
655 case 4: // Old LinkOnceAnyLinkage
656 case 10: // Old WeakODRLinkage
657 case 11: // Old LinkOnceODRLinkage
662 static GlobalValue::LinkageTypes getDecodedLinkage(unsigned Val) {
664 default: // Map unknown/new linkages to external
666 return GlobalValue::ExternalLinkage;
668 return GlobalValue::AppendingLinkage;
670 return GlobalValue::InternalLinkage;
672 return GlobalValue::ExternalLinkage; // Obsolete DLLImportLinkage
674 return GlobalValue::ExternalLinkage; // Obsolete DLLExportLinkage
676 return GlobalValue::ExternalWeakLinkage;
678 return GlobalValue::CommonLinkage;
680 return GlobalValue::PrivateLinkage;
682 return GlobalValue::AvailableExternallyLinkage;
684 return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateLinkage
686 return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateWeakLinkage
688 return GlobalValue::ExternalLinkage; // Obsolete LinkOnceODRAutoHideLinkage
689 case 1: // Old value with implicit comdat.
691 return GlobalValue::WeakAnyLinkage;
692 case 10: // Old value with implicit comdat.
694 return GlobalValue::WeakODRLinkage;
695 case 4: // Old value with implicit comdat.
697 return GlobalValue::LinkOnceAnyLinkage;
698 case 11: // Old value with implicit comdat.
700 return GlobalValue::LinkOnceODRLinkage;
704 static GlobalValue::VisibilityTypes getDecodedVisibility(unsigned Val) {
706 default: // Map unknown visibilities to default.
707 case 0: return GlobalValue::DefaultVisibility;
708 case 1: return GlobalValue::HiddenVisibility;
709 case 2: return GlobalValue::ProtectedVisibility;
713 static GlobalValue::DLLStorageClassTypes
714 getDecodedDLLStorageClass(unsigned Val) {
716 default: // Map unknown values to default.
717 case 0: return GlobalValue::DefaultStorageClass;
718 case 1: return GlobalValue::DLLImportStorageClass;
719 case 2: return GlobalValue::DLLExportStorageClass;
723 static GlobalVariable::ThreadLocalMode getDecodedThreadLocalMode(unsigned Val) {
725 case 0: return GlobalVariable::NotThreadLocal;
726 default: // Map unknown non-zero value to general dynamic.
727 case 1: return GlobalVariable::GeneralDynamicTLSModel;
728 case 2: return GlobalVariable::LocalDynamicTLSModel;
729 case 3: return GlobalVariable::InitialExecTLSModel;
730 case 4: return GlobalVariable::LocalExecTLSModel;
734 static int getDecodedCastOpcode(unsigned Val) {
737 case bitc::CAST_TRUNC : return Instruction::Trunc;
738 case bitc::CAST_ZEXT : return Instruction::ZExt;
739 case bitc::CAST_SEXT : return Instruction::SExt;
740 case bitc::CAST_FPTOUI : return Instruction::FPToUI;
741 case bitc::CAST_FPTOSI : return Instruction::FPToSI;
742 case bitc::CAST_UITOFP : return Instruction::UIToFP;
743 case bitc::CAST_SITOFP : return Instruction::SIToFP;
744 case bitc::CAST_FPTRUNC : return Instruction::FPTrunc;
745 case bitc::CAST_FPEXT : return Instruction::FPExt;
746 case bitc::CAST_PTRTOINT: return Instruction::PtrToInt;
747 case bitc::CAST_INTTOPTR: return Instruction::IntToPtr;
748 case bitc::CAST_BITCAST : return Instruction::BitCast;
749 case bitc::CAST_ADDRSPACECAST: return Instruction::AddrSpaceCast;
753 static int getDecodedBinaryOpcode(unsigned Val, Type *Ty) {
754 bool IsFP = Ty->isFPOrFPVectorTy();
755 // BinOps are only valid for int/fp or vector of int/fp types
756 if (!IsFP && !Ty->isIntOrIntVectorTy())
762 case bitc::BINOP_ADD:
763 return IsFP ? Instruction::FAdd : Instruction::Add;
764 case bitc::BINOP_SUB:
765 return IsFP ? Instruction::FSub : Instruction::Sub;
766 case bitc::BINOP_MUL:
767 return IsFP ? Instruction::FMul : Instruction::Mul;
768 case bitc::BINOP_UDIV:
769 return IsFP ? -1 : Instruction::UDiv;
770 case bitc::BINOP_SDIV:
771 return IsFP ? Instruction::FDiv : Instruction::SDiv;
772 case bitc::BINOP_UREM:
773 return IsFP ? -1 : Instruction::URem;
774 case bitc::BINOP_SREM:
775 return IsFP ? Instruction::FRem : Instruction::SRem;
776 case bitc::BINOP_SHL:
777 return IsFP ? -1 : Instruction::Shl;
778 case bitc::BINOP_LSHR:
779 return IsFP ? -1 : Instruction::LShr;
780 case bitc::BINOP_ASHR:
781 return IsFP ? -1 : Instruction::AShr;
782 case bitc::BINOP_AND:
783 return IsFP ? -1 : Instruction::And;
785 return IsFP ? -1 : Instruction::Or;
786 case bitc::BINOP_XOR:
787 return IsFP ? -1 : Instruction::Xor;
791 static AtomicRMWInst::BinOp getDecodedRMWOperation(unsigned Val) {
793 default: return AtomicRMWInst::BAD_BINOP;
794 case bitc::RMW_XCHG: return AtomicRMWInst::Xchg;
795 case bitc::RMW_ADD: return AtomicRMWInst::Add;
796 case bitc::RMW_SUB: return AtomicRMWInst::Sub;
797 case bitc::RMW_AND: return AtomicRMWInst::And;
798 case bitc::RMW_NAND: return AtomicRMWInst::Nand;
799 case bitc::RMW_OR: return AtomicRMWInst::Or;
800 case bitc::RMW_XOR: return AtomicRMWInst::Xor;
801 case bitc::RMW_MAX: return AtomicRMWInst::Max;
802 case bitc::RMW_MIN: return AtomicRMWInst::Min;
803 case bitc::RMW_UMAX: return AtomicRMWInst::UMax;
804 case bitc::RMW_UMIN: return AtomicRMWInst::UMin;
808 static AtomicOrdering getDecodedOrdering(unsigned Val) {
810 case bitc::ORDERING_NOTATOMIC: return NotAtomic;
811 case bitc::ORDERING_UNORDERED: return Unordered;
812 case bitc::ORDERING_MONOTONIC: return Monotonic;
813 case bitc::ORDERING_ACQUIRE: return Acquire;
814 case bitc::ORDERING_RELEASE: return Release;
815 case bitc::ORDERING_ACQREL: return AcquireRelease;
816 default: // Map unknown orderings to sequentially-consistent.
817 case bitc::ORDERING_SEQCST: return SequentiallyConsistent;
821 static SynchronizationScope getDecodedSynchScope(unsigned Val) {
823 case bitc::SYNCHSCOPE_SINGLETHREAD: return SingleThread;
824 default: // Map unknown scopes to cross-thread.
825 case bitc::SYNCHSCOPE_CROSSTHREAD: return CrossThread;
829 static Comdat::SelectionKind getDecodedComdatSelectionKind(unsigned Val) {
831 default: // Map unknown selection kinds to any.
832 case bitc::COMDAT_SELECTION_KIND_ANY:
834 case bitc::COMDAT_SELECTION_KIND_EXACT_MATCH:
835 return Comdat::ExactMatch;
836 case bitc::COMDAT_SELECTION_KIND_LARGEST:
837 return Comdat::Largest;
838 case bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES:
839 return Comdat::NoDuplicates;
840 case bitc::COMDAT_SELECTION_KIND_SAME_SIZE:
841 return Comdat::SameSize;
845 static FastMathFlags getDecodedFastMathFlags(unsigned Val) {
847 if (0 != (Val & FastMathFlags::UnsafeAlgebra))
848 FMF.setUnsafeAlgebra();
849 if (0 != (Val & FastMathFlags::NoNaNs))
851 if (0 != (Val & FastMathFlags::NoInfs))
853 if (0 != (Val & FastMathFlags::NoSignedZeros))
854 FMF.setNoSignedZeros();
855 if (0 != (Val & FastMathFlags::AllowReciprocal))
856 FMF.setAllowReciprocal();
860 static void upgradeDLLImportExportLinkage(llvm::GlobalValue *GV, unsigned Val) {
862 case 5: GV->setDLLStorageClass(GlobalValue::DLLImportStorageClass); break;
863 case 6: GV->setDLLStorageClass(GlobalValue::DLLExportStorageClass); break;
869 /// \brief A class for maintaining the slot number definition
870 /// as a placeholder for the actual definition for forward constants defs.
871 class ConstantPlaceHolder : public ConstantExpr {
872 void operator=(const ConstantPlaceHolder &) = delete;
875 // allocate space for exactly one operand
876 void *operator new(size_t s) { return User::operator new(s, 1); }
877 explicit ConstantPlaceHolder(Type *Ty, LLVMContext &Context)
878 : ConstantExpr(Ty, Instruction::UserOp1, &Op<0>(), 1) {
879 Op<0>() = UndefValue::get(Type::getInt32Ty(Context));
882 /// \brief Methods to support type inquiry through isa, cast, and dyn_cast.
883 static bool classof(const Value *V) {
884 return isa<ConstantExpr>(V) &&
885 cast<ConstantExpr>(V)->getOpcode() == Instruction::UserOp1;
888 /// Provide fast operand accessors
889 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
893 // FIXME: can we inherit this from ConstantExpr?
895 struct OperandTraits<ConstantPlaceHolder> :
896 public FixedNumOperandTraits<ConstantPlaceHolder, 1> {
898 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantPlaceHolder, Value)
901 bool BitcodeReaderValueList::assignValue(Value *V, unsigned Idx) {
910 WeakVH &OldV = ValuePtrs[Idx];
916 // Handle constants and non-constants (e.g. instrs) differently for
918 if (Constant *PHC = dyn_cast<Constant>(&*OldV)) {
919 ResolveConstants.push_back(std::make_pair(PHC, Idx));
922 // If there was a forward reference to this value, replace it.
923 Value *PrevVal = OldV;
924 // Check operator constraints. We only put cleanuppads or catchpads in
925 // the forward value map if the value is constrained to match.
926 if (CatchPadInst *CatchPad = dyn_cast<CatchPadInst>(PrevVal)) {
927 if (!isa<CatchPadInst>(V))
929 // Delete the dummy basic block that was created with the sentinel
931 BasicBlock *DummyBlock = CatchPad->getUnwindDest();
932 assert(DummyBlock == CatchPad->getNormalDest());
933 CatchPad->dropAllReferences();
935 } else if (isa<CleanupPadInst>(PrevVal)) {
936 if (!isa<CleanupPadInst>(V))
939 OldV->replaceAllUsesWith(V);
947 Constant *BitcodeReaderValueList::getConstantFwdRef(unsigned Idx,
952 if (Value *V = ValuePtrs[Idx]) {
953 if (Ty != V->getType())
954 report_fatal_error("Type mismatch in constant table!");
955 return cast<Constant>(V);
958 // Create and return a placeholder, which will later be RAUW'd.
959 Constant *C = new ConstantPlaceHolder(Ty, Context);
964 Value *BitcodeReaderValueList::getValueFwdRef(unsigned Idx, Type *Ty,
965 OperatorConstraint OC) {
966 // Bail out for a clearly invalid value. This would make us call resize(0)
973 if (Value *V = ValuePtrs[Idx]) {
974 // If the types don't match, it's invalid.
975 if (Ty && Ty != V->getType())
979 // Use dyn_cast to enforce operator constraints
982 return dyn_cast<CatchPadInst>(V);
984 return dyn_cast<CleanupPadInst>(V);
986 llvm_unreachable("Unexpected operator constraint");
990 // No type specified, must be invalid reference.
991 if (!Ty) return nullptr;
993 // Create and return a placeholder, which will later be RAUW'd.
997 V = new Argument(Ty);
1000 BasicBlock *BB = BasicBlock::Create(Context);
1001 V = CatchPadInst::Create(BB, BB, {});
1005 assert(OC == OC_CleanupPad && "unexpected operator constraint");
1006 V = CleanupPadInst::Create(Context, {});
1014 /// Once all constants are read, this method bulk resolves any forward
1015 /// references. The idea behind this is that we sometimes get constants (such
1016 /// as large arrays) which reference *many* forward ref constants. Replacing
1017 /// each of these causes a lot of thrashing when building/reuniquing the
1018 /// constant. Instead of doing this, we look at all the uses and rewrite all
1019 /// the place holders at once for any constant that uses a placeholder.
1020 void BitcodeReaderValueList::resolveConstantForwardRefs() {
1021 // Sort the values by-pointer so that they are efficient to look up with a
1023 std::sort(ResolveConstants.begin(), ResolveConstants.end());
1025 SmallVector<Constant*, 64> NewOps;
1027 while (!ResolveConstants.empty()) {
1028 Value *RealVal = operator[](ResolveConstants.back().second);
1029 Constant *Placeholder = ResolveConstants.back().first;
1030 ResolveConstants.pop_back();
1032 // Loop over all users of the placeholder, updating them to reference the
1033 // new value. If they reference more than one placeholder, update them all
1035 while (!Placeholder->use_empty()) {
1036 auto UI = Placeholder->user_begin();
1039 // If the using object isn't uniqued, just update the operands. This
1040 // handles instructions and initializers for global variables.
1041 if (!isa<Constant>(U) || isa<GlobalValue>(U)) {
1042 UI.getUse().set(RealVal);
1046 // Otherwise, we have a constant that uses the placeholder. Replace that
1047 // constant with a new constant that has *all* placeholder uses updated.
1048 Constant *UserC = cast<Constant>(U);
1049 for (User::op_iterator I = UserC->op_begin(), E = UserC->op_end();
1052 if (!isa<ConstantPlaceHolder>(*I)) {
1053 // Not a placeholder reference.
1055 } else if (*I == Placeholder) {
1056 // Common case is that it just references this one placeholder.
1059 // Otherwise, look up the placeholder in ResolveConstants.
1060 ResolveConstantsTy::iterator It =
1061 std::lower_bound(ResolveConstants.begin(), ResolveConstants.end(),
1062 std::pair<Constant*, unsigned>(cast<Constant>(*I),
1064 assert(It != ResolveConstants.end() && It->first == *I);
1065 NewOp = operator[](It->second);
1068 NewOps.push_back(cast<Constant>(NewOp));
1071 // Make the new constant.
1073 if (ConstantArray *UserCA = dyn_cast<ConstantArray>(UserC)) {
1074 NewC = ConstantArray::get(UserCA->getType(), NewOps);
1075 } else if (ConstantStruct *UserCS = dyn_cast<ConstantStruct>(UserC)) {
1076 NewC = ConstantStruct::get(UserCS->getType(), NewOps);
1077 } else if (isa<ConstantVector>(UserC)) {
1078 NewC = ConstantVector::get(NewOps);
1080 assert(isa<ConstantExpr>(UserC) && "Must be a ConstantExpr.");
1081 NewC = cast<ConstantExpr>(UserC)->getWithOperands(NewOps);
1084 UserC->replaceAllUsesWith(NewC);
1085 UserC->destroyConstant();
1089 // Update all ValueHandles, they should be the only users at this point.
1090 Placeholder->replaceAllUsesWith(RealVal);
1095 void BitcodeReaderMDValueList::assignValue(Metadata *MD, unsigned Idx) {
1096 if (Idx == size()) {
1104 TrackingMDRef &OldMD = MDValuePtrs[Idx];
1110 // If there was a forward reference to this value, replace it.
1111 TempMDTuple PrevMD(cast<MDTuple>(OldMD.get()));
1112 PrevMD->replaceAllUsesWith(MD);
1116 Metadata *BitcodeReaderMDValueList::getValueFwdRef(unsigned Idx) {
1120 if (Metadata *MD = MDValuePtrs[Idx])
1123 // Track forward refs to be resolved later.
1125 MinFwdRef = std::min(MinFwdRef, Idx);
1126 MaxFwdRef = std::max(MaxFwdRef, Idx);
1129 MinFwdRef = MaxFwdRef = Idx;
1133 // Create and return a placeholder, which will later be RAUW'd.
1134 Metadata *MD = MDNode::getTemporary(Context, None).release();
1135 MDValuePtrs[Idx].reset(MD);
1139 void BitcodeReaderMDValueList::tryToResolveCycles() {
1145 // Still forward references... can't resolve cycles.
1148 // Resolve any cycles.
1149 for (unsigned I = MinFwdRef, E = MaxFwdRef + 1; I != E; ++I) {
1150 auto &MD = MDValuePtrs[I];
1151 auto *N = dyn_cast_or_null<MDNode>(MD);
1155 assert(!N->isTemporary() && "Unexpected forward reference");
1159 // Make sure we return early again until there's another forward ref.
1163 Type *BitcodeReader::getTypeByID(unsigned ID) {
1164 // The type table size is always specified correctly.
1165 if (ID >= TypeList.size())
1168 if (Type *Ty = TypeList[ID])
1171 // If we have a forward reference, the only possible case is when it is to a
1172 // named struct. Just create a placeholder for now.
1173 return TypeList[ID] = createIdentifiedStructType(Context);
1176 StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context,
1178 auto *Ret = StructType::create(Context, Name);
1179 IdentifiedStructTypes.push_back(Ret);
1183 StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context) {
1184 auto *Ret = StructType::create(Context);
1185 IdentifiedStructTypes.push_back(Ret);
1190 //===----------------------------------------------------------------------===//
1191 // Functions for parsing blocks from the bitcode file
1192 //===----------------------------------------------------------------------===//
1195 /// \brief This fills an AttrBuilder object with the LLVM attributes that have
1196 /// been decoded from the given integer. This function must stay in sync with
1197 /// 'encodeLLVMAttributesForBitcode'.
1198 static void decodeLLVMAttributesForBitcode(AttrBuilder &B,
1199 uint64_t EncodedAttrs) {
1200 // FIXME: Remove in 4.0.
1202 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
1203 // the bits above 31 down by 11 bits.
1204 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
1205 assert((!Alignment || isPowerOf2_32(Alignment)) &&
1206 "Alignment must be a power of two.");
1209 B.addAlignmentAttr(Alignment);
1210 B.addRawValue(((EncodedAttrs & (0xfffffULL << 32)) >> 11) |
1211 (EncodedAttrs & 0xffff));
1214 std::error_code BitcodeReader::parseAttributeBlock() {
1215 if (Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID))
1216 return error("Invalid record");
1218 if (!MAttributes.empty())
1219 return error("Invalid multiple blocks");
1221 SmallVector<uint64_t, 64> Record;
1223 SmallVector<AttributeSet, 8> Attrs;
1225 // Read all the records.
1227 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
1229 switch (Entry.Kind) {
1230 case BitstreamEntry::SubBlock: // Handled for us already.
1231 case BitstreamEntry::Error:
1232 return error("Malformed block");
1233 case BitstreamEntry::EndBlock:
1234 return std::error_code();
1235 case BitstreamEntry::Record:
1236 // The interesting case.
1242 switch (Stream.readRecord(Entry.ID, Record)) {
1243 default: // Default behavior: ignore.
1245 case bitc::PARAMATTR_CODE_ENTRY_OLD: { // ENTRY: [paramidx0, attr0, ...]
1246 // FIXME: Remove in 4.0.
1247 if (Record.size() & 1)
1248 return error("Invalid record");
1250 for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
1252 decodeLLVMAttributesForBitcode(B, Record[i+1]);
1253 Attrs.push_back(AttributeSet::get(Context, Record[i], B));
1256 MAttributes.push_back(AttributeSet::get(Context, Attrs));
1260 case bitc::PARAMATTR_CODE_ENTRY: { // ENTRY: [attrgrp0, attrgrp1, ...]
1261 for (unsigned i = 0, e = Record.size(); i != e; ++i)
1262 Attrs.push_back(MAttributeGroups[Record[i]]);
1264 MAttributes.push_back(AttributeSet::get(Context, Attrs));
1272 // Returns Attribute::None on unrecognized codes.
1273 static Attribute::AttrKind getAttrFromCode(uint64_t Code) {
1276 return Attribute::None;
1277 case bitc::ATTR_KIND_ALIGNMENT:
1278 return Attribute::Alignment;
1279 case bitc::ATTR_KIND_ALWAYS_INLINE:
1280 return Attribute::AlwaysInline;
1281 case bitc::ATTR_KIND_ARGMEMONLY:
1282 return Attribute::ArgMemOnly;
1283 case bitc::ATTR_KIND_BUILTIN:
1284 return Attribute::Builtin;
1285 case bitc::ATTR_KIND_BY_VAL:
1286 return Attribute::ByVal;
1287 case bitc::ATTR_KIND_IN_ALLOCA:
1288 return Attribute::InAlloca;
1289 case bitc::ATTR_KIND_COLD:
1290 return Attribute::Cold;
1291 case bitc::ATTR_KIND_CONVERGENT:
1292 return Attribute::Convergent;
1293 case bitc::ATTR_KIND_INLINE_HINT:
1294 return Attribute::InlineHint;
1295 case bitc::ATTR_KIND_IN_REG:
1296 return Attribute::InReg;
1297 case bitc::ATTR_KIND_JUMP_TABLE:
1298 return Attribute::JumpTable;
1299 case bitc::ATTR_KIND_MIN_SIZE:
1300 return Attribute::MinSize;
1301 case bitc::ATTR_KIND_NAKED:
1302 return Attribute::Naked;
1303 case bitc::ATTR_KIND_NEST:
1304 return Attribute::Nest;
1305 case bitc::ATTR_KIND_NO_ALIAS:
1306 return Attribute::NoAlias;
1307 case bitc::ATTR_KIND_NO_BUILTIN:
1308 return Attribute::NoBuiltin;
1309 case bitc::ATTR_KIND_NO_CAPTURE:
1310 return Attribute::NoCapture;
1311 case bitc::ATTR_KIND_NO_DUPLICATE:
1312 return Attribute::NoDuplicate;
1313 case bitc::ATTR_KIND_NO_IMPLICIT_FLOAT:
1314 return Attribute::NoImplicitFloat;
1315 case bitc::ATTR_KIND_NO_INLINE:
1316 return Attribute::NoInline;
1317 case bitc::ATTR_KIND_NO_RECURSE:
1318 return Attribute::NoRecurse;
1319 case bitc::ATTR_KIND_NON_LAZY_BIND:
1320 return Attribute::NonLazyBind;
1321 case bitc::ATTR_KIND_NON_NULL:
1322 return Attribute::NonNull;
1323 case bitc::ATTR_KIND_DEREFERENCEABLE:
1324 return Attribute::Dereferenceable;
1325 case bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL:
1326 return Attribute::DereferenceableOrNull;
1327 case bitc::ATTR_KIND_NO_RED_ZONE:
1328 return Attribute::NoRedZone;
1329 case bitc::ATTR_KIND_NO_RETURN:
1330 return Attribute::NoReturn;
1331 case bitc::ATTR_KIND_NO_UNWIND:
1332 return Attribute::NoUnwind;
1333 case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE:
1334 return Attribute::OptimizeForSize;
1335 case bitc::ATTR_KIND_OPTIMIZE_NONE:
1336 return Attribute::OptimizeNone;
1337 case bitc::ATTR_KIND_READ_NONE:
1338 return Attribute::ReadNone;
1339 case bitc::ATTR_KIND_READ_ONLY:
1340 return Attribute::ReadOnly;
1341 case bitc::ATTR_KIND_RETURNED:
1342 return Attribute::Returned;
1343 case bitc::ATTR_KIND_RETURNS_TWICE:
1344 return Attribute::ReturnsTwice;
1345 case bitc::ATTR_KIND_S_EXT:
1346 return Attribute::SExt;
1347 case bitc::ATTR_KIND_STACK_ALIGNMENT:
1348 return Attribute::StackAlignment;
1349 case bitc::ATTR_KIND_STACK_PROTECT:
1350 return Attribute::StackProtect;
1351 case bitc::ATTR_KIND_STACK_PROTECT_REQ:
1352 return Attribute::StackProtectReq;
1353 case bitc::ATTR_KIND_STACK_PROTECT_STRONG:
1354 return Attribute::StackProtectStrong;
1355 case bitc::ATTR_KIND_SAFESTACK:
1356 return Attribute::SafeStack;
1357 case bitc::ATTR_KIND_STRUCT_RET:
1358 return Attribute::StructRet;
1359 case bitc::ATTR_KIND_SANITIZE_ADDRESS:
1360 return Attribute::SanitizeAddress;
1361 case bitc::ATTR_KIND_SANITIZE_THREAD:
1362 return Attribute::SanitizeThread;
1363 case bitc::ATTR_KIND_SANITIZE_MEMORY:
1364 return Attribute::SanitizeMemory;
1365 case bitc::ATTR_KIND_UW_TABLE:
1366 return Attribute::UWTable;
1367 case bitc::ATTR_KIND_Z_EXT:
1368 return Attribute::ZExt;
1372 std::error_code BitcodeReader::parseAlignmentValue(uint64_t Exponent,
1373 unsigned &Alignment) {
1374 // Note: Alignment in bitcode files is incremented by 1, so that zero
1375 // can be used for default alignment.
1376 if (Exponent > Value::MaxAlignmentExponent + 1)
1377 return error("Invalid alignment value");
1378 Alignment = (1 << static_cast<unsigned>(Exponent)) >> 1;
1379 return std::error_code();
1382 std::error_code BitcodeReader::parseAttrKind(uint64_t Code,
1383 Attribute::AttrKind *Kind) {
1384 *Kind = getAttrFromCode(Code);
1385 if (*Kind == Attribute::None)
1386 return error(BitcodeError::CorruptedBitcode,
1387 "Unknown attribute kind (" + Twine(Code) + ")");
1388 return std::error_code();
1391 std::error_code BitcodeReader::parseAttributeGroupBlock() {
1392 if (Stream.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID))
1393 return error("Invalid record");
1395 if (!MAttributeGroups.empty())
1396 return error("Invalid multiple blocks");
1398 SmallVector<uint64_t, 64> Record;
1400 // Read all the records.
1402 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
1404 switch (Entry.Kind) {
1405 case BitstreamEntry::SubBlock: // Handled for us already.
1406 case BitstreamEntry::Error:
1407 return error("Malformed block");
1408 case BitstreamEntry::EndBlock:
1409 return std::error_code();
1410 case BitstreamEntry::Record:
1411 // The interesting case.
1417 switch (Stream.readRecord(Entry.ID, Record)) {
1418 default: // Default behavior: ignore.
1420 case bitc::PARAMATTR_GRP_CODE_ENTRY: { // ENTRY: [grpid, idx, a0, a1, ...]
1421 if (Record.size() < 3)
1422 return error("Invalid record");
1424 uint64_t GrpID = Record[0];
1425 uint64_t Idx = Record[1]; // Index of the object this attribute refers to.
1428 for (unsigned i = 2, e = Record.size(); i != e; ++i) {
1429 if (Record[i] == 0) { // Enum attribute
1430 Attribute::AttrKind Kind;
1431 if (std::error_code EC = parseAttrKind(Record[++i], &Kind))
1434 B.addAttribute(Kind);
1435 } else if (Record[i] == 1) { // Integer attribute
1436 Attribute::AttrKind Kind;
1437 if (std::error_code EC = parseAttrKind(Record[++i], &Kind))
1439 if (Kind == Attribute::Alignment)
1440 B.addAlignmentAttr(Record[++i]);
1441 else if (Kind == Attribute::StackAlignment)
1442 B.addStackAlignmentAttr(Record[++i]);
1443 else if (Kind == Attribute::Dereferenceable)
1444 B.addDereferenceableAttr(Record[++i]);
1445 else if (Kind == Attribute::DereferenceableOrNull)
1446 B.addDereferenceableOrNullAttr(Record[++i]);
1447 } else { // String attribute
1448 assert((Record[i] == 3 || Record[i] == 4) &&
1449 "Invalid attribute group entry");
1450 bool HasValue = (Record[i++] == 4);
1451 SmallString<64> KindStr;
1452 SmallString<64> ValStr;
1454 while (Record[i] != 0 && i != e)
1455 KindStr += Record[i++];
1456 assert(Record[i] == 0 && "Kind string not null terminated");
1459 // Has a value associated with it.
1460 ++i; // Skip the '0' that terminates the "kind" string.
1461 while (Record[i] != 0 && i != e)
1462 ValStr += Record[i++];
1463 assert(Record[i] == 0 && "Value string not null terminated");
1466 B.addAttribute(KindStr.str(), ValStr.str());
1470 MAttributeGroups[GrpID] = AttributeSet::get(Context, Idx, B);
1477 std::error_code BitcodeReader::parseTypeTable() {
1478 if (Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW))
1479 return error("Invalid record");
1481 return parseTypeTableBody();
1484 std::error_code BitcodeReader::parseTypeTableBody() {
1485 if (!TypeList.empty())
1486 return error("Invalid multiple blocks");
1488 SmallVector<uint64_t, 64> Record;
1489 unsigned NumRecords = 0;
1491 SmallString<64> TypeName;
1493 // Read all the records for this type table.
1495 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
1497 switch (Entry.Kind) {
1498 case BitstreamEntry::SubBlock: // Handled for us already.
1499 case BitstreamEntry::Error:
1500 return error("Malformed block");
1501 case BitstreamEntry::EndBlock:
1502 if (NumRecords != TypeList.size())
1503 return error("Malformed block");
1504 return std::error_code();
1505 case BitstreamEntry::Record:
1506 // The interesting case.
1512 Type *ResultTy = nullptr;
1513 switch (Stream.readRecord(Entry.ID, Record)) {
1515 return error("Invalid value");
1516 case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries]
1517 // TYPE_CODE_NUMENTRY contains a count of the number of types in the
1518 // type list. This allows us to reserve space.
1519 if (Record.size() < 1)
1520 return error("Invalid record");
1521 TypeList.resize(Record[0]);
1523 case bitc::TYPE_CODE_VOID: // VOID
1524 ResultTy = Type::getVoidTy(Context);
1526 case bitc::TYPE_CODE_HALF: // HALF
1527 ResultTy = Type::getHalfTy(Context);
1529 case bitc::TYPE_CODE_FLOAT: // FLOAT
1530 ResultTy = Type::getFloatTy(Context);
1532 case bitc::TYPE_CODE_DOUBLE: // DOUBLE
1533 ResultTy = Type::getDoubleTy(Context);
1535 case bitc::TYPE_CODE_X86_FP80: // X86_FP80
1536 ResultTy = Type::getX86_FP80Ty(Context);
1538 case bitc::TYPE_CODE_FP128: // FP128
1539 ResultTy = Type::getFP128Ty(Context);
1541 case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128
1542 ResultTy = Type::getPPC_FP128Ty(Context);
1544 case bitc::TYPE_CODE_LABEL: // LABEL
1545 ResultTy = Type::getLabelTy(Context);
1547 case bitc::TYPE_CODE_METADATA: // METADATA
1548 ResultTy = Type::getMetadataTy(Context);
1550 case bitc::TYPE_CODE_X86_MMX: // X86_MMX
1551 ResultTy = Type::getX86_MMXTy(Context);
1553 case bitc::TYPE_CODE_TOKEN: // TOKEN
1554 ResultTy = Type::getTokenTy(Context);
1556 case bitc::TYPE_CODE_INTEGER: { // INTEGER: [width]
1557 if (Record.size() < 1)
1558 return error("Invalid record");
1560 uint64_t NumBits = Record[0];
1561 if (NumBits < IntegerType::MIN_INT_BITS ||
1562 NumBits > IntegerType::MAX_INT_BITS)
1563 return error("Bitwidth for integer type out of range");
1564 ResultTy = IntegerType::get(Context, NumBits);
1567 case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or
1568 // [pointee type, address space]
1569 if (Record.size() < 1)
1570 return error("Invalid record");
1571 unsigned AddressSpace = 0;
1572 if (Record.size() == 2)
1573 AddressSpace = Record[1];
1574 ResultTy = getTypeByID(Record[0]);
1576 !PointerType::isValidElementType(ResultTy))
1577 return error("Invalid type");
1578 ResultTy = PointerType::get(ResultTy, AddressSpace);
1581 case bitc::TYPE_CODE_FUNCTION_OLD: {
1582 // FIXME: attrid is dead, remove it in LLVM 4.0
1583 // FUNCTION: [vararg, attrid, retty, paramty x N]
1584 if (Record.size() < 3)
1585 return error("Invalid record");
1586 SmallVector<Type*, 8> ArgTys;
1587 for (unsigned i = 3, e = Record.size(); i != e; ++i) {
1588 if (Type *T = getTypeByID(Record[i]))
1589 ArgTys.push_back(T);
1594 ResultTy = getTypeByID(Record[2]);
1595 if (!ResultTy || ArgTys.size() < Record.size()-3)
1596 return error("Invalid type");
1598 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
1601 case bitc::TYPE_CODE_FUNCTION: {
1602 // FUNCTION: [vararg, retty, paramty x N]
1603 if (Record.size() < 2)
1604 return error("Invalid record");
1605 SmallVector<Type*, 8> ArgTys;
1606 for (unsigned i = 2, e = Record.size(); i != e; ++i) {
1607 if (Type *T = getTypeByID(Record[i])) {
1608 if (!FunctionType::isValidArgumentType(T))
1609 return error("Invalid function argument type");
1610 ArgTys.push_back(T);
1616 ResultTy = getTypeByID(Record[1]);
1617 if (!ResultTy || ArgTys.size() < Record.size()-2)
1618 return error("Invalid type");
1620 ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
1623 case bitc::TYPE_CODE_STRUCT_ANON: { // STRUCT: [ispacked, eltty x N]
1624 if (Record.size() < 1)
1625 return error("Invalid record");
1626 SmallVector<Type*, 8> EltTys;
1627 for (unsigned i = 1, e = Record.size(); i != e; ++i) {
1628 if (Type *T = getTypeByID(Record[i]))
1629 EltTys.push_back(T);
1633 if (EltTys.size() != Record.size()-1)
1634 return error("Invalid type");
1635 ResultTy = StructType::get(Context, EltTys, Record[0]);
1638 case bitc::TYPE_CODE_STRUCT_NAME: // STRUCT_NAME: [strchr x N]
1639 if (convertToString(Record, 0, TypeName))
1640 return error("Invalid record");
1643 case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N]
1644 if (Record.size() < 1)
1645 return error("Invalid record");
1647 if (NumRecords >= TypeList.size())
1648 return error("Invalid TYPE table");
1650 // Check to see if this was forward referenced, if so fill in the temp.
1651 StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
1653 Res->setName(TypeName);
1654 TypeList[NumRecords] = nullptr;
1655 } else // Otherwise, create a new struct.
1656 Res = createIdentifiedStructType(Context, TypeName);
1659 SmallVector<Type*, 8> EltTys;
1660 for (unsigned i = 1, e = Record.size(); i != e; ++i) {
1661 if (Type *T = getTypeByID(Record[i]))
1662 EltTys.push_back(T);
1666 if (EltTys.size() != Record.size()-1)
1667 return error("Invalid record");
1668 Res->setBody(EltTys, Record[0]);
1672 case bitc::TYPE_CODE_OPAQUE: { // OPAQUE: []
1673 if (Record.size() != 1)
1674 return error("Invalid record");
1676 if (NumRecords >= TypeList.size())
1677 return error("Invalid TYPE table");
1679 // Check to see if this was forward referenced, if so fill in the temp.
1680 StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
1682 Res->setName(TypeName);
1683 TypeList[NumRecords] = nullptr;
1684 } else // Otherwise, create a new struct with no body.
1685 Res = createIdentifiedStructType(Context, TypeName);
1690 case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty]
1691 if (Record.size() < 2)
1692 return error("Invalid record");
1693 ResultTy = getTypeByID(Record[1]);
1694 if (!ResultTy || !ArrayType::isValidElementType(ResultTy))
1695 return error("Invalid type");
1696 ResultTy = ArrayType::get(ResultTy, Record[0]);
1698 case bitc::TYPE_CODE_VECTOR: // VECTOR: [numelts, eltty]
1699 if (Record.size() < 2)
1700 return error("Invalid record");
1702 return error("Invalid vector length");
1703 ResultTy = getTypeByID(Record[1]);
1704 if (!ResultTy || !StructType::isValidElementType(ResultTy))
1705 return error("Invalid type");
1706 ResultTy = VectorType::get(ResultTy, Record[0]);
1710 if (NumRecords >= TypeList.size())
1711 return error("Invalid TYPE table");
1712 if (TypeList[NumRecords])
1714 "Invalid TYPE table: Only named structs can be forward referenced");
1715 assert(ResultTy && "Didn't read a type?");
1716 TypeList[NumRecords++] = ResultTy;
1720 std::error_code BitcodeReader::parseOperandBundleTags() {
1721 if (Stream.EnterSubBlock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID))
1722 return error("Invalid record");
1724 if (!BundleTags.empty())
1725 return error("Invalid multiple blocks");
1727 SmallVector<uint64_t, 64> Record;
1730 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
1732 switch (Entry.Kind) {
1733 case BitstreamEntry::SubBlock: // Handled for us already.
1734 case BitstreamEntry::Error:
1735 return error("Malformed block");
1736 case BitstreamEntry::EndBlock:
1737 return std::error_code();
1738 case BitstreamEntry::Record:
1739 // The interesting case.
1743 // Tags are implicitly mapped to integers by their order.
1745 if (Stream.readRecord(Entry.ID, Record) != bitc::OPERAND_BUNDLE_TAG)
1746 return error("Invalid record");
1748 // OPERAND_BUNDLE_TAG: [strchr x N]
1749 BundleTags.emplace_back();
1750 if (convertToString(Record, 0, BundleTags.back()))
1751 return error("Invalid record");
1756 /// Associate a value with its name from the given index in the provided record.
1757 ErrorOr<Value *> BitcodeReader::recordValue(SmallVectorImpl<uint64_t> &Record,
1758 unsigned NameIndex, Triple &TT) {
1759 SmallString<128> ValueName;
1760 if (convertToString(Record, NameIndex, ValueName))
1761 return error("Invalid record");
1762 unsigned ValueID = Record[0];
1763 if (ValueID >= ValueList.size() || !ValueList[ValueID])
1764 return error("Invalid record");
1765 Value *V = ValueList[ValueID];
1767 StringRef NameStr(ValueName.data(), ValueName.size());
1768 if (NameStr.find_first_of(0) != StringRef::npos)
1769 return error("Invalid value name");
1770 V->setName(NameStr);
1771 auto *GO = dyn_cast<GlobalObject>(V);
1773 if (GO->getComdat() == reinterpret_cast<Comdat *>(1)) {
1774 if (TT.isOSBinFormatMachO())
1775 GO->setComdat(nullptr);
1777 GO->setComdat(TheModule->getOrInsertComdat(V->getName()));
1783 /// Parse the value symbol table at either the current parsing location or
1784 /// at the given bit offset if provided.
1785 std::error_code BitcodeReader::parseValueSymbolTable(uint64_t Offset) {
1786 uint64_t CurrentBit;
1787 // Pass in the Offset to distinguish between calling for the module-level
1788 // VST (where we want to jump to the VST offset) and the function-level
1789 // VST (where we don't).
1791 // Save the current parsing location so we can jump back at the end
1793 CurrentBit = Stream.GetCurrentBitNo();
1794 Stream.JumpToBit(Offset * 32);
1796 // Do some checking if we are in debug mode.
1797 BitstreamEntry Entry = Stream.advance();
1798 assert(Entry.Kind == BitstreamEntry::SubBlock);
1799 assert(Entry.ID == bitc::VALUE_SYMTAB_BLOCK_ID);
1801 // In NDEBUG mode ignore the output so we don't get an unused variable
1807 // Compute the delta between the bitcode indices in the VST (the word offset
1808 // to the word-aligned ENTER_SUBBLOCK for the function block, and that
1809 // expected by the lazy reader. The reader's EnterSubBlock expects to have
1810 // already read the ENTER_SUBBLOCK code (size getAbbrevIDWidth) and BlockID
1811 // (size BlockIDWidth). Note that we access the stream's AbbrevID width here
1812 // just before entering the VST subblock because: 1) the EnterSubBlock
1813 // changes the AbbrevID width; 2) the VST block is nested within the same
1814 // outer MODULE_BLOCK as the FUNCTION_BLOCKs and therefore have the same
1815 // AbbrevID width before calling EnterSubBlock; and 3) when we want to
1816 // jump to the FUNCTION_BLOCK using this offset later, we don't want
1817 // to rely on the stream's AbbrevID width being that of the MODULE_BLOCK.
1818 unsigned FuncBitcodeOffsetDelta =
1819 Stream.getAbbrevIDWidth() + bitc::BlockIDWidth;
1821 if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
1822 return error("Invalid record");
1824 SmallVector<uint64_t, 64> Record;
1826 Triple TT(TheModule->getTargetTriple());
1828 // Read all the records for this value table.
1829 SmallString<128> ValueName;
1831 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
1833 switch (Entry.Kind) {
1834 case BitstreamEntry::SubBlock: // Handled for us already.
1835 case BitstreamEntry::Error:
1836 return error("Malformed block");
1837 case BitstreamEntry::EndBlock:
1839 Stream.JumpToBit(CurrentBit);
1840 return std::error_code();
1841 case BitstreamEntry::Record:
1842 // The interesting case.
1848 switch (Stream.readRecord(Entry.ID, Record)) {
1849 default: // Default behavior: unknown type.
1851 case bitc::VST_CODE_ENTRY: { // VST_ENTRY: [valueid, namechar x N]
1852 ErrorOr<Value *> ValOrErr = recordValue(Record, 1, TT);
1853 if (std::error_code EC = ValOrErr.getError())
1858 case bitc::VST_CODE_FNENTRY: {
1859 // VST_FNENTRY: [valueid, offset, namechar x N]
1860 ErrorOr<Value *> ValOrErr = recordValue(Record, 2, TT);
1861 if (std::error_code EC = ValOrErr.getError())
1863 Value *V = ValOrErr.get();
1865 auto *GO = dyn_cast<GlobalObject>(V);
1867 // If this is an alias, need to get the actual Function object
1868 // it aliases, in order to set up the DeferredFunctionInfo entry below.
1869 auto *GA = dyn_cast<GlobalAlias>(V);
1871 GO = GA->getBaseObject();
1875 uint64_t FuncWordOffset = Record[1];
1876 Function *F = dyn_cast<Function>(GO);
1878 uint64_t FuncBitOffset = FuncWordOffset * 32;
1879 DeferredFunctionInfo[F] = FuncBitOffset + FuncBitcodeOffsetDelta;
1880 // Set the LastFunctionBlockBit to point to the last function block.
1881 // Later when parsing is resumed after function materialization,
1882 // we can simply skip that last function block.
1883 if (FuncBitOffset > LastFunctionBlockBit)
1884 LastFunctionBlockBit = FuncBitOffset;
1887 case bitc::VST_CODE_BBENTRY: {
1888 if (convertToString(Record, 1, ValueName))
1889 return error("Invalid record");
1890 BasicBlock *BB = getBasicBlock(Record[0]);
1892 return error("Invalid record");
1894 BB->setName(StringRef(ValueName.data(), ValueName.size()));
1902 /// Parse a single METADATA_KIND record, inserting result in MDKindMap.
1904 BitcodeReader::parseMetadataKindRecord(SmallVectorImpl<uint64_t> &Record) {
1905 if (Record.size() < 2)
1906 return error("Invalid record");
1908 unsigned Kind = Record[0];
1909 SmallString<8> Name(Record.begin() + 1, Record.end());
1911 unsigned NewKind = TheModule->getMDKindID(Name.str());
1912 if (!MDKindMap.insert(std::make_pair(Kind, NewKind)).second)
1913 return error("Conflicting METADATA_KIND records");
1914 return std::error_code();
1917 static int64_t unrotateSign(uint64_t U) { return U & 1 ? ~(U >> 1) : U >> 1; }
1919 /// Parse a METADATA_BLOCK. If ModuleLevel is true then we are parsing
1920 /// module level metadata.
1921 std::error_code BitcodeReader::parseMetadata(bool ModuleLevel) {
1922 IsMetadataMaterialized = true;
1923 unsigned NextMDValueNo = MDValueList.size();
1924 if (ModuleLevel && SeenModuleValuesRecord) {
1925 // Now that we are parsing the module level metadata, we want to restart
1926 // the numbering of the MD values, and replace temp MD created earlier
1927 // with their real values. If we saw a METADATA_VALUE record then we
1928 // would have set the MDValueList size to the number specified in that
1929 // record, to support parsing function-level metadata first, and we need
1930 // to reset back to 0 to fill the MDValueList in with the parsed module
1931 // The function-level metadata parsing should have reset the MDValueList
1932 // size back to the value reported by the METADATA_VALUE record, saved in
1934 assert(NumModuleMDs == MDValueList.size() &&
1935 "Expected MDValueList to only contain module level values");
1939 if (Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID))
1940 return error("Invalid record");
1942 SmallVector<uint64_t, 64> Record;
1945 [&](unsigned ID) -> Metadata *{ return MDValueList.getValueFwdRef(ID); };
1946 auto getMDOrNull = [&](unsigned ID) -> Metadata *{
1948 return getMD(ID - 1);
1951 auto getMDString = [&](unsigned ID) -> MDString *{
1952 // This requires that the ID is not really a forward reference. In
1953 // particular, the MDString must already have been resolved.
1954 return cast_or_null<MDString>(getMDOrNull(ID));
1957 #define GET_OR_DISTINCT(CLASS, DISTINCT, ARGS) \
1958 (DISTINCT ? CLASS::getDistinct ARGS : CLASS::get ARGS)
1960 // Read all the records.
1962 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
1964 switch (Entry.Kind) {
1965 case BitstreamEntry::SubBlock: // Handled for us already.
1966 case BitstreamEntry::Error:
1967 return error("Malformed block");
1968 case BitstreamEntry::EndBlock:
1969 MDValueList.tryToResolveCycles();
1970 assert((!(ModuleLevel && SeenModuleValuesRecord) ||
1971 NumModuleMDs == MDValueList.size()) &&
1972 "Inconsistent bitcode: METADATA_VALUES mismatch");
1973 return std::error_code();
1974 case BitstreamEntry::Record:
1975 // The interesting case.
1981 unsigned Code = Stream.readRecord(Entry.ID, Record);
1982 bool IsDistinct = false;
1984 default: // Default behavior: ignore.
1986 case bitc::METADATA_NAME: {
1987 // Read name of the named metadata.
1988 SmallString<8> Name(Record.begin(), Record.end());
1990 Code = Stream.ReadCode();
1992 unsigned NextBitCode = Stream.readRecord(Code, Record);
1993 if (NextBitCode != bitc::METADATA_NAMED_NODE)
1994 return error("METADATA_NAME not followed by METADATA_NAMED_NODE");
1996 // Read named metadata elements.
1997 unsigned Size = Record.size();
1998 NamedMDNode *NMD = TheModule->getOrInsertNamedMetadata(Name);
1999 for (unsigned i = 0; i != Size; ++i) {
2000 MDNode *MD = dyn_cast_or_null<MDNode>(MDValueList.getValueFwdRef(Record[i]));
2002 return error("Invalid record");
2003 NMD->addOperand(MD);
2007 case bitc::METADATA_OLD_FN_NODE: {
2008 // FIXME: Remove in 4.0.
2009 // This is a LocalAsMetadata record, the only type of function-local
2011 if (Record.size() % 2 == 1)
2012 return error("Invalid record");
2014 // If this isn't a LocalAsMetadata record, we're dropping it. This used
2015 // to be legal, but there's no upgrade path.
2016 auto dropRecord = [&] {
2017 MDValueList.assignValue(MDNode::get(Context, None), NextMDValueNo++);
2019 if (Record.size() != 2) {
2024 Type *Ty = getTypeByID(Record[0]);
2025 if (Ty->isMetadataTy() || Ty->isVoidTy()) {
2030 MDValueList.assignValue(
2031 LocalAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)),
2035 case bitc::METADATA_OLD_NODE: {
2036 // FIXME: Remove in 4.0.
2037 if (Record.size() % 2 == 1)
2038 return error("Invalid record");
2040 unsigned Size = Record.size();
2041 SmallVector<Metadata *, 8> Elts;
2042 for (unsigned i = 0; i != Size; i += 2) {
2043 Type *Ty = getTypeByID(Record[i]);
2045 return error("Invalid record");
2046 if (Ty->isMetadataTy())
2047 Elts.push_back(MDValueList.getValueFwdRef(Record[i+1]));
2048 else if (!Ty->isVoidTy()) {
2050 ValueAsMetadata::get(ValueList.getValueFwdRef(Record[i + 1], Ty));
2051 assert(isa<ConstantAsMetadata>(MD) &&
2052 "Expected non-function-local metadata");
2055 Elts.push_back(nullptr);
2057 MDValueList.assignValue(MDNode::get(Context, Elts), NextMDValueNo++);
2060 case bitc::METADATA_VALUE: {
2061 if (Record.size() != 2)
2062 return error("Invalid record");
2064 Type *Ty = getTypeByID(Record[0]);
2065 if (Ty->isMetadataTy() || Ty->isVoidTy())
2066 return error("Invalid record");
2068 MDValueList.assignValue(
2069 ValueAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)),
2073 case bitc::METADATA_DISTINCT_NODE:
2076 case bitc::METADATA_NODE: {
2077 SmallVector<Metadata *, 8> Elts;
2078 Elts.reserve(Record.size());
2079 for (unsigned ID : Record)
2080 Elts.push_back(ID ? MDValueList.getValueFwdRef(ID - 1) : nullptr);
2081 MDValueList.assignValue(IsDistinct ? MDNode::getDistinct(Context, Elts)
2082 : MDNode::get(Context, Elts),
2086 case bitc::METADATA_LOCATION: {
2087 if (Record.size() != 5)
2088 return error("Invalid record");
2090 unsigned Line = Record[1];
2091 unsigned Column = Record[2];
2092 MDNode *Scope = cast<MDNode>(MDValueList.getValueFwdRef(Record[3]));
2093 Metadata *InlinedAt =
2094 Record[4] ? MDValueList.getValueFwdRef(Record[4] - 1) : nullptr;
2095 MDValueList.assignValue(
2096 GET_OR_DISTINCT(DILocation, Record[0],
2097 (Context, Line, Column, Scope, InlinedAt)),
2101 case bitc::METADATA_GENERIC_DEBUG: {
2102 if (Record.size() < 4)
2103 return error("Invalid record");
2105 unsigned Tag = Record[1];
2106 unsigned Version = Record[2];
2108 if (Tag >= 1u << 16 || Version != 0)
2109 return error("Invalid record");
2111 auto *Header = getMDString(Record[3]);
2112 SmallVector<Metadata *, 8> DwarfOps;
2113 for (unsigned I = 4, E = Record.size(); I != E; ++I)
2114 DwarfOps.push_back(Record[I] ? MDValueList.getValueFwdRef(Record[I] - 1)
2116 MDValueList.assignValue(GET_OR_DISTINCT(GenericDINode, Record[0],
2117 (Context, Tag, Header, DwarfOps)),
2121 case bitc::METADATA_SUBRANGE: {
2122 if (Record.size() != 3)
2123 return error("Invalid record");
2125 MDValueList.assignValue(
2126 GET_OR_DISTINCT(DISubrange, Record[0],
2127 (Context, Record[1], unrotateSign(Record[2]))),
2131 case bitc::METADATA_ENUMERATOR: {
2132 if (Record.size() != 3)
2133 return error("Invalid record");
2135 MDValueList.assignValue(GET_OR_DISTINCT(DIEnumerator, Record[0],
2136 (Context, unrotateSign(Record[1]),
2137 getMDString(Record[2]))),
2141 case bitc::METADATA_BASIC_TYPE: {
2142 if (Record.size() != 6)
2143 return error("Invalid record");
2145 MDValueList.assignValue(
2146 GET_OR_DISTINCT(DIBasicType, Record[0],
2147 (Context, Record[1], getMDString(Record[2]),
2148 Record[3], Record[4], Record[5])),
2152 case bitc::METADATA_DERIVED_TYPE: {
2153 if (Record.size() != 12)
2154 return error("Invalid record");
2156 MDValueList.assignValue(
2157 GET_OR_DISTINCT(DIDerivedType, Record[0],
2158 (Context, Record[1], getMDString(Record[2]),
2159 getMDOrNull(Record[3]), Record[4],
2160 getMDOrNull(Record[5]), getMDOrNull(Record[6]),
2161 Record[7], Record[8], Record[9], Record[10],
2162 getMDOrNull(Record[11]))),
2166 case bitc::METADATA_COMPOSITE_TYPE: {
2167 if (Record.size() != 16)
2168 return error("Invalid record");
2170 MDValueList.assignValue(
2171 GET_OR_DISTINCT(DICompositeType, Record[0],
2172 (Context, Record[1], getMDString(Record[2]),
2173 getMDOrNull(Record[3]), Record[4],
2174 getMDOrNull(Record[5]), getMDOrNull(Record[6]),
2175 Record[7], Record[8], Record[9], Record[10],
2176 getMDOrNull(Record[11]), Record[12],
2177 getMDOrNull(Record[13]), getMDOrNull(Record[14]),
2178 getMDString(Record[15]))),
2182 case bitc::METADATA_SUBROUTINE_TYPE: {
2183 if (Record.size() != 3)
2184 return error("Invalid record");
2186 MDValueList.assignValue(
2187 GET_OR_DISTINCT(DISubroutineType, Record[0],
2188 (Context, Record[1], getMDOrNull(Record[2]))),
2193 case bitc::METADATA_MODULE: {
2194 if (Record.size() != 6)
2195 return error("Invalid record");
2197 MDValueList.assignValue(
2198 GET_OR_DISTINCT(DIModule, Record[0],
2199 (Context, getMDOrNull(Record[1]),
2200 getMDString(Record[2]), getMDString(Record[3]),
2201 getMDString(Record[4]), getMDString(Record[5]))),
2206 case bitc::METADATA_FILE: {
2207 if (Record.size() != 3)
2208 return error("Invalid record");
2210 MDValueList.assignValue(
2211 GET_OR_DISTINCT(DIFile, Record[0], (Context, getMDString(Record[1]),
2212 getMDString(Record[2]))),
2216 case bitc::METADATA_COMPILE_UNIT: {
2217 if (Record.size() < 14 || Record.size() > 16)
2218 return error("Invalid record");
2220 // Ignore Record[0], which indicates whether this compile unit is
2221 // distinct. It's always distinct.
2222 MDValueList.assignValue(
2223 DICompileUnit::getDistinct(
2224 Context, Record[1], getMDOrNull(Record[2]),
2225 getMDString(Record[3]), Record[4], getMDString(Record[5]),
2226 Record[6], getMDString(Record[7]), Record[8],
2227 getMDOrNull(Record[9]), getMDOrNull(Record[10]),
2228 getMDOrNull(Record[11]), getMDOrNull(Record[12]),
2229 getMDOrNull(Record[13]),
2230 Record.size() <= 15 ? 0 : getMDOrNull(Record[15]),
2231 Record.size() <= 14 ? 0 : Record[14]),
2235 case bitc::METADATA_SUBPROGRAM: {
2236 if (Record.size() != 18 && Record.size() != 19)
2237 return error("Invalid record");
2239 bool HasFn = Record.size() == 19;
2240 DISubprogram *SP = GET_OR_DISTINCT(
2242 Record[0] || Record[8], // All definitions should be distinct.
2243 (Context, getMDOrNull(Record[1]), getMDString(Record[2]),
2244 getMDString(Record[3]), getMDOrNull(Record[4]), Record[5],
2245 getMDOrNull(Record[6]), Record[7], Record[8], Record[9],
2246 getMDOrNull(Record[10]), Record[11], Record[12], Record[13],
2247 Record[14], getMDOrNull(Record[15 + HasFn]),
2248 getMDOrNull(Record[16 + HasFn]), getMDOrNull(Record[17 + HasFn])));
2249 MDValueList.assignValue(SP, NextMDValueNo++);
2251 // Upgrade sp->function mapping to function->sp mapping.
2252 if (HasFn && Record[15]) {
2253 if (auto *CMD = dyn_cast<ConstantAsMetadata>(getMDOrNull(Record[15])))
2254 if (auto *F = dyn_cast<Function>(CMD->getValue())) {
2255 if (F->isMaterializable())
2256 // Defer until materialized; unmaterialized functions may not have
2258 FunctionsWithSPs[F] = SP;
2259 else if (!F->empty())
2260 F->setSubprogram(SP);
2265 case bitc::METADATA_LEXICAL_BLOCK: {
2266 if (Record.size() != 5)
2267 return error("Invalid record");
2269 MDValueList.assignValue(
2270 GET_OR_DISTINCT(DILexicalBlock, Record[0],
2271 (Context, getMDOrNull(Record[1]),
2272 getMDOrNull(Record[2]), Record[3], Record[4])),
2276 case bitc::METADATA_LEXICAL_BLOCK_FILE: {
2277 if (Record.size() != 4)
2278 return error("Invalid record");
2280 MDValueList.assignValue(
2281 GET_OR_DISTINCT(DILexicalBlockFile, Record[0],
2282 (Context, getMDOrNull(Record[1]),
2283 getMDOrNull(Record[2]), Record[3])),
2287 case bitc::METADATA_NAMESPACE: {
2288 if (Record.size() != 5)
2289 return error("Invalid record");
2291 MDValueList.assignValue(
2292 GET_OR_DISTINCT(DINamespace, Record[0],
2293 (Context, getMDOrNull(Record[1]),
2294 getMDOrNull(Record[2]), getMDString(Record[3]),
2299 case bitc::METADATA_MACRO: {
2300 if (Record.size() != 5)
2301 return error("Invalid record");
2303 MDValueList.assignValue(
2304 GET_OR_DISTINCT(DIMacro, Record[0],
2305 (Context, Record[1], Record[2],
2306 getMDString(Record[3]), getMDString(Record[4]))),
2310 case bitc::METADATA_MACRO_FILE: {
2311 if (Record.size() != 5)
2312 return error("Invalid record");
2314 MDValueList.assignValue(
2315 GET_OR_DISTINCT(DIMacroFile, Record[0],
2316 (Context, Record[1], Record[2],
2317 getMDOrNull(Record[3]), getMDOrNull(Record[4]))),
2321 case bitc::METADATA_TEMPLATE_TYPE: {
2322 if (Record.size() != 3)
2323 return error("Invalid record");
2325 MDValueList.assignValue(GET_OR_DISTINCT(DITemplateTypeParameter,
2327 (Context, getMDString(Record[1]),
2328 getMDOrNull(Record[2]))),
2332 case bitc::METADATA_TEMPLATE_VALUE: {
2333 if (Record.size() != 5)
2334 return error("Invalid record");
2336 MDValueList.assignValue(
2337 GET_OR_DISTINCT(DITemplateValueParameter, Record[0],
2338 (Context, Record[1], getMDString(Record[2]),
2339 getMDOrNull(Record[3]), getMDOrNull(Record[4]))),
2343 case bitc::METADATA_GLOBAL_VAR: {
2344 if (Record.size() != 11)
2345 return error("Invalid record");
2347 MDValueList.assignValue(
2348 GET_OR_DISTINCT(DIGlobalVariable, Record[0],
2349 (Context, getMDOrNull(Record[1]),
2350 getMDString(Record[2]), getMDString(Record[3]),
2351 getMDOrNull(Record[4]), Record[5],
2352 getMDOrNull(Record[6]), Record[7], Record[8],
2353 getMDOrNull(Record[9]), getMDOrNull(Record[10]))),
2357 case bitc::METADATA_LOCAL_VAR: {
2358 // 10th field is for the obseleted 'inlinedAt:' field.
2359 if (Record.size() < 8 || Record.size() > 10)
2360 return error("Invalid record");
2362 // 2nd field used to be an artificial tag, either DW_TAG_auto_variable or
2363 // DW_TAG_arg_variable.
2364 bool HasTag = Record.size() > 8;
2365 MDValueList.assignValue(
2366 GET_OR_DISTINCT(DILocalVariable, Record[0],
2367 (Context, getMDOrNull(Record[1 + HasTag]),
2368 getMDString(Record[2 + HasTag]),
2369 getMDOrNull(Record[3 + HasTag]), Record[4 + HasTag],
2370 getMDOrNull(Record[5 + HasTag]), Record[6 + HasTag],
2371 Record[7 + HasTag])),
2375 case bitc::METADATA_EXPRESSION: {
2376 if (Record.size() < 1)
2377 return error("Invalid record");
2379 MDValueList.assignValue(
2380 GET_OR_DISTINCT(DIExpression, Record[0],
2381 (Context, makeArrayRef(Record).slice(1))),
2385 case bitc::METADATA_OBJC_PROPERTY: {
2386 if (Record.size() != 8)
2387 return error("Invalid record");
2389 MDValueList.assignValue(
2390 GET_OR_DISTINCT(DIObjCProperty, Record[0],
2391 (Context, getMDString(Record[1]),
2392 getMDOrNull(Record[2]), Record[3],
2393 getMDString(Record[4]), getMDString(Record[5]),
2394 Record[6], getMDOrNull(Record[7]))),
2398 case bitc::METADATA_IMPORTED_ENTITY: {
2399 if (Record.size() != 6)
2400 return error("Invalid record");
2402 MDValueList.assignValue(
2403 GET_OR_DISTINCT(DIImportedEntity, Record[0],
2404 (Context, Record[1], getMDOrNull(Record[2]),
2405 getMDOrNull(Record[3]), Record[4],
2406 getMDString(Record[5]))),
2410 case bitc::METADATA_STRING: {
2411 std::string String(Record.begin(), Record.end());
2412 llvm::UpgradeMDStringConstant(String);
2413 Metadata *MD = MDString::get(Context, String);
2414 MDValueList.assignValue(MD, NextMDValueNo++);
2417 case bitc::METADATA_KIND: {
2418 // Support older bitcode files that had METADATA_KIND records in a
2419 // block with METADATA_BLOCK_ID.
2420 if (std::error_code EC = parseMetadataKindRecord(Record))
2426 #undef GET_OR_DISTINCT
2429 /// Parse the metadata kinds out of the METADATA_KIND_BLOCK.
2430 std::error_code BitcodeReader::parseMetadataKinds() {
2431 if (Stream.EnterSubBlock(bitc::METADATA_KIND_BLOCK_ID))
2432 return error("Invalid record");
2434 SmallVector<uint64_t, 64> Record;
2436 // Read all the records.
2438 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
2440 switch (Entry.Kind) {
2441 case BitstreamEntry::SubBlock: // Handled for us already.
2442 case BitstreamEntry::Error:
2443 return error("Malformed block");
2444 case BitstreamEntry::EndBlock:
2445 return std::error_code();
2446 case BitstreamEntry::Record:
2447 // The interesting case.
2453 unsigned Code = Stream.readRecord(Entry.ID, Record);
2455 default: // Default behavior: ignore.
2457 case bitc::METADATA_KIND: {
2458 if (std::error_code EC = parseMetadataKindRecord(Record))
2466 /// Decode a signed value stored with the sign bit in the LSB for dense VBR
2468 uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V) {
2473 // There is no such thing as -0 with integers. "-0" really means MININT.
2477 /// Resolve all of the initializers for global values and aliases that we can.
2478 std::error_code BitcodeReader::resolveGlobalAndAliasInits() {
2479 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInitWorklist;
2480 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInitWorklist;
2481 std::vector<std::pair<Function*, unsigned> > FunctionPrefixWorklist;
2482 std::vector<std::pair<Function*, unsigned> > FunctionPrologueWorklist;
2483 std::vector<std::pair<Function*, unsigned> > FunctionPersonalityFnWorklist;
2485 GlobalInitWorklist.swap(GlobalInits);
2486 AliasInitWorklist.swap(AliasInits);
2487 FunctionPrefixWorklist.swap(FunctionPrefixes);
2488 FunctionPrologueWorklist.swap(FunctionPrologues);
2489 FunctionPersonalityFnWorklist.swap(FunctionPersonalityFns);
2491 while (!GlobalInitWorklist.empty()) {
2492 unsigned ValID = GlobalInitWorklist.back().second;
2493 if (ValID >= ValueList.size()) {
2494 // Not ready to resolve this yet, it requires something later in the file.
2495 GlobalInits.push_back(GlobalInitWorklist.back());
2497 if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
2498 GlobalInitWorklist.back().first->setInitializer(C);
2500 return error("Expected a constant");
2502 GlobalInitWorklist.pop_back();
2505 while (!AliasInitWorklist.empty()) {
2506 unsigned ValID = AliasInitWorklist.back().second;
2507 if (ValID >= ValueList.size()) {
2508 AliasInits.push_back(AliasInitWorklist.back());
2510 Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]);
2512 return error("Expected a constant");
2513 GlobalAlias *Alias = AliasInitWorklist.back().first;
2514 if (C->getType() != Alias->getType())
2515 return error("Alias and aliasee types don't match");
2516 Alias->setAliasee(C);
2518 AliasInitWorklist.pop_back();
2521 while (!FunctionPrefixWorklist.empty()) {
2522 unsigned ValID = FunctionPrefixWorklist.back().second;
2523 if (ValID >= ValueList.size()) {
2524 FunctionPrefixes.push_back(FunctionPrefixWorklist.back());
2526 if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
2527 FunctionPrefixWorklist.back().first->setPrefixData(C);
2529 return error("Expected a constant");
2531 FunctionPrefixWorklist.pop_back();
2534 while (!FunctionPrologueWorklist.empty()) {
2535 unsigned ValID = FunctionPrologueWorklist.back().second;
2536 if (ValID >= ValueList.size()) {
2537 FunctionPrologues.push_back(FunctionPrologueWorklist.back());
2539 if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
2540 FunctionPrologueWorklist.back().first->setPrologueData(C);
2542 return error("Expected a constant");
2544 FunctionPrologueWorklist.pop_back();
2547 while (!FunctionPersonalityFnWorklist.empty()) {
2548 unsigned ValID = FunctionPersonalityFnWorklist.back().second;
2549 if (ValID >= ValueList.size()) {
2550 FunctionPersonalityFns.push_back(FunctionPersonalityFnWorklist.back());
2552 if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
2553 FunctionPersonalityFnWorklist.back().first->setPersonalityFn(C);
2555 return error("Expected a constant");
2557 FunctionPersonalityFnWorklist.pop_back();
2560 return std::error_code();
2563 static APInt readWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) {
2564 SmallVector<uint64_t, 8> Words(Vals.size());
2565 std::transform(Vals.begin(), Vals.end(), Words.begin(),
2566 BitcodeReader::decodeSignRotatedValue);
2568 return APInt(TypeBits, Words);
2571 std::error_code BitcodeReader::parseConstants() {
2572 if (Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID))
2573 return error("Invalid record");
2575 SmallVector<uint64_t, 64> Record;
2577 // Read all the records for this value table.
2578 Type *CurTy = Type::getInt32Ty(Context);
2579 unsigned NextCstNo = ValueList.size();
2581 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
2583 switch (Entry.Kind) {
2584 case BitstreamEntry::SubBlock: // Handled for us already.
2585 case BitstreamEntry::Error:
2586 return error("Malformed block");
2587 case BitstreamEntry::EndBlock:
2588 if (NextCstNo != ValueList.size())
2589 return error("Invalid ronstant reference");
2591 // Once all the constants have been read, go through and resolve forward
2593 ValueList.resolveConstantForwardRefs();
2594 return std::error_code();
2595 case BitstreamEntry::Record:
2596 // The interesting case.
2603 unsigned BitCode = Stream.readRecord(Entry.ID, Record);
2605 default: // Default behavior: unknown constant
2606 case bitc::CST_CODE_UNDEF: // UNDEF
2607 V = UndefValue::get(CurTy);
2609 case bitc::CST_CODE_SETTYPE: // SETTYPE: [typeid]
2611 return error("Invalid record");
2612 if (Record[0] >= TypeList.size() || !TypeList[Record[0]])
2613 return error("Invalid record");
2614 CurTy = TypeList[Record[0]];
2615 continue; // Skip the ValueList manipulation.
2616 case bitc::CST_CODE_NULL: // NULL
2617 V = Constant::getNullValue(CurTy);
2619 case bitc::CST_CODE_INTEGER: // INTEGER: [intval]
2620 if (!CurTy->isIntegerTy() || Record.empty())
2621 return error("Invalid record");
2622 V = ConstantInt::get(CurTy, decodeSignRotatedValue(Record[0]));
2624 case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval]
2625 if (!CurTy->isIntegerTy() || Record.empty())
2626 return error("Invalid record");
2629 readWideAPInt(Record, cast<IntegerType>(CurTy)->getBitWidth());
2630 V = ConstantInt::get(Context, VInt);
2634 case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval]
2636 return error("Invalid record");
2637 if (CurTy->isHalfTy())
2638 V = ConstantFP::get(Context, APFloat(APFloat::IEEEhalf,
2639 APInt(16, (uint16_t)Record[0])));
2640 else if (CurTy->isFloatTy())
2641 V = ConstantFP::get(Context, APFloat(APFloat::IEEEsingle,
2642 APInt(32, (uint32_t)Record[0])));
2643 else if (CurTy->isDoubleTy())
2644 V = ConstantFP::get(Context, APFloat(APFloat::IEEEdouble,
2645 APInt(64, Record[0])));
2646 else if (CurTy->isX86_FP80Ty()) {
2647 // Bits are not stored the same way as a normal i80 APInt, compensate.
2648 uint64_t Rearrange[2];
2649 Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16);
2650 Rearrange[1] = Record[0] >> 48;
2651 V = ConstantFP::get(Context, APFloat(APFloat::x87DoubleExtended,
2652 APInt(80, Rearrange)));
2653 } else if (CurTy->isFP128Ty())
2654 V = ConstantFP::get(Context, APFloat(APFloat::IEEEquad,
2655 APInt(128, Record)));
2656 else if (CurTy->isPPC_FP128Ty())
2657 V = ConstantFP::get(Context, APFloat(APFloat::PPCDoubleDouble,
2658 APInt(128, Record)));
2660 V = UndefValue::get(CurTy);
2664 case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number]
2666 return error("Invalid record");
2668 unsigned Size = Record.size();
2669 SmallVector<Constant*, 16> Elts;
2671 if (StructType *STy = dyn_cast<StructType>(CurTy)) {
2672 for (unsigned i = 0; i != Size; ++i)
2673 Elts.push_back(ValueList.getConstantFwdRef(Record[i],
2674 STy->getElementType(i)));
2675 V = ConstantStruct::get(STy, Elts);
2676 } else if (ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) {
2677 Type *EltTy = ATy->getElementType();
2678 for (unsigned i = 0; i != Size; ++i)
2679 Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
2680 V = ConstantArray::get(ATy, Elts);
2681 } else if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) {
2682 Type *EltTy = VTy->getElementType();
2683 for (unsigned i = 0; i != Size; ++i)
2684 Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
2685 V = ConstantVector::get(Elts);
2687 V = UndefValue::get(CurTy);
2691 case bitc::CST_CODE_STRING: // STRING: [values]
2692 case bitc::CST_CODE_CSTRING: { // CSTRING: [values]
2694 return error("Invalid record");
2696 SmallString<16> Elts(Record.begin(), Record.end());
2697 V = ConstantDataArray::getString(Context, Elts,
2698 BitCode == bitc::CST_CODE_CSTRING);
2701 case bitc::CST_CODE_DATA: {// DATA: [n x value]
2703 return error("Invalid record");
2705 Type *EltTy = cast<SequentialType>(CurTy)->getElementType();
2706 unsigned Size = Record.size();
2708 if (EltTy->isIntegerTy(8)) {
2709 SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end());
2710 if (isa<VectorType>(CurTy))
2711 V = ConstantDataVector::get(Context, Elts);
2713 V = ConstantDataArray::get(Context, Elts);
2714 } else if (EltTy->isIntegerTy(16)) {
2715 SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end());
2716 if (isa<VectorType>(CurTy))
2717 V = ConstantDataVector::get(Context, Elts);
2719 V = ConstantDataArray::get(Context, Elts);
2720 } else if (EltTy->isIntegerTy(32)) {
2721 SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end());
2722 if (isa<VectorType>(CurTy))
2723 V = ConstantDataVector::get(Context, Elts);
2725 V = ConstantDataArray::get(Context, Elts);
2726 } else if (EltTy->isIntegerTy(64)) {
2727 SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end());
2728 if (isa<VectorType>(CurTy))
2729 V = ConstantDataVector::get(Context, Elts);
2731 V = ConstantDataArray::get(Context, Elts);
2732 } else if (EltTy->isFloatTy()) {
2733 SmallVector<float, 16> Elts(Size);
2734 std::transform(Record.begin(), Record.end(), Elts.begin(), BitsToFloat);
2735 if (isa<VectorType>(CurTy))
2736 V = ConstantDataVector::get(Context, Elts);
2738 V = ConstantDataArray::get(Context, Elts);
2739 } else if (EltTy->isDoubleTy()) {
2740 SmallVector<double, 16> Elts(Size);
2741 std::transform(Record.begin(), Record.end(), Elts.begin(),
2743 if (isa<VectorType>(CurTy))
2744 V = ConstantDataVector::get(Context, Elts);
2746 V = ConstantDataArray::get(Context, Elts);
2748 return error("Invalid type for value");
2753 case bitc::CST_CODE_CE_BINOP: { // CE_BINOP: [opcode, opval, opval]
2754 if (Record.size() < 3)
2755 return error("Invalid record");
2756 int Opc = getDecodedBinaryOpcode(Record[0], CurTy);
2758 V = UndefValue::get(CurTy); // Unknown binop.
2760 Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy);
2761 Constant *RHS = ValueList.getConstantFwdRef(Record[2], CurTy);
2763 if (Record.size() >= 4) {
2764 if (Opc == Instruction::Add ||
2765 Opc == Instruction::Sub ||
2766 Opc == Instruction::Mul ||
2767 Opc == Instruction::Shl) {
2768 if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP))
2769 Flags |= OverflowingBinaryOperator::NoSignedWrap;
2770 if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
2771 Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
2772 } else if (Opc == Instruction::SDiv ||
2773 Opc == Instruction::UDiv ||
2774 Opc == Instruction::LShr ||
2775 Opc == Instruction::AShr) {
2776 if (Record[3] & (1 << bitc::PEO_EXACT))
2777 Flags |= SDivOperator::IsExact;
2780 V = ConstantExpr::get(Opc, LHS, RHS, Flags);
2784 case bitc::CST_CODE_CE_CAST: { // CE_CAST: [opcode, opty, opval]
2785 if (Record.size() < 3)
2786 return error("Invalid record");
2787 int Opc = getDecodedCastOpcode(Record[0]);
2789 V = UndefValue::get(CurTy); // Unknown cast.
2791 Type *OpTy = getTypeByID(Record[1]);
2793 return error("Invalid record");
2794 Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy);
2795 V = UpgradeBitCastExpr(Opc, Op, CurTy);
2796 if (!V) V = ConstantExpr::getCast(Opc, Op, CurTy);
2800 case bitc::CST_CODE_CE_INBOUNDS_GEP:
2801 case bitc::CST_CODE_CE_GEP: { // CE_GEP: [n x operands]
2803 Type *PointeeType = nullptr;
2804 if (Record.size() % 2)
2805 PointeeType = getTypeByID(Record[OpNum++]);
2806 SmallVector<Constant*, 16> Elts;
2807 while (OpNum != Record.size()) {
2808 Type *ElTy = getTypeByID(Record[OpNum++]);
2810 return error("Invalid record");
2811 Elts.push_back(ValueList.getConstantFwdRef(Record[OpNum++], ElTy));
2816 cast<SequentialType>(Elts[0]->getType()->getScalarType())
2818 return error("Explicit gep operator type does not match pointee type "
2819 "of pointer operand");
2821 ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
2822 V = ConstantExpr::getGetElementPtr(PointeeType, Elts[0], Indices,
2824 bitc::CST_CODE_CE_INBOUNDS_GEP);
2827 case bitc::CST_CODE_CE_SELECT: { // CE_SELECT: [opval#, opval#, opval#]
2828 if (Record.size() < 3)
2829 return error("Invalid record");
2831 Type *SelectorTy = Type::getInt1Ty(Context);
2833 // The selector might be an i1 or an <n x i1>
2834 // Get the type from the ValueList before getting a forward ref.
2835 if (VectorType *VTy = dyn_cast<VectorType>(CurTy))
2836 if (Value *V = ValueList[Record[0]])
2837 if (SelectorTy != V->getType())
2838 SelectorTy = VectorType::get(SelectorTy, VTy->getNumElements());
2840 V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0],
2842 ValueList.getConstantFwdRef(Record[1],CurTy),
2843 ValueList.getConstantFwdRef(Record[2],CurTy));
2846 case bitc::CST_CODE_CE_EXTRACTELT
2847 : { // CE_EXTRACTELT: [opty, opval, opty, opval]
2848 if (Record.size() < 3)
2849 return error("Invalid record");
2851 dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
2853 return error("Invalid record");
2854 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
2855 Constant *Op1 = nullptr;
2856 if (Record.size() == 4) {
2857 Type *IdxTy = getTypeByID(Record[2]);
2859 return error("Invalid record");
2860 Op1 = ValueList.getConstantFwdRef(Record[3], IdxTy);
2861 } else // TODO: Remove with llvm 4.0
2862 Op1 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context));
2864 return error("Invalid record");
2865 V = ConstantExpr::getExtractElement(Op0, Op1);
2868 case bitc::CST_CODE_CE_INSERTELT
2869 : { // CE_INSERTELT: [opval, opval, opty, opval]
2870 VectorType *OpTy = dyn_cast<VectorType>(CurTy);
2871 if (Record.size() < 3 || !OpTy)
2872 return error("Invalid record");
2873 Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy);
2874 Constant *Op1 = ValueList.getConstantFwdRef(Record[1],
2875 OpTy->getElementType());
2876 Constant *Op2 = nullptr;
2877 if (Record.size() == 4) {
2878 Type *IdxTy = getTypeByID(Record[2]);
2880 return error("Invalid record");
2881 Op2 = ValueList.getConstantFwdRef(Record[3], IdxTy);
2882 } else // TODO: Remove with llvm 4.0
2883 Op2 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context));
2885 return error("Invalid record");
2886 V = ConstantExpr::getInsertElement(Op0, Op1, Op2);
2889 case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval]
2890 VectorType *OpTy = dyn_cast<VectorType>(CurTy);
2891 if (Record.size() < 3 || !OpTy)
2892 return error("Invalid record");
2893 Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy);
2894 Constant *Op1 = ValueList.getConstantFwdRef(Record[1], OpTy);
2895 Type *ShufTy = VectorType::get(Type::getInt32Ty(Context),
2896 OpTy->getNumElements());
2897 Constant *Op2 = ValueList.getConstantFwdRef(Record[2], ShufTy);
2898 V = ConstantExpr::getShuffleVector(Op0, Op1, Op2);
2901 case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval]
2902 VectorType *RTy = dyn_cast<VectorType>(CurTy);
2904 dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
2905 if (Record.size() < 4 || !RTy || !OpTy)
2906 return error("Invalid record");
2907 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
2908 Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
2909 Type *ShufTy = VectorType::get(Type::getInt32Ty(Context),
2910 RTy->getNumElements());
2911 Constant *Op2 = ValueList.getConstantFwdRef(Record[3], ShufTy);
2912 V = ConstantExpr::getShuffleVector(Op0, Op1, Op2);
2915 case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred]
2916 if (Record.size() < 4)
2917 return error("Invalid record");
2918 Type *OpTy = getTypeByID(Record[0]);
2920 return error("Invalid record");
2921 Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
2922 Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
2924 if (OpTy->isFPOrFPVectorTy())
2925 V = ConstantExpr::getFCmp(Record[3], Op0, Op1);
2927 V = ConstantExpr::getICmp(Record[3], Op0, Op1);
2930 // This maintains backward compatibility, pre-asm dialect keywords.
2931 // FIXME: Remove with the 4.0 release.
2932 case bitc::CST_CODE_INLINEASM_OLD: {
2933 if (Record.size() < 2)
2934 return error("Invalid record");
2935 std::string AsmStr, ConstrStr;
2936 bool HasSideEffects = Record[0] & 1;
2937 bool IsAlignStack = Record[0] >> 1;
2938 unsigned AsmStrSize = Record[1];
2939 if (2+AsmStrSize >= Record.size())
2940 return error("Invalid record");
2941 unsigned ConstStrSize = Record[2+AsmStrSize];
2942 if (3+AsmStrSize+ConstStrSize > Record.size())
2943 return error("Invalid record");
2945 for (unsigned i = 0; i != AsmStrSize; ++i)
2946 AsmStr += (char)Record[2+i];
2947 for (unsigned i = 0; i != ConstStrSize; ++i)
2948 ConstrStr += (char)Record[3+AsmStrSize+i];
2949 PointerType *PTy = cast<PointerType>(CurTy);
2950 V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()),
2951 AsmStr, ConstrStr, HasSideEffects, IsAlignStack);
2954 // This version adds support for the asm dialect keywords (e.g.,
2956 case bitc::CST_CODE_INLINEASM: {
2957 if (Record.size() < 2)
2958 return error("Invalid record");
2959 std::string AsmStr, ConstrStr;
2960 bool HasSideEffects = Record[0] & 1;
2961 bool IsAlignStack = (Record[0] >> 1) & 1;
2962 unsigned AsmDialect = Record[0] >> 2;
2963 unsigned AsmStrSize = Record[1];
2964 if (2+AsmStrSize >= Record.size())
2965 return error("Invalid record");
2966 unsigned ConstStrSize = Record[2+AsmStrSize];
2967 if (3+AsmStrSize+ConstStrSize > Record.size())
2968 return error("Invalid record");
2970 for (unsigned i = 0; i != AsmStrSize; ++i)
2971 AsmStr += (char)Record[2+i];
2972 for (unsigned i = 0; i != ConstStrSize; ++i)
2973 ConstrStr += (char)Record[3+AsmStrSize+i];
2974 PointerType *PTy = cast<PointerType>(CurTy);
2975 V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()),
2976 AsmStr, ConstrStr, HasSideEffects, IsAlignStack,
2977 InlineAsm::AsmDialect(AsmDialect));
2980 case bitc::CST_CODE_BLOCKADDRESS:{
2981 if (Record.size() < 3)
2982 return error("Invalid record");
2983 Type *FnTy = getTypeByID(Record[0]);
2985 return error("Invalid record");
2987 dyn_cast_or_null<Function>(ValueList.getConstantFwdRef(Record[1],FnTy));
2989 return error("Invalid record");
2991 // Don't let Fn get dematerialized.
2992 BlockAddressesTaken.insert(Fn);
2994 // If the function is already parsed we can insert the block address right
2997 unsigned BBID = Record[2];
2999 // Invalid reference to entry block.
3000 return error("Invalid ID");
3002 Function::iterator BBI = Fn->begin(), BBE = Fn->end();
3003 for (size_t I = 0, E = BBID; I != E; ++I) {
3005 return error("Invalid ID");
3010 // Otherwise insert a placeholder and remember it so it can be inserted
3011 // when the function is parsed.
3012 auto &FwdBBs = BasicBlockFwdRefs[Fn];
3014 BasicBlockFwdRefQueue.push_back(Fn);
3015 if (FwdBBs.size() < BBID + 1)
3016 FwdBBs.resize(BBID + 1);
3018 FwdBBs[BBID] = BasicBlock::Create(Context);
3021 V = BlockAddress::get(Fn, BB);
3026 if (ValueList.assignValue(V, NextCstNo))
3027 return error("Invalid forward reference");
3032 std::error_code BitcodeReader::parseUseLists() {
3033 if (Stream.EnterSubBlock(bitc::USELIST_BLOCK_ID))
3034 return error("Invalid record");
3036 // Read all the records.
3037 SmallVector<uint64_t, 64> Record;
3039 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
3041 switch (Entry.Kind) {
3042 case BitstreamEntry::SubBlock: // Handled for us already.
3043 case BitstreamEntry::Error:
3044 return error("Malformed block");
3045 case BitstreamEntry::EndBlock:
3046 return std::error_code();
3047 case BitstreamEntry::Record:
3048 // The interesting case.
3052 // Read a use list record.
3055 switch (Stream.readRecord(Entry.ID, Record)) {
3056 default: // Default behavior: unknown type.
3058 case bitc::USELIST_CODE_BB:
3061 case bitc::USELIST_CODE_DEFAULT: {
3062 unsigned RecordLength = Record.size();
3063 if (RecordLength < 3)
3064 // Records should have at least an ID and two indexes.
3065 return error("Invalid record");
3066 unsigned ID = Record.back();
3071 assert(ID < FunctionBBs.size() && "Basic block not found");
3072 V = FunctionBBs[ID];
3075 unsigned NumUses = 0;
3076 SmallDenseMap<const Use *, unsigned, 16> Order;
3077 for (const Use &U : V->uses()) {
3078 if (++NumUses > Record.size())
3080 Order[&U] = Record[NumUses - 1];
3082 if (Order.size() != Record.size() || NumUses > Record.size())
3083 // Mismatches can happen if the functions are being materialized lazily
3084 // (out-of-order), or a value has been upgraded.
3087 V->sortUseList([&](const Use &L, const Use &R) {
3088 return Order.lookup(&L) < Order.lookup(&R);
3096 /// When we see the block for metadata, remember where it is and then skip it.
3097 /// This lets us lazily deserialize the metadata.
3098 std::error_code BitcodeReader::rememberAndSkipMetadata() {
3099 // Save the current stream state.
3100 uint64_t CurBit = Stream.GetCurrentBitNo();
3101 DeferredMetadataInfo.push_back(CurBit);
3103 // Skip over the block for now.
3104 if (Stream.SkipBlock())
3105 return error("Invalid record");
3106 return std::error_code();
3109 std::error_code BitcodeReader::materializeMetadata() {
3110 for (uint64_t BitPos : DeferredMetadataInfo) {
3111 // Move the bit stream to the saved position.
3112 Stream.JumpToBit(BitPos);
3113 if (std::error_code EC = parseMetadata(true))
3116 DeferredMetadataInfo.clear();
3117 return std::error_code();
3120 void BitcodeReader::setStripDebugInfo() { StripDebugInfo = true; }
3122 /// When we see the block for a function body, remember where it is and then
3123 /// skip it. This lets us lazily deserialize the functions.
3124 std::error_code BitcodeReader::rememberAndSkipFunctionBody() {
3125 // Get the function we are talking about.
3126 if (FunctionsWithBodies.empty())
3127 return error("Insufficient function protos");
3129 Function *Fn = FunctionsWithBodies.back();
3130 FunctionsWithBodies.pop_back();
3132 // Save the current stream state.
3133 uint64_t CurBit = Stream.GetCurrentBitNo();
3135 (DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo[Fn] == CurBit) &&
3136 "Mismatch between VST and scanned function offsets");
3137 DeferredFunctionInfo[Fn] = CurBit;
3139 // Skip over the function block for now.
3140 if (Stream.SkipBlock())
3141 return error("Invalid record");
3142 return std::error_code();
3145 std::error_code BitcodeReader::globalCleanup() {
3146 // Patch the initializers for globals and aliases up.
3147 resolveGlobalAndAliasInits();
3148 if (!GlobalInits.empty() || !AliasInits.empty())
3149 return error("Malformed global initializer set");
3151 // Look for intrinsic functions which need to be upgraded at some point
3152 for (Function &F : *TheModule) {
3154 if (UpgradeIntrinsicFunction(&F, NewFn))
3155 UpgradedIntrinsics[&F] = NewFn;
3158 // Look for global variables which need to be renamed.
3159 for (GlobalVariable &GV : TheModule->globals())
3160 UpgradeGlobalVariable(&GV);
3162 // Force deallocation of memory for these vectors to favor the client that
3163 // want lazy deserialization.
3164 std::vector<std::pair<GlobalVariable*, unsigned> >().swap(GlobalInits);
3165 std::vector<std::pair<GlobalAlias*, unsigned> >().swap(AliasInits);
3166 return std::error_code();
3169 /// Support for lazy parsing of function bodies. This is required if we
3170 /// either have an old bitcode file without a VST forward declaration record,
3171 /// or if we have an anonymous function being materialized, since anonymous
3172 /// functions do not have a name and are therefore not in the VST.
3173 std::error_code BitcodeReader::rememberAndSkipFunctionBodies() {
3174 Stream.JumpToBit(NextUnreadBit);
3176 if (Stream.AtEndOfStream())
3177 return error("Could not find function in stream");
3179 if (!SeenFirstFunctionBody)
3180 return error("Trying to materialize functions before seeing function blocks");
3182 // An old bitcode file with the symbol table at the end would have
3183 // finished the parse greedily.
3184 assert(SeenValueSymbolTable);
3186 SmallVector<uint64_t, 64> Record;
3189 BitstreamEntry Entry = Stream.advance();
3190 switch (Entry.Kind) {
3192 return error("Expect SubBlock");
3193 case BitstreamEntry::SubBlock:
3196 return error("Expect function block");
3197 case bitc::FUNCTION_BLOCK_ID:
3198 if (std::error_code EC = rememberAndSkipFunctionBody())
3200 NextUnreadBit = Stream.GetCurrentBitNo();
3201 return std::error_code();
3207 std::error_code BitcodeReader::parseBitcodeVersion() {
3208 if (Stream.EnterSubBlock(bitc::IDENTIFICATION_BLOCK_ID))
3209 return error("Invalid record");
3211 // Read all the records.
3212 SmallVector<uint64_t, 64> Record;
3214 BitstreamEntry Entry = Stream.advance();
3216 switch (Entry.Kind) {
3218 case BitstreamEntry::Error:
3219 return error("Malformed block");
3220 case BitstreamEntry::EndBlock:
3221 return std::error_code();
3222 case BitstreamEntry::Record:
3223 // The interesting case.
3229 unsigned BitCode = Stream.readRecord(Entry.ID, Record);
3231 default: // Default behavior: reject
3232 return error("Invalid value");
3233 case bitc::IDENTIFICATION_CODE_STRING: { // IDENTIFICATION: [strchr x
3235 convertToString(Record, 0, ProducerIdentification);
3238 case bitc::IDENTIFICATION_CODE_EPOCH: { // EPOCH: [epoch#]
3239 unsigned epoch = (unsigned)Record[0];
3240 if (epoch != bitc::BITCODE_CURRENT_EPOCH) {
3242 Twine("Incompatible epoch: Bitcode '") + Twine(epoch) +
3243 "' vs current: '" + Twine(bitc::BITCODE_CURRENT_EPOCH) + "'");
3250 std::error_code BitcodeReader::parseModule(uint64_t ResumeBit,
3251 bool ShouldLazyLoadMetadata) {
3253 Stream.JumpToBit(ResumeBit);
3254 else if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
3255 return error("Invalid record");
3257 SmallVector<uint64_t, 64> Record;
3258 std::vector<std::string> SectionTable;
3259 std::vector<std::string> GCTable;
3261 // Read all the records for this module.
3263 BitstreamEntry Entry = Stream.advance();
3265 switch (Entry.Kind) {
3266 case BitstreamEntry::Error:
3267 return error("Malformed block");
3268 case BitstreamEntry::EndBlock:
3269 return globalCleanup();
3271 case BitstreamEntry::SubBlock:
3273 default: // Skip unknown content.
3274 if (Stream.SkipBlock())
3275 return error("Invalid record");
3277 case bitc::BLOCKINFO_BLOCK_ID:
3278 if (Stream.ReadBlockInfoBlock())
3279 return error("Malformed block");
3281 case bitc::PARAMATTR_BLOCK_ID:
3282 if (std::error_code EC = parseAttributeBlock())
3285 case bitc::PARAMATTR_GROUP_BLOCK_ID:
3286 if (std::error_code EC = parseAttributeGroupBlock())
3289 case bitc::TYPE_BLOCK_ID_NEW:
3290 if (std::error_code EC = parseTypeTable())
3293 case bitc::VALUE_SYMTAB_BLOCK_ID:
3294 if (!SeenValueSymbolTable) {
3295 // Either this is an old form VST without function index and an
3296 // associated VST forward declaration record (which would have caused
3297 // the VST to be jumped to and parsed before it was encountered
3298 // normally in the stream), or there were no function blocks to
3299 // trigger an earlier parsing of the VST.
3300 assert(VSTOffset == 0 || FunctionsWithBodies.empty());
3301 if (std::error_code EC = parseValueSymbolTable())
3303 SeenValueSymbolTable = true;
3305 // We must have had a VST forward declaration record, which caused
3306 // the parser to jump to and parse the VST earlier.
3307 assert(VSTOffset > 0);
3308 if (Stream.SkipBlock())
3309 return error("Invalid record");
3312 case bitc::CONSTANTS_BLOCK_ID:
3313 if (std::error_code EC = parseConstants())
3315 if (std::error_code EC = resolveGlobalAndAliasInits())
3318 case bitc::METADATA_BLOCK_ID:
3319 if (ShouldLazyLoadMetadata && !IsMetadataMaterialized) {
3320 if (std::error_code EC = rememberAndSkipMetadata())
3324 assert(DeferredMetadataInfo.empty() && "Unexpected deferred metadata");
3325 if (std::error_code EC = parseMetadata(true))
3328 case bitc::METADATA_KIND_BLOCK_ID:
3329 if (std::error_code EC = parseMetadataKinds())
3332 case bitc::FUNCTION_BLOCK_ID:
3333 // If this is the first function body we've seen, reverse the
3334 // FunctionsWithBodies list.
3335 if (!SeenFirstFunctionBody) {
3336 std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end());
3337 if (std::error_code EC = globalCleanup())
3339 SeenFirstFunctionBody = true;
3342 if (VSTOffset > 0) {
3343 // If we have a VST forward declaration record, make sure we
3344 // parse the VST now if we haven't already. It is needed to
3345 // set up the DeferredFunctionInfo vector for lazy reading.
3346 if (!SeenValueSymbolTable) {
3347 if (std::error_code EC =
3348 BitcodeReader::parseValueSymbolTable(VSTOffset))
3350 SeenValueSymbolTable = true;
3351 // Fall through so that we record the NextUnreadBit below.
3352 // This is necessary in case we have an anonymous function that
3353 // is later materialized. Since it will not have a VST entry we
3354 // need to fall back to the lazy parse to find its offset.
3356 // If we have a VST forward declaration record, but have already
3357 // parsed the VST (just above, when the first function body was
3358 // encountered here), then we are resuming the parse after
3359 // materializing functions. The ResumeBit points to the
3360 // start of the last function block recorded in the
3361 // DeferredFunctionInfo map. Skip it.
3362 if (Stream.SkipBlock())
3363 return error("Invalid record");
3368 // Support older bitcode files that did not have the function
3369 // index in the VST, nor a VST forward declaration record, as
3370 // well as anonymous functions that do not have VST entries.
3371 // Build the DeferredFunctionInfo vector on the fly.
3372 if (std::error_code EC = rememberAndSkipFunctionBody())
3375 // Suspend parsing when we reach the function bodies. Subsequent
3376 // materialization calls will resume it when necessary. If the bitcode
3377 // file is old, the symbol table will be at the end instead and will not
3378 // have been seen yet. In this case, just finish the parse now.
3379 if (SeenValueSymbolTable) {
3380 NextUnreadBit = Stream.GetCurrentBitNo();
3381 return std::error_code();
3384 case bitc::USELIST_BLOCK_ID:
3385 if (std::error_code EC = parseUseLists())
3388 case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID:
3389 if (std::error_code EC = parseOperandBundleTags())
3395 case BitstreamEntry::Record:
3396 // The interesting case.
3402 auto BitCode = Stream.readRecord(Entry.ID, Record);
3404 default: break; // Default behavior, ignore unknown content.
3405 case bitc::MODULE_CODE_VERSION: { // VERSION: [version#]
3406 if (Record.size() < 1)
3407 return error("Invalid record");
3408 // Only version #0 and #1 are supported so far.
3409 unsigned module_version = Record[0];
3410 switch (module_version) {
3412 return error("Invalid value");
3414 UseRelativeIDs = false;
3417 UseRelativeIDs = true;
3422 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]
3424 if (convertToString(Record, 0, S))
3425 return error("Invalid record");
3426 TheModule->setTargetTriple(S);
3429 case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N]
3431 if (convertToString(Record, 0, S))
3432 return error("Invalid record");
3433 TheModule->setDataLayout(S);
3436 case bitc::MODULE_CODE_ASM: { // ASM: [strchr x N]
3438 if (convertToString(Record, 0, S))
3439 return error("Invalid record");
3440 TheModule->setModuleInlineAsm(S);
3443 case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N]
3444 // FIXME: Remove in 4.0.
3446 if (convertToString(Record, 0, S))
3447 return error("Invalid record");
3451 case bitc::MODULE_CODE_SECTIONNAME: { // SECTIONNAME: [strchr x N]
3453 if (convertToString(Record, 0, S))
3454 return error("Invalid record");
3455 SectionTable.push_back(S);
3458 case bitc::MODULE_CODE_GCNAME: { // SECTIONNAME: [strchr x N]
3460 if (convertToString(Record, 0, S))
3461 return error("Invalid record");
3462 GCTable.push_back(S);
3465 case bitc::MODULE_CODE_COMDAT: { // COMDAT: [selection_kind, name]
3466 if (Record.size() < 2)
3467 return error("Invalid record");
3468 Comdat::SelectionKind SK = getDecodedComdatSelectionKind(Record[0]);
3469 unsigned ComdatNameSize = Record[1];
3470 std::string ComdatName;
3471 ComdatName.reserve(ComdatNameSize);
3472 for (unsigned i = 0; i != ComdatNameSize; ++i)
3473 ComdatName += (char)Record[2 + i];
3474 Comdat *C = TheModule->getOrInsertComdat(ComdatName);
3475 C->setSelectionKind(SK);
3476 ComdatList.push_back(C);
3479 // GLOBALVAR: [pointer type, isconst, initid,
3480 // linkage, alignment, section, visibility, threadlocal,
3481 // unnamed_addr, externally_initialized, dllstorageclass,
3483 case bitc::MODULE_CODE_GLOBALVAR: {
3484 if (Record.size() < 6)
3485 return error("Invalid record");
3486 Type *Ty = getTypeByID(Record[0]);
3488 return error("Invalid record");
3489 bool isConstant = Record[1] & 1;
3490 bool explicitType = Record[1] & 2;
3491 unsigned AddressSpace;
3493 AddressSpace = Record[1] >> 2;
3495 if (!Ty->isPointerTy())
3496 return error("Invalid type for value");
3497 AddressSpace = cast<PointerType>(Ty)->getAddressSpace();
3498 Ty = cast<PointerType>(Ty)->getElementType();
3501 uint64_t RawLinkage = Record[3];
3502 GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage);
3504 if (std::error_code EC = parseAlignmentValue(Record[4], Alignment))
3506 std::string Section;
3508 if (Record[5]-1 >= SectionTable.size())
3509 return error("Invalid ID");
3510 Section = SectionTable[Record[5]-1];
3512 GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility;
3513 // Local linkage must have default visibility.
3514 if (Record.size() > 6 && !GlobalValue::isLocalLinkage(Linkage))
3515 // FIXME: Change to an error if non-default in 4.0.
3516 Visibility = getDecodedVisibility(Record[6]);
3518 GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal;
3519 if (Record.size() > 7)
3520 TLM = getDecodedThreadLocalMode(Record[7]);
3522 bool UnnamedAddr = false;
3523 if (Record.size() > 8)
3524 UnnamedAddr = Record[8];
3526 bool ExternallyInitialized = false;
3527 if (Record.size() > 9)
3528 ExternallyInitialized = Record[9];
3530 GlobalVariable *NewGV =
3531 new GlobalVariable(*TheModule, Ty, isConstant, Linkage, nullptr, "", nullptr,
3532 TLM, AddressSpace, ExternallyInitialized);
3533 NewGV->setAlignment(Alignment);
3534 if (!Section.empty())
3535 NewGV->setSection(Section);
3536 NewGV->setVisibility(Visibility);
3537 NewGV->setUnnamedAddr(UnnamedAddr);
3539 if (Record.size() > 10)
3540 NewGV->setDLLStorageClass(getDecodedDLLStorageClass(Record[10]));
3542 upgradeDLLImportExportLinkage(NewGV, RawLinkage);
3544 ValueList.push_back(NewGV);
3546 // Remember which value to use for the global initializer.
3547 if (unsigned InitID = Record[2])
3548 GlobalInits.push_back(std::make_pair(NewGV, InitID-1));
3550 if (Record.size() > 11) {
3551 if (unsigned ComdatID = Record[11]) {
3552 if (ComdatID > ComdatList.size())
3553 return error("Invalid global variable comdat ID");
3554 NewGV->setComdat(ComdatList[ComdatID - 1]);
3556 } else if (hasImplicitComdat(RawLinkage)) {
3557 NewGV->setComdat(reinterpret_cast<Comdat *>(1));
3561 // FUNCTION: [type, callingconv, isproto, linkage, paramattr,
3562 // alignment, section, visibility, gc, unnamed_addr,
3563 // prologuedata, dllstorageclass, comdat, prefixdata]
3564 case bitc::MODULE_CODE_FUNCTION: {
3565 if (Record.size() < 8)
3566 return error("Invalid record");
3567 Type *Ty = getTypeByID(Record[0]);
3569 return error("Invalid record");
3570 if (auto *PTy = dyn_cast<PointerType>(Ty))
3571 Ty = PTy->getElementType();
3572 auto *FTy = dyn_cast<FunctionType>(Ty);
3574 return error("Invalid type for value");
3575 auto CC = static_cast<CallingConv::ID>(Record[1]);
3576 if (CC & ~CallingConv::MaxID)
3577 return error("Invalid calling convention ID");
3579 Function *Func = Function::Create(FTy, GlobalValue::ExternalLinkage,
3582 Func->setCallingConv(CC);
3583 bool isProto = Record[2];
3584 uint64_t RawLinkage = Record[3];
3585 Func->setLinkage(getDecodedLinkage(RawLinkage));
3586 Func->setAttributes(getAttributes(Record[4]));
3589 if (std::error_code EC = parseAlignmentValue(Record[5], Alignment))
3591 Func->setAlignment(Alignment);
3593 if (Record[6]-1 >= SectionTable.size())
3594 return error("Invalid ID");
3595 Func->setSection(SectionTable[Record[6]-1]);
3597 // Local linkage must have default visibility.
3598 if (!Func->hasLocalLinkage())
3599 // FIXME: Change to an error if non-default in 4.0.
3600 Func->setVisibility(getDecodedVisibility(Record[7]));
3601 if (Record.size() > 8 && Record[8]) {
3602 if (Record[8]-1 >= GCTable.size())
3603 return error("Invalid ID");
3604 Func->setGC(GCTable[Record[8]-1].c_str());
3606 bool UnnamedAddr = false;
3607 if (Record.size() > 9)
3608 UnnamedAddr = Record[9];
3609 Func->setUnnamedAddr(UnnamedAddr);
3610 if (Record.size() > 10 && Record[10] != 0)
3611 FunctionPrologues.push_back(std::make_pair(Func, Record[10]-1));
3613 if (Record.size() > 11)
3614 Func->setDLLStorageClass(getDecodedDLLStorageClass(Record[11]));
3616 upgradeDLLImportExportLinkage(Func, RawLinkage);
3618 if (Record.size() > 12) {
3619 if (unsigned ComdatID = Record[12]) {
3620 if (ComdatID > ComdatList.size())
3621 return error("Invalid function comdat ID");
3622 Func->setComdat(ComdatList[ComdatID - 1]);
3624 } else if (hasImplicitComdat(RawLinkage)) {
3625 Func->setComdat(reinterpret_cast<Comdat *>(1));
3628 if (Record.size() > 13 && Record[13] != 0)
3629 FunctionPrefixes.push_back(std::make_pair(Func, Record[13]-1));
3631 if (Record.size() > 14 && Record[14] != 0)
3632 FunctionPersonalityFns.push_back(std::make_pair(Func, Record[14] - 1));
3634 ValueList.push_back(Func);
3636 // If this is a function with a body, remember the prototype we are
3637 // creating now, so that we can match up the body with them later.
3639 Func->setIsMaterializable(true);
3640 FunctionsWithBodies.push_back(Func);
3641 DeferredFunctionInfo[Func] = 0;
3645 // ALIAS: [alias type, addrspace, aliasee val#, linkage]
3646 // ALIAS: [alias type, addrspace, aliasee val#, linkage, visibility, dllstorageclass]
3647 case bitc::MODULE_CODE_ALIAS:
3648 case bitc::MODULE_CODE_ALIAS_OLD: {
3649 bool NewRecord = BitCode == bitc::MODULE_CODE_ALIAS;
3650 if (Record.size() < (3 + (unsigned)NewRecord))
3651 return error("Invalid record");
3653 Type *Ty = getTypeByID(Record[OpNum++]);
3655 return error("Invalid record");
3659 auto *PTy = dyn_cast<PointerType>(Ty);
3661 return error("Invalid type for value");
3662 Ty = PTy->getElementType();
3663 AddrSpace = PTy->getAddressSpace();
3665 AddrSpace = Record[OpNum++];
3668 auto Val = Record[OpNum++];
3669 auto Linkage = Record[OpNum++];
3670 auto *NewGA = GlobalAlias::create(
3671 Ty, AddrSpace, getDecodedLinkage(Linkage), "", TheModule);
3672 // Old bitcode files didn't have visibility field.
3673 // Local linkage must have default visibility.
3674 if (OpNum != Record.size()) {
3675 auto VisInd = OpNum++;
3676 if (!NewGA->hasLocalLinkage())
3677 // FIXME: Change to an error if non-default in 4.0.
3678 NewGA->setVisibility(getDecodedVisibility(Record[VisInd]));
3680 if (OpNum != Record.size())
3681 NewGA->setDLLStorageClass(getDecodedDLLStorageClass(Record[OpNum++]));
3683 upgradeDLLImportExportLinkage(NewGA, Linkage);
3684 if (OpNum != Record.size())
3685 NewGA->setThreadLocalMode(getDecodedThreadLocalMode(Record[OpNum++]));
3686 if (OpNum != Record.size())
3687 NewGA->setUnnamedAddr(Record[OpNum++]);
3688 ValueList.push_back(NewGA);
3689 AliasInits.push_back(std::make_pair(NewGA, Val));
3692 /// MODULE_CODE_PURGEVALS: [numvals]
3693 case bitc::MODULE_CODE_PURGEVALS:
3694 // Trim down the value list to the specified size.
3695 if (Record.size() < 1 || Record[0] > ValueList.size())
3696 return error("Invalid record");
3697 ValueList.shrinkTo(Record[0]);
3699 /// MODULE_CODE_VSTOFFSET: [offset]
3700 case bitc::MODULE_CODE_VSTOFFSET:
3701 if (Record.size() < 1)
3702 return error("Invalid record");
3703 VSTOffset = Record[0];
3705 /// MODULE_CODE_METADATA_VALUES: [numvals]
3706 case bitc::MODULE_CODE_METADATA_VALUES:
3707 if (Record.size() < 1)
3708 return error("Invalid record");
3709 assert(!IsMetadataMaterialized);
3710 // This record contains the number of metadata values in the module-level
3711 // METADATA_BLOCK. It is used to support lazy parsing of metadata as
3712 // a postpass, where we will parse function-level metadata first.
3713 // This is needed because the ids of metadata are assigned implicitly
3714 // based on their ordering in the bitcode, with the function-level
3715 // metadata ids starting after the module-level metadata ids. Otherwise,
3716 // we would have to parse the module-level metadata block to prime the
3717 // MDValueList when we are lazy loading metadata during function
3718 // importing. Initialize the MDValueList size here based on the
3719 // record value, regardless of whether we are doing lazy metadata
3720 // loading, so that we have consistent handling and assertion
3721 // checking in parseMetadata for module-level metadata.
3722 NumModuleMDs = Record[0];
3723 SeenModuleValuesRecord = true;
3724 assert(MDValueList.size() == 0);
3725 MDValueList.resize(NumModuleMDs);
3732 /// Helper to read the header common to all bitcode files.
3733 static bool hasValidBitcodeHeader(BitstreamCursor &Stream) {
3734 // Sniff for the signature.
3735 if (Stream.Read(8) != 'B' ||
3736 Stream.Read(8) != 'C' ||
3737 Stream.Read(4) != 0x0 ||
3738 Stream.Read(4) != 0xC ||
3739 Stream.Read(4) != 0xE ||
3740 Stream.Read(4) != 0xD)
3746 BitcodeReader::parseBitcodeInto(std::unique_ptr<DataStreamer> Streamer,
3747 Module *M, bool ShouldLazyLoadMetadata) {
3750 if (std::error_code EC = initStream(std::move(Streamer)))
3753 // Sniff for the signature.
3754 if (!hasValidBitcodeHeader(Stream))
3755 return error("Invalid bitcode signature");
3757 // We expect a number of well-defined blocks, though we don't necessarily
3758 // need to understand them all.
3760 if (Stream.AtEndOfStream()) {
3761 // We didn't really read a proper Module.
3762 return error("Malformed IR file");
3765 BitstreamEntry Entry =
3766 Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs);
3768 if (Entry.Kind != BitstreamEntry::SubBlock)
3769 return error("Malformed block");
3771 if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID) {
3772 parseBitcodeVersion();
3776 if (Entry.ID == bitc::MODULE_BLOCK_ID)
3777 return parseModule(0, ShouldLazyLoadMetadata);
3779 if (Stream.SkipBlock())
3780 return error("Invalid record");
3784 ErrorOr<std::string> BitcodeReader::parseModuleTriple() {
3785 if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
3786 return error("Invalid record");
3788 SmallVector<uint64_t, 64> Record;
3791 // Read all the records for this module.
3793 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
3795 switch (Entry.Kind) {
3796 case BitstreamEntry::SubBlock: // Handled for us already.
3797 case BitstreamEntry::Error:
3798 return error("Malformed block");
3799 case BitstreamEntry::EndBlock:
3801 case BitstreamEntry::Record:
3802 // The interesting case.
3807 switch (Stream.readRecord(Entry.ID, Record)) {
3808 default: break; // Default behavior, ignore unknown content.
3809 case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N]
3811 if (convertToString(Record, 0, S))
3812 return error("Invalid record");
3819 llvm_unreachable("Exit infinite loop");
3822 ErrorOr<std::string> BitcodeReader::parseTriple() {
3823 if (std::error_code EC = initStream(nullptr))
3826 // Sniff for the signature.
3827 if (!hasValidBitcodeHeader(Stream))
3828 return error("Invalid bitcode signature");
3830 // We expect a number of well-defined blocks, though we don't necessarily
3831 // need to understand them all.
3833 BitstreamEntry Entry = Stream.advance();
3835 switch (Entry.Kind) {
3836 case BitstreamEntry::Error:
3837 return error("Malformed block");
3838 case BitstreamEntry::EndBlock:
3839 return std::error_code();
3841 case BitstreamEntry::SubBlock:
3842 if (Entry.ID == bitc::MODULE_BLOCK_ID)
3843 return parseModuleTriple();
3845 // Ignore other sub-blocks.
3846 if (Stream.SkipBlock())
3847 return error("Malformed block");
3850 case BitstreamEntry::Record:
3851 Stream.skipRecord(Entry.ID);
3857 ErrorOr<std::string> BitcodeReader::parseIdentificationBlock() {
3858 if (std::error_code EC = initStream(nullptr))
3861 // Sniff for the signature.
3862 if (!hasValidBitcodeHeader(Stream))
3863 return error("Invalid bitcode signature");
3865 // We expect a number of well-defined blocks, though we don't necessarily
3866 // need to understand them all.
3868 BitstreamEntry Entry = Stream.advance();
3869 switch (Entry.Kind) {
3870 case BitstreamEntry::Error:
3871 return error("Malformed block");
3872 case BitstreamEntry::EndBlock:
3873 return std::error_code();
3875 case BitstreamEntry::SubBlock:
3876 if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID) {
3877 if (std::error_code EC = parseBitcodeVersion())
3879 return ProducerIdentification;
3881 // Ignore other sub-blocks.
3882 if (Stream.SkipBlock())
3883 return error("Malformed block");
3885 case BitstreamEntry::Record:
3886 Stream.skipRecord(Entry.ID);
3892 /// Parse metadata attachments.
3893 std::error_code BitcodeReader::parseMetadataAttachment(Function &F) {
3894 if (Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID))
3895 return error("Invalid record");
3897 SmallVector<uint64_t, 64> Record;
3899 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
3901 switch (Entry.Kind) {
3902 case BitstreamEntry::SubBlock: // Handled for us already.
3903 case BitstreamEntry::Error:
3904 return error("Malformed block");
3905 case BitstreamEntry::EndBlock:
3906 return std::error_code();
3907 case BitstreamEntry::Record:
3908 // The interesting case.
3912 // Read a metadata attachment record.
3914 switch (Stream.readRecord(Entry.ID, Record)) {
3915 default: // Default behavior: ignore.
3917 case bitc::METADATA_ATTACHMENT: {
3918 unsigned RecordLength = Record.size();
3920 return error("Invalid record");
3921 if (RecordLength % 2 == 0) {
3922 // A function attachment.
3923 for (unsigned I = 0; I != RecordLength; I += 2) {
3924 auto K = MDKindMap.find(Record[I]);
3925 if (K == MDKindMap.end())
3926 return error("Invalid ID");
3927 Metadata *MD = MDValueList.getValueFwdRef(Record[I + 1]);
3928 F.setMetadata(K->second, cast<MDNode>(MD));
3933 // An instruction attachment.
3934 Instruction *Inst = InstructionList[Record[0]];
3935 for (unsigned i = 1; i != RecordLength; i = i+2) {
3936 unsigned Kind = Record[i];
3937 DenseMap<unsigned, unsigned>::iterator I =
3938 MDKindMap.find(Kind);
3939 if (I == MDKindMap.end())
3940 return error("Invalid ID");
3941 Metadata *Node = MDValueList.getValueFwdRef(Record[i + 1]);
3942 if (isa<LocalAsMetadata>(Node))
3943 // Drop the attachment. This used to be legal, but there's no
3946 Inst->setMetadata(I->second, cast<MDNode>(Node));
3947 if (I->second == LLVMContext::MD_tbaa)
3948 InstsWithTBAATag.push_back(Inst);
3956 static std::error_code typeCheckLoadStoreInst(DiagnosticHandlerFunction DH,
3957 Type *ValType, Type *PtrType) {
3958 if (!isa<PointerType>(PtrType))
3959 return error(DH, "Load/Store operand is not a pointer type");
3960 Type *ElemType = cast<PointerType>(PtrType)->getElementType();
3962 if (ValType && ValType != ElemType)
3963 return error(DH, "Explicit load/store type does not match pointee type of "
3965 if (!PointerType::isLoadableOrStorableType(ElemType))
3966 return error(DH, "Cannot load/store from pointer");
3967 return std::error_code();
3970 /// Lazily parse the specified function body block.
3971 std::error_code BitcodeReader::parseFunctionBody(Function *F) {
3972 if (Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID))
3973 return error("Invalid record");
3975 InstructionList.clear();
3976 unsigned ModuleValueListSize = ValueList.size();
3977 unsigned ModuleMDValueListSize = MDValueList.size();
3979 // Add all the function arguments to the value table.
3980 for (Argument &I : F->args())
3981 ValueList.push_back(&I);
3983 unsigned NextValueNo = ValueList.size();
3984 BasicBlock *CurBB = nullptr;
3985 unsigned CurBBNo = 0;
3988 auto getLastInstruction = [&]() -> Instruction * {
3989 if (CurBB && !CurBB->empty())
3990 return &CurBB->back();
3991 else if (CurBBNo && FunctionBBs[CurBBNo - 1] &&
3992 !FunctionBBs[CurBBNo - 1]->empty())
3993 return &FunctionBBs[CurBBNo - 1]->back();
3997 std::vector<OperandBundleDef> OperandBundles;
3999 // Read all the records.
4000 SmallVector<uint64_t, 64> Record;
4002 BitstreamEntry Entry = Stream.advance();
4004 switch (Entry.Kind) {
4005 case BitstreamEntry::Error:
4006 return error("Malformed block");
4007 case BitstreamEntry::EndBlock:
4008 goto OutOfRecordLoop;
4010 case BitstreamEntry::SubBlock:
4012 default: // Skip unknown content.
4013 if (Stream.SkipBlock())
4014 return error("Invalid record");
4016 case bitc::CONSTANTS_BLOCK_ID:
4017 if (std::error_code EC = parseConstants())
4019 NextValueNo = ValueList.size();
4021 case bitc::VALUE_SYMTAB_BLOCK_ID:
4022 if (std::error_code EC = parseValueSymbolTable())
4025 case bitc::METADATA_ATTACHMENT_ID:
4026 if (std::error_code EC = parseMetadataAttachment(*F))
4029 case bitc::METADATA_BLOCK_ID:
4030 if (std::error_code EC = parseMetadata())
4033 case bitc::USELIST_BLOCK_ID:
4034 if (std::error_code EC = parseUseLists())
4040 case BitstreamEntry::Record:
4041 // The interesting case.
4047 Instruction *I = nullptr;
4048 unsigned BitCode = Stream.readRecord(Entry.ID, Record);
4050 default: // Default behavior: reject
4051 return error("Invalid value");
4052 case bitc::FUNC_CODE_DECLAREBLOCKS: { // DECLAREBLOCKS: [nblocks]
4053 if (Record.size() < 1 || Record[0] == 0)
4054 return error("Invalid record");
4055 // Create all the basic blocks for the function.
4056 FunctionBBs.resize(Record[0]);
4058 // See if anything took the address of blocks in this function.
4059 auto BBFRI = BasicBlockFwdRefs.find(F);
4060 if (BBFRI == BasicBlockFwdRefs.end()) {
4061 for (unsigned i = 0, e = FunctionBBs.size(); i != e; ++i)
4062 FunctionBBs[i] = BasicBlock::Create(Context, "", F);
4064 auto &BBRefs = BBFRI->second;
4065 // Check for invalid basic block references.
4066 if (BBRefs.size() > FunctionBBs.size())
4067 return error("Invalid ID");
4068 assert(!BBRefs.empty() && "Unexpected empty array");
4069 assert(!BBRefs.front() && "Invalid reference to entry block");
4070 for (unsigned I = 0, E = FunctionBBs.size(), RE = BBRefs.size(); I != E;
4072 if (I < RE && BBRefs[I]) {
4073 BBRefs[I]->insertInto(F);
4074 FunctionBBs[I] = BBRefs[I];
4076 FunctionBBs[I] = BasicBlock::Create(Context, "", F);
4079 // Erase from the table.
4080 BasicBlockFwdRefs.erase(BBFRI);
4083 CurBB = FunctionBBs[0];
4087 case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: // DEBUG_LOC_AGAIN
4088 // This record indicates that the last instruction is at the same
4089 // location as the previous instruction with a location.
4090 I = getLastInstruction();
4093 return error("Invalid record");
4094 I->setDebugLoc(LastLoc);
4098 case bitc::FUNC_CODE_DEBUG_LOC: { // DEBUG_LOC: [line, col, scope, ia]
4099 I = getLastInstruction();
4100 if (!I || Record.size() < 4)
4101 return error("Invalid record");
4103 unsigned Line = Record[0], Col = Record[1];
4104 unsigned ScopeID = Record[2], IAID = Record[3];
4106 MDNode *Scope = nullptr, *IA = nullptr;
4107 if (ScopeID) Scope = cast<MDNode>(MDValueList.getValueFwdRef(ScopeID-1));
4108 if (IAID) IA = cast<MDNode>(MDValueList.getValueFwdRef(IAID-1));
4109 LastLoc = DebugLoc::get(Line, Col, Scope, IA);
4110 I->setDebugLoc(LastLoc);
4115 case bitc::FUNC_CODE_INST_BINOP: { // BINOP: [opval, ty, opval, opcode]
4118 if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
4119 popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) ||
4120 OpNum+1 > Record.size())
4121 return error("Invalid record");
4123 int Opc = getDecodedBinaryOpcode(Record[OpNum++], LHS->getType());
4125 return error("Invalid record");
4126 I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
4127 InstructionList.push_back(I);
4128 if (OpNum < Record.size()) {
4129 if (Opc == Instruction::Add ||
4130 Opc == Instruction::Sub ||
4131 Opc == Instruction::Mul ||
4132 Opc == Instruction::Shl) {
4133 if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP))
4134 cast<BinaryOperator>(I)->setHasNoSignedWrap(true);
4135 if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
4136 cast<BinaryOperator>(I)->setHasNoUnsignedWrap(true);
4137 } else if (Opc == Instruction::SDiv ||
4138 Opc == Instruction::UDiv ||
4139 Opc == Instruction::LShr ||
4140 Opc == Instruction::AShr) {
4141 if (Record[OpNum] & (1 << bitc::PEO_EXACT))
4142 cast<BinaryOperator>(I)->setIsExact(true);
4143 } else if (isa<FPMathOperator>(I)) {
4144 FastMathFlags FMF = getDecodedFastMathFlags(Record[OpNum]);
4146 I->setFastMathFlags(FMF);
4152 case bitc::FUNC_CODE_INST_CAST: { // CAST: [opval, opty, destty, castopc]
4155 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
4156 OpNum+2 != Record.size())
4157 return error("Invalid record");
4159 Type *ResTy = getTypeByID(Record[OpNum]);
4160 int Opc = getDecodedCastOpcode(Record[OpNum + 1]);
4161 if (Opc == -1 || !ResTy)
4162 return error("Invalid record");
4163 Instruction *Temp = nullptr;
4164 if ((I = UpgradeBitCastInst(Opc, Op, ResTy, Temp))) {
4166 InstructionList.push_back(Temp);
4167 CurBB->getInstList().push_back(Temp);
4170 auto CastOp = (Instruction::CastOps)Opc;
4171 if (!CastInst::castIsValid(CastOp, Op, ResTy))
4172 return error("Invalid cast");
4173 I = CastInst::Create(CastOp, Op, ResTy);
4175 InstructionList.push_back(I);
4178 case bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD:
4179 case bitc::FUNC_CODE_INST_GEP_OLD:
4180 case bitc::FUNC_CODE_INST_GEP: { // GEP: type, [n x operands]
4186 if (BitCode == bitc::FUNC_CODE_INST_GEP) {
4187 InBounds = Record[OpNum++];
4188 Ty = getTypeByID(Record[OpNum++]);
4190 InBounds = BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD;
4195 if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr))
4196 return error("Invalid record");
4199 Ty = cast<SequentialType>(BasePtr->getType()->getScalarType())
4202 cast<SequentialType>(BasePtr->getType()->getScalarType())
4205 "Explicit gep type does not match pointee type of pointer operand");
4207 SmallVector<Value*, 16> GEPIdx;
4208 while (OpNum != Record.size()) {
4210 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
4211 return error("Invalid record");
4212 GEPIdx.push_back(Op);
4215 I = GetElementPtrInst::Create(Ty, BasePtr, GEPIdx);
4217 InstructionList.push_back(I);
4219 cast<GetElementPtrInst>(I)->setIsInBounds(true);
4223 case bitc::FUNC_CODE_INST_EXTRACTVAL: {
4224 // EXTRACTVAL: [opty, opval, n x indices]
4227 if (getValueTypePair(Record, OpNum, NextValueNo, Agg))
4228 return error("Invalid record");
4230 unsigned RecSize = Record.size();
4231 if (OpNum == RecSize)
4232 return error("EXTRACTVAL: Invalid instruction with 0 indices");
4234 SmallVector<unsigned, 4> EXTRACTVALIdx;
4235 Type *CurTy = Agg->getType();
4236 for (; OpNum != RecSize; ++OpNum) {
4237 bool IsArray = CurTy->isArrayTy();
4238 bool IsStruct = CurTy->isStructTy();
4239 uint64_t Index = Record[OpNum];
4241 if (!IsStruct && !IsArray)
4242 return error("EXTRACTVAL: Invalid type");
4243 if ((unsigned)Index != Index)
4244 return error("Invalid value");
4245 if (IsStruct && Index >= CurTy->subtypes().size())
4246 return error("EXTRACTVAL: Invalid struct index");
4247 if (IsArray && Index >= CurTy->getArrayNumElements())
4248 return error("EXTRACTVAL: Invalid array index");
4249 EXTRACTVALIdx.push_back((unsigned)Index);
4252 CurTy = CurTy->subtypes()[Index];
4254 CurTy = CurTy->subtypes()[0];
4257 I = ExtractValueInst::Create(Agg, EXTRACTVALIdx);
4258 InstructionList.push_back(I);
4262 case bitc::FUNC_CODE_INST_INSERTVAL: {
4263 // INSERTVAL: [opty, opval, opty, opval, n x indices]
4266 if (getValueTypePair(Record, OpNum, NextValueNo, Agg))
4267 return error("Invalid record");
4269 if (getValueTypePair(Record, OpNum, NextValueNo, Val))
4270 return error("Invalid record");
4272 unsigned RecSize = Record.size();
4273 if (OpNum == RecSize)
4274 return error("INSERTVAL: Invalid instruction with 0 indices");
4276 SmallVector<unsigned, 4> INSERTVALIdx;
4277 Type *CurTy = Agg->getType();
4278 for (; OpNum != RecSize; ++OpNum) {
4279 bool IsArray = CurTy->isArrayTy();
4280 bool IsStruct = CurTy->isStructTy();
4281 uint64_t Index = Record[OpNum];
4283 if (!IsStruct && !IsArray)
4284 return error("INSERTVAL: Invalid type");
4285 if ((unsigned)Index != Index)
4286 return error("Invalid value");
4287 if (IsStruct && Index >= CurTy->subtypes().size())
4288 return error("INSERTVAL: Invalid struct index");
4289 if (IsArray && Index >= CurTy->getArrayNumElements())
4290 return error("INSERTVAL: Invalid array index");
4292 INSERTVALIdx.push_back((unsigned)Index);
4294 CurTy = CurTy->subtypes()[Index];
4296 CurTy = CurTy->subtypes()[0];
4299 if (CurTy != Val->getType())
4300 return error("Inserted value type doesn't match aggregate type");
4302 I = InsertValueInst::Create(Agg, Val, INSERTVALIdx);
4303 InstructionList.push_back(I);
4307 case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval]
4308 // obsolete form of select
4309 // handles select i1 ... in old bitcode
4311 Value *TrueVal, *FalseVal, *Cond;
4312 if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) ||
4313 popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) ||
4314 popValue(Record, OpNum, NextValueNo, Type::getInt1Ty(Context), Cond))
4315 return error("Invalid record");
4317 I = SelectInst::Create(Cond, TrueVal, FalseVal);
4318 InstructionList.push_back(I);
4322 case bitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [ty,opval,opval,predty,pred]
4323 // new form of select
4324 // handles select i1 or select [N x i1]
4326 Value *TrueVal, *FalseVal, *Cond;
4327 if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) ||
4328 popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) ||
4329 getValueTypePair(Record, OpNum, NextValueNo, Cond))
4330 return error("Invalid record");
4332 // select condition can be either i1 or [N x i1]
4333 if (VectorType* vector_type =
4334 dyn_cast<VectorType>(Cond->getType())) {
4336 if (vector_type->getElementType() != Type::getInt1Ty(Context))
4337 return error("Invalid type for value");
4340 if (Cond->getType() != Type::getInt1Ty(Context))
4341 return error("Invalid type for value");
4344 I = SelectInst::Create(Cond, TrueVal, FalseVal);
4345 InstructionList.push_back(I);
4349 case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval]
4352 if (getValueTypePair(Record, OpNum, NextValueNo, Vec) ||
4353 getValueTypePair(Record, OpNum, NextValueNo, Idx))
4354 return error("Invalid record");
4355 if (!Vec->getType()->isVectorTy())
4356 return error("Invalid type for value");
4357 I = ExtractElementInst::Create(Vec, Idx);
4358 InstructionList.push_back(I);
4362 case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval]
4364 Value *Vec, *Elt, *Idx;
4365 if (getValueTypePair(Record, OpNum, NextValueNo, Vec))
4366 return error("Invalid record");
4367 if (!Vec->getType()->isVectorTy())
4368 return error("Invalid type for value");
4369 if (popValue(Record, OpNum, NextValueNo,
4370 cast<VectorType>(Vec->getType())->getElementType(), Elt) ||
4371 getValueTypePair(Record, OpNum, NextValueNo, Idx))
4372 return error("Invalid record");
4373 I = InsertElementInst::Create(Vec, Elt, Idx);
4374 InstructionList.push_back(I);
4378 case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval]
4380 Value *Vec1, *Vec2, *Mask;
4381 if (getValueTypePair(Record, OpNum, NextValueNo, Vec1) ||
4382 popValue(Record, OpNum, NextValueNo, Vec1->getType(), Vec2))
4383 return error("Invalid record");
4385 if (getValueTypePair(Record, OpNum, NextValueNo, Mask))
4386 return error("Invalid record");
4387 if (!Vec1->getType()->isVectorTy() || !Vec2->getType()->isVectorTy())
4388 return error("Invalid type for value");
4389 I = new ShuffleVectorInst(Vec1, Vec2, Mask);
4390 InstructionList.push_back(I);
4394 case bitc::FUNC_CODE_INST_CMP: // CMP: [opty, opval, opval, pred]
4395 // Old form of ICmp/FCmp returning bool
4396 // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were
4397 // both legal on vectors but had different behaviour.
4398 case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred]
4399 // FCmp/ICmp returning bool or vector of bool
4403 if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
4404 popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS))
4405 return error("Invalid record");
4407 unsigned PredVal = Record[OpNum];
4408 bool IsFP = LHS->getType()->isFPOrFPVectorTy();
4410 if (IsFP && Record.size() > OpNum+1)
4411 FMF = getDecodedFastMathFlags(Record[++OpNum]);
4413 if (OpNum+1 != Record.size())
4414 return error("Invalid record");
4416 if (LHS->getType()->isFPOrFPVectorTy())
4417 I = new FCmpInst((FCmpInst::Predicate)PredVal, LHS, RHS);
4419 I = new ICmpInst((ICmpInst::Predicate)PredVal, LHS, RHS);
4422 I->setFastMathFlags(FMF);
4423 InstructionList.push_back(I);
4427 case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>]
4429 unsigned Size = Record.size();
4431 I = ReturnInst::Create(Context);
4432 InstructionList.push_back(I);
4437 Value *Op = nullptr;
4438 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
4439 return error("Invalid record");
4440 if (OpNum != Record.size())
4441 return error("Invalid record");
4443 I = ReturnInst::Create(Context, Op);
4444 InstructionList.push_back(I);
4447 case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#]
4448 if (Record.size() != 1 && Record.size() != 3)
4449 return error("Invalid record");
4450 BasicBlock *TrueDest = getBasicBlock(Record[0]);
4452 return error("Invalid record");
4454 if (Record.size() == 1) {
4455 I = BranchInst::Create(TrueDest);
4456 InstructionList.push_back(I);
4459 BasicBlock *FalseDest = getBasicBlock(Record[1]);
4460 Value *Cond = getValue(Record, 2, NextValueNo,
4461 Type::getInt1Ty(Context));
4462 if (!FalseDest || !Cond)
4463 return error("Invalid record");
4464 I = BranchInst::Create(TrueDest, FalseDest, Cond);
4465 InstructionList.push_back(I);
4469 case bitc::FUNC_CODE_INST_CLEANUPRET: { // CLEANUPRET: [val] or [val,bb#]
4470 if (Record.size() != 1 && Record.size() != 2)
4471 return error("Invalid record");
4473 Value *CleanupPad = getValue(Record, Idx++, NextValueNo,
4474 Type::getTokenTy(Context), OC_CleanupPad);
4476 return error("Invalid record");
4477 BasicBlock *UnwindDest = nullptr;
4478 if (Record.size() == 2) {
4479 UnwindDest = getBasicBlock(Record[Idx++]);
4481 return error("Invalid record");
4484 I = CleanupReturnInst::Create(cast<CleanupPadInst>(CleanupPad),
4486 InstructionList.push_back(I);
4489 case bitc::FUNC_CODE_INST_CATCHRET: { // CATCHRET: [val,bb#]
4490 if (Record.size() != 2)
4491 return error("Invalid record");
4493 Value *CatchPad = getValue(Record, Idx++, NextValueNo,
4494 Type::getTokenTy(Context), OC_CatchPad);
4496 return error("Invalid record");
4497 BasicBlock *BB = getBasicBlock(Record[Idx++]);
4499 return error("Invalid record");
4501 I = CatchReturnInst::Create(cast<CatchPadInst>(CatchPad), BB);
4502 InstructionList.push_back(I);
4505 case bitc::FUNC_CODE_INST_CATCHPAD: { // CATCHPAD: [bb#,bb#,num,(ty,val)*]
4506 if (Record.size() < 3)
4507 return error("Invalid record");
4509 BasicBlock *NormalBB = getBasicBlock(Record[Idx++]);
4511 return error("Invalid record");
4512 BasicBlock *UnwindBB = getBasicBlock(Record[Idx++]);
4514 return error("Invalid record");
4515 unsigned NumArgOperands = Record[Idx++];
4516 SmallVector<Value *, 2> Args;
4517 for (unsigned Op = 0; Op != NumArgOperands; ++Op) {
4519 if (getValueTypePair(Record, Idx, NextValueNo, Val))
4520 return error("Invalid record");
4521 Args.push_back(Val);
4523 if (Record.size() != Idx)
4524 return error("Invalid record");
4526 I = CatchPadInst::Create(NormalBB, UnwindBB, Args);
4527 InstructionList.push_back(I);
4530 case bitc::FUNC_CODE_INST_TERMINATEPAD: { // TERMINATEPAD: [bb#,num,(ty,val)*]
4531 if (Record.size() < 1)
4532 return error("Invalid record");
4534 bool HasUnwindDest = !!Record[Idx++];
4535 BasicBlock *UnwindDest = nullptr;
4536 if (HasUnwindDest) {
4537 if (Idx == Record.size())
4538 return error("Invalid record");
4539 UnwindDest = getBasicBlock(Record[Idx++]);
4541 return error("Invalid record");
4543 unsigned NumArgOperands = Record[Idx++];
4544 SmallVector<Value *, 2> Args;
4545 for (unsigned Op = 0; Op != NumArgOperands; ++Op) {
4547 if (getValueTypePair(Record, Idx, NextValueNo, Val))
4548 return error("Invalid record");
4549 Args.push_back(Val);
4551 if (Record.size() != Idx)
4552 return error("Invalid record");
4554 I = TerminatePadInst::Create(Context, UnwindDest, Args);
4555 InstructionList.push_back(I);
4558 case bitc::FUNC_CODE_INST_CLEANUPPAD: { // CLEANUPPAD: [num,(ty,val)*]
4559 if (Record.size() < 1)
4560 return error("Invalid record");
4562 unsigned NumArgOperands = Record[Idx++];
4563 SmallVector<Value *, 2> Args;
4564 for (unsigned Op = 0; Op != NumArgOperands; ++Op) {
4566 if (getValueTypePair(Record, Idx, NextValueNo, Val))
4567 return error("Invalid record");
4568 Args.push_back(Val);
4570 if (Record.size() != Idx)
4571 return error("Invalid record");
4573 I = CleanupPadInst::Create(Context, Args);
4574 InstructionList.push_back(I);
4577 case bitc::FUNC_CODE_INST_CATCHENDPAD: { // CATCHENDPADINST: [bb#] or []
4578 if (Record.size() > 1)
4579 return error("Invalid record");
4580 BasicBlock *BB = nullptr;
4581 if (Record.size() == 1) {
4582 BB = getBasicBlock(Record[0]);
4584 return error("Invalid record");
4586 I = CatchEndPadInst::Create(Context, BB);
4587 InstructionList.push_back(I);
4590 case bitc::FUNC_CODE_INST_CLEANUPENDPAD: { // CLEANUPENDPADINST: [val] or [val,bb#]
4591 if (Record.size() != 1 && Record.size() != 2)
4592 return error("Invalid record");
4594 Value *CleanupPad = getValue(Record, Idx++, NextValueNo,
4595 Type::getTokenTy(Context), OC_CleanupPad);
4597 return error("Invalid record");
4599 BasicBlock *BB = nullptr;
4600 if (Record.size() == 2) {
4601 BB = getBasicBlock(Record[Idx++]);
4603 return error("Invalid record");
4605 I = CleanupEndPadInst::Create(cast<CleanupPadInst>(CleanupPad), BB);
4606 InstructionList.push_back(I);
4609 case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...]
4611 if ((Record[0] >> 16) == SWITCH_INST_MAGIC) {
4612 // "New" SwitchInst format with case ranges. The changes to write this
4613 // format were reverted but we still recognize bitcode that uses it.
4614 // Hopefully someday we will have support for case ranges and can use
4615 // this format again.
4617 Type *OpTy = getTypeByID(Record[1]);
4618 unsigned ValueBitWidth = cast<IntegerType>(OpTy)->getBitWidth();
4620 Value *Cond = getValue(Record, 2, NextValueNo, OpTy);
4621 BasicBlock *Default = getBasicBlock(Record[3]);
4622 if (!OpTy || !Cond || !Default)
4623 return error("Invalid record");
4625 unsigned NumCases = Record[4];
4627 SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
4628 InstructionList.push_back(SI);
4630 unsigned CurIdx = 5;
4631 for (unsigned i = 0; i != NumCases; ++i) {
4632 SmallVector<ConstantInt*, 1> CaseVals;
4633 unsigned NumItems = Record[CurIdx++];
4634 for (unsigned ci = 0; ci != NumItems; ++ci) {
4635 bool isSingleNumber = Record[CurIdx++];
4638 unsigned ActiveWords = 1;
4639 if (ValueBitWidth > 64)
4640 ActiveWords = Record[CurIdx++];
4641 Low = readWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords),
4643 CurIdx += ActiveWords;
4645 if (!isSingleNumber) {
4647 if (ValueBitWidth > 64)
4648 ActiveWords = Record[CurIdx++];
4649 APInt High = readWideAPInt(
4650 makeArrayRef(&Record[CurIdx], ActiveWords), ValueBitWidth);
4651 CurIdx += ActiveWords;
4653 // FIXME: It is not clear whether values in the range should be
4654 // compared as signed or unsigned values. The partially
4655 // implemented changes that used this format in the past used
4656 // unsigned comparisons.
4657 for ( ; Low.ule(High); ++Low)
4658 CaseVals.push_back(ConstantInt::get(Context, Low));
4660 CaseVals.push_back(ConstantInt::get(Context, Low));
4662 BasicBlock *DestBB = getBasicBlock(Record[CurIdx++]);
4663 for (SmallVector<ConstantInt*, 1>::iterator cvi = CaseVals.begin(),
4664 cve = CaseVals.end(); cvi != cve; ++cvi)
4665 SI->addCase(*cvi, DestBB);
4671 // Old SwitchInst format without case ranges.
4673 if (Record.size() < 3 || (Record.size() & 1) == 0)
4674 return error("Invalid record");
4675 Type *OpTy = getTypeByID(Record[0]);
4676 Value *Cond = getValue(Record, 1, NextValueNo, OpTy);
4677 BasicBlock *Default = getBasicBlock(Record[2]);
4678 if (!OpTy || !Cond || !Default)
4679 return error("Invalid record");
4680 unsigned NumCases = (Record.size()-3)/2;
4681 SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
4682 InstructionList.push_back(SI);
4683 for (unsigned i = 0, e = NumCases; i != e; ++i) {
4684 ConstantInt *CaseVal =
4685 dyn_cast_or_null<ConstantInt>(getFnValueByID(Record[3+i*2], OpTy));
4686 BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]);
4687 if (!CaseVal || !DestBB) {
4689 return error("Invalid record");
4691 SI->addCase(CaseVal, DestBB);
4696 case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...]
4697 if (Record.size() < 2)
4698 return error("Invalid record");
4699 Type *OpTy = getTypeByID(Record[0]);
4700 Value *Address = getValue(Record, 1, NextValueNo, OpTy);
4701 if (!OpTy || !Address)
4702 return error("Invalid record");
4703 unsigned NumDests = Record.size()-2;
4704 IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests);
4705 InstructionList.push_back(IBI);
4706 for (unsigned i = 0, e = NumDests; i != e; ++i) {
4707 if (BasicBlock *DestBB = getBasicBlock(Record[2+i])) {
4708 IBI->addDestination(DestBB);
4711 return error("Invalid record");
4718 case bitc::FUNC_CODE_INST_INVOKE: {
4719 // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...]
4720 if (Record.size() < 4)
4721 return error("Invalid record");
4723 AttributeSet PAL = getAttributes(Record[OpNum++]);
4724 unsigned CCInfo = Record[OpNum++];
4725 BasicBlock *NormalBB = getBasicBlock(Record[OpNum++]);
4726 BasicBlock *UnwindBB = getBasicBlock(Record[OpNum++]);
4728 FunctionType *FTy = nullptr;
4729 if (CCInfo >> 13 & 1 &&
4730 !(FTy = dyn_cast<FunctionType>(getTypeByID(Record[OpNum++]))))
4731 return error("Explicit invoke type is not a function type");
4734 if (getValueTypePair(Record, OpNum, NextValueNo, Callee))
4735 return error("Invalid record");
4737 PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType());
4739 return error("Callee is not a pointer");
4741 FTy = dyn_cast<FunctionType>(CalleeTy->getElementType());
4743 return error("Callee is not of pointer to function type");
4744 } else if (CalleeTy->getElementType() != FTy)
4745 return error("Explicit invoke type does not match pointee type of "
4747 if (Record.size() < FTy->getNumParams() + OpNum)
4748 return error("Insufficient operands to call");
4750 SmallVector<Value*, 16> Ops;
4751 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
4752 Ops.push_back(getValue(Record, OpNum, NextValueNo,
4753 FTy->getParamType(i)));
4755 return error("Invalid record");
4758 if (!FTy->isVarArg()) {
4759 if (Record.size() != OpNum)
4760 return error("Invalid record");
4762 // Read type/value pairs for varargs params.
4763 while (OpNum != Record.size()) {
4765 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
4766 return error("Invalid record");
4771 I = InvokeInst::Create(Callee, NormalBB, UnwindBB, Ops, OperandBundles);
4772 OperandBundles.clear();
4773 InstructionList.push_back(I);
4774 cast<InvokeInst>(I)->setCallingConv(
4775 static_cast<CallingConv::ID>(CallingConv::MaxID & CCInfo));
4776 cast<InvokeInst>(I)->setAttributes(PAL);
4779 case bitc::FUNC_CODE_INST_RESUME: { // RESUME: [opval]
4781 Value *Val = nullptr;
4782 if (getValueTypePair(Record, Idx, NextValueNo, Val))
4783 return error("Invalid record");
4784 I = ResumeInst::Create(Val);
4785 InstructionList.push_back(I);
4788 case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE
4789 I = new UnreachableInst(Context);
4790 InstructionList.push_back(I);
4792 case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...]
4793 if (Record.size() < 1 || ((Record.size()-1)&1))
4794 return error("Invalid record");
4795 Type *Ty = getTypeByID(Record[0]);
4797 return error("Invalid record");
4799 PHINode *PN = PHINode::Create(Ty, (Record.size()-1)/2);
4800 InstructionList.push_back(PN);
4802 for (unsigned i = 0, e = Record.size()-1; i != e; i += 2) {
4804 // With the new function encoding, it is possible that operands have
4805 // negative IDs (for forward references). Use a signed VBR
4806 // representation to keep the encoding small.
4808 V = getValueSigned(Record, 1+i, NextValueNo, Ty);
4810 V = getValue(Record, 1+i, NextValueNo, Ty);
4811 BasicBlock *BB = getBasicBlock(Record[2+i]);
4813 return error("Invalid record");
4814 PN->addIncoming(V, BB);
4820 case bitc::FUNC_CODE_INST_LANDINGPAD:
4821 case bitc::FUNC_CODE_INST_LANDINGPAD_OLD: {
4822 // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?]
4824 if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD) {
4825 if (Record.size() < 3)
4826 return error("Invalid record");
4828 assert(BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD);
4829 if (Record.size() < 4)
4830 return error("Invalid record");
4832 Type *Ty = getTypeByID(Record[Idx++]);
4834 return error("Invalid record");
4835 if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD) {
4836 Value *PersFn = nullptr;
4837 if (getValueTypePair(Record, Idx, NextValueNo, PersFn))
4838 return error("Invalid record");
4840 if (!F->hasPersonalityFn())
4841 F->setPersonalityFn(cast<Constant>(PersFn));
4842 else if (F->getPersonalityFn() != cast<Constant>(PersFn))
4843 return error("Personality function mismatch");
4846 bool IsCleanup = !!Record[Idx++];
4847 unsigned NumClauses = Record[Idx++];
4848 LandingPadInst *LP = LandingPadInst::Create(Ty, NumClauses);
4849 LP->setCleanup(IsCleanup);
4850 for (unsigned J = 0; J != NumClauses; ++J) {
4851 LandingPadInst::ClauseType CT =
4852 LandingPadInst::ClauseType(Record[Idx++]); (void)CT;
4855 if (getValueTypePair(Record, Idx, NextValueNo, Val)) {
4857 return error("Invalid record");
4860 assert((CT != LandingPadInst::Catch ||
4861 !isa<ArrayType>(Val->getType())) &&
4862 "Catch clause has a invalid type!");
4863 assert((CT != LandingPadInst::Filter ||
4864 isa<ArrayType>(Val->getType())) &&
4865 "Filter clause has invalid type!");
4866 LP->addClause(cast<Constant>(Val));
4870 InstructionList.push_back(I);
4874 case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align]
4875 if (Record.size() != 4)
4876 return error("Invalid record");
4877 uint64_t AlignRecord = Record[3];
4878 const uint64_t InAllocaMask = uint64_t(1) << 5;
4879 const uint64_t ExplicitTypeMask = uint64_t(1) << 6;
4880 // Reserve bit 7 for SwiftError flag.
4881 // const uint64_t SwiftErrorMask = uint64_t(1) << 7;
4882 const uint64_t FlagMask = InAllocaMask | ExplicitTypeMask;
4883 bool InAlloca = AlignRecord & InAllocaMask;
4884 Type *Ty = getTypeByID(Record[0]);
4885 if ((AlignRecord & ExplicitTypeMask) == 0) {
4886 auto *PTy = dyn_cast_or_null<PointerType>(Ty);
4888 return error("Old-style alloca with a non-pointer type");
4889 Ty = PTy->getElementType();
4891 Type *OpTy = getTypeByID(Record[1]);
4892 Value *Size = getFnValueByID(Record[2], OpTy);
4894 if (std::error_code EC =
4895 parseAlignmentValue(AlignRecord & ~FlagMask, Align)) {
4899 return error("Invalid record");
4900 AllocaInst *AI = new AllocaInst(Ty, Size, Align);
4901 AI->setUsedWithInAlloca(InAlloca);
4903 InstructionList.push_back(I);
4906 case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol]
4909 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
4910 (OpNum + 2 != Record.size() && OpNum + 3 != Record.size()))
4911 return error("Invalid record");
4914 if (OpNum + 3 == Record.size())
4915 Ty = getTypeByID(Record[OpNum++]);
4916 if (std::error_code EC =
4917 typeCheckLoadStoreInst(DiagnosticHandler, Ty, Op->getType()))
4920 Ty = cast<PointerType>(Op->getType())->getElementType();
4923 if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align))
4925 I = new LoadInst(Ty, Op, "", Record[OpNum + 1], Align);
4927 InstructionList.push_back(I);
4930 case bitc::FUNC_CODE_INST_LOADATOMIC: {
4931 // LOADATOMIC: [opty, op, align, vol, ordering, synchscope]
4934 if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
4935 (OpNum + 4 != Record.size() && OpNum + 5 != Record.size()))
4936 return error("Invalid record");
4939 if (OpNum + 5 == Record.size())
4940 Ty = getTypeByID(Record[OpNum++]);
4941 if (std::error_code EC =
4942 typeCheckLoadStoreInst(DiagnosticHandler, Ty, Op->getType()))
4945 Ty = cast<PointerType>(Op->getType())->getElementType();
4947 AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]);
4948 if (Ordering == NotAtomic || Ordering == Release ||
4949 Ordering == AcquireRelease)
4950 return error("Invalid record");
4951 if (Ordering != NotAtomic && Record[OpNum] == 0)
4952 return error("Invalid record");
4953 SynchronizationScope SynchScope = getDecodedSynchScope(Record[OpNum + 3]);
4956 if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align))
4958 I = new LoadInst(Op, "", Record[OpNum+1], Align, Ordering, SynchScope);
4960 InstructionList.push_back(I);
4963 case bitc::FUNC_CODE_INST_STORE:
4964 case bitc::FUNC_CODE_INST_STORE_OLD: { // STORE2:[ptrty, ptr, val, align, vol]
4967 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
4968 (BitCode == bitc::FUNC_CODE_INST_STORE
4969 ? getValueTypePair(Record, OpNum, NextValueNo, Val)
4970 : popValue(Record, OpNum, NextValueNo,
4971 cast<PointerType>(Ptr->getType())->getElementType(),
4973 OpNum + 2 != Record.size())
4974 return error("Invalid record");
4976 if (std::error_code EC = typeCheckLoadStoreInst(
4977 DiagnosticHandler, Val->getType(), Ptr->getType()))
4980 if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align))
4982 I = new StoreInst(Val, Ptr, Record[OpNum+1], Align);
4983 InstructionList.push_back(I);
4986 case bitc::FUNC_CODE_INST_STOREATOMIC:
4987 case bitc::FUNC_CODE_INST_STOREATOMIC_OLD: {
4988 // STOREATOMIC: [ptrty, ptr, val, align, vol, ordering, synchscope]
4991 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
4992 (BitCode == bitc::FUNC_CODE_INST_STOREATOMIC
4993 ? getValueTypePair(Record, OpNum, NextValueNo, Val)
4994 : popValue(Record, OpNum, NextValueNo,
4995 cast<PointerType>(Ptr->getType())->getElementType(),
4997 OpNum + 4 != Record.size())
4998 return error("Invalid record");
5000 if (std::error_code EC = typeCheckLoadStoreInst(
5001 DiagnosticHandler, Val->getType(), Ptr->getType()))
5003 AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]);
5004 if (Ordering == NotAtomic || Ordering == Acquire ||
5005 Ordering == AcquireRelease)
5006 return error("Invalid record");
5007 SynchronizationScope SynchScope = getDecodedSynchScope(Record[OpNum + 3]);
5008 if (Ordering != NotAtomic && Record[OpNum] == 0)
5009 return error("Invalid record");
5012 if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align))
5014 I = new StoreInst(Val, Ptr, Record[OpNum+1], Align, Ordering, SynchScope);
5015 InstructionList.push_back(I);
5018 case bitc::FUNC_CODE_INST_CMPXCHG_OLD:
5019 case bitc::FUNC_CODE_INST_CMPXCHG: {
5020 // CMPXCHG:[ptrty, ptr, cmp, new, vol, successordering, synchscope,
5021 // failureordering?, isweak?]
5023 Value *Ptr, *Cmp, *New;
5024 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
5025 (BitCode == bitc::FUNC_CODE_INST_CMPXCHG
5026 ? getValueTypePair(Record, OpNum, NextValueNo, Cmp)
5027 : popValue(Record, OpNum, NextValueNo,
5028 cast<PointerType>(Ptr->getType())->getElementType(),
5030 popValue(Record, OpNum, NextValueNo, Cmp->getType(), New) ||
5031 Record.size() < OpNum + 3 || Record.size() > OpNum + 5)
5032 return error("Invalid record");
5033 AtomicOrdering SuccessOrdering = getDecodedOrdering(Record[OpNum + 1]);
5034 if (SuccessOrdering == NotAtomic || SuccessOrdering == Unordered)
5035 return error("Invalid record");
5036 SynchronizationScope SynchScope = getDecodedSynchScope(Record[OpNum + 2]);
5038 if (std::error_code EC = typeCheckLoadStoreInst(
5039 DiagnosticHandler, Cmp->getType(), Ptr->getType()))
5041 AtomicOrdering FailureOrdering;
5042 if (Record.size() < 7)
5044 AtomicCmpXchgInst::getStrongestFailureOrdering(SuccessOrdering);
5046 FailureOrdering = getDecodedOrdering(Record[OpNum + 3]);
5048 I = new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering, FailureOrdering,
5050 cast<AtomicCmpXchgInst>(I)->setVolatile(Record[OpNum]);
5052 if (Record.size() < 8) {
5053 // Before weak cmpxchgs existed, the instruction simply returned the
5054 // value loaded from memory, so bitcode files from that era will be
5055 // expecting the first component of a modern cmpxchg.
5056 CurBB->getInstList().push_back(I);
5057 I = ExtractValueInst::Create(I, 0);
5059 cast<AtomicCmpXchgInst>(I)->setWeak(Record[OpNum+4]);
5062 InstructionList.push_back(I);
5065 case bitc::FUNC_CODE_INST_ATOMICRMW: {
5066 // ATOMICRMW:[ptrty, ptr, val, op, vol, ordering, synchscope]
5069 if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
5070 popValue(Record, OpNum, NextValueNo,
5071 cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
5072 OpNum+4 != Record.size())
5073 return error("Invalid record");
5074 AtomicRMWInst::BinOp Operation = getDecodedRMWOperation(Record[OpNum]);
5075 if (Operation < AtomicRMWInst::FIRST_BINOP ||
5076 Operation > AtomicRMWInst::LAST_BINOP)
5077 return error("Invalid record");
5078 AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]);
5079 if (Ordering == NotAtomic || Ordering == Unordered)
5080 return error("Invalid record");
5081 SynchronizationScope SynchScope = getDecodedSynchScope(Record[OpNum + 3]);
5082 I = new AtomicRMWInst(Operation, Ptr, Val, Ordering, SynchScope);
5083 cast<AtomicRMWInst>(I)->setVolatile(Record[OpNum+1]);
5084 InstructionList.push_back(I);
5087 case bitc::FUNC_CODE_INST_FENCE: { // FENCE:[ordering, synchscope]
5088 if (2 != Record.size())
5089 return error("Invalid record");
5090 AtomicOrdering Ordering = getDecodedOrdering(Record[0]);
5091 if (Ordering == NotAtomic || Ordering == Unordered ||
5092 Ordering == Monotonic)
5093 return error("Invalid record");
5094 SynchronizationScope SynchScope = getDecodedSynchScope(Record[1]);
5095 I = new FenceInst(Context, Ordering, SynchScope);
5096 InstructionList.push_back(I);
5099 case bitc::FUNC_CODE_INST_CALL: {
5100 // CALL: [paramattrs, cc, fnty, fnid, arg0, arg1...]
5101 if (Record.size() < 3)
5102 return error("Invalid record");
5105 AttributeSet PAL = getAttributes(Record[OpNum++]);
5106 unsigned CCInfo = Record[OpNum++];
5108 FunctionType *FTy = nullptr;
5109 if (CCInfo >> bitc::CALL_EXPLICIT_TYPE & 1 &&
5110 !(FTy = dyn_cast<FunctionType>(getTypeByID(Record[OpNum++]))))
5111 return error("Explicit call type is not a function type");
5114 if (getValueTypePair(Record, OpNum, NextValueNo, Callee))
5115 return error("Invalid record");
5117 PointerType *OpTy = dyn_cast<PointerType>(Callee->getType());
5119 return error("Callee is not a pointer type");
5121 FTy = dyn_cast<FunctionType>(OpTy->getElementType());
5123 return error("Callee is not of pointer to function type");
5124 } else if (OpTy->getElementType() != FTy)
5125 return error("Explicit call type does not match pointee type of "
5127 if (Record.size() < FTy->getNumParams() + OpNum)
5128 return error("Insufficient operands to call");
5130 SmallVector<Value*, 16> Args;
5131 // Read the fixed params.
5132 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
5133 if (FTy->getParamType(i)->isLabelTy())
5134 Args.push_back(getBasicBlock(Record[OpNum]));
5136 Args.push_back(getValue(Record, OpNum, NextValueNo,
5137 FTy->getParamType(i)));
5139 return error("Invalid record");
5142 // Read type/value pairs for varargs params.
5143 if (!FTy->isVarArg()) {
5144 if (OpNum != Record.size())
5145 return error("Invalid record");
5147 while (OpNum != Record.size()) {
5149 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
5150 return error("Invalid record");
5155 I = CallInst::Create(FTy, Callee, Args, OperandBundles);
5156 OperandBundles.clear();
5157 InstructionList.push_back(I);
5158 cast<CallInst>(I)->setCallingConv(
5159 static_cast<CallingConv::ID>((0x7ff & CCInfo) >> bitc::CALL_CCONV));
5160 CallInst::TailCallKind TCK = CallInst::TCK_None;
5161 if (CCInfo & 1 << bitc::CALL_TAIL)
5162 TCK = CallInst::TCK_Tail;
5163 if (CCInfo & (1 << bitc::CALL_MUSTTAIL))
5164 TCK = CallInst::TCK_MustTail;
5165 if (CCInfo & (1 << bitc::CALL_NOTAIL))
5166 TCK = CallInst::TCK_NoTail;
5167 cast<CallInst>(I)->setTailCallKind(TCK);
5168 cast<CallInst>(I)->setAttributes(PAL);
5171 case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty]
5172 if (Record.size() < 3)
5173 return error("Invalid record");
5174 Type *OpTy = getTypeByID(Record[0]);
5175 Value *Op = getValue(Record, 1, NextValueNo, OpTy);
5176 Type *ResTy = getTypeByID(Record[2]);
5177 if (!OpTy || !Op || !ResTy)
5178 return error("Invalid record");
5179 I = new VAArgInst(Op, ResTy);
5180 InstructionList.push_back(I);
5184 case bitc::FUNC_CODE_OPERAND_BUNDLE: {
5185 // A call or an invoke can be optionally prefixed with some variable
5186 // number of operand bundle blocks. These blocks are read into
5187 // OperandBundles and consumed at the next call or invoke instruction.
5189 if (Record.size() < 1 || Record[0] >= BundleTags.size())
5190 return error("Invalid record");
5192 std::vector<Value *> Inputs;
5195 while (OpNum != Record.size()) {
5197 if (getValueTypePair(Record, OpNum, NextValueNo, Op))
5198 return error("Invalid record");
5199 Inputs.push_back(Op);
5202 OperandBundles.emplace_back(BundleTags[Record[0]], std::move(Inputs));
5207 // Add instruction to end of current BB. If there is no current BB, reject
5211 return error("Invalid instruction with no BB");
5213 if (!OperandBundles.empty()) {
5215 return error("Operand bundles found with no consumer");
5217 CurBB->getInstList().push_back(I);
5219 // If this was a terminator instruction, move to the next block.
5220 if (isa<TerminatorInst>(I)) {
5222 CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : nullptr;
5225 // Non-void values get registered in the value table for future use.
5226 if (I && !I->getType()->isVoidTy())
5227 if (ValueList.assignValue(I, NextValueNo++))
5228 return error("Invalid forward reference");
5233 if (!OperandBundles.empty())
5234 return error("Operand bundles found with no consumer");
5236 // Check the function list for unresolved values.
5237 if (Argument *A = dyn_cast<Argument>(ValueList.back())) {
5238 if (!A->getParent()) {
5239 // We found at least one unresolved value. Nuke them all to avoid leaks.
5240 for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){
5241 if ((A = dyn_cast_or_null<Argument>(ValueList[i])) && !A->getParent()) {
5242 A->replaceAllUsesWith(UndefValue::get(A->getType()));
5246 return error("Never resolved value found in function");
5250 // FIXME: Check for unresolved forward-declared metadata references
5251 // and clean up leaks.
5253 // Trim the value list down to the size it was before we parsed this function.
5254 ValueList.shrinkTo(ModuleValueListSize);
5255 MDValueList.shrinkTo(ModuleMDValueListSize);
5256 std::vector<BasicBlock*>().swap(FunctionBBs);
5257 return std::error_code();
5260 /// Find the function body in the bitcode stream
5261 std::error_code BitcodeReader::findFunctionInStream(
5263 DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator) {
5264 while (DeferredFunctionInfoIterator->second == 0) {
5265 // This is the fallback handling for the old format bitcode that
5266 // didn't contain the function index in the VST, or when we have
5267 // an anonymous function which would not have a VST entry.
5268 // Assert that we have one of those two cases.
5269 assert(VSTOffset == 0 || !F->hasName());
5270 // Parse the next body in the stream and set its position in the
5271 // DeferredFunctionInfo map.
5272 if (std::error_code EC = rememberAndSkipFunctionBodies())
5275 return std::error_code();
5278 //===----------------------------------------------------------------------===//
5279 // GVMaterializer implementation
5280 //===----------------------------------------------------------------------===//
5282 void BitcodeReader::releaseBuffer() { Buffer.release(); }
5284 std::error_code BitcodeReader::materialize(GlobalValue *GV) {
5285 // In older bitcode we must materialize the metadata before parsing
5286 // any functions, in order to set up the MDValueList properly.
5287 if (!SeenModuleValuesRecord) {
5288 if (std::error_code EC = materializeMetadata())
5292 Function *F = dyn_cast<Function>(GV);
5293 // If it's not a function or is already material, ignore the request.
5294 if (!F || !F->isMaterializable())
5295 return std::error_code();
5297 DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F);
5298 assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!");
5299 // If its position is recorded as 0, its body is somewhere in the stream
5300 // but we haven't seen it yet.
5301 if (DFII->second == 0)
5302 if (std::error_code EC = findFunctionInStream(F, DFII))
5305 // Move the bit stream to the saved position of the deferred function body.
5306 Stream.JumpToBit(DFII->second);
5308 if (std::error_code EC = parseFunctionBody(F))
5310 F->setIsMaterializable(false);
5315 // Upgrade any old intrinsic calls in the function.
5316 for (auto &I : UpgradedIntrinsics) {
5317 for (auto UI = I.first->user_begin(), UE = I.first->user_end(); UI != UE;) {
5320 if (CallInst *CI = dyn_cast<CallInst>(U))
5321 UpgradeIntrinsicCall(CI, I.second);
5325 // Finish fn->subprogram upgrade for materialized functions.
5326 if (DISubprogram *SP = FunctionsWithSPs.lookup(F))
5327 F->setSubprogram(SP);
5329 // Bring in any functions that this function forward-referenced via
5331 return materializeForwardReferencedFunctions();
5334 bool BitcodeReader::isDematerializable(const GlobalValue *GV) const {
5335 const Function *F = dyn_cast<Function>(GV);
5336 if (!F || F->isDeclaration())
5339 // Dematerializing F would leave dangling references that wouldn't be
5340 // reconnected on re-materialization.
5341 if (BlockAddressesTaken.count(F))
5344 return DeferredFunctionInfo.count(const_cast<Function*>(F));
5347 void BitcodeReader::dematerialize(GlobalValue *GV) {
5348 Function *F = dyn_cast<Function>(GV);
5349 // If this function isn't dematerializable, this is a noop.
5350 if (!F || !isDematerializable(F))
5353 assert(DeferredFunctionInfo.count(F) && "No info to read function later?");
5355 // Just forget the function body, we can remat it later.
5356 F->dropAllReferences();
5357 F->setIsMaterializable(true);
5360 std::error_code BitcodeReader::materializeModule(Module *M) {
5361 assert(M == TheModule &&
5362 "Can only Materialize the Module this BitcodeReader is attached to.");
5364 if (std::error_code EC = materializeMetadata())
5367 // Promise to materialize all forward references.
5368 WillMaterializeAllForwardRefs = true;
5370 // Iterate over the module, deserializing any functions that are still on
5372 for (Function &F : *TheModule) {
5373 if (std::error_code EC = materialize(&F))
5376 // At this point, if there are any function bodies, parse the rest of
5377 // the bits in the module past the last function block we have recorded
5378 // through either lazy scanning or the VST.
5379 if (LastFunctionBlockBit || NextUnreadBit)
5380 parseModule(LastFunctionBlockBit > NextUnreadBit ? LastFunctionBlockBit
5383 // Check that all block address forward references got resolved (as we
5385 if (!BasicBlockFwdRefs.empty())
5386 return error("Never resolved function from blockaddress");
5388 // Upgrade any intrinsic calls that slipped through (should not happen!) and
5389 // delete the old functions to clean up. We can't do this unless the entire
5390 // module is materialized because there could always be another function body
5391 // with calls to the old function.
5392 for (auto &I : UpgradedIntrinsics) {
5393 for (auto *U : I.first->users()) {
5394 if (CallInst *CI = dyn_cast<CallInst>(U))
5395 UpgradeIntrinsicCall(CI, I.second);
5397 if (!I.first->use_empty())
5398 I.first->replaceAllUsesWith(I.second);
5399 I.first->eraseFromParent();
5401 UpgradedIntrinsics.clear();
5403 for (unsigned I = 0, E = InstsWithTBAATag.size(); I < E; I++)
5404 UpgradeInstWithTBAATag(InstsWithTBAATag[I]);
5406 UpgradeDebugInfo(*M);
5407 return std::error_code();
5410 std::vector<StructType *> BitcodeReader::getIdentifiedStructTypes() const {
5411 return IdentifiedStructTypes;
5415 BitcodeReader::initStream(std::unique_ptr<DataStreamer> Streamer) {
5417 return initLazyStream(std::move(Streamer));
5418 return initStreamFromBuffer();
5421 std::error_code BitcodeReader::initStreamFromBuffer() {
5422 const unsigned char *BufPtr = (const unsigned char*)Buffer->getBufferStart();
5423 const unsigned char *BufEnd = BufPtr+Buffer->getBufferSize();
5425 if (Buffer->getBufferSize() & 3)
5426 return error("Invalid bitcode signature");
5428 // If we have a wrapper header, parse it and ignore the non-bc file contents.
5429 // The magic number is 0x0B17C0DE stored in little endian.
5430 if (isBitcodeWrapper(BufPtr, BufEnd))
5431 if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true))
5432 return error("Invalid bitcode wrapper header");
5434 StreamFile.reset(new BitstreamReader(BufPtr, BufEnd));
5435 Stream.init(&*StreamFile);
5437 return std::error_code();
5441 BitcodeReader::initLazyStream(std::unique_ptr<DataStreamer> Streamer) {
5442 // Check and strip off the bitcode wrapper; BitstreamReader expects never to
5445 llvm::make_unique<StreamingMemoryObject>(std::move(Streamer));
5446 StreamingMemoryObject &Bytes = *OwnedBytes;
5447 StreamFile = llvm::make_unique<BitstreamReader>(std::move(OwnedBytes));
5448 Stream.init(&*StreamFile);
5450 unsigned char buf[16];
5451 if (Bytes.readBytes(buf, 16, 0) != 16)
5452 return error("Invalid bitcode signature");
5454 if (!isBitcode(buf, buf + 16))
5455 return error("Invalid bitcode signature");
5457 if (isBitcodeWrapper(buf, buf + 4)) {
5458 const unsigned char *bitcodeStart = buf;
5459 const unsigned char *bitcodeEnd = buf + 16;
5460 SkipBitcodeWrapperHeader(bitcodeStart, bitcodeEnd, false);
5461 Bytes.dropLeadingBytes(bitcodeStart - buf);
5462 Bytes.setKnownObjectSize(bitcodeEnd - bitcodeStart);
5464 return std::error_code();
5467 std::error_code FunctionIndexBitcodeReader::error(BitcodeError E,
5468 const Twine &Message) {
5469 return ::error(DiagnosticHandler, make_error_code(E), Message);
5472 std::error_code FunctionIndexBitcodeReader::error(const Twine &Message) {
5473 return ::error(DiagnosticHandler,
5474 make_error_code(BitcodeError::CorruptedBitcode), Message);
5477 std::error_code FunctionIndexBitcodeReader::error(BitcodeError E) {
5478 return ::error(DiagnosticHandler, make_error_code(E));
5481 FunctionIndexBitcodeReader::FunctionIndexBitcodeReader(
5482 MemoryBuffer *Buffer, DiagnosticHandlerFunction DiagnosticHandler,
5483 bool IsLazy, bool CheckFuncSummaryPresenceOnly)
5484 : DiagnosticHandler(DiagnosticHandler), Buffer(Buffer), IsLazy(IsLazy),
5485 CheckFuncSummaryPresenceOnly(CheckFuncSummaryPresenceOnly) {}
5487 FunctionIndexBitcodeReader::FunctionIndexBitcodeReader(
5488 DiagnosticHandlerFunction DiagnosticHandler, bool IsLazy,
5489 bool CheckFuncSummaryPresenceOnly)
5490 : DiagnosticHandler(DiagnosticHandler), Buffer(nullptr), IsLazy(IsLazy),
5491 CheckFuncSummaryPresenceOnly(CheckFuncSummaryPresenceOnly) {}
5493 void FunctionIndexBitcodeReader::freeState() { Buffer = nullptr; }
5495 void FunctionIndexBitcodeReader::releaseBuffer() { Buffer.release(); }
5497 // Specialized value symbol table parser used when reading function index
5498 // blocks where we don't actually create global values.
5499 // At the end of this routine the function index is populated with a map
5500 // from function name to FunctionInfo. The function info contains
5501 // the function block's bitcode offset as well as the offset into the
5502 // function summary section.
5503 std::error_code FunctionIndexBitcodeReader::parseValueSymbolTable() {
5504 if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
5505 return error("Invalid record");
5507 SmallVector<uint64_t, 64> Record;
5509 // Read all the records for this value table.
5510 SmallString<128> ValueName;
5512 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
5514 switch (Entry.Kind) {
5515 case BitstreamEntry::SubBlock: // Handled for us already.
5516 case BitstreamEntry::Error:
5517 return error("Malformed block");
5518 case BitstreamEntry::EndBlock:
5519 return std::error_code();
5520 case BitstreamEntry::Record:
5521 // The interesting case.
5527 switch (Stream.readRecord(Entry.ID, Record)) {
5528 default: // Default behavior: ignore (e.g. VST_CODE_BBENTRY records).
5530 case bitc::VST_CODE_FNENTRY: {
5531 // VST_FNENTRY: [valueid, offset, namechar x N]
5532 if (convertToString(Record, 2, ValueName))
5533 return error("Invalid record");
5534 unsigned ValueID = Record[0];
5535 uint64_t FuncOffset = Record[1];
5536 std::unique_ptr<FunctionInfo> FuncInfo =
5537 llvm::make_unique<FunctionInfo>(FuncOffset);
5538 if (foundFuncSummary() && !IsLazy) {
5539 DenseMap<uint64_t, std::unique_ptr<FunctionSummary>>::iterator SMI =
5540 SummaryMap.find(ValueID);
5541 assert(SMI != SummaryMap.end() && "Summary info not found");
5542 FuncInfo->setFunctionSummary(std::move(SMI->second));
5544 TheIndex->addFunctionInfo(ValueName, std::move(FuncInfo));
5549 case bitc::VST_CODE_COMBINED_FNENTRY: {
5550 // VST_FNENTRY: [offset, namechar x N]
5551 if (convertToString(Record, 1, ValueName))
5552 return error("Invalid record");
5553 uint64_t FuncSummaryOffset = Record[0];
5554 std::unique_ptr<FunctionInfo> FuncInfo =
5555 llvm::make_unique<FunctionInfo>(FuncSummaryOffset);
5556 if (foundFuncSummary() && !IsLazy) {
5557 DenseMap<uint64_t, std::unique_ptr<FunctionSummary>>::iterator SMI =
5558 SummaryMap.find(FuncSummaryOffset);
5559 assert(SMI != SummaryMap.end() && "Summary info not found");
5560 FuncInfo->setFunctionSummary(std::move(SMI->second));
5562 TheIndex->addFunctionInfo(ValueName, std::move(FuncInfo));
5571 // Parse just the blocks needed for function index building out of the module.
5572 // At the end of this routine the function Index is populated with a map
5573 // from function name to FunctionInfo. The function info contains
5574 // either the parsed function summary information (when parsing summaries
5575 // eagerly), or just to the function summary record's offset
5576 // if parsing lazily (IsLazy).
5577 std::error_code FunctionIndexBitcodeReader::parseModule() {
5578 if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
5579 return error("Invalid record");
5581 // Read the function index for this module.
5583 BitstreamEntry Entry = Stream.advance();
5585 switch (Entry.Kind) {
5586 case BitstreamEntry::Error:
5587 return error("Malformed block");
5588 case BitstreamEntry::EndBlock:
5589 return std::error_code();
5591 case BitstreamEntry::SubBlock:
5592 if (CheckFuncSummaryPresenceOnly) {
5593 if (Entry.ID == bitc::FUNCTION_SUMMARY_BLOCK_ID) {
5594 SeenFuncSummary = true;
5595 // No need to parse the rest since we found the summary.
5596 return std::error_code();
5598 if (Stream.SkipBlock())
5599 return error("Invalid record");
5603 default: // Skip unknown content.
5604 if (Stream.SkipBlock())
5605 return error("Invalid record");
5607 case bitc::BLOCKINFO_BLOCK_ID:
5608 // Need to parse these to get abbrev ids (e.g. for VST)
5609 if (Stream.ReadBlockInfoBlock())
5610 return error("Malformed block");
5612 case bitc::VALUE_SYMTAB_BLOCK_ID:
5613 if (std::error_code EC = parseValueSymbolTable())
5616 case bitc::FUNCTION_SUMMARY_BLOCK_ID:
5617 SeenFuncSummary = true;
5619 // Lazy parsing of summary info, skip it.
5620 if (Stream.SkipBlock())
5621 return error("Invalid record");
5622 } else if (std::error_code EC = parseEntireSummary())
5625 case bitc::MODULE_STRTAB_BLOCK_ID:
5626 if (std::error_code EC = parseModuleStringTable())
5632 case BitstreamEntry::Record:
5633 Stream.skipRecord(Entry.ID);
5639 // Eagerly parse the entire function summary block (i.e. for all functions
5640 // in the index). This populates the FunctionSummary objects in
5642 std::error_code FunctionIndexBitcodeReader::parseEntireSummary() {
5643 if (Stream.EnterSubBlock(bitc::FUNCTION_SUMMARY_BLOCK_ID))
5644 return error("Invalid record");
5646 SmallVector<uint64_t, 64> Record;
5649 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
5651 switch (Entry.Kind) {
5652 case BitstreamEntry::SubBlock: // Handled for us already.
5653 case BitstreamEntry::Error:
5654 return error("Malformed block");
5655 case BitstreamEntry::EndBlock:
5656 return std::error_code();
5657 case BitstreamEntry::Record:
5658 // The interesting case.
5662 // Read a record. The record format depends on whether this
5663 // is a per-module index or a combined index file. In the per-module
5664 // case the records contain the associated value's ID for correlation
5665 // with VST entries. In the combined index the correlation is done
5666 // via the bitcode offset of the summary records (which were saved
5667 // in the combined index VST entries). The records also contain
5668 // information used for ThinLTO renaming and importing.
5670 uint64_t CurRecordBit = Stream.GetCurrentBitNo();
5671 switch (Stream.readRecord(Entry.ID, Record)) {
5672 default: // Default behavior: ignore.
5674 // FS_PERMODULE_ENTRY: [valueid, islocal, instcount]
5675 case bitc::FS_CODE_PERMODULE_ENTRY: {
5676 unsigned ValueID = Record[0];
5677 bool IsLocal = Record[1];
5678 unsigned InstCount = Record[2];
5679 std::unique_ptr<FunctionSummary> FS =
5680 llvm::make_unique<FunctionSummary>(InstCount);
5681 FS->setLocalFunction(IsLocal);
5682 // The module path string ref set in the summary must be owned by the
5683 // index's module string table. Since we don't have a module path
5684 // string table section in the per-module index, we create a single
5685 // module path string table entry with an empty (0) ID to take
5688 TheIndex->addModulePath(Buffer->getBufferIdentifier(), 0));
5689 SummaryMap[ValueID] = std::move(FS);
5691 // FS_COMBINED_ENTRY: [modid, instcount]
5692 case bitc::FS_CODE_COMBINED_ENTRY: {
5693 uint64_t ModuleId = Record[0];
5694 unsigned InstCount = Record[1];
5695 std::unique_ptr<FunctionSummary> FS =
5696 llvm::make_unique<FunctionSummary>(InstCount);
5697 FS->setModulePath(ModuleIdMap[ModuleId]);
5698 SummaryMap[CurRecordBit] = std::move(FS);
5702 llvm_unreachable("Exit infinite loop");
5705 // Parse the module string table block into the Index.
5706 // This populates the ModulePathStringTable map in the index.
5707 std::error_code FunctionIndexBitcodeReader::parseModuleStringTable() {
5708 if (Stream.EnterSubBlock(bitc::MODULE_STRTAB_BLOCK_ID))
5709 return error("Invalid record");
5711 SmallVector<uint64_t, 64> Record;
5713 SmallString<128> ModulePath;
5715 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
5717 switch (Entry.Kind) {
5718 case BitstreamEntry::SubBlock: // Handled for us already.
5719 case BitstreamEntry::Error:
5720 return error("Malformed block");
5721 case BitstreamEntry::EndBlock:
5722 return std::error_code();
5723 case BitstreamEntry::Record:
5724 // The interesting case.
5729 switch (Stream.readRecord(Entry.ID, Record)) {
5730 default: // Default behavior: ignore.
5732 case bitc::MST_CODE_ENTRY: {
5733 // MST_ENTRY: [modid, namechar x N]
5734 if (convertToString(Record, 1, ModulePath))
5735 return error("Invalid record");
5736 uint64_t ModuleId = Record[0];
5737 StringRef ModulePathInMap = TheIndex->addModulePath(ModulePath, ModuleId);
5738 ModuleIdMap[ModuleId] = ModulePathInMap;
5744 llvm_unreachable("Exit infinite loop");
5747 // Parse the function info index from the bitcode streamer into the given index.
5748 std::error_code FunctionIndexBitcodeReader::parseSummaryIndexInto(
5749 std::unique_ptr<DataStreamer> Streamer, FunctionInfoIndex *I) {
5752 if (std::error_code EC = initStream(std::move(Streamer)))
5755 // Sniff for the signature.
5756 if (!hasValidBitcodeHeader(Stream))
5757 return error("Invalid bitcode signature");
5759 // We expect a number of well-defined blocks, though we don't necessarily
5760 // need to understand them all.
5762 if (Stream.AtEndOfStream()) {
5763 // We didn't really read a proper Module block.
5764 return error("Malformed block");
5767 BitstreamEntry Entry =
5768 Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs);
5770 if (Entry.Kind != BitstreamEntry::SubBlock)
5771 return error("Malformed block");
5773 // If we see a MODULE_BLOCK, parse it to find the blocks needed for
5774 // building the function summary index.
5775 if (Entry.ID == bitc::MODULE_BLOCK_ID)
5776 return parseModule();
5778 if (Stream.SkipBlock())
5779 return error("Invalid record");
5783 // Parse the function information at the given offset in the buffer into
5784 // the index. Used to support lazy parsing of function summaries from the
5785 // combined index during importing.
5786 // TODO: This function is not yet complete as it won't have a consumer
5787 // until ThinLTO function importing is added.
5788 std::error_code FunctionIndexBitcodeReader::parseFunctionSummary(
5789 std::unique_ptr<DataStreamer> Streamer, FunctionInfoIndex *I,
5790 size_t FunctionSummaryOffset) {
5793 if (std::error_code EC = initStream(std::move(Streamer)))
5796 // Sniff for the signature.
5797 if (!hasValidBitcodeHeader(Stream))
5798 return error("Invalid bitcode signature");
5800 Stream.JumpToBit(FunctionSummaryOffset);
5802 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
5804 switch (Entry.Kind) {
5806 return error("Malformed block");
5807 case BitstreamEntry::Record:
5808 // The expected case.
5812 // TODO: Read a record. This interface will be completed when ThinLTO
5813 // importing is added so that it can be tested.
5814 SmallVector<uint64_t, 64> Record;
5815 switch (Stream.readRecord(Entry.ID, Record)) {
5816 case bitc::FS_CODE_COMBINED_ENTRY:
5818 return error("Invalid record");
5821 return std::error_code();
5825 FunctionIndexBitcodeReader::initStream(std::unique_ptr<DataStreamer> Streamer) {
5827 return initLazyStream(std::move(Streamer));
5828 return initStreamFromBuffer();
5831 std::error_code FunctionIndexBitcodeReader::initStreamFromBuffer() {
5832 const unsigned char *BufPtr = (const unsigned char *)Buffer->getBufferStart();
5833 const unsigned char *BufEnd = BufPtr + Buffer->getBufferSize();
5835 if (Buffer->getBufferSize() & 3)
5836 return error("Invalid bitcode signature");
5838 // If we have a wrapper header, parse it and ignore the non-bc file contents.
5839 // The magic number is 0x0B17C0DE stored in little endian.
5840 if (isBitcodeWrapper(BufPtr, BufEnd))
5841 if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true))
5842 return error("Invalid bitcode wrapper header");
5844 StreamFile.reset(new BitstreamReader(BufPtr, BufEnd));
5845 Stream.init(&*StreamFile);
5847 return std::error_code();
5850 std::error_code FunctionIndexBitcodeReader::initLazyStream(
5851 std::unique_ptr<DataStreamer> Streamer) {
5852 // Check and strip off the bitcode wrapper; BitstreamReader expects never to
5855 llvm::make_unique<StreamingMemoryObject>(std::move(Streamer));
5856 StreamingMemoryObject &Bytes = *OwnedBytes;
5857 StreamFile = llvm::make_unique<BitstreamReader>(std::move(OwnedBytes));
5858 Stream.init(&*StreamFile);
5860 unsigned char buf[16];
5861 if (Bytes.readBytes(buf, 16, 0) != 16)
5862 return error("Invalid bitcode signature");
5864 if (!isBitcode(buf, buf + 16))
5865 return error("Invalid bitcode signature");
5867 if (isBitcodeWrapper(buf, buf + 4)) {
5868 const unsigned char *bitcodeStart = buf;
5869 const unsigned char *bitcodeEnd = buf + 16;
5870 SkipBitcodeWrapperHeader(bitcodeStart, bitcodeEnd, false);
5871 Bytes.dropLeadingBytes(bitcodeStart - buf);
5872 Bytes.setKnownObjectSize(bitcodeEnd - bitcodeStart);
5874 return std::error_code();
5878 class BitcodeErrorCategoryType : public std::error_category {
5879 const char *name() const LLVM_NOEXCEPT override {
5880 return "llvm.bitcode";
5882 std::string message(int IE) const override {
5883 BitcodeError E = static_cast<BitcodeError>(IE);
5885 case BitcodeError::InvalidBitcodeSignature:
5886 return "Invalid bitcode signature";
5887 case BitcodeError::CorruptedBitcode:
5888 return "Corrupted bitcode";
5890 llvm_unreachable("Unknown error type!");
5895 static ManagedStatic<BitcodeErrorCategoryType> ErrorCategory;
5897 const std::error_category &llvm::BitcodeErrorCategory() {
5898 return *ErrorCategory;
5901 //===----------------------------------------------------------------------===//
5902 // External interface
5903 //===----------------------------------------------------------------------===//
5905 static ErrorOr<std::unique_ptr<Module>>
5906 getBitcodeModuleImpl(std::unique_ptr<DataStreamer> Streamer, StringRef Name,
5907 BitcodeReader *R, LLVMContext &Context,
5908 bool MaterializeAll, bool ShouldLazyLoadMetadata) {
5909 std::unique_ptr<Module> M = make_unique<Module>(Name, Context);
5910 M->setMaterializer(R);
5912 auto cleanupOnError = [&](std::error_code EC) {
5913 R->releaseBuffer(); // Never take ownership on error.
5917 // Delay parsing Metadata if ShouldLazyLoadMetadata is true.
5918 if (std::error_code EC = R->parseBitcodeInto(std::move(Streamer), M.get(),
5919 ShouldLazyLoadMetadata))
5920 return cleanupOnError(EC);
5922 if (MaterializeAll) {
5923 // Read in the entire module, and destroy the BitcodeReader.
5924 if (std::error_code EC = M->materializeAllPermanently())
5925 return cleanupOnError(EC);
5927 // Resolve forward references from blockaddresses.
5928 if (std::error_code EC = R->materializeForwardReferencedFunctions())
5929 return cleanupOnError(EC);
5931 return std::move(M);
5934 /// \brief Get a lazy one-at-time loading module from bitcode.
5936 /// This isn't always used in a lazy context. In particular, it's also used by
5937 /// \a parseBitcodeFile(). If this is truly lazy, then we need to eagerly pull
5938 /// in forward-referenced functions from block address references.
5940 /// \param[in] MaterializeAll Set to \c true if we should materialize
5942 static ErrorOr<std::unique_ptr<Module>>
5943 getLazyBitcodeModuleImpl(std::unique_ptr<MemoryBuffer> &&Buffer,
5944 LLVMContext &Context, bool MaterializeAll,
5945 DiagnosticHandlerFunction DiagnosticHandler,
5946 bool ShouldLazyLoadMetadata = false) {
5948 new BitcodeReader(Buffer.get(), Context, DiagnosticHandler);
5950 ErrorOr<std::unique_ptr<Module>> Ret =
5951 getBitcodeModuleImpl(nullptr, Buffer->getBufferIdentifier(), R, Context,
5952 MaterializeAll, ShouldLazyLoadMetadata);
5956 Buffer.release(); // The BitcodeReader owns it now.
5960 ErrorOr<std::unique_ptr<Module>> llvm::getLazyBitcodeModule(
5961 std::unique_ptr<MemoryBuffer> &&Buffer, LLVMContext &Context,
5962 DiagnosticHandlerFunction DiagnosticHandler, bool ShouldLazyLoadMetadata) {
5963 return getLazyBitcodeModuleImpl(std::move(Buffer), Context, false,
5964 DiagnosticHandler, ShouldLazyLoadMetadata);
5967 ErrorOr<std::unique_ptr<Module>> llvm::getStreamedBitcodeModule(
5968 StringRef Name, std::unique_ptr<DataStreamer> Streamer,
5969 LLVMContext &Context, DiagnosticHandlerFunction DiagnosticHandler) {
5970 std::unique_ptr<Module> M = make_unique<Module>(Name, Context);
5971 BitcodeReader *R = new BitcodeReader(Context, DiagnosticHandler);
5973 return getBitcodeModuleImpl(std::move(Streamer), Name, R, Context, false,
5977 ErrorOr<std::unique_ptr<Module>>
5978 llvm::parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context,
5979 DiagnosticHandlerFunction DiagnosticHandler) {
5980 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false);
5981 return getLazyBitcodeModuleImpl(std::move(Buf), Context, true,
5983 // TODO: Restore the use-lists to the in-memory state when the bitcode was
5984 // written. We must defer until the Module has been fully materialized.
5988 llvm::getBitcodeTargetTriple(MemoryBufferRef Buffer, LLVMContext &Context,
5989 DiagnosticHandlerFunction DiagnosticHandler) {
5990 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false);
5991 auto R = llvm::make_unique<BitcodeReader>(Buf.release(), Context,
5993 ErrorOr<std::string> Triple = R->parseTriple();
5994 if (Triple.getError())
5996 return Triple.get();
6000 llvm::getBitcodeProducerString(MemoryBufferRef Buffer, LLVMContext &Context,
6001 DiagnosticHandlerFunction DiagnosticHandler) {
6002 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false);
6003 BitcodeReader R(Buf.release(), Context, DiagnosticHandler);
6004 ErrorOr<std::string> ProducerString = R.parseIdentificationBlock();
6005 if (ProducerString.getError())
6007 return ProducerString.get();
6010 // Parse the specified bitcode buffer, returning the function info index.
6011 // If IsLazy is false, parse the entire function summary into
6012 // the index. Otherwise skip the function summary section, and only create
6013 // an index object with a map from function name to function summary offset.
6014 // The index is used to perform lazy function summary reading later.
6015 ErrorOr<std::unique_ptr<FunctionInfoIndex>>
6016 llvm::getFunctionInfoIndex(MemoryBufferRef Buffer,
6017 DiagnosticHandlerFunction DiagnosticHandler,
6019 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false);
6020 FunctionIndexBitcodeReader R(Buf.get(), DiagnosticHandler, IsLazy);
6022 auto Index = llvm::make_unique<FunctionInfoIndex>();
6024 auto cleanupOnError = [&](std::error_code EC) {
6025 R.releaseBuffer(); // Never take ownership on error.
6029 if (std::error_code EC = R.parseSummaryIndexInto(nullptr, Index.get()))
6030 return cleanupOnError(EC);
6032 Buf.release(); // The FunctionIndexBitcodeReader owns it now.
6033 return std::move(Index);
6036 // Check if the given bitcode buffer contains a function summary block.
6037 bool llvm::hasFunctionSummary(MemoryBufferRef Buffer,
6038 DiagnosticHandlerFunction DiagnosticHandler) {
6039 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false);
6040 FunctionIndexBitcodeReader R(Buf.get(), DiagnosticHandler, false, true);
6042 auto cleanupOnError = [&](std::error_code EC) {
6043 R.releaseBuffer(); // Never take ownership on error.
6047 if (std::error_code EC = R.parseSummaryIndexInto(nullptr, nullptr))
6048 return cleanupOnError(EC);
6050 Buf.release(); // The FunctionIndexBitcodeReader owns it now.
6051 return R.foundFuncSummary();
6054 // This method supports lazy reading of function summary data from the combined
6055 // index during ThinLTO function importing. When reading the combined index
6056 // file, getFunctionInfoIndex is first invoked with IsLazy=true.
6057 // Then this method is called for each function considered for importing,
6058 // to parse the summary information for the given function name into
6060 std::error_code llvm::readFunctionSummary(
6061 MemoryBufferRef Buffer, DiagnosticHandlerFunction DiagnosticHandler,
6062 StringRef FunctionName, std::unique_ptr<FunctionInfoIndex> Index) {
6063 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false);
6064 FunctionIndexBitcodeReader R(Buf.get(), DiagnosticHandler);
6066 auto cleanupOnError = [&](std::error_code EC) {
6067 R.releaseBuffer(); // Never take ownership on error.
6071 // Lookup the given function name in the FunctionMap, which may
6072 // contain a list of function infos in the case of a COMDAT. Walk through
6073 // and parse each function summary info at the function summary offset
6074 // recorded when parsing the value symbol table.
6075 for (const auto &FI : Index->getFunctionInfoList(FunctionName)) {
6076 size_t FunctionSummaryOffset = FI->bitcodeIndex();
6077 if (std::error_code EC =
6078 R.parseFunctionSummary(nullptr, Index.get(), FunctionSummaryOffset))
6079 return cleanupOnError(EC);
6082 Buf.release(); // The FunctionIndexBitcodeReader owns it now.
6083 return std::error_code();