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