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