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