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