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