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