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

Generated by: LCOV version 1.13