LLVM  mainline
BitcodeReader.cpp
Go to the documentation of this file.
00001 //===- BitcodeReader.cpp - Internal BitcodeReader implementation ----------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #include "llvm/Bitcode/ReaderWriter.h"
00011 #include "llvm/ADT/STLExtras.h"
00012 #include "llvm/ADT/SmallString.h"
00013 #include "llvm/ADT/SmallVector.h"
00014 #include "llvm/ADT/Triple.h"
00015 #include "llvm/Bitcode/BitstreamReader.h"
00016 #include "llvm/Bitcode/LLVMBitCodes.h"
00017 #include "llvm/IR/AutoUpgrade.h"
00018 #include "llvm/IR/Constants.h"
00019 #include "llvm/IR/DebugInfo.h"
00020 #include "llvm/IR/DebugInfoMetadata.h"
00021 #include "llvm/IR/DerivedTypes.h"
00022 #include "llvm/IR/DiagnosticPrinter.h"
00023 #include "llvm/IR/GVMaterializer.h"
00024 #include "llvm/IR/InlineAsm.h"
00025 #include "llvm/IR/IntrinsicInst.h"
00026 #include "llvm/IR/LLVMContext.h"
00027 #include "llvm/IR/Module.h"
00028 #include "llvm/IR/OperandTraits.h"
00029 #include "llvm/IR/Operator.h"
00030 #include "llvm/IR/FunctionInfo.h"
00031 #include "llvm/IR/ValueHandle.h"
00032 #include "llvm/Support/DataStream.h"
00033 #include "llvm/Support/ManagedStatic.h"
00034 #include "llvm/Support/MathExtras.h"
00035 #include "llvm/Support/MemoryBuffer.h"
00036 #include "llvm/Support/raw_ostream.h"
00037 #include <deque>
00038 using namespace llvm;
00039 
00040 namespace {
00041 enum {
00042   SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex
00043 };
00044 
00045 class BitcodeReaderValueList {
00046   std::vector<WeakVH> ValuePtrs;
00047 
00048   /// As we resolve forward-referenced constants, we add information about them
00049   /// to this vector.  This allows us to resolve them in bulk instead of
00050   /// resolving each reference at a time.  See the code in
00051   /// ResolveConstantForwardRefs for more information about this.
00052   ///
00053   /// The key of this vector is the placeholder constant, the value is the slot
00054   /// number that holds the resolved value.
00055   typedef std::vector<std::pair<Constant*, unsigned> > ResolveConstantsTy;
00056   ResolveConstantsTy ResolveConstants;
00057   LLVMContext &Context;
00058 public:
00059   BitcodeReaderValueList(LLVMContext &C) : Context(C) {}
00060   ~BitcodeReaderValueList() {
00061     assert(ResolveConstants.empty() && "Constants not resolved?");
00062   }
00063 
00064   // vector compatibility methods
00065   unsigned size() const { return ValuePtrs.size(); }
00066   void resize(unsigned N) { ValuePtrs.resize(N); }
00067   void push_back(Value *V) { ValuePtrs.emplace_back(V); }
00068 
00069   void clear() {
00070     assert(ResolveConstants.empty() && "Constants not resolved?");
00071     ValuePtrs.clear();
00072   }
00073 
00074   Value *operator[](unsigned i) const {
00075     assert(i < ValuePtrs.size());
00076     return ValuePtrs[i];
00077   }
00078 
00079   Value *back() const { return ValuePtrs.back(); }
00080     void pop_back() { ValuePtrs.pop_back(); }
00081   bool empty() const { return ValuePtrs.empty(); }
00082   void shrinkTo(unsigned N) {
00083     assert(N <= size() && "Invalid shrinkTo request!");
00084     ValuePtrs.resize(N);
00085   }
00086 
00087   Constant *getConstantFwdRef(unsigned Idx, Type *Ty);
00088   Value *getValueFwdRef(unsigned Idx, Type *Ty);
00089 
00090   void assignValue(Value *V, unsigned Idx);
00091 
00092   /// Once all constants are read, this method bulk resolves any forward
00093   /// references.
00094   void resolveConstantForwardRefs();
00095 };
00096 
00097 class BitcodeReaderMetadataList {
00098   unsigned NumFwdRefs;
00099   bool AnyFwdRefs;
00100   unsigned MinFwdRef;
00101   unsigned MaxFwdRef;
00102   std::vector<TrackingMDRef> MetadataPtrs;
00103 
00104   LLVMContext &Context;
00105 public:
00106   BitcodeReaderMetadataList(LLVMContext &C)
00107       : NumFwdRefs(0), AnyFwdRefs(false), Context(C) {}
00108 
00109   // vector compatibility methods
00110   unsigned size() const { return MetadataPtrs.size(); }
00111   void resize(unsigned N) { MetadataPtrs.resize(N); }
00112   void push_back(Metadata *MD) { MetadataPtrs.emplace_back(MD); }
00113   void clear() { MetadataPtrs.clear(); }
00114   Metadata *back() const { return MetadataPtrs.back(); }
00115   void pop_back() { MetadataPtrs.pop_back(); }
00116   bool empty() const { return MetadataPtrs.empty(); }
00117 
00118   Metadata *operator[](unsigned i) const {
00119     assert(i < MetadataPtrs.size());
00120     return MetadataPtrs[i];
00121   }
00122 
00123   void shrinkTo(unsigned N) {
00124     assert(N <= size() && "Invalid shrinkTo request!");
00125     MetadataPtrs.resize(N);
00126   }
00127 
00128   Metadata *getValueFwdRef(unsigned Idx);
00129   void assignValue(Metadata *MD, unsigned Idx);
00130   void tryToResolveCycles();
00131 };
00132 
00133 class BitcodeReader : public GVMaterializer {
00134   LLVMContext &Context;
00135   Module *TheModule = nullptr;
00136   std::unique_ptr<MemoryBuffer> Buffer;
00137   std::unique_ptr<BitstreamReader> StreamFile;
00138   BitstreamCursor Stream;
00139   // Next offset to start scanning for lazy parsing of function bodies.
00140   uint64_t NextUnreadBit = 0;
00141   // Last function offset found in the VST.
00142   uint64_t LastFunctionBlockBit = 0;
00143   bool SeenValueSymbolTable = false;
00144   uint64_t VSTOffset = 0;
00145   // Contains an arbitrary and optional string identifying the bitcode producer
00146   std::string ProducerIdentification;
00147   // Number of module level metadata records specified by the
00148   // MODULE_CODE_METADATA_VALUES record.
00149   unsigned NumModuleMDs = 0;
00150   // Support older bitcode without the MODULE_CODE_METADATA_VALUES record.
00151   bool SeenModuleValuesRecord = false;
00152 
00153   std::vector<Type*> TypeList;
00154   BitcodeReaderValueList ValueList;
00155   BitcodeReaderMetadataList MetadataList;
00156   std::vector<Comdat *> ComdatList;
00157   SmallVector<Instruction *, 64> InstructionList;
00158 
00159   std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
00160   std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
00161   std::vector<std::pair<Function*, unsigned> > FunctionPrefixes;
00162   std::vector<std::pair<Function*, unsigned> > FunctionPrologues;
00163   std::vector<std::pair<Function*, unsigned> > FunctionPersonalityFns;
00164 
00165   SmallVector<Instruction*, 64> InstsWithTBAATag;
00166 
00167   /// The set of attributes by index.  Index zero in the file is for null, and
00168   /// is thus not represented here.  As such all indices are off by one.
00169   std::vector<AttributeSet> MAttributes;
00170 
00171   /// The set of attribute groups.
00172   std::map<unsigned, AttributeSet> MAttributeGroups;
00173 
00174   /// While parsing a function body, this is a list of the basic blocks for the
00175   /// function.
00176   std::vector<BasicBlock*> FunctionBBs;
00177 
00178   // When reading the module header, this list is populated with functions that
00179   // have bodies later in the file.
00180   std::vector<Function*> FunctionsWithBodies;
00181 
00182   // When intrinsic functions are encountered which require upgrading they are
00183   // stored here with their replacement function.
00184   typedef DenseMap<Function*, Function*> UpgradedIntrinsicMap;
00185   UpgradedIntrinsicMap UpgradedIntrinsics;
00186 
00187   // Map the bitcode's custom MDKind ID to the Module's MDKind ID.
00188   DenseMap<unsigned, unsigned> MDKindMap;
00189 
00190   // Several operations happen after the module header has been read, but
00191   // before function bodies are processed. This keeps track of whether
00192   // we've done this yet.
00193   bool SeenFirstFunctionBody = false;
00194 
00195   /// When function bodies are initially scanned, this map contains info about
00196   /// where to find deferred function body in the stream.
00197   DenseMap<Function*, uint64_t> DeferredFunctionInfo;
00198 
00199   /// When Metadata block is initially scanned when parsing the module, we may
00200   /// choose to defer parsing of the metadata. This vector contains info about
00201   /// which Metadata blocks are deferred.
00202   std::vector<uint64_t> DeferredMetadataInfo;
00203 
00204   /// These are basic blocks forward-referenced by block addresses.  They are
00205   /// inserted lazily into functions when they're loaded.  The basic block ID is
00206   /// its index into the vector.
00207   DenseMap<Function *, std::vector<BasicBlock *>> BasicBlockFwdRefs;
00208   std::deque<Function *> BasicBlockFwdRefQueue;
00209 
00210   /// Indicates that we are using a new encoding for instruction operands where
00211   /// most operands in the current FUNCTION_BLOCK are encoded relative to the
00212   /// instruction number, for a more compact encoding.  Some instruction
00213   /// operands are not relative to the instruction ID: basic block numbers, and
00214   /// types. Once the old style function blocks have been phased out, we would
00215   /// not need this flag.
00216   bool UseRelativeIDs = false;
00217 
00218   /// True if all functions will be materialized, negating the need to process
00219   /// (e.g.) blockaddress forward references.
00220   bool WillMaterializeAllForwardRefs = false;
00221 
00222   /// True if any Metadata block has been materialized.
00223   bool IsMetadataMaterialized = false;
00224 
00225   bool StripDebugInfo = false;
00226 
00227   /// Functions that need to be matched with subprograms when upgrading old
00228   /// metadata.
00229   SmallDenseMap<Function *, DISubprogram *, 16> FunctionsWithSPs;
00230 
00231   std::vector<std::string> BundleTags;
00232 
00233 public:
00234   std::error_code error(BitcodeError E, const Twine &Message);
00235   std::error_code error(BitcodeError E);
00236   std::error_code error(const Twine &Message);
00237 
00238   BitcodeReader(MemoryBuffer *Buffer, LLVMContext &Context);
00239   BitcodeReader(LLVMContext &Context);
00240   ~BitcodeReader() override { freeState(); }
00241 
00242   std::error_code materializeForwardReferencedFunctions();
00243 
00244   void freeState();
00245 
00246   void releaseBuffer();
00247 
00248   std::error_code materialize(GlobalValue *GV) override;
00249   std::error_code materializeModule() override;
00250   std::vector<StructType *> getIdentifiedStructTypes() const override;
00251 
00252   /// \brief Main interface to parsing a bitcode buffer.
00253   /// \returns true if an error occurred.
00254   std::error_code parseBitcodeInto(std::unique_ptr<DataStreamer> Streamer,
00255                                    Module *M,
00256                                    bool ShouldLazyLoadMetadata = false);
00257 
00258   /// \brief Cheap mechanism to just extract module triple
00259   /// \returns true if an error occurred.
00260   ErrorOr<std::string> parseTriple();
00261 
00262   /// Cheap mechanism to just extract the identification block out of bitcode.
00263   ErrorOr<std::string> parseIdentificationBlock();
00264 
00265   static uint64_t decodeSignRotatedValue(uint64_t V);
00266 
00267   /// Materialize any deferred Metadata block.
00268   std::error_code materializeMetadata() override;
00269 
00270   void setStripDebugInfo() override;
00271 
00272   /// Save the mapping between the metadata values and the corresponding
00273   /// value id that were recorded in the MetadataList during parsing. If
00274   /// OnlyTempMD is true, then only record those entries that are still
00275   /// temporary metadata. This interface is used when metadata linking is
00276   /// performed as a postpass, such as during function importing.
00277   void saveMetadataList(DenseMap<const Metadata *, unsigned> &MetadataToIDs,
00278                         bool OnlyTempMD) override;
00279 
00280 private:
00281   /// Parse the "IDENTIFICATION_BLOCK_ID" block, populate the
00282   // ProducerIdentification data member, and do some basic enforcement on the
00283   // "epoch" encoded in the bitcode.
00284   std::error_code parseBitcodeVersion();
00285 
00286   std::vector<StructType *> IdentifiedStructTypes;
00287   StructType *createIdentifiedStructType(LLVMContext &Context, StringRef Name);
00288   StructType *createIdentifiedStructType(LLVMContext &Context);
00289 
00290   Type *getTypeByID(unsigned ID);
00291   Value *getFnValueByID(unsigned ID, Type *Ty) {
00292     if (Ty && Ty->isMetadataTy())
00293       return MetadataAsValue::get(Ty->getContext(), getFnMetadataByID(ID));
00294     return ValueList.getValueFwdRef(ID, Ty);
00295   }
00296   Metadata *getFnMetadataByID(unsigned ID) {
00297     return MetadataList.getValueFwdRef(ID);
00298   }
00299   BasicBlock *getBasicBlock(unsigned ID) const {
00300     if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID
00301     return FunctionBBs[ID];
00302   }
00303   AttributeSet getAttributes(unsigned i) const {
00304     if (i-1 < MAttributes.size())
00305       return MAttributes[i-1];
00306     return AttributeSet();
00307   }
00308 
00309   /// Read a value/type pair out of the specified record from slot 'Slot'.
00310   /// Increment Slot past the number of slots used in the record. Return true on
00311   /// failure.
00312   bool getValueTypePair(SmallVectorImpl<uint64_t> &Record, unsigned &Slot,
00313                         unsigned InstNum, Value *&ResVal) {
00314     if (Slot == Record.size()) return true;
00315     unsigned ValNo = (unsigned)Record[Slot++];
00316     // Adjust the ValNo, if it was encoded relative to the InstNum.
00317     if (UseRelativeIDs)
00318       ValNo = InstNum - ValNo;
00319     if (ValNo < InstNum) {
00320       // If this is not a forward reference, just return the value we already
00321       // have.
00322       ResVal = getFnValueByID(ValNo, nullptr);
00323       return ResVal == nullptr;
00324     }
00325     if (Slot == Record.size())
00326       return true;
00327 
00328     unsigned TypeNo = (unsigned)Record[Slot++];
00329     ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
00330     return ResVal == nullptr;
00331   }
00332 
00333   /// Read a value out of the specified record from slot 'Slot'. Increment Slot
00334   /// past the number of slots used by the value in the record. Return true if
00335   /// there is an error.
00336   bool popValue(SmallVectorImpl<uint64_t> &Record, unsigned &Slot,
00337                 unsigned InstNum, Type *Ty, Value *&ResVal) {
00338     if (getValue(Record, Slot, InstNum, Ty, ResVal))
00339       return true;
00340     // All values currently take a single record slot.
00341     ++Slot;
00342     return false;
00343   }
00344 
00345   /// Like popValue, but does not increment the Slot number.
00346   bool getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
00347                 unsigned InstNum, Type *Ty, Value *&ResVal) {
00348     ResVal = getValue(Record, Slot, InstNum, Ty);
00349     return ResVal == nullptr;
00350   }
00351 
00352   /// Version of getValue that returns ResVal directly, or 0 if there is an
00353   /// error.
00354   Value *getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
00355                   unsigned InstNum, Type *Ty) {
00356     if (Slot == Record.size()) return nullptr;
00357     unsigned ValNo = (unsigned)Record[Slot];
00358     // Adjust the ValNo, if it was encoded relative to the InstNum.
00359     if (UseRelativeIDs)
00360       ValNo = InstNum - ValNo;
00361     return getFnValueByID(ValNo, Ty);
00362   }
00363 
00364   /// Like getValue, but decodes signed VBRs.
00365   Value *getValueSigned(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
00366                         unsigned InstNum, Type *Ty) {
00367     if (Slot == Record.size()) return nullptr;
00368     unsigned ValNo = (unsigned)decodeSignRotatedValue(Record[Slot]);
00369     // Adjust the ValNo, if it was encoded relative to the InstNum.
00370     if (UseRelativeIDs)
00371       ValNo = InstNum - ValNo;
00372     return getFnValueByID(ValNo, Ty);
00373   }
00374 
00375   /// Converts alignment exponent (i.e. power of two (or zero)) to the
00376   /// corresponding alignment to use. If alignment is too large, returns
00377   /// a corresponding error code.
00378   std::error_code parseAlignmentValue(uint64_t Exponent, unsigned &Alignment);
00379   std::error_code parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind);
00380   std::error_code parseModule(uint64_t ResumeBit,
00381                               bool ShouldLazyLoadMetadata = false);
00382   std::error_code parseAttributeBlock();
00383   std::error_code parseAttributeGroupBlock();
00384   std::error_code parseTypeTable();
00385   std::error_code parseTypeTableBody();
00386   std::error_code parseOperandBundleTags();
00387 
00388   ErrorOr<Value *> recordValue(SmallVectorImpl<uint64_t> &Record,
00389                                unsigned NameIndex, Triple &TT);
00390   std::error_code parseValueSymbolTable(uint64_t Offset = 0);
00391   std::error_code parseConstants();
00392   std::error_code rememberAndSkipFunctionBodies();
00393   std::error_code rememberAndSkipFunctionBody();
00394   /// Save the positions of the Metadata blocks and skip parsing the blocks.
00395   std::error_code rememberAndSkipMetadata();
00396   std::error_code parseFunctionBody(Function *F);
00397   std::error_code globalCleanup();
00398   std::error_code resolveGlobalAndAliasInits();
00399   std::error_code parseMetadata(bool ModuleLevel = false);
00400   std::error_code parseMetadataKinds();
00401   std::error_code parseMetadataKindRecord(SmallVectorImpl<uint64_t> &Record);
00402   std::error_code parseMetadataAttachment(Function &F);
00403   ErrorOr<std::string> parseModuleTriple();
00404   std::error_code parseUseLists();
00405   std::error_code initStream(std::unique_ptr<DataStreamer> Streamer);
00406   std::error_code initStreamFromBuffer();
00407   std::error_code initLazyStream(std::unique_ptr<DataStreamer> Streamer);
00408   std::error_code findFunctionInStream(
00409       Function *F,
00410       DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator);
00411 };
00412 
00413 /// Class to manage reading and parsing function summary index bitcode
00414 /// files/sections.
00415 class FunctionIndexBitcodeReader {
00416   DiagnosticHandlerFunction DiagnosticHandler;
00417 
00418   /// Eventually points to the function index built during parsing.
00419   FunctionInfoIndex *TheIndex = nullptr;
00420 
00421   std::unique_ptr<MemoryBuffer> Buffer;
00422   std::unique_ptr<BitstreamReader> StreamFile;
00423   BitstreamCursor Stream;
00424 
00425   /// \brief Used to indicate whether we are doing lazy parsing of summary data.
00426   ///
00427   /// If false, the summary section is fully parsed into the index during
00428   /// the initial parse. Otherwise, if true, the caller is expected to
00429   /// invoke \a readFunctionSummary for each summary needed, and the summary
00430   /// section is thus parsed lazily.
00431   bool IsLazy = false;
00432 
00433   /// Used to indicate whether caller only wants to check for the presence
00434   /// of the function summary bitcode section. All blocks are skipped,
00435   /// but the SeenFuncSummary boolean is set.
00436   bool CheckFuncSummaryPresenceOnly = false;
00437 
00438   /// Indicates whether we have encountered a function summary section
00439   /// yet during parsing, used when checking if file contains function
00440   /// summary section.
00441   bool SeenFuncSummary = false;
00442 
00443   /// \brief Map populated during function summary section parsing, and
00444   /// consumed during ValueSymbolTable parsing.
00445   ///
00446   /// Used to correlate summary records with VST entries. For the per-module
00447   /// index this maps the ValueID to the parsed function summary, and
00448   /// for the combined index this maps the summary record's bitcode
00449   /// offset to the function summary (since in the combined index the
00450   /// VST records do not hold value IDs but rather hold the function
00451   /// summary record offset).
00452   DenseMap<uint64_t, std::unique_ptr<FunctionSummary>> SummaryMap;
00453 
00454   /// Map populated during module path string table parsing, from the
00455   /// module ID to a string reference owned by the index's module
00456   /// path string table, used to correlate with combined index function
00457   /// summary records.
00458   DenseMap<uint64_t, StringRef> ModuleIdMap;
00459 
00460 public:
00461   std::error_code error(BitcodeError E, const Twine &Message);
00462   std::error_code error(BitcodeError E);
00463   std::error_code error(const Twine &Message);
00464 
00465   FunctionIndexBitcodeReader(MemoryBuffer *Buffer,
00466                              DiagnosticHandlerFunction DiagnosticHandler,
00467                              bool IsLazy = false,
00468                              bool CheckFuncSummaryPresenceOnly = false);
00469   FunctionIndexBitcodeReader(DiagnosticHandlerFunction DiagnosticHandler,
00470                              bool IsLazy = false,
00471                              bool CheckFuncSummaryPresenceOnly = false);
00472   ~FunctionIndexBitcodeReader() { freeState(); }
00473 
00474   void freeState();
00475 
00476   void releaseBuffer();
00477 
00478   /// Check if the parser has encountered a function summary section.
00479   bool foundFuncSummary() { return SeenFuncSummary; }
00480 
00481   /// \brief Main interface to parsing a bitcode buffer.
00482   /// \returns true if an error occurred.
00483   std::error_code parseSummaryIndexInto(std::unique_ptr<DataStreamer> Streamer,
00484                                         FunctionInfoIndex *I);
00485 
00486   /// \brief Interface for parsing a function summary lazily.
00487   std::error_code parseFunctionSummary(std::unique_ptr<DataStreamer> Streamer,
00488                                        FunctionInfoIndex *I,
00489                                        size_t FunctionSummaryOffset);
00490 
00491 private:
00492   std::error_code parseModule();
00493   std::error_code parseValueSymbolTable();
00494   std::error_code parseEntireSummary();
00495   std::error_code parseModuleStringTable();
00496   std::error_code initStream(std::unique_ptr<DataStreamer> Streamer);
00497   std::error_code initStreamFromBuffer();
00498   std::error_code initLazyStream(std::unique_ptr<DataStreamer> Streamer);
00499 };
00500 } // namespace
00501 
00502 BitcodeDiagnosticInfo::BitcodeDiagnosticInfo(std::error_code EC,
00503                                              DiagnosticSeverity Severity,
00504                                              const Twine &Msg)
00505     : DiagnosticInfo(DK_Bitcode, Severity), Msg(Msg), EC(EC) {}
00506 
00507 void BitcodeDiagnosticInfo::print(DiagnosticPrinter &DP) const { DP << Msg; }
00508 
00509 static std::error_code error(DiagnosticHandlerFunction DiagnosticHandler,
00510                              std::error_code EC, const Twine &Message) {
00511   BitcodeDiagnosticInfo DI(EC, DS_Error, Message);
00512   DiagnosticHandler(DI);
00513   return EC;
00514 }
00515 
00516 static std::error_code error(DiagnosticHandlerFunction DiagnosticHandler,
00517                              std::error_code EC) {
00518   return error(DiagnosticHandler, EC, EC.message());
00519 }
00520 
00521 static std::error_code error(LLVMContext &Context, std::error_code EC,
00522                              const Twine &Message) {
00523   return error([&](const DiagnosticInfo &DI) { Context.diagnose(DI); }, EC,
00524                Message);
00525 }
00526 
00527 static std::error_code error(LLVMContext &Context, std::error_code EC) {
00528   return error(Context, EC, EC.message());
00529 }
00530 
00531 static std::error_code error(LLVMContext &Context, const Twine &Message) {
00532   return error(Context, make_error_code(BitcodeError::CorruptedBitcode),
00533                Message);
00534 }
00535 
00536 std::error_code BitcodeReader::error(BitcodeError E, const Twine &Message) {
00537   if (!ProducerIdentification.empty()) {
00538     return ::error(Context, make_error_code(E),
00539                    Message + " (Producer: '" + ProducerIdentification +
00540                        "' Reader: 'LLVM " + LLVM_VERSION_STRING "')");
00541   }
00542   return ::error(Context, make_error_code(E), Message);
00543 }
00544 
00545 std::error_code BitcodeReader::error(const Twine &Message) {
00546   if (!ProducerIdentification.empty()) {
00547     return ::error(Context, make_error_code(BitcodeError::CorruptedBitcode),
00548                    Message + " (Producer: '" + ProducerIdentification +
00549                        "' Reader: 'LLVM " + LLVM_VERSION_STRING "')");
00550   }
00551   return ::error(Context, make_error_code(BitcodeError::CorruptedBitcode),
00552                  Message);
00553 }
00554 
00555 std::error_code BitcodeReader::error(BitcodeError E) {
00556   return ::error(Context, make_error_code(E));
00557 }
00558 
00559 BitcodeReader::BitcodeReader(MemoryBuffer *Buffer, LLVMContext &Context)
00560     : Context(Context), Buffer(Buffer), ValueList(Context),
00561       MetadataList(Context) {}
00562 
00563 BitcodeReader::BitcodeReader(LLVMContext &Context)
00564     : Context(Context), Buffer(nullptr), ValueList(Context),
00565       MetadataList(Context) {}
00566 
00567 std::error_code BitcodeReader::materializeForwardReferencedFunctions() {
00568   if (WillMaterializeAllForwardRefs)
00569     return std::error_code();
00570 
00571   // Prevent recursion.
00572   WillMaterializeAllForwardRefs = true;
00573 
00574   while (!BasicBlockFwdRefQueue.empty()) {
00575     Function *F = BasicBlockFwdRefQueue.front();
00576     BasicBlockFwdRefQueue.pop_front();
00577     assert(F && "Expected valid function");
00578     if (!BasicBlockFwdRefs.count(F))
00579       // Already materialized.
00580       continue;
00581 
00582     // Check for a function that isn't materializable to prevent an infinite
00583     // loop.  When parsing a blockaddress stored in a global variable, there
00584     // isn't a trivial way to check if a function will have a body without a
00585     // linear search through FunctionsWithBodies, so just check it here.
00586     if (!F->isMaterializable())
00587       return error("Never resolved function from blockaddress");
00588 
00589     // Try to materialize F.
00590     if (std::error_code EC = materialize(F))
00591       return EC;
00592   }
00593   assert(BasicBlockFwdRefs.empty() && "Function missing from queue");
00594 
00595   // Reset state.
00596   WillMaterializeAllForwardRefs = false;
00597   return std::error_code();
00598 }
00599 
00600 void BitcodeReader::freeState() {
00601   Buffer = nullptr;
00602   std::vector<Type*>().swap(TypeList);
00603   ValueList.clear();
00604   MetadataList.clear();
00605   std::vector<Comdat *>().swap(ComdatList);
00606 
00607   std::vector<AttributeSet>().swap(MAttributes);
00608   std::vector<BasicBlock*>().swap(FunctionBBs);
00609   std::vector<Function*>().swap(FunctionsWithBodies);
00610   DeferredFunctionInfo.clear();
00611   DeferredMetadataInfo.clear();
00612   MDKindMap.clear();
00613 
00614   assert(BasicBlockFwdRefs.empty() && "Unresolved blockaddress fwd references");
00615   BasicBlockFwdRefQueue.clear();
00616 }
00617 
00618 //===----------------------------------------------------------------------===//
00619 //  Helper functions to implement forward reference resolution, etc.
00620 //===----------------------------------------------------------------------===//
00621 
00622 /// Convert a string from a record into an std::string, return true on failure.
00623 template <typename StrTy>
00624 static bool convertToString(ArrayRef<uint64_t> Record, unsigned Idx,
00625                             StrTy &Result) {
00626   if (Idx > Record.size())
00627     return true;
00628 
00629   for (unsigned i = Idx, e = Record.size(); i != e; ++i)
00630     Result += (char)Record[i];
00631   return false;
00632 }
00633 
00634 static bool hasImplicitComdat(size_t Val) {
00635   switch (Val) {
00636   default:
00637     return false;
00638   case 1:  // Old WeakAnyLinkage
00639   case 4:  // Old LinkOnceAnyLinkage
00640   case 10: // Old WeakODRLinkage
00641   case 11: // Old LinkOnceODRLinkage
00642     return true;
00643   }
00644 }
00645 
00646 static GlobalValue::LinkageTypes getDecodedLinkage(unsigned Val) {
00647   switch (Val) {
00648   default: // Map unknown/new linkages to external
00649   case 0:
00650     return GlobalValue::ExternalLinkage;
00651   case 2:
00652     return GlobalValue::AppendingLinkage;
00653   case 3:
00654     return GlobalValue::InternalLinkage;
00655   case 5:
00656     return GlobalValue::ExternalLinkage; // Obsolete DLLImportLinkage
00657   case 6:
00658     return GlobalValue::ExternalLinkage; // Obsolete DLLExportLinkage
00659   case 7:
00660     return GlobalValue::ExternalWeakLinkage;
00661   case 8:
00662     return GlobalValue::CommonLinkage;
00663   case 9:
00664     return GlobalValue::PrivateLinkage;
00665   case 12:
00666     return GlobalValue::AvailableExternallyLinkage;
00667   case 13:
00668     return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateLinkage
00669   case 14:
00670     return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateWeakLinkage
00671   case 15:
00672     return GlobalValue::ExternalLinkage; // Obsolete LinkOnceODRAutoHideLinkage
00673   case 1: // Old value with implicit comdat.
00674   case 16:
00675     return GlobalValue::WeakAnyLinkage;
00676   case 10: // Old value with implicit comdat.
00677   case 17:
00678     return GlobalValue::WeakODRLinkage;
00679   case 4: // Old value with implicit comdat.
00680   case 18:
00681     return GlobalValue::LinkOnceAnyLinkage;
00682   case 11: // Old value with implicit comdat.
00683   case 19:
00684     return GlobalValue::LinkOnceODRLinkage;
00685   }
00686 }
00687 
00688 static GlobalValue::VisibilityTypes getDecodedVisibility(unsigned Val) {
00689   switch (Val) {
00690   default: // Map unknown visibilities to default.
00691   case 0: return GlobalValue::DefaultVisibility;
00692   case 1: return GlobalValue::HiddenVisibility;
00693   case 2: return GlobalValue::ProtectedVisibility;
00694   }
00695 }
00696 
00697 static GlobalValue::DLLStorageClassTypes
00698 getDecodedDLLStorageClass(unsigned Val) {
00699   switch (Val) {
00700   default: // Map unknown values to default.
00701   case 0: return GlobalValue::DefaultStorageClass;
00702   case 1: return GlobalValue::DLLImportStorageClass;
00703   case 2: return GlobalValue::DLLExportStorageClass;
00704   }
00705 }
00706 
00707 static GlobalVariable::ThreadLocalMode getDecodedThreadLocalMode(unsigned Val) {
00708   switch (Val) {
00709     case 0: return GlobalVariable::NotThreadLocal;
00710     default: // Map unknown non-zero value to general dynamic.
00711     case 1: return GlobalVariable::GeneralDynamicTLSModel;
00712     case 2: return GlobalVariable::LocalDynamicTLSModel;
00713     case 3: return GlobalVariable::InitialExecTLSModel;
00714     case 4: return GlobalVariable::LocalExecTLSModel;
00715   }
00716 }
00717 
00718 static int getDecodedCastOpcode(unsigned Val) {
00719   switch (Val) {
00720   default: return -1;
00721   case bitc::CAST_TRUNC   : return Instruction::Trunc;
00722   case bitc::CAST_ZEXT    : return Instruction::ZExt;
00723   case bitc::CAST_SEXT    : return Instruction::SExt;
00724   case bitc::CAST_FPTOUI  : return Instruction::FPToUI;
00725   case bitc::CAST_FPTOSI  : return Instruction::FPToSI;
00726   case bitc::CAST_UITOFP  : return Instruction::UIToFP;
00727   case bitc::CAST_SITOFP  : return Instruction::SIToFP;
00728   case bitc::CAST_FPTRUNC : return Instruction::FPTrunc;
00729   case bitc::CAST_FPEXT   : return Instruction::FPExt;
00730   case bitc::CAST_PTRTOINT: return Instruction::PtrToInt;
00731   case bitc::CAST_INTTOPTR: return Instruction::IntToPtr;
00732   case bitc::CAST_BITCAST : return Instruction::BitCast;
00733   case bitc::CAST_ADDRSPACECAST: return Instruction::AddrSpaceCast;
00734   }
00735 }
00736 
00737 static int getDecodedBinaryOpcode(unsigned Val, Type *Ty) {
00738   bool IsFP = Ty->isFPOrFPVectorTy();
00739   // BinOps are only valid for int/fp or vector of int/fp types
00740   if (!IsFP && !Ty->isIntOrIntVectorTy())
00741     return -1;
00742 
00743   switch (Val) {
00744   default:
00745     return -1;
00746   case bitc::BINOP_ADD:
00747     return IsFP ? Instruction::FAdd : Instruction::Add;
00748   case bitc::BINOP_SUB:
00749     return IsFP ? Instruction::FSub : Instruction::Sub;
00750   case bitc::BINOP_MUL:
00751     return IsFP ? Instruction::FMul : Instruction::Mul;
00752   case bitc::BINOP_UDIV:
00753     return IsFP ? -1 : Instruction::UDiv;
00754   case bitc::BINOP_SDIV:
00755     return IsFP ? Instruction::FDiv : Instruction::SDiv;
00756   case bitc::BINOP_UREM:
00757     return IsFP ? -1 : Instruction::URem;
00758   case bitc::BINOP_SREM:
00759     return IsFP ? Instruction::FRem : Instruction::SRem;
00760   case bitc::BINOP_SHL:
00761     return IsFP ? -1 : Instruction::Shl;
00762   case bitc::BINOP_LSHR:
00763     return IsFP ? -1 : Instruction::LShr;
00764   case bitc::BINOP_ASHR:
00765     return IsFP ? -1 : Instruction::AShr;
00766   case bitc::BINOP_AND:
00767     return IsFP ? -1 : Instruction::And;
00768   case bitc::BINOP_OR:
00769     return IsFP ? -1 : Instruction::Or;
00770   case bitc::BINOP_XOR:
00771     return IsFP ? -1 : Instruction::Xor;
00772   }
00773 }
00774 
00775 static AtomicRMWInst::BinOp getDecodedRMWOperation(unsigned Val) {
00776   switch (Val) {
00777   default: return AtomicRMWInst::BAD_BINOP;
00778   case bitc::RMW_XCHG: return AtomicRMWInst::Xchg;
00779   case bitc::RMW_ADD: return AtomicRMWInst::Add;
00780   case bitc::RMW_SUB: return AtomicRMWInst::Sub;
00781   case bitc::RMW_AND: return AtomicRMWInst::And;
00782   case bitc::RMW_NAND: return AtomicRMWInst::Nand;
00783   case bitc::RMW_OR: return AtomicRMWInst::Or;
00784   case bitc::RMW_XOR: return AtomicRMWInst::Xor;
00785   case bitc::RMW_MAX: return AtomicRMWInst::Max;
00786   case bitc::RMW_MIN: return AtomicRMWInst::Min;
00787   case bitc::RMW_UMAX: return AtomicRMWInst::UMax;
00788   case bitc::RMW_UMIN: return AtomicRMWInst::UMin;
00789   }
00790 }
00791 
00792 static AtomicOrdering getDecodedOrdering(unsigned Val) {
00793   switch (Val) {
00794   case bitc::ORDERING_NOTATOMIC: return NotAtomic;
00795   case bitc::ORDERING_UNORDERED: return Unordered;
00796   case bitc::ORDERING_MONOTONIC: return Monotonic;
00797   case bitc::ORDERING_ACQUIRE: return Acquire;
00798   case bitc::ORDERING_RELEASE: return Release;
00799   case bitc::ORDERING_ACQREL: return AcquireRelease;
00800   default: // Map unknown orderings to sequentially-consistent.
00801   case bitc::ORDERING_SEQCST: return SequentiallyConsistent;
00802   }
00803 }
00804 
00805 static SynchronizationScope getDecodedSynchScope(unsigned Val) {
00806   switch (Val) {
00807   case bitc::SYNCHSCOPE_SINGLETHREAD: return SingleThread;
00808   default: // Map unknown scopes to cross-thread.
00809   case bitc::SYNCHSCOPE_CROSSTHREAD: return CrossThread;
00810   }
00811 }
00812 
00813 static Comdat::SelectionKind getDecodedComdatSelectionKind(unsigned Val) {
00814   switch (Val) {
00815   default: // Map unknown selection kinds to any.
00816   case bitc::COMDAT_SELECTION_KIND_ANY:
00817     return Comdat::Any;
00818   case bitc::COMDAT_SELECTION_KIND_EXACT_MATCH:
00819     return Comdat::ExactMatch;
00820   case bitc::COMDAT_SELECTION_KIND_LARGEST:
00821     return Comdat::Largest;
00822   case bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES:
00823     return Comdat::NoDuplicates;
00824   case bitc::COMDAT_SELECTION_KIND_SAME_SIZE:
00825     return Comdat::SameSize;
00826   }
00827 }
00828 
00829 static FastMathFlags getDecodedFastMathFlags(unsigned Val) {
00830   FastMathFlags FMF;
00831   if (0 != (Val & FastMathFlags::UnsafeAlgebra))
00832     FMF.setUnsafeAlgebra();
00833   if (0 != (Val & FastMathFlags::NoNaNs))
00834     FMF.setNoNaNs();
00835   if (0 != (Val & FastMathFlags::NoInfs))
00836     FMF.setNoInfs();
00837   if (0 != (Val & FastMathFlags::NoSignedZeros))
00838     FMF.setNoSignedZeros();
00839   if (0 != (Val & FastMathFlags::AllowReciprocal))
00840     FMF.setAllowReciprocal();
00841   return FMF;
00842 }
00843 
00844 static void upgradeDLLImportExportLinkage(llvm::GlobalValue *GV, unsigned Val) {
00845   switch (Val) {
00846   case 5: GV->setDLLStorageClass(GlobalValue::DLLImportStorageClass); break;
00847   case 6: GV->setDLLStorageClass(GlobalValue::DLLExportStorageClass); break;
00848   }
00849 }
00850 
00851 namespace llvm {
00852 namespace {
00853 /// \brief A class for maintaining the slot number definition
00854 /// as a placeholder for the actual definition for forward constants defs.
00855 class ConstantPlaceHolder : public ConstantExpr {
00856   void operator=(const ConstantPlaceHolder &) = delete;
00857 
00858 public:
00859   // allocate space for exactly one operand
00860   void *operator new(size_t s) { return User::operator new(s, 1); }
00861   explicit ConstantPlaceHolder(Type *Ty, LLVMContext &Context)
00862       : ConstantExpr(Ty, Instruction::UserOp1, &Op<0>(), 1) {
00863     Op<0>() = UndefValue::get(Type::getInt32Ty(Context));
00864   }
00865 
00866   /// \brief Methods to support type inquiry through isa, cast, and dyn_cast.
00867   static bool classof(const Value *V) {
00868     return isa<ConstantExpr>(V) &&
00869            cast<ConstantExpr>(V)->getOpcode() == Instruction::UserOp1;
00870   }
00871 
00872   /// Provide fast operand accessors
00873   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
00874 };
00875 }
00876 
00877 // FIXME: can we inherit this from ConstantExpr?
00878 template <>
00879 struct OperandTraits<ConstantPlaceHolder> :
00880   public FixedNumOperandTraits<ConstantPlaceHolder, 1> {
00881 };
00882 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantPlaceHolder, Value)
00883 }
00884 
00885 void BitcodeReaderValueList::assignValue(Value *V, unsigned Idx) {
00886   if (Idx == size()) {
00887     push_back(V);
00888     return;
00889   }
00890 
00891   if (Idx >= size())
00892     resize(Idx+1);
00893 
00894   WeakVH &OldV = ValuePtrs[Idx];
00895   if (!OldV) {
00896     OldV = V;
00897     return;
00898   }
00899 
00900   // Handle constants and non-constants (e.g. instrs) differently for
00901   // efficiency.
00902   if (Constant *PHC = dyn_cast<Constant>(&*OldV)) {
00903     ResolveConstants.push_back(std::make_pair(PHC, Idx));
00904     OldV = V;
00905   } else {
00906     // If there was a forward reference to this value, replace it.
00907     Value *PrevVal = OldV;
00908     OldV->replaceAllUsesWith(V);
00909     delete PrevVal;
00910   }
00911 
00912   return;
00913 }
00914 
00915 
00916 Constant *BitcodeReaderValueList::getConstantFwdRef(unsigned Idx,
00917                                                     Type *Ty) {
00918   if (Idx >= size())
00919     resize(Idx + 1);
00920 
00921   if (Value *V = ValuePtrs[Idx]) {
00922     if (Ty != V->getType())
00923       report_fatal_error("Type mismatch in constant table!");
00924     return cast<Constant>(V);
00925   }
00926 
00927   // Create and return a placeholder, which will later be RAUW'd.
00928   Constant *C = new ConstantPlaceHolder(Ty, Context);
00929   ValuePtrs[Idx] = C;
00930   return C;
00931 }
00932 
00933 Value *BitcodeReaderValueList::getValueFwdRef(unsigned Idx, Type *Ty) {
00934   // Bail out for a clearly invalid value. This would make us call resize(0)
00935   if (Idx == UINT_MAX)
00936     return nullptr;
00937 
00938   if (Idx >= size())
00939     resize(Idx + 1);
00940 
00941   if (Value *V = ValuePtrs[Idx]) {
00942     // If the types don't match, it's invalid.
00943     if (Ty && Ty != V->getType())
00944       return nullptr;
00945     return V;
00946   }
00947 
00948   // No type specified, must be invalid reference.
00949   if (!Ty) return nullptr;
00950 
00951   // Create and return a placeholder, which will later be RAUW'd.
00952   Value *V = new Argument(Ty);
00953   ValuePtrs[Idx] = V;
00954   return V;
00955 }
00956 
00957 /// Once all constants are read, this method bulk resolves any forward
00958 /// references.  The idea behind this is that we sometimes get constants (such
00959 /// as large arrays) which reference *many* forward ref constants.  Replacing
00960 /// each of these causes a lot of thrashing when building/reuniquing the
00961 /// constant.  Instead of doing this, we look at all the uses and rewrite all
00962 /// the place holders at once for any constant that uses a placeholder.
00963 void BitcodeReaderValueList::resolveConstantForwardRefs() {
00964   // Sort the values by-pointer so that they are efficient to look up with a
00965   // binary search.
00966   std::sort(ResolveConstants.begin(), ResolveConstants.end());
00967 
00968   SmallVector<Constant*, 64> NewOps;
00969 
00970   while (!ResolveConstants.empty()) {
00971     Value *RealVal = operator[](ResolveConstants.back().second);
00972     Constant *Placeholder = ResolveConstants.back().first;
00973     ResolveConstants.pop_back();
00974 
00975     // Loop over all users of the placeholder, updating them to reference the
00976     // new value.  If they reference more than one placeholder, update them all
00977     // at once.
00978     while (!Placeholder->use_empty()) {
00979       auto UI = Placeholder->user_begin();
00980       User *U = *UI;
00981 
00982       // If the using object isn't uniqued, just update the operands.  This
00983       // handles instructions and initializers for global variables.
00984       if (!isa<Constant>(U) || isa<GlobalValue>(U)) {
00985         UI.getUse().set(RealVal);
00986         continue;
00987       }
00988 
00989       // Otherwise, we have a constant that uses the placeholder.  Replace that
00990       // constant with a new constant that has *all* placeholder uses updated.
00991       Constant *UserC = cast<Constant>(U);
00992       for (User::op_iterator I = UserC->op_begin(), E = UserC->op_end();
00993            I != E; ++I) {
00994         Value *NewOp;
00995         if (!isa<ConstantPlaceHolder>(*I)) {
00996           // Not a placeholder reference.
00997           NewOp = *I;
00998         } else if (*I == Placeholder) {
00999           // Common case is that it just references this one placeholder.
01000           NewOp = RealVal;
01001         } else {
01002           // Otherwise, look up the placeholder in ResolveConstants.
01003           ResolveConstantsTy::iterator It =
01004             std::lower_bound(ResolveConstants.begin(), ResolveConstants.end(),
01005                              std::pair<Constant*, unsigned>(cast<Constant>(*I),
01006                                                             0));
01007           assert(It != ResolveConstants.end() && It->first == *I);
01008           NewOp = operator[](It->second);
01009         }
01010 
01011         NewOps.push_back(cast<Constant>(NewOp));
01012       }
01013 
01014       // Make the new constant.
01015       Constant *NewC;
01016       if (ConstantArray *UserCA = dyn_cast<ConstantArray>(UserC)) {
01017         NewC = ConstantArray::get(UserCA->getType(), NewOps);
01018       } else if (ConstantStruct *UserCS = dyn_cast<ConstantStruct>(UserC)) {
01019         NewC = ConstantStruct::get(UserCS->getType(), NewOps);
01020       } else if (isa<ConstantVector>(UserC)) {
01021         NewC = ConstantVector::get(NewOps);
01022       } else {
01023         assert(isa<ConstantExpr>(UserC) && "Must be a ConstantExpr.");
01024         NewC = cast<ConstantExpr>(UserC)->getWithOperands(NewOps);
01025       }
01026 
01027       UserC->replaceAllUsesWith(NewC);
01028       UserC->destroyConstant();
01029       NewOps.clear();
01030     }
01031 
01032     // Update all ValueHandles, they should be the only users at this point.
01033     Placeholder->replaceAllUsesWith(RealVal);
01034     delete Placeholder;
01035   }
01036 }
01037 
01038 void BitcodeReaderMetadataList::assignValue(Metadata *MD, unsigned Idx) {
01039   if (Idx == size()) {
01040     push_back(MD);
01041     return;
01042   }
01043 
01044   if (Idx >= size())
01045     resize(Idx+1);
01046 
01047   TrackingMDRef &OldMD = MetadataPtrs[Idx];
01048   if (!OldMD) {
01049     OldMD.reset(MD);
01050     return;
01051   }
01052 
01053   // If there was a forward reference to this value, replace it.
01054   TempMDTuple PrevMD(cast<MDTuple>(OldMD.get()));
01055   PrevMD->replaceAllUsesWith(MD);
01056   --NumFwdRefs;
01057 }
01058 
01059 Metadata *BitcodeReaderMetadataList::getValueFwdRef(unsigned Idx) {
01060   if (Idx >= size())
01061     resize(Idx + 1);
01062 
01063   if (Metadata *MD = MetadataPtrs[Idx])
01064     return MD;
01065 
01066   // Track forward refs to be resolved later.
01067   if (AnyFwdRefs) {
01068     MinFwdRef = std::min(MinFwdRef, Idx);
01069     MaxFwdRef = std::max(MaxFwdRef, Idx);
01070   } else {
01071     AnyFwdRefs = true;
01072     MinFwdRef = MaxFwdRef = Idx;
01073   }
01074   ++NumFwdRefs;
01075 
01076   // Create and return a placeholder, which will later be RAUW'd.
01077   Metadata *MD = MDNode::getTemporary(Context, None).release();
01078   MetadataPtrs[Idx].reset(MD);
01079   return MD;
01080 }
01081 
01082 void BitcodeReaderMetadataList::tryToResolveCycles() {
01083   if (!AnyFwdRefs)
01084     // Nothing to do.
01085     return;
01086 
01087   if (NumFwdRefs)
01088     // Still forward references... can't resolve cycles.
01089     return;
01090 
01091   // Resolve any cycles.
01092   for (unsigned I = MinFwdRef, E = MaxFwdRef + 1; I != E; ++I) {
01093     auto &MD = MetadataPtrs[I];
01094     auto *N = dyn_cast_or_null<MDNode>(MD);
01095     if (!N)
01096       continue;
01097 
01098     assert(!N->isTemporary() && "Unexpected forward reference");
01099     N->resolveCycles();
01100   }
01101 
01102   // Make sure we return early again until there's another forward ref.
01103   AnyFwdRefs = false;
01104 }
01105 
01106 Type *BitcodeReader::getTypeByID(unsigned ID) {
01107   // The type table size is always specified correctly.
01108   if (ID >= TypeList.size())
01109     return nullptr;
01110 
01111   if (Type *Ty = TypeList[ID])
01112     return Ty;
01113 
01114   // If we have a forward reference, the only possible case is when it is to a
01115   // named struct.  Just create a placeholder for now.
01116   return TypeList[ID] = createIdentifiedStructType(Context);
01117 }
01118 
01119 StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context,
01120                                                       StringRef Name) {
01121   auto *Ret = StructType::create(Context, Name);
01122   IdentifiedStructTypes.push_back(Ret);
01123   return Ret;
01124 }
01125 
01126 StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context) {
01127   auto *Ret = StructType::create(Context);
01128   IdentifiedStructTypes.push_back(Ret);
01129   return Ret;
01130 }
01131 
01132 
01133 //===----------------------------------------------------------------------===//
01134 //  Functions for parsing blocks from the bitcode file
01135 //===----------------------------------------------------------------------===//
01136 
01137 
01138 /// \brief This fills an AttrBuilder object with the LLVM attributes that have
01139 /// been decoded from the given integer. This function must stay in sync with
01140 /// 'encodeLLVMAttributesForBitcode'.
01141 static void decodeLLVMAttributesForBitcode(AttrBuilder &B,
01142                                            uint64_t EncodedAttrs) {
01143   // FIXME: Remove in 4.0.
01144 
01145   // The alignment is stored as a 16-bit raw value from bits 31--16.  We shift
01146   // the bits above 31 down by 11 bits.
01147   unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
01148   assert((!Alignment || isPowerOf2_32(Alignment)) &&
01149          "Alignment must be a power of two.");
01150 
01151   if (Alignment)
01152     B.addAlignmentAttr(Alignment);
01153   B.addRawValue(((EncodedAttrs & (0xfffffULL << 32)) >> 11) |
01154                 (EncodedAttrs & 0xffff));
01155 }
01156 
01157 std::error_code BitcodeReader::parseAttributeBlock() {
01158   if (Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID))
01159     return error("Invalid record");
01160 
01161   if (!MAttributes.empty())
01162     return error("Invalid multiple blocks");
01163 
01164   SmallVector<uint64_t, 64> Record;
01165 
01166   SmallVector<AttributeSet, 8> Attrs;
01167 
01168   // Read all the records.
01169   while (1) {
01170     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
01171 
01172     switch (Entry.Kind) {
01173     case BitstreamEntry::SubBlock: // Handled for us already.
01174     case BitstreamEntry::Error:
01175       return error("Malformed block");
01176     case BitstreamEntry::EndBlock:
01177       return std::error_code();
01178     case BitstreamEntry::Record:
01179       // The interesting case.
01180       break;
01181     }
01182 
01183     // Read a record.
01184     Record.clear();
01185     switch (Stream.readRecord(Entry.ID, Record)) {
01186     default:  // Default behavior: ignore.
01187       break;
01188     case bitc::PARAMATTR_CODE_ENTRY_OLD: { // ENTRY: [paramidx0, attr0, ...]
01189       // FIXME: Remove in 4.0.
01190       if (Record.size() & 1)
01191         return error("Invalid record");
01192 
01193       for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
01194         AttrBuilder B;
01195         decodeLLVMAttributesForBitcode(B, Record[i+1]);
01196         Attrs.push_back(AttributeSet::get(Context, Record[i], B));
01197       }
01198 
01199       MAttributes.push_back(AttributeSet::get(Context, Attrs));
01200       Attrs.clear();
01201       break;
01202     }
01203     case bitc::PARAMATTR_CODE_ENTRY: { // ENTRY: [attrgrp0, attrgrp1, ...]
01204       for (unsigned i = 0, e = Record.size(); i != e; ++i)
01205         Attrs.push_back(MAttributeGroups[Record[i]]);
01206 
01207       MAttributes.push_back(AttributeSet::get(Context, Attrs));
01208       Attrs.clear();
01209       break;
01210     }
01211     }
01212   }
01213 }
01214 
01215 // Returns Attribute::None on unrecognized codes.
01216 static Attribute::AttrKind getAttrFromCode(uint64_t Code) {
01217   switch (Code) {
01218   default:
01219     return Attribute::None;
01220   case bitc::ATTR_KIND_ALIGNMENT:
01221     return Attribute::Alignment;
01222   case bitc::ATTR_KIND_ALWAYS_INLINE:
01223     return Attribute::AlwaysInline;
01224   case bitc::ATTR_KIND_ARGMEMONLY:
01225     return Attribute::ArgMemOnly;
01226   case bitc::ATTR_KIND_BUILTIN:
01227     return Attribute::Builtin;
01228   case bitc::ATTR_KIND_BY_VAL:
01229     return Attribute::ByVal;
01230   case bitc::ATTR_KIND_IN_ALLOCA:
01231     return Attribute::InAlloca;
01232   case bitc::ATTR_KIND_COLD:
01233     return Attribute::Cold;
01234   case bitc::ATTR_KIND_CONVERGENT:
01235     return Attribute::Convergent;
01236   case bitc::ATTR_KIND_INACCESSIBLEMEM_ONLY:
01237     return Attribute::InaccessibleMemOnly;
01238   case bitc::ATTR_KIND_INACCESSIBLEMEM_OR_ARGMEMONLY:
01239     return Attribute::InaccessibleMemOrArgMemOnly;
01240   case bitc::ATTR_KIND_INLINE_HINT:
01241     return Attribute::InlineHint;
01242   case bitc::ATTR_KIND_IN_REG:
01243     return Attribute::InReg;
01244   case bitc::ATTR_KIND_JUMP_TABLE:
01245     return Attribute::JumpTable;
01246   case bitc::ATTR_KIND_MIN_SIZE:
01247     return Attribute::MinSize;
01248   case bitc::ATTR_KIND_NAKED:
01249     return Attribute::Naked;
01250   case bitc::ATTR_KIND_NEST:
01251     return Attribute::Nest;
01252   case bitc::ATTR_KIND_NO_ALIAS:
01253     return Attribute::NoAlias;
01254   case bitc::ATTR_KIND_NO_BUILTIN:
01255     return Attribute::NoBuiltin;
01256   case bitc::ATTR_KIND_NO_CAPTURE:
01257     return Attribute::NoCapture;
01258   case bitc::ATTR_KIND_NO_DUPLICATE:
01259     return Attribute::NoDuplicate;
01260   case bitc::ATTR_KIND_NO_IMPLICIT_FLOAT:
01261     return Attribute::NoImplicitFloat;
01262   case bitc::ATTR_KIND_NO_INLINE:
01263     return Attribute::NoInline;
01264   case bitc::ATTR_KIND_NO_RECURSE:
01265     return Attribute::NoRecurse;
01266   case bitc::ATTR_KIND_NON_LAZY_BIND:
01267     return Attribute::NonLazyBind;
01268   case bitc::ATTR_KIND_NON_NULL:
01269     return Attribute::NonNull;
01270   case bitc::ATTR_KIND_DEREFERENCEABLE:
01271     return Attribute::Dereferenceable;
01272   case bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL:
01273     return Attribute::DereferenceableOrNull;
01274   case bitc::ATTR_KIND_NO_RED_ZONE:
01275     return Attribute::NoRedZone;
01276   case bitc::ATTR_KIND_NO_RETURN:
01277     return Attribute::NoReturn;
01278   case bitc::ATTR_KIND_NO_UNWIND:
01279     return Attribute::NoUnwind;
01280   case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE:
01281     return Attribute::OptimizeForSize;
01282   case bitc::ATTR_KIND_OPTIMIZE_NONE:
01283     return Attribute::OptimizeNone;
01284   case bitc::ATTR_KIND_READ_NONE:
01285     return Attribute::ReadNone;
01286   case bitc::ATTR_KIND_READ_ONLY:
01287     return Attribute::ReadOnly;
01288   case bitc::ATTR_KIND_RETURNED:
01289     return Attribute::Returned;
01290   case bitc::ATTR_KIND_RETURNS_TWICE:
01291     return Attribute::ReturnsTwice;
01292   case bitc::ATTR_KIND_S_EXT:
01293     return Attribute::SExt;
01294   case bitc::ATTR_KIND_STACK_ALIGNMENT:
01295     return Attribute::StackAlignment;
01296   case bitc::ATTR_KIND_STACK_PROTECT:
01297     return Attribute::StackProtect;
01298   case bitc::ATTR_KIND_STACK_PROTECT_REQ:
01299     return Attribute::StackProtectReq;
01300   case bitc::ATTR_KIND_STACK_PROTECT_STRONG:
01301     return Attribute::StackProtectStrong;
01302   case bitc::ATTR_KIND_SAFESTACK:
01303     return Attribute::SafeStack;
01304   case bitc::ATTR_KIND_STRUCT_RET:
01305     return Attribute::StructRet;
01306   case bitc::ATTR_KIND_SANITIZE_ADDRESS:
01307     return Attribute::SanitizeAddress;
01308   case bitc::ATTR_KIND_SANITIZE_THREAD:
01309     return Attribute::SanitizeThread;
01310   case bitc::ATTR_KIND_SANITIZE_MEMORY:
01311     return Attribute::SanitizeMemory;
01312   case bitc::ATTR_KIND_UW_TABLE:
01313     return Attribute::UWTable;
01314   case bitc::ATTR_KIND_Z_EXT:
01315     return Attribute::ZExt;
01316   }
01317 }
01318 
01319 std::error_code BitcodeReader::parseAlignmentValue(uint64_t Exponent,
01320                                                    unsigned &Alignment) {
01321   // Note: Alignment in bitcode files is incremented by 1, so that zero
01322   // can be used for default alignment.
01323   if (Exponent > Value::MaxAlignmentExponent + 1)
01324     return error("Invalid alignment value");
01325   Alignment = (1 << static_cast<unsigned>(Exponent)) >> 1;
01326   return std::error_code();
01327 }
01328 
01329 std::error_code BitcodeReader::parseAttrKind(uint64_t Code,
01330                                              Attribute::AttrKind *Kind) {
01331   *Kind = getAttrFromCode(Code);
01332   if (*Kind == Attribute::None)
01333     return error(BitcodeError::CorruptedBitcode,
01334                  "Unknown attribute kind (" + Twine(Code) + ")");
01335   return std::error_code();
01336 }
01337 
01338 std::error_code BitcodeReader::parseAttributeGroupBlock() {
01339   if (Stream.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID))
01340     return error("Invalid record");
01341 
01342   if (!MAttributeGroups.empty())
01343     return error("Invalid multiple blocks");
01344 
01345   SmallVector<uint64_t, 64> Record;
01346 
01347   // Read all the records.
01348   while (1) {
01349     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
01350 
01351     switch (Entry.Kind) {
01352     case BitstreamEntry::SubBlock: // Handled for us already.
01353     case BitstreamEntry::Error:
01354       return error("Malformed block");
01355     case BitstreamEntry::EndBlock:
01356       return std::error_code();
01357     case BitstreamEntry::Record:
01358       // The interesting case.
01359       break;
01360     }
01361 
01362     // Read a record.
01363     Record.clear();
01364     switch (Stream.readRecord(Entry.ID, Record)) {
01365     default:  // Default behavior: ignore.
01366       break;
01367     case bitc::PARAMATTR_GRP_CODE_ENTRY: { // ENTRY: [grpid, idx, a0, a1, ...]
01368       if (Record.size() < 3)
01369         return error("Invalid record");
01370 
01371       uint64_t GrpID = Record[0];
01372       uint64_t Idx = Record[1]; // Index of the object this attribute refers to.
01373 
01374       AttrBuilder B;
01375       for (unsigned i = 2, e = Record.size(); i != e; ++i) {
01376         if (Record[i] == 0) {        // Enum attribute
01377           Attribute::AttrKind Kind;
01378           if (std::error_code EC = parseAttrKind(Record[++i], &Kind))
01379             return EC;
01380 
01381           B.addAttribute(Kind);
01382         } else if (Record[i] == 1) { // Integer attribute
01383           Attribute::AttrKind Kind;
01384           if (std::error_code EC = parseAttrKind(Record[++i], &Kind))
01385             return EC;
01386           if (Kind == Attribute::Alignment)
01387             B.addAlignmentAttr(Record[++i]);
01388           else if (Kind == Attribute::StackAlignment)
01389             B.addStackAlignmentAttr(Record[++i]);
01390           else if (Kind == Attribute::Dereferenceable)
01391             B.addDereferenceableAttr(Record[++i]);
01392           else if (Kind == Attribute::DereferenceableOrNull)
01393             B.addDereferenceableOrNullAttr(Record[++i]);
01394         } else {                     // String attribute
01395           assert((Record[i] == 3 || Record[i] == 4) &&
01396                  "Invalid attribute group entry");
01397           bool HasValue = (Record[i++] == 4);
01398           SmallString<64> KindStr;
01399           SmallString<64> ValStr;
01400 
01401           while (Record[i] != 0 && i != e)
01402             KindStr += Record[i++];
01403           assert(Record[i] == 0 && "Kind string not null terminated");
01404 
01405           if (HasValue) {
01406             // Has a value associated with it.
01407             ++i; // Skip the '0' that terminates the "kind" string.
01408             while (Record[i] != 0 && i != e)
01409               ValStr += Record[i++];
01410             assert(Record[i] == 0 && "Value string not null terminated");
01411           }
01412 
01413           B.addAttribute(KindStr.str(), ValStr.str());
01414         }
01415       }
01416 
01417       MAttributeGroups[GrpID] = AttributeSet::get(Context, Idx, B);
01418       break;
01419     }
01420     }
01421   }
01422 }
01423 
01424 std::error_code BitcodeReader::parseTypeTable() {
01425   if (Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW))
01426     return error("Invalid record");
01427 
01428   return parseTypeTableBody();
01429 }
01430 
01431 std::error_code BitcodeReader::parseTypeTableBody() {
01432   if (!TypeList.empty())
01433     return error("Invalid multiple blocks");
01434 
01435   SmallVector<uint64_t, 64> Record;
01436   unsigned NumRecords = 0;
01437 
01438   SmallString<64> TypeName;
01439 
01440   // Read all the records for this type table.
01441   while (1) {
01442     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
01443 
01444     switch (Entry.Kind) {
01445     case BitstreamEntry::SubBlock: // Handled for us already.
01446     case BitstreamEntry::Error:
01447       return error("Malformed block");
01448     case BitstreamEntry::EndBlock:
01449       if (NumRecords != TypeList.size())
01450         return error("Malformed block");
01451       return std::error_code();
01452     case BitstreamEntry::Record:
01453       // The interesting case.
01454       break;
01455     }
01456 
01457     // Read a record.
01458     Record.clear();
01459     Type *ResultTy = nullptr;
01460     switch (Stream.readRecord(Entry.ID, Record)) {
01461     default:
01462       return error("Invalid value");
01463     case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries]
01464       // TYPE_CODE_NUMENTRY contains a count of the number of types in the
01465       // type list.  This allows us to reserve space.
01466       if (Record.size() < 1)
01467         return error("Invalid record");
01468       TypeList.resize(Record[0]);
01469       continue;
01470     case bitc::TYPE_CODE_VOID:      // VOID
01471       ResultTy = Type::getVoidTy(Context);
01472       break;
01473     case bitc::TYPE_CODE_HALF:     // HALF
01474       ResultTy = Type::getHalfTy(Context);
01475       break;
01476     case bitc::TYPE_CODE_FLOAT:     // FLOAT
01477       ResultTy = Type::getFloatTy(Context);
01478       break;
01479     case bitc::TYPE_CODE_DOUBLE:    // DOUBLE
01480       ResultTy = Type::getDoubleTy(Context);
01481       break;
01482     case bitc::TYPE_CODE_X86_FP80:  // X86_FP80
01483       ResultTy = Type::getX86_FP80Ty(Context);
01484       break;
01485     case bitc::TYPE_CODE_FP128:     // FP128
01486       ResultTy = Type::getFP128Ty(Context);
01487       break;
01488     case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128
01489       ResultTy = Type::getPPC_FP128Ty(Context);
01490       break;
01491     case bitc::TYPE_CODE_LABEL:     // LABEL
01492       ResultTy = Type::getLabelTy(Context);
01493       break;
01494     case bitc::TYPE_CODE_METADATA:  // METADATA
01495       ResultTy = Type::getMetadataTy(Context);
01496       break;
01497     case bitc::TYPE_CODE_X86_MMX:   // X86_MMX
01498       ResultTy = Type::getX86_MMXTy(Context);
01499       break;
01500     case bitc::TYPE_CODE_TOKEN:     // TOKEN
01501       ResultTy = Type::getTokenTy(Context);
01502       break;
01503     case bitc::TYPE_CODE_INTEGER: { // INTEGER: [width]
01504       if (Record.size() < 1)
01505         return error("Invalid record");
01506 
01507       uint64_t NumBits = Record[0];
01508       if (NumBits < IntegerType::MIN_INT_BITS ||
01509           NumBits > IntegerType::MAX_INT_BITS)
01510         return error("Bitwidth for integer type out of range");
01511       ResultTy = IntegerType::get(Context, NumBits);
01512       break;
01513     }
01514     case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or
01515                                     //          [pointee type, address space]
01516       if (Record.size() < 1)
01517         return error("Invalid record");
01518       unsigned AddressSpace = 0;
01519       if (Record.size() == 2)
01520         AddressSpace = Record[1];
01521       ResultTy = getTypeByID(Record[0]);
01522       if (!ResultTy ||
01523           !PointerType::isValidElementType(ResultTy))
01524         return error("Invalid type");
01525       ResultTy = PointerType::get(ResultTy, AddressSpace);
01526       break;
01527     }
01528     case bitc::TYPE_CODE_FUNCTION_OLD: {
01529       // FIXME: attrid is dead, remove it in LLVM 4.0
01530       // FUNCTION: [vararg, attrid, retty, paramty x N]
01531       if (Record.size() < 3)
01532         return error("Invalid record");
01533       SmallVector<Type*, 8> ArgTys;
01534       for (unsigned i = 3, e = Record.size(); i != e; ++i) {
01535         if (Type *T = getTypeByID(Record[i]))
01536           ArgTys.push_back(T);
01537         else
01538           break;
01539       }
01540 
01541       ResultTy = getTypeByID(Record[2]);
01542       if (!ResultTy || ArgTys.size() < Record.size()-3)
01543         return error("Invalid type");
01544 
01545       ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
01546       break;
01547     }
01548     case bitc::TYPE_CODE_FUNCTION: {
01549       // FUNCTION: [vararg, retty, paramty x N]
01550       if (Record.size() < 2)
01551         return error("Invalid record");
01552       SmallVector<Type*, 8> ArgTys;
01553       for (unsigned i = 2, e = Record.size(); i != e; ++i) {
01554         if (Type *T = getTypeByID(Record[i])) {
01555           if (!FunctionType::isValidArgumentType(T))
01556             return error("Invalid function argument type");
01557           ArgTys.push_back(T);
01558         }
01559         else
01560           break;
01561       }
01562 
01563       ResultTy = getTypeByID(Record[1]);
01564       if (!ResultTy || ArgTys.size() < Record.size()-2)
01565         return error("Invalid type");
01566 
01567       ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
01568       break;
01569     }
01570     case bitc::TYPE_CODE_STRUCT_ANON: {  // STRUCT: [ispacked, eltty x N]
01571       if (Record.size() < 1)
01572         return error("Invalid record");
01573       SmallVector<Type*, 8> EltTys;
01574       for (unsigned i = 1, e = Record.size(); i != e; ++i) {
01575         if (Type *T = getTypeByID(Record[i]))
01576           EltTys.push_back(T);
01577         else
01578           break;
01579       }
01580       if (EltTys.size() != Record.size()-1)
01581         return error("Invalid type");
01582       ResultTy = StructType::get(Context, EltTys, Record[0]);
01583       break;
01584     }
01585     case bitc::TYPE_CODE_STRUCT_NAME:   // STRUCT_NAME: [strchr x N]
01586       if (convertToString(Record, 0, TypeName))
01587         return error("Invalid record");
01588       continue;
01589 
01590     case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N]
01591       if (Record.size() < 1)
01592         return error("Invalid record");
01593 
01594       if (NumRecords >= TypeList.size())
01595         return error("Invalid TYPE table");
01596 
01597       // Check to see if this was forward referenced, if so fill in the temp.
01598       StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
01599       if (Res) {
01600         Res->setName(TypeName);
01601         TypeList[NumRecords] = nullptr;
01602       } else  // Otherwise, create a new struct.
01603         Res = createIdentifiedStructType(Context, TypeName);
01604       TypeName.clear();
01605 
01606       SmallVector<Type*, 8> EltTys;
01607       for (unsigned i = 1, e = Record.size(); i != e; ++i) {
01608         if (Type *T = getTypeByID(Record[i]))
01609           EltTys.push_back(T);
01610         else
01611           break;
01612       }
01613       if (EltTys.size() != Record.size()-1)
01614         return error("Invalid record");
01615       Res->setBody(EltTys, Record[0]);
01616       ResultTy = Res;
01617       break;
01618     }
01619     case bitc::TYPE_CODE_OPAQUE: {       // OPAQUE: []
01620       if (Record.size() != 1)
01621         return error("Invalid record");
01622 
01623       if (NumRecords >= TypeList.size())
01624         return error("Invalid TYPE table");
01625 
01626       // Check to see if this was forward referenced, if so fill in the temp.
01627       StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
01628       if (Res) {
01629         Res->setName(TypeName);
01630         TypeList[NumRecords] = nullptr;
01631       } else  // Otherwise, create a new struct with no body.
01632         Res = createIdentifiedStructType(Context, TypeName);
01633       TypeName.clear();
01634       ResultTy = Res;
01635       break;
01636     }
01637     case bitc::TYPE_CODE_ARRAY:     // ARRAY: [numelts, eltty]
01638       if (Record.size() < 2)
01639         return error("Invalid record");
01640       ResultTy = getTypeByID(Record[1]);
01641       if (!ResultTy || !ArrayType::isValidElementType(ResultTy))
01642         return error("Invalid type");
01643       ResultTy = ArrayType::get(ResultTy, Record[0]);
01644       break;
01645     case bitc::TYPE_CODE_VECTOR:    // VECTOR: [numelts, eltty]
01646       if (Record.size() < 2)
01647         return error("Invalid record");
01648       if (Record[0] == 0)
01649         return error("Invalid vector length");
01650       ResultTy = getTypeByID(Record[1]);
01651       if (!ResultTy || !StructType::isValidElementType(ResultTy))
01652         return error("Invalid type");
01653       ResultTy = VectorType::get(ResultTy, Record[0]);
01654       break;
01655     }
01656 
01657     if (NumRecords >= TypeList.size())
01658       return error("Invalid TYPE table");
01659     if (TypeList[NumRecords])
01660       return error(
01661           "Invalid TYPE table: Only named structs can be forward referenced");
01662     assert(ResultTy && "Didn't read a type?");
01663     TypeList[NumRecords++] = ResultTy;
01664   }
01665 }
01666 
01667 std::error_code BitcodeReader::parseOperandBundleTags() {
01668   if (Stream.EnterSubBlock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID))
01669     return error("Invalid record");
01670 
01671   if (!BundleTags.empty())
01672     return error("Invalid multiple blocks");
01673 
01674   SmallVector<uint64_t, 64> Record;
01675 
01676   while (1) {
01677     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
01678 
01679     switch (Entry.Kind) {
01680     case BitstreamEntry::SubBlock: // Handled for us already.
01681     case BitstreamEntry::Error:
01682       return error("Malformed block");
01683     case BitstreamEntry::EndBlock:
01684       return std::error_code();
01685     case BitstreamEntry::Record:
01686       // The interesting case.
01687       break;
01688     }
01689 
01690     // Tags are implicitly mapped to integers by their order.
01691 
01692     if (Stream.readRecord(Entry.ID, Record) != bitc::OPERAND_BUNDLE_TAG)
01693       return error("Invalid record");
01694 
01695     // OPERAND_BUNDLE_TAG: [strchr x N]
01696     BundleTags.emplace_back();
01697     if (convertToString(Record, 0, BundleTags.back()))
01698       return error("Invalid record");
01699     Record.clear();
01700   }
01701 }
01702 
01703 /// Associate a value with its name from the given index in the provided record.
01704 ErrorOr<Value *> BitcodeReader::recordValue(SmallVectorImpl<uint64_t> &Record,
01705                                             unsigned NameIndex, Triple &TT) {
01706   SmallString<128> ValueName;
01707   if (convertToString(Record, NameIndex, ValueName))
01708     return error("Invalid record");
01709   unsigned ValueID = Record[0];
01710   if (ValueID >= ValueList.size() || !ValueList[ValueID])
01711     return error("Invalid record");
01712   Value *V = ValueList[ValueID];
01713 
01714   StringRef NameStr(ValueName.data(), ValueName.size());
01715   if (NameStr.find_first_of(0) != StringRef::npos)
01716     return error("Invalid value name");
01717   V->setName(NameStr);
01718   auto *GO = dyn_cast<GlobalObject>(V);
01719   if (GO) {
01720     if (GO->getComdat() == reinterpret_cast<Comdat *>(1)) {
01721       if (TT.isOSBinFormatMachO())
01722         GO->setComdat(nullptr);
01723       else
01724         GO->setComdat(TheModule->getOrInsertComdat(V->getName()));
01725     }
01726   }
01727   return V;
01728 }
01729 
01730 /// Parse the value symbol table at either the current parsing location or
01731 /// at the given bit offset if provided.
01732 std::error_code BitcodeReader::parseValueSymbolTable(uint64_t Offset) {
01733   uint64_t CurrentBit;
01734   // Pass in the Offset to distinguish between calling for the module-level
01735   // VST (where we want to jump to the VST offset) and the function-level
01736   // VST (where we don't).
01737   if (Offset > 0) {
01738     // Save the current parsing location so we can jump back at the end
01739     // of the VST read.
01740     CurrentBit = Stream.GetCurrentBitNo();
01741     Stream.JumpToBit(Offset * 32);
01742 #ifndef NDEBUG
01743     // Do some checking if we are in debug mode.
01744     BitstreamEntry Entry = Stream.advance();
01745     assert(Entry.Kind == BitstreamEntry::SubBlock);
01746     assert(Entry.ID == bitc::VALUE_SYMTAB_BLOCK_ID);
01747 #else
01748     // In NDEBUG mode ignore the output so we don't get an unused variable
01749     // warning.
01750     Stream.advance();
01751 #endif
01752   }
01753 
01754   // Compute the delta between the bitcode indices in the VST (the word offset
01755   // to the word-aligned ENTER_SUBBLOCK for the function block, and that
01756   // expected by the lazy reader. The reader's EnterSubBlock expects to have
01757   // already read the ENTER_SUBBLOCK code (size getAbbrevIDWidth) and BlockID
01758   // (size BlockIDWidth). Note that we access the stream's AbbrevID width here
01759   // just before entering the VST subblock because: 1) the EnterSubBlock
01760   // changes the AbbrevID width; 2) the VST block is nested within the same
01761   // outer MODULE_BLOCK as the FUNCTION_BLOCKs and therefore have the same
01762   // AbbrevID width before calling EnterSubBlock; and 3) when we want to
01763   // jump to the FUNCTION_BLOCK using this offset later, we don't want
01764   // to rely on the stream's AbbrevID width being that of the MODULE_BLOCK.
01765   unsigned FuncBitcodeOffsetDelta =
01766       Stream.getAbbrevIDWidth() + bitc::BlockIDWidth;
01767 
01768   if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
01769     return error("Invalid record");
01770 
01771   SmallVector<uint64_t, 64> Record;
01772 
01773   Triple TT(TheModule->getTargetTriple());
01774 
01775   // Read all the records for this value table.
01776   SmallString<128> ValueName;
01777   while (1) {
01778     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
01779 
01780     switch (Entry.Kind) {
01781     case BitstreamEntry::SubBlock: // Handled for us already.
01782     case BitstreamEntry::Error:
01783       return error("Malformed block");
01784     case BitstreamEntry::EndBlock:
01785       if (Offset > 0)
01786         Stream.JumpToBit(CurrentBit);
01787       return std::error_code();
01788     case BitstreamEntry::Record:
01789       // The interesting case.
01790       break;
01791     }
01792 
01793     // Read a record.
01794     Record.clear();
01795     switch (Stream.readRecord(Entry.ID, Record)) {
01796     default:  // Default behavior: unknown type.
01797       break;
01798     case bitc::VST_CODE_ENTRY: {  // VST_ENTRY: [valueid, namechar x N]
01799       ErrorOr<Value *> ValOrErr = recordValue(Record, 1, TT);
01800       if (std::error_code EC = ValOrErr.getError())
01801         return EC;
01802       ValOrErr.get();
01803       break;
01804     }
01805     case bitc::VST_CODE_FNENTRY: {
01806       // VST_FNENTRY: [valueid, offset, namechar x N]
01807       ErrorOr<Value *> ValOrErr = recordValue(Record, 2, TT);
01808       if (std::error_code EC = ValOrErr.getError())
01809         return EC;
01810       Value *V = ValOrErr.get();
01811 
01812       auto *GO = dyn_cast<GlobalObject>(V);
01813       if (!GO) {
01814         // If this is an alias, need to get the actual Function object
01815         // it aliases, in order to set up the DeferredFunctionInfo entry below.
01816         auto *GA = dyn_cast<GlobalAlias>(V);
01817         if (GA)
01818           GO = GA->getBaseObject();
01819         assert(GO);
01820       }
01821 
01822       uint64_t FuncWordOffset = Record[1];
01823       Function *F = dyn_cast<Function>(GO);
01824       assert(F);
01825       uint64_t FuncBitOffset = FuncWordOffset * 32;
01826       DeferredFunctionInfo[F] = FuncBitOffset + FuncBitcodeOffsetDelta;
01827       // Set the LastFunctionBlockBit to point to the last function block.
01828       // Later when parsing is resumed after function materialization,
01829       // we can simply skip that last function block.
01830       if (FuncBitOffset > LastFunctionBlockBit)
01831         LastFunctionBlockBit = FuncBitOffset;
01832       break;
01833     }
01834     case bitc::VST_CODE_BBENTRY: {
01835       if (convertToString(Record, 1, ValueName))
01836         return error("Invalid record");
01837       BasicBlock *BB = getBasicBlock(Record[0]);
01838       if (!BB)
01839         return error("Invalid record");
01840 
01841       BB->setName(StringRef(ValueName.data(), ValueName.size()));
01842       ValueName.clear();
01843       break;
01844     }
01845     }
01846   }
01847 }
01848 
01849 /// Parse a single METADATA_KIND record, inserting result in MDKindMap.
01850 std::error_code
01851 BitcodeReader::parseMetadataKindRecord(SmallVectorImpl<uint64_t> &Record) {
01852   if (Record.size() < 2)
01853     return error("Invalid record");
01854 
01855   unsigned Kind = Record[0];
01856   SmallString<8> Name(Record.begin() + 1, Record.end());
01857 
01858   unsigned NewKind = TheModule->getMDKindID(Name.str());
01859   if (!MDKindMap.insert(std::make_pair(Kind, NewKind)).second)
01860     return error("Conflicting METADATA_KIND records");
01861   return std::error_code();
01862 }
01863 
01864 static int64_t unrotateSign(uint64_t U) { return U & 1 ? ~(U >> 1) : U >> 1; }
01865 
01866 /// Parse a METADATA_BLOCK. If ModuleLevel is true then we are parsing
01867 /// module level metadata.
01868 std::error_code BitcodeReader::parseMetadata(bool ModuleLevel) {
01869   IsMetadataMaterialized = true;
01870   unsigned NextMetadataNo = MetadataList.size();
01871   if (ModuleLevel && SeenModuleValuesRecord) {
01872     // Now that we are parsing the module level metadata, we want to restart
01873     // the numbering of the MD values, and replace temp MD created earlier
01874     // with their real values. If we saw a METADATA_VALUE record then we
01875     // would have set the MetadataList size to the number specified in that
01876     // record, to support parsing function-level metadata first, and we need
01877     // to reset back to 0 to fill the MetadataList in with the parsed module
01878     // The function-level metadata parsing should have reset the MetadataList
01879     // size back to the value reported by the METADATA_VALUE record, saved in
01880     // NumModuleMDs.
01881     assert(NumModuleMDs == MetadataList.size() &&
01882            "Expected MetadataList to only contain module level values");
01883     NextMetadataNo = 0;
01884   }
01885 
01886   if (Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID))
01887     return error("Invalid record");
01888 
01889   SmallVector<uint64_t, 64> Record;
01890 
01891   auto getMD = [&](unsigned ID) -> Metadata * {
01892     return MetadataList.getValueFwdRef(ID);
01893   };
01894   auto getMDOrNull = [&](unsigned ID) -> Metadata *{
01895     if (ID)
01896       return getMD(ID - 1);
01897     return nullptr;
01898   };
01899   auto getMDString = [&](unsigned ID) -> MDString *{
01900     // This requires that the ID is not really a forward reference.  In
01901     // particular, the MDString must already have been resolved.
01902     return cast_or_null<MDString>(getMDOrNull(ID));
01903   };
01904 
01905 #define GET_OR_DISTINCT(CLASS, DISTINCT, ARGS)                                 \
01906   (DISTINCT ? CLASS::getDistinct ARGS : CLASS::get ARGS)
01907 
01908   // Read all the records.
01909   while (1) {
01910     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
01911 
01912     switch (Entry.Kind) {
01913     case BitstreamEntry::SubBlock: // Handled for us already.
01914     case BitstreamEntry::Error:
01915       return error("Malformed block");
01916     case BitstreamEntry::EndBlock:
01917       MetadataList.tryToResolveCycles();
01918       assert((!(ModuleLevel && SeenModuleValuesRecord) ||
01919               NumModuleMDs == MetadataList.size()) &&
01920              "Inconsistent bitcode: METADATA_VALUES mismatch");
01921       return std::error_code();
01922     case BitstreamEntry::Record:
01923       // The interesting case.
01924       break;
01925     }
01926 
01927     // Read a record.
01928     Record.clear();
01929     unsigned Code = Stream.readRecord(Entry.ID, Record);
01930     bool IsDistinct = false;
01931     switch (Code) {
01932     default:  // Default behavior: ignore.
01933       break;
01934     case bitc::METADATA_NAME: {
01935       // Read name of the named metadata.
01936       SmallString<8> Name(Record.begin(), Record.end());
01937       Record.clear();
01938       Code = Stream.ReadCode();
01939 
01940       unsigned NextBitCode = Stream.readRecord(Code, Record);
01941       if (NextBitCode != bitc::METADATA_NAMED_NODE)
01942         return error("METADATA_NAME not followed by METADATA_NAMED_NODE");
01943 
01944       // Read named metadata elements.
01945       unsigned Size = Record.size();
01946       NamedMDNode *NMD = TheModule->getOrInsertNamedMetadata(Name);
01947       for (unsigned i = 0; i != Size; ++i) {
01948         MDNode *MD =
01949             dyn_cast_or_null<MDNode>(MetadataList.getValueFwdRef(Record[i]));
01950         if (!MD)
01951           return error("Invalid record");
01952         NMD->addOperand(MD);
01953       }
01954       break;
01955     }
01956     case bitc::METADATA_OLD_FN_NODE: {
01957       // FIXME: Remove in 4.0.
01958       // This is a LocalAsMetadata record, the only type of function-local
01959       // metadata.
01960       if (Record.size() % 2 == 1)
01961         return error("Invalid record");
01962 
01963       // If this isn't a LocalAsMetadata record, we're dropping it.  This used
01964       // to be legal, but there's no upgrade path.
01965       auto dropRecord = [&] {
01966         MetadataList.assignValue(MDNode::get(Context, None), NextMetadataNo++);
01967       };
01968       if (Record.size() != 2) {
01969         dropRecord();
01970         break;
01971       }
01972 
01973       Type *Ty = getTypeByID(Record[0]);
01974       if (Ty->isMetadataTy() || Ty->isVoidTy()) {
01975         dropRecord();
01976         break;
01977       }
01978 
01979       MetadataList.assignValue(
01980           LocalAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)),
01981           NextMetadataNo++);
01982       break;
01983     }
01984     case bitc::METADATA_OLD_NODE: {
01985       // FIXME: Remove in 4.0.
01986       if (Record.size() % 2 == 1)
01987         return error("Invalid record");
01988 
01989       unsigned Size = Record.size();
01990       SmallVector<Metadata *, 8> Elts;
01991       for (unsigned i = 0; i != Size; i += 2) {
01992         Type *Ty = getTypeByID(Record[i]);
01993         if (!Ty)
01994           return error("Invalid record");
01995         if (Ty->isMetadataTy())
01996           Elts.push_back(MetadataList.getValueFwdRef(Record[i + 1]));
01997         else if (!Ty->isVoidTy()) {
01998           auto *MD =
01999               ValueAsMetadata::get(ValueList.getValueFwdRef(Record[i + 1], Ty));
02000           assert(isa<ConstantAsMetadata>(MD) &&
02001                  "Expected non-function-local metadata");
02002           Elts.push_back(MD);
02003         } else
02004           Elts.push_back(nullptr);
02005       }
02006       MetadataList.assignValue(MDNode::get(Context, Elts), NextMetadataNo++);
02007       break;
02008     }
02009     case bitc::METADATA_VALUE: {
02010       if (Record.size() != 2)
02011         return error("Invalid record");
02012 
02013       Type *Ty = getTypeByID(Record[0]);
02014       if (Ty->isMetadataTy() || Ty->isVoidTy())
02015         return error("Invalid record");
02016 
02017       MetadataList.assignValue(
02018           ValueAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)),
02019           NextMetadataNo++);
02020       break;
02021     }
02022     case bitc::METADATA_DISTINCT_NODE:
02023       IsDistinct = true;
02024       // fallthrough...
02025     case bitc::METADATA_NODE: {
02026       SmallVector<Metadata *, 8> Elts;
02027       Elts.reserve(Record.size());
02028       for (unsigned ID : Record)
02029         Elts.push_back(ID ? MetadataList.getValueFwdRef(ID - 1) : nullptr);
02030       MetadataList.assignValue(IsDistinct ? MDNode::getDistinct(Context, Elts)
02031                                           : MDNode::get(Context, Elts),
02032                                NextMetadataNo++);
02033       break;
02034     }
02035     case bitc::METADATA_LOCATION: {
02036       if (Record.size() != 5)
02037         return error("Invalid record");
02038 
02039       unsigned Line = Record[1];
02040       unsigned Column = Record[2];
02041       MDNode *Scope = cast<MDNode>(MetadataList.getValueFwdRef(Record[3]));
02042       Metadata *InlinedAt =
02043           Record[4] ? MetadataList.getValueFwdRef(Record[4] - 1) : nullptr;
02044       MetadataList.assignValue(
02045           GET_OR_DISTINCT(DILocation, Record[0],
02046                           (Context, Line, Column, Scope, InlinedAt)),
02047           NextMetadataNo++);
02048       break;
02049     }
02050     case bitc::METADATA_GENERIC_DEBUG: {
02051       if (Record.size() < 4)
02052         return error("Invalid record");
02053 
02054       unsigned Tag = Record[1];
02055       unsigned Version = Record[2];
02056 
02057       if (Tag >= 1u << 16 || Version != 0)
02058         return error("Invalid record");
02059 
02060       auto *Header = getMDString(Record[3]);
02061       SmallVector<Metadata *, 8> DwarfOps;
02062       for (unsigned I = 4, E = Record.size(); I != E; ++I)
02063         DwarfOps.push_back(
02064             Record[I] ? MetadataList.getValueFwdRef(Record[I] - 1) : nullptr);
02065       MetadataList.assignValue(
02066           GET_OR_DISTINCT(GenericDINode, Record[0],
02067                           (Context, Tag, Header, DwarfOps)),
02068           NextMetadataNo++);
02069       break;
02070     }
02071     case bitc::METADATA_SUBRANGE: {
02072       if (Record.size() != 3)
02073         return error("Invalid record");
02074 
02075       MetadataList.assignValue(
02076           GET_OR_DISTINCT(DISubrange, Record[0],
02077                           (Context, Record[1], unrotateSign(Record[2]))),
02078           NextMetadataNo++);
02079       break;
02080     }
02081     case bitc::METADATA_ENUMERATOR: {
02082       if (Record.size() != 3)
02083         return error("Invalid record");
02084 
02085       MetadataList.assignValue(
02086           GET_OR_DISTINCT(
02087               DIEnumerator, Record[0],
02088               (Context, unrotateSign(Record[1]), getMDString(Record[2]))),
02089           NextMetadataNo++);
02090       break;
02091     }
02092     case bitc::METADATA_BASIC_TYPE: {
02093       if (Record.size() != 6)
02094         return error("Invalid record");
02095 
02096       MetadataList.assignValue(
02097           GET_OR_DISTINCT(DIBasicType, Record[0],
02098                           (Context, Record[1], getMDString(Record[2]),
02099                            Record[3], Record[4], Record[5])),
02100           NextMetadataNo++);
02101       break;
02102     }
02103     case bitc::METADATA_DERIVED_TYPE: {
02104       if (Record.size() != 12)
02105         return error("Invalid record");
02106 
02107       MetadataList.assignValue(
02108           GET_OR_DISTINCT(DIDerivedType, Record[0],
02109                           (Context, Record[1], getMDString(Record[2]),
02110                            getMDOrNull(Record[3]), Record[4],
02111                            getMDOrNull(Record[5]), getMDOrNull(Record[6]),
02112                            Record[7], Record[8], Record[9], Record[10],
02113                            getMDOrNull(Record[11]))),
02114           NextMetadataNo++);
02115       break;
02116     }
02117     case bitc::METADATA_COMPOSITE_TYPE: {
02118       if (Record.size() != 16)
02119         return error("Invalid record");
02120 
02121       MetadataList.assignValue(
02122           GET_OR_DISTINCT(DICompositeType, Record[0],
02123                           (Context, Record[1], getMDString(Record[2]),
02124                            getMDOrNull(Record[3]), Record[4],
02125                            getMDOrNull(Record[5]), getMDOrNull(Record[6]),
02126                            Record[7], Record[8], Record[9], Record[10],
02127                            getMDOrNull(Record[11]), Record[12],
02128                            getMDOrNull(Record[13]), getMDOrNull(Record[14]),
02129                            getMDString(Record[15]))),
02130           NextMetadataNo++);
02131       break;
02132     }
02133     case bitc::METADATA_SUBROUTINE_TYPE: {
02134       if (Record.size() != 3)
02135         return error("Invalid record");
02136 
02137       MetadataList.assignValue(
02138           GET_OR_DISTINCT(DISubroutineType, Record[0],
02139                           (Context, Record[1], getMDOrNull(Record[2]))),
02140           NextMetadataNo++);
02141       break;
02142     }
02143 
02144     case bitc::METADATA_MODULE: {
02145       if (Record.size() != 6)
02146         return error("Invalid record");
02147 
02148       MetadataList.assignValue(
02149           GET_OR_DISTINCT(DIModule, Record[0],
02150                           (Context, getMDOrNull(Record[1]),
02151                            getMDString(Record[2]), getMDString(Record[3]),
02152                            getMDString(Record[4]), getMDString(Record[5]))),
02153           NextMetadataNo++);
02154       break;
02155     }
02156 
02157     case bitc::METADATA_FILE: {
02158       if (Record.size() != 3)
02159         return error("Invalid record");
02160 
02161       MetadataList.assignValue(
02162           GET_OR_DISTINCT(DIFile, Record[0], (Context, getMDString(Record[1]),
02163                                               getMDString(Record[2]))),
02164           NextMetadataNo++);
02165       break;
02166     }
02167     case bitc::METADATA_COMPILE_UNIT: {
02168       if (Record.size() < 14 || Record.size() > 16)
02169         return error("Invalid record");
02170 
02171       // Ignore Record[0], which indicates whether this compile unit is
02172       // distinct.  It's always distinct.
02173       MetadataList.assignValue(
02174           DICompileUnit::getDistinct(
02175               Context, Record[1], getMDOrNull(Record[2]),
02176               getMDString(Record[3]), Record[4], getMDString(Record[5]),
02177               Record[6], getMDString(Record[7]), Record[8],
02178               getMDOrNull(Record[9]), getMDOrNull(Record[10]),
02179               getMDOrNull(Record[11]), getMDOrNull(Record[12]),
02180               getMDOrNull(Record[13]),
02181               Record.size() <= 15 ? 0 : getMDOrNull(Record[15]),
02182               Record.size() <= 14 ? 0 : Record[14]),
02183           NextMetadataNo++);
02184       break;
02185     }
02186     case bitc::METADATA_SUBPROGRAM: {
02187       if (Record.size() != 18 && Record.size() != 19)
02188         return error("Invalid record");
02189 
02190       bool HasFn = Record.size() == 19;
02191       DISubprogram *SP = GET_OR_DISTINCT(
02192           DISubprogram,
02193           Record[0] || Record[8], // All definitions should be distinct.
02194           (Context, getMDOrNull(Record[1]), getMDString(Record[2]),
02195            getMDString(Record[3]), getMDOrNull(Record[4]), Record[5],
02196            getMDOrNull(Record[6]), Record[7], Record[8], Record[9],
02197            getMDOrNull(Record[10]), Record[11], Record[12], Record[13],
02198            Record[14], getMDOrNull(Record[15 + HasFn]),
02199            getMDOrNull(Record[16 + HasFn]), getMDOrNull(Record[17 + HasFn])));
02200       MetadataList.assignValue(SP, NextMetadataNo++);
02201 
02202       // Upgrade sp->function mapping to function->sp mapping.
02203       if (HasFn && Record[15]) {
02204         if (auto *CMD = dyn_cast<ConstantAsMetadata>(getMDOrNull(Record[15])))
02205           if (auto *F = dyn_cast<Function>(CMD->getValue())) {
02206             if (F->isMaterializable())
02207               // Defer until materialized; unmaterialized functions may not have
02208               // metadata.
02209               FunctionsWithSPs[F] = SP;
02210             else if (!F->empty())
02211               F->setSubprogram(SP);
02212           }
02213       }
02214       break;
02215     }
02216     case bitc::METADATA_LEXICAL_BLOCK: {
02217       if (Record.size() != 5)
02218         return error("Invalid record");
02219 
02220       MetadataList.assignValue(
02221           GET_OR_DISTINCT(DILexicalBlock, Record[0],
02222                           (Context, getMDOrNull(Record[1]),
02223                            getMDOrNull(Record[2]), Record[3], Record[4])),
02224           NextMetadataNo++);
02225       break;
02226     }
02227     case bitc::METADATA_LEXICAL_BLOCK_FILE: {
02228       if (Record.size() != 4)
02229         return error("Invalid record");
02230 
02231       MetadataList.assignValue(
02232           GET_OR_DISTINCT(DILexicalBlockFile, Record[0],
02233                           (Context, getMDOrNull(Record[1]),
02234                            getMDOrNull(Record[2]), Record[3])),
02235           NextMetadataNo++);
02236       break;
02237     }
02238     case bitc::METADATA_NAMESPACE: {
02239       if (Record.size() != 5)
02240         return error("Invalid record");
02241 
02242       MetadataList.assignValue(
02243           GET_OR_DISTINCT(DINamespace, Record[0],
02244                           (Context, getMDOrNull(Record[1]),
02245                            getMDOrNull(Record[2]), getMDString(Record[3]),
02246                            Record[4])),
02247           NextMetadataNo++);
02248       break;
02249     }
02250     case bitc::METADATA_MACRO: {
02251       if (Record.size() != 5)
02252         return error("Invalid record");
02253 
02254       MetadataList.assignValue(
02255           GET_OR_DISTINCT(DIMacro, Record[0],
02256                           (Context, Record[1], Record[2],
02257                            getMDString(Record[3]), getMDString(Record[4]))),
02258           NextMetadataNo++);
02259       break;
02260     }
02261     case bitc::METADATA_MACRO_FILE: {
02262       if (Record.size() != 5)
02263         return error("Invalid record");
02264 
02265       MetadataList.assignValue(
02266           GET_OR_DISTINCT(DIMacroFile, Record[0],
02267                           (Context, Record[1], Record[2],
02268                            getMDOrNull(Record[3]), getMDOrNull(Record[4]))),
02269           NextMetadataNo++);
02270       break;
02271     }
02272     case bitc::METADATA_TEMPLATE_TYPE: {
02273       if (Record.size() != 3)
02274         return error("Invalid record");
02275 
02276       MetadataList.assignValue(GET_OR_DISTINCT(DITemplateTypeParameter,
02277                                                Record[0],
02278                                                (Context, getMDString(Record[1]),
02279                                                 getMDOrNull(Record[2]))),
02280                                NextMetadataNo++);
02281       break;
02282     }
02283     case bitc::METADATA_TEMPLATE_VALUE: {
02284       if (Record.size() != 5)
02285         return error("Invalid record");
02286 
02287       MetadataList.assignValue(
02288           GET_OR_DISTINCT(DITemplateValueParameter, Record[0],
02289                           (Context, Record[1], getMDString(Record[2]),
02290                            getMDOrNull(Record[3]), getMDOrNull(Record[4]))),
02291           NextMetadataNo++);
02292       break;
02293     }
02294     case bitc::METADATA_GLOBAL_VAR: {
02295       if (Record.size() != 11)
02296         return error("Invalid record");
02297 
02298       MetadataList.assignValue(
02299           GET_OR_DISTINCT(DIGlobalVariable, Record[0],
02300                           (Context, getMDOrNull(Record[1]),
02301                            getMDString(Record[2]), getMDString(Record[3]),
02302                            getMDOrNull(Record[4]), Record[5],
02303                            getMDOrNull(Record[6]), Record[7], Record[8],
02304                            getMDOrNull(Record[9]), getMDOrNull(Record[10]))),
02305           NextMetadataNo++);
02306       break;
02307     }
02308     case bitc::METADATA_LOCAL_VAR: {
02309       // 10th field is for the obseleted 'inlinedAt:' field.
02310       if (Record.size() < 8 || Record.size() > 10)
02311         return error("Invalid record");
02312 
02313       // 2nd field used to be an artificial tag, either DW_TAG_auto_variable or
02314       // DW_TAG_arg_variable.
02315       bool HasTag = Record.size() > 8;
02316       MetadataList.assignValue(
02317           GET_OR_DISTINCT(DILocalVariable, Record[0],
02318                           (Context, getMDOrNull(Record[1 + HasTag]),
02319                            getMDString(Record[2 + HasTag]),
02320                            getMDOrNull(Record[3 + HasTag]), Record[4 + HasTag],
02321                            getMDOrNull(Record[5 + HasTag]), Record[6 + HasTag],
02322                            Record[7 + HasTag])),
02323           NextMetadataNo++);
02324       break;
02325     }
02326     case bitc::METADATA_EXPRESSION: {
02327       if (Record.size() < 1)
02328         return error("Invalid record");
02329 
02330       MetadataList.assignValue(
02331           GET_OR_DISTINCT(DIExpression, Record[0],
02332                           (Context, makeArrayRef(Record).slice(1))),
02333           NextMetadataNo++);
02334       break;
02335     }
02336     case bitc::METADATA_OBJC_PROPERTY: {
02337       if (Record.size() != 8)
02338         return error("Invalid record");
02339 
02340       MetadataList.assignValue(
02341           GET_OR_DISTINCT(DIObjCProperty, Record[0],
02342                           (Context, getMDString(Record[1]),
02343                            getMDOrNull(Record[2]), Record[3],
02344                            getMDString(Record[4]), getMDString(Record[5]),
02345                            Record[6], getMDOrNull(Record[7]))),
02346           NextMetadataNo++);
02347       break;
02348     }
02349     case bitc::METADATA_IMPORTED_ENTITY: {
02350       if (Record.size() != 6)
02351         return error("Invalid record");
02352 
02353       MetadataList.assignValue(
02354           GET_OR_DISTINCT(DIImportedEntity, Record[0],
02355                           (Context, Record[1], getMDOrNull(Record[2]),
02356                            getMDOrNull(Record[3]), Record[4],
02357                            getMDString(Record[5]))),
02358           NextMetadataNo++);
02359       break;
02360     }
02361     case bitc::METADATA_STRING: {
02362       std::string String(Record.begin(), Record.end());
02363       llvm::UpgradeMDStringConstant(String);
02364       Metadata *MD = MDString::get(Context, String);
02365       MetadataList.assignValue(MD, NextMetadataNo++);
02366       break;
02367     }
02368     case bitc::METADATA_KIND: {
02369       // Support older bitcode files that had METADATA_KIND records in a
02370       // block with METADATA_BLOCK_ID.
02371       if (std::error_code EC = parseMetadataKindRecord(Record))
02372         return EC;
02373       break;
02374     }
02375     }
02376   }
02377 #undef GET_OR_DISTINCT
02378 }
02379 
02380 /// Parse the metadata kinds out of the METADATA_KIND_BLOCK.
02381 std::error_code BitcodeReader::parseMetadataKinds() {
02382   if (Stream.EnterSubBlock(bitc::METADATA_KIND_BLOCK_ID))
02383     return error("Invalid record");
02384 
02385   SmallVector<uint64_t, 64> Record;
02386 
02387   // Read all the records.
02388   while (1) {
02389     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
02390 
02391     switch (Entry.Kind) {
02392     case BitstreamEntry::SubBlock: // Handled for us already.
02393     case BitstreamEntry::Error:
02394       return error("Malformed block");
02395     case BitstreamEntry::EndBlock:
02396       return std::error_code();
02397     case BitstreamEntry::Record:
02398       // The interesting case.
02399       break;
02400     }
02401 
02402     // Read a record.
02403     Record.clear();
02404     unsigned Code = Stream.readRecord(Entry.ID, Record);
02405     switch (Code) {
02406     default: // Default behavior: ignore.
02407       break;
02408     case bitc::METADATA_KIND: {
02409       if (std::error_code EC = parseMetadataKindRecord(Record))
02410         return EC;
02411       break;
02412     }
02413     }
02414   }
02415 }
02416 
02417 /// Decode a signed value stored with the sign bit in the LSB for dense VBR
02418 /// encoding.
02419 uint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V) {
02420   if ((V & 1) == 0)
02421     return V >> 1;
02422   if (V != 1)
02423     return -(V >> 1);
02424   // There is no such thing as -0 with integers.  "-0" really means MININT.
02425   return 1ULL << 63;
02426 }
02427 
02428 /// Resolve all of the initializers for global values and aliases that we can.
02429 std::error_code BitcodeReader::resolveGlobalAndAliasInits() {
02430   std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInitWorklist;
02431   std::vector<std::pair<GlobalAlias*, unsigned> > AliasInitWorklist;
02432   std::vector<std::pair<Function*, unsigned> > FunctionPrefixWorklist;
02433   std::vector<std::pair<Function*, unsigned> > FunctionPrologueWorklist;
02434   std::vector<std::pair<Function*, unsigned> > FunctionPersonalityFnWorklist;
02435 
02436   GlobalInitWorklist.swap(GlobalInits);
02437   AliasInitWorklist.swap(AliasInits);
02438   FunctionPrefixWorklist.swap(FunctionPrefixes);
02439   FunctionPrologueWorklist.swap(FunctionPrologues);
02440   FunctionPersonalityFnWorklist.swap(FunctionPersonalityFns);
02441 
02442   while (!GlobalInitWorklist.empty()) {
02443     unsigned ValID = GlobalInitWorklist.back().second;
02444     if (ValID >= ValueList.size()) {
02445       // Not ready to resolve this yet, it requires something later in the file.
02446       GlobalInits.push_back(GlobalInitWorklist.back());
02447     } else {
02448       if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
02449         GlobalInitWorklist.back().first->setInitializer(C);
02450       else
02451         return error("Expected a constant");
02452     }
02453     GlobalInitWorklist.pop_back();
02454   }
02455 
02456   while (!AliasInitWorklist.empty()) {
02457     unsigned ValID = AliasInitWorklist.back().second;
02458     if (ValID >= ValueList.size()) {
02459       AliasInits.push_back(AliasInitWorklist.back());
02460     } else {
02461       Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]);
02462       if (!C)
02463         return error("Expected a constant");
02464       GlobalAlias *Alias = AliasInitWorklist.back().first;
02465       if (C->getType() != Alias->getType())
02466         return error("Alias and aliasee types don't match");
02467       Alias->setAliasee(C);
02468     }
02469     AliasInitWorklist.pop_back();
02470   }
02471 
02472   while (!FunctionPrefixWorklist.empty()) {
02473     unsigned ValID = FunctionPrefixWorklist.back().second;
02474     if (ValID >= ValueList.size()) {
02475       FunctionPrefixes.push_back(FunctionPrefixWorklist.back());
02476     } else {
02477       if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
02478         FunctionPrefixWorklist.back().first->setPrefixData(C);
02479       else
02480         return error("Expected a constant");
02481     }
02482     FunctionPrefixWorklist.pop_back();
02483   }
02484 
02485   while (!FunctionPrologueWorklist.empty()) {
02486     unsigned ValID = FunctionPrologueWorklist.back().second;
02487     if (ValID >= ValueList.size()) {
02488       FunctionPrologues.push_back(FunctionPrologueWorklist.back());
02489     } else {
02490       if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
02491         FunctionPrologueWorklist.back().first->setPrologueData(C);
02492       else
02493         return error("Expected a constant");
02494     }
02495     FunctionPrologueWorklist.pop_back();
02496   }
02497 
02498   while (!FunctionPersonalityFnWorklist.empty()) {
02499     unsigned ValID = FunctionPersonalityFnWorklist.back().second;
02500     if (ValID >= ValueList.size()) {
02501       FunctionPersonalityFns.push_back(FunctionPersonalityFnWorklist.back());
02502     } else {
02503       if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
02504         FunctionPersonalityFnWorklist.back().first->setPersonalityFn(C);
02505       else
02506         return error("Expected a constant");
02507     }
02508     FunctionPersonalityFnWorklist.pop_back();
02509   }
02510 
02511   return std::error_code();
02512 }
02513 
02514 static APInt readWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) {
02515   SmallVector<uint64_t, 8> Words(Vals.size());
02516   std::transform(Vals.begin(), Vals.end(), Words.begin(),
02517                  BitcodeReader::decodeSignRotatedValue);
02518 
02519   return APInt(TypeBits, Words);
02520 }
02521 
02522 std::error_code BitcodeReader::parseConstants() {
02523   if (Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID))
02524     return error("Invalid record");
02525 
02526   SmallVector<uint64_t, 64> Record;
02527 
02528   // Read all the records for this value table.
02529   Type *CurTy = Type::getInt32Ty(Context);
02530   unsigned NextCstNo = ValueList.size();
02531   while (1) {
02532     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
02533 
02534     switch (Entry.Kind) {
02535     case BitstreamEntry::SubBlock: // Handled for us already.
02536     case BitstreamEntry::Error:
02537       return error("Malformed block");
02538     case BitstreamEntry::EndBlock:
02539       if (NextCstNo != ValueList.size())
02540         return error("Invalid constant reference");
02541 
02542       // Once all the constants have been read, go through and resolve forward
02543       // references.
02544       ValueList.resolveConstantForwardRefs();
02545       return std::error_code();
02546     case BitstreamEntry::Record:
02547       // The interesting case.
02548       break;
02549     }
02550 
02551     // Read a record.
02552     Record.clear();
02553     Value *V = nullptr;
02554     unsigned BitCode = Stream.readRecord(Entry.ID, Record);
02555     switch (BitCode) {
02556     default:  // Default behavior: unknown constant
02557     case bitc::CST_CODE_UNDEF:     // UNDEF
02558       V = UndefValue::get(CurTy);
02559       break;
02560     case bitc::CST_CODE_SETTYPE:   // SETTYPE: [typeid]
02561       if (Record.empty())
02562         return error("Invalid record");
02563       if (Record[0] >= TypeList.size() || !TypeList[Record[0]])
02564         return error("Invalid record");
02565       CurTy = TypeList[Record[0]];
02566       continue;  // Skip the ValueList manipulation.
02567     case bitc::CST_CODE_NULL:      // NULL
02568       V = Constant::getNullValue(CurTy);
02569       break;
02570     case bitc::CST_CODE_INTEGER:   // INTEGER: [intval]
02571       if (!CurTy->isIntegerTy() || Record.empty())
02572         return error("Invalid record");
02573       V = ConstantInt::get(CurTy, decodeSignRotatedValue(Record[0]));
02574       break;
02575     case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval]
02576       if (!CurTy->isIntegerTy() || Record.empty())
02577         return error("Invalid record");
02578 
02579       APInt VInt =
02580           readWideAPInt(Record, cast<IntegerType>(CurTy)->getBitWidth());
02581       V = ConstantInt::get(Context, VInt);
02582 
02583       break;
02584     }
02585     case bitc::CST_CODE_FLOAT: {    // FLOAT: [fpval]
02586       if (Record.empty())
02587         return error("Invalid record");
02588       if (CurTy->isHalfTy())
02589         V = ConstantFP::get(Context, APFloat(APFloat::IEEEhalf,
02590                                              APInt(16, (uint16_t)Record[0])));
02591       else if (CurTy->isFloatTy())
02592         V = ConstantFP::get(Context, APFloat(APFloat::IEEEsingle,
02593                                              APInt(32, (uint32_t)Record[0])));
02594       else if (CurTy->isDoubleTy())
02595         V = ConstantFP::get(Context, APFloat(APFloat::IEEEdouble,
02596                                              APInt(64, Record[0])));
02597       else if (CurTy->isX86_FP80Ty()) {
02598         // Bits are not stored the same way as a normal i80 APInt, compensate.
02599         uint64_t Rearrange[2];
02600         Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16);
02601         Rearrange[1] = Record[0] >> 48;
02602         V = ConstantFP::get(Context, APFloat(APFloat::x87DoubleExtended,
02603                                              APInt(80, Rearrange)));
02604       } else if (CurTy->isFP128Ty())
02605         V = ConstantFP::get(Context, APFloat(APFloat::IEEEquad,
02606                                              APInt(128, Record)));
02607       else if (CurTy->isPPC_FP128Ty())
02608         V = ConstantFP::get(Context, APFloat(APFloat::PPCDoubleDouble,
02609                                              APInt(128, Record)));
02610       else
02611         V = UndefValue::get(CurTy);
02612       break;
02613     }
02614 
02615     case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number]
02616       if (Record.empty())
02617         return error("Invalid record");
02618 
02619       unsigned Size = Record.size();
02620       SmallVector<Constant*, 16> Elts;
02621 
02622       if (StructType *STy = dyn_cast<StructType>(CurTy)) {
02623         for (unsigned i = 0; i != Size; ++i)
02624           Elts.push_back(ValueList.getConstantFwdRef(Record[i],
02625                                                      STy->getElementType(i)));
02626         V = ConstantStruct::get(STy, Elts);
02627       } else if (ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) {
02628         Type *EltTy = ATy->getElementType();
02629         for (unsigned i = 0; i != Size; ++i)
02630           Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
02631         V = ConstantArray::get(ATy, Elts);
02632       } else if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) {
02633         Type *EltTy = VTy->getElementType();
02634         for (unsigned i = 0; i != Size; ++i)
02635           Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
02636         V = ConstantVector::get(Elts);
02637       } else {
02638         V = UndefValue::get(CurTy);
02639       }
02640       break;
02641     }
02642     case bitc::CST_CODE_STRING:    // STRING: [values]
02643     case bitc::CST_CODE_CSTRING: { // CSTRING: [values]
02644       if (Record.empty())
02645         return error("Invalid record");
02646 
02647       SmallString<16> Elts(Record.begin(), Record.end());
02648       V = ConstantDataArray::getString(Context, Elts,
02649                                        BitCode == bitc::CST_CODE_CSTRING);
02650       break;
02651     }
02652     case bitc::CST_CODE_DATA: {// DATA: [n x value]
02653       if (Record.empty())
02654         return error("Invalid record");
02655 
02656       Type *EltTy = cast<SequentialType>(CurTy)->getElementType();
02657       if (EltTy->isIntegerTy(8)) {
02658         SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end());
02659         if (isa<VectorType>(CurTy))
02660           V = ConstantDataVector::get(Context, Elts);
02661         else
02662           V = ConstantDataArray::get(Context, Elts);
02663       } else if (EltTy->isIntegerTy(16)) {
02664         SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end());
02665         if (isa<VectorType>(CurTy))
02666           V = ConstantDataVector::get(Context, Elts);
02667         else
02668           V = ConstantDataArray::get(Context, Elts);
02669       } else if (EltTy->isIntegerTy(32)) {
02670         SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end());
02671         if (isa<VectorType>(CurTy))
02672           V = ConstantDataVector::get(Context, Elts);
02673         else
02674           V = ConstantDataArray::get(Context, Elts);
02675       } else if (EltTy->isIntegerTy(64)) {
02676         SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end());
02677         if (isa<VectorType>(CurTy))
02678           V = ConstantDataVector::get(Context, Elts);
02679         else
02680           V = ConstantDataArray::get(Context, Elts);
02681       } else if (EltTy->isHalfTy()) {
02682         SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end());
02683         if (isa<VectorType>(CurTy))
02684           V = ConstantDataVector::getFP(Context, Elts);
02685         else
02686           V = ConstantDataArray::getFP(Context, Elts);
02687       } else if (EltTy->isFloatTy()) {
02688         SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end());
02689         if (isa<VectorType>(CurTy))
02690           V = ConstantDataVector::getFP(Context, Elts);
02691         else
02692           V = ConstantDataArray::getFP(Context, Elts);
02693       } else if (EltTy->isDoubleTy()) {
02694         SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end());
02695         if (isa<VectorType>(CurTy))
02696           V = ConstantDataVector::getFP(Context, Elts);
02697         else
02698           V = ConstantDataArray::getFP(Context, Elts);
02699       } else {
02700         return error("Invalid type for value");
02701       }
02702       break;
02703     }
02704 
02705     case bitc::CST_CODE_CE_BINOP: {  // CE_BINOP: [opcode, opval, opval]
02706       if (Record.size() < 3)
02707         return error("Invalid record");
02708       int Opc = getDecodedBinaryOpcode(Record[0], CurTy);
02709       if (Opc < 0) {
02710         V = UndefValue::get(CurTy);  // Unknown binop.
02711       } else {
02712         Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy);
02713         Constant *RHS = ValueList.getConstantFwdRef(Record[2], CurTy);
02714         unsigned Flags = 0;
02715         if (Record.size() >= 4) {
02716           if (Opc == Instruction::Add ||
02717               Opc == Instruction::Sub ||
02718               Opc == Instruction::Mul ||
02719               Opc == Instruction::Shl) {
02720             if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP))
02721               Flags |= OverflowingBinaryOperator::NoSignedWrap;
02722             if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
02723               Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
02724           } else if (Opc == Instruction::SDiv ||
02725                      Opc == Instruction::UDiv ||
02726                      Opc == Instruction::LShr ||
02727                      Opc == Instruction::AShr) {
02728             if (Record[3] & (1 << bitc::PEO_EXACT))
02729               Flags |= SDivOperator::IsExact;
02730           }
02731         }
02732         V = ConstantExpr::get(Opc, LHS, RHS, Flags);
02733       }
02734       break;
02735     }
02736     case bitc::CST_CODE_CE_CAST: {  // CE_CAST: [opcode, opty, opval]
02737       if (Record.size() < 3)
02738         return error("Invalid record");
02739       int Opc = getDecodedCastOpcode(Record[0]);
02740       if (Opc < 0) {
02741         V = UndefValue::get(CurTy);  // Unknown cast.
02742       } else {
02743         Type *OpTy = getTypeByID(Record[1]);
02744         if (!OpTy)
02745           return error("Invalid record");
02746         Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy);
02747         V = UpgradeBitCastExpr(Opc, Op, CurTy);
02748         if (!V) V = ConstantExpr::getCast(Opc, Op, CurTy);
02749       }
02750       break;
02751     }
02752     case bitc::CST_CODE_CE_INBOUNDS_GEP:
02753     case bitc::CST_CODE_CE_GEP: {  // CE_GEP:        [n x operands]
02754       unsigned OpNum = 0;
02755       Type *PointeeType = nullptr;
02756       if (Record.size() % 2)
02757         PointeeType = getTypeByID(Record[OpNum++]);
02758       SmallVector<Constant*, 16> Elts;
02759       while (OpNum != Record.size()) {
02760         Type *ElTy = getTypeByID(Record[OpNum++]);
02761         if (!ElTy)
02762           return error("Invalid record");
02763         Elts.push_back(ValueList.getConstantFwdRef(Record[OpNum++], ElTy));
02764       }
02765 
02766       if (PointeeType &&
02767           PointeeType !=
02768               cast<SequentialType>(Elts[0]->getType()->getScalarType())
02769                   ->getElementType())
02770         return error("Explicit gep operator type does not match pointee type "
02771                      "of pointer operand");
02772 
02773       ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
02774       V = ConstantExpr::getGetElementPtr(PointeeType, Elts[0], Indices,
02775                                          BitCode ==
02776                                              bitc::CST_CODE_CE_INBOUNDS_GEP);
02777       break;
02778     }
02779     case bitc::CST_CODE_CE_SELECT: {  // CE_SELECT: [opval#, opval#, opval#]
02780       if (Record.size() < 3)
02781         return error("Invalid record");
02782 
02783       Type *SelectorTy = Type::getInt1Ty(Context);
02784 
02785       // The selector might be an i1 or an <n x i1>
02786       // Get the type from the ValueList before getting a forward ref.
02787       if (VectorType *VTy = dyn_cast<VectorType>(CurTy))
02788         if (Value *V = ValueList[Record[0]])
02789           if (SelectorTy != V->getType())
02790             SelectorTy = VectorType::get(SelectorTy, VTy->getNumElements());
02791 
02792       V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0],
02793                                                               SelectorTy),
02794                                   ValueList.getConstantFwdRef(Record[1],CurTy),
02795                                   ValueList.getConstantFwdRef(Record[2],CurTy));
02796       break;
02797     }
02798     case bitc::CST_CODE_CE_EXTRACTELT
02799         : { // CE_EXTRACTELT: [opty, opval, opty, opval]
02800       if (Record.size() < 3)
02801         return error("Invalid record");
02802       VectorType *OpTy =
02803         dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
02804       if (!OpTy)
02805         return error("Invalid record");
02806       Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
02807       Constant *Op1 = nullptr;
02808       if (Record.size() == 4) {
02809         Type *IdxTy = getTypeByID(Record[2]);
02810         if (!IdxTy)
02811           return error("Invalid record");
02812         Op1 = ValueList.getConstantFwdRef(Record[3], IdxTy);
02813       } else // TODO: Remove with llvm 4.0
02814         Op1 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context));
02815       if (!Op1)
02816         return error("Invalid record");
02817       V = ConstantExpr::getExtractElement(Op0, Op1);
02818       break;
02819     }
02820     case bitc::CST_CODE_CE_INSERTELT
02821         : { // CE_INSERTELT: [opval, opval, opty, opval]
02822       VectorType *OpTy = dyn_cast<VectorType>(CurTy);
02823       if (Record.size() < 3 || !OpTy)
02824         return error("Invalid record");
02825       Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy);
02826       Constant *Op1 = ValueList.getConstantFwdRef(Record[1],
02827                                                   OpTy->getElementType());
02828       Constant *Op2 = nullptr;
02829       if (Record.size() == 4) {
02830         Type *IdxTy = getTypeByID(Record[2]);
02831         if (!IdxTy)
02832           return error("Invalid record");
02833         Op2 = ValueList.getConstantFwdRef(Record[3], IdxTy);
02834       } else // TODO: Remove with llvm 4.0
02835         Op2 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context));
02836       if (!Op2)
02837         return error("Invalid record");
02838       V = ConstantExpr::getInsertElement(Op0, Op1, Op2);
02839       break;
02840     }
02841     case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval]
02842       VectorType *OpTy = dyn_cast<VectorType>(CurTy);
02843       if (Record.size() < 3 || !OpTy)
02844         return error("Invalid record");
02845       Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy);
02846       Constant *Op1 = ValueList.getConstantFwdRef(Record[1], OpTy);
02847       Type *ShufTy = VectorType::get(Type::getInt32Ty(Context),
02848                                                  OpTy->getNumElements());
02849       Constant *Op2 = ValueList.getConstantFwdRef(Record[2], ShufTy);
02850       V = ConstantExpr::getShuffleVector(Op0, Op1, Op2);
02851       break;
02852     }
02853     case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval]
02854       VectorType *RTy = dyn_cast<VectorType>(CurTy);
02855       VectorType *OpTy =
02856         dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
02857       if (Record.size() < 4 || !RTy || !OpTy)
02858         return error("Invalid record");
02859       Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
02860       Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
02861       Type *ShufTy = VectorType::get(Type::getInt32Ty(Context),
02862                                                  RTy->getNumElements());
02863       Constant *Op2 = ValueList.getConstantFwdRef(Record[3], ShufTy);
02864       V = ConstantExpr::getShuffleVector(Op0, Op1, Op2);
02865       break;
02866     }
02867     case bitc::CST_CODE_CE_CMP: {     // CE_CMP: [opty, opval, opval, pred]
02868       if (Record.size() < 4)
02869         return error("Invalid record");
02870       Type *OpTy = getTypeByID(Record[0]);
02871       if (!OpTy)
02872         return error("Invalid record");
02873       Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
02874       Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
02875 
02876       if (OpTy->isFPOrFPVectorTy())
02877         V = ConstantExpr::getFCmp(Record[3], Op0, Op1);
02878       else
02879         V = ConstantExpr::getICmp(Record[3], Op0, Op1);
02880       break;
02881     }
02882     // This maintains backward compatibility, pre-asm dialect keywords.
02883     // FIXME: Remove with the 4.0 release.
02884     case bitc::CST_CODE_INLINEASM_OLD: {
02885       if (Record.size() < 2)
02886         return error("Invalid record");
02887       std::string AsmStr, ConstrStr;
02888       bool HasSideEffects = Record[0] & 1;
02889       bool IsAlignStack = Record[0] >> 1;
02890       unsigned AsmStrSize = Record[1];
02891       if (2+AsmStrSize >= Record.size())
02892         return error("Invalid record");
02893       unsigned ConstStrSize = Record[2+AsmStrSize];
02894       if (3+AsmStrSize+ConstStrSize > Record.size())
02895         return error("Invalid record");
02896 
02897       for (unsigned i = 0; i != AsmStrSize; ++i)
02898         AsmStr += (char)Record[2+i];
02899       for (unsigned i = 0; i != ConstStrSize; ++i)
02900         ConstrStr += (char)Record[3+AsmStrSize+i];
02901       PointerType *PTy = cast<PointerType>(CurTy);
02902       V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()),
02903                          AsmStr, ConstrStr, HasSideEffects, IsAlignStack);
02904       break;
02905     }
02906     // This version adds support for the asm dialect keywords (e.g.,
02907     // inteldialect).
02908     case bitc::CST_CODE_INLINEASM: {
02909       if (Record.size() < 2)
02910         return error("Invalid record");
02911       std::string AsmStr, ConstrStr;
02912       bool HasSideEffects = Record[0] & 1;
02913       bool IsAlignStack = (Record[0] >> 1) & 1;
02914       unsigned AsmDialect = Record[0] >> 2;
02915       unsigned AsmStrSize = Record[1];
02916       if (2+AsmStrSize >= Record.size())
02917         return error("Invalid record");
02918       unsigned ConstStrSize = Record[2+AsmStrSize];
02919       if (3+AsmStrSize+ConstStrSize > Record.size())
02920         return error("Invalid record");
02921 
02922       for (unsigned i = 0; i != AsmStrSize; ++i)
02923         AsmStr += (char)Record[2+i];
02924       for (unsigned i = 0; i != ConstStrSize; ++i)
02925         ConstrStr += (char)Record[3+AsmStrSize+i];
02926       PointerType *PTy = cast<PointerType>(CurTy);
02927       V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()),
02928                          AsmStr, ConstrStr, HasSideEffects, IsAlignStack,
02929                          InlineAsm::AsmDialect(AsmDialect));
02930       break;
02931     }
02932     case bitc::CST_CODE_BLOCKADDRESS:{
02933       if (Record.size() < 3)
02934         return error("Invalid record");
02935       Type *FnTy = getTypeByID(Record[0]);
02936       if (!FnTy)
02937         return error("Invalid record");
02938       Function *Fn =
02939         dyn_cast_or_null<Function>(ValueList.getConstantFwdRef(Record[1],FnTy));
02940       if (!Fn)
02941         return error("Invalid record");
02942 
02943       // If the function is already parsed we can insert the block address right
02944       // away.
02945       BasicBlock *BB;
02946       unsigned BBID = Record[2];
02947       if (!BBID)
02948         // Invalid reference to entry block.
02949         return error("Invalid ID");
02950       if (!Fn->empty()) {
02951         Function::iterator BBI = Fn->begin(), BBE = Fn->end();
02952         for (size_t I = 0, E = BBID; I != E; ++I) {
02953           if (BBI == BBE)
02954             return error("Invalid ID");
02955           ++BBI;
02956         }
02957         BB = &*BBI;
02958       } else {
02959         // Otherwise insert a placeholder and remember it so it can be inserted
02960         // when the function is parsed.
02961         auto &FwdBBs = BasicBlockFwdRefs[Fn];
02962         if (FwdBBs.empty())
02963           BasicBlockFwdRefQueue.push_back(Fn);
02964         if (FwdBBs.size() < BBID + 1)
02965           FwdBBs.resize(BBID + 1);
02966         if (!FwdBBs[BBID])
02967           FwdBBs[BBID] = BasicBlock::Create(Context);
02968         BB = FwdBBs[BBID];
02969       }
02970       V = BlockAddress::get(Fn, BB);
02971       break;
02972     }
02973     }
02974 
02975     ValueList.assignValue(V, NextCstNo);
02976     ++NextCstNo;
02977   }
02978 }
02979 
02980 std::error_code BitcodeReader::parseUseLists() {
02981   if (Stream.EnterSubBlock(bitc::USELIST_BLOCK_ID))
02982     return error("Invalid record");
02983 
02984   // Read all the records.
02985   SmallVector<uint64_t, 64> Record;
02986   while (1) {
02987     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
02988 
02989     switch (Entry.Kind) {
02990     case BitstreamEntry::SubBlock: // Handled for us already.
02991     case BitstreamEntry::Error:
02992       return error("Malformed block");
02993     case BitstreamEntry::EndBlock:
02994       return std::error_code();
02995     case BitstreamEntry::Record:
02996       // The interesting case.
02997       break;
02998     }
02999 
03000     // Read a use list record.
03001     Record.clear();
03002     bool IsBB = false;
03003     switch (Stream.readRecord(Entry.ID, Record)) {
03004     default:  // Default behavior: unknown type.
03005       break;
03006     case bitc::USELIST_CODE_BB:
03007       IsBB = true;
03008       // fallthrough
03009     case bitc::USELIST_CODE_DEFAULT: {
03010       unsigned RecordLength = Record.size();
03011       if (RecordLength < 3)
03012         // Records should have at least an ID and two indexes.
03013         return error("Invalid record");
03014       unsigned ID = Record.back();
03015       Record.pop_back();
03016 
03017       Value *V;
03018       if (IsBB) {
03019         assert(ID < FunctionBBs.size() && "Basic block not found");
03020         V = FunctionBBs[ID];
03021       } else
03022         V = ValueList[ID];
03023       unsigned NumUses = 0;
03024       SmallDenseMap<const Use *, unsigned, 16> Order;
03025       for (const Use &U : V->materialized_uses()) {
03026         if (++NumUses > Record.size())
03027           break;
03028         Order[&U] = Record[NumUses - 1];
03029       }
03030       if (Order.size() != Record.size() || NumUses > Record.size())
03031         // Mismatches can happen if the functions are being materialized lazily
03032         // (out-of-order), or a value has been upgraded.
03033         break;
03034 
03035       V->sortUseList([&](const Use &L, const Use &R) {
03036         return Order.lookup(&L) < Order.lookup(&R);
03037       });
03038       break;
03039     }
03040     }
03041   }
03042 }
03043 
03044 /// When we see the block for metadata, remember where it is and then skip it.
03045 /// This lets us lazily deserialize the metadata.
03046 std::error_code BitcodeReader::rememberAndSkipMetadata() {
03047   // Save the current stream state.
03048   uint64_t CurBit = Stream.GetCurrentBitNo();
03049   DeferredMetadataInfo.push_back(CurBit);
03050 
03051   // Skip over the block for now.
03052   if (Stream.SkipBlock())
03053     return error("Invalid record");
03054   return std::error_code();
03055 }
03056 
03057 std::error_code BitcodeReader::materializeMetadata() {
03058   for (uint64_t BitPos : DeferredMetadataInfo) {
03059     // Move the bit stream to the saved position.
03060     Stream.JumpToBit(BitPos);
03061     if (std::error_code EC = parseMetadata(true))
03062       return EC;
03063   }
03064   DeferredMetadataInfo.clear();
03065   return std::error_code();
03066 }
03067 
03068 void BitcodeReader::setStripDebugInfo() { StripDebugInfo = true; }
03069 
03070 void BitcodeReader::saveMetadataList(
03071     DenseMap<const Metadata *, unsigned> &MetadataToIDs, bool OnlyTempMD) {
03072   for (unsigned ID = 0; ID < MetadataList.size(); ++ID) {
03073     Metadata *MD = MetadataList[ID];
03074     auto *N = dyn_cast_or_null<MDNode>(MD);
03075     assert((!N || (N->isResolved() || N->isTemporary())) &&
03076            "Found non-resolved non-temp MDNode while saving metadata");
03077     // Save all values if !OnlyTempMD, otherwise just the temporary metadata.
03078     // Note that in the !OnlyTempMD case we need to save all Metadata, not
03079     // just MDNode, as we may have references to other types of module-level
03080     // metadata (e.g. ValueAsMetadata) from instructions.
03081     if (!OnlyTempMD || (N && N->isTemporary())) {
03082       // Will call this after materializing each function, in order to
03083       // handle remapping of the function's instructions/metadata.
03084       auto IterBool = MetadataToIDs.insert(std::make_pair(MD, ID));
03085       // See if we already have an entry in that case.
03086       if (OnlyTempMD && !IterBool.second) {
03087         assert(IterBool.first->second == ID &&
03088                "Inconsistent metadata value id");
03089         continue;
03090       }
03091       if (N && N->isTemporary())
03092         // Ensure that we assert if someone tries to RAUW this temporary
03093         // metadata while it is the key of a map. The flag will be set back
03094         // to true when the saved metadata list is destroyed.
03095         N->setCanReplace(false);
03096     }
03097   }
03098 }
03099 
03100 /// When we see the block for a function body, remember where it is and then
03101 /// skip it.  This lets us lazily deserialize the functions.
03102 std::error_code BitcodeReader::rememberAndSkipFunctionBody() {
03103   // Get the function we are talking about.
03104   if (FunctionsWithBodies.empty())
03105     return error("Insufficient function protos");
03106 
03107   Function *Fn = FunctionsWithBodies.back();
03108   FunctionsWithBodies.pop_back();
03109 
03110   // Save the current stream state.
03111   uint64_t CurBit = Stream.GetCurrentBitNo();
03112   assert(
03113       (DeferredFunctionInfo[Fn] == 0 || DeferredFunctionInfo[Fn] == CurBit) &&
03114       "Mismatch between VST and scanned function offsets");
03115   DeferredFunctionInfo[Fn] = CurBit;
03116 
03117   // Skip over the function block for now.
03118   if (Stream.SkipBlock())
03119     return error("Invalid record");
03120   return std::error_code();
03121 }
03122 
03123 std::error_code BitcodeReader::globalCleanup() {
03124   // Patch the initializers for globals and aliases up.
03125   resolveGlobalAndAliasInits();
03126   if (!GlobalInits.empty() || !AliasInits.empty())
03127     return error("Malformed global initializer set");
03128 
03129   // Look for intrinsic functions which need to be upgraded at some point
03130   for (Function &F : *TheModule) {
03131     Function *NewFn;
03132     if (UpgradeIntrinsicFunction(&F, NewFn))
03133       UpgradedIntrinsics[&F] = NewFn;
03134   }
03135 
03136   // Look for global variables which need to be renamed.
03137   for (GlobalVariable &GV : TheModule->globals())
03138     UpgradeGlobalVariable(&GV);
03139 
03140   // Force deallocation of memory for these vectors to favor the client that
03141   // want lazy deserialization.
03142   std::vector<std::pair<GlobalVariable*, unsigned> >().swap(GlobalInits);
03143   std::vector<std::pair<GlobalAlias*, unsigned> >().swap(AliasInits);
03144   return std::error_code();
03145 }
03146 
03147 /// Support for lazy parsing of function bodies. This is required if we
03148 /// either have an old bitcode file without a VST forward declaration record,
03149 /// or if we have an anonymous function being materialized, since anonymous
03150 /// functions do not have a name and are therefore not in the VST.
03151 std::error_code BitcodeReader::rememberAndSkipFunctionBodies() {
03152   Stream.JumpToBit(NextUnreadBit);
03153 
03154   if (Stream.AtEndOfStream())
03155     return error("Could not find function in stream");
03156 
03157   if (!SeenFirstFunctionBody)
03158     return error("Trying to materialize functions before seeing function blocks");
03159 
03160   // An old bitcode file with the symbol table at the end would have
03161   // finished the parse greedily.
03162   assert(SeenValueSymbolTable);
03163 
03164   SmallVector<uint64_t, 64> Record;
03165 
03166   while (1) {
03167     BitstreamEntry Entry = Stream.advance();
03168     switch (Entry.Kind) {
03169     default:
03170       return error("Expect SubBlock");
03171     case BitstreamEntry::SubBlock:
03172       switch (Entry.ID) {
03173       default:
03174         return error("Expect function block");
03175       case bitc::FUNCTION_BLOCK_ID:
03176         if (std::error_code EC = rememberAndSkipFunctionBody())
03177           return EC;
03178         NextUnreadBit = Stream.GetCurrentBitNo();
03179         return std::error_code();
03180       }
03181     }
03182   }
03183 }
03184 
03185 std::error_code BitcodeReader::parseBitcodeVersion() {
03186   if (Stream.EnterSubBlock(bitc::IDENTIFICATION_BLOCK_ID))
03187     return error("Invalid record");
03188 
03189   // Read all the records.
03190   SmallVector<uint64_t, 64> Record;
03191   while (1) {
03192     BitstreamEntry Entry = Stream.advance();
03193 
03194     switch (Entry.Kind) {
03195     default:
03196     case BitstreamEntry::Error:
03197       return error("Malformed block");
03198     case BitstreamEntry::EndBlock:
03199       return std::error_code();
03200     case BitstreamEntry::Record:
03201       // The interesting case.
03202       break;
03203     }
03204 
03205     // Read a record.
03206     Record.clear();
03207     unsigned BitCode = Stream.readRecord(Entry.ID, Record);
03208     switch (BitCode) {
03209     default: // Default behavior: reject
03210       return error("Invalid value");
03211     case bitc::IDENTIFICATION_CODE_STRING: { // IDENTIFICATION:      [strchr x
03212                                              // N]
03213       convertToString(Record, 0, ProducerIdentification);
03214       break;
03215     }
03216     case bitc::IDENTIFICATION_CODE_EPOCH: { // EPOCH:      [epoch#]
03217       unsigned epoch = (unsigned)Record[0];
03218       if (epoch != bitc::BITCODE_CURRENT_EPOCH) {
03219         return error(
03220           Twine("Incompatible epoch: Bitcode '") + Twine(epoch) +
03221           "' vs current: '" + Twine(bitc::BITCODE_CURRENT_EPOCH) + "'");
03222       }
03223     }
03224     }
03225   }
03226 }
03227 
03228 std::error_code BitcodeReader::parseModule(uint64_t ResumeBit,
03229                                            bool ShouldLazyLoadMetadata) {
03230   if (ResumeBit)
03231     Stream.JumpToBit(ResumeBit);
03232   else if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
03233     return error("Invalid record");
03234 
03235   SmallVector<uint64_t, 64> Record;
03236   std::vector<std::string> SectionTable;
03237   std::vector<std::string> GCTable;
03238 
03239   // Read all the records for this module.
03240   while (1) {
03241     BitstreamEntry Entry = Stream.advance();
03242 
03243     switch (Entry.Kind) {
03244     case BitstreamEntry::Error:
03245       return error("Malformed block");
03246     case BitstreamEntry::EndBlock:
03247       return globalCleanup();
03248 
03249     case BitstreamEntry::SubBlock:
03250       switch (Entry.ID) {
03251       default:  // Skip unknown content.
03252         if (Stream.SkipBlock())
03253           return error("Invalid record");
03254         break;
03255       case bitc::BLOCKINFO_BLOCK_ID:
03256         if (Stream.ReadBlockInfoBlock())
03257           return error("Malformed block");
03258         break;
03259       case bitc::PARAMATTR_BLOCK_ID:
03260         if (std::error_code EC = parseAttributeBlock())
03261           return EC;
03262         break;
03263       case bitc::PARAMATTR_GROUP_BLOCK_ID:
03264         if (std::error_code EC = parseAttributeGroupBlock())
03265           return EC;
03266         break;
03267       case bitc::TYPE_BLOCK_ID_NEW:
03268         if (std::error_code EC = parseTypeTable())
03269           return EC;
03270         break;
03271       case bitc::VALUE_SYMTAB_BLOCK_ID:
03272         if (!SeenValueSymbolTable) {
03273           // Either this is an old form VST without function index and an
03274           // associated VST forward declaration record (which would have caused
03275           // the VST to be jumped to and parsed before it was encountered
03276           // normally in the stream), or there were no function blocks to
03277           // trigger an earlier parsing of the VST.
03278           assert(VSTOffset == 0 || FunctionsWithBodies.empty());
03279           if (std::error_code EC = parseValueSymbolTable())
03280             return EC;
03281           SeenValueSymbolTable = true;
03282         } else {
03283           // We must have had a VST forward declaration record, which caused
03284           // the parser to jump to and parse the VST earlier.
03285           assert(VSTOffset > 0);
03286           if (Stream.SkipBlock())
03287             return error("Invalid record");
03288         }
03289         break;
03290       case bitc::CONSTANTS_BLOCK_ID:
03291         if (std::error_code EC = parseConstants())
03292           return EC;
03293         if (std::error_code EC = resolveGlobalAndAliasInits())
03294           return EC;
03295         break;
03296       case bitc::METADATA_BLOCK_ID:
03297         if (ShouldLazyLoadMetadata && !IsMetadataMaterialized) {
03298           if (std::error_code EC = rememberAndSkipMetadata())
03299             return EC;
03300           break;
03301         }
03302         assert(DeferredMetadataInfo.empty() && "Unexpected deferred metadata");
03303         if (std::error_code EC = parseMetadata(true))
03304           return EC;
03305         break;
03306       case bitc::METADATA_KIND_BLOCK_ID:
03307         if (std::error_code EC = parseMetadataKinds())
03308           return EC;
03309         break;
03310       case bitc::FUNCTION_BLOCK_ID:
03311         // If this is the first function body we've seen, reverse the
03312         // FunctionsWithBodies list.
03313         if (!SeenFirstFunctionBody) {
03314           std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end());
03315           if (std::error_code EC = globalCleanup())
03316             return EC;
03317           SeenFirstFunctionBody = true;
03318         }
03319 
03320         if (VSTOffset > 0) {
03321           // If we have a VST forward declaration record, make sure we
03322           // parse the VST now if we haven't already. It is needed to
03323           // set up the DeferredFunctionInfo vector for lazy reading.
03324           if (!SeenValueSymbolTable) {
03325             if (std::error_code EC =
03326                     BitcodeReader::parseValueSymbolTable(VSTOffset))
03327               return EC;
03328             SeenValueSymbolTable = true;
03329             // Fall through so that we record the NextUnreadBit below.
03330             // This is necessary in case we have an anonymous function that
03331             // is later materialized. Since it will not have a VST entry we
03332             // need to fall back to the lazy parse to find its offset.
03333           } else {
03334             // If we have a VST forward declaration record, but have already
03335             // parsed the VST (just above, when the first function body was
03336             // encountered here), then we are resuming the parse after
03337             // materializing functions. The ResumeBit points to the
03338             // start of the last function block recorded in the
03339             // DeferredFunctionInfo map. Skip it.
03340             if (Stream.SkipBlock())
03341               return error("Invalid record");
03342             continue;
03343           }
03344         }
03345 
03346         // Support older bitcode files that did not have the function
03347         // index in the VST, nor a VST forward declaration record, as
03348         // well as anonymous functions that do not have VST entries.
03349         // Build the DeferredFunctionInfo vector on the fly.
03350         if (std::error_code EC = rememberAndSkipFunctionBody())
03351           return EC;
03352 
03353         // Suspend parsing when we reach the function bodies. Subsequent
03354         // materialization calls will resume it when necessary. If the bitcode
03355         // file is old, the symbol table will be at the end instead and will not
03356         // have been seen yet. In this case, just finish the parse now.
03357         if (SeenValueSymbolTable) {
03358           NextUnreadBit = Stream.GetCurrentBitNo();
03359           return std::error_code();
03360         }
03361         break;
03362       case bitc::USELIST_BLOCK_ID:
03363         if (std::error_code EC = parseUseLists())
03364           return EC;
03365         break;
03366       case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID:
03367         if (std::error_code EC = parseOperandBundleTags())
03368           return EC;
03369         break;
03370       }
03371       continue;
03372 
03373     case BitstreamEntry::Record:
03374       // The interesting case.
03375       break;
03376     }
03377 
03378 
03379     // Read a record.
03380     auto BitCode = Stream.readRecord(Entry.ID, Record);
03381     switch (BitCode) {
03382     default: break;  // Default behavior, ignore unknown content.
03383     case bitc::MODULE_CODE_VERSION: {  // VERSION: [version#]
03384       if (Record.size() < 1)
03385         return error("Invalid record");
03386       // Only version #0 and #1 are supported so far.
03387       unsigned module_version = Record[0];
03388       switch (module_version) {
03389         default:
03390           return error("Invalid value");
03391         case 0:
03392           UseRelativeIDs = false;
03393           break;
03394         case 1:
03395           UseRelativeIDs = true;
03396           break;
03397       }
03398       break;
03399     }
03400     case bitc::MODULE_CODE_TRIPLE: {  // TRIPLE: [strchr x N]
03401       std::string S;
03402       if (convertToString(Record, 0, S))
03403         return error("Invalid record");
03404       TheModule->setTargetTriple(S);
03405       break;
03406     }
03407     case bitc::MODULE_CODE_DATALAYOUT: {  // DATALAYOUT: [strchr x N]
03408       std::string S;
03409       if (convertToString(Record, 0, S))
03410         return error("Invalid record");
03411       TheModule->setDataLayout(S);
03412       break;
03413     }
03414     case bitc::MODULE_CODE_ASM: {  // ASM: [strchr x N]
03415       std::string S;
03416       if (convertToString(Record, 0, S))
03417         return error("Invalid record");
03418       TheModule->setModuleInlineAsm(S);
03419       break;
03420     }
03421     case bitc::MODULE_CODE_DEPLIB: {  // DEPLIB: [strchr x N]
03422       // FIXME: Remove in 4.0.
03423       std::string S;
03424       if (convertToString(Record, 0, S))
03425         return error("Invalid record");
03426       // Ignore value.
03427       break;
03428     }
03429     case bitc::MODULE_CODE_SECTIONNAME: {  // SECTIONNAME: [strchr x N]
03430       std::string S;
03431       if (convertToString(Record, 0, S))
03432         return error("Invalid record");
03433       SectionTable.push_back(S);
03434       break;
03435     }
03436     case bitc::MODULE_CODE_GCNAME: {  // SECTIONNAME: [strchr x N]
03437       std::string S;
03438       if (convertToString(Record, 0, S))
03439         return error("Invalid record");
03440       GCTable.push_back(S);
03441       break;
03442     }
03443     case bitc::MODULE_CODE_COMDAT: { // COMDAT: [selection_kind, name]
03444       if (Record.size() < 2)
03445         return error("Invalid record");
03446       Comdat::SelectionKind SK = getDecodedComdatSelectionKind(Record[0]);
03447       unsigned ComdatNameSize = Record[1];
03448       std::string ComdatName;
03449       ComdatName.reserve(ComdatNameSize);
03450       for (unsigned i = 0; i != ComdatNameSize; ++i)
03451         ComdatName += (char)Record[2 + i];
03452       Comdat *C = TheModule->getOrInsertComdat(ComdatName);
03453       C->setSelectionKind(SK);
03454       ComdatList.push_back(C);
03455       break;
03456     }
03457     // GLOBALVAR: [pointer type, isconst, initid,
03458     //             linkage, alignment, section, visibility, threadlocal,
03459     //             unnamed_addr, externally_initialized, dllstorageclass,
03460     //             comdat]
03461     case bitc::MODULE_CODE_GLOBALVAR: {
03462       if (Record.size() < 6)
03463         return error("Invalid record");
03464       Type *Ty = getTypeByID(Record[0]);
03465       if (!Ty)
03466         return error("Invalid record");
03467       bool isConstant = Record[1] & 1;
03468       bool explicitType = Record[1] & 2;
03469       unsigned AddressSpace;
03470       if (explicitType) {
03471         AddressSpace = Record[1] >> 2;
03472       } else {
03473         if (!Ty->isPointerTy())
03474           return error("Invalid type for value");
03475         AddressSpace = cast<PointerType>(Ty)->getAddressSpace();
03476         Ty = cast<PointerType>(Ty)->getElementType();
03477       }
03478 
03479       uint64_t RawLinkage = Record[3];
03480       GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage);
03481       unsigned Alignment;
03482       if (std::error_code EC = parseAlignmentValue(Record[4], Alignment))
03483         return EC;
03484       std::string Section;
03485       if (Record[5]) {
03486         if (Record[5]-1 >= SectionTable.size())
03487           return error("Invalid ID");
03488         Section = SectionTable[Record[5]-1];
03489       }
03490       GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility;
03491       // Local linkage must have default visibility.
03492       if (Record.size() > 6 && !GlobalValue::isLocalLinkage(Linkage))
03493         // FIXME: Change to an error if non-default in 4.0.
03494         Visibility = getDecodedVisibility(Record[6]);
03495 
03496       GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal;
03497       if (Record.size() > 7)
03498         TLM = getDecodedThreadLocalMode(Record[7]);
03499 
03500       bool UnnamedAddr = false;
03501       if (Record.size() > 8)
03502         UnnamedAddr = Record[8];
03503 
03504       bool ExternallyInitialized = false;
03505       if (Record.size() > 9)
03506         ExternallyInitialized = Record[9];
03507 
03508       GlobalVariable *NewGV =
03509         new GlobalVariable(*TheModule, Ty, isConstant, Linkage, nullptr, "", nullptr,
03510                            TLM, AddressSpace, ExternallyInitialized);
03511       NewGV->setAlignment(Alignment);
03512       if (!Section.empty())
03513         NewGV->setSection(Section);
03514       NewGV->setVisibility(Visibility);
03515       NewGV->setUnnamedAddr(UnnamedAddr);
03516 
03517       if (Record.size() > 10)
03518         NewGV->setDLLStorageClass(getDecodedDLLStorageClass(Record[10]));
03519       else
03520         upgradeDLLImportExportLinkage(NewGV, RawLinkage);
03521 
03522       ValueList.push_back(NewGV);
03523 
03524       // Remember which value to use for the global initializer.
03525       if (unsigned InitID = Record[2])
03526         GlobalInits.push_back(std::make_pair(NewGV, InitID-1));
03527 
03528       if (Record.size() > 11) {
03529         if (unsigned ComdatID = Record[11]) {
03530           if (ComdatID > ComdatList.size())
03531             return error("Invalid global variable comdat ID");
03532           NewGV->setComdat(ComdatList[ComdatID - 1]);
03533         }
03534       } else if (hasImplicitComdat(RawLinkage)) {
03535         NewGV->setComdat(reinterpret_cast<Comdat *>(1));
03536       }
03537       break;
03538     }
03539     // FUNCTION:  [type, callingconv, isproto, linkage, paramattr,
03540     //             alignment, section, visibility, gc, unnamed_addr,
03541     //             prologuedata, dllstorageclass, comdat, prefixdata]
03542     case bitc::MODULE_CODE_FUNCTION: {
03543       if (Record.size() < 8)
03544         return error("Invalid record");
03545       Type *Ty = getTypeByID(Record[0]);
03546       if (!Ty)
03547         return error("Invalid record");
03548       if (auto *PTy = dyn_cast<PointerType>(Ty))
03549         Ty = PTy->getElementType();
03550       auto *FTy = dyn_cast<FunctionType>(Ty);
03551       if (!FTy)
03552         return error("Invalid type for value");
03553       auto CC = static_cast<CallingConv::ID>(Record[1]);
03554       if (CC & ~CallingConv::MaxID)
03555         return error("Invalid calling convention ID");
03556 
03557       Function *Func = Function::Create(FTy, GlobalValue::ExternalLinkage,
03558                                         "", TheModule);
03559 
03560       Func->setCallingConv(CC);
03561       bool isProto = Record[2];
03562       uint64_t RawLinkage = Record[3];
03563       Func->setLinkage(getDecodedLinkage(RawLinkage));
03564       Func->setAttributes(getAttributes(Record[4]));
03565 
03566       unsigned Alignment;
03567       if (std::error_code EC = parseAlignmentValue(Record[5], Alignment))
03568         return EC;
03569       Func->setAlignment(Alignment);
03570       if (Record[6]) {
03571         if (Record[6]-1 >= SectionTable.size())
03572           return error("Invalid ID");
03573         Func->setSection(SectionTable[Record[6]-1]);
03574       }
03575       // Local linkage must have default visibility.
03576       if (!Func->hasLocalLinkage())
03577         // FIXME: Change to an error if non-default in 4.0.
03578         Func->setVisibility(getDecodedVisibility(Record[7]));
03579       if (Record.size() > 8 && Record[8]) {
03580         if (Record[8]-1 >= GCTable.size())
03581           return error("Invalid ID");
03582         Func->setGC(GCTable[Record[8]-1].c_str());
03583       }
03584       bool UnnamedAddr = false;
03585       if (Record.size() > 9)
03586         UnnamedAddr = Record[9];
03587       Func->setUnnamedAddr(UnnamedAddr);
03588       if (Record.size() > 10 && Record[10] != 0)
03589         FunctionPrologues.push_back(std::make_pair(Func, Record[10]-1));
03590 
03591       if (Record.size() > 11)
03592         Func->setDLLStorageClass(getDecodedDLLStorageClass(Record[11]));
03593       else
03594         upgradeDLLImportExportLinkage(Func, RawLinkage);
03595 
03596       if (Record.size() > 12) {
03597         if (unsigned ComdatID = Record[12]) {
03598           if (ComdatID > ComdatList.size())
03599             return error("Invalid function comdat ID");
03600           Func->setComdat(ComdatList[ComdatID - 1]);
03601         }
03602       } else if (hasImplicitComdat(RawLinkage)) {
03603         Func->setComdat(reinterpret_cast<Comdat *>(1));
03604       }
03605 
03606       if (Record.size() > 13 && Record[13] != 0)
03607         FunctionPrefixes.push_back(std::make_pair(Func, Record[13]-1));
03608 
03609       if (Record.size() > 14 && Record[14] != 0)
03610         FunctionPersonalityFns.push_back(std::make_pair(Func, Record[14] - 1));
03611 
03612       ValueList.push_back(Func);
03613 
03614       // If this is a function with a body, remember the prototype we are
03615       // creating now, so that we can match up the body with them later.
03616       if (!isProto) {
03617         Func->setIsMaterializable(true);
03618         FunctionsWithBodies.push_back(Func);
03619         DeferredFunctionInfo[Func] = 0;
03620       }
03621       break;
03622     }
03623     // ALIAS: [alias type, addrspace, aliasee val#, linkage]
03624     // ALIAS: [alias type, addrspace, aliasee val#, linkage, visibility, dllstorageclass]
03625     case bitc::MODULE_CODE_ALIAS:
03626     case bitc::MODULE_CODE_ALIAS_OLD: {
03627       bool NewRecord = BitCode == bitc::MODULE_CODE_ALIAS;
03628       if (Record.size() < (3 + (unsigned)NewRecord))
03629         return error("Invalid record");
03630       unsigned OpNum = 0;
03631       Type *Ty = getTypeByID(Record[OpNum++]);
03632       if (!Ty)
03633         return error("Invalid record");
03634 
03635       unsigned AddrSpace;
03636       if (!NewRecord) {
03637         auto *PTy = dyn_cast<PointerType>(Ty);
03638         if (!PTy)
03639           return error("Invalid type for value");
03640         Ty = PTy->getElementType();
03641         AddrSpace = PTy->getAddressSpace();
03642       } else {
03643         AddrSpace = Record[OpNum++];
03644       }
03645 
03646       auto Val = Record[OpNum++];
03647       auto Linkage = Record[OpNum++];
03648       auto *NewGA = GlobalAlias::create(
03649           Ty, AddrSpace, getDecodedLinkage(Linkage), "", TheModule);
03650       // Old bitcode files didn't have visibility field.
03651       // Local linkage must have default visibility.
03652       if (OpNum != Record.size()) {
03653         auto VisInd = OpNum++;
03654         if (!NewGA->hasLocalLinkage())
03655           // FIXME: Change to an error if non-default in 4.0.
03656           NewGA->setVisibility(getDecodedVisibility(Record[VisInd]));
03657       }
03658       if (OpNum != Record.size())
03659         NewGA->setDLLStorageClass(getDecodedDLLStorageClass(Record[OpNum++]));
03660       else
03661         upgradeDLLImportExportLinkage(NewGA, Linkage);
03662       if (OpNum != Record.size())
03663         NewGA->setThreadLocalMode(getDecodedThreadLocalMode(Record[OpNum++]));
03664       if (OpNum != Record.size())
03665         NewGA->setUnnamedAddr(Record[OpNum++]);
03666       ValueList.push_back(NewGA);
03667       AliasInits.push_back(std::make_pair(NewGA, Val));
03668       break;
03669     }
03670     /// MODULE_CODE_PURGEVALS: [numvals]
03671     case bitc::MODULE_CODE_PURGEVALS:
03672       // Trim down the value list to the specified size.
03673       if (Record.size() < 1 || Record[0] > ValueList.size())
03674         return error("Invalid record");
03675       ValueList.shrinkTo(Record[0]);
03676       break;
03677     /// MODULE_CODE_VSTOFFSET: [offset]
03678     case bitc::MODULE_CODE_VSTOFFSET:
03679       if (Record.size() < 1)
03680         return error("Invalid record");
03681       VSTOffset = Record[0];
03682       break;
03683     /// MODULE_CODE_METADATA_VALUES: [numvals]
03684     case bitc::MODULE_CODE_METADATA_VALUES:
03685       if (Record.size() < 1)
03686         return error("Invalid record");
03687       assert(!IsMetadataMaterialized);
03688       // This record contains the number of metadata values in the module-level
03689       // METADATA_BLOCK. It is used to support lazy parsing of metadata as
03690       // a postpass, where we will parse function-level metadata first.
03691       // This is needed because the ids of metadata are assigned implicitly
03692       // based on their ordering in the bitcode, with the function-level
03693       // metadata ids starting after the module-level metadata ids. Otherwise,
03694       // we would have to parse the module-level metadata block to prime the
03695       // MetadataList when we are lazy loading metadata during function
03696       // importing. Initialize the MetadataList size here based on the
03697       // record value, regardless of whether we are doing lazy metadata
03698       // loading, so that we have consistent handling and assertion
03699       // checking in parseMetadata for module-level metadata.
03700       NumModuleMDs = Record[0];
03701       SeenModuleValuesRecord = true;
03702       assert(MetadataList.size() == 0);
03703       MetadataList.resize(NumModuleMDs);
03704       break;
03705     }
03706     Record.clear();
03707   }
03708 }
03709 
03710 /// Helper to read the header common to all bitcode files.
03711 static bool hasValidBitcodeHeader(BitstreamCursor &Stream) {
03712   // Sniff for the signature.
03713   if (Stream.Read(8) != 'B' ||
03714       Stream.Read(8) != 'C' ||
03715       Stream.Read(4) != 0x0 ||
03716       Stream.Read(4) != 0xC ||
03717       Stream.Read(4) != 0xE ||
03718       Stream.Read(4) != 0xD)
03719     return false;
03720   return true;
03721 }
03722 
03723 std::error_code
03724 BitcodeReader::parseBitcodeInto(std::unique_ptr<DataStreamer> Streamer,
03725                                 Module *M, bool ShouldLazyLoadMetadata) {
03726   TheModule = M;
03727 
03728   if (std::error_code EC = initStream(std::move(Streamer)))
03729     return EC;
03730 
03731   // Sniff for the signature.
03732   if (!hasValidBitcodeHeader(Stream))
03733     return error("Invalid bitcode signature");
03734 
03735   // We expect a number of well-defined blocks, though we don't necessarily
03736   // need to understand them all.
03737   while (1) {
03738     if (Stream.AtEndOfStream()) {
03739       // We didn't really read a proper Module.
03740       return error("Malformed IR file");
03741     }
03742 
03743     BitstreamEntry Entry =
03744       Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs);
03745 
03746     if (Entry.Kind != BitstreamEntry::SubBlock)
03747       return error("Malformed block");
03748 
03749     if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID) {
03750       parseBitcodeVersion();
03751       continue;
03752     }
03753 
03754     if (Entry.ID == bitc::MODULE_BLOCK_ID)
03755       return parseModule(0, ShouldLazyLoadMetadata);
03756 
03757     if (Stream.SkipBlock())
03758       return error("Invalid record");
03759   }
03760 }
03761 
03762 ErrorOr<std::string> BitcodeReader::parseModuleTriple() {
03763   if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
03764     return error("Invalid record");
03765 
03766   SmallVector<uint64_t, 64> Record;
03767 
03768   std::string Triple;
03769   // Read all the records for this module.
03770   while (1) {
03771     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
03772 
03773     switch (Entry.Kind) {
03774     case BitstreamEntry::SubBlock: // Handled for us already.
03775     case BitstreamEntry::Error:
03776       return error("Malformed block");
03777     case BitstreamEntry::EndBlock:
03778       return Triple;
03779     case BitstreamEntry::Record:
03780       // The interesting case.
03781       break;
03782     }
03783 
03784     // Read a record.
03785     switch (Stream.readRecord(Entry.ID, Record)) {
03786     default: break;  // Default behavior, ignore unknown content.
03787     case bitc::MODULE_CODE_TRIPLE: {  // TRIPLE: [strchr x N]
03788       std::string S;
03789       if (convertToString(Record, 0, S))
03790         return error("Invalid record");
03791       Triple = S;
03792       break;
03793     }
03794     }
03795     Record.clear();
03796   }
03797   llvm_unreachable("Exit infinite loop");
03798 }
03799 
03800 ErrorOr<std::string> BitcodeReader::parseTriple() {
03801   if (std::error_code EC = initStream(nullptr))
03802     return EC;
03803 
03804   // Sniff for the signature.
03805   if (!hasValidBitcodeHeader(Stream))
03806     return error("Invalid bitcode signature");
03807 
03808   // We expect a number of well-defined blocks, though we don't necessarily
03809   // need to understand them all.
03810   while (1) {
03811     BitstreamEntry Entry = Stream.advance();
03812 
03813     switch (Entry.Kind) {
03814     case BitstreamEntry::Error:
03815       return error("Malformed block");
03816     case BitstreamEntry::EndBlock:
03817       return std::error_code();
03818 
03819     case BitstreamEntry::SubBlock:
03820       if (Entry.ID == bitc::MODULE_BLOCK_ID)
03821         return parseModuleTriple();
03822 
03823       // Ignore other sub-blocks.
03824       if (Stream.SkipBlock())
03825         return error("Malformed block");
03826       continue;
03827 
03828     case BitstreamEntry::Record:
03829       Stream.skipRecord(Entry.ID);
03830       continue;
03831     }
03832   }
03833 }
03834 
03835 ErrorOr<std::string> BitcodeReader::parseIdentificationBlock() {
03836   if (std::error_code EC = initStream(nullptr))
03837     return EC;
03838 
03839   // Sniff for the signature.
03840   if (!hasValidBitcodeHeader(Stream))
03841     return error("Invalid bitcode signature");
03842 
03843   // We expect a number of well-defined blocks, though we don't necessarily
03844   // need to understand them all.
03845   while (1) {
03846     BitstreamEntry Entry = Stream.advance();
03847     switch (Entry.Kind) {
03848     case BitstreamEntry::Error:
03849       return error("Malformed block");
03850     case BitstreamEntry::EndBlock:
03851       return std::error_code();
03852 
03853     case BitstreamEntry::SubBlock:
03854       if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID) {
03855         if (std::error_code EC = parseBitcodeVersion())
03856           return EC;
03857         return ProducerIdentification;
03858       }
03859       // Ignore other sub-blocks.
03860       if (Stream.SkipBlock())
03861         return error("Malformed block");
03862       continue;
03863     case BitstreamEntry::Record:
03864       Stream.skipRecord(Entry.ID);
03865       continue;
03866     }
03867   }
03868 }
03869 
03870 /// Parse metadata attachments.
03871 std::error_code BitcodeReader::parseMetadataAttachment(Function &F) {
03872   if (Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID))
03873     return error("Invalid record");
03874 
03875   SmallVector<uint64_t, 64> Record;
03876   while (1) {
03877     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
03878 
03879     switch (Entry.Kind) {
03880     case BitstreamEntry::SubBlock: // Handled for us already.
03881     case BitstreamEntry::Error:
03882       return error("Malformed block");
03883     case BitstreamEntry::EndBlock:
03884       return std::error_code();
03885     case BitstreamEntry::Record:
03886       // The interesting case.
03887       break;
03888     }
03889 
03890     // Read a metadata attachment record.
03891     Record.clear();
03892     switch (Stream.readRecord(Entry.ID, Record)) {
03893     default:  // Default behavior: ignore.
03894       break;
03895     case bitc::METADATA_ATTACHMENT: {
03896       unsigned RecordLength = Record.size();
03897       if (Record.empty())
03898         return error("Invalid record");
03899       if (RecordLength % 2 == 0) {
03900         // A function attachment.
03901         for (unsigned I = 0; I != RecordLength; I += 2) {
03902           auto K = MDKindMap.find(Record[I]);
03903           if (K == MDKindMap.end())
03904             return error("Invalid ID");
03905           Metadata *MD = MetadataList.getValueFwdRef(Record[I + 1]);
03906           F.setMetadata(K->second, cast<MDNode>(MD));
03907         }
03908         continue;
03909       }
03910 
03911       // An instruction attachment.
03912       Instruction *Inst = InstructionList[Record[0]];
03913       for (unsigned i = 1; i != RecordLength; i = i+2) {
03914         unsigned Kind = Record[i];
03915         DenseMap<unsigned, unsigned>::iterator I =
03916           MDKindMap.find(Kind);
03917         if (I == MDKindMap.end())
03918           return error("Invalid ID");
03919         Metadata *Node = MetadataList.getValueFwdRef(Record[i + 1]);
03920         if (isa<LocalAsMetadata>(Node))
03921           // Drop the attachment.  This used to be legal, but there's no
03922           // upgrade path.
03923           break;
03924         Inst->setMetadata(I->second, cast<MDNode>(Node));
03925         if (I->second == LLVMContext::MD_tbaa)
03926           InstsWithTBAATag.push_back(Inst);
03927       }
03928       break;
03929     }
03930     }
03931   }
03932 }
03933 
03934 static std::error_code typeCheckLoadStoreInst(Type *ValType, Type *PtrType) {
03935   LLVMContext &Context = PtrType->getContext();
03936   if (!isa<PointerType>(PtrType))
03937     return error(Context, "Load/Store operand is not a pointer type");
03938   Type *ElemType = cast<PointerType>(PtrType)->getElementType();
03939 
03940   if (ValType && ValType != ElemType)
03941     return error(Context, "Explicit load/store type does not match pointee "
03942                           "type of pointer operand");
03943   if (!PointerType::isLoadableOrStorableType(ElemType))
03944     return error(Context, "Cannot load/store from pointer");
03945   return std::error_code();
03946 }
03947 
03948 /// Lazily parse the specified function body block.
03949 std::error_code BitcodeReader::parseFunctionBody(Function *F) {
03950   if (Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID))
03951     return error("Invalid record");
03952 
03953   InstructionList.clear();
03954   unsigned ModuleValueListSize = ValueList.size();
03955   unsigned ModuleMetadataListSize = MetadataList.size();
03956 
03957   // Add all the function arguments to the value table.
03958   for (Argument &I : F->args())
03959     ValueList.push_back(&I);
03960 
03961   unsigned NextValueNo = ValueList.size();
03962   BasicBlock *CurBB = nullptr;
03963   unsigned CurBBNo = 0;
03964 
03965   DebugLoc LastLoc;
03966   auto getLastInstruction = [&]() -> Instruction * {
03967     if (CurBB && !CurBB->empty())
03968       return &CurBB->back();
03969     else if (CurBBNo && FunctionBBs[CurBBNo - 1] &&
03970              !FunctionBBs[CurBBNo - 1]->empty())
03971       return &FunctionBBs[CurBBNo - 1]->back();
03972     return nullptr;
03973   };
03974 
03975   std::vector<OperandBundleDef> OperandBundles;
03976 
03977   // Read all the records.
03978   SmallVector<uint64_t, 64> Record;
03979   while (1) {
03980     BitstreamEntry Entry = Stream.advance();
03981 
03982     switch (Entry.Kind) {
03983     case BitstreamEntry::Error:
03984       return error("Malformed block");
03985     case BitstreamEntry::EndBlock:
03986       goto OutOfRecordLoop;
03987 
03988     case BitstreamEntry::SubBlock:
03989       switch (Entry.ID) {
03990       default:  // Skip unknown content.
03991         if (Stream.SkipBlock())
03992           return error("Invalid record");
03993         break;
03994       case bitc::CONSTANTS_BLOCK_ID:
03995         if (std::error_code EC = parseConstants())
03996           return EC;
03997         NextValueNo = ValueList.size();
03998         break;
03999       case bitc::VALUE_SYMTAB_BLOCK_ID:
04000         if (std::error_code EC = parseValueSymbolTable())
04001           return EC;
04002         break;
04003       case bitc::METADATA_ATTACHMENT_ID:
04004         if (std::error_code EC = parseMetadataAttachment(*F))
04005           return EC;
04006         break;
04007       case bitc::METADATA_BLOCK_ID:
04008         if (std::error_code EC = parseMetadata())
04009           return EC;
04010         break;
04011       case bitc::USELIST_BLOCK_ID:
04012         if (std::error_code EC = parseUseLists())
04013           return EC;
04014         break;
04015       }
04016       continue;
04017 
04018     case BitstreamEntry::Record:
04019       // The interesting case.
04020       break;
04021     }
04022 
04023     // Read a record.
04024     Record.clear();
04025     Instruction *I = nullptr;
04026     unsigned BitCode = Stream.readRecord(Entry.ID, Record);
04027     switch (BitCode) {
04028     default: // Default behavior: reject
04029       return error("Invalid value");
04030     case bitc::FUNC_CODE_DECLAREBLOCKS: {   // DECLAREBLOCKS: [nblocks]
04031       if (Record.size() < 1 || Record[0] == 0)
04032         return error("Invalid record");
04033       // Create all the basic blocks for the function.
04034       FunctionBBs.resize(Record[0]);
04035 
04036       // See if anything took the address of blocks in this function.
04037       auto BBFRI = BasicBlockFwdRefs.find(F);
04038       if (BBFRI == BasicBlockFwdRefs.end()) {
04039         for (unsigned i = 0, e = FunctionBBs.size(); i != e; ++i)
04040           FunctionBBs[i] = BasicBlock::Create(Context, "", F);
04041       } else {
04042         auto &BBRefs = BBFRI->second;
04043         // Check for invalid basic block references.
04044         if (BBRefs.size() > FunctionBBs.size())
04045           return error("Invalid ID");
04046         assert(!BBRefs.empty() && "Unexpected empty array");
04047         assert(!BBRefs.front() && "Invalid reference to entry block");
04048         for (unsigned I = 0, E = FunctionBBs.size(), RE = BBRefs.size(); I != E;
04049              ++I)
04050           if (I < RE && BBRefs[I]) {
04051             BBRefs[I]->insertInto(F);
04052             FunctionBBs[I] = BBRefs[I];
04053           } else {
04054             FunctionBBs[I] = BasicBlock::Create(Context, "", F);
04055           }
04056 
04057         // Erase from the table.
04058         BasicBlockFwdRefs.erase(BBFRI);
04059       }
04060 
04061       CurBB = FunctionBBs[0];
04062       continue;
04063     }
04064 
04065     case bitc::FUNC_CODE_DEBUG_LOC_AGAIN:  // DEBUG_LOC_AGAIN
04066       // This record indicates that the last instruction is at the same
04067       // location as the previous instruction with a location.
04068       I = getLastInstruction();
04069 
04070       if (!I)
04071         return error("Invalid record");
04072       I->setDebugLoc(LastLoc);
04073       I = nullptr;
04074       continue;
04075 
04076     case bitc::FUNC_CODE_DEBUG_LOC: {      // DEBUG_LOC: [line, col, scope, ia]
04077       I = getLastInstruction();
04078       if (!I || Record.size() < 4)
04079         return error("Invalid record");
04080 
04081       unsigned Line = Record[0], Col = Record[1];
04082       unsigned ScopeID = Record[2], IAID = Record[3];
04083 
04084       MDNode *Scope = nullptr, *IA = nullptr;
04085       if (ScopeID)
04086         Scope = cast<MDNode>(MetadataList.getValueFwdRef(ScopeID - 1));
04087       if (IAID)
04088         IA = cast<MDNode>(MetadataList.getValueFwdRef(IAID - 1));
04089       LastLoc = DebugLoc::get(Line, Col, Scope, IA);
04090       I->setDebugLoc(LastLoc);
04091       I = nullptr;
04092       continue;
04093     }
04094 
04095     case bitc::FUNC_CODE_INST_BINOP: {    // BINOP: [opval, ty, opval, opcode]
04096       unsigned OpNum = 0;
04097       Value *LHS, *RHS;
04098       if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
04099           popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS) ||
04100           OpNum+1 > Record.size())
04101         return error("Invalid record");
04102 
04103       int Opc = getDecodedBinaryOpcode(Record[OpNum++], LHS->getType());
04104       if (Opc == -1)
04105         return error("Invalid record");
04106       I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
04107       InstructionList.push_back(I);
04108       if (OpNum < Record.size()) {
04109         if (Opc == Instruction::Add ||
04110             Opc == Instruction::Sub ||
04111             Opc == Instruction::Mul ||
04112             Opc == Instruction::Shl) {
04113           if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP))
04114             cast<BinaryOperator>(I)->setHasNoSignedWrap(true);
04115           if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
04116             cast<BinaryOperator>(I)->setHasNoUnsignedWrap(true);
04117         } else if (Opc == Instruction::SDiv ||
04118                    Opc == Instruction::UDiv ||
04119                    Opc == Instruction::LShr ||
04120                    Opc == Instruction::AShr) {
04121           if (Record[OpNum] & (1 << bitc::PEO_EXACT))
04122             cast<BinaryOperator>(I)->setIsExact(true);
04123         } else if (isa<FPMathOperator>(I)) {
04124           FastMathFlags FMF = getDecodedFastMathFlags(Record[OpNum]);
04125           if (FMF.any())
04126             I->setFastMathFlags(FMF);
04127         }
04128 
04129       }
04130       break;
04131     }
04132     case bitc::FUNC_CODE_INST_CAST: {    // CAST: [opval, opty, destty, castopc]
04133       unsigned OpNum = 0;
04134       Value *Op;
04135       if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
04136           OpNum+2 != Record.size())
04137         return error("Invalid record");
04138 
04139       Type *ResTy = getTypeByID(Record[OpNum]);
04140       int Opc = getDecodedCastOpcode(Record[OpNum + 1]);
04141       if (Opc == -1 || !ResTy)
04142         return error("Invalid record");
04143       Instruction *Temp = nullptr;
04144       if ((I = UpgradeBitCastInst(Opc, Op, ResTy, Temp))) {
04145         if (Temp) {
04146           InstructionList.push_back(Temp);
04147           CurBB->getInstList().push_back(Temp);
04148         }
04149       } else {
04150         auto CastOp = (Instruction::CastOps)Opc;
04151         if (!CastInst::castIsValid(CastOp, Op, ResTy))
04152           return error("Invalid cast");
04153         I = CastInst::Create(CastOp, Op, ResTy);
04154       }
04155       InstructionList.push_back(I);
04156       break;
04157     }
04158     case bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD:
04159     case bitc::FUNC_CODE_INST_GEP_OLD:
04160     case bitc::FUNC_CODE_INST_GEP: { // GEP: type, [n x operands]
04161       unsigned OpNum = 0;
04162 
04163       Type *Ty;
04164       bool InBounds;
04165 
04166       if (BitCode == bitc::FUNC_CODE_INST_GEP) {
04167         InBounds = Record[OpNum++];
04168         Ty = getTypeByID(Record[OpNum++]);
04169       } else {
04170         InBounds = BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD;
04171         Ty = nullptr;
04172       }
04173 
04174       Value *BasePtr;
04175       if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr))
04176         return error("Invalid record");
04177 
04178       if (!Ty)
04179         Ty = cast<SequentialType>(BasePtr->getType()->getScalarType())
04180                  ->getElementType();
04181       else if (Ty !=
04182                cast<SequentialType>(BasePtr->getType()->getScalarType())
04183                    ->getElementType())
04184         return error(
04185             "Explicit gep type does not match pointee type of pointer operand");
04186 
04187       SmallVector<Value*, 16> GEPIdx;
04188       while (OpNum != Record.size()) {
04189         Value *Op;
04190         if (getValueTypePair(Record, OpNum, NextValueNo, Op))
04191           return error("Invalid record");
04192         GEPIdx.push_back(Op);
04193       }
04194 
04195       I = GetElementPtrInst::Create(Ty, BasePtr, GEPIdx);
04196 
04197       InstructionList.push_back(I);
04198       if (InBounds)
04199         cast<GetElementPtrInst>(I)->setIsInBounds(true);
04200       break;
04201     }
04202 
04203     case bitc::FUNC_CODE_INST_EXTRACTVAL: {
04204                                        // EXTRACTVAL: [opty, opval, n x indices]
04205       unsigned OpNum = 0;
04206       Value *Agg;
04207       if (getValueTypePair(Record, OpNum, NextValueNo, Agg))
04208         return error("Invalid record");
04209 
04210       unsigned RecSize = Record.size();
04211       if (OpNum == RecSize)
04212         return error("EXTRACTVAL: Invalid instruction with 0 indices");
04213 
04214       SmallVector<unsigned, 4> EXTRACTVALIdx;
04215       Type *CurTy = Agg->getType();
04216       for (; OpNum != RecSize; ++OpNum) {
04217         bool IsArray = CurTy->isArrayTy();
04218         bool IsStruct = CurTy->isStructTy();
04219         uint64_t Index = Record[OpNum];
04220 
04221         if (!IsStruct && !IsArray)
04222           return error("EXTRACTVAL: Invalid type");
04223         if ((unsigned)Index != Index)
04224           return error("Invalid value");
04225         if (IsStruct && Index >= CurTy->subtypes().size())
04226           return error("EXTRACTVAL: Invalid struct index");
04227         if (IsArray && Index >= CurTy->getArrayNumElements())
04228           return error("EXTRACTVAL: Invalid array index");
04229         EXTRACTVALIdx.push_back((unsigned)Index);
04230 
04231         if (IsStruct)
04232           CurTy = CurTy->subtypes()[Index];
04233         else
04234           CurTy = CurTy->subtypes()[0];
04235       }
04236 
04237       I = ExtractValueInst::Create(Agg, EXTRACTVALIdx);
04238       InstructionList.push_back(I);
04239       break;
04240     }
04241 
04242     case bitc::FUNC_CODE_INST_INSERTVAL: {
04243                            // INSERTVAL: [opty, opval, opty, opval, n x indices]
04244       unsigned OpNum = 0;
04245       Value *Agg;
04246       if (getValueTypePair(Record, OpNum, NextValueNo, Agg))
04247         return error("Invalid record");
04248       Value *Val;
04249       if (getValueTypePair(Record, OpNum, NextValueNo, Val))
04250         return error("Invalid record");
04251 
04252       unsigned RecSize = Record.size();
04253       if (OpNum == RecSize)
04254         return error("INSERTVAL: Invalid instruction with 0 indices");
04255 
04256       SmallVector<unsigned, 4> INSERTVALIdx;
04257       Type *CurTy = Agg->getType();
04258       for (; OpNum != RecSize; ++OpNum) {
04259         bool IsArray = CurTy->isArrayTy();
04260         bool IsStruct = CurTy->isStructTy();
04261         uint64_t Index = Record[OpNum];
04262 
04263         if (!IsStruct && !IsArray)
04264           return error("INSERTVAL: Invalid type");
04265         if ((unsigned)Index != Index)
04266           return error("Invalid value");
04267         if (IsStruct && Index >= CurTy->subtypes().size())
04268           return error("INSERTVAL: Invalid struct index");
04269         if (IsArray && Index >= CurTy->getArrayNumElements())
04270           return error("INSERTVAL: Invalid array index");
04271 
04272         INSERTVALIdx.push_back((unsigned)Index);
04273         if (IsStruct)
04274           CurTy = CurTy->subtypes()[Index];
04275         else
04276           CurTy = CurTy->subtypes()[0];
04277       }
04278 
04279       if (CurTy != Val->getType())
04280         return error("Inserted value type doesn't match aggregate type");
04281 
04282       I = InsertValueInst::Create(Agg, Val, INSERTVALIdx);
04283       InstructionList.push_back(I);
04284       break;
04285     }
04286 
04287     case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval]
04288       // obsolete form of select
04289       // handles select i1 ... in old bitcode
04290       unsigned OpNum = 0;
04291       Value *TrueVal, *FalseVal, *Cond;
04292       if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) ||
04293           popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) ||
04294           popValue(Record, OpNum, NextValueNo, Type::getInt1Ty(Context), Cond))
04295         return error("Invalid record");
04296 
04297       I = SelectInst::Create(Cond, TrueVal, FalseVal);
04298       InstructionList.push_back(I);
04299       break;
04300     }
04301 
04302     case bitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [ty,opval,opval,predty,pred]
04303       // new form of select
04304       // handles select i1 or select [N x i1]
04305       unsigned OpNum = 0;
04306       Value *TrueVal, *FalseVal, *Cond;
04307       if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) ||
04308           popValue(Record, OpNum, NextValueNo, TrueVal->getType(), FalseVal) ||
04309           getValueTypePair(Record, OpNum, NextValueNo, Cond))
04310         return error("Invalid record");
04311 
04312       // select condition can be either i1 or [N x i1]
04313       if (VectorType* vector_type =
04314           dyn_cast<VectorType>(Cond->getType())) {
04315         // expect <n x i1>
04316         if (vector_type->getElementType() != Type::getInt1Ty(Context))
04317           return error("Invalid type for value");
04318       } else {
04319         // expect i1
04320         if (Cond->getType() != Type::getInt1Ty(Context))
04321           return error("Invalid type for value");
04322       }
04323 
04324       I = SelectInst::Create(Cond, TrueVal, FalseVal);
04325       InstructionList.push_back(I);
04326       break;
04327     }
04328 
04329     case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval]
04330       unsigned OpNum = 0;
04331       Value *Vec, *Idx;
04332       if (getValueTypePair(Record, OpNum, NextValueNo, Vec) ||
04333           getValueTypePair(Record, OpNum, NextValueNo, Idx))
04334         return error("Invalid record");
04335       if (!Vec->getType()->isVectorTy())
04336         return error("Invalid type for value");
04337       I = ExtractElementInst::Create(Vec, Idx);
04338       InstructionList.push_back(I);
04339       break;
04340     }
04341 
04342     case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval]
04343       unsigned OpNum = 0;
04344       Value *Vec, *Elt, *Idx;
04345       if (getValueTypePair(Record, OpNum, NextValueNo, Vec))
04346         return error("Invalid record");
04347       if (!Vec->getType()->isVectorTy())
04348         return error("Invalid type for value");
04349       if (popValue(Record, OpNum, NextValueNo,
04350                    cast<VectorType>(Vec->getType())->getElementType(), Elt) ||
04351           getValueTypePair(Record, OpNum, NextValueNo, Idx))
04352         return error("Invalid record");
04353       I = InsertElementInst::Create(Vec, Elt, Idx);
04354       InstructionList.push_back(I);
04355       break;
04356     }
04357 
04358     case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval]
04359       unsigned OpNum = 0;
04360       Value *Vec1, *Vec2, *Mask;
04361       if (getValueTypePair(Record, OpNum, NextValueNo, Vec1) ||
04362           popValue(Record, OpNum, NextValueNo, Vec1->getType(), Vec2))
04363         return error("Invalid record");
04364 
04365       if (getValueTypePair(Record, OpNum, NextValueNo, Mask))
04366         return error("Invalid record");
04367       if (!Vec1->getType()->isVectorTy() || !Vec2->getType()->isVectorTy())
04368         return error("Invalid type for value");
04369       I = new ShuffleVectorInst(Vec1, Vec2, Mask);
04370       InstructionList.push_back(I);
04371       break;
04372     }
04373 
04374     case bitc::FUNC_CODE_INST_CMP:   // CMP: [opty, opval, opval, pred]
04375       // Old form of ICmp/FCmp returning bool
04376       // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were
04377       // both legal on vectors but had different behaviour.
04378     case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred]
04379       // FCmp/ICmp returning bool or vector of bool
04380 
04381       unsigned OpNum = 0;
04382       Value *LHS, *RHS;
04383       if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
04384           popValue(Record, OpNum, NextValueNo, LHS->getType(), RHS))
04385         return error("Invalid record");
04386 
04387       unsigned PredVal = Record[OpNum];
04388       bool IsFP = LHS->getType()->isFPOrFPVectorTy();
04389       FastMathFlags FMF;
04390       if (IsFP && Record.size() > OpNum+1)
04391         FMF = getDecodedFastMathFlags(Record[++OpNum]);
04392 
04393       if (OpNum+1 != Record.size())
04394         return error("Invalid record");
04395 
04396       if (LHS->getType()->isFPOrFPVectorTy())
04397         I = new FCmpInst((FCmpInst::Predicate)PredVal, LHS, RHS);
04398       else
04399         I = new ICmpInst((ICmpInst::Predicate)PredVal, LHS, RHS);
04400 
04401       if (FMF.any())
04402         I->setFastMathFlags(FMF);
04403       InstructionList.push_back(I);
04404       break;
04405     }
04406 
04407     case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>]
04408       {
04409         unsigned Size = Record.size();
04410         if (Size == 0) {
04411           I = ReturnInst::Create(Context);
04412           InstructionList.push_back(I);
04413           break;
04414         }
04415 
04416         unsigned OpNum = 0;
04417         Value *Op = nullptr;
04418         if (getValueTypePair(Record, OpNum, NextValueNo, Op))
04419           return error("Invalid record");
04420         if (OpNum != Record.size())
04421           return error("Invalid record");
04422 
04423         I = ReturnInst::Create(Context, Op);
04424         InstructionList.push_back(I);
04425         break;
04426       }
04427     case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#]
04428       if (Record.size() != 1 && Record.size() != 3)
04429         return error("Invalid record");
04430       BasicBlock *TrueDest = getBasicBlock(Record[0]);
04431       if (!TrueDest)
04432         return error("Invalid record");
04433 
04434       if (Record.size() == 1) {
04435         I = BranchInst::Create(TrueDest);
04436         InstructionList.push_back(I);
04437       }
04438       else {
04439         BasicBlock *FalseDest = getBasicBlock(Record[1]);
04440         Value *Cond = getValue(Record, 2, NextValueNo,
04441                                Type::getInt1Ty(Context));
04442         if (!FalseDest || !Cond)
04443           return error("Invalid record");
04444         I = BranchInst::Create(TrueDest, FalseDest, Cond);
04445         InstructionList.push_back(I);
04446       }
04447       break;
04448     }
04449     case bitc::FUNC_CODE_INST_CLEANUPRET: { // CLEANUPRET: [val] or [val,bb#]
04450       if (Record.size() != 1 && Record.size() != 2)
04451         return error("Invalid record");
04452       unsigned Idx = 0;
04453       Value *CleanupPad =
04454           getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context));
04455       if (!CleanupPad)
04456         return error("Invalid record");
04457       BasicBlock *UnwindDest = nullptr;
04458       if (Record.size() == 2) {
04459         UnwindDest = getBasicBlock(Record[Idx++]);
04460         if (!UnwindDest)
04461           return error("Invalid record");
04462       }
04463 
04464       I = CleanupReturnInst::Create(CleanupPad, UnwindDest);
04465       InstructionList.push_back(I);
04466       break;
04467     }
04468     case bitc::FUNC_CODE_INST_CATCHRET: { // CATCHRET: [val,bb#]
04469       if (Record.size() != 2)
04470         return error("Invalid record");
04471       unsigned Idx = 0;
04472       Value *CatchPad =
04473           getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context));
04474       if (!CatchPad)
04475         return error("Invalid record");
04476       BasicBlock *BB = getBasicBlock(Record[Idx++]);
04477       if (!BB)
04478         return error("Invalid record");
04479 
04480       I = CatchReturnInst::Create(CatchPad, BB);
04481       InstructionList.push_back(I);
04482       break;
04483     }
04484     case bitc::FUNC_CODE_INST_CATCHSWITCH: { // CATCHSWITCH: [tok,num,(bb)*,bb?]
04485       // We must have, at minimum, the outer scope and the number of arguments.
04486       if (Record.size() < 2)
04487         return error("Invalid record");
04488 
04489       unsigned Idx = 0;
04490 
04491       Value *ParentPad =
04492           getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context));
04493 
04494       unsigned NumHandlers = Record[Idx++];
04495 
04496       SmallVector<BasicBlock *, 2> Handlers;
04497       for (unsigned Op = 0; Op != NumHandlers; ++Op) {
04498         BasicBlock *BB = getBasicBlock(Record[Idx++]);
04499         if (!BB)
04500           return error("Invalid record");
04501         Handlers.push_back(BB);
04502       }
04503 
04504       BasicBlock *UnwindDest = nullptr;
04505       if (Idx + 1 == Record.size()) {
04506         UnwindDest = getBasicBlock(Record[Idx++]);
04507         if (!UnwindDest)
04508           return error("Invalid record");
04509       }
04510 
04511       if (Record.size() != Idx)
04512         return error("Invalid record");
04513 
04514       auto *CatchSwitch =
04515           CatchSwitchInst::Create(ParentPad, UnwindDest, NumHandlers);
04516       for (BasicBlock *Handler : Handlers)
04517         CatchSwitch->addHandler(Handler);
04518       I = CatchSwitch;
04519       InstructionList.push_back(I);
04520       break;
04521     }
04522     case bitc::FUNC_CODE_INST_CATCHPAD:
04523     case bitc::FUNC_CODE_INST_CLEANUPPAD: { // [tok,num,(ty,val)*]
04524       // We must have, at minimum, the outer scope and the number of arguments.
04525       if (Record.size() < 2)
04526         return error("Invalid record");
04527 
04528       unsigned Idx = 0;
04529 
04530       Value *ParentPad =
04531           getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context));
04532 
04533       unsigned NumArgOperands = Record[Idx++];
04534 
04535       SmallVector<Value *, 2> Args;
04536       for (unsigned Op = 0; Op != NumArgOperands; ++Op) {
04537         Value *Val;
04538         if (getValueTypePair(Record, Idx, NextValueNo, Val))
04539           return error("Invalid record");
04540         Args.push_back(Val);
04541       }
04542 
04543       if (Record.size() != Idx)
04544         return error("Invalid record");
04545 
04546       if (BitCode == bitc::FUNC_CODE_INST_CLEANUPPAD)
04547         I = CleanupPadInst::Create(ParentPad, Args);
04548       else
04549         I = CatchPadInst::Create(ParentPad, Args);
04550       InstructionList.push_back(I);
04551       break;
04552     }
04553     case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...]
04554       // Check magic
04555       if ((Record[0] >> 16) == SWITCH_INST_MAGIC) {
04556         // "New" SwitchInst format with case ranges. The changes to write this
04557         // format were reverted but we still recognize bitcode that uses it.
04558         // Hopefully someday we will have support for case ranges and can use
04559         // this format again.
04560 
04561         Type *OpTy = getTypeByID(Record[1]);
04562         unsigned ValueBitWidth = cast<IntegerType>(OpTy)->getBitWidth();
04563 
04564         Value *Cond = getValue(Record, 2, NextValueNo, OpTy);
04565         BasicBlock *Default = getBasicBlock(Record[3]);
04566         if (!OpTy || !Cond || !Default)
04567           return error("Invalid record");
04568 
04569         unsigned NumCases = Record[4];
04570 
04571         SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
04572         InstructionList.push_back(SI);
04573 
04574         unsigned CurIdx = 5;
04575         for (unsigned i = 0; i != NumCases; ++i) {
04576           SmallVector<ConstantInt*, 1> CaseVals;
04577           unsigned NumItems = Record[CurIdx++];
04578           for (unsigned ci = 0; ci != NumItems; ++ci) {
04579             bool isSingleNumber = Record[CurIdx++];
04580 
04581             APInt Low;
04582             unsigned ActiveWords = 1;
04583             if (ValueBitWidth > 64)
04584               ActiveWords = Record[CurIdx++];
04585             Low = readWideAPInt(makeArrayRef(&Record[CurIdx], ActiveWords),
04586                                 ValueBitWidth);
04587             CurIdx += ActiveWords;
04588 
04589             if (!isSingleNumber) {
04590               ActiveWords = 1;
04591               if (ValueBitWidth > 64)
04592                 ActiveWords = Record[CurIdx++];
04593               APInt High = readWideAPInt(
04594                   makeArrayRef(&Record[CurIdx], ActiveWords), ValueBitWidth);
04595               CurIdx += ActiveWords;
04596 
04597               // FIXME: It is not clear whether values in the range should be
04598               // compared as signed or unsigned values. The partially
04599               // implemented changes that used this format in the past used
04600               // unsigned comparisons.
04601               for ( ; Low.ule(High); ++Low)
04602                 CaseVals.push_back(ConstantInt::get(Context, Low));
04603             } else
04604               CaseVals.push_back(ConstantInt::get(Context, Low));
04605           }
04606           BasicBlock *DestBB = getBasicBlock(Record[CurIdx++]);
04607           for (SmallVector<ConstantInt*, 1>::iterator cvi = CaseVals.begin(),
04608                  cve = CaseVals.end(); cvi != cve; ++cvi)
04609             SI->addCase(*cvi, DestBB);
04610         }
04611         I = SI;
04612         break;
04613       }
04614 
04615       // Old SwitchInst format without case ranges.
04616 
04617       if (Record.size() < 3 || (Record.size() & 1) == 0)
04618         return error("Invalid record");
04619       Type *OpTy = getTypeByID(Record[0]);
04620       Value *Cond = getValue(Record, 1, NextValueNo, OpTy);
04621       BasicBlock *Default = getBasicBlock(Record[2]);
04622       if (!OpTy || !Cond || !Default)
04623         return error("Invalid record");
04624       unsigned NumCases = (Record.size()-3)/2;
04625       SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
04626       InstructionList.push_back(SI);
04627       for (unsigned i = 0, e = NumCases; i != e; ++i) {
04628         ConstantInt *CaseVal =
04629           dyn_cast_or_null<ConstantInt>(getFnValueByID(Record[3+i*2], OpTy));
04630         BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]);
04631         if (!CaseVal || !DestBB) {
04632           delete SI;
04633           return error("Invalid record");
04634         }
04635         SI->addCase(CaseVal, DestBB);
04636       }
04637       I = SI;
04638       break;
04639     }
04640     case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...]
04641       if (Record.size() < 2)
04642         return error("Invalid record");
04643       Type *OpTy = getTypeByID(Record[0]);
04644       Value *Address = getValue(Record, 1, NextValueNo, OpTy);
04645       if (!OpTy || !Address)
04646         return error("Invalid record");
04647       unsigned NumDests = Record.size()-2;
04648       IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests);
04649       InstructionList.push_back(IBI);
04650       for (unsigned i = 0, e = NumDests; i != e; ++i) {
04651         if (BasicBlock *DestBB = getBasicBlock(Record[2+i])) {
04652           IBI->addDestination(DestBB);
04653         } else {
04654           delete IBI;
04655           return error("Invalid record");
04656         }
04657       }
04658       I = IBI;
04659       break;
04660     }
04661 
04662     case bitc::FUNC_CODE_INST_INVOKE: {
04663       // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...]
04664       if (Record.size() < 4)
04665         return error("Invalid record");
04666       unsigned OpNum = 0;
04667       AttributeSet PAL = getAttributes(Record[OpNum++]);
04668       unsigned CCInfo = Record[OpNum++];
04669       BasicBlock *NormalBB = getBasicBlock(Record[OpNum++]);
04670       BasicBlock *UnwindBB = getBasicBlock(Record[OpNum++]);
04671 
04672       FunctionType *FTy = nullptr;
04673       if (CCInfo >> 13 & 1 &&
04674           !(FTy = dyn_cast<FunctionType>(getTypeByID(Record[OpNum++]))))
04675         return error("Explicit invoke type is not a function type");
04676 
04677       Value *Callee;
04678       if (getValueTypePair(Record, OpNum, NextValueNo, Callee))
04679         return error("Invalid record");
04680 
04681       PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType());
04682       if (!CalleeTy)
04683         return error("Callee is not a pointer");
04684       if (!FTy) {
04685         FTy = dyn_cast<FunctionType>(CalleeTy->getElementType());
04686         if (!FTy)
04687           return error("Callee is not of pointer to function type");
04688       } else if (CalleeTy->getElementType() != FTy)
04689         return error("Explicit invoke type does not match pointee type of "
04690                      "callee operand");
04691       if (Record.size() < FTy->getNumParams() + OpNum)
04692         return error("Insufficient operands to call");
04693 
04694       SmallVector<Value*, 16> Ops;
04695       for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
04696         Ops.push_back(getValue(Record, OpNum, NextValueNo,
04697                                FTy->getParamType(i)));
04698         if (!Ops.back())
04699           return error("Invalid record");
04700       }
04701 
04702       if (!FTy->isVarArg()) {
04703         if (Record.size() != OpNum)
04704           return error("Invalid record");
04705       } else {
04706         // Read type/value pairs for varargs params.
04707         while (OpNum != Record.size()) {
04708           Value *Op;
04709           if (getValueTypePair(Record, OpNum, NextValueNo, Op))
04710             return error("Invalid record");
04711           Ops.push_back(Op);
04712         }
04713       }
04714 
04715       I = InvokeInst::Create(Callee, NormalBB, UnwindBB, Ops, OperandBundles);
04716       OperandBundles.clear();
04717       InstructionList.push_back(I);
04718       cast<InvokeInst>(I)->setCallingConv(
04719           static_cast<CallingConv::ID>(CallingConv::MaxID & CCInfo));
04720       cast<InvokeInst>(I)->setAttributes(PAL);
04721       break;
04722     }
04723     case bitc::FUNC_CODE_INST_RESUME: { // RESUME: [opval]
04724       unsigned Idx = 0;
04725       Value *Val = nullptr;
04726       if (getValueTypePair(Record, Idx, NextValueNo, Val))
04727         return error("Invalid record");
04728       I = ResumeInst::Create(Val);
04729       InstructionList.push_back(I);
04730       break;
04731     }
04732     case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE
04733       I = new UnreachableInst(Context);
04734       InstructionList.push_back(I);
04735       break;
04736     case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...]
04737       if (Record.size() < 1 || ((Record.size()-1)&1))
04738         return error("Invalid record");
04739       Type *Ty = getTypeByID(Record[0]);
04740       if (!Ty)
04741         return error("Invalid record");
04742 
04743       PHINode *PN = PHINode::Create(Ty, (Record.size()-1)/2);
04744       InstructionList.push_back(PN);
04745 
04746       for (unsigned i = 0, e = Record.size()-1; i != e; i += 2) {
04747         Value *V;
04748         // With the new function encoding, it is possible that operands have
04749         // negative IDs (for forward references).  Use a signed VBR
04750         // representation to keep the encoding small.
04751         if (UseRelativeIDs)
04752           V = getValueSigned(Record, 1+i, NextValueNo, Ty);
04753         else
04754           V = getValue(Record, 1+i, NextValueNo, Ty);
04755         BasicBlock *BB = getBasicBlock(Record[2+i]);
04756         if (!V || !BB)
04757           return error("Invalid record");
04758         PN->addIncoming(V, BB);
04759       }
04760       I = PN;
04761       break;
04762     }
04763 
04764     case bitc::FUNC_CODE_INST_LANDINGPAD:
04765     case bitc::FUNC_CODE_INST_LANDINGPAD_OLD: {
04766       // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?]
04767       unsigned Idx = 0;
04768       if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD) {
04769         if (Record.size() < 3)
04770           return error("Invalid record");
04771       } else {
04772         assert(BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD);
04773         if (Record.size() < 4)
04774           return error("Invalid record");
04775       }
04776       Type *Ty = getTypeByID(Record[Idx++]);
04777       if (!Ty)
04778         return error("Invalid record");
04779       if (BitCode == bitc::FUNC_CODE_INST_LANDINGPAD_OLD) {
04780         Value *PersFn = nullptr;
04781         if (getValueTypePair(Record, Idx, NextValueNo, PersFn))
04782           return error("Invalid record");
04783 
04784         if (!F->hasPersonalityFn())
04785           F->setPersonalityFn(cast<Constant>(PersFn));
04786         else if (F->getPersonalityFn() != cast<Constant>(PersFn))
04787           return error("Personality function mismatch");
04788       }
04789 
04790       bool IsCleanup = !!Record[Idx++];
04791       unsigned NumClauses = Record[Idx++];
04792       LandingPadInst *LP = LandingPadInst::Create(Ty, NumClauses);
04793       LP->setCleanup(IsCleanup);
04794       for (unsigned J = 0; J != NumClauses; ++J) {
04795         LandingPadInst::ClauseType CT =
04796           LandingPadInst::ClauseType(Record[Idx++]); (void)CT;
04797         Value *Val;
04798 
04799         if (getValueTypePair(Record, Idx, NextValueNo, Val)) {
04800           delete LP;
04801           return error("Invalid record");
04802         }
04803 
04804         assert((CT != LandingPadInst::Catch ||
04805                 !isa<ArrayType>(Val->getType())) &&
04806                "Catch clause has a invalid type!");
04807         assert((CT != LandingPadInst::Filter ||
04808                 isa<ArrayType>(Val->getType())) &&
04809                "Filter clause has invalid type!");
04810         LP->addClause(cast<Constant>(Val));
04811       }
04812 
04813       I = LP;
04814       InstructionList.push_back(I);
04815       break;
04816     }
04817 
04818     case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align]
04819       if (Record.size() != 4)
04820         return error("Invalid record");
04821       uint64_t AlignRecord = Record[3];
04822       const uint64_t InAllocaMask = uint64_t(1) << 5;
04823       const uint64_t ExplicitTypeMask = uint64_t(1) << 6;
04824       // Reserve bit 7 for SwiftError flag.
04825       // const uint64_t SwiftErrorMask = uint64_t(1) << 7;
04826       const uint64_t FlagMask = InAllocaMask | ExplicitTypeMask;
04827       bool InAlloca = AlignRecord & InAllocaMask;
04828       Type *Ty = getTypeByID(Record[0]);
04829       if ((AlignRecord & ExplicitTypeMask) == 0) {
04830         auto *PTy = dyn_cast_or_null<PointerType>(Ty);
04831         if (!PTy)
04832           return error("Old-style alloca with a non-pointer type");
04833         Ty = PTy->getElementType();
04834       }
04835       Type *OpTy = getTypeByID(Record[1]);
04836       Value *Size = getFnValueByID(Record[2], OpTy);
04837       unsigned Align;
04838       if (std::error_code EC =
04839               parseAlignmentValue(AlignRecord & ~FlagMask, Align)) {
04840         return EC;
04841       }
04842       if (!Ty || !Size)
04843         return error("Invalid record");
04844       AllocaInst *AI = new AllocaInst(Ty, Size, Align);
04845       AI->setUsedWithInAlloca(InAlloca);
04846       I = AI;
04847       InstructionList.push_back(I);
04848       break;
04849     }
04850     case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol]
04851       unsigned OpNum = 0;
04852       Value *Op;
04853       if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
04854           (OpNum + 2 != Record.size() && OpNum + 3 != Record.size()))
04855         return error("Invalid record");
04856 
04857       Type *Ty = nullptr;
04858       if (OpNum + 3 == Record.size())
04859         Ty = getTypeByID(Record[OpNum++]);
04860       if (std::error_code EC = typeCheckLoadStoreInst(Ty, Op->getType()))
04861         return EC;
04862       if (!Ty)
04863         Ty = cast<PointerType>(Op->getType())->getElementType();
04864 
04865       unsigned Align;
04866       if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align))
04867         return EC;
04868       I = new LoadInst(Ty, Op, "", Record[OpNum + 1], Align);
04869 
04870       InstructionList.push_back(I);
04871       break;
04872     }
04873     case bitc::FUNC_CODE_INST_LOADATOMIC: {
04874        // LOADATOMIC: [opty, op, align, vol, ordering, synchscope]
04875       unsigned OpNum = 0;
04876       Value *Op;
04877       if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
04878           (OpNum + 4 != Record.size() && OpNum + 5 != Record.size()))
04879         return error("Invalid record");
04880 
04881       Type *Ty = nullptr;
04882       if (OpNum + 5 == Record.size())
04883         Ty = getTypeByID(Record[OpNum++]);
04884       if (std::error_code EC = typeCheckLoadStoreInst(Ty, Op->getType()))
04885         return EC;
04886       if (!Ty)
04887         Ty = cast<PointerType>(Op->getType())->getElementType();
04888 
04889       AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]);
04890       if (Ordering == NotAtomic || Ordering == Release ||
04891           Ordering == AcquireRelease)
04892         return error("Invalid record");
04893       if (Ordering != NotAtomic && Record[OpNum] == 0)
04894         return error("Invalid record");
04895       SynchronizationScope SynchScope = getDecodedSynchScope(Record[OpNum + 3]);
04896 
04897       unsigned Align;
04898       if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align))
04899         return EC;
04900       I = new LoadInst(Op, "", Record[OpNum+1], Align, Ordering, SynchScope);
04901 
04902       InstructionList.push_back(I);
04903       break;
04904     }
04905     case bitc::FUNC_CODE_INST_STORE:
04906     case bitc::FUNC_CODE_INST_STORE_OLD: { // STORE2:[ptrty, ptr, val, align, vol]
04907       unsigned OpNum = 0;
04908       Value *Val, *Ptr;
04909       if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
04910           (BitCode == bitc::FUNC_CODE_INST_STORE
04911                ? getValueTypePair(Record, OpNum, NextValueNo, Val)
04912                : popValue(Record, OpNum, NextValueNo,
04913                           cast<PointerType>(Ptr->getType())->getElementType(),
04914                           Val)) ||
04915           OpNum + 2 != Record.size())
04916         return error("Invalid record");
04917 
04918       if (std::error_code EC =
04919               typeCheckLoadStoreInst(Val->getType(), Ptr->getType()))
04920         return EC;
04921       unsigned Align;
04922       if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align))
04923         return EC;
04924       I = new StoreInst(Val, Ptr, Record[OpNum+1], Align);
04925       InstructionList.push_back(I);
04926       break;
04927     }
04928     case bitc::FUNC_CODE_INST_STOREATOMIC:
04929     case bitc::FUNC_CODE_INST_STOREATOMIC_OLD: {
04930       // STOREATOMIC: [ptrty, ptr, val, align, vol, ordering, synchscope]
04931       unsigned OpNum = 0;
04932       Value *Val, *Ptr;
04933       if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
04934           (BitCode == bitc::FUNC_CODE_INST_STOREATOMIC
04935                ? getValueTypePair(Record, OpNum, NextValueNo, Val)
04936                : popValue(Record, OpNum, NextValueNo,
04937                           cast<PointerType>(Ptr->getType())->getElementType(),
04938                           Val)) ||
04939           OpNum + 4 != Record.size())
04940         return error("Invalid record");
04941 
04942       if (std::error_code EC =
04943               typeCheckLoadStoreInst(Val->getType(), Ptr->getType()))
04944         return EC;
04945       AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]);
04946       if (Ordering == NotAtomic || Ordering == Acquire ||
04947           Ordering == AcquireRelease)
04948         return error("Invalid record");
04949       SynchronizationScope SynchScope = getDecodedSynchScope(Record[OpNum + 3]);
04950       if (Ordering != NotAtomic && Record[OpNum] == 0)
04951         return error("Invalid record");
04952 
04953       unsigned Align;
04954       if (std::error_code EC = parseAlignmentValue(Record[OpNum], Align))
04955         return EC;
04956       I = new StoreInst(Val, Ptr, Record[OpNum+1], Align, Ordering, SynchScope);
04957       InstructionList.push_back(I);
04958       break;
04959     }
04960     case bitc::FUNC_CODE_INST_CMPXCHG_OLD:
04961     case bitc::FUNC_CODE_INST_CMPXCHG: {
04962       // CMPXCHG:[ptrty, ptr, cmp, new, vol, successordering, synchscope,
04963       //          failureordering?, isweak?]
04964       unsigned OpNum = 0;
04965       Value *Ptr, *Cmp, *New;
04966       if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
04967           (BitCode == bitc::FUNC_CODE_INST_CMPXCHG
04968                ? getValueTypePair(Record, OpNum, NextValueNo, Cmp)
04969                : popValue(Record, OpNum, NextValueNo,
04970                           cast<PointerType>(Ptr->getType())->getElementType(),
04971                           Cmp)) ||
04972           popValue(Record, OpNum, NextValueNo, Cmp->getType(), New) ||
04973           Record.size() < OpNum + 3 || Record.size() > OpNum + 5)
04974         return error("Invalid record");
04975       AtomicOrdering SuccessOrdering = getDecodedOrdering(Record[OpNum + 1]);
04976       if (SuccessOrdering == NotAtomic || SuccessOrdering == Unordered)
04977         return error("Invalid record");
04978       SynchronizationScope SynchScope = getDecodedSynchScope(Record[OpNum + 2]);
04979 
04980       if (std::error_code EC =
04981               typeCheckLoadStoreInst(Cmp->getType(), Ptr->getType()))
04982         return EC;
04983       AtomicOrdering FailureOrdering;
04984       if (Record.size() < 7)
04985         FailureOrdering =
04986             AtomicCmpXchgInst::getStrongestFailureOrdering(SuccessOrdering);
04987       else
04988         FailureOrdering = getDecodedOrdering(Record[OpNum + 3]);
04989 
04990       I = new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering, FailureOrdering,
04991                                 SynchScope);
04992       cast<AtomicCmpXchgInst>(I)->setVolatile(Record[OpNum]);
04993 
04994       if (Record.size() < 8) {
04995         // Before weak cmpxchgs existed, the instruction simply returned the
04996         // value loaded from memory, so bitcode files from that era will be
04997         // expecting the first component of a modern cmpxchg.
04998         CurBB->getInstList().push_back(I);
04999         I = ExtractValueInst::Create(I, 0);
05000       } else {
05001         cast<AtomicCmpXchgInst>(I)->setWeak(Record[OpNum+4]);
05002       }
05003 
05004       InstructionList.push_back(I);
05005       break;
05006     }
05007     case bitc::FUNC_CODE_INST_ATOMICRMW: {
05008       // ATOMICRMW:[ptrty, ptr, val, op, vol, ordering, synchscope]
05009       unsigned OpNum = 0;
05010       Value *Ptr, *Val;
05011       if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
05012           popValue(Record, OpNum, NextValueNo,
05013                     cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
05014           OpNum+4 != Record.size())
05015         return error("Invalid record");
05016       AtomicRMWInst::BinOp Operation = getDecodedRMWOperation(Record[OpNum]);
05017       if (Operation < AtomicRMWInst::FIRST_BINOP ||
05018           Operation > AtomicRMWInst::LAST_BINOP)
05019         return error("Invalid record");
05020       AtomicOrdering Ordering = getDecodedOrdering(Record[OpNum + 2]);
05021       if (Ordering == NotAtomic || Ordering == Unordered)
05022         return error("Invalid record");
05023       SynchronizationScope SynchScope = getDecodedSynchScope(Record[OpNum + 3]);
05024       I = new AtomicRMWInst(Operation, Ptr, Val, Ordering, SynchScope);
05025       cast<AtomicRMWInst>(I)->setVolatile(Record[OpNum+1]);
05026       InstructionList.push_back(I);
05027       break;
05028     }
05029     case bitc::FUNC_CODE_INST_FENCE: { // FENCE:[ordering, synchscope]
05030       if (2 != Record.size())
05031         return error("Invalid record");
05032       AtomicOrdering Ordering = getDecodedOrdering(Record[0]);
05033       if (Ordering == NotAtomic || Ordering == Unordered ||
05034           Ordering == Monotonic)
05035         return error("Invalid record");
05036       SynchronizationScope SynchScope = getDecodedSynchScope(Record[1]);
05037       I = new FenceInst(Context, Ordering, SynchScope);
05038       InstructionList.push_back(I);
05039       break;
05040     }
05041     case bitc::FUNC_CODE_INST_CALL: {
05042       // CALL: [paramattrs, cc, fmf, fnty, fnid, arg0, arg1...]
05043       if (Record.size() < 3)
05044         return error("Invalid record");
05045 
05046       unsigned OpNum = 0;
05047       AttributeSet PAL = getAttributes(Record[OpNum++]);
05048       unsigned CCInfo = Record[OpNum++];
05049 
05050       FastMathFlags FMF;
05051       if ((CCInfo >> bitc::CALL_FMF) & 1) {
05052         FMF = getDecodedFastMathFlags(Record[OpNum++]);
05053         if (!FMF.any())
05054           return error("Fast math flags indicator set for call with no FMF");
05055       }
05056 
05057       FunctionType *FTy = nullptr;
05058       if (CCInfo >> bitc::CALL_EXPLICIT_TYPE & 1 &&
05059           !(FTy = dyn_cast<FunctionType>(getTypeByID(Record[OpNum++]))))
05060         return error("Explicit call type is not a function type");
05061 
05062       Value *Callee;
05063       if (getValueTypePair(Record, OpNum, NextValueNo, Callee))
05064         return error("Invalid record");
05065 
05066       PointerType *OpTy = dyn_cast<PointerType>(Callee->getType());
05067       if (!OpTy)
05068         return error("Callee is not a pointer type");
05069       if (!FTy) {
05070         FTy = dyn_cast<FunctionType>(OpTy->getElementType());
05071         if (!FTy)
05072           return error("Callee is not of pointer to function type");
05073       } else if (OpTy->getElementType() != FTy)
05074         return error("Explicit call type does not match pointee type of "
05075                      "callee operand");
05076       if (Record.size() < FTy->getNumParams() + OpNum)
05077         return error("Insufficient operands to call");
05078 
05079       SmallVector<Value*, 16> Args;
05080       // Read the fixed params.
05081       for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
05082         if (FTy->getParamType(i)->isLabelTy())
05083           Args.push_back(getBasicBlock(Record[OpNum]));
05084         else
05085           Args.push_back(getValue(Record, OpNum, NextValueNo,
05086                                   FTy->getParamType(i)));
05087         if (!Args.back())
05088           return error("Invalid record");
05089       }
05090 
05091       // Read type/value pairs for varargs params.
05092       if (!FTy->isVarArg()) {
05093         if (OpNum != Record.size())
05094           return error("Invalid record");
05095       } else {
05096         while (OpNum != Record.size()) {
05097           Value *Op;
05098           if (getValueTypePair(Record, OpNum, NextValueNo, Op))
05099             return error("Invalid record");
05100           Args.push_back(Op);
05101         }
05102       }
05103 
05104       I = CallInst::Create(FTy, Callee, Args, OperandBundles);
05105       OperandBundles.clear();
05106       InstructionList.push_back(I);
05107       cast<CallInst>(I)->setCallingConv(
05108           static_cast<CallingConv::ID>((0x7ff & CCInfo) >> bitc::CALL_CCONV));
05109       CallInst::TailCallKind TCK = CallInst::TCK_None;
05110       if (CCInfo & 1 << bitc::CALL_TAIL)
05111         TCK = CallInst::TCK_Tail;
05112       if (CCInfo & (1 << bitc::CALL_MUSTTAIL))
05113         TCK = CallInst::TCK_MustTail;
05114       if (CCInfo & (1 << bitc::CALL_NOTAIL))
05115         TCK = CallInst::TCK_NoTail;
05116       cast<CallInst>(I)->setTailCallKind(TCK);
05117       cast<CallInst>(I)->setAttributes(PAL);
05118       if (FMF.any()) {
05119         if (!isa<FPMathOperator>(I))
05120           return error("Fast-math-flags specified for call without "
05121                        "floating-point scalar or vector return type");
05122         I->setFastMathFlags(FMF);
05123       }
05124       break;
05125     }
05126     case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty]
05127       if (Record.size() < 3)
05128         return error("Invalid record");
05129       Type *OpTy = getTypeByID(Record[0]);
05130       Value *Op = getValue(Record, 1, NextValueNo, OpTy);
05131       Type *ResTy = getTypeByID(Record[2]);
05132       if (!OpTy || !Op || !ResTy)
05133         return error("Invalid record");
05134       I = new VAArgInst(Op, ResTy);
05135       InstructionList.push_back(I);
05136       break;
05137     }
05138 
05139     case bitc::FUNC_CODE_OPERAND_BUNDLE: {
05140       // A call or an invoke can be optionally prefixed with some variable
05141       // number of operand bundle blocks.  These blocks are read into
05142       // OperandBundles and consumed at the next call or invoke instruction.
05143 
05144       if (Record.size() < 1 || Record[0] >= BundleTags.size())
05145         return error("Invalid record");
05146 
05147       std::vector<Value *> Inputs;
05148 
05149       unsigned OpNum = 1;
05150       while (OpNum != Record.size()) {
05151         Value *Op;
05152         if (getValueTypePair(Record, OpNum, NextValueNo, Op))
05153           return error("Invalid record");
05154         Inputs.push_back(Op);
05155       }
05156 
05157       OperandBundles.emplace_back(BundleTags[Record[0]], std::move(Inputs));
05158       continue;
05159     }
05160     }
05161 
05162     // Add instruction to end of current BB.  If there is no current BB, reject
05163     // this file.
05164     if (!CurBB) {
05165       delete I;
05166       return error("Invalid instruction with no BB");
05167     }
05168     if (!OperandBundles.empty()) {
05169       delete I;
05170       return error("Operand bundles found with no consumer");
05171     }
05172     CurBB->getInstList().push_back(I);
05173 
05174     // If this was a terminator instruction, move to the next block.
05175     if (isa<TerminatorInst>(I)) {
05176       ++CurBBNo;
05177       CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : nullptr;
05178     }
05179 
05180     // Non-void values get registered in the value table for future use.
05181     if (I && !I->getType()->isVoidTy())
05182       ValueList.assignValue(I, NextValueNo++);
05183   }
05184 
05185 OutOfRecordLoop:
05186 
05187   if (!OperandBundles.empty())
05188     return error("Operand bundles found with no consumer");
05189 
05190   // Check the function list for unresolved values.
05191   if (Argument *A = dyn_cast<Argument>(ValueList.back())) {
05192     if (!A->getParent()) {
05193       // We found at least one unresolved value.  Nuke them all to avoid leaks.
05194       for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){
05195         if ((A = dyn_cast_or_null<Argument>(ValueList[i])) && !A->getParent()) {
05196           A->replaceAllUsesWith(UndefValue::get(A->getType()));
05197           delete A;
05198         }
05199       }
05200       return error("Never resolved value found in function");
05201     }
05202   }
05203 
05204   // FIXME: Check for unresolved forward-declared metadata references
05205   // and clean up leaks.
05206 
05207   // Trim the value list down to the size it was before we parsed this function.
05208   ValueList.shrinkTo(ModuleValueListSize);
05209   MetadataList.shrinkTo(ModuleMetadataListSize);
05210   std::vector<BasicBlock*>().swap(FunctionBBs);
05211   return std::error_code();
05212 }
05213 
05214 /// Find the function body in the bitcode stream
05215 std::error_code BitcodeReader::findFunctionInStream(
05216     Function *F,
05217     DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator) {
05218   while (DeferredFunctionInfoIterator->second == 0) {
05219     // This is the fallback handling for the old format bitcode that
05220     // didn't contain the function index in the VST, or when we have
05221     // an anonymous function which would not have a VST entry.
05222     // Assert that we have one of those two cases.
05223     assert(VSTOffset == 0 || !F->hasName());
05224     // Parse the next body in the stream and set its position in the
05225     // DeferredFunctionInfo map.
05226     if (std::error_code EC = rememberAndSkipFunctionBodies())
05227       return EC;
05228   }
05229   return std::error_code();
05230 }
05231 
05232 //===----------------------------------------------------------------------===//
05233 // GVMaterializer implementation
05234 //===----------------------------------------------------------------------===//
05235 
05236 void BitcodeReader::releaseBuffer() { Buffer.release(); }
05237 
05238 std::error_code BitcodeReader::materialize(GlobalValue *GV) {
05239   // In older bitcode we must materialize the metadata before parsing
05240   // any functions, in order to set up the MetadataList properly.
05241   if (!SeenModuleValuesRecord) {
05242     if (std::error_code EC = materializeMetadata())
05243       return EC;
05244   }
05245 
05246   Function *F = dyn_cast<Function>(GV);
05247   // If it's not a function or is already material, ignore the request.
05248   if (!F || !F->isMaterializable())
05249     return std::error_code();
05250 
05251   DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F);
05252   assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!");
05253   // If its position is recorded as 0, its body is somewhere in the stream
05254   // but we haven't seen it yet.
05255   if (DFII->second == 0)
05256     if (std::error_code EC = findFunctionInStream(F, DFII))
05257       return EC;
05258 
05259   // Move the bit stream to the saved position of the deferred function body.
05260   Stream.JumpToBit(DFII->second);
05261 
05262   if (std::error_code EC = parseFunctionBody(F))
05263     return EC;
05264   F->setIsMaterializable(false);
05265 
05266   if (StripDebugInfo)
05267     stripDebugInfo(*F);
05268 
05269   // Upgrade any old intrinsic calls in the function.
05270   for (auto &I : UpgradedIntrinsics) {
05271     for (auto UI = I.first->materialized_user_begin(), UE = I.first->user_end();
05272          UI != UE;) {
05273       User *U = *UI;
05274       ++UI;
05275       if (CallInst *CI = dyn_cast<CallInst>(U))
05276         UpgradeIntrinsicCall(CI, I.second);
05277     }
05278   }
05279 
05280   // Finish fn->subprogram upgrade for materialized functions.
05281   if (DISubprogram *SP = FunctionsWithSPs.lookup(F))
05282     F->setSubprogram(SP);
05283 
05284   // Bring in any functions that this function forward-referenced via
05285   // blockaddresses.
05286   return materializeForwardReferencedFunctions();
05287 }
05288 
05289 std::error_code BitcodeReader::materializeModule() {
05290   if (std::error_code EC = materializeMetadata())
05291     return EC;
05292 
05293   // Promise to materialize all forward references.
05294   WillMaterializeAllForwardRefs = true;
05295 
05296   // Iterate over the module, deserializing any functions that are still on
05297   // disk.
05298   for (Function &F : *TheModule) {
05299     if (std::error_code EC = materialize(&F))
05300       return EC;
05301   }
05302   // At this point, if there are any function bodies, parse the rest of
05303   // the bits in the module past the last function block we have recorded
05304   // through either lazy scanning or the VST.
05305   if (LastFunctionBlockBit || NextUnreadBit)
05306     parseModule(LastFunctionBlockBit > NextUnreadBit ? LastFunctionBlockBit
05307                                                      : NextUnreadBit);
05308 
05309   // Check that all block address forward references got resolved (as we
05310   // promised above).
05311   if (!BasicBlockFwdRefs.empty())
05312     return error("Never resolved function from blockaddress");
05313 
05314   // Upgrade any intrinsic calls that slipped through (should not happen!) and
05315   // delete the old functions to clean up. We can't do this unless the entire
05316   // module is materialized because there could always be another function body
05317   // with calls to the old function.
05318   for (auto &I : UpgradedIntrinsics) {
05319     for (auto *U : I.first->users()) {
05320       if (CallInst *CI = dyn_cast<CallInst>(U))
05321         UpgradeIntrinsicCall(CI, I.second);
05322     }
05323     if (!I.first->use_empty())
05324       I.first->replaceAllUsesWith(I.second);
05325     I.first->eraseFromParent();
05326   }
05327   UpgradedIntrinsics.clear();
05328 
05329   for (unsigned I = 0, E = InstsWithTBAATag.size(); I < E; I++)
05330     UpgradeInstWithTBAATag(InstsWithTBAATag[I]);
05331 
05332   UpgradeDebugInfo(*TheModule);
05333   return std::error_code();
05334 }
05335 
05336 std::vector<StructType *> BitcodeReader::getIdentifiedStructTypes() const {
05337   return IdentifiedStructTypes;
05338 }
05339 
05340 std::error_code
05341 BitcodeReader::initStream(std::unique_ptr<DataStreamer> Streamer) {
05342   if (Streamer)
05343     return initLazyStream(std::move(Streamer));
05344   return initStreamFromBuffer();
05345 }
05346 
05347 std::error_code BitcodeReader::initStreamFromBuffer() {
05348   const unsigned char *BufPtr = (const unsigned char*)Buffer->getBufferStart();
05349   const unsigned char *BufEnd = BufPtr+Buffer->getBufferSize();
05350 
05351   if (Buffer->getBufferSize() & 3)
05352     return error("Invalid bitcode signature");
05353 
05354   // If we have a wrapper header, parse it and ignore the non-bc file contents.
05355   // The magic number is 0x0B17C0DE stored in little endian.
05356   if (isBitcodeWrapper(BufPtr, BufEnd))
05357     if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true))
05358       return error("Invalid bitcode wrapper header");
05359 
05360   StreamFile.reset(new BitstreamReader(BufPtr, BufEnd));
05361   Stream.init(&*StreamFile);
05362 
05363   return std::error_code();
05364 }
05365 
05366 std::error_code
05367 BitcodeReader::initLazyStream(std::unique_ptr<DataStreamer> Streamer) {
05368   // Check and strip off the bitcode wrapper; BitstreamReader expects never to
05369   // see it.
05370   auto OwnedBytes =
05371       llvm::make_unique<StreamingMemoryObject>(std::move(Streamer));
05372   StreamingMemoryObject &Bytes = *OwnedBytes;
05373   StreamFile = llvm::make_unique<BitstreamReader>(std::move(OwnedBytes));
05374   Stream.init(&*StreamFile);
05375 
05376   unsigned char buf[16];
05377   if (Bytes.readBytes(buf, 16, 0) != 16)
05378     return error("Invalid bitcode signature");
05379 
05380   if (!isBitcode(buf, buf + 16))
05381     return error("Invalid bitcode signature");
05382 
05383   if (isBitcodeWrapper(buf, buf + 4)) {
05384     const unsigned char *bitcodeStart = buf;
05385     const unsigned char *bitcodeEnd = buf + 16;
05386     SkipBitcodeWrapperHeader(bitcodeStart, bitcodeEnd, false);
05387     Bytes.dropLeadingBytes(bitcodeStart - buf);
05388     Bytes.setKnownObjectSize(bitcodeEnd - bitcodeStart);
05389   }
05390   return std::error_code();
05391 }
05392 
05393 std::error_code FunctionIndexBitcodeReader::error(BitcodeError E,
05394                                                   const Twine &Message) {
05395   return ::error(DiagnosticHandler, make_error_code(E), Message);
05396 }
05397 
05398 std::error_code FunctionIndexBitcodeReader::error(const Twine &Message) {
05399   return ::error(DiagnosticHandler,
05400                  make_error_code(BitcodeError::CorruptedBitcode), Message);
05401 }
05402 
05403 std::error_code FunctionIndexBitcodeReader::error(BitcodeError E) {
05404   return ::error(DiagnosticHandler, make_error_code(E));
05405 }
05406 
05407 FunctionIndexBitcodeReader::FunctionIndexBitcodeReader(
05408     MemoryBuffer *Buffer, DiagnosticHandlerFunction DiagnosticHandler,
05409     bool IsLazy, bool CheckFuncSummaryPresenceOnly)
05410     : DiagnosticHandler(DiagnosticHandler), Buffer(Buffer), IsLazy(IsLazy),
05411       CheckFuncSummaryPresenceOnly(CheckFuncSummaryPresenceOnly) {}
05412 
05413 FunctionIndexBitcodeReader::FunctionIndexBitcodeReader(
05414     DiagnosticHandlerFunction DiagnosticHandler, bool IsLazy,
05415     bool CheckFuncSummaryPresenceOnly)
05416     : DiagnosticHandler(DiagnosticHandler), Buffer(nullptr), IsLazy(IsLazy),
05417       CheckFuncSummaryPresenceOnly(CheckFuncSummaryPresenceOnly) {}
05418 
05419 void FunctionIndexBitcodeReader::freeState() { Buffer = nullptr; }
05420 
05421 void FunctionIndexBitcodeReader::releaseBuffer() { Buffer.release(); }
05422 
05423 // Specialized value symbol table parser used when reading function index
05424 // blocks where we don't actually create global values.
05425 // At the end of this routine the function index is populated with a map
05426 // from function name to FunctionInfo. The function info contains
05427 // the function block's bitcode offset as well as the offset into the
05428 // function summary section.
05429 std::error_code FunctionIndexBitcodeReader::parseValueSymbolTable() {
05430   if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
05431     return error("Invalid record");
05432 
05433   SmallVector<uint64_t, 64> Record;
05434 
05435   // Read all the records for this value table.
05436   SmallString<128> ValueName;
05437   while (1) {
05438     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
05439 
05440     switch (Entry.Kind) {
05441     case BitstreamEntry::SubBlock: // Handled for us already.
05442     case BitstreamEntry::Error:
05443       return error("Malformed block");
05444     case BitstreamEntry::EndBlock:
05445       return std::error_code();
05446     case BitstreamEntry::Record:
05447       // The interesting case.
05448       break;
05449     }
05450 
05451     // Read a record.
05452     Record.clear();
05453     switch (Stream.readRecord(Entry.ID, Record)) {
05454     default: // Default behavior: ignore (e.g. VST_CODE_BBENTRY records).
05455       break;
05456     case bitc::VST_CODE_FNENTRY: {
05457       // VST_FNENTRY: [valueid, offset, namechar x N]
05458       if (convertToString(Record, 2, ValueName))
05459         return error("Invalid record");
05460       unsigned ValueID = Record[0];
05461       uint64_t FuncOffset = Record[1];
05462       std::unique_ptr<FunctionInfo> FuncInfo =
05463           llvm::make_unique<FunctionInfo>(FuncOffset);
05464       if (foundFuncSummary() && !IsLazy) {
05465         DenseMap<uint64_t, std::unique_ptr<FunctionSummary>>::iterator SMI =
05466             SummaryMap.find(ValueID);
05467         assert(SMI != SummaryMap.end() && "Summary info not found");
05468         FuncInfo->setFunctionSummary(std::move(SMI->second));
05469       }
05470       TheIndex->addFunctionInfo(ValueName, std::move(FuncInfo));
05471 
05472       ValueName.clear();
05473       break;
05474     }
05475     case bitc::VST_CODE_COMBINED_FNENTRY: {
05476       // VST_FNENTRY: [offset, namechar x N]
05477       if (convertToString(Record, 1, ValueName))
05478         return error("Invalid record");
05479       uint64_t FuncSummaryOffset = Record[0];
05480       std::unique_ptr<FunctionInfo> FuncInfo =
05481           llvm::make_unique<FunctionInfo>(FuncSummaryOffset);
05482       if (foundFuncSummary() && !IsLazy) {
05483         DenseMap<uint64_t, std::unique_ptr<FunctionSummary>>::iterator SMI =
05484             SummaryMap.find(FuncSummaryOffset);
05485         assert(SMI != SummaryMap.end() && "Summary info not found");
05486         FuncInfo->setFunctionSummary(std::move(SMI->second));
05487       }
05488       TheIndex->addFunctionInfo(ValueName, std::move(FuncInfo));
05489 
05490       ValueName.clear();
05491       break;
05492     }
05493     }
05494   }
05495 }
05496 
05497 // Parse just the blocks needed for function index building out of the module.
05498 // At the end of this routine the function Index is populated with a map
05499 // from function name to FunctionInfo. The function info contains
05500 // either the parsed function summary information (when parsing summaries
05501 // eagerly), or just to the function summary record's offset
05502 // if parsing lazily (IsLazy).
05503 std::error_code FunctionIndexBitcodeReader::parseModule() {
05504   if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
05505     return error("Invalid record");
05506 
05507   // Read the function index for this module.
05508   while (1) {
05509     BitstreamEntry Entry = Stream.advance();
05510 
05511     switch (Entry.Kind) {
05512     case BitstreamEntry::Error:
05513       return error("Malformed block");
05514     case BitstreamEntry::EndBlock:
05515       return std::error_code();
05516 
05517     case BitstreamEntry::SubBlock:
05518       if (CheckFuncSummaryPresenceOnly) {
05519         if (Entry.ID == bitc::FUNCTION_SUMMARY_BLOCK_ID) {
05520           SeenFuncSummary = true;
05521           // No need to parse the rest since we found the summary.
05522           return std::error_code();
05523         }
05524         if (Stream.SkipBlock())
05525           return error("Invalid record");
05526         continue;
05527       }
05528       switch (Entry.ID) {
05529       default: // Skip unknown content.
05530         if (Stream.SkipBlock())
05531           return error("Invalid record");
05532         break;
05533       case bitc::BLOCKINFO_BLOCK_ID:
05534         // Need to parse these to get abbrev ids (e.g. for VST)
05535         if (Stream.ReadBlockInfoBlock())
05536           return error("Malformed block");
05537         break;
05538       case bitc::VALUE_SYMTAB_BLOCK_ID:
05539         if (std::error_code EC = parseValueSymbolTable())
05540           return EC;
05541         break;
05542       case bitc::FUNCTION_SUMMARY_BLOCK_ID:
05543         SeenFuncSummary = true;
05544         if (IsLazy) {
05545           // Lazy parsing of summary info, skip it.
05546           if (Stream.SkipBlock())
05547             return error("Invalid record");
05548         } else if (std::error_code EC = parseEntireSummary())
05549           return EC;
05550         break;
05551       case bitc::MODULE_STRTAB_BLOCK_ID:
05552         if (std::error_code EC = parseModuleStringTable())
05553           return EC;
05554         break;
05555       }
05556       continue;
05557 
05558     case BitstreamEntry::Record:
05559       Stream.skipRecord(Entry.ID);
05560       continue;
05561     }
05562   }
05563 }
05564 
05565 // Eagerly parse the entire function summary block (i.e. for all functions
05566 // in the index). This populates the FunctionSummary objects in
05567 // the index.
05568 std::error_code FunctionIndexBitcodeReader::parseEntireSummary() {
05569   if (Stream.EnterSubBlock(bitc::FUNCTION_SUMMARY_BLOCK_ID))
05570     return error("Invalid record");
05571 
05572   SmallVector<uint64_t, 64> Record;
05573 
05574   while (1) {
05575     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
05576 
05577     switch (Entry.Kind) {
05578     case BitstreamEntry::SubBlock: // Handled for us already.
05579     case BitstreamEntry::Error:
05580       return error("Malformed block");
05581     case BitstreamEntry::EndBlock:
05582       return std::error_code();
05583     case BitstreamEntry::Record:
05584       // The interesting case.
05585       break;
05586     }
05587 
05588     // Read a record. The record format depends on whether this
05589     // is a per-module index or a combined index file. In the per-module
05590     // case the records contain the associated value's ID for correlation
05591     // with VST entries. In the combined index the correlation is done
05592     // via the bitcode offset of the summary records (which were saved
05593     // in the combined index VST entries). The records also contain
05594     // information used for ThinLTO renaming and importing.
05595     Record.clear();
05596     uint64_t CurRecordBit = Stream.GetCurrentBitNo();
05597     switch (Stream.readRecord(Entry.ID, Record)) {
05598     default: // Default behavior: ignore.
05599       break;
05600     // FS_PERMODULE_ENTRY: [valueid, islocal, instcount]
05601     case bitc::FS_CODE_PERMODULE_ENTRY: {
05602       unsigned ValueID = Record[0];
05603       bool IsLocal = Record[1];
05604       unsigned InstCount = Record[2];
05605       std::unique_ptr<FunctionSummary> FS =
05606           llvm::make_unique<FunctionSummary>(InstCount);
05607       FS->setLocalFunction(IsLocal);
05608       // The module path string ref set in the summary must be owned by the
05609       // index's module string table. Since we don't have a module path
05610       // string table section in the per-module index, we create a single
05611       // module path string table entry with an empty (0) ID to take
05612       // ownership.
05613       FS->setModulePath(
05614           TheIndex->addModulePath(Buffer->getBufferIdentifier(), 0));
05615       SummaryMap[ValueID] = std::move(FS);
05616     }
05617     // FS_COMBINED_ENTRY: [modid, instcount]
05618     case bitc::FS_CODE_COMBINED_ENTRY: {
05619       uint64_t ModuleId = Record[0];
05620       unsigned InstCount = Record[1];
05621       std::unique_ptr<FunctionSummary> FS =
05622           llvm::make_unique<FunctionSummary>(InstCount);
05623       FS->setModulePath(ModuleIdMap[ModuleId]);
05624       SummaryMap[CurRecordBit] = std::move(FS);
05625     }
05626     }
05627   }
05628   llvm_unreachable("Exit infinite loop");
05629 }
05630 
05631 // Parse the  module string table block into the Index.
05632 // This populates the ModulePathStringTable map in the index.
05633 std::error_code FunctionIndexBitcodeReader::parseModuleStringTable() {
05634   if (Stream.EnterSubBlock(bitc::MODULE_STRTAB_BLOCK_ID))
05635     return error("Invalid record");
05636 
05637   SmallVector<uint64_t, 64> Record;
05638 
05639   SmallString<128> ModulePath;
05640   while (1) {
05641     BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
05642 
05643     switch (Entry.Kind) {
05644     case BitstreamEntry::SubBlock: // Handled for us already.
05645     case BitstreamEntry::Error:
05646       return error("Malformed block");
05647     case BitstreamEntry::EndBlock:
05648       return std::error_code();
05649     case BitstreamEntry::Record:
05650       // The interesting case.
05651       break;
05652     }
05653 
05654     Record.clear();
05655     switch (Stream.readRecord(Entry.ID, Record)) {
05656     default: // Default behavior: ignore.
05657       break;
05658     case bitc::MST_CODE_ENTRY: {
05659       // MST_ENTRY: [modid, namechar x N]
05660       if (convertToString(Record, 1, ModulePath))
05661         return error("Invalid record");
05662       uint64_t ModuleId = Record[0];
05663       StringRef ModulePathInMap = TheIndex->addModulePath(ModulePath, ModuleId);
05664       ModuleIdMap[ModuleId] = ModulePathInMap;
05665       ModulePath.clear();
05666       break;
05667     }
05668     }
05669   }
05670   llvm_unreachable("Exit infinite loop");
05671 }
05672 
05673 // Parse the function info index from the bitcode streamer into the given index.
05674 std::error_code FunctionIndexBitcodeReader::parseSummaryIndexInto(
05675     std::unique_ptr<DataStreamer> Streamer, FunctionInfoIndex *I) {
05676   TheIndex = I;
05677 
05678   if (std::error_code EC = initStream(std::move(Streamer)))
05679     return EC;
05680 
05681   // Sniff for the signature.
05682   if (!hasValidBitcodeHeader(Stream))
05683     return error("Invalid bitcode signature");
05684 
05685   // We expect a number of well-defined blocks, though we don't necessarily
05686   // need to understand them all.
05687   while (1) {
05688     if (Stream.AtEndOfStream()) {
05689       // We didn't really read a proper Module block.
05690       return error("Malformed block");
05691     }
05692 
05693     BitstreamEntry Entry =
05694         Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs);
05695 
05696     if (Entry.Kind != BitstreamEntry::SubBlock)
05697       return error("Malformed block");
05698 
05699     // If we see a MODULE_BLOCK, parse it to find the blocks needed for
05700     // building the function summary index.
05701     if (Entry.ID == bitc::MODULE_BLOCK_ID)
05702       return parseModule();
05703 
05704     if (Stream.SkipBlock())
05705       return error("Invalid record");
05706   }
05707 }
05708 
05709 // Parse the function information at the given offset in the buffer into
05710 // the index. Used to support lazy parsing of function summaries from the
05711 // combined index during importing.
05712 // TODO: This function is not yet complete as it won't have a consumer
05713 // until ThinLTO function importing is added.
05714 std::error_code FunctionIndexBitcodeReader::parseFunctionSummary(
05715     std::unique_ptr<DataStreamer> Streamer, FunctionInfoIndex *I,
05716     size_t FunctionSummaryOffset) {
05717   TheIndex = I;
05718 
05719   if (std::error_code EC = initStream(std::move(Streamer)))
05720     return EC;
05721 
05722   // Sniff for the signature.
05723   if (!hasValidBitcodeHeader(Stream))
05724     return error("Invalid bitcode signature");
05725 
05726   Stream.JumpToBit(FunctionSummaryOffset);
05727 
05728   BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
05729 
05730   switch (Entry.Kind) {
05731   default:
05732     return error("Malformed block");
05733   case BitstreamEntry::Record:
05734     // The expected case.
05735     break;
05736   }
05737 
05738   // TODO: Read a record. This interface will be completed when ThinLTO
05739   // importing is added so that it can be tested.
05740   SmallVector<uint64_t, 64> Record;
05741   switch (Stream.readRecord(Entry.ID, Record)) {
05742   case bitc::FS_CODE_COMBINED_ENTRY:
05743   default:
05744     return error("Invalid record");
05745   }
05746 
05747   return std::error_code();
05748 }
05749 
05750 std::error_code
05751 FunctionIndexBitcodeReader::initStream(std::unique_ptr<DataStreamer> Streamer) {
05752   if (Streamer)
05753     return initLazyStream(std::move(Streamer));
05754   return initStreamFromBuffer();
05755 }
05756 
05757 std::error_code FunctionIndexBitcodeReader::initStreamFromBuffer() {
05758   const unsigned char *BufPtr = (const unsigned char *)Buffer->getBufferStart();
05759   const unsigned char *BufEnd = BufPtr + Buffer->getBufferSize();
05760 
05761   if (Buffer->getBufferSize() & 3)
05762     return error("Invalid bitcode signature");
05763 
05764   // If we have a wrapper header, parse it and ignore the non-bc file contents.
05765   // The magic number is 0x0B17C0DE stored in little endian.
05766   if (isBitcodeWrapper(BufPtr, BufEnd))
05767     if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true))
05768       return error("Invalid bitcode wrapper header");
05769 
05770   StreamFile.reset(new BitstreamReader(BufPtr, BufEnd));
05771   Stream.init(&*StreamFile);
05772 
05773   return std::error_code();
05774 }
05775 
05776 std::error_code FunctionIndexBitcodeReader::initLazyStream(
05777     std::unique_ptr<DataStreamer> Streamer) {
05778   // Check and strip off the bitcode wrapper; BitstreamReader expects never to
05779   // see it.
05780   auto OwnedBytes =
05781       llvm::make_unique<StreamingMemoryObject>(std::move(Streamer));
05782   StreamingMemoryObject &Bytes = *OwnedBytes;
05783   StreamFile = llvm::make_unique<BitstreamReader>(std::move(OwnedBytes));
05784   Stream.init(&*StreamFile);
05785 
05786   unsigned char buf[16];
05787   if (Bytes.readBytes(buf, 16, 0) != 16)
05788     return error("Invalid bitcode signature");
05789 
05790   if (!isBitcode(buf, buf + 16))
05791     return error("Invalid bitcode signature");
05792 
05793   if (isBitcodeWrapper(buf, buf + 4)) {
05794     const unsigned char *bitcodeStart = buf;
05795     const unsigned char *bitcodeEnd = buf + 16;
05796     SkipBitcodeWrapperHeader(bitcodeStart, bitcodeEnd, false);
05797     Bytes.dropLeadingBytes(bitcodeStart - buf);
05798     Bytes.setKnownObjectSize(bitcodeEnd - bitcodeStart);
05799   }
05800   return std::error_code();
05801 }
05802 
05803 namespace {
05804 class BitcodeErrorCategoryType : public std::error_category {
05805   const char *name() const LLVM_NOEXCEPT override {
05806     return "llvm.bitcode";
05807   }
05808   std::string message(int IE) const override {
05809     BitcodeError E = static_cast<BitcodeError>(IE);
05810     switch (E) {
05811     case BitcodeError::InvalidBitcodeSignature:
05812       return "Invalid bitcode signature";
05813     case BitcodeError::CorruptedBitcode:
05814       return "Corrupted bitcode";
05815     }
05816     llvm_unreachable("Unknown error type!");
05817   }
05818 };
05819 }
05820 
05821 static ManagedStatic<BitcodeErrorCategoryType> ErrorCategory;
05822 
05823 const std::error_category &llvm::BitcodeErrorCategory() {
05824   return *ErrorCategory;
05825 }
05826 
05827 //===----------------------------------------------------------------------===//
05828 // External interface
05829 //===----------------------------------------------------------------------===//
05830 
05831 static ErrorOr<std::unique_ptr<Module>>
05832 getBitcodeModuleImpl(std::unique_ptr<DataStreamer> Streamer, StringRef Name,
05833                      BitcodeReader *R, LLVMContext &Context,
05834                      bool MaterializeAll, bool ShouldLazyLoadMetadata) {
05835   std::unique_ptr<Module> M = make_unique<Module>(Name, Context);
05836   M->setMaterializer(R);
05837 
05838   auto cleanupOnError = [&](std::error_code EC) {
05839     R->releaseBuffer(); // Never take ownership on error.
05840     return EC;
05841   };
05842 
05843   // Delay parsing Metadata if ShouldLazyLoadMetadata is true.
05844   if (std::error_code EC = R->parseBitcodeInto(std::move(Streamer), M.get(),
05845                                                ShouldLazyLoadMetadata))
05846     return cleanupOnError(EC);
05847 
05848   if (MaterializeAll) {
05849     // Read in the entire module, and destroy the BitcodeReader.
05850     if (std::error_code EC = M->materializeAll())
05851       return cleanupOnError(EC);
05852   } else {
05853     // Resolve forward references from blockaddresses.
05854     if (std::error_code EC = R->materializeForwardReferencedFunctions())
05855       return cleanupOnError(EC);
05856   }
05857   return std::move(M);
05858 }
05859 
05860 /// \brief Get a lazy one-at-time loading module from bitcode.
05861 ///
05862 /// This isn't always used in a lazy context.  In particular, it's also used by
05863 /// \a parseBitcodeFile().  If this is truly lazy, then we need to eagerly pull
05864 /// in forward-referenced functions from block address references.
05865 ///
05866 /// \param[in] MaterializeAll Set to \c true if we should materialize
05867 /// everything.
05868 static ErrorOr<std::unique_ptr<Module>>
05869 getLazyBitcodeModuleImpl(std::unique_ptr<MemoryBuffer> &&Buffer,
05870                          LLVMContext &Context, bool MaterializeAll,
05871                          bool ShouldLazyLoadMetadata = false) {
05872   BitcodeReader *R = new BitcodeReader(Buffer.get(), Context);
05873 
05874   ErrorOr<std::unique_ptr<Module>> Ret =
05875       getBitcodeModuleImpl(nullptr, Buffer->getBufferIdentifier(), R, Context,
05876                            MaterializeAll, ShouldLazyLoadMetadata);
05877   if (!Ret)
05878     return Ret;
05879 
05880   Buffer.release(); // The BitcodeReader owns it now.
05881   return Ret;
05882 }
05883 
05884 ErrorOr<std::unique_ptr<Module>>
05885 llvm::getLazyBitcodeModule(std::unique_ptr<MemoryBuffer> &&Buffer,
05886                            LLVMContext &Context, bool ShouldLazyLoadMetadata) {
05887   return getLazyBitcodeModuleImpl(std::move(Buffer), Context, false,
05888                                   ShouldLazyLoadMetadata);
05889 }
05890 
05891 ErrorOr<std::unique_ptr<Module>>
05892 llvm::getStreamedBitcodeModule(StringRef Name,
05893                                std::unique_ptr<DataStreamer> Streamer,
05894                                LLVMContext &Context) {
05895   std::unique_ptr<Module> M = make_unique<Module>(Name, Context);
05896   BitcodeReader *R = new BitcodeReader(Context);
05897 
05898   return getBitcodeModuleImpl(std::move(Streamer), Name, R, Context, false,
05899                               false);
05900 }
05901 
05902 ErrorOr<std::unique_ptr<Module>> llvm::parseBitcodeFile(MemoryBufferRef Buffer,
05903                                                         LLVMContext &Context) {
05904   std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false);
05905   return getLazyBitcodeModuleImpl(std::move(Buf), Context, true);
05906   // TODO: Restore the use-lists to the in-memory state when the bitcode was
05907   // written.  We must defer until the Module has been fully materialized.
05908 }
05909 
05910 std::string llvm::getBitcodeTargetTriple(MemoryBufferRef Buffer,
05911                                          LLVMContext &Context) {
05912   std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false);
05913   auto R = llvm::make_unique<BitcodeReader>(Buf.release(), Context);
05914   ErrorOr<std::string> Triple = R->parseTriple();
05915   if (Triple.getError())
05916     return "";
05917   return Triple.get();
05918 }
05919 
05920 std::string llvm::getBitcodeProducerString(MemoryBufferRef Buffer,
05921                                            LLVMContext &Context) {
05922   std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false);
05923   BitcodeReader R(Buf.release(), Context);
05924   ErrorOr<std::string> ProducerString = R.parseIdentificationBlock();
05925   if (ProducerString.getError())
05926     return "";
05927   return ProducerString.get();
05928 }
05929 
05930 // Parse the specified bitcode buffer, returning the function info index.
05931 // If IsLazy is false, parse the entire function summary into
05932 // the index. Otherwise skip the function summary section, and only create
05933 // an index object with a map from function name to function summary offset.
05934 // The index is used to perform lazy function summary reading later.
05935 ErrorOr<std::unique_ptr<FunctionInfoIndex>>
05936 llvm::getFunctionInfoIndex(MemoryBufferRef Buffer,
05937                            DiagnosticHandlerFunction DiagnosticHandler,
05938                            bool IsLazy) {
05939   std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false);
05940   FunctionIndexBitcodeReader R(Buf.get(), DiagnosticHandler, IsLazy);
05941 
05942   auto Index = llvm::make_unique<FunctionInfoIndex>();
05943 
05944   auto cleanupOnError = [&](std::error_code EC) {
05945     R.releaseBuffer(); // Never take ownership on error.
05946     return EC;
05947   };
05948 
05949   if (std::error_code EC = R.parseSummaryIndexInto(nullptr, Index.get()))
05950     return cleanupOnError(EC);
05951 
05952   Buf.release(); // The FunctionIndexBitcodeReader owns it now.
05953   return std::move(Index);
05954 }
05955 
05956 // Check if the given bitcode buffer contains a function summary block.
05957 bool llvm::hasFunctionSummary(MemoryBufferRef Buffer,
05958                               DiagnosticHandlerFunction DiagnosticHandler) {
05959   std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false);
05960   FunctionIndexBitcodeReader R(Buf.get(), DiagnosticHandler, false, true);
05961 
05962   auto cleanupOnError = [&](std::error_code EC) {
05963     R.releaseBuffer(); // Never take ownership on error.
05964     return false;
05965   };
05966 
05967   if (std::error_code EC = R.parseSummaryIndexInto(nullptr, nullptr))
05968     return cleanupOnError(EC);
05969 
05970   Buf.release(); // The FunctionIndexBitcodeReader owns it now.
05971   return R.foundFuncSummary();
05972 }
05973 
05974 // This method supports lazy reading of function summary data from the combined
05975 // index during ThinLTO function importing. When reading the combined index
05976 // file, getFunctionInfoIndex is first invoked with IsLazy=true.
05977 // Then this method is called for each function considered for importing,
05978 // to parse the summary information for the given function name into
05979 // the index.
05980 std::error_code llvm::readFunctionSummary(
05981     MemoryBufferRef Buffer, DiagnosticHandlerFunction DiagnosticHandler,
05982     StringRef FunctionName, std::unique_ptr<FunctionInfoIndex> Index) {
05983   std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false);
05984   FunctionIndexBitcodeReader R(Buf.get(), DiagnosticHandler);
05985 
05986   auto cleanupOnError = [&](std::error_code EC) {
05987     R.releaseBuffer(); // Never take ownership on error.
05988     return EC;
05989   };
05990 
05991   // Lookup the given function name in the FunctionMap, which may
05992   // contain a list of function infos in the case of a COMDAT. Walk through
05993   // and parse each function summary info at the function summary offset
05994   // recorded when parsing the value symbol table.
05995   for (const auto &FI : Index->getFunctionInfoList(FunctionName)) {
05996     size_t FunctionSummaryOffset = FI->bitcodeIndex();
05997     if (std::error_code EC =
05998             R.parseFunctionSummary(nullptr, Index.get(), FunctionSummaryOffset))
05999       return cleanupOnError(EC);
06000   }
06001 
06002   Buf.release(); // The FunctionIndexBitcodeReader owns it now.
06003   return std::error_code();
06004 }