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