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