LCOV - code coverage report
Current view: top level - lib/Bitcode/Reader - BitcodeReader.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 2119 2605 81.3 %
Date: 2018-07-13 00:08:38 Functions: 99 105 94.3 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13