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