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