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