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