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