1 //===- BitcodeReader.cpp - Internal BitcodeReader implementation ----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/ADT/STLExtras.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/ADT/SmallVector.h"
13 #include "llvm/ADT/Triple.h"
14 #include "llvm/Bitcode/BitstreamReader.h"
15 #include "llvm/Bitcode/LLVMBitCodes.h"
16 #include "llvm/Bitcode/ReaderWriter.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/ModuleSummaryIndex.h"
29 #include "llvm/IR/OperandTraits.h"
30 #include "llvm/IR/Operator.h"
31 #include "llvm/IR/ValueHandle.h"
32 #include "llvm/Support/CommandLine.h"
33 #include "llvm/Support/DataStream.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/ManagedStatic.h"
36 #include "llvm/Support/MathExtras.h"
37 #include "llvm/Support/MemoryBuffer.h"
38 #include "llvm/Support/raw_ostream.h"
39 #include <deque>
40 
41 using namespace llvm;
42 
43 static cl::opt<bool> PrintSummaryGUIDs(
44     "print-summary-global-ids", cl::init(false), cl::Hidden,
45     cl::desc(
46         "Print the global id for each value when reading the module summary"));
47 
48 namespace {
49 enum {
50   SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex
51 };
52 
53 class BitcodeReaderValueList {
54   std::vector<WeakVH> ValuePtrs;
55 
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.
60   ///
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;
65   LLVMContext &Context;
66 public:
67   BitcodeReaderValueList(LLVMContext &C) : Context(C) {}
68   ~BitcodeReaderValueList() {
69     assert(ResolveConstants.empty() && "Constants not resolved?");
70   }
71 
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); }
76 
77   void clear() {
78     assert(ResolveConstants.empty() && "Constants not resolved?");
79     ValuePtrs.clear();
80   }
81 
82   Value *operator[](unsigned i) const {
83     assert(i < ValuePtrs.size());
84     return ValuePtrs[i];
85   }
86 
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!");
92     ValuePtrs.resize(N);
93   }
94 
95   Constant *getConstantFwdRef(unsigned Idx, Type *Ty);
96   Value *getValueFwdRef(unsigned Idx, Type *Ty);
97 
98   void assignValue(Value *V, unsigned Idx);
99 
100   /// Once all constants are read, this method bulk resolves any forward
101   /// references.
102   void resolveConstantForwardRefs();
103 };
104 
105 class BitcodeReaderMetadataList {
106   unsigned NumFwdRefs;
107   bool AnyFwdRefs;
108   unsigned MinFwdRef;
109   unsigned MaxFwdRef;
110 
111   /// Array of metadata references.
112   ///
113   /// Don't use std::vector here.  Some versions of libc++ copy (instead of
114   /// move) on resize, and TrackingMDRef is very expensive to copy.
115   SmallVector<TrackingMDRef, 1> MetadataPtrs;
116 
117   /// Structures for resolving old type refs.
118   struct {
119     SmallDenseMap<MDString *, TempMDTuple, 1> Unknown;
120     SmallDenseMap<MDString *, DICompositeType *, 1> Final;
121     SmallDenseMap<MDString *, DICompositeType *, 1> FwdDecls;
122     SmallVector<std::pair<TrackingMDRef, TempMDTuple>, 1> Arrays;
123   } OldTypeRefs;
124 
125   LLVMContext &Context;
126 public:
127   BitcodeReaderMetadataList(LLVMContext &C)
128       : NumFwdRefs(0), AnyFwdRefs(false), Context(C) {}
129 
130   // vector compatibility methods
131   unsigned size() const { return MetadataPtrs.size(); }
132   void resize(unsigned N) { MetadataPtrs.resize(N); }
133   void push_back(Metadata *MD) { MetadataPtrs.emplace_back(MD); }
134   void clear() { MetadataPtrs.clear(); }
135   Metadata *back() const { return MetadataPtrs.back(); }
136   void pop_back() { MetadataPtrs.pop_back(); }
137   bool empty() const { return MetadataPtrs.empty(); }
138 
139   Metadata *operator[](unsigned i) const {
140     assert(i < MetadataPtrs.size());
141     return MetadataPtrs[i];
142   }
143 
144   Metadata *lookup(unsigned I) const {
145     if (I < MetadataPtrs.size())
146       return MetadataPtrs[I];
147     return nullptr;
148   }
149 
150   void shrinkTo(unsigned N) {
151     assert(N <= size() && "Invalid shrinkTo request!");
152     assert(!AnyFwdRefs && "Unexpected forward refs");
153     MetadataPtrs.resize(N);
154   }
155 
156   /// Return the given metadata, creating a replaceable forward reference if
157   /// necessary.
158   Metadata *getMetadataFwdRef(unsigned Idx);
159 
160   /// Return the the given metadata only if it is fully resolved.
161   ///
162   /// Gives the same result as \a lookup(), unless \a MDNode::isResolved()
163   /// would give \c false.
164   Metadata *getMetadataIfResolved(unsigned Idx);
165 
166   MDNode *getMDNodeFwdRefOrNull(unsigned Idx);
167   void assignValue(Metadata *MD, unsigned Idx);
168   void tryToResolveCycles();
169   bool hasFwdRefs() const { return AnyFwdRefs; }
170 
171   /// Upgrade a type that had an MDString reference.
172   void addTypeRef(MDString &UUID, DICompositeType &CT);
173 
174   /// Upgrade a type that had an MDString reference.
175   Metadata *upgradeTypeRef(Metadata *MaybeUUID);
176 
177   /// Upgrade a type ref array that may have MDString references.
178   Metadata *upgradeTypeRefArray(Metadata *MaybeTuple);
179 
180 private:
181   Metadata *resolveTypeRefArray(Metadata *MaybeTuple);
182 };
183 
184 class BitcodeReader : public GVMaterializer {
185   LLVMContext &Context;
186   Module *TheModule = nullptr;
187   std::unique_ptr<MemoryBuffer> Buffer;
188   std::unique_ptr<BitstreamReader> StreamFile;
189   BitstreamCursor Stream;
190   // Next offset to start scanning for lazy parsing of function bodies.
191   uint64_t NextUnreadBit = 0;
192   // Last function offset found in the VST.
193   uint64_t LastFunctionBlockBit = 0;
194   bool SeenValueSymbolTable = false;
195   uint64_t VSTOffset = 0;
196   // Contains an arbitrary and optional string identifying the bitcode producer
197   std::string ProducerIdentification;
198 
199   std::vector<Type*> TypeList;
200   BitcodeReaderValueList ValueList;
201   BitcodeReaderMetadataList MetadataList;
202   std::vector<Comdat *> ComdatList;
203   SmallVector<Instruction *, 64> InstructionList;
204 
205   std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
206   std::vector<std::pair<GlobalIndirectSymbol*, unsigned> > IndirectSymbolInits;
207   std::vector<std::pair<Function*, unsigned> > FunctionPrefixes;
208   std::vector<std::pair<Function*, unsigned> > FunctionPrologues;
209   std::vector<std::pair<Function*, unsigned> > FunctionPersonalityFns;
210 
211   SmallVector<Instruction*, 64> InstsWithTBAATag;
212 
213   bool HasSeenOldLoopTags = false;
214 
215   /// The set of attributes by index.  Index zero in the file is for null, and
216   /// is thus not represented here.  As such all indices are off by one.
217   std::vector<AttributeSet> MAttributes;
218 
219   /// The set of attribute groups.
220   std::map<unsigned, AttributeSet> MAttributeGroups;
221 
222   /// While parsing a function body, this is a list of the basic blocks for the
223   /// function.
224   std::vector<BasicBlock*> FunctionBBs;
225 
226   // When reading the module header, this list is populated with functions that
227   // have bodies later in the file.
228   std::vector<Function*> FunctionsWithBodies;
229 
230   // When intrinsic functions are encountered which require upgrading they are
231   // stored here with their replacement function.
232   typedef DenseMap<Function*, Function*> UpgradedIntrinsicMap;
233   UpgradedIntrinsicMap UpgradedIntrinsics;
234 
235   // Map the bitcode's custom MDKind ID to the Module's MDKind ID.
236   DenseMap<unsigned, unsigned> MDKindMap;
237 
238   // Several operations happen after the module header has been read, but
239   // before function bodies are processed. This keeps track of whether
240   // we've done this yet.
241   bool SeenFirstFunctionBody = false;
242 
243   /// When function bodies are initially scanned, this map contains info about
244   /// where to find deferred function body in the stream.
245   DenseMap<Function*, uint64_t> DeferredFunctionInfo;
246 
247   /// When Metadata block is initially scanned when parsing the module, we may
248   /// choose to defer parsing of the metadata. This vector contains info about
249   /// which Metadata blocks are deferred.
250   std::vector<uint64_t> DeferredMetadataInfo;
251 
252   /// These are basic blocks forward-referenced by block addresses.  They are
253   /// inserted lazily into functions when they're loaded.  The basic block ID is
254   /// its index into the vector.
255   DenseMap<Function *, std::vector<BasicBlock *>> BasicBlockFwdRefs;
256   std::deque<Function *> BasicBlockFwdRefQueue;
257 
258   /// Indicates that we are using a new encoding for instruction operands where
259   /// most operands in the current FUNCTION_BLOCK are encoded relative to the
260   /// instruction number, for a more compact encoding.  Some instruction
261   /// operands are not relative to the instruction ID: basic block numbers, and
262   /// types. Once the old style function blocks have been phased out, we would
263   /// not need this flag.
264   bool UseRelativeIDs = false;
265 
266   /// True if all functions will be materialized, negating the need to process
267   /// (e.g.) blockaddress forward references.
268   bool WillMaterializeAllForwardRefs = false;
269 
270   /// True if any Metadata block has been materialized.
271   bool IsMetadataMaterialized = false;
272 
273   bool StripDebugInfo = false;
274 
275   /// Functions that need to be matched with subprograms when upgrading old
276   /// metadata.
277   SmallDenseMap<Function *, DISubprogram *, 16> FunctionsWithSPs;
278 
279   std::vector<std::string> BundleTags;
280 
281 public:
282   std::error_code error(BitcodeError E, const Twine &Message);
283   std::error_code error(BitcodeError E);
284   std::error_code error(const Twine &Message);
285 
286   BitcodeReader(MemoryBuffer *Buffer, LLVMContext &Context);
287   BitcodeReader(LLVMContext &Context);
288   ~BitcodeReader() override { freeState(); }
289 
290   std::error_code materializeForwardReferencedFunctions();
291 
292   void freeState();
293 
294   void releaseBuffer();
295 
296   std::error_code materialize(GlobalValue *GV) override;
297   std::error_code materializeModule() override;
298   std::vector<StructType *> getIdentifiedStructTypes() const override;
299 
300   /// \brief Main interface to parsing a bitcode buffer.
301   /// \returns true if an error occurred.
302   std::error_code parseBitcodeInto(std::unique_ptr<DataStreamer> Streamer,
303                                    Module *M,
304                                    bool ShouldLazyLoadMetadata = false);
305 
306   /// \brief Cheap mechanism to just extract module triple
307   /// \returns true if an error occurred.
308   ErrorOr<std::string> parseTriple();
309 
310   /// Cheap mechanism to just extract the identification block out of bitcode.
311   ErrorOr<std::string> parseIdentificationBlock();
312 
313   static uint64_t decodeSignRotatedValue(uint64_t V);
314 
315   /// Materialize any deferred Metadata block.
316   std::error_code materializeMetadata() override;
317 
318   void setStripDebugInfo() override;
319 
320 private:
321   /// Parse the "IDENTIFICATION_BLOCK_ID" block, populate the
322   // ProducerIdentification data member, and do some basic enforcement on the
323   // "epoch" encoded in the bitcode.
324   std::error_code parseBitcodeVersion();
325 
326   std::vector<StructType *> IdentifiedStructTypes;
327   StructType *createIdentifiedStructType(LLVMContext &Context, StringRef Name);
328   StructType *createIdentifiedStructType(LLVMContext &Context);
329 
330   Type *getTypeByID(unsigned ID);
331   Value *getFnValueByID(unsigned ID, Type *Ty) {
332     if (Ty && Ty->isMetadataTy())
333       return MetadataAsValue::get(Ty->getContext(), getFnMetadataByID(ID));
334     return ValueList.getValueFwdRef(ID, Ty);
335   }
336   Metadata *getFnMetadataByID(unsigned ID) {
337     return MetadataList.getMetadataFwdRef(ID);
338   }
339   BasicBlock *getBasicBlock(unsigned ID) const {
340     if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID
341     return FunctionBBs[ID];
342   }
343   AttributeSet getAttributes(unsigned i) const {
344     if (i-1 < MAttributes.size())
345       return MAttributes[i-1];
346     return AttributeSet();
347   }
348 
349   /// Read a value/type pair out of the specified record from slot 'Slot'.
350   /// Increment Slot past the number of slots used in the record. Return true on
351   /// failure.
352   bool getValueTypePair(SmallVectorImpl<uint64_t> &Record, unsigned &Slot,
353                         unsigned InstNum, Value *&ResVal) {
354     if (Slot == Record.size()) return true;
355     unsigned ValNo = (unsigned)Record[Slot++];
356     // Adjust the ValNo, if it was encoded relative to the InstNum.
357     if (UseRelativeIDs)
358       ValNo = InstNum - ValNo;
359     if (ValNo < InstNum) {
360       // If this is not a forward reference, just return the value we already
361       // have.
362       ResVal = getFnValueByID(ValNo, nullptr);
363       return ResVal == nullptr;
364     }
365     if (Slot == Record.size())
366       return true;
367 
368     unsigned TypeNo = (unsigned)Record[Slot++];
369     ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
370     return ResVal == nullptr;
371   }
372 
373   /// Read a value out of the specified record from slot 'Slot'. Increment Slot
374   /// past the number of slots used by the value in the record. Return true if
375   /// there is an error.
376   bool popValue(SmallVectorImpl<uint64_t> &Record, unsigned &Slot,
377                 unsigned InstNum, Type *Ty, Value *&ResVal) {
378     if (getValue(Record, Slot, InstNum, Ty, ResVal))
379       return true;
380     // All values currently take a single record slot.
381     ++Slot;
382     return false;
383   }
384 
385   /// Like popValue, but does not increment the Slot number.
386   bool getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
387                 unsigned InstNum, Type *Ty, Value *&ResVal) {
388     ResVal = getValue(Record, Slot, InstNum, Ty);
389     return ResVal == nullptr;
390   }
391 
392   /// Version of getValue that returns ResVal directly, or 0 if there is an
393   /// error.
394   Value *getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
395                   unsigned InstNum, Type *Ty) {
396     if (Slot == Record.size()) return nullptr;
397     unsigned ValNo = (unsigned)Record[Slot];
398     // Adjust the ValNo, if it was encoded relative to the InstNum.
399     if (UseRelativeIDs)
400       ValNo = InstNum - ValNo;
401     return getFnValueByID(ValNo, Ty);
402   }
403 
404   /// Like getValue, but decodes signed VBRs.
405   Value *getValueSigned(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
406                         unsigned InstNum, Type *Ty) {
407     if (Slot == Record.size()) return nullptr;
408     unsigned ValNo = (unsigned)decodeSignRotatedValue(Record[Slot]);
409     // Adjust the ValNo, if it was encoded relative to the InstNum.
410     if (UseRelativeIDs)
411       ValNo = InstNum - ValNo;
412     return getFnValueByID(ValNo, Ty);
413   }
414 
415   /// Converts alignment exponent (i.e. power of two (or zero)) to the
416   /// corresponding alignment to use. If alignment is too large, returns
417   /// a corresponding error code.
418   std::error_code parseAlignmentValue(uint64_t Exponent, unsigned &Alignment);
419   std::error_code parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind);
420   std::error_code parseModule(uint64_t ResumeBit,
421                               bool ShouldLazyLoadMetadata = false);
422   std::error_code parseAttributeBlock();
423   std::error_code parseAttributeGroupBlock();
424   std::error_code parseTypeTable();
425   std::error_code parseTypeTableBody();
426   std::error_code parseOperandBundleTags();
427 
428   ErrorOr<Value *> recordValue(SmallVectorImpl<uint64_t> &Record,
429                                unsigned NameIndex, Triple &TT);
430   std::error_code parseValueSymbolTable(uint64_t Offset = 0);
431   std::error_code parseConstants();
432   std::error_code rememberAndSkipFunctionBodies();
433   std::error_code rememberAndSkipFunctionBody();
434   /// Save the positions of the Metadata blocks and skip parsing the blocks.
435   std::error_code rememberAndSkipMetadata();
436   std::error_code parseFunctionBody(Function *F);
437   std::error_code globalCleanup();
438   std::error_code resolveGlobalAndIndirectSymbolInits();
439   std::error_code parseMetadata(bool ModuleLevel = false);
440   std::error_code parseMetadataStrings(ArrayRef<uint64_t> Record,
441                                        StringRef Blob,
442                                        unsigned &NextMetadataNo);
443   std::error_code parseMetadataKinds();
444   std::error_code parseMetadataKindRecord(SmallVectorImpl<uint64_t> &Record);
445   std::error_code parseMetadataAttachment(Function &F);
446   ErrorOr<std::string> parseModuleTriple();
447   std::error_code parseUseLists();
448   std::error_code initStream(std::unique_ptr<DataStreamer> Streamer);
449   std::error_code initStreamFromBuffer();
450   std::error_code initLazyStream(std::unique_ptr<DataStreamer> Streamer);
451   std::error_code findFunctionInStream(
452       Function *F,
453       DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator);
454 };
455 
456 /// Class to manage reading and parsing function summary index bitcode
457 /// files/sections.
458 class ModuleSummaryIndexBitcodeReader {
459   DiagnosticHandlerFunction DiagnosticHandler;
460 
461   /// Eventually points to the module index built during parsing.
462   ModuleSummaryIndex *TheIndex = nullptr;
463 
464   std::unique_ptr<MemoryBuffer> Buffer;
465   std::unique_ptr<BitstreamReader> StreamFile;
466   BitstreamCursor Stream;
467 
468   /// Used to indicate whether caller only wants to check for the presence
469   /// of the global value summary bitcode section. All blocks are skipped,
470   /// but the SeenGlobalValSummary boolean is set.
471   bool CheckGlobalValSummaryPresenceOnly = false;
472 
473   /// Indicates whether we have encountered a global value summary section
474   /// yet during parsing, used when checking if file contains global value
475   /// summary section.
476   bool SeenGlobalValSummary = false;
477 
478   /// Indicates whether we have already parsed the VST, used for error checking.
479   bool SeenValueSymbolTable = false;
480 
481   /// Set to the offset of the VST recorded in the MODULE_CODE_VSTOFFSET record.
482   /// Used to enable on-demand parsing of the VST.
483   uint64_t VSTOffset = 0;
484 
485   // Map to save ValueId to GUID association that was recorded in the
486   // ValueSymbolTable. It is used after the VST is parsed to convert
487   // call graph edges read from the function summary from referencing
488   // callees by their ValueId to using the GUID instead, which is how
489   // they are recorded in the summary index being built.
490   // We save a second GUID which is the same as the first one, but ignoring the
491   // linkage, i.e. for value other than local linkage they are identical.
492   DenseMap<unsigned, std::pair<GlobalValue::GUID, GlobalValue::GUID>>
493       ValueIdToCallGraphGUIDMap;
494 
495   /// Map to save the association between summary offset in the VST to the
496   /// GUID created when parsing it. Used to add newly parsed summaries to
497   /// the index.
498   DenseMap<uint64_t, GlobalValue::GUID> SummaryOffsetToGUIDMap;
499 
500   /// Map populated during module path string table parsing, from the
501   /// module ID to a string reference owned by the index's module
502   /// path string table, used to correlate with combined index
503   /// summary records.
504   DenseMap<uint64_t, StringRef> ModuleIdMap;
505 
506   /// Original source file name recorded in a bitcode record.
507   std::string SourceFileName;
508 
509 public:
510   std::error_code error(BitcodeError E, const Twine &Message);
511   std::error_code error(BitcodeError E);
512   std::error_code error(const Twine &Message);
513 
514   ModuleSummaryIndexBitcodeReader(
515       MemoryBuffer *Buffer, DiagnosticHandlerFunction DiagnosticHandler,
516       bool CheckGlobalValSummaryPresenceOnly = false);
517   ModuleSummaryIndexBitcodeReader(
518       DiagnosticHandlerFunction DiagnosticHandler,
519       bool CheckGlobalValSummaryPresenceOnly = false);
520   ~ModuleSummaryIndexBitcodeReader() { freeState(); }
521 
522   void freeState();
523 
524   void releaseBuffer();
525 
526   /// Check if the parser has encountered a summary section.
527   bool foundGlobalValSummary() { return SeenGlobalValSummary; }
528 
529   /// \brief Main interface to parsing a bitcode buffer.
530   /// \returns true if an error occurred.
531   std::error_code parseSummaryIndexInto(std::unique_ptr<DataStreamer> Streamer,
532                                         ModuleSummaryIndex *I);
533 
534   /// \brief Interface for parsing a summary lazily.
535   std::error_code
536   parseGlobalValueSummary(std::unique_ptr<DataStreamer> Streamer,
537                           ModuleSummaryIndex *I, size_t SummaryOffset);
538 
539 private:
540   std::error_code parseModule();
541   std::error_code parseValueSymbolTable(
542       uint64_t Offset,
543       DenseMap<unsigned, GlobalValue::LinkageTypes> &ValueIdToLinkageMap);
544   std::error_code parseEntireSummary();
545   std::error_code parseModuleStringTable();
546   std::error_code initStream(std::unique_ptr<DataStreamer> Streamer);
547   std::error_code initStreamFromBuffer();
548   std::error_code initLazyStream(std::unique_ptr<DataStreamer> Streamer);
549   std::pair<GlobalValue::GUID, GlobalValue::GUID>
550   getGUIDFromValueId(unsigned ValueId);
551   GlobalValue::GUID getGUIDFromOffset(uint64_t Offset);
552 };
553 } // end anonymous namespace
554 
555 BitcodeDiagnosticInfo::BitcodeDiagnosticInfo(std::error_code EC,
556                                              DiagnosticSeverity Severity,
557                                              const Twine &Msg)
558     : DiagnosticInfo(DK_Bitcode, Severity), Msg(Msg), EC(EC) {}
559 
560 void BitcodeDiagnosticInfo::print(DiagnosticPrinter &DP) const { DP << Msg; }
561 
562 static std::error_code error(DiagnosticHandlerFunction DiagnosticHandler,
563                              std::error_code EC, const Twine &Message) {
564   BitcodeDiagnosticInfo DI(EC, DS_Error, Message);
565   DiagnosticHandler(DI);
566   return EC;
567 }
568 
569 static std::error_code error(DiagnosticHandlerFunction DiagnosticHandler,
570                              std::error_code EC) {
571   return error(DiagnosticHandler, EC, EC.message());
572 }
573 
574 static std::error_code error(LLVMContext &Context, std::error_code EC,
575                              const Twine &Message) {
576   return error([&](const DiagnosticInfo &DI) { Context.diagnose(DI); }, EC,
577                Message);
578 }
579 
580 static std::error_code error(LLVMContext &Context, std::error_code EC) {
581   return error(Context, EC, EC.message());
582 }
583 
584 static std::error_code error(LLVMContext &Context, const Twine &Message) {
585   return error(Context, make_error_code(BitcodeError::CorruptedBitcode),
586                Message);
587 }
588 
589 std::error_code BitcodeReader::error(BitcodeError E, const Twine &Message) {
590   if (!ProducerIdentification.empty()) {
591     return ::error(Context, make_error_code(E),
592                    Message + " (Producer: '" + ProducerIdentification +
593                        "' Reader: 'LLVM " + LLVM_VERSION_STRING "')");
594   }
595   return ::error(Context, make_error_code(E), Message);
596 }
597 
598 std::error_code BitcodeReader::error(const Twine &Message) {
599   if (!ProducerIdentification.empty()) {
600     return ::error(Context, make_error_code(BitcodeError::CorruptedBitcode),
601                    Message + " (Producer: '" + ProducerIdentification +
602                        "' Reader: 'LLVM " + LLVM_VERSION_STRING "')");
603   }
604   return ::error(Context, make_error_code(BitcodeError::CorruptedBitcode),
605                  Message);
606 }
607 
608 std::error_code BitcodeReader::error(BitcodeError E) {
609   return ::error(Context, make_error_code(E));
610 }
611 
612 BitcodeReader::BitcodeReader(MemoryBuffer *Buffer, LLVMContext &Context)
613     : Context(Context), Buffer(Buffer), ValueList(Context),
614       MetadataList(Context) {}
615 
616 BitcodeReader::BitcodeReader(LLVMContext &Context)
617     : Context(Context), Buffer(nullptr), ValueList(Context),
618       MetadataList(Context) {}
619 
620 std::error_code BitcodeReader::materializeForwardReferencedFunctions() {
621   if (WillMaterializeAllForwardRefs)
622     return std::error_code();
623 
624   // Prevent recursion.
625   WillMaterializeAllForwardRefs = true;
626 
627   while (!BasicBlockFwdRefQueue.empty()) {
628     Function *F = BasicBlockFwdRefQueue.front();
629     BasicBlockFwdRefQueue.pop_front();
630     assert(F && "Expected valid function");
631     if (!BasicBlockFwdRefs.count(F))
632       // Already materialized.
633       continue;
634 
635     // Check for a function that isn't materializable to prevent an infinite
636     // loop.  When parsing a blockaddress stored in a global variable, there
637     // isn't a trivial way to check if a function will have a body without a
638     // linear search through FunctionsWithBodies, so just check it here.
639     if (!F->isMaterializable())
640       return error("Never resolved function from blockaddress");
641 
642     // Try to materialize F.
643     if (std::error_code EC = materialize(F))
644       return EC;
645   }
646   assert(BasicBlockFwdRefs.empty() && "Function missing from queue");
647 
648   // Reset state.
649   WillMaterializeAllForwardRefs = false;
650   return std::error_code();
651 }
652 
653 void BitcodeReader::freeState() {
654   Buffer = nullptr;
655   std::vector<Type*>().swap(TypeList);
656   ValueList.clear();
657   MetadataList.clear();
658   std::vector<Comdat *>().swap(ComdatList);
659 
660   std::vector<AttributeSet>().swap(MAttributes);
661   std::vector<BasicBlock*>().swap(FunctionBBs);
662   std::vector<Function*>().swap(FunctionsWithBodies);
663   DeferredFunctionInfo.clear();
664   DeferredMetadataInfo.clear();
665   MDKindMap.clear();
666 
667   assert(BasicBlockFwdRefs.empty() && "Unresolved blockaddress fwd references");
668   BasicBlockFwdRefQueue.clear();
669 }
670 
671 //===----------------------------------------------------------------------===//
672 //  Helper functions to implement forward reference resolution, etc.
673 //===----------------------------------------------------------------------===//
674 
675 /// Convert a string from a record into an std::string, return true on failure.
676 template <typename StrTy>
677 static bool convertToString(ArrayRef<uint64_t> Record, unsigned Idx,
678                             StrTy &Result) {
679   if (Idx > Record.size())
680     return true;
681 
682   for (unsigned i = Idx, e = Record.size(); i != e; ++i)
683     Result += (char)Record[i];
684   return false;
685 }
686 
687 static bool hasImplicitComdat(size_t Val) {
688   switch (Val) {
689   default:
690     return false;
691   case 1:  // Old WeakAnyLinkage
692   case 4:  // Old LinkOnceAnyLinkage
693   case 10: // Old WeakODRLinkage
694   case 11: // Old LinkOnceODRLinkage
695     return true;
696   }
697 }
698 
699 static GlobalValue::LinkageTypes getDecodedLinkage(unsigned Val) {
700   switch (Val) {
701   default: // Map unknown/new linkages to external
702   case 0:
703     return GlobalValue::ExternalLinkage;
704   case 2:
705     return GlobalValue::AppendingLinkage;
706   case 3:
707     return GlobalValue::InternalLinkage;
708   case 5:
709     return GlobalValue::ExternalLinkage; // Obsolete DLLImportLinkage
710   case 6:
711     return GlobalValue::ExternalLinkage; // Obsolete DLLExportLinkage
712   case 7:
713     return GlobalValue::ExternalWeakLinkage;
714   case 8:
715     return GlobalValue::CommonLinkage;
716   case 9:
717     return GlobalValue::PrivateLinkage;
718   case 12:
719     return GlobalValue::AvailableExternallyLinkage;
720   case 13:
721     return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateLinkage
722   case 14:
723     return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateWeakLinkage
724   case 15:
725     return GlobalValue::ExternalLinkage; // Obsolete LinkOnceODRAutoHideLinkage
726   case 1: // Old value with implicit comdat.
727   case 16:
728     return GlobalValue::WeakAnyLinkage;
729   case 10: // Old value with implicit comdat.
730   case 17:
731     return GlobalValue::WeakODRLinkage;
732   case 4: // Old value with implicit comdat.
733   case 18:
734     return GlobalValue::LinkOnceAnyLinkage;
735   case 11: // Old value with implicit comdat.
736   case 19:
737     return GlobalValue::LinkOnceODRLinkage;
738   }
739 }
740 
741 // Decode the flags for GlobalValue in the summary
742 static GlobalValueSummary::GVFlags getDecodedGVSummaryFlags(uint64_t RawFlags,
743                                                             uint64_t Version) {
744   // Summary were not emitted before LLVM 3.9, we don't need to upgrade Linkage
745   // like getDecodedLinkage() above. Any future change to the linkage enum and
746   // to getDecodedLinkage() will need to be taken into account here as above.
747   auto Linkage = GlobalValue::LinkageTypes(RawFlags & 0xF); // 4 bits
748   RawFlags = RawFlags >> 4;
749   auto HasSection = RawFlags & 0x1; // bool
750   return GlobalValueSummary::GVFlags(Linkage, HasSection);
751 }
752 
753 static GlobalValue::VisibilityTypes getDecodedVisibility(unsigned Val) {
754   switch (Val) {
755   default: // Map unknown visibilities to default.
756   case 0: return GlobalValue::DefaultVisibility;
757   case 1: return GlobalValue::HiddenVisibility;
758   case 2: return GlobalValue::ProtectedVisibility;
759   }
760 }
761 
762 static GlobalValue::DLLStorageClassTypes
763 getDecodedDLLStorageClass(unsigned Val) {
764   switch (Val) {
765   default: // Map unknown values to default.
766   case 0: return GlobalValue::DefaultStorageClass;
767   case 1: return GlobalValue::DLLImportStorageClass;
768   case 2: return GlobalValue::DLLExportStorageClass;
769   }
770 }
771 
772 static GlobalVariable::ThreadLocalMode getDecodedThreadLocalMode(unsigned Val) {
773   switch (Val) {
774     case 0: return GlobalVariable::NotThreadLocal;
775     default: // Map unknown non-zero value to general dynamic.
776     case 1: return GlobalVariable::GeneralDynamicTLSModel;
777     case 2: return GlobalVariable::LocalDynamicTLSModel;
778     case 3: return GlobalVariable::InitialExecTLSModel;
779     case 4: return GlobalVariable::LocalExecTLSModel;
780   }
781 }
782 
783 static int getDecodedCastOpcode(unsigned Val) {
784   switch (Val) {
785   default: return -1;
786   case bitc::CAST_TRUNC   : return Instruction::Trunc;
787   case bitc::CAST_ZEXT    : return Instruction::ZExt;
788   case bitc::CAST_SEXT    : return Instruction::SExt;
789   case bitc::CAST_FPTOUI  : return Instruction::FPToUI;
790   case bitc::CAST_FPTOSI  : return Instruction::FPToSI;
791   case bitc::CAST_UITOFP  : return Instruction::UIToFP;
792   case bitc::CAST_SITOFP  : return Instruction::SIToFP;
793   case bitc::CAST_FPTRUNC : return Instruction::FPTrunc;
794   case bitc::CAST_FPEXT   : return Instruction::FPExt;
795   case bitc::CAST_PTRTOINT: return Instruction::PtrToInt;
796   case bitc::CAST_INTTOPTR: return Instruction::IntToPtr;
797   case bitc::CAST_BITCAST : return Instruction::BitCast;
798   case bitc::CAST_ADDRSPACECAST: return Instruction::AddrSpaceCast;
799   }
800 }
801 
802 static int getDecodedBinaryOpcode(unsigned Val, Type *Ty) {
803   bool IsFP = Ty->isFPOrFPVectorTy();
804   // BinOps are only valid for int/fp or vector of int/fp types
805   if (!IsFP && !Ty->isIntOrIntVectorTy())
806     return -1;
807 
808   switch (Val) {
809   default:
810     return -1;
811   case bitc::BINOP_ADD:
812     return IsFP ? Instruction::FAdd : Instruction::Add;
813   case bitc::BINOP_SUB:
814     return IsFP ? Instruction::FSub : Instruction::Sub;
815   case bitc::BINOP_MUL:
816     return IsFP ? Instruction::FMul : Instruction::Mul;
817   case bitc::BINOP_UDIV:
818     return IsFP ? -1 : Instruction::UDiv;
819   case bitc::BINOP_SDIV:
820     return IsFP ? Instruction::FDiv : Instruction::SDiv;
821   case bitc::BINOP_UREM:
822     return IsFP ? -1 : Instruction::URem;
823   case bitc::BINOP_SREM:
824     return IsFP ? Instruction::FRem : Instruction::SRem;
825   case bitc::BINOP_SHL:
826     return IsFP ? -1 : Instruction::Shl;
827   case bitc::BINOP_LSHR:
828     return IsFP ? -1 : Instruction::LShr;
829   case bitc::BINOP_ASHR:
830     return IsFP ? -1 : Instruction::AShr;
831   case bitc::BINOP_AND:
832     return IsFP ? -1 : Instruction::And;
833   case bitc::BINOP_OR:
834     return IsFP ? -1 : Instruction::Or;
835   case bitc::BINOP_XOR:
836     return IsFP ? -1 : Instruction::Xor;
837   }
838 }
839 
840 static AtomicRMWInst::BinOp getDecodedRMWOperation(unsigned Val) {
841   switch (Val) {
842   default: return AtomicRMWInst::BAD_BINOP;
843   case bitc::RMW_XCHG: return AtomicRMWInst::Xchg;
844   case bitc::RMW_ADD: return AtomicRMWInst::Add;
845   case bitc::RMW_SUB: return AtomicRMWInst::Sub;
846   case bitc::RMW_AND: return AtomicRMWInst::And;
847   case bitc::RMW_NAND: return AtomicRMWInst::Nand;
848   case bitc::RMW_OR: return AtomicRMWInst::Or;
849   case bitc::RMW_XOR: return AtomicRMWInst::Xor;
850   case bitc::RMW_MAX: return AtomicRMWInst::Max;
851   case bitc::RMW_MIN: return AtomicRMWInst::Min;
852   case bitc::RMW_UMAX: return AtomicRMWInst::UMax;
853   case bitc::RMW_UMIN: return AtomicRMWInst::UMin;
854   }
855 }
856 
857 static AtomicOrdering getDecodedOrdering(unsigned Val) {
858   switch (Val) {
859   case bitc::ORDERING_NOTATOMIC: return AtomicOrdering::NotAtomic;
860   case bitc::ORDERING_UNORDERED: return AtomicOrdering::Unordered;
861   case bitc::ORDERING_MONOTONIC: return AtomicOrdering::Monotonic;
862   case bitc::ORDERING_ACQUIRE: return AtomicOrdering::Acquire;
863   case bitc::ORDERING_RELEASE: return AtomicOrdering::Release;
864   case bitc::ORDERING_ACQREL: return AtomicOrdering::AcquireRelease;
865   default: // Map unknown orderings to sequentially-consistent.
866   case bitc::ORDERING_SEQCST: return AtomicOrdering::SequentiallyConsistent;
867   }
868 }
869 
870 static SynchronizationScope getDecodedSynchScope(unsigned Val) {
871   switch (Val) {
872   case bitc::SYNCHSCOPE_SINGLETHREAD: return SingleThread;
873   default: // Map unknown scopes to cross-thread.
874   case bitc::SYNCHSCOPE_CROSSTHREAD: return CrossThread;
875   }
876 }
877 
878 static Comdat::SelectionKind getDecodedComdatSelectionKind(unsigned Val) {
879   switch (Val) {
880   default: // Map unknown selection kinds to any.
881   case bitc::COMDAT_SELECTION_KIND_ANY:
882     return Comdat::Any;
883   case bitc::COMDAT_SELECTION_KIND_EXACT_MATCH:
884     return Comdat::ExactMatch;
885   case bitc::COMDAT_SELECTION_KIND_LARGEST:
886     return Comdat::Largest;
887   case bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES:
888     return Comdat::NoDuplicates;
889   case bitc::COMDAT_SELECTION_KIND_SAME_SIZE:
890     return Comdat::SameSize;
891   }
892 }
893 
894 static FastMathFlags getDecodedFastMathFlags(unsigned Val) {
895   FastMathFlags FMF;
896   if (0 != (Val & FastMathFlags::UnsafeAlgebra))
897     FMF.setUnsafeAlgebra();
898   if (0 != (Val & FastMathFlags::NoNaNs))
899     FMF.setNoNaNs();
900   if (0 != (Val & FastMathFlags::NoInfs))
901     FMF.setNoInfs();
902   if (0 != (Val & FastMathFlags::NoSignedZeros))
903     FMF.setNoSignedZeros();
904   if (0 != (Val & FastMathFlags::AllowReciprocal))
905     FMF.setAllowReciprocal();
906   return FMF;
907 }
908 
909 static void upgradeDLLImportExportLinkage(llvm::GlobalValue *GV, unsigned Val) {
910   switch (Val) {
911   case 5: GV->setDLLStorageClass(GlobalValue::DLLImportStorageClass); break;
912   case 6: GV->setDLLStorageClass(GlobalValue::DLLExportStorageClass); break;
913   }
914 }
915 
916 namespace llvm {
917 namespace {
918 /// \brief A class for maintaining the slot number definition
919 /// as a placeholder for the actual definition for forward constants defs.
920 class ConstantPlaceHolder : public ConstantExpr {
921   void operator=(const ConstantPlaceHolder &) = delete;
922 
923 public:
924   // allocate space for exactly one operand
925   void *operator new(size_t s) { return User::operator new(s, 1); }
926   explicit ConstantPlaceHolder(Type *Ty, LLVMContext &Context)
927       : ConstantExpr(Ty, Instruction::UserOp1, &Op<0>(), 1) {
928     Op<0>() = UndefValue::get(Type::getInt32Ty(Context));
929   }
930 
931   /// \brief Methods to support type inquiry through isa, cast, and dyn_cast.
932   static bool classof(const Value *V) {
933     return isa<ConstantExpr>(V) &&
934            cast<ConstantExpr>(V)->getOpcode() == Instruction::UserOp1;
935   }
936 
937   /// Provide fast operand accessors
938   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
939 };
940 } // end anonymous namespace
941 
942 // FIXME: can we inherit this from ConstantExpr?
943 template <>
944 struct OperandTraits<ConstantPlaceHolder> :
945   public FixedNumOperandTraits<ConstantPlaceHolder, 1> {
946 };
947 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantPlaceHolder, Value)
948 } // end namespace llvm
949 
950 void BitcodeReaderValueList::assignValue(Value *V, unsigned Idx) {
951   if (Idx == size()) {
952     push_back(V);
953     return;
954   }
955 
956   if (Idx >= size())
957     resize(Idx+1);
958 
959   WeakVH &OldV = ValuePtrs[Idx];
960   if (!OldV) {
961     OldV = V;
962     return;
963   }
964 
965   // Handle constants and non-constants (e.g. instrs) differently for
966   // efficiency.
967   if (Constant *PHC = dyn_cast<Constant>(&*OldV)) {
968     ResolveConstants.push_back(std::make_pair(PHC, Idx));
969     OldV = V;
970   } else {
971     // If there was a forward reference to this value, replace it.
972     Value *PrevVal = OldV;
973     OldV->replaceAllUsesWith(V);
974     delete PrevVal;
975   }
976 }
977 
978 Constant *BitcodeReaderValueList::getConstantFwdRef(unsigned Idx,
979                                                     Type *Ty) {
980   if (Idx >= size())
981     resize(Idx + 1);
982 
983   if (Value *V = ValuePtrs[Idx]) {
984     if (Ty != V->getType())
985       report_fatal_error("Type mismatch in constant table!");
986     return cast<Constant>(V);
987   }
988 
989   // Create and return a placeholder, which will later be RAUW'd.
990   Constant *C = new ConstantPlaceHolder(Ty, Context);
991   ValuePtrs[Idx] = C;
992   return C;
993 }
994 
995 Value *BitcodeReaderValueList::getValueFwdRef(unsigned Idx, Type *Ty) {
996   // Bail out for a clearly invalid value. This would make us call resize(0)
997   if (Idx == UINT_MAX)
998     return nullptr;
999 
1000   if (Idx >= size())
1001     resize(Idx + 1);
1002 
1003   if (Value *V = ValuePtrs[Idx]) {
1004     // If the types don't match, it's invalid.
1005     if (Ty && Ty != V->getType())
1006       return nullptr;
1007     return V;
1008   }
1009 
1010   // No type specified, must be invalid reference.
1011   if (!Ty) return nullptr;
1012 
1013   // Create and return a placeholder, which will later be RAUW'd.
1014   Value *V = new Argument(Ty);
1015   ValuePtrs[Idx] = V;
1016   return V;
1017 }
1018 
1019 /// Once all constants are read, this method bulk resolves any forward
1020 /// references.  The idea behind this is that we sometimes get constants (such
1021 /// as large arrays) which reference *many* forward ref constants.  Replacing
1022 /// each of these causes a lot of thrashing when building/reuniquing the
1023 /// constant.  Instead of doing this, we look at all the uses and rewrite all
1024 /// the place holders at once for any constant that uses a placeholder.
1025 void BitcodeReaderValueList::resolveConstantForwardRefs() {
1026   // Sort the values by-pointer so that they are efficient to look up with a
1027   // binary search.
1028   std::sort(ResolveConstants.begin(), ResolveConstants.end());
1029 
1030   SmallVector<Constant*, 64> NewOps;
1031 
1032   while (!ResolveConstants.empty()) {
1033     Value *RealVal = operator[](ResolveConstants.back().second);
1034     Constant *Placeholder = ResolveConstants.back().first;
1035     ResolveConstants.pop_back();
1036 
1037     // Loop over all users of the placeholder, updating them to reference the
1038     // new value.  If they reference more than one placeholder, update them all
1039     // at once.
1040     while (!Placeholder->use_empty()) {
1041       auto UI = Placeholder->user_begin();
1042       User *U = *UI;
1043 
1044       // If the using object isn't uniqued, just update the operands.  This
1045       // handles instructions and initializers for global variables.
1046       if (!isa<Constant>(U) || isa<GlobalValue>(U)) {
1047         UI.getUse().set(RealVal);
1048         continue;
1049       }
1050 
1051       // Otherwise, we have a constant that uses the placeholder.  Replace that
1052       // constant with a new constant that has *all* placeholder uses updated.
1053       Constant *UserC = cast<Constant>(U);
1054       for (User::op_iterator I = UserC->op_begin(), E = UserC->op_end();
1055            I != E; ++I) {
1056         Value *NewOp;
1057         if (!isa<ConstantPlaceHolder>(*I)) {
1058           // Not a placeholder reference.
1059           NewOp = *I;
1060         } else if (*I == Placeholder) {
1061           // Common case is that it just references this one placeholder.
1062           NewOp = RealVal;
1063         } else {
1064           // Otherwise, look up the placeholder in ResolveConstants.
1065           ResolveConstantsTy::iterator It =
1066             std::lower_bound(ResolveConstants.begin(), ResolveConstants.end(),
1067                              std::pair<Constant*, unsigned>(cast<Constant>(*I),
1068                                                             0));
1069           assert(It != ResolveConstants.end() && It->first == *I);
1070           NewOp = operator[](It->second);
1071         }
1072 
1073         NewOps.push_back(cast<Constant>(NewOp));
1074       }
1075 
1076       // Make the new constant.
1077       Constant *NewC;
1078       if (ConstantArray *UserCA = dyn_cast<ConstantArray>(UserC)) {
1079         NewC = ConstantArray::get(UserCA->getType(), NewOps);
1080       } else if (ConstantStruct *UserCS = dyn_cast<ConstantStruct>(UserC)) {
1081         NewC = ConstantStruct::get(UserCS->getType(), NewOps);
1082       } else if (isa<ConstantVector>(UserC)) {
1083         NewC = ConstantVector::get(NewOps);
1084       } else {
1085         assert(isa<ConstantExpr>(UserC) && "Must be a ConstantExpr.");
1086         NewC = cast<ConstantExpr>(UserC)->getWithOperands(NewOps);
1087       }
1088 
1089       UserC->replaceAllUsesWith(NewC);
1090       UserC->destroyConstant();
1091       NewOps.clear();
1092     }
1093 
1094     // Update all ValueHandles, they should be the only users at this point.
1095     Placeholder->replaceAllUsesWith(RealVal);
1096     delete Placeholder;
1097   }
1098 }
1099 
1100 void BitcodeReaderMetadataList::assignValue(Metadata *MD, unsigned Idx) {
1101   if (Idx == size()) {
1102     push_back(MD);
1103     return;
1104   }
1105 
1106   if (Idx >= size())
1107     resize(Idx+1);
1108 
1109   TrackingMDRef &OldMD = MetadataPtrs[Idx];
1110   if (!OldMD) {
1111     OldMD.reset(MD);
1112     return;
1113   }
1114 
1115   // If there was a forward reference to this value, replace it.
1116   TempMDTuple PrevMD(cast<MDTuple>(OldMD.get()));
1117   PrevMD->replaceAllUsesWith(MD);
1118   --NumFwdRefs;
1119 }
1120 
1121 Metadata *BitcodeReaderMetadataList::getMetadataFwdRef(unsigned Idx) {
1122   if (Idx >= size())
1123     resize(Idx + 1);
1124 
1125   if (Metadata *MD = MetadataPtrs[Idx])
1126     return MD;
1127 
1128   // Track forward refs to be resolved later.
1129   if (AnyFwdRefs) {
1130     MinFwdRef = std::min(MinFwdRef, Idx);
1131     MaxFwdRef = std::max(MaxFwdRef, Idx);
1132   } else {
1133     AnyFwdRefs = true;
1134     MinFwdRef = MaxFwdRef = Idx;
1135   }
1136   ++NumFwdRefs;
1137 
1138   // Create and return a placeholder, which will later be RAUW'd.
1139   Metadata *MD = MDNode::getTemporary(Context, None).release();
1140   MetadataPtrs[Idx].reset(MD);
1141   return MD;
1142 }
1143 
1144 Metadata *BitcodeReaderMetadataList::getMetadataIfResolved(unsigned Idx) {
1145   Metadata *MD = lookup(Idx);
1146   if (auto *N = dyn_cast_or_null<MDNode>(MD))
1147     if (!N->isResolved())
1148       return nullptr;
1149   return MD;
1150 }
1151 
1152 MDNode *BitcodeReaderMetadataList::getMDNodeFwdRefOrNull(unsigned Idx) {
1153   return dyn_cast_or_null<MDNode>(getMetadataFwdRef(Idx));
1154 }
1155 
1156 void BitcodeReaderMetadataList::tryToResolveCycles() {
1157   if (NumFwdRefs)
1158     // Still forward references... can't resolve cycles.
1159     return;
1160 
1161   bool DidReplaceTypeRefs = false;
1162 
1163   // Give up on finding a full definition for any forward decls that remain.
1164   for (const auto &Ref : OldTypeRefs.FwdDecls)
1165     OldTypeRefs.Final.insert(Ref);
1166   OldTypeRefs.FwdDecls.clear();
1167 
1168   // Upgrade from old type ref arrays.  In strange cases, this could add to
1169   // OldTypeRefs.Unknown.
1170   for (const auto &Array : OldTypeRefs.Arrays) {
1171     DidReplaceTypeRefs = true;
1172     Array.second->replaceAllUsesWith(resolveTypeRefArray(Array.first.get()));
1173   }
1174   OldTypeRefs.Arrays.clear();
1175 
1176   // Replace old string-based type refs with the resolved node, if possible.
1177   // If we haven't seen the node, leave it to the verifier to complain about
1178   // the invalid string reference.
1179   for (const auto &Ref : OldTypeRefs.Unknown) {
1180     DidReplaceTypeRefs = true;
1181     if (DICompositeType *CT = OldTypeRefs.Final.lookup(Ref.first))
1182       Ref.second->replaceAllUsesWith(CT);
1183     else
1184       Ref.second->replaceAllUsesWith(Ref.first);
1185   }
1186   OldTypeRefs.Unknown.clear();
1187 
1188   // Make sure all the upgraded types are resolved.
1189   if (DidReplaceTypeRefs) {
1190     AnyFwdRefs = true;
1191     MinFwdRef = 0;
1192     MaxFwdRef = MetadataPtrs.size() - 1;
1193   }
1194 
1195   if (!AnyFwdRefs)
1196     // Nothing to do.
1197     return;
1198 
1199   // Resolve any cycles.
1200   for (unsigned I = MinFwdRef, E = MaxFwdRef + 1; I != E; ++I) {
1201     auto &MD = MetadataPtrs[I];
1202     auto *N = dyn_cast_or_null<MDNode>(MD);
1203     if (!N)
1204       continue;
1205 
1206     assert(!N->isTemporary() && "Unexpected forward reference");
1207     N->resolveCycles();
1208   }
1209 
1210   // Make sure we return early again until there's another forward ref.
1211   AnyFwdRefs = false;
1212 }
1213 
1214 void BitcodeReaderMetadataList::addTypeRef(MDString &UUID,
1215                                            DICompositeType &CT) {
1216   assert(CT.getRawIdentifier() == &UUID && "Mismatched UUID");
1217   if (CT.isForwardDecl())
1218     OldTypeRefs.FwdDecls.insert(std::make_pair(&UUID, &CT));
1219   else
1220     OldTypeRefs.Final.insert(std::make_pair(&UUID, &CT));
1221 }
1222 
1223 Metadata *BitcodeReaderMetadataList::upgradeTypeRef(Metadata *MaybeUUID) {
1224   auto *UUID = dyn_cast_or_null<MDString>(MaybeUUID);
1225   if (LLVM_LIKELY(!UUID))
1226     return MaybeUUID;
1227 
1228   if (auto *CT = OldTypeRefs.Final.lookup(UUID))
1229     return CT;
1230 
1231   auto &Ref = OldTypeRefs.Unknown[UUID];
1232   if (!Ref)
1233     Ref = MDNode::getTemporary(Context, None);
1234   return Ref.get();
1235 }
1236 
1237 Metadata *BitcodeReaderMetadataList::upgradeTypeRefArray(Metadata *MaybeTuple) {
1238   auto *Tuple = dyn_cast_or_null<MDTuple>(MaybeTuple);
1239   if (!Tuple || Tuple->isDistinct())
1240     return MaybeTuple;
1241 
1242   // Look through the array immediately if possible.
1243   if (!Tuple->isTemporary())
1244     return resolveTypeRefArray(Tuple);
1245 
1246   // Create and return a placeholder to use for now.  Eventually
1247   // resolveTypeRefArrays() will be resolve this forward reference.
1248   OldTypeRefs.Arrays.emplace_back();
1249   OldTypeRefs.Arrays.back().first.reset(Tuple);
1250   OldTypeRefs.Arrays.back().second = MDTuple::getTemporary(Context, None);
1251   return OldTypeRefs.Arrays.back().second.get();
1252 }
1253 
1254 Metadata *BitcodeReaderMetadataList::resolveTypeRefArray(Metadata *MaybeTuple) {
1255   auto *Tuple = dyn_cast_or_null<MDTuple>(MaybeTuple);
1256   if (!Tuple || Tuple->isDistinct())
1257     return MaybeTuple;
1258 
1259   // Look through the DITypeRefArray, upgrading each DITypeRef.
1260   SmallVector<Metadata *, 32> Ops;
1261   Ops.reserve(Tuple->getNumOperands());
1262   for (Metadata *MD : Tuple->operands())
1263     Ops.push_back(upgradeTypeRef(MD));
1264 
1265   return MDTuple::get(Context, Ops);
1266 }
1267 
1268 Type *BitcodeReader::getTypeByID(unsigned ID) {
1269   // The type table size is always specified correctly.
1270   if (ID >= TypeList.size())
1271     return nullptr;
1272 
1273   if (Type *Ty = TypeList[ID])
1274     return Ty;
1275 
1276   // If we have a forward reference, the only possible case is when it is to a
1277   // named struct.  Just create a placeholder for now.
1278   return TypeList[ID] = createIdentifiedStructType(Context);
1279 }
1280 
1281 StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context,
1282                                                       StringRef Name) {
1283   auto *Ret = StructType::create(Context, Name);
1284   IdentifiedStructTypes.push_back(Ret);
1285   return Ret;
1286 }
1287 
1288 StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context) {
1289   auto *Ret = StructType::create(Context);
1290   IdentifiedStructTypes.push_back(Ret);
1291   return Ret;
1292 }
1293 
1294 //===----------------------------------------------------------------------===//
1295 //  Functions for parsing blocks from the bitcode file
1296 //===----------------------------------------------------------------------===//
1297 
1298 
1299 /// \brief This fills an AttrBuilder object with the LLVM attributes that have
1300 /// been decoded from the given integer. This function must stay in sync with
1301 /// 'encodeLLVMAttributesForBitcode'.
1302 static void decodeLLVMAttributesForBitcode(AttrBuilder &B,
1303                                            uint64_t EncodedAttrs) {
1304   // FIXME: Remove in 4.0.
1305 
1306   // The alignment is stored as a 16-bit raw value from bits 31--16.  We shift
1307   // the bits above 31 down by 11 bits.
1308   unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
1309   assert((!Alignment || isPowerOf2_32(Alignment)) &&
1310          "Alignment must be a power of two.");
1311 
1312   if (Alignment)
1313     B.addAlignmentAttr(Alignment);
1314   B.addRawValue(((EncodedAttrs & (0xfffffULL << 32)) >> 11) |
1315                 (EncodedAttrs & 0xffff));
1316 }
1317 
1318 std::error_code BitcodeReader::parseAttributeBlock() {
1319   if (Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID))
1320     return error("Invalid record");
1321 
1322   if (!MAttributes.empty())
1323     return error("Invalid multiple blocks");
1324 
1325   SmallVector<uint64_t, 64> Record;
1326 
1327   SmallVector<AttributeSet, 8> Attrs;
1328 
1329   // Read all the records.
1330   while (1) {
1331     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
1332 
1333     switch (Entry.Kind) {
1334     case BitstreamEntry::SubBlock: // Handled for us already.
1335     case BitstreamEntry::Error:
1336       return error("Malformed block");
1337     case BitstreamEntry::EndBlock:
1338       return std::error_code();
1339     case BitstreamEntry::Record:
1340       // The interesting case.
1341       break;
1342     }
1343 
1344     // Read a record.
1345     Record.clear();
1346     switch (Stream.readRecord(Entry.ID, Record)) {
1347     default:  // Default behavior: ignore.
1348       break;
1349     case bitc::PARAMATTR_CODE_ENTRY_OLD: { // ENTRY: [paramidx0, attr0, ...]
1350       // FIXME: Remove in 4.0.
1351       if (Record.size() & 1)
1352         return error("Invalid record");
1353 
1354       for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
1355         AttrBuilder B;
1356         decodeLLVMAttributesForBitcode(B, Record[i+1]);
1357         Attrs.push_back(AttributeSet::get(Context, Record[i], B));
1358       }
1359 
1360       MAttributes.push_back(AttributeSet::get(Context, Attrs));
1361       Attrs.clear();
1362       break;
1363     }
1364     case bitc::PARAMATTR_CODE_ENTRY: { // ENTRY: [attrgrp0, attrgrp1, ...]
1365       for (unsigned i = 0, e = Record.size(); i != e; ++i)
1366         Attrs.push_back(MAttributeGroups[Record[i]]);
1367 
1368       MAttributes.push_back(AttributeSet::get(Context, Attrs));
1369       Attrs.clear();
1370       break;
1371     }
1372     }
1373   }
1374 }
1375 
1376 // Returns Attribute::None on unrecognized codes.
1377 static Attribute::AttrKind getAttrFromCode(uint64_t Code) {
1378   switch (Code) {
1379   default:
1380     return Attribute::None;
1381   case bitc::ATTR_KIND_ALIGNMENT:
1382     return Attribute::Alignment;
1383   case bitc::ATTR_KIND_ALWAYS_INLINE:
1384     return Attribute::AlwaysInline;
1385   case bitc::ATTR_KIND_ARGMEMONLY:
1386     return Attribute::ArgMemOnly;
1387   case bitc::ATTR_KIND_BUILTIN:
1388     return Attribute::Builtin;
1389   case bitc::ATTR_KIND_BY_VAL:
1390     return Attribute::ByVal;
1391   case bitc::ATTR_KIND_IN_ALLOCA:
1392     return Attribute::InAlloca;
1393   case bitc::ATTR_KIND_COLD:
1394     return Attribute::Cold;
1395   case bitc::ATTR_KIND_CONVERGENT:
1396     return Attribute::Convergent;
1397   case bitc::ATTR_KIND_INACCESSIBLEMEM_ONLY:
1398     return Attribute::InaccessibleMemOnly;
1399   case bitc::ATTR_KIND_INACCESSIBLEMEM_OR_ARGMEMONLY:
1400     return Attribute::InaccessibleMemOrArgMemOnly;
1401   case bitc::ATTR_KIND_INLINE_HINT:
1402     return Attribute::InlineHint;
1403   case bitc::ATTR_KIND_IN_REG:
1404     return Attribute::InReg;
1405   case bitc::ATTR_KIND_JUMP_TABLE:
1406     return Attribute::JumpTable;
1407   case bitc::ATTR_KIND_MIN_SIZE:
1408     return Attribute::MinSize;
1409   case bitc::ATTR_KIND_NAKED:
1410     return Attribute::Naked;
1411   case bitc::ATTR_KIND_NEST:
1412     return Attribute::Nest;
1413   case bitc::ATTR_KIND_NO_ALIAS:
1414     return Attribute::NoAlias;
1415   case bitc::ATTR_KIND_NO_BUILTIN:
1416     return Attribute::NoBuiltin;
1417   case bitc::ATTR_KIND_NO_CAPTURE:
1418     return Attribute::NoCapture;
1419   case bitc::ATTR_KIND_NO_DUPLICATE:
1420     return Attribute::NoDuplicate;
1421   case bitc::ATTR_KIND_NO_IMPLICIT_FLOAT:
1422     return Attribute::NoImplicitFloat;
1423   case bitc::ATTR_KIND_NO_INLINE:
1424     return Attribute::NoInline;
1425   case bitc::ATTR_KIND_NO_RECURSE:
1426     return Attribute::NoRecurse;
1427   case bitc::ATTR_KIND_NON_LAZY_BIND:
1428     return Attribute::NonLazyBind;
1429   case bitc::ATTR_KIND_NON_NULL:
1430     return Attribute::NonNull;
1431   case bitc::ATTR_KIND_DEREFERENCEABLE:
1432     return Attribute::Dereferenceable;
1433   case bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL:
1434     return Attribute::DereferenceableOrNull;
1435   case bitc::ATTR_KIND_ALLOC_SIZE:
1436     return Attribute::AllocSize;
1437   case bitc::ATTR_KIND_NO_RED_ZONE:
1438     return Attribute::NoRedZone;
1439   case bitc::ATTR_KIND_NO_RETURN:
1440     return Attribute::NoReturn;
1441   case bitc::ATTR_KIND_NO_UNWIND:
1442     return Attribute::NoUnwind;
1443   case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE:
1444     return Attribute::OptimizeForSize;
1445   case bitc::ATTR_KIND_OPTIMIZE_NONE:
1446     return Attribute::OptimizeNone;
1447   case bitc::ATTR_KIND_READ_NONE:
1448     return Attribute::ReadNone;
1449   case bitc::ATTR_KIND_READ_ONLY:
1450     return Attribute::ReadOnly;
1451   case bitc::ATTR_KIND_RETURNED:
1452     return Attribute::Returned;
1453   case bitc::ATTR_KIND_RETURNS_TWICE:
1454     return Attribute::ReturnsTwice;
1455   case bitc::ATTR_KIND_S_EXT:
1456     return Attribute::SExt;
1457   case bitc::ATTR_KIND_STACK_ALIGNMENT:
1458     return Attribute::StackAlignment;
1459   case bitc::ATTR_KIND_STACK_PROTECT:
1460     return Attribute::StackProtect;
1461   case bitc::ATTR_KIND_STACK_PROTECT_REQ:
1462     return Attribute::StackProtectReq;
1463   case bitc::ATTR_KIND_STACK_PROTECT_STRONG:
1464     return Attribute::StackProtectStrong;
1465   case bitc::ATTR_KIND_SAFESTACK:
1466     return Attribute::SafeStack;
1467   case bitc::ATTR_KIND_STRUCT_RET:
1468     return Attribute::StructRet;
1469   case bitc::ATTR_KIND_SANITIZE_ADDRESS:
1470     return Attribute::SanitizeAddress;
1471   case bitc::ATTR_KIND_SANITIZE_THREAD:
1472     return Attribute::SanitizeThread;
1473   case bitc::ATTR_KIND_SANITIZE_MEMORY:
1474     return Attribute::SanitizeMemory;
1475   case bitc::ATTR_KIND_SWIFT_ERROR:
1476     return Attribute::SwiftError;
1477   case bitc::ATTR_KIND_SWIFT_SELF:
1478     return Attribute::SwiftSelf;
1479   case bitc::ATTR_KIND_UW_TABLE:
1480     return Attribute::UWTable;
1481   case bitc::ATTR_KIND_Z_EXT:
1482     return Attribute::ZExt;
1483   }
1484 }
1485 
1486 std::error_code BitcodeReader::parseAlignmentValue(uint64_t Exponent,
1487                                                    unsigned &Alignment) {
1488   // Note: Alignment in bitcode files is incremented by 1, so that zero
1489   // can be used for default alignment.
1490   if (Exponent > Value::MaxAlignmentExponent + 1)
1491     return error("Invalid alignment value");
1492   Alignment = (1 << static_cast<unsigned>(Exponent)) >> 1;
1493   return std::error_code();
1494 }
1495 
1496 std::error_code BitcodeReader::parseAttrKind(uint64_t Code,
1497                                              Attribute::AttrKind *Kind) {
1498   *Kind = getAttrFromCode(Code);
1499   if (*Kind == Attribute::None)
1500     return error(BitcodeError::CorruptedBitcode,
1501                  "Unknown attribute kind (" + Twine(Code) + ")");
1502   return std::error_code();
1503 }
1504 
1505 std::error_code BitcodeReader::parseAttributeGroupBlock() {
1506   if (Stream.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID))
1507     return error("Invalid record");
1508 
1509   if (!MAttributeGroups.empty())
1510     return error("Invalid multiple blocks");
1511 
1512   SmallVector<uint64_t, 64> Record;
1513 
1514   // Read all the records.
1515   while (1) {
1516     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
1517 
1518     switch (Entry.Kind) {
1519     case BitstreamEntry::SubBlock: // Handled for us already.
1520     case BitstreamEntry::Error:
1521       return error("Malformed block");
1522     case BitstreamEntry::EndBlock:
1523       return std::error_code();
1524     case BitstreamEntry::Record:
1525       // The interesting case.
1526       break;
1527     }
1528 
1529     // Read a record.
1530     Record.clear();
1531     switch (Stream.readRecord(Entry.ID, Record)) {
1532     default:  // Default behavior: ignore.
1533       break;
1534     case bitc::PARAMATTR_GRP_CODE_ENTRY: { // ENTRY: [grpid, idx, a0, a1, ...]
1535       if (Record.size() < 3)
1536         return error("Invalid record");
1537 
1538       uint64_t GrpID = Record[0];
1539       uint64_t Idx = Record[1]; // Index of the object this attribute refers to.
1540 
1541       AttrBuilder B;
1542       for (unsigned i = 2, e = Record.size(); i != e; ++i) {
1543         if (Record[i] == 0) {        // Enum attribute
1544           Attribute::AttrKind Kind;
1545           if (std::error_code EC = parseAttrKind(Record[++i], &Kind))
1546             return EC;
1547 
1548           B.addAttribute(Kind);
1549         } else if (Record[i] == 1) { // Integer attribute
1550           Attribute::AttrKind Kind;
1551           if (std::error_code EC = parseAttrKind(Record[++i], &Kind))
1552             return EC;
1553           if (Kind == Attribute::Alignment)
1554             B.addAlignmentAttr(Record[++i]);
1555           else if (Kind == Attribute::StackAlignment)
1556             B.addStackAlignmentAttr(Record[++i]);
1557           else if (Kind == Attribute::Dereferenceable)
1558             B.addDereferenceableAttr(Record[++i]);
1559           else if (Kind == Attribute::DereferenceableOrNull)
1560             B.addDereferenceableOrNullAttr(Record[++i]);
1561           else if (Kind == Attribute::AllocSize)
1562             B.addAllocSizeAttrFromRawRepr(Record[++i]);
1563         } else {                     // String attribute
1564           assert((Record[i] == 3 || Record[i] == 4) &&
1565                  "Invalid attribute group entry");
1566           bool HasValue = (Record[i++] == 4);
1567           SmallString<64> KindStr;
1568           SmallString<64> ValStr;
1569 
1570           while (Record[i] != 0 && i != e)
1571             KindStr += Record[i++];
1572           assert(Record[i] == 0 && "Kind string not null terminated");
1573 
1574           if (HasValue) {
1575             // Has a value associated with it.
1576             ++i; // Skip the '0' that terminates the "kind" string.
1577             while (Record[i] != 0 && i != e)
1578               ValStr += Record[i++];
1579             assert(Record[i] == 0 && "Value string not null terminated");
1580           }
1581 
1582           B.addAttribute(KindStr.str(), ValStr.str());
1583         }
1584       }
1585 
1586       MAttributeGroups[GrpID] = AttributeSet::get(Context, Idx, B);
1587       break;
1588     }
1589     }
1590   }
1591 }
1592 
1593 std::error_code BitcodeReader::parseTypeTable() {
1594   if (Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW))
1595     return error("Invalid record");
1596 
1597   return parseTypeTableBody();
1598 }
1599 
1600 std::error_code BitcodeReader::parseTypeTableBody() {
1601   if (!TypeList.empty())
1602     return error("Invalid multiple blocks");
1603 
1604   SmallVector<uint64_t, 64> Record;
1605   unsigned NumRecords = 0;
1606 
1607   SmallString<64> TypeName;
1608 
1609   // Read all the records for this type table.
1610   while (1) {
1611     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
1612 
1613     switch (Entry.Kind) {
1614     case BitstreamEntry::SubBlock: // Handled for us already.
1615     case BitstreamEntry::Error:
1616       return error("Malformed block");
1617     case BitstreamEntry::EndBlock:
1618       if (NumRecords != TypeList.size())
1619         return error("Malformed block");
1620       return std::error_code();
1621     case BitstreamEntry::Record:
1622       // The interesting case.
1623       break;
1624     }
1625 
1626     // Read a record.
1627     Record.clear();
1628     Type *ResultTy = nullptr;
1629     switch (Stream.readRecord(Entry.ID, Record)) {
1630     default:
1631       return error("Invalid value");
1632     case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries]
1633       // TYPE_CODE_NUMENTRY contains a count of the number of types in the
1634       // type list.  This allows us to reserve space.
1635       if (Record.size() < 1)
1636         return error("Invalid record");
1637       TypeList.resize(Record[0]);
1638       continue;
1639     case bitc::TYPE_CODE_VOID:      // VOID
1640       ResultTy = Type::getVoidTy(Context);
1641       break;
1642     case bitc::TYPE_CODE_HALF:     // HALF
1643       ResultTy = Type::getHalfTy(Context);
1644       break;
1645     case bitc::TYPE_CODE_FLOAT:     // FLOAT
1646       ResultTy = Type::getFloatTy(Context);
1647       break;
1648     case bitc::TYPE_CODE_DOUBLE:    // DOUBLE
1649       ResultTy = Type::getDoubleTy(Context);
1650       break;
1651     case bitc::TYPE_CODE_X86_FP80:  // X86_FP80
1652       ResultTy = Type::getX86_FP80Ty(Context);
1653       break;
1654     case bitc::TYPE_CODE_FP128:     // FP128
1655       ResultTy = Type::getFP128Ty(Context);
1656       break;
1657     case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128
1658       ResultTy = Type::getPPC_FP128Ty(Context);
1659       break;
1660     case bitc::TYPE_CODE_LABEL:     // LABEL
1661       ResultTy = Type::getLabelTy(Context);
1662       break;
1663     case bitc::TYPE_CODE_METADATA:  // METADATA
1664       ResultTy = Type::getMetadataTy(Context);
1665       break;
1666     case bitc::TYPE_CODE_X86_MMX:   // X86_MMX
1667       ResultTy = Type::getX86_MMXTy(Context);
1668       break;
1669     case bitc::TYPE_CODE_TOKEN:     // TOKEN
1670       ResultTy = Type::getTokenTy(Context);
1671       break;
1672     case bitc::TYPE_CODE_INTEGER: { // INTEGER: [width]
1673       if (Record.size() < 1)
1674         return error("Invalid record");
1675 
1676       uint64_t NumBits = Record[0];
1677       if (NumBits < IntegerType::MIN_INT_BITS ||
1678           NumBits > IntegerType::MAX_INT_BITS)
1679         return error("Bitwidth for integer type out of range");
1680       ResultTy = IntegerType::get(Context, NumBits);
1681       break;
1682     }
1683     case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or
1684                                     //          [pointee type, address space]
1685       if (Record.size() < 1)
1686         return error("Invalid record");
1687       unsigned AddressSpace = 0;
1688       if (Record.size() == 2)
1689         AddressSpace = Record[1];
1690       ResultTy = getTypeByID(Record[0]);
1691       if (!ResultTy ||
1692           !PointerType::isValidElementType(ResultTy))
1693         return error("Invalid type");
1694       ResultTy = PointerType::get(ResultTy, AddressSpace);
1695       break;
1696     }
1697     case bitc::TYPE_CODE_FUNCTION_OLD: {
1698       // FIXME: attrid is dead, remove it in LLVM 4.0
1699       // FUNCTION: [vararg, attrid, retty, paramty x N]
1700       if (Record.size() < 3)
1701         return error("Invalid record");
1702       SmallVector<Type*, 8> ArgTys;
1703       for (unsigned i = 3, e = Record.size(); i != e; ++i) {
1704         if (Type *T = getTypeByID(Record[i]))
1705           ArgTys.push_back(T);
1706         else
1707           break;
1708       }
1709 
1710       ResultTy = getTypeByID(Record[2]);
1711       if (!ResultTy || ArgTys.size() < Record.size()-3)
1712         return error("Invalid type");
1713 
1714       ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
1715       break;
1716     }
1717     case bitc::TYPE_CODE_FUNCTION: {
1718       // FUNCTION: [vararg, retty, paramty x N]
1719       if (Record.size() < 2)
1720         return error("Invalid record");
1721       SmallVector<Type*, 8> ArgTys;
1722       for (unsigned i = 2, e = Record.size(); i != e; ++i) {
1723         if (Type *T = getTypeByID(Record[i])) {
1724           if (!FunctionType::isValidArgumentType(T))
1725             return error("Invalid function argument type");
1726           ArgTys.push_back(T);
1727         }
1728         else
1729           break;
1730       }
1731 
1732       ResultTy = getTypeByID(Record[1]);
1733       if (!ResultTy || ArgTys.size() < Record.size()-2)
1734         return error("Invalid type");
1735 
1736       ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
1737       break;
1738     }
1739     case bitc::TYPE_CODE_STRUCT_ANON: {  // STRUCT: [ispacked, eltty x N]
1740       if (Record.size() < 1)
1741         return error("Invalid record");
1742       SmallVector<Type*, 8> EltTys;
1743       for (unsigned i = 1, e = Record.size(); i != e; ++i) {
1744         if (Type *T = getTypeByID(Record[i]))
1745           EltTys.push_back(T);
1746         else
1747           break;
1748       }
1749       if (EltTys.size() != Record.size()-1)
1750         return error("Invalid type");
1751       ResultTy = StructType::get(Context, EltTys, Record[0]);
1752       break;
1753     }
1754     case bitc::TYPE_CODE_STRUCT_NAME:   // STRUCT_NAME: [strchr x N]
1755       if (convertToString(Record, 0, TypeName))
1756         return error("Invalid record");
1757       continue;
1758 
1759     case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N]
1760       if (Record.size() < 1)
1761         return error("Invalid record");
1762 
1763       if (NumRecords >= TypeList.size())
1764         return error("Invalid TYPE table");
1765 
1766       // Check to see if this was forward referenced, if so fill in the temp.
1767       StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
1768       if (Res) {
1769         Res->setName(TypeName);
1770         TypeList[NumRecords] = nullptr;
1771       } else  // Otherwise, create a new struct.
1772         Res = createIdentifiedStructType(Context, TypeName);
1773       TypeName.clear();
1774 
1775       SmallVector<Type*, 8> EltTys;
1776       for (unsigned i = 1, e = Record.size(); i != e; ++i) {
1777         if (Type *T = getTypeByID(Record[i]))
1778           EltTys.push_back(T);
1779         else
1780           break;
1781       }
1782       if (EltTys.size() != Record.size()-1)
1783         return error("Invalid record");
1784       Res->setBody(EltTys, Record[0]);
1785       ResultTy = Res;
1786       break;
1787     }
1788     case bitc::TYPE_CODE_OPAQUE: {       // OPAQUE: []
1789       if (Record.size() != 1)
1790         return error("Invalid record");
1791 
1792       if (NumRecords >= TypeList.size())
1793         return error("Invalid TYPE table");
1794 
1795       // Check to see if this was forward referenced, if so fill in the temp.
1796       StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
1797       if (Res) {
1798         Res->setName(TypeName);
1799         TypeList[NumRecords] = nullptr;
1800       } else  // Otherwise, create a new struct with no body.
1801         Res = createIdentifiedStructType(Context, TypeName);
1802       TypeName.clear();
1803       ResultTy = Res;
1804       break;
1805     }
1806     case bitc::TYPE_CODE_ARRAY:     // ARRAY: [numelts, eltty]
1807       if (Record.size() < 2)
1808         return error("Invalid record");
1809       ResultTy = getTypeByID(Record[1]);
1810       if (!ResultTy || !ArrayType::isValidElementType(ResultTy))
1811         return error("Invalid type");
1812       ResultTy = ArrayType::get(ResultTy, Record[0]);
1813       break;
1814     case bitc::TYPE_CODE_VECTOR:    // VECTOR: [numelts, eltty]
1815       if (Record.size() < 2)
1816         return error("Invalid record");
1817       if (Record[0] == 0)
1818         return error("Invalid vector length");
1819       ResultTy = getTypeByID(Record[1]);
1820       if (!ResultTy || !StructType::isValidElementType(ResultTy))
1821         return error("Invalid type");
1822       ResultTy = VectorType::get(ResultTy, Record[0]);
1823       break;
1824     }
1825 
1826     if (NumRecords >= TypeList.size())
1827       return error("Invalid TYPE table");
1828     if (TypeList[NumRecords])
1829       return error(
1830           "Invalid TYPE table: Only named structs can be forward referenced");
1831     assert(ResultTy && "Didn't read a type?");
1832     TypeList[NumRecords++] = ResultTy;
1833   }
1834 }
1835 
1836 std::error_code BitcodeReader::parseOperandBundleTags() {
1837   if (Stream.EnterSubBlock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID))
1838     return error("Invalid record");
1839 
1840   if (!BundleTags.empty())
1841     return error("Invalid multiple blocks");
1842 
1843   SmallVector<uint64_t, 64> Record;
1844 
1845   while (1) {
1846     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
1847 
1848     switch (Entry.Kind) {
1849     case BitstreamEntry::SubBlock: // Handled for us already.
1850     case BitstreamEntry::Error:
1851       return error("Malformed block");
1852     case BitstreamEntry::EndBlock:
1853       return std::error_code();
1854     case BitstreamEntry::Record:
1855       // The interesting case.
1856       break;
1857     }
1858 
1859     // Tags are implicitly mapped to integers by their order.
1860 
1861     if (Stream.readRecord(Entry.ID, Record) != bitc::OPERAND_BUNDLE_TAG)
1862       return error("Invalid record");
1863 
1864     // OPERAND_BUNDLE_TAG: [strchr x N]
1865     BundleTags.emplace_back();
1866     if (convertToString(Record, 0, BundleTags.back()))
1867       return error("Invalid record");
1868     Record.clear();
1869   }
1870 }
1871 
1872 /// Associate a value with its name from the given index in the provided record.
1873 ErrorOr<Value *> BitcodeReader::recordValue(SmallVectorImpl<uint64_t> &Record,
1874                                             unsigned NameIndex, Triple &TT) {
1875   SmallString<128> ValueName;
1876   if (convertToString(Record, NameIndex, ValueName))
1877     return error("Invalid record");
1878   unsigned ValueID = Record[0];
1879   if (ValueID >= ValueList.size() || !ValueList[ValueID])
1880     return error("Invalid record");
1881   Value *V = ValueList[ValueID];
1882 
1883   StringRef NameStr(ValueName.data(), ValueName.size());
1884   if (NameStr.find_first_of(0) != StringRef::npos)
1885     return error("Invalid value name");
1886   V->setName(NameStr);
1887   auto *GO = dyn_cast<GlobalObject>(V);
1888   if (GO) {
1889     if (GO->getComdat() == reinterpret_cast<Comdat *>(1)) {
1890       if (TT.isOSBinFormatMachO())
1891         GO->setComdat(nullptr);
1892       else
1893         GO->setComdat(TheModule->getOrInsertComdat(V->getName()));
1894     }
1895   }
1896   return V;
1897 }
1898 
1899 /// Helper to note and return the current location, and jump to the given
1900 /// offset.
1901 static uint64_t jumpToValueSymbolTable(uint64_t Offset,
1902                                        BitstreamCursor &Stream) {
1903   // Save the current parsing location so we can jump back at the end
1904   // of the VST read.
1905   uint64_t CurrentBit = Stream.GetCurrentBitNo();
1906   Stream.JumpToBit(Offset * 32);
1907 #ifndef NDEBUG
1908   // Do some checking if we are in debug mode.
1909   BitstreamEntry Entry = Stream.advance();
1910   assert(Entry.Kind == BitstreamEntry::SubBlock);
1911   assert(Entry.ID == bitc::VALUE_SYMTAB_BLOCK_ID);
1912 #else
1913   // In NDEBUG mode ignore the output so we don't get an unused variable
1914   // warning.
1915   Stream.advance();
1916 #endif
1917   return CurrentBit;
1918 }
1919 
1920 /// Parse the value symbol table at either the current parsing location or
1921 /// at the given bit offset if provided.
1922 std::error_code BitcodeReader::parseValueSymbolTable(uint64_t Offset) {
1923   uint64_t CurrentBit;
1924   // Pass in the Offset to distinguish between calling for the module-level
1925   // VST (where we want to jump to the VST offset) and the function-level
1926   // VST (where we don't).
1927   if (Offset > 0)
1928     CurrentBit = jumpToValueSymbolTable(Offset, Stream);
1929 
1930   // Compute the delta between the bitcode indices in the VST (the word offset
1931   // to the word-aligned ENTER_SUBBLOCK for the function block, and that
1932   // expected by the lazy reader. The reader's EnterSubBlock expects to have
1933   // already read the ENTER_SUBBLOCK code (size getAbbrevIDWidth) and BlockID
1934   // (size BlockIDWidth). Note that we access the stream's AbbrevID width here
1935   // just before entering the VST subblock because: 1) the EnterSubBlock
1936   // changes the AbbrevID width; 2) the VST block is nested within the same
1937   // outer MODULE_BLOCK as the FUNCTION_BLOCKs and therefore have the same
1938   // AbbrevID width before calling EnterSubBlock; and 3) when we want to
1939   // jump to the FUNCTION_BLOCK using this offset later, we don't want
1940   // to rely on the stream's AbbrevID width being that of the MODULE_BLOCK.
1941   unsigned FuncBitcodeOffsetDelta =
1942       Stream.getAbbrevIDWidth() + bitc::BlockIDWidth;
1943 
1944   if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
1945     return error("Invalid record");
1946 
1947   SmallVector<uint64_t, 64> Record;
1948 
1949   Triple TT(TheModule->getTargetTriple());
1950 
1951   // Read all the records for this value table.
1952   SmallString<128> ValueName;
1953   while (1) {
1954     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
1955 
1956     switch (Entry.Kind) {
1957     case BitstreamEntry::SubBlock: // Handled for us already.
1958     case BitstreamEntry::Error:
1959       return error("Malformed block");
1960     case BitstreamEntry::EndBlock:
1961       if (Offset > 0)
1962         Stream.JumpToBit(CurrentBit);
1963       return std::error_code();
1964     case BitstreamEntry::Record:
1965       // The interesting case.
1966       break;
1967     }
1968 
1969     // Read a record.
1970     Record.clear();
1971     switch (Stream.readRecord(Entry.ID, Record)) {
1972     default:  // Default behavior: unknown type.
1973       break;
1974     case bitc::VST_CODE_ENTRY: {  // VST_CODE_ENTRY: [valueid, namechar x N]
1975       ErrorOr<Value *> ValOrErr = recordValue(Record, 1, TT);
1976       if (std::error_code EC = ValOrErr.getError())
1977         return EC;
1978       ValOrErr.get();
1979       break;
1980     }
1981     case bitc::VST_CODE_FNENTRY: {
1982       // VST_CODE_FNENTRY: [valueid, offset, namechar x N]
1983       ErrorOr<Value *> ValOrErr = recordValue(Record, 2, TT);
1984       if (std::error_code EC = ValOrErr.getError())
1985         return EC;
1986       Value *V = ValOrErr.get();
1987 
1988       auto *GO = dyn_cast<GlobalObject>(V);
1989       if (!GO) {
1990         // If this is an alias, need to get the actual Function object
1991         // it aliases, in order to set up the DeferredFunctionInfo entry below.
1992         auto *GA = dyn_cast<GlobalAlias>(V);
1993         if (GA)
1994           GO = GA->getBaseObject();
1995         assert(GO);
1996       }
1997 
1998       uint64_t FuncWordOffset = Record[1];
1999       Function *F = dyn_cast<Function>(GO);
2000       assert(F);
2001       uint64_t FuncBitOffset = FuncWordOffset * 32;
2002       DeferredFunctionInfo[F] = FuncBitOffset + FuncBitcodeOffsetDelta;
2003       // Set the LastFunctionBlockBit to point to the last function block.
2004       // Later when parsing is resumed after function materialization,
2005       // we can simply skip that last function block.
2006       if (FuncBitOffset > LastFunctionBlockBit)
2007         LastFunctionBlockBit = FuncBitOffset;
2008       break;
2009     }
2010     case bitc::VST_CODE_BBENTRY: {
2011       if (convertToString(Record, 1, ValueName))
2012         return error("Invalid record");
2013       BasicBlock *BB = getBasicBlock(Record[0]);
2014       if (!BB)
2015         return error("Invalid record");
2016 
2017       BB->setName(StringRef(ValueName.data(), ValueName.size()));
2018       ValueName.clear();
2019       break;
2020     }
2021     }
2022   }
2023 }
2024 
2025 /// Parse a single METADATA_KIND record, inserting result in MDKindMap.
2026 std::error_code
2027 BitcodeReader::parseMetadataKindRecord(SmallVectorImpl<uint64_t> &Record) {
2028   if (Record.size() < 2)
2029     return error("Invalid record");
2030 
2031   unsigned Kind = Record[0];
2032   SmallString<8> Name(Record.begin() + 1, Record.end());
2033 
2034   unsigned NewKind = TheModule->getMDKindID(Name.str());
2035   if (!MDKindMap.insert(std::make_pair(Kind, NewKind)).second)
2036     return error("Conflicting METADATA_KIND records");
2037   return std::error_code();
2038 }
2039 
2040 static int64_t unrotateSign(uint64_t U) { return U & 1 ? ~(U >> 1) : U >> 1; }
2041 
2042 std::error_code BitcodeReader::parseMetadataStrings(ArrayRef<uint64_t> Record,
2043                                                     StringRef Blob,
2044                                                     unsigned &NextMetadataNo) {
2045   // All the MDStrings in the block are emitted together in a single
2046   // record.  The strings are concatenated and stored in a blob along with
2047   // their sizes.
2048   if (Record.size() != 2)
2049     return error("Invalid record: metadata strings layout");
2050 
2051   unsigned NumStrings = Record[0];
2052   unsigned StringsOffset = Record[1];
2053   if (!NumStrings)
2054     return error("Invalid record: metadata strings with no strings");
2055   if (StringsOffset > Blob.size())
2056     return error("Invalid record: metadata strings corrupt offset");
2057 
2058   StringRef Lengths = Blob.slice(0, StringsOffset);
2059   SimpleBitstreamCursor R(*StreamFile);
2060   R.jumpToPointer(Lengths.begin());
2061 
2062   // Ensure that Blob doesn't get invalidated, even if this is reading from
2063   // a StreamingMemoryObject with corrupt data.
2064   R.setArtificialByteLimit(R.getCurrentByteNo() + StringsOffset);
2065 
2066   StringRef Strings = Blob.drop_front(StringsOffset);
2067   do {
2068     if (R.AtEndOfStream())
2069       return error("Invalid record: metadata strings bad length");
2070 
2071     unsigned Size = R.ReadVBR(6);
2072     if (Strings.size() < Size)
2073       return error("Invalid record: metadata strings truncated chars");
2074 
2075     MetadataList.assignValue(MDString::get(Context, Strings.slice(0, Size)),
2076                              NextMetadataNo++);
2077     Strings = Strings.drop_front(Size);
2078   } while (--NumStrings);
2079 
2080   return std::error_code();
2081 }
2082 
2083 namespace {
2084 class PlaceholderQueue {
2085   // Placeholders would thrash around when moved, so store in a std::deque
2086   // instead of some sort of vector.
2087   std::deque<DistinctMDOperandPlaceholder> PHs;
2088 
2089 public:
2090   DistinctMDOperandPlaceholder &getPlaceholderOp(unsigned ID);
2091   void flush(BitcodeReaderMetadataList &MetadataList);
2092 };
2093 } // end namespace
2094 
2095 DistinctMDOperandPlaceholder &PlaceholderQueue::getPlaceholderOp(unsigned ID) {
2096   PHs.emplace_back(ID);
2097   return PHs.back();
2098 }
2099 
2100 void PlaceholderQueue::flush(BitcodeReaderMetadataList &MetadataList) {
2101   while (!PHs.empty()) {
2102     PHs.front().replaceUseWith(
2103         MetadataList.getMetadataFwdRef(PHs.front().getID()));
2104     PHs.pop_front();
2105   }
2106 }
2107 
2108 /// Parse a METADATA_BLOCK. If ModuleLevel is true then we are parsing
2109 /// module level metadata.
2110 std::error_code BitcodeReader::parseMetadata(bool ModuleLevel) {
2111   assert((ModuleLevel || DeferredMetadataInfo.empty()) &&
2112          "Must read all module-level metadata before function-level");
2113 
2114   IsMetadataMaterialized = true;
2115   unsigned NextMetadataNo = MetadataList.size();
2116 
2117   if (!ModuleLevel && MetadataList.hasFwdRefs())
2118     return error("Invalid metadata: fwd refs into function blocks");
2119 
2120   if (Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID))
2121     return error("Invalid record");
2122 
2123   std::vector<std::pair<DICompileUnit *, Metadata *>> CUSubprograms;
2124   SmallVector<uint64_t, 64> Record;
2125 
2126   PlaceholderQueue Placeholders;
2127   bool IsDistinct;
2128   auto getMD = [&](unsigned ID) -> Metadata * {
2129     if (!IsDistinct)
2130       return MetadataList.getMetadataFwdRef(ID);
2131     if (auto *MD = MetadataList.getMetadataIfResolved(ID))
2132       return MD;
2133     return &Placeholders.getPlaceholderOp(ID);
2134   };
2135   auto getMDOrNull = [&](unsigned ID) -> Metadata * {
2136     if (ID)
2137       return getMD(ID - 1);
2138     return nullptr;
2139   };
2140   auto getMDOrNullWithoutPlaceholders = [&](unsigned ID) -> Metadata * {
2141     if (ID)
2142       return MetadataList.getMetadataFwdRef(ID - 1);
2143     return nullptr;
2144   };
2145   auto getMDString = [&](unsigned ID) -> MDString *{
2146     // This requires that the ID is not really a forward reference.  In
2147     // particular, the MDString must already have been resolved.
2148     return cast_or_null<MDString>(getMDOrNull(ID));
2149   };
2150 
2151   // Support for old type refs.
2152   auto getDITypeRefOrNull = [&](unsigned ID) {
2153     return MetadataList.upgradeTypeRef(getMDOrNull(ID));
2154   };
2155 
2156 #define GET_OR_DISTINCT(CLASS, ARGS)                                           \
2157   (IsDistinct ? CLASS::getDistinct ARGS : CLASS::get ARGS)
2158 
2159   // Read all the records.
2160   while (1) {
2161     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
2162 
2163     switch (Entry.Kind) {
2164     case BitstreamEntry::SubBlock: // Handled for us already.
2165     case BitstreamEntry::Error:
2166       return error("Malformed block");
2167     case BitstreamEntry::EndBlock:
2168       // Upgrade old-style CU <-> SP pointers to point from SP to CU.
2169       for (auto CU_SP : CUSubprograms)
2170         if (auto *SPs = dyn_cast_or_null<MDTuple>(CU_SP.second))
2171           for (auto &Op : SPs->operands())
2172             if (auto *SP = dyn_cast_or_null<MDNode>(Op))
2173               SP->replaceOperandWith(7, CU_SP.first);
2174 
2175       MetadataList.tryToResolveCycles();
2176       Placeholders.flush(MetadataList);
2177       return std::error_code();
2178     case BitstreamEntry::Record:
2179       // The interesting case.
2180       break;
2181     }
2182 
2183     // Read a record.
2184     Record.clear();
2185     StringRef Blob;
2186     unsigned Code = Stream.readRecord(Entry.ID, Record, &Blob);
2187     IsDistinct = false;
2188     switch (Code) {
2189     default:  // Default behavior: ignore.
2190       break;
2191     case bitc::METADATA_NAME: {
2192       // Read name of the named metadata.
2193       SmallString<8> Name(Record.begin(), Record.end());
2194       Record.clear();
2195       Code = Stream.ReadCode();
2196 
2197       unsigned NextBitCode = Stream.readRecord(Code, Record);
2198       if (NextBitCode != bitc::METADATA_NAMED_NODE)
2199         return error("METADATA_NAME not followed by METADATA_NAMED_NODE");
2200 
2201       // Read named metadata elements.
2202       unsigned Size = Record.size();
2203       NamedMDNode *NMD = TheModule->getOrInsertNamedMetadata(Name);
2204       for (unsigned i = 0; i != Size; ++i) {
2205         MDNode *MD = MetadataList.getMDNodeFwdRefOrNull(Record[i]);
2206         if (!MD)
2207           return error("Invalid record");
2208         NMD->addOperand(MD);
2209       }
2210       break;
2211     }
2212     case bitc::METADATA_OLD_FN_NODE: {
2213       // FIXME: Remove in 4.0.
2214       // This is a LocalAsMetadata record, the only type of function-local
2215       // metadata.
2216       if (Record.size() % 2 == 1)
2217         return error("Invalid record");
2218 
2219       // If this isn't a LocalAsMetadata record, we're dropping it.  This used
2220       // to be legal, but there's no upgrade path.
2221       auto dropRecord = [&] {
2222         MetadataList.assignValue(MDNode::get(Context, None), NextMetadataNo++);
2223       };
2224       if (Record.size() != 2) {
2225         dropRecord();
2226         break;
2227       }
2228 
2229       Type *Ty = getTypeByID(Record[0]);
2230       if (Ty->isMetadataTy() || Ty->isVoidTy()) {
2231         dropRecord();
2232         break;
2233       }
2234 
2235       MetadataList.assignValue(
2236           LocalAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)),
2237           NextMetadataNo++);
2238       break;
2239     }
2240     case bitc::METADATA_OLD_NODE: {
2241       // FIXME: Remove in 4.0.
2242       if (Record.size() % 2 == 1)
2243         return error("Invalid record");
2244 
2245       unsigned Size = Record.size();
2246       SmallVector<Metadata *, 8> Elts;
2247       for (unsigned i = 0; i != Size; i += 2) {
2248         Type *Ty = getTypeByID(Record[i]);
2249         if (!Ty)
2250           return error("Invalid record");
2251         if (Ty->isMetadataTy())
2252           Elts.push_back(getMD(Record[i + 1]));
2253         else if (!Ty->isVoidTy()) {
2254           auto *MD =
2255               ValueAsMetadata::get(ValueList.getValueFwdRef(Record[i + 1], Ty));
2256           assert(isa<ConstantAsMetadata>(MD) &&
2257                  "Expected non-function-local metadata");
2258           Elts.push_back(MD);
2259         } else
2260           Elts.push_back(nullptr);
2261       }
2262       MetadataList.assignValue(MDNode::get(Context, Elts), NextMetadataNo++);
2263       break;
2264     }
2265     case bitc::METADATA_VALUE: {
2266       if (Record.size() != 2)
2267         return error("Invalid record");
2268 
2269       Type *Ty = getTypeByID(Record[0]);
2270       if (Ty->isMetadataTy() || Ty->isVoidTy())
2271         return error("Invalid record");
2272 
2273       MetadataList.assignValue(
2274           ValueAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)),
2275           NextMetadataNo++);
2276       break;
2277     }
2278     case bitc::METADATA_DISTINCT_NODE:
2279       IsDistinct = true;
2280       // fallthrough...
2281     case bitc::METADATA_NODE: {
2282       SmallVector<Metadata *, 8> Elts;
2283       Elts.reserve(Record.size());
2284       for (unsigned ID : Record)
2285         Elts.push_back(getMDOrNull(ID));
2286       MetadataList.assignValue(IsDistinct ? MDNode::getDistinct(Context, Elts)
2287                                           : MDNode::get(Context, Elts),
2288                                NextMetadataNo++);
2289       break;
2290     }
2291     case bitc::METADATA_LOCATION: {
2292       if (Record.size() != 5)
2293         return error("Invalid record");
2294 
2295       IsDistinct = Record[0];
2296       unsigned Line = Record[1];
2297       unsigned Column = Record[2];
2298       Metadata *Scope = getMD(Record[3]);
2299       Metadata *InlinedAt = getMDOrNull(Record[4]);
2300       MetadataList.assignValue(
2301           GET_OR_DISTINCT(DILocation,
2302                           (Context, Line, Column, Scope, InlinedAt)),
2303           NextMetadataNo++);
2304       break;
2305     }
2306     case bitc::METADATA_GENERIC_DEBUG: {
2307       if (Record.size() < 4)
2308         return error("Invalid record");
2309 
2310       IsDistinct = Record[0];
2311       unsigned Tag = Record[1];
2312       unsigned Version = Record[2];
2313 
2314       if (Tag >= 1u << 16 || Version != 0)
2315         return error("Invalid record");
2316 
2317       auto *Header = getMDString(Record[3]);
2318       SmallVector<Metadata *, 8> DwarfOps;
2319       for (unsigned I = 4, E = Record.size(); I != E; ++I)
2320         DwarfOps.push_back(getMDOrNull(Record[I]));
2321       MetadataList.assignValue(
2322           GET_OR_DISTINCT(GenericDINode, (Context, Tag, Header, DwarfOps)),
2323           NextMetadataNo++);
2324       break;
2325     }
2326     case bitc::METADATA_SUBRANGE: {
2327       if (Record.size() != 3)
2328         return error("Invalid record");
2329 
2330       IsDistinct = Record[0];
2331       MetadataList.assignValue(
2332           GET_OR_DISTINCT(DISubrange,
2333                           (Context, Record[1], unrotateSign(Record[2]))),
2334           NextMetadataNo++);
2335       break;
2336     }
2337     case bitc::METADATA_ENUMERATOR: {
2338       if (Record.size() != 3)
2339         return error("Invalid record");
2340 
2341       IsDistinct = Record[0];
2342       MetadataList.assignValue(
2343           GET_OR_DISTINCT(DIEnumerator, (Context, unrotateSign(Record[1]),
2344                                          getMDString(Record[2]))),
2345           NextMetadataNo++);
2346       break;
2347     }
2348     case bitc::METADATA_BASIC_TYPE: {
2349       if (Record.size() != 6)
2350         return error("Invalid record");
2351 
2352       IsDistinct = Record[0];
2353       MetadataList.assignValue(
2354           GET_OR_DISTINCT(DIBasicType,
2355                           (Context, Record[1], getMDString(Record[2]),
2356                            Record[3], Record[4], Record[5])),
2357           NextMetadataNo++);
2358       break;
2359     }
2360     case bitc::METADATA_DERIVED_TYPE: {
2361       if (Record.size() != 12)
2362         return error("Invalid record");
2363 
2364       IsDistinct = Record[0];
2365       MetadataList.assignValue(
2366           GET_OR_DISTINCT(
2367               DIDerivedType,
2368               (Context, Record[1], getMDString(Record[2]),
2369                getMDOrNull(Record[3]), Record[4], getDITypeRefOrNull(Record[5]),
2370                getDITypeRefOrNull(Record[6]), Record[7], Record[8], Record[9],
2371                Record[10], getDITypeRefOrNull(Record[11]))),
2372           NextMetadataNo++);
2373       break;
2374     }
2375     case bitc::METADATA_COMPOSITE_TYPE: {
2376       if (Record.size() != 16)
2377         return error("Invalid record");
2378 
2379       // If we have a UUID and this is not a forward declaration, lookup the
2380       // mapping.
2381       IsDistinct = Record[0] & 0x1;
2382       bool IsNotUsedInTypeRef = Record[0] >= 2;
2383       unsigned Tag = Record[1];
2384       MDString *Name = getMDString(Record[2]);
2385       Metadata *File = getMDOrNull(Record[3]);
2386       unsigned Line = Record[4];
2387       Metadata *Scope = getDITypeRefOrNull(Record[5]);
2388       Metadata *BaseType = getDITypeRefOrNull(Record[6]);
2389       uint64_t SizeInBits = Record[7];
2390       uint64_t AlignInBits = Record[8];
2391       uint64_t OffsetInBits = Record[9];
2392       unsigned Flags = Record[10];
2393       Metadata *Elements = getMDOrNull(Record[11]);
2394       unsigned RuntimeLang = Record[12];
2395       Metadata *VTableHolder = getDITypeRefOrNull(Record[13]);
2396       Metadata *TemplateParams = getMDOrNull(Record[14]);
2397       auto *Identifier = getMDString(Record[15]);
2398       DICompositeType *CT = nullptr;
2399       if (Identifier)
2400         CT = DICompositeType::buildODRType(
2401             Context, *Identifier, Tag, Name, File, Line, Scope, BaseType,
2402             SizeInBits, AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
2403             VTableHolder, TemplateParams);
2404 
2405       // Create a node if we didn't get a lazy ODR type.
2406       if (!CT)
2407         CT = GET_OR_DISTINCT(DICompositeType,
2408                              (Context, Tag, Name, File, Line, Scope, BaseType,
2409                               SizeInBits, AlignInBits, OffsetInBits, Flags,
2410                               Elements, RuntimeLang, VTableHolder,
2411                               TemplateParams, Identifier));
2412       if (!IsNotUsedInTypeRef && Identifier)
2413         MetadataList.addTypeRef(*Identifier, *cast<DICompositeType>(CT));
2414 
2415       MetadataList.assignValue(CT, NextMetadataNo++);
2416       break;
2417     }
2418     case bitc::METADATA_SUBROUTINE_TYPE: {
2419       if (Record.size() != 3)
2420         return error("Invalid record");
2421 
2422       IsDistinct = Record[0] & 0x1;
2423       bool IsOldTypeRefArray = Record[0] < 2;
2424       Metadata *Types = getMDOrNull(Record[2]);
2425       if (LLVM_UNLIKELY(IsOldTypeRefArray))
2426         Types = MetadataList.upgradeTypeRefArray(Types);
2427 
2428       MetadataList.assignValue(
2429           GET_OR_DISTINCT(DISubroutineType, (Context, Record[1], Types)),
2430           NextMetadataNo++);
2431       break;
2432     }
2433 
2434     case bitc::METADATA_MODULE: {
2435       if (Record.size() != 6)
2436         return error("Invalid record");
2437 
2438       IsDistinct = Record[0];
2439       MetadataList.assignValue(
2440           GET_OR_DISTINCT(DIModule,
2441                           (Context, getMDOrNull(Record[1]),
2442                            getMDString(Record[2]), getMDString(Record[3]),
2443                            getMDString(Record[4]), getMDString(Record[5]))),
2444           NextMetadataNo++);
2445       break;
2446     }
2447 
2448     case bitc::METADATA_FILE: {
2449       if (Record.size() != 3)
2450         return error("Invalid record");
2451 
2452       IsDistinct = Record[0];
2453       MetadataList.assignValue(
2454           GET_OR_DISTINCT(DIFile, (Context, getMDString(Record[1]),
2455                                    getMDString(Record[2]))),
2456           NextMetadataNo++);
2457       break;
2458     }
2459     case bitc::METADATA_COMPILE_UNIT: {
2460       if (Record.size() < 14 || Record.size() > 16)
2461         return error("Invalid record");
2462 
2463       // Ignore Record[0], which indicates whether this compile unit is
2464       // distinct.  It's always distinct.
2465       IsDistinct = true;
2466       auto *CU = DICompileUnit::getDistinct(
2467           Context, Record[1], getMDOrNull(Record[2]), getMDString(Record[3]),
2468           Record[4], getMDString(Record[5]), Record[6], getMDString(Record[7]),
2469           Record[8], getMDOrNull(Record[9]), getMDOrNull(Record[10]),
2470           getMDOrNull(Record[12]), getMDOrNull(Record[13]),
2471           Record.size() <= 15 ? nullptr : getMDOrNull(Record[15]),
2472           Record.size() <= 14 ? 0 : Record[14]);
2473 
2474       MetadataList.assignValue(CU, NextMetadataNo++);
2475 
2476       // Move the Upgrade the list of subprograms.
2477       if (Metadata *SPs = getMDOrNullWithoutPlaceholders(Record[11]))
2478         CUSubprograms.push_back({CU, SPs});
2479       break;
2480     }
2481     case bitc::METADATA_SUBPROGRAM: {
2482       if (Record.size() != 18 && Record.size() != 19)
2483         return error("Invalid record");
2484 
2485       IsDistinct =
2486           Record[0] || Record[8]; // All definitions should be distinct.
2487       // Version 1 has a Function as Record[15].
2488       // Version 2 has removed Record[15].
2489       // Version 3 has the Unit as Record[15].
2490       Metadata *CUorFn = getMDOrNull(Record[15]);
2491       unsigned Offset = Record.size() == 19 ? 1 : 0;
2492       bool HasFn = Offset && dyn_cast_or_null<ConstantAsMetadata>(CUorFn);
2493       bool HasCU = Offset && !HasFn;
2494       DISubprogram *SP = GET_OR_DISTINCT(
2495           DISubprogram,
2496           (Context, getDITypeRefOrNull(Record[1]), getMDString(Record[2]),
2497            getMDString(Record[3]), getMDOrNull(Record[4]), Record[5],
2498            getMDOrNull(Record[6]), Record[7], Record[8], Record[9],
2499            getDITypeRefOrNull(Record[10]), Record[11], Record[12], Record[13],
2500            Record[14], HasCU ? CUorFn : nullptr,
2501            getMDOrNull(Record[15 + Offset]), getMDOrNull(Record[16 + Offset]),
2502            getMDOrNull(Record[17 + Offset])));
2503       MetadataList.assignValue(SP, NextMetadataNo++);
2504 
2505       // Upgrade sp->function mapping to function->sp mapping.
2506       if (HasFn) {
2507         if (auto *CMD = dyn_cast<ConstantAsMetadata>(CUorFn))
2508           if (auto *F = dyn_cast<Function>(CMD->getValue())) {
2509             if (F->isMaterializable())
2510               // Defer until materialized; unmaterialized functions may not have
2511               // metadata.
2512               FunctionsWithSPs[F] = SP;
2513             else if (!F->empty())
2514               F->setSubprogram(SP);
2515           }
2516       }
2517       break;
2518     }
2519     case bitc::METADATA_LEXICAL_BLOCK: {
2520       if (Record.size() != 5)
2521         return error("Invalid record");
2522 
2523       IsDistinct = Record[0];
2524       MetadataList.assignValue(
2525           GET_OR_DISTINCT(DILexicalBlock,
2526                           (Context, getMDOrNull(Record[1]),
2527                            getMDOrNull(Record[2]), Record[3], Record[4])),
2528           NextMetadataNo++);
2529       break;
2530     }
2531     case bitc::METADATA_LEXICAL_BLOCK_FILE: {
2532       if (Record.size() != 4)
2533         return error("Invalid record");
2534 
2535       IsDistinct = Record[0];
2536       MetadataList.assignValue(
2537           GET_OR_DISTINCT(DILexicalBlockFile,
2538                           (Context, getMDOrNull(Record[1]),
2539                            getMDOrNull(Record[2]), Record[3])),
2540           NextMetadataNo++);
2541       break;
2542     }
2543     case bitc::METADATA_NAMESPACE: {
2544       if (Record.size() != 5)
2545         return error("Invalid record");
2546 
2547       IsDistinct = Record[0];
2548       MetadataList.assignValue(
2549           GET_OR_DISTINCT(DINamespace, (Context, getMDOrNull(Record[1]),
2550                                         getMDOrNull(Record[2]),
2551                                         getMDString(Record[3]), Record[4])),
2552           NextMetadataNo++);
2553       break;
2554     }
2555     case bitc::METADATA_MACRO: {
2556       if (Record.size() != 5)
2557         return error("Invalid record");
2558 
2559       IsDistinct = Record[0];
2560       MetadataList.assignValue(
2561           GET_OR_DISTINCT(DIMacro,
2562                           (Context, Record[1], Record[2],
2563                            getMDString(Record[3]), getMDString(Record[4]))),
2564           NextMetadataNo++);
2565       break;
2566     }
2567     case bitc::METADATA_MACRO_FILE: {
2568       if (Record.size() != 5)
2569         return error("Invalid record");
2570 
2571       IsDistinct = Record[0];
2572       MetadataList.assignValue(
2573           GET_OR_DISTINCT(DIMacroFile,
2574                           (Context, Record[1], Record[2],
2575                            getMDOrNull(Record[3]), getMDOrNull(Record[4]))),
2576           NextMetadataNo++);
2577       break;
2578     }
2579     case bitc::METADATA_TEMPLATE_TYPE: {
2580       if (Record.size() != 3)
2581         return error("Invalid record");
2582 
2583       IsDistinct = Record[0];
2584       MetadataList.assignValue(GET_OR_DISTINCT(DITemplateTypeParameter,
2585                                                (Context, getMDString(Record[1]),
2586                                                 getDITypeRefOrNull(Record[2]))),
2587                                NextMetadataNo++);
2588       break;
2589     }
2590     case bitc::METADATA_TEMPLATE_VALUE: {
2591       if (Record.size() != 5)
2592         return error("Invalid record");
2593 
2594       IsDistinct = Record[0];
2595       MetadataList.assignValue(
2596           GET_OR_DISTINCT(DITemplateValueParameter,
2597                           (Context, Record[1], getMDString(Record[2]),
2598                            getDITypeRefOrNull(Record[3]),
2599                            getMDOrNull(Record[4]))),
2600           NextMetadataNo++);
2601       break;
2602     }
2603     case bitc::METADATA_GLOBAL_VAR: {
2604       if (Record.size() != 11)
2605         return error("Invalid record");
2606 
2607       IsDistinct = Record[0];
2608       MetadataList.assignValue(
2609           GET_OR_DISTINCT(DIGlobalVariable,
2610                           (Context, getMDOrNull(Record[1]),
2611                            getMDString(Record[2]), getMDString(Record[3]),
2612                            getMDOrNull(Record[4]), Record[5],
2613                            getDITypeRefOrNull(Record[6]), Record[7], Record[8],
2614                            getMDOrNull(Record[9]), getMDOrNull(Record[10]))),
2615           NextMetadataNo++);
2616       break;
2617     }
2618     case bitc::METADATA_LOCAL_VAR: {
2619       // 10th field is for the obseleted 'inlinedAt:' field.
2620       if (Record.size() < 8 || Record.size() > 10)
2621         return error("Invalid record");
2622 
2623       // 2nd field used to be an artificial tag, either DW_TAG_auto_variable or
2624       // DW_TAG_arg_variable.
2625       IsDistinct = Record[0];
2626       bool HasTag = Record.size() > 8;
2627       MetadataList.assignValue(
2628           GET_OR_DISTINCT(DILocalVariable,
2629                           (Context, getMDOrNull(Record[1 + HasTag]),
2630                            getMDString(Record[2 + HasTag]),
2631                            getMDOrNull(Record[3 + HasTag]), Record[4 + HasTag],
2632                            getDITypeRefOrNull(Record[5 + HasTag]),
2633                            Record[6 + HasTag], Record[7 + HasTag])),
2634           NextMetadataNo++);
2635       break;
2636     }
2637     case bitc::METADATA_EXPRESSION: {
2638       if (Record.size() < 1)
2639         return error("Invalid record");
2640 
2641       IsDistinct = Record[0];
2642       MetadataList.assignValue(
2643           GET_OR_DISTINCT(DIExpression,
2644                           (Context, makeArrayRef(Record).slice(1))),
2645           NextMetadataNo++);
2646       break;
2647     }
2648     case bitc::METADATA_OBJC_PROPERTY: {
2649       if (Record.size() != 8)
2650         return error("Invalid record");
2651 
2652       IsDistinct = Record[0];
2653       MetadataList.assignValue(
2654           GET_OR_DISTINCT(DIObjCProperty,
2655                           (Context, getMDString(Record[1]),
2656                            getMDOrNull(Record[2]), Record[3],
2657                            getMDString(Record[4]), getMDString(Record[5]),
2658                            Record[6], getDITypeRefOrNull(Record[7]))),
2659           NextMetadataNo++);
2660       break;
2661     }
2662     case bitc::METADATA_IMPORTED_ENTITY: {
2663       if (Record.size() != 6)
2664         return error("Invalid record");
2665 
2666       IsDistinct = Record[0];
2667       MetadataList.assignValue(
2668           GET_OR_DISTINCT(DIImportedEntity,
2669                           (Context, Record[1], getMDOrNull(Record[2]),
2670                            getDITypeRefOrNull(Record[3]), Record[4],
2671                            getMDString(Record[5]))),
2672           NextMetadataNo++);
2673       break;
2674     }
2675     case bitc::METADATA_STRING_OLD: {
2676       std::string String(Record.begin(), Record.end());
2677 
2678       // Test for upgrading !llvm.loop.
2679       HasSeenOldLoopTags |= mayBeOldLoopAttachmentTag(String);
2680 
2681       Metadata *MD = MDString::get(Context, String);
2682       MetadataList.assignValue(MD, NextMetadataNo++);
2683       break;
2684     }
2685     case bitc::METADATA_STRINGS:
2686       if (std::error_code EC =
2687               parseMetadataStrings(Record, Blob, NextMetadataNo))
2688         return EC;
2689       break;
2690     case bitc::METADATA_KIND: {
2691       // Support older bitcode files that had METADATA_KIND records in a
2692       // block with METADATA_BLOCK_ID.
2693       if (std::error_code EC = parseMetadataKindRecord(Record))
2694         return EC;
2695       break;
2696     }
2697     }
2698   }
2699 #undef GET_OR_DISTINCT
2700 }
2701 
2702 /// Parse the metadata kinds out of the METADATA_KIND_BLOCK.
2703 std::error_code BitcodeReader::parseMetadataKinds() {
2704   if (Stream.EnterSubBlock(bitc::METADATA_KIND_BLOCK_ID))
2705     return error("Invalid record");
2706 
2707   SmallVector<uint64_t, 64> Record;
2708 
2709   // Read all the records.
2710   while (1) {
2711     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
2712 
2713     switch (Entry.Kind) {
2714     case BitstreamEntry::SubBlock: // Handled for us already.
2715     case BitstreamEntry::Error:
2716       return error("Malformed block");
2717     case BitstreamEntry::EndBlock:
2718       return std::error_code();
2719     case BitstreamEntry::Record:
2720       // The interesting case.
2721       break;
2722     }
2723 
2724     // Read a record.
2725     Record.clear();
2726     unsigned Code = Stream.readRecord(Entry.ID, Record);
2727     switch (Code) {
2728     default: // Default behavior: ignore.
2729       break;
2730     case bitc::METADATA_KIND: {
2731       if (std::error_code EC = parseMetadataKindRecord(Record))
2732         return EC;
2733       break;
2734     }
2735     }
2736   }
2737 }
2738 
2739 /// Decode a signed value stored with the sign bit in the LSB for dense VBR
2740 /// encoding.
2741 uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V) {
2742   if ((V & 1) == 0)
2743     return V >> 1;
2744   if (V != 1)
2745     return -(V >> 1);
2746   // There is no such thing as -0 with integers.  "-0" really means MININT.
2747   return 1ULL << 63;
2748 }
2749 
2750 /// Resolve all of the initializers for global values and aliases that we can.
2751 std::error_code BitcodeReader::resolveGlobalAndIndirectSymbolInits() {
2752   std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInitWorklist;
2753   std::vector<std::pair<GlobalIndirectSymbol*, unsigned> >
2754       IndirectSymbolInitWorklist;
2755   std::vector<std::pair<Function*, unsigned> > FunctionPrefixWorklist;
2756   std::vector<std::pair<Function*, unsigned> > FunctionPrologueWorklist;
2757   std::vector<std::pair<Function*, unsigned> > FunctionPersonalityFnWorklist;
2758 
2759   GlobalInitWorklist.swap(GlobalInits);
2760   IndirectSymbolInitWorklist.swap(IndirectSymbolInits);
2761   FunctionPrefixWorklist.swap(FunctionPrefixes);
2762   FunctionPrologueWorklist.swap(FunctionPrologues);
2763   FunctionPersonalityFnWorklist.swap(FunctionPersonalityFns);
2764 
2765   while (!GlobalInitWorklist.empty()) {
2766     unsigned ValID = GlobalInitWorklist.back().second;
2767     if (ValID >= ValueList.size()) {
2768       // Not ready to resolve this yet, it requires something later in the file.
2769       GlobalInits.push_back(GlobalInitWorklist.back());
2770     } else {
2771       if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
2772         GlobalInitWorklist.back().first->setInitializer(C);
2773       else
2774         return error("Expected a constant");
2775     }
2776     GlobalInitWorklist.pop_back();
2777   }
2778 
2779   while (!IndirectSymbolInitWorklist.empty()) {
2780     unsigned ValID = IndirectSymbolInitWorklist.back().second;
2781     if (ValID >= ValueList.size()) {
2782       IndirectSymbolInits.push_back(IndirectSymbolInitWorklist.back());
2783     } else {
2784       Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]);
2785       if (!C)
2786         return error("Expected a constant");
2787       GlobalIndirectSymbol *GIS = IndirectSymbolInitWorklist.back().first;
2788       if (isa<GlobalAlias>(GIS) && C->getType() != GIS->getType())
2789         return error("Alias and aliasee types don't match");
2790       GIS->setIndirectSymbol(C);
2791     }
2792     IndirectSymbolInitWorklist.pop_back();
2793   }
2794 
2795   while (!FunctionPrefixWorklist.empty()) {
2796     unsigned ValID = FunctionPrefixWorklist.back().second;
2797     if (ValID >= ValueList.size()) {
2798       FunctionPrefixes.push_back(FunctionPrefixWorklist.back());
2799     } else {
2800       if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
2801         FunctionPrefixWorklist.back().first->setPrefixData(C);
2802       else
2803         return error("Expected a constant");
2804     }
2805     FunctionPrefixWorklist.pop_back();
2806   }
2807 
2808   while (!FunctionPrologueWorklist.empty()) {
2809     unsigned ValID = FunctionPrologueWorklist.back().second;
2810     if (ValID >= ValueList.size()) {
2811       FunctionPrologues.push_back(FunctionPrologueWorklist.back());
2812     } else {
2813       if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
2814         FunctionPrologueWorklist.back().first->setPrologueData(C);
2815       else
2816         return error("Expected a constant");
2817     }
2818     FunctionPrologueWorklist.pop_back();
2819   }
2820 
2821   while (!FunctionPersonalityFnWorklist.empty()) {
2822     unsigned ValID = FunctionPersonalityFnWorklist.back().second;
2823     if (ValID >= ValueList.size()) {
2824       FunctionPersonalityFns.push_back(FunctionPersonalityFnWorklist.back());
2825     } else {
2826       if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
2827         FunctionPersonalityFnWorklist.back().first->setPersonalityFn(C);
2828       else
2829         return error("Expected a constant");
2830     }
2831     FunctionPersonalityFnWorklist.pop_back();
2832   }
2833 
2834   return std::error_code();
2835 }
2836 
2837 static APInt readWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) {
2838   SmallVector<uint64_t, 8> Words(Vals.size());
2839   std::transform(Vals.begin(), Vals.end(), Words.begin(),
2840                  BitcodeReader::decodeSignRotatedValue);
2841 
2842   return APInt(TypeBits, Words);
2843 }
2844 
2845 std::error_code BitcodeReader::parseConstants() {
2846   if (Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID))
2847     return error("Invalid record");
2848 
2849   SmallVector<uint64_t, 64> Record;
2850 
2851   // Read all the records for this value table.
2852   Type *CurTy = Type::getInt32Ty(Context);
2853   unsigned NextCstNo = ValueList.size();
2854   while (1) {
2855     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
2856 
2857     switch (Entry.Kind) {
2858     case BitstreamEntry::SubBlock: // Handled for us already.
2859     case BitstreamEntry::Error:
2860       return error("Malformed block");
2861     case BitstreamEntry::EndBlock:
2862       if (NextCstNo != ValueList.size())
2863         return error("Invalid constant reference");
2864 
2865       // Once all the constants have been read, go through and resolve forward
2866       // references.
2867       ValueList.resolveConstantForwardRefs();
2868       return std::error_code();
2869     case BitstreamEntry::Record:
2870       // The interesting case.
2871       break;
2872     }
2873 
2874     // Read a record.
2875     Record.clear();
2876     Value *V = nullptr;
2877     unsigned BitCode = Stream.readRecord(Entry.ID, Record);
2878     switch (BitCode) {
2879     default:  // Default behavior: unknown constant
2880     case bitc::CST_CODE_UNDEF:     // UNDEF
2881       V = UndefValue::get(CurTy);
2882       break;
2883     case bitc::CST_CODE_SETTYPE:   // SETTYPE: [typeid]
2884       if (Record.empty())
2885         return error("Invalid record");
2886       if (Record[0] >= TypeList.size() || !TypeList[Record[0]])
2887         return error("Invalid record");
2888       CurTy = TypeList[Record[0]];
2889       continue;  // Skip the ValueList manipulation.
2890     case bitc::CST_CODE_NULL:      // NULL
2891       V = Constant::getNullValue(CurTy);
2892       break;
2893     case bitc::CST_CODE_INTEGER:   // INTEGER: [intval]
2894       if (!CurTy->isIntegerTy() || Record.empty())
2895         return error("Invalid record");
2896       V = ConstantInt::get(CurTy, decodeSignRotatedValue(Record[0]));
2897       break;
2898     case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval]
2899       if (!CurTy->isIntegerTy() || Record.empty())
2900         return error("Invalid record");
2901 
2902       APInt VInt =
2903           readWideAPInt(Record, cast<IntegerType>(CurTy)->getBitWidth());
2904       V = ConstantInt::get(Context, VInt);
2905 
2906       break;
2907     }
2908     case bitc::CST_CODE_FLOAT: {    // FLOAT: [fpval]
2909       if (Record.empty())
2910         return error("Invalid record");
2911       if (CurTy->isHalfTy())
2912         V = ConstantFP::get(Context, APFloat(APFloat::IEEEhalf,
2913                                              APInt(16, (uint16_t)Record[0])));
2914       else if (CurTy->isFloatTy())
2915         V = ConstantFP::get(Context, APFloat(APFloat::IEEEsingle,
2916                                              APInt(32, (uint32_t)Record[0])));
2917       else if (CurTy->isDoubleTy())
2918         V = ConstantFP::get(Context, APFloat(APFloat::IEEEdouble,
2919                                              APInt(64, Record[0])));
2920       else if (CurTy->isX86_FP80Ty()) {
2921         // Bits are not stored the same way as a normal i80 APInt, compensate.
2922         uint64_t Rearrange[2];
2923         Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16);
2924         Rearrange[1] = Record[0] >> 48;
2925         V = ConstantFP::get(Context, APFloat(APFloat::x87DoubleExtended,
2926                                              APInt(80, Rearrange)));
2927       } else if (CurTy->isFP128Ty())
2928         V = ConstantFP::get(Context, APFloat(APFloat::IEEEquad,
2929                                              APInt(128, Record)));
2930       else if (CurTy->isPPC_FP128Ty())
2931         V = ConstantFP::get(Context, APFloat(APFloat::PPCDoubleDouble,
2932                                              APInt(128, Record)));
2933       else
2934         V = UndefValue::get(CurTy);
2935       break;
2936     }
2937 
2938     case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number]
2939       if (Record.empty())
2940         return error("Invalid record");
2941 
2942       unsigned Size = Record.size();
2943       SmallVector<Constant*, 16> Elts;
2944 
2945       if (StructType *STy = dyn_cast<StructType>(CurTy)) {
2946         for (unsigned i = 0; i != Size; ++i)
2947           Elts.push_back(ValueList.getConstantFwdRef(Record[i],
2948                                                      STy->getElementType(i)));
2949         V = ConstantStruct::get(STy, Elts);
2950       } else if (ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) {
2951         Type *EltTy = ATy->getElementType();
2952         for (unsigned i = 0; i != Size; ++i)
2953           Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
2954         V = ConstantArray::get(ATy, Elts);
2955       } else if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) {
2956         Type *EltTy = VTy->getElementType();
2957         for (unsigned i = 0; i != Size; ++i)
2958           Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
2959         V = ConstantVector::get(Elts);
2960       } else {
2961         V = UndefValue::get(CurTy);
2962       }
2963       break;
2964     }
2965     case bitc::CST_CODE_STRING:    // STRING: [values]
2966     case bitc::CST_CODE_CSTRING: { // CSTRING: [values]
2967       if (Record.empty())
2968         return error("Invalid record");
2969 
2970       SmallString<16> Elts(Record.begin(), Record.end());
2971       V = ConstantDataArray::getString(Context, Elts,
2972                                        BitCode == bitc::CST_CODE_CSTRING);
2973       break;
2974     }
2975     case bitc::CST_CODE_DATA: {// DATA: [n x value]
2976       if (Record.empty())
2977         return error("Invalid record");
2978 
2979       Type *EltTy = cast<SequentialType>(CurTy)->getElementType();
2980       if (EltTy->isIntegerTy(8)) {
2981         SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end());
2982         if (isa<VectorType>(CurTy))
2983           V = ConstantDataVector::get(Context, Elts);
2984         else
2985           V = ConstantDataArray::get(Context, Elts);
2986       } else if (EltTy->isIntegerTy(16)) {
2987         SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end());
2988         if (isa<VectorType>(CurTy))
2989           V = ConstantDataVector::get(Context, Elts);
2990         else
2991           V = ConstantDataArray::get(Context, Elts);
2992       } else if (EltTy->isIntegerTy(32)) {
2993         SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end());
2994         if (isa<VectorType>(CurTy))
2995           V = ConstantDataVector::get(Context, Elts);
2996         else
2997           V = ConstantDataArray::get(Context, Elts);
2998       } else if (EltTy->isIntegerTy(64)) {
2999         SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end());
3000         if (isa<VectorType>(CurTy))
3001           V = ConstantDataVector::get(Context, Elts);
3002         else
3003           V = ConstantDataArray::get(Context, Elts);
3004       } else if (EltTy->isHalfTy()) {
3005         SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end());
3006         if (isa<VectorType>(CurTy))
3007           V = ConstantDataVector::getFP(Context, Elts);
3008         else
3009           V = ConstantDataArray::getFP(Context, Elts);
3010       } else if (EltTy->isFloatTy()) {
3011         SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end());
3012         if (isa<VectorType>(CurTy))
3013           V = ConstantDataVector::getFP(Context, Elts);
3014         else
3015           V = ConstantDataArray::getFP(Context, Elts);
3016       } else if (EltTy->isDoubleTy()) {
3017         SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end());
3018         if (isa<VectorType>(CurTy))
3019           V = ConstantDataVector::getFP(Context, Elts);
3020         else
3021           V = ConstantDataArray::getFP(Context, Elts);
3022       } else {
3023         return error("Invalid type for value");
3024       }
3025       break;
3026     }
3027     case bitc::CST_CODE_CE_BINOP: {  // CE_BINOP: [opcode, opval, opval]
3028       if (Record.size() < 3)
3029         return error("Invalid record");
3030       int Opc = getDecodedBinaryOpcode(Record[0], CurTy);
3031       if (Opc < 0) {
3032         V = UndefValue::get(CurTy);  // Unknown binop.
3033       } else {
3034         Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy);
3035         Constant *RHS = ValueList.getConstantFwdRef(Record[2], CurTy);
3036         unsigned Flags = 0;
3037         if (Record.size() >= 4) {
3038           if (Opc == Instruction::Add ||
3039               Opc == Instruction::Sub ||
3040               Opc == Instruction::Mul ||
3041               Opc == Instruction::Shl) {
3042             if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP))
3043               Flags |= OverflowingBinaryOperator::NoSignedWrap;
3044             if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
3045               Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
3046           } else if (Opc == Instruction::SDiv ||
3047                      Opc == Instruction::UDiv ||
3048                      Opc == Instruction::LShr ||
3049                      Opc == Instruction::AShr) {
3050             if (Record[3] & (1 << bitc::PEO_EXACT))
3051               Flags |= SDivOperator::IsExact;
3052           }
3053         }
3054         V = ConstantExpr::get(Opc, LHS, RHS, Flags);
3055       }
3056       break;
3057     }
3058     case bitc::CST_CODE_CE_CAST: {  // CE_CAST: [opcode, opty, opval]
3059       if (Record.size() < 3)
3060         return error("Invalid record");
3061       int Opc = getDecodedCastOpcode(Record[0]);
3062       if (Opc < 0) {
3063         V = UndefValue::get(CurTy);  // Unknown cast.
3064       } else {
3065         Type *OpTy = getTypeByID(Record[1]);
3066         if (!OpTy)
3067           return error("Invalid record");
3068         Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy);
3069         V = UpgradeBitCastExpr(Opc, Op, CurTy);
3070         if (!V) V = ConstantExpr::getCast(Opc, Op, CurTy);
3071       }
3072       break;
3073     }
3074     case bitc::CST_CODE_CE_INBOUNDS_GEP:
3075     case bitc::CST_CODE_CE_GEP: {  // CE_GEP:        [n x operands]
3076       unsigned OpNum = 0;
3077       Type *PointeeType = nullptr;
3078       if (Record.size() % 2)
3079         PointeeType = getTypeByID(Record[OpNum++]);
3080       SmallVector<Constant*, 16> Elts;
3081       while (OpNum != Record.size()) {
3082         Type *ElTy = getTypeByID(Record[OpNum++]);
3083         if (!ElTy)
3084           return error("Invalid record");
3085         Elts.push_back(ValueList.getConstantFwdRef(Record[OpNum++], ElTy));
3086       }
3087 
3088       if (PointeeType &&
3089           PointeeType !=
3090               cast<SequentialType>(Elts[0]->getType()->getScalarType())
3091                   ->getElementType())
3092         return error("Explicit gep operator type does not match pointee type "
3093                      "of pointer operand");
3094 
3095       ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
3096       V = ConstantExpr::getGetElementPtr(PointeeType, Elts[0], Indices,
3097                                          BitCode ==
3098                                              bitc::CST_CODE_CE_INBOUNDS_GEP);
3099       break;
3100     }
3101     case bitc::CST_CODE_CE_SELECT: {  // CE_SELECT: [opval#, opval#, opval#]
3102       if (Record.size() < 3)
3103         return error("Invalid record");
3104 
3105       Type *SelectorTy = Type::getInt1Ty(Context);
3106 
3107       // The selector might be an i1 or an <n x i1>
3108       // Get the type from the ValueList before getting a forward ref.
3109       if (VectorType *VTy = dyn_cast<VectorType>(CurTy))
3110         if (Value *V = ValueList[Record[0]])
3111           if (SelectorTy != V->getType())
3112             SelectorTy = VectorType::get(SelectorTy, VTy->getNumElements());
3113 
3114       V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0],
3115                                                               SelectorTy),
3116                                   ValueList.getConstantFwdRef(Record[1],CurTy),
3117                                   ValueList.getConstantFwdRef(Record[2],CurTy));
3118       break;
3119     }
3120     case bitc::CST_CODE_CE_EXTRACTELT
3121         : { // CE_EXTRACTELT: [opty, opval, opty, opval]
3122       if (Record.size() < 3)
3123         return error("Invalid record");
3124       VectorType *OpTy =
3125         dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
3126       if (!OpTy)
3127         return error("Invalid record");
3128       Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
3129       Constant *Op1 = nullptr;
3130       if (Record.size() == 4) {
3131         Type *IdxTy = getTypeByID(Record[2]);
3132         if (!IdxTy)
3133           return error("Invalid record");
3134         Op1 = ValueList.getConstantFwdRef(Record[3], IdxTy);
3135       } else // TODO: Remove with llvm 4.0
3136         Op1 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context));
3137       if (!Op1)
3138         return error("Invalid record");
3139       V = ConstantExpr::getExtractElement(Op0, Op1);
3140       break;
3141     }
3142     case bitc::CST_CODE_CE_INSERTELT
3143         : { // CE_INSERTELT: [opval, opval, opty, opval]
3144       VectorType *OpTy = dyn_cast<VectorType>(CurTy);
3145       if (Record.size() < 3 || !OpTy)
3146         return error("Invalid record");
3147       Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy);
3148       Constant *Op1 = ValueList.getConstantFwdRef(Record[1],
3149                                                   OpTy->getElementType());
3150       Constant *Op2 = nullptr;
3151       if (Record.size() == 4) {
3152         Type *IdxTy = getTypeByID(Record[2]);
3153         if (!IdxTy)
3154           return error("Invalid record");
3155         Op2 = ValueList.getConstantFwdRef(Record[3], IdxTy);
3156       } else // TODO: Remove with llvm 4.0
3157         Op2 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context));
3158       if (!Op2)
3159         return error("Invalid record");
3160       V = ConstantExpr::getInsertElement(Op0, Op1, Op2);
3161       break;
3162     }
3163     case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval]
3164       VectorType *OpTy = dyn_cast<VectorType>(CurTy);
3165       if (Record.size() < 3 || !OpTy)
3166         return error("Invalid record");
3167       Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy);
3168       Constant *Op1 = ValueList.getConstantFwdRef(Record[1], OpTy);
3169       Type *ShufTy = VectorType::get(Type::getInt32Ty(Context),
3170                                                  OpTy->getNumElements());
3171       Constant *Op2 = ValueList.getConstantFwdRef(Record[2], ShufTy);
3172       V = ConstantExpr::getShuffleVector(Op0, Op1, Op2);
3173       break;
3174     }
3175     case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval]
3176       VectorType *RTy = dyn_cast<VectorType>(CurTy);
3177       VectorType *OpTy =
3178         dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
3179       if (Record.size() < 4 || !RTy || !OpTy)
3180         return error("Invalid record");
3181       Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
3182       Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
3183       Type *ShufTy = VectorType::get(Type::getInt32Ty(Context),
3184                                                  RTy->getNumElements());
3185       Constant *Op2 = ValueList.getConstantFwdRef(Record[3], ShufTy);
3186       V = ConstantExpr::getShuffleVector(Op0, Op1, Op2);
3187       break;
3188     }
3189     case bitc::CST_CODE_CE_CMP: {     // CE_CMP: [opty, opval, opval, pred]
3190       if (Record.size() < 4)
3191         return error("Invalid record");
3192       Type *OpTy = getTypeByID(Record[0]);
3193       if (!OpTy)
3194         return error("Invalid record");
3195       Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
3196       Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
3197 
3198       if (OpTy->isFPOrFPVectorTy())
3199         V = ConstantExpr::getFCmp(Record[3], Op0, Op1);
3200       else
3201         V = ConstantExpr::getICmp(Record[3], Op0, Op1);
3202       break;
3203     }
3204     // This maintains backward compatibility, pre-asm dialect keywords.
3205     // FIXME: Remove with the 4.0 release.
3206     case bitc::CST_CODE_INLINEASM_OLD: {
3207       if (Record.size() < 2)
3208         return error("Invalid record");
3209       std::string AsmStr, ConstrStr;
3210       bool HasSideEffects = Record[0] & 1;
3211       bool IsAlignStack = Record[0] >> 1;
3212       unsigned AsmStrSize = Record[1];
3213       if (2+AsmStrSize >= Record.size())
3214         return error("Invalid record");
3215       unsigned ConstStrSize = Record[2+AsmStrSize];
3216       if (3+AsmStrSize+ConstStrSize > Record.size())
3217         return error("Invalid record");
3218 
3219       for (unsigned i = 0; i != AsmStrSize; ++i)
3220         AsmStr += (char)Record[2+i];
3221       for (unsigned i = 0; i != ConstStrSize; ++i)
3222         ConstrStr += (char)Record[3+AsmStrSize+i];
3223       PointerType *PTy = cast<PointerType>(CurTy);
3224       V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()),
3225                          AsmStr, ConstrStr, HasSideEffects, IsAlignStack);
3226       break;
3227     }
3228     // This version adds support for the asm dialect keywords (e.g.,
3229     // inteldialect).
3230     case bitc::CST_CODE_INLINEASM: {
3231       if (Record.size() < 2)
3232         return error("Invalid record");
3233       std::string AsmStr, ConstrStr;
3234       bool HasSideEffects = Record[0] & 1;
3235       bool IsAlignStack = (Record[0] >> 1) & 1;
3236       unsigned AsmDialect = Record[0] >> 2;
3237       unsigned AsmStrSize = Record[1];
3238       if (2+AsmStrSize >= Record.size())
3239         return error("Invalid record");
3240       unsigned ConstStrSize = Record[2+AsmStrSize];
3241       if (3+AsmStrSize+ConstStrSize > Record.size())
3242         return error("Invalid record");
3243 
3244       for (unsigned i = 0; i != AsmStrSize; ++i)
3245         AsmStr += (char)Record[2+i];
3246       for (unsigned i = 0; i != ConstStrSize; ++i)
3247         ConstrStr += (char)Record[3+AsmStrSize+i];
3248       PointerType *PTy = cast<PointerType>(CurTy);
3249       V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()),
3250                          AsmStr, ConstrStr, HasSideEffects, IsAlignStack,
3251                          InlineAsm::AsmDialect(AsmDialect));
3252       break;
3253     }
3254     case bitc::CST_CODE_BLOCKADDRESS:{
3255       if (Record.size() < 3)
3256         return error("Invalid record");
3257       Type *FnTy = getTypeByID(Record[0]);
3258       if (!FnTy)
3259         return error("Invalid record");
3260       Function *Fn =
3261         dyn_cast_or_null<Function>(ValueList.getConstantFwdRef(Record[1],FnTy));
3262       if (!Fn)
3263         return error("Invalid record");
3264 
3265       // If the function is already parsed we can insert the block address right
3266       // away.
3267       BasicBlock *BB;
3268       unsigned BBID = Record[2];
3269       if (!BBID)
3270         // Invalid reference to entry block.
3271         return error("Invalid ID");
3272       if (!Fn->empty()) {
3273         Function::iterator BBI = Fn->begin(), BBE = Fn->end();
3274         for (size_t I = 0, E = BBID; I != E; ++I) {
3275           if (BBI == BBE)
3276             return error("Invalid ID");
3277           ++BBI;
3278         }
3279         BB = &*BBI;
3280       } else {
3281         // Otherwise insert a placeholder and remember it so it can be inserted
3282         // when the function is parsed.
3283         auto &FwdBBs = BasicBlockFwdRefs[Fn];
3284         if (FwdBBs.empty())
3285           BasicBlockFwdRefQueue.push_back(Fn);
3286         if (FwdBBs.size() < BBID + 1)
3287           FwdBBs.resize(BBID + 1);
3288         if (!FwdBBs[BBID])
3289           FwdBBs[BBID] = BasicBlock::Create(Context);
3290         BB = FwdBBs[BBID];
3291       }
3292       V = BlockAddress::get(Fn, BB);
3293       break;
3294     }
3295     }
3296 
3297     ValueList.assignValue(V, NextCstNo);
3298     ++NextCstNo;
3299   }
3300 }
3301 
3302 std::error_code BitcodeReader::parseUseLists() {
3303   if (Stream.EnterSubBlock(bitc::USELIST_BLOCK_ID))
3304     return error("Invalid record");
3305 
3306   // Read all the records.
3307   SmallVector<uint64_t, 64> Record;
3308   while (1) {
3309     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
3310 
3311     switch (Entry.Kind) {
3312     case BitstreamEntry::SubBlock: // Handled for us already.
3313     case BitstreamEntry::Error:
3314       return error("Malformed block");
3315     case BitstreamEntry::EndBlock:
3316       return std::error_code();
3317     case BitstreamEntry::Record:
3318       // The interesting case.
3319       break;
3320     }
3321 
3322     // Read a use list record.
3323     Record.clear();
3324     bool IsBB = false;
3325     switch (Stream.readRecord(Entry.ID, Record)) {
3326     default:  // Default behavior: unknown type.
3327       break;
3328     case bitc::USELIST_CODE_BB:
3329       IsBB = true;
3330       // fallthrough
3331     case bitc::USELIST_CODE_DEFAULT: {
3332       unsigned RecordLength = Record.size();
3333       if (RecordLength < 3)
3334         // Records should have at least an ID and two indexes.
3335         return error("Invalid record");
3336       unsigned ID = Record.back();
3337       Record.pop_back();
3338 
3339       Value *V;
3340       if (IsBB) {
3341         assert(ID < FunctionBBs.size() && "Basic block not found");
3342         V = FunctionBBs[ID];
3343       } else
3344         V = ValueList[ID];
3345       unsigned NumUses = 0;
3346       SmallDenseMap<const Use *, unsigned, 16> Order;
3347       for (const Use &U : V->materialized_uses()) {
3348         if (++NumUses > Record.size())
3349           break;
3350         Order[&U] = Record[NumUses - 1];
3351       }
3352       if (Order.size() != Record.size() || NumUses > Record.size())
3353         // Mismatches can happen if the functions are being materialized lazily
3354         // (out-of-order), or a value has been upgraded.
3355         break;
3356 
3357       V->sortUseList([&](const Use &L, const Use &R) {
3358         return Order.lookup(&L) < Order.lookup(&R);
3359       });
3360       break;
3361     }
3362     }
3363   }
3364 }
3365 
3366 /// When we see the block for metadata, remember where it is and then skip it.
3367 /// This lets us lazily deserialize the metadata.
3368 std::error_code BitcodeReader::rememberAndSkipMetadata() {
3369   // Save the current stream state.
3370   uint64_t CurBit = Stream.GetCurrentBitNo();
3371   DeferredMetadataInfo.push_back(CurBit);
3372 
3373   // Skip over the block for now.
3374   if (Stream.SkipBlock())
3375     return error("Invalid record");
3376   return std::error_code();
3377 }
3378 
3379 std::error_code BitcodeReader::materializeMetadata() {
3380   for (uint64_t BitPos : DeferredMetadataInfo) {
3381     // Move the bit stream to the saved position.
3382     Stream.JumpToBit(BitPos);
3383     if (std::error_code EC = parseMetadata(true))
3384       return EC;
3385   }
3386   DeferredMetadataInfo.clear();
3387   return std::error_code();
3388 }
3389 
3390 void BitcodeReader::setStripDebugInfo() { StripDebugInfo = true; }
3391 
3392 /// When we see the block for a function body, remember where it is and then
3393 /// skip it.  This lets us lazily deserialize the functions.
3394 std::error_code BitcodeReader::rememberAndSkipFunctionBody() {
3395   // Get the function we are talking about.
3396   if (FunctionsWithBodies.empty())
3397     return error("Insufficient function protos");
3398 
3399   Function *Fn = FunctionsWithBodies.back();
3400   FunctionsWithBodies.pop_back();
3401 
3402   // Save the current stream state.
3403   uint64_t CurBit = Stream.GetCurrentBitNo();
3404   assert(
3405       (DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo[Fn] == CurBit) &&
3406       "Mismatch between VST and scanned function offsets");
3407   DeferredFunctionInfo[Fn] = CurBit;
3408 
3409   // Skip over the function block for now.
3410   if (Stream.SkipBlock())
3411     return error("Invalid record");
3412   return std::error_code();
3413 }
3414 
3415 std::error_code BitcodeReader::globalCleanup() {
3416   // Patch the initializers for globals and aliases up.
3417   resolveGlobalAndIndirectSymbolInits();
3418   if (!GlobalInits.empty() || !IndirectSymbolInits.empty())
3419     return error("Malformed global initializer set");
3420 
3421   // Look for intrinsic functions which need to be upgraded at some point
3422   for (Function &F : *TheModule) {
3423     Function *NewFn;
3424     if (UpgradeIntrinsicFunction(&F, NewFn))
3425       UpgradedIntrinsics[&F] = NewFn;
3426   }
3427 
3428   // Look for global variables which need to be renamed.
3429   for (GlobalVariable &GV : TheModule->globals())
3430     UpgradeGlobalVariable(&GV);
3431 
3432   // Force deallocation of memory for these vectors to favor the client that
3433   // want lazy deserialization.
3434   std::vector<std::pair<GlobalVariable*, unsigned> >().swap(GlobalInits);
3435   std::vector<std::pair<GlobalIndirectSymbol*, unsigned> >().swap(
3436       IndirectSymbolInits);
3437   return std::error_code();
3438 }
3439 
3440 /// Support for lazy parsing of function bodies. This is required if we
3441 /// either have an old bitcode file without a VST forward declaration record,
3442 /// or if we have an anonymous function being materialized, since anonymous
3443 /// functions do not have a name and are therefore not in the VST.
3444 std::error_code BitcodeReader::rememberAndSkipFunctionBodies() {
3445   Stream.JumpToBit(NextUnreadBit);
3446 
3447   if (Stream.AtEndOfStream())
3448     return error("Could not find function in stream");
3449 
3450   if (!SeenFirstFunctionBody)
3451     return error("Trying to materialize functions before seeing function blocks");
3452 
3453   // An old bitcode file with the symbol table at the end would have
3454   // finished the parse greedily.
3455   assert(SeenValueSymbolTable);
3456 
3457   SmallVector<uint64_t, 64> Record;
3458 
3459   while (1) {
3460     BitstreamEntry Entry = Stream.advance();
3461     switch (Entry.Kind) {
3462     default:
3463       return error("Expect SubBlock");
3464     case BitstreamEntry::SubBlock:
3465       switch (Entry.ID) {
3466       default:
3467         return error("Expect function block");
3468       case bitc::FUNCTION_BLOCK_ID:
3469         if (std::error_code EC = rememberAndSkipFunctionBody())
3470           return EC;
3471         NextUnreadBit = Stream.GetCurrentBitNo();
3472         return std::error_code();
3473       }
3474     }
3475   }
3476 }
3477 
3478 std::error_code BitcodeReader::parseBitcodeVersion() {
3479   if (Stream.EnterSubBlock(bitc::IDENTIFICATION_BLOCK_ID))
3480     return error("Invalid record");
3481 
3482   // Read all the records.
3483   SmallVector<uint64_t, 64> Record;
3484   while (1) {
3485     BitstreamEntry Entry = Stream.advance();
3486 
3487     switch (Entry.Kind) {
3488     default:
3489     case BitstreamEntry::Error:
3490       return error("Malformed block");
3491     case BitstreamEntry::EndBlock:
3492       return std::error_code();
3493     case BitstreamEntry::Record:
3494       // The interesting case.
3495       break;
3496     }
3497 
3498     // Read a record.
3499     Record.clear();
3500     unsigned BitCode = Stream.readRecord(Entry.ID, Record);
3501     switch (BitCode) {
3502     default: // Default behavior: reject
3503       return error("Invalid value");
3504     case bitc::IDENTIFICATION_CODE_STRING: { // IDENTIFICATION:      [strchr x
3505                                              // N]
3506       convertToString(Record, 0, ProducerIdentification);
3507       break;
3508     }
3509     case bitc::IDENTIFICATION_CODE_EPOCH: { // EPOCH:      [epoch#]
3510       unsigned epoch = (unsigned)Record[0];
3511       if (epoch != bitc::BITCODE_CURRENT_EPOCH) {
3512         return error(
3513           Twine("Incompatible epoch: Bitcode '") + Twine(epoch) +
3514           "' vs current: '" + Twine(bitc::BITCODE_CURRENT_EPOCH) + "'");
3515       }
3516     }
3517     }
3518   }
3519 }
3520 
3521 std::error_code BitcodeReader::parseModule(uint64_t ResumeBit,
3522                                            bool ShouldLazyLoadMetadata) {
3523   if (ResumeBit)
3524     Stream.JumpToBit(ResumeBit);
3525   else if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
3526     return error("Invalid record");
3527 
3528   SmallVector<uint64_t, 64> Record;
3529   std::vector<std::string> SectionTable;
3530   std::vector<std::string> GCTable;
3531 
3532   // Read all the records for this module.
3533   while (1) {
3534     BitstreamEntry Entry = Stream.advance();
3535 
3536     switch (Entry.Kind) {
3537     case BitstreamEntry::Error:
3538       return error("Malformed block");
3539     case BitstreamEntry::EndBlock:
3540       return globalCleanup();
3541 
3542     case BitstreamEntry::SubBlock:
3543       switch (Entry.ID) {
3544       default:  // Skip unknown content.
3545         if (Stream.SkipBlock())
3546           return error("Invalid record");
3547         break;
3548       case bitc::BLOCKINFO_BLOCK_ID:
3549         if (Stream.ReadBlockInfoBlock())
3550           return error("Malformed block");
3551         break;
3552       case bitc::PARAMATTR_BLOCK_ID:
3553         if (std::error_code EC = parseAttributeBlock())
3554           return EC;
3555         break;
3556       case bitc::PARAMATTR_GROUP_BLOCK_ID:
3557         if (std::error_code EC = parseAttributeGroupBlock())
3558           return EC;
3559         break;
3560       case bitc::TYPE_BLOCK_ID_NEW:
3561         if (std::error_code EC = parseTypeTable())
3562           return EC;
3563         break;
3564       case bitc::VALUE_SYMTAB_BLOCK_ID:
3565         if (!SeenValueSymbolTable) {
3566           // Either this is an old form VST without function index and an
3567           // associated VST forward declaration record (which would have caused
3568           // the VST to be jumped to and parsed before it was encountered
3569           // normally in the stream), or there were no function blocks to
3570           // trigger an earlier parsing of the VST.
3571           assert(VSTOffset == 0 || FunctionsWithBodies.empty());
3572           if (std::error_code EC = parseValueSymbolTable())
3573             return EC;
3574           SeenValueSymbolTable = true;
3575         } else {
3576           // We must have had a VST forward declaration record, which caused
3577           // the parser to jump to and parse the VST earlier.
3578           assert(VSTOffset > 0);
3579           if (Stream.SkipBlock())
3580             return error("Invalid record");
3581         }
3582         break;
3583       case bitc::CONSTANTS_BLOCK_ID:
3584         if (std::error_code EC = parseConstants())
3585           return EC;
3586         if (std::error_code EC = resolveGlobalAndIndirectSymbolInits())
3587           return EC;
3588         break;
3589       case bitc::METADATA_BLOCK_ID:
3590         if (ShouldLazyLoadMetadata && !IsMetadataMaterialized) {
3591           if (std::error_code EC = rememberAndSkipMetadata())
3592             return EC;
3593           break;
3594         }
3595         assert(DeferredMetadataInfo.empty() && "Unexpected deferred metadata");
3596         if (std::error_code EC = parseMetadata(true))
3597           return EC;
3598         break;
3599       case bitc::METADATA_KIND_BLOCK_ID:
3600         if (std::error_code EC = parseMetadataKinds())
3601           return EC;
3602         break;
3603       case bitc::FUNCTION_BLOCK_ID:
3604         // If this is the first function body we've seen, reverse the
3605         // FunctionsWithBodies list.
3606         if (!SeenFirstFunctionBody) {
3607           std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end());
3608           if (std::error_code EC = globalCleanup())
3609             return EC;
3610           SeenFirstFunctionBody = true;
3611         }
3612 
3613         if (VSTOffset > 0) {
3614           // If we have a VST forward declaration record, make sure we
3615           // parse the VST now if we haven't already. It is needed to
3616           // set up the DeferredFunctionInfo vector for lazy reading.
3617           if (!SeenValueSymbolTable) {
3618             if (std::error_code EC =
3619                     BitcodeReader::parseValueSymbolTable(VSTOffset))
3620               return EC;
3621             SeenValueSymbolTable = true;
3622             // Fall through so that we record the NextUnreadBit below.
3623             // This is necessary in case we have an anonymous function that
3624             // is later materialized. Since it will not have a VST entry we
3625             // need to fall back to the lazy parse to find its offset.
3626           } else {
3627             // If we have a VST forward declaration record, but have already
3628             // parsed the VST (just above, when the first function body was
3629             // encountered here), then we are resuming the parse after
3630             // materializing functions. The ResumeBit points to the
3631             // start of the last function block recorded in the
3632             // DeferredFunctionInfo map. Skip it.
3633             if (Stream.SkipBlock())
3634               return error("Invalid record");
3635             continue;
3636           }
3637         }
3638 
3639         // Support older bitcode files that did not have the function
3640         // index in the VST, nor a VST forward declaration record, as
3641         // well as anonymous functions that do not have VST entries.
3642         // Build the DeferredFunctionInfo vector on the fly.
3643         if (std::error_code EC = rememberAndSkipFunctionBody())
3644           return EC;
3645 
3646         // Suspend parsing when we reach the function bodies. Subsequent
3647         // materialization calls will resume it when necessary. If the bitcode
3648         // file is old, the symbol table will be at the end instead and will not
3649         // have been seen yet. In this case, just finish the parse now.
3650         if (SeenValueSymbolTable) {
3651           NextUnreadBit = Stream.GetCurrentBitNo();
3652           return std::error_code();
3653         }
3654         break;
3655       case bitc::USELIST_BLOCK_ID:
3656         if (std::error_code EC = parseUseLists())
3657           return EC;
3658         break;
3659       case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID:
3660         if (std::error_code EC = parseOperandBundleTags())
3661           return EC;
3662         break;
3663       }
3664       continue;
3665 
3666     case BitstreamEntry::Record:
3667       // The interesting case.
3668       break;
3669     }
3670 
3671     // Read a record.
3672     auto BitCode = Stream.readRecord(Entry.ID, Record);
3673     switch (BitCode) {
3674     default: break;  // Default behavior, ignore unknown content.
3675     case bitc::MODULE_CODE_VERSION: {  // VERSION: [version#]
3676       if (Record.size() < 1)
3677         return error("Invalid record");
3678       // Only version #0 and #1 are supported so far.
3679       unsigned module_version = Record[0];
3680       switch (module_version) {
3681         default:
3682           return error("Invalid value");
3683         case 0:
3684           UseRelativeIDs = false;
3685           break;
3686         case 1:
3687           UseRelativeIDs = true;
3688           break;
3689       }
3690       break;
3691     }
3692     case bitc::MODULE_CODE_TRIPLE: {  // TRIPLE: [strchr x N]
3693       std::string S;
3694       if (convertToString(Record, 0, S))
3695         return error("Invalid record");
3696       TheModule->setTargetTriple(S);
3697       break;
3698     }
3699     case bitc::MODULE_CODE_DATALAYOUT: {  // DATALAYOUT: [strchr x N]
3700       std::string S;
3701       if (convertToString(Record, 0, S))
3702         return error("Invalid record");
3703       TheModule->setDataLayout(S);
3704       break;
3705     }
3706     case bitc::MODULE_CODE_ASM: {  // ASM: [strchr x N]
3707       std::string S;
3708       if (convertToString(Record, 0, S))
3709         return error("Invalid record");
3710       TheModule->setModuleInlineAsm(S);
3711       break;
3712     }
3713     case bitc::MODULE_CODE_DEPLIB: {  // DEPLIB: [strchr x N]
3714       // FIXME: Remove in 4.0.
3715       std::string S;
3716       if (convertToString(Record, 0, S))
3717         return error("Invalid record");
3718       // Ignore value.
3719       break;
3720     }
3721     case bitc::MODULE_CODE_SECTIONNAME: {  // SECTIONNAME: [strchr x N]
3722       std::string S;
3723       if (convertToString(Record, 0, S))
3724         return error("Invalid record");
3725       SectionTable.push_back(S);
3726       break;
3727     }
3728     case bitc::MODULE_CODE_GCNAME: {  // SECTIONNAME: [strchr x N]
3729       std::string S;
3730       if (convertToString(Record, 0, S))
3731         return error("Invalid record");
3732       GCTable.push_back(S);
3733       break;
3734     }
3735     case bitc::MODULE_CODE_COMDAT: { // COMDAT: [selection_kind, name]
3736       if (Record.size() < 2)
3737         return error("Invalid record");
3738       Comdat::SelectionKind SK = getDecodedComdatSelectionKind(Record[0]);
3739       unsigned ComdatNameSize = Record[1];
3740       std::string ComdatName;
3741       ComdatName.reserve(ComdatNameSize);
3742       for (unsigned i = 0; i != ComdatNameSize; ++i)
3743         ComdatName += (char)Record[2 + i];
3744       Comdat *C = TheModule->getOrInsertComdat(ComdatName);
3745       C->setSelectionKind(SK);
3746       ComdatList.push_back(C);
3747       break;
3748     }
3749     // GLOBALVAR: [pointer type, isconst, initid,
3750     //             linkage, alignment, section, visibility, threadlocal,
3751     //             unnamed_addr, externally_initialized, dllstorageclass,
3752     //             comdat]
3753     case bitc::MODULE_CODE_GLOBALVAR: {
3754       if (Record.size() < 6)
3755         return error("Invalid record");
3756       Type *Ty = getTypeByID(Record[0]);
3757       if (!Ty)
3758         return error("Invalid record");
3759       bool isConstant = Record[1] & 1;
3760       bool explicitType = Record[1] & 2;
3761       unsigned AddressSpace;
3762       if (explicitType) {
3763         AddressSpace = Record[1] >> 2;
3764       } else {
3765         if (!Ty->isPointerTy())
3766           return error("Invalid type for value");
3767         AddressSpace = cast<PointerType>(Ty)->getAddressSpace();
3768         Ty = cast<PointerType>(Ty)->getElementType();
3769       }
3770 
3771       uint64_t RawLinkage = Record[3];
3772       GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage);
3773       unsigned Alignment;
3774       if (std::error_code EC = parseAlignmentValue(Record[4], Alignment))
3775         return EC;
3776       std::string Section;
3777       if (Record[5]) {
3778         if (Record[5]-1 >= SectionTable.size())
3779           return error("Invalid ID");
3780         Section = SectionTable[Record[5]-1];
3781       }
3782       GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility;
3783       // Local linkage must have default visibility.
3784       if (Record.size() > 6 && !GlobalValue::isLocalLinkage(Linkage))
3785         // FIXME: Change to an error if non-default in 4.0.
3786         Visibility = getDecodedVisibility(Record[6]);
3787 
3788       GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal;
3789       if (Record.size() > 7)
3790         TLM = getDecodedThreadLocalMode(Record[7]);
3791 
3792       bool UnnamedAddr = false;
3793       if (Record.size() > 8)
3794         UnnamedAddr = Record[8];
3795 
3796       bool ExternallyInitialized = false;
3797       if (Record.size() > 9)
3798         ExternallyInitialized = Record[9];
3799 
3800       GlobalVariable *NewGV =
3801         new GlobalVariable(*TheModule, Ty, isConstant, Linkage, nullptr, "", nullptr,
3802                            TLM, AddressSpace, ExternallyInitialized);
3803       NewGV->setAlignment(Alignment);
3804       if (!Section.empty())
3805         NewGV->setSection(Section);
3806       NewGV->setVisibility(Visibility);
3807       NewGV->setUnnamedAddr(UnnamedAddr);
3808 
3809       if (Record.size() > 10)
3810         NewGV->setDLLStorageClass(getDecodedDLLStorageClass(Record[10]));
3811       else
3812         upgradeDLLImportExportLinkage(NewGV, RawLinkage);
3813 
3814       ValueList.push_back(NewGV);
3815 
3816       // Remember which value to use for the global initializer.
3817       if (unsigned InitID = Record[2])
3818         GlobalInits.push_back(std::make_pair(NewGV, InitID-1));
3819 
3820       if (Record.size() > 11) {
3821         if (unsigned ComdatID = Record[11]) {
3822           if (ComdatID > ComdatList.size())
3823             return error("Invalid global variable comdat ID");
3824           NewGV->setComdat(ComdatList[ComdatID - 1]);
3825         }
3826       } else if (hasImplicitComdat(RawLinkage)) {
3827         NewGV->setComdat(reinterpret_cast<Comdat *>(1));
3828       }
3829       break;
3830     }
3831     // FUNCTION:  [type, callingconv, isproto, linkage, paramattr,
3832     //             alignment, section, visibility, gc, unnamed_addr,
3833     //             prologuedata, dllstorageclass, comdat, prefixdata]
3834     case bitc::MODULE_CODE_FUNCTION: {
3835       if (Record.size() < 8)
3836         return error("Invalid record");
3837       Type *Ty = getTypeByID(Record[0]);
3838       if (!Ty)
3839         return error("Invalid record");
3840       if (auto *PTy = dyn_cast<PointerType>(Ty))
3841         Ty = PTy->getElementType();
3842       auto *FTy = dyn_cast<FunctionType>(Ty);
3843       if (!FTy)
3844         return error("Invalid type for value");
3845       auto CC = static_cast<CallingConv::ID>(Record[1]);
3846       if (CC & ~CallingConv::MaxID)
3847         return error("Invalid calling convention ID");
3848 
3849       Function *Func = Function::Create(FTy, GlobalValue::ExternalLinkage,
3850                                         "", TheModule);
3851 
3852       Func->setCallingConv(CC);
3853       bool isProto = Record[2];
3854       uint64_t RawLinkage = Record[3];
3855       Func->setLinkage(getDecodedLinkage(RawLinkage));
3856       Func->setAttributes(getAttributes(Record[4]));
3857 
3858       unsigned Alignment;
3859       if (std::error_code EC = parseAlignmentValue(Record[5], Alignment))
3860         return EC;
3861       Func->setAlignment(Alignment);
3862       if (Record[6]) {
3863         if (Record[6]-1 >= SectionTable.size())
3864           return error("Invalid ID");
3865         Func->setSection(SectionTable[Record[6]-1]);
3866       }
3867       // Local linkage must have default visibility.
3868       if (!Func->hasLocalLinkage())
3869         // FIXME: Change to an error if non-default in 4.0.
3870         Func->setVisibility(getDecodedVisibility(Record[7]));
3871       if (Record.size() > 8 && Record[8]) {
3872         if (Record[8]-1 >= GCTable.size())
3873           return error("Invalid ID");
3874         Func->setGC(GCTable[Record[8]-1].c_str());
3875       }
3876       bool UnnamedAddr = false;
3877       if (Record.size() > 9)
3878         UnnamedAddr = Record[9];
3879       Func->setUnnamedAddr(UnnamedAddr);
3880       if (Record.size() > 10 && Record[10] != 0)
3881         FunctionPrologues.push_back(std::make_pair(Func, Record[10]-1));
3882 
3883       if (Record.size() > 11)
3884         Func->setDLLStorageClass(getDecodedDLLStorageClass(Record[11]));
3885       else
3886         upgradeDLLImportExportLinkage(Func, RawLinkage);
3887 
3888       if (Record.size() > 12) {
3889         if (unsigned ComdatID = Record[12]) {
3890           if (ComdatID > ComdatList.size())
3891             return error("Invalid function comdat ID");
3892           Func->setComdat(ComdatList[ComdatID - 1]);
3893         }
3894       } else if (hasImplicitComdat(RawLinkage)) {
3895         Func->setComdat(reinterpret_cast<Comdat *>(1));
3896       }
3897 
3898       if (Record.size() > 13 && Record[13] != 0)
3899         FunctionPrefixes.push_back(std::make_pair(Func, Record[13]-1));
3900 
3901       if (Record.size() > 14 && Record[14] != 0)
3902         FunctionPersonalityFns.push_back(std::make_pair(Func, Record[14] - 1));
3903 
3904       ValueList.push_back(Func);
3905 
3906       // If this is a function with a body, remember the prototype we are
3907       // creating now, so that we can match up the body with them later.
3908       if (!isProto) {
3909         Func->setIsMaterializable(true);
3910         FunctionsWithBodies.push_back(Func);
3911         DeferredFunctionInfo[Func] = 0;
3912       }
3913       break;
3914     }
3915     // ALIAS: [alias type, addrspace, aliasee val#, linkage]
3916     // ALIAS: [alias type, addrspace, aliasee val#, linkage, visibility, dllstorageclass]
3917     // IFUNC: [alias type, addrspace, aliasee val#, linkage, visibility, dllstorageclass]
3918     case bitc::MODULE_CODE_IFUNC:
3919     case bitc::MODULE_CODE_ALIAS:
3920     case bitc::MODULE_CODE_ALIAS_OLD: {
3921       bool NewRecord = BitCode != bitc::MODULE_CODE_ALIAS_OLD;
3922       if (Record.size() < (3 + (unsigned)NewRecord))
3923         return error("Invalid record");
3924       unsigned OpNum = 0;
3925       Type *Ty = getTypeByID(Record[OpNum++]);
3926       if (!Ty)
3927         return error("Invalid record");
3928 
3929       unsigned AddrSpace;
3930       if (!NewRecord) {
3931         auto *PTy = dyn_cast<PointerType>(Ty);
3932         if (!PTy)
3933           return error("Invalid type for value");
3934         Ty = PTy->getElementType();
3935         AddrSpace = PTy->getAddressSpace();
3936       } else {
3937         AddrSpace = Record[OpNum++];
3938       }
3939 
3940       auto Val = Record[OpNum++];
3941       auto Linkage = Record[OpNum++];
3942       GlobalIndirectSymbol *NewGA;
3943       if (BitCode == bitc::MODULE_CODE_ALIAS ||
3944           BitCode == bitc::MODULE_CODE_ALIAS_OLD)
3945         NewGA = GlobalAlias::create(Ty, AddrSpace, getDecodedLinkage(Linkage),
3946                                     "", TheModule);
3947       else
3948         NewGA = GlobalIFunc::create(Ty, AddrSpace, getDecodedLinkage(Linkage),
3949                                     "", nullptr, TheModule);
3950       // Old bitcode files didn't have visibility field.
3951       // Local linkage must have default visibility.
3952       if (OpNum != Record.size()) {
3953         auto VisInd = OpNum++;
3954         if (!NewGA->hasLocalLinkage())
3955           // FIXME: Change to an error if non-default in 4.0.
3956           NewGA->setVisibility(getDecodedVisibility(Record[VisInd]));
3957       }
3958       if (OpNum != Record.size())
3959         NewGA->setDLLStorageClass(getDecodedDLLStorageClass(Record[OpNum++]));
3960       else
3961         upgradeDLLImportExportLinkage(NewGA, Linkage);
3962       if (OpNum != Record.size())
3963         NewGA->setThreadLocalMode(getDecodedThreadLocalMode(Record[OpNum++]));
3964       if (OpNum != Record.size())
3965         NewGA->setUnnamedAddr(Record[OpNum++]);
3966       ValueList.push_back(NewGA);
3967       IndirectSymbolInits.push_back(std::make_pair(NewGA, Val));
3968       break;
3969     }
3970     /// MODULE_CODE_PURGEVALS: [numvals]
3971     case bitc::MODULE_CODE_PURGEVALS:
3972       // Trim down the value list to the specified size.
3973       if (Record.size() < 1 || Record[0] > ValueList.size())
3974         return error("Invalid record");
3975       ValueList.shrinkTo(Record[0]);
3976       break;
3977     /// MODULE_CODE_VSTOFFSET: [offset]
3978     case bitc::MODULE_CODE_VSTOFFSET:
3979       if (Record.size() < 1)
3980         return error("Invalid record");
3981       VSTOffset = Record[0];
3982       break;
3983     /// MODULE_CODE_SOURCE_FILENAME: [namechar x N]
3984     case bitc::MODULE_CODE_SOURCE_FILENAME:
3985       SmallString<128> ValueName;
3986       if (convertToString(Record, 0, ValueName))
3987         return error("Invalid record");
3988       TheModule->setSourceFileName(ValueName);
3989       break;
3990     }
3991     Record.clear();
3992   }
3993 }
3994 
3995 /// Helper to read the header common to all bitcode files.
3996 static bool hasValidBitcodeHeader(BitstreamCursor &Stream) {
3997   // Sniff for the signature.
3998   if (Stream.Read(8) != 'B' ||
3999       Stream.Read(8) != 'C' ||
4000       Stream.Read(4) != 0x0 ||
4001       Stream.Read(4) != 0xC ||
4002       Stream.Read(4) != 0xE ||
4003       Stream.Read(4) != 0xD)
4004     return false;
4005   return true;
4006 }
4007 
4008 std::error_code
4009 BitcodeReader::parseBitcodeInto(std::unique_ptr<DataStreamer> Streamer,
4010                                 Module *M, bool ShouldLazyLoadMetadata) {
4011   TheModule = M;
4012 
4013   if (std::error_code EC = initStream(std::move(Streamer)))
4014     return EC;
4015 
4016   // Sniff for the signature.
4017   if (!hasValidBitcodeHeader(Stream))
4018     return error("Invalid bitcode signature");
4019 
4020   // We expect a number of well-defined blocks, though we don't necessarily
4021   // need to understand them all.
4022   while (1) {
4023     if (Stream.AtEndOfStream()) {
4024       // We didn't really read a proper Module.
4025       return error("Malformed IR file");
4026     }
4027 
4028     BitstreamEntry Entry =
4029       Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs);
4030 
4031     if (Entry.Kind != BitstreamEntry::SubBlock)
4032       return error("Malformed block");
4033 
4034     if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID) {
4035       parseBitcodeVersion();
4036       continue;
4037     }
4038 
4039     if (Entry.ID == bitc::MODULE_BLOCK_ID)
4040       return parseModule(0, ShouldLazyLoadMetadata);
4041 
4042     if (Stream.SkipBlock())
4043       return error("Invalid record");
4044   }
4045 }
4046 
4047 ErrorOr<std::string> BitcodeReader::parseModuleTriple() {
4048   if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
4049     return error("Invalid record");
4050 
4051   SmallVector<uint64_t, 64> Record;
4052 
4053   std::string Triple;
4054   // Read all the records for this module.
4055   while (1) {
4056     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
4057 
4058     switch (Entry.Kind) {
4059     case BitstreamEntry::SubBlock: // Handled for us already.
4060     case BitstreamEntry::Error:
4061       return error("Malformed block");
4062     case BitstreamEntry::EndBlock:
4063       return Triple;
4064     case BitstreamEntry::Record:
4065       // The interesting case.
4066       break;
4067     }
4068 
4069     // Read a record.
4070     switch (Stream.readRecord(Entry.ID, Record)) {
4071     default: break;  // Default behavior, ignore unknown content.
4072     case bitc::MODULE_CODE_TRIPLE: {  // TRIPLE: [strchr x N]
4073       std::string S;
4074       if (convertToString(Record, 0, S))
4075         return error("Invalid record");
4076       Triple = S;
4077       break;
4078     }
4079     }
4080     Record.clear();
4081   }
4082   llvm_unreachable("Exit infinite loop");
4083 }
4084 
4085 ErrorOr<std::string> BitcodeReader::parseTriple() {
4086   if (std::error_code EC = initStream(nullptr))
4087     return EC;
4088 
4089   // Sniff for the signature.
4090   if (!hasValidBitcodeHeader(Stream))
4091     return error("Invalid bitcode signature");
4092 
4093   // We expect a number of well-defined blocks, though we don't necessarily
4094   // need to understand them all.
4095   while (1) {
4096     BitstreamEntry Entry = Stream.advance();
4097 
4098     switch (Entry.Kind) {
4099     case BitstreamEntry::Error:
4100       return error("Malformed block");
4101     case BitstreamEntry::EndBlock:
4102       return std::error_code();
4103 
4104     case BitstreamEntry::SubBlock:
4105       if (Entry.ID == bitc::MODULE_BLOCK_ID)
4106         return parseModuleTriple();
4107 
4108       // Ignore other sub-blocks.
4109       if (Stream.SkipBlock())
4110         return error("Malformed block");
4111       continue;
4112 
4113     case BitstreamEntry::Record:
4114       Stream.skipRecord(Entry.ID);
4115       continue;
4116     }
4117   }
4118 }
4119 
4120 ErrorOr<std::string> BitcodeReader::parseIdentificationBlock() {
4121   if (std::error_code EC = initStream(nullptr))
4122     return EC;
4123 
4124   // Sniff for the signature.
4125   if (!hasValidBitcodeHeader(Stream))
4126     return error("Invalid bitcode signature");
4127 
4128   // We expect a number of well-defined blocks, though we don't necessarily
4129   // need to understand them all.
4130   while (1) {
4131     BitstreamEntry Entry = Stream.advance();
4132     switch (Entry.Kind) {
4133     case BitstreamEntry::Error:
4134       return error("Malformed block");
4135     case BitstreamEntry::EndBlock:
4136       return std::error_code();
4137 
4138     case BitstreamEntry::SubBlock:
4139       if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID) {
4140         if (std::error_code EC = parseBitcodeVersion())
4141           return EC;
4142         return ProducerIdentification;
4143       }
4144       // Ignore other sub-blocks.
4145       if (Stream.SkipBlock())
4146         return error("Malformed block");
4147       continue;
4148     case BitstreamEntry::Record:
4149       Stream.skipRecord(Entry.ID);
4150       continue;
4151     }
4152   }
4153 }
4154 
4155 /// Parse metadata attachments.
4156 std::error_code BitcodeReader::parseMetadataAttachment(Function &F) {
4157   if (Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID))
4158     return error("Invalid record");
4159 
4160   SmallVector<uint64_t, 64> Record;
4161   while (1) {
4162     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
4163 
4164     switch (Entry.Kind) {
4165     case BitstreamEntry::SubBlock: // Handled for us already.
4166     case BitstreamEntry::Error:
4167       return error("Malformed block");
4168     case BitstreamEntry::EndBlock:
4169       return std::error_code();
4170     case BitstreamEntry::Record:
4171       // The interesting case.
4172       break;
4173     }
4174 
4175     // Read a metadata attachment record.
4176     Record.clear();
4177     switch (Stream.readRecord(Entry.ID, Record)) {
4178     default:  // Default behavior: ignore.
4179       break;
4180     case bitc::METADATA_ATTACHMENT: {
4181       unsigned RecordLength = Record.size();
4182       if (Record.empty())
4183         return error("Invalid record");
4184       if (RecordLength % 2 == 0) {
4185         // A function attachment.
4186         for (unsigned I = 0; I != RecordLength; I += 2) {
4187           auto K = MDKindMap.find(Record[I]);
4188           if (K == MDKindMap.end())
4189             return error("Invalid ID");
4190           MDNode *MD = MetadataList.getMDNodeFwdRefOrNull(Record[I + 1]);
4191           if (!MD)
4192             return error("Invalid metadata attachment");
4193           F.setMetadata(K->second, MD);
4194         }
4195         continue;
4196       }
4197 
4198       // An instruction attachment.
4199       Instruction *Inst = InstructionList[Record[0]];
4200       for (unsigned i = 1; i != RecordLength; i = i+2) {
4201         unsigned Kind = Record[i];
4202         DenseMap<unsigned, unsigned>::iterator I =
4203           MDKindMap.find(Kind);
4204         if (I == MDKindMap.end())
4205           return error("Invalid ID");
4206         Metadata *Node = MetadataList.getMetadataFwdRef(Record[i + 1]);
4207         if (isa<LocalAsMetadata>(Node))
4208           // Drop the attachment.  This used to be legal, but there's no
4209           // upgrade path.
4210           break;
4211         MDNode *MD = dyn_cast_or_null<MDNode>(Node);
4212         if (!MD)
4213           return error("Invalid metadata attachment");
4214 
4215         if (HasSeenOldLoopTags && I->second == LLVMContext::MD_loop)
4216           MD = upgradeInstructionLoopAttachment(*MD);
4217 
4218         Inst->setMetadata(I->second, MD);
4219         if (I->second == LLVMContext::MD_tbaa) {
4220           InstsWithTBAATag.push_back(Inst);
4221           continue;
4222         }
4223       }
4224       break;
4225     }
4226     }
4227   }
4228 }
4229 
4230 static std::error_code typeCheckLoadStoreInst(Type *ValType, Type *PtrType) {
4231   LLVMContext &Context = PtrType->getContext();
4232   if (!isa<PointerType>(PtrType))
4233     return error(Context, "Load/Store operand is not a pointer type");
4234   Type *ElemType = cast<PointerType>(PtrType)->getElementType();
4235 
4236   if (ValType && ValType != ElemType)
4237     return error(Context, "Explicit load/store type does not match pointee "
4238                           "type of pointer operand");
4239   if (!PointerType::isLoadableOrStorableType(ElemType))
4240     return error(Context, "Cannot load/store from pointer");
4241   return std::error_code();
4242 }
4243 
4244 /// Lazily parse the specified function body block.
4245 std::error_code BitcodeReader::parseFunctionBody(Function *F) {
4246   if (Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID))
4247     return error("Invalid record");
4248 
4249   // Unexpected unresolved metadata when parsing function.
4250   if (MetadataList.hasFwdRefs())
4251     return error("Invalid function metadata: incoming forward references");
4252 
4253   InstructionList.clear();
4254   unsigned ModuleValueListSize = ValueList.size();
4255   unsigned ModuleMetadataListSize = MetadataList.size();
4256 
4257   // Add all the function arguments to the value table.
4258   for (Argument &I : F->args())
4259     ValueList.push_back(&I);
4260 
4261   unsigned NextValueNo = ValueList.size();
4262   BasicBlock *CurBB = nullptr;
4263   unsigned CurBBNo = 0;
4264 
4265   DebugLoc LastLoc;
4266   auto getLastInstruction = [&]() -> Instruction * {
4267     if (CurBB && !CurBB->empty())
4268       return &CurBB->back();
4269     else if (CurBBNo && FunctionBBs[CurBBNo - 1] &&
4270              !FunctionBBs[CurBBNo - 1]->empty())
4271       return &FunctionBBs[CurBBNo - 1]->back();
4272     return nullptr;
4273   };
4274 
4275   std::vector<OperandBundleDef> OperandBundles;
4276 
4277   // Read all the records.
4278   SmallVector<uint64_t, 64> Record;
4279   while (1) {
4280     BitstreamEntry Entry = Stream.advance();
4281 
4282     switch (Entry.Kind) {
4283     case BitstreamEntry::Error:
4284       return error("Malformed block");
4285     case BitstreamEntry::EndBlock:
4286       goto OutOfRecordLoop;
4287 
4288     case BitstreamEntry::SubBlock:
4289       switch (Entry.ID) {
4290       default:  // Skip unknown content.
4291         if (Stream.SkipBlock())
4292           return error("Invalid record");
4293         break;
4294       case bitc::CONSTANTS_BLOCK_ID:
4295         if (std::error_code EC = parseConstants())
4296           return EC;
4297         NextValueNo = ValueList.size();
4298         break;
4299       case bitc::VALUE_SYMTAB_BLOCK_ID:
4300         if (std::error_code EC = parseValueSymbolTable())
4301           return EC;
4302         break;
4303       case bitc::METADATA_ATTACHMENT_ID:
4304         if (std::error_code EC = parseMetadataAttachment(*F))
4305           return EC;
4306         break;
4307       case bitc::METADATA_BLOCK_ID:
4308         if (std::error_code EC = parseMetadata())
4309           return EC;
4310         break;
4311       case bitc::USELIST_BLOCK_ID:
4312         if (std::error_code EC = parseUseLists())
4313           return EC;
4314         break;
4315       }
4316       continue;
4317 
4318     case BitstreamEntry::Record:
4319       // The interesting case.
4320       break;
4321     }
4322 
4323     // Read a record.
4324     Record.clear();
4325     Instruction *I = nullptr;
4326     unsigned BitCode = Stream.readRecord(Entry.ID, Record);
4327     switch (BitCode) {
4328     default: // Default behavior: reject
4329       return error("Invalid value");
4330     case bitc::FUNC_CODE_DECLAREBLOCKS: {   // DECLAREBLOCKS: [nblocks]
4331       if (Record.size() < 1 || Record[0] == 0)
4332         return error("Invalid record");
4333       // Create all the basic blocks for the function.
4334       FunctionBBs.resize(Record[0]);
4335 
4336       // See if anything took the address of blocks in this function.
4337       auto BBFRI = BasicBlockFwdRefs.find(F);
4338       if (BBFRI == BasicBlockFwdRefs.end()) {
4339         for (unsigned i = 0, e = FunctionBBs.size(); i != e; ++i)
4340           FunctionBBs[i] = BasicBlock::Create(Context, "", F);
4341       } else {
4342         auto &BBRefs = BBFRI->second;
4343         // Check for invalid basic block references.
4344         if (BBRefs.size() > FunctionBBs.size())
4345           return error("Invalid ID");
4346         assert(!BBRefs.empty() && "Unexpected empty array");
4347         assert(!BBRefs.front() && "Invalid reference to entry block");
4348         for (unsigned I = 0, E = FunctionBBs.size(), RE = BBRefs.size(); I != E;
4349              ++I)
4350           if (I < RE && BBRefs[I]) {
4351             BBRefs[I]->insertInto(F);
4352             FunctionBBs[I] = BBRefs[I];
4353           } else {
4354             FunctionBBs[I] = BasicBlock::Create(Context, "", F);
4355           }
4356 
4357         // Erase from the table.
4358         BasicBlockFwdRefs.erase(BBFRI);
4359       }
4360 
4361       CurBB = FunctionBBs[0];
4362       continue;
4363     }
4364 
4365     case bitc::FUNC_CODE_DEBUG_LOC_AGAIN:  // DEBUG_LOC_AGAIN
4366       // This record indicates that the last instruction is at the same
4367       // location as the previous instruction with a location.
4368       I = getLastInstruction();
4369 
4370       if (!I)
4371         return error("Invalid record");
4372       I->setDebugLoc(LastLoc);
4373       I = nullptr;
4374       continue;
4375 
4376     case bitc::FUNC_CODE_DEBUG_LOC: {      // DEBUG_LOC: [line, col, scope, ia]
4377       I = getLastInstruction();
4378       if (!I || Record.size() < 4)
4379         return error("Invalid record");
4380 
4381       unsigned Line = Record[0], Col = Record[1];
4382       unsigned ScopeID = Record[2], IAID = Record[3];
4383 
4384       MDNode *Scope = nullptr, *IA = nullptr;
4385       if (ScopeID) {
4386         Scope = MetadataList.getMDNodeFwdRefOrNull(ScopeID - 1);
4387         if (!Scope)
4388           return error("Invalid record");
4389       }
4390       if (IAID) {
4391         IA = MetadataList.getMDNodeFwdRefOrNull(IAID - 1);
4392         if (!IA)
4393           return error("Invalid record");
4394       }
4395       LastLoc = DebugLoc::get(Line, Col, Scope, IA);
4396       I->setDebugLoc(LastLoc);
4397       I = nullptr;
4398       continue;
4399     }
4400 
4401     case bitc::FUNC_CODE_INST_BINOP: {    // BINOP: [opval, ty, opval, opcode]
4402       unsigned OpNum = 0;
4403       Value *LHS, *RHS;
4404       if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
4405           popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) ||
4406           OpNum+1 > Record.size())
4407         return error("Invalid record");
4408 
4409       int Opc = getDecodedBinaryOpcode(Record[OpNum++], LHS->getType());
4410       if (Opc == -1)
4411         return error("Invalid record");
4412       I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
4413       InstructionList.push_back(I);
4414       if (OpNum < Record.size()) {
4415         if (Opc == Instruction::Add ||
4416             Opc == Instruction::Sub ||
4417             Opc == Instruction::Mul ||
4418             Opc == Instruction::Shl) {
4419           if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP))
4420             cast<BinaryOperator>(I)->setHasNoSignedWrap(true);
4421           if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
4422             cast<BinaryOperator>(I)->setHasNoUnsignedWrap(true);
4423         } else if (Opc == Instruction::SDiv ||
4424                    Opc == Instruction::UDiv ||
4425                    Opc == Instruction::LShr ||
4426                    Opc == Instruction::AShr) {
4427           if (Record[OpNum] & (1 << bitc::PEO_EXACT))
4428             cast<BinaryOperator>(I)->setIsExact(true);
4429         } else if (isa<FPMathOperator>(I)) {
4430           FastMathFlags FMF = getDecodedFastMathFlags(Record[OpNum]);
4431           if (FMF.any())
4432             I->setFastMathFlags(FMF);
4433         }
4434 
4435       }
4436       break;
4437     }
4438     case bitc::FUNC_CODE_INST_CAST: {    // CAST: [opval, opty, destty, castopc]
4439       unsigned OpNum = 0;
4440       Value *Op;
4441       if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
4442           OpNum+2 != Record.size())
4443         return error("Invalid record");
4444 
4445       Type *ResTy = getTypeByID(Record[OpNum]);
4446       int Opc = getDecodedCastOpcode(Record[OpNum + 1]);
4447       if (Opc == -1 || !ResTy)
4448         return error("Invalid record");
4449       Instruction *Temp = nullptr;
4450       if ((I = UpgradeBitCastInst(Opc, Op, ResTy, Temp))) {
4451         if (Temp) {
4452           InstructionList.push_back(Temp);
4453           CurBB->getInstList().push_back(Temp);
4454         }
4455       } else {
4456         auto CastOp = (Instruction::CastOps)Opc;
4457         if (!CastInst::castIsValid(CastOp, Op, ResTy))
4458           return error("Invalid cast");
4459         I = CastInst::Create(CastOp, Op, ResTy);
4460       }
4461       InstructionList.push_back(I);
4462       break;
4463     }
4464     case bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD:
4465     case bitc::FUNC_CODE_INST_GEP_OLD:
4466     case bitc::FUNC_CODE_INST_GEP: { // GEP: type, [n x operands]
4467       unsigned OpNum = 0;
4468 
4469       Type *Ty;
4470       bool InBounds;
4471 
4472       if (BitCode == bitc::FUNC_CODE_INST_GEP) {
4473         InBounds = Record[OpNum++];
4474         Ty = getTypeByID(Record[OpNum++]);
4475       } else {
4476         InBounds = BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD;
4477         Ty = nullptr;
4478       }
4479 
4480       Value *BasePtr;
4481       if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr))
4482         return error("Invalid record");
4483 
4484       if (!Ty)
4485         Ty = cast<SequentialType>(BasePtr->getType()->getScalarType())
4486                  ->getElementType();
4487       else if (Ty !=
4488                cast<SequentialType>(BasePtr->getType()->getScalarType())
4489                    ->getElementType())
4490         return error(
4491             "Explicit gep type does not match pointee type of pointer operand");
4492 
4493       SmallVector<Value*, 16> GEPIdx;
4494       while (OpNum != Record.size()) {
4495         Value *Op;
4496         if (getValueTypePair(Record, OpNum, NextValueNo, Op))
4497           return error("Invalid record");
4498         GEPIdx.push_back(Op);
4499       }
4500 
4501       I = GetElementPtrInst::Create(Ty, BasePtr, GEPIdx);
4502 
4503       InstructionList.push_back(I);
4504       if (InBounds)
4505         cast<GetElementPtrInst>(I)->setIsInBounds(true);
4506       break;
4507     }
4508 
4509     case bitc::FUNC_CODE_INST_EXTRACTVAL: {
4510                                        // EXTRACTVAL: [opty, opval, n x indices]
4511       unsigned OpNum = 0;
4512       Value *Agg;
4513       if (getValueTypePair(Record, OpNum, NextValueNo, Agg))
4514         return error("Invalid record");
4515 
4516       unsigned RecSize = Record.size();
4517       if (OpNum == RecSize)
4518         return error("EXTRACTVAL: Invalid instruction with 0 indices");
4519 
4520       SmallVector<unsigned, 4> EXTRACTVALIdx;
4521       Type *CurTy = Agg->getType();
4522       for (; OpNum != RecSize; ++OpNum) {
4523         bool IsArray = CurTy->isArrayTy();
4524         bool IsStruct = CurTy->isStructTy();
4525         uint64_t Index = Record[OpNum];
4526 
4527         if (!IsStruct && !IsArray)
4528           return error("EXTRACTVAL: Invalid type");
4529         if ((unsigned)Index != Index)
4530           return error("Invalid value");
4531         if (IsStruct && Index >= CurTy->subtypes().size())
4532           return error("EXTRACTVAL: Invalid struct index");
4533         if (IsArray && Index >= CurTy->getArrayNumElements())
4534           return error("EXTRACTVAL: Invalid array index");
4535         EXTRACTVALIdx.push_back((unsigned)Index);
4536 
4537         if (IsStruct)
4538           CurTy = CurTy->subtypes()[Index];
4539         else
4540           CurTy = CurTy->subtypes()[0];
4541       }
4542 
4543       I = ExtractValueInst::Create(Agg, EXTRACTVALIdx);
4544       InstructionList.push_back(I);
4545       break;
4546     }
4547 
4548     case bitc::FUNC_CODE_INST_INSERTVAL: {
4549                            // INSERTVAL: [opty, opval, opty, opval, n x indices]
4550       unsigned OpNum = 0;
4551       Value *Agg;
4552       if (getValueTypePair(Record, OpNum, NextValueNo, Agg))
4553         return error("Invalid record");
4554       Value *Val;
4555       if (getValueTypePair(Record, OpNum, NextValueNo, Val))
4556         return error("Invalid record");
4557 
4558       unsigned RecSize = Record.size();
4559       if (OpNum == RecSize)
4560         return error("INSERTVAL: Invalid instruction with 0 indices");
4561 
4562       SmallVector<unsigned, 4> INSERTVALIdx;
4563       Type *CurTy = Agg->getType();
4564       for (; OpNum != RecSize; ++OpNum) {
4565         bool IsArray = CurTy->isArrayTy();
4566         bool IsStruct = CurTy->isStructTy();
4567         uint64_t Index = Record[OpNum];
4568 
4569         if (!IsStruct && !IsArray)
4570           return error("INSERTVAL: Invalid type");
4571         if ((unsigned)Index != Index)
4572           return error("Invalid value");
4573         if (IsStruct && Index >= CurTy->subtypes().size())
4574           return error("INSERTVAL: Invalid struct index");
4575         if (IsArray && Index >= CurTy->getArrayNumElements())
4576           return error("INSERTVAL: Invalid array index");
4577 
4578         INSERTVALIdx.push_back((unsigned)Index);
4579         if (IsStruct)
4580           CurTy = CurTy->subtypes()[Index];
4581         else
4582           CurTy = CurTy->subtypes()[0];
4583       }
4584 
4585       if (CurTy != Val->getType())
4586         return error("Inserted value type doesn't match aggregate type");
4587 
4588       I = InsertValueInst::Create(Agg, Val, INSERTVALIdx);
4589       InstructionList.push_back(I);
4590       break;
4591     }
4592 
4593     case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval]
4594       // obsolete form of select
4595       // handles select i1 ... in old bitcode
4596       unsigned OpNum = 0;
4597       Value *TrueVal, *FalseVal, *Cond;
4598       if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) ||
4599           popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) ||
4600           popValue(Record, OpNum, NextValueNo, Type::getInt1Ty(Context), Cond))
4601         return error("Invalid record");
4602 
4603       I = SelectInst::Create(Cond, TrueVal, FalseVal);
4604       InstructionList.push_back(I);
4605       break;
4606     }
4607 
4608     case bitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [ty,opval,opval,predty,pred]
4609       // new form of select
4610       // handles select i1 or select [N x i1]
4611       unsigned OpNum = 0;
4612       Value *TrueVal, *FalseVal, *Cond;
4613       if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) ||
4614           popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) ||
4615           getValueTypePair(Record, OpNum, NextValueNo, Cond))
4616         return error("Invalid record");
4617 
4618       // select condition can be either i1 or [N x i1]
4619       if (VectorType* vector_type =
4620           dyn_cast<VectorType>(Cond->getType())) {
4621         // expect <n x i1>
4622         if (vector_type->getElementType() != Type::getInt1Ty(Context))
4623           return error("Invalid type for value");
4624       } else {
4625         // expect i1
4626         if (Cond->getType() != Type::getInt1Ty(Context))
4627           return error("Invalid type for value");
4628       }
4629 
4630       I = SelectInst::Create(Cond, TrueVal, FalseVal);
4631       InstructionList.push_back(I);
4632       break;
4633     }
4634 
4635     case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval]
4636       unsigned OpNum = 0;
4637       Value *Vec, *Idx;
4638       if (getValueTypePair(Record, OpNum, NextValueNo, Vec) ||
4639           getValueTypePair(Record, OpNum, NextValueNo, Idx))
4640         return error("Invalid record");
4641       if (!Vec->getType()->isVectorTy())
4642         return error("Invalid type for value");
4643       I = ExtractElementInst::Create(Vec, Idx);
4644       InstructionList.push_back(I);
4645       break;
4646     }
4647 
4648     case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval]
4649       unsigned OpNum = 0;
4650       Value *Vec, *Elt, *Idx;
4651       if (getValueTypePair(Record, OpNum, NextValueNo, Vec))
4652         return error("Invalid record");
4653       if (!Vec->getType()->isVectorTy())
4654         return error("Invalid type for value");
4655       if (popValue(Record, OpNum, NextValueNo,
4656                    cast<VectorType>(Vec->getType())->getElementType(), Elt) ||
4657           getValueTypePair(Record, OpNum, NextValueNo, Idx))
4658         return error("Invalid record");
4659       I = InsertElementInst::Create(Vec, Elt, Idx);
4660       InstructionList.push_back(I);
4661       break;
4662     }
4663 
4664     case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval]
4665       unsigned OpNum = 0;
4666       Value *Vec1, *Vec2, *Mask;
4667       if (getValueTypePair(Record, OpNum, NextValueNo, Vec1) ||
4668           popValue(Record, OpNum, NextValueNo, Vec1->getType(), Vec2))
4669         return error("Invalid record");
4670 
4671       if (getValueTypePair(Record, OpNum, NextValueNo, Mask))
4672         return error("Invalid record");
4673       if (!Vec1->getType()->isVectorTy() || !Vec2->getType()->isVectorTy())
4674         return error("Invalid type for value");
4675       I = new ShuffleVectorInst(Vec1, Vec2, Mask);
4676       InstructionList.push_back(I);
4677       break;
4678     }
4679 
4680     case bitc::FUNC_CODE_INST_CMP:   // CMP: [opty, opval, opval, pred]
4681       // Old form of ICmp/FCmp returning bool
4682       // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were
4683       // both legal on vectors but had different behaviour.
4684     case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred]
4685       // FCmp/ICmp returning bool or vector of bool
4686 
4687       unsigned OpNum = 0;
4688       Value *LHS, *RHS;
4689       if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
4690           popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS))
4691         return error("Invalid record");
4692 
4693       unsigned PredVal = Record[OpNum];
4694       bool IsFP = LHS->getType()->isFPOrFPVectorTy();
4695       FastMathFlags FMF;
4696       if (IsFP && Record.size() > OpNum+1)
4697         FMF = getDecodedFastMathFlags(Record[++OpNum]);
4698 
4699       if (OpNum+1 != Record.size())
4700         return error("Invalid record");
4701 
4702       if (LHS->getType()->isFPOrFPVectorTy())
4703         I = new FCmpInst((FCmpInst::Predicate)PredVal, LHS, RHS);
4704       else
4705         I = new ICmpInst((ICmpInst::Predicate)PredVal, LHS, RHS);
4706 
4707       if (FMF.any())
4708         I->setFastMathFlags(FMF);
4709       InstructionList.push_back(I);
4710       break;
4711     }
4712 
4713     case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>]
4714       {
4715         unsigned Size = Record.size();
4716         if (Size == 0) {
4717           I = ReturnInst::Create(Context);
4718           InstructionList.push_back(I);
4719           break;
4720         }
4721 
4722         unsigned OpNum = 0;
4723         Value *Op = nullptr;
4724         if (getValueTypePair(Record, OpNum, NextValueNo, Op))
4725           return error("Invalid record");
4726         if (OpNum != Record.size())
4727           return error("Invalid record");
4728 
4729         I = ReturnInst::Create(Context, Op);
4730         InstructionList.push_back(I);
4731         break;
4732       }
4733     case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#]
4734       if (Record.size() != 1 && Record.size() != 3)
4735         return error("Invalid record");
4736       BasicBlock *TrueDest = getBasicBlock(Record[0]);
4737       if (!TrueDest)
4738         return error("Invalid record");
4739 
4740       if (Record.size() == 1) {
4741         I = BranchInst::Create(TrueDest);
4742         InstructionList.push_back(I);
4743       }
4744       else {
4745         BasicBlock *FalseDest = getBasicBlock(Record[1]);
4746         Value *Cond = getValue(Record, 2, NextValueNo,
4747                                Type::getInt1Ty(Context));
4748         if (!FalseDest || !Cond)
4749           return error("Invalid record");
4750         I = BranchInst::Create(TrueDest, FalseDest, Cond);
4751         InstructionList.push_back(I);
4752       }
4753       break;
4754     }
4755     case bitc::FUNC_CODE_INST_CLEANUPRET: { // CLEANUPRET: [val] or [val,bb#]
4756       if (Record.size() != 1 && Record.size() != 2)
4757         return error("Invalid record");
4758       unsigned Idx = 0;
4759       Value *CleanupPad =
4760           getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context));
4761       if (!CleanupPad)
4762         return error("Invalid record");
4763       BasicBlock *UnwindDest = nullptr;
4764       if (Record.size() == 2) {
4765         UnwindDest = getBasicBlock(Record[Idx++]);
4766         if (!UnwindDest)
4767           return error("Invalid record");
4768       }
4769 
4770       I = CleanupReturnInst::Create(CleanupPad, UnwindDest);
4771       InstructionList.push_back(I);
4772       break;
4773     }
4774     case bitc::FUNC_CODE_INST_CATCHRET: { // CATCHRET: [val,bb#]
4775       if (Record.size() != 2)
4776         return error("Invalid record");
4777       unsigned Idx = 0;
4778       Value *CatchPad =
4779           getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context));
4780       if (!CatchPad)
4781         return error("Invalid record");
4782       BasicBlock *BB = getBasicBlock(Record[Idx++]);
4783       if (!BB)
4784         return error("Invalid record");
4785 
4786       I = CatchReturnInst::Create(CatchPad, BB);
4787       InstructionList.push_back(I);
4788       break;
4789     }
4790     case bitc::FUNC_CODE_INST_CATCHSWITCH: { // CATCHSWITCH: [tok,num,(bb)*,bb?]
4791       // We must have, at minimum, the outer scope and the number of arguments.
4792       if (Record.size() < 2)
4793         return error("Invalid record");
4794 
4795       unsigned Idx = 0;
4796 
4797       Value *ParentPad =
4798           getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context));
4799 
4800       unsigned NumHandlers = Record[Idx++];
4801 
4802       SmallVector<BasicBlock *, 2> Handlers;
4803       for (unsigned Op = 0; Op != NumHandlers; ++Op) {
4804         BasicBlock *BB = getBasicBlock(Record[Idx++]);
4805         if (!BB)
4806           return error("Invalid record");
4807         Handlers.push_back(BB);
4808       }
4809 
4810       BasicBlock *UnwindDest = nullptr;
4811       if (Idx + 1 == Record.size()) {
4812         UnwindDest = getBasicBlock(Record[Idx++]);
4813         if (!UnwindDest)
4814           return error("Invalid record");
4815       }
4816 
4817       if (Record.size() != Idx)
4818         return error("Invalid record");
4819 
4820       auto *CatchSwitch =
4821           CatchSwitchInst::Create(ParentPad, UnwindDest, NumHandlers);
4822       for (BasicBlock *Handler : Handlers)
4823         CatchSwitch->addHandler(Handler);
4824       I = CatchSwitch;
4825       InstructionList.push_back(I);
4826       break;
4827     }
4828     case bitc::FUNC_CODE_INST_CATCHPAD:
4829     case bitc::FUNC_CODE_INST_CLEANUPPAD: { // [tok,num,(ty,val)*]
4830       // We must have, at minimum, the outer scope and the number of arguments.
4831       if (Record.size() < 2)
4832         return error("Invalid record");
4833 
4834       unsigned Idx = 0;
4835 
4836       Value *ParentPad =
4837           getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context));
4838 
4839       unsigned NumArgOperands = Record[Idx++];
4840 
4841       SmallVector<Value *, 2> Args;
4842       for (unsigned Op = 0; Op != NumArgOperands; ++Op) {
4843         Value *Val;
4844         if (getValueTypePair(Record, Idx, NextValueNo, Val))
4845           return error("Invalid record");
4846         Args.push_back(Val);
4847       }
4848 
4849       if (Record.size() != Idx)
4850         return error("Invalid record");
4851 
4852       if (BitCode == bitc::FUNC_CODE_INST_CLEANUPPAD)
4853         I = CleanupPadInst::Create(ParentPad, Args);
4854       else
4855         I = CatchPadInst::Create(ParentPad, Args);
4856       InstructionList.push_back(I);
4857       break;
4858     }
4859     case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...]
4860       // Check magic
4861       if ((Record[0] >> 16) == SWITCH_INST_MAGIC) {
4862         // "New" SwitchInst format with case ranges. The changes to write this
4863         // format were reverted but we still recognize bitcode that uses it.
4864         // Hopefully someday we will have support for case ranges and can use
4865         // this format again.
4866 
4867         Type *OpTy = getTypeByID(Record[1]);
4868         unsigned ValueBitWidth = cast<IntegerType>(OpTy)->getBitWidth();
4869 
4870         Value *Cond = getValue(Record, 2, NextValueNo, OpTy);
4871         BasicBlock *Default = getBasicBlock(Record[3]);
4872         if (!OpTy || !Cond || !Default)
4873           return error("Invalid record");
4874 
4875         unsigned NumCases = Record[4];
4876 
4877         SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
4878         InstructionList.push_back(SI);
4879 
4880         unsigned CurIdx = 5;
4881         for (unsigned i = 0; i != NumCases; ++i) {
4882           SmallVector<ConstantInt*, 1> CaseVals;
4883           unsigned NumItems = Record[CurIdx++];
4884           for (unsigned ci = 0; ci != NumItems; ++ci) {
4885             bool isSingleNumber = Record[CurIdx++];
4886 
4887             APInt Low;
4888             unsigned ActiveWords = 1;
4889             if (ValueBitWidth > 64)
4890               ActiveWords = Record[CurIdx++];
4891             Low = readWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords),
4892                                 ValueBitWidth);
4893             CurIdx += ActiveWords;
4894 
4895             if (!isSingleNumber) {
4896               ActiveWords = 1;
4897               if (ValueBitWidth > 64)
4898                 ActiveWords = Record[CurIdx++];
4899               APInt High = readWideAPInt(
4900                   makeArrayRef(&Record[CurIdx], ActiveWords), ValueBitWidth);
4901               CurIdx += ActiveWords;
4902 
4903               // FIXME: It is not clear whether values in the range should be
4904               // compared as signed or unsigned values. The partially
4905               // implemented changes that used this format in the past used
4906               // unsigned comparisons.
4907               for ( ; Low.ule(High); ++Low)
4908                 CaseVals.push_back(ConstantInt::get(Context, Low));
4909             } else
4910               CaseVals.push_back(ConstantInt::get(Context, Low));
4911           }
4912           BasicBlock *DestBB = getBasicBlock(Record[CurIdx++]);
4913           for (SmallVector<ConstantInt*, 1>::iterator cvi = CaseVals.begin(),
4914                  cve = CaseVals.end(); cvi != cve; ++cvi)
4915             SI->addCase(*cvi, DestBB);
4916         }
4917         I = SI;
4918         break;
4919       }
4920 
4921       // Old SwitchInst format without case ranges.
4922 
4923       if (Record.size() < 3 || (Record.size() & 1) == 0)
4924         return error("Invalid record");
4925       Type *OpTy = getTypeByID(Record[0]);
4926       Value *Cond = getValue(Record, 1, NextValueNo, OpTy);
4927       BasicBlock *Default = getBasicBlock(Record[2]);
4928       if (!OpTy || !Cond || !Default)
4929         return error("Invalid record");
4930       unsigned NumCases = (Record.size()-3)/2;
4931       SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
4932       InstructionList.push_back(SI);
4933       for (unsigned i = 0, e = NumCases; i != e; ++i) {
4934         ConstantInt *CaseVal =
4935           dyn_cast_or_null<ConstantInt>(getFnValueByID(Record[3+i*2], OpTy));
4936         BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]);
4937         if (!CaseVal || !DestBB) {
4938           delete SI;
4939           return error("Invalid record");
4940         }
4941         SI->addCase(CaseVal, DestBB);
4942       }
4943       I = SI;
4944       break;
4945     }
4946     case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...]
4947       if (Record.size() < 2)
4948         return error("Invalid record");
4949       Type *OpTy = getTypeByID(Record[0]);
4950       Value *Address = getValue(Record, 1, NextValueNo, OpTy);
4951       if (!OpTy || !Address)
4952         return error("Invalid record");
4953       unsigned NumDests = Record.size()-2;
4954       IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests);
4955       InstructionList.push_back(IBI);
4956       for (unsigned i = 0, e = NumDests; i != e; ++i) {
4957         if (BasicBlock *DestBB = getBasicBlock(Record[2+i])) {
4958           IBI->addDestination(DestBB);
4959         } else {
4960           delete IBI;
4961           return error("Invalid record");
4962         }
4963       }
4964       I = IBI;
4965       break;
4966     }
4967 
4968     case bitc::FUNC_CODE_INST_INVOKE: {
4969       // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...]
4970       if (Record.size() < 4)
4971         return error("Invalid record");
4972       unsigned OpNum = 0;
4973       AttributeSet PAL = getAttributes(Record[OpNum++]);
4974       unsigned CCInfo = Record[OpNum++];
4975       BasicBlock *NormalBB = getBasicBlock(Record[OpNum++]);
4976       BasicBlock *UnwindBB = getBasicBlock(Record[OpNum++]);
4977 
4978       FunctionType *FTy = nullptr;
4979       if (CCInfo >> 13 & 1 &&
4980           !(FTy = dyn_cast<FunctionType>(getTypeByID(Record[OpNum++]))))
4981         return error("Explicit invoke type is not a function type");
4982 
4983       Value *Callee;
4984       if (getValueTypePair(Record, OpNum, NextValueNo, Callee))
4985         return error("Invalid record");
4986 
4987       PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType());
4988       if (!CalleeTy)
4989         return error("Callee is not a pointer");
4990       if (!FTy) {
4991         FTy = dyn_cast<FunctionType>(CalleeTy->getElementType());
4992         if (!FTy)
4993           return error("Callee is not of pointer to function type");
4994       } else if (CalleeTy->getElementType() != FTy)
4995         return error("Explicit invoke type does not match pointee type of "
4996                      "callee operand");
4997       if (Record.size() < FTy->getNumParams() + OpNum)
4998         return error("Insufficient operands to call");
4999 
5000       SmallVector<Value*, 16> Ops;
5001       for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
5002         Ops.push_back(getValue(Record, OpNum, NextValueNo,
5003                                FTy->getParamType(i)));
5004         if (!Ops.back())
5005           return error("Invalid record");
5006       }
5007 
5008       if (!FTy->isVarArg()) {
5009         if (Record.size() != OpNum)
5010           return error("Invalid record");
5011       } else {
5012         // Read type/value pairs for varargs params.
5013         while (OpNum != Record.size()) {
5014           Value *Op;
5015           if (getValueTypePair(Record, OpNum, NextValueNo, Op))
5016             return error("Invalid record");
5017           Ops.push_back(Op);
5018         }
5019       }
5020 
5021       I = InvokeInst::Create(Callee, NormalBB, UnwindBB, Ops, OperandBundles);
5022       OperandBundles.clear();
5023       InstructionList.push_back(I);
5024       cast<InvokeInst>(I)->setCallingConv(
5025           static_cast<CallingConv::ID>(CallingConv::MaxID & CCInfo));
5026       cast<InvokeInst>(I)->setAttributes(PAL);
5027       break;
5028     }
5029     case bitc::FUNC_CODE_INST_RESUME: { // RESUME: [opval]
5030       unsigned Idx = 0;
5031       Value *Val = nullptr;
5032       if (getValueTypePair(Record, Idx, NextValueNo, Val))
5033         return error("Invalid record");
5034       I = ResumeInst::Create(Val);
5035       InstructionList.push_back(I);
5036       break;
5037     }
5038     case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE
5039       I = new UnreachableInst(Context);
5040       InstructionList.push_back(I);
5041       break;
5042     case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...]
5043       if (Record.size() < 1 || ((Record.size()-1)&1))
5044         return error("Invalid record");
5045       Type *Ty = getTypeByID(Record[0]);
5046       if (!Ty)
5047         return error("Invalid record");
5048 
5049       PHINode *PN = PHINode::Create(Ty, (Record.size()-1)/2);
5050       InstructionList.push_back(PN);
5051 
5052       for (unsigned i = 0, e = Record.size()-1; i != e; i += 2) {
5053         Value *V;
5054         // With the new function encoding, it is possible that operands have
5055         // negative IDs (for forward references).  Use a signed VBR
5056         // representation to keep the encoding small.
5057         if (UseRelativeIDs)
5058           V = getValueSigned(Record, 1+i, NextValueNo, Ty);
5059         else
5060           V = getValue(Record, 1+i, NextValueNo, Ty);
5061         BasicBlock *BB = getBasicBlock(Record[2+i]);
5062         if (!V || !BB)
5063           return error("Invalid record");
5064         PN->addIncoming(V, BB);
5065       }
5066       I = PN;
5067       break;
5068     }
5069 
5070     case bitc::FUNC_CODE_INST_LANDINGPAD:
5071     case bitc::FUNC_CODE_INST_LANDINGPAD_OLD: {
5072       // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?]
5073       unsigned Idx = 0;
5074       if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD) {
5075         if (Record.size() < 3)
5076           return error("Invalid record");
5077       } else {
5078         assert(BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD);
5079         if (Record.size() < 4)
5080           return error("Invalid record");
5081       }
5082       Type *Ty = getTypeByID(Record[Idx++]);
5083       if (!Ty)
5084         return error("Invalid record");
5085       if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD) {
5086         Value *PersFn = nullptr;
5087         if (getValueTypePair(Record, Idx, NextValueNo, PersFn))
5088           return error("Invalid record");
5089 
5090         if (!F->hasPersonalityFn())
5091           F->setPersonalityFn(cast<Constant>(PersFn));
5092         else if (F->getPersonalityFn() != cast<Constant>(PersFn))
5093           return error("Personality function mismatch");
5094       }
5095 
5096       bool IsCleanup = !!Record[Idx++];
5097       unsigned NumClauses = Record[Idx++];
5098       LandingPadInst *LP = LandingPadInst::Create(Ty, NumClauses);
5099       LP->setCleanup(IsCleanup);
5100       for (unsigned J = 0; J != NumClauses; ++J) {
5101         LandingPadInst::ClauseType CT =
5102           LandingPadInst::ClauseType(Record[Idx++]); (void)CT;
5103         Value *Val;
5104 
5105         if (getValueTypePair(Record, Idx, NextValueNo, Val)) {
5106           delete LP;
5107           return error("Invalid record");
5108         }
5109 
5110         assert((CT != LandingPadInst::Catch ||
5111                 !isa<ArrayType>(Val->getType())) &&
5112                "Catch clause has a invalid type!");
5113         assert((CT != LandingPadInst::Filter ||
5114                 isa<ArrayType>(Val->getType())) &&
5115                "Filter clause has invalid type!");
5116         LP->addClause(cast<Constant>(Val));
5117       }
5118 
5119       I = LP;
5120       InstructionList.push_back(I);
5121       break;
5122     }
5123 
5124     case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align]
5125       if (Record.size() != 4)
5126         return error("Invalid record");
5127       uint64_t AlignRecord = Record[3];
5128       const uint64_t InAllocaMask = uint64_t(1) << 5;
5129       const uint64_t ExplicitTypeMask = uint64_t(1) << 6;
5130       const uint64_t SwiftErrorMask = uint64_t(1) << 7;
5131       const uint64_t FlagMask = InAllocaMask | ExplicitTypeMask |
5132                                 SwiftErrorMask;
5133       bool InAlloca = AlignRecord & InAllocaMask;
5134       bool SwiftError = AlignRecord & SwiftErrorMask;
5135       Type *Ty = getTypeByID(Record[0]);
5136       if ((AlignRecord & ExplicitTypeMask) == 0) {
5137         auto *PTy = dyn_cast_or_null<PointerType>(Ty);
5138         if (!PTy)
5139           return error("Old-style alloca with a non-pointer type");
5140         Ty = PTy->getElementType();
5141       }
5142       Type *OpTy = getTypeByID(Record[1]);
5143       Value *Size = getFnValueByID(Record[2], OpTy);
5144       unsigned Align;
5145       if (std::error_code EC =
5146               parseAlignmentValue(AlignRecord & ~FlagMask, Align)) {
5147         return EC;
5148       }
5149       if (!Ty || !Size)
5150         return error("Invalid record");
5151       AllocaInst *AI = new AllocaInst(Ty, Size, Align);
5152       AI->setUsedWithInAlloca(InAlloca);
5153       AI->setSwiftError(SwiftError);
5154       I = AI;
5155       InstructionList.push_back(I);
5156       break;
5157     }
5158     case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol]
5159       unsigned OpNum = 0;
5160       Value *Op;
5161       if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
5162           (OpNum + 2 != Record.size() && OpNum + 3 != Record.size()))
5163         return error("Invalid record");
5164 
5165       Type *Ty = nullptr;
5166       if (OpNum + 3 == Record.size())
5167         Ty = getTypeByID(Record[OpNum++]);
5168       if (std::error_code EC = typeCheckLoadStoreInst(Ty, Op->getType()))
5169         return EC;
5170       if (!Ty)
5171         Ty = cast<PointerType>(Op->getType())->getElementType();
5172 
5173       unsigned Align;
5174       if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align))
5175         return EC;
5176       I = new LoadInst(Ty, Op, "", Record[OpNum + 1], Align);
5177 
5178       InstructionList.push_back(I);
5179       break;
5180     }
5181     case bitc::FUNC_CODE_INST_LOADATOMIC: {
5182        // LOADATOMIC: [opty, op, align, vol, ordering, synchscope]
5183       unsigned OpNum = 0;
5184       Value *Op;
5185       if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
5186           (OpNum + 4 != Record.size() && OpNum + 5 != Record.size()))
5187         return error("Invalid record");
5188 
5189       Type *Ty = nullptr;
5190       if (OpNum + 5 == Record.size())
5191         Ty = getTypeByID(Record[OpNum++]);
5192       if (std::error_code EC = typeCheckLoadStoreInst(Ty, Op->getType()))
5193         return EC;
5194       if (!Ty)
5195         Ty = cast<PointerType>(Op->getType())->getElementType();
5196 
5197       AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]);
5198       if (Ordering == AtomicOrdering::NotAtomic ||
5199           Ordering == AtomicOrdering::Release ||
5200           Ordering == AtomicOrdering::AcquireRelease)
5201         return error("Invalid record");
5202       if (Ordering != AtomicOrdering::NotAtomic && Record[OpNum] == 0)
5203         return error("Invalid record");
5204       SynchronizationScope SynchScope = getDecodedSynchScope(Record[OpNum + 3]);
5205 
5206       unsigned Align;
5207       if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align))
5208         return EC;
5209       I = new LoadInst(Op, "", Record[OpNum+1], Align, Ordering, SynchScope);
5210 
5211       InstructionList.push_back(I);
5212       break;
5213     }
5214     case bitc::FUNC_CODE_INST_STORE:
5215     case bitc::FUNC_CODE_INST_STORE_OLD: { // STORE2:[ptrty, ptr, val, align, vol]
5216       unsigned OpNum = 0;
5217       Value *Val, *Ptr;
5218       if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
5219           (BitCode == bitc::FUNC_CODE_INST_STORE
5220                ? getValueTypePair(Record, OpNum, NextValueNo, Val)
5221                : popValue(Record, OpNum, NextValueNo,
5222                           cast<PointerType>(Ptr->getType())->getElementType(),
5223                           Val)) ||
5224           OpNum + 2 != Record.size())
5225         return error("Invalid record");
5226 
5227       if (std::error_code EC =
5228               typeCheckLoadStoreInst(Val->getType(), Ptr->getType()))
5229         return EC;
5230       unsigned Align;
5231       if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align))
5232         return EC;
5233       I = new StoreInst(Val, Ptr, Record[OpNum+1], Align);
5234       InstructionList.push_back(I);
5235       break;
5236     }
5237     case bitc::FUNC_CODE_INST_STOREATOMIC:
5238     case bitc::FUNC_CODE_INST_STOREATOMIC_OLD: {
5239       // STOREATOMIC: [ptrty, ptr, val, align, vol, ordering, synchscope]
5240       unsigned OpNum = 0;
5241       Value *Val, *Ptr;
5242       if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
5243           (BitCode == bitc::FUNC_CODE_INST_STOREATOMIC
5244                ? getValueTypePair(Record, OpNum, NextValueNo, Val)
5245                : popValue(Record, OpNum, NextValueNo,
5246                           cast<PointerType>(Ptr->getType())->getElementType(),
5247                           Val)) ||
5248           OpNum + 4 != Record.size())
5249         return error("Invalid record");
5250 
5251       if (std::error_code EC =
5252               typeCheckLoadStoreInst(Val->getType(), Ptr->getType()))
5253         return EC;
5254       AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]);
5255       if (Ordering == AtomicOrdering::NotAtomic ||
5256           Ordering == AtomicOrdering::Acquire ||
5257           Ordering == AtomicOrdering::AcquireRelease)
5258         return error("Invalid record");
5259       SynchronizationScope SynchScope = getDecodedSynchScope(Record[OpNum + 3]);
5260       if (Ordering != AtomicOrdering::NotAtomic && Record[OpNum] == 0)
5261         return error("Invalid record");
5262 
5263       unsigned Align;
5264       if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align))
5265         return EC;
5266       I = new StoreInst(Val, Ptr, Record[OpNum+1], Align, Ordering, SynchScope);
5267       InstructionList.push_back(I);
5268       break;
5269     }
5270     case bitc::FUNC_CODE_INST_CMPXCHG_OLD:
5271     case bitc::FUNC_CODE_INST_CMPXCHG: {
5272       // CMPXCHG:[ptrty, ptr, cmp, new, vol, successordering, synchscope,
5273       //          failureordering?, isweak?]
5274       unsigned OpNum = 0;
5275       Value *Ptr, *Cmp, *New;
5276       if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
5277           (BitCode == bitc::FUNC_CODE_INST_CMPXCHG
5278                ? getValueTypePair(Record, OpNum, NextValueNo, Cmp)
5279                : popValue(Record, OpNum, NextValueNo,
5280                           cast<PointerType>(Ptr->getType())->getElementType(),
5281                           Cmp)) ||
5282           popValue(Record, OpNum, NextValueNo, Cmp->getType(), New) ||
5283           Record.size() < OpNum + 3 || Record.size() > OpNum + 5)
5284         return error("Invalid record");
5285       AtomicOrdering SuccessOrdering = getDecodedOrdering(Record[OpNum + 1]);
5286       if (SuccessOrdering == AtomicOrdering::NotAtomic ||
5287           SuccessOrdering == AtomicOrdering::Unordered)
5288         return error("Invalid record");
5289       SynchronizationScope SynchScope = getDecodedSynchScope(Record[OpNum + 2]);
5290 
5291       if (std::error_code EC =
5292               typeCheckLoadStoreInst(Cmp->getType(), Ptr->getType()))
5293         return EC;
5294       AtomicOrdering FailureOrdering;
5295       if (Record.size() < 7)
5296         FailureOrdering =
5297             AtomicCmpXchgInst::getStrongestFailureOrdering(SuccessOrdering);
5298       else
5299         FailureOrdering = getDecodedOrdering(Record[OpNum + 3]);
5300 
5301       I = new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering, FailureOrdering,
5302                                 SynchScope);
5303       cast<AtomicCmpXchgInst>(I)->setVolatile(Record[OpNum]);
5304 
5305       if (Record.size() < 8) {
5306         // Before weak cmpxchgs existed, the instruction simply returned the
5307         // value loaded from memory, so bitcode files from that era will be
5308         // expecting the first component of a modern cmpxchg.
5309         CurBB->getInstList().push_back(I);
5310         I = ExtractValueInst::Create(I, 0);
5311       } else {
5312         cast<AtomicCmpXchgInst>(I)->setWeak(Record[OpNum+4]);
5313       }
5314 
5315       InstructionList.push_back(I);
5316       break;
5317     }
5318     case bitc::FUNC_CODE_INST_ATOMICRMW: {
5319       // ATOMICRMW:[ptrty, ptr, val, op, vol, ordering, synchscope]
5320       unsigned OpNum = 0;
5321       Value *Ptr, *Val;
5322       if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
5323           popValue(Record, OpNum, NextValueNo,
5324                     cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
5325           OpNum+4 != Record.size())
5326         return error("Invalid record");
5327       AtomicRMWInst::BinOp Operation = getDecodedRMWOperation(Record[OpNum]);
5328       if (Operation < AtomicRMWInst::FIRST_BINOP ||
5329           Operation > AtomicRMWInst::LAST_BINOP)
5330         return error("Invalid record");
5331       AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]);
5332       if (Ordering == AtomicOrdering::NotAtomic ||
5333           Ordering == AtomicOrdering::Unordered)
5334         return error("Invalid record");
5335       SynchronizationScope SynchScope = getDecodedSynchScope(Record[OpNum + 3]);
5336       I = new AtomicRMWInst(Operation, Ptr, Val, Ordering, SynchScope);
5337       cast<AtomicRMWInst>(I)->setVolatile(Record[OpNum+1]);
5338       InstructionList.push_back(I);
5339       break;
5340     }
5341     case bitc::FUNC_CODE_INST_FENCE: { // FENCE:[ordering, synchscope]
5342       if (2 != Record.size())
5343         return error("Invalid record");
5344       AtomicOrdering Ordering = getDecodedOrdering(Record[0]);
5345       if (Ordering == AtomicOrdering::NotAtomic ||
5346           Ordering == AtomicOrdering::Unordered ||
5347           Ordering == AtomicOrdering::Monotonic)
5348         return error("Invalid record");
5349       SynchronizationScope SynchScope = getDecodedSynchScope(Record[1]);
5350       I = new FenceInst(Context, Ordering, SynchScope);
5351       InstructionList.push_back(I);
5352       break;
5353     }
5354     case bitc::FUNC_CODE_INST_CALL: {
5355       // CALL: [paramattrs, cc, fmf, fnty, fnid, arg0, arg1...]
5356       if (Record.size() < 3)
5357         return error("Invalid record");
5358 
5359       unsigned OpNum = 0;
5360       AttributeSet PAL = getAttributes(Record[OpNum++]);
5361       unsigned CCInfo = Record[OpNum++];
5362 
5363       FastMathFlags FMF;
5364       if ((CCInfo >> bitc::CALL_FMF) & 1) {
5365         FMF = getDecodedFastMathFlags(Record[OpNum++]);
5366         if (!FMF.any())
5367           return error("Fast math flags indicator set for call with no FMF");
5368       }
5369 
5370       FunctionType *FTy = nullptr;
5371       if (CCInfo >> bitc::CALL_EXPLICIT_TYPE & 1 &&
5372           !(FTy = dyn_cast<FunctionType>(getTypeByID(Record[OpNum++]))))
5373         return error("Explicit call type is not a function type");
5374 
5375       Value *Callee;
5376       if (getValueTypePair(Record, OpNum, NextValueNo, Callee))
5377         return error("Invalid record");
5378 
5379       PointerType *OpTy = dyn_cast<PointerType>(Callee->getType());
5380       if (!OpTy)
5381         return error("Callee is not a pointer type");
5382       if (!FTy) {
5383         FTy = dyn_cast<FunctionType>(OpTy->getElementType());
5384         if (!FTy)
5385           return error("Callee is not of pointer to function type");
5386       } else if (OpTy->getElementType() != FTy)
5387         return error("Explicit call type does not match pointee type of "
5388                      "callee operand");
5389       if (Record.size() < FTy->getNumParams() + OpNum)
5390         return error("Insufficient operands to call");
5391 
5392       SmallVector<Value*, 16> Args;
5393       // Read the fixed params.
5394       for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
5395         if (FTy->getParamType(i)->isLabelTy())
5396           Args.push_back(getBasicBlock(Record[OpNum]));
5397         else
5398           Args.push_back(getValue(Record, OpNum, NextValueNo,
5399                                   FTy->getParamType(i)));
5400         if (!Args.back())
5401           return error("Invalid record");
5402       }
5403 
5404       // Read type/value pairs for varargs params.
5405       if (!FTy->isVarArg()) {
5406         if (OpNum != Record.size())
5407           return error("Invalid record");
5408       } else {
5409         while (OpNum != Record.size()) {
5410           Value *Op;
5411           if (getValueTypePair(Record, OpNum, NextValueNo, Op))
5412             return error("Invalid record");
5413           Args.push_back(Op);
5414         }
5415       }
5416 
5417       I = CallInst::Create(FTy, Callee, Args, OperandBundles);
5418       OperandBundles.clear();
5419       InstructionList.push_back(I);
5420       cast<CallInst>(I)->setCallingConv(
5421           static_cast<CallingConv::ID>((0x7ff & CCInfo) >> bitc::CALL_CCONV));
5422       CallInst::TailCallKind TCK = CallInst::TCK_None;
5423       if (CCInfo & 1 << bitc::CALL_TAIL)
5424         TCK = CallInst::TCK_Tail;
5425       if (CCInfo & (1 << bitc::CALL_MUSTTAIL))
5426         TCK = CallInst::TCK_MustTail;
5427       if (CCInfo & (1 << bitc::CALL_NOTAIL))
5428         TCK = CallInst::TCK_NoTail;
5429       cast<CallInst>(I)->setTailCallKind(TCK);
5430       cast<CallInst>(I)->setAttributes(PAL);
5431       if (FMF.any()) {
5432         if (!isa<FPMathOperator>(I))
5433           return error("Fast-math-flags specified for call without "
5434                        "floating-point scalar or vector return type");
5435         I->setFastMathFlags(FMF);
5436       }
5437       break;
5438     }
5439     case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty]
5440       if (Record.size() < 3)
5441         return error("Invalid record");
5442       Type *OpTy = getTypeByID(Record[0]);
5443       Value *Op = getValue(Record, 1, NextValueNo, OpTy);
5444       Type *ResTy = getTypeByID(Record[2]);
5445       if (!OpTy || !Op || !ResTy)
5446         return error("Invalid record");
5447       I = new VAArgInst(Op, ResTy);
5448       InstructionList.push_back(I);
5449       break;
5450     }
5451 
5452     case bitc::FUNC_CODE_OPERAND_BUNDLE: {
5453       // A call or an invoke can be optionally prefixed with some variable
5454       // number of operand bundle blocks.  These blocks are read into
5455       // OperandBundles and consumed at the next call or invoke instruction.
5456 
5457       if (Record.size() < 1 || Record[0] >= BundleTags.size())
5458         return error("Invalid record");
5459 
5460       std::vector<Value *> Inputs;
5461 
5462       unsigned OpNum = 1;
5463       while (OpNum != Record.size()) {
5464         Value *Op;
5465         if (getValueTypePair(Record, OpNum, NextValueNo, Op))
5466           return error("Invalid record");
5467         Inputs.push_back(Op);
5468       }
5469 
5470       OperandBundles.emplace_back(BundleTags[Record[0]], std::move(Inputs));
5471       continue;
5472     }
5473     }
5474 
5475     // Add instruction to end of current BB.  If there is no current BB, reject
5476     // this file.
5477     if (!CurBB) {
5478       delete I;
5479       return error("Invalid instruction with no BB");
5480     }
5481     if (!OperandBundles.empty()) {
5482       delete I;
5483       return error("Operand bundles found with no consumer");
5484     }
5485     CurBB->getInstList().push_back(I);
5486 
5487     // If this was a terminator instruction, move to the next block.
5488     if (isa<TerminatorInst>(I)) {
5489       ++CurBBNo;
5490       CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : nullptr;
5491     }
5492 
5493     // Non-void values get registered in the value table for future use.
5494     if (I && !I->getType()->isVoidTy())
5495       ValueList.assignValue(I, NextValueNo++);
5496   }
5497 
5498 OutOfRecordLoop:
5499 
5500   if (!OperandBundles.empty())
5501     return error("Operand bundles found with no consumer");
5502 
5503   // Check the function list for unresolved values.
5504   if (Argument *A = dyn_cast<Argument>(ValueList.back())) {
5505     if (!A->getParent()) {
5506       // We found at least one unresolved value.  Nuke them all to avoid leaks.
5507       for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){
5508         if ((A = dyn_cast_or_null<Argument>(ValueList[i])) && !A->getParent()) {
5509           A->replaceAllUsesWith(UndefValue::get(A->getType()));
5510           delete A;
5511         }
5512       }
5513       return error("Never resolved value found in function");
5514     }
5515   }
5516 
5517   // Unexpected unresolved metadata about to be dropped.
5518   if (MetadataList.hasFwdRefs())
5519     return error("Invalid function metadata: outgoing forward refs");
5520 
5521   // Trim the value list down to the size it was before we parsed this function.
5522   ValueList.shrinkTo(ModuleValueListSize);
5523   MetadataList.shrinkTo(ModuleMetadataListSize);
5524   std::vector<BasicBlock*>().swap(FunctionBBs);
5525   return std::error_code();
5526 }
5527 
5528 /// Find the function body in the bitcode stream
5529 std::error_code BitcodeReader::findFunctionInStream(
5530     Function *F,
5531     DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator) {
5532   while (DeferredFunctionInfoIterator->second == 0) {
5533     // This is the fallback handling for the old format bitcode that
5534     // didn't contain the function index in the VST, or when we have
5535     // an anonymous function which would not have a VST entry.
5536     // Assert that we have one of those two cases.
5537     assert(VSTOffset == 0 || !F->hasName());
5538     // Parse the next body in the stream and set its position in the
5539     // DeferredFunctionInfo map.
5540     if (std::error_code EC = rememberAndSkipFunctionBodies())
5541       return EC;
5542   }
5543   return std::error_code();
5544 }
5545 
5546 //===----------------------------------------------------------------------===//
5547 // GVMaterializer implementation
5548 //===----------------------------------------------------------------------===//
5549 
5550 void BitcodeReader::releaseBuffer() { Buffer.release(); }
5551 
5552 std::error_code BitcodeReader::materialize(GlobalValue *GV) {
5553   Function *F = dyn_cast<Function>(GV);
5554   // If it's not a function or is already material, ignore the request.
5555   if (!F || !F->isMaterializable())
5556     return std::error_code();
5557 
5558   DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F);
5559   assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!");
5560   // If its position is recorded as 0, its body is somewhere in the stream
5561   // but we haven't seen it yet.
5562   if (DFII->second == 0)
5563     if (std::error_code EC = findFunctionInStream(F, DFII))
5564       return EC;
5565 
5566   // Materialize metadata before parsing any function bodies.
5567   if (std::error_code EC = materializeMetadata())
5568     return EC;
5569 
5570   // Move the bit stream to the saved position of the deferred function body.
5571   Stream.JumpToBit(DFII->second);
5572 
5573   if (std::error_code EC = parseFunctionBody(F))
5574     return EC;
5575   F->setIsMaterializable(false);
5576 
5577   if (StripDebugInfo)
5578     stripDebugInfo(*F);
5579 
5580   // Upgrade any old intrinsic calls in the function.
5581   for (auto &I : UpgradedIntrinsics) {
5582     for (auto UI = I.first->materialized_user_begin(), UE = I.first->user_end();
5583          UI != UE;) {
5584       User *U = *UI;
5585       ++UI;
5586       if (CallInst *CI = dyn_cast<CallInst>(U))
5587         UpgradeIntrinsicCall(CI, I.second);
5588     }
5589   }
5590 
5591   // Finish fn->subprogram upgrade for materialized functions.
5592   if (DISubprogram *SP = FunctionsWithSPs.lookup(F))
5593     F->setSubprogram(SP);
5594 
5595   // Bring in any functions that this function forward-referenced via
5596   // blockaddresses.
5597   return materializeForwardReferencedFunctions();
5598 }
5599 
5600 std::error_code BitcodeReader::materializeModule() {
5601   if (std::error_code EC = materializeMetadata())
5602     return EC;
5603 
5604   // Promise to materialize all forward references.
5605   WillMaterializeAllForwardRefs = true;
5606 
5607   // Iterate over the module, deserializing any functions that are still on
5608   // disk.
5609   for (Function &F : *TheModule) {
5610     if (std::error_code EC = materialize(&F))
5611       return EC;
5612   }
5613   // At this point, if there are any function bodies, parse the rest of
5614   // the bits in the module past the last function block we have recorded
5615   // through either lazy scanning or the VST.
5616   if (LastFunctionBlockBit || NextUnreadBit)
5617     parseModule(LastFunctionBlockBit > NextUnreadBit ? LastFunctionBlockBit
5618                                                      : NextUnreadBit);
5619 
5620   // Check that all block address forward references got resolved (as we
5621   // promised above).
5622   if (!BasicBlockFwdRefs.empty())
5623     return error("Never resolved function from blockaddress");
5624 
5625   // Upgrading intrinsic calls before TBAA can cause TBAA metadata to be lost,
5626   // to prevent this instructions with TBAA tags should be upgraded first.
5627   for (unsigned I = 0, E = InstsWithTBAATag.size(); I < E; I++)
5628     UpgradeInstWithTBAATag(InstsWithTBAATag[I]);
5629 
5630   // Upgrade any intrinsic calls that slipped through (should not happen!) and
5631   // delete the old functions to clean up. We can't do this unless the entire
5632   // module is materialized because there could always be another function body
5633   // with calls to the old function.
5634   for (auto &I : UpgradedIntrinsics) {
5635     for (auto *U : I.first->users()) {
5636       if (CallInst *CI = dyn_cast<CallInst>(U))
5637         UpgradeIntrinsicCall(CI, I.second);
5638     }
5639     if (!I.first->use_empty())
5640       I.first->replaceAllUsesWith(I.second);
5641     I.first->eraseFromParent();
5642   }
5643   UpgradedIntrinsics.clear();
5644 
5645   UpgradeDebugInfo(*TheModule);
5646   return std::error_code();
5647 }
5648 
5649 std::vector<StructType *> BitcodeReader::getIdentifiedStructTypes() const {
5650   return IdentifiedStructTypes;
5651 }
5652 
5653 std::error_code
5654 BitcodeReader::initStream(std::unique_ptr<DataStreamer> Streamer) {
5655   if (Streamer)
5656     return initLazyStream(std::move(Streamer));
5657   return initStreamFromBuffer();
5658 }
5659 
5660 std::error_code BitcodeReader::initStreamFromBuffer() {
5661   const unsigned char *BufPtr = (const unsigned char*)Buffer->getBufferStart();
5662   const unsigned char *BufEnd = BufPtr+Buffer->getBufferSize();
5663 
5664   if (Buffer->getBufferSize() & 3)
5665     return error("Invalid bitcode signature");
5666 
5667   // If we have a wrapper header, parse it and ignore the non-bc file contents.
5668   // The magic number is 0x0B17C0DE stored in little endian.
5669   if (isBitcodeWrapper(BufPtr, BufEnd))
5670     if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true))
5671       return error("Invalid bitcode wrapper header");
5672 
5673   StreamFile.reset(new BitstreamReader(BufPtr, BufEnd));
5674   Stream.init(&*StreamFile);
5675 
5676   return std::error_code();
5677 }
5678 
5679 std::error_code
5680 BitcodeReader::initLazyStream(std::unique_ptr<DataStreamer> Streamer) {
5681   // Check and strip off the bitcode wrapper; BitstreamReader expects never to
5682   // see it.
5683   auto OwnedBytes =
5684       llvm::make_unique<StreamingMemoryObject>(std::move(Streamer));
5685   StreamingMemoryObject &Bytes = *OwnedBytes;
5686   StreamFile = llvm::make_unique<BitstreamReader>(std::move(OwnedBytes));
5687   Stream.init(&*StreamFile);
5688 
5689   unsigned char buf[16];
5690   if (Bytes.readBytes(buf, 16, 0) != 16)
5691     return error("Invalid bitcode signature");
5692 
5693   if (!isBitcode(buf, buf + 16))
5694     return error("Invalid bitcode signature");
5695 
5696   if (isBitcodeWrapper(buf, buf + 4)) {
5697     const unsigned char *bitcodeStart = buf;
5698     const unsigned char *bitcodeEnd = buf + 16;
5699     SkipBitcodeWrapperHeader(bitcodeStart, bitcodeEnd, false);
5700     Bytes.dropLeadingBytes(bitcodeStart - buf);
5701     Bytes.setKnownObjectSize(bitcodeEnd - bitcodeStart);
5702   }
5703   return std::error_code();
5704 }
5705 
5706 std::error_code ModuleSummaryIndexBitcodeReader::error(BitcodeError E,
5707                                                        const Twine &Message) {
5708   return ::error(DiagnosticHandler, make_error_code(E), Message);
5709 }
5710 
5711 std::error_code ModuleSummaryIndexBitcodeReader::error(const Twine &Message) {
5712   return ::error(DiagnosticHandler,
5713                  make_error_code(BitcodeError::CorruptedBitcode), Message);
5714 }
5715 
5716 std::error_code ModuleSummaryIndexBitcodeReader::error(BitcodeError E) {
5717   return ::error(DiagnosticHandler, make_error_code(E));
5718 }
5719 
5720 ModuleSummaryIndexBitcodeReader::ModuleSummaryIndexBitcodeReader(
5721     MemoryBuffer *Buffer, DiagnosticHandlerFunction DiagnosticHandler,
5722     bool CheckGlobalValSummaryPresenceOnly)
5723     : DiagnosticHandler(DiagnosticHandler), Buffer(Buffer),
5724       CheckGlobalValSummaryPresenceOnly(CheckGlobalValSummaryPresenceOnly) {}
5725 
5726 ModuleSummaryIndexBitcodeReader::ModuleSummaryIndexBitcodeReader(
5727     DiagnosticHandlerFunction DiagnosticHandler,
5728     bool CheckGlobalValSummaryPresenceOnly)
5729     : DiagnosticHandler(DiagnosticHandler), Buffer(nullptr),
5730       CheckGlobalValSummaryPresenceOnly(CheckGlobalValSummaryPresenceOnly) {}
5731 
5732 void ModuleSummaryIndexBitcodeReader::freeState() { Buffer = nullptr; }
5733 
5734 void ModuleSummaryIndexBitcodeReader::releaseBuffer() { Buffer.release(); }
5735 
5736 std::pair<GlobalValue::GUID, GlobalValue::GUID>
5737 ModuleSummaryIndexBitcodeReader::getGUIDFromValueId(unsigned ValueId) {
5738   auto VGI = ValueIdToCallGraphGUIDMap.find(ValueId);
5739   assert(VGI != ValueIdToCallGraphGUIDMap.end());
5740   return VGI->second;
5741 }
5742 
5743 GlobalValue::GUID
5744 ModuleSummaryIndexBitcodeReader::getGUIDFromOffset(uint64_t Offset) {
5745   auto I = SummaryOffsetToGUIDMap.find(Offset);
5746   assert(I != SummaryOffsetToGUIDMap.end());
5747   return I->second;
5748 }
5749 
5750 // Specialized value symbol table parser used when reading module index
5751 // blocks where we don't actually create global values. The parsed information
5752 // is saved in the bitcode reader for use when later parsing summaries.
5753 std::error_code ModuleSummaryIndexBitcodeReader::parseValueSymbolTable(
5754     uint64_t Offset,
5755     DenseMap<unsigned, GlobalValue::LinkageTypes> &ValueIdToLinkageMap) {
5756   assert(Offset > 0 && "Expected non-zero VST offset");
5757   uint64_t CurrentBit = jumpToValueSymbolTable(Offset, Stream);
5758 
5759   if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
5760     return error("Invalid record");
5761 
5762   SmallVector<uint64_t, 64> Record;
5763 
5764   // Read all the records for this value table.
5765   SmallString<128> ValueName;
5766   while (1) {
5767     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
5768 
5769     switch (Entry.Kind) {
5770     case BitstreamEntry::SubBlock: // Handled for us already.
5771     case BitstreamEntry::Error:
5772       return error("Malformed block");
5773     case BitstreamEntry::EndBlock:
5774       // Done parsing VST, jump back to wherever we came from.
5775       Stream.JumpToBit(CurrentBit);
5776       return std::error_code();
5777     case BitstreamEntry::Record:
5778       // The interesting case.
5779       break;
5780     }
5781 
5782     // Read a record.
5783     Record.clear();
5784     switch (Stream.readRecord(Entry.ID, Record)) {
5785     default: // Default behavior: ignore (e.g. VST_CODE_BBENTRY records).
5786       break;
5787     case bitc::VST_CODE_ENTRY: { // VST_CODE_ENTRY: [valueid, namechar x N]
5788       if (convertToString(Record, 1, ValueName))
5789         return error("Invalid record");
5790       unsigned ValueID = Record[0];
5791       assert(!SourceFileName.empty());
5792       auto VLI = ValueIdToLinkageMap.find(ValueID);
5793       assert(VLI != ValueIdToLinkageMap.end() &&
5794              "No linkage found for VST entry?");
5795       auto Linkage = VLI->second;
5796       std::string GlobalId =
5797           GlobalValue::getGlobalIdentifier(ValueName, Linkage, SourceFileName);
5798       auto ValueGUID = GlobalValue::getGUID(GlobalId);
5799       auto OriginalNameID = ValueGUID;
5800       if (GlobalValue::isLocalLinkage(Linkage))
5801         OriginalNameID = GlobalValue::getGUID(ValueName);
5802       if (PrintSummaryGUIDs)
5803         dbgs() << "GUID " << ValueGUID << "(" << OriginalNameID << ") is "
5804                << ValueName << "\n";
5805       ValueIdToCallGraphGUIDMap[ValueID] =
5806           std::make_pair(ValueGUID, OriginalNameID);
5807       ValueName.clear();
5808       break;
5809     }
5810     case bitc::VST_CODE_FNENTRY: {
5811       // VST_CODE_FNENTRY: [valueid, offset, namechar x N]
5812       if (convertToString(Record, 2, ValueName))
5813         return error("Invalid record");
5814       unsigned ValueID = Record[0];
5815       assert(!SourceFileName.empty());
5816       auto VLI = ValueIdToLinkageMap.find(ValueID);
5817       assert(VLI != ValueIdToLinkageMap.end() &&
5818              "No linkage found for VST entry?");
5819       auto Linkage = VLI->second;
5820       std::string FunctionGlobalId = GlobalValue::getGlobalIdentifier(
5821           ValueName, VLI->second, SourceFileName);
5822       auto FunctionGUID = GlobalValue::getGUID(FunctionGlobalId);
5823       auto OriginalNameID = FunctionGUID;
5824       if (GlobalValue::isLocalLinkage(Linkage))
5825         OriginalNameID = GlobalValue::getGUID(ValueName);
5826       if (PrintSummaryGUIDs)
5827         dbgs() << "GUID " << FunctionGUID << "(" << OriginalNameID << ") is "
5828                << ValueName << "\n";
5829       ValueIdToCallGraphGUIDMap[ValueID] =
5830           std::make_pair(FunctionGUID, OriginalNameID);
5831 
5832       ValueName.clear();
5833       break;
5834     }
5835     case bitc::VST_CODE_COMBINED_GVDEFENTRY: {
5836       // VST_CODE_COMBINED_GVDEFENTRY: [valueid, offset, guid]
5837       unsigned ValueID = Record[0];
5838       uint64_t GlobalValSummaryOffset = Record[1];
5839       GlobalValue::GUID GlobalValGUID = Record[2];
5840       SummaryOffsetToGUIDMap[GlobalValSummaryOffset] = GlobalValGUID;
5841       // The "original name", which is the second value of the pair will be
5842       // overriden later by a FS_COMBINED_ORIGINAL_NAME in the combined index.
5843       ValueIdToCallGraphGUIDMap[ValueID] =
5844           std::make_pair(GlobalValGUID, GlobalValGUID);
5845       break;
5846     }
5847     case bitc::VST_CODE_COMBINED_ENTRY: {
5848       // VST_CODE_COMBINED_ENTRY: [valueid, refguid]
5849       unsigned ValueID = Record[0];
5850       GlobalValue::GUID RefGUID = Record[1];
5851       // The "original name", which is the second value of the pair will be
5852       // overriden later by a FS_COMBINED_ORIGINAL_NAME in the combined index.
5853       ValueIdToCallGraphGUIDMap[ValueID] = std::make_pair(RefGUID, RefGUID);
5854       break;
5855     }
5856     }
5857   }
5858 }
5859 
5860 // Parse just the blocks needed for building the index out of the module.
5861 // At the end of this routine the module Index is populated with a map
5862 // from global value id to GlobalValueSummary objects.
5863 std::error_code ModuleSummaryIndexBitcodeReader::parseModule() {
5864   if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
5865     return error("Invalid record");
5866 
5867   SmallVector<uint64_t, 64> Record;
5868   DenseMap<unsigned, GlobalValue::LinkageTypes> ValueIdToLinkageMap;
5869   unsigned ValueId = 0;
5870 
5871   // Read the index for this module.
5872   while (1) {
5873     BitstreamEntry Entry = Stream.advance();
5874 
5875     switch (Entry.Kind) {
5876     case BitstreamEntry::Error:
5877       return error("Malformed block");
5878     case BitstreamEntry::EndBlock:
5879       return std::error_code();
5880 
5881     case BitstreamEntry::SubBlock:
5882       if (CheckGlobalValSummaryPresenceOnly) {
5883         if (Entry.ID == bitc::GLOBALVAL_SUMMARY_BLOCK_ID) {
5884           SeenGlobalValSummary = true;
5885           // No need to parse the rest since we found the summary.
5886           return std::error_code();
5887         }
5888         if (Stream.SkipBlock())
5889           return error("Invalid record");
5890         continue;
5891       }
5892       switch (Entry.ID) {
5893       default: // Skip unknown content.
5894         if (Stream.SkipBlock())
5895           return error("Invalid record");
5896         break;
5897       case bitc::BLOCKINFO_BLOCK_ID:
5898         // Need to parse these to get abbrev ids (e.g. for VST)
5899         if (Stream.ReadBlockInfoBlock())
5900           return error("Malformed block");
5901         break;
5902       case bitc::VALUE_SYMTAB_BLOCK_ID:
5903         // Should have been parsed earlier via VSTOffset, unless there
5904         // is no summary section.
5905         assert(((SeenValueSymbolTable && VSTOffset > 0) ||
5906                 !SeenGlobalValSummary) &&
5907                "Expected early VST parse via VSTOffset record");
5908         if (Stream.SkipBlock())
5909           return error("Invalid record");
5910         break;
5911       case bitc::GLOBALVAL_SUMMARY_BLOCK_ID:
5912         assert(VSTOffset > 0 && "Expected non-zero VST offset");
5913         assert(!SeenValueSymbolTable &&
5914                "Already read VST when parsing summary block?");
5915         if (std::error_code EC =
5916                 parseValueSymbolTable(VSTOffset, ValueIdToLinkageMap))
5917           return EC;
5918         SeenValueSymbolTable = true;
5919         SeenGlobalValSummary = true;
5920         if (std::error_code EC = parseEntireSummary())
5921           return EC;
5922         break;
5923       case bitc::MODULE_STRTAB_BLOCK_ID:
5924         if (std::error_code EC = parseModuleStringTable())
5925           return EC;
5926         break;
5927       }
5928       continue;
5929 
5930     case BitstreamEntry::Record: {
5931         Record.clear();
5932         auto BitCode = Stream.readRecord(Entry.ID, Record);
5933         switch (BitCode) {
5934         default:
5935           break; // Default behavior, ignore unknown content.
5936         /// MODULE_CODE_SOURCE_FILENAME: [namechar x N]
5937         case bitc::MODULE_CODE_SOURCE_FILENAME: {
5938           SmallString<128> ValueName;
5939           if (convertToString(Record, 0, ValueName))
5940             return error("Invalid record");
5941           SourceFileName = ValueName.c_str();
5942           break;
5943         }
5944         /// MODULE_CODE_HASH: [5*i32]
5945         case bitc::MODULE_CODE_HASH: {
5946           if (Record.size() != 5)
5947             return error("Invalid hash length " + Twine(Record.size()).str());
5948           if (!TheIndex)
5949             break;
5950           if (TheIndex->modulePaths().empty())
5951             // Does not have any summary emitted.
5952             break;
5953           if (TheIndex->modulePaths().size() != 1)
5954             return error("Don't expect multiple modules defined?");
5955           auto &Hash = TheIndex->modulePaths().begin()->second.second;
5956           int Pos = 0;
5957           for (auto &Val : Record) {
5958             assert(!(Val >> 32) && "Unexpected high bits set");
5959             Hash[Pos++] = Val;
5960           }
5961           break;
5962         }
5963         /// MODULE_CODE_VSTOFFSET: [offset]
5964         case bitc::MODULE_CODE_VSTOFFSET:
5965           if (Record.size() < 1)
5966             return error("Invalid record");
5967           VSTOffset = Record[0];
5968           break;
5969         // GLOBALVAR: [pointer type, isconst, initid,
5970         //             linkage, alignment, section, visibility, threadlocal,
5971         //             unnamed_addr, externally_initialized, dllstorageclass,
5972         //             comdat]
5973         case bitc::MODULE_CODE_GLOBALVAR: {
5974           if (Record.size() < 6)
5975             return error("Invalid record");
5976           uint64_t RawLinkage = Record[3];
5977           GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage);
5978           ValueIdToLinkageMap[ValueId++] = Linkage;
5979           break;
5980         }
5981         // FUNCTION:  [type, callingconv, isproto, linkage, paramattr,
5982         //             alignment, section, visibility, gc, unnamed_addr,
5983         //             prologuedata, dllstorageclass, comdat, prefixdata]
5984         case bitc::MODULE_CODE_FUNCTION: {
5985           if (Record.size() < 8)
5986             return error("Invalid record");
5987           uint64_t RawLinkage = Record[3];
5988           GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage);
5989           ValueIdToLinkageMap[ValueId++] = Linkage;
5990           break;
5991         }
5992         // ALIAS: [alias type, addrspace, aliasee val#, linkage, visibility,
5993         // dllstorageclass]
5994         case bitc::MODULE_CODE_ALIAS: {
5995           if (Record.size() < 6)
5996             return error("Invalid record");
5997           uint64_t RawLinkage = Record[3];
5998           GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage);
5999           ValueIdToLinkageMap[ValueId++] = Linkage;
6000           break;
6001         }
6002         }
6003       }
6004       continue;
6005     }
6006   }
6007 }
6008 
6009 // Eagerly parse the entire summary block. This populates the GlobalValueSummary
6010 // objects in the index.
6011 std::error_code ModuleSummaryIndexBitcodeReader::parseEntireSummary() {
6012   if (Stream.EnterSubBlock(bitc::GLOBALVAL_SUMMARY_BLOCK_ID))
6013     return error("Invalid record");
6014   SmallVector<uint64_t, 64> Record;
6015 
6016   // Parse version
6017   {
6018     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
6019     if (Entry.Kind != BitstreamEntry::Record)
6020       return error("Invalid Summary Block: record for version expected");
6021     if (Stream.readRecord(Entry.ID, Record) != bitc::FS_VERSION)
6022       return error("Invalid Summary Block: version expected");
6023   }
6024   const uint64_t Version = Record[0];
6025   if (Version != 1)
6026     return error("Invalid summary version " + Twine(Version) + ", 1 expected");
6027   Record.clear();
6028 
6029   // Keep around the last seen summary to be used when we see an optional
6030   // "OriginalName" attachement.
6031   GlobalValueSummary *LastSeenSummary = nullptr;
6032   bool Combined = false;
6033   // For aliases in the combined summary, we need to know which summary
6034   // corresponds to the aliasee offset saved in the alias summary. It isn't
6035   // sufficient to just map to the aliasee GUID, since in the combined summary
6036   // there may be multiple values with the same GUID.
6037   DenseMap<uint64_t, GlobalValueSummary *> OffsetToSummaryMap;
6038   while (1) {
6039     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
6040 
6041     switch (Entry.Kind) {
6042     case BitstreamEntry::SubBlock: // Handled for us already.
6043     case BitstreamEntry::Error:
6044       return error("Malformed block");
6045     case BitstreamEntry::EndBlock:
6046       // For a per-module index, remove any entries that still have empty
6047       // summaries. The VST parsing creates entries eagerly for all symbols,
6048       // but not all have associated summaries (e.g. it doesn't know how to
6049       // distinguish between VST_CODE_ENTRY for function declarations vs global
6050       // variables with initializers that end up with a summary). Remove those
6051       // entries now so that we don't need to rely on the combined index merger
6052       // to clean them up (especially since that may not run for the first
6053       // module's index if we merge into that).
6054       if (!Combined)
6055         TheIndex->removeEmptySummaryEntries();
6056       return std::error_code();
6057     case BitstreamEntry::Record:
6058       // The interesting case.
6059       break;
6060     }
6061 
6062     // Read a record. The record format depends on whether this
6063     // is a per-module index or a combined index file. In the per-module
6064     // case the records contain the associated value's ID for correlation
6065     // with VST entries. In the combined index the correlation is done
6066     // via the bitcode offset of the summary records (which were saved
6067     // in the combined index VST entries). The records also contain
6068     // information used for ThinLTO renaming and importing.
6069     Record.clear();
6070     uint64_t CurRecordBit = Stream.GetCurrentBitNo();
6071     auto BitCode = Stream.readRecord(Entry.ID, Record);
6072     switch (BitCode) {
6073     default: // Default behavior: ignore.
6074       break;
6075     // FS_PERMODULE: [valueid, flags, instcount, numrefs, numrefs x valueid,
6076     //                n x (valueid, callsitecount)]
6077     // FS_PERMODULE_PROFILE: [valueid, flags, instcount, numrefs,
6078     //                        numrefs x valueid,
6079     //                        n x (valueid, callsitecount, profilecount)]
6080     case bitc::FS_PERMODULE:
6081     case bitc::FS_PERMODULE_PROFILE: {
6082       unsigned ValueID = Record[0];
6083       uint64_t RawFlags = Record[1];
6084       unsigned InstCount = Record[2];
6085       unsigned NumRefs = Record[3];
6086       auto Flags = getDecodedGVSummaryFlags(RawFlags, Version);
6087       std::unique_ptr<FunctionSummary> FS =
6088           llvm::make_unique<FunctionSummary>(Flags, InstCount);
6089       // The module path string ref set in the summary must be owned by the
6090       // index's module string table. Since we don't have a module path
6091       // string table section in the per-module index, we create a single
6092       // module path string table entry with an empty (0) ID to take
6093       // ownership.
6094       FS->setModulePath(
6095           TheIndex->addModulePath(Buffer->getBufferIdentifier(), 0)->first());
6096       static int RefListStartIndex = 4;
6097       int CallGraphEdgeStartIndex = RefListStartIndex + NumRefs;
6098       assert(Record.size() >= RefListStartIndex + NumRefs &&
6099              "Record size inconsistent with number of references");
6100       for (unsigned I = 4, E = CallGraphEdgeStartIndex; I != E; ++I) {
6101         unsigned RefValueId = Record[I];
6102         GlobalValue::GUID RefGUID = getGUIDFromValueId(RefValueId).first;
6103         FS->addRefEdge(RefGUID);
6104       }
6105       bool HasProfile = (BitCode == bitc::FS_PERMODULE_PROFILE);
6106       for (unsigned I = CallGraphEdgeStartIndex, E = Record.size(); I != E;
6107            ++I) {
6108         unsigned CalleeValueId = Record[I];
6109         unsigned CallsiteCount = Record[++I];
6110         uint64_t ProfileCount = HasProfile ? Record[++I] : 0;
6111         GlobalValue::GUID CalleeGUID = getGUIDFromValueId(CalleeValueId).first;
6112         FS->addCallGraphEdge(CalleeGUID,
6113                              CalleeInfo(CallsiteCount, ProfileCount));
6114       }
6115       auto GUID = getGUIDFromValueId(ValueID);
6116       FS->setOriginalName(GUID.second);
6117       TheIndex->addGlobalValueSummary(GUID.first, std::move(FS));
6118       break;
6119     }
6120     // FS_ALIAS: [valueid, flags, valueid]
6121     // Aliases must be emitted (and parsed) after all FS_PERMODULE entries, as
6122     // they expect all aliasee summaries to be available.
6123     case bitc::FS_ALIAS: {
6124       unsigned ValueID = Record[0];
6125       uint64_t RawFlags = Record[1];
6126       unsigned AliaseeID = Record[2];
6127       auto Flags = getDecodedGVSummaryFlags(RawFlags, Version);
6128       std::unique_ptr<AliasSummary> AS = llvm::make_unique<AliasSummary>(Flags);
6129       // The module path string ref set in the summary must be owned by the
6130       // index's module string table. Since we don't have a module path
6131       // string table section in the per-module index, we create a single
6132       // module path string table entry with an empty (0) ID to take
6133       // ownership.
6134       AS->setModulePath(
6135           TheIndex->addModulePath(Buffer->getBufferIdentifier(), 0)->first());
6136 
6137       GlobalValue::GUID AliaseeGUID = getGUIDFromValueId(AliaseeID).first;
6138       auto *AliaseeSummary = TheIndex->getGlobalValueSummary(AliaseeGUID);
6139       if (!AliaseeSummary)
6140         return error("Alias expects aliasee summary to be parsed");
6141       AS->setAliasee(AliaseeSummary);
6142 
6143       auto GUID = getGUIDFromValueId(ValueID);
6144       AS->setOriginalName(GUID.second);
6145       TheIndex->addGlobalValueSummary(GUID.first, std::move(AS));
6146       break;
6147     }
6148     // FS_PERMODULE_GLOBALVAR_INIT_REFS: [valueid, flags, n x valueid]
6149     case bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS: {
6150       unsigned ValueID = Record[0];
6151       uint64_t RawFlags = Record[1];
6152       auto Flags = getDecodedGVSummaryFlags(RawFlags, Version);
6153       std::unique_ptr<GlobalVarSummary> FS =
6154           llvm::make_unique<GlobalVarSummary>(Flags);
6155       FS->setModulePath(
6156           TheIndex->addModulePath(Buffer->getBufferIdentifier(), 0)->first());
6157       for (unsigned I = 2, E = Record.size(); I != E; ++I) {
6158         unsigned RefValueId = Record[I];
6159         GlobalValue::GUID RefGUID = getGUIDFromValueId(RefValueId).first;
6160         FS->addRefEdge(RefGUID);
6161       }
6162       auto GUID = getGUIDFromValueId(ValueID);
6163       FS->setOriginalName(GUID.second);
6164       TheIndex->addGlobalValueSummary(GUID.first, std::move(FS));
6165       break;
6166     }
6167     // FS_COMBINED: [modid, flags, instcount, numrefs, numrefs x valueid,
6168     //               n x (valueid, callsitecount)]
6169     // FS_COMBINED_PROFILE: [modid, flags, instcount, numrefs,
6170     //                       numrefs x valueid,
6171     //                       n x (valueid, callsitecount, profilecount)]
6172     case bitc::FS_COMBINED:
6173     case bitc::FS_COMBINED_PROFILE: {
6174       uint64_t ModuleId = Record[0];
6175       uint64_t RawFlags = Record[1];
6176       unsigned InstCount = Record[2];
6177       unsigned NumRefs = Record[3];
6178       auto Flags = getDecodedGVSummaryFlags(RawFlags, Version);
6179       std::unique_ptr<FunctionSummary> FS =
6180           llvm::make_unique<FunctionSummary>(Flags, InstCount);
6181       LastSeenSummary = FS.get();
6182       FS->setModulePath(ModuleIdMap[ModuleId]);
6183       static int RefListStartIndex = 4;
6184       int CallGraphEdgeStartIndex = RefListStartIndex + NumRefs;
6185       assert(Record.size() >= RefListStartIndex + NumRefs &&
6186              "Record size inconsistent with number of references");
6187       for (unsigned I = 4, E = CallGraphEdgeStartIndex; I != E; ++I) {
6188         unsigned RefValueId = Record[I];
6189         GlobalValue::GUID RefGUID = getGUIDFromValueId(RefValueId).first;
6190         FS->addRefEdge(RefGUID);
6191       }
6192       bool HasProfile = (BitCode == bitc::FS_COMBINED_PROFILE);
6193       for (unsigned I = CallGraphEdgeStartIndex, E = Record.size(); I != E;
6194            ++I) {
6195         unsigned CalleeValueId = Record[I];
6196         unsigned CallsiteCount = Record[++I];
6197         uint64_t ProfileCount = HasProfile ? Record[++I] : 0;
6198         GlobalValue::GUID CalleeGUID = getGUIDFromValueId(CalleeValueId).first;
6199         FS->addCallGraphEdge(CalleeGUID,
6200                              CalleeInfo(CallsiteCount, ProfileCount));
6201       }
6202       GlobalValue::GUID GUID = getGUIDFromOffset(CurRecordBit);
6203       OffsetToSummaryMap[CurRecordBit] = FS.get();
6204       TheIndex->addGlobalValueSummary(GUID, std::move(FS));
6205       Combined = true;
6206       break;
6207     }
6208     // FS_COMBINED_ALIAS: [modid, flags, offset]
6209     // Aliases must be emitted (and parsed) after all FS_COMBINED entries, as
6210     // they expect all aliasee summaries to be available.
6211     case bitc::FS_COMBINED_ALIAS: {
6212       uint64_t ModuleId = Record[0];
6213       uint64_t RawFlags = Record[1];
6214       uint64_t AliaseeSummaryOffset = Record[2];
6215       auto Flags = getDecodedGVSummaryFlags(RawFlags, Version);
6216       std::unique_ptr<AliasSummary> AS = llvm::make_unique<AliasSummary>(Flags);
6217       LastSeenSummary = AS.get();
6218       AS->setModulePath(ModuleIdMap[ModuleId]);
6219 
6220       auto *AliaseeSummary = OffsetToSummaryMap[AliaseeSummaryOffset];
6221       if (!AliaseeSummary)
6222         return error("Alias expects aliasee summary to be parsed");
6223       AS->setAliasee(AliaseeSummary);
6224 
6225       GlobalValue::GUID GUID = getGUIDFromOffset(CurRecordBit);
6226       TheIndex->addGlobalValueSummary(GUID, std::move(AS));
6227       Combined = true;
6228       break;
6229     }
6230     // FS_COMBINED_GLOBALVAR_INIT_REFS: [modid, flags, n x valueid]
6231     case bitc::FS_COMBINED_GLOBALVAR_INIT_REFS: {
6232       uint64_t ModuleId = Record[0];
6233       uint64_t RawFlags = Record[1];
6234       auto Flags = getDecodedGVSummaryFlags(RawFlags, Version);
6235       std::unique_ptr<GlobalVarSummary> FS =
6236           llvm::make_unique<GlobalVarSummary>(Flags);
6237       LastSeenSummary = FS.get();
6238       FS->setModulePath(ModuleIdMap[ModuleId]);
6239       for (unsigned I = 2, E = Record.size(); I != E; ++I) {
6240         unsigned RefValueId = Record[I];
6241         GlobalValue::GUID RefGUID = getGUIDFromValueId(RefValueId).first;
6242         FS->addRefEdge(RefGUID);
6243       }
6244       GlobalValue::GUID GUID = getGUIDFromOffset(CurRecordBit);
6245       OffsetToSummaryMap[CurRecordBit] = FS.get();
6246       TheIndex->addGlobalValueSummary(GUID, std::move(FS));
6247       Combined = true;
6248       break;
6249     }
6250     // FS_COMBINED_ORIGINAL_NAME: [original_name]
6251     case bitc::FS_COMBINED_ORIGINAL_NAME: {
6252       uint64_t OriginalName = Record[0];
6253       if (!LastSeenSummary)
6254         return error("Name attachment that does not follow a combined record");
6255       LastSeenSummary->setOriginalName(OriginalName);
6256       // Reset the LastSeenSummary
6257       LastSeenSummary = nullptr;
6258     }
6259     }
6260   }
6261   llvm_unreachable("Exit infinite loop");
6262 }
6263 
6264 // Parse the  module string table block into the Index.
6265 // This populates the ModulePathStringTable map in the index.
6266 std::error_code ModuleSummaryIndexBitcodeReader::parseModuleStringTable() {
6267   if (Stream.EnterSubBlock(bitc::MODULE_STRTAB_BLOCK_ID))
6268     return error("Invalid record");
6269 
6270   SmallVector<uint64_t, 64> Record;
6271 
6272   SmallString<128> ModulePath;
6273   ModulePathStringTableTy::iterator LastSeenModulePath;
6274   while (1) {
6275     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
6276 
6277     switch (Entry.Kind) {
6278     case BitstreamEntry::SubBlock: // Handled for us already.
6279     case BitstreamEntry::Error:
6280       return error("Malformed block");
6281     case BitstreamEntry::EndBlock:
6282       return std::error_code();
6283     case BitstreamEntry::Record:
6284       // The interesting case.
6285       break;
6286     }
6287 
6288     Record.clear();
6289     switch (Stream.readRecord(Entry.ID, Record)) {
6290     default: // Default behavior: ignore.
6291       break;
6292     case bitc::MST_CODE_ENTRY: {
6293       // MST_ENTRY: [modid, namechar x N]
6294       uint64_t ModuleId = Record[0];
6295 
6296       if (convertToString(Record, 1, ModulePath))
6297         return error("Invalid record");
6298 
6299       LastSeenModulePath = TheIndex->addModulePath(ModulePath, ModuleId);
6300       ModuleIdMap[ModuleId] = LastSeenModulePath->first();
6301 
6302       ModulePath.clear();
6303       break;
6304     }
6305     /// MST_CODE_HASH: [5*i32]
6306     case bitc::MST_CODE_HASH: {
6307       if (Record.size() != 5)
6308         return error("Invalid hash length " + Twine(Record.size()).str());
6309       if (LastSeenModulePath == TheIndex->modulePaths().end())
6310         return error("Invalid hash that does not follow a module path");
6311       int Pos = 0;
6312       for (auto &Val : Record) {
6313         assert(!(Val >> 32) && "Unexpected high bits set");
6314         LastSeenModulePath->second.second[Pos++] = Val;
6315       }
6316       // Reset LastSeenModulePath to avoid overriding the hash unexpectedly.
6317       LastSeenModulePath = TheIndex->modulePaths().end();
6318       break;
6319     }
6320     }
6321   }
6322   llvm_unreachable("Exit infinite loop");
6323 }
6324 
6325 // Parse the function info index from the bitcode streamer into the given index.
6326 std::error_code ModuleSummaryIndexBitcodeReader::parseSummaryIndexInto(
6327     std::unique_ptr<DataStreamer> Streamer, ModuleSummaryIndex *I) {
6328   TheIndex = I;
6329 
6330   if (std::error_code EC = initStream(std::move(Streamer)))
6331     return EC;
6332 
6333   // Sniff for the signature.
6334   if (!hasValidBitcodeHeader(Stream))
6335     return error("Invalid bitcode signature");
6336 
6337   // We expect a number of well-defined blocks, though we don't necessarily
6338   // need to understand them all.
6339   while (1) {
6340     if (Stream.AtEndOfStream()) {
6341       // We didn't really read a proper Module block.
6342       return error("Malformed block");
6343     }
6344 
6345     BitstreamEntry Entry =
6346         Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs);
6347 
6348     if (Entry.Kind != BitstreamEntry::SubBlock)
6349       return error("Malformed block");
6350 
6351     // If we see a MODULE_BLOCK, parse it to find the blocks needed for
6352     // building the function summary index.
6353     if (Entry.ID == bitc::MODULE_BLOCK_ID)
6354       return parseModule();
6355 
6356     if (Stream.SkipBlock())
6357       return error("Invalid record");
6358   }
6359 }
6360 
6361 // Parse the summary information at the given offset in the buffer into
6362 // the index. Used to support lazy parsing of summaries from the
6363 // combined index during importing.
6364 // TODO: This function is not yet complete as it won't have a consumer
6365 // until ThinLTO function importing is added.
6366 std::error_code ModuleSummaryIndexBitcodeReader::parseGlobalValueSummary(
6367     std::unique_ptr<DataStreamer> Streamer, ModuleSummaryIndex *I,
6368     size_t SummaryOffset) {
6369   TheIndex = I;
6370 
6371   if (std::error_code EC = initStream(std::move(Streamer)))
6372     return EC;
6373 
6374   // Sniff for the signature.
6375   if (!hasValidBitcodeHeader(Stream))
6376     return error("Invalid bitcode signature");
6377 
6378   Stream.JumpToBit(SummaryOffset);
6379 
6380   BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
6381 
6382   switch (Entry.Kind) {
6383   default:
6384     return error("Malformed block");
6385   case BitstreamEntry::Record:
6386     // The expected case.
6387     break;
6388   }
6389 
6390   // TODO: Read a record. This interface will be completed when ThinLTO
6391   // importing is added so that it can be tested.
6392   SmallVector<uint64_t, 64> Record;
6393   switch (Stream.readRecord(Entry.ID, Record)) {
6394   case bitc::FS_COMBINED:
6395   case bitc::FS_COMBINED_PROFILE:
6396   case bitc::FS_COMBINED_GLOBALVAR_INIT_REFS:
6397   default:
6398     return error("Invalid record");
6399   }
6400 
6401   return std::error_code();
6402 }
6403 
6404 std::error_code ModuleSummaryIndexBitcodeReader::initStream(
6405     std::unique_ptr<DataStreamer> Streamer) {
6406   if (Streamer)
6407     return initLazyStream(std::move(Streamer));
6408   return initStreamFromBuffer();
6409 }
6410 
6411 std::error_code ModuleSummaryIndexBitcodeReader::initStreamFromBuffer() {
6412   const unsigned char *BufPtr = (const unsigned char *)Buffer->getBufferStart();
6413   const unsigned char *BufEnd = BufPtr + Buffer->getBufferSize();
6414 
6415   if (Buffer->getBufferSize() & 3)
6416     return error("Invalid bitcode signature");
6417 
6418   // If we have a wrapper header, parse it and ignore the non-bc file contents.
6419   // The magic number is 0x0B17C0DE stored in little endian.
6420   if (isBitcodeWrapper(BufPtr, BufEnd))
6421     if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true))
6422       return error("Invalid bitcode wrapper header");
6423 
6424   StreamFile.reset(new BitstreamReader(BufPtr, BufEnd));
6425   Stream.init(&*StreamFile);
6426 
6427   return std::error_code();
6428 }
6429 
6430 std::error_code ModuleSummaryIndexBitcodeReader::initLazyStream(
6431     std::unique_ptr<DataStreamer> Streamer) {
6432   // Check and strip off the bitcode wrapper; BitstreamReader expects never to
6433   // see it.
6434   auto OwnedBytes =
6435       llvm::make_unique<StreamingMemoryObject>(std::move(Streamer));
6436   StreamingMemoryObject &Bytes = *OwnedBytes;
6437   StreamFile = llvm::make_unique<BitstreamReader>(std::move(OwnedBytes));
6438   Stream.init(&*StreamFile);
6439 
6440   unsigned char buf[16];
6441   if (Bytes.readBytes(buf, 16, 0) != 16)
6442     return error("Invalid bitcode signature");
6443 
6444   if (!isBitcode(buf, buf + 16))
6445     return error("Invalid bitcode signature");
6446 
6447   if (isBitcodeWrapper(buf, buf + 4)) {
6448     const unsigned char *bitcodeStart = buf;
6449     const unsigned char *bitcodeEnd = buf + 16;
6450     SkipBitcodeWrapperHeader(bitcodeStart, bitcodeEnd, false);
6451     Bytes.dropLeadingBytes(bitcodeStart - buf);
6452     Bytes.setKnownObjectSize(bitcodeEnd - bitcodeStart);
6453   }
6454   return std::error_code();
6455 }
6456 
6457 namespace {
6458 class BitcodeErrorCategoryType : public std::error_category {
6459   const char *name() const LLVM_NOEXCEPT override {
6460     return "llvm.bitcode";
6461   }
6462   std::string message(int IE) const override {
6463     BitcodeError E = static_cast<BitcodeError>(IE);
6464     switch (E) {
6465     case BitcodeError::InvalidBitcodeSignature:
6466       return "Invalid bitcode signature";
6467     case BitcodeError::CorruptedBitcode:
6468       return "Corrupted bitcode";
6469     }
6470     llvm_unreachable("Unknown error type!");
6471   }
6472 };
6473 } // end anonymous namespace
6474 
6475 static ManagedStatic<BitcodeErrorCategoryType> ErrorCategory;
6476 
6477 const std::error_category &llvm::BitcodeErrorCategory() {
6478   return *ErrorCategory;
6479 }
6480 
6481 //===----------------------------------------------------------------------===//
6482 // External interface
6483 //===----------------------------------------------------------------------===//
6484 
6485 static ErrorOr<std::unique_ptr<Module>>
6486 getBitcodeModuleImpl(std::unique_ptr<DataStreamer> Streamer, StringRef Name,
6487                      BitcodeReader *R, LLVMContext &Context,
6488                      bool MaterializeAll, bool ShouldLazyLoadMetadata) {
6489   std::unique_ptr<Module> M = make_unique<Module>(Name, Context);
6490   M->setMaterializer(R);
6491 
6492   auto cleanupOnError = [&](std::error_code EC) {
6493     R->releaseBuffer(); // Never take ownership on error.
6494     return EC;
6495   };
6496 
6497   // Delay parsing Metadata if ShouldLazyLoadMetadata is true.
6498   if (std::error_code EC = R->parseBitcodeInto(std::move(Streamer), M.get(),
6499                                                ShouldLazyLoadMetadata))
6500     return cleanupOnError(EC);
6501 
6502   if (MaterializeAll) {
6503     // Read in the entire module, and destroy the BitcodeReader.
6504     if (std::error_code EC = M->materializeAll())
6505       return cleanupOnError(EC);
6506   } else {
6507     // Resolve forward references from blockaddresses.
6508     if (std::error_code EC = R->materializeForwardReferencedFunctions())
6509       return cleanupOnError(EC);
6510   }
6511   return std::move(M);
6512 }
6513 
6514 /// \brief Get a lazy one-at-time loading module from bitcode.
6515 ///
6516 /// This isn't always used in a lazy context.  In particular, it's also used by
6517 /// \a parseBitcodeFile().  If this is truly lazy, then we need to eagerly pull
6518 /// in forward-referenced functions from block address references.
6519 ///
6520 /// \param[in] MaterializeAll Set to \c true if we should materialize
6521 /// everything.
6522 static ErrorOr<std::unique_ptr<Module>>
6523 getLazyBitcodeModuleImpl(std::unique_ptr<MemoryBuffer> &&Buffer,
6524                          LLVMContext &Context, bool MaterializeAll,
6525                          bool ShouldLazyLoadMetadata = false) {
6526   BitcodeReader *R = new BitcodeReader(Buffer.get(), Context);
6527 
6528   ErrorOr<std::unique_ptr<Module>> Ret =
6529       getBitcodeModuleImpl(nullptr, Buffer->getBufferIdentifier(), R, Context,
6530                            MaterializeAll, ShouldLazyLoadMetadata);
6531   if (!Ret)
6532     return Ret;
6533 
6534   Buffer.release(); // The BitcodeReader owns it now.
6535   return Ret;
6536 }
6537 
6538 ErrorOr<std::unique_ptr<Module>>
6539 llvm::getLazyBitcodeModule(std::unique_ptr<MemoryBuffer> &&Buffer,
6540                            LLVMContext &Context, bool ShouldLazyLoadMetadata) {
6541   return getLazyBitcodeModuleImpl(std::move(Buffer), Context, false,
6542                                   ShouldLazyLoadMetadata);
6543 }
6544 
6545 ErrorOr<std::unique_ptr<Module>>
6546 llvm::getStreamedBitcodeModule(StringRef Name,
6547                                std::unique_ptr<DataStreamer> Streamer,
6548                                LLVMContext &Context) {
6549   std::unique_ptr<Module> M = make_unique<Module>(Name, Context);
6550   BitcodeReader *R = new BitcodeReader(Context);
6551 
6552   return getBitcodeModuleImpl(std::move(Streamer), Name, R, Context, false,
6553                               false);
6554 }
6555 
6556 ErrorOr<std::unique_ptr<Module>> llvm::parseBitcodeFile(MemoryBufferRef Buffer,
6557                                                         LLVMContext &Context) {
6558   std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false);
6559   return getLazyBitcodeModuleImpl(std::move(Buf), Context, true);
6560   // TODO: Restore the use-lists to the in-memory state when the bitcode was
6561   // written.  We must defer until the Module has been fully materialized.
6562 }
6563 
6564 std::string llvm::getBitcodeTargetTriple(MemoryBufferRef Buffer,
6565                                          LLVMContext &Context) {
6566   std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false);
6567   auto R = llvm::make_unique<BitcodeReader>(Buf.release(), Context);
6568   ErrorOr<std::string> Triple = R->parseTriple();
6569   if (Triple.getError())
6570     return "";
6571   return Triple.get();
6572 }
6573 
6574 std::string llvm::getBitcodeProducerString(MemoryBufferRef Buffer,
6575                                            LLVMContext &Context) {
6576   std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false);
6577   BitcodeReader R(Buf.release(), Context);
6578   ErrorOr<std::string> ProducerString = R.parseIdentificationBlock();
6579   if (ProducerString.getError())
6580     return "";
6581   return ProducerString.get();
6582 }
6583 
6584 // Parse the specified bitcode buffer, returning the function info index.
6585 ErrorOr<std::unique_ptr<ModuleSummaryIndex>>
6586 llvm::getModuleSummaryIndex(MemoryBufferRef Buffer,
6587                             DiagnosticHandlerFunction DiagnosticHandler) {
6588   std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false);
6589   ModuleSummaryIndexBitcodeReader R(Buf.get(), DiagnosticHandler);
6590 
6591   auto Index = llvm::make_unique<ModuleSummaryIndex>();
6592 
6593   auto cleanupOnError = [&](std::error_code EC) {
6594     R.releaseBuffer(); // Never take ownership on error.
6595     return EC;
6596   };
6597 
6598   if (std::error_code EC = R.parseSummaryIndexInto(nullptr, Index.get()))
6599     return cleanupOnError(EC);
6600 
6601   Buf.release(); // The ModuleSummaryIndexBitcodeReader owns it now.
6602   return std::move(Index);
6603 }
6604 
6605 // Check if the given bitcode buffer contains a global value summary block.
6606 bool llvm::hasGlobalValueSummary(MemoryBufferRef Buffer,
6607                                  DiagnosticHandlerFunction DiagnosticHandler) {
6608   std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false);
6609   ModuleSummaryIndexBitcodeReader R(Buf.get(), DiagnosticHandler, true);
6610 
6611   auto cleanupOnError = [&](std::error_code EC) {
6612     R.releaseBuffer(); // Never take ownership on error.
6613     return false;
6614   };
6615 
6616   if (std::error_code EC = R.parseSummaryIndexInto(nullptr, nullptr))
6617     return cleanupOnError(EC);
6618 
6619   Buf.release(); // The ModuleSummaryIndexBitcodeReader owns it now.
6620   return R.foundGlobalValSummary();
6621 }
6622