LCOV - code coverage report
Current view: top level - lib/Bitcode/Writer - BitcodeWriter.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 1648 1828 90.2 %
Date: 2018-10-20 13:21:21 Functions: 87 102 85.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- Bitcode/Writer/BitcodeWriter.cpp - Bitcode Writer ------------------===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // Bitcode writer implementation.
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #include "llvm/Bitcode/BitcodeWriter.h"
      15             : #include "ValueEnumerator.h"
      16             : #include "llvm/ADT/APFloat.h"
      17             : #include "llvm/ADT/APInt.h"
      18             : #include "llvm/ADT/ArrayRef.h"
      19             : #include "llvm/ADT/DenseMap.h"
      20             : #include "llvm/ADT/None.h"
      21             : #include "llvm/ADT/Optional.h"
      22             : #include "llvm/ADT/STLExtras.h"
      23             : #include "llvm/ADT/SmallString.h"
      24             : #include "llvm/ADT/SmallVector.h"
      25             : #include "llvm/ADT/StringMap.h"
      26             : #include "llvm/ADT/StringRef.h"
      27             : #include "llvm/ADT/Triple.h"
      28             : #include "llvm/Bitcode/BitCodes.h"
      29             : #include "llvm/Bitcode/BitstreamWriter.h"
      30             : #include "llvm/Bitcode/LLVMBitCodes.h"
      31             : #include "llvm/Config/llvm-config.h"
      32             : #include "llvm/IR/Attributes.h"
      33             : #include "llvm/IR/BasicBlock.h"
      34             : #include "llvm/IR/CallSite.h"
      35             : #include "llvm/IR/Comdat.h"
      36             : #include "llvm/IR/Constant.h"
      37             : #include "llvm/IR/Constants.h"
      38             : #include "llvm/IR/DebugInfoMetadata.h"
      39             : #include "llvm/IR/DebugLoc.h"
      40             : #include "llvm/IR/DerivedTypes.h"
      41             : #include "llvm/IR/Function.h"
      42             : #include "llvm/IR/GlobalAlias.h"
      43             : #include "llvm/IR/GlobalIFunc.h"
      44             : #include "llvm/IR/GlobalObject.h"
      45             : #include "llvm/IR/GlobalValue.h"
      46             : #include "llvm/IR/GlobalVariable.h"
      47             : #include "llvm/IR/InlineAsm.h"
      48             : #include "llvm/IR/InstrTypes.h"
      49             : #include "llvm/IR/Instruction.h"
      50             : #include "llvm/IR/Instructions.h"
      51             : #include "llvm/IR/LLVMContext.h"
      52             : #include "llvm/IR/Metadata.h"
      53             : #include "llvm/IR/Module.h"
      54             : #include "llvm/IR/ModuleSummaryIndex.h"
      55             : #include "llvm/IR/Operator.h"
      56             : #include "llvm/IR/Type.h"
      57             : #include "llvm/IR/UseListOrder.h"
      58             : #include "llvm/IR/Value.h"
      59             : #include "llvm/IR/ValueSymbolTable.h"
      60             : #include "llvm/MC/StringTableBuilder.h"
      61             : #include "llvm/Object/IRSymtab.h"
      62             : #include "llvm/Support/AtomicOrdering.h"
      63             : #include "llvm/Support/Casting.h"
      64             : #include "llvm/Support/CommandLine.h"
      65             : #include "llvm/Support/Endian.h"
      66             : #include "llvm/Support/Error.h"
      67             : #include "llvm/Support/ErrorHandling.h"
      68             : #include "llvm/Support/MathExtras.h"
      69             : #include "llvm/Support/SHA1.h"
      70             : #include "llvm/Support/TargetRegistry.h"
      71             : #include "llvm/Support/raw_ostream.h"
      72             : #include <algorithm>
      73             : #include <cassert>
      74             : #include <cstddef>
      75             : #include <cstdint>
      76             : #include <iterator>
      77             : #include <map>
      78             : #include <memory>
      79             : #include <string>
      80             : #include <utility>
      81             : #include <vector>
      82             : 
      83             : using namespace llvm;
      84             : 
      85             : static cl::opt<unsigned>
      86             :     IndexThreshold("bitcode-mdindex-threshold", cl::Hidden, cl::init(25),
      87             :                    cl::desc("Number of metadatas above which we emit an index "
      88             :                             "to enable lazy-loading"));
      89             : 
      90             : cl::opt<bool> WriteRelBFToSummary(
      91             :     "write-relbf-to-summary", cl::Hidden, cl::init(false),
      92             :     cl::desc("Write relative block frequency to function summary "));
      93             : 
      94             : extern FunctionSummary::ForceSummaryHotnessType ForceSummaryEdgesCold;
      95             : 
      96             : namespace {
      97             : 
      98             : /// These are manifest constants used by the bitcode writer. They do not need to
      99             : /// be kept in sync with the reader, but need to be consistent within this file.
     100             : enum {
     101             :   // VALUE_SYMTAB_BLOCK abbrev id's.
     102             :   VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
     103             :   VST_ENTRY_7_ABBREV,
     104             :   VST_ENTRY_6_ABBREV,
     105             :   VST_BBENTRY_6_ABBREV,
     106             : 
     107             :   // CONSTANTS_BLOCK abbrev id's.
     108             :   CONSTANTS_SETTYPE_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
     109             :   CONSTANTS_INTEGER_ABBREV,
     110             :   CONSTANTS_CE_CAST_Abbrev,
     111             :   CONSTANTS_NULL_Abbrev,
     112             : 
     113             :   // FUNCTION_BLOCK abbrev id's.
     114             :   FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
     115             :   FUNCTION_INST_BINOP_ABBREV,
     116             :   FUNCTION_INST_BINOP_FLAGS_ABBREV,
     117             :   FUNCTION_INST_CAST_ABBREV,
     118             :   FUNCTION_INST_RET_VOID_ABBREV,
     119             :   FUNCTION_INST_RET_VAL_ABBREV,
     120             :   FUNCTION_INST_UNREACHABLE_ABBREV,
     121             :   FUNCTION_INST_GEP_ABBREV,
     122             : };
     123             : 
     124             : /// Abstract class to manage the bitcode writing, subclassed for each bitcode
     125             : /// file type.
     126             : class BitcodeWriterBase {
     127             : protected:
     128             :   /// The stream created and owned by the client.
     129             :   BitstreamWriter &Stream;
     130             : 
     131             :   StringTableBuilder &StrtabBuilder;
     132             : 
     133             : public:
     134             :   /// Constructs a BitcodeWriterBase object that writes to the provided
     135             :   /// \p Stream.
     136             :   BitcodeWriterBase(BitstreamWriter &Stream, StringTableBuilder &StrtabBuilder)
     137        4925 :       : Stream(Stream), StrtabBuilder(StrtabBuilder) {}
     138             : 
     139             : protected:
     140             :   void writeBitcodeHeader();
     141             :   void writeModuleVersion();
     142             : };
     143             : 
     144           0 : void BitcodeWriterBase::writeModuleVersion() {
     145             :   // VERSION: [version#]
     146        4925 :   Stream.EmitRecord(bitc::MODULE_CODE_VERSION, ArrayRef<uint64_t>{2});
     147           0 : }
     148             : 
     149             : /// Base class to manage the module bitcode writing, currently subclassed for
     150             : /// ModuleBitcodeWriter and ThinLinkBitcodeWriter.
     151        4717 : class ModuleBitcodeWriterBase : public BitcodeWriterBase {
     152             : protected:
     153             :   /// The Module to write to bitcode.
     154             :   const Module &M;
     155             : 
     156             :   /// Enumerates ids for all values in the module.
     157             :   ValueEnumerator VE;
     158             : 
     159             :   /// Optional per-module index to write for ThinLTO.
     160             :   const ModuleSummaryIndex *Index;
     161             : 
     162             :   /// Map that holds the correspondence between GUIDs in the summary index,
     163             :   /// that came from indirect call profiles, and a value id generated by this
     164             :   /// class to use in the VST and summary block records.
     165             :   std::map<GlobalValue::GUID, unsigned> GUIDToValueIdMap;
     166             : 
     167             :   /// Tracks the last value id recorded in the GUIDToValueMap.
     168             :   unsigned GlobalValueId;
     169             : 
     170             :   /// Saves the offset of the VSTOffset record that must eventually be
     171             :   /// backpatched with the offset of the actual VST.
     172             :   uint64_t VSTOffsetPlaceholder = 0;
     173             : 
     174             : public:
     175             :   /// Constructs a ModuleBitcodeWriterBase object for the given Module,
     176             :   /// writing to the provided \p Buffer.
     177        4717 :   ModuleBitcodeWriterBase(const Module &M, StringTableBuilder &StrtabBuilder,
     178             :                           BitstreamWriter &Stream,
     179             :                           bool ShouldPreserveUseListOrder,
     180             :                           const ModuleSummaryIndex *Index)
     181        4717 :       : BitcodeWriterBase(Stream, StrtabBuilder), M(M),
     182        4717 :         VE(M, ShouldPreserveUseListOrder), Index(Index) {
     183             :     // Assign ValueIds to any callee values in the index that came from
     184             :     // indirect call profiles and were recorded as a GUID not a Value*
     185             :     // (which would have been assigned an ID by the ValueEnumerator).
     186             :     // The starting ValueId is just after the number of values in the
     187             :     // ValueEnumerator, so that they can be emitted in the VST.
     188        4717 :     GlobalValueId = VE.getValues().size();
     189        4717 :     if (!Index)
     190             :       return;
     191        1586 :     for (const auto &GUIDSummaryLists : *Index)
     192             :       // Examine all summaries for this GUID.
     193        2043 :       for (auto &Summary : GUIDSummaryLists.second.SummaryList)
     194             :         if (auto FS = dyn_cast<FunctionSummary>(Summary.get()))
     195             :           // For each call in the function summary, see if the call
     196             :           // is to a GUID (which means it is for an indirect call,
     197             :           // otherwise we would have a Value for it). If so, synthesize
     198             :           // a value id.
     199         935 :           for (auto &CallEdge : FS->calls())
     200         320 :             if (!CallEdge.first.haveGVs() || !CallEdge.first.getValue())
     201           9 :               assignValueId(CallEdge.first.getGUID());
     202             :   }
     203             : 
     204             : protected:
     205             :   void writePerModuleGlobalValueSummary();
     206             : 
     207             : private:
     208             :   void writePerModuleFunctionSummaryRecord(SmallVector<uint64_t, 64> &NameVals,
     209             :                                            GlobalValueSummary *Summary,
     210             :                                            unsigned ValueID,
     211             :                                            unsigned FSCallsAbbrev,
     212             :                                            unsigned FSCallsProfileAbbrev,
     213             :                                            const Function &F);
     214             :   void writeModuleLevelReferences(const GlobalVariable &V,
     215             :                                   SmallVector<uint64_t, 64> &NameVals,
     216             :                                   unsigned FSModRefsAbbrev);
     217             : 
     218             :   void assignValueId(GlobalValue::GUID ValGUID) {
     219           9 :     GUIDToValueIdMap[ValGUID] = ++GlobalValueId;
     220             :   }
     221             : 
     222             :   unsigned getValueId(GlobalValue::GUID ValGUID) {
     223             :     const auto &VMI = GUIDToValueIdMap.find(ValGUID);
     224             :     // Expect that any GUID value had a value Id assigned by an
     225             :     // earlier call to assignValueId.
     226             :     assert(VMI != GUIDToValueIdMap.end() &&
     227             :            "GUID does not have assigned value Id");
     228           9 :     return VMI->second;
     229             :   }
     230             : 
     231             :   // Helper to get the valueId for the type of value recorded in VI.
     232         320 :   unsigned getValueId(ValueInfo VI) {
     233         320 :     if (!VI.haveGVs() || !VI.getValue())
     234           9 :       return getValueId(VI.getGUID());
     235         311 :     return VE.getValueID(VI.getValue());
     236             :   }
     237             : 
     238             :   std::map<GlobalValue::GUID, unsigned> &valueIds() { return GUIDToValueIdMap; }
     239             : };
     240             : 
     241             : /// Class to manage the bitcode writing for a module.
     242             : class ModuleBitcodeWriter : public ModuleBitcodeWriterBase {
     243             :   /// Pointer to the buffer allocated by caller for bitcode writing.
     244             :   const SmallVectorImpl<char> &Buffer;
     245             : 
     246             :   /// True if a module hash record should be written.
     247             :   bool GenerateHash;
     248             : 
     249             :   /// If non-null, when GenerateHash is true, the resulting hash is written
     250             :   /// into ModHash.
     251             :   ModuleHash *ModHash;
     252             : 
     253             :   SHA1 Hasher;
     254             : 
     255             :   /// The start bit of the identification block.
     256             :   uint64_t BitcodeStartBit;
     257             : 
     258             : public:
     259             :   /// Constructs a ModuleBitcodeWriter object for the given Module,
     260             :   /// writing to the provided \p Buffer.
     261        4708 :   ModuleBitcodeWriter(const Module &M, SmallVectorImpl<char> &Buffer,
     262             :                       StringTableBuilder &StrtabBuilder,
     263             :                       BitstreamWriter &Stream, bool ShouldPreserveUseListOrder,
     264             :                       const ModuleSummaryIndex *Index, bool GenerateHash,
     265             :                       ModuleHash *ModHash = nullptr)
     266        4708 :       : ModuleBitcodeWriterBase(M, StrtabBuilder, Stream,
     267             :                                 ShouldPreserveUseListOrder, Index),
     268             :         Buffer(Buffer), GenerateHash(GenerateHash), ModHash(ModHash),
     269        4708 :         BitcodeStartBit(Stream.GetCurrentBitNo()) {}
     270             : 
     271             :   /// Emit the current module to the bitstream.
     272             :   void write();
     273             : 
     274             : private:
     275           0 :   uint64_t bitcodeStartBit() { return BitcodeStartBit; }
     276             : 
     277             :   size_t addToStrtab(StringRef Str);
     278             : 
     279             :   void writeAttributeGroupTable();
     280             :   void writeAttributeTable();
     281             :   void writeTypeTable();
     282             :   void writeComdats();
     283             :   void writeValueSymbolTableForwardDecl();
     284             :   void writeModuleInfo();
     285             :   void writeValueAsMetadata(const ValueAsMetadata *MD,
     286             :                             SmallVectorImpl<uint64_t> &Record);
     287             :   void writeMDTuple(const MDTuple *N, SmallVectorImpl<uint64_t> &Record,
     288             :                     unsigned Abbrev);
     289             :   unsigned createDILocationAbbrev();
     290             :   void writeDILocation(const DILocation *N, SmallVectorImpl<uint64_t> &Record,
     291             :                        unsigned &Abbrev);
     292             :   unsigned createGenericDINodeAbbrev();
     293             :   void writeGenericDINode(const GenericDINode *N,
     294             :                           SmallVectorImpl<uint64_t> &Record, unsigned &Abbrev);
     295             :   void writeDISubrange(const DISubrange *N, SmallVectorImpl<uint64_t> &Record,
     296             :                        unsigned Abbrev);
     297             :   void writeDIEnumerator(const DIEnumerator *N,
     298             :                          SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
     299             :   void writeDIBasicType(const DIBasicType *N, SmallVectorImpl<uint64_t> &Record,
     300             :                         unsigned Abbrev);
     301             :   void writeDIDerivedType(const DIDerivedType *N,
     302             :                           SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
     303             :   void writeDICompositeType(const DICompositeType *N,
     304             :                             SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
     305             :   void writeDISubroutineType(const DISubroutineType *N,
     306             :                              SmallVectorImpl<uint64_t> &Record,
     307             :                              unsigned Abbrev);
     308             :   void writeDIFile(const DIFile *N, SmallVectorImpl<uint64_t> &Record,
     309             :                    unsigned Abbrev);
     310             :   void writeDICompileUnit(const DICompileUnit *N,
     311             :                           SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
     312             :   void writeDISubprogram(const DISubprogram *N,
     313             :                          SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
     314             :   void writeDILexicalBlock(const DILexicalBlock *N,
     315             :                            SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
     316             :   void writeDILexicalBlockFile(const DILexicalBlockFile *N,
     317             :                                SmallVectorImpl<uint64_t> &Record,
     318             :                                unsigned Abbrev);
     319             :   void writeDINamespace(const DINamespace *N, SmallVectorImpl<uint64_t> &Record,
     320             :                         unsigned Abbrev);
     321             :   void writeDIMacro(const DIMacro *N, SmallVectorImpl<uint64_t> &Record,
     322             :                     unsigned Abbrev);
     323             :   void writeDIMacroFile(const DIMacroFile *N, SmallVectorImpl<uint64_t> &Record,
     324             :                         unsigned Abbrev);
     325             :   void writeDIModule(const DIModule *N, SmallVectorImpl<uint64_t> &Record,
     326             :                      unsigned Abbrev);
     327             :   void writeDITemplateTypeParameter(const DITemplateTypeParameter *N,
     328             :                                     SmallVectorImpl<uint64_t> &Record,
     329             :                                     unsigned Abbrev);
     330             :   void writeDITemplateValueParameter(const DITemplateValueParameter *N,
     331             :                                      SmallVectorImpl<uint64_t> &Record,
     332             :                                      unsigned Abbrev);
     333             :   void writeDIGlobalVariable(const DIGlobalVariable *N,
     334             :                              SmallVectorImpl<uint64_t> &Record,
     335             :                              unsigned Abbrev);
     336             :   void writeDILocalVariable(const DILocalVariable *N,
     337             :                             SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
     338             :   void writeDILabel(const DILabel *N,
     339             :                     SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
     340             :   void writeDIExpression(const DIExpression *N,
     341             :                          SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
     342             :   void writeDIGlobalVariableExpression(const DIGlobalVariableExpression *N,
     343             :                                        SmallVectorImpl<uint64_t> &Record,
     344             :                                        unsigned Abbrev);
     345             :   void writeDIObjCProperty(const DIObjCProperty *N,
     346             :                            SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
     347             :   void writeDIImportedEntity(const DIImportedEntity *N,
     348             :                              SmallVectorImpl<uint64_t> &Record,
     349             :                              unsigned Abbrev);
     350             :   unsigned createNamedMetadataAbbrev();
     351             :   void writeNamedMetadata(SmallVectorImpl<uint64_t> &Record);
     352             :   unsigned createMetadataStringsAbbrev();
     353             :   void writeMetadataStrings(ArrayRef<const Metadata *> Strings,
     354             :                             SmallVectorImpl<uint64_t> &Record);
     355             :   void writeMetadataRecords(ArrayRef<const Metadata *> MDs,
     356             :                             SmallVectorImpl<uint64_t> &Record,
     357             :                             std::vector<unsigned> *MDAbbrevs = nullptr,
     358             :                             std::vector<uint64_t> *IndexPos = nullptr);
     359             :   void writeModuleMetadata();
     360             :   void writeFunctionMetadata(const Function &F);
     361             :   void writeFunctionMetadataAttachment(const Function &F);
     362             :   void writeGlobalVariableMetadataAttachment(const GlobalVariable &GV);
     363             :   void pushGlobalMetadataAttachment(SmallVectorImpl<uint64_t> &Record,
     364             :                                     const GlobalObject &GO);
     365             :   void writeModuleMetadataKinds();
     366             :   void writeOperandBundleTags();
     367             :   void writeSyncScopeNames();
     368             :   void writeConstants(unsigned FirstVal, unsigned LastVal, bool isGlobal);
     369             :   void writeModuleConstants();
     370             :   bool pushValueAndType(const Value *V, unsigned InstID,
     371             :                         SmallVectorImpl<unsigned> &Vals);
     372             :   void writeOperandBundles(ImmutableCallSite CS, unsigned InstID);
     373             :   void pushValue(const Value *V, unsigned InstID,
     374             :                  SmallVectorImpl<unsigned> &Vals);
     375             :   void pushValueSigned(const Value *V, unsigned InstID,
     376             :                        SmallVectorImpl<uint64_t> &Vals);
     377             :   void writeInstruction(const Instruction &I, unsigned InstID,
     378             :                         SmallVectorImpl<unsigned> &Vals);
     379             :   void writeFunctionLevelValueSymbolTable(const ValueSymbolTable &VST);
     380             :   void writeGlobalValueSymbolTable(
     381             :       DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex);
     382             :   void writeUseList(UseListOrder &&Order);
     383             :   void writeUseListBlock(const Function *F);
     384             :   void
     385             :   writeFunction(const Function &F,
     386             :                 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex);
     387             :   void writeBlockInfo();
     388             :   void writeModuleHash(size_t BlockStartPos);
     389             : 
     390             :   unsigned getEncodedSyncScopeID(SyncScope::ID SSID) {
     391         508 :     return unsigned(SSID);
     392             :   }
     393             : };
     394             : 
     395             : /// Class to manage the bitcode writing for a combined index.
     396             : class IndexBitcodeWriter : public BitcodeWriterBase {
     397             :   /// The combined index to write to bitcode.
     398             :   const ModuleSummaryIndex &Index;
     399             : 
     400             :   /// When writing a subset of the index for distributed backends, client
     401             :   /// provides a map of modules to the corresponding GUIDs/summaries to write.
     402             :   const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex;
     403             : 
     404             :   /// Map that holds the correspondence between the GUID used in the combined
     405             :   /// index and a value id generated by this class to use in references.
     406             :   std::map<GlobalValue::GUID, unsigned> GUIDToValueIdMap;
     407             : 
     408             :   /// Tracks the last value id recorded in the GUIDToValueMap.
     409             :   unsigned GlobalValueId = 0;
     410             : 
     411             : public:
     412             :   /// Constructs a IndexBitcodeWriter object for the given combined index,
     413             :   /// writing to the provided \p Buffer. When writing a subset of the index
     414             :   /// for a distributed backend, provide a \p ModuleToSummariesForIndex map.
     415             :   IndexBitcodeWriter(BitstreamWriter &Stream, StringTableBuilder &StrtabBuilder,
     416             :                      const ModuleSummaryIndex &Index,
     417             :                      const std::map<std::string, GVSummaryMapTy>
     418             :                          *ModuleToSummariesForIndex = nullptr)
     419         208 :       : BitcodeWriterBase(Stream, StrtabBuilder), Index(Index),
     420         208 :         ModuleToSummariesForIndex(ModuleToSummariesForIndex) {
     421             :     // Assign unique value ids to all summaries to be written, for use
     422             :     // in writing out the call graph edges. Save the mapping from GUID
     423             :     // to the new global value id to use when writing those edges, which
     424             :     // are currently saved in the index in terms of GUID.
     425         208 :     forEachSummary([&](GVInfo I, bool) {
     426           0 :       GUIDToValueIdMap[I.first] = ++GlobalValueId;
     427             :     });
     428             :   }
     429             : 
     430             :   /// The below iterator returns the GUID and associated summary.
     431             :   using GVInfo = std::pair<GlobalValue::GUID, GlobalValueSummary *>;
     432             : 
     433             :   /// Calls the callback for each value GUID and summary to be written to
     434             :   /// bitcode. This hides the details of whether they are being pulled from the
     435             :   /// entire index or just those in a provided ModuleToSummariesForIndex map.
     436             :   template<typename Functor>
     437           0 :   void forEachSummary(Functor Callback) {
     438           0 :     if (ModuleToSummariesForIndex) {
     439           0 :       for (auto &M : *ModuleToSummariesForIndex)
     440           0 :         for (auto &Summary : M.second) {
     441           0 :           Callback(Summary, false);
     442             :           // Ensure aliasee is handled, e.g. for assigning a valueId,
     443             :           // even if we are not importing the aliasee directly (the
     444             :           // imported alias will contain a copy of aliasee).
     445           0 :           if (auto *AS = dyn_cast<AliasSummary>(Summary.getSecond()))
     446           0 :             Callback({AS->getAliaseeGUID(), &AS->getAliasee()}, true);
     447             :         }
     448             :     } else {
     449           0 :       for (auto &Summaries : Index)
     450           0 :         for (auto &Summary : Summaries.second.SummaryList)
     451           0 :           Callback({Summaries.first, Summary.get()}, false);
     452             :     }
     453           0 :   }
     454           0 : 
     455           0 :   /// Calls the callback for each entry in the modulePaths StringMap that
     456           0 :   /// should be written to the module path string table. This hides the details
     457           0 :   /// of whether they are being pulled from the entire index or just those in a
     458           0 :   /// provided ModuleToSummariesForIndex map.
     459             :   template <typename Functor> void forEachModule(Functor Callback) {
     460             :     if (ModuleToSummariesForIndex) {
     461             :       for (const auto &M : *ModuleToSummariesForIndex) {
     462           0 :         const auto &MPI = Index.modulePaths().find(M.first);
     463           0 :         if (MPI == Index.modulePaths().end()) {
     464             :           // This should only happen if the bitcode file was empty, in which
     465             :           // case we shouldn't be importing (the ModuleToSummariesForIndex
     466           0 :           // would only include the module we are writing and index for).
     467           0 :           assert(ModuleToSummariesForIndex->size() == 1);
     468           0 :           continue;
     469             :         }
     470           0 :         Callback(*MPI);
     471           0 :       }
     472           0 :     } else {
     473           0 :       for (const auto &MPSE : Index.modulePaths())
     474           0 :         Callback(MPSE);
     475           0 :     }
     476             :   }
     477             : 
     478             :   /// Main entry point for writing a combined index to bitcode.
     479           0 :   void write();
     480           0 : 
     481             : private:
     482             :   void writeModStrings();
     483           0 :   void writeCombinedGlobalValueSummary();
     484           0 : 
     485           0 :   Optional<unsigned> getValueId(GlobalValue::GUID ValGUID) {
     486             :     auto VMI = GUIDToValueIdMap.find(ValGUID);
     487           0 :     if (VMI == GUIDToValueIdMap.end())
     488             :       return None;
     489             :     return VMI->second;
     490             :   }
     491             : 
     492             :   std::map<GlobalValue::GUID, unsigned> &valueIds() { return GUIDToValueIdMap; }
     493           0 : };
     494           0 : 
     495           0 : } // end anonymous namespace
     496           0 : 
     497           0 : static unsigned getEncodedCastOpcode(unsigned Opcode) {
     498             :   switch (Opcode) {
     499             :   default: llvm_unreachable("Unknown cast instruction!");
     500             :   case Instruction::Trunc   : return bitc::CAST_TRUNC;
     501             :   case Instruction::ZExt    : return bitc::CAST_ZEXT;
     502           0 :   case Instruction::SExt    : return bitc::CAST_SEXT;
     503             :   case Instruction::FPToUI  : return bitc::CAST_FPTOUI;
     504           0 :   case Instruction::FPToSI  : return bitc::CAST_FPTOSI;
     505             :   case Instruction::UIToFP  : return bitc::CAST_UITOFP;
     506             :   case Instruction::SIToFP  : return bitc::CAST_SITOFP;
     507           0 :   case Instruction::FPTrunc : return bitc::CAST_FPTRUNC;
     508           0 :   case Instruction::FPExt   : return bitc::CAST_FPEXT;
     509             :   case Instruction::PtrToInt: return bitc::CAST_PTRTOINT;
     510           0 :   case Instruction::IntToPtr: return bitc::CAST_INTTOPTR;
     511             :   case Instruction::BitCast : return bitc::CAST_BITCAST;
     512             :   case Instruction::AddrSpaceCast: return bitc::CAST_ADDRSPACECAST;
     513             :   }
     514             : }
     515             : 
     516             : static unsigned getEncodedBinaryOpcode(unsigned Opcode) {
     517             :   switch (Opcode) {
     518             :   default: llvm_unreachable("Unknown binary instruction!");
     519             :   case Instruction::Add:
     520             :   case Instruction::FAdd: return bitc::BINOP_ADD;
     521             :   case Instruction::Sub:
     522             :   case Instruction::FSub: return bitc::BINOP_SUB;
     523             :   case Instruction::Mul:
     524             :   case Instruction::FMul: return bitc::BINOP_MUL;
     525             :   case Instruction::UDiv: return bitc::BINOP_UDIV;
     526             :   case Instruction::FDiv:
     527             :   case Instruction::SDiv: return bitc::BINOP_SDIV;
     528             :   case Instruction::URem: return bitc::BINOP_UREM;
     529             :   case Instruction::FRem:
     530             :   case Instruction::SRem: return bitc::BINOP_SREM;
     531             :   case Instruction::Shl:  return bitc::BINOP_SHL;
     532             :   case Instruction::LShr: return bitc::BINOP_LSHR;
     533           0 :   case Instruction::AShr: return bitc::BINOP_ASHR;
     534             :   case Instruction::And:  return bitc::BINOP_AND;
     535             :   case Instruction::Or:   return bitc::BINOP_OR;
     536             :   case Instruction::Xor:  return bitc::BINOP_XOR;
     537             :   }
     538             : }
     539             : 
     540             : static unsigned getEncodedRMWOperation(AtomicRMWInst::BinOp Op) {
     541             :   switch (Op) {
     542             :   default: llvm_unreachable("Unknown RMW operation!");
     543             :   case AtomicRMWInst::Xchg: return bitc::RMW_XCHG;
     544             :   case AtomicRMWInst::Add: return bitc::RMW_ADD;
     545             :   case AtomicRMWInst::Sub: return bitc::RMW_SUB;
     546             :   case AtomicRMWInst::And: return bitc::RMW_AND;
     547             :   case AtomicRMWInst::Nand: return bitc::RMW_NAND;
     548             :   case AtomicRMWInst::Or: return bitc::RMW_OR;
     549             :   case AtomicRMWInst::Xor: return bitc::RMW_XOR;
     550             :   case AtomicRMWInst::Max: return bitc::RMW_MAX;
     551             :   case AtomicRMWInst::Min: return bitc::RMW_MIN;
     552           0 :   case AtomicRMWInst::UMax: return bitc::RMW_UMAX;
     553             :   case AtomicRMWInst::UMin: return bitc::RMW_UMIN;
     554             :   }
     555             : }
     556             : 
     557             : static unsigned getEncodedOrdering(AtomicOrdering Ordering) {
     558             :   switch (Ordering) {
     559             :   case AtomicOrdering::NotAtomic: return bitc::ORDERING_NOTATOMIC;
     560             :   case AtomicOrdering::Unordered: return bitc::ORDERING_UNORDERED;
     561             :   case AtomicOrdering::Monotonic: return bitc::ORDERING_MONOTONIC;
     562             :   case AtomicOrdering::Acquire: return bitc::ORDERING_ACQUIRE;
     563             :   case AtomicOrdering::Release: return bitc::ORDERING_RELEASE;
     564             :   case AtomicOrdering::AcquireRelease: return bitc::ORDERING_ACQREL;
     565             :   case AtomicOrdering::SequentiallyConsistent: return bitc::ORDERING_SEQCST;
     566             :   }
     567             :   llvm_unreachable("Invalid ordering");
     568             : }
     569             : 
     570             : static void writeStringRecord(BitstreamWriter &Stream, unsigned Code,
     571             :                               StringRef Str, unsigned AbbrevToUse) {
     572             :   SmallVector<unsigned, 64> Vals;
     573             : 
     574             :   // Code: [strchar x N]
     575             :   for (unsigned i = 0, e = Str.size(); i != e; ++i) {
     576           0 :     if (AbbrevToUse && !BitCodeAbbrevOp::isChar6(Str[i]))
     577             :       AbbrevToUse = 0;
     578             :     Vals.push_back(Str[i]);
     579             :   }
     580             : 
     581             :   // Emit the finished record.
     582             :   Stream.EmitRecord(Code, Vals, AbbrevToUse);
     583             : }
     584             : 
     585             : static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind) {
     586             :   switch (Kind) {
     587             :   case Attribute::Alignment:
     588             :     return bitc::ATTR_KIND_ALIGNMENT;
     589             :   case Attribute::AllocSize:
     590             :     return bitc::ATTR_KIND_ALLOC_SIZE;
     591             :   case Attribute::AlwaysInline:
     592         696 :     return bitc::ATTR_KIND_ALWAYS_INLINE;
     593             :   case Attribute::ArgMemOnly:
     594          38 :     return bitc::ATTR_KIND_ARGMEMONLY;
     595         309 :   case Attribute::Builtin:
     596         110 :     return bitc::ATTR_KIND_BUILTIN;
     597          58 :   case Attribute::ByVal:
     598          41 :     return bitc::ATTR_KIND_BY_VAL;
     599         140 :   case Attribute::Convergent:
     600             :     return bitc::ATTR_KIND_CONVERGENT;
     601           0 :   case Attribute::InAlloca:
     602             :     return bitc::ATTR_KIND_IN_ALLOCA;
     603             :   case Attribute::Cold:
     604       13368 :     return bitc::ATTR_KIND_COLD;
     605             :   case Attribute::InaccessibleMemOnly:
     606             :     return bitc::ATTR_KIND_INACCESSIBLEMEM_ONLY;
     607             :   case Attribute::InaccessibleMemOrArgMemOnly:
     608             :     return bitc::ATTR_KIND_INACCESSIBLEMEM_OR_ARGMEMONLY;
     609      288129 :   case Attribute::InlineHint:
     610      274761 :     return bitc::ATTR_KIND_INLINE_HINT;
     611             :   case Attribute::InReg:
     612      549522 :     return bitc::ATTR_KIND_IN_REG;
     613             :   case Attribute::JumpTable:
     614             :     return bitc::ATTR_KIND_JUMP_TABLE;
     615             :   case Attribute::MinSize:
     616       13368 :     return bitc::ATTR_KIND_MIN_SIZE;
     617       13368 :   case Attribute::Naked:
     618             :     return bitc::ATTR_KIND_NAKED;
     619             :   case Attribute::Nest:
     620             :     return bitc::ATTR_KIND_NEST;
     621             :   case Attribute::NoAlias:
     622             :     return bitc::ATTR_KIND_NO_ALIAS;
     623             :   case Attribute::NoBuiltin:
     624             :     return bitc::ATTR_KIND_NO_BUILTIN;
     625             :   case Attribute::NoCapture:
     626             :     return bitc::ATTR_KIND_NO_CAPTURE;
     627             :   case Attribute::NoDuplicate:
     628             :     return bitc::ATTR_KIND_NO_DUPLICATE;
     629             :   case Attribute::NoImplicitFloat:
     630             :     return bitc::ATTR_KIND_NO_IMPLICIT_FLOAT;
     631             :   case Attribute::NoInline:
     632             :     return bitc::ATTR_KIND_NO_INLINE;
     633             :   case Attribute::NoRecurse:
     634             :     return bitc::ATTR_KIND_NO_RECURSE;
     635             :   case Attribute::NonLazyBind:
     636             :     return bitc::ATTR_KIND_NON_LAZY_BIND;
     637             :   case Attribute::NonNull:
     638             :     return bitc::ATTR_KIND_NON_NULL;
     639             :   case Attribute::Dereferenceable:
     640             :     return bitc::ATTR_KIND_DEREFERENCEABLE;
     641             :   case Attribute::DereferenceableOrNull:
     642             :     return bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL;
     643             :   case Attribute::NoRedZone:
     644             :     return bitc::ATTR_KIND_NO_RED_ZONE;
     645             :   case Attribute::NoReturn:
     646             :     return bitc::ATTR_KIND_NO_RETURN;
     647             :   case Attribute::NoCfCheck:
     648             :     return bitc::ATTR_KIND_NOCF_CHECK;
     649             :   case Attribute::NoUnwind:
     650             :     return bitc::ATTR_KIND_NO_UNWIND;
     651             :   case Attribute::OptForFuzzing:
     652             :     return bitc::ATTR_KIND_OPT_FOR_FUZZING;
     653             :   case Attribute::OptimizeForSize:
     654             :     return bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE;
     655             :   case Attribute::OptimizeNone:
     656             :     return bitc::ATTR_KIND_OPTIMIZE_NONE;
     657             :   case Attribute::ReadNone:
     658             :     return bitc::ATTR_KIND_READ_NONE;
     659             :   case Attribute::ReadOnly:
     660             :     return bitc::ATTR_KIND_READ_ONLY;
     661             :   case Attribute::Returned:
     662             :     return bitc::ATTR_KIND_RETURNED;
     663             :   case Attribute::ReturnsTwice:
     664             :     return bitc::ATTR_KIND_RETURNS_TWICE;
     665             :   case Attribute::SExt:
     666             :     return bitc::ATTR_KIND_S_EXT;
     667             :   case Attribute::Speculatable:
     668             :     return bitc::ATTR_KIND_SPECULATABLE;
     669             :   case Attribute::StackAlignment:
     670             :     return bitc::ATTR_KIND_STACK_ALIGNMENT;
     671             :   case Attribute::StackProtect:
     672             :     return bitc::ATTR_KIND_STACK_PROTECT;
     673             :   case Attribute::StackProtectReq:
     674             :     return bitc::ATTR_KIND_STACK_PROTECT_REQ;
     675             :   case Attribute::StackProtectStrong:
     676             :     return bitc::ATTR_KIND_STACK_PROTECT_STRONG;
     677             :   case Attribute::SafeStack:
     678             :     return bitc::ATTR_KIND_SAFESTACK;
     679             :   case Attribute::ShadowCallStack:
     680             :     return bitc::ATTR_KIND_SHADOWCALLSTACK;
     681             :   case Attribute::StrictFP:
     682             :     return bitc::ATTR_KIND_STRICT_FP;
     683             :   case Attribute::StructRet:
     684             :     return bitc::ATTR_KIND_STRUCT_RET;
     685             :   case Attribute::SanitizeAddress:
     686             :     return bitc::ATTR_KIND_SANITIZE_ADDRESS;
     687             :   case Attribute::SanitizeHWAddress:
     688             :     return bitc::ATTR_KIND_SANITIZE_HWADDRESS;
     689             :   case Attribute::SanitizeThread:
     690             :     return bitc::ATTR_KIND_SANITIZE_THREAD;
     691             :   case Attribute::SanitizeMemory:
     692             :     return bitc::ATTR_KIND_SANITIZE_MEMORY;
     693             :   case Attribute::SpeculativeLoadHardening:
     694             :     return bitc::ATTR_KIND_SPECULATIVE_LOAD_HARDENING;
     695             :   case Attribute::SwiftError:
     696             :     return bitc::ATTR_KIND_SWIFT_ERROR;
     697             :   case Attribute::SwiftSelf:
     698             :     return bitc::ATTR_KIND_SWIFT_SELF;
     699             :   case Attribute::UWTable:
     700             :     return bitc::ATTR_KIND_UW_TABLE;
     701             :   case Attribute::WriteOnly:
     702             :     return bitc::ATTR_KIND_WRITEONLY;
     703             :   case Attribute::ZExt:
     704             :     return bitc::ATTR_KIND_Z_EXT;
     705             :   case Attribute::EndAttrKinds:
     706             :     llvm_unreachable("Can not encode end-attribute kinds marker.");
     707             :   case Attribute::None:
     708             :     llvm_unreachable("Can not encode none-attribute.");
     709             :   }
     710             : 
     711             :   llvm_unreachable("Trying to encode unknown attribute");
     712             : }
     713             : 
     714             : void ModuleBitcodeWriter::writeAttributeGroupTable() {
     715             :   const std::vector<ValueEnumerator::IndexAndAttrSet> &AttrGrps =
     716             :       VE.getAttributeGroups();
     717             :   if (AttrGrps.empty()) return;
     718             : 
     719             :   Stream.EnterSubblock(bitc::PARAMATTR_GROUP_BLOCK_ID, 3);
     720             : 
     721             :   SmallVector<uint64_t, 64> Record;
     722             :   for (ValueEnumerator::IndexAndAttrSet Pair : AttrGrps) {
     723             :     unsigned AttrListIndex = Pair.first;
     724             :     AttributeSet AS = Pair.second;
     725             :     Record.push_back(VE.getAttributeGroupID(Pair));
     726             :     Record.push_back(AttrListIndex);
     727             : 
     728             :     for (Attribute Attr : AS) {
     729             :       if (Attr.isEnumAttribute()) {
     730             :         Record.push_back(0);
     731             :         Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
     732             :       } else if (Attr.isIntAttribute()) {
     733             :         Record.push_back(1);
     734             :         Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
     735             :         Record.push_back(Attr.getValueAsInt());
     736             :       } else {
     737             :         StringRef Kind = Attr.getKindAsString();
     738             :         StringRef Val = Attr.getValueAsString();
     739             : 
     740             :         Record.push_back(Val.empty() ? 3 : 4);
     741             :         Record.append(Kind.begin(), Kind.end());
     742             :         Record.push_back(0);
     743             :         if (!Val.empty()) {
     744             :           Record.append(Val.begin(), Val.end());
     745           0 :           Record.push_back(0);
     746             :         }
     747             :       }
     748        4708 :     }
     749             : 
     750             :     Stream.EmitRecord(bitc::PARAMATTR_GRP_CODE_ENTRY, Record);
     751        4708 :     Record.clear();
     752             :   }
     753        1340 : 
     754             :   Stream.ExitBlock();
     755             : }
     756        6146 : 
     757             : void ModuleBitcodeWriter::writeAttributeTable() {
     758        4806 :   const std::vector<AttributeList> &Attrs = VE.getAttributeLists();
     759        4806 :   if (Attrs.empty()) return;
     760        4806 : 
     761             :   Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3);
     762       23280 : 
     763       18474 :   SmallVector<uint64_t, 64> Record;
     764        6756 :   for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
     765       13512 :     AttributeList AL = Attrs[i];
     766       11718 :     for (unsigned i = AL.index_begin(), e = AL.index_end(); i != e; ++i) {
     767         892 :       AttributeSet AS = AL.getAttributes(i);
     768        1784 :       if (AS.hasAttributes())
     769         892 :         Record.push_back(VE.getAttributeGroupID({i, AS}));
     770             :     }
     771       10826 : 
     772       10826 :     Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record);
     773             :     Record.clear();
     774       21578 :   }
     775       21652 : 
     776       10826 :   Stream.ExitBlock();
     777       10826 : }
     778       10752 : 
     779       10752 : /// WriteTypeTable - Write out the type table for a module.
     780             : void ModuleBitcodeWriter::writeTypeTable() {
     781             :   const ValueEnumerator::TypeList &TypeList = VE.getTypes();
     782             : 
     783             :   Stream.EnterSubblock(bitc::TYPE_BLOCK_ID_NEW, 4 /*count from # abbrevs */);
     784        4806 :   SmallVector<uint64_t, 64> TypeVals;
     785             : 
     786             :   uint64_t NumBits = VE.computeBitsRequiredForTypeIndicies();
     787             : 
     788        1340 :   // Abbrev for TYPE_CODE_POINTER.
     789             :   auto Abbv = std::make_shared<BitCodeAbbrev>();
     790             :   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_POINTER));
     791        4708 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
     792             :   Abbv->Add(BitCodeAbbrevOp(0));  // Addrspace = 0
     793        4708 :   unsigned PtrAbbrev = Stream.EmitAbbrev(std::move(Abbv));
     794             : 
     795        1340 :   // Abbrev for TYPE_CODE_FUNCTION.
     796             :   Abbv = std::make_shared<BitCodeAbbrev>();
     797             :   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION));
     798        6601 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));  // isvararg
     799        7842 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
     800       12899 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
     801        8978 :   unsigned FunctionAbbrev = Stream.EmitAbbrev(std::move(Abbv));
     802        8978 : 
     803        6365 :   // Abbrev for TYPE_CODE_STRUCT_ANON.
     804             :   Abbv = std::make_shared<BitCodeAbbrev>();
     805             :   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_ANON));
     806        3921 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));  // ispacked
     807             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
     808             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
     809             :   unsigned StructAnonAbbrev = Stream.EmitAbbrev(std::move(Abbv));
     810        1340 : 
     811             :   // Abbrev for TYPE_CODE_STRUCT_NAME.
     812             :   Abbv = std::make_shared<BitCodeAbbrev>();
     813             :   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAME));
     814        4708 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
     815             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
     816             :   unsigned StructNameAbbrev = Stream.EmitAbbrev(std::move(Abbv));
     817        4708 : 
     818             :   // Abbrev for TYPE_CODE_STRUCT_NAMED.
     819             :   Abbv = std::make_shared<BitCodeAbbrev>();
     820        4708 :   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAMED));
     821             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));  // ispacked
     822             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
     823             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
     824        4708 :   unsigned StructNamedAbbrev = Stream.EmitAbbrev(std::move(Abbv));
     825        4708 : 
     826        4708 :   // Abbrev for TYPE_CODE_ARRAY.
     827       14124 :   Abbv = std::make_shared<BitCodeAbbrev>();
     828             :   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY));
     829             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));   // size
     830        4708 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
     831        4708 :   unsigned ArrayAbbrev = Stream.EmitAbbrev(std::move(Abbv));
     832        4708 : 
     833        4708 :   // Emit an entry count so the reader can reserve space.
     834        4708 :   TypeVals.push_back(TypeList.size());
     835       14124 :   Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
     836             :   TypeVals.clear();
     837             : 
     838        4708 :   // Loop over all of the types, emitting each in turn.
     839        4708 :   for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
     840        4708 :     Type *T = TypeList[i];
     841        4708 :     int AbbrevToUse = 0;
     842        4708 :     unsigned Code = 0;
     843       14124 : 
     844             :     switch (T->getTypeID()) {
     845             :     case Type::VoidTyID:      Code = bitc::TYPE_CODE_VOID;      break;
     846        4708 :     case Type::HalfTyID:      Code = bitc::TYPE_CODE_HALF;      break;
     847        4708 :     case Type::FloatTyID:     Code = bitc::TYPE_CODE_FLOAT;     break;
     848        4708 :     case Type::DoubleTyID:    Code = bitc::TYPE_CODE_DOUBLE;    break;
     849        4708 :     case Type::X86_FP80TyID:  Code = bitc::TYPE_CODE_X86_FP80;  break;
     850       14124 :     case Type::FP128TyID:     Code = bitc::TYPE_CODE_FP128;     break;
     851             :     case Type::PPC_FP128TyID: Code = bitc::TYPE_CODE_PPC_FP128; break;
     852             :     case Type::LabelTyID:     Code = bitc::TYPE_CODE_LABEL;     break;
     853        4708 :     case Type::MetadataTyID:  Code = bitc::TYPE_CODE_METADATA;  break;
     854        4708 :     case Type::X86_MMXTyID:   Code = bitc::TYPE_CODE_X86_MMX;   break;
     855        4708 :     case Type::TokenTyID:     Code = bitc::TYPE_CODE_TOKEN;     break;
     856        4708 :     case Type::IntegerTyID:
     857        4708 :       // INTEGER: [width]
     858       14124 :       Code = bitc::TYPE_CODE_INTEGER;
     859             :       TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
     860             :       break;
     861        4708 :     case Type::PointerTyID: {
     862        4708 :       PointerType *PTy = cast<PointerType>(T);
     863        4708 :       // POINTER: [pointee type, address space]
     864        4708 :       Code = bitc::TYPE_CODE_POINTER;
     865        9416 :       TypeVals.push_back(VE.getTypeID(PTy->getElementType()));
     866             :       unsigned AddressSpace = PTy->getAddressSpace();
     867             :       TypeVals.push_back(AddressSpace);
     868        9416 :       if (AddressSpace == 0) AbbrevToUse = PtrAbbrev;
     869        4708 :       break;
     870             :     }
     871             :     case Type::FunctionTyID: {
     872             :       FunctionType *FT = cast<FunctionType>(T);
     873       65272 :       // FUNCTION: [isvararg, retty, paramty x N]
     874      111712 :       Code = bitc::TYPE_CODE_FUNCTION;
     875             :       TypeVals.push_back(FT->isVarArg());
     876             :       TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
     877             :       for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
     878       55856 :         TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
     879        3722 :       AbbrevToUse = FunctionAbbrev;
     880          25 :       break;
     881         392 :     }
     882         256 :     case Type::StructTyID: {
     883          22 :       StructType *ST = cast<StructType>(T);
     884          11 :       // STRUCT: [ispacked, eltty x N]
     885          11 :       TypeVals.push_back(ST->isPacked());
     886         776 :       // Output all of the element types.
     887        4708 :       for (StructType::element_iterator I = ST->element_begin(),
     888           9 :            E = ST->element_end(); I != E; ++I)
     889          13 :         TypeVals.push_back(VE.getTypeID(*I));
     890        6553 : 
     891             :       if (ST->isLiteral()) {
     892             :         Code = bitc::TYPE_CODE_STRUCT_ANON;
     893        6553 :         AbbrevToUse = StructAnonAbbrev;
     894        6553 :       } else {
     895             :         if (ST->isOpaque()) {
     896             :           Code = bitc::TYPE_CODE_OPAQUE;
     897             :         } else {
     898             :           Code = bitc::TYPE_CODE_STRUCT_NAMED;
     899       21754 :           AbbrevToUse = StructNamedAbbrev;
     900             :         }
     901       21754 : 
     902       21754 :         // Emit the name if it is present.
     903             :         if (!ST->getName().empty())
     904             :           writeStringRecord(Stream, bitc::TYPE_CODE_STRUCT_NAME, ST->getName(),
     905             :                             StructNameAbbrev);
     906             :       }
     907             :       break;
     908             :     }
     909        9829 :     case Type::ArrayTyID: {
     910        9829 :       ArrayType *AT = cast<ArrayType>(T);
     911       24044 :       // ARRAY: [numelts, eltty]
     912       14215 :       Code = bitc::TYPE_CODE_ARRAY;
     913        9829 :       TypeVals.push_back(AT->getNumElements());
     914        9829 :       TypeVals.push_back(VE.getTypeID(AT->getElementType()));
     915             :       AbbrevToUse = ArrayAbbrev;
     916             :       break;
     917             :     }
     918             :     case Type::VectorTyID: {
     919        3631 :       VectorType *VT = cast<VectorType>(T);
     920             :       // VECTOR [numelts, eltty]
     921       17252 :       Code = bitc::TYPE_CODE_VECTOR;
     922       17252 :       TypeVals.push_back(VT->getNumElements());
     923       13621 :       TypeVals.push_back(VE.getTypeID(VT->getElementType()));
     924             :       break;
     925        3631 :     }
     926             :     }
     927         679 : 
     928             :     // Emit the finished record.
     929        2952 :     Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
     930             :     TypeVals.clear();
     931             :   }
     932             : 
     933        2808 :   Stream.ExitBlock();
     934             : }
     935             : 
     936             : static unsigned getEncodedLinkage(const GlobalValue::LinkageTypes Linkage) {
     937        2952 :   switch (Linkage) {
     938        2850 :   case GlobalValue::ExternalLinkage:
     939             :     return 0;
     940             :   case GlobalValue::WeakAnyLinkage:
     941             :     return 16;
     942             :   case GlobalValue::AppendingLinkage:
     943             :     return 2;
     944             :   case GlobalValue::InternalLinkage:
     945             :     return 3;
     946             :   case GlobalValue::LinkOnceAnyLinkage:
     947        3625 :     return 18;
     948        3625 :   case GlobalValue::ExternalWeakLinkage:
     949        3625 :     return 7;
     950        3625 :   case GlobalValue::CommonLinkage:
     951             :     return 8;
     952             :   case GlobalValue::PrivateLinkage:
     953             :     return 9;
     954             :   case GlobalValue::WeakODRLinkage:
     955             :     return 17;
     956         519 :   case GlobalValue::LinkOnceODRLinkage:
     957         519 :     return 19;
     958         519 :   case GlobalValue::AvailableExternallyLinkage:
     959             :     return 12;
     960             :   }
     961             :   llvm_unreachable("Invalid linkage");
     962             : }
     963       55856 : 
     964             : static unsigned getEncodedLinkage(const GlobalValue &GV) {
     965             :   return getEncodedLinkage(GV.getLinkage());
     966             : }
     967        4708 : 
     968        4708 : static uint64_t getEncodedFFlags(FunctionSummary::FFlags Flags) {
     969             :   uint64_t RawFlags = 0;
     970             :   RawFlags |= Flags.ReadNone;
     971             :   RawFlags |= (Flags.ReadOnly << 1);
     972             :   RawFlags |= (Flags.NoRecurse << 2);
     973             :   RawFlags |= (Flags.ReturnDoesNotAlias << 3);
     974             :   return RawFlags;
     975             : }
     976             : 
     977             : // Decode the flags for GlobalValue in the summary
     978             : static uint64_t getEncodedGVSummaryFlags(GlobalValueSummary::GVFlags Flags) {
     979             :   uint64_t RawFlags = 0;
     980             : 
     981             :   RawFlags |= Flags.NotEligibleToImport; // bool
     982             :   RawFlags |= (Flags.Live << 1);
     983             :   RawFlags |= (Flags.DSOLocal << 2);
     984             : 
     985             :   // Linkage don't need to be remapped at that time for the summary. Any future
     986             :   // change to the getEncodedLinkage() function will need to be taken into
     987             :   // account here as well.
     988             :   RawFlags = (RawFlags << 4) | Flags.Linkage; // 4 bits
     989             : 
     990             :   return RawFlags;
     991             : }
     992             : 
     993             : static unsigned getEncodedVisibility(const GlobalValue &GV) {
     994             :   switch (GV.getVisibility()) {
     995           0 :   case GlobalValue::DefaultVisibility:   return 0;
     996             :   case GlobalValue::HiddenVisibility:    return 1;
     997             :   case GlobalValue::ProtectedVisibility: return 2;
     998             :   }
     999             :   llvm_unreachable("Invalid visibility");
    1000             : }
    1001             : 
    1002             : static unsigned getEncodedDLLStorageClass(const GlobalValue &GV) {
    1003             :   switch (GV.getDLLStorageClass()) {
    1004         615 :   case GlobalValue::DefaultStorageClass:   return 0;
    1005         615 :   case GlobalValue::DLLImportStorageClass: return 1;
    1006         615 :   case GlobalValue::DLLExportStorageClass: return 2;
    1007         615 :   }
    1008             :   llvm_unreachable("Invalid DLL storage class");
    1009             : }
    1010             : 
    1011             : static unsigned getEncodedThreadLocalMode(const GlobalValue &GV) {
    1012             :   switch (GV.getThreadLocalMode()) {
    1013             :     case GlobalVariable::NotThreadLocal:         return 0;
    1014             :     case GlobalVariable::GeneralDynamicTLSModel: return 1;
    1015         985 :     case GlobalVariable::LocalDynamicTLSModel:   return 2;
    1016         985 :     case GlobalVariable::InitialExecTLSModel:    return 3;
    1017         985 :     case GlobalVariable::LocalExecTLSModel:      return 4;
    1018             :   }
    1019             :   llvm_unreachable("Invalid TLS model");
    1020             : }
    1021             : 
    1022         985 : static unsigned getEncodedComdatSelectionKind(const Comdat &C) {
    1023             :   switch (C.getSelectionKind()) {
    1024             :   case Comdat::Any:
    1025             :     return bitc::COMDAT_SELECTION_KIND_ANY;
    1026             :   case Comdat::ExactMatch:
    1027             :     return bitc::COMDAT_SELECTION_KIND_EXACT_MATCH;
    1028             :   case Comdat::Largest:
    1029             :     return bitc::COMDAT_SELECTION_KIND_LARGEST;
    1030             :   case Comdat::NoDuplicates:
    1031             :     return bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES;
    1032             :   case Comdat::SameSize:
    1033           0 :     return bitc::COMDAT_SELECTION_KIND_SAME_SIZE;
    1034             :   }
    1035             :   llvm_unreachable("Invalid selection kind");
    1036             : }
    1037             : 
    1038             : static unsigned getEncodedUnnamedAddr(const GlobalValue &GV) {
    1039             :   switch (GV.getUnnamedAddr()) {
    1040             :   case GlobalValue::UnnamedAddr::None:   return 0;
    1041             :   case GlobalValue::UnnamedAddr::Local:  return 2;
    1042           0 :   case GlobalValue::UnnamedAddr::Global: return 1;
    1043             :   }
    1044             :   llvm_unreachable("Invalid unnamed_addr");
    1045             : }
    1046             : 
    1047             : size_t ModuleBitcodeWriter::addToStrtab(StringRef Str) {
    1048             :   if (GenerateHash)
    1049             :     Hasher.update(Str);
    1050             :   return StrtabBuilder.add(Str);
    1051             : }
    1052             : 
    1053           0 : void ModuleBitcodeWriter::writeComdats() {
    1054             :   SmallVector<unsigned, 64> Vals;
    1055             :   for (const Comdat *C : VE.getComdats()) {
    1056             :     // COMDAT: [strtab offset, strtab size, selection_kind]
    1057        2107 :     Vals.push_back(addToStrtab(C->getName()));
    1058             :     Vals.push_back(C->getName().size());
    1059             :     Vals.push_back(getEncodedComdatSelectionKind(*C));
    1060             :     Stream.EmitRecord(bitc::MODULE_CODE_COMDAT, Vals, /*AbbrevToUse=*/0);
    1061             :     Vals.clear();
    1062             :   }
    1063             : }
    1064             : 
    1065             : /// Write a record that will eventually hold the word offset of the
    1066             : /// module-level VST. For now the offset is 0, which will be backpatched
    1067             : /// after the real VST is written. Saves the bit offset to backpatch.
    1068             : void ModuleBitcodeWriter::writeValueSymbolTableForwardDecl() {
    1069           0 :   // Write a placeholder value in for the offset of the real VST,
    1070             :   // which is written after the function blocks so that it can include
    1071             :   // the offset of each function. The placeholder offset will be
    1072             :   // updated when the real VST is written.
    1073             :   auto Abbv = std::make_shared<BitCodeAbbrev>();
    1074             :   Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_VSTOFFSET));
    1075             :   // Blocks are 32-bit aligned, so we can use a 32-bit word offset to
    1076             :   // hold the real VST offset. Must use fixed instead of VBR as we don't
    1077             :   // know how many VBR chunks to reserve ahead of time.
    1078           0 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
    1079             :   unsigned VSTOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbv));
    1080             : 
    1081       29563 :   // Emit the placeholder
    1082       29563 :   uint64_t Vals[] = {bitc::MODULE_CODE_VSTOFFSET, 0};
    1083         290 :   Stream.EmitRecordWithAbbrev(VSTOffsetAbbrev, Vals);
    1084       29563 : 
    1085             :   // Compute and save the bit offset to the placeholder, which will be
    1086             :   // patched when the real VST is written. We can simply subtract the 32-bit
    1087        4708 :   // fixed size from the current bit number to get the location to backpatch.
    1088             :   VSTOffsetPlaceholder = Stream.GetCurrentBitNo() - 32;
    1089        6815 : }
    1090             : 
    1091        2107 : enum StringEncoding { SE_Char6, SE_Fixed7, SE_Fixed8 };
    1092        2107 : 
    1093        2107 : /// Determine the encoding to use for the given string name and length.
    1094        2107 : static StringEncoding getStringEncoding(StringRef Str) {
    1095             :   bool isChar6 = true;
    1096             :   for (char C : Str) {
    1097        4708 :     if (isChar6)
    1098             :       isChar6 = BitCodeAbbrevOp::isChar6(C);
    1099             :     if ((unsigned char)C & 128)
    1100             :       // don't bother scanning the rest.
    1101             :       return SE_Fixed8;
    1102           0 :   }
    1103             :   if (isChar6)
    1104             :     return SE_Char6;
    1105             :   return SE_Fixed7;
    1106             : }
    1107             : 
    1108           0 : /// Emit top-level description of module, including target triple, inline asm,
    1109             : /// descriptors for global variables, and function prototype info.
    1110             : /// Returns the bit offset to backpatch with the location of the real VST.
    1111             : void ModuleBitcodeWriter::writeModuleInfo() {
    1112           0 :   // Emit various pieces of data attached to a module.
    1113           0 :   if (!M.getTargetTriple().empty())
    1114             :     writeStringRecord(Stream, bitc::MODULE_CODE_TRIPLE, M.getTargetTriple(),
    1115             :                       0 /*TODO*/);
    1116           0 :   const std::string &DL = M.getDataLayoutStr();
    1117           0 :   if (!DL.empty())
    1118             :     writeStringRecord(Stream, bitc::MODULE_CODE_DATALAYOUT, DL, 0 /*TODO*/);
    1119             :   if (!M.getModuleInlineAsm().empty())
    1120             :     writeStringRecord(Stream, bitc::MODULE_CODE_ASM, M.getModuleInlineAsm(),
    1121             :                       0 /*TODO*/);
    1122           0 : 
    1123           0 :   // Emit information about sections and GC, computing how many there are. Also
    1124             :   // compute the maximum alignment value.
    1125             :   std::map<std::string, unsigned> SectionMap;
    1126             :   std::map<std::string, unsigned> GCMap;
    1127             :   unsigned MaxAlignment = 0;
    1128       96723 :   unsigned MaxGlobalType = 0;
    1129             :   for (const GlobalValue &GV : M.globals()) {
    1130     1170052 :     MaxAlignment = std::max(MaxAlignment, GV.getAlignment());
    1131     1073329 :     MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV.getValueType()));
    1132             :     if (GV.hasSection()) {
    1133     1073329 :       // Give section names unique ID's.
    1134             :       unsigned &Entry = SectionMap[GV.getSection()];
    1135             :       if (!Entry) {
    1136             :         writeStringRecord(Stream, bitc::MODULE_CODE_SECTIONNAME, GV.getSection(),
    1137       96723 :                           0 /*TODO*/);
    1138       91626 :         Entry = SectionMap.size();
    1139             :       }
    1140             :     }
    1141             :   }
    1142             :   for (const Function &F : M) {
    1143             :     MaxAlignment = std::max(MaxAlignment, F.getAlignment());
    1144             :     if (F.hasSection()) {
    1145        4708 :       // Give section names unique ID's.
    1146             :       unsigned &Entry = SectionMap[F.getSection()];
    1147        9416 :       if (!Entry) {
    1148        2876 :         writeStringRecord(Stream, bitc::MODULE_CODE_SECTIONNAME, F.getSection(),
    1149             :                           0 /*TODO*/);
    1150        4708 :         Entry = SectionMap.size();
    1151        4708 :       }
    1152        2501 :     }
    1153        9416 :     if (F.hasGC()) {
    1154          88 :       // Same for GC names.
    1155             :       unsigned &Entry = GCMap[F.getGC()];
    1156             :       if (!Entry) {
    1157             :         writeStringRecord(Stream, bitc::MODULE_CODE_GCNAME, F.getGC(),
    1158             :                           0 /*TODO*/);
    1159             :         Entry = GCMap.size();
    1160             :       }
    1161        4708 :     }
    1162        4708 :   }
    1163       14748 : 
    1164       10040 :   // Emit abbrev for globals, now that we know # sections and max alignment.
    1165       13835 :   unsigned SimpleGVarAbbrev = 0;
    1166       10040 :   if (!M.global_empty()) {
    1167             :     // Add an abbrev for common globals with no visibility or thread localness.
    1168        2312 :     auto Abbv = std::make_shared<BitCodeAbbrev>();
    1169        1156 :     Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
    1170         297 :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
    1171             :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
    1172         297 :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
    1173             :                               Log2_32_Ceil(MaxGlobalType+1)));
    1174             :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // AddrSpace << 2
    1175             :                                                            //| explicitType << 1
    1176       21309 :                                                            //| constant
    1177       16601 :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // Initializer.
    1178       16601 :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // Linkage.
    1179             :     if (MaxAlignment == 0)                                 // Alignment.
    1180         102 :       Abbv->Add(BitCodeAbbrevOp(0));
    1181          51 :     else {
    1182          34 :       unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1;
    1183             :       Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
    1184          34 :                                Log2_32_Ceil(MaxEncAlignment+1)));
    1185             :     }
    1186             :     if (SectionMap.empty())                                    // Section.
    1187       16601 :       Abbv->Add(BitCodeAbbrevOp(0));
    1188             :     else
    1189          16 :       Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
    1190          16 :                                Log2_32_Ceil(SectionMap.size()+1)));
    1191          14 :     // Don't bother emitting vis + thread local.
    1192             :     SimpleGVarAbbrev = Stream.EmitAbbrev(std::move(Abbv));
    1193          14 :   }
    1194             : 
    1195             :   SmallVector<unsigned, 64> Vals;
    1196             :   // Emit the module's source file name.
    1197             :   {
    1198             :     StringEncoding Bits = getStringEncoding(M.getSourceFileName());
    1199             :     BitCodeAbbrevOp AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8);
    1200        9416 :     if (Bits == SE_Char6)
    1201             :       AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Char6);
    1202             :     else if (Bits == SE_Fixed7)
    1203        1572 :       AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7);
    1204        1572 : 
    1205        1572 :     // MODULE_CODE_SOURCE_FILENAME: [namechar x N]
    1206        1572 :     auto Abbv = std::make_shared<BitCodeAbbrev>();
    1207        1572 :     Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_SOURCE_FILENAME));
    1208        1572 :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
    1209             :     Abbv->Add(AbbrevOpToUse);
    1210             :     unsigned FilenameAbbrev = Stream.EmitAbbrev(std::move(Abbv));
    1211        1572 : 
    1212        1572 :     for (const auto P : M.getSourceFileName())
    1213        1572 :       Vals.push_back((unsigned char)P);
    1214         942 : 
    1215             :     // Emit the finished record.
    1216         630 :     Stream.EmitRecord(bitc::MODULE_CODE_SOURCE_FILENAME, Vals, FilenameAbbrev);
    1217         630 :     Vals.clear();
    1218             :   }
    1219             : 
    1220        1572 :   // Emit the global variable information.
    1221        1291 :   for (const GlobalVariable &GV : M.globals()) {
    1222             :     unsigned AbbrevToUse = 0;
    1223         281 : 
    1224         281 :     // GLOBALVAR: [strtab offset, strtab size, type, isconst, initid,
    1225             :     //             linkage, alignment, section, visibility, threadlocal,
    1226        4716 :     //             unnamed_addr, externally_initialized, dllstorageclass,
    1227             :     //             comdat, attributes, DSO_Local]
    1228             :     Vals.push_back(addToStrtab(GV.getName()));
    1229             :     Vals.push_back(GV.getName().size());
    1230             :     Vals.push_back(VE.getTypeID(GV.getValueType()));
    1231             :     Vals.push_back(GV.getType()->getAddressSpace() << 2 | 2 | GV.isConstant());
    1232        9416 :     Vals.push_back(GV.isDeclaration() ? 0 :
    1233             :                    (VE.getValueID(GV.getInitializer()) + 1));
    1234        4708 :     Vals.push_back(getEncodedLinkage(GV));
    1235          54 :     Vals.push_back(Log2_32(GV.getAlignment())+1);
    1236        4654 :     Vals.push_back(GV.hasSection() ? SectionMap[GV.getSection()] : 0);
    1237        4654 :     if (GV.isThreadLocal() ||
    1238             :         GV.getVisibility() != GlobalValue::DefaultVisibility ||
    1239             :         GV.getUnnamedAddr() != GlobalValue::UnnamedAddr::None ||
    1240             :         GV.isExternallyInitialized() ||
    1241        4708 :         GV.getDLLStorageClass() != GlobalValue::DefaultStorageClass ||
    1242        4708 :         GV.hasComdat() ||
    1243             :         GV.hasAttributes() ||
    1244        9416 :         GV.isDSOLocal()) {
    1245             :       Vals.push_back(getEncodedVisibility(GV));
    1246      250768 :       Vals.push_back(getEncodedThreadLocalMode(GV));
    1247      246060 :       Vals.push_back(getEncodedUnnamedAddr(GV));
    1248             :       Vals.push_back(GV.isExternallyInitialized());
    1249             :       Vals.push_back(getEncodedDLLStorageClass(GV));
    1250        4708 :       Vals.push_back(GV.hasComdat() ? VE.getComdatID(GV.getComdat()) : 0);
    1251             : 
    1252             :       auto AL = GV.getAttributesAsList(AttributeList::FunctionIndex);
    1253             :       Vals.push_back(VE.getAttributeListID(AL));
    1254             : 
    1255       14748 :       Vals.push_back(GV.isDSOLocal());
    1256             :     } else {
    1257             :       AbbrevToUse = SimpleGVarAbbrev;
    1258             :     }
    1259             : 
    1260             :     Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
    1261             :     Vals.clear();
    1262       10040 :   }
    1263       10040 : 
    1264       10040 :   // Emit the function proto information.
    1265       20080 :   for (const Function &F : M) {
    1266       18469 :     // FUNCTION:  [strtab offset, strtab size, type, callingconv, isproto,
    1267       16858 :     //             linkage, paramattrs, alignment, section, visibility, gc,
    1268       10040 :     //             unnamed_addr, prologuedata, dllstorageclass, comdat,
    1269       10040 :     //             prefixdata, personalityfn, DSO_Local, addrspace]
    1270       12352 :     Vals.push_back(addToStrtab(F.getName()));
    1271        9988 :     Vals.push_back(F.getName().size());
    1272        9638 :     Vals.push_back(VE.getTypeID(F.getFunctionType()));
    1273        6436 :     Vals.push_back(F.getCallingConv());
    1274        6427 :     Vals.push_back(F.isDeclaration());
    1275        6412 :     Vals.push_back(getEncodedLinkage(F));
    1276        6168 :     Vals.push_back(VE.getAttributeListID(F.getAttributes()));
    1277       16196 :     Vals.push_back(Log2_32(F.getAlignment())+1);
    1278             :     Vals.push_back(F.hasSection() ? SectionMap[F.getSection()] : 0);
    1279        4414 :     Vals.push_back(getEncodedVisibility(F));
    1280        4414 :     Vals.push_back(F.hasGC() ? GCMap[F.getGC()] : 0);
    1281        4414 :     Vals.push_back(getEncodedUnnamedAddr(F));
    1282        4414 :     Vals.push_back(F.hasPrologueData() ? (VE.getValueID(F.getPrologueData()) + 1)
    1283        4414 :                                        : 0);
    1284        4414 :     Vals.push_back(getEncodedDLLStorageClass(F));
    1285             :     Vals.push_back(F.hasComdat() ? VE.getComdatID(F.getComdat()) : 0);
    1286        4414 :     Vals.push_back(F.hasPrefixData() ? (VE.getValueID(F.getPrefixData()) + 1)
    1287        4414 :                                      : 0);
    1288             :     Vals.push_back(
    1289        4414 :         F.hasPersonalityFn() ? (VE.getValueID(F.getPersonalityFn()) + 1) : 0);
    1290             : 
    1291             :     Vals.push_back(F.isDSOLocal());
    1292             :     Vals.push_back(F.getAddressSpace());
    1293             : 
    1294       10040 :     unsigned AbbrevToUse = 0;
    1295             :     Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
    1296             :     Vals.clear();
    1297             :   }
    1298             : 
    1299       21309 :   // Emit the alias information.
    1300             :   for (const GlobalAlias &A : M.aliases()) {
    1301             :     // ALIAS: [strtab offset, strtab size, alias type, aliasee val#, linkage,
    1302             :     //         visibility, dllstorageclass, threadlocal, unnamed_addr,
    1303             :     //         DSO_Local]
    1304       16601 :     Vals.push_back(addToStrtab(A.getName()));
    1305       16601 :     Vals.push_back(A.getName().size());
    1306       16601 :     Vals.push_back(VE.getTypeID(A.getValueType()));
    1307       16601 :     Vals.push_back(A.getType()->getAddressSpace());
    1308       16601 :     Vals.push_back(VE.getValueID(A.getAliasee()));
    1309       16601 :     Vals.push_back(getEncodedLinkage(A));
    1310       16601 :     Vals.push_back(getEncodedVisibility(A));
    1311       16601 :     Vals.push_back(getEncodedDLLStorageClass(A));
    1312       16703 :     Vals.push_back(getEncodedThreadLocalMode(A));
    1313       16601 :     Vals.push_back(getEncodedUnnamedAddr(A));
    1314       16601 :     Vals.push_back(A.isDSOLocal());
    1315       16601 : 
    1316       16601 :     unsigned AbbrevToUse = 0;
    1317             :     Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
    1318       16601 :     Vals.clear();
    1319       16601 :   }
    1320       16601 : 
    1321             :   // Emit the ifunc information.
    1322       16601 :   for (const GlobalIFunc &I : M.ifuncs()) {
    1323       16601 :     // IFUNC: [strtab offset, strtab size, ifunc type, address space, resolver
    1324             :     //         val#, linkage, visibility, DSO_Local]
    1325       16601 :     Vals.push_back(addToStrtab(I.getName()));
    1326       16601 :     Vals.push_back(I.getName().size());
    1327             :     Vals.push_back(VE.getTypeID(I.getValueType()));
    1328             :     Vals.push_back(I.getType()->getAddressSpace());
    1329       16601 :     Vals.push_back(VE.getValueID(I.getResolver()));
    1330             :     Vals.push_back(getEncodedLinkage(I));
    1331             :     Vals.push_back(getEncodedVisibility(I));
    1332             :     Vals.push_back(I.isDSOLocal());
    1333             :     Stream.EmitRecord(bitc::MODULE_CODE_IFUNC, Vals);
    1334        5492 :     Vals.clear();
    1335             :   }
    1336             : 
    1337             :   writeValueSymbolTableForwardDecl();
    1338         784 : }
    1339         784 : 
    1340         784 : static uint64_t getOptimizationFlags(const Value *V) {
    1341         784 :   uint64_t Flags = 0;
    1342         784 : 
    1343         784 :   if (const auto *OBO = dyn_cast<OverflowingBinaryOperator>(V)) {
    1344         784 :     if (OBO->hasNoSignedWrap())
    1345         784 :       Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP;
    1346         784 :     if (OBO->hasNoUnsignedWrap())
    1347         784 :       Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP;
    1348         784 :   } else if (const auto *PEO = dyn_cast<PossiblyExactOperator>(V)) {
    1349             :     if (PEO->isExact())
    1350             :       Flags |= 1 << bitc::PEO_EXACT;
    1351         784 :   } else if (const auto *FPMO = dyn_cast<FPMathOperator>(V)) {
    1352             :     if (FPMO->hasAllowReassoc())
    1353             :       Flags |= bitc::AllowReassoc;
    1354             :     if (FPMO->hasNoNaNs())
    1355             :       Flags |= bitc::NoNaNs;
    1356        4739 :     if (FPMO->hasNoInfs())
    1357             :       Flags |= bitc::NoInfs;
    1358             :     if (FPMO->hasNoSignedZeros())
    1359          31 :       Flags |= bitc::NoSignedZeros;
    1360          31 :     if (FPMO->hasAllowReciprocal())
    1361          31 :       Flags |= bitc::AllowReciprocal;
    1362          31 :     if (FPMO->hasAllowContract())
    1363          31 :       Flags |= bitc::AllowContract;
    1364          31 :     if (FPMO->hasApproxFunc())
    1365          31 :       Flags |= bitc::ApproxFunc;
    1366          31 :   }
    1367          31 : 
    1368             :   return Flags;
    1369             : }
    1370             : 
    1371        4708 : void ModuleBitcodeWriter::writeValueAsMetadata(
    1372        4708 :     const ValueAsMetadata *MD, SmallVectorImpl<uint64_t> &Record) {
    1373             :   // Mimic an MDNode with a value as one operand.
    1374       36498 :   Value *V = MD->getValue();
    1375             :   Record.push_back(VE.getTypeID(V->getType()));
    1376             :   Record.push_back(VE.getValueID(V));
    1377        8887 :   Stream.EmitRecord(bitc::METADATA_VALUE, Record, 0);
    1378        8887 :   Record.clear();
    1379             : }
    1380        8887 : 
    1381         728 : void ModuleBitcodeWriter::writeMDTuple(const MDTuple *N,
    1382         663 :                                        SmallVectorImpl<uint64_t> &Record,
    1383         663 :                                        unsigned Abbrev) {
    1384             :   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
    1385        1043 :     Metadata *MD = N->getOperand(i);
    1386        1043 :     assert(!(MD && isa<LocalAsMetadata>(MD)) &&
    1387             :            "Unexpected function-local metadata");
    1388        1043 :     Record.push_back(VE.getMetadataOrNullID(MD));
    1389         155 :   }
    1390        1043 :   Stream.EmitRecord(N->isDistinct() ? bitc::METADATA_DISTINCT_NODE
    1391          54 :                                     : bitc::METADATA_NODE,
    1392        1043 :                     Record, Abbrev);
    1393          36 :   Record.clear();
    1394        1043 : }
    1395          29 : 
    1396        1043 : unsigned ModuleBitcodeWriter::createDILocationAbbrev() {
    1397          37 :   // Assume the column is usually under 128, and always output the inlined-at
    1398        1043 :   // location (it's never more expensive than building an array size 1).
    1399          42 :   auto Abbv = std::make_shared<BitCodeAbbrev>();
    1400             :   Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_LOCATION));
    1401             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
    1402       36498 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
    1403             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
    1404             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
    1405        7258 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
    1406             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
    1407             :   return Stream.EmitAbbrev(std::move(Abbv));
    1408        7258 : }
    1409        7258 : 
    1410        7258 : void ModuleBitcodeWriter::writeDILocation(const DILocation *N,
    1411        7258 :                                           SmallVectorImpl<uint64_t> &Record,
    1412             :                                           unsigned &Abbrev) {
    1413        7258 :   if (!Abbrev)
    1414             :     Abbrev = createDILocationAbbrev();
    1415        8688 : 
    1416             :   Record.push_back(N->isDistinct());
    1417             :   Record.push_back(N->getLine());
    1418       33346 :   Record.push_back(N->getColumn());
    1419       24658 :   Record.push_back(VE.getMetadataID(N->getScope()));
    1420             :   Record.push_back(VE.getMetadataOrNullID(N->getInlinedAt()));
    1421             :   Record.push_back(N->isImplicitCode());
    1422       24658 : 
    1423             :   Stream.EmitRecord(bitc::METADATA_LOCATION, Record, Abbrev);
    1424       24586 :   Record.clear();
    1425             : }
    1426             : 
    1427             : unsigned ModuleBitcodeWriter::createGenericDINodeAbbrev() {
    1428        8688 :   // Assume the column is usually under 128, and always output the inlined-at
    1429             :   // location (it's never more expensive than building an array size 1).
    1430           0 :   auto Abbv = std::make_shared<BitCodeAbbrev>();
    1431             :   Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_GENERIC_DEBUG));
    1432             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
    1433             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
    1434           0 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
    1435           0 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
    1436           0 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
    1437           0 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
    1438           0 :   return Stream.EmitAbbrev(std::move(Abbv));
    1439           0 : }
    1440           0 : 
    1441           0 : void ModuleBitcodeWriter::writeGenericDINode(const GenericDINode *N,
    1442             :                                              SmallVectorImpl<uint64_t> &Record,
    1443             :                                              unsigned &Abbrev) {
    1444          59 :   if (!Abbrev)
    1445             :     Abbrev = createGenericDINodeAbbrev();
    1446             : 
    1447          59 :   Record.push_back(N->isDistinct());
    1448          13 :   Record.push_back(N->getTag());
    1449             :   Record.push_back(0); // Per-tag version field; unused for now.
    1450         118 : 
    1451          59 :   for (auto &I : N->operands())
    1452          59 :     Record.push_back(VE.getMetadataOrNullID(I));
    1453          59 : 
    1454          59 :   Stream.EmitRecord(bitc::METADATA_GENERIC_DEBUG, Record, Abbrev);
    1455          59 :   Record.clear();
    1456             : }
    1457          59 : 
    1458             : static uint64_t rotateSign(int64_t I) {
    1459          59 :   uint64_t U = I;
    1460             :   return I < 0 ? ~(U << 1) : U << 1;
    1461           0 : }
    1462             : 
    1463             : void ModuleBitcodeWriter::writeDISubrange(const DISubrange *N,
    1464             :                                           SmallVectorImpl<uint64_t> &Record,
    1465           0 :                                           unsigned Abbrev) {
    1466           0 :   const uint64_t Version = 1 << 1;
    1467           0 :   Record.push_back((uint64_t)N->isDistinct() | Version);
    1468           0 :   Record.push_back(VE.getMetadataOrNullID(N->getRawCountNode()));
    1469           0 :   Record.push_back(rotateSign(N->getLowerBound()));
    1470           0 : 
    1471           0 :   Stream.EmitRecord(bitc::METADATA_SUBRANGE, Record, Abbrev);
    1472           0 :   Record.clear();
    1473             : }
    1474             : 
    1475          20 : void ModuleBitcodeWriter::writeDIEnumerator(const DIEnumerator *N,
    1476             :                                             SmallVectorImpl<uint64_t> &Record,
    1477             :                                             unsigned Abbrev) {
    1478          20 :   Record.push_back((N->isUnsigned() << 1) | N->isDistinct());
    1479           0 :   Record.push_back(rotateSign(N->getValue()));
    1480             :   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
    1481          40 : 
    1482          20 :   Stream.EmitRecord(bitc::METADATA_ENUMERATOR, Record, Abbrev);
    1483          20 :   Record.clear();
    1484             : }
    1485          55 : 
    1486          35 : void ModuleBitcodeWriter::writeDIBasicType(const DIBasicType *N,
    1487             :                                            SmallVectorImpl<uint64_t> &Record,
    1488          20 :                                            unsigned Abbrev) {
    1489             :   Record.push_back(N->isDistinct());
    1490          20 :   Record.push_back(N->getTag());
    1491             :   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
    1492             :   Record.push_back(N->getSizeInBits());
    1493          81 :   Record.push_back(N->getAlignInBits());
    1494          81 :   Record.push_back(N->getEncoding());
    1495             :   Record.push_back(N->getFlags());
    1496             : 
    1497          49 :   Stream.EmitRecord(bitc::METADATA_BASIC_TYPE, Record, Abbrev);
    1498             :   Record.clear();
    1499             : }
    1500             : 
    1501          98 : void ModuleBitcodeWriter::writeDIDerivedType(const DIDerivedType *N,
    1502          49 :                                              SmallVectorImpl<uint64_t> &Record,
    1503          98 :                                              unsigned Abbrev) {
    1504             :   Record.push_back(N->isDistinct());
    1505          49 :   Record.push_back(N->getTag());
    1506             :   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
    1507          49 :   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
    1508             :   Record.push_back(N->getLine());
    1509          32 :   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
    1510             :   Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
    1511             :   Record.push_back(N->getSizeInBits());
    1512          96 :   Record.push_back(N->getAlignInBits());
    1513          64 :   Record.push_back(N->getOffsetInBits());
    1514          32 :   Record.push_back(N->getFlags());
    1515             :   Record.push_back(VE.getMetadataOrNullID(N->getExtraData()));
    1516          32 : 
    1517             :   // DWARF address space is encoded as N->getDWARFAddressSpace() + 1. 0 means
    1518          32 :   // that there is no DWARF address space associated with DIDerivedType.
    1519             :   if (const auto &DWARFAddressSpace = N->getDWARFAddressSpace())
    1520         296 :     Record.push_back(*DWARFAddressSpace + 1);
    1521             :   else
    1522             :     Record.push_back(0);
    1523         592 : 
    1524         296 :   Stream.EmitRecord(bitc::METADATA_DERIVED_TYPE, Record, Abbrev);
    1525         296 :   Record.clear();
    1526         296 : }
    1527         296 : 
    1528         296 : void ModuleBitcodeWriter::writeDICompositeType(
    1529         296 :     const DICompositeType *N, SmallVectorImpl<uint64_t> &Record,
    1530             :     unsigned Abbrev) {
    1531         296 :   const unsigned IsNotUsedInOldTypeRef = 0x2;
    1532             :   Record.push_back(IsNotUsedInOldTypeRef | (unsigned)N->isDistinct());
    1533         296 :   Record.push_back(N->getTag());
    1534             :   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
    1535         269 :   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
    1536             :   Record.push_back(N->getLine());
    1537             :   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
    1538         538 :   Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
    1539         269 :   Record.push_back(N->getSizeInBits());
    1540         269 :   Record.push_back(N->getAlignInBits());
    1541         269 :   Record.push_back(N->getOffsetInBits());
    1542         269 :   Record.push_back(N->getFlags());
    1543         269 :   Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
    1544         269 :   Record.push_back(N->getRuntimeLang());
    1545         269 :   Record.push_back(VE.getMetadataOrNullID(N->getVTableHolder()));
    1546         269 :   Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
    1547         269 :   Record.push_back(VE.getMetadataOrNullID(N->getRawIdentifier()));
    1548         269 :   Record.push_back(VE.getMetadataOrNullID(N->getDiscriminator()));
    1549         269 : 
    1550             :   Stream.EmitRecord(bitc::METADATA_COMPOSITE_TYPE, Record, Abbrev);
    1551             :   Record.clear();
    1552             : }
    1553         269 : 
    1554           6 : void ModuleBitcodeWriter::writeDISubroutineType(
    1555             :     const DISubroutineType *N, SmallVectorImpl<uint64_t> &Record,
    1556         263 :     unsigned Abbrev) {
    1557             :   const unsigned HasNoOldTypeRefs = 0x2;
    1558         269 :   Record.push_back(HasNoOldTypeRefs | (unsigned)N->isDistinct());
    1559             :   Record.push_back(N->getFlags());
    1560         269 :   Record.push_back(VE.getMetadataOrNullID(N->getTypeArray().get()));
    1561             :   Record.push_back(N->getCC());
    1562         350 : 
    1563             :   Stream.EmitRecord(bitc::METADATA_SUBROUTINE_TYPE, Record, Abbrev);
    1564             :   Record.clear();
    1565             : }
    1566         700 : 
    1567         350 : void ModuleBitcodeWriter::writeDIFile(const DIFile *N,
    1568         350 :                                       SmallVectorImpl<uint64_t> &Record,
    1569         350 :                                       unsigned Abbrev) {
    1570         350 :   Record.push_back(N->isDistinct());
    1571         350 :   Record.push_back(VE.getMetadataOrNullID(N->getRawFilename()));
    1572         350 :   Record.push_back(VE.getMetadataOrNullID(N->getRawDirectory()));
    1573         350 :   if (N->getRawChecksum()) {
    1574         350 :     Record.push_back(N->getRawChecksum()->Kind);
    1575         350 :     Record.push_back(VE.getMetadataOrNullID(N->getRawChecksum()->Value));
    1576         350 :   } else {
    1577         350 :     // Maintain backwards compatibility with the old internal representation of
    1578         350 :     // CSK_None in ChecksumKind by writing nulls here when Checksum is None.
    1579         350 :     Record.push_back(0);
    1580         350 :     Record.push_back(VE.getMetadataOrNullID(nullptr));
    1581         350 :   }
    1582         350 :   auto Source = N->getRawSource();
    1583             :   if (Source)
    1584         350 :     Record.push_back(VE.getMetadataOrNullID(*Source));
    1585             : 
    1586         350 :   Stream.EmitRecord(bitc::METADATA_FILE, Record, Abbrev);
    1587             :   Record.clear();
    1588         368 : }
    1589             : 
    1590             : void ModuleBitcodeWriter::writeDICompileUnit(const DICompileUnit *N,
    1591             :                                              SmallVectorImpl<uint64_t> &Record,
    1592         736 :                                              unsigned Abbrev) {
    1593         368 :   assert(N->isDistinct() && "Expected distinct compile units");
    1594         368 :   Record.push_back(/* IsDistinct */ true);
    1595         368 :   Record.push_back(N->getSourceLanguage());
    1596             :   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
    1597         368 :   Record.push_back(VE.getMetadataOrNullID(N->getRawProducer()));
    1598             :   Record.push_back(N->isOptimized());
    1599         368 :   Record.push_back(VE.getMetadataOrNullID(N->getRawFlags()));
    1600             :   Record.push_back(N->getRuntimeVersion());
    1601         697 :   Record.push_back(VE.getMetadataOrNullID(N->getRawSplitDebugFilename()));
    1602             :   Record.push_back(N->getEmissionKind());
    1603             :   Record.push_back(VE.getMetadataOrNullID(N->getEnumTypes().get()));
    1604        1394 :   Record.push_back(VE.getMetadataOrNullID(N->getRetainedTypes().get()));
    1605         697 :   Record.push_back(/* subprograms */ 0);
    1606         697 :   Record.push_back(VE.getMetadataOrNullID(N->getGlobalVariables().get()));
    1607         697 :   Record.push_back(VE.getMetadataOrNullID(N->getImportedEntities().get()));
    1608          22 :   Record.push_back(N->getDWOId());
    1609          22 :   Record.push_back(VE.getMetadataOrNullID(N->getMacros().get()));
    1610             :   Record.push_back(N->getSplitDebugInlining());
    1611             :   Record.push_back(N->getDebugInfoForProfiling());
    1612             :   Record.push_back((unsigned)N->getNameTableKind());
    1613         675 : 
    1614         675 :   Stream.EmitRecord(bitc::METADATA_COMPILE_UNIT, Record, Abbrev);
    1615             :   Record.clear();
    1616             : }
    1617         697 : 
    1618          12 : void ModuleBitcodeWriter::writeDISubprogram(const DISubprogram *N,
    1619             :                                             SmallVectorImpl<uint64_t> &Record,
    1620         697 :                                             unsigned Abbrev) {
    1621             :   uint64_t HasUnitFlag = 1 << 1;
    1622         697 :   Record.push_back(N->isDistinct() | HasUnitFlag);
    1623             :   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
    1624         726 :   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
    1625             :   Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
    1626             :   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
    1627             :   Record.push_back(N->getLine());
    1628         726 :   Record.push_back(VE.getMetadataOrNullID(N->getType()));
    1629         726 :   Record.push_back(N->isLocalToUnit());
    1630         726 :   Record.push_back(N->isDefinition());
    1631         726 :   Record.push_back(N->getScopeLine());
    1632         726 :   Record.push_back(VE.getMetadataOrNullID(N->getContainingType()));
    1633         726 :   Record.push_back(N->getVirtuality());
    1634         726 :   Record.push_back(N->getVirtualIndex());
    1635         726 :   Record.push_back(N->getFlags());
    1636         726 :   Record.push_back(N->isOptimized());
    1637         726 :   Record.push_back(VE.getMetadataOrNullID(N->getRawUnit()));
    1638         726 :   Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
    1639         726 :   Record.push_back(VE.getMetadataOrNullID(N->getDeclaration()));
    1640         726 :   Record.push_back(VE.getMetadataOrNullID(N->getRetainedNodes().get()));
    1641         726 :   Record.push_back(N->getThisAdjustment());
    1642         726 :   Record.push_back(VE.getMetadataOrNullID(N->getThrownTypes().get()));
    1643         726 : 
    1644         726 :   Stream.EmitRecord(bitc::METADATA_SUBPROGRAM, Record, Abbrev);
    1645         726 :   Record.clear();
    1646         726 : }
    1647             : 
    1648         726 : void ModuleBitcodeWriter::writeDILexicalBlock(const DILexicalBlock *N,
    1649             :                                               SmallVectorImpl<uint64_t> &Record,
    1650         726 :                                               unsigned Abbrev) {
    1651             :   Record.push_back(N->isDistinct());
    1652         555 :   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
    1653             :   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
    1654             :   Record.push_back(N->getLine());
    1655             :   Record.push_back(N->getColumn());
    1656        1110 : 
    1657         555 :   Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK, Record, Abbrev);
    1658         555 :   Record.clear();
    1659         555 : }
    1660         555 : 
    1661         555 : void ModuleBitcodeWriter::writeDILexicalBlockFile(
    1662         555 :     const DILexicalBlockFile *N, SmallVectorImpl<uint64_t> &Record,
    1663         555 :     unsigned Abbrev) {
    1664         555 :   Record.push_back(N->isDistinct());
    1665         555 :   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
    1666         555 :   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
    1667         555 :   Record.push_back(N->getDiscriminator());
    1668         555 : 
    1669         555 :   Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK_FILE, Record, Abbrev);
    1670         555 :   Record.clear();
    1671         555 : }
    1672         555 : 
    1673         555 : void ModuleBitcodeWriter::writeDINamespace(const DINamespace *N,
    1674         555 :                                            SmallVectorImpl<uint64_t> &Record,
    1675         555 :                                            unsigned Abbrev) {
    1676         555 :   Record.push_back(N->isDistinct() | N->getExportSymbols() << 1);
    1677             :   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
    1678         555 :   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
    1679             : 
    1680         555 :   Stream.EmitRecord(bitc::METADATA_NAMESPACE, Record, Abbrev);
    1681             :   Record.clear();
    1682         139 : }
    1683             : 
    1684             : void ModuleBitcodeWriter::writeDIMacro(const DIMacro *N,
    1685         278 :                                        SmallVectorImpl<uint64_t> &Record,
    1686         139 :                                        unsigned Abbrev) {
    1687         139 :   Record.push_back(N->isDistinct());
    1688         139 :   Record.push_back(N->getMacinfoType());
    1689         139 :   Record.push_back(N->getLine());
    1690             :   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
    1691         139 :   Record.push_back(VE.getMetadataOrNullID(N->getRawValue()));
    1692             : 
    1693         139 :   Stream.EmitRecord(bitc::METADATA_MACRO, Record, Abbrev);
    1694             :   Record.clear();
    1695          52 : }
    1696             : 
    1697             : void ModuleBitcodeWriter::writeDIMacroFile(const DIMacroFile *N,
    1698         104 :                                            SmallVectorImpl<uint64_t> &Record,
    1699          52 :                                            unsigned Abbrev) {
    1700          52 :   Record.push_back(N->isDistinct());
    1701          52 :   Record.push_back(N->getMacinfoType());
    1702             :   Record.push_back(N->getLine());
    1703          52 :   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
    1704             :   Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
    1705          52 : 
    1706             :   Stream.EmitRecord(bitc::METADATA_MACRO_FILE, Record, Abbrev);
    1707          30 :   Record.clear();
    1708             : }
    1709             : 
    1710          90 : void ModuleBitcodeWriter::writeDIModule(const DIModule *N,
    1711          30 :                                         SmallVectorImpl<uint64_t> &Record,
    1712          30 :                                         unsigned Abbrev) {
    1713             :   Record.push_back(N->isDistinct());
    1714          30 :   for (auto &I : N->operands())
    1715             :     Record.push_back(VE.getMetadataOrNullID(I));
    1716          30 : 
    1717             :   Stream.EmitRecord(bitc::METADATA_MODULE, Record, Abbrev);
    1718          13 :   Record.clear();
    1719             : }
    1720             : 
    1721          26 : void ModuleBitcodeWriter::writeDITemplateTypeParameter(
    1722          13 :     const DITemplateTypeParameter *N, SmallVectorImpl<uint64_t> &Record,
    1723          13 :     unsigned Abbrev) {
    1724          13 :   Record.push_back(N->isDistinct());
    1725          13 :   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
    1726             :   Record.push_back(VE.getMetadataOrNullID(N->getType()));
    1727          13 : 
    1728             :   Stream.EmitRecord(bitc::METADATA_TEMPLATE_TYPE, Record, Abbrev);
    1729          13 :   Record.clear();
    1730             : }
    1731          19 : 
    1732             : void ModuleBitcodeWriter::writeDITemplateValueParameter(
    1733             :     const DITemplateValueParameter *N, SmallVectorImpl<uint64_t> &Record,
    1734          38 :     unsigned Abbrev) {
    1735          19 :   Record.push_back(N->isDistinct());
    1736          19 :   Record.push_back(N->getTag());
    1737          19 :   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
    1738          19 :   Record.push_back(VE.getMetadataOrNullID(N->getType()));
    1739             :   Record.push_back(VE.getMetadataOrNullID(N->getValue()));
    1740          19 : 
    1741             :   Stream.EmitRecord(bitc::METADATA_TEMPLATE_VALUE, Record, Abbrev);
    1742          19 :   Record.clear();
    1743             : }
    1744          12 : 
    1745             : void ModuleBitcodeWriter::writeDIGlobalVariable(
    1746             :     const DIGlobalVariable *N, SmallVectorImpl<uint64_t> &Record,
    1747          24 :     unsigned Abbrev) {
    1748          72 :   const uint64_t Version = 2 << 1;
    1749          60 :   Record.push_back((uint64_t)N->isDistinct() | Version);
    1750             :   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
    1751          12 :   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
    1752             :   Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
    1753          12 :   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
    1754             :   Record.push_back(N->getLine());
    1755          28 :   Record.push_back(VE.getMetadataOrNullID(N->getType()));
    1756             :   Record.push_back(N->isLocalToUnit());
    1757             :   Record.push_back(N->isDefinition());
    1758          56 :   Record.push_back(VE.getMetadataOrNullID(N->getStaticDataMemberDeclaration()));
    1759          28 :   Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams()));
    1760          28 :   Record.push_back(N->getAlignInBits());
    1761             : 
    1762          28 :   Stream.EmitRecord(bitc::METADATA_GLOBAL_VAR, Record, Abbrev);
    1763             :   Record.clear();
    1764          28 : }
    1765             : 
    1766          46 : void ModuleBitcodeWriter::writeDILocalVariable(
    1767             :     const DILocalVariable *N, SmallVectorImpl<uint64_t> &Record,
    1768             :     unsigned Abbrev) {
    1769          92 :   // In order to support all possible bitcode formats in BitcodeReader we need
    1770          46 :   // to distinguish the following cases:
    1771          46 :   // 1) Record has no artificial tag (Record[1]),
    1772          46 :   //   has no obsolete inlinedAt field (Record[9]).
    1773          46 :   //   In this case Record size will be 8, HasAlignment flag is false.
    1774             :   // 2) Record has artificial tag (Record[1]),
    1775          46 :   //   has no obsolete inlignedAt field (Record[9]).
    1776             :   //   In this case Record size will be 9, HasAlignment flag is false.
    1777          46 :   // 3) Record has both artificial tag (Record[1]) and
    1778             :   //   obsolete inlignedAt field (Record[9]).
    1779          66 :   //   In this case Record size will be 10, HasAlignment flag is false.
    1780             :   // 4) Record has neither artificial tag, nor inlignedAt field, but
    1781             :   //   HasAlignment flag is true and Record[8] contains alignment value.
    1782             :   const uint64_t HasAlignmentFlag = 1 << 1;
    1783         132 :   Record.push_back((uint64_t)N->isDistinct() | HasAlignmentFlag);
    1784          66 :   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
    1785          66 :   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
    1786          66 :   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
    1787          66 :   Record.push_back(N->getLine());
    1788          66 :   Record.push_back(VE.getMetadataOrNullID(N->getType()));
    1789          66 :   Record.push_back(N->getArg());
    1790          66 :   Record.push_back(N->getFlags());
    1791          66 :   Record.push_back(N->getAlignInBits());
    1792          66 : 
    1793          66 :   Stream.EmitRecord(bitc::METADATA_LOCAL_VAR, Record, Abbrev);
    1794          66 :   Record.clear();
    1795             : }
    1796          66 : 
    1797             : void ModuleBitcodeWriter::writeDILabel(
    1798          66 :     const DILabel *N, SmallVectorImpl<uint64_t> &Record,
    1799             :     unsigned Abbrev) {
    1800         254 :   Record.push_back((uint64_t)N->isDistinct());
    1801             :   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
    1802             :   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
    1803             :   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
    1804             :   Record.push_back(N->getLine());
    1805             : 
    1806             :   Stream.EmitRecord(bitc::METADATA_LABEL, Record, Abbrev);
    1807             :   Record.clear();
    1808             : }
    1809             : 
    1810             : void ModuleBitcodeWriter::writeDIExpression(const DIExpression *N,
    1811             :                                             SmallVectorImpl<uint64_t> &Record,
    1812             :                                             unsigned Abbrev) {
    1813             :   Record.reserve(N->getElements().size() + 1);
    1814             :   const uint64_t Version = 3 << 1;
    1815             :   Record.push_back((uint64_t)N->isDistinct() | Version);
    1816             :   Record.append(N->elements_begin(), N->elements_end());
    1817         508 : 
    1818         254 :   Stream.EmitRecord(bitc::METADATA_EXPRESSION, Record, Abbrev);
    1819         254 :   Record.clear();
    1820         254 : }
    1821         254 : 
    1822         254 : void ModuleBitcodeWriter::writeDIGlobalVariableExpression(
    1823         254 :     const DIGlobalVariableExpression *N, SmallVectorImpl<uint64_t> &Record,
    1824         254 :     unsigned Abbrev) {
    1825         254 :   Record.push_back(N->isDistinct());
    1826             :   Record.push_back(VE.getMetadataOrNullID(N->getVariable()));
    1827         254 :   Record.push_back(VE.getMetadataOrNullID(N->getExpression()));
    1828             : 
    1829         254 :   Stream.EmitRecord(bitc::METADATA_GLOBAL_VAR_EXPR, Record, Abbrev);
    1830             :   Record.clear();
    1831          12 : }
    1832             : 
    1833             : void ModuleBitcodeWriter::writeDIObjCProperty(const DIObjCProperty *N,
    1834          24 :                                               SmallVectorImpl<uint64_t> &Record,
    1835          12 :                                               unsigned Abbrev) {
    1836          12 :   Record.push_back(N->isDistinct());
    1837          12 :   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
    1838          12 :   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
    1839             :   Record.push_back(N->getLine());
    1840          12 :   Record.push_back(VE.getMetadataOrNullID(N->getRawSetterName()));
    1841             :   Record.push_back(VE.getMetadataOrNullID(N->getRawGetterName()));
    1842          12 :   Record.push_back(N->getAttributes());
    1843             :   Record.push_back(VE.getMetadataOrNullID(N->getType()));
    1844           0 : 
    1845             :   Stream.EmitRecord(bitc::METADATA_OBJC_PROPERTY, Record, Abbrev);
    1846             :   Record.clear();
    1847           0 : }
    1848             : 
    1849           0 : void ModuleBitcodeWriter::writeDIImportedEntity(
    1850           0 :     const DIImportedEntity *N, SmallVectorImpl<uint64_t> &Record,
    1851             :     unsigned Abbrev) {
    1852           0 :   Record.push_back(N->isDistinct());
    1853             :   Record.push_back(N->getTag());
    1854           0 :   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
    1855             :   Record.push_back(VE.getMetadataOrNullID(N->getEntity()));
    1856          45 :   Record.push_back(N->getLine());
    1857             :   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
    1858             :   Record.push_back(VE.getMetadataOrNullID(N->getRawFile()));
    1859          90 : 
    1860          45 :   Stream.EmitRecord(bitc::METADATA_IMPORTED_ENTITY, Record, Abbrev);
    1861          45 :   Record.clear();
    1862             : }
    1863          45 : 
    1864             : unsigned ModuleBitcodeWriter::createNamedMetadataAbbrev() {
    1865          45 :   auto Abbv = std::make_shared<BitCodeAbbrev>();
    1866             :   Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_NAME));
    1867          16 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
    1868             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
    1869             :   return Stream.EmitAbbrev(std::move(Abbv));
    1870          32 : }
    1871          16 : 
    1872          16 : void ModuleBitcodeWriter::writeNamedMetadata(
    1873          16 :     SmallVectorImpl<uint64_t> &Record) {
    1874          16 :   if (M.named_metadata_empty())
    1875          16 :     return;
    1876          16 : 
    1877          16 :   unsigned Abbrev = createNamedMetadataAbbrev();
    1878             :   for (const NamedMDNode &NMD : M.named_metadata()) {
    1879          16 :     // Write name.
    1880             :     StringRef Str = NMD.getName();
    1881          16 :     Record.append(Str.bytes_begin(), Str.bytes_end());
    1882             :     Stream.EmitRecord(bitc::METADATA_NAME, Record, Abbrev);
    1883          26 :     Record.clear();
    1884             : 
    1885             :     // Write named metadata operands.
    1886          52 :     for (const MDNode *N : NMD.operands())
    1887          26 :       Record.push_back(VE.getMetadataID(N));
    1888          26 :     Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0);
    1889          26 :     Record.clear();
    1890          26 :   }
    1891          26 : }
    1892          26 : 
    1893             : unsigned ModuleBitcodeWriter::createMetadataStringsAbbrev() {
    1894          26 :   auto Abbv = std::make_shared<BitCodeAbbrev>();
    1895             :   Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRINGS));
    1896          26 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of strings
    1897             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // offset to chars
    1898           0 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
    1899             :   return Stream.EmitAbbrev(std::move(Abbv));
    1900           0 : }
    1901           0 : 
    1902           0 : /// Write out a record for MDString.
    1903           0 : ///
    1904             : /// All the metadata strings in a metadata block are emitted in a single
    1905             : /// record.  The sizes and strings themselves are shoved into a blob.
    1906        1249 : void ModuleBitcodeWriter::writeMetadataStrings(
    1907             :     ArrayRef<const Metadata *> Strings, SmallVectorImpl<uint64_t> &Record) {
    1908        2498 :   if (Strings.empty())
    1909             :     return;
    1910             : 
    1911        1205 :   // Start the record with the number of strings.
    1912        3709 :   Record.push_back(bitc::METADATA_STRINGS);
    1913             :   Record.push_back(Strings.size());
    1914        2504 : 
    1915        2504 :   // Emit the sizes of the strings in the blob.
    1916        2504 :   SmallString<256> Blob;
    1917             :   {
    1918             :     BitstreamWriter W(Blob);
    1919             :     for (const Metadata *MD : Strings)
    1920        8128 :       W.EmitVBR(cast<MDString>(MD)->getLength(), 6);
    1921        5624 :     W.FlushToWord();
    1922        2504 :   }
    1923             : 
    1924             :   // Add the offset to the strings to the record.
    1925             :   Record.push_back(Blob.size());
    1926             : 
    1927           0 :   // Add the strings to the blob.
    1928             :   for (const Metadata *MD : Strings)
    1929           0 :     Blob.append(cast<MDString>(MD)->getString());
    1930           0 : 
    1931           0 :   // Emit the final record.
    1932           0 :   Stream.EmitRecordWithBlob(createMetadataStringsAbbrev(), Record, Blob);
    1933           0 :   Record.clear();
    1934             : }
    1935             : 
    1936             : // Generates an enum to use as an index in the Abbrev array of Metadata record.
    1937             : enum MetadataAbbrev : unsigned {
    1938             : #define HANDLE_MDNODE_LEAF(CLASS) CLASS##AbbrevID,
    1939             : #include "llvm/IR/Metadata.def"
    1940        2667 :   LastPlusOne
    1941             : };
    1942        2667 : 
    1943         872 : void ModuleBitcodeWriter::writeMetadataRecords(
    1944             :     ArrayRef<const Metadata *> MDs, SmallVectorImpl<uint64_t> &Record,
    1945             :     std::vector<unsigned> *MDAbbrevs, std::vector<uint64_t> *IndexPos) {
    1946        1795 :   if (MDs.empty())
    1947        1795 :     return;
    1948             : 
    1949             :   // Initialize MDNode abbreviations.
    1950             : #define HANDLE_MDNODE_LEAF(CLASS) unsigned CLASS##Abbrev = 0;
    1951             : #include "llvm/IR/Metadata.def"
    1952        1795 : 
    1953        9905 :   for (const Metadata *MD : MDs) {
    1954        8110 :     if (IndexPos)
    1955        1795 :       IndexPos->push_back(Stream.GetCurrentBitNo());
    1956             :     if (const MDNode *N = dyn_cast<MDNode>(MD)) {
    1957             :       assert(N->isResolved() && "Expected forward references to be resolved");
    1958             : 
    1959        3590 :       switch (N->getMetadataID()) {
    1960             :       default:
    1961             :         llvm_unreachable("Invalid MDNode subclass");
    1962        9905 : #define HANDLE_MDNODE_LEAF(CLASS)                                              \
    1963        8110 :   case Metadata::CLASS##Kind:                                                  \
    1964             :     if (MDAbbrevs)                                                             \
    1965             :       write##CLASS(cast<CLASS>(N), Record,                                     \
    1966        5385 :                    (*MDAbbrevs)[MetadataAbbrev::CLASS##AbbrevID]);             \
    1967             :     else                                                                       \
    1968             :       write##CLASS(cast<CLASS>(N), Record, CLASS##Abbrev);                     \
    1969             :     continue;
    1970             : #include "llvm/IR/Metadata.def"
    1971             :       }
    1972             :     }
    1973             :     writeValueAsMetadata(cast<ValueAsMetadata>(MD), Record);
    1974             :   }
    1975             : }
    1976             : 
    1977        2667 : void ModuleBitcodeWriter::writeModuleMetadata() {
    1978             :   if (!VE.hasMDs() && M.named_metadata_empty())
    1979             :     return;
    1980        2667 : 
    1981          53 :   Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 4);
    1982             :   SmallVector<uint64_t, 64> Record;
    1983             : 
    1984             :   // Emit all abbrevs upfront, so that the reader can jump in the middle of the
    1985             :   // block and load any metadata.
    1986             :   std::vector<unsigned> MDAbbrevs;
    1987       22840 : 
    1988       20226 :   MDAbbrevs.resize(MetadataAbbrev::LastPlusOne);
    1989       30606 :   MDAbbrevs[MetadataAbbrev::DILocationAbbrevID] = createDILocationAbbrev();
    1990             :   MDAbbrevs[MetadataAbbrev::GenericDINodeAbbrevID] =
    1991             :       createGenericDINodeAbbrev();
    1992             : 
    1993       12968 :   auto Abbv = std::make_shared<BitCodeAbbrev>();
    1994           0 :   Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_INDEX_OFFSET));
    1995           0 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
    1996             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
    1997             :   unsigned OffsetAbbrev = Stream.EmitAbbrev(std::move(Abbv));
    1998             : 
    1999             :   Abbv = std::make_shared<BitCodeAbbrev>();
    2000             :   Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_INDEX));
    2001             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
    2002             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
    2003             :   unsigned IndexAbbrev = Stream.EmitAbbrev(std::move(Abbv));
    2004             : 
    2005             :   // Emit MDStrings together upfront.
    2006             :   writeMetadataStrings(VE.getMDStrings(), Record);
    2007        7258 : 
    2008             :   // We only emit an index for the metadata record if we have more than a given
    2009             :   // (naive) threshold of metadatas, otherwise it is not worth it.
    2010             :   if (VE.getNonMDStrings().size() > IndexThreshold) {
    2011        4708 :     // Write a placeholder value in for the offset of the metadata index,
    2012        4708 :     // which is written after the records, so that it can include
    2013        3459 :     // the offset of each entry. The placeholder offset will be
    2014             :     // updated after all records are emitted.
    2015        1249 :     uint64_t Vals[] = {0, 0};
    2016             :     Stream.EmitRecord(bitc::METADATA_INDEX_OFFSET, Vals, OffsetAbbrev);
    2017             :   }
    2018             : 
    2019             :   // Compute and save the bit offset to the current position, which will be
    2020             :   // patched when we emit the index later. We can simply subtract the 64-bit
    2021             :   // fixed size from the current bit number to get the location to backpatch.
    2022        1249 :   uint64_t IndexOffsetRecordBitPos = Stream.GetCurrentBitNo();
    2023        1249 : 
    2024        1249 :   // This index will contain the bitpos for each individual record.
    2025        1249 :   std::vector<uint64_t> IndexPos;
    2026             :   IndexPos.reserve(VE.getNonMDStrings().size());
    2027             : 
    2028        1249 :   // Write all the records
    2029        1249 :   writeMetadataRecords(VE.getNonMDStrings(), Record, &MDAbbrevs, &IndexPos);
    2030        1249 : 
    2031        3747 :   if (VE.getNonMDStrings().size() > IndexThreshold) {
    2032             :     // Now that we have emitted all the records we will emit the index. But
    2033        1249 :     // first
    2034        1249 :     // backpatch the forward reference so that the reader can skip the records
    2035        1249 :     // efficiently.
    2036        1249 :     Stream.BackpatchWord64(IndexOffsetRecordBitPos - 64,
    2037        3747 :                            Stream.GetCurrentBitNo() - IndexOffsetRecordBitPos);
    2038             : 
    2039             :     // Delta encode the index.
    2040        1249 :     uint64_t PreviousValue = IndexOffsetRecordBitPos;
    2041             :     for (auto &Elt : IndexPos) {
    2042             :       auto EltDelta = Elt - PreviousValue;
    2043             :       PreviousValue = Elt;
    2044        1249 :       Elt = EltDelta;
    2045             :     }
    2046             :     // Emit the index record.
    2047             :     Stream.EmitRecord(bitc::METADATA_INDEX, IndexPos, IndexAbbrev);
    2048             :     IndexPos.clear();
    2049         134 :   }
    2050         134 : 
    2051             :   // Write the named metadata now.
    2052             :   writeNamedMetadata(Record);
    2053             : 
    2054             :   auto AddDeclAttachedMetadata = [&](const GlobalObject &GO) {
    2055             :     SmallVector<uint64_t, 4> Record;
    2056        1249 :     Record.push_back(VE.getValueID(&GO));
    2057             :     pushGlobalMetadataAttachment(Record, GO);
    2058             :     Stream.EmitRecord(bitc::METADATA_GLOBAL_DECL_ATTACHMENT, Record);
    2059             :   };
    2060        1249 :   for (const Function &F : M)
    2061             :     if (F.isDeclaration() && F.hasMetadata())
    2062             :       AddDeclAttachedMetadata(F);
    2063        1249 :   // FIXME: Only store metadata for declarations here, and move data for global
    2064             :   // variable definitions to a separate block (PR28134).
    2065        1249 :   for (const GlobalVariable &GV : M.globals())
    2066             :     if (GV.hasMetadata())
    2067             :       AddDeclAttachedMetadata(GV);
    2068             : 
    2069             :   Stream.ExitBlock();
    2070         268 : }
    2071             : 
    2072             : void ModuleBitcodeWriter::writeFunctionMetadata(const Function &F) {
    2073             :   if (!VE.hasMDs())
    2074             :     return;
    2075        6989 : 
    2076        6855 :   Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
    2077             :   SmallVector<uint64_t, 64> Record;
    2078        6855 :   writeMetadataStrings(VE.getMDStrings(), Record);
    2079             :   writeMetadataRecords(VE.getNonMDStrings(), Record);
    2080             :   Stream.ExitBlock();
    2081         134 : }
    2082             : 
    2083             : void ModuleBitcodeWriter::pushGlobalMetadataAttachment(
    2084             :     SmallVectorImpl<uint64_t> &Record, const GlobalObject &GO) {
    2085             :   // [n x [id, mdnode]]
    2086        1249 :   SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
    2087             :   GO.getAllMetadata(MDs);
    2088             :   for (const auto &I : MDs) {
    2089             :     Record.push_back(I.first);
    2090             :     Record.push_back(VE.getMetadataID(I.second));
    2091             :   }
    2092             : }
    2093        1249 : 
    2094       10504 : void ModuleBitcodeWriter::writeFunctionMetadataAttachment(const Function &F) {
    2095        9255 :   Stream.EnterSubblock(bitc::METADATA_ATTACHMENT_ID, 3);
    2096          13 : 
    2097             :   SmallVector<uint64_t, 64> Record;
    2098             : 
    2099        8716 :   if (F.hasMetadata()) {
    2100        7467 :     pushGlobalMetadataAttachment(Record, F);
    2101         124 :     Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
    2102             :     Record.clear();
    2103        1249 :   }
    2104             : 
    2105             :   // Write metadata attachments
    2106           0 :   // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]]
    2107           0 :   SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
    2108           0 :   for (const BasicBlock &BB : F)
    2109             :     for (const Instruction &I : BB) {
    2110           0 :       MDs.clear();
    2111             :       I.getAllMetadataOtherThanDebugLoc(MDs);
    2112           0 : 
    2113           0 :       // If no metadata, ignore instruction.
    2114           0 :       if (MDs.empty()) continue;
    2115             : 
    2116             :       Record.push_back(VE.getInstructionID(&I));
    2117         541 : 
    2118             :       for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
    2119             :         Record.push_back(MDs[i].first);
    2120             :         Record.push_back(VE.getMetadataID(MDs[i].second));
    2121         541 :       }
    2122        1173 :       Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
    2123         632 :       Record.clear();
    2124         632 :     }
    2125             : 
    2126         541 :   Stream.ExitBlock();
    2127             : }
    2128        1404 : 
    2129        1404 : void ModuleBitcodeWriter::writeModuleMetadataKinds() {
    2130             :   SmallVector<uint64_t, 64> Record;
    2131             : 
    2132             :   // Write metadata kinds
    2133        1404 :   // METADATA_KIND - [n x [id, name]]
    2134         404 :   SmallVector<StringRef, 8> Names;
    2135         404 :   M.getMDKindNames(Names);
    2136             : 
    2137             :   if (Names.empty()) return;
    2138             : 
    2139             :   Stream.EnterSubblock(bitc::METADATA_KIND_BLOCK_ID, 3);
    2140             : 
    2141             :   for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) {
    2142       10900 :     Record.push_back(MDKindID);
    2143       61765 :     StringRef KName = Names[MDKindID];
    2144             :     Record.append(KName.begin(), KName.end());
    2145             : 
    2146             :     Stream.EmitRecord(bitc::METADATA_KIND, Record, 0);
    2147             :     Record.clear();
    2148       52269 :   }
    2149             : 
    2150        6110 :   Stream.ExitBlock();
    2151             : }
    2152       12320 : 
    2153       12420 : void ModuleBitcodeWriter::writeOperandBundleTags() {
    2154        6210 :   // Write metadata kinds
    2155             :   //
    2156        6110 :   // OPERAND_BUNDLE_TAGS_BLOCK_ID : N x OPERAND_BUNDLE_TAG
    2157             :   //
    2158             :   // OPERAND_BUNDLE_TAG - [strchr x N]
    2159             : 
    2160        1404 :   SmallVector<StringRef, 8> Tags;
    2161        1404 :   M.getOperandBundleTags(Tags);
    2162             : 
    2163           0 :   if (Tags.empty())
    2164             :     return;
    2165             : 
    2166             :   Stream.EnterSubblock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID, 3);
    2167             : 
    2168             :   SmallVector<uint64_t, 64> Record;
    2169           0 : 
    2170             :   for (auto Tag : Tags) {
    2171           0 :     Record.append(Tag.begin(), Tag.end());
    2172             : 
    2173           0 :     Stream.EmitRecord(bitc::OPERAND_BUNDLE_TAG, Record, 0);
    2174             :     Record.clear();
    2175           0 :   }
    2176           0 : 
    2177           0 :   Stream.ExitBlock();
    2178           0 : }
    2179             : 
    2180           0 : void ModuleBitcodeWriter::writeSyncScopeNames() {
    2181             :   SmallVector<StringRef, 8> SSNs;
    2182             :   M.getContext().getSyncScopeNames(SSNs);
    2183             :   if (SSNs.empty())
    2184           0 :     return;
    2185             : 
    2186             :   Stream.EnterSubblock(bitc::SYNC_SCOPE_NAMES_BLOCK_ID, 2);
    2187           0 : 
    2188             :   SmallVector<uint64_t, 64> Record;
    2189             :   for (auto SSN : SSNs) {
    2190             :     Record.append(SSN.begin(), SSN.end());
    2191             :     Stream.EmitRecord(bitc::SYNC_SCOPE_NAME, Record, 0);
    2192             :     Record.clear();
    2193             :   }
    2194             : 
    2195           0 :   Stream.ExitBlock();
    2196             : }
    2197           0 : 
    2198           0 : static void emitSignedInt64(SmallVectorImpl<uint64_t> &Vals, uint64_t V) {
    2199             :   if ((int64_t)V >= 0)
    2200           0 :     Vals.push_back(V << 1);
    2201             :   else
    2202             :     Vals.push_back((-V << 1) | 1);
    2203             : }
    2204           0 : 
    2205           0 : void ModuleBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal,
    2206             :                                          bool isGlobal) {
    2207           0 :   if (FirstVal == LastVal) return;
    2208             : 
    2209             :   Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
    2210             : 
    2211           0 :   unsigned AggregateAbbrev = 0;
    2212             :   unsigned String8Abbrev = 0;
    2213             :   unsigned CString7Abbrev = 0;
    2214           0 :   unsigned CString6Abbrev = 0;
    2215             :   // If this is a constant pool for the module, emit module-specific abbrevs.
    2216           0 :   if (isGlobal) {
    2217           0 :     // Abbrev for CST_CODE_AGGREGATE.
    2218           0 :     auto Abbv = std::make_shared<BitCodeAbbrev>();
    2219             :     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE));
    2220           0 :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
    2221             :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
    2222             :     AggregateAbbrev = Stream.EmitAbbrev(std::move(Abbv));
    2223           0 : 
    2224           0 :     // Abbrev for CST_CODE_STRING.
    2225           0 :     Abbv = std::make_shared<BitCodeAbbrev>();
    2226             :     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
    2227             :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
    2228             :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
    2229           0 :     String8Abbrev = Stream.EmitAbbrev(std::move(Abbv));
    2230             :     // Abbrev for CST_CODE_CSTRING.
    2231             :     Abbv = std::make_shared<BitCodeAbbrev>();
    2232       21053 :     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
    2233       21053 :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
    2234       19159 :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
    2235             :     CString7Abbrev = Stream.EmitAbbrev(std::move(Abbv));
    2236        1894 :     // Abbrev for CST_CODE_CSTRING.
    2237       21053 :     Abbv = std::make_shared<BitCodeAbbrev>();
    2238             :     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
    2239       14166 :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
    2240             :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
    2241       14166 :     CString6Abbrev = Stream.EmitAbbrev(std::move(Abbv));
    2242             :   }
    2243        8251 : 
    2244             :   SmallVector<uint64_t, 64> Record;
    2245             : 
    2246             :   const ValueEnumerator::ValueList &Vals = VE.getValues();
    2247             :   Type *LastTy = nullptr;
    2248             :   for (unsigned i = FirstVal; i != LastVal; ++i) {
    2249             :     const Value *V = Vals[i].first;
    2250        8251 :     // If we need to switch types, do so now.
    2251             :     if (V->getType() != LastTy) {
    2252             :       LastTy = V->getType();
    2253        1988 :       Record.push_back(VE.getTypeID(LastTy));
    2254        1988 :       Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record,
    2255        1988 :                         CONSTANTS_SETTYPE_ABBREV);
    2256        5964 :       Record.clear();
    2257             :     }
    2258             : 
    2259        1988 :     if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
    2260        1988 :       Record.push_back(unsigned(IA->hasSideEffects()) |
    2261        1988 :                        unsigned(IA->isAlignStack()) << 1 |
    2262        1988 :                        unsigned(IA->getDialect()&1) << 2);
    2263        5964 : 
    2264             :       // Add the asm string.
    2265        1988 :       const std::string &AsmStr = IA->getAsmString();
    2266        1988 :       Record.push_back(AsmStr.size());
    2267        1988 :       Record.append(AsmStr.begin(), AsmStr.end());
    2268        1988 : 
    2269        5964 :       // Add the constraint string.
    2270             :       const std::string &ConstraintStr = IA->getConstraintString();
    2271        1988 :       Record.push_back(ConstraintStr.size());
    2272        1988 :       Record.append(ConstraintStr.begin(), ConstraintStr.end());
    2273        1988 :       Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record);
    2274        1988 :       Record.clear();
    2275        5964 :       continue;
    2276             :     }
    2277             :     const Constant *C = cast<Constant>(V);
    2278             :     unsigned Code = -1U;
    2279             :     unsigned AbbrevToUse = 0;
    2280             :     if (C->isNullValue()) {
    2281             :       Code = bitc::CST_CODE_NULL;
    2282       43556 :     } else if (isa<UndefValue>(C)) {
    2283       35305 :       Code = bitc::CST_CODE_UNDEF;
    2284             :     } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
    2285       35305 :       if (IV->getBitWidth() <= 64) {
    2286             :         uint64_t V = IV->getSExtValue();
    2287       18024 :         emitSignedInt64(Record, V);
    2288       18024 :         Code = bitc::CST_CODE_INTEGER;
    2289             :         AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
    2290             :       } else {                             // Wide integers, > 64 bits in size.
    2291             :         // We have an arbitrary precision integer value to write whose
    2292             :         // bit width is > 64. However, in canonical unsigned integer
    2293             :         // format it is likely that the high bits are going to be zero.
    2294         192 :         // So, we only write the number of active words.
    2295         192 :         unsigned NWords = IV->getValue().getActiveWords();
    2296          64 :         const uint64_t *RawWords = IV->getValue().getRawData();
    2297             :         for (unsigned i = 0; i != NWords; ++i) {
    2298             :           emitSignedInt64(Record, RawWords[i]);
    2299             :         }
    2300          64 :         Code = bitc::CST_CODE_WIDE_INTEGER;
    2301          64 :       }
    2302             :     } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
    2303             :       Code = bitc::CST_CODE_FLOAT;
    2304             :       Type *Ty = CFP->getType();
    2305          64 :       if (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy()) {
    2306          64 :         Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
    2307          64 :       } else if (Ty->isX86_FP80Ty()) {
    2308             :         // api needed to prevent premature destruction
    2309             :         // bits are not in the same order as a normal i80 APInt, compensate.
    2310             :         APInt api = CFP->getValueAPF().bitcastToAPInt();
    2311             :         const uint64_t *p = api.getRawData();
    2312             :         Record.push_back((p[1] << 48) | (p[0] >> 16));
    2313             :         Record.push_back(p[0] & 0xffffLL);
    2314       35241 :       } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) {
    2315             :         APInt api = CFP->getValueAPF().bitcastToAPInt();
    2316       30444 :         const uint64_t *p = api.getRawData();
    2317             :         Record.push_back(p[0]);
    2318             :         Record.push_back(p[1]);
    2319       17438 :       } else {
    2320       17416 :         assert(0 && "Unknown FP type!");
    2321       17416 :       }
    2322             :     } else if (isa<ConstantDataSequential>(C) &&
    2323             :                cast<ConstantDataSequential>(C)->isString()) {
    2324             :       const ConstantDataSequential *Str = cast<ConstantDataSequential>(C);
    2325             :       // Emit constant strings specially.
    2326             :       unsigned NumElts = Str->getNumElements();
    2327             :       // If this is a null-terminated string, use the denser CSTRING encoding.
    2328             :       if (Str->isCString()) {
    2329             :         Code = bitc::CST_CODE_CSTRING;
    2330             :         --NumElts;  // Don't encode the null, which isn't allowed by char6.
    2331          58 :       } else {
    2332          36 :         Code = bitc::CST_CODE_STRING;
    2333             :         AbbrevToUse = String8Abbrev;
    2334             :       }
    2335             :       bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
    2336             :       bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
    2337             :       for (unsigned i = 0; i != NumElts; ++i) {
    2338         537 :         unsigned char V = Str->getElementAsInteger(i);
    2339         537 :         Record.push_back(V);
    2340        1046 :         isCStr7 &= (V & 128) == 0;
    2341          14 :         if (isCStrChar6)
    2342             :           isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
    2343             :       }
    2344          13 : 
    2345             :       if (isCStrChar6)
    2346          13 :         AbbrevToUse = CString6Abbrev;
    2347          13 :       else if (isCStr7)
    2348           1 :         AbbrevToUse = CString7Abbrev;
    2349           1 :     } else if (const ConstantDataSequential *CDS =
    2350             :                   dyn_cast<ConstantDataSequential>(C)) {
    2351           1 :       Code = bitc::CST_CODE_DATA;
    2352           1 :       Type *EltTy = CDS->getType()->getElementType();
    2353             :       if (isa<IntegerType>(EltTy)) {
    2354             :         for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
    2355             :           Record.push_back(CDS->getElementAsInteger(i));
    2356        2235 :       } else {
    2357        2235 :         for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
    2358             :           Record.push_back(
    2359             :               CDS->getElementAsAPFloat(i).bitcastToAPInt().getLimitedValue());
    2360        1313 :       }
    2361             :     } else if (isa<ConstantAggregate>(C)) {
    2362        1313 :       Code = bitc::CST_CODE_AGGREGATE;
    2363             :       for (const Value *Op : C->operands())
    2364        1283 :         Record.push_back(VE.getValueID(Op));
    2365             :       AbbrevToUse = AggregateAbbrev;
    2366             :     } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
    2367             :       switch (CE->getOpcode()) {
    2368             :       default:
    2369        1313 :         if (Instruction::isCast(CE->getOpcode())) {
    2370             :           Code = bitc::CST_CODE_CE_CAST;
    2371       51962 :           Record.push_back(getEncodedCastOpcode(CE->getOpcode()));
    2372       50649 :           Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
    2373       50649 :           Record.push_back(VE.getValueID(C->getOperand(0)));
    2374       50649 :           AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
    2375       50649 :         } else {
    2376             :           assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
    2377             :           Code = bitc::CST_CODE_CE_BINOP;
    2378             :           Record.push_back(getEncodedBinaryOpcode(CE->getOpcode()));
    2379        1313 :           Record.push_back(VE.getValueID(C->getOperand(0)));
    2380             :           Record.push_back(VE.getValueID(C->getOperand(1)));
    2381         254 :           uint64_t Flags = getOptimizationFlags(CE);
    2382             :           if (Flags != 0)
    2383             :             Record.push_back(Flags);
    2384             :         }
    2385             :         break;
    2386         922 :       case Instruction::GetElementPtr: {
    2387         922 :         Code = bitc::CST_CODE_CE_GEP;
    2388        3334 :         const auto *GO = cast<GEPOperator>(C);
    2389        2456 :         Record.push_back(VE.getTypeID(GO->getSourceElementType()));
    2390             :         if (Optional<unsigned> Idx = GO->getInRangeIndex()) {
    2391         178 :           Code = bitc::CST_CODE_CE_GEP_WITH_INRANGE_INDEX;
    2392         134 :           Record.push_back((*Idx << 1) | GO->isInBounds());
    2393         268 :         } else if (GO->isInBounds())
    2394             :           Code = bitc::CST_CODE_CE_INBOUNDS_GEP;
    2395             :         for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
    2396             :           Record.push_back(VE.getTypeID(C->getOperand(i)->getType()));
    2397       16387 :           Record.push_back(VE.getValueID(C->getOperand(i)));
    2398       10603 :         }
    2399             :         break;
    2400             :       }
    2401        6791 :       case Instruction::Select:
    2402             :         Code = bitc::CST_CODE_CE_SELECT;
    2403        3507 :         Record.push_back(VE.getValueID(C->getOperand(0)));
    2404             :         Record.push_back(VE.getValueID(C->getOperand(1)));
    2405        3363 :         Record.push_back(VE.getValueID(C->getOperand(2)));
    2406        6726 :         break;
    2407        6726 :       case Instruction::ExtractElement:
    2408             :         Code = bitc::CST_CODE_CE_EXTRACTELT;
    2409             :         Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
    2410             :         Record.push_back(VE.getValueID(C->getOperand(0)));
    2411             :         Record.push_back(VE.getTypeID(C->getOperand(1)->getType()));
    2412         144 :         Record.push_back(VE.getValueID(C->getOperand(1)));
    2413         288 :         break;
    2414         144 :       case Instruction::InsertElement:
    2415         144 :         Code = bitc::CST_CODE_CE_INSERTELT;
    2416         144 :         Record.push_back(VE.getValueID(C->getOperand(0)));
    2417          85 :         Record.push_back(VE.getValueID(C->getOperand(1)));
    2418             :         Record.push_back(VE.getTypeID(C->getOperand(2)->getType()));
    2419             :         Record.push_back(VE.getValueID(C->getOperand(2)));
    2420        3203 :         break;
    2421             :       case Instruction::ShuffleVector:
    2422             :         // If the return type and argument types are the same, this is a
    2423        6406 :         // standard shufflevector instruction.  If the types are different,
    2424        3203 :         // then the shuffle is widening or truncating the input vectors, and
    2425             :         // the argument type must also be encoded.
    2426          60 :         if (C->getType() == C->getOperand(0)->getType()) {
    2427        3173 :           Code = bitc::CST_CODE_CE_SHUFFLEVEC;
    2428             :         } else {
    2429       12798 :           Code = bitc::CST_CODE_CE_SHUFVEC_EX;
    2430       19190 :           Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
    2431       19190 :         }
    2432             :         Record.push_back(VE.getValueID(C->getOperand(0)));
    2433             :         Record.push_back(VE.getValueID(C->getOperand(1)));
    2434             :         Record.push_back(VE.getValueID(C->getOperand(2)));
    2435           7 :         break;
    2436             :       case Instruction::ICmp:
    2437          14 :       case Instruction::FCmp:
    2438           7 :         Code = bitc::CST_CODE_CE_CMP;
    2439           7 :         Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
    2440           7 :         Record.push_back(VE.getValueID(C->getOperand(0)));
    2441          15 :         Record.push_back(VE.getValueID(C->getOperand(1)));
    2442             :         Record.push_back(CE->getPredicate());
    2443          30 :         break;
    2444          30 :       }
    2445          15 :     } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
    2446          15 :       Code = bitc::CST_CODE_BLOCKADDRESS;
    2447          15 :       Record.push_back(VE.getTypeID(BA->getFunction()->getType()));
    2448           0 :       Record.push_back(VE.getValueID(BA->getFunction()));
    2449             :       Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock()));
    2450           0 :     } else {
    2451           0 : #ifndef NDEBUG
    2452           0 :       C->dump();
    2453           0 : #endif
    2454           0 :       llvm_unreachable("Unknown constant!");
    2455           0 :     }
    2456             :     Stream.EmitRecord(Code, Record, AbbrevToUse);
    2457             :     Record.clear();
    2458             :   }
    2459             : 
    2460           0 :   Stream.ExitBlock();
    2461             : }
    2462             : 
    2463             : void ModuleBitcodeWriter::writeModuleConstants() {
    2464           0 :   const ValueEnumerator::ValueList &Vals = VE.getValues();
    2465             : 
    2466           0 :   // Find the first constant to emit, which is the first non-globalvalue value.
    2467           0 :   // We know globalvalues have been emitted by WriteModuleInfo.
    2468           0 :   for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
    2469           0 :     if (!isa<GlobalValue>(Vals[i].first)) {
    2470          59 :       writeConstants(i, Vals.size(), true);
    2471             :       return;
    2472             :     }
    2473         118 :   }
    2474         118 : }
    2475          59 : 
    2476          59 : /// pushValueAndType - The file has to encode both the value and type id for
    2477          59 : /// many values, because we need to know what type to create for forward
    2478             : /// references.  However, most operands are not forward references, so this type
    2479             : /// field is not needed.
    2480             : ///
    2481          84 : /// This function adds V's value ID to Vals.  If the value ID is higher than the
    2482         168 : /// instruction ID, then it is a forward reference, and it also includes the
    2483          84 : /// type ID.  The value ID that is written is encoded relative to the InstID.
    2484             : bool ModuleBitcodeWriter::pushValueAndType(const Value *V, unsigned InstID,
    2485             :                                            SmallVectorImpl<unsigned> &Vals) {
    2486             :   unsigned ValID = VE.getValueID(V);
    2487             :   // Make encoding relative to the InstID.
    2488           0 :   Vals.push_back(InstID - ValID);
    2489             :   if (ValID >= InstID) {
    2490       35241 :     Vals.push_back(VE.getTypeID(V->getType()));
    2491             :     return true;
    2492             :   }
    2493             :   return false;
    2494        8251 : }
    2495             : 
    2496             : void ModuleBitcodeWriter::writeOperandBundles(ImmutableCallSite CS,
    2497        4708 :                                               unsigned InstID) {
    2498             :   SmallVector<unsigned, 64> Record;
    2499             :   LLVMContext &C = CS.getInstruction()->getContext();
    2500             : 
    2501             :   for (unsigned i = 0, e = CS.getNumOperandBundles(); i != e; ++i) {
    2502       36872 :     const auto &Bundle = CS.getOperandBundleAt(i);
    2503       58888 :     Record.push_back(C.getOperandBundleTagID(Bundle.getTagName()));
    2504        1988 : 
    2505        1988 :     for (auto &Input : Bundle.Inputs)
    2506             :       pushValueAndType(Input, InstID, Record);
    2507             : 
    2508             :     Stream.EmitRecord(bitc::FUNC_CODE_OPERAND_BUNDLE, Record);
    2509             :     Record.clear();
    2510             :   }
    2511             : }
    2512             : 
    2513             : /// pushValue - Like pushValueAndType, but where the type of the value is
    2514             : /// omitted (perhaps it was already encoded in an earlier operand).
    2515             : void ModuleBitcodeWriter::pushValue(const Value *V, unsigned InstID,
    2516             :                                     SmallVectorImpl<unsigned> &Vals) {
    2517             :   unsigned ValID = VE.getValueID(V);
    2518      178658 :   Vals.push_back(InstID - ValID);
    2519             : }
    2520      178658 : 
    2521             : void ModuleBitcodeWriter::pushValueSigned(const Value *V, unsigned InstID,
    2522      178658 :                                           SmallVectorImpl<uint64_t> &Vals) {
    2523      178658 :   unsigned ValID = VE.getValueID(V);
    2524          89 :   int64_t diff = ((int32_t)InstID - (int32_t)ValID);
    2525          89 :   emitSignedInt64(Vals, diff);
    2526             : }
    2527             : 
    2528             : /// WriteInstruction - Emit an instruction to the specified stream.
    2529             : void ModuleBitcodeWriter::writeInstruction(const Instruction &I,
    2530          43 :                                            unsigned InstID,
    2531             :                                            SmallVectorImpl<unsigned> &Vals) {
    2532             :   unsigned Code = 0;
    2533          43 :   unsigned AbbrevToUse = 0;
    2534             :   VE.setInstructionID(&I);
    2535         113 :   switch (I.getOpcode()) {
    2536          70 :   default:
    2537         140 :     if (Instruction::isCast(I.getOpcode())) {
    2538             :       Code = bitc::FUNC_CODE_INST_CAST;
    2539         234 :       if (!pushValueAndType(I.getOperand(0), InstID, Vals))
    2540         164 :         AbbrevToUse = FUNCTION_INST_CAST_ABBREV;
    2541             :       Vals.push_back(VE.getTypeID(I.getType()));
    2542          70 :       Vals.push_back(getEncodedCastOpcode(I.getOpcode()));
    2543             :     } else {
    2544             :       assert(isa<BinaryOperator>(I) && "Unknown instruction!");
    2545          43 :       Code = bitc::FUNC_CODE_INST_BINOP;
    2546             :       if (!pushValueAndType(I.getOperand(0), InstID, Vals))
    2547             :         AbbrevToUse = FUNCTION_INST_BINOP_ABBREV;
    2548             :       pushValue(I.getOperand(1), InstID, Vals);
    2549             :       Vals.push_back(getEncodedBinaryOpcode(I.getOpcode()));
    2550             :       uint64_t Flags = getOptimizationFlags(&I);
    2551       64089 :       if (Flags != 0) {
    2552       64089 :         if (AbbrevToUse == FUNCTION_INST_BINOP_ABBREV)
    2553             :           AbbrevToUse = FUNCTION_INST_BINOP_FLAGS_ABBREV;
    2554             :         Vals.push_back(Flags);
    2555             :       }
    2556             :     }
    2557        3601 :     break;
    2558        3601 : 
    2559        3601 :   case Instruction::GetElementPtr: {
    2560             :     Code = bitc::FUNC_CODE_INST_GEP;
    2561             :     AbbrevToUse = FUNCTION_INST_GEP_ABBREV;
    2562             :     auto &GEPInst = cast<GetElementPtrInst>(I);
    2563      177019 :     Vals.push_back(GEPInst.isInBounds());
    2564             :     Vals.push_back(VE.getTypeID(GEPInst.getSourceElementType()));
    2565             :     for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
    2566             :       pushValueAndType(I.getOperand(i), InstID, Vals);
    2567             :     break;
    2568      177019 :   }
    2569      177019 :   case Instruction::ExtractValue: {
    2570             :     Code = bitc::FUNC_CODE_INST_EXTRACTVAL;
    2571       20567 :     pushValueAndType(I.getOperand(0), InstID, Vals);
    2572             :     const ExtractValueInst *EVI = cast<ExtractValueInst>(&I);
    2573       19526 :     Vals.append(EVI->idx_begin(), EVI->idx_end());
    2574             :     break;
    2575        9763 :   }
    2576        9763 :   case Instruction::InsertValue: {
    2577             :     Code = bitc::FUNC_CODE_INST_INSERTVAL;
    2578             :     pushValueAndType(I.getOperand(0), InstID, Vals);
    2579             :     pushValueAndType(I.getOperand(1), InstID, Vals);
    2580       21608 :     const InsertValueInst *IVI = cast<InsertValueInst>(&I);
    2581             :     Vals.append(IVI->idx_begin(), IVI->idx_end());
    2582             :     break;
    2583       10804 :   }
    2584       10804 :   case Instruction::Select:
    2585       10804 :     Code = bitc::FUNC_CODE_INST_VSELECT;
    2586        7834 :     pushValueAndType(I.getOperand(1), InstID, Vals);
    2587             :     pushValue(I.getOperand(2), InstID, Vals);
    2588        7834 :     pushValueAndType(I.getOperand(0), InstID, Vals);
    2589             :     break;
    2590             :   case Instruction::ExtractElement:
    2591             :     Code = bitc::FUNC_CODE_INST_EXTRACTELT;
    2592             :     pushValueAndType(I.getOperand(0), InstID, Vals);
    2593       10680 :     pushValueAndType(I.getOperand(1), InstID, Vals);
    2594             :     break;
    2595             :   case Instruction::InsertElement:
    2596             :     Code = bitc::FUNC_CODE_INST_INSERTELT;
    2597       10680 :     pushValueAndType(I.getOperand(0), InstID, Vals);
    2598       10680 :     pushValue(I.getOperand(1), InstID, Vals);
    2599       40936 :     pushValueAndType(I.getOperand(2), InstID, Vals);
    2600       60512 :     break;
    2601             :   case Instruction::ShuffleVector:
    2602             :     Code = bitc::FUNC_CODE_INST_SHUFFLEVEC;
    2603         260 :     pushValueAndType(I.getOperand(0), InstID, Vals);
    2604             :     pushValue(I.getOperand(1), InstID, Vals);
    2605         520 :     pushValue(I.getOperand(2), InstID, Vals);
    2606             :     break;
    2607         260 :   case Instruction::ICmp:
    2608         260 :   case Instruction::FCmp: {
    2609             :     // compare returning Int1Ty or vector of Int1Ty
    2610          38 :     Code = bitc::FUNC_CODE_INST_CMP2;
    2611             :     pushValueAndType(I.getOperand(0), InstID, Vals);
    2612          76 :     pushValue(I.getOperand(1), InstID, Vals);
    2613          38 :     Vals.push_back(cast<CmpInst>(I).getPredicate());
    2614             :     uint64_t Flags = getOptimizationFlags(&I);
    2615          38 :     if (Flags != 0)
    2616          38 :       Vals.push_back(Flags);
    2617             :     break;
    2618         206 :   }
    2619             : 
    2620         412 :   case Instruction::Ret:
    2621             :     {
    2622         206 :       Code = bitc::FUNC_CODE_INST_RET;
    2623         206 :       unsigned NumOperands = I.getNumOperands();
    2624          46 :       if (NumOperands == 0)
    2625             :         AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV;
    2626          92 :       else if (NumOperands == 1) {
    2627          46 :         if (!pushValueAndType(I.getOperand(0), InstID, Vals))
    2628          46 :           AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV;
    2629          28 :       } else {
    2630             :         for (unsigned i = 0, e = NumOperands; i != e; ++i)
    2631          56 :           pushValueAndType(I.getOperand(i), InstID, Vals);
    2632             :       }
    2633          28 :     }
    2634          28 :     break;
    2635          39 :   case Instruction::Br:
    2636             :     {
    2637          78 :       Code = bitc::FUNC_CODE_INST_BR;
    2638             :       const BranchInst &II = cast<BranchInst>(I);
    2639             :       Vals.push_back(VE.getValueID(II.getSuccessor(0)));
    2640             :       if (II.isConditional()) {
    2641        9237 :         Vals.push_back(VE.getValueID(II.getSuccessor(1)));
    2642             :         pushValue(II.getCondition(), InstID, Vals);
    2643             :       }
    2644             :     }
    2645       18474 :     break;
    2646             :   case Instruction::Switch:
    2647        9237 :     {
    2648        9237 :       Code = bitc::FUNC_CODE_INST_SWITCH;
    2649        9237 :       const SwitchInst &SI = cast<SwitchInst>(I);
    2650           3 :       Vals.push_back(VE.getTypeID(SI.getCondition()->getType()));
    2651             :       pushValue(SI.getCondition(), InstID, Vals);
    2652             :       Vals.push_back(VE.getValueID(SI.getDefaultDest()));
    2653             :       for (auto Case : SI.cases()) {
    2654       12156 :         Vals.push_back(VE.getValueID(Case.getCaseValue()));
    2655             :         Vals.push_back(VE.getValueID(Case.getCaseSuccessor()));
    2656             :       }
    2657             :     }
    2658       12156 :     break;
    2659             :   case Instruction::IndirectBr:
    2660        3498 :     Code = bitc::FUNC_CODE_INST_INDIRECTBR;
    2661        6996 :     Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
    2662             :     // Encode the address operand as relative, but not the basic blocks.
    2663             :     pushValue(I.getOperand(0), InstID, Vals);
    2664           0 :     for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i)
    2665           0 :       Vals.push_back(VE.getValueID(I.getOperand(i)));
    2666             :     break;
    2667             : 
    2668             :   case Instruction::Invoke: {
    2669       17323 :     const InvokeInst *II = cast<InvokeInst>(&I);
    2670             :     const Value *Callee = II->getCalledValue();
    2671             :     FunctionType *FTy = II->getFunctionType();
    2672             : 
    2673       17323 :     if (II->hasOperandBundles())
    2674       17323 :       writeOperandBundles(II, InstID);
    2675        5844 : 
    2676             :     Code = bitc::FUNC_CODE_INST_INVOKE;
    2677             : 
    2678             :     Vals.push_back(VE.getAttributeListID(II->getAttributes()));
    2679             :     Vals.push_back(II->getCallingConv() | 1 << 13);
    2680          95 :     Vals.push_back(VE.getValueID(II->getNormalDest()));
    2681             :     Vals.push_back(VE.getValueID(II->getUnwindDest()));
    2682             :     Vals.push_back(VE.getTypeID(FTy));
    2683             :     pushValueAndType(Callee, InstID, Vals);
    2684         190 : 
    2685             :     // Emit value #'s for the fixed parameters.
    2686          95 :     for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
    2687         346 :       pushValue(I.getOperand(i), InstID, Vals); // fixed param.
    2688         251 : 
    2689         251 :     // Emit type/value pairs for varargs params.
    2690             :     if (FTy->isVarArg()) {
    2691             :       for (unsigned i = FTy->getNumParams(), e = II->getNumArgOperands();
    2692          95 :            i != e; ++i)
    2693          17 :         pushValueAndType(I.getOperand(i), InstID, Vals); // vararg
    2694             :     }
    2695          34 :     break;
    2696             :   }
    2697             :   case Instruction::Resume:
    2698          43 :     Code = bitc::FUNC_CODE_INST_RESUME;
    2699          43 :     pushValueAndType(I.getOperand(0), InstID, Vals);
    2700             :     break;
    2701             :   case Instruction::CleanupRet: {
    2702             :     Code = bitc::FUNC_CODE_INST_CLEANUPRET;
    2703             :     const auto &CRI = cast<CleanupReturnInst>(I);
    2704             :     pushValue(CRI.getCleanupPad(), InstID, Vals);
    2705         159 :     if (CRI.hasUnwindDest())
    2706             :       Vals.push_back(VE.getValueID(CRI.getUnwindDest()));
    2707         159 :     break;
    2708          22 :   }
    2709             :   case Instruction::CatchRet: {
    2710             :     Code = bitc::FUNC_CODE_INST_CATCHRET;
    2711             :     const auto &CRI = cast<CatchReturnInst>(I);
    2712         200 :     pushValue(CRI.getCatchPad(), InstID, Vals);
    2713         159 :     Vals.push_back(VE.getValueID(CRI.getSuccessor()));
    2714         159 :     break;
    2715         159 :   }
    2716         159 :   case Instruction::CleanupPad:
    2717         159 :   case Instruction::CatchPad: {
    2718             :     const auto &FuncletPad = cast<FuncletPadInst>(I);
    2719             :     Code = isa<CatchPadInst>(FuncletPad) ? bitc::FUNC_CODE_INST_CATCHPAD
    2720         267 :                                          : bitc::FUNC_CODE_INST_CLEANUPPAD;
    2721         108 :     pushValue(FuncletPad.getParentPad(), InstID, Vals);
    2722             : 
    2723             :     unsigned NumArgOperands = FuncletPad.getNumArgOperands();
    2724         159 :     Vals.push_back(NumArgOperands);
    2725          12 :     for (unsigned Op = 0; Op != NumArgOperands; ++Op)
    2726          12 :       pushValueAndType(FuncletPad.getArgOperand(Op), InstID, Vals);
    2727          12 :     break;
    2728             :   }
    2729             :   case Instruction::CatchSwitch: {
    2730             :     Code = bitc::FUNC_CODE_INST_CATCHSWITCH;
    2731          15 :     const auto &CatchSwitch = cast<CatchSwitchInst>(I);
    2732             : 
    2733          30 :     pushValue(CatchSwitch.getParentPad(), InstID, Vals);
    2734          15 : 
    2735          12 :     unsigned NumHandlers = CatchSwitch.getNumHandlers();
    2736             :     Vals.push_back(NumHandlers);
    2737             :     for (const BasicBlock *CatchPadBB : CatchSwitch.handlers())
    2738             :       Vals.push_back(VE.getValueID(CatchPadBB));
    2739          12 : 
    2740           2 :     if (CatchSwitch.hasUnwindDest())
    2741             :       Vals.push_back(VE.getValueID(CatchSwitch.getUnwindDest()));
    2742             :     break;
    2743          10 :   }
    2744             :   case Instruction::Unreachable:
    2745             :     Code = bitc::FUNC_CODE_INST_UNREACHABLE;
    2746             :     AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV;
    2747          10 :     break;
    2748          10 : 
    2749             :   case Instruction::PHI: {
    2750             :     const PHINode &PN = cast<PHINode>(I);
    2751             :     Code = bitc::FUNC_CODE_INST_PHI;
    2752             :     // With the newer instruction encoding, forward references could give
    2753          44 :     // negative valued IDs.  This is most common for PHIs, so we use
    2754             :     // signed VBRs.
    2755             :     SmallVector<uint64_t, 128> Vals64;
    2756             :     Vals64.push_back(VE.getTypeID(PN.getType()));
    2757          44 :     for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
    2758          44 :       pushValueSigned(PN.getIncomingValue(i), InstID, Vals64);
    2759          71 :       Vals64.push_back(VE.getValueID(PN.getIncomingBlock(i)));
    2760          27 :     }
    2761             :     // Emit a Vals64 vector and exit.
    2762             :     Stream.EmitRecord(Code, Vals64, AbbrevToUse);
    2763          21 :     Vals64.clear();
    2764             :     return;
    2765             :   }
    2766             : 
    2767             :   case Instruction::LandingPad: {
    2768             :     const LandingPadInst &LP = cast<LandingPadInst>(I);
    2769          21 :     Code = bitc::FUNC_CODE_INST_LANDINGPAD;
    2770          21 :     Vals.push_back(VE.getTypeID(LP.getType()));
    2771          44 :     Vals.push_back(LP.isCleanup());
    2772          23 :     Vals.push_back(LP.getNumClauses());
    2773             :     for (unsigned I = 0, E = LP.getNumClauses(); I != E; ++I) {
    2774          21 :       if (LP.isCatch(I))
    2775           8 :         Vals.push_back(LandingPadInst::Catch);
    2776             :       else
    2777             :         Vals.push_back(LandingPadInst::Filter);
    2778             :       pushValueAndType(LP.getClause(I), InstID, Vals);
    2779             :     }
    2780             :     break;
    2781             :   }
    2782             : 
    2783             :   case Instruction::Alloca: {
    2784             :     Code = bitc::FUNC_CODE_INST_ALLOCA;
    2785             :     const AllocaInst &AI = cast<AllocaInst>(I);
    2786             :     Vals.push_back(VE.getTypeID(AI.getAllocatedType()));
    2787             :     Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
    2788             :     Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
    2789             :     unsigned AlignRecord = Log2_32(AI.getAlignment()) + 1;
    2790        1831 :     assert(Log2_32(Value::MaximumAlignment) + 1 < 1 << 5 &&
    2791        5432 :            "not enough bits for maximum alignment");
    2792             :     assert(AlignRecord < 1 << 5 && "alignment greater than 1 << 64");
    2793        3601 :     AlignRecord |= AI.isUsedWithInAlloca() << 5;
    2794             :     AlignRecord |= 1 << 6;
    2795             :     AlignRecord |= AI.isSwiftError() << 7;
    2796        1831 :     Vals.push_back(AlignRecord);
    2797             :     break;
    2798             :   }
    2799             : 
    2800             :   case Instruction::Load:
    2801             :     if (cast<LoadInst>(I).isAtomic()) {
    2802             :       Code = bitc::FUNC_CODE_INST_LOADATOMIC;
    2803             :       pushValueAndType(I.getOperand(0), InstID, Vals);
    2804          84 :     } else {
    2805          84 :       Code = bitc::FUNC_CODE_INST_LOAD;
    2806          84 :       if (!pushValueAndType(I.getOperand(0), InstID, Vals)) // ptr
    2807         107 :         AbbrevToUse = FUNCTION_INST_LOAD_ABBREV;
    2808          23 :     }
    2809          18 :     Vals.push_back(VE.getTypeID(I.getType()));
    2810             :     Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1);
    2811           5 :     Vals.push_back(cast<LoadInst>(I).isVolatile());
    2812          23 :     if (cast<LoadInst>(I).isAtomic()) {
    2813             :       Vals.push_back(getEncodedOrdering(cast<LoadInst>(I).getOrdering()));
    2814             :       Vals.push_back(getEncodedSyncScopeID(cast<LoadInst>(I).getSyncScopeID()));
    2815             :     }
    2816             :     break;
    2817       24583 :   case Instruction::Store:
    2818             :     if (cast<StoreInst>(I).isAtomic())
    2819             :       Code = bitc::FUNC_CODE_INST_STOREATOMIC;
    2820       24583 :     else
    2821       49166 :       Code = bitc::FUNC_CODE_INST_STORE;
    2822       24583 :     pushValueAndType(I.getOperand(1), InstID, Vals); // ptrty + ptr
    2823       24583 :     pushValueAndType(I.getOperand(0), InstID, Vals); // valty + val
    2824             :     Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1);
    2825             :     Vals.push_back(cast<StoreInst>(I).isVolatile());
    2826             :     if (cast<StoreInst>(I).isAtomic()) {
    2827       24583 :       Vals.push_back(getEncodedOrdering(cast<StoreInst>(I).getOrdering()));
    2828       24583 :       Vals.push_back(
    2829       24583 :           getEncodedSyncScopeID(cast<StoreInst>(I).getSyncScopeID()));
    2830       24583 :     }
    2831             :     break;
    2832             :   case Instruction::AtomicCmpXchg:
    2833             :     Code = bitc::FUNC_CODE_INST_CMPXCHG;
    2834             :     pushValueAndType(I.getOperand(0), InstID, Vals); // ptrty + ptr
    2835       30228 :     pushValueAndType(I.getOperand(1), InstID, Vals); // cmp.
    2836             :     pushValue(I.getOperand(2), InstID, Vals);        // newval.
    2837         210 :     Vals.push_back(cast<AtomicCmpXchgInst>(I).isVolatile());
    2838             :     Vals.push_back(
    2839             :         getEncodedOrdering(cast<AtomicCmpXchgInst>(I).getSuccessOrdering()));
    2840       60246 :     Vals.push_back(
    2841             :         getEncodedSyncScopeID(cast<AtomicCmpXchgInst>(I).getSyncScopeID()));
    2842             :     Vals.push_back(
    2843       30228 :         getEncodedOrdering(cast<AtomicCmpXchgInst>(I).getFailureOrdering()));
    2844       30228 :     Vals.push_back(cast<AtomicCmpXchgInst>(I).isWeak());
    2845       30228 :     break;
    2846       30228 :   case Instruction::AtomicRMW:
    2847         105 :     Code = bitc::FUNC_CODE_INST_ATOMICRMW;
    2848         105 :     pushValueAndType(I.getOperand(0), InstID, Vals); // ptrty + ptr
    2849             :     pushValue(I.getOperand(1), InstID, Vals);        // val.
    2850             :     Vals.push_back(
    2851             :         getEncodedRMWOperation(cast<AtomicRMWInst>(I).getOperation()));
    2852       32412 :     Vals.push_back(cast<AtomicRMWInst>(I).isVolatile());
    2853             :     Vals.push_back(getEncodedOrdering(cast<AtomicRMWInst>(I).getOrdering()));
    2854             :     Vals.push_back(
    2855             :         getEncodedSyncScopeID(cast<AtomicRMWInst>(I).getSyncScopeID()));
    2856       64824 :     break;
    2857       32412 :   case Instruction::Fence:
    2858       63987 :     Code = bitc::FUNC_CODE_INST_FENCE;
    2859       32412 :     Vals.push_back(getEncodedOrdering(cast<FenceInst>(I).getOrdering()));
    2860       32412 :     Vals.push_back(getEncodedSyncScopeID(cast<FenceInst>(I).getSyncScopeID()));
    2861          86 :     break;
    2862          86 :   case Instruction::Call: {
    2863          86 :     const CallInst &CI = cast<CallInst>(I);
    2864             :     FunctionType *FTy = CI.getFunctionType();
    2865             : 
    2866         188 :     if (CI.hasOperandBundles())
    2867             :       writeOperandBundles(&CI, InstID);
    2868         376 : 
    2869         188 :     Code = bitc::FUNC_CODE_INST_CALL;
    2870             : 
    2871         188 :     Vals.push_back(VE.getAttributeListID(CI.getAttributes()));
    2872         188 : 
    2873         188 :     unsigned Flags = getOptimizationFlags(&I);
    2874         188 :     Vals.push_back(CI.getCallingConv() << bitc::CALL_CCONV |
    2875         188 :                    unsigned(CI.isTailCall()) << bitc::CALL_TAIL |
    2876         188 :                    unsigned(CI.isMustTailCall()) << bitc::CALL_MUSTTAIL |
    2877         188 :                    1 << bitc::CALL_EXPLICIT_TYPE |
    2878         188 :                    unsigned(CI.isNoTailCall()) << bitc::CALL_NOTAIL |
    2879         188 :                    unsigned(Flags != 0) << bitc::CALL_FMF);
    2880          85 :     if (Flags != 0)
    2881             :       Vals.push_back(Flags);
    2882         170 : 
    2883             :     Vals.push_back(VE.getTypeID(FTy));
    2884          85 :     pushValueAndType(CI.getCalledValue(), InstID, Vals); // Callee
    2885          85 : 
    2886          85 :     // Emit value #'s for the fixed parameters.
    2887          85 :     for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
    2888          85 :       // Check for labels (can happen with asm labels).
    2889          85 :       if (FTy->getParamType(i)->isLabelTy())
    2890          85 :         Vals.push_back(VE.getValueID(CI.getArgOperand(i)));
    2891          44 :       else
    2892             :         pushValue(CI.getArgOperand(i), InstID, Vals); // fixed param.
    2893          88 :     }
    2894          44 : 
    2895          44 :     // Emit type/value pairs for varargs params.
    2896             :     if (FTy->isVarArg()) {
    2897             :       for (unsigned i = FTy->getNumParams(), e = CI.getNumArgOperands();
    2898       16313 :            i != e; ++i)
    2899             :         pushValueAndType(CI.getArgOperand(i), InstID, Vals); // varargs
    2900       16313 :     }
    2901          21 :     break;
    2902             :   }
    2903             :   case Instruction::VAArg:
    2904             :     Code = bitc::FUNC_CODE_INST_VAARG;
    2905       19568 :     Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));   // valistty
    2906             :     pushValue(I.getOperand(0), InstID, Vals);                   // valist.
    2907       16313 :     Vals.push_back(VE.getTypeID(I.getType())); // restype.
    2908       16313 :     break;
    2909       16313 :   }
    2910       16313 : 
    2911       16313 :   Stream.EmitRecord(Code, Vals, AbbrevToUse);
    2912       32610 :   Vals.clear();
    2913             : }
    2914       16313 : 
    2915          16 : /// Write a GlobalValue VST to the module. The purpose of this data structure is
    2916             : /// to allow clients to efficiently find the function body.
    2917       16313 : void ModuleBitcodeWriter::writeGlobalValueSymbolTable(
    2918       16313 :   DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex) {
    2919             :   // Get the offset of the VST we are writing, and backpatch it into
    2920             :   // the VST forward declaration record.
    2921       53617 :   uint64_t VSTOffset = Stream.GetCurrentBitNo();
    2922             :   // The BitcodeStartBit was the stream offset of the identification block.
    2923       74608 :   VSTOffset -= bitcodeStartBit();
    2924           2 :   assert((VSTOffset & 31) == 0 && "VST block not 32-bit aligned");
    2925             :   // Note that we add 1 here because the offset is relative to one word
    2926             :   // before the start of the identification block, which was historically
    2927             :   // always the start of the regular bitcode header.
    2928             :   Stream.BackpatchWord(VSTOffsetPlaceholder, VSTOffset / 32 + 1);
    2929             : 
    2930       16313 :   Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
    2931        3401 : 
    2932        3401 :   auto Abbv = std::make_shared<BitCodeAbbrev>();
    2933        1945 :   Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_FNENTRY));
    2934             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
    2935             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
    2936             :   unsigned FnEntryAbbrev = Stream.EmitAbbrev(std::move(Abbv));
    2937          10 : 
    2938             :   for (const Function &F : M) {
    2939          20 :     uint64_t Record[2];
    2940             : 
    2941          10 :     if (F.isDeclaration())
    2942          10 :       continue;
    2943             : 
    2944             :     Record[0] = VE.getValueID(&F);
    2945      175188 : 
    2946             :     // Save the word offset of the function (from the start of the
    2947             :     // actual bitcode written to the stream).
    2948             :     uint64_t BitcodeIndex = FunctionToBitcodeIndex[&F] - bitcodeStartBit();
    2949             :     assert((BitcodeIndex & 31) == 0 && "function block not 32-bit aligned");
    2950             :     // Note that we add 1 here because the offset is relative to one word
    2951        4708 :     // before the start of the identification block, which was historically
    2952             :     // always the start of the regular bitcode header.
    2953             :     Record[1] = BitcodeIndex / 32 + 1;
    2954             : 
    2955        4708 :     Stream.EmitRecord(bitc::VST_CODE_FNENTRY, Record, FnEntryAbbrev);
    2956             :   }
    2957        4708 : 
    2958             :   Stream.ExitBlock();
    2959             : }
    2960             : 
    2961             : /// Emit names for arguments, instructions and basic blocks in a function.
    2962        4708 : void ModuleBitcodeWriter::writeFunctionLevelValueSymbolTable(
    2963             :     const ValueSymbolTable &VST) {
    2964        4708 :   if (VST.empty())
    2965             :     return;
    2966             : 
    2967        4708 :   Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
    2968        4708 : 
    2969        4708 :   // FIXME: Set up the abbrev, we know how many values there are!
    2970        9416 :   // FIXME: We know if the type names can use 7-bit ascii.
    2971             :   SmallVector<uint64_t, 64> NameVals;
    2972       21309 : 
    2973             :   for (const ValueName &Name : VST) {
    2974             :     // Figure out the encoding to use for the name.
    2975       16601 :     StringEncoding Bits = getStringEncoding(Name.getKey());
    2976        4423 : 
    2977             :     unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
    2978       12178 :     NameVals.push_back(VE.getValueID(Name.getValue()));
    2979             : 
    2980             :     // VST_CODE_ENTRY:   [valueid, namechar x N]
    2981             :     // VST_CODE_BBENTRY: [bbid, namechar x N]
    2982       12178 :     unsigned Code;
    2983             :     if (isa<BasicBlock>(Name.getValue())) {
    2984             :       Code = bitc::VST_CODE_BBENTRY;
    2985             :       if (Bits == SE_Char6)
    2986             :         AbbrevToUse = VST_BBENTRY_6_ABBREV;
    2987       12178 :     } else {
    2988             :       Code = bitc::VST_CODE_ENTRY;
    2989       12178 :       if (Bits == SE_Char6)
    2990             :         AbbrevToUse = VST_ENTRY_6_ABBREV;
    2991             :       else if (Bits == SE_Fixed7)
    2992        4708 :         AbbrevToUse = VST_ENTRY_7_ABBREV;
    2993        4708 :     }
    2994             : 
    2995             :     for (const auto P : Name.getKey())
    2996       12089 :       NameVals.push_back((unsigned char)P);
    2997             : 
    2998       12089 :     // Emit the finished record.
    2999        2873 :     Stream.EmitRecord(Code, NameVals, AbbrevToUse);
    3000             :     NameVals.clear();
    3001        9216 :   }
    3002             : 
    3003             :   Stream.ExitBlock();
    3004             : }
    3005             : 
    3006             : void ModuleBitcodeWriter::writeUseList(UseListOrder &&Order) {
    3007      100883 :   assert(Order.Shuffle.size() >= 2 && "Shuffle too small");
    3008             :   unsigned Code;
    3009       91667 :   if (isa<BasicBlock>(Order.V))
    3010             :     Code = bitc::USELIST_CODE_BB;
    3011             :   else
    3012       91667 :     Code = bitc::USELIST_CODE_DEFAULT;
    3013             : 
    3014             :   SmallVector<uint64_t, 64> Record(Order.Shuffle.begin(), Order.Shuffle.end());
    3015             :   Record.push_back(VE.getValueID(Order.V));
    3016             :   Stream.EmitRecord(Code, Record);
    3017      183334 : }
    3018             : 
    3019       25739 : void ModuleBitcodeWriter::writeUseListBlock(const Function *F) {
    3020             :   assert(VE.shouldPreserveUseListOrder() &&
    3021             :          "Expected to be preserving use-list order");
    3022             : 
    3023       65928 :   auto hasMore = [&]() {
    3024             :     return !VE.UseListOrders.empty() && VE.UseListOrders.back().F == F;
    3025          74 :   };
    3026             :   if (!hasMore())
    3027             :     // Nothing to do.
    3028             :     return;
    3029      882475 : 
    3030      790808 :   Stream.EnterSubblock(bitc::USELIST_BLOCK_ID, 3);
    3031             :   while (hasMore()) {
    3032             :     writeUseList(std::move(VE.UseListOrders.back()));
    3033       91667 :     VE.UseListOrders.pop_back();
    3034             :   }
    3035             :   Stream.ExitBlock();
    3036             : }
    3037        9216 : 
    3038             : /// Emit a function body to the module stream.
    3039             : void ModuleBitcodeWriter::writeFunction(
    3040        1552 :     const Function &F,
    3041             :     DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex) {
    3042             :   // Save the bitcode index of the start of this function block for recording
    3043        3104 :   // in the VST.
    3044             :   FunctionToBitcodeIndex[&F] = Stream.GetCurrentBitNo();
    3045             : 
    3046             :   Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4);
    3047             :   VE.incorporateFunction(F);
    3048        1552 : 
    3049        1552 :   SmallVector<unsigned, 64> Vals;
    3050        1552 : 
    3051        1552 :   // Emit the number of basic blocks, so the reader can create them ahead of
    3052             :   // time.
    3053        7499 :   Vals.push_back(VE.getBasicBlocks().size());
    3054             :   Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals);
    3055             :   Vals.clear();
    3056             : 
    3057             :   // If there are function-local constants, emit them now.
    3058        9717 :   unsigned CstStart, CstEnd;
    3059             :   VE.getFunctionConstantRange(CstStart, CstEnd);
    3060             :   writeConstants(CstStart, CstEnd, false);
    3061             : 
    3062             :   // If there is function-local metadata, emit it now.
    3063             :   writeFunctionMetadata(F);
    3064         666 : 
    3065             :   // Keep a running idea of what the instruction ID is.
    3066        1552 :   unsigned InstID = CstEnd;
    3067             : 
    3068             :   bool NeedsMetadataAttachment = F.hasMetadata();
    3069         666 : 
    3070             :   DILocation *LastDL = nullptr;
    3071             :   // Finally, emit all the instructions, in order.
    3072             :   for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
    3073       12178 :     for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
    3074             :          I != E; ++I) {
    3075             :       writeInstruction(*I, InstID, Vals);
    3076             : 
    3077             :       if (!I->getType()->isVoidTy())
    3078       12178 :         ++InstID;
    3079             : 
    3080       12178 :       // If the instruction has metadata, write a metadata attachment later.
    3081       12178 :       NeedsMetadataAttachment |= I->hasMetadataOtherThanDebugLoc();
    3082             : 
    3083             :       // If the instruction has a debug location, emit it.
    3084             :       DILocation *DL = I->getDebugLoc();
    3085             :       if (!DL)
    3086             :         continue;
    3087       24356 : 
    3088       12178 :       if (DL == LastDL) {
    3089             :         // Just repeat the same debug loc as last time.
    3090             :         Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC_AGAIN, Vals);
    3091             :         continue;
    3092             :       }
    3093       12178 : 
    3094       12178 :       Vals.push_back(DL->getLine());
    3095             :       Vals.push_back(DL->getColumn());
    3096             :       Vals.push_back(VE.getMetadataOrNullID(DL->getScope()));
    3097       12178 :       Vals.push_back(VE.getMetadataOrNullID(DL->getInlinedAt()));
    3098             :       Vals.push_back(DL->isImplicitCode());
    3099             :       Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC, Vals);
    3100             :       Vals.clear();
    3101             : 
    3102             :       LastDL = DL;
    3103             :     }
    3104             : 
    3105             :   // Emit names for all the instructions etc.
    3106       42274 :   if (auto *Symtab = F.getValueSymbolTable())
    3107             :     writeFunctionLevelValueSymbolTable(*Symtab);
    3108      207115 : 
    3109      177019 :   if (NeedsMetadataAttachment)
    3110             :     writeFunctionMetadataAttachment(F);
    3111      354038 :   if (VE.shouldPreserveUseListOrder())
    3112      102383 :     writeUseListBlock(&F);
    3113             :   VE.purgeFunction();
    3114             :   Stream.ExitBlock();
    3115      177019 : }
    3116             : 
    3117             : // Emit blockinfo, which defines the standard abbreviations etc.
    3118             : void ModuleBitcodeWriter::writeBlockInfo() {
    3119      177019 :   // We only want to emit block info records for blocks that have multiple
    3120             :   // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK.
    3121             :   // Other blocks can define their abbrevs inline.
    3122        2146 :   Stream.EnterBlockInfoBlock();
    3123             : 
    3124         847 :   { // 8-bit fixed-width VST_CODE_ENTRY/VST_CODE_BBENTRY strings.
    3125         847 :     auto Abbv = std::make_shared<BitCodeAbbrev>();
    3126             :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));
    3127             :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
    3128        1299 :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
    3129        2598 :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
    3130        1299 :     if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, Abbv) !=
    3131        1299 :         VST_ENTRY_8_ABBREV)
    3132        1299 :       llvm_unreachable("Unexpected abbrev ordering!");
    3133        1299 :   }
    3134             : 
    3135             :   { // 7-bit fixed width VST_CODE_ENTRY strings.
    3136             :     auto Abbv = std::make_shared<BitCodeAbbrev>();
    3137             :     Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
    3138             :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
    3139             :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
    3140       12178 :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
    3141       12089 :     if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, Abbv) !=
    3142             :         VST_ENTRY_7_ABBREV)
    3143       12178 :       llvm_unreachable("Unexpected abbrev ordering!");
    3144        1404 :   }
    3145       12178 :   { // 6-bit char6 VST_CODE_ENTRY strings.
    3146        4712 :     auto Abbv = std::make_shared<BitCodeAbbrev>();
    3147       12178 :     Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
    3148       12178 :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
    3149       12178 :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
    3150             :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
    3151             :     if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, Abbv) !=
    3152        4708 :         VST_ENTRY_6_ABBREV)
    3153             :       llvm_unreachable("Unexpected abbrev ordering!");
    3154             :   }
    3155             :   { // 6-bit char6 VST_CODE_BBENTRY strings.
    3156        4708 :     auto Abbv = std::make_shared<BitCodeAbbrev>();
    3157             :     Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY));
    3158             :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
    3159             :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
    3160        4708 :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
    3161        4708 :     if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, Abbv) !=
    3162        4708 :         VST_BBENTRY_6_ABBREV)
    3163        4708 :       llvm_unreachable("Unexpected abbrev ordering!");
    3164        9416 :   }
    3165             : 
    3166           0 :   { // SETTYPE abbrev for CONSTANTS_BLOCK.
    3167             :     auto Abbv = std::make_shared<BitCodeAbbrev>();
    3168             :     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE));
    3169             :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
    3170             :                               VE.computeBitsRequiredForTypeIndicies()));
    3171        4708 :     if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) !=
    3172        4708 :         CONSTANTS_SETTYPE_ABBREV)
    3173        4708 :       llvm_unreachable("Unexpected abbrev ordering!");
    3174        4708 :   }
    3175        9416 : 
    3176             :   { // INTEGER abbrev for CONSTANTS_BLOCK.
    3177           0 :     auto Abbv = std::make_shared<BitCodeAbbrev>();
    3178             :     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER));
    3179             :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
    3180             :     if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) !=
    3181        4708 :         CONSTANTS_INTEGER_ABBREV)
    3182        4708 :       llvm_unreachable("Unexpected abbrev ordering!");
    3183        4708 :   }
    3184        4708 : 
    3185        9416 :   { // CE_CAST abbrev for CONSTANTS_BLOCK.
    3186             :     auto Abbv = std::make_shared<BitCodeAbbrev>();
    3187           0 :     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST));
    3188             :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4));  // cast opc
    3189             :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,       // typeid
    3190             :                               VE.computeBitsRequiredForTypeIndicies()));
    3191        4708 :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));    // value id
    3192        4708 : 
    3193        4708 :     if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) !=
    3194        4708 :         CONSTANTS_CE_CAST_Abbrev)
    3195        9416 :       llvm_unreachable("Unexpected abbrev ordering!");
    3196             :   }
    3197           0 :   { // NULL abbrev for CONSTANTS_BLOCK.
    3198             :     auto Abbv = std::make_shared<BitCodeAbbrev>();
    3199             :     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL));
    3200             :     if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) !=
    3201             :         CONSTANTS_NULL_Abbrev)
    3202        4708 :       llvm_unreachable("Unexpected abbrev ordering!");
    3203        4708 :   }
    3204             : 
    3205        9416 :   // FIXME: This should only use space for first class types!
    3206             : 
    3207           0 :   { // INST_LOAD abbrev for FUNCTION_BLOCK.
    3208             :     auto Abbv = std::make_shared<BitCodeAbbrev>();
    3209             :     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD));
    3210             :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr
    3211             :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,    // dest ty
    3212        4708 :                               VE.computeBitsRequiredForTypeIndicies()));
    3213        4708 :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align
    3214        9416 :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
    3215             :     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
    3216           0 :         FUNCTION_INST_LOAD_ABBREV)
    3217             :       llvm_unreachable("Unexpected abbrev ordering!");
    3218             :   }
    3219             :   { // INST_BINOP abbrev for FUNCTION_BLOCK.
    3220             :     auto Abbv = std::make_shared<BitCodeAbbrev>();
    3221        4708 :     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
    3222        4708 :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
    3223        9416 :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
    3224             :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
    3225        4708 :     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
    3226             :         FUNCTION_INST_BINOP_ABBREV)
    3227        9416 :       llvm_unreachable("Unexpected abbrev ordering!");
    3228             :   }
    3229           0 :   { // INST_BINOP_FLAGS abbrev for FUNCTION_BLOCK.
    3230             :     auto Abbv = std::make_shared<BitCodeAbbrev>();
    3231             :     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
    3232             :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
    3233        4708 :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
    3234        9416 :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
    3235             :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); // flags
    3236           0 :     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
    3237             :         FUNCTION_INST_BINOP_FLAGS_ABBREV)
    3238             :       llvm_unreachable("Unexpected abbrev ordering!");
    3239             :   }
    3240             :   { // INST_CAST abbrev for FUNCTION_BLOCK.
    3241             :     auto Abbv = std::make_shared<BitCodeAbbrev>();
    3242             :     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST));
    3243        4708 :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));    // OpVal
    3244        4708 :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,       // dest ty
    3245        9416 :                               VE.computeBitsRequiredForTypeIndicies()));
    3246             :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4));  // opc
    3247        4708 :     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
    3248        4708 :         FUNCTION_INST_CAST_ABBREV)
    3249        9416 :       llvm_unreachable("Unexpected abbrev ordering!");
    3250             :   }
    3251           0 : 
    3252             :   { // INST_RET abbrev for FUNCTION_BLOCK.
    3253             :     auto Abbv = std::make_shared<BitCodeAbbrev>();
    3254             :     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
    3255        4708 :     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
    3256        4708 :         FUNCTION_INST_RET_VOID_ABBREV)
    3257        4708 :       llvm_unreachable("Unexpected abbrev ordering!");
    3258        4708 :   }
    3259        9416 :   { // INST_RET abbrev for FUNCTION_BLOCK.
    3260             :     auto Abbv = std::make_shared<BitCodeAbbrev>();
    3261           0 :     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
    3262             :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID
    3263             :     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
    3264             :         FUNCTION_INST_RET_VAL_ABBREV)
    3265        4708 :       llvm_unreachable("Unexpected abbrev ordering!");
    3266        4708 :   }
    3267        4708 :   { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
    3268        4708 :     auto Abbv = std::make_shared<BitCodeAbbrev>();
    3269        4708 :     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE));
    3270        9416 :     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
    3271             :         FUNCTION_INST_UNREACHABLE_ABBREV)
    3272           0 :       llvm_unreachable("Unexpected abbrev ordering!");
    3273             :   }
    3274             :   {
    3275             :     auto Abbv = std::make_shared<BitCodeAbbrev>();
    3276        4708 :     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_GEP));
    3277        4708 :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
    3278        9416 :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
    3279             :                               Log2_32_Ceil(VE.getTypes().size() + 1)));
    3280        4708 :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
    3281        9416 :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
    3282             :     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
    3283           0 :         FUNCTION_INST_GEP_ABBREV)
    3284             :       llvm_unreachable("Unexpected abbrev ordering!");
    3285             :   }
    3286             : 
    3287             :   Stream.ExitBlock();
    3288        4708 : }
    3289        9416 : 
    3290             : /// Write the module path strings, currently only used when generating
    3291           0 : /// a combined index file.
    3292             : void IndexBitcodeWriter::writeModStrings() {
    3293             :   Stream.EnterSubblock(bitc::MODULE_STRTAB_BLOCK_ID, 3);
    3294             : 
    3295        4708 :   // TODO: See which abbrev sizes we actually need to emit
    3296        4708 : 
    3297        9416 :   // 8-bit fixed-width MST_ENTRY strings.
    3298             :   auto Abbv = std::make_shared<BitCodeAbbrev>();
    3299           0 :   Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
    3300             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
    3301             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
    3302             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
    3303        4708 :   unsigned Abbrev8Bit = Stream.EmitAbbrev(std::move(Abbv));
    3304        9416 : 
    3305             :   // 7-bit fixed width MST_ENTRY strings.
    3306           0 :   Abbv = std::make_shared<BitCodeAbbrev>();
    3307             :   Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
    3308             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
    3309             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
    3310        4708 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
    3311        4708 :   unsigned Abbrev7Bit = Stream.EmitAbbrev(std::move(Abbv));
    3312        4708 : 
    3313        9416 :   // 6-bit char6 MST_ENTRY strings.
    3314        4708 :   Abbv = std::make_shared<BitCodeAbbrev>();
    3315        4708 :   Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
    3316        9416 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
    3317             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
    3318           0 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
    3319             :   unsigned Abbrev6Bit = Stream.EmitAbbrev(std::move(Abbv));
    3320             : 
    3321        4708 :   // Module Hash, 160 bits SHA1. Optionally, emitted after each MST_CODE_ENTRY.
    3322        4708 :   Abbv = std::make_shared<BitCodeAbbrev>();
    3323             :   Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_HASH));
    3324             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
    3325             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
    3326         208 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
    3327         208 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
    3328             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
    3329             :   unsigned AbbrevHash = Stream.EmitAbbrev(std::move(Abbv));
    3330             : 
    3331             :   SmallVector<unsigned, 64> Vals;
    3332             :   forEachModule(
    3333         208 :       [&](const StringMapEntry<std::pair<uint64_t, ModuleHash>> &MPSE) {
    3334         208 :         StringRef Key = MPSE.getKey();
    3335         208 :         const auto &Value = MPSE.getValue();
    3336         208 :         StringEncoding Bits = getStringEncoding(Key);
    3337         416 :         unsigned AbbrevToUse = Abbrev8Bit;
    3338             :         if (Bits == SE_Char6)
    3339             :           AbbrevToUse = Abbrev6Bit;
    3340         208 :         else if (Bits == SE_Fixed7)
    3341         208 :           AbbrevToUse = Abbrev7Bit;
    3342         208 : 
    3343         208 :         Vals.push_back(Value.first);
    3344         208 :         Vals.append(Key.begin(), Key.end());
    3345         416 : 
    3346             :         // Emit the finished record.
    3347             :         Stream.EmitRecord(bitc::MST_CODE_ENTRY, Vals, AbbrevToUse);
    3348         208 : 
    3349         208 :         // Emit an optional hash for the module now
    3350         208 :         const auto &Hash = Value.second;
    3351         208 :         if (llvm::any_of(Hash, [](uint32_t H) { return H; })) {
    3352         208 :           Vals.assign(Hash.begin(), Hash.end());
    3353         416 :           // Emit the hash record.
    3354             :           Stream.EmitRecord(bitc::MST_CODE_HASH, Vals, AbbrevHash);
    3355             :         }
    3356         208 : 
    3357         208 :         Vals.clear();
    3358         208 :       });
    3359         208 :   Stream.ExitBlock();
    3360         208 : }
    3361         208 : 
    3362         208 : /// Write the function type metadata related records that need to appear before
    3363         416 : /// a function summary entry (whether per-module or combined).
    3364             : static void writeFunctionTypeMetadataRecords(BitstreamWriter &Stream,
    3365             :                                              FunctionSummary *FS) {
    3366         208 :   if (!FS->type_tests().empty())
    3367             :     Stream.EmitRecord(bitc::FS_TYPE_TESTS, FS->type_tests());
    3368             : 
    3369             :   SmallVector<uint64_t, 64> Record;
    3370             : 
    3371             :   auto WriteVFuncIdVec = [&](uint64_t Ty,
    3372             :                              ArrayRef<FunctionSummary::VFuncId> VFs) {
    3373             :     if (VFs.empty())
    3374             :       return;
    3375             :     Record.clear();
    3376             :     for (auto &VF : VFs) {
    3377             :       Record.push_back(VF.GUID);
    3378             :       Record.push_back(VF.Offset);
    3379             :     }
    3380             :     Stream.EmitRecord(Ty, Record);
    3381             :   };
    3382             : 
    3383             :   WriteVFuncIdVec(bitc::FS_TYPE_TEST_ASSUME_VCALLS,
    3384             :                   FS->type_test_assume_vcalls());
    3385             :   WriteVFuncIdVec(bitc::FS_TYPE_CHECKED_LOAD_VCALLS,
    3386             :                   FS->type_checked_load_vcalls());
    3387             : 
    3388             :   auto WriteConstVCallVec = [&](uint64_t Ty,
    3389             :                                 ArrayRef<FunctionSummary::ConstVCall> VCs) {
    3390             :     for (auto &VC : VCs) {
    3391             :       Record.clear();
    3392             :       Record.push_back(VC.VFunc.GUID);
    3393         208 :       Record.push_back(VC.VFunc.Offset);
    3394         208 :       Record.insert(Record.end(), VC.Args.begin(), VC.Args.end());
    3395             :       Stream.EmitRecord(Ty, Record);
    3396             :     }
    3397             :   };
    3398        1153 : 
    3399             :   WriteConstVCallVec(bitc::FS_TYPE_TEST_ASSUME_CONST_VCALL,
    3400        1153 :                      FS->type_test_assume_const_vcalls());
    3401          43 :   WriteConstVCallVec(bitc::FS_TYPE_CHECKED_LOAD_CONST_VCALL,
    3402             :                      FS->type_checked_load_const_vcalls());
    3403             : }
    3404             : 
    3405             : /// Collect type IDs from type tests used by function.
    3406             : static void
    3407             : getReferencedTypeIds(FunctionSummary *FS,
    3408             :                      std::set<GlobalValue::GUID> &ReferencedTypeIds) {
    3409             :   if (!FS->type_tests().empty())
    3410             :     for (auto &TT : FS->type_tests())
    3411             :       ReferencedTypeIds.insert(TT);
    3412             : 
    3413             :   auto GetReferencedTypesFromVFuncIdVec =
    3414             :       [&](ArrayRef<FunctionSummary::VFuncId> VFs) {
    3415        1153 :         for (auto &VF : VFs)
    3416             :           ReferencedTypeIds.insert(VF.GUID);
    3417        1153 :       };
    3418             : 
    3419        1153 :   GetReferencedTypesFromVFuncIdVec(FS->type_test_assume_vcalls());
    3420             :   GetReferencedTypesFromVFuncIdVec(FS->type_checked_load_vcalls());
    3421             : 
    3422             :   auto GetReferencedTypesFromConstVCallVec =
    3423             :       [&](ArrayRef<FunctionSummary::ConstVCall> VCs) {
    3424             :         for (auto &VC : VCs)
    3425             :           ReferencedTypeIds.insert(VC.VFunc.GUID);
    3426             :       };
    3427             : 
    3428             :   GetReferencedTypesFromConstVCallVec(FS->type_test_assume_const_vcalls());
    3429             :   GetReferencedTypesFromConstVCallVec(FS->type_checked_load_const_vcalls());
    3430             : }
    3431        1153 : 
    3432             : static void writeWholeProgramDevirtResolutionByArg(
    3433        1153 :     SmallVector<uint64_t, 64> &NameVals, const std::vector<uint64_t> &args,
    3434             :     const WholeProgramDevirtResolution::ByArg &ByArg) {
    3435        1153 :   NameVals.push_back(args.size());
    3436             :   NameVals.insert(NameVals.end(), args.begin(), args.end());
    3437        1153 : 
    3438             :   NameVals.push_back(ByArg.TheKind);
    3439             :   NameVals.push_back(ByArg.Info);
    3440             :   NameVals.push_back(ByArg.Byte);
    3441         654 :   NameVals.push_back(ByArg.Bit);
    3442             : }
    3443         654 : 
    3444          55 : static void writeWholeProgramDevirtResolution(
    3445             :     SmallVector<uint64_t, 64> &NameVals, StringTableBuilder &StrtabBuilder,
    3446             :     uint64_t Id, const WholeProgramDevirtResolution &Wpd) {
    3447             :   NameVals.push_back(Id);
    3448             : 
    3449        1326 :   NameVals.push_back(Wpd.TheKind);
    3450          18 :   NameVals.push_back(StrtabBuilder.add(Wpd.SingleImplName));
    3451             :   NameVals.push_back(Wpd.SingleImplName.size());
    3452             : 
    3453             :   NameVals.push_back(Wpd.ResByArg.size());
    3454             :   for (auto &A : Wpd.ResByArg)
    3455             :     writeWholeProgramDevirtResolutionByArg(NameVals, A.first, A.second);
    3456             : }
    3457             : 
    3458        1323 : static void writeTypeIdSummaryRecord(SmallVector<uint64_t, 64> &NameVals,
    3459          15 :                                      StringTableBuilder &StrtabBuilder,
    3460             :                                      const std::string &Id,
    3461             :                                      const TypeIdSummary &Summary) {
    3462             :   NameVals.push_back(StrtabBuilder.add(Id));
    3463             :   NameVals.push_back(Id.size());
    3464         654 : 
    3465             :   NameVals.push_back(Summary.TTRes.TheKind);
    3466           4 :   NameVals.push_back(Summary.TTRes.SizeM1BitWidth);
    3467             :   NameVals.push_back(Summary.TTRes.AlignLog2);
    3468             :   NameVals.push_back(Summary.TTRes.SizeM1);
    3469           8 :   NameVals.push_back(Summary.TTRes.BitMask);
    3470           8 :   NameVals.push_back(Summary.TTRes.InlineBits);
    3471             : 
    3472           4 :   for (auto &W : Summary.WPDRes)
    3473           4 :     writeWholeProgramDevirtResolution(NameVals, StrtabBuilder, W.first,
    3474           4 :                                       W.second);
    3475           4 : }
    3476           4 : 
    3477             : // Helper to emit a single function summary record.
    3478          17 : void ModuleBitcodeWriterBase::writePerModuleFunctionSummaryRecord(
    3479             :     SmallVector<uint64_t, 64> &NameVals, GlobalValueSummary *Summary,
    3480             :     unsigned ValueID, unsigned FSCallsAbbrev, unsigned FSCallsProfileAbbrev,
    3481          17 :     const Function &F) {
    3482             :   NameVals.push_back(ValueID);
    3483          17 : 
    3484          17 :   FunctionSummary *FS = cast<FunctionSummary>(Summary);
    3485          17 :   writeFunctionTypeMetadataRecords(Stream, FS);
    3486             : 
    3487          17 :   NameVals.push_back(getEncodedGVSummaryFlags(FS->flags()));
    3488          21 :   NameVals.push_back(FS->instCount());
    3489           4 :   NameVals.push_back(getEncodedFFlags(FS->fflags()));
    3490          17 :   NameVals.push_back(FS->refs().size());
    3491             : 
    3492          25 :   for (auto &RI : FS->refs())
    3493             :     NameVals.push_back(VE.getValueID(RI.getValue()));
    3494             : 
    3495             :   bool HasProfileData =
    3496          50 :       F.hasProfileData() || ForceSummaryEdgesCold != FunctionSummary::FSHT_None;
    3497          25 :   for (auto &ECI : FS->calls()) {
    3498             :     NameVals.push_back(getValueId(ECI.first));
    3499          25 :     if (HasProfileData)
    3500          25 :       NameVals.push_back(static_cast<uint8_t>(ECI.second.Hotness));
    3501          25 :     else if (WriteRelBFToSummary)
    3502          25 :       NameVals.push_back(ECI.second.RelBlockFreq);
    3503          25 :   }
    3504          25 : 
    3505             :   unsigned FSAbbrev = (HasProfileData ? FSCallsProfileAbbrev : FSCallsAbbrev);
    3506          42 :   unsigned Code =
    3507          17 :       (HasProfileData ? bitc::FS_PERMODULE_PROFILE
    3508          17 :                       : (WriteRelBFToSummary ? bitc::FS_PERMODULE_RELBF
    3509          25 :                                              : bitc::FS_PERMODULE));
    3510             : 
    3511             :   // Emit the finished record.
    3512         615 :   Stream.EmitRecord(Code, NameVals, FSAbbrev);
    3513             :   NameVals.clear();
    3514             : }
    3515             : 
    3516         615 : // Collect the global value references in the given variable's initializer,
    3517             : // and emit them in a summary record.
    3518             : void ModuleBitcodeWriterBase::writeModuleLevelReferences(
    3519         615 :     const GlobalVariable &V, SmallVector<uint64_t, 64> &NameVals,
    3520             :     unsigned FSModRefsAbbrev) {
    3521         615 :   auto VI = Index->getValueInfo(V.getGUID());
    3522         615 :   if (!VI || VI.getSummaryList().empty()) {
    3523         615 :     // Only declarations should not have a summary (a declaration might however
    3524         615 :     // have a summary if the def was in module level asm).
    3525             :     assert(V.isDeclaration());
    3526         725 :     return;
    3527         220 :   }
    3528             :   auto *Summary = VI.getSummaryList()[0].get();
    3529             :   NameVals.push_back(VE.getValueID(&V));
    3530         615 :   GlobalVarSummary *VS = cast<GlobalVarSummary>(Summary);
    3531         935 :   NameVals.push_back(getEncodedGVSummaryFlags(VS->flags()));
    3532         320 : 
    3533         320 :   unsigned SizeBeforeRefs = NameVals.size();
    3534          49 :   for (auto &RI : VS->refs())
    3535         271 :     NameVals.push_back(VE.getValueID(RI.getValue()));
    3536           2 :   // Sort the refs for determinism output, the vector returned by FS->refs() has
    3537             :   // been initialized from a DenseSet.
    3538             :   llvm::sort(NameVals.begin() + SizeBeforeRefs, NameVals.end());
    3539         615 : 
    3540             :   Stream.EmitRecord(bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS, NameVals,
    3541         615 :                     FSModRefsAbbrev);
    3542             :   NameVals.clear();
    3543             : }
    3544             : 
    3545             : // Current version for the summary.
    3546         615 : // This is bumped whenever we introduce changes in the way some record are
    3547             : // interpreted, like flags for instance.
    3548         615 : static const uint64_t INDEX_VERSION = 4;
    3549             : 
    3550             : /// Emit the per-module summary section alongside the rest of
    3551             : /// the module's bitcode.
    3552         175 : void ModuleBitcodeWriterBase::writePerModuleGlobalValueSummary() {
    3553             :   // By default we compile with ThinLTO if the module has a summary, but the
    3554             :   // client can request full LTO with a module flag.
    3555         175 :   bool IsThinLTO = true;
    3556         175 :   if (auto *MD =
    3557             :           mdconst::extract_or_null<ConstantInt>(M.getModuleFlag("ThinLTO")))
    3558             :     IsThinLTO = MD->getZExtValue();
    3559             :   Stream.EnterSubblock(IsThinLTO ? bitc::GLOBALVAL_SUMMARY_BLOCK_ID
    3560             :                                  : bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID,
    3561             :                        4);
    3562             : 
    3563         126 :   Stream.EmitRecord(bitc::FS_VERSION, ArrayRef<uint64_t>{INDEX_VERSION});
    3564             : 
    3565         126 :   if (Index->begin() == Index->end()) {
    3566             :     Stream.ExitBlock();
    3567         126 :     return;
    3568         187 :   }
    3569          61 : 
    3570             :   for (const auto &GVI : valueIds()) {
    3571             :     Stream.EmitRecord(bitc::FS_VALUE_GUID,
    3572         126 :                       ArrayRef<uint64_t>{GVI.second, GVI.first});
    3573             :   }
    3574         126 : 
    3575             :   // Abbrev for FS_PERMODULE_PROFILE.
    3576             :   auto Abbv = std::make_shared<BitCodeAbbrev>();
    3577             :   Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE_PROFILE));
    3578             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));   // valueid
    3579             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // flags
    3580             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));   // instcount
    3581             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4));   // fflags
    3582             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4));   // numrefs
    3583             :   // numrefs x valueid, n x (valueid, hotness)
    3584             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
    3585             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
    3586         408 :   unsigned FSCallsProfileAbbrev = Stream.EmitAbbrev(std::move(Abbv));
    3587             : 
    3588             :   // Abbrev for FS_PERMODULE or FS_PERMODULE_RELBF.
    3589             :   Abbv = std::make_shared<BitCodeAbbrev>();
    3590          45 :   if (WriteRelBFToSummary)
    3591        1224 :     Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE_RELBF));
    3592          45 :   else
    3593         453 :     Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE));
    3594             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));   // valueid
    3595             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // flags
    3596             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));   // instcount
    3597         816 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4));   // fflags
    3598             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4));   // numrefs
    3599         816 :   // numrefs x valueid, n x (valueid [, rel_block_freq])
    3600          24 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
    3601          24 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
    3602             :   unsigned FSCallsAbbrev = Stream.EmitAbbrev(std::move(Abbv));
    3603             : 
    3604         393 :   // Abbrev for FS_PERMODULE_GLOBALVAR_INIT_REFS.
    3605           9 :   Abbv = std::make_shared<BitCodeAbbrev>();
    3606           9 :   Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS));
    3607             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
    3608             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
    3609             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));  // valueids
    3610             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
    3611         384 :   unsigned FSModRefsAbbrev = Stream.EmitAbbrev(std::move(Abbv));
    3612         384 : 
    3613         384 :   // Abbrev for FS_ALIAS.
    3614         384 :   Abbv = std::make_shared<BitCodeAbbrev>();
    3615         384 :   Abbv->Add(BitCodeAbbrevOp(bitc::FS_ALIAS));
    3616         384 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));   // valueid
    3617             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // flags
    3618         384 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));   // valueid
    3619         384 :   unsigned FSAliasAbbrev = Stream.EmitAbbrev(std::move(Abbv));
    3620        1152 : 
    3621             :   SmallVector<uint64_t, 64> NameVals;
    3622             :   // Iterate over the list of functions instead of the Index to
    3623         384 :   // ensure the ordering is stable.
    3624         384 :   for (const Function &F : M) {
    3625           2 :     // Summary emission does not support anonymous functions, they have to
    3626             :     // renamed using the anonymous function renaming pass.
    3627         382 :     if (!F.hasName())
    3628         384 :       report_fatal_error("Unexpected anonymous function when writing summary");
    3629         384 : 
    3630         384 :     ValueInfo VI = Index->getValueInfo(F.getGUID());
    3631         384 :     if (!VI || VI.getSummaryList().empty()) {
    3632         384 :       // Only declarations should not have a summary (a declaration might
    3633             :       // however have a summary if the def was in module level asm).
    3634         384 :       assert(F.isDeclaration());
    3635         384 :       continue;
    3636        1152 :     }
    3637             :     auto *Summary = VI.getSummaryList()[0].get();
    3638             :     writePerModuleFunctionSummaryRecord(NameVals, Summary, VE.getValueID(&F),
    3639         384 :                                         FSCallsAbbrev, FSCallsProfileAbbrev, F);
    3640         384 :   }
    3641         384 : 
    3642         384 :   // Capture references from GlobalVariable initializers, which are outside
    3643         384 :   // of a function scope.
    3644         384 :   for (const GlobalVariable &G : M.globals())
    3645        1152 :     writeModuleLevelReferences(G, NameVals, FSModRefsAbbrev);
    3646             : 
    3647             :   for (const GlobalAlias &A : M.aliases()) {
    3648         384 :     auto *Aliasee = A.getBaseObject();
    3649         384 :     if (!Aliasee->hasName())
    3650         384 :       // Nameless function don't have an entry in the summary, skip it.
    3651         384 :       continue;
    3652         384 :     auto AliasId = VE.getValueID(&A);
    3653        1152 :     auto AliaseeId = VE.getValueID(Aliasee);
    3654             :     NameVals.push_back(AliasId);
    3655             :     auto *Summary = Index->getGlobalValueSummary(A);
    3656             :     AliasSummary *AS = cast<AliasSummary>(Summary);
    3657             :     NameVals.push_back(getEncodedGVSummaryFlags(AS->flags()));
    3658        1332 :     NameVals.push_back(AliaseeId);
    3659             :     Stream.EmitRecord(bitc::FS_ALIAS, NameVals, FSAliasAbbrev);
    3660             :     NameVals.clear();
    3661         948 :   }
    3662           0 : 
    3663             :   Stream.ExitBlock();
    3664         948 : }
    3665         948 : 
    3666             : /// Emit the combined summary section into the combined index file.
    3667             : void IndexBitcodeWriter::writeCombinedGlobalValueSummary() {
    3668             :   Stream.EnterSubblock(bitc::GLOBALVAL_SUMMARY_BLOCK_ID, 3);
    3669             :   Stream.EmitRecord(bitc::FS_VERSION, ArrayRef<uint64_t>{INDEX_VERSION});
    3670             : 
    3671             :   // Write the index flags.
    3672         615 :   uint64_t Flags = 0;
    3673             :   if (Index.withGlobalValueDeadStripping())
    3674             :     Flags |= 0x1;
    3675             :   if (Index.skipModuleByDistributedBackend())
    3676             :     Flags |= 0x2;
    3677             :   Stream.EmitRecord(bitc::FS_FLAGS, ArrayRef<uint64_t>{Flags});
    3678         559 : 
    3679         175 :   for (const auto &GVI : valueIds()) {
    3680             :     Stream.EmitRecord(bitc::FS_VALUE_GUID,
    3681         508 :                       ArrayRef<uint64_t>{GVI.second, GVI.first});
    3682         124 :   }
    3683         124 : 
    3684             :   // Abbrev for FS_COMBINED.
    3685             :   auto Abbv = std::make_shared<BitCodeAbbrev>();
    3686         124 :   Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED));
    3687         124 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));   // valueid
    3688         124 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));   // modid
    3689         124 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // flags
    3690             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));   // instcount
    3691         124 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4));   // fflags
    3692         124 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4));   // numrefs
    3693         124 :   // numrefs x valueid, n x (valueid)
    3694             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
    3695             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
    3696             :   unsigned FSCallsAbbrev = Stream.EmitAbbrev(std::move(Abbv));
    3697         384 : 
    3698             :   // Abbrev for FS_COMBINED_PROFILE.
    3699             :   Abbv = std::make_shared<BitCodeAbbrev>();
    3700             :   Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED_PROFILE));
    3701         208 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));   // valueid
    3702         208 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));   // modid
    3703         416 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // flags
    3704             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));   // instcount
    3705             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4));   // fflags
    3706             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4));   // numrefs
    3707         208 :   // numrefs x valueid, n x (valueid, hotness)
    3708             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
    3709         208 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
    3710           2 :   unsigned FSCallsProfileAbbrev = Stream.EmitAbbrev(std::move(Abbv));
    3711         416 : 
    3712             :   // Abbrev for FS_COMBINED_GLOBALVAR_INIT_REFS.
    3713         912 :   Abbv = std::make_shared<BitCodeAbbrev>();
    3714         704 :   Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED_GLOBALVAR_INIT_REFS));
    3715         704 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));   // valueid
    3716             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));   // modid
    3717             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // flags
    3718             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));    // valueids
    3719             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
    3720         208 :   unsigned FSModRefsAbbrev = Stream.EmitAbbrev(std::move(Abbv));
    3721         208 : 
    3722         208 :   // Abbrev for FS_COMBINED_ALIAS.
    3723         208 :   Abbv = std::make_shared<BitCodeAbbrev>();
    3724         208 :   Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED_ALIAS));
    3725         208 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));   // valueid
    3726         208 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));   // modid
    3727             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // flags
    3728         208 :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));   // valueid
    3729         208 :   unsigned FSAliasAbbrev = Stream.EmitAbbrev(std::move(Abbv));
    3730         416 : 
    3731             :   // The aliases are emitted as a post-pass, and will point to the value
    3732             :   // id of the aliasee. Save them in a vector for post-processing.
    3733         208 :   SmallVector<AliasSummary *, 64> Aliases;
    3734         208 : 
    3735         208 :   // Save the value id for each summary for alias emission.
    3736         208 :   DenseMap<const GlobalValueSummary *, unsigned> SummaryToValueIdMap;
    3737         208 : 
    3738         208 :   SmallVector<uint64_t, 64> NameVals;
    3739         208 : 
    3740         208 :   // Set that will be populated during call to writeFunctionTypeMetadataRecords
    3741             :   // with the type ids referenced by this index file.
    3742         208 :   std::set<GlobalValue::GUID> ReferencedTypeIds;
    3743         208 : 
    3744         416 :   // For local linkage, we also emit the original name separately
    3745             :   // immediately after the record.
    3746             :   auto MaybeEmitOriginalName = [&](GlobalValueSummary &S) {
    3747         208 :     if (!GlobalValue::isLocalLinkage(S.linkage()))
    3748         208 :       return;
    3749         208 :     NameVals.push_back(S.getOriginalName());
    3750         208 :     Stream.EmitRecord(bitc::FS_COMBINED_ORIGINAL_NAME, NameVals);
    3751         208 :     NameVals.clear();
    3752         208 :   };
    3753         208 : 
    3754         416 :   forEachSummary([&](GVInfo I, bool IsAliasee) {
    3755             :     GlobalValueSummary *S = I.second;
    3756             :     assert(S);
    3757         208 : 
    3758         208 :     auto ValueId = getValueId(I.first);
    3759         208 :     assert(ValueId);
    3760         208 :     SummaryToValueIdMap[S] = *ValueId;
    3761         208 : 
    3762         208 :     // If this is invoked for an aliasee, we want to record the above
    3763         624 :     // mapping, but then not emit a summary entry (if the aliasee is
    3764             :     // to be imported, we will invoke this separately with IsAliasee=false).
    3765             :     if (IsAliasee)
    3766             :       return;
    3767             : 
    3768             :     if (auto *AS = dyn_cast<AliasSummary>(S)) {
    3769             :       // Will process aliases as a post-pass because the reader wants all
    3770             :       // global to be loaded first.
    3771             :       Aliases.push_back(AS);
    3772             :       return;
    3773             :     }
    3774             : 
    3775             :     if (auto *VS = dyn_cast<GlobalVarSummary>(S)) {
    3776             :       NameVals.push_back(*ValueId);
    3777             :       NameVals.push_back(Index.getModuleId(VS->modulePath()));
    3778             :       NameVals.push_back(getEncodedGVSummaryFlags(VS->flags()));
    3779             :       for (auto &RI : VS->refs()) {
    3780             :         auto RefValueId = getValueId(RI.getGUID());
    3781             :         if (!RefValueId)
    3782             :           continue;
    3783             :         NameVals.push_back(*RefValueId);
    3784             :       }
    3785             : 
    3786         208 :       // Emit the finished record.
    3787             :       Stream.EmitRecord(bitc::FS_COMBINED_GLOBALVAR_INIT_REFS, NameVals,
    3788         208 :                         FSModRefsAbbrev);
    3789             :       NameVals.clear();
    3790             :       MaybeEmitOriginalName(*S);
    3791             :       return;
    3792             :     }
    3793             : 
    3794             :     auto *FS = cast<FunctionSummary>(S);
    3795             :     writeFunctionTypeMetadataRecords(Stream, FS);
    3796             :     getReferencedTypeIds(FS, ReferencedTypeIds);
    3797             : 
    3798             :     NameVals.push_back(*ValueId);
    3799             :     NameVals.push_back(Index.getModuleId(FS->modulePath()));
    3800             :     NameVals.push_back(getEncodedGVSummaryFlags(FS->flags()));
    3801             :     NameVals.push_back(FS->instCount());
    3802             :     NameVals.push_back(getEncodedFFlags(FS->fflags()));
    3803             :     // Fill in below
    3804             :     NameVals.push_back(0);
    3805             : 
    3806             :     unsigned Count = 0;
    3807             :     for (auto &RI : FS->refs()) {
    3808             :       auto RefValueId = getValueId(RI.getGUID());
    3809             :       if (!RefValueId)
    3810             :         continue;
    3811             :       NameVals.push_back(*RefValueId);
    3812             :       Count++;
    3813             :     }
    3814             :     NameVals[5] = Count;
    3815             : 
    3816             :     bool HasProfileData = false;
    3817             :     for (auto &EI : FS->calls()) {
    3818             :       HasProfileData |=
    3819             :           EI.second.getHotness() != CalleeInfo::HotnessType::Unknown;
    3820             :       if (HasProfileData)
    3821             :         break;
    3822             :     }
    3823             : 
    3824             :     for (auto &EI : FS->calls()) {
    3825             :       // If this GUID doesn't have a value id, it doesn't have a function
    3826             :       // summary and we don't need to record any calls to it.
    3827             :       GlobalValue::GUID GUID = EI.first.getGUID();
    3828             :       auto CallValueId = getValueId(GUID);
    3829             :       if (!CallValueId) {
    3830             :         // For SamplePGO, the indirect call targets for local functions will
    3831             :         // have its original name annotated in profile. We try to find the
    3832             :         // corresponding PGOFuncName as the GUID.
    3833             :         GUID = Index.getGUIDFromOriginalID(GUID);
    3834             :         if (GUID == 0)
    3835             :           continue;
    3836             :         CallValueId = getValueId(GUID);
    3837             :         if (!CallValueId)
    3838             :           continue;
    3839             :         // The mapping from OriginalId to GUID may return a GUID
    3840             :         // that corresponds to a static variable. Filter it out here.
    3841             :         // This can happen when
    3842             :         // 1) There is a call to a library function which does not have
    3843             :         // a CallValidId;
    3844             :         // 2) There is a static variable with the  OriginalGUID identical
    3845             :         // to the GUID of the library function in 1);
    3846             :         // When this happens, the logic for SamplePGO kicks in and
    3847             :         // the static variable in 2) will be found, which needs to be
    3848             :         // filtered out.
    3849             :         auto *GVSum = Index.getGlobalValueSummary(GUID, false);
    3850             :         if (GVSum &&
    3851             :             GVSum->getSummaryKind() == GlobalValueSummary::GlobalVarKind)
    3852             :           continue;
    3853             :       }
    3854             :       NameVals.push_back(*CallValueId);
    3855             :       if (HasProfileData)
    3856             :         NameVals.push_back(static_cast<uint8_t>(EI.second.Hotness));
    3857             :     }
    3858             : 
    3859             :     unsigned FSAbbrev = (HasProfileData ? FSCallsProfileAbbrev : FSCallsAbbrev);
    3860             :     unsigned Code =
    3861             :         (HasProfileData ? bitc::FS_COMBINED_PROFILE : bitc::FS_COMBINED);
    3862             : 
    3863             :     // Emit the finished record.
    3864             :     Stream.EmitRecord(Code, NameVals, FSAbbrev);
    3865             :     NameVals.clear();
    3866             :     MaybeEmitOriginalName(*S);
    3867             :   });
    3868             : 
    3869             :   for (auto *AS : Aliases) {
    3870             :     auto AliasValueId = SummaryToValueIdMap[AS];
    3871             :     assert(AliasValueId);
    3872             :     NameVals.push_back(AliasValueId);
    3873             :     NameVals.push_back(Index.getModuleId(AS->modulePath()));
    3874             :     NameVals.push_back(getEncodedGVSummaryFlags(AS->flags()));
    3875             :     auto AliaseeValueId = SummaryToValueIdMap[&AS->getAliasee()];
    3876             :     assert(AliaseeValueId);
    3877             :     NameVals.push_back(AliaseeValueId);
    3878             : 
    3879             :     // Emit the finished record.
    3880             :     Stream.EmitRecord(bitc::FS_COMBINED_ALIAS, NameVals, FSAliasAbbrev);
    3881             :     NameVals.clear();
    3882             :     MaybeEmitOriginalName(*AS);
    3883             : 
    3884             :     if (auto *FS = dyn_cast<FunctionSummary>(&AS->getAliasee()))
    3885             :       getReferencedTypeIds(FS, ReferencedTypeIds);
    3886             :   }
    3887             : 
    3888             :   if (!Index.cfiFunctionDefs().empty()) {
    3889             :     for (auto &S : Index.cfiFunctionDefs()) {
    3890             :       NameVals.push_back(StrtabBuilder.add(S));
    3891             :       NameVals.push_back(S.size());
    3892             :     }
    3893             :     Stream.EmitRecord(bitc::FS_CFI_FUNCTION_DEFS, NameVals);
    3894             :     NameVals.clear();
    3895             :   }
    3896             : 
    3897             :   if (!Index.cfiFunctionDecls().empty()) {
    3898             :     for (auto &S : Index.cfiFunctionDecls()) {
    3899             :       NameVals.push_back(StrtabBuilder.add(S));
    3900             :       NameVals.push_back(S.size());
    3901             :     }
    3902             :     Stream.EmitRecord(bitc::FS_CFI_FUNCTION_DECLS, NameVals);
    3903         328 :     NameVals.clear();
    3904         120 :   }
    3905             : 
    3906         120 :   // Walk the GUIDs that were referenced, and write the
    3907         120 :   // corresponding type id records.
    3908         120 :   for (auto &T : ReferencedTypeIds) {
    3909         120 :     auto TidIter = Index.typeIds().equal_range(T);
    3910             :     for (auto It = TidIter.first; It != TidIter.second; ++It) {
    3911         120 :       writeTypeIdSummaryRecord(NameVals, StrtabBuilder, It->second.first,
    3912             :                                It->second.second);
    3913             :       Stream.EmitRecord(bitc::FS_TYPE_ID, NameVals);
    3914         120 :       NameVals.clear();
    3915             :     }
    3916         120 :   }
    3917             : 
    3918             :   Stream.ExitBlock();
    3919         116 : }
    3920             : 
    3921             : /// Create the "IDENTIFICATION_BLOCK_ID" containing a single string with the
    3922         416 : /// current llvm version, and a record for the epoch number.
    3923           6 : static void writeIdentificationBlock(BitstreamWriter &Stream) {
    3924           6 :   Stream.EnterSubblock(bitc::IDENTIFICATION_BLOCK_ID, 5);
    3925           3 : 
    3926             :   // Write the "user readable" string identifying the bitcode producer
    3927           3 :   auto Abbv = std::make_shared<BitCodeAbbrev>();
    3928             :   Abbv->Add(BitCodeAbbrevOp(bitc::IDENTIFICATION_CODE_STRING));
    3929             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
    3930             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
    3931         416 :   auto StringAbbrev = Stream.EmitAbbrev(std::move(Abbv));
    3932           4 :   writeStringRecord(Stream, bitc::IDENTIFICATION_CODE_STRING,
    3933           4 :                     "LLVM" LLVM_VERSION_STRING, StringAbbrev);
    3934           2 : 
    3935             :   // Write the epoch version
    3936           2 :   Abbv = std::make_shared<BitCodeAbbrev>();
    3937             :   Abbv->Add(BitCodeAbbrevOp(bitc::IDENTIFICATION_CODE_EPOCH));
    3938             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
    3939             :   auto EpochAbbrev = Stream.EmitAbbrev(std::move(Abbv));
    3940             :   SmallVector<unsigned, 1> Vals = {bitc::BITCODE_CURRENT_EPOCH};
    3941             :   Stream.EmitRecord(bitc::IDENTIFICATION_CODE_EPOCH, Vals, EpochAbbrev);
    3942         243 :   Stream.ExitBlock();
    3943          35 : }
    3944          60 : 
    3945          25 : void ModuleBitcodeWriter::writeModuleHash(size_t BlockStartPos) {
    3946          25 :   // Emit the module's hash.
    3947          25 :   // MODULE_CODE_HASH: [5*i32]
    3948             :   if (GenerateHash) {
    3949             :     uint32_t Vals[5];
    3950             :     Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&(Buffer)[BlockStartPos],
    3951             :                                     Buffer.size() - BlockStartPos));
    3952         208 :     StringRef Hash = Hasher.result();
    3953         208 :     for (int Pos = 0; Pos < 20; Pos += 4) {
    3954             :       Vals[Pos / 4] = support::endian::read32be(Hash.data() + Pos);
    3955             :     }
    3956             : 
    3957        4708 :     // Emit the finished record.
    3958        4708 :     Stream.EmitRecord(bitc::MODULE_CODE_HASH, Vals);
    3959             : 
    3960             :     if (ModHash)
    3961             :       // Save the written hash value.
    3962        4708 :       std::copy(std::begin(Vals), std::end(Vals), std::begin(*ModHash));
    3963        4708 :   }
    3964        4708 : }
    3965        9416 : 
    3966        4708 : void ModuleBitcodeWriter::write() {
    3967             :   writeIdentificationBlock(Stream);
    3968             : 
    3969             :   Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
    3970        4708 :   size_t BlockStartPos = Buffer.size();
    3971        4708 : 
    3972        4708 :   writeModuleVersion();
    3973        9416 : 
    3974             :   // Emit blockinfo, which defines the standard abbreviations etc.
    3975        4708 :   writeBlockInfo();
    3976        4708 : 
    3977        4708 :   // Emit information about attribute groups.
    3978             :   writeAttributeGroupTable();
    3979        4708 : 
    3980             :   // Emit information about parameter attributes.
    3981             :   writeAttributeTable();
    3982        4708 : 
    3983             :   // Emit information describing all of the types in the module.
    3984         188 :   writeTypeTable();
    3985          94 : 
    3986          94 :   writeComdats();
    3987         564 : 
    3988         940 :   // Emit top-level description of module, including target triple, inline asm,
    3989             :   // descriptors for global variables, and function prototype info.
    3990             :   writeModuleInfo();
    3991             : 
    3992          94 :   // Emit constants.
    3993             :   writeModuleConstants();
    3994          94 : 
    3995             :   // Emit metadata kind names.
    3996             :   writeModuleMetadataKinds();
    3997             : 
    3998        4708 :   // Emit metadata.
    3999             :   writeModuleMetadata();
    4000        4708 : 
    4001        4708 :   // Emit module-level use-lists.
    4002             :   if (VE.shouldPreserveUseListOrder())
    4003        4708 :     writeUseListBlock(nullptr);
    4004        4708 : 
    4005             :   writeOperandBundleTags();
    4006        4708 :   writeSyncScopeNames();
    4007             : 
    4008             :   // Emit function bodies.
    4009        4708 :   DenseMap<const Function *, uint64_t> FunctionToBitcodeIndex;
    4010             :   for (Module::const_iterator F = M.begin(), E = M.end(); F != E; ++F)
    4011             :     if (!F->isDeclaration())
    4012        4708 :       writeFunction(*F, FunctionToBitcodeIndex);
    4013             : 
    4014             :   // Need to write after the above call to WriteFunction which populates
    4015        4708 :   // the summary information in the index.
    4016             :   if (Index)
    4017             :     writePerModuleGlobalValueSummary();
    4018        4708 : 
    4019             :   writeGlobalValueSymbolTable(FunctionToBitcodeIndex);
    4020        4708 : 
    4021             :   writeModuleHash(BlockStartPos);
    4022             : 
    4023             :   Stream.ExitBlock();
    4024        4708 : }
    4025             : 
    4026             : static void writeInt32ToBuffer(uint32_t Value, SmallVectorImpl<char> &Buffer,
    4027        4708 :                                uint32_t &Position) {
    4028             :   support::endian::write32le(&Buffer[Position], Value);
    4029             :   Position += 4;
    4030        4708 : }
    4031             : 
    4032             : /// If generating a bc file on darwin, we have to emit a
    4033        4708 : /// header and trailer to make it compatible with the system archiver.  To do
    4034             : /// this we emit the following header, and then emit a trailer that pads the
    4035             : /// file out to be a multiple of 16 bytes.
    4036        4708 : ///
    4037        2787 : /// struct bc_header {
    4038             : ///   uint32_t Magic;         // 0x0B17C0DE
    4039        4708 : ///   uint32_t Version;       // Version, currently always 0.
    4040        4708 : ///   uint32_t BitcodeOffset; // Offset to traditional bitcode file.
    4041             : ///   uint32_t BitcodeSize;   // Size of traditional bitcode file.
    4042             : ///   uint32_t CPUType;       // CPU specifier.
    4043             : ///   ... potentially more later ...
    4044       21309 : /// };
    4045       16601 : static void emitDarwinBCHeaderAndTrailer(SmallVectorImpl<char> &Buffer,
    4046       12178 :                                          const Triple &TT) {
    4047             :   unsigned CPUType = ~0U;
    4048             : 
    4049             :   // Match x86_64-*, i[3-9]86-*, powerpc-*, powerpc64-*, arm-*, thumb-*,
    4050        4708 :   // armv[0-9]-*, thumbv[0-9]-*, armv5te-*, or armv6t2-*. The CPUType is a magic
    4051         399 :   // number from /usr/include/mach/machine.h.  It is ok to reproduce the
    4052             :   // specific constants here because they are implicitly part of the Darwin ABI.
    4053        4708 :   enum {
    4054             :     DARWIN_CPU_ARCH_ABI64      = 0x01000000,
    4055        4708 :     DARWIN_CPU_TYPE_X86        = 7,
    4056             :     DARWIN_CPU_TYPE_ARM        = 12,
    4057        4708 :     DARWIN_CPU_TYPE_POWERPC    = 18
    4058        4708 :   };
    4059             : 
    4060             :   Triple::ArchType Arch = TT.getArch();
    4061             :   if (Arch == Triple::x86_64)
    4062             :     CPUType = DARWIN_CPU_TYPE_X86 | DARWIN_CPU_ARCH_ABI64;
    4063             :   else if (Arch == Triple::x86)
    4064             :     CPUType = DARWIN_CPU_TYPE_X86;
    4065             :   else if (Arch == Triple::ppc)
    4066             :     CPUType = DARWIN_CPU_TYPE_POWERPC;
    4067             :   else if (Arch == Triple::ppc64)
    4068             :     CPUType = DARWIN_CPU_TYPE_POWERPC | DARWIN_CPU_ARCH_ABI64;
    4069             :   else if (Arch == Triple::arm || Arch == Triple::thumb)
    4070             :     CPUType = DARWIN_CPU_TYPE_ARM;
    4071             : 
    4072             :   // Traditional Bitcode starts after header.
    4073             :   assert(Buffer.size() >= BWH_HeaderSize &&
    4074             :          "Expected header size to be reserved");
    4075             :   unsigned BCOffset = BWH_HeaderSize;
    4076             :   unsigned BCSize = Buffer.size() - BWH_HeaderSize;
    4077             : 
    4078             :   // Write the magic and version.
    4079         244 :   unsigned Position = 0;
    4080             :   writeInt32ToBuffer(0x0B17C0DE, Buffer, Position);
    4081             :   writeInt32ToBuffer(0, Buffer, Position); // Version.
    4082             :   writeInt32ToBuffer(BCOffset, Buffer, Position);
    4083             :   writeInt32ToBuffer(BCSize, Buffer, Position);
    4084             :   writeInt32ToBuffer(CPUType, Buffer, Position);
    4085             : 
    4086             :   // If the file is not a multiple of 16 bytes, insert dummy padding.
    4087             :   while (Buffer.size() & 15)
    4088             :     Buffer.push_back(0);
    4089             : }
    4090             : 
    4091             : /// Helper to write the header common to all bitcode files.
    4092             : static void writeBitcodeHeader(BitstreamWriter &Stream) {
    4093             :   // Emit the file header.
    4094         244 :   Stream.Emit((unsigned)'B', 8);
    4095         244 :   Stream.Emit((unsigned)'C', 8);
    4096             :   Stream.Emit(0x0, 4);
    4097          44 :   Stream.Emit(0xC, 4);
    4098             :   Stream.Emit(0xE, 4);
    4099          11 :   Stream.Emit(0xD, 4);
    4100             : }
    4101           7 : 
    4102             : BitcodeWriter::BitcodeWriter(SmallVectorImpl<char> &Buffer)
    4103           7 :     : Buffer(Buffer), Stream(new BitstreamWriter(Buffer)) {
    4104             :   writeBitcodeHeader(*Stream);
    4105             : }
    4106             : 
    4107             : BitcodeWriter::~BitcodeWriter() { assert(WroteStrtab); }
    4108             : 
    4109             : void BitcodeWriter::writeBlob(unsigned Block, unsigned Record, StringRef Blob) {
    4110         244 :   Stream->EnterSubblock(Block, 3);
    4111             : 
    4112             :   auto Abbv = std::make_shared<BitCodeAbbrev>();
    4113             :   Abbv->Add(BitCodeAbbrevOp(Record));
    4114             :   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
    4115             :   auto AbbrevNo = Stream->EmitAbbrev(std::move(Abbv));
    4116             : 
    4117             :   Stream->EmitRecordWithBlob(AbbrevNo, ArrayRef<uint64_t>{Record}, Blob);
    4118             : 
    4119             :   Stream->ExitBlock();
    4120             : }
    4121        1668 : 
    4122        1424 : void BitcodeWriter::writeSymtab() {
    4123         244 :   assert(!WroteStrtab && !WroteSymtab);
    4124             : 
    4125             :   // If any module has module-level inline asm, we will require a registered asm
    4126        4922 :   // parser for the target so that we can create an accurate symbol table for
    4127             :   // the module.
    4128        4922 :   for (Module *M : Mods) {
    4129        4922 :     if (M->getModuleInlineAsm().empty())
    4130        4922 :       continue;
    4131        4922 : 
    4132        4922 :     std::string Err;
    4133        4922 :     const Triple TT(M->getTargetTriple());
    4134        4922 :     const Target *T = TargetRegistry::lookupTarget(TT.str(), Err);
    4135             :     if (!T || !T->hasMCAsmParser())
    4136        4922 :       return;
    4137        4922 :   }
    4138        4922 : 
    4139        4922 :   WroteSymtab = true;
    4140             :   SmallVector<char, 0> Symtab;
    4141        9608 :   // The irsymtab::build function may be unable to create a symbol table if the
    4142             :   // module is malformed (e.g. it contains an invalid alias). Writing a symbol
    4143        7372 :   // table is not required for correctness, but we still want to be able to
    4144        7372 :   // write malformed modules to bitcode files, so swallow the error.
    4145             :   if (Error E = irsymtab::build(Mods, Symtab, StrtabBuilder, Alloc)) {
    4146             :     consumeError(std::move(E));
    4147        7372 :     return;
    4148        7372 :   }
    4149       14744 : 
    4150             :   writeBlob(bitc::SYMTAB_BLOCK_ID, bitc::SYMTAB_BLOB,
    4151        7372 :             {Symtab.data(), Symtab.size()});
    4152             : }
    4153        7372 : 
    4154        7372 : void BitcodeWriter::writeStrtab() {
    4155             :   assert(!WroteStrtab);
    4156        4683 : 
    4157             :   std::vector<char> Strtab;
    4158             :   StrtabBuilder.finalizeInOrder();
    4159             :   Strtab.resize(StrtabBuilder.getSize());
    4160             :   StrtabBuilder.write((uint8_t *)Strtab.data());
    4161             : 
    4162        9360 :   writeBlob(bitc::STRTAB_BLOCK_ID, bitc::STRTAB_BLOB,
    4163        4711 :             {Strtab.data(), Strtab.size()});
    4164        4623 : 
    4165             :   WroteStrtab = true;
    4166             : }
    4167          88 : 
    4168          88 : void BitcodeWriter::copyStrtab(StringRef Strtab) {
    4169          88 :   writeBlob(bitc::STRTAB_BLOCK_ID, bitc::STRTAB_BLOB, Strtab);
    4170             :   WroteStrtab = true;
    4171             : }
    4172             : 
    4173        4649 : void BitcodeWriter::writeModule(const Module &M,
    4174             :                                 bool ShouldPreserveUseListOrder,
    4175             :                                 const ModuleSummaryIndex *Index,
    4176             :                                 bool GenerateHash, ModuleHash *ModHash) {
    4177             :   assert(!WroteStrtab);
    4178             : 
    4179       11504 :   // The Mods vector is used by irsymtab::build, which requires non-const
    4180        4412 :   // Modules in case it needs to materialize metadata. But the bitcode writer
    4181             :   // requires that the module is materialized, so we can cast to non-const here,
    4182             :   // after checking that it is in fact materialized.
    4183             :   assert(M.isMaterialized());
    4184        2443 :   Mods.push_back(const_cast<Module *>(&M));
    4185        2443 : 
    4186             :   ModuleBitcodeWriter ModuleWriter(M, Buffer, StrtabBuilder, *Stream,
    4187             :                                    ShouldPreserveUseListOrder, Index,
    4188        4895 :                                    GenerateHash, ModHash);
    4189             :   ModuleWriter.write();
    4190             : }
    4191             : 
    4192        4895 : void BitcodeWriter::writeIndex(
    4193        4895 :     const ModuleSummaryIndex *Index,
    4194        4895 :     const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex) {
    4195             :   IndexBitcodeWriter IndexWriter(*Stream, StrtabBuilder, *Index,
    4196        4895 :                                  ModuleToSummariesForIndex);
    4197        4895 :   IndexWriter.write();
    4198             : }
    4199        4895 : 
    4200        4895 : /// Write the specified module to the specified output stream.
    4201             : void llvm::WriteBitcodeToFile(const Module &M, raw_ostream &Out,
    4202          34 :                               bool ShouldPreserveUseListOrder,
    4203          34 :                               const ModuleSummaryIndex *Index,
    4204          34 :                               bool GenerateHash, ModuleHash *ModHash) {
    4205          34 :   SmallVector<char, 0> Buffer;
    4206             :   Buffer.reserve(256*1024);
    4207        4708 : 
    4208             :   // If this is darwin or another generic macho target, reserve space for the
    4209             :   // header.
    4210             :   Triple TT(M.getTargetTriple());
    4211             :   if (TT.isOSDarwin() || TT.isOSBinFormatMachO())
    4212             :     Buffer.insert(Buffer.begin(), BWH_HeaderSize, 0);
    4213             : 
    4214             :   BitcodeWriter Writer(Buffer);
    4215             :   Writer.writeModule(M, ShouldPreserveUseListOrder, Index, GenerateHash,
    4216             :                      ModHash);
    4217             :   Writer.writeSymtab();
    4218        4708 :   Writer.writeStrtab();
    4219             : 
    4220        4708 :   if (TT.isOSDarwin() || TT.isOSBinFormatMachO())
    4221             :     emitDarwinBCHeaderAndTrailer(Buffer, TT);
    4222        9416 : 
    4223        4708 :   // Write the generated bitstream to "Out".
    4224        4708 :   Out.write((char*)&Buffer.front(), Buffer.size());
    4225             : }
    4226         208 : 
    4227             : void IndexBitcodeWriter::write() {
    4228             :   Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
    4229         208 : 
    4230             :   writeModuleVersion();
    4231         208 : 
    4232         208 :   // Write the module paths in the combined index.
    4233             :   writeModStrings();
    4234             : 
    4235        4623 :   // Write the summary combined index records.
    4236             :   writeCombinedGlobalValueSummary();
    4237             : 
    4238             :   Stream.ExitBlock();
    4239             : }
    4240             : 
    4241             : // Write the specified module summary index to the given raw output stream,
    4242             : // where it will be written in a new bitcode block. This is used when
    4243             : // writing the combined index file for ThinLTO. When writing a subset of the
    4244        4623 : // index for a distributed backend, provide a \p ModuleToSummariesForIndex map.
    4245        4380 : void llvm::WriteIndexToFile(
    4246         488 :     const ModuleSummaryIndex &Index, raw_ostream &Out,
    4247             :     const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex) {
    4248        9246 :   SmallVector<char, 0> Buffer;
    4249        4623 :   Buffer.reserve(256 * 1024);
    4250             : 
    4251        4623 :   BitcodeWriter Writer(Buffer);
    4252        4623 :   Writer.writeIndex(&Index, ModuleToSummariesForIndex);
    4253             :   Writer.writeStrtab();
    4254        4380 : 
    4255         244 :   Out.write((char *)&Buffer.front(), Buffer.size());
    4256             : }
    4257             : 
    4258        9246 : namespace {
    4259        4623 : 
    4260             : /// Class to manage the bitcode writing for a thin link bitcode file.
    4261         208 : class ThinLinkBitcodeWriter : public ModuleBitcodeWriterBase {
    4262         208 :   /// ModHash is for use in ThinLTO incremental build, generated while writing
    4263             :   /// the module bitcode file.
    4264         208 :   const ModuleHash *ModHash;
    4265             : 
    4266             : public:
    4267         208 :   ThinLinkBitcodeWriter(const Module &M, StringTableBuilder &StrtabBuilder,
    4268             :                         BitstreamWriter &Stream,
    4269             :                         const ModuleSummaryIndex &Index,
    4270         208 :                         const ModuleHash &ModHash)
    4271             :       : ModuleBitcodeWriterBase(M, StrtabBuilder, Stream,
    4272         208 :                                 /*ShouldPreserveUseListOrder=*/false, &Index),
    4273         208 :         ModHash(&ModHash) {}
    4274             : 
    4275             :   void write();
    4276             : 
    4277             : private:
    4278             :   void writeSimplifiedModuleInfo();
    4279         208 : };
    4280             : 
    4281             : } // end anonymous namespace
    4282             : 
    4283             : // This function writes a simpilified module info for thin link bitcode file.
    4284             : // It only contains the source file name along with the name(the offset and
    4285         416 : // size in strtab) and linkage for global values. For the global value info
    4286         208 : // entry, in order to keep linkage at offset 5, there are three zeros used
    4287         208 : // as padding.
    4288             : void ThinLinkBitcodeWriter::writeSimplifiedModuleInfo() {
    4289         416 :   SmallVector<unsigned, 64> Vals;
    4290         208 :   // Emit the module's source file name.
    4291             :   {
    4292             :     StringEncoding Bits = getStringEncoding(M.getSourceFileName());
    4293             :     BitCodeAbbrevOp AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8);
    4294             :     if (Bits == SE_Char6)
    4295             :       AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Char6);
    4296             :     else if (Bits == SE_Fixed7)
    4297             :       AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7);
    4298             : 
    4299             :     // MODULE_CODE_SOURCE_FILENAME: [namechar x N]
    4300             :     auto Abbv = std::make_shared<BitCodeAbbrev>();
    4301             :     Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_SOURCE_FILENAME));
    4302             :     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
    4303             :     Abbv->Add(AbbrevOpToUse);
    4304             :     unsigned FilenameAbbrev = Stream.EmitAbbrev(std::move(Abbv));
    4305           9 : 
    4306             :     for (const auto P : M.getSourceFileName())
    4307          18 :       Vals.push_back((unsigned char)P);
    4308             : 
    4309             :     Stream.EmitRecord(bitc::MODULE_CODE_SOURCE_FILENAME, Vals, FilenameAbbrev);
    4310             :     Vals.clear();
    4311             :   }
    4312             : 
    4313             :   // Emit the global variable information.
    4314             :   for (const GlobalVariable &GV : M.globals()) {
    4315             :     // GLOBALVAR: [strtab offset, strtab size, 0, 0, 0, linkage]
    4316             :     Vals.push_back(StrtabBuilder.add(GV.getName()));
    4317             :     Vals.push_back(GV.getName().size());
    4318             :     Vals.push_back(0);
    4319             :     Vals.push_back(0);
    4320             :     Vals.push_back(0);
    4321             :     Vals.push_back(getEncodedLinkage(GV));
    4322           9 : 
    4323             :     Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals);
    4324             :     Vals.clear();
    4325             :   }
    4326          18 : 
    4327             :   // Emit the function proto information.
    4328           9 :   for (const Function &F : M) {
    4329           0 :     // FUNCTION:  [strtab offset, strtab size, 0, 0, 0, linkage]
    4330           9 :     Vals.push_back(StrtabBuilder.add(F.getName()));
    4331           9 :     Vals.push_back(F.getName().size());
    4332             :     Vals.push_back(0);
    4333             :     Vals.push_back(0);
    4334             :     Vals.push_back(0);
    4335           9 :     Vals.push_back(getEncodedLinkage(F));
    4336           9 : 
    4337             :     Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals);
    4338          18 :     Vals.clear();
    4339             :   }
    4340         797 : 
    4341         788 :   // Emit the alias information.
    4342             :   for (const GlobalAlias &A : M.aliases()) {
    4343           9 :     // ALIAS: [strtab offset, strtab size, 0, 0, 0, linkage]
    4344             :     Vals.push_back(StrtabBuilder.add(A.getName()));
    4345             :     Vals.push_back(A.getName().size());
    4346             :     Vals.push_back(0);
    4347             :     Vals.push_back(0);
    4348          12 :     Vals.push_back(0);
    4349             :     Vals.push_back(getEncodedLinkage(A));
    4350           3 : 
    4351           3 :     Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals);
    4352           3 :     Vals.clear();
    4353           3 :   }
    4354           3 : 
    4355           3 :   // Emit the ifunc information.
    4356             :   for (const GlobalIFunc &I : M.ifuncs()) {
    4357           3 :     // IFUNC: [strtab offset, strtab size, 0, 0, 0, linkage]
    4358             :     Vals.push_back(StrtabBuilder.add(I.getName()));
    4359             :     Vals.push_back(I.getName().size());
    4360             :     Vals.push_back(0);
    4361             :     Vals.push_back(0);
    4362          21 :     Vals.push_back(0);
    4363             :     Vals.push_back(getEncodedLinkage(I));
    4364          12 : 
    4365          12 :     Stream.EmitRecord(bitc::MODULE_CODE_IFUNC, Vals);
    4366          12 :     Vals.clear();
    4367          12 :   }
    4368          12 : }
    4369          12 : 
    4370             : void ThinLinkBitcodeWriter::write() {
    4371          12 :   Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
    4372             : 
    4373             :   writeModuleVersion();
    4374             : 
    4375             :   writeSimplifiedModuleInfo();
    4376          10 : 
    4377             :   writePerModuleGlobalValueSummary();
    4378           1 : 
    4379           1 :   // Write module hash.
    4380           1 :   Stream.EmitRecord(bitc::MODULE_CODE_HASH, ArrayRef<uint32_t>(*ModHash));
    4381           1 : 
    4382           1 :   Stream.ExitBlock();
    4383           1 : }
    4384             : 
    4385           1 : void BitcodeWriter::writeThinLinkBitcode(const Module &M,
    4386             :                                          const ModuleSummaryIndex &Index,
    4387             :                                          const ModuleHash &ModHash) {
    4388             :   assert(!WroteStrtab);
    4389             : 
    4390           9 :   // The Mods vector is used by irsymtab::build, which requires non-const
    4391             :   // Modules in case it needs to materialize metadata. But the bitcode writer
    4392           0 :   // requires that the module is materialized, so we can cast to non-const here,
    4393           0 :   // after checking that it is in fact materialized.
    4394           0 :   assert(M.isMaterialized());
    4395           0 :   Mods.push_back(const_cast<Module *>(&M));
    4396           0 : 
    4397           0 :   ThinLinkBitcodeWriter ThinLinkWriter(M, StrtabBuilder, *Stream, Index,
    4398             :                                        ModHash);
    4399           0 :   ThinLinkWriter.write();
    4400             : }
    4401             : 
    4402           9 : // Write the specified thin link bitcode file to the given raw output stream,
    4403             : // where it will be written in a new bitcode block. This is used when
    4404           9 : // writing the per-module index file for ThinLTO.
    4405           9 : void llvm::WriteThinLinkBitcodeToFile(const Module &M, raw_ostream &Out,
    4406             :                                       const ModuleSummaryIndex &Index,
    4407           9 :                                       const ModuleHash &ModHash) {
    4408             :   SmallVector<char, 0> Buffer;
    4409           9 :   Buffer.reserve(256 * 1024);
    4410             : 
    4411           9 :   BitcodeWriter Writer(Buffer);
    4412             :   Writer.writeThinLinkBitcode(M, Index, ModHash);
    4413             :   Writer.writeSymtab();
    4414          18 :   Writer.writeStrtab();
    4415             : 
    4416           9 :   Out.write((char *)&Buffer.front(), Buffer.size());
    4417           9 : }

Generated by: LCOV version 1.13