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

Generated by: LCOV version 1.13