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