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