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