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