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