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