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