LLVM  13.0.0git
BitcodeWriter.cpp
Go to the documentation of this file.
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 
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"
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"
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"
55 #include "llvm/IR/Operator.h"
56 #include "llvm/IR/Type.h"
57 #include "llvm/IR/UseListOrder.h"
58 #include "llvm/IR/Value.h"
61 #include "llvm/Object/IRSymtab.h"
63 #include "llvm/Support/Casting.h"
65 #include "llvm/Support/Endian.h"
66 #include "llvm/Support/Error.h"
69 #include "llvm/Support/SHA1.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"));
90  "bitcode-flush-threshold", cl::Hidden, cl::init(512),
91  cl::desc("The threshold (unit M) for flushing LLVM bitcode."));
92 
94  "write-relbf-to-summary", cl::Hidden, cl::init(false),
95  cl::desc("Write relative block frequency to function summary "));
96 
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,
294  unsigned Abbrev);
295  unsigned createDILocationAbbrev();
297  unsigned &Abbrev);
298  unsigned createGenericDINodeAbbrev();
299  void writeGenericDINode(const GenericDINode *N,
300  SmallVectorImpl<uint64_t> &Record, unsigned &Abbrev);
302  unsigned Abbrev);
305  unsigned Abbrev);
306  void writeDIEnumerator(const DIEnumerator *N,
307  SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
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);
315  SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
318  unsigned Abbrev);
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);
326  SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
329  unsigned Abbrev);
330  void writeDICommonBlock(const DICommonBlock *N,
331  SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
333  unsigned Abbrev);
335  unsigned Abbrev);
337  unsigned Abbrev);
339  unsigned Abbrev);
341  unsigned Abbrev);
344  unsigned Abbrev);
347  unsigned Abbrev);
350  unsigned Abbrev);
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);
359  unsigned Abbrev);
361  SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
364  unsigned Abbrev);
365  unsigned createNamedMetadataAbbrev();
366  void writeNamedMetadata(SmallVectorImpl<uint64_t> &Record);
367  unsigned createMetadataStringsAbbrev();
368  void writeMetadataStrings(ArrayRef<const Metadata *> Strings,
370  void writeMetadataRecords(ArrayRef<const Metadata *> MDs,
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,
387  void writeOperandBundles(const CallBase &CB, unsigned InstID);
388  void pushValue(const Value *V, unsigned InstID,
390  void pushValueSigned(const Value *V, unsigned InstID,
392  void writeInstruction(const Instruction &I, unsigned InstID,
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 
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) {
592  }
593  llvm_unreachable("Invalid ordering");
594 }
595 
596 static void writeStringRecord(BitstreamWriter &Stream, unsigned Code,
597  StringRef Str, unsigned AbbrevToUse) {
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 
612  switch (Kind) {
613  case Attribute::Alignment:
615  case Attribute::AllocSize:
617  case Attribute::AlwaysInline:
619  case Attribute::ArgMemOnly:
621  case Attribute::Builtin:
623  case Attribute::ByVal:
624  return bitc::ATTR_KIND_BY_VAL;
627  case Attribute::InAlloca:
629  case Attribute::Cold:
630  return bitc::ATTR_KIND_COLD;
631  case Attribute::Hot:
632  return bitc::ATTR_KIND_HOT;
633  case Attribute::InaccessibleMemOnly:
635  case Attribute::InaccessibleMemOrArgMemOnly:
637  case Attribute::InlineHint:
639  case Attribute::InReg:
640  return bitc::ATTR_KIND_IN_REG;
643  case Attribute::MinSize:
645  case Attribute::Naked:
646  return bitc::ATTR_KIND_NAKED;
647  case Attribute::Nest:
648  return bitc::ATTR_KIND_NEST;
649  case Attribute::NoAlias:
651  case Attribute::NoBuiltin:
653  case Attribute::NoCallback:
655  case Attribute::NoCapture:
657  case Attribute::NoDuplicate:
659  case Attribute::NoFree:
660  return bitc::ATTR_KIND_NOFREE;
661  case Attribute::NoImplicitFloat:
663  case Attribute::NoInline:
665  case Attribute::NoRecurse:
667  case Attribute::NoMerge:
669  case Attribute::NonLazyBind:
671  case Attribute::NonNull:
673  case Attribute::Dereferenceable:
675  case Attribute::DereferenceableOrNull:
677  case Attribute::NoRedZone:
679  case Attribute::NoReturn:
681  case Attribute::NoSync:
682  return bitc::ATTR_KIND_NOSYNC;
683  case Attribute::NoCfCheck:
685  case Attribute::NoProfile:
687  case Attribute::NoUnwind:
689  case Attribute::NullPointerIsValid:
691  case Attribute::OptForFuzzing:
693  case Attribute::OptimizeForSize:
695  case Attribute::OptimizeNone:
697  case Attribute::ReadNone:
699  case Attribute::ReadOnly:
701  case Attribute::Returned:
703  case Attribute::ReturnsTwice:
705  case Attribute::SExt:
706  return bitc::ATTR_KIND_S_EXT;
707  case Attribute::Speculatable:
709  case Attribute::StackAlignment:
711  case Attribute::StackProtect:
713  case Attribute::StackProtectReq:
715  case Attribute::StackProtectStrong:
717  case Attribute::SafeStack:
719  case Attribute::ShadowCallStack:
721  case Attribute::StrictFP:
723  case Attribute::StructRet:
725  case Attribute::SanitizeAddress:
727  case Attribute::SanitizeHWAddress:
729  case Attribute::SanitizeThread:
731  case Attribute::SanitizeMemory:
733  case Attribute::SpeculativeLoadHardening:
735  case Attribute::SwiftError:
737  case Attribute::SwiftSelf:
739  case Attribute::UWTable:
741  case Attribute::VScaleRange:
743  case Attribute::WillReturn:
745  case Attribute::WriteOnly:
747  case Attribute::ZExt:
748  return bitc::ATTR_KIND_Z_EXT;
749  case Attribute::ImmArg:
750  return bitc::ATTR_KIND_IMMARG;
751  case Attribute::SanitizeMemTag:
753  case Attribute::Preallocated:
755  case Attribute::NoUndef:
757  case Attribute::ByRef:
758  return bitc::ATTR_KIND_BYREF;
759  case Attribute::MustProgress:
762  llvm_unreachable("Can not encode end-attribute kinds marker.");
763  case Attribute::None:
764  llvm_unreachable("Can not encode none-attribute.");
765  case Attribute::EmptyKey:
767  llvm_unreachable("Trying to encode EmptyKey/TombstoneKey");
768  }
769 
770  llvm_unreachable("Trying to encode unknown attribute");
771 }
772 
773 void ModuleBitcodeWriter::writeAttributeGroupTable() {
774  const std::vector<ValueEnumerator::IndexAndAttrSet> &AttrGrps =
775  VE.getAttributeGroups();
776  if (AttrGrps.empty()) return;
777 
779 
781  for (ValueEnumerator::IndexAndAttrSet Pair : AttrGrps) {
782  unsigned AttrListIndex = Pair.first;
783  AttributeSet AS = Pair.second;
784  Record.push_back(VE.getAttributeGroupID(Pair));
785  Record.push_back(AttrListIndex);
786 
787  for (Attribute Attr : AS) {
788  if (Attr.isEnumAttribute()) {
789  Record.push_back(0);
790  Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
791  } else if (Attr.isIntAttribute()) {
792  Record.push_back(1);
793  Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
794  Record.push_back(Attr.getValueAsInt());
795  } else if (Attr.isStringAttribute()) {
796  StringRef Kind = Attr.getKindAsString();
797  StringRef Val = Attr.getValueAsString();
798 
799  Record.push_back(Val.empty() ? 3 : 4);
800  Record.append(Kind.begin(), Kind.end());
801  Record.push_back(0);
802  if (!Val.empty()) {
803  Record.append(Val.begin(), Val.end());
804  Record.push_back(0);
805  }
806  } else {
807  assert(Attr.isTypeAttribute());
808  Type *Ty = Attr.getValueAsType();
809  Record.push_back(Ty ? 6 : 5);
810  Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
811  if (Ty)
812  Record.push_back(VE.getTypeID(Attr.getValueAsType()));
813  }
814  }
815 
817  Record.clear();
818  }
819 
820  Stream.ExitBlock();
821 }
822 
823 void ModuleBitcodeWriter::writeAttributeTable() {
824  const std::vector<AttributeList> &Attrs = VE.getAttributeLists();
825  if (Attrs.empty()) return;
826 
828 
830  for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
831  AttributeList AL = Attrs[i];
832  for (unsigned i = AL.index_begin(), e = AL.index_end(); i != e; ++i) {
833  AttributeSet AS = AL.getAttributes(i);
834  if (AS.hasAttributes())
835  Record.push_back(VE.getAttributeGroupID({i, AS}));
836  }
837 
839  Record.clear();
840  }
841 
842  Stream.ExitBlock();
843 }
844 
845 /// WriteTypeTable - Write out the type table for a module.
846 void ModuleBitcodeWriter::writeTypeTable() {
847  const ValueEnumerator::TypeList &TypeList = VE.getTypes();
848 
849  Stream.EnterSubblock(bitc::TYPE_BLOCK_ID_NEW, 4 /*count from # abbrevs */);
850  SmallVector<uint64_t, 64> TypeVals;
851 
852  uint64_t NumBits = VE.computeBitsRequiredForTypeIndicies();
853 
854  // Abbrev for TYPE_CODE_POINTER.
855  auto Abbv = std::make_shared<BitCodeAbbrev>();
857  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
858  Abbv->Add(BitCodeAbbrevOp(0)); // Addrspace = 0
859  unsigned PtrAbbrev = Stream.EmitAbbrev(std::move(Abbv));
860 
861  // Abbrev for TYPE_CODE_FUNCTION.
862  Abbv = std::make_shared<BitCodeAbbrev>();
864  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg
866  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
867  unsigned FunctionAbbrev = Stream.EmitAbbrev(std::move(Abbv));
868 
869  // Abbrev for TYPE_CODE_STRUCT_ANON.
870  Abbv = std::make_shared<BitCodeAbbrev>();
872  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
874  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
875  unsigned StructAnonAbbrev = Stream.EmitAbbrev(std::move(Abbv));
876 
877  // Abbrev for TYPE_CODE_STRUCT_NAME.
878  Abbv = std::make_shared<BitCodeAbbrev>();
882  unsigned StructNameAbbrev = Stream.EmitAbbrev(std::move(Abbv));
883 
884  // Abbrev for TYPE_CODE_STRUCT_NAMED.
885  Abbv = std::make_shared<BitCodeAbbrev>();
887  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
889  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
890  unsigned StructNamedAbbrev = Stream.EmitAbbrev(std::move(Abbv));
891 
892  // Abbrev for TYPE_CODE_ARRAY.
893  Abbv = std::make_shared<BitCodeAbbrev>();
895  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size
896  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
897  unsigned ArrayAbbrev = Stream.EmitAbbrev(std::move(Abbv));
898 
899  // Emit an entry count so the reader can reserve space.
900  TypeVals.push_back(TypeList.size());
901  Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
902  TypeVals.clear();
903 
904  // Loop over all of the types, emitting each in turn.
905  for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
906  Type *T = TypeList[i];
907  int AbbrevToUse = 0;
908  unsigned Code = 0;
909 
910  switch (T->getTypeID()) {
924  case Type::IntegerTyID:
925  // INTEGER: [width]
927  TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
928  break;
929  case Type::PointerTyID: {
930  PointerType *PTy = cast<PointerType>(T);
931  // POINTER: [pointee type, address space]
933  TypeVals.push_back(VE.getTypeID(PTy->getElementType()));
934  unsigned AddressSpace = PTy->getAddressSpace();
935  TypeVals.push_back(AddressSpace);
936  if (AddressSpace == 0) AbbrevToUse = PtrAbbrev;
937  break;
938  }
939  case Type::FunctionTyID: {
940  FunctionType *FT = cast<FunctionType>(T);
941  // FUNCTION: [isvararg, retty, paramty x N]
943  TypeVals.push_back(FT->isVarArg());
944  TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
945  for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
946  TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
947  AbbrevToUse = FunctionAbbrev;
948  break;
949  }
950  case Type::StructTyID: {
951  StructType *ST = cast<StructType>(T);
952  // STRUCT: [ispacked, eltty x N]
953  TypeVals.push_back(ST->isPacked());
954  // Output all of the element types.
955  for (StructType::element_iterator I = ST->element_begin(),
956  E = ST->element_end(); I != E; ++I)
957  TypeVals.push_back(VE.getTypeID(*I));
958 
959  if (ST->isLiteral()) {
961  AbbrevToUse = StructAnonAbbrev;
962  } else {
963  if (ST->isOpaque()) {
965  } else {
967  AbbrevToUse = StructNamedAbbrev;
968  }
969 
970  // Emit the name if it is present.
971  if (!ST->getName().empty())
973  StructNameAbbrev);
974  }
975  break;
976  }
977  case Type::ArrayTyID: {
978  ArrayType *AT = cast<ArrayType>(T);
979  // ARRAY: [numelts, eltty]
981  TypeVals.push_back(AT->getNumElements());
982  TypeVals.push_back(VE.getTypeID(AT->getElementType()));
983  AbbrevToUse = ArrayAbbrev;
984  break;
985  }
988  VectorType *VT = cast<VectorType>(T);
989  // VECTOR [numelts, eltty] or
990  // [numelts, eltty, scalable]
992  TypeVals.push_back(VT->getElementCount().getKnownMinValue());
993  TypeVals.push_back(VE.getTypeID(VT->getElementType()));
994  if (isa<ScalableVectorType>(VT))
995  TypeVals.push_back(true);
996  break;
997  }
998  }
999 
1000  // Emit the finished record.
1001  Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
1002  TypeVals.clear();
1003  }
1004 
1005  Stream.ExitBlock();
1006 }
1007 
1009  switch (Linkage) {
1011  return 0;
1013  return 16;
1015  return 2;
1017  return 3;
1019  return 18;
1021  return 7;
1023  return 8;
1025  return 9;
1027  return 17;
1029  return 19;
1031  return 12;
1032  }
1033  llvm_unreachable("Invalid linkage");
1034 }
1035 
1036 static unsigned getEncodedLinkage(const GlobalValue &GV) {
1037  return getEncodedLinkage(GV.getLinkage());
1038 }
1039 
1041  uint64_t RawFlags = 0;
1042  RawFlags |= Flags.ReadNone;
1043  RawFlags |= (Flags.ReadOnly << 1);
1044  RawFlags |= (Flags.NoRecurse << 2);
1045  RawFlags |= (Flags.ReturnDoesNotAlias << 3);
1046  RawFlags |= (Flags.NoInline << 4);
1047  RawFlags |= (Flags.AlwaysInline << 5);
1048  return RawFlags;
1049 }
1050 
1051 // Decode the flags for GlobalValue in the summary. See getDecodedGVSummaryFlags
1052 // in BitcodeReader.cpp.
1054  uint64_t RawFlags = 0;
1055 
1056  RawFlags |= Flags.NotEligibleToImport; // bool
1057  RawFlags |= (Flags.Live << 1);
1058  RawFlags |= (Flags.DSOLocal << 2);
1059  RawFlags |= (Flags.CanAutoHide << 3);
1060 
1061  // Linkage don't need to be remapped at that time for the summary. Any future
1062  // change to the getEncodedLinkage() function will need to be taken into
1063  // account here as well.
1064  RawFlags = (RawFlags << 4) | Flags.Linkage; // 4 bits
1065 
1066  RawFlags |= (Flags.Visibility << 8); // 2 bits
1067 
1068  return RawFlags;
1069 }
1070 
1072  uint64_t RawFlags = Flags.MaybeReadOnly | (Flags.MaybeWriteOnly << 1) |
1073  (Flags.Constant << 2) | Flags.VCallVisibility << 3;
1074  return RawFlags;
1075 }
1076 
1077 static unsigned getEncodedVisibility(const GlobalValue &GV) {
1078  switch (GV.getVisibility()) {
1079  case GlobalValue::DefaultVisibility: return 0;
1080  case GlobalValue::HiddenVisibility: return 1;
1081  case GlobalValue::ProtectedVisibility: return 2;
1082  }
1083  llvm_unreachable("Invalid visibility");
1084 }
1085 
1086 static unsigned getEncodedDLLStorageClass(const GlobalValue &GV) {
1087  switch (GV.getDLLStorageClass()) {
1088  case GlobalValue::DefaultStorageClass: return 0;
1089  case GlobalValue::DLLImportStorageClass: return 1;
1090  case GlobalValue::DLLExportStorageClass: return 2;
1091  }
1092  llvm_unreachable("Invalid DLL storage class");
1093 }
1094 
1095 static unsigned getEncodedThreadLocalMode(const GlobalValue &GV) {
1096  switch (GV.getThreadLocalMode()) {
1097  case GlobalVariable::NotThreadLocal: return 0;
1099  case GlobalVariable::LocalDynamicTLSModel: return 2;
1100  case GlobalVariable::InitialExecTLSModel: return 3;
1101  case GlobalVariable::LocalExecTLSModel: return 4;
1102  }
1103  llvm_unreachable("Invalid TLS model");
1104 }
1105 
1106 static unsigned getEncodedComdatSelectionKind(const Comdat &C) {
1107  switch (C.getSelectionKind()) {
1108  case Comdat::Any:
1110  case Comdat::ExactMatch:
1112  case Comdat::Largest:
1114  case Comdat::NoDuplicates:
1116  case Comdat::SameSize:
1118  }
1119  llvm_unreachable("Invalid selection kind");
1120 }
1121 
1122 static unsigned getEncodedUnnamedAddr(const GlobalValue &GV) {
1123  switch (GV.getUnnamedAddr()) {
1124  case GlobalValue::UnnamedAddr::None: return 0;
1125  case GlobalValue::UnnamedAddr::Local: return 2;
1126  case GlobalValue::UnnamedAddr::Global: return 1;
1127  }
1128  llvm_unreachable("Invalid unnamed_addr");
1129 }
1130 
1131 size_t ModuleBitcodeWriter::addToStrtab(StringRef Str) {
1132  if (GenerateHash)
1133  Hasher.update(Str);
1134  return StrtabBuilder.add(Str);
1135 }
1136 
1137 void ModuleBitcodeWriter::writeComdats() {
1139  for (const Comdat *C : VE.getComdats()) {
1140  // COMDAT: [strtab offset, strtab size, selection_kind]
1141  Vals.push_back(addToStrtab(C->getName()));
1142  Vals.push_back(C->getName().size());
1143  Vals.push_back(getEncodedComdatSelectionKind(*C));
1144  Stream.EmitRecord(bitc::MODULE_CODE_COMDAT, Vals, /*AbbrevToUse=*/0);
1145  Vals.clear();
1146  }
1147 }
1148 
1149 /// Write a record that will eventually hold the word offset of the
1150 /// module-level VST. For now the offset is 0, which will be backpatched
1151 /// after the real VST is written. Saves the bit offset to backpatch.
1152 void ModuleBitcodeWriter::writeValueSymbolTableForwardDecl() {
1153  // Write a placeholder value in for the offset of the real VST,
1154  // which is written after the function blocks so that it can include
1155  // the offset of each function. The placeholder offset will be
1156  // updated when the real VST is written.
1157  auto Abbv = std::make_shared<BitCodeAbbrev>();
1159  // Blocks are 32-bit aligned, so we can use a 32-bit word offset to
1160  // hold the real VST offset. Must use fixed instead of VBR as we don't
1161  // know how many VBR chunks to reserve ahead of time.
1162  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1163  unsigned VSTOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1164 
1165  // Emit the placeholder
1166  uint64_t Vals[] = {bitc::MODULE_CODE_VSTOFFSET, 0};
1167  Stream.EmitRecordWithAbbrev(VSTOffsetAbbrev, Vals);
1168 
1169  // Compute and save the bit offset to the placeholder, which will be
1170  // patched when the real VST is written. We can simply subtract the 32-bit
1171  // fixed size from the current bit number to get the location to backpatch.
1172  VSTOffsetPlaceholder = Stream.GetCurrentBitNo() - 32;
1173 }
1174 
1176 
1177 /// Determine the encoding to use for the given string name and length.
1179  bool isChar6 = true;
1180  for (char C : Str) {
1181  if (isChar6)
1182  isChar6 = BitCodeAbbrevOp::isChar6(C);
1183  if ((unsigned char)C & 128)
1184  // don't bother scanning the rest.
1185  return SE_Fixed8;
1186  }
1187  if (isChar6)
1188  return SE_Char6;
1189  return SE_Fixed7;
1190 }
1191 
1192 /// Emit top-level description of module, including target triple, inline asm,
1193 /// descriptors for global variables, and function prototype info.
1194 /// Returns the bit offset to backpatch with the location of the real VST.
1195 void ModuleBitcodeWriter::writeModuleInfo() {
1196  // Emit various pieces of data attached to a module.
1197  if (!M.getTargetTriple().empty())
1198  writeStringRecord(Stream, bitc::MODULE_CODE_TRIPLE, M.getTargetTriple(),
1199  0 /*TODO*/);
1200  const std::string &DL = M.getDataLayoutStr();
1201  if (!DL.empty())
1202  writeStringRecord(Stream, bitc::MODULE_CODE_DATALAYOUT, DL, 0 /*TODO*/);
1203  if (!M.getModuleInlineAsm().empty())
1204  writeStringRecord(Stream, bitc::MODULE_CODE_ASM, M.getModuleInlineAsm(),
1205  0 /*TODO*/);
1206 
1207  // Emit information about sections and GC, computing how many there are. Also
1208  // compute the maximum alignment value.
1209  std::map<std::string, unsigned> SectionMap;
1210  std::map<std::string, unsigned> GCMap;
1211  MaybeAlign MaxAlignment;
1212  unsigned MaxGlobalType = 0;
1213  const auto UpdateMaxAlignment = [&MaxAlignment](const MaybeAlign A) {
1214  if (A)
1215  MaxAlignment = !MaxAlignment ? *A : std::max(*MaxAlignment, *A);
1216  };
1217  for (const GlobalVariable &GV : M.globals()) {
1218  UpdateMaxAlignment(GV.getAlign());
1219  MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV.getValueType()));
1220  if (GV.hasSection()) {
1221  // Give section names unique ID's.
1222  unsigned &Entry = SectionMap[std::string(GV.getSection())];
1223  if (!Entry) {
1224  writeStringRecord(Stream, bitc::MODULE_CODE_SECTIONNAME, GV.getSection(),
1225  0 /*TODO*/);
1226  Entry = SectionMap.size();
1227  }
1228  }
1229  }
1230  for (const Function &F : M) {
1231  UpdateMaxAlignment(F.getAlign());
1232  if (F.hasSection()) {
1233  // Give section names unique ID's.
1234  unsigned &Entry = SectionMap[std::string(F.getSection())];
1235  if (!Entry) {
1236  writeStringRecord(Stream, bitc::MODULE_CODE_SECTIONNAME, F.getSection(),
1237  0 /*TODO*/);
1238  Entry = SectionMap.size();
1239  }
1240  }
1241  if (F.hasGC()) {
1242  // Same for GC names.
1243  unsigned &Entry = GCMap[F.getGC()];
1244  if (!Entry) {
1245  writeStringRecord(Stream, bitc::MODULE_CODE_GCNAME, F.getGC(),
1246  0 /*TODO*/);
1247  Entry = GCMap.size();
1248  }
1249  }
1250  }
1251 
1252  // Emit abbrev for globals, now that we know # sections and max alignment.
1253  unsigned SimpleGVarAbbrev = 0;
1254  if (!M.global_empty()) {
1255  // Add an abbrev for common globals with no visibility or thread localness.
1256  auto Abbv = std::make_shared<BitCodeAbbrev>();
1258  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1259  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1261  Log2_32_Ceil(MaxGlobalType+1)));
1262  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // AddrSpace << 2
1263  //| explicitType << 1
1264  //| constant
1265  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer.
1266  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // Linkage.
1267  if (!MaxAlignment) // Alignment.
1268  Abbv->Add(BitCodeAbbrevOp(0));
1269  else {
1270  unsigned MaxEncAlignment = getEncodedAlign(MaxAlignment);
1272  Log2_32_Ceil(MaxEncAlignment+1)));
1273  }
1274  if (SectionMap.empty()) // Section.
1275  Abbv->Add(BitCodeAbbrevOp(0));
1276  else
1278  Log2_32_Ceil(SectionMap.size()+1)));
1279  // Don't bother emitting vis + thread local.
1280  SimpleGVarAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1281  }
1282 
1284  // Emit the module's source file name.
1285  {
1286  StringEncoding Bits = getStringEncoding(M.getSourceFileName());
1288  if (Bits == SE_Char6)
1289  AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Char6);
1290  else if (Bits == SE_Fixed7)
1291  AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7);
1292 
1293  // MODULE_CODE_SOURCE_FILENAME: [namechar x N]
1294  auto Abbv = std::make_shared<BitCodeAbbrev>();
1297  Abbv->Add(AbbrevOpToUse);
1298  unsigned FilenameAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1299 
1300  for (const auto P : M.getSourceFileName())
1301  Vals.push_back((unsigned char)P);
1302 
1303  // Emit the finished record.
1304  Stream.EmitRecord(bitc::MODULE_CODE_SOURCE_FILENAME, Vals, FilenameAbbrev);
1305  Vals.clear();
1306  }
1307 
1308  // Emit the global variable information.
1309  for (const GlobalVariable &GV : M.globals()) {
1310  unsigned AbbrevToUse = 0;
1311 
1312  // GLOBALVAR: [strtab offset, strtab size, type, isconst, initid,
1313  // linkage, alignment, section, visibility, threadlocal,
1314  // unnamed_addr, externally_initialized, dllstorageclass,
1315  // comdat, attributes, DSO_Local]
1316  Vals.push_back(addToStrtab(GV.getName()));
1317  Vals.push_back(GV.getName().size());
1318  Vals.push_back(VE.getTypeID(GV.getValueType()));
1319  Vals.push_back(GV.getType()->getAddressSpace() << 2 | 2 | GV.isConstant());
1320  Vals.push_back(GV.isDeclaration() ? 0 :
1321  (VE.getValueID(GV.getInitializer()) + 1));
1322  Vals.push_back(getEncodedLinkage(GV));
1323  Vals.push_back(getEncodedAlign(GV.getAlign()));
1324  Vals.push_back(GV.hasSection() ? SectionMap[std::string(GV.getSection())]
1325  : 0);
1326  if (GV.isThreadLocal() ||
1327  GV.getVisibility() != GlobalValue::DefaultVisibility ||
1328  GV.getUnnamedAddr() != GlobalValue::UnnamedAddr::None ||
1329  GV.isExternallyInitialized() ||
1330  GV.getDLLStorageClass() != GlobalValue::DefaultStorageClass ||
1331  GV.hasComdat() ||
1332  GV.hasAttributes() ||
1333  GV.isDSOLocal() ||
1334  GV.hasPartition()) {
1335  Vals.push_back(getEncodedVisibility(GV));
1336  Vals.push_back(getEncodedThreadLocalMode(GV));
1337  Vals.push_back(getEncodedUnnamedAddr(GV));
1338  Vals.push_back(GV.isExternallyInitialized());
1339  Vals.push_back(getEncodedDLLStorageClass(GV));
1340  Vals.push_back(GV.hasComdat() ? VE.getComdatID(GV.getComdat()) : 0);
1341 
1342  auto AL = GV.getAttributesAsList(AttributeList::FunctionIndex);
1343  Vals.push_back(VE.getAttributeListID(AL));
1344 
1345  Vals.push_back(GV.isDSOLocal());
1346  Vals.push_back(addToStrtab(GV.getPartition()));
1347  Vals.push_back(GV.getPartition().size());
1348  } else {
1349  AbbrevToUse = SimpleGVarAbbrev;
1350  }
1351 
1352  Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
1353  Vals.clear();
1354  }
1355 
1356  // Emit the function proto information.
1357  for (const Function &F : M) {
1358  // FUNCTION: [strtab offset, strtab size, type, callingconv, isproto,
1359  // linkage, paramattrs, alignment, section, visibility, gc,
1360  // unnamed_addr, prologuedata, dllstorageclass, comdat,
1361  // prefixdata, personalityfn, DSO_Local, addrspace]
1362  Vals.push_back(addToStrtab(F.getName()));
1363  Vals.push_back(F.getName().size());
1364  Vals.push_back(VE.getTypeID(F.getFunctionType()));
1365  Vals.push_back(F.getCallingConv());
1366  Vals.push_back(F.isDeclaration());
1367  Vals.push_back(getEncodedLinkage(F));
1368  Vals.push_back(VE.getAttributeListID(F.getAttributes()));
1369  Vals.push_back(getEncodedAlign(F.getAlign()));
1370  Vals.push_back(F.hasSection() ? SectionMap[std::string(F.getSection())]
1371  : 0);
1372  Vals.push_back(getEncodedVisibility(F));
1373  Vals.push_back(F.hasGC() ? GCMap[F.getGC()] : 0);
1374  Vals.push_back(getEncodedUnnamedAddr(F));
1375  Vals.push_back(F.hasPrologueData() ? (VE.getValueID(F.getPrologueData()) + 1)
1376  : 0);
1377  Vals.push_back(getEncodedDLLStorageClass(F));
1378  Vals.push_back(F.hasComdat() ? VE.getComdatID(F.getComdat()) : 0);
1379  Vals.push_back(F.hasPrefixData() ? (VE.getValueID(F.getPrefixData()) + 1)
1380  : 0);
1381  Vals.push_back(
1382  F.hasPersonalityFn() ? (VE.getValueID(F.getPersonalityFn()) + 1) : 0);
1383 
1384  Vals.push_back(F.isDSOLocal());
1385  Vals.push_back(F.getAddressSpace());
1386  Vals.push_back(addToStrtab(F.getPartition()));
1387  Vals.push_back(F.getPartition().size());
1388 
1389  unsigned AbbrevToUse = 0;
1390  Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
1391  Vals.clear();
1392  }
1393 
1394  // Emit the alias information.
1395  for (const GlobalAlias &A : M.aliases()) {
1396  // ALIAS: [strtab offset, strtab size, alias type, aliasee val#, linkage,
1397  // visibility, dllstorageclass, threadlocal, unnamed_addr,
1398  // DSO_Local]
1399  Vals.push_back(addToStrtab(A.getName()));
1400  Vals.push_back(A.getName().size());
1401  Vals.push_back(VE.getTypeID(A.getValueType()));
1402  Vals.push_back(A.getType()->getAddressSpace());
1403  Vals.push_back(VE.getValueID(A.getAliasee()));
1404  Vals.push_back(getEncodedLinkage(A));
1405  Vals.push_back(getEncodedVisibility(A));
1406  Vals.push_back(getEncodedDLLStorageClass(A));
1407  Vals.push_back(getEncodedThreadLocalMode(A));
1408  Vals.push_back(getEncodedUnnamedAddr(A));
1409  Vals.push_back(A.isDSOLocal());
1410  Vals.push_back(addToStrtab(A.getPartition()));
1411  Vals.push_back(A.getPartition().size());
1412 
1413  unsigned AbbrevToUse = 0;
1414  Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
1415  Vals.clear();
1416  }
1417 
1418  // Emit the ifunc information.
1419  for (const GlobalIFunc &I : M.ifuncs()) {
1420  // IFUNC: [strtab offset, strtab size, ifunc type, address space, resolver
1421  // val#, linkage, visibility, DSO_Local]
1422  Vals.push_back(addToStrtab(I.getName()));
1423  Vals.push_back(I.getName().size());
1424  Vals.push_back(VE.getTypeID(I.getValueType()));
1425  Vals.push_back(I.getType()->getAddressSpace());
1426  Vals.push_back(VE.getValueID(I.getResolver()));
1427  Vals.push_back(getEncodedLinkage(I));
1428  Vals.push_back(getEncodedVisibility(I));
1429  Vals.push_back(I.isDSOLocal());
1430  Vals.push_back(addToStrtab(I.getPartition()));
1431  Vals.push_back(I.getPartition().size());
1432  Stream.EmitRecord(bitc::MODULE_CODE_IFUNC, Vals);
1433  Vals.clear();
1434  }
1435 
1436  writeValueSymbolTableForwardDecl();
1437 }
1438 
1439 static uint64_t getOptimizationFlags(const Value *V) {
1440  uint64_t Flags = 0;
1441 
1442  if (const auto *OBO = dyn_cast<OverflowingBinaryOperator>(V)) {
1443  if (OBO->hasNoSignedWrap())
1444  Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP;
1445  if (OBO->hasNoUnsignedWrap())
1446  Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP;
1447  } else if (const auto *PEO = dyn_cast<PossiblyExactOperator>(V)) {
1448  if (PEO->isExact())
1449  Flags |= 1 << bitc::PEO_EXACT;
1450  } else if (const auto *FPMO = dyn_cast<FPMathOperator>(V)) {
1451  if (FPMO->hasAllowReassoc())
1452  Flags |= bitc::AllowReassoc;
1453  if (FPMO->hasNoNaNs())
1454  Flags |= bitc::NoNaNs;
1455  if (FPMO->hasNoInfs())
1456  Flags |= bitc::NoInfs;
1457  if (FPMO->hasNoSignedZeros())
1458  Flags |= bitc::NoSignedZeros;
1459  if (FPMO->hasAllowReciprocal())
1460  Flags |= bitc::AllowReciprocal;
1461  if (FPMO->hasAllowContract())
1462  Flags |= bitc::AllowContract;
1463  if (FPMO->hasApproxFunc())
1464  Flags |= bitc::ApproxFunc;
1465  }
1466 
1467  return Flags;
1468 }
1469 
1470 void ModuleBitcodeWriter::writeValueAsMetadata(
1472  // Mimic an MDNode with a value as one operand.
1473  Value *V = MD->getValue();
1474  Record.push_back(VE.getTypeID(V->getType()));
1475  Record.push_back(VE.getValueID(V));
1477  Record.clear();
1478 }
1479 
1482  unsigned Abbrev) {
1483  for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1484  Metadata *MD = N->getOperand(i);
1485  assert(!(MD && isa<LocalAsMetadata>(MD)) &&
1486  "Unexpected function-local metadata");
1487  Record.push_back(VE.getMetadataOrNullID(MD));
1488  }
1489  Stream.EmitRecord(N->isDistinct() ? bitc::METADATA_DISTINCT_NODE
1491  Record, Abbrev);
1492  Record.clear();
1493 }
1494 
1495 unsigned ModuleBitcodeWriter::createDILocationAbbrev() {
1496  // Assume the column is usually under 128, and always output the inlined-at
1497  // location (it's never more expensive than building an array size 1).
1498  auto Abbv = std::make_shared<BitCodeAbbrev>();
1500  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1501  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1502  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1503  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1504  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1505  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1506  return Stream.EmitAbbrev(std::move(Abbv));
1507 }
1508 
1511  unsigned &Abbrev) {
1512  if (!Abbrev)
1513  Abbrev = createDILocationAbbrev();
1514 
1515  Record.push_back(N->isDistinct());
1516  Record.push_back(N->getLine());
1517  Record.push_back(N->getColumn());
1518  Record.push_back(VE.getMetadataID(N->getScope()));
1519  Record.push_back(VE.getMetadataOrNullID(N->getInlinedAt()));
1520  Record.push_back(N->isImplicitCode());
1521 
1522  Stream.EmitRecord(bitc::METADATA_LOCATION, Record, Abbrev);
1523  Record.clear();
1524 }
1525 
1526 unsigned ModuleBitcodeWriter::createGenericDINodeAbbrev() {
1527  // Assume the column is usually under 128, and always output the inlined-at
1528  // location (it's never more expensive than building an array size 1).
1529  auto Abbv = std::make_shared<BitCodeAbbrev>();
1531  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1532  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1533  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1534  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1536  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1537  return Stream.EmitAbbrev(std::move(Abbv));
1538 }
1539 
1542  unsigned &Abbrev) {
1543  if (!Abbrev)
1544  Abbrev = createGenericDINodeAbbrev();
1545 
1546  Record.push_back(N->isDistinct());
1547  Record.push_back(N->getTag());
1548  Record.push_back(0); // Per-tag version field; unused for now.
1549 
1550  for (auto &I : N->operands())
1551  Record.push_back(VE.getMetadataOrNullID(I));
1552 
1554  Record.clear();
1555 }
1556 
1559  unsigned Abbrev) {
1560  const uint64_t Version = 2 << 1;
1561  Record.push_back((uint64_t)N->isDistinct() | Version);
1562  Record.push_back(VE.getMetadataOrNullID(N->getRawCountNode()));
1563  Record.push_back(VE.getMetadataOrNullID(N->getRawLowerBound()));
1564  Record.push_back(VE.getMetadataOrNullID(N->getRawUpperBound()));
1565  Record.push_back(VE.getMetadataOrNullID(N->getRawStride()));
1566 
1567  Stream.EmitRecord(bitc::METADATA_SUBRANGE, Record, Abbrev);
1568  Record.clear();
1569 }
1570 
1573  unsigned Abbrev) {
1574  Record.push_back((uint64_t)N->isDistinct());
1575  Record.push_back(VE.getMetadataOrNullID(N->getRawCountNode()));
1576  Record.push_back(VE.getMetadataOrNullID(N->getRawLowerBound()));
1577  Record.push_back(VE.getMetadataOrNullID(N->getRawUpperBound()));
1578  Record.push_back(VE.getMetadataOrNullID(N->getRawStride()));
1579 
1581  Record.clear();
1582 }
1583 
1584 static void emitSignedInt64(SmallVectorImpl<uint64_t> &Vals, uint64_t V) {
1585  if ((int64_t)V >= 0)
1586  Vals.push_back(V << 1);
1587  else
1588  Vals.push_back((-V << 1) | 1);
1589 }
1590 
1591 static void emitWideAPInt(SmallVectorImpl<uint64_t> &Vals, const APInt &A) {
1592  // We have an arbitrary precision integer value to write whose
1593  // bit width is > 64. However, in canonical unsigned integer
1594  // format it is likely that the high bits are going to be zero.
1595  // So, we only write the number of active words.
1596  unsigned NumWords = A.getActiveWords();
1597  const uint64_t *RawData = A.getRawData();
1598  for (unsigned i = 0; i < NumWords; i++)
1599  emitSignedInt64(Vals, RawData[i]);
1600 }
1601 
1604  unsigned Abbrev) {
1605  const uint64_t IsBigInt = 1 << 2;
1606  Record.push_back(IsBigInt | (N->isUnsigned() << 1) | N->isDistinct());
1607  Record.push_back(N->getValue().getBitWidth());
1608  Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1609  emitWideAPInt(Record, N->getValue());
1610 
1611  Stream.EmitRecord(bitc::METADATA_ENUMERATOR, Record, Abbrev);
1612  Record.clear();
1613 }
1614 
1617  unsigned Abbrev) {
1618  Record.push_back(N->isDistinct());
1619  Record.push_back(N->getTag());
1620  Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1621  Record.push_back(N->getSizeInBits());
1622  Record.push_back(N->getAlignInBits());
1623  Record.push_back(N->getEncoding());
1624  Record.push_back(N->getFlags());
1625 
1626  Stream.EmitRecord(bitc::METADATA_BASIC_TYPE, Record, Abbrev);
1627  Record.clear();
1628 }
1629 
1632  unsigned Abbrev) {
1633  Record.push_back(N->isDistinct());
1634  Record.push_back(N->getTag());
1635  Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1636  Record.push_back(VE.getMetadataOrNullID(N->getStringLength()));
1637  Record.push_back(VE.getMetadataOrNullID(N->getStringLengthExp()));
1638  Record.push_back(N->getSizeInBits());
1639  Record.push_back(N->getAlignInBits());
1640  Record.push_back(N->getEncoding());
1641 
1642  Stream.EmitRecord(bitc::METADATA_STRING_TYPE, Record, Abbrev);
1643  Record.clear();
1644 }
1645 
1648  unsigned Abbrev) {
1649  Record.push_back(N->isDistinct());
1650  Record.push_back(N->getTag());
1651  Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1652  Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1653  Record.push_back(N->getLine());
1654  Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1655  Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
1656  Record.push_back(N->getSizeInBits());
1657  Record.push_back(N->getAlignInBits());
1658  Record.push_back(N->getOffsetInBits());
1659  Record.push_back(N->getFlags());
1660  Record.push_back(VE.getMetadataOrNullID(N->getExtraData()));
1661 
1662  // DWARF address space is encoded as N->getDWARFAddressSpace() + 1. 0 means
1663  // that there is no DWARF address space associated with DIDerivedType.
1664  if (const auto &DWARFAddressSpace = N->getDWARFAddressSpace())
1665  Record.push_back(*DWARFAddressSpace + 1);
1666  else
1667  Record.push_back(0);
1668 
1670  Record.clear();
1671 }
1672 
1675  unsigned Abbrev) {
1676  const unsigned IsNotUsedInOldTypeRef = 0x2;
1677  Record.push_back(IsNotUsedInOldTypeRef | (unsigned)N->isDistinct());
1678  Record.push_back(N->getTag());
1679  Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1680  Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1681  Record.push_back(N->getLine());
1682  Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1683  Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
1684  Record.push_back(N->getSizeInBits());
1685  Record.push_back(N->getAlignInBits());
1686  Record.push_back(N->getOffsetInBits());
1687  Record.push_back(N->getFlags());
1688  Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
1689  Record.push_back(N->getRuntimeLang());
1690  Record.push_back(VE.getMetadataOrNullID(N->getVTableHolder()));
1691  Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
1692  Record.push_back(VE.getMetadataOrNullID(N->getRawIdentifier()));
1693  Record.push_back(VE.getMetadataOrNullID(N->getDiscriminator()));
1694  Record.push_back(VE.getMetadataOrNullID(N->getRawDataLocation()));
1695  Record.push_back(VE.getMetadataOrNullID(N->getRawAssociated()));
1696  Record.push_back(VE.getMetadataOrNullID(N->getRawAllocated()));
1697  Record.push_back(VE.getMetadataOrNullID(N->getRawRank()));
1698 
1700  Record.clear();
1701 }
1702 
1705  unsigned Abbrev) {
1706  const unsigned HasNoOldTypeRefs = 0x2;
1707  Record.push_back(HasNoOldTypeRefs | (unsigned)N->isDistinct());
1708  Record.push_back(N->getFlags());
1709  Record.push_back(VE.getMetadataOrNullID(N->getTypeArray().get()));
1710  Record.push_back(N->getCC());
1711 
1713  Record.clear();
1714 }
1715 
1718  unsigned Abbrev) {
1719  Record.push_back(N->isDistinct());
1720  Record.push_back(VE.getMetadataOrNullID(N->getRawFilename()));
1721  Record.push_back(VE.getMetadataOrNullID(N->getRawDirectory()));
1722  if (N->getRawChecksum()) {
1723  Record.push_back(N->getRawChecksum()->Kind);
1724  Record.push_back(VE.getMetadataOrNullID(N->getRawChecksum()->Value));
1725  } else {
1726  // Maintain backwards compatibility with the old internal representation of
1727  // CSK_None in ChecksumKind by writing nulls here when Checksum is None.
1728  Record.push_back(0);
1729  Record.push_back(VE.getMetadataOrNullID(nullptr));
1730  }
1731  auto Source = N->getRawSource();
1732  if (Source)
1733  Record.push_back(VE.getMetadataOrNullID(*Source));
1734 
1735  Stream.EmitRecord(bitc::METADATA_FILE, Record, Abbrev);
1736  Record.clear();
1737 }
1738 
1741  unsigned Abbrev) {
1742  assert(N->isDistinct() && "Expected distinct compile units");
1743  Record.push_back(/* IsDistinct */ true);
1744  Record.push_back(N->getSourceLanguage());
1745  Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1746  Record.push_back(VE.getMetadataOrNullID(N->getRawProducer()));
1747  Record.push_back(N->isOptimized());
1748  Record.push_back(VE.getMetadataOrNullID(N->getRawFlags()));
1749  Record.push_back(N->getRuntimeVersion());
1750  Record.push_back(VE.getMetadataOrNullID(N->getRawSplitDebugFilename()));
1751  Record.push_back(N->getEmissionKind());
1752  Record.push_back(VE.getMetadataOrNullID(N->getEnumTypes().get()));
1753  Record.push_back(VE.getMetadataOrNullID(N->getRetainedTypes().get()));
1754  Record.push_back(/* subprograms */ 0);
1755  Record.push_back(VE.getMetadataOrNullID(N->getGlobalVariables().get()));
1756  Record.push_back(VE.getMetadataOrNullID(N->getImportedEntities().get()));
1757  Record.push_back(N->getDWOId());
1758  Record.push_back(VE.getMetadataOrNullID(N->getMacros().get()));
1759  Record.push_back(N->getSplitDebugInlining());
1760  Record.push_back(N->getDebugInfoForProfiling());
1761  Record.push_back((unsigned)N->getNameTableKind());
1762  Record.push_back(N->getRangesBaseAddress());
1763  Record.push_back(VE.getMetadataOrNullID(N->getRawSysRoot()));
1764  Record.push_back(VE.getMetadataOrNullID(N->getRawSDK()));
1765 
1767  Record.clear();
1768 }
1769 
1772  unsigned Abbrev) {
1773  const uint64_t HasUnitFlag = 1 << 1;
1774  const uint64_t HasSPFlagsFlag = 1 << 2;
1775  Record.push_back(uint64_t(N->isDistinct()) | HasUnitFlag | HasSPFlagsFlag);
1776  Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1777  Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1778  Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
1779  Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1780  Record.push_back(N->getLine());
1781  Record.push_back(VE.getMetadataOrNullID(N->getType()));
1782  Record.push_back(N->getScopeLine());
1783  Record.push_back(VE.getMetadataOrNullID(N->getContainingType()));
1784  Record.push_back(N->getSPFlags());
1785  Record.push_back(N->getVirtualIndex());
1786  Record.push_back(N->getFlags());
1787  Record.push_back(VE.getMetadataOrNullID(N->getRawUnit()));
1788  Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
1789  Record.push_back(VE.getMetadataOrNullID(N->getDeclaration()));
1790  Record.push_back(VE.getMetadataOrNullID(N->getRetainedNodes().get()));
1791  Record.push_back(N->getThisAdjustment());
1792  Record.push_back(VE.getMetadataOrNullID(N->getThrownTypes().get()));
1793 
1794  Stream.EmitRecord(bitc::METADATA_SUBPROGRAM, Record, Abbrev);
1795  Record.clear();
1796 }
1797 
1800  unsigned Abbrev) {
1801  Record.push_back(N->isDistinct());
1802  Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1803  Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1804  Record.push_back(N->getLine());
1805  Record.push_back(N->getColumn());
1806 
1808  Record.clear();
1809 }
1810 
1813  unsigned Abbrev) {
1814  Record.push_back(N->isDistinct());
1815  Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1816  Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1817  Record.push_back(N->getDiscriminator());
1818 
1820  Record.clear();
1821 }
1822 
1825  unsigned Abbrev) {
1826  Record.push_back(N->isDistinct());
1827  Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1828  Record.push_back(VE.getMetadataOrNullID(N->getDecl()));
1829  Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1830  Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1831  Record.push_back(N->getLineNo());
1832 
1834  Record.clear();
1835 }
1836 
1839  unsigned Abbrev) {
1840  Record.push_back(N->isDistinct() | N->getExportSymbols() << 1);
1841  Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1842  Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1843 
1844  Stream.EmitRecord(bitc::METADATA_NAMESPACE, Record, Abbrev);
1845  Record.clear();
1846 }
1847 
1850  unsigned Abbrev) {
1851  Record.push_back(N->isDistinct());
1852  Record.push_back(N->getMacinfoType());
1853  Record.push_back(N->getLine());
1854  Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1855  Record.push_back(VE.getMetadataOrNullID(N->getRawValue()));
1856 
1857  Stream.EmitRecord(bitc::METADATA_MACRO, Record, Abbrev);
1858  Record.clear();
1859 }
1860 
1863  unsigned Abbrev) {
1864  Record.push_back(N->isDistinct());
1865  Record.push_back(N->getMacinfoType());
1866  Record.push_back(N->getLine());
1867  Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1868  Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
1869 
1870  Stream.EmitRecord(bitc::METADATA_MACRO_FILE, Record, Abbrev);
1871  Record.clear();
1872 }
1873 
1876  unsigned Abbrev) {
1877  Record.reserve(N->getArgs().size());
1878  for (ValueAsMetadata *MD : N->getArgs())
1879  Record.push_back(VE.getMetadataOrNullID(MD));
1880 
1881  Stream.EmitRecord(bitc::METADATA_ARG_LIST, Record, Abbrev);
1882  Record.clear();
1883 }
1884 
1887  unsigned Abbrev) {
1888  Record.push_back(N->isDistinct());
1889  for (auto &I : N->operands())
1890  Record.push_back(VE.getMetadataOrNullID(I));
1891  Record.push_back(N->getLineNo());
1892  Record.push_back(N->getIsDecl());
1893 
1894  Stream.EmitRecord(bitc::METADATA_MODULE, Record, Abbrev);
1895  Record.clear();
1896 }
1897 
1900  unsigned Abbrev) {
1901  Record.push_back(N->isDistinct());
1902  Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1903  Record.push_back(VE.getMetadataOrNullID(N->getType()));
1904  Record.push_back(N->isDefault());
1905 
1907  Record.clear();
1908 }
1909 
1912  unsigned Abbrev) {
1913  Record.push_back(N->isDistinct());
1914  Record.push_back(N->getTag());
1915  Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1916  Record.push_back(VE.getMetadataOrNullID(N->getType()));
1917  Record.push_back(N->isDefault());
1918  Record.push_back(VE.getMetadataOrNullID(N->getValue()));
1919 
1921  Record.clear();
1922 }
1923 
1926  unsigned Abbrev) {
1927  const uint64_t Version = 2 << 1;
1928  Record.push_back((uint64_t)N->isDistinct() | Version);
1929  Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1930  Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1931  Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
1932  Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1933  Record.push_back(N->getLine());
1934  Record.push_back(VE.getMetadataOrNullID(N->getType()));
1935  Record.push_back(N->isLocalToUnit());
1936  Record.push_back(N->isDefinition());
1937  Record.push_back(VE.getMetadataOrNullID(N->getStaticDataMemberDeclaration()));
1938  Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams()));
1939  Record.push_back(N->getAlignInBits());
1940 
1941  Stream.EmitRecord(bitc::METADATA_GLOBAL_VAR, Record, Abbrev);
1942  Record.clear();
1943 }
1944 
1947  unsigned Abbrev) {
1948  // In order to support all possible bitcode formats in BitcodeReader we need
1949  // to distinguish the following cases:
1950  // 1) Record has no artificial tag (Record[1]),
1951  // has no obsolete inlinedAt field (Record[9]).
1952  // In this case Record size will be 8, HasAlignment flag is false.
1953  // 2) Record has artificial tag (Record[1]),
1954  // has no obsolete inlignedAt field (Record[9]).
1955  // In this case Record size will be 9, HasAlignment flag is false.
1956  // 3) Record has both artificial tag (Record[1]) and
1957  // obsolete inlignedAt field (Record[9]).
1958  // In this case Record size will be 10, HasAlignment flag is false.
1959  // 4) Record has neither artificial tag, nor inlignedAt field, but
1960  // HasAlignment flag is true and Record[8] contains alignment value.
1961  const uint64_t HasAlignmentFlag = 1 << 1;
1962  Record.push_back((uint64_t)N->isDistinct() | HasAlignmentFlag);
1963  Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1964  Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1965  Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1966  Record.push_back(N->getLine());
1967  Record.push_back(VE.getMetadataOrNullID(N->getType()));
1968  Record.push_back(N->getArg());
1969  Record.push_back(N->getFlags());
1970  Record.push_back(N->getAlignInBits());
1971 
1972  Stream.EmitRecord(bitc::METADATA_LOCAL_VAR, Record, Abbrev);
1973  Record.clear();
1974 }
1975 
1978  unsigned Abbrev) {
1979  Record.push_back((uint64_t)N->isDistinct());
1980  Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1981  Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1982  Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1983  Record.push_back(N->getLine());
1984 
1985  Stream.EmitRecord(bitc::METADATA_LABEL, Record, Abbrev);
1986  Record.clear();
1987 }
1988 
1991  unsigned Abbrev) {
1992  Record.reserve(N->getElements().size() + 1);
1993  const uint64_t Version = 3 << 1;
1994  Record.push_back((uint64_t)N->isDistinct() | Version);
1995  Record.append(N->elements_begin(), N->elements_end());
1996 
1997  Stream.EmitRecord(bitc::METADATA_EXPRESSION, Record, Abbrev);
1998  Record.clear();
1999 }
2000 
2003  unsigned Abbrev) {
2004  Record.push_back(N->isDistinct());
2005  Record.push_back(VE.getMetadataOrNullID(N->getVariable()));
2006  Record.push_back(VE.getMetadataOrNullID(N->getExpression()));
2007 
2009  Record.clear();
2010 }
2011 
2014  unsigned Abbrev) {
2015  Record.push_back(N->isDistinct());
2016  Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2017  Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2018  Record.push_back(N->getLine());
2019  Record.push_back(VE.getMetadataOrNullID(N->getRawSetterName()));
2020  Record.push_back(VE.getMetadataOrNullID(N->getRawGetterName()));
2021  Record.push_back(N->getAttributes());
2022  Record.push_back(VE.getMetadataOrNullID(N->getType()));
2023 
2025  Record.clear();
2026 }
2027 
2030  unsigned Abbrev) {
2031  Record.push_back(N->isDistinct());
2032  Record.push_back(N->getTag());
2033  Record.push_back(VE.getMetadataOrNullID(N->getScope()));
2034  Record.push_back(VE.getMetadataOrNullID(N->getEntity()));
2035  Record.push_back(N->getLine());
2036  Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2037  Record.push_back(VE.getMetadataOrNullID(N->getRawFile()));
2038 
2040  Record.clear();
2041 }
2042 
2043 unsigned ModuleBitcodeWriter::createNamedMetadataAbbrev() {
2044  auto Abbv = std::make_shared<BitCodeAbbrev>();
2047  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
2048  return Stream.EmitAbbrev(std::move(Abbv));
2049 }
2050 
2051 void ModuleBitcodeWriter::writeNamedMetadata(
2053  if (M.named_metadata_empty())
2054  return;
2055 
2056  unsigned Abbrev = createNamedMetadataAbbrev();
2057  for (const NamedMDNode &NMD : M.named_metadata()) {
2058  // Write name.
2059  StringRef Str = NMD.getName();
2060  Record.append(Str.bytes_begin(), Str.bytes_end());
2061  Stream.EmitRecord(bitc::METADATA_NAME, Record, Abbrev);
2062  Record.clear();
2063 
2064  // Write named metadata operands.
2065  for (const MDNode *N : NMD.operands())
2066  Record.push_back(VE.getMetadataID(N));
2068  Record.clear();
2069  }
2070 }
2071 
2072 unsigned ModuleBitcodeWriter::createMetadataStringsAbbrev() {
2073  auto Abbv = std::make_shared<BitCodeAbbrev>();
2075  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of strings
2076  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // offset to chars
2078  return Stream.EmitAbbrev(std::move(Abbv));
2079 }
2080 
2081 /// Write out a record for MDString.
2082 ///
2083 /// All the metadata strings in a metadata block are emitted in a single
2084 /// record. The sizes and strings themselves are shoved into a blob.
2085 void ModuleBitcodeWriter::writeMetadataStrings(
2087  if (Strings.empty())
2088  return;
2089 
2090  // Start the record with the number of strings.
2091  Record.push_back(bitc::METADATA_STRINGS);
2092  Record.push_back(Strings.size());
2093 
2094  // Emit the sizes of the strings in the blob.
2095  SmallString<256> Blob;
2096  {
2097  BitstreamWriter W(Blob);
2098  for (const Metadata *MD : Strings)
2099  W.EmitVBR(cast<MDString>(MD)->getLength(), 6);
2100  W.FlushToWord();
2101  }
2102 
2103  // Add the offset to the strings to the record.
2104  Record.push_back(Blob.size());
2105 
2106  // Add the strings to the blob.
2107  for (const Metadata *MD : Strings)
2108  Blob.append(cast<MDString>(MD)->getString());
2109 
2110  // Emit the final record.
2111  Stream.EmitRecordWithBlob(createMetadataStringsAbbrev(), Record, Blob);
2112  Record.clear();
2113 }
2114 
2115 // Generates an enum to use as an index in the Abbrev array of Metadata record.
2116 enum MetadataAbbrev : unsigned {
2117 #define HANDLE_MDNODE_LEAF(CLASS) CLASS##AbbrevID,
2118 #include "llvm/IR/Metadata.def"
2120 };
2121 
2122 void ModuleBitcodeWriter::writeMetadataRecords(
2124  std::vector<unsigned> *MDAbbrevs, std::vector<uint64_t> *IndexPos) {
2125  if (MDs.empty())
2126  return;
2127 
2128  // Initialize MDNode abbreviations.
2129 #define HANDLE_MDNODE_LEAF(CLASS) unsigned CLASS##Abbrev = 0;
2130 #include "llvm/IR/Metadata.def"
2131 
2132  for (const Metadata *MD : MDs) {
2133  if (IndexPos)
2134  IndexPos->push_back(Stream.GetCurrentBitNo());
2135  if (const MDNode *N = dyn_cast<MDNode>(MD)) {
2136  assert(N->isResolved() && "Expected forward references to be resolved");
2137 
2138  switch (N->getMetadataID()) {
2139  default:
2140  llvm_unreachable("Invalid MDNode subclass");
2141 #define HANDLE_MDNODE_LEAF(CLASS) \
2142  case Metadata::CLASS##Kind: \
2143  if (MDAbbrevs) \
2144  write##CLASS(cast<CLASS>(N), Record, \
2145  (*MDAbbrevs)[MetadataAbbrev::CLASS##AbbrevID]); \
2146  else \
2147  write##CLASS(cast<CLASS>(N), Record, CLASS##Abbrev); \
2148  continue;
2149 #include "llvm/IR/Metadata.def"
2150  }
2151  }
2152  writeValueAsMetadata(cast<ValueAsMetadata>(MD), Record);
2153  }
2154 }
2155 
2156 void ModuleBitcodeWriter::writeModuleMetadata() {
2157  if (!VE.hasMDs() && M.named_metadata_empty())
2158  return;
2159 
2162 
2163  // Emit all abbrevs upfront, so that the reader can jump in the middle of the
2164  // block and load any metadata.
2165  std::vector<unsigned> MDAbbrevs;
2166 
2167  MDAbbrevs.resize(MetadataAbbrev::LastPlusOne);
2168  MDAbbrevs[MetadataAbbrev::DILocationAbbrevID] = createDILocationAbbrev();
2169  MDAbbrevs[MetadataAbbrev::GenericDINodeAbbrevID] =
2170  createGenericDINodeAbbrev();
2171 
2172  auto Abbv = std::make_shared<BitCodeAbbrev>();
2174  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2175  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2176  unsigned OffsetAbbrev = Stream.EmitAbbrev(std::move(Abbv));
2177 
2178  Abbv = std::make_shared<BitCodeAbbrev>();
2181  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
2182  unsigned IndexAbbrev = Stream.EmitAbbrev(std::move(Abbv));
2183 
2184  // Emit MDStrings together upfront.
2185  writeMetadataStrings(VE.getMDStrings(), Record);
2186 
2187  // We only emit an index for the metadata record if we have more than a given
2188  // (naive) threshold of metadatas, otherwise it is not worth it.
2189  if (VE.getNonMDStrings().size() > IndexThreshold) {
2190  // Write a placeholder value in for the offset of the metadata index,
2191  // which is written after the records, so that it can include
2192  // the offset of each entry. The placeholder offset will be
2193  // updated after all records are emitted.
2194  uint64_t Vals[] = {0, 0};
2195  Stream.EmitRecord(bitc::METADATA_INDEX_OFFSET, Vals, OffsetAbbrev);
2196  }
2197 
2198  // Compute and save the bit offset to the current position, which will be
2199  // patched when we emit the index later. We can simply subtract the 64-bit
2200  // fixed size from the current bit number to get the location to backpatch.
2201  uint64_t IndexOffsetRecordBitPos = Stream.GetCurrentBitNo();
2202 
2203  // This index will contain the bitpos for each individual record.
2204  std::vector<uint64_t> IndexPos;
2205  IndexPos.reserve(VE.getNonMDStrings().size());
2206 
2207  // Write all the records
2208  writeMetadataRecords(VE.getNonMDStrings(), Record, &MDAbbrevs, &IndexPos);
2209 
2210  if (VE.getNonMDStrings().size() > IndexThreshold) {
2211  // Now that we have emitted all the records we will emit the index. But
2212  // first
2213  // backpatch the forward reference so that the reader can skip the records
2214  // efficiently.
2215  Stream.BackpatchWord64(IndexOffsetRecordBitPos - 64,
2216  Stream.GetCurrentBitNo() - IndexOffsetRecordBitPos);
2217 
2218  // Delta encode the index.
2219  uint64_t PreviousValue = IndexOffsetRecordBitPos;
2220  for (auto &Elt : IndexPos) {
2221  auto EltDelta = Elt - PreviousValue;
2222  PreviousValue = Elt;
2223  Elt = EltDelta;
2224  }
2225  // Emit the index record.
2226  Stream.EmitRecord(bitc::METADATA_INDEX, IndexPos, IndexAbbrev);
2227  IndexPos.clear();
2228  }
2229 
2230  // Write the named metadata now.
2231  writeNamedMetadata(Record);
2232 
2233  auto AddDeclAttachedMetadata = [&](const GlobalObject &GO) {
2235  Record.push_back(VE.getValueID(&GO));
2236  pushGlobalMetadataAttachment(Record, GO);
2238  };
2239  for (const Function &F : M)
2240  if (F.isDeclaration() && F.hasMetadata())
2241  AddDeclAttachedMetadata(F);
2242  // FIXME: Only store metadata for declarations here, and move data for global
2243  // variable definitions to a separate block (PR28134).
2244  for (const GlobalVariable &GV : M.globals())
2245  if (GV.hasMetadata())
2246  AddDeclAttachedMetadata(GV);
2247 
2248  Stream.ExitBlock();
2249 }
2250 
2251 void ModuleBitcodeWriter::writeFunctionMetadata(const Function &F) {
2252  if (!VE.hasMDs())
2253  return;
2254 
2257  writeMetadataStrings(VE.getMDStrings(), Record);
2258  writeMetadataRecords(VE.getNonMDStrings(), Record);
2259  Stream.ExitBlock();
2260 }
2261 
2262 void ModuleBitcodeWriter::pushGlobalMetadataAttachment(
2264  // [n x [id, mdnode]]
2266  GO.getAllMetadata(MDs);
2267  for (const auto &I : MDs) {
2268  Record.push_back(I.first);
2269  Record.push_back(VE.getMetadataID(I.second));
2270  }
2271 }
2272 
2273 void ModuleBitcodeWriter::writeFunctionMetadataAttachment(const Function &F) {
2275 
2277 
2278  if (F.hasMetadata()) {
2279  pushGlobalMetadataAttachment(Record, F);
2281  Record.clear();
2282  }
2283 
2284  // Write metadata attachments
2285  // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]]
2287  for (const BasicBlock &BB : F)
2288  for (const Instruction &I : BB) {
2289  MDs.clear();
2290  I.getAllMetadataOtherThanDebugLoc(MDs);
2291 
2292  // If no metadata, ignore instruction.
2293  if (MDs.empty()) continue;
2294 
2295  Record.push_back(VE.getInstructionID(&I));
2296 
2297  for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
2298  Record.push_back(MDs[i].first);
2299  Record.push_back(VE.getMetadataID(MDs[i].second));
2300  }
2302  Record.clear();
2303  }
2304 
2305  Stream.ExitBlock();
2306 }
2307 
2308 void ModuleBitcodeWriter::writeModuleMetadataKinds() {
2310 
2311  // Write metadata kinds
2312  // METADATA_KIND - [n x [id, name]]
2314  M.getMDKindNames(Names);
2315 
2316  if (Names.empty()) return;
2317 
2319 
2320  for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) {
2321  Record.push_back(MDKindID);
2322  StringRef KName = Names[MDKindID];
2323  Record.append(KName.begin(), KName.end());
2324 
2326  Record.clear();
2327  }
2328 
2329  Stream.ExitBlock();
2330 }
2331 
2332 void ModuleBitcodeWriter::writeOperandBundleTags() {
2333  // Write metadata kinds
2334  //
2335  // OPERAND_BUNDLE_TAGS_BLOCK_ID : N x OPERAND_BUNDLE_TAG
2336  //
2337  // OPERAND_BUNDLE_TAG - [strchr x N]
2338 
2340  M.getOperandBundleTags(Tags);
2341 
2342  if (Tags.empty())
2343  return;
2344 
2346 
2348 
2349  for (auto Tag : Tags) {
2350  Record.append(Tag.begin(), Tag.end());
2351 
2353  Record.clear();
2354  }
2355 
2356  Stream.ExitBlock();
2357 }
2358 
2359 void ModuleBitcodeWriter::writeSyncScopeNames() {
2361  M.getContext().getSyncScopeNames(SSNs);
2362  if (SSNs.empty())
2363  return;
2364 
2366 
2368  for (auto SSN : SSNs) {
2369  Record.append(SSN.begin(), SSN.end());
2371  Record.clear();
2372  }
2373 
2374  Stream.ExitBlock();
2375 }
2376 
2377 void ModuleBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal,
2378  bool isGlobal) {
2379  if (FirstVal == LastVal) return;
2380 
2382 
2383  unsigned AggregateAbbrev = 0;
2384  unsigned String8Abbrev = 0;
2385  unsigned CString7Abbrev = 0;
2386  unsigned CString6Abbrev = 0;
2387  // If this is a constant pool for the module, emit module-specific abbrevs.
2388  if (isGlobal) {
2389  // Abbrev for CST_CODE_AGGREGATE.
2390  auto Abbv = std::make_shared<BitCodeAbbrev>();
2393  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
2394  AggregateAbbrev = Stream.EmitAbbrev(std::move(Abbv));
2395 
2396  // Abbrev for CST_CODE_STRING.
2397  Abbv = std::make_shared<BitCodeAbbrev>();
2400  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
2401  String8Abbrev = Stream.EmitAbbrev(std::move(Abbv));
2402  // Abbrev for CST_CODE_CSTRING.
2403  Abbv = std::make_shared<BitCodeAbbrev>();
2406  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
2407  CString7Abbrev = Stream.EmitAbbrev(std::move(Abbv));
2408  // Abbrev for CST_CODE_CSTRING.
2409  Abbv = std::make_shared<BitCodeAbbrev>();
2413  CString6Abbrev = Stream.EmitAbbrev(std::move(Abbv));
2414  }
2415 
2417 
2418  const ValueEnumerator::ValueList &Vals = VE.getValues();
2419  Type *LastTy = nullptr;
2420  for (unsigned i = FirstVal; i != LastVal; ++i) {
2421  const Value *V = Vals[i].first;
2422  // If we need to switch types, do so now.
2423  if (V->getType() != LastTy) {
2424  LastTy = V->getType();
2425  Record.push_back(VE.getTypeID(LastTy));
2427  CONSTANTS_SETTYPE_ABBREV);
2428  Record.clear();
2429  }
2430 
2431  if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
2432  Record.push_back(unsigned(IA->hasSideEffects()) |
2433  unsigned(IA->isAlignStack()) << 1 |
2434  unsigned(IA->getDialect()&1) << 2);
2435 
2436  // Add the asm string.
2437  const std::string &AsmStr = IA->getAsmString();
2438  Record.push_back(AsmStr.size());
2439  Record.append(AsmStr.begin(), AsmStr.end());
2440 
2441  // Add the constraint string.
2442  const std::string &ConstraintStr = IA->getConstraintString();
2443  Record.push_back(ConstraintStr.size());
2444  Record.append(ConstraintStr.begin(), ConstraintStr.end());
2446  Record.clear();
2447  continue;
2448  }
2449  const Constant *C = cast<Constant>(V);
2450  unsigned Code = -1U;
2451  unsigned AbbrevToUse = 0;
2452  if (C->isNullValue()) {
2454  } else if (isa<PoisonValue>(C)) {
2456  } else if (isa<UndefValue>(C)) {
2458  } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
2459  if (IV->getBitWidth() <= 64) {
2460  uint64_t V = IV->getSExtValue();
2461  emitSignedInt64(Record, V);
2463  AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
2464  } else { // Wide integers, > 64 bits in size.
2465  emitWideAPInt(Record, IV->getValue());
2467  }
2468  } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
2470  Type *Ty = CFP->getType();
2471  if (Ty->isHalfTy() || Ty->isBFloatTy() || Ty->isFloatTy() ||
2472  Ty->isDoubleTy()) {
2473  Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
2474  } else if (Ty->isX86_FP80Ty()) {
2475  // api needed to prevent premature destruction
2476  // bits are not in the same order as a normal i80 APInt, compensate.
2477  APInt api = CFP->getValueAPF().bitcastToAPInt();
2478  const uint64_t *p = api.getRawData();
2479  Record.push_back((p[1] << 48) | (p[0] >> 16));
2480  Record.push_back(p[0] & 0xffffLL);
2481  } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) {
2482  APInt api = CFP->getValueAPF().bitcastToAPInt();
2483  const uint64_t *p = api.getRawData();
2484  Record.push_back(p[0]);
2485  Record.push_back(p[1]);
2486  } else {
2487  assert(0 && "Unknown FP type!");
2488  }
2489  } else if (isa<ConstantDataSequential>(C) &&
2490  cast<ConstantDataSequential>(C)->isString()) {
2491  const ConstantDataSequential *Str = cast<ConstantDataSequential>(C);
2492  // Emit constant strings specially.
2493  unsigned NumElts = Str->getNumElements();
2494  // If this is a null-terminated string, use the denser CSTRING encoding.
2495  if (Str->isCString()) {
2497  --NumElts; // Don't encode the null, which isn't allowed by char6.
2498  } else {
2500  AbbrevToUse = String8Abbrev;
2501  }
2502  bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
2503  bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
2504  for (unsigned i = 0; i != NumElts; ++i) {
2505  unsigned char V = Str->getElementAsInteger(i);
2506  Record.push_back(V);
2507  isCStr7 &= (V & 128) == 0;
2508  if (isCStrChar6)
2509  isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
2510  }
2511 
2512  if (isCStrChar6)
2513  AbbrevToUse = CString6Abbrev;
2514  else if (isCStr7)
2515  AbbrevToUse = CString7Abbrev;
2516  } else if (const ConstantDataSequential *CDS =
2517  dyn_cast<ConstantDataSequential>(C)) {
2519  Type *EltTy = CDS->getElementType();
2520  if (isa<IntegerType>(EltTy)) {
2521  for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
2522  Record.push_back(CDS->getElementAsInteger(i));
2523  } else {
2524  for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
2525  Record.push_back(
2526  CDS->getElementAsAPFloat(i).bitcastToAPInt().getLimitedValue());
2527  }
2528  } else if (isa<ConstantAggregate>(C)) {
2530  for (const Value *Op : C->operands())
2531  Record.push_back(VE.getValueID(Op));
2532  AbbrevToUse = AggregateAbbrev;
2533  } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
2534  switch (CE->getOpcode()) {
2535  default:
2536  if (Instruction::isCast(CE->getOpcode())) {
2538  Record.push_back(getEncodedCastOpcode(CE->getOpcode()));
2539  Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
2540  Record.push_back(VE.getValueID(C->getOperand(0)));
2541  AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
2542  } else {
2543  assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
2545  Record.push_back(getEncodedBinaryOpcode(CE->getOpcode()));
2546  Record.push_back(VE.getValueID(C->getOperand(0)));
2547  Record.push_back(VE.getValueID(C->getOperand(1)));
2548  uint64_t Flags = getOptimizationFlags(CE);
2549  if (Flags != 0)
2550  Record.push_back(Flags);
2551  }
2552  break;
2553  case Instruction::FNeg: {
2554  assert(CE->getNumOperands() == 1 && "Unknown constant expr!");
2556  Record.push_back(getEncodedUnaryOpcode(CE->getOpcode()));
2557  Record.push_back(VE.getValueID(C->getOperand(0)));
2558  uint64_t Flags = getOptimizationFlags(CE);
2559  if (Flags != 0)
2560  Record.push_back(Flags);
2561  break;
2562  }
2563  case Instruction::GetElementPtr: {
2565  const auto *GO = cast<GEPOperator>(C);
2566  Record.push_back(VE.getTypeID(GO->getSourceElementType()));
2567  if (Optional<unsigned> Idx = GO->getInRangeIndex()) {
2569  Record.push_back((*Idx << 1) | GO->isInBounds());
2570  } else if (GO->isInBounds())
2572  for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
2573  Record.push_back(VE.getTypeID(C->getOperand(i)->getType()));
2574  Record.push_back(VE.getValueID(C->getOperand(i)));
2575  }
2576  break;
2577  }
2578  case Instruction::Select:
2580  Record.push_back(VE.getValueID(C->getOperand(0)));
2581  Record.push_back(VE.getValueID(C->getOperand(1)));
2582  Record.push_back(VE.getValueID(C->getOperand(2)));
2583  break;
2584  case Instruction::ExtractElement:
2586  Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
2587  Record.push_back(VE.getValueID(C->getOperand(0)));
2588  Record.push_back(VE.getTypeID(C->getOperand(1)->getType()));
2589  Record.push_back(VE.getValueID(C->getOperand(1)));
2590  break;
2591  case Instruction::InsertElement:
2593  Record.push_back(VE.getValueID(C->getOperand(0)));
2594  Record.push_back(VE.getValueID(C->getOperand(1)));
2595  Record.push_back(VE.getTypeID(C->getOperand(2)->getType()));
2596  Record.push_back(VE.getValueID(C->getOperand(2)));
2597  break;
2598  case Instruction::ShuffleVector:
2599  // If the return type and argument types are the same, this is a
2600  // standard shufflevector instruction. If the types are different,
2601  // then the shuffle is widening or truncating the input vectors, and
2602  // the argument type must also be encoded.
2603  if (C->getType() == C->getOperand(0)->getType()) {
2605  } else {
2607  Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
2608  }
2609  Record.push_back(VE.getValueID(C->getOperand(0)));
2610  Record.push_back(VE.getValueID(C->getOperand(1)));
2611  Record.push_back(VE.getValueID(CE->getShuffleMaskForBitcode()));
2612  break;
2613  case Instruction::ICmp:
2614  case Instruction::FCmp:
2616  Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
2617  Record.push_back(VE.getValueID(C->getOperand(0)));
2618  Record.push_back(VE.getValueID(C->getOperand(1)));
2619  Record.push_back(CE->getPredicate());
2620  break;
2621  }
2622  } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
2624  Record.push_back(VE.getTypeID(BA->getFunction()->getType()));
2625  Record.push_back(VE.getValueID(BA->getFunction()));
2626  Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock()));
2627  } else if (const auto *Equiv = dyn_cast<DSOLocalEquivalent>(C)) {
2629  Record.push_back(VE.getTypeID(Equiv->getGlobalValue()->getType()));
2630  Record.push_back(VE.getValueID(Equiv->getGlobalValue()));
2631  } else {
2632 #ifndef NDEBUG
2633  C->dump();
2634 #endif
2635  llvm_unreachable("Unknown constant!");
2636  }
2637  Stream.EmitRecord(Code, Record, AbbrevToUse);
2638  Record.clear();
2639  }
2640 
2641  Stream.ExitBlock();
2642 }
2643 
2644 void ModuleBitcodeWriter::writeModuleConstants() {
2645  const ValueEnumerator::ValueList &Vals = VE.getValues();
2646 
2647  // Find the first constant to emit, which is the first non-globalvalue value.
2648  // We know globalvalues have been emitted by WriteModuleInfo.
2649  for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
2650  if (!isa<GlobalValue>(Vals[i].first)) {
2651  writeConstants(i, Vals.size(), true);
2652  return;
2653  }
2654  }
2655 }
2656 
2657 /// pushValueAndType - The file has to encode both the value and type id for
2658 /// many values, because we need to know what type to create for forward
2659 /// references. However, most operands are not forward references, so this type
2660 /// field is not needed.
2661 ///
2662 /// This function adds V's value ID to Vals. If the value ID is higher than the
2663 /// instruction ID, then it is a forward reference, and it also includes the
2664 /// type ID. The value ID that is written is encoded relative to the InstID.
2665 bool ModuleBitcodeWriter::pushValueAndType(const Value *V, unsigned InstID,
2666  SmallVectorImpl<unsigned> &Vals) {
2667  unsigned ValID = VE.getValueID(V);
2668  // Make encoding relative to the InstID.
2669  Vals.push_back(InstID - ValID);
2670  if (ValID >= InstID) {
2671  Vals.push_back(VE.getTypeID(V->getType()));
2672  return true;
2673  }
2674  return false;
2675 }
2676 
2677 void ModuleBitcodeWriter::writeOperandBundles(const CallBase &CS,
2678  unsigned InstID) {
2680  LLVMContext &C = CS.getContext();
2681 
2682  for (unsigned i = 0, e = CS.getNumOperandBundles(); i != e; ++i) {
2683  const auto &Bundle = CS.getOperandBundleAt(i);
2684  Record.push_back(C.getOperandBundleTagID(Bundle.getTagName()));
2685 
2686  for (auto &Input : Bundle.Inputs)
2687  pushValueAndType(Input, InstID, Record);
2688 
2690  Record.clear();
2691  }
2692 }
2693 
2694 /// pushValue - Like pushValueAndType, but where the type of the value is
2695 /// omitted (perhaps it was already encoded in an earlier operand).
2696 void ModuleBitcodeWriter::pushValue(const Value *V, unsigned InstID,
2697  SmallVectorImpl<unsigned> &Vals) {
2698  unsigned ValID = VE.getValueID(V);
2699  Vals.push_back(InstID - ValID);
2700 }
2701 
2702 void ModuleBitcodeWriter::pushValueSigned(const Value *V, unsigned InstID,
2703  SmallVectorImpl<uint64_t> &Vals) {
2704  unsigned ValID = VE.getValueID(V);
2705  int64_t diff = ((int32_t)InstID - (int32_t)ValID);
2706  emitSignedInt64(Vals, diff);
2707 }
2708 
2709 /// WriteInstruction - Emit an instruction to the specified stream.
2710 void ModuleBitcodeWriter::writeInstruction(const Instruction &I,
2711  unsigned InstID,
2712  SmallVectorImpl<unsigned> &Vals) {
2713  unsigned Code = 0;
2714  unsigned AbbrevToUse = 0;
2715  VE.setInstructionID(&I);
2716  switch (I.getOpcode()) {
2717  default:
2718  if (Instruction::isCast(I.getOpcode())) {
2720  if (!pushValueAndType(I.getOperand(0), InstID, Vals))
2721  AbbrevToUse = FUNCTION_INST_CAST_ABBREV;
2722  Vals.push_back(VE.getTypeID(I.getType()));
2723  Vals.push_back(getEncodedCastOpcode(I.getOpcode()));
2724  } else {
2725  assert(isa<BinaryOperator>(I) && "Unknown instruction!");
2727  if (!pushValueAndType(I.getOperand(0), InstID, Vals))
2728  AbbrevToUse = FUNCTION_INST_BINOP_ABBREV;
2729  pushValue(I.getOperand(1), InstID, Vals);
2730  Vals.push_back(getEncodedBinaryOpcode(I.getOpcode()));
2731  uint64_t Flags = getOptimizationFlags(&I);
2732  if (Flags != 0) {
2733  if (AbbrevToUse == FUNCTION_INST_BINOP_ABBREV)
2734  AbbrevToUse = FUNCTION_INST_BINOP_FLAGS_ABBREV;
2735  Vals.push_back(Flags);
2736  }
2737  }
2738  break;
2739  case Instruction::FNeg: {
2741  if (!pushValueAndType(I.getOperand(0), InstID, Vals))
2742  AbbrevToUse = FUNCTION_INST_UNOP_ABBREV;
2743  Vals.push_back(getEncodedUnaryOpcode(I.getOpcode()));
2744  uint64_t Flags = getOptimizationFlags(&I);
2745  if (Flags != 0) {
2746  if (AbbrevToUse == FUNCTION_INST_UNOP_ABBREV)
2747  AbbrevToUse = FUNCTION_INST_UNOP_FLAGS_ABBREV;
2748  Vals.push_back(Flags);
2749  }
2750  break;
2751  }
2752  case Instruction::GetElementPtr: {
2754  AbbrevToUse = FUNCTION_INST_GEP_ABBREV;
2755  auto &GEPInst = cast<GetElementPtrInst>(I);
2756  Vals.push_back(GEPInst.isInBounds());
2757  Vals.push_back(VE.getTypeID(GEPInst.getSourceElementType()));
2758  for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
2759  pushValueAndType(I.getOperand(i), InstID, Vals);
2760  break;
2761  }
2762  case Instruction::ExtractValue: {
2764  pushValueAndType(I.getOperand(0), InstID, Vals);
2765  const ExtractValueInst *EVI = cast<ExtractValueInst>(&I);
2766  Vals.append(EVI->idx_begin(), EVI->idx_end());
2767  break;
2768  }
2769  case Instruction::InsertValue: {
2771  pushValueAndType(I.getOperand(0), InstID, Vals);
2772  pushValueAndType(I.getOperand(1), InstID, Vals);
2773  const InsertValueInst *IVI = cast<InsertValueInst>(&I);
2774  Vals.append(IVI->idx_begin(), IVI->idx_end());
2775  break;
2776  }
2777  case Instruction::Select: {
2779  pushValueAndType(I.getOperand(1), InstID, Vals);
2780  pushValue(I.getOperand(2), InstID, Vals);
2781  pushValueAndType(I.getOperand(0), InstID, Vals);
2782  uint64_t Flags = getOptimizationFlags(&I);
2783  if (Flags != 0)
2784  Vals.push_back(Flags);
2785  break;
2786  }
2787  case Instruction::ExtractElement:
2789  pushValueAndType(I.getOperand(0), InstID, Vals);
2790  pushValueAndType(I.getOperand(1), InstID, Vals);
2791  break;
2792  case Instruction::InsertElement:
2794  pushValueAndType(I.getOperand(0), InstID, Vals);
2795  pushValue(I.getOperand(1), InstID, Vals);
2796  pushValueAndType(I.getOperand(2), InstID, Vals);
2797  break;
2798  case Instruction::ShuffleVector:
2800  pushValueAndType(I.getOperand(0), InstID, Vals);
2801  pushValue(I.getOperand(1), InstID, Vals);
2802  pushValue(cast<ShuffleVectorInst>(I).getShuffleMaskForBitcode(), InstID,
2803  Vals);
2804  break;
2805  case Instruction::ICmp:
2806  case Instruction::FCmp: {
2807  // compare returning Int1Ty or vector of Int1Ty
2809  pushValueAndType(I.getOperand(0), InstID, Vals);
2810  pushValue(I.getOperand(1), InstID, Vals);
2811  Vals.push_back(cast<CmpInst>(I).getPredicate());
2812  uint64_t Flags = getOptimizationFlags(&I);
2813  if (Flags != 0)
2814  Vals.push_back(Flags);
2815  break;
2816  }
2817 
2818  case Instruction::Ret:
2819  {
2821  unsigned NumOperands = I.getNumOperands();
2822  if (NumOperands == 0)
2823  AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV;
2824  else if (NumOperands == 1) {
2825  if (!pushValueAndType(I.getOperand(0), InstID, Vals))
2826  AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV;
2827  } else {
2828  for (unsigned i = 0, e = NumOperands; i != e; ++i)
2829  pushValueAndType(I.getOperand(i), InstID, Vals);
2830  }
2831  }
2832  break;
2833  case Instruction::Br:
2834  {
2836  const BranchInst &II = cast<BranchInst>(I);
2837  Vals.push_back(VE.getValueID(II.getSuccessor(0)));
2838  if (II.isConditional()) {
2839  Vals.push_back(VE.getValueID(II.getSuccessor(1)));
2840  pushValue(II.getCondition(), InstID, Vals);
2841  }
2842  }
2843  break;
2844  case Instruction::Switch:
2845  {
2847  const SwitchInst &SI = cast<SwitchInst>(I);
2848  Vals.push_back(VE.getTypeID(SI.getCondition()->getType()));
2849  pushValue(SI.getCondition(), InstID, Vals);
2850  Vals.push_back(VE.getValueID(SI.getDefaultDest()));
2851  for (auto Case : SI.cases()) {
2852  Vals.push_back(VE.getValueID(Case.getCaseValue()));
2853  Vals.push_back(VE.getValueID(Case.getCaseSuccessor()));
2854  }
2855  }
2856  break;
2857  case Instruction::IndirectBr:
2859  Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
2860  // Encode the address operand as relative, but not the basic blocks.
2861  pushValue(I.getOperand(0), InstID, Vals);
2862  for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i)
2863  Vals.push_back(VE.getValueID(I.getOperand(i)));
2864  break;
2865 
2866  case Instruction::Invoke: {
2867  const InvokeInst *II = cast<InvokeInst>(&I);
2868  const Value *Callee = II->getCalledOperand();
2869  FunctionType *FTy = II->getFunctionType();
2870 
2871  if (II->hasOperandBundles())
2872  writeOperandBundles(*II, InstID);
2873 
2875 
2876  Vals.push_back(VE.getAttributeListID(II->getAttributes()));
2877  Vals.push_back(II->getCallingConv() | 1 << 13);
2878  Vals.push_back(VE.getValueID(II->getNormalDest()));
2879  Vals.push_back(VE.getValueID(II->getUnwindDest()));
2880  Vals.push_back(VE.getTypeID(FTy));
2881  pushValueAndType(Callee, InstID, Vals);
2882 
2883  // Emit value #'s for the fixed parameters.
2884  for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
2885  pushValue(I.getOperand(i), InstID, Vals); // fixed param.
2886 
2887  // Emit type/value pairs for varargs params.
2888  if (FTy->isVarArg()) {
2889  for (unsigned i = FTy->getNumParams(), e = II->getNumArgOperands();
2890  i != e; ++i)
2891  pushValueAndType(I.getOperand(i), InstID, Vals); // vararg
2892  }
2893  break;
2894  }
2895  case Instruction::Resume:
2897  pushValueAndType(I.getOperand(0), InstID, Vals);
2898  break;
2899  case Instruction::CleanupRet: {
2901  const auto &CRI = cast<CleanupReturnInst>(I);
2902  pushValue(CRI.getCleanupPad(), InstID, Vals);
2903  if (CRI.hasUnwindDest())
2904  Vals.push_back(VE.getValueID(CRI.getUnwindDest()));
2905  break;
2906  }
2907  case Instruction::CatchRet: {
2909  const auto &CRI = cast<CatchReturnInst>(I);
2910  pushValue(CRI.getCatchPad(), InstID, Vals);
2911  Vals.push_back(VE.getValueID(CRI.getSuccessor()));
2912  break;
2913  }
2914  case Instruction::CleanupPad:
2915  case Instruction::CatchPad: {
2916  const auto &FuncletPad = cast<FuncletPadInst>(I);
2917  Code = isa<CatchPadInst>(FuncletPad) ? bitc::FUNC_CODE_INST_CATCHPAD
2919  pushValue(FuncletPad.getParentPad(), InstID, Vals);
2920 
2921  unsigned NumArgOperands = FuncletPad.getNumArgOperands();
2922  Vals.push_back(NumArgOperands);
2923  for (unsigned Op = 0; Op != NumArgOperands; ++Op)
2924  pushValueAndType(FuncletPad.getArgOperand(Op), InstID, Vals);
2925  break;
2926  }
2927  case Instruction::CatchSwitch: {
2929  const auto &CatchSwitch = cast<CatchSwitchInst>(I);
2930 
2931  pushValue(CatchSwitch.getParentPad(), InstID, Vals);
2932 
2933  unsigned NumHandlers = CatchSwitch.getNumHandlers();
2934  Vals.push_back(NumHandlers);
2935  for (const BasicBlock *CatchPadBB : CatchSwitch.handlers())
2936  Vals.push_back(VE.getValueID(CatchPadBB));
2937 
2938  if (CatchSwitch.hasUnwindDest())
2939  Vals.push_back(VE.getValueID(CatchSwitch.getUnwindDest()));
2940  break;
2941  }
2942  case Instruction::CallBr: {
2943  const CallBrInst *CBI = cast<CallBrInst>(&I);
2944  const Value *Callee = CBI->getCalledOperand();
2945  FunctionType *FTy = CBI->getFunctionType();
2946 
2947  if (CBI->hasOperandBundles())
2948  writeOperandBundles(*CBI, InstID);
2949 
2951 
2952  Vals.push_back(VE.getAttributeListID(CBI->getAttributes()));
2953 
2954  Vals.push_back(CBI->getCallingConv() << bitc::CALL_CCONV |
2956 
2957  Vals.push_back(VE.getValueID(CBI->getDefaultDest()));
2958  Vals.push_back(CBI->getNumIndirectDests());
2959  for (unsigned i = 0, e = CBI->getNumIndirectDests(); i != e; ++i)
2960  Vals.push_back(VE.getValueID(CBI->getIndirectDest(i)));
2961 
2962  Vals.push_back(VE.getTypeID(FTy));
2963  pushValueAndType(Callee, InstID, Vals);
2964 
2965  // Emit value #'s for the fixed parameters.
2966  for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
2967  pushValue(I.getOperand(i), InstID, Vals); // fixed param.
2968 
2969  // Emit type/value pairs for varargs params.
2970  if (FTy->isVarArg()) {
2971  for (unsigned i = FTy->getNumParams(), e = CBI->getNumArgOperands();
2972  i != e; ++i)
2973  pushValueAndType(I.getOperand(i), InstID, Vals); // vararg
2974  }
2975  break;
2976  }
2977  case Instruction::Unreachable:
2979  AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV;
2980  break;
2981 
2982  case Instruction::PHI: {
2983  const PHINode &PN = cast<PHINode>(I);
2985  // With the newer instruction encoding, forward references could give
2986  // negative valued IDs. This is most common for PHIs, so we use
2987  // signed VBRs.
2989  Vals64.push_back(VE.getTypeID(PN.getType()));
2990  for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
2991  pushValueSigned(PN.getIncomingValue(i), InstID, Vals64);
2992  Vals64.push_back(VE.getValueID(PN.getIncomingBlock(i)));
2993  }
2994 
2995  uint64_t Flags = getOptimizationFlags(&I);
2996  if (Flags != 0)
2997  Vals64.push_back(Flags);
2998 
2999  // Emit a Vals64 vector and exit.
3000  Stream.EmitRecord(Code, Vals64, AbbrevToUse);
3001  Vals64.clear();
3002  return;
3003  }
3004 
3005  case Instruction::LandingPad: {
3006  const LandingPadInst &LP = cast<LandingPadInst>(I);
3008  Vals.push_back(VE.getTypeID(LP.getType()));
3009  Vals.push_back(LP.isCleanup());
3010  Vals.push_back(LP.getNumClauses());
3011  for (unsigned I = 0, E = LP.getNumClauses(); I != E; ++I) {
3012  if (LP.isCatch(I))
3013  Vals.push_back(LandingPadInst::Catch);
3014  else
3015  Vals.push_back(LandingPadInst::Filter);
3016  pushValueAndType(LP.getClause(I), InstID, Vals);
3017  }
3018  break;
3019  }
3020 
3021  case Instruction::Alloca: {
3023  const AllocaInst &AI = cast<AllocaInst>(I);
3024  Vals.push_back(VE.getTypeID(AI.getAllocatedType()));
3025  Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
3026  Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
3027  using APV = AllocaPackedValues;
3028  unsigned Record = 0;
3029  Bitfield::set<APV::Align>(Record, getEncodedAlign(AI.getAlign()));
3030  Bitfield::set<APV::UsedWithInAlloca>(Record, AI.isUsedWithInAlloca());
3031  Bitfield::set<APV::ExplicitType>(Record, true);
3032  Bitfield::set<APV::SwiftError>(Record, AI.isSwiftError());
3033  Vals.push_back(Record);
3034  break;
3035  }
3036 
3037  case Instruction::Load:
3038  if (cast<LoadInst>(I).isAtomic()) {
3040  pushValueAndType(I.getOperand(0), InstID, Vals);
3041  } else {
3043  if (!pushValueAndType(I.getOperand(0), InstID, Vals)) // ptr
3044  AbbrevToUse = FUNCTION_INST_LOAD_ABBREV;
3045  }
3046  Vals.push_back(VE.getTypeID(I.getType()));
3047  Vals.push_back(getEncodedAlign(cast<LoadInst>(I).getAlign()));
3048  Vals.push_back(cast<LoadInst>(I).isVolatile());
3049  if (cast<LoadInst>(I).isAtomic()) {
3050  Vals.push_back(getEncodedOrdering(cast<LoadInst>(I).getOrdering()));
3051  Vals.push_back(getEncodedSyncScopeID(cast<LoadInst>(I).getSyncScopeID()));
3052  }
3053  break;
3054  case Instruction::Store:
3055  if (cast<StoreInst>(I).isAtomic())
3057  else
3059  pushValueAndType(I.getOperand(1), InstID, Vals); // ptrty + ptr
3060  pushValueAndType(I.getOperand(0), InstID, Vals); // valty + val
3061  Vals.push_back(getEncodedAlign(cast<StoreInst>(I).getAlign()));
3062  Vals.push_back(cast<StoreInst>(I).isVolatile());
3063  if (cast<StoreInst>(I).isAtomic()) {
3064  Vals.push_back(getEncodedOrdering(cast<StoreInst>(I).getOrdering()));
3065  Vals.push_back(
3066  getEncodedSyncScopeID(cast<StoreInst>(I).getSyncScopeID()));
3067  }
3068  break;
3069  case Instruction::AtomicCmpXchg:
3071  pushValueAndType(I.getOperand(0), InstID, Vals); // ptrty + ptr
3072  pushValueAndType(I.getOperand(1), InstID, Vals); // cmp.
3073  pushValue(I.getOperand(2), InstID, Vals); // newval.
3074  Vals.push_back(cast<AtomicCmpXchgInst>(I).isVolatile());
3075  Vals.push_back(
3076  getEncodedOrdering(cast<AtomicCmpXchgInst>(I).getSuccessOrdering()));
3077  Vals.push_back(
3078  getEncodedSyncScopeID(cast<AtomicCmpXchgInst>(I).getSyncScopeID()));
3079  Vals.push_back(
3080  getEncodedOrdering(cast<AtomicCmpXchgInst>(I).getFailureOrdering()));
3081  Vals.push_back(cast<AtomicCmpXchgInst>(I).isWeak());
3082  Vals.push_back(getEncodedAlign(cast<AtomicCmpXchgInst>(I).getAlign()));
3083  break;
3084  case Instruction::AtomicRMW:
3086  pushValueAndType(I.getOperand(0), InstID, Vals); // ptrty + ptr
3087  pushValue(I.getOperand(1), InstID, Vals); // val.
3088  Vals.push_back(
3089  getEncodedRMWOperation(cast<AtomicRMWInst>(I).getOperation()));
3090  Vals.push_back(cast<AtomicRMWInst>(I).isVolatile());
3091  Vals.push_back(getEncodedOrdering(cast<AtomicRMWInst>(I).getOrdering()));
3092  Vals.push_back(
3093  getEncodedSyncScopeID(cast<AtomicRMWInst>(I).getSyncScopeID()));
3094  Vals.push_back(getEncodedAlign(cast<AtomicRMWInst>(I).getAlign()));
3095  break;
3096  case Instruction::Fence:
3098  Vals.push_back(getEncodedOrdering(cast<FenceInst>(I).getOrdering()));
3099  Vals.push_back(getEncodedSyncScopeID(cast<FenceInst>(I).getSyncScopeID()));
3100  break;
3101  case Instruction::Call: {
3102  const CallInst &CI = cast<CallInst>(I);
3103  FunctionType *FTy = CI.getFunctionType();
3104 
3105  if (CI.hasOperandBundles())
3106  writeOperandBundles(CI, InstID);
3107 
3109 
3110  Vals.push_back(VE.getAttributeListID(CI.getAttributes()));
3111 
3112  unsigned Flags = getOptimizationFlags(&I);
3113  Vals.push_back(CI.getCallingConv() << bitc::CALL_CCONV |
3114  unsigned(CI.isTailCall()) << bitc::CALL_TAIL |
3115  unsigned(CI.isMustTailCall()) << bitc::CALL_MUSTTAIL |
3117  unsigned(CI.isNoTailCall()) << bitc::CALL_NOTAIL |
3118  unsigned(Flags != 0) << bitc::CALL_FMF);
3119  if (Flags != 0)
3120  Vals.push_back(Flags);
3121 
3122  Vals.push_back(VE.getTypeID(FTy));
3123  pushValueAndType(CI.getCalledOperand(), InstID, Vals); // Callee
3124 
3125  // Emit value #'s for the fixed parameters.
3126  for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
3127  // Check for labels (can happen with asm labels).
3128  if (FTy->getParamType(i)->isLabelTy())
3129  Vals.push_back(VE.getValueID(CI.getArgOperand(i)));
3130  else
3131  pushValue(CI.getArgOperand(i), InstID, Vals); // fixed param.
3132  }
3133 
3134  // Emit type/value pairs for varargs params.
3135  if (FTy->isVarArg()) {
3136  for (unsigned i = FTy->getNumParams(), e = CI.getNumArgOperands();
3137  i != e; ++i)
3138  pushValueAndType(CI.getArgOperand(i), InstID, Vals); // varargs
3139  }
3140  break;
3141  }
3142  case Instruction::VAArg:
3144  Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty
3145  pushValue(I.getOperand(0), InstID, Vals); // valist.
3146  Vals.push_back(VE.getTypeID(I.getType())); // restype.
3147  break;
3148  case Instruction::Freeze:
3150  pushValueAndType(I.getOperand(0), InstID, Vals);
3151  break;
3152  }
3153 
3154  Stream.EmitRecord(Code, Vals, AbbrevToUse);
3155  Vals.clear();
3156 }
3157 
3158 /// Write a GlobalValue VST to the module. The purpose of this data structure is
3159 /// to allow clients to efficiently find the function body.
3160 void ModuleBitcodeWriter::writeGlobalValueSymbolTable(
3161  DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex) {
3162  // Get the offset of the VST we are writing, and backpatch it into
3163  // the VST forward declaration record.
3164  uint64_t VSTOffset = Stream.GetCurrentBitNo();
3165  // The BitcodeStartBit was the stream offset of the identification block.
3166  VSTOffset -= bitcodeStartBit();
3167  assert((VSTOffset & 31) == 0 && "VST block not 32-bit aligned");
3168  // Note that we add 1 here because the offset is relative to one word
3169  // before the start of the identification block, which was historically
3170  // always the start of the regular bitcode header.
3171  Stream.BackpatchWord(VSTOffsetPlaceholder, VSTOffset / 32 + 1);
3172 
3174 
3175  auto Abbv = std::make_shared<BitCodeAbbrev>();
3177  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
3178  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
3179  unsigned FnEntryAbbrev = Stream.EmitAbbrev(std::move(Abbv));
3180 
3181  for (const Function &F : M) {
3182  uint64_t Record[2];
3183 
3184  if (F.isDeclaration())
3185  continue;
3186 
3187  Record[0] = VE.getValueID(&F);
3188 
3189  // Save the word offset of the function (from the start of the
3190  // actual bitcode written to the stream).
3191  uint64_t BitcodeIndex = FunctionToBitcodeIndex[&F] - bitcodeStartBit();
3192  assert((BitcodeIndex & 31) == 0 && "function block not 32-bit aligned");
3193  // Note that we add 1 here because the offset is relative to one word
3194  // before the start of the identification block, which was historically
3195  // always the start of the regular bitcode header.
3196  Record[1] = BitcodeIndex / 32 + 1;
3197 
3198  Stream.EmitRecord(bitc::VST_CODE_FNENTRY, Record, FnEntryAbbrev);
3199  }
3200 
3201  Stream.ExitBlock();
3202 }
3203 
3204 /// Emit names for arguments, instructions and basic blocks in a function.
3205 void ModuleBitcodeWriter::writeFunctionLevelValueSymbolTable(
3206  const ValueSymbolTable &VST) {
3207  if (VST.empty())
3208  return;
3209 
3211 
3212  // FIXME: Set up the abbrev, we know how many values there are!
3213  // FIXME: We know if the type names can use 7-bit ascii.
3214  SmallVector<uint64_t, 64> NameVals;
3215 
3216  for (const ValueName &Name : VST) {
3217  // Figure out the encoding to use for the name.
3219 
3220  unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
3221  NameVals.push_back(VE.getValueID(Name.getValue()));
3222 
3223  // VST_CODE_ENTRY: [valueid, namechar x N]
3224  // VST_CODE_BBENTRY: [bbid, namechar x N]
3225  unsigned Code;
3226  if (isa<BasicBlock>(Name.getValue())) {
3228  if (Bits == SE_Char6)
3229  AbbrevToUse = VST_BBENTRY_6_ABBREV;
3230  } else {
3232  if (Bits == SE_Char6)
3233  AbbrevToUse = VST_ENTRY_6_ABBREV;
3234  else if (Bits == SE_Fixed7)
3235  AbbrevToUse = VST_ENTRY_7_ABBREV;
3236  }
3237 
3238  for (const auto P : Name.getKey())
3239  NameVals.push_back((unsigned char)P);
3240 
3241  // Emit the finished record.
3242  Stream.EmitRecord(Code, NameVals, AbbrevToUse);
3243  NameVals.clear();
3244  }
3245 
3246  Stream.ExitBlock();
3247 }
3248 
3249 void ModuleBitcodeWriter::writeUseList(UseListOrder &&Order) {
3250  assert(Order.Shuffle.size() >= 2 && "Shuffle too small");
3251  unsigned Code;
3252  if (isa<BasicBlock>(Order.V))
3254  else
3256 
3257  SmallVector<uint64_t, 64> Record(Order.Shuffle.begin(), Order.Shuffle.end());
3258  Record.push_back(VE.getValueID(Order.V));
3259  Stream.EmitRecord(Code, Record);
3260 }
3261 
3262 void ModuleBitcodeWriter::writeUseListBlock(const Function *F) {
3264  "Expected to be preserving use-list order");
3265 
3266  auto hasMore = [&]() {
3267  return !VE.UseListOrders.empty() && VE.UseListOrders.back().F == F;
3268  };
3269  if (!hasMore())
3270  // Nothing to do.
3271  return;
3272 
3274  while (hasMore()) {
3275  writeUseList(std::move(VE.UseListOrders.back()));
3276  VE.UseListOrders.pop_back();
3277  }
3278  Stream.ExitBlock();
3279 }
3280 
3281 /// Emit a function body to the module stream.
3282 void ModuleBitcodeWriter::writeFunction(
3283  const Function &F,
3284  DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex) {
3285  // Save the bitcode index of the start of this function block for recording
3286  // in the VST.
3287  FunctionToBitcodeIndex[&F] = Stream.GetCurrentBitNo();
3288 
3290  VE.incorporateFunction(F);
3291 
3293 
3294  // Emit the number of basic blocks, so the reader can create them ahead of
3295  // time.
3296  Vals.push_back(VE.getBasicBlocks().size());
3298  Vals.clear();
3299 
3300  // If there are function-local constants, emit them now.
3301  unsigned CstStart, CstEnd;
3302  VE.getFunctionConstantRange(CstStart, CstEnd);
3303  writeConstants(CstStart, CstEnd, false);
3304 
3305  // If there is function-local metadata, emit it now.
3306  writeFunctionMetadata(F);
3307 
3308  // Keep a running idea of what the instruction ID is.
3309  unsigned InstID = CstEnd;
3310 
3311  bool NeedsMetadataAttachment = F.hasMetadata();
3312 
3313  DILocation *LastDL = nullptr;
3314  // Finally, emit all the instructions, in order.
3315  for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
3316  for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
3317  I != E; ++I) {
3318  writeInstruction(*I, InstID, Vals);
3319 
3320  if (!I->getType()->isVoidTy())
3321  ++InstID;
3322 
3323  // If the instruction has metadata, write a metadata attachment later.
3324  NeedsMetadataAttachment |= I->hasMetadataOtherThanDebugLoc();
3325 
3326  // If the instruction has a debug location, emit it.
3327  DILocation *DL = I->getDebugLoc();
3328  if (!DL)
3329  continue;
3330 
3331  if (DL == LastDL) {
3332  // Just repeat the same debug loc as last time.
3334  continue;
3335  }
3336 
3337  Vals.push_back(DL->getLine());
3338  Vals.push_back(DL->getColumn());
3339  Vals.push_back(VE.getMetadataOrNullID(DL->getScope()));
3340  Vals.push_back(VE.getMetadataOrNullID(DL->getInlinedAt()));
3341  Vals.push_back(DL->isImplicitCode());
3342  Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC, Vals);
3343  Vals.clear();
3344 
3345  LastDL = DL;
3346  }
3347 
3348  // Emit names for all the instructions etc.
3349  if (auto *Symtab = F.getValueSymbolTable())
3350  writeFunctionLevelValueSymbolTable(*Symtab);
3351 
3352  if (NeedsMetadataAttachment)
3353  writeFunctionMetadataAttachment(F);
3354  if (VE.shouldPreserveUseListOrder())
3355  writeUseListBlock(&F);
3356  VE.purgeFunction();
3357  Stream.ExitBlock();
3358 }
3359 
3360 // Emit blockinfo, which defines the standard abbreviations etc.
3361 void ModuleBitcodeWriter::writeBlockInfo() {
3362  // We only want to emit block info records for blocks that have multiple
3363  // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK.
3364  // Other blocks can define their abbrevs inline.
3365  Stream.EnterBlockInfoBlock();
3366 
3367  { // 8-bit fixed-width VST_CODE_ENTRY/VST_CODE_BBENTRY strings.
3368  auto Abbv = std::make_shared<BitCodeAbbrev>();
3369  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));
3370  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3372  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
3374  VST_ENTRY_8_ABBREV)
3375  llvm_unreachable("Unexpected abbrev ordering!");
3376  }
3377 
3378  { // 7-bit fixed width VST_CODE_ENTRY strings.
3379  auto Abbv = std::make_shared<BitCodeAbbrev>();
3381  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3383  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
3385  VST_ENTRY_7_ABBREV)
3386  llvm_unreachable("Unexpected abbrev ordering!");
3387  }
3388  { // 6-bit char6 VST_CODE_ENTRY strings.
3389  auto Abbv = std::make_shared<BitCodeAbbrev>();
3391  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3395  VST_ENTRY_6_ABBREV)
3396  llvm_unreachable("Unexpected abbrev ordering!");
3397  }
3398  { // 6-bit char6 VST_CODE_BBENTRY strings.
3399  auto Abbv = std::make_shared<BitCodeAbbrev>();
3401  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3405  VST_BBENTRY_6_ABBREV)
3406  llvm_unreachable("Unexpected abbrev ordering!");
3407  }
3408 
3409  { // SETTYPE abbrev for CONSTANTS_BLOCK.
3410  auto Abbv = std::make_shared<BitCodeAbbrev>();
3414  if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) !=
3415  CONSTANTS_SETTYPE_ABBREV)
3416  llvm_unreachable("Unexpected abbrev ordering!");
3417  }
3418 
3419  { // INTEGER abbrev for CONSTANTS_BLOCK.
3420  auto Abbv = std::make_shared<BitCodeAbbrev>();
3422  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3423  if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) !=
3424  CONSTANTS_INTEGER_ABBREV)
3425  llvm_unreachable("Unexpected abbrev ordering!");
3426  }
3427 
3428  { // CE_CAST abbrev for CONSTANTS_BLOCK.
3429  auto Abbv = std::make_shared<BitCodeAbbrev>();
3431  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc
3432  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid
3434  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
3435 
3436  if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) !=
3437  CONSTANTS_CE_CAST_Abbrev)
3438  llvm_unreachable("Unexpected abbrev ordering!");
3439  }
3440  { // NULL abbrev for CONSTANTS_BLOCK.
3441  auto Abbv = std::make_shared<BitCodeAbbrev>();
3443  if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) !=
3444  CONSTANTS_NULL_Abbrev)
3445  llvm_unreachable("Unexpected abbrev ordering!");
3446  }
3447 
3448  // FIXME: This should only use space for first class types!
3449 
3450  { // INST_LOAD abbrev for FUNCTION_BLOCK.
3451  auto Abbv = std::make_shared<BitCodeAbbrev>();
3453  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr
3454  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
3456  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align
3457  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
3458  if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3459  FUNCTION_INST_LOAD_ABBREV)
3460  llvm_unreachable("Unexpected abbrev ordering!");
3461  }
3462  { // INST_UNOP abbrev for FUNCTION_BLOCK.
3463  auto Abbv = std::make_shared<BitCodeAbbrev>();
3465  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
3466  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
3467  if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3468  FUNCTION_INST_UNOP_ABBREV)
3469  llvm_unreachable("Unexpected abbrev ordering!");
3470  }
3471  { // INST_UNOP_FLAGS abbrev for FUNCTION_BLOCK.
3472  auto Abbv = std::make_shared<BitCodeAbbrev>();
3474  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
3475  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
3476  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); // flags
3477  if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3478  FUNCTION_INST_UNOP_FLAGS_ABBREV)
3479  llvm_unreachable("Unexpected abbrev ordering!");
3480  }
3481  { // INST_BINOP abbrev for FUNCTION_BLOCK.
3482  auto Abbv = std::make_shared<BitCodeAbbrev>();
3484  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
3485  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
3486  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
3487  if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3488  FUNCTION_INST_BINOP_ABBREV)
3489  llvm_unreachable("Unexpected abbrev ordering!");
3490  }
3491  { // INST_BINOP_FLAGS abbrev for FUNCTION_BLOCK.
3492  auto Abbv = std::make_shared<BitCodeAbbrev>();
3494  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
3495  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
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_BINOP_FLAGS_ABBREV)
3500  llvm_unreachable("Unexpected abbrev ordering!");
3501  }
3502  { // INST_CAST abbrev for FUNCTION_BLOCK.
3503  auto Abbv = std::make_shared<BitCodeAbbrev>();
3505  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal
3506  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
3508  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
3509  if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3510  FUNCTION_INST_CAST_ABBREV)
3511  llvm_unreachable("Unexpected abbrev ordering!");
3512  }
3513 
3514  { // INST_RET abbrev for FUNCTION_BLOCK.
3515  auto Abbv = std::make_shared<BitCodeAbbrev>();
3517  if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3518  FUNCTION_INST_RET_VOID_ABBREV)
3519  llvm_unreachable("Unexpected abbrev ordering!");
3520  }
3521  { // INST_RET abbrev for FUNCTION_BLOCK.
3522  auto Abbv = std::make_shared<BitCodeAbbrev>();
3524  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID
3525  if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3526  FUNCTION_INST_RET_VAL_ABBREV)
3527  llvm_unreachable("Unexpected abbrev ordering!");
3528  }
3529  { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
3530  auto Abbv = std::make_shared<BitCodeAbbrev>();
3532  if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3533  FUNCTION_INST_UNREACHABLE_ABBREV)
3534  llvm_unreachable("Unexpected abbrev ordering!");
3535  }
3536  {
3537  auto Abbv = std::make_shared<BitCodeAbbrev>();
3539  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
3540  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
3541  Log2_32_Ceil(VE.getTypes().size() + 1)));
3543  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
3544  if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3545  FUNCTION_INST_GEP_ABBREV)
3546  llvm_unreachable("Unexpected abbrev ordering!");
3547  }
3548 
3549  Stream.ExitBlock();
3550 }
3551 
3552 /// Write the module path strings, currently only used when generating
3553 /// a combined index file.
3554 void IndexBitcodeWriter::writeModStrings() {
3556 
3557  // TODO: See which abbrev sizes we actually need to emit
3558 
3559  // 8-bit fixed-width MST_ENTRY strings.
3560  auto Abbv = std::make_shared<BitCodeAbbrev>();
3562  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3564  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
3565  unsigned Abbrev8Bit = Stream.EmitAbbrev(std::move(Abbv));
3566 
3567  // 7-bit fixed width MST_ENTRY strings.
3568  Abbv = std::make_shared<BitCodeAbbrev>();
3570  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3572  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
3573  unsigned Abbrev7Bit = Stream.EmitAbbrev(std::move(Abbv));
3574 
3575  // 6-bit char6 MST_ENTRY strings.
3576  Abbv = std::make_shared<BitCodeAbbrev>();
3578  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3581  unsigned Abbrev6Bit = Stream.EmitAbbrev(std::move(Abbv));
3582 
3583  // Module Hash, 160 bits SHA1. Optionally, emitted after each MST_CODE_ENTRY.
3584  Abbv = std::make_shared<BitCodeAbbrev>();
3586  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3587  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3588  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3589  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3590  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3591  unsigned AbbrevHash = Stream.EmitAbbrev(std::move(Abbv));
3592 
3594  forEachModule(
3595  [&](const StringMapEntry<std::pair<uint64_t, ModuleHash>> &MPSE) {
3596  StringRef Key = MPSE.getKey();
3597  const auto &Value = MPSE.getValue();
3599  unsigned AbbrevToUse = Abbrev8Bit;
3600  if (Bits == SE_Char6)
3601  AbbrevToUse = Abbrev6Bit;
3602  else if (Bits == SE_Fixed7)
3603  AbbrevToUse = Abbrev7Bit;
3604 
3605  Vals.push_back(Value.first);
3606  Vals.append(Key.begin(), Key.end());
3607 
3608  // Emit the finished record.
3609  Stream.EmitRecord(bitc::MST_CODE_ENTRY, Vals, AbbrevToUse);
3610 
3611  // Emit an optional hash for the module now
3612  const auto &Hash = Value.second;
3613  if (llvm::any_of(Hash, [](uint32_t H) { return H; })) {
3614  Vals.assign(Hash.begin(), Hash.end());
3615  // Emit the hash record.
3616  Stream.EmitRecord(bitc::MST_CODE_HASH, Vals, AbbrevHash);
3617  }
3618 
3619  Vals.clear();
3620  });
3621  Stream.ExitBlock();
3622 }
3623 
3624 /// Write the function type metadata related records that need to appear before
3625 /// a function summary entry (whether per-module or combined).
3626 template <typename Fn>
3629  Fn GetValueID) {
3630  if (!FS->type_tests().empty())
3631  Stream.EmitRecord(bitc::FS_TYPE_TESTS, FS->type_tests());
3632 
3634 
3635  auto WriteVFuncIdVec = [&](uint64_t Ty,
3637  if (VFs.empty())
3638  return;
3639  Record.clear();
3640  for (auto &VF : VFs) {
3641  Record.push_back(VF.GUID);
3642  Record.push_back(VF.Offset);
3643  }
3644  Stream.EmitRecord(Ty, Record);
3645  };
3646 
3647  WriteVFuncIdVec(bitc::FS_TYPE_TEST_ASSUME_VCALLS,
3648  FS->type_test_assume_vcalls());
3649  WriteVFuncIdVec(bitc::FS_TYPE_CHECKED_LOAD_VCALLS,
3650  FS->type_checked_load_vcalls());
3651 
3652  auto WriteConstVCallVec = [&](uint64_t Ty,
3654  for (auto &VC : VCs) {
3655  Record.clear();
3656  Record.push_back(VC.VFunc.GUID);
3657  Record.push_back(VC.VFunc.Offset);
3658  llvm::append_range(Record, VC.Args);
3659  Stream.EmitRecord(Ty, Record);
3660  }
3661  };
3662 
3663  WriteConstVCallVec(bitc::FS_TYPE_TEST_ASSUME_CONST_VCALL,
3664  FS->type_test_assume_const_vcalls());
3665  WriteConstVCallVec(bitc::FS_TYPE_CHECKED_LOAD_CONST_VCALL,
3666  FS->type_checked_load_const_vcalls());
3667 
3668  auto WriteRange = [&](ConstantRange Range) {
3669  Range = Range.sextOrTrunc(FunctionSummary::ParamAccess::RangeWidth);
3670  assert(Range.getLower().getNumWords() == 1);
3671  assert(Range.getUpper().getNumWords() == 1);
3672  emitSignedInt64(Record, *Range.getLower().getRawData());
3673  emitSignedInt64(Record, *Range.getUpper().getRawData());
3674  };
3675 
3676  if (!FS->paramAccesses().empty()) {
3677  Record.clear();
3678  for (auto &Arg : FS->paramAccesses()) {
3679  size_t UndoSize = Record.size();
3680  Record.push_back(Arg.ParamNo);
3681  WriteRange(Arg.Use);
3682  Record.push_back(Arg.Calls.size());
3683  for (auto &Call : Arg.Calls) {
3684  Record.push_back(Call.ParamNo);
3685  Optional<unsigned> ValueID = GetValueID(Call.Callee);
3686  if (!ValueID) {
3687  // If ValueID is unknown we can't drop just this call, we must drop
3688  // entire parameter.
3689  Record.resize(UndoSize);
3690  break;
3691  }
3692  Record.push_back(*ValueID);
3693  WriteRange(Call.Offsets);
3694  }
3695  }
3696  if (!Record.empty())
3698  }
3699 }
3700 
3701 /// Collect type IDs from type tests used by function.
3702 static void
3704  std::set<GlobalValue::GUID> &ReferencedTypeIds) {
3705  if (!FS->type_tests().empty())
3706  for (auto &TT : FS->type_tests())
3707  ReferencedTypeIds.insert(TT);
3708 
3709  auto GetReferencedTypesFromVFuncIdVec =
3711  for (auto &VF : VFs)
3712  ReferencedTypeIds.insert(VF.GUID);
3713  };
3714 
3715  GetReferencedTypesFromVFuncIdVec(FS->type_test_assume_vcalls());
3716  GetReferencedTypesFromVFuncIdVec(FS->type_checked_load_vcalls());
3717 
3718  auto GetReferencedTypesFromConstVCallVec =
3720  for (auto &VC : VCs)
3721  ReferencedTypeIds.insert(VC.VFunc.GUID);
3722  };
3723 
3724  GetReferencedTypesFromConstVCallVec(FS->type_test_assume_const_vcalls());
3725  GetReferencedTypesFromConstVCallVec(FS->type_checked_load_const_vcalls());
3726 }
3727 
3729  SmallVector<uint64_t, 64> &NameVals, const std::vector<uint64_t> &args,
3730  const WholeProgramDevirtResolution::ByArg &ByArg) {
3731  NameVals.push_back(args.size());
3732  llvm::append_range(NameVals, args);
3733 
3734  NameVals.push_back(ByArg.TheKind);
3735  NameVals.push_back(ByArg.Info);
3736  NameVals.push_back(ByArg.Byte);
3737  NameVals.push_back(ByArg.Bit);
3738 }
3739 
3741  SmallVector<uint64_t, 64> &NameVals, StringTableBuilder &StrtabBuilder,
3742  uint64_t Id, const WholeProgramDevirtResolution &Wpd) {
3743  NameVals.push_back(Id);
3744 
3745  NameVals.push_back(Wpd.TheKind);
3746  NameVals.push_back(StrtabBuilder.add(Wpd.SingleImplName));
3747  NameVals.push_back(Wpd.SingleImplName.size());
3748 
3749  NameVals.push_back(Wpd.ResByArg.size());
3750  for (auto &A : Wpd.ResByArg)
3751  writeWholeProgramDevirtResolutionByArg(NameVals, A.first, A.second);
3752 }
3753 
3755  StringTableBuilder &StrtabBuilder,
3756  const std::string &Id,
3757  const TypeIdSummary &Summary) {
3758  NameVals.push_back(StrtabBuilder.add(Id));
3759  NameVals.push_back(Id.size());
3760 
3761  NameVals.push_back(Summary.TTRes.TheKind);
3762  NameVals.push_back(Summary.TTRes.SizeM1BitWidth);
3763  NameVals.push_back(Summary.TTRes.AlignLog2);
3764  NameVals.push_back(Summary.TTRes.SizeM1);
3765  NameVals.push_back(Summary.TTRes.BitMask);
3766  NameVals.push_back(Summary.TTRes.InlineBits);
3767 
3768  for (auto &W : Summary.WPDRes)
3769  writeWholeProgramDevirtResolution(NameVals, StrtabBuilder, W.first,
3770  W.second);
3771 }
3772 
3774  SmallVector<uint64_t, 64> &NameVals, StringTableBuilder &StrtabBuilder,
3775  const std::string &Id, const TypeIdCompatibleVtableInfo &Summary,
3776  ValueEnumerator &VE) {
3777  NameVals.push_back(StrtabBuilder.add(Id));
3778  NameVals.push_back(Id.size());
3779 
3780  for (auto &P : Summary) {
3781  NameVals.push_back(P.AddressPointOffset);
3782  NameVals.push_back(VE.getValueID(P.VTableVI.getValue()));
3783  }
3784 }
3785 
3786 // Helper to emit a single function summary record.
3787 void ModuleBitcodeWriterBase::writePerModuleFunctionSummaryRecord(
3788  SmallVector<uint64_t, 64> &NameVals, GlobalValueSummary *Summary,
3789  unsigned ValueID, unsigned FSCallsAbbrev, unsigned FSCallsProfileAbbrev,
3790  const Function &F) {
3791  NameVals.push_back(ValueID);
3792 
3793  FunctionSummary *FS = cast<FunctionSummary>(Summary);
3794 
3796  Stream, FS, [&](const ValueInfo &VI) -> Optional<unsigned> {
3797  return {VE.getValueID(VI.getValue())};
3798  });
3799 
3800  auto SpecialRefCnts = FS->specialRefCounts();
3801  NameVals.push_back(getEncodedGVSummaryFlags(FS->flags()));
3802  NameVals.push_back(FS->instCount());
3803  NameVals.push_back(getEncodedFFlags(FS->fflags()));
3804  NameVals.push_back(FS->refs().size());
3805  NameVals.push_back(SpecialRefCnts.first); // rorefcnt
3806  NameVals.push_back(SpecialRefCnts.second); // worefcnt
3807 
3808  for (auto &RI : FS->refs())
3809  NameVals.push_back(VE.getValueID(RI.getValue()));
3810 
3811  bool HasProfileData =
3812  F.hasProfileData() || ForceSummaryEdgesCold != FunctionSummary::FSHT_None;
3813  for (auto &ECI : FS->calls()) {
3814  NameVals.push_back(getValueId(ECI.first));
3815  if (HasProfileData)
3816  NameVals.push_back(static_cast<uint8_t>(ECI.second.Hotness));
3817  else if (WriteRelBFToSummary)
3818  NameVals.push_back(ECI.second.RelBlockFreq);
3819  }
3820 
3821  unsigned FSAbbrev = (HasProfileData ? FSCallsProfileAbbrev : FSCallsAbbrev);
3822  unsigned Code =
3823  (HasProfileData ? bitc::FS_PERMODULE_PROFILE
3825  : bitc::FS_PERMODULE));
3826 
3827  // Emit the finished record.
3828  Stream.EmitRecord(Code, NameVals, FSAbbrev);
3829  NameVals.clear();
3830 }
3831 
3832 // Collect the global value references in the given variable's initializer,
3833 // and emit them in a summary record.
3834 void ModuleBitcodeWriterBase::writeModuleLevelReferences(
3835  const GlobalVariable &V, SmallVector<uint64_t, 64> &NameVals,
3836  unsigned FSModRefsAbbrev, unsigned FSModVTableRefsAbbrev) {
3837  auto VI = Index->getValueInfo(V.getGUID());
3838  if (!VI || VI.getSummaryList().empty()) {
3839  // Only declarations should not have a summary (a declaration might however
3840  // have a summary if the def was in module level asm).
3841  assert(V.isDeclaration());
3842  return;
3843  }
3844  auto *Summary = VI.getSummaryList()[0].get();
3845  NameVals.push_back(VE.getValueID(&V));
3846  GlobalVarSummary *VS = cast<GlobalVarSummary>(Summary);
3847  NameVals.push_back(getEncodedGVSummaryFlags(VS->flags()));
3848  NameVals.push_back(getEncodedGVarFlags(VS->varflags()));
3849 
3850  auto VTableFuncs = VS->vTableFuncs();
3851  if (!VTableFuncs.empty())
3852  NameVals.push_back(VS->refs().size());
3853 
3854  unsigned SizeBeforeRefs = NameVals.size();
3855  for (auto &RI : VS->refs())
3856  NameVals.push_back(VE.getValueID(RI.getValue()));
3857  // Sort the refs for determinism output, the vector returned by FS->refs() has
3858  // been initialized from a DenseSet.
3859  llvm::sort(drop_begin(NameVals, SizeBeforeRefs));
3860 
3861  if (VTableFuncs.empty())
3863  FSModRefsAbbrev);
3864  else {
3865  // VTableFuncs pairs should already be sorted by offset.
3866  for (auto &P : VTableFuncs) {
3867  NameVals.push_back(VE.getValueID(P.FuncVI.getValue()));
3868  NameVals.push_back(P.VTableOffset);
3869  }
3870 
3872  FSModVTableRefsAbbrev);
3873  }
3874  NameVals.clear();
3875 }
3876 
3877 /// Emit the per-module summary section alongside the rest of
3878 /// the module's bitcode.
3879 void ModuleBitcodeWriterBase::writePerModuleGlobalValueSummary() {
3880  // By default we compile with ThinLTO if the module has a summary, but the
3881  // client can request full LTO with a module flag.
3882  bool IsThinLTO = true;
3883  if (auto *MD =
3884  mdconst::extract_or_null<ConstantInt>(M.getModuleFlag("ThinLTO")))
3885  IsThinLTO = MD->getZExtValue();
3888  4);
3889 
3890  Stream.EmitRecord(
3893 
3894  // Write the index flags.
3895  uint64_t Flags = 0;
3896  // Bits 1-3 are set only in the combined index, skip them.
3897  if (Index->enableSplitLTOUnit())
3898  Flags |= 0x8;
3900 
3901  if (Index->begin() == Index->end()) {
3902  Stream.ExitBlock();
3903  return;
3904  }
3905 
3906  for (const auto &GVI : valueIds()) {
3908  ArrayRef<uint64_t>{GVI.second, GVI.first});
3909  }
3910 
3911  // Abbrev for FS_PERMODULE_PROFILE.
3912  auto Abbv = std::make_shared<BitCodeAbbrev>();
3914  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
3915  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
3916  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
3917  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // fflags
3918  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
3919  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // rorefcnt
3920  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // worefcnt
3921  // numrefs x valueid, n x (valueid, hotness)
3923  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3924  unsigned FSCallsProfileAbbrev = Stream.EmitAbbrev(std::move(Abbv));
3925 
3926  // Abbrev for FS_PERMODULE or FS_PERMODULE_RELBF.
3927  Abbv = std::make_shared<BitCodeAbbrev>();
3928  if (WriteRelBFToSummary)
3930  else
3931  Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE));
3932  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
3933  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
3934  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
3935  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // fflags
3936  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
3937  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // rorefcnt
3938  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // worefcnt
3939  // numrefs x valueid, n x (valueid [, rel_block_freq])
3941  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3942  unsigned FSCallsAbbrev = Stream.EmitAbbrev(std::move(Abbv));
3943 
3944  // Abbrev for FS_PERMODULE_GLOBALVAR_INIT_REFS.
3945  Abbv = std::make_shared<BitCodeAbbrev>();
3947  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
3948  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
3949  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); // valueids
3950  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3951  unsigned FSModRefsAbbrev = Stream.EmitAbbrev(std::move(Abbv));
3952 
3953  // Abbrev for FS_PERMODULE_VTABLE_GLOBALVAR_INIT_REFS.
3954  Abbv = std::make_shared<BitCodeAbbrev>();
3956  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
3957  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
3958  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
3959  // numrefs x valueid, n x (valueid , offset)
3961  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3962  unsigned FSModVTableRefsAbbrev = Stream.EmitAbbrev(std::move(Abbv));
3963 
3964  // Abbrev for FS_ALIAS.
3965  Abbv = std::make_shared<BitCodeAbbrev>();
3966  Abbv->Add(BitCodeAbbrevOp(bitc::FS_ALIAS));
3967  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
3968  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
3969  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
3970  unsigned FSAliasAbbrev = Stream.EmitAbbrev(std::move(Abbv));
3971 
3972  // Abbrev for FS_TYPE_ID_METADATA
3973  Abbv = std::make_shared<BitCodeAbbrev>();
3975  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // typeid strtab index
3976  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // typeid length
3977  // n x (valueid , offset)
3979  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
3980  unsigned TypeIdCompatibleVtableAbbrev = Stream.EmitAbbrev(std::move(Abbv));
3981 
3982  SmallVector<uint64_t, 64> NameVals;
3983  // Iterate over the list of functions instead of the Index to
3984  // ensure the ordering is stable.
3985  for (const Function &F : M) {
3986  // Summary emission does not support anonymous functions, they have to
3987  // renamed using the anonymous function renaming pass.
3988  if (!F.hasName())
3989  report_fatal_error("Unexpected anonymous function when writing summary");
3990 
3991  ValueInfo VI = Index->getValueInfo(F.getGUID());
3992  if (!VI || VI.getSummaryList().empty()) {
3993  // Only declarations should not have a summary (a declaration might
3994  // however have a summary if the def was in module level asm).
3995  assert(F.isDeclaration());
3996  continue;
3997  }
3998  auto *Summary = VI.getSummaryList()[0].get();
3999  writePerModuleFunctionSummaryRecord(NameVals, Summary, VE.getValueID(&F),
4000  FSCallsAbbrev, FSCallsProfileAbbrev, F);
4001  }
4002 
4003  // Capture references from GlobalVariable initializers, which are outside
4004  // of a function scope.
4005  for (const GlobalVariable &G : M.globals())
4006  writeModuleLevelReferences(G, NameVals, FSModRefsAbbrev,
4007  FSModVTableRefsAbbrev);
4008 
4009  for (const GlobalAlias &A : M.aliases()) {
4010  auto *Aliasee = A.getBaseObject();
4011  if (!Aliasee->hasName())
4012  // Nameless function don't have an entry in the summary, skip it.
4013  continue;
4014  auto AliasId = VE.getValueID(&A);
4015  auto AliaseeId = VE.getValueID(Aliasee);
4016  NameVals.push_back(AliasId);
4017  auto *Summary = Index->getGlobalValueSummary(A);
4018  AliasSummary *AS = cast<AliasSummary>(Summary);
4019  NameVals.push_back(getEncodedGVSummaryFlags(AS->flags()));
4020  NameVals.push_back(AliaseeId);
4021  Stream.EmitRecord(bitc::FS_ALIAS, NameVals, FSAliasAbbrev);
4022  NameVals.clear();
4023  }
4024 
4025  for (auto &S : Index->typeIdCompatibleVtableMap()) {
4026  writeTypeIdCompatibleVtableSummaryRecord(NameVals, StrtabBuilder, S.first,
4027  S.second, VE);
4028  Stream.EmitRecord(bitc::FS_TYPE_ID_METADATA, NameVals,
4029  TypeIdCompatibleVtableAbbrev);
4030  NameVals.clear();
4031  }
4032 
4034  ArrayRef<uint64_t>{Index->getBlockCount()});
4035 
4036  Stream.ExitBlock();
4037 }
4038 
4039 /// Emit the combined summary section into the combined index file.
4040 void IndexBitcodeWriter::writeCombinedGlobalValueSummary() {
4042  Stream.EmitRecord(
4045 
4046  // Write the index flags.
4047  Stream.EmitRecord(bitc::FS_FLAGS, ArrayRef<uint64_t>{Index.getFlags()});
4048 
4049  for (const auto &GVI : valueIds()) {
4051  ArrayRef<uint64_t>{GVI.second, GVI.first});
4052  }
4053 
4054  // Abbrev for FS_COMBINED.
4055  auto Abbv = std::make_shared<BitCodeAbbrev>();
4056  Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED));
4057  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
4058  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
4059  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
4060  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
4061  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // fflags
4062  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // entrycount
4063  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
4064  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // rorefcnt
4065  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // worefcnt
4066  // numrefs x valueid, n x (valueid)
4068  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
4069  unsigned FSCallsAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4070 
4071  // Abbrev for FS_COMBINED_PROFILE.
4072  Abbv = std::make_shared<BitCodeAbbrev>();
4074  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
4075  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
4076  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
4077  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
4078  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // fflags
4079  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // entrycount
4080  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
4081  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // rorefcnt
4082  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // worefcnt
4083  // numrefs x valueid, n x (valueid, hotness)
4085  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
4086  unsigned FSCallsProfileAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4087 
4088  // Abbrev for FS_COMBINED_GLOBALVAR_INIT_REFS.
4089  Abbv = std::make_shared<BitCodeAbbrev>();
4091  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
4092  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
4093  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
4094  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); // valueids
4095  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
4096  unsigned FSModRefsAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4097 
4098  // Abbrev for FS_COMBINED_ALIAS.
4099  Abbv = std::make_shared<BitCodeAbbrev>();
4101  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
4102  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
4103  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
4104  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
4105  unsigned FSAliasAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4106 
4107  // The aliases are emitted as a post-pass, and will point to the value
4108  // id of the aliasee. Save them in a vector for post-processing.
4110 
4111  // Save the value id for each summary for alias emission.
4113 
4114  SmallVector<uint64_t, 64> NameVals;
4115 
4116  // Set that will be populated during call to writeFunctionTypeMetadataRecords
4117  // with the type ids referenced by this index file.
4118  std::set<GlobalValue::GUID> ReferencedTypeIds;
4119 
4120  // For local linkage, we also emit the original name separately
4121  // immediately after the record.
4122  auto MaybeEmitOriginalName = [&](GlobalValueSummary &S) {
4123  if (!GlobalValue::isLocalLinkage(S.linkage()))
4124  return;
4125  NameVals.push_back(S.getOriginalName());
4126  Stream.EmitRecord(bitc::FS_COMBINED_ORIGINAL_NAME, NameVals);
4127  NameVals.clear();
4128  };
4129 
4130  std::set<GlobalValue::GUID> DefOrUseGUIDs;
4131  forEachSummary([&](GVInfo I, bool IsAliasee) {
4132  GlobalValueSummary *S = I.second;
4133  assert(S);
4134  DefOrUseGUIDs.insert(I.first);
4135  for (const ValueInfo &VI : S->refs())
4136  DefOrUseGUIDs.insert(VI.getGUID());
4137 
4138  auto ValueId = getValueId(I.first);
4139  assert(ValueId);
4140  SummaryToValueIdMap[S] = *ValueId;
4141 
4142  // If this is invoked for an aliasee, we want to record the above
4143  // mapping, but then not emit a summary entry (if the aliasee is
4144  // to be imported, we will invoke this separately with IsAliasee=false).
4145  if (IsAliasee)
4146  return;
4147 
4148  if (auto *AS = dyn_cast<AliasSummary>(S)) {
4149  // Will process aliases as a post-pass because the reader wants all
4150  // global to be loaded first.
4151  Aliases.push_back(AS);
4152  return;
4153  }
4154 
4155  if (auto *VS = dyn_cast<GlobalVarSummary>(S)) {
4156  NameVals.push_back(*ValueId);
4157  NameVals.push_back(Index.getModuleId(VS->modulePath()));
4158  NameVals.push_back(getEncodedGVSummaryFlags(VS->flags()));
4159  NameVals.push_back(getEncodedGVarFlags(VS->varflags()));
4160  for (auto &RI : VS->refs()) {
4161  auto RefValueId = getValueId(RI.getGUID());
4162  if (!RefValueId)
4163  continue;
4164  NameVals.push_back(*RefValueId);
4165  }
4166 
4167  // Emit the finished record.
4169  FSModRefsAbbrev);
4170  NameVals.clear();
4171  MaybeEmitOriginalName(*S);
4172  return;
4173  }
4174 
4175  auto GetValueId = [&](const ValueInfo &VI) -> Optional<unsigned> {
4176  GlobalValue::GUID GUID = VI.getGUID();
4177  Optional<unsigned> CallValueId = getValueId(GUID);
4178  if (CallValueId)
4179  return CallValueId;
4180  // For SamplePGO, the indirect call targets for local functions will
4181  // have its original name annotated in profile. We try to find the
4182  // corresponding PGOFuncName as the GUID.
4183  GUID = Index.getGUIDFromOriginalID(GUID);
4184  if (!GUID)
4185  return None;
4186  CallValueId = getValueId(GUID);
4187  if (!CallValueId)
4188  return None;
4189  // The mapping from OriginalId to GUID may return a GUID
4190  // that corresponds to a static variable. Filter it out here.
4191  // This can happen when
4192  // 1) There is a call to a library function which does not have
4193  // a CallValidId;
4194  // 2) There is a static variable with the OriginalGUID identical
4195  // to the GUID of the library function in 1);
4196  // When this happens, the logic for SamplePGO kicks in and
4197  // the static variable in 2) will be found, which needs to be
4198  // filtered out.
4199  auto *GVSum = Index.getGlobalValueSummary(GUID, false);
4200  if (GVSum && GVSum->getSummaryKind() == GlobalValueSummary::GlobalVarKind)
4201  return None;
4202  return CallValueId;
4203  };
4204 
4205  auto *FS = cast<FunctionSummary>(S);
4206  writeFunctionTypeMetadataRecords(Stream, FS, GetValueId);
4207  getReferencedTypeIds(FS, ReferencedTypeIds);
4208 
4209  NameVals.push_back(*ValueId);
4210  NameVals.push_back(Index.getModuleId(FS->modulePath()));
4211  NameVals.push_back(getEncodedGVSummaryFlags(FS->flags()));
4212  NameVals.push_back(FS->instCount());
4213  NameVals.push_back(getEncodedFFlags(FS->fflags()));
4214  NameVals.push_back(FS->entryCount());
4215 
4216  // Fill in below
4217  NameVals.push_back(0); // numrefs
4218  NameVals.push_back(0); // rorefcnt
4219  NameVals.push_back(0); // worefcnt
4220 
4221  unsigned Count = 0, RORefCnt = 0, WORefCnt = 0;
4222  for (auto &RI : FS->refs()) {
4223  auto RefValueId = getValueId(RI.getGUID());
4224  if (!RefValueId)
4225  continue;
4226  NameVals.push_back(*RefValueId);
4227  if (RI.isReadOnly())
4228  RORefCnt++;
4229  else if (RI.isWriteOnly())
4230  WORefCnt++;
4231  Count++;
4232  }
4233  NameVals[6] = Count;
4234  NameVals[7] = RORefCnt;
4235  NameVals[8] = WORefCnt;
4236 
4237  bool HasProfileData = false;
4238  for (auto &EI : FS->calls()) {
4239  HasProfileData |=
4240  EI.second.getHotness() != CalleeInfo::HotnessType::Unknown;
4241  if (HasProfileData)
4242  break;
4243  }
4244 
4245  for (auto &EI : FS->calls()) {
4246  // If this GUID doesn't have a value id, it doesn't have a function
4247  // summary and we don't need to record any calls to it.
4248  Optional<unsigned> CallValueId = GetValueId(EI.first);
4249  if (!CallValueId)
4250  continue;
4251  NameVals.push_back(*CallValueId);
4252  if (HasProfileData)
4253  NameVals.push_back(static_cast<uint8_t>(EI.second.Hotness));
4254  }
4255 
4256  unsigned FSAbbrev = (HasProfileData ? FSCallsProfileAbbrev : FSCallsAbbrev);
4257  unsigned Code =
4258  (HasProfileData ? bitc::FS_COMBINED_PROFILE : bitc::FS_COMBINED);
4259 
4260  // Emit the finished record.
4261  Stream.EmitRecord(Code, NameVals, FSAbbrev);
4262  NameVals.clear();
4263  MaybeEmitOriginalName(*S);
4264  });
4265 
4266  for (auto *AS : Aliases) {
4267  auto AliasValueId = SummaryToValueIdMap[AS];
4268  assert(AliasValueId);
4269  NameVals.push_back(AliasValueId);
4270  NameVals.push_back(Index.getModuleId(AS->modulePath()));
4271  NameVals.push_back(getEncodedGVSummaryFlags(AS->flags()));
4272  auto AliaseeValueId = SummaryToValueIdMap[&AS->getAliasee()];
4273  assert(AliaseeValueId);
4274  NameVals.push_back(AliaseeValueId);
4275 
4276  // Emit the finished record.
4277  Stream.EmitRecord(bitc::FS_COMBINED_ALIAS, NameVals, FSAliasAbbrev);
4278  NameVals.clear();
4279  MaybeEmitOriginalName(*AS);
4280 
4281  if (auto *FS = dyn_cast<FunctionSummary>(&AS->getAliasee()))
4282  getReferencedTypeIds(FS, ReferencedTypeIds);
4283  }
4284 
4285  if (!Index.cfiFunctionDefs().empty()) {
4286  for (auto &S : Index.cfiFunctionDefs()) {
4287  if (DefOrUseGUIDs.count(
4289  NameVals.push_back(StrtabBuilder.add(S));
4290  NameVals.push_back(S.size());
4291  }
4292  }
4293  if (!NameVals.empty()) {
4294  Stream.EmitRecord(bitc::FS_CFI_FUNCTION_DEFS, NameVals);
4295  NameVals.clear();
4296  }
4297  }
4298 
4299  if (!Index.cfiFunctionDecls().empty()) {
4300  for (auto &S : Index.cfiFunctionDecls()) {
4301  if (DefOrUseGUIDs.count(
4303  NameVals.push_back(StrtabBuilder.add(S));
4304  NameVals.push_back(S.size());
4305  }
4306  }
4307  if (!NameVals.empty()) {
4308  Stream.EmitRecord(bitc::FS_CFI_FUNCTION_DECLS, NameVals);
4309  NameVals.clear();
4310  }
4311  }
4312 
4313  // Walk the GUIDs that were referenced, and write the
4314  // corresponding type id records.
4315  for (auto &T : ReferencedTypeIds) {
4316  auto TidIter = Index.typeIds().equal_range(T);
4317  for (auto It = TidIter.first; It != TidIter.second; ++It) {
4318  writeTypeIdSummaryRecord(NameVals, StrtabBuilder, It->second.first,
4319  It->second.second);
4320  Stream.EmitRecord(bitc::FS_TYPE_ID, NameVals);
4321  NameVals.clear();
4322  }
4323  }
4324 
4326  ArrayRef<uint64_t>{Index.getBlockCount()});
4327 
4328  Stream.ExitBlock();
4329 }
4330 
4331 /// Create the "IDENTIFICATION_BLOCK_ID" containing a single string with the
4332 /// current llvm version, and a record for the epoch number.
4335 
4336  // Write the "user readable" string identifying the bitcode producer
4337  auto Abbv = std::make_shared<BitCodeAbbrev>();
4341  auto StringAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4343  "LLVM" LLVM_VERSION_STRING, StringAbbrev);
4344 
4345  // Write the epoch version
4346  Abbv = std::make_shared<BitCodeAbbrev>();
4348  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
4349  auto EpochAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4350  constexpr std::array<unsigned, 1> Vals = {{bitc::BITCODE_CURRENT_EPOCH}};
4351  Stream.EmitRecord(bitc::IDENTIFICATION_CODE_EPOCH, Vals, EpochAbbrev);
4352  Stream.ExitBlock();
4353 }
4354 
4355 void ModuleBitcodeWriter::writeModuleHash(size_t BlockStartPos) {
4356  // Emit the module's hash.
4357  // MODULE_CODE_HASH: [5*i32]
4358  if (GenerateHash) {
4359  uint32_t Vals[5];
4360  Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&(Buffer)[BlockStartPos],
4361  Buffer.size() - BlockStartPos));
4362  StringRef Hash = Hasher.result();
4363  for (int Pos = 0; Pos < 20; Pos += 4) {
4364  Vals[Pos / 4] = support::endian::read32be(Hash.data() + Pos);
4365  }
4366 
4367  // Emit the finished record.
4368  Stream.EmitRecord(bitc::MODULE_CODE_HASH, Vals);
4369 
4370  if (ModHash)
4371  // Save the written hash value.
4372  llvm::copy(Vals, std::begin(*ModHash));
4373  }
4374 }
4375 
4377  writeIdentificationBlock(Stream);
4378 
4380  size_t BlockStartPos = Buffer.size();
4381 
4382  writeModuleVersion();
4383 
4384  // Emit blockinfo, which defines the standard abbreviations etc.
4385  writeBlockInfo();
4386 
4387  // Emit information describing all of the types in the module.
4388  writeTypeTable();
4389 
4390  // Emit information about attribute groups.
4391  writeAttributeGroupTable();
4392 
4393  // Emit information about parameter attributes.
4394  writeAttributeTable();
4395 
4396  writeComdats();
4397 
4398  // Emit top-level description of module, including target triple, inline asm,
4399  // descriptors for global variables, and function prototype info.
4400  writeModuleInfo();
4401 
4402  // Emit constants.
4403  writeModuleConstants();
4404 
4405  // Emit metadata kind names.
4406  writeModuleMetadataKinds();
4407 
4408  // Emit metadata.
4409  writeModuleMetadata();
4410 
4411  // Emit module-level use-lists.
4412  if (VE.shouldPreserveUseListOrder())
4413  writeUseListBlock(nullptr);
4414 
4415  writeOperandBundleTags();
4416  writeSyncScopeNames();
4417 
4418  // Emit function bodies.
4419  DenseMap<const Function *, uint64_t> FunctionToBitcodeIndex;
4420  for (Module::const_iterator F = M.begin(), E = M.end(); F != E; ++F)
4421  if (!F->isDeclaration())
4422  writeFunction(*F, FunctionToBitcodeIndex);
4423 
4424  // Need to write after the above call to WriteFunction which populates
4425  // the summary information in the index.
4426  if (Index)
4427  writePerModuleGlobalValueSummary();
4428 
4429  writeGlobalValueSymbolTable(FunctionToBitcodeIndex);
4430 
4431  writeModuleHash(BlockStartPos);
4432 
4433  Stream.ExitBlock();
4434 }
4435 
4437  uint32_t &Position) {
4438  support::endian::write32le(&Buffer[Position], Value);
4439  Position += 4;
4440 }
4441 
4442 /// If generating a bc file on darwin, we have to emit a
4443 /// header and trailer to make it compatible with the system archiver. To do
4444 /// this we emit the following header, and then emit a trailer that pads the
4445 /// file out to be a multiple of 16 bytes.
4446 ///
4447 /// struct bc_header {
4448 /// uint32_t Magic; // 0x0B17C0DE
4449 /// uint32_t Version; // Version, currently always 0.
4450 /// uint32_t BitcodeOffset; // Offset to traditional bitcode file.
4451 /// uint32_t BitcodeSize; // Size of traditional bitcode file.
4452 /// uint32_t CPUType; // CPU specifier.
4453 /// ... potentially more later ...
4454 /// };
4456  const Triple &TT) {
4457  unsigned CPUType = ~0U;
4458 
4459  // Match x86_64-*, i[3-9]86-*, powerpc-*, powerpc64-*, arm-*, thumb-*,
4460  // armv[0-9]-*, thumbv[0-9]-*, armv5te-*, or armv6t2-*. The CPUType is a magic
4461  // number from /usr/include/mach/machine.h. It is ok to reproduce the
4462  // specific constants here because they are implicitly part of the Darwin ABI.
4463  enum {
4464  DARWIN_CPU_ARCH_ABI64 = 0x01000000,
4465  DARWIN_CPU_TYPE_X86 = 7,
4466  DARWIN_CPU_TYPE_ARM = 12,
4467  DARWIN_CPU_TYPE_POWERPC = 18
4468  };
4469 
4470  Triple::ArchType Arch = TT.getArch();
4471  if (Arch == Triple::x86_64)
4472  CPUType = DARWIN_CPU_TYPE_X86 | DARWIN_CPU_ARCH_ABI64;
4473  else if (Arch == Triple::x86)
4474  CPUType = DARWIN_CPU_TYPE_X86;
4475  else if (Arch == Triple::ppc)
4476  CPUType = DARWIN_CPU_TYPE_POWERPC;
4477  else if (Arch == Triple::ppc64)
4478  CPUType = DARWIN_CPU_TYPE_POWERPC | DARWIN_CPU_ARCH_ABI64;
4479  else if (Arch == Triple::arm || Arch == Triple::thumb)
4480  CPUType = DARWIN_CPU_TYPE_ARM;
4481 
4482  // Traditional Bitcode starts after header.
4483  assert(Buffer.size() >= BWH_HeaderSize &&
4484  "Expected header size to be reserved");
4485  unsigned BCOffset = BWH_HeaderSize;
4486  unsigned BCSize = Buffer.size() - BWH_HeaderSize;
4487 
4488  // Write the magic and version.
4489  unsigned Position = 0;
4490  writeInt32ToBuffer(0x0B17C0DE, Buffer, Position);
4491  writeInt32ToBuffer(0, Buffer, Position); // Version.
4492  writeInt32ToBuffer(BCOffset, Buffer, Position);
4493  writeInt32ToBuffer(BCSize, Buffer, Position);
4494  writeInt32ToBuffer(CPUType, Buffer, Position);
4495 
4496  // If the file is not a multiple of 16 bytes, insert dummy padding.
4497  while (Buffer.size() & 15)
4498  Buffer.push_back(0);
4499 }
4500 
4501 /// Helper to write the header common to all bitcode files.
4502 static void writeBitcodeHeader(BitstreamWriter &Stream) {
4503  // Emit the file header.
4504  Stream.Emit((unsigned)'B', 8);
4505  Stream.Emit((unsigned)'C', 8);
4506  Stream.Emit(0x0, 4);
4507  Stream.Emit(0xC, 4);
4508  Stream.Emit(0xE, 4);
4509  Stream.Emit(0xD, 4);
4510 }
4511 
4513  : Buffer(Buffer), Stream(new BitstreamWriter(Buffer, FS, FlushThreshold)) {
4514  writeBitcodeHeader(*Stream);
4515 }
4516 
4518 
4519 void BitcodeWriter::writeBlob(unsigned Block, unsigned Record, StringRef Blob) {
4520  Stream->EnterSubblock(Block, 3);
4521 
4522  auto Abbv = std::make_shared<BitCodeAbbrev>();
4523  Abbv->Add(BitCodeAbbrevOp(Record));
4525  auto AbbrevNo = Stream->EmitAbbrev(std::move(Abbv));
4526 
4527  Stream->EmitRecordWithBlob(AbbrevNo, ArrayRef<uint64_t>{Record}, Blob);
4528 
4529  Stream->ExitBlock();
4530 }
4531 
4533  assert(!WroteStrtab && !WroteSymtab);
4534 
4535  // If any module has module-level inline asm, we will require a registered asm
4536  // parser for the target so that we can create an accurate symbol table for
4537  // the module.
4538  for (Module *M : Mods) {
4539  if (M->getModuleInlineAsm().empty())
4540  continue;
4541 
4542  std::string Err;
4543  const Triple TT(M->getTargetTriple());
4544  const Target *T = TargetRegistry::lookupTarget(TT.str(), Err);
4545  if (!T || !T->hasMCAsmParser())
4546  return;
4547  }
4548 
4549  WroteSymtab = true;
4550  SmallVector<char, 0> Symtab;
4551  // The irsymtab::build function may be unable to create a symbol table if the
4552  // module is malformed (e.g. it contains an invalid alias). Writing a symbol
4553  // table is not required for correctness, but we still want to be able to
4554  // write malformed modules to bitcode files, so swallow the error.
4555  if (Error E = irsymtab::build(Mods, Symtab, StrtabBuilder, Alloc)) {
4557  return;
4558  }
4559 
4561  {Symtab.data(), Symtab.size()});
4562 }
4563 
4565  assert(!WroteStrtab);
4566 
4567  std::vector<char> Strtab;
4568  StrtabBuilder.finalizeInOrder();
4569  Strtab.resize(StrtabBuilder.getSize());
4570  StrtabBuilder.write((uint8_t *)Strtab.data());
4571 
4573  {Strtab.data(), Strtab.size()});
4574 
4575  WroteStrtab = true;
4576 }
4577 
4579  writeBlob(bitc::STRTAB_BLOCK_ID, bitc::STRTAB_BLOB, Strtab);
4580  WroteStrtab = true;
4581 }
4582 
4584  bool ShouldPreserveUseListOrder,
4585  const ModuleSummaryIndex *Index,
4586  bool GenerateHash, ModuleHash *ModHash) {
4587  assert(!WroteStrtab);
4588 
4589  // The Mods vector is used by irsymtab::build, which requires non-const
4590  // Modules in case it needs to materialize metadata. But the bitcode writer
4591  // requires that the module is materialized, so we can cast to non-const here,
4592  // after checking that it is in fact materialized.
4593  assert(M.isMaterialized());
4594  Mods.push_back(const_cast<Module *>(&M));
4595 
4596  ModuleBitcodeWriter ModuleWriter(M, Buffer, StrtabBuilder, *Stream,
4597  ShouldPreserveUseListOrder, Index,
4598  GenerateHash, ModHash);
4599  ModuleWriter.write();
4600 }
4601 
4603  const ModuleSummaryIndex *Index,
4604  const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex) {
4605  IndexBitcodeWriter IndexWriter(*Stream, StrtabBuilder, *Index,
4606  ModuleToSummariesForIndex);
4607  IndexWriter.write();
4608 }
4609 
4610 /// Write the specified module to the specified output stream.
4612  bool ShouldPreserveUseListOrder,
4613  const ModuleSummaryIndex *Index,
4614  bool GenerateHash, ModuleHash *ModHash) {
4615  SmallVector<char, 0> Buffer;
4616  Buffer.reserve(256*1024);
4617 
4618  // If this is darwin or another generic macho target, reserve space for the
4619  // header.
4620  Triple TT(M.getTargetTriple());
4621  if (TT.isOSDarwin() || TT.isOSBinFormatMachO())
4622  Buffer.insert(Buffer.begin(), BWH_HeaderSize, 0);
4623 
4624  BitcodeWriter Writer(Buffer, dyn_cast<raw_fd_stream>(&Out));
4625  Writer.writeModule(M, ShouldPreserveUseListOrder, Index, GenerateHash,
4626  ModHash);
4627  Writer.writeSymtab();
4628  Writer.writeStrtab();
4629 
4630  if (TT.isOSDarwin() || TT.isOSBinFormatMachO())
4631  emitDarwinBCHeaderAndTrailer(Buffer, TT);
4632 
4633  // Write the generated bitstream to "Out".
4634  if (!Buffer.empty())
4635  Out.write((char *)&Buffer.front(), Buffer.size());
4636 }
4637 
4640 
4641  writeModuleVersion();
4642 
4643  // Write the module paths in the combined index.
4644  writeModStrings();
4645 
4646  // Write the summary combined index records.
4647  writeCombinedGlobalValueSummary();
4648 
4649  Stream.ExitBlock();
4650 }
4651 
4652 // Write the specified module summary index to the given raw output stream,
4653 // where it will be written in a new bitcode block. This is used when
4654 // writing the combined index file for ThinLTO. When writing a subset of the
4655 // index for a distributed backend, provide a \p ModuleToSummariesForIndex map.
4657  const ModuleSummaryIndex &Index, raw_ostream &Out,
4658  const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex) {
4659  SmallVector<char, 0> Buffer;
4660  Buffer.reserve(256 * 1024);
4661 
4662  BitcodeWriter Writer(Buffer);
4663  Writer.writeIndex(&Index, ModuleToSummariesForIndex);
4664  Writer.writeStrtab();
4665 
4666  Out.write((char *)&Buffer.front(), Buffer.size());
4667 }
4668 
4669 namespace {
4670 
4671 /// Class to manage the bitcode writing for a thin link bitcode file.
4672 class ThinLinkBitcodeWriter : public ModuleBitcodeWriterBase {
4673  /// ModHash is for use in ThinLTO incremental build, generated while writing
4674  /// the module bitcode file.
4675  const ModuleHash *ModHash;
4676 
4677 public:
4678  ThinLinkBitcodeWriter(const Module &M, StringTableBuilder &StrtabBuilder,
4679  BitstreamWriter &Stream,
4680  const ModuleSummaryIndex &Index,
4681  const ModuleHash &ModHash)
4682  : ModuleBitcodeWriterBase(M, StrtabBuilder, Stream,
4683  /*ShouldPreserveUseListOrder=*/false, &Index),
4684  ModHash(&ModHash) {}
4685 
4686  void write();
4687 
4688 private:
4689  void writeSimplifiedModuleInfo();
4690 };
4691 
4692 } // end anonymous namespace
4693 
4694 // This function writes a simpilified module info for thin link bitcode file.
4695 // It only contains the source file name along with the name(the offset and
4696 // size in strtab) and linkage for global values. For the global value info
4697 // entry, in order to keep linkage at offset 5, there are three zeros used
4698 // as padding.
4699 void ThinLinkBitcodeWriter::writeSimplifiedModuleInfo() {
4701  // Emit the module's source file name.
4702  {
4703  StringEncoding Bits = getStringEncoding(M.getSourceFileName());
4705  if (Bits == SE_Char6)
4706  AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Char6);
4707  else if (Bits == SE_Fixed7)
4708  AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7);
4709 
4710  // MODULE_CODE_SOURCE_FILENAME: [namechar x N]
4711  auto Abbv = std::make_shared<BitCodeAbbrev>();
4712  Abbv->Add(