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