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

Generated by: LCOV version 1.13