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