LLVM 19.0.0git
BitcodeWriter.cpp
Go to the documentation of this file.
1//===- Bitcode/Writer/BitcodeWriter.cpp - Bitcode Writer ------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Bitcode writer implementation.
10//
11//===----------------------------------------------------------------------===//
12
14#include "ValueEnumerator.h"
15#include "llvm/ADT/APFloat.h"
16#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/SetVector.h"
24#include "llvm/ADT/StringMap.h"
25#include "llvm/ADT/StringRef.h"
31#include "llvm/Config/llvm-config.h"
32#include "llvm/IR/Attributes.h"
33#include "llvm/IR/BasicBlock.h"
34#include "llvm/IR/Comdat.h"
35#include "llvm/IR/Constant.h"
36#include "llvm/IR/Constants.h"
38#include "llvm/IR/DebugLoc.h"
40#include "llvm/IR/Function.h"
41#include "llvm/IR/GlobalAlias.h"
42#include "llvm/IR/GlobalIFunc.h"
44#include "llvm/IR/GlobalValue.h"
46#include "llvm/IR/InlineAsm.h"
47#include "llvm/IR/InstrTypes.h"
48#include "llvm/IR/Instruction.h"
50#include "llvm/IR/LLVMContext.h"
51#include "llvm/IR/Metadata.h"
52#include "llvm/IR/Module.h"
54#include "llvm/IR/Operator.h"
55#include "llvm/IR/Type.h"
57#include "llvm/IR/Value.h"
65#include "llvm/Support/Endian.h"
66#include "llvm/Support/Error.h"
69#include "llvm/Support/SHA1.h"
72#include <algorithm>
73#include <cassert>
74#include <cstddef>
75#include <cstdint>
76#include <iterator>
77#include <map>
78#include <memory>
79#include <optional>
80#include <string>
81#include <utility>
82#include <vector>
83
84using namespace llvm;
85
87 IndexThreshold("bitcode-mdindex-threshold", cl::Hidden, cl::init(25),
88 cl::desc("Number of metadatas above which we emit an index "
89 "to enable lazy-loading"));
91 "bitcode-flush-threshold", cl::Hidden, cl::init(512),
92 cl::desc("The threshold (unit M) for flushing LLVM bitcode."));
93
95 "write-relbf-to-summary", cl::Hidden, cl::init(false),
96 cl::desc("Write relative block frequency to function summary "));
97
98namespace llvm {
100}
101
104
105namespace {
106
107/// These are manifest constants used by the bitcode writer. They do not need to
108/// be kept in sync with the reader, but need to be consistent within this file.
109enum {
110 // VALUE_SYMTAB_BLOCK abbrev id's.
111 VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
112 VST_ENTRY_7_ABBREV,
113 VST_ENTRY_6_ABBREV,
114 VST_BBENTRY_6_ABBREV,
115
116 // CONSTANTS_BLOCK abbrev id's.
117 CONSTANTS_SETTYPE_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
118 CONSTANTS_INTEGER_ABBREV,
119 CONSTANTS_CE_CAST_Abbrev,
120 CONSTANTS_NULL_Abbrev,
121
122 // FUNCTION_BLOCK abbrev id's.
123 FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
124 FUNCTION_INST_UNOP_ABBREV,
125 FUNCTION_INST_UNOP_FLAGS_ABBREV,
126 FUNCTION_INST_BINOP_ABBREV,
127 FUNCTION_INST_BINOP_FLAGS_ABBREV,
128 FUNCTION_INST_CAST_ABBREV,
129 FUNCTION_INST_CAST_FLAGS_ABBREV,
130 FUNCTION_INST_RET_VOID_ABBREV,
131 FUNCTION_INST_RET_VAL_ABBREV,
132 FUNCTION_INST_UNREACHABLE_ABBREV,
133 FUNCTION_INST_GEP_ABBREV,
134 FUNCTION_DEBUG_RECORD_VALUE_ABBREV,
135};
136
137/// Abstract class to manage the bitcode writing, subclassed for each bitcode
138/// file type.
139class BitcodeWriterBase {
140protected:
141 /// The stream created and owned by the client.
142 BitstreamWriter &Stream;
143
144 StringTableBuilder &StrtabBuilder;
145
146public:
147 /// Constructs a BitcodeWriterBase object that writes to the provided
148 /// \p Stream.
149 BitcodeWriterBase(BitstreamWriter &Stream, StringTableBuilder &StrtabBuilder)
150 : Stream(Stream), StrtabBuilder(StrtabBuilder) {}
151
152protected:
153 void writeModuleVersion();
154};
155
156void BitcodeWriterBase::writeModuleVersion() {
157 // VERSION: [version#]
158 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, ArrayRef<uint64_t>{2});
159}
160
161/// Base class to manage the module bitcode writing, currently subclassed for
162/// ModuleBitcodeWriter and ThinLinkBitcodeWriter.
163class ModuleBitcodeWriterBase : public BitcodeWriterBase {
164protected:
165 /// The Module to write to bitcode.
166 const Module &M;
167
168 /// Enumerates ids for all values in the module.
170
171 /// Optional per-module index to write for ThinLTO.
173
174 /// Map that holds the correspondence between GUIDs in the summary index,
175 /// that came from indirect call profiles, and a value id generated by this
176 /// class to use in the VST and summary block records.
177 std::map<GlobalValue::GUID, unsigned> GUIDToValueIdMap;
178
179 /// Tracks the last value id recorded in the GUIDToValueMap.
180 unsigned GlobalValueId;
181
182 /// Saves the offset of the VSTOffset record that must eventually be
183 /// backpatched with the offset of the actual VST.
184 uint64_t VSTOffsetPlaceholder = 0;
185
186public:
187 /// Constructs a ModuleBitcodeWriterBase object for the given Module,
188 /// writing to the provided \p Buffer.
189 ModuleBitcodeWriterBase(const Module &M, StringTableBuilder &StrtabBuilder,
190 BitstreamWriter &Stream,
191 bool ShouldPreserveUseListOrder,
193 : BitcodeWriterBase(Stream, StrtabBuilder), M(M),
194 VE(M, ShouldPreserveUseListOrder), Index(Index) {
195 // Assign ValueIds to any callee values in the index that came from
196 // indirect call profiles and were recorded as a GUID not a Value*
197 // (which would have been assigned an ID by the ValueEnumerator).
198 // The starting ValueId is just after the number of values in the
199 // ValueEnumerator, so that they can be emitted in the VST.
200 GlobalValueId = VE.getValues().size();
201 if (!Index)
202 return;
203 for (const auto &GUIDSummaryLists : *Index)
204 // Examine all summaries for this GUID.
205 for (auto &Summary : GUIDSummaryLists.second.SummaryList)
206 if (auto FS = dyn_cast<FunctionSummary>(Summary.get())) {
207 // For each call in the function summary, see if the call
208 // is to a GUID (which means it is for an indirect call,
209 // otherwise we would have a Value for it). If so, synthesize
210 // a value id.
211 for (auto &CallEdge : FS->calls())
212 if (!CallEdge.first.haveGVs() || !CallEdge.first.getValue())
213 assignValueId(CallEdge.first.getGUID());
214
215 // For each referenced variables in the function summary, see if the
216 // variable is represented by a GUID (as opposed to a symbol to
217 // declarations or definitions in the module). If so, synthesize a
218 // value id.
219 for (auto &RefEdge : FS->refs())
220 if (!RefEdge.haveGVs() || !RefEdge.getValue())
221 assignValueId(RefEdge.getGUID());
222 }
223 }
224
225protected:
226 void writePerModuleGlobalValueSummary();
227
228private:
229 void writePerModuleFunctionSummaryRecord(
231 unsigned ValueID, unsigned FSCallsAbbrev, unsigned FSCallsProfileAbbrev,
232 unsigned CallsiteAbbrev, unsigned AllocAbbrev, const Function &F);
233 void writeModuleLevelReferences(const GlobalVariable &V,
235 unsigned FSModRefsAbbrev,
236 unsigned FSModVTableRefsAbbrev);
237
238 void assignValueId(GlobalValue::GUID ValGUID) {
239 GUIDToValueIdMap[ValGUID] = ++GlobalValueId;
240 }
241
242 unsigned getValueId(GlobalValue::GUID ValGUID) {
243 const auto &VMI = GUIDToValueIdMap.find(ValGUID);
244 // Expect that any GUID value had a value Id assigned by an
245 // earlier call to assignValueId.
246 assert(VMI != GUIDToValueIdMap.end() &&
247 "GUID does not have assigned value Id");
248 return VMI->second;
249 }
250
251 // Helper to get the valueId for the type of value recorded in VI.
252 unsigned getValueId(ValueInfo VI) {
253 if (!VI.haveGVs() || !VI.getValue())
254 return getValueId(VI.getGUID());
255 return VE.getValueID(VI.getValue());
256 }
257
258 std::map<GlobalValue::GUID, unsigned> &valueIds() { return GUIDToValueIdMap; }
259};
260
261/// Class to manage the bitcode writing for a module.
262class ModuleBitcodeWriter : public ModuleBitcodeWriterBase {
263 /// Pointer to the buffer allocated by caller for bitcode writing.
264 const SmallVectorImpl<char> &Buffer;
265
266 /// True if a module hash record should be written.
267 bool GenerateHash;
268
269 /// If non-null, when GenerateHash is true, the resulting hash is written
270 /// into ModHash.
271 ModuleHash *ModHash;
272
273 SHA1 Hasher;
274
275 /// The start bit of the identification block.
276 uint64_t BitcodeStartBit;
277
278public:
279 /// Constructs a ModuleBitcodeWriter object for the given Module,
280 /// writing to the provided \p Buffer.
281 ModuleBitcodeWriter(const Module &M, SmallVectorImpl<char> &Buffer,
282 StringTableBuilder &StrtabBuilder,
283 BitstreamWriter &Stream, bool ShouldPreserveUseListOrder,
284 const ModuleSummaryIndex *Index, bool GenerateHash,
285 ModuleHash *ModHash = nullptr)
286 : ModuleBitcodeWriterBase(M, StrtabBuilder, Stream,
287 ShouldPreserveUseListOrder, Index),
288 Buffer(Buffer), GenerateHash(GenerateHash), ModHash(ModHash),
289 BitcodeStartBit(Stream.GetCurrentBitNo()) {}
290
291 /// Emit the current module to the bitstream.
292 void write();
293
294private:
295 uint64_t bitcodeStartBit() { return BitcodeStartBit; }
296
297 size_t addToStrtab(StringRef Str);
298
299 void writeAttributeGroupTable();
300 void writeAttributeTable();
301 void writeTypeTable();
302 void writeComdats();
303 void writeValueSymbolTableForwardDecl();
304 void writeModuleInfo();
305 void writeValueAsMetadata(const ValueAsMetadata *MD,
308 unsigned Abbrev);
309 unsigned createDILocationAbbrev();
311 unsigned &Abbrev);
312 unsigned createGenericDINodeAbbrev();
314 SmallVectorImpl<uint64_t> &Record, unsigned &Abbrev);
316 unsigned Abbrev);
319 unsigned Abbrev);
320 void writeDIEnumerator(const DIEnumerator *N,
321 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
323 unsigned Abbrev);
324 void writeDIStringType(const DIStringType *N,
325 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
327 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
329 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
332 unsigned Abbrev);
334 unsigned Abbrev);
336 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
337 void writeDISubprogram(const DISubprogram *N,
338 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
340 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
343 unsigned Abbrev);
345 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
347 unsigned Abbrev);
349 unsigned Abbrev);
351 unsigned Abbrev);
354 unsigned Abbrev);
356 unsigned Abbrev);
359 unsigned Abbrev);
362 unsigned Abbrev);
365 unsigned Abbrev);
367 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
368 void writeDILabel(const DILabel *N,
369 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
370 void writeDIExpression(const DIExpression *N,
371 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
374 unsigned Abbrev);
376 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
379 unsigned Abbrev);
380 unsigned createNamedMetadataAbbrev();
381 void writeNamedMetadata(SmallVectorImpl<uint64_t> &Record);
382 unsigned createMetadataStringsAbbrev();
383 void writeMetadataStrings(ArrayRef<const Metadata *> Strings,
385 void writeMetadataRecords(ArrayRef<const Metadata *> MDs,
387 std::vector<unsigned> *MDAbbrevs = nullptr,
388 std::vector<uint64_t> *IndexPos = nullptr);
389 void writeModuleMetadata();
390 void writeFunctionMetadata(const Function &F);
391 void writeFunctionMetadataAttachment(const Function &F);
392 void pushGlobalMetadataAttachment(SmallVectorImpl<uint64_t> &Record,
393 const GlobalObject &GO);
394 void writeModuleMetadataKinds();
395 void writeOperandBundleTags();
396 void writeSyncScopeNames();
397 void writeConstants(unsigned FirstVal, unsigned LastVal, bool isGlobal);
398 void writeModuleConstants();
399 bool pushValueAndType(const Value *V, unsigned InstID,
401 void writeOperandBundles(const CallBase &CB, unsigned InstID);
402 void pushValue(const Value *V, unsigned InstID,
404 void pushValueSigned(const Value *V, unsigned InstID,
406 void writeInstruction(const Instruction &I, unsigned InstID,
408 void writeFunctionLevelValueSymbolTable(const ValueSymbolTable &VST);
409 void writeGlobalValueSymbolTable(
410 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex);
411 void writeUseList(UseListOrder &&Order);
412 void writeUseListBlock(const Function *F);
413 void
414 writeFunction(const Function &F,
415 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex);
416 void writeBlockInfo();
417 void writeModuleHash(size_t BlockStartPos);
418
419 unsigned getEncodedSyncScopeID(SyncScope::ID SSID) {
420 return unsigned(SSID);
421 }
422
423 unsigned getEncodedAlign(MaybeAlign Alignment) { return encode(Alignment); }
424};
425
426/// Class to manage the bitcode writing for a combined index.
427class IndexBitcodeWriter : public BitcodeWriterBase {
428 /// The combined index to write to bitcode.
430
431 /// When writing a subset of the index for distributed backends, client
432 /// provides a map of modules to the corresponding GUIDs/summaries to write.
433 const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex;
434
435 /// Map that holds the correspondence between the GUID used in the combined
436 /// index and a value id generated by this class to use in references.
437 std::map<GlobalValue::GUID, unsigned> GUIDToValueIdMap;
438
439 // The sorted stack id indices actually used in the summary entries being
440 // written, which will be a subset of those in the full index in the case of
441 // distributed indexes.
442 std::vector<unsigned> StackIdIndices;
443
444 /// Tracks the last value id recorded in the GUIDToValueMap.
445 unsigned GlobalValueId = 0;
446
447 /// Tracks the assignment of module paths in the module path string table to
448 /// an id assigned for use in summary references to the module path.
450
451public:
452 /// Constructs a IndexBitcodeWriter object for the given combined index,
453 /// writing to the provided \p Buffer. When writing a subset of the index
454 /// for a distributed backend, provide a \p ModuleToSummariesForIndex map.
455 IndexBitcodeWriter(BitstreamWriter &Stream, StringTableBuilder &StrtabBuilder,
457 const std::map<std::string, GVSummaryMapTy>
458 *ModuleToSummariesForIndex = nullptr)
459 : BitcodeWriterBase(Stream, StrtabBuilder), Index(Index),
460 ModuleToSummariesForIndex(ModuleToSummariesForIndex) {
461 // Assign unique value ids to all summaries to be written, for use
462 // in writing out the call graph edges. Save the mapping from GUID
463 // to the new global value id to use when writing those edges, which
464 // are currently saved in the index in terms of GUID.
465 forEachSummary([&](GVInfo I, bool IsAliasee) {
466 GUIDToValueIdMap[I.first] = ++GlobalValueId;
467 if (IsAliasee)
468 return;
469 auto *FS = dyn_cast<FunctionSummary>(I.second);
470 if (!FS)
471 return;
472 // Record all stack id indices actually used in the summary entries being
473 // written, so that we can compact them in the case of distributed ThinLTO
474 // indexes.
475 for (auto &CI : FS->callsites()) {
476 // If the stack id list is empty, this callsite info was synthesized for
477 // a missing tail call frame. Ensure that the callee's GUID gets a value
478 // id. Normally we only generate these for defined summaries, which in
479 // the case of distributed ThinLTO is only the functions already defined
480 // in the module or that we want to import. We don't bother to include
481 // all the callee symbols as they aren't normally needed in the backend.
482 // However, for the synthesized callsite infos we do need the callee
483 // GUID in the backend so that we can correlate the identified callee
484 // with this callsite info (which for non-tail calls is done by the
485 // ordering of the callsite infos and verified via stack ids).
486 if (CI.StackIdIndices.empty()) {
487 GUIDToValueIdMap[CI.Callee.getGUID()] = ++GlobalValueId;
488 continue;
489 }
490 for (auto Idx : CI.StackIdIndices)
491 StackIdIndices.push_back(Idx);
492 }
493 for (auto &AI : FS->allocs())
494 for (auto &MIB : AI.MIBs)
495 for (auto Idx : MIB.StackIdIndices)
496 StackIdIndices.push_back(Idx);
497 });
498 llvm::sort(StackIdIndices);
499 StackIdIndices.erase(
500 std::unique(StackIdIndices.begin(), StackIdIndices.end()),
501 StackIdIndices.end());
502 }
503
504 /// The below iterator returns the GUID and associated summary.
505 using GVInfo = std::pair<GlobalValue::GUID, GlobalValueSummary *>;
506
507 /// Calls the callback for each value GUID and summary to be written to
508 /// bitcode. This hides the details of whether they are being pulled from the
509 /// entire index or just those in a provided ModuleToSummariesForIndex map.
510 template<typename Functor>
511 void forEachSummary(Functor Callback) {
512 if (ModuleToSummariesForIndex) {
513 for (auto &M : *ModuleToSummariesForIndex)
514 for (auto &Summary : M.second) {
515 Callback(Summary, false);
516 // Ensure aliasee is handled, e.g. for assigning a valueId,
517 // even if we are not importing the aliasee directly (the
518 // imported alias will contain a copy of aliasee).
519 if (auto *AS = dyn_cast<AliasSummary>(Summary.getSecond()))
520 Callback({AS->getAliaseeGUID(), &AS->getAliasee()}, true);
521 }
522 } else {
523 for (auto &Summaries : Index)
524 for (auto &Summary : Summaries.second.SummaryList)
525 Callback({Summaries.first, Summary.get()}, false);
526 }
527 }
528
529 /// Calls the callback for each entry in the modulePaths StringMap that
530 /// should be written to the module path string table. This hides the details
531 /// of whether they are being pulled from the entire index or just those in a
532 /// provided ModuleToSummariesForIndex map.
533 template <typename Functor> void forEachModule(Functor Callback) {
534 if (ModuleToSummariesForIndex) {
535 for (const auto &M : *ModuleToSummariesForIndex) {
536 const auto &MPI = Index.modulePaths().find(M.first);
537 if (MPI == Index.modulePaths().end()) {
538 // This should only happen if the bitcode file was empty, in which
539 // case we shouldn't be importing (the ModuleToSummariesForIndex
540 // would only include the module we are writing and index for).
541 assert(ModuleToSummariesForIndex->size() == 1);
542 continue;
543 }
544 Callback(*MPI);
545 }
546 } else {
547 // Since StringMap iteration order isn't guaranteed, order by path string
548 // first.
549 // FIXME: Make this a vector of StringMapEntry instead to avoid the later
550 // map lookup.
551 std::vector<StringRef> ModulePaths;
552 for (auto &[ModPath, _] : Index.modulePaths())
553 ModulePaths.push_back(ModPath);
554 llvm::sort(ModulePaths.begin(), ModulePaths.end());
555 for (auto &ModPath : ModulePaths)
556 Callback(*Index.modulePaths().find(ModPath));
557 }
558 }
559
560 /// Main entry point for writing a combined index to bitcode.
561 void write();
562
563private:
564 void writeModStrings();
565 void writeCombinedGlobalValueSummary();
566
567 std::optional<unsigned> getValueId(GlobalValue::GUID ValGUID) {
568 auto VMI = GUIDToValueIdMap.find(ValGUID);
569 if (VMI == GUIDToValueIdMap.end())
570 return std::nullopt;
571 return VMI->second;
572 }
573
574 std::map<GlobalValue::GUID, unsigned> &valueIds() { return GUIDToValueIdMap; }
575};
576
577} // end anonymous namespace
578
579static unsigned getEncodedCastOpcode(unsigned Opcode) {
580 switch (Opcode) {
581 default: llvm_unreachable("Unknown cast instruction!");
582 case Instruction::Trunc : return bitc::CAST_TRUNC;
583 case Instruction::ZExt : return bitc::CAST_ZEXT;
584 case Instruction::SExt : return bitc::CAST_SEXT;
585 case Instruction::FPToUI : return bitc::CAST_FPTOUI;
586 case Instruction::FPToSI : return bitc::CAST_FPTOSI;
587 case Instruction::UIToFP : return bitc::CAST_UITOFP;
588 case Instruction::SIToFP : return bitc::CAST_SITOFP;
589 case Instruction::FPTrunc : return bitc::CAST_FPTRUNC;
590 case Instruction::FPExt : return bitc::CAST_FPEXT;
591 case Instruction::PtrToInt: return bitc::CAST_PTRTOINT;
592 case Instruction::IntToPtr: return bitc::CAST_INTTOPTR;
593 case Instruction::BitCast : return bitc::CAST_BITCAST;
594 case Instruction::AddrSpaceCast: return bitc::CAST_ADDRSPACECAST;
595 }
596}
597
598static unsigned getEncodedUnaryOpcode(unsigned Opcode) {
599 switch (Opcode) {
600 default: llvm_unreachable("Unknown binary instruction!");
601 case Instruction::FNeg: return bitc::UNOP_FNEG;
602 }
603}
604
605static unsigned getEncodedBinaryOpcode(unsigned Opcode) {
606 switch (Opcode) {
607 default: llvm_unreachable("Unknown binary instruction!");
608 case Instruction::Add:
609 case Instruction::FAdd: return bitc::BINOP_ADD;
610 case Instruction::Sub:
611 case Instruction::FSub: return bitc::BINOP_SUB;
612 case Instruction::Mul:
613 case Instruction::FMul: return bitc::BINOP_MUL;
614 case Instruction::UDiv: return bitc::BINOP_UDIV;
615 case Instruction::FDiv:
616 case Instruction::SDiv: return bitc::BINOP_SDIV;
617 case Instruction::URem: return bitc::BINOP_UREM;
618 case Instruction::FRem:
619 case Instruction::SRem: return bitc::BINOP_SREM;
620 case Instruction::Shl: return bitc::BINOP_SHL;
621 case Instruction::LShr: return bitc::BINOP_LSHR;
622 case Instruction::AShr: return bitc::BINOP_ASHR;
623 case Instruction::And: return bitc::BINOP_AND;
624 case Instruction::Or: return bitc::BINOP_OR;
625 case Instruction::Xor: return bitc::BINOP_XOR;
626 }
627}
628
630 switch (Op) {
631 default: llvm_unreachable("Unknown RMW operation!");
637 case AtomicRMWInst::Or: return bitc::RMW_OR;
648 return bitc::RMW_UINC_WRAP;
650 return bitc::RMW_UDEC_WRAP;
651 }
652}
653
654static unsigned getEncodedOrdering(AtomicOrdering Ordering) {
655 switch (Ordering) {
656 case AtomicOrdering::NotAtomic: return bitc::ORDERING_NOTATOMIC;
657 case AtomicOrdering::Unordered: return bitc::ORDERING_UNORDERED;
658 case AtomicOrdering::Monotonic: return bitc::ORDERING_MONOTONIC;
659 case AtomicOrdering::Acquire: return bitc::ORDERING_ACQUIRE;
660 case AtomicOrdering::Release: return bitc::ORDERING_RELEASE;
661 case AtomicOrdering::AcquireRelease: return bitc::ORDERING_ACQREL;
662 case AtomicOrdering::SequentiallyConsistent: return bitc::ORDERING_SEQCST;
663 }
664 llvm_unreachable("Invalid ordering");
665}
666
667static void writeStringRecord(BitstreamWriter &Stream, unsigned Code,
668 StringRef Str, unsigned AbbrevToUse) {
670
671 // Code: [strchar x N]
672 for (char C : Str) {
673 if (AbbrevToUse && !BitCodeAbbrevOp::isChar6(C))
674 AbbrevToUse = 0;
675 Vals.push_back(C);
676 }
677
678 // Emit the finished record.
679 Stream.EmitRecord(Code, Vals, AbbrevToUse);
680}
681
683 switch (Kind) {
684 case Attribute::Alignment:
686 case Attribute::AllocAlign:
688 case Attribute::AllocSize:
690 case Attribute::AlwaysInline:
692 case Attribute::Builtin:
694 case Attribute::ByVal:
696 case Attribute::Convergent:
698 case Attribute::InAlloca:
700 case Attribute::Cold:
702 case Attribute::DisableSanitizerInstrumentation:
704 case Attribute::FnRetThunkExtern:
706 case Attribute::Hot:
707 return bitc::ATTR_KIND_HOT;
708 case Attribute::ElementType:
710 case Attribute::InlineHint:
712 case Attribute::InReg:
714 case Attribute::JumpTable:
716 case Attribute::MinSize:
718 case Attribute::AllocatedPointer:
720 case Attribute::AllocKind:
722 case Attribute::Memory:
724 case Attribute::NoFPClass:
726 case Attribute::Naked:
728 case Attribute::Nest:
730 case Attribute::NoAlias:
732 case Attribute::NoBuiltin:
734 case Attribute::NoCallback:
736 case Attribute::NoCapture:
738 case Attribute::NoDuplicate:
740 case Attribute::NoFree:
742 case Attribute::NoImplicitFloat:
744 case Attribute::NoInline:
746 case Attribute::NoRecurse:
748 case Attribute::NoMerge:
750 case Attribute::NonLazyBind:
752 case Attribute::NonNull:
754 case Attribute::Dereferenceable:
756 case Attribute::DereferenceableOrNull:
758 case Attribute::NoRedZone:
760 case Attribute::NoReturn:
762 case Attribute::NoSync:
764 case Attribute::NoCfCheck:
766 case Attribute::NoProfile:
768 case Attribute::SkipProfile:
770 case Attribute::NoUnwind:
772 case Attribute::NoSanitizeBounds:
774 case Attribute::NoSanitizeCoverage:
776 case Attribute::NullPointerIsValid:
778 case Attribute::OptimizeForDebugging:
780 case Attribute::OptForFuzzing:
782 case Attribute::OptimizeForSize:
784 case Attribute::OptimizeNone:
786 case Attribute::ReadNone:
788 case Attribute::ReadOnly:
790 case Attribute::Returned:
792 case Attribute::ReturnsTwice:
794 case Attribute::SExt:
796 case Attribute::Speculatable:
798 case Attribute::StackAlignment:
800 case Attribute::StackProtect:
802 case Attribute::StackProtectReq:
804 case Attribute::StackProtectStrong:
806 case Attribute::SafeStack:
808 case Attribute::ShadowCallStack:
810 case Attribute::StrictFP:
812 case Attribute::StructRet:
814 case Attribute::SanitizeAddress:
816 case Attribute::SanitizeHWAddress:
818 case Attribute::SanitizeThread:
820 case Attribute::SanitizeMemory:
822 case Attribute::SpeculativeLoadHardening:
824 case Attribute::SwiftError:
826 case Attribute::SwiftSelf:
828 case Attribute::SwiftAsync:
830 case Attribute::UWTable:
832 case Attribute::VScaleRange:
834 case Attribute::WillReturn:
836 case Attribute::WriteOnly:
838 case Attribute::ZExt:
840 case Attribute::ImmArg:
842 case Attribute::SanitizeMemTag:
844 case Attribute::Preallocated:
846 case Attribute::NoUndef:
848 case Attribute::ByRef:
850 case Attribute::MustProgress:
852 case Attribute::PresplitCoroutine:
854 case Attribute::Writable:
856 case Attribute::CoroDestroyOnlyWhenComplete:
858 case Attribute::DeadOnUnwind:
860 case Attribute::Range:
863 llvm_unreachable("Can not encode end-attribute kinds marker.");
864 case Attribute::None:
865 llvm_unreachable("Can not encode none-attribute.");
868 llvm_unreachable("Trying to encode EmptyKey/TombstoneKey");
869 }
870
871 llvm_unreachable("Trying to encode unknown attribute");
872}
873
875 if ((int64_t)V >= 0)
876 Vals.push_back(V << 1);
877 else
878 Vals.push_back((-V << 1) | 1);
879}
880
881static void emitWideAPInt(SmallVectorImpl<uint64_t> &Vals, const APInt &A) {
882 // We have an arbitrary precision integer value to write whose
883 // bit width is > 64. However, in canonical unsigned integer
884 // format it is likely that the high bits are going to be zero.
885 // So, we only write the number of active words.
886 unsigned NumWords = A.getActiveWords();
887 const uint64_t *RawData = A.getRawData();
888 for (unsigned i = 0; i < NumWords; i++)
889 emitSignedInt64(Vals, RawData[i]);
890}
891
893 const ConstantRange &CR) {
894 unsigned BitWidth = CR.getBitWidth();
895 Record.push_back(BitWidth);
896 if (BitWidth > 64) {
897 Record.push_back(CR.getLower().getActiveWords() |
898 (uint64_t(CR.getUpper().getActiveWords()) << 32));
901 } else {
904 }
905}
906
907void ModuleBitcodeWriter::writeAttributeGroupTable() {
908 const std::vector<ValueEnumerator::IndexAndAttrSet> &AttrGrps =
910 if (AttrGrps.empty()) return;
911
913
915 for (ValueEnumerator::IndexAndAttrSet Pair : AttrGrps) {
916 unsigned AttrListIndex = Pair.first;
917 AttributeSet AS = Pair.second;
918 Record.push_back(VE.getAttributeGroupID(Pair));
919 Record.push_back(AttrListIndex);
920
921 for (Attribute Attr : AS) {
922 if (Attr.isEnumAttribute()) {
923 Record.push_back(0);
924 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
925 } else if (Attr.isIntAttribute()) {
926 Record.push_back(1);
927 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
928 Record.push_back(Attr.getValueAsInt());
929 } else if (Attr.isStringAttribute()) {
930 StringRef Kind = Attr.getKindAsString();
931 StringRef Val = Attr.getValueAsString();
932
933 Record.push_back(Val.empty() ? 3 : 4);
934 Record.append(Kind.begin(), Kind.end());
935 Record.push_back(0);
936 if (!Val.empty()) {
937 Record.append(Val.begin(), Val.end());
938 Record.push_back(0);
939 }
940 } else if (Attr.isTypeAttribute()) {
941 Type *Ty = Attr.getValueAsType();
942 Record.push_back(Ty ? 6 : 5);
943 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
944 if (Ty)
945 Record.push_back(VE.getTypeID(Attr.getValueAsType()));
946 } else {
947 assert(Attr.isConstantRangeAttribute());
948 Record.push_back(7);
949 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
950 emitConstantRange(Record, Attr.getValueAsConstantRange());
951 }
952 }
953
955 Record.clear();
956 }
957
958 Stream.ExitBlock();
959}
960
961void ModuleBitcodeWriter::writeAttributeTable() {
962 const std::vector<AttributeList> &Attrs = VE.getAttributeLists();
963 if (Attrs.empty()) return;
964
966
968 for (const AttributeList &AL : Attrs) {
969 for (unsigned i : AL.indexes()) {
970 AttributeSet AS = AL.getAttributes(i);
971 if (AS.hasAttributes())
972 Record.push_back(VE.getAttributeGroupID({i, AS}));
973 }
974
976 Record.clear();
977 }
978
979 Stream.ExitBlock();
980}
981
982/// WriteTypeTable - Write out the type table for a module.
983void ModuleBitcodeWriter::writeTypeTable() {
984 const ValueEnumerator::TypeList &TypeList = VE.getTypes();
985
986 Stream.EnterSubblock(bitc::TYPE_BLOCK_ID_NEW, 4 /*count from # abbrevs */);
988
990
991 // Abbrev for TYPE_CODE_OPAQUE_POINTER.
992 auto Abbv = std::make_shared<BitCodeAbbrev>();
994 Abbv->Add(BitCodeAbbrevOp(0)); // Addrspace = 0
995 unsigned OpaquePtrAbbrev = Stream.EmitAbbrev(std::move(Abbv));
996
997 // Abbrev for TYPE_CODE_FUNCTION.
998 Abbv = std::make_shared<BitCodeAbbrev>();
1000 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg
1002 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
1003 unsigned FunctionAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1004
1005 // Abbrev for TYPE_CODE_STRUCT_ANON.
1006 Abbv = std::make_shared<BitCodeAbbrev>();
1008 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
1010 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
1011 unsigned StructAnonAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1012
1013 // Abbrev for TYPE_CODE_STRUCT_NAME.
1014 Abbv = std::make_shared<BitCodeAbbrev>();
1018 unsigned StructNameAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1019
1020 // Abbrev for TYPE_CODE_STRUCT_NAMED.
1021 Abbv = std::make_shared<BitCodeAbbrev>();
1023 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
1025 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
1026 unsigned StructNamedAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1027
1028 // Abbrev for TYPE_CODE_ARRAY.
1029 Abbv = std::make_shared<BitCodeAbbrev>();
1031 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size
1032 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
1033 unsigned ArrayAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1034
1035 // Emit an entry count so the reader can reserve space.
1036 TypeVals.push_back(TypeList.size());
1037 Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
1038 TypeVals.clear();
1039
1040 // Loop over all of the types, emitting each in turn.
1041 for (Type *T : TypeList) {
1042 int AbbrevToUse = 0;
1043 unsigned Code = 0;
1044
1045 switch (T->getTypeID()) {
1059 case Type::IntegerTyID:
1060 // INTEGER: [width]
1062 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
1063 break;
1064 case Type::PointerTyID: {
1065 PointerType *PTy = cast<PointerType>(T);
1066 unsigned AddressSpace = PTy->getAddressSpace();
1067 // OPAQUE_POINTER: [address space]
1069 TypeVals.push_back(AddressSpace);
1070 if (AddressSpace == 0)
1071 AbbrevToUse = OpaquePtrAbbrev;
1072 break;
1073 }
1074 case Type::FunctionTyID: {
1075 FunctionType *FT = cast<FunctionType>(T);
1076 // FUNCTION: [isvararg, retty, paramty x N]
1078 TypeVals.push_back(FT->isVarArg());
1079 TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
1080 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
1081 TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
1082 AbbrevToUse = FunctionAbbrev;
1083 break;
1084 }
1085 case Type::StructTyID: {
1086 StructType *ST = cast<StructType>(T);
1087 // STRUCT: [ispacked, eltty x N]
1088 TypeVals.push_back(ST->isPacked());
1089 // Output all of the element types.
1090 for (Type *ET : ST->elements())
1091 TypeVals.push_back(VE.getTypeID(ET));
1092
1093 if (ST->isLiteral()) {
1095 AbbrevToUse = StructAnonAbbrev;
1096 } else {
1097 if (ST->isOpaque()) {
1099 } else {
1101 AbbrevToUse = StructNamedAbbrev;
1102 }
1103
1104 // Emit the name if it is present.
1105 if (!ST->getName().empty())
1107 StructNameAbbrev);
1108 }
1109 break;
1110 }
1111 case Type::ArrayTyID: {
1112 ArrayType *AT = cast<ArrayType>(T);
1113 // ARRAY: [numelts, eltty]
1115 TypeVals.push_back(AT->getNumElements());
1116 TypeVals.push_back(VE.getTypeID(AT->getElementType()));
1117 AbbrevToUse = ArrayAbbrev;
1118 break;
1119 }
1122 VectorType *VT = cast<VectorType>(T);
1123 // VECTOR [numelts, eltty] or
1124 // [numelts, eltty, scalable]
1126 TypeVals.push_back(VT->getElementCount().getKnownMinValue());
1127 TypeVals.push_back(VE.getTypeID(VT->getElementType()));
1128 if (isa<ScalableVectorType>(VT))
1129 TypeVals.push_back(true);
1130 break;
1131 }
1132 case Type::TargetExtTyID: {
1133 TargetExtType *TET = cast<TargetExtType>(T);
1136 StructNameAbbrev);
1137 TypeVals.push_back(TET->getNumTypeParameters());
1138 for (Type *InnerTy : TET->type_params())
1139 TypeVals.push_back(VE.getTypeID(InnerTy));
1140 for (unsigned IntParam : TET->int_params())
1141 TypeVals.push_back(IntParam);
1142 break;
1143 }
1145 llvm_unreachable("Typed pointers cannot be added to IR modules");
1146 }
1147
1148 // Emit the finished record.
1149 Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
1150 TypeVals.clear();
1151 }
1152
1153 Stream.ExitBlock();
1154}
1155
1156static unsigned getEncodedLinkage(const GlobalValue::LinkageTypes Linkage) {
1157 switch (Linkage) {
1159 return 0;
1161 return 16;
1163 return 2;
1165 return 3;
1167 return 18;
1169 return 7;
1171 return 8;
1173 return 9;
1175 return 17;
1177 return 19;
1179 return 12;
1180 }
1181 llvm_unreachable("Invalid linkage");
1182}
1183
1184static unsigned getEncodedLinkage(const GlobalValue &GV) {
1185 return getEncodedLinkage(GV.getLinkage());
1186}
1187
1189 uint64_t RawFlags = 0;
1190 RawFlags |= Flags.ReadNone;
1191 RawFlags |= (Flags.ReadOnly << 1);
1192 RawFlags |= (Flags.NoRecurse << 2);
1193 RawFlags |= (Flags.ReturnDoesNotAlias << 3);
1194 RawFlags |= (Flags.NoInline << 4);
1195 RawFlags |= (Flags.AlwaysInline << 5);
1196 RawFlags |= (Flags.NoUnwind << 6);
1197 RawFlags |= (Flags.MayThrow << 7);
1198 RawFlags |= (Flags.HasUnknownCall << 8);
1199 RawFlags |= (Flags.MustBeUnreachable << 9);
1200 return RawFlags;
1201}
1202
1203// Decode the flags for GlobalValue in the summary. See getDecodedGVSummaryFlags
1204// in BitcodeReader.cpp.
1206 uint64_t RawFlags = 0;
1207
1208 RawFlags |= Flags.NotEligibleToImport; // bool
1209 RawFlags |= (Flags.Live << 1);
1210 RawFlags |= (Flags.DSOLocal << 2);
1211 RawFlags |= (Flags.CanAutoHide << 3);
1212
1213 // Linkage don't need to be remapped at that time for the summary. Any future
1214 // change to the getEncodedLinkage() function will need to be taken into
1215 // account here as well.
1216 RawFlags = (RawFlags << 4) | Flags.Linkage; // 4 bits
1217
1218 RawFlags |= (Flags.Visibility << 8); // 2 bits
1219
1220 RawFlags |= (Flags.ImportType << 10); // 1 bit
1221
1222 return RawFlags;
1223}
1224
1226 uint64_t RawFlags = Flags.MaybeReadOnly | (Flags.MaybeWriteOnly << 1) |
1227 (Flags.Constant << 2) | Flags.VCallVisibility << 3;
1228 return RawFlags;
1229}
1230
1232 uint64_t RawFlags = 0;
1233
1234 RawFlags |= CI.Hotness; // 3 bits
1235 RawFlags |= (CI.HasTailCall << 3); // 1 bit
1236
1237 return RawFlags;
1238}
1239
1241 uint64_t RawFlags = 0;
1242
1243 RawFlags |= CI.RelBlockFreq; // CalleeInfo::RelBlockFreqBits bits
1244 RawFlags |= (CI.HasTailCall << CalleeInfo::RelBlockFreqBits); // 1 bit
1245
1246 return RawFlags;
1247}
1248
1249static unsigned getEncodedVisibility(const GlobalValue &GV) {
1250 switch (GV.getVisibility()) {
1251 case GlobalValue::DefaultVisibility: return 0;
1252 case GlobalValue::HiddenVisibility: return 1;
1253 case GlobalValue::ProtectedVisibility: return 2;
1254 }
1255 llvm_unreachable("Invalid visibility");
1256}
1257
1258static unsigned getEncodedDLLStorageClass(const GlobalValue &GV) {
1259 switch (GV.getDLLStorageClass()) {
1260 case GlobalValue::DefaultStorageClass: return 0;
1263 }
1264 llvm_unreachable("Invalid DLL storage class");
1265}
1266
1267static unsigned getEncodedThreadLocalMode(const GlobalValue &GV) {
1268 switch (GV.getThreadLocalMode()) {
1269 case GlobalVariable::NotThreadLocal: return 0;
1270 case GlobalVariable::GeneralDynamicTLSModel: return 1;
1271 case GlobalVariable::LocalDynamicTLSModel: return 2;
1272 case GlobalVariable::InitialExecTLSModel: return 3;
1273 case GlobalVariable::LocalExecTLSModel: return 4;
1274 }
1275 llvm_unreachable("Invalid TLS model");
1276}
1277
1278static unsigned getEncodedComdatSelectionKind(const Comdat &C) {
1279 switch (C.getSelectionKind()) {
1280 case Comdat::Any:
1282 case Comdat::ExactMatch:
1284 case Comdat::Largest:
1288 case Comdat::SameSize:
1290 }
1291 llvm_unreachable("Invalid selection kind");
1292}
1293
1294static unsigned getEncodedUnnamedAddr(const GlobalValue &GV) {
1295 switch (GV.getUnnamedAddr()) {
1296 case GlobalValue::UnnamedAddr::None: return 0;
1297 case GlobalValue::UnnamedAddr::Local: return 2;
1298 case GlobalValue::UnnamedAddr::Global: return 1;
1299 }
1300 llvm_unreachable("Invalid unnamed_addr");
1301}
1302
1303size_t ModuleBitcodeWriter::addToStrtab(StringRef Str) {
1304 if (GenerateHash)
1305 Hasher.update(Str);
1306 return StrtabBuilder.add(Str);
1307}
1308
1309void ModuleBitcodeWriter::writeComdats() {
1311 for (const Comdat *C : VE.getComdats()) {
1312 // COMDAT: [strtab offset, strtab size, selection_kind]
1313 Vals.push_back(addToStrtab(C->getName()));
1314 Vals.push_back(C->getName().size());
1316 Stream.EmitRecord(bitc::MODULE_CODE_COMDAT, Vals, /*AbbrevToUse=*/0);
1317 Vals.clear();
1318 }
1319}
1320
1321/// Write a record that will eventually hold the word offset of the
1322/// module-level VST. For now the offset is 0, which will be backpatched
1323/// after the real VST is written. Saves the bit offset to backpatch.
1324void ModuleBitcodeWriter::writeValueSymbolTableForwardDecl() {
1325 // Write a placeholder value in for the offset of the real VST,
1326 // which is written after the function blocks so that it can include
1327 // the offset of each function. The placeholder offset will be
1328 // updated when the real VST is written.
1329 auto Abbv = std::make_shared<BitCodeAbbrev>();
1331 // Blocks are 32-bit aligned, so we can use a 32-bit word offset to
1332 // hold the real VST offset. Must use fixed instead of VBR as we don't
1333 // know how many VBR chunks to reserve ahead of time.
1335 unsigned VSTOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1336
1337 // Emit the placeholder
1339 Stream.EmitRecordWithAbbrev(VSTOffsetAbbrev, Vals);
1340
1341 // Compute and save the bit offset to the placeholder, which will be
1342 // patched when the real VST is written. We can simply subtract the 32-bit
1343 // fixed size from the current bit number to get the location to backpatch.
1344 VSTOffsetPlaceholder = Stream.GetCurrentBitNo() - 32;
1345}
1346
1348
1349/// Determine the encoding to use for the given string name and length.
1351 bool isChar6 = true;
1352 for (char C : Str) {
1353 if (isChar6)
1354 isChar6 = BitCodeAbbrevOp::isChar6(C);
1355 if ((unsigned char)C & 128)
1356 // don't bother scanning the rest.
1357 return SE_Fixed8;
1358 }
1359 if (isChar6)
1360 return SE_Char6;
1361 return SE_Fixed7;
1362}
1363
1364static_assert(sizeof(GlobalValue::SanitizerMetadata) <= sizeof(unsigned),
1365 "Sanitizer Metadata is too large for naive serialization.");
1366static unsigned
1368 return Meta.NoAddress | (Meta.NoHWAddress << 1) |
1369 (Meta.Memtag << 2) | (Meta.IsDynInit << 3);
1370}
1371
1372/// Emit top-level description of module, including target triple, inline asm,
1373/// descriptors for global variables, and function prototype info.
1374/// Returns the bit offset to backpatch with the location of the real VST.
1375void ModuleBitcodeWriter::writeModuleInfo() {
1376 // Emit various pieces of data attached to a module.
1377 if (!M.getTargetTriple().empty())
1378 writeStringRecord(Stream, bitc::MODULE_CODE_TRIPLE, M.getTargetTriple(),
1379 0 /*TODO*/);
1380 const std::string &DL = M.getDataLayoutStr();
1381 if (!DL.empty())
1383 if (!M.getModuleInlineAsm().empty())
1384 writeStringRecord(Stream, bitc::MODULE_CODE_ASM, M.getModuleInlineAsm(),
1385 0 /*TODO*/);
1386
1387 // Emit information about sections and GC, computing how many there are. Also
1388 // compute the maximum alignment value.
1389 std::map<std::string, unsigned> SectionMap;
1390 std::map<std::string, unsigned> GCMap;
1391 MaybeAlign MaxAlignment;
1392 unsigned MaxGlobalType = 0;
1393 const auto UpdateMaxAlignment = [&MaxAlignment](const MaybeAlign A) {
1394 if (A)
1395 MaxAlignment = !MaxAlignment ? *A : std::max(*MaxAlignment, *A);
1396 };
1397 for (const GlobalVariable &GV : M.globals()) {
1398 UpdateMaxAlignment(GV.getAlign());
1399 MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV.getValueType()));
1400 if (GV.hasSection()) {
1401 // Give section names unique ID's.
1402 unsigned &Entry = SectionMap[std::string(GV.getSection())];
1403 if (!Entry) {
1404 writeStringRecord(Stream, bitc::MODULE_CODE_SECTIONNAME, GV.getSection(),
1405 0 /*TODO*/);
1406 Entry = SectionMap.size();
1407 }
1408 }
1409 }
1410 for (const Function &F : M) {
1411 UpdateMaxAlignment(F.getAlign());
1412 if (F.hasSection()) {
1413 // Give section names unique ID's.
1414 unsigned &Entry = SectionMap[std::string(F.getSection())];
1415 if (!Entry) {
1417 0 /*TODO*/);
1418 Entry = SectionMap.size();
1419 }
1420 }
1421 if (F.hasGC()) {
1422 // Same for GC names.
1423 unsigned &Entry = GCMap[F.getGC()];
1424 if (!Entry) {
1426 0 /*TODO*/);
1427 Entry = GCMap.size();
1428 }
1429 }
1430 }
1431
1432 // Emit abbrev for globals, now that we know # sections and max alignment.
1433 unsigned SimpleGVarAbbrev = 0;
1434 if (!M.global_empty()) {
1435 // Add an abbrev for common globals with no visibility or thread localness.
1436 auto Abbv = std::make_shared<BitCodeAbbrev>();
1441 Log2_32_Ceil(MaxGlobalType+1)));
1442 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // AddrSpace << 2
1443 //| explicitType << 1
1444 //| constant
1445 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer.
1446 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // Linkage.
1447 if (!MaxAlignment) // Alignment.
1448 Abbv->Add(BitCodeAbbrevOp(0));
1449 else {
1450 unsigned MaxEncAlignment = getEncodedAlign(MaxAlignment);
1452 Log2_32_Ceil(MaxEncAlignment+1)));
1453 }
1454 if (SectionMap.empty()) // Section.
1455 Abbv->Add(BitCodeAbbrevOp(0));
1456 else
1458 Log2_32_Ceil(SectionMap.size()+1)));
1459 // Don't bother emitting vis + thread local.
1460 SimpleGVarAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1461 }
1462
1464 // Emit the module's source file name.
1465 {
1466 StringEncoding Bits = getStringEncoding(M.getSourceFileName());
1468 if (Bits == SE_Char6)
1469 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Char6);
1470 else if (Bits == SE_Fixed7)
1471 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7);
1472
1473 // MODULE_CODE_SOURCE_FILENAME: [namechar x N]
1474 auto Abbv = std::make_shared<BitCodeAbbrev>();
1477 Abbv->Add(AbbrevOpToUse);
1478 unsigned FilenameAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1479
1480 for (const auto P : M.getSourceFileName())
1481 Vals.push_back((unsigned char)P);
1482
1483 // Emit the finished record.
1484 Stream.EmitRecord(bitc::MODULE_CODE_SOURCE_FILENAME, Vals, FilenameAbbrev);
1485 Vals.clear();
1486 }
1487
1488 // Emit the global variable information.
1489 for (const GlobalVariable &GV : M.globals()) {
1490 unsigned AbbrevToUse = 0;
1491
1492 // GLOBALVAR: [strtab offset, strtab size, type, isconst, initid,
1493 // linkage, alignment, section, visibility, threadlocal,
1494 // unnamed_addr, externally_initialized, dllstorageclass,
1495 // comdat, attributes, DSO_Local, GlobalSanitizer, code_model]
1496 Vals.push_back(addToStrtab(GV.getName()));
1497 Vals.push_back(GV.getName().size());
1498 Vals.push_back(VE.getTypeID(GV.getValueType()));
1499 Vals.push_back(GV.getType()->getAddressSpace() << 2 | 2 | GV.isConstant());
1500 Vals.push_back(GV.isDeclaration() ? 0 :
1501 (VE.getValueID(GV.getInitializer()) + 1));
1502 Vals.push_back(getEncodedLinkage(GV));
1503 Vals.push_back(getEncodedAlign(GV.getAlign()));
1504 Vals.push_back(GV.hasSection() ? SectionMap[std::string(GV.getSection())]
1505 : 0);
1506 if (GV.isThreadLocal() ||
1507 GV.getVisibility() != GlobalValue::DefaultVisibility ||
1508 GV.getUnnamedAddr() != GlobalValue::UnnamedAddr::None ||
1509 GV.isExternallyInitialized() ||
1510 GV.getDLLStorageClass() != GlobalValue::DefaultStorageClass ||
1511 GV.hasComdat() || GV.hasAttributes() || GV.isDSOLocal() ||
1512 GV.hasPartition() || GV.hasSanitizerMetadata() || GV.getCodeModel()) {
1516 Vals.push_back(GV.isExternallyInitialized());
1518 Vals.push_back(GV.hasComdat() ? VE.getComdatID(GV.getComdat()) : 0);
1519
1520 auto AL = GV.getAttributesAsList(AttributeList::FunctionIndex);
1521 Vals.push_back(VE.getAttributeListID(AL));
1522
1523 Vals.push_back(GV.isDSOLocal());
1524 Vals.push_back(addToStrtab(GV.getPartition()));
1525 Vals.push_back(GV.getPartition().size());
1526
1527 Vals.push_back((GV.hasSanitizerMetadata() ? serializeSanitizerMetadata(
1528 GV.getSanitizerMetadata())
1529 : 0));
1530 Vals.push_back(GV.getCodeModelRaw());
1531 } else {
1532 AbbrevToUse = SimpleGVarAbbrev;
1533 }
1534
1535 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
1536 Vals.clear();
1537 }
1538
1539 // Emit the function proto information.
1540 for (const Function &F : M) {
1541 // FUNCTION: [strtab offset, strtab size, type, callingconv, isproto,
1542 // linkage, paramattrs, alignment, section, visibility, gc,
1543 // unnamed_addr, prologuedata, dllstorageclass, comdat,
1544 // prefixdata, personalityfn, DSO_Local, addrspace]
1545 Vals.push_back(addToStrtab(F.getName()));
1546 Vals.push_back(F.getName().size());
1547 Vals.push_back(VE.getTypeID(F.getFunctionType()));
1548 Vals.push_back(F.getCallingConv());
1549 Vals.push_back(F.isDeclaration());
1551 Vals.push_back(VE.getAttributeListID(F.getAttributes()));
1552 Vals.push_back(getEncodedAlign(F.getAlign()));
1553 Vals.push_back(F.hasSection() ? SectionMap[std::string(F.getSection())]
1554 : 0);
1556 Vals.push_back(F.hasGC() ? GCMap[F.getGC()] : 0);
1558 Vals.push_back(F.hasPrologueData() ? (VE.getValueID(F.getPrologueData()) + 1)
1559 : 0);
1561 Vals.push_back(F.hasComdat() ? VE.getComdatID(F.getComdat()) : 0);
1562 Vals.push_back(F.hasPrefixData() ? (VE.getValueID(F.getPrefixData()) + 1)
1563 : 0);
1564 Vals.push_back(
1565 F.hasPersonalityFn() ? (VE.getValueID(F.getPersonalityFn()) + 1) : 0);
1566
1567 Vals.push_back(F.isDSOLocal());
1568 Vals.push_back(F.getAddressSpace());
1569 Vals.push_back(addToStrtab(F.getPartition()));
1570 Vals.push_back(F.getPartition().size());
1571
1572 unsigned AbbrevToUse = 0;
1573 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
1574 Vals.clear();
1575 }
1576
1577 // Emit the alias information.
1578 for (const GlobalAlias &A : M.aliases()) {
1579 // ALIAS: [strtab offset, strtab size, alias type, aliasee val#, linkage,
1580 // visibility, dllstorageclass, threadlocal, unnamed_addr,
1581 // DSO_Local]
1582 Vals.push_back(addToStrtab(A.getName()));
1583 Vals.push_back(A.getName().size());
1584 Vals.push_back(VE.getTypeID(A.getValueType()));
1585 Vals.push_back(A.getType()->getAddressSpace());
1586 Vals.push_back(VE.getValueID(A.getAliasee()));
1592 Vals.push_back(A.isDSOLocal());
1593 Vals.push_back(addToStrtab(A.getPartition()));
1594 Vals.push_back(A.getPartition().size());
1595
1596 unsigned AbbrevToUse = 0;
1597 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
1598 Vals.clear();
1599 }
1600
1601 // Emit the ifunc information.
1602 for (const GlobalIFunc &I : M.ifuncs()) {
1603 // IFUNC: [strtab offset, strtab size, ifunc type, address space, resolver
1604 // val#, linkage, visibility, DSO_Local]
1605 Vals.push_back(addToStrtab(I.getName()));
1606 Vals.push_back(I.getName().size());
1607 Vals.push_back(VE.getTypeID(I.getValueType()));
1608 Vals.push_back(I.getType()->getAddressSpace());
1609 Vals.push_back(VE.getValueID(I.getResolver()));
1612 Vals.push_back(I.isDSOLocal());
1613 Vals.push_back(addToStrtab(I.getPartition()));
1614 Vals.push_back(I.getPartition().size());
1615 Stream.EmitRecord(bitc::MODULE_CODE_IFUNC, Vals);
1616 Vals.clear();
1617 }
1618
1619 writeValueSymbolTableForwardDecl();
1620}
1621
1623 uint64_t Flags = 0;
1624
1625 if (const auto *OBO = dyn_cast<OverflowingBinaryOperator>(V)) {
1626 if (OBO->hasNoSignedWrap())
1627 Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP;
1628 if (OBO->hasNoUnsignedWrap())
1629 Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP;
1630 } else if (const auto *PEO = dyn_cast<PossiblyExactOperator>(V)) {
1631 if (PEO->isExact())
1632 Flags |= 1 << bitc::PEO_EXACT;
1633 } else if (const auto *PDI = dyn_cast<PossiblyDisjointInst>(V)) {
1634 if (PDI->isDisjoint())
1635 Flags |= 1 << bitc::PDI_DISJOINT;
1636 } else if (const auto *FPMO = dyn_cast<FPMathOperator>(V)) {
1637 if (FPMO->hasAllowReassoc())
1638 Flags |= bitc::AllowReassoc;
1639 if (FPMO->hasNoNaNs())
1640 Flags |= bitc::NoNaNs;
1641 if (FPMO->hasNoInfs())
1642 Flags |= bitc::NoInfs;
1643 if (FPMO->hasNoSignedZeros())
1644 Flags |= bitc::NoSignedZeros;
1645 if (FPMO->hasAllowReciprocal())
1646 Flags |= bitc::AllowReciprocal;
1647 if (FPMO->hasAllowContract())
1648 Flags |= bitc::AllowContract;
1649 if (FPMO->hasApproxFunc())
1650 Flags |= bitc::ApproxFunc;
1651 } else if (const auto *NNI = dyn_cast<PossiblyNonNegInst>(V)) {
1652 if (NNI->hasNonNeg())
1653 Flags |= 1 << bitc::PNNI_NON_NEG;
1654 } else if (const auto *TI = dyn_cast<TruncInst>(V)) {
1655 if (TI->hasNoSignedWrap())
1656 Flags |= 1 << bitc::TIO_NO_SIGNED_WRAP;
1657 if (TI->hasNoUnsignedWrap())
1658 Flags |= 1 << bitc::TIO_NO_UNSIGNED_WRAP;
1659 }
1660
1661 return Flags;
1662}
1663
1664void ModuleBitcodeWriter::writeValueAsMetadata(
1666 // Mimic an MDNode with a value as one operand.
1667 Value *V = MD->getValue();
1668 Record.push_back(VE.getTypeID(V->getType()));
1669 Record.push_back(VE.getValueID(V));
1671 Record.clear();
1672}
1673
1674void ModuleBitcodeWriter::writeMDTuple(const MDTuple *N,
1676 unsigned Abbrev) {
1677 for (const MDOperand &MDO : N->operands()) {
1678 Metadata *MD = MDO;
1679 assert(!(MD && isa<LocalAsMetadata>(MD)) &&
1680 "Unexpected function-local metadata");
1681 Record.push_back(VE.getMetadataOrNullID(MD));
1682 }
1683 Stream.EmitRecord(N->isDistinct() ? bitc::METADATA_DISTINCT_NODE
1685 Record, Abbrev);
1686 Record.clear();
1687}
1688
1689unsigned ModuleBitcodeWriter::createDILocationAbbrev() {
1690 // Assume the column is usually under 128, and always output the inlined-at
1691 // location (it's never more expensive than building an array size 1).
1692 auto Abbv = std::make_shared<BitCodeAbbrev>();
1700 return Stream.EmitAbbrev(std::move(Abbv));
1701}
1702
1703void ModuleBitcodeWriter::writeDILocation(const DILocation *N,
1705 unsigned &Abbrev) {
1706 if (!Abbrev)
1707 Abbrev = createDILocationAbbrev();
1708
1709 Record.push_back(N->isDistinct());
1710 Record.push_back(N->getLine());
1711 Record.push_back(N->getColumn());
1712 Record.push_back(VE.getMetadataID(N->getScope()));
1713 Record.push_back(VE.getMetadataOrNullID(N->getInlinedAt()));
1714 Record.push_back(N->isImplicitCode());
1715
1716 Stream.EmitRecord(bitc::METADATA_LOCATION, Record, Abbrev);
1717 Record.clear();
1718}
1719
1720unsigned ModuleBitcodeWriter::createGenericDINodeAbbrev() {
1721 // Assume the column is usually under 128, and always output the inlined-at
1722 // location (it's never more expensive than building an array size 1).
1723 auto Abbv = std::make_shared<BitCodeAbbrev>();
1731 return Stream.EmitAbbrev(std::move(Abbv));
1732}
1733
1734void ModuleBitcodeWriter::writeGenericDINode(const GenericDINode *N,
1736 unsigned &Abbrev) {
1737 if (!Abbrev)
1738 Abbrev = createGenericDINodeAbbrev();
1739
1740 Record.push_back(N->isDistinct());
1741 Record.push_back(N->getTag());
1742 Record.push_back(0); // Per-tag version field; unused for now.
1743
1744 for (auto &I : N->operands())
1745 Record.push_back(VE.getMetadataOrNullID(I));
1746
1748 Record.clear();
1749}
1750
1751void ModuleBitcodeWriter::writeDISubrange(const DISubrange *N,
1753 unsigned Abbrev) {
1754 const uint64_t Version = 2 << 1;
1755 Record.push_back((uint64_t)N->isDistinct() | Version);
1756 Record.push_back(VE.getMetadataOrNullID(N->getRawCountNode()));
1757 Record.push_back(VE.getMetadataOrNullID(N->getRawLowerBound()));
1758 Record.push_back(VE.getMetadataOrNullID(N->getRawUpperBound()));
1759 Record.push_back(VE.getMetadataOrNullID(N->getRawStride()));
1760
1761 Stream.EmitRecord(bitc::METADATA_SUBRANGE, Record, Abbrev);
1762 Record.clear();
1763}
1764
1765void ModuleBitcodeWriter::writeDIGenericSubrange(
1767 unsigned Abbrev) {
1768 Record.push_back((uint64_t)N->isDistinct());
1769 Record.push_back(VE.getMetadataOrNullID(N->getRawCountNode()));
1770 Record.push_back(VE.getMetadataOrNullID(N->getRawLowerBound()));
1771 Record.push_back(VE.getMetadataOrNullID(N->getRawUpperBound()));
1772 Record.push_back(VE.getMetadataOrNullID(N->getRawStride()));
1773
1775 Record.clear();
1776}
1777
1778void ModuleBitcodeWriter::writeDIEnumerator(const DIEnumerator *N,
1780 unsigned Abbrev) {
1781 const uint64_t IsBigInt = 1 << 2;
1782 Record.push_back(IsBigInt | (N->isUnsigned() << 1) | N->isDistinct());
1783 Record.push_back(N->getValue().getBitWidth());
1784 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1785 emitWideAPInt(Record, N->getValue());
1786
1788 Record.clear();
1789}
1790
1791void ModuleBitcodeWriter::writeDIBasicType(const DIBasicType *N,
1793 unsigned Abbrev) {
1794 Record.push_back(N->isDistinct());
1795 Record.push_back(N->getTag());
1796 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1797 Record.push_back(N->getSizeInBits());
1798 Record.push_back(N->getAlignInBits());
1799 Record.push_back(N->getEncoding());
1800 Record.push_back(N->getFlags());
1801
1803 Record.clear();
1804}
1805
1806void ModuleBitcodeWriter::writeDIStringType(const DIStringType *N,
1808 unsigned Abbrev) {
1809 Record.push_back(N->isDistinct());
1810 Record.push_back(N->getTag());
1811 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1812 Record.push_back(VE.getMetadataOrNullID(N->getStringLength()));
1813 Record.push_back(VE.getMetadataOrNullID(N->getStringLengthExp()));
1814 Record.push_back(VE.getMetadataOrNullID(N->getStringLocationExp()));
1815 Record.push_back(N->getSizeInBits());
1816 Record.push_back(N->getAlignInBits());
1817 Record.push_back(N->getEncoding());
1818
1820 Record.clear();
1821}
1822
1823void ModuleBitcodeWriter::writeDIDerivedType(const DIDerivedType *N,
1825 unsigned Abbrev) {
1826 Record.push_back(N->isDistinct());
1827 Record.push_back(N->getTag());
1828 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1829 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1830 Record.push_back(N->getLine());
1831 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1832 Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
1833 Record.push_back(N->getSizeInBits());
1834 Record.push_back(N->getAlignInBits());
1835 Record.push_back(N->getOffsetInBits());
1836 Record.push_back(N->getFlags());
1837 Record.push_back(VE.getMetadataOrNullID(N->getExtraData()));
1838
1839 // DWARF address space is encoded as N->getDWARFAddressSpace() + 1. 0 means
1840 // that there is no DWARF address space associated with DIDerivedType.
1841 if (const auto &DWARFAddressSpace = N->getDWARFAddressSpace())
1842 Record.push_back(*DWARFAddressSpace + 1);
1843 else
1844 Record.push_back(0);
1845
1846 Record.push_back(VE.getMetadataOrNullID(N->getAnnotations().get()));
1847
1848 if (auto PtrAuthData = N->getPtrAuthData())
1849 Record.push_back(PtrAuthData->RawData);
1850 else
1851 Record.push_back(0);
1852
1854 Record.clear();
1855}
1856
1857void ModuleBitcodeWriter::writeDICompositeType(
1859 unsigned Abbrev) {
1860 const unsigned IsNotUsedInOldTypeRef = 0x2;
1861 Record.push_back(IsNotUsedInOldTypeRef | (unsigned)N->isDistinct());
1862 Record.push_back(N->getTag());
1863 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1864 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1865 Record.push_back(N->getLine());
1866 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1867 Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
1868 Record.push_back(N->getSizeInBits());
1869 Record.push_back(N->getAlignInBits());
1870 Record.push_back(N->getOffsetInBits());
1871 Record.push_back(N->getFlags());
1872 Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
1873 Record.push_back(N->getRuntimeLang());
1874 Record.push_back(VE.getMetadataOrNullID(N->getVTableHolder()));
1875 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
1876 Record.push_back(VE.getMetadataOrNullID(N->getRawIdentifier()));
1877 Record.push_back(VE.getMetadataOrNullID(N->getDiscriminator()));
1878 Record.push_back(VE.getMetadataOrNullID(N->getRawDataLocation()));
1879 Record.push_back(VE.getMetadataOrNullID(N->getRawAssociated()));
1880 Record.push_back(VE.getMetadataOrNullID(N->getRawAllocated()));
1881 Record.push_back(VE.getMetadataOrNullID(N->getRawRank()));
1882 Record.push_back(VE.getMetadataOrNullID(N->getAnnotations().get()));
1883
1885 Record.clear();
1886}
1887
1888void ModuleBitcodeWriter::writeDISubroutineType(
1890 unsigned Abbrev) {
1891 const unsigned HasNoOldTypeRefs = 0x2;
1892 Record.push_back(HasNoOldTypeRefs | (unsigned)N->isDistinct());
1893 Record.push_back(N->getFlags());
1894 Record.push_back(VE.getMetadataOrNullID(N->getTypeArray().get()));
1895 Record.push_back(N->getCC());
1896
1898 Record.clear();
1899}
1900
1901void ModuleBitcodeWriter::writeDIFile(const DIFile *N,
1903 unsigned Abbrev) {
1904 Record.push_back(N->isDistinct());
1905 Record.push_back(VE.getMetadataOrNullID(N->getRawFilename()));
1906 Record.push_back(VE.getMetadataOrNullID(N->getRawDirectory()));
1907 if (N->getRawChecksum()) {
1908 Record.push_back(N->getRawChecksum()->Kind);
1909 Record.push_back(VE.getMetadataOrNullID(N->getRawChecksum()->Value));
1910 } else {
1911 // Maintain backwards compatibility with the old internal representation of
1912 // CSK_None in ChecksumKind by writing nulls here when Checksum is None.
1913 Record.push_back(0);
1914 Record.push_back(VE.getMetadataOrNullID(nullptr));
1915 }
1916 auto Source = N->getRawSource();
1917 if (Source)
1918 Record.push_back(VE.getMetadataOrNullID(Source));
1919
1920 Stream.EmitRecord(bitc::METADATA_FILE, Record, Abbrev);
1921 Record.clear();
1922}
1923
1924void ModuleBitcodeWriter::writeDICompileUnit(const DICompileUnit *N,
1926 unsigned Abbrev) {
1927 assert(N->isDistinct() && "Expected distinct compile units");
1928 Record.push_back(/* IsDistinct */ true);
1929 Record.push_back(N->getSourceLanguage());
1930 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1931 Record.push_back(VE.getMetadataOrNullID(N->getRawProducer()));
1932 Record.push_back(N->isOptimized());
1933 Record.push_back(VE.getMetadataOrNullID(N->getRawFlags()));
1934 Record.push_back(N->getRuntimeVersion());
1935 Record.push_back(VE.getMetadataOrNullID(N->getRawSplitDebugFilename()));
1936 Record.push_back(N->getEmissionKind());
1937 Record.push_back(VE.getMetadataOrNullID(N->getEnumTypes().get()));
1938 Record.push_back(VE.getMetadataOrNullID(N->getRetainedTypes().get()));
1939 Record.push_back(/* subprograms */ 0);
1940 Record.push_back(VE.getMetadataOrNullID(N->getGlobalVariables().get()));
1941 Record.push_back(VE.getMetadataOrNullID(N->getImportedEntities().get()));
1942 Record.push_back(N->getDWOId());
1943 Record.push_back(VE.getMetadataOrNullID(N->getMacros().get()));
1944 Record.push_back(N->getSplitDebugInlining());
1945 Record.push_back(N->getDebugInfoForProfiling());
1946 Record.push_back((unsigned)N->getNameTableKind());
1947 Record.push_back(N->getRangesBaseAddress());
1948 Record.push_back(VE.getMetadataOrNullID(N->getRawSysRoot()));
1949 Record.push_back(VE.getMetadataOrNullID(N->getRawSDK()));
1950
1952 Record.clear();
1953}
1954
1955void ModuleBitcodeWriter::writeDISubprogram(const DISubprogram *N,
1957 unsigned Abbrev) {
1958 const uint64_t HasUnitFlag = 1 << 1;
1959 const uint64_t HasSPFlagsFlag = 1 << 2;
1960 Record.push_back(uint64_t(N->isDistinct()) | HasUnitFlag | HasSPFlagsFlag);
1961 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1962 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1963 Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
1964 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1965 Record.push_back(N->getLine());
1966 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1967 Record.push_back(N->getScopeLine());
1968 Record.push_back(VE.getMetadataOrNullID(N->getContainingType()));
1969 Record.push_back(N->getSPFlags());
1970 Record.push_back(N->getVirtualIndex());
1971 Record.push_back(N->getFlags());
1972 Record.push_back(VE.getMetadataOrNullID(N->getRawUnit()));
1973 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
1974 Record.push_back(VE.getMetadataOrNullID(N->getDeclaration()));
1975 Record.push_back(VE.getMetadataOrNullID(N->getRetainedNodes().get()));
1976 Record.push_back(N->getThisAdjustment());
1977 Record.push_back(VE.getMetadataOrNullID(N->getThrownTypes().get()));
1978 Record.push_back(VE.getMetadataOrNullID(N->getAnnotations().get()));
1979 Record.push_back(VE.getMetadataOrNullID(N->getRawTargetFuncName()));
1980
1982 Record.clear();
1983}
1984
1985void ModuleBitcodeWriter::writeDILexicalBlock(const DILexicalBlock *N,
1987 unsigned Abbrev) {
1988 Record.push_back(N->isDistinct());
1989 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1990 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1991 Record.push_back(N->getLine());
1992 Record.push_back(N->getColumn());
1993
1995 Record.clear();
1996}
1997
1998void ModuleBitcodeWriter::writeDILexicalBlockFile(
2000 unsigned Abbrev) {
2001 Record.push_back(N->isDistinct());
2002 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
2003 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2004 Record.push_back(N->getDiscriminator());
2005
2007 Record.clear();
2008}
2009
2010void ModuleBitcodeWriter::writeDICommonBlock(const DICommonBlock *N,
2012 unsigned Abbrev) {
2013 Record.push_back(N->isDistinct());
2014 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
2015 Record.push_back(VE.getMetadataOrNullID(N->getDecl()));
2016 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2017 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2018 Record.push_back(N->getLineNo());
2019
2021 Record.clear();
2022}
2023
2024void ModuleBitcodeWriter::writeDINamespace(const DINamespace *N,
2026 unsigned Abbrev) {
2027 Record.push_back(N->isDistinct() | N->getExportSymbols() << 1);
2028 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
2029 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2030
2032 Record.clear();
2033}
2034
2035void ModuleBitcodeWriter::writeDIMacro(const DIMacro *N,
2037 unsigned Abbrev) {
2038 Record.push_back(N->isDistinct());
2039 Record.push_back(N->getMacinfoType());
2040 Record.push_back(N->getLine());
2041 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2042 Record.push_back(VE.getMetadataOrNullID(N->getRawValue()));
2043
2044 Stream.EmitRecord(bitc::METADATA_MACRO, Record, Abbrev);
2045 Record.clear();
2046}
2047
2048void ModuleBitcodeWriter::writeDIMacroFile(const DIMacroFile *N,
2050 unsigned Abbrev) {
2051 Record.push_back(N->isDistinct());
2052 Record.push_back(N->getMacinfoType());
2053 Record.push_back(N->getLine());
2054 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2055 Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
2056
2058 Record.clear();
2059}
2060
2061void ModuleBitcodeWriter::writeDIArgList(const DIArgList *N,
2063 Record.reserve(N->getArgs().size());
2064 for (ValueAsMetadata *MD : N->getArgs())
2065 Record.push_back(VE.getMetadataID(MD));
2066
2068 Record.clear();
2069}
2070
2071void ModuleBitcodeWriter::writeDIModule(const DIModule *N,
2073 unsigned Abbrev) {
2074 Record.push_back(N->isDistinct());
2075 for (auto &I : N->operands())
2076 Record.push_back(VE.getMetadataOrNullID(I));
2077 Record.push_back(N->getLineNo());
2078 Record.push_back(N->getIsDecl());
2079
2080 Stream.EmitRecord(bitc::METADATA_MODULE, Record, Abbrev);
2081 Record.clear();
2082}
2083
2084void ModuleBitcodeWriter::writeDIAssignID(const DIAssignID *N,
2086 unsigned Abbrev) {
2087 // There are no arguments for this metadata type.
2088 Record.push_back(N->isDistinct());
2090 Record.clear();
2091}
2092
2093void ModuleBitcodeWriter::writeDITemplateTypeParameter(
2095 unsigned Abbrev) {
2096 Record.push_back(N->isDistinct());
2097 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2098 Record.push_back(VE.getMetadataOrNullID(N->getType()));
2099 Record.push_back(N->isDefault());
2100
2102 Record.clear();
2103}
2104
2105void ModuleBitcodeWriter::writeDITemplateValueParameter(
2107 unsigned Abbrev) {
2108 Record.push_back(N->isDistinct());
2109 Record.push_back(N->getTag());
2110 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2111 Record.push_back(VE.getMetadataOrNullID(N->getType()));
2112 Record.push_back(N->isDefault());
2113 Record.push_back(VE.getMetadataOrNullID(N->getValue()));
2114
2116 Record.clear();
2117}
2118
2119void ModuleBitcodeWriter::writeDIGlobalVariable(
2121 unsigned Abbrev) {
2122 const uint64_t Version = 2 << 1;
2123 Record.push_back((uint64_t)N->isDistinct() | Version);
2124 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
2125 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2126 Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
2127 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2128 Record.push_back(N->getLine());
2129 Record.push_back(VE.getMetadataOrNullID(N->getType()));
2130 Record.push_back(N->isLocalToUnit());
2131 Record.push_back(N->isDefinition());
2132 Record.push_back(VE.getMetadataOrNullID(N->getStaticDataMemberDeclaration()));
2133 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams()));
2134 Record.push_back(N->getAlignInBits());
2135 Record.push_back(VE.getMetadataOrNullID(N->getAnnotations().get()));
2136
2138 Record.clear();
2139}
2140
2141void ModuleBitcodeWriter::writeDILocalVariable(
2143 unsigned Abbrev) {
2144 // In order to support all possible bitcode formats in BitcodeReader we need
2145 // to distinguish the following cases:
2146 // 1) Record has no artificial tag (Record[1]),
2147 // has no obsolete inlinedAt field (Record[9]).
2148 // In this case Record size will be 8, HasAlignment flag is false.
2149 // 2) Record has artificial tag (Record[1]),
2150 // has no obsolete inlignedAt field (Record[9]).
2151 // In this case Record size will be 9, HasAlignment flag is false.
2152 // 3) Record has both artificial tag (Record[1]) and
2153 // obsolete inlignedAt field (Record[9]).
2154 // In this case Record size will be 10, HasAlignment flag is false.
2155 // 4) Record has neither artificial tag, nor inlignedAt field, but
2156 // HasAlignment flag is true and Record[8] contains alignment value.
2157 const uint64_t HasAlignmentFlag = 1 << 1;
2158 Record.push_back((uint64_t)N->isDistinct() | HasAlignmentFlag);
2159 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
2160 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2161 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2162 Record.push_back(N->getLine());
2163 Record.push_back(VE.getMetadataOrNullID(N->getType()));
2164 Record.push_back(N->getArg());
2165 Record.push_back(N->getFlags());
2166 Record.push_back(N->getAlignInBits());
2167 Record.push_back(VE.getMetadataOrNullID(N->getAnnotations().get()));
2168
2170 Record.clear();
2171}
2172
2173void ModuleBitcodeWriter::writeDILabel(
2175 unsigned Abbrev) {
2176 Record.push_back((uint64_t)N->isDistinct());
2177 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
2178 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2179 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2180 Record.push_back(N->getLine());
2181
2182 Stream.EmitRecord(bitc::METADATA_LABEL, Record, Abbrev);
2183 Record.clear();
2184}
2185
2186void ModuleBitcodeWriter::writeDIExpression(const DIExpression *N,
2188 unsigned Abbrev) {
2189 Record.reserve(N->getElements().size() + 1);
2190 const uint64_t Version = 3 << 1;
2191 Record.push_back((uint64_t)N->isDistinct() | Version);
2192 Record.append(N->elements_begin(), N->elements_end());
2193
2195 Record.clear();
2196}
2197
2198void ModuleBitcodeWriter::writeDIGlobalVariableExpression(
2200 unsigned Abbrev) {
2201 Record.push_back(N->isDistinct());
2202 Record.push_back(VE.getMetadataOrNullID(N->getVariable()));
2203 Record.push_back(VE.getMetadataOrNullID(N->getExpression()));
2204
2206 Record.clear();
2207}
2208
2209void ModuleBitcodeWriter::writeDIObjCProperty(const DIObjCProperty *N,
2211 unsigned Abbrev) {
2212 Record.push_back(N->isDistinct());
2213 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2214 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2215 Record.push_back(N->getLine());
2216 Record.push_back(VE.getMetadataOrNullID(N->getRawSetterName()));
2217 Record.push_back(VE.getMetadataOrNullID(N->getRawGetterName()));
2218 Record.push_back(N->getAttributes());
2219 Record.push_back(VE.getMetadataOrNullID(N->getType()));
2220
2222 Record.clear();
2223}
2224
2225void ModuleBitcodeWriter::writeDIImportedEntity(
2227 unsigned Abbrev) {
2228 Record.push_back(N->isDistinct());
2229 Record.push_back(N->getTag());
2230 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
2231 Record.push_back(VE.getMetadataOrNullID(N->getEntity()));
2232 Record.push_back(N->getLine());
2233 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2234 Record.push_back(VE.getMetadataOrNullID(N->getRawFile()));
2235 Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
2236
2238 Record.clear();
2239}
2240
2241unsigned ModuleBitcodeWriter::createNamedMetadataAbbrev() {
2242 auto Abbv = std::make_shared<BitCodeAbbrev>();
2246 return Stream.EmitAbbrev(std::move(Abbv));
2247}
2248
2249void ModuleBitcodeWriter::writeNamedMetadata(
2251 if (M.named_metadata_empty())
2252 return;
2253
2254 unsigned Abbrev = createNamedMetadataAbbrev();
2255 for (const NamedMDNode &NMD : M.named_metadata()) {
2256 // Write name.
2257 StringRef Str = NMD.getName();
2258 Record.append(Str.bytes_begin(), Str.bytes_end());
2259 Stream.EmitRecord(bitc::METADATA_NAME, Record, Abbrev);
2260 Record.clear();
2261
2262 // Write named metadata operands.
2263 for (const MDNode *N : NMD.operands())
2264 Record.push_back(VE.getMetadataID(N));
2266 Record.clear();
2267 }
2268}
2269
2270unsigned ModuleBitcodeWriter::createMetadataStringsAbbrev() {
2271 auto Abbv = std::make_shared<BitCodeAbbrev>();
2273 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of strings
2274 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // offset to chars
2276 return Stream.EmitAbbrev(std::move(Abbv));
2277}
2278
2279/// Write out a record for MDString.
2280///
2281/// All the metadata strings in a metadata block are emitted in a single
2282/// record. The sizes and strings themselves are shoved into a blob.
2283void ModuleBitcodeWriter::writeMetadataStrings(
2285 if (Strings.empty())
2286 return;
2287
2288 // Start the record with the number of strings.
2289 Record.push_back(bitc::METADATA_STRINGS);
2290 Record.push_back(Strings.size());
2291
2292 // Emit the sizes of the strings in the blob.
2293 SmallString<256> Blob;
2294 {
2295 BitstreamWriter W(Blob);
2296 for (const Metadata *MD : Strings)
2297 W.EmitVBR(cast<MDString>(MD)->getLength(), 6);
2298 W.FlushToWord();
2299 }
2300
2301 // Add the offset to the strings to the record.
2302 Record.push_back(Blob.size());
2303
2304 // Add the strings to the blob.
2305 for (const Metadata *MD : Strings)
2306 Blob.append(cast<MDString>(MD)->getString());
2307
2308 // Emit the final record.
2309 Stream.EmitRecordWithBlob(createMetadataStringsAbbrev(), Record, Blob);
2310 Record.clear();
2311}
2312
2313// Generates an enum to use as an index in the Abbrev array of Metadata record.
2314enum MetadataAbbrev : unsigned {
2315#define HANDLE_MDNODE_LEAF(CLASS) CLASS##AbbrevID,
2316#include "llvm/IR/Metadata.def"
2319
2320void ModuleBitcodeWriter::writeMetadataRecords(
2322 std::vector<unsigned> *MDAbbrevs, std::vector<uint64_t> *IndexPos) {
2323 if (MDs.empty())
2324 return;
2325
2326 // Initialize MDNode abbreviations.
2327#define HANDLE_MDNODE_LEAF(CLASS) unsigned CLASS##Abbrev = 0;
2328#include "llvm/IR/Metadata.def"
2329
2330 for (const Metadata *MD : MDs) {
2331 if (IndexPos)
2332 IndexPos->push_back(Stream.GetCurrentBitNo());
2333 if (const MDNode *N = dyn_cast<MDNode>(MD)) {
2334 assert(N->isResolved() && "Expected forward references to be resolved");
2335
2336 switch (N->getMetadataID()) {
2337 default:
2338 llvm_unreachable("Invalid MDNode subclass");
2339#define HANDLE_MDNODE_LEAF(CLASS) \
2340 case Metadata::CLASS##Kind: \
2341 if (MDAbbrevs) \
2342 write##CLASS(cast<CLASS>(N), Record, \
2343 (*MDAbbrevs)[MetadataAbbrev::CLASS##AbbrevID]); \
2344 else \
2345 write##CLASS(cast<CLASS>(N), Record, CLASS##Abbrev); \
2346 continue;
2347#include "llvm/IR/Metadata.def"
2348 }
2349 }
2350 if (auto *AL = dyn_cast<DIArgList>(MD)) {
2352 continue;
2353 }
2354 writeValueAsMetadata(cast<ValueAsMetadata>(MD), Record);
2355 }
2356}
2357
2358void ModuleBitcodeWriter::writeModuleMetadata() {
2359 if (!VE.hasMDs() && M.named_metadata_empty())
2360 return;
2361
2364
2365 // Emit all abbrevs upfront, so that the reader can jump in the middle of the
2366 // block and load any metadata.
2367 std::vector<unsigned> MDAbbrevs;
2368
2369 MDAbbrevs.resize(MetadataAbbrev::LastPlusOne);
2370 MDAbbrevs[MetadataAbbrev::DILocationAbbrevID] = createDILocationAbbrev();
2371 MDAbbrevs[MetadataAbbrev::GenericDINodeAbbrevID] =
2372 createGenericDINodeAbbrev();
2373
2374 auto Abbv = std::make_shared<BitCodeAbbrev>();
2378 unsigned OffsetAbbrev = Stream.EmitAbbrev(std::move(Abbv));
2379
2380 Abbv = std::make_shared<BitCodeAbbrev>();
2384 unsigned IndexAbbrev = Stream.EmitAbbrev(std::move(Abbv));
2385
2386 // Emit MDStrings together upfront.
2387 writeMetadataStrings(VE.getMDStrings(), Record);
2388
2389 // We only emit an index for the metadata record if we have more than a given
2390 // (naive) threshold of metadatas, otherwise it is not worth it.
2391 if (VE.getNonMDStrings().size() > IndexThreshold) {
2392 // Write a placeholder value in for the offset of the metadata index,
2393 // which is written after the records, so that it can include
2394 // the offset of each entry. The placeholder offset will be
2395 // updated after all records are emitted.
2396 uint64_t Vals[] = {0, 0};
2397 Stream.EmitRecord(bitc::METADATA_INDEX_OFFSET, Vals, OffsetAbbrev);
2398 }
2399
2400 // Compute and save the bit offset to the current position, which will be
2401 // patched when we emit the index later. We can simply subtract the 64-bit
2402 // fixed size from the current bit number to get the location to backpatch.
2403 uint64_t IndexOffsetRecordBitPos = Stream.GetCurrentBitNo();
2404
2405 // This index will contain the bitpos for each individual record.
2406 std::vector<uint64_t> IndexPos;
2407 IndexPos.reserve(VE.getNonMDStrings().size());
2408
2409 // Write all the records
2410 writeMetadataRecords(VE.getNonMDStrings(), Record, &MDAbbrevs, &IndexPos);
2411
2412 if (VE.getNonMDStrings().size() > IndexThreshold) {
2413 // Now that we have emitted all the records we will emit the index. But
2414 // first
2415 // backpatch the forward reference so that the reader can skip the records
2416 // efficiently.
2417 Stream.BackpatchWord64(IndexOffsetRecordBitPos - 64,
2418 Stream.GetCurrentBitNo() - IndexOffsetRecordBitPos);
2419
2420 // Delta encode the index.
2421 uint64_t PreviousValue = IndexOffsetRecordBitPos;
2422 for (auto &Elt : IndexPos) {
2423 auto EltDelta = Elt - PreviousValue;
2424 PreviousValue = Elt;
2425 Elt = EltDelta;
2426 }
2427 // Emit the index record.
2428 Stream.EmitRecord(bitc::METADATA_INDEX, IndexPos, IndexAbbrev);
2429 IndexPos.clear();
2430 }
2431
2432 // Write the named metadata now.
2433 writeNamedMetadata(Record);
2434
2435 auto AddDeclAttachedMetadata = [&](const GlobalObject &GO) {
2437 Record.push_back(VE.getValueID(&GO));
2438 pushGlobalMetadataAttachment(Record, GO);
2440 };
2441 for (const Function &F : M)
2442 if (F.isDeclaration() && F.hasMetadata())
2443 AddDeclAttachedMetadata(F);
2444 // FIXME: Only store metadata for declarations here, and move data for global
2445 // variable definitions to a separate block (PR28134).
2446 for (const GlobalVariable &GV : M.globals())
2447 if (GV.hasMetadata())
2448 AddDeclAttachedMetadata(GV);
2449
2450 Stream.ExitBlock();
2451}
2452
2453void ModuleBitcodeWriter::writeFunctionMetadata(const Function &F) {
2454 if (!VE.hasMDs())
2455 return;
2456
2459 writeMetadataStrings(VE.getMDStrings(), Record);
2460 writeMetadataRecords(VE.getNonMDStrings(), Record);
2461 Stream.ExitBlock();
2462}
2463
2464void ModuleBitcodeWriter::pushGlobalMetadataAttachment(
2466 // [n x [id, mdnode]]
2468 GO.getAllMetadata(MDs);
2469 for (const auto &I : MDs) {
2470 Record.push_back(I.first);
2471 Record.push_back(VE.getMetadataID(I.second));
2472 }
2473}
2474
2475void ModuleBitcodeWriter::writeFunctionMetadataAttachment(const Function &F) {
2477
2479
2480 if (F.hasMetadata()) {
2481 pushGlobalMetadataAttachment(Record, F);
2483 Record.clear();
2484 }
2485
2486 // Write metadata attachments
2487 // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]]
2489 for (const BasicBlock &BB : F)
2490 for (const Instruction &I : BB) {
2491 MDs.clear();
2492 I.getAllMetadataOtherThanDebugLoc(MDs);
2493
2494 // If no metadata, ignore instruction.
2495 if (MDs.empty()) continue;
2496
2497 Record.push_back(VE.getInstructionID(&I));
2498
2499 for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
2500 Record.push_back(MDs[i].first);
2501 Record.push_back(VE.getMetadataID(MDs[i].second));
2502 }
2504 Record.clear();
2505 }
2506
2507 Stream.ExitBlock();
2508}
2509
2510void ModuleBitcodeWriter::writeModuleMetadataKinds() {
2512
2513 // Write metadata kinds
2514 // METADATA_KIND - [n x [id, name]]
2516 M.getMDKindNames(Names);
2517
2518 if (Names.empty()) return;
2519
2521
2522 for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) {
2523 Record.push_back(MDKindID);
2524 StringRef KName = Names[MDKindID];
2525 Record.append(KName.begin(), KName.end());
2526
2528 Record.clear();
2529 }
2530
2531 Stream.ExitBlock();
2532}
2533
2534void ModuleBitcodeWriter::writeOperandBundleTags() {
2535 // Write metadata kinds
2536 //
2537 // OPERAND_BUNDLE_TAGS_BLOCK_ID : N x OPERAND_BUNDLE_TAG
2538 //
2539 // OPERAND_BUNDLE_TAG - [strchr x N]
2540
2542 M.getOperandBundleTags(Tags);
2543
2544 if (Tags.empty())
2545 return;
2546
2548
2550
2551 for (auto Tag : Tags) {
2552 Record.append(Tag.begin(), Tag.end());
2553
2555 Record.clear();
2556 }
2557
2558 Stream.ExitBlock();
2559}
2560
2561void ModuleBitcodeWriter::writeSyncScopeNames() {
2563 M.getContext().getSyncScopeNames(SSNs);
2564 if (SSNs.empty())
2565 return;
2566
2568
2570 for (auto SSN : SSNs) {
2571 Record.append(SSN.begin(), SSN.end());
2573 Record.clear();
2574 }
2575
2576 Stream.ExitBlock();
2577}
2578
2579void ModuleBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal,
2580 bool isGlobal) {
2581 if (FirstVal == LastVal) return;
2582
2584
2585 unsigned AggregateAbbrev = 0;
2586 unsigned String8Abbrev = 0;
2587 unsigned CString7Abbrev = 0;
2588 unsigned CString6Abbrev = 0;
2589 // If this is a constant pool for the module, emit module-specific abbrevs.
2590 if (isGlobal) {
2591 // Abbrev for CST_CODE_AGGREGATE.
2592 auto Abbv = std::make_shared<BitCodeAbbrev>();
2595 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
2596 AggregateAbbrev = Stream.EmitAbbrev(std::move(Abbv));
2597
2598 // Abbrev for CST_CODE_STRING.
2599 Abbv = std::make_shared<BitCodeAbbrev>();
2603 String8Abbrev = Stream.EmitAbbrev(std::move(Abbv));
2604 // Abbrev for CST_CODE_CSTRING.
2605 Abbv = std::make_shared<BitCodeAbbrev>();
2609 CString7Abbrev = Stream.EmitAbbrev(std::move(Abbv));
2610 // Abbrev for CST_CODE_CSTRING.
2611 Abbv = std::make_shared<BitCodeAbbrev>();
2615 CString6Abbrev = Stream.EmitAbbrev(std::move(Abbv));
2616 }
2617
2619
2620 const ValueEnumerator::ValueList &Vals = VE.getValues();
2621 Type *LastTy = nullptr;
2622 for (unsigned i = FirstVal; i != LastVal; ++i) {
2623 const Value *V = Vals[i].first;
2624 // If we need to switch types, do so now.
2625 if (V->getType() != LastTy) {
2626 LastTy = V->getType();
2627 Record.push_back(VE.getTypeID(LastTy));
2629 CONSTANTS_SETTYPE_ABBREV);
2630 Record.clear();
2631 }
2632
2633 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
2634 Record.push_back(VE.getTypeID(IA->getFunctionType()));
2635 Record.push_back(
2636 unsigned(IA->hasSideEffects()) | unsigned(IA->isAlignStack()) << 1 |
2637 unsigned(IA->getDialect() & 1) << 2 | unsigned(IA->canThrow()) << 3);
2638
2639 // Add the asm string.
2640 const std::string &AsmStr = IA->getAsmString();
2641 Record.push_back(AsmStr.size());
2642 Record.append(AsmStr.begin(), AsmStr.end());
2643
2644 // Add the constraint string.
2645 const std::string &ConstraintStr = IA->getConstraintString();
2646 Record.push_back(ConstraintStr.size());
2647 Record.append(ConstraintStr.begin(), ConstraintStr.end());
2649 Record.clear();
2650 continue;
2651 }
2652 const Constant *C = cast<Constant>(V);
2653 unsigned Code = -1U;
2654 unsigned AbbrevToUse = 0;
2655 if (C->isNullValue()) {
2657 } else if (isa<PoisonValue>(C)) {
2659 } else if (isa<UndefValue>(C)) {
2661 } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
2662 if (IV->getBitWidth() <= 64) {
2663 uint64_t V = IV->getSExtValue();
2666 AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
2667 } else { // Wide integers, > 64 bits in size.
2668 emitWideAPInt(Record, IV->getValue());
2670 }
2671 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
2673 Type *Ty = CFP->getType()->getScalarType();
2674 if (Ty->isHalfTy() || Ty->isBFloatTy() || Ty->isFloatTy() ||
2675 Ty->isDoubleTy()) {
2676 Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
2677 } else if (Ty->isX86_FP80Ty()) {
2678 // api needed to prevent premature destruction
2679 // bits are not in the same order as a normal i80 APInt, compensate.
2680 APInt api = CFP->getValueAPF().bitcastToAPInt();
2681 const uint64_t *p = api.getRawData();
2682 Record.push_back((p[1] << 48) | (p[0] >> 16));
2683 Record.push_back(p[0] & 0xffffLL);
2684 } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) {
2685 APInt api = CFP->getValueAPF().bitcastToAPInt();
2686 const uint64_t *p = api.getRawData();
2687 Record.push_back(p[0]);
2688 Record.push_back(p[1]);
2689 } else {
2690 assert(0 && "Unknown FP type!");
2691 }
2692 } else if (isa<ConstantDataSequential>(C) &&
2693 cast<ConstantDataSequential>(C)->isString()) {
2694 const ConstantDataSequential *Str = cast<ConstantDataSequential>(C);
2695 // Emit constant strings specially.
2696 unsigned NumElts = Str->getNumElements();
2697 // If this is a null-terminated string, use the denser CSTRING encoding.
2698 if (Str->isCString()) {
2700 --NumElts; // Don't encode the null, which isn't allowed by char6.
2701 } else {
2703 AbbrevToUse = String8Abbrev;
2704 }
2705 bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
2706 bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
2707 for (unsigned i = 0; i != NumElts; ++i) {
2708 unsigned char V = Str->getElementAsInteger(i);
2709 Record.push_back(V);
2710 isCStr7 &= (V & 128) == 0;
2711 if (isCStrChar6)
2712 isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
2713 }
2714
2715 if (isCStrChar6)
2716 AbbrevToUse = CString6Abbrev;
2717 else if (isCStr7)
2718 AbbrevToUse = CString7Abbrev;
2719 } else if (const ConstantDataSequential *CDS =
2720 dyn_cast<ConstantDataSequential>(C)) {
2722 Type *EltTy = CDS->getElementType();
2723 if (isa<IntegerType>(EltTy)) {
2724 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
2725 Record.push_back(CDS->getElementAsInteger(i));
2726 } else {
2727 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
2728 Record.push_back(
2729 CDS->getElementAsAPFloat(i).bitcastToAPInt().getLimitedValue());
2730 }
2731 } else if (isa<ConstantAggregate>(C)) {
2733 for (const Value *Op : C->operands())
2734 Record.push_back(VE.getValueID(Op));
2735 AbbrevToUse = AggregateAbbrev;
2736 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
2737 switch (CE->getOpcode()) {
2738 default:
2739 if (Instruction::isCast(CE->getOpcode())) {
2741 Record.push_back(getEncodedCastOpcode(CE->getOpcode()));
2742 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
2743 Record.push_back(VE.getValueID(C->getOperand(0)));
2744 AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
2745 } else {
2746 assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
2748 Record.push_back(getEncodedBinaryOpcode(CE->getOpcode()));
2749 Record.push_back(VE.getValueID(C->getOperand(0)));
2750 Record.push_back(VE.getValueID(C->getOperand(1)));
2752 if (Flags != 0)
2753 Record.push_back(Flags);
2754 }
2755 break;
2756 case Instruction::FNeg: {
2757 assert(CE->getNumOperands() == 1 && "Unknown constant expr!");
2759 Record.push_back(getEncodedUnaryOpcode(CE->getOpcode()));
2760 Record.push_back(VE.getValueID(C->getOperand(0)));
2762 if (Flags != 0)
2763 Record.push_back(Flags);
2764 break;
2765 }
2766 case Instruction::GetElementPtr: {
2768 const auto *GO = cast<GEPOperator>(C);
2769 Record.push_back(VE.getTypeID(GO->getSourceElementType()));
2770 if (std::optional<ConstantRange> Range = GO->getInRange()) {
2772 Record.push_back(GO->isInBounds());
2773 emitConstantRange(Record, *Range);
2774 } else if (GO->isInBounds())
2776 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
2777 Record.push_back(VE.getTypeID(C->getOperand(i)->getType()));
2778 Record.push_back(VE.getValueID(C->getOperand(i)));
2779 }
2780 break;
2781 }
2782 case Instruction::ExtractElement:
2784 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
2785 Record.push_back(VE.getValueID(C->getOperand(0)));
2786 Record.push_back(VE.getTypeID(C->getOperand(1)->getType()));
2787 Record.push_back(VE.getValueID(C->getOperand(1)));
2788 break;
2789 case Instruction::InsertElement:
2791 Record.push_back(VE.getValueID(C->getOperand(0)));
2792 Record.push_back(VE.getValueID(C->getOperand(1)));
2793 Record.push_back(VE.getTypeID(C->getOperand(2)->getType()));
2794 Record.push_back(VE.getValueID(C->getOperand(2)));
2795 break;
2796 case Instruction::ShuffleVector:
2797 // If the return type and argument types are the same, this is a
2798 // standard shufflevector instruction. If the types are different,
2799 // then the shuffle is widening or truncating the input vectors, and
2800 // the argument type must also be encoded.
2801 if (C->getType() == C->getOperand(0)->getType()) {
2803 } else {
2805 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
2806 }
2807 Record.push_back(VE.getValueID(C->getOperand(0)));
2808 Record.push_back(VE.getValueID(C->getOperand(1)));
2809 Record.push_back(VE.getValueID(CE->getShuffleMaskForBitcode()));
2810 break;
2811 case Instruction::ICmp:
2812 case Instruction::FCmp:
2814 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
2815 Record.push_back(VE.getValueID(C->getOperand(0)));
2816 Record.push_back(VE.getValueID(C->getOperand(1)));
2817 Record.push_back(CE->getPredicate());
2818 break;
2819 }
2820 } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
2822 Record.push_back(VE.getTypeID(BA->getFunction()->getType()));
2823 Record.push_back(VE.getValueID(BA->getFunction()));
2824 Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock()));
2825 } else if (const auto *Equiv = dyn_cast<DSOLocalEquivalent>(C)) {
2827 Record.push_back(VE.getTypeID(Equiv->getGlobalValue()->getType()));
2828 Record.push_back(VE.getValueID(Equiv->getGlobalValue()));
2829 } else if (const auto *NC = dyn_cast<NoCFIValue>(C)) {
2831 Record.push_back(VE.getTypeID(NC->getGlobalValue()->getType()));
2832 Record.push_back(VE.getValueID(NC->getGlobalValue()));
2833 } else {
2834#ifndef NDEBUG
2835 C->dump();
2836#endif
2837 llvm_unreachable("Unknown constant!");
2838 }
2839 Stream.EmitRecord(Code, Record, AbbrevToUse);
2840 Record.clear();
2841 }
2842
2843 Stream.ExitBlock();
2844}
2845
2846void ModuleBitcodeWriter::writeModuleConstants() {
2847 const ValueEnumerator::ValueList &Vals = VE.getValues();
2848
2849 // Find the first constant to emit, which is the first non-globalvalue value.
2850 // We know globalvalues have been emitted by WriteModuleInfo.
2851 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
2852 if (!isa<GlobalValue>(Vals[i].first)) {
2853 writeConstants(i, Vals.size(), true);
2854 return;
2855 }
2856 }
2857}
2858
2859/// pushValueAndType - The file has to encode both the value and type id for
2860/// many values, because we need to know what type to create for forward
2861/// references. However, most operands are not forward references, so this type
2862/// field is not needed.
2863///
2864/// This function adds V's value ID to Vals. If the value ID is higher than the
2865/// instruction ID, then it is a forward reference, and it also includes the
2866/// type ID. The value ID that is written is encoded relative to the InstID.
2867bool ModuleBitcodeWriter::pushValueAndType(const Value *V, unsigned InstID,
2869 unsigned ValID = VE.getValueID(V);
2870 // Make encoding relative to the InstID.
2871 Vals.push_back(InstID - ValID);
2872 if (ValID >= InstID) {
2873 Vals.push_back(VE.getTypeID(V->getType()));
2874 return true;
2875 }
2876 return false;
2877}
2878
2879void ModuleBitcodeWriter::writeOperandBundles(const CallBase &CS,
2880 unsigned InstID) {
2882 LLVMContext &C = CS.getContext();
2883
2884 for (unsigned i = 0, e = CS.getNumOperandBundles(); i != e; ++i) {
2885 const auto &Bundle = CS.getOperandBundleAt(i);
2886 Record.push_back(C.getOperandBundleTagID(Bundle.getTagName()));
2887
2888 for (auto &Input : Bundle.Inputs)
2889 pushValueAndType(Input, InstID, Record);
2890
2892 Record.clear();
2893 }
2894}
2895
2896/// pushValue - Like pushValueAndType, but where the type of the value is
2897/// omitted (perhaps it was already encoded in an earlier operand).
2898void ModuleBitcodeWriter::pushValue(const Value *V, unsigned InstID,
2900 unsigned ValID = VE.getValueID(V);
2901 Vals.push_back(InstID - ValID);
2902}
2903
2904void ModuleBitcodeWriter::pushValueSigned(const Value *V, unsigned InstID,
2906 unsigned ValID = VE.getValueID(V);
2907 int64_t diff = ((int32_t)InstID - (int32_t)ValID);
2908 emitSignedInt64(Vals, diff);
2909}
2910
2911/// WriteInstruction - Emit an instruction to the specified stream.
2912void ModuleBitcodeWriter::writeInstruction(const Instruction &I,
2913 unsigned InstID,
2915 unsigned Code = 0;
2916 unsigned AbbrevToUse = 0;
2917 VE.setInstructionID(&I);
2918 switch (I.getOpcode()) {
2919 default:
2920 if (Instruction::isCast(I.getOpcode())) {
2922 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
2923 AbbrevToUse = FUNCTION_INST_CAST_ABBREV;
2924 Vals.push_back(VE.getTypeID(I.getType()));
2925 Vals.push_back(getEncodedCastOpcode(I.getOpcode()));
2927 if (Flags != 0) {
2928 if (AbbrevToUse == FUNCTION_INST_CAST_ABBREV)
2929 AbbrevToUse = FUNCTION_INST_CAST_FLAGS_ABBREV;
2930 Vals.push_back(Flags);
2931 }
2932 } else {
2933 assert(isa<BinaryOperator>(I) && "Unknown instruction!");
2935 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
2936 AbbrevToUse = FUNCTION_INST_BINOP_ABBREV;
2937 pushValue(I.getOperand(1), InstID, Vals);
2938 Vals.push_back(getEncodedBinaryOpcode(I.getOpcode()));
2940 if (Flags != 0) {
2941 if (AbbrevToUse == FUNCTION_INST_BINOP_ABBREV)
2942 AbbrevToUse = FUNCTION_INST_BINOP_FLAGS_ABBREV;
2943 Vals.push_back(Flags);
2944 }
2945 }
2946 break;
2947 case Instruction::FNeg: {
2949 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
2950 AbbrevToUse = FUNCTION_INST_UNOP_ABBREV;
2951 Vals.push_back(getEncodedUnaryOpcode(I.getOpcode()));
2953 if (Flags != 0) {
2954 if (AbbrevToUse == FUNCTION_INST_UNOP_ABBREV)
2955 AbbrevToUse = FUNCTION_INST_UNOP_FLAGS_ABBREV;
2956 Vals.push_back(Flags);
2957 }
2958 break;
2959 }
2960 case Instruction::GetElementPtr: {
2962 AbbrevToUse = FUNCTION_INST_GEP_ABBREV;
2963 auto &GEPInst = cast<GetElementPtrInst>(I);
2964 Vals.push_back(GEPInst.isInBounds());
2965 Vals.push_back(VE.getTypeID(GEPInst.getSourceElementType()));
2966 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
2967 pushValueAndType(I.getOperand(i), InstID, Vals);
2968 break;
2969 }
2970 case Instruction::ExtractValue: {
2972 pushValueAndType(I.getOperand(0), InstID, Vals);
2973 const ExtractValueInst *EVI = cast<ExtractValueInst>(&I);
2974 Vals.append(EVI->idx_begin(), EVI->idx_end());
2975 break;
2976 }
2977 case Instruction::InsertValue: {
2979 pushValueAndType(I.getOperand(0), InstID, Vals);
2980 pushValueAndType(I.getOperand(1), InstID, Vals);
2981 const InsertValueInst *IVI = cast<InsertValueInst>(&I);
2982 Vals.append(IVI->idx_begin(), IVI->idx_end());
2983 break;
2984 }
2985 case Instruction::Select: {
2987 pushValueAndType(I.getOperand(1), InstID, Vals);
2988 pushValue(I.getOperand(2), InstID, Vals);
2989 pushValueAndType(I.getOperand(0), InstID, Vals);
2991 if (Flags != 0)
2992 Vals.push_back(Flags);
2993 break;
2994 }
2995 case Instruction::ExtractElement:
2997 pushValueAndType(I.getOperand(0), InstID, Vals);
2998 pushValueAndType(I.getOperand(1), InstID, Vals);
2999 break;
3000 case Instruction::InsertElement:
3002 pushValueAndType(I.getOperand(0), InstID, Vals);
3003 pushValue(I.getOperand(1), InstID, Vals);
3004 pushValueAndType(I.getOperand(2), InstID, Vals);
3005 break;
3006 case Instruction::ShuffleVector:
3008 pushValueAndType(I.getOperand(0), InstID, Vals);
3009 pushValue(I.getOperand(1), InstID, Vals);
3010 pushValue(cast<ShuffleVectorInst>(I).getShuffleMaskForBitcode(), InstID,
3011 Vals);
3012 break;
3013 case Instruction::ICmp:
3014 case Instruction::FCmp: {
3015 // compare returning Int1Ty or vector of Int1Ty
3017 pushValueAndType(I.getOperand(0), InstID, Vals);
3018 pushValue(I.getOperand(1), InstID, Vals);
3019 Vals.push_back(cast<CmpInst>(I).getPredicate());
3021 if (Flags != 0)
3022 Vals.push_back(Flags);
3023 break;
3024 }
3025
3026 case Instruction::Ret:
3027 {
3029 unsigned NumOperands = I.getNumOperands();
3030 if (NumOperands == 0)
3031 AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV;
3032 else if (NumOperands == 1) {
3033 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
3034 AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV;
3035 } else {
3036 for (unsigned i = 0, e = NumOperands; i != e; ++i)
3037 pushValueAndType(I.getOperand(i), InstID, Vals);
3038 }
3039 }
3040 break;
3041 case Instruction::Br:
3042 {
3044 const BranchInst &II = cast<BranchInst>(I);
3045 Vals.push_back(VE.getValueID(II.getSuccessor(0)));
3046 if (II.isConditional()) {
3047 Vals.push_back(VE.getValueID(II.getSuccessor(1)));
3048 pushValue(II.getCondition(), InstID, Vals);
3049 }
3050 }
3051 break;
3052 case Instruction::Switch:
3053 {
3055 const SwitchInst &SI = cast<SwitchInst>(I);
3056 Vals.push_back(VE.getTypeID(SI.getCondition()->getType()));
3057 pushValue(SI.getCondition(), InstID, Vals);
3058 Vals.push_back(VE.getValueID(SI.getDefaultDest()));
3059 for (auto Case : SI.cases()) {
3060 Vals.push_back(VE.getValueID(Case.getCaseValue()));
3061 Vals.push_back(VE.getValueID(Case.getCaseSuccessor()));
3062 }
3063 }
3064 break;
3065 case Instruction::IndirectBr:
3067 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
3068 // Encode the address operand as relative, but not the basic blocks.
3069 pushValue(I.getOperand(0), InstID, Vals);
3070 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i)
3071 Vals.push_back(VE.getValueID(I.getOperand(i)));
3072 break;
3073
3074 case Instruction::Invoke: {
3075 const InvokeInst *II = cast<InvokeInst>(&I);
3076 const Value *Callee = II->getCalledOperand();
3077 FunctionType *FTy = II->getFunctionType();
3078
3079 if (II->hasOperandBundles())
3080 writeOperandBundles(*II, InstID);
3081
3083
3085 Vals.push_back(II->getCallingConv() | 1 << 13);
3086 Vals.push_back(VE.getValueID(II->getNormalDest()));
3087 Vals.push_back(VE.getValueID(II->getUnwindDest()));
3088 Vals.push_back(VE.getTypeID(FTy));
3089 pushValueAndType(Callee, InstID, Vals);
3090
3091 // Emit value #'s for the fixed parameters.
3092 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
3093 pushValue(I.getOperand(i), InstID, Vals); // fixed param.
3094
3095 // Emit type/value pairs for varargs params.
3096 if (FTy->isVarArg()) {
3097 for (unsigned i = FTy->getNumParams(), e = II->arg_size(); i != e; ++i)
3098 pushValueAndType(I.getOperand(i), InstID, Vals); // vararg
3099 }
3100 break;
3101 }
3102 case Instruction::Resume:
3104 pushValueAndType(I.getOperand(0), InstID, Vals);
3105 break;
3106 case Instruction::CleanupRet: {
3108 const auto &CRI = cast<CleanupReturnInst>(I);
3109 pushValue(CRI.getCleanupPad(), InstID, Vals);
3110 if (CRI.hasUnwindDest())
3111 Vals.push_back(VE.getValueID(CRI.getUnwindDest()));
3112 break;
3113 }
3114 case Instruction::CatchRet: {
3116 const auto &CRI = cast<CatchReturnInst>(I);
3117 pushValue(CRI.getCatchPad(), InstID, Vals);
3118 Vals.push_back(VE.getValueID(CRI.getSuccessor()));
3119 break;
3120 }
3121 case Instruction::CleanupPad:
3122 case Instruction::CatchPad: {
3123 const auto &FuncletPad = cast<FuncletPadInst>(I);
3124 Code = isa<CatchPadInst>(FuncletPad) ? bitc::FUNC_CODE_INST_CATCHPAD
3126 pushValue(FuncletPad.getParentPad(), InstID, Vals);
3127
3128 unsigned NumArgOperands = FuncletPad.arg_size();
3129 Vals.push_back(NumArgOperands);
3130 for (unsigned Op = 0; Op != NumArgOperands; ++Op)
3131 pushValueAndType(FuncletPad.getArgOperand(Op), InstID, Vals);
3132 break;
3133 }
3134 case Instruction::CatchSwitch: {
3136 const auto &CatchSwitch = cast<CatchSwitchInst>(I);
3137
3138 pushValue(CatchSwitch.getParentPad(), InstID, Vals);
3139
3140 unsigned NumHandlers = CatchSwitch.getNumHandlers();
3141 Vals.push_back(NumHandlers);
3142 for (const BasicBlock *CatchPadBB : CatchSwitch.handlers())
3143 Vals.push_back(VE.getValueID(CatchPadBB));
3144
3145 if (CatchSwitch.hasUnwindDest())
3146 Vals.push_back(VE.getValueID(CatchSwitch.getUnwindDest()));
3147 break;
3148 }
3149 case Instruction::CallBr: {
3150 const CallBrInst *CBI = cast<CallBrInst>(&I);
3151 const Value *Callee = CBI->getCalledOperand();
3152 FunctionType *FTy = CBI->getFunctionType();
3153
3154 if (CBI->hasOperandBundles())
3155 writeOperandBundles(*CBI, InstID);
3156
3158
3160
3163
3164 Vals.push_back(VE.getValueID(CBI->getDefaultDest()));
3165 Vals.push_back(CBI->getNumIndirectDests());
3166 for (unsigned i = 0, e = CBI->getNumIndirectDests(); i != e; ++i)
3167 Vals.push_back(VE.getValueID(CBI->getIndirectDest(i)));
3168
3169 Vals.push_back(VE.getTypeID(FTy));
3170 pushValueAndType(Callee, InstID, Vals);
3171
3172 // Emit value #'s for the fixed parameters.
3173 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
3174 pushValue(I.getOperand(i), InstID, Vals); // fixed param.
3175
3176 // Emit type/value pairs for varargs params.
3177 if (FTy->isVarArg()) {
3178 for (unsigned i = FTy->getNumParams(), e = CBI->arg_size(); i != e; ++i)
3179 pushValueAndType(I.getOperand(i), InstID, Vals); // vararg
3180 }
3181 break;
3182 }
3183 case Instruction::Unreachable:
3185 AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV;
3186 break;
3187
3188 case Instruction::PHI: {
3189 const PHINode &PN = cast<PHINode>(I);
3191 // With the newer instruction encoding, forward references could give
3192 // negative valued IDs. This is most common for PHIs, so we use
3193 // signed VBRs.
3195 Vals64.push_back(VE.getTypeID(PN.getType()));
3196 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
3197 pushValueSigned(PN.getIncomingValue(i), InstID, Vals64);
3198 Vals64.push_back(VE.getValueID(PN.getIncomingBlock(i)));
3199 }
3200
3202 if (Flags != 0)
3203 Vals64.push_back(Flags);
3204
3205 // Emit a Vals64 vector and exit.
3206 Stream.EmitRecord(Code, Vals64, AbbrevToUse);
3207 Vals64.clear();
3208 return;
3209 }
3210
3211 case Instruction::LandingPad: {
3212 const LandingPadInst &LP = cast<LandingPadInst>(I);
3214 Vals.push_back(VE.getTypeID(LP.getType()));
3215 Vals.push_back(LP.isCleanup());
3216 Vals.push_back(LP.getNumClauses());
3217 for (unsigned I = 0, E = LP.getNumClauses(); I != E; ++I) {
3218 if (LP.isCatch(I))
3220 else
3222 pushValueAndType(LP.getClause(I), InstID, Vals);
3223 }
3224 break;
3225 }
3226
3227 case Instruction::Alloca: {
3229 const AllocaInst &AI = cast<AllocaInst>(I);
3230 Vals.push_back(VE.getTypeID(AI.getAllocatedType()));
3231 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
3232 Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
3233 using APV = AllocaPackedValues;
3234 unsigned Record = 0;
3235 unsigned EncodedAlign = getEncodedAlign(AI.getAlign());
3236 Bitfield::set<APV::AlignLower>(
3237 Record, EncodedAlign & ((1 << APV::AlignLower::Bits) - 1));
3238 Bitfield::set<APV::AlignUpper>(Record,
3239 EncodedAlign >> APV::AlignLower::Bits);
3240 Bitfield::set<APV::UsedWithInAlloca>(Record, AI.isUsedWithInAlloca());
3241 Bitfield::set<APV::ExplicitType>(Record, true);
3242 Bitfield::set<APV::SwiftError>(Record, AI.isSwiftError());
3243 Vals.push_back(Record);
3244
3245 unsigned AS = AI.getAddressSpace();
3246 if (AS != M.getDataLayout().getAllocaAddrSpace())
3247 Vals.push_back(AS);
3248 break;
3249 }
3250
3251 case Instruction::Load:
3252 if (cast<LoadInst>(I).isAtomic()) {
3254 pushValueAndType(I.getOperand(0), InstID, Vals);
3255 } else {
3257 if (!pushValueAndType(I.getOperand(0), InstID, Vals)) // ptr
3258 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV;
3259 }
3260 Vals.push_back(VE.getTypeID(I.getType()));
3261 Vals.push_back(getEncodedAlign(cast<LoadInst>(I).getAlign()));
3262 Vals.push_back(cast<LoadInst>(I).isVolatile());
3263 if (cast<LoadInst>(I).isAtomic()) {
3264 Vals.push_back(getEncodedOrdering(cast<LoadInst>(I).getOrdering()));
3265 Vals.push_back(getEncodedSyncScopeID(cast<LoadInst>(I).getSyncScopeID()));
3266 }
3267 break;
3268 case Instruction::Store:
3269 if (cast<StoreInst>(I).isAtomic())
3271 else
3273 pushValueAndType(I.getOperand(1), InstID, Vals); // ptrty + ptr
3274 pushValueAndType(I.getOperand(0), InstID, Vals); // valty + val
3275 Vals.push_back(getEncodedAlign(cast<StoreInst>(I).getAlign()));
3276 Vals.push_back(cast<StoreInst>(I).isVolatile());
3277 if (cast<StoreInst>(I).isAtomic()) {
3278 Vals.push_back(getEncodedOrdering(cast<StoreInst>(I).getOrdering()));
3279 Vals.push_back(
3280 getEncodedSyncScopeID(cast<StoreInst>(I).getSyncScopeID()));
3281 }
3282 break;
3283 case Instruction::AtomicCmpXchg:
3285 pushValueAndType(I.getOperand(0), InstID, Vals); // ptrty + ptr
3286 pushValueAndType(I.getOperand(1), InstID, Vals); // cmp.
3287 pushValue(I.getOperand(2), InstID, Vals); // newval.
3288 Vals.push_back(cast<AtomicCmpXchgInst>(I).isVolatile());
3289 Vals.push_back(
3290 getEncodedOrdering(cast<AtomicCmpXchgInst>(I).getSuccessOrdering()));
3291 Vals.push_back(
3292 getEncodedSyncScopeID(cast<AtomicCmpXchgInst>(I).getSyncScopeID()));
3293 Vals.push_back(
3294 getEncodedOrdering(cast<AtomicCmpXchgInst>(I).getFailureOrdering()));
3295 Vals.push_back(cast<AtomicCmpXchgInst>(I).isWeak());
3296 Vals.push_back(getEncodedAlign(cast<AtomicCmpXchgInst>(I).getAlign()));
3297 break;
3298 case Instruction::AtomicRMW:
3300 pushValueAndType(I.getOperand(0), InstID, Vals); // ptrty + ptr
3301 pushValueAndType(I.getOperand(1), InstID, Vals); // valty + val
3302 Vals.push_back(
3303 getEncodedRMWOperation(cast<AtomicRMWInst>(I).getOperation()));
3304 Vals.push_back(cast<AtomicRMWInst>(I).isVolatile());
3305 Vals.push_back(getEncodedOrdering(cast<AtomicRMWInst>(I).getOrdering()));
3306 Vals.push_back(
3307 getEncodedSyncScopeID(cast<AtomicRMWInst>(I).getSyncScopeID()));
3308 Vals.push_back(getEncodedAlign(cast<AtomicRMWInst>(I).getAlign()));
3309 break;
3310 case Instruction::Fence:
3312 Vals.push_back(getEncodedOrdering(cast<FenceInst>(I).getOrdering()));
3313 Vals.push_back(getEncodedSyncScopeID(cast<FenceInst>(I).getSyncScopeID()));
3314 break;
3315 case Instruction::Call: {
3316 const CallInst &CI = cast<CallInst>(I);
3317 FunctionType *FTy = CI.getFunctionType();
3318
3319 if (CI.hasOperandBundles())
3320 writeOperandBundles(CI, InstID);
3321
3323
3325
3326 unsigned Flags = getOptimizationFlags(&I);
3328 unsigned(CI.isTailCall()) << bitc::CALL_TAIL |
3329 unsigned(CI.isMustTailCall()) << bitc::CALL_MUSTTAIL |
3331 unsigned(CI.isNoTailCall()) << bitc::CALL_NOTAIL |
3332 unsigned(Flags != 0) << bitc::CALL_FMF);
3333 if (Flags != 0)
3334 Vals.push_back(Flags);
3335
3336 Vals.push_back(VE.getTypeID(FTy));
3337 pushValueAndType(CI.getCalledOperand(), InstID, Vals); // Callee
3338
3339 // Emit value #'s for the fixed parameters.
3340 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
3341 // Check for labels (can happen with asm labels).
3342 if (FTy->getParamType(i)->isLabelTy())
3343 Vals.push_back(VE.getValueID(CI.getArgOperand(i)));
3344 else
3345 pushValue(CI.getArgOperand(i), InstID, Vals); // fixed param.
3346 }
3347
3348 // Emit type/value pairs for varargs params.
3349 if (FTy->isVarArg()) {
3350 for (unsigned i = FTy->getNumParams(), e = CI.arg_size(); i != e; ++i)
3351 pushValueAndType(CI.getArgOperand(i), InstID, Vals); // varargs
3352 }
3353 break;
3354 }
3355 case Instruction::VAArg:
3357 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty
3358 pushValue(I.getOperand(0), InstID, Vals); // valist.
3359 Vals.push_back(VE.getTypeID(I.getType())); // restype.
3360 break;
3361 case Instruction::Freeze:
3363 pushValueAndType(I.getOperand(0), InstID, Vals);
3364 break;
3365 }
3366
3367 Stream.EmitRecord(Code, Vals, AbbrevToUse);
3368 Vals.clear();
3369}
3370
3371/// Write a GlobalValue VST to the module. The purpose of this data structure is
3372/// to allow clients to efficiently find the function body.
3373void ModuleBitcodeWriter::writeGlobalValueSymbolTable(
3374 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex) {
3375 // Get the offset of the VST we are writing, and backpatch it into
3376 // the VST forward declaration record.
3377 uint64_t VSTOffset = Stream.GetCurrentBitNo();
3378 // The BitcodeStartBit was the stream offset of the identification block.
3379 VSTOffset -= bitcodeStartBit();
3380 assert((VSTOffset & 31) == 0 && "VST block not 32-bit aligned");
3381 // Note that we add 1 here because the offset is relative to one word
3382 // before the start of the identification block, which was historically
3383 // always the start of the regular bitcode header.
3384 Stream.BackpatchWord(VSTOffsetPlaceholder, VSTOffset / 32 + 1);
3385
3387
3388 auto Abbv = std::make_shared<BitCodeAbbrev>();
3390 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
3391 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
3392 unsigned FnEntryAbbrev = Stream.EmitAbbrev(std::move(Abbv));
3393
3394 for (const Function &F : M) {
3395 uint64_t Record[2];
3396
3397 if (F.isDeclaration())
3398 continue;
3399
3400 Record[0] = VE.getValueID(&F);
3401
3402 // Save the word offset of the function (from the start of the
3403 // actual bitcode written to the stream).
3404 uint64_t BitcodeIndex = FunctionToBitcodeIndex[&F] - bitcodeStartBit();
3405 assert((BitcodeIndex & 31) == 0 && "function block not 32-bit aligned");
3406 // Note that we add 1 here because the offset is relative to one word
3407 // before the start of the identification block, which was historically
3408 // always the start of the regular bitcode header.
3409 Record[1] = BitcodeIndex / 32 + 1;
3410
3411 Stream.EmitRecord(bitc::VST_CODE_FNENTRY, Record, FnEntryAbbrev);
3412 }
3413
3414 Stream.ExitBlock();
3415}
3416
3417/// Emit names for arguments, instructions and basic blocks in a function.
3418void ModuleBitcodeWriter::writeFunctionLevelValueSymbolTable(
3419 const ValueSymbolTable &VST) {
3420 if (VST.empty())
3421 return;
3422
3424
3425 // FIXME: Set up the abbrev, we know how many values there are!
3426 // FIXME: We know if the type names can use 7-bit ascii.
3428
3429 for (const ValueName &Name : VST) {
3430 // Figure out the encoding to use for the name.
3432
3433 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
3434 NameVals.push_back(VE.getValueID(Name.getValue()));
3435
3436 // VST_CODE_ENTRY: [valueid, namechar x N]
3437 // VST_CODE_BBENTRY: [bbid, namechar x N]
3438 unsigned Code;
3439 if (isa<BasicBlock>(Name.getValue())) {
3441 if (Bits == SE_Char6)
3442 AbbrevToUse = VST_BBENTRY_6_ABBREV;
3443 } else {
3445 if (Bits == SE_Char6)
3446 AbbrevToUse = VST_ENTRY_6_ABBREV;
3447 else if (Bits == SE_Fixed7)
3448 AbbrevToUse = VST_ENTRY_7_ABBREV;
3449 }
3450
3451 for (const auto P : Name.getKey())
3452 NameVals.push_back((unsigned char)P);
3453
3454 // Emit the finished record.
3455 Stream.EmitRecord(Code, NameVals, AbbrevToUse);
3456 NameVals.clear();
3457 }
3458
3459 Stream.ExitBlock();
3460}
3461
3462void ModuleBitcodeWriter::writeUseList(UseListOrder &&Order) {
3463 assert(Order.Shuffle.size() >= 2 && "Shuffle too small");
3464 unsigned Code;
3465 if (isa<BasicBlock>(Order.V))
3467 else
3469
3470 SmallVector<uint64_t, 64> Record(Order.Shuffle.begin(), Order.Shuffle.end());
3471 Record.push_back(VE.getValueID(Order.V));
3472 Stream.EmitRecord(Code, Record);
3473}
3474
3475void ModuleBitcodeWriter::writeUseListBlock(const Function *F) {
3477 "Expected to be preserving use-list order");
3478
3479 auto hasMore = [&]() {
3480 return !VE.UseListOrders.empty() && VE.UseListOrders.back().F == F;
3481 };
3482 if (!hasMore())
3483 // Nothing to do.
3484 return;
3485
3487 while (hasMore()) {
3488 writeUseList(std::move(VE.UseListOrders.back()));
3489 VE.UseListOrders.pop_back();
3490 }
3491 Stream.ExitBlock();
3492}
3493
3494/// Emit a function body to the module stream.
3495void ModuleBitcodeWriter::writeFunction(
3496 const Function &F,
3497 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex) {
3498 // Save the bitcode index of the start of this function block for recording
3499 // in the VST.
3500 FunctionToBitcodeIndex[&F] = Stream.GetCurrentBitNo();
3501
3504
3506
3507 // Emit the number of basic blocks, so the reader can create them ahead of
3508 // time.
3509 Vals.push_back(VE.getBasicBlocks().size());
3511 Vals.clear();
3512
3513 // If there are function-local constants, emit them now.
3514 unsigned CstStart, CstEnd;
3515 VE.getFunctionConstantRange(CstStart, CstEnd);
3516 writeConstants(CstStart, CstEnd, false);
3517
3518 // If there is function-local metadata, emit it now.
3519 writeFunctionMetadata(F);
3520
3521 // Keep a running idea of what the instruction ID is.
3522 unsigned InstID = CstEnd;
3523
3524 bool NeedsMetadataAttachment = F.hasMetadata();
3525
3526 DILocation *LastDL = nullptr;
3527 SmallSetVector<Function *, 4> BlockAddressUsers;
3528
3529 // Finally, emit all the instructions, in order.
3530 for (const BasicBlock &BB : F) {
3531 for (const Instruction &I : BB) {
3532 writeInstruction(I, InstID, Vals);
3533
3534 if (!I.getType()->isVoidTy())
3535 ++InstID;
3536
3537 // If the instruction has metadata, write a metadata attachment later.
3538 NeedsMetadataAttachment |= I.hasMetadataOtherThanDebugLoc();
3539
3540 // If the instruction has a debug location, emit it.
3541 if (DILocation *DL = I.getDebugLoc()) {
3542 if (DL == LastDL) {
3543 // Just repeat the same debug loc as last time.
3545 } else {
3546 Vals.push_back(DL->getLine());
3547 Vals.push_back(DL->getColumn());
3548 Vals.push_back(VE.getMetadataOrNullID(DL->getScope()));
3549 Vals.push_back(VE.getMetadataOrNullID(DL->getInlinedAt()));
3550 Vals.push_back(DL->isImplicitCode());
3552 Vals.clear();
3553 LastDL = DL;
3554 }
3555 }
3556
3557 // If the instruction has DbgRecords attached to it, emit them. Note that
3558 // they come after the instruction so that it's easy to attach them again
3559 // when reading the bitcode, even though conceptually the debug locations
3560 // start "before" the instruction.
3561 if (I.hasDbgRecords() && WriteNewDbgInfoFormatToBitcode) {
3562 /// Try to push the value only (unwrapped), otherwise push the
3563 /// metadata wrapped value. Returns true if the value was pushed
3564 /// without the ValueAsMetadata wrapper.
3565 auto PushValueOrMetadata = [&Vals, InstID,
3566 this](Metadata *RawLocation) {
3567 assert(RawLocation &&
3568 "RawLocation unexpectedly null in DbgVariableRecord");
3569 if (ValueAsMetadata *VAM = dyn_cast<ValueAsMetadata>(RawLocation)) {
3570 SmallVector<unsigned, 2> ValAndType;
3571 // If the value is a fwd-ref the type is also pushed. We don't
3572 // want the type, so fwd-refs are kept wrapped (pushValueAndType
3573 // returns false if the value is pushed without type).
3574 if (!pushValueAndType(VAM->getValue(), InstID, ValAndType)) {
3575 Vals.push_back(ValAndType[0]);
3576 return true;
3577 }
3578 }
3579 // The metadata is a DIArgList, or ValueAsMetadata wrapping a
3580 // fwd-ref. Push the metadata ID.
3581 Vals.push_back(VE.getMetadataID(RawLocation));
3582 return false;
3583 };
3584
3585 // Write out non-instruction debug information attached to this
3586 // instruction. Write it after the instruction so that it's easy to
3587 // re-attach to the instruction reading the records in.
3588 for (DbgRecord &DR : I.DebugMarker->getDbgRecordRange()) {
3589 if (DbgLabelRecord *DLR = dyn_cast<DbgLabelRecord>(&DR)) {
3590 Vals.push_back(VE.getMetadataID(&*DLR->getDebugLoc()));
3591 Vals.push_back(VE.getMetadataID(DLR->getLabel()));
3593 Vals.clear();
3594 continue;
3595 }
3596
3597 // First 3 fields are common to all kinds:
3598 // DILocation, DILocalVariable, DIExpression
3599 // dbg_value (FUNC_CODE_DEBUG_RECORD_VALUE)
3600 // ..., LocationMetadata
3601 // dbg_value (FUNC_CODE_DEBUG_RECORD_VALUE_SIMPLE - abbrev'd)
3602 // ..., Value
3603 // dbg_declare (FUNC_CODE_DEBUG_RECORD_DECLARE)
3604 // ..., LocationMetadata
3605 // dbg_assign (FUNC_CODE_DEBUG_RECORD_ASSIGN)
3606 // ..., LocationMetadata, DIAssignID, DIExpression, LocationMetadata
3607 DbgVariableRecord &DVR = cast<DbgVariableRecord>(DR);
3608 Vals.push_back(VE.getMetadataID(&*DVR.getDebugLoc()));
3609 Vals.push_back(VE.getMetadataID(DVR.getVariable()));
3610 Vals.push_back(VE.getMetadataID(DVR.getExpression()));
3611 if (DVR.isDbgValue()) {
3612 if (PushValueOrMetadata(DVR.getRawLocation()))
3614 FUNCTION_DEBUG_RECORD_VALUE_ABBREV);
3615 else
3617 } else if (DVR.isDbgDeclare()) {
3618 Vals.push_back(VE.getMetadataID(DVR.getRawLocation()));
3620 } else {
3621 assert(DVR.isDbgAssign() && "Unexpected DbgRecord kind");
3622 Vals.push_back(VE.getMetadataID(DVR.getRawLocation()));
3623 Vals.push_back(VE.getMetadataID(DVR.getAssignID()));
3625 Vals.push_back(VE.getMetadataID(DVR.getRawAddress()));
3627 }
3628 Vals.clear();
3629 }
3630 }
3631 }
3632
3633 if (BlockAddress *BA = BlockAddress::lookup(&BB)) {
3634 SmallVector<Value *> Worklist{BA};
3635 SmallPtrSet<Value *, 8> Visited{BA};
3636 while (!Worklist.empty()) {
3637 Value *V = Worklist.pop_back_val();
3638 for (User *U : V->users()) {
3639 if (auto *I = dyn_cast<Instruction>(U)) {
3640 Function *P = I->getFunction();
3641 if (P != &F)
3642 BlockAddressUsers.insert(P);
3643 } else if (isa<Constant>(U) && !isa<GlobalValue>(U) &&
3644 Visited.insert(U).second)
3645 Worklist.push_back(U);
3646 }
3647 }
3648 }
3649 }
3650
3651 if (!BlockAddressUsers.empty()) {
3652 Vals.resize(BlockAddressUsers.size());
3653 for (auto I : llvm::enumerate(BlockAddressUsers))
3654 Vals[I.index()] = VE.getValueID(I.value());
3656 Vals.clear();
3657 }
3658
3659 // Emit names for all the instructions etc.
3660 if (auto *Symtab = F.getValueSymbolTable())
3661 writeFunctionLevelValueSymbolTable(*Symtab);
3662
3663 if (NeedsMetadataAttachment)
3664 writeFunctionMetadataAttachment(F);
3666 writeUseListBlock(&F);
3667 VE.purgeFunction();
3668 Stream.ExitBlock();
3669}
3670
3671// Emit blockinfo, which defines the standard abbreviations etc.
3672void ModuleBitcodeWriter::writeBlockInfo() {
3673 // We only want to emit block info records for blocks that have multiple
3674 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK.
3675 // Other blocks can define their abbrevs inline.
3676 Stream.EnterBlockInfoBlock();
3677
3678 { // 8-bit fixed-width VST_CODE_ENTRY/VST_CODE_BBENTRY strings.
3679 auto Abbv = std::make_shared<BitCodeAbbrev>();
3685 VST_ENTRY_8_ABBREV)
3686 llvm_unreachable("Unexpected abbrev ordering!");
3687 }
3688
3689 { // 7-bit fixed width VST_CODE_ENTRY strings.
3690 auto Abbv = std::make_shared<BitCodeAbbrev>();
3696 VST_ENTRY_7_ABBREV)
3697 llvm_unreachable("Unexpected abbrev ordering!");
3698 }
3699 { // 6-bit char6 VST_CODE_ENTRY strings.
3700 auto Abbv = std::make_shared<BitCodeAbbrev>();
3706 VST_ENTRY_6_ABBREV)
3707 llvm_unreachable("Unexpected abbrev ordering!");
3708 }
3709 { // 6-bit char6 VST_CODE_BBENTRY strings.
3710 auto Abbv = std::make_shared<BitCodeAbbrev>();
3716 VST_BBENTRY_6_ABBREV)
3717 llvm_unreachable("Unexpected abbrev ordering!");
3718 }
3719
3720 { // SETTYPE abbrev for CONSTANTS_BLOCK.
3721 auto Abbv = std::make_shared<BitCodeAbbrev>();
3726 CONSTANTS_SETTYPE_ABBREV)
3727 llvm_unreachable("Unexpected abbrev ordering!");
3728 }
3729
3730 { // INTEGER abbrev for CONSTANTS_BLOCK.
3731 auto Abbv = std::make_shared<BitCodeAbbrev>();
3735 CONSTANTS_INTEGER_ABBREV)
3736 llvm_unreachable("Unexpected abbrev ordering!");
3737 }
3738
3739 { // CE_CAST abbrev for CONSTANTS_BLOCK.
3740 auto Abbv = std::make_shared<BitCodeAbbrev>();
3742 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc
3743 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid
3745 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
3746
3748 CONSTANTS_CE_CAST_Abbrev)
3749 llvm_unreachable("Unexpected abbrev ordering!");
3750 }
3751 { // NULL abbrev for CONSTANTS_BLOCK.
3752 auto Abbv = std::make_shared<BitCodeAbbrev>();
3755 CONSTANTS_NULL_Abbrev)
3756 llvm_unreachable("Unexpected abbrev ordering!");
3757 }
3758
3759 // FIXME: This should only use space for first class types!
3760
3761 { // INST_LOAD abbrev for FUNCTION_BLOCK.
3762 auto Abbv = std::make_shared<BitCodeAbbrev>();
3764 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr
3765 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
3767 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align
3768 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
3769 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3770 FUNCTION_INST_LOAD_ABBREV)
3771 llvm_unreachable("Unexpected abbrev ordering!");
3772 }
3773 { // INST_UNOP abbrev for FUNCTION_BLOCK.
3774 auto Abbv = std::make_shared<BitCodeAbbrev>();
3776 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
3777 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
3778 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3779 FUNCTION_INST_UNOP_ABBREV)
3780 llvm_unreachable("Unexpected abbrev ordering!");
3781 }
3782 { // INST_UNOP_FLAGS abbrev for FUNCTION_BLOCK.
3783 auto Abbv = std::make_shared<BitCodeAbbrev>();
3785 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
3786 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
3787 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); // flags
3788 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3789 FUNCTION_INST_UNOP_FLAGS_ABBREV)
3790 llvm_unreachable("Unexpected abbrev ordering!");
3791 }
3792 { // INST_BINOP abbrev for FUNCTION_BLOCK.
3793 auto Abbv = std::make_shared<BitCodeAbbrev>();
3795 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
3796 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
3797 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
3798 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3799 FUNCTION_INST_BINOP_ABBREV)
3800 llvm_unreachable("Unexpected abbrev ordering!");
3801 }
3802 { // INST_BINOP_FLAGS abbrev for FUNCTION_BLOCK.
3803 auto Abbv = std::make_shared<BitCodeAbbrev>();
3805 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
3806 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
3807 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
3808 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); // flags
3809 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3810 FUNCTION_INST_BINOP_FLAGS_ABBREV)
3811 llvm_unreachable("Unexpected abbrev ordering!");
3812 }
3813 { // INST_CAST abbrev for FUNCTION_BLOCK.
3814 auto Abbv = std::make_shared<BitCodeAbbrev>();
3816 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal
3817 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
3819 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
3820 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3821 FUNCTION_INST_CAST_ABBREV)
3822 llvm_unreachable("Unexpected abbrev ordering!");
3823 }
3824 { // INST_CAST_FLAGS abbrev for FUNCTION_BLOCK.
3825 auto Abbv = std::make_shared<BitCodeAbbrev>();
3827 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal
3828 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
3830 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
3831 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); // flags
3832 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3833 FUNCTION_INST_CAST_FLAGS_ABBREV)
3834 llvm_unreachable("Unexpected abbrev ordering!");
3835 }
3836
3837 { // INST_RET abbrev for FUNCTION_BLOCK.
3838 auto Abbv = std::make_shared<BitCodeAbbrev>();
3840 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3841 FUNCTION_INST_RET_VOID_ABBREV)
3842 llvm_unreachable("Unexpected abbrev ordering!");
3843 }
3844 { // INST_RET abbrev for FUNCTION_BLOCK.
3845 auto Abbv = std::make_shared<BitCodeAbbrev>();
3847 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID
3848 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3849 FUNCTION_INST_RET_VAL_ABBREV)
3850 llvm_unreachable("Unexpected abbrev ordering!");
3851 }
3852 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
3853 auto Abbv = std::make_shared<BitCodeAbbrev>();
3855 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3856 FUNCTION_INST_UNREACHABLE_ABBREV)
3857 llvm_unreachable("Unexpected abbrev ordering!");
3858 }
3859 {
3860 auto Abbv = std::make_shared<BitCodeAbbrev>();
3863 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
3864 Log2_32_Ceil(VE.getTypes().size() + 1)));
3867 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3868 FUNCTION_INST_GEP_ABBREV)
3869 llvm_unreachable("Unexpected abbrev ordering!");
3870 }
3871 {
3872 auto Abbv = std::make_shared<BitCodeAbbrev>();
3874 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 7)); // dbgloc
3875 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 7)); // var
3876 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 7)); // expr
3877 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // val
3878 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3879 FUNCTION_DEBUG_RECORD_VALUE_ABBREV)
3880 llvm_unreachable("Unexpected abbrev ordering! 1");
3881 }
3882 Stream.ExitBlock();
3883}
3884
3885/// Write the module path strings, currently only used when generating
3886/// a combined index file.
3887void IndexBitcodeWriter::writeModStrings() {
3889
3890 // TODO: See which abbrev sizes we actually need to emit
3891
3892 // 8-bit fixed-width MST_ENTRY strings.
3893 auto Abbv = std::make_shared<BitCodeAbbrev>();
3898 unsigned Abbrev8Bit = Stream.EmitAbbrev(std::move(Abbv));
3899
3900 // 7-bit fixed width MST_ENTRY strings.
3901 Abbv = std::make_shared<BitCodeAbbrev>();
3906 unsigned Abbrev7Bit = Stream.EmitAbbrev(std::move(Abbv));
3907
3908 // 6-bit char6 MST_ENTRY strings.
3909 Abbv = std::make_shared<BitCodeAbbrev>();
3914 unsigned Abbrev6Bit = Stream.EmitAbbrev(std::move(Abbv));
3915
3916 // Module Hash, 160 bits SHA1. Optionally, emitted after each MST_CODE_ENTRY.
3917 Abbv = std::make_shared<BitCodeAbbrev>();
3924 unsigned AbbrevHash = Stream.EmitAbbrev(std::move(Abbv));
3925
3927 forEachModule([&](const StringMapEntry<ModuleHash> &MPSE) {
3928 StringRef Key = MPSE.getKey();
3929 const auto &Hash = MPSE.getValue();
3931 unsigned AbbrevToUse = Abbrev8Bit;
3932 if (Bits == SE_Char6)
3933 AbbrevToUse = Abbrev6Bit;
3934 else if (Bits == SE_Fixed7)
3935 AbbrevToUse = Abbrev7Bit;
3936
3937 auto ModuleId = ModuleIdMap.size();
3938 ModuleIdMap[Key] = ModuleId;
3939 Vals.push_back(ModuleId);
3940 Vals.append(Key.begin(), Key.end());
3941
3942 // Emit the finished record.
3943 Stream.EmitRecord(bitc::MST_CODE_ENTRY, Vals, AbbrevToUse);
3944
3945 // Emit an optional hash for the module now
3946 if (llvm::any_of(Hash, [](uint32_t H) { return H; })) {
3947 Vals.assign(Hash.begin(), Hash.end());
3948 // Emit the hash record.
3949 Stream.EmitRecord(bitc::MST_CODE_HASH, Vals, AbbrevHash);
3950 }
3951
3952 Vals.clear();
3953 });
3954 Stream.ExitBlock();
3955}
3956
3957/// Write the function type metadata related records that need to appear before
3958/// a function summary entry (whether per-module or combined).
3959template <typename Fn>
3961 FunctionSummary *FS,
3962 Fn GetValueID) {
3963 if (!FS->type_tests().empty())
3964 Stream.EmitRecord(bitc::FS_TYPE_TESTS, FS->type_tests());
3965
3967
3968 auto WriteVFuncIdVec = [&](uint64_t Ty,
3970 if (VFs.empty())
3971 return;
3972 Record.clear();
3973 for (auto &VF : VFs) {
3974 Record.push_back(VF.GUID);
3975 Record.push_back(VF.Offset);
3976 }
3977 Stream.EmitRecord(Ty, Record);
3978 };
3979
3980 WriteVFuncIdVec(bitc::FS_TYPE_TEST_ASSUME_VCALLS,
3981 FS->type_test_assume_vcalls());
3982 WriteVFuncIdVec(bitc::FS_TYPE_CHECKED_LOAD_VCALLS,
3983 FS->type_checked_load_vcalls());
3984
3985 auto WriteConstVCallVec = [&](uint64_t Ty,
3987 for (auto &VC : VCs) {
3988 Record.clear();
3989 Record.push_back(VC.VFunc.GUID);
3990 Record.push_back(VC.VFunc.Offset);
3991 llvm::append_range(Record, VC.Args);
3992 Stream.EmitRecord(Ty, Record);
3993 }
3994 };
3995
3996 WriteConstVCallVec(bitc::FS_TYPE_TEST_ASSUME_CONST_VCALL,
3997 FS->type_test_assume_const_vcalls());
3998 WriteConstVCallVec(bitc::FS_TYPE_CHECKED_LOAD_CONST_VCALL,
3999 FS->type_checked_load_const_vcalls());
4000
4001 auto WriteRange = [&](ConstantRange Range) {
4002 Range = Range.sextOrTrunc(FunctionSummary::ParamAccess::RangeWidth);
4003 assert(Range.getLower().getNumWords() == 1);
4004 assert(Range.getUpper().getNumWords() == 1);
4005 emitSignedInt64(Record, *Range.getLower().getRawData());
4006 emitSignedInt64(Record, *Range.getUpper().getRawData());
4007 };
4008
4009 if (!FS->paramAccesses().empty()) {
4010 Record.clear();
4011 for (auto &Arg : FS->paramAccesses()) {
4012 size_t UndoSize = Record.size();
4013 Record.push_back(Arg.ParamNo);
4014 WriteRange(Arg.Use);
4015 Record.push_back(Arg.Calls.size());
4016 for (auto &Call : Arg.Calls) {
4017 Record.push_back(Call.ParamNo);
4018 std::optional<unsigned> ValueID = GetValueID(Call.Callee);
4019 if (!ValueID) {
4020 // If ValueID is unknown we can't drop just this call, we must drop
4021 // entire parameter.
4022 Record.resize(UndoSize);
4023 break;
4024 }
4025 Record.push_back(*ValueID);
4026 WriteRange(Call.Offsets);
4027 }
4028 }
4029 if (!Record.empty())
4031 }
4032}
4033
4034/// Collect type IDs from type tests used by function.
4035static void
4037 std::set<GlobalValue::GUID> &ReferencedTypeIds) {
4038 if (!FS->type_tests().empty())
4039 for (auto &TT : FS->type_tests())
4040 ReferencedTypeIds.insert(TT);
4041
4042 auto GetReferencedTypesFromVFuncIdVec =
4044 for (auto &VF : VFs)
4045 ReferencedTypeIds.insert(VF.GUID);
4046 };
4047
4048 GetReferencedTypesFromVFuncIdVec(FS->type_test_assume_vcalls());
4049 GetReferencedTypesFromVFuncIdVec(FS->type_checked_load_vcalls());
4050
4051 auto GetReferencedTypesFromConstVCallVec =
4053 for (auto &VC : VCs)
4054 ReferencedTypeIds.insert(VC.VFunc.GUID);
4055 };
4056
4057 GetReferencedTypesFromConstVCallVec(FS->type_test_assume_const_vcalls());
4058 GetReferencedTypesFromConstVCallVec(FS->type_checked_load_const_vcalls());
4059}
4060
4062 SmallVector<uint64_t, 64> &NameVals, const std::vector<uint64_t> &args,
4064 NameVals.push_back(args.size());
4065 llvm::append_range(NameVals, args);
4066
4067 NameVals.push_back(ByArg.TheKind);
4068 NameVals.push_back(ByArg.Info);
4069 NameVals.push_back(ByArg.Byte);
4070 NameVals.push_back(ByArg.Bit);
4071}
4072
4074 SmallVector<uint64_t, 64> &NameVals, StringTableBuilder &StrtabBuilder,
4075 uint64_t Id, const WholeProgramDevirtResolution &Wpd) {
4076 NameVals.push_back(Id);
4077
4078 NameVals.push_back(Wpd.TheKind);
4079 NameVals.push_back(StrtabBuilder.add(Wpd.SingleImplName));
4080 NameVals.push_back(Wpd.SingleImplName.size());
4081
4082 NameVals.push_back(Wpd.ResByArg.size());
4083 for (auto &A : Wpd.ResByArg)
4084 writeWholeProgramDevirtResolutionByArg(NameVals, A.first, A.second);
4085}
4086
4088 StringTableBuilder &StrtabBuilder,
4089 const std::string &Id,
4090 const TypeIdSummary &Summary) {
4091 NameVals.push_back(StrtabBuilder.add(Id));
4092 NameVals.push_back(Id.size());
4093
4094 NameVals.push_back(Summary.TTRes.TheKind);
4095 NameVals.push_back(Summary.TTRes.SizeM1BitWidth);
4096 NameVals.push_back(Summary.TTRes.AlignLog2);
4097 NameVals.push_back(Summary.TTRes.SizeM1);
4098 NameVals.push_back(Summary.TTRes.BitMask);
4099 NameVals.push_back(Summary.TTRes.InlineBits);
4100
4101 for (auto &W : Summary.WPDRes)
4102 writeWholeProgramDevirtResolution(NameVals, StrtabBuilder, W.first,
4103 W.second);
4104}
4105
4107 SmallVector<uint64_t, 64> &NameVals, StringTableBuilder &StrtabBuilder,
4108 const std::string &Id, const TypeIdCompatibleVtableInfo &Summary,
4109 ValueEnumerator &VE) {
4110 NameVals.push_back(StrtabBuilder.add(Id));
4111 NameVals.push_back(Id.size());
4112
4113 for (auto &P : Summary) {
4114 NameVals.push_back(P.AddressPointOffset);
4115 NameVals.push_back(VE.getValueID(P.VTableVI.getValue()));
4116 }
4117}
4118
4120 BitstreamWriter &Stream, FunctionSummary *FS, unsigned CallsiteAbbrev,
4121 unsigned AllocAbbrev, bool PerModule,
4122 std::function<unsigned(const ValueInfo &VI)> GetValueID,
4123 std::function<unsigned(unsigned)> GetStackIndex) {
4125
4126 for (auto &CI : FS->callsites()) {
4127 Record.clear();
4128 // Per module callsite clones should always have a single entry of
4129 // value 0.
4130 assert(!PerModule || (CI.Clones.size() == 1 && CI.Clones[0] == 0));
4131 Record.push_back(GetValueID(CI.Callee));
4132 if (!PerModule) {
4133 Record.push_back(CI.StackIdIndices.size());
4134 Record.push_back(CI.Clones.size());
4135 }
4136 for (auto Id : CI.StackIdIndices)
4137 Record.push_back(GetStackIndex(Id));
4138 if (!PerModule) {
4139 for (auto V : CI.Clones)
4140 Record.push_back(V);
4141 }
4144 Record, CallsiteAbbrev);
4145 }
4146
4147 for (auto &AI : FS->allocs()) {
4148 Record.clear();
4149 // Per module alloc versions should always have a single entry of
4150 // value 0.
4151 assert(!PerModule || (AI.Versions.size() == 1 && AI.Versions[0] == 0));
4152 if (!PerModule) {
4153 Record.push_back(AI.MIBs.size());
4154 Record.push_back(AI.Versions.size());
4155 }
4156 for (auto &MIB : AI.MIBs) {
4157 Record.push_back((uint8_t)MIB.AllocType);
4158 Record.push_back(MIB.StackIdIndices.size());
4159 for (auto Id : MIB.StackIdIndices)
4160 Record.push_back(GetStackIndex(Id));
4161 }
4162 if (!PerModule) {
4163 for (auto V : AI.Versions)
4164 Record.push_back(V);
4165 }
4166 Stream.EmitRecord(PerModule ? bitc::FS_PERMODULE_ALLOC_INFO
4168 Record, AllocAbbrev);
4169 }
4170}
4171
4172// Helper to emit a single function summary record.
4173void ModuleBitcodeWriterBase::writePerModuleFunctionSummaryRecord(
4175 unsigned ValueID, unsigned FSCallsRelBFAbbrev,
4176 unsigned FSCallsProfileAbbrev, unsigned CallsiteAbbrev,
4177 unsigned AllocAbbrev, const Function &F) {
4178 NameVals.push_back(ValueID);
4179
4180 FunctionSummary *FS = cast<FunctionSummary>(Summary);
4181
4183 Stream, FS, [&](const ValueInfo &VI) -> std::optional<unsigned> {
4184 return {VE.getValueID(VI.getValue())};
4185 });
4186
4188 Stream, FS, CallsiteAbbrev, AllocAbbrev,
4189 /*PerModule*/ true,
4190 /*GetValueId*/ [&](const ValueInfo &VI) { return getValueId(VI); },
4191 /*GetStackIndex*/ [&](unsigned I) { return I; });
4192
4193 auto SpecialRefCnts = FS->specialRefCounts();
4194 NameVals.push_back(getEncodedGVSummaryFlags(FS->flags()));
4195 NameVals.push_back(FS->instCount());
4196 NameVals.push_back(getEncodedFFlags(FS->fflags()));
4197 NameVals.push_back(FS->refs().size());
4198 NameVals.push_back(SpecialRefCnts.first); // rorefcnt
4199 NameVals.push_back(SpecialRefCnts.second); // worefcnt
4200
4201 for (auto &RI : FS->refs())
4202 NameVals.push_back(getValueId(RI));
4203
4204 const bool UseRelBFRecord =
4205 WriteRelBFToSummary && !F.hasProfileData() &&
4207 for (auto &ECI : FS->calls()) {
4208 NameVals.push_back(getValueId(ECI.first));
4209 if (UseRelBFRecord)
4210 NameVals.push_back(getEncodedRelBFCallEdgeInfo(ECI.second));
4211 else
4212 NameVals.push_back(getEncodedHotnessCallEdgeInfo(ECI.second));
4213 }
4214
4215 unsigned FSAbbrev =
4216 (UseRelBFRecord ? FSCallsRelBFAbbrev : FSCallsProfileAbbrev);
4217 unsigned Code =
4219
4220 // Emit the finished record.
4221 Stream.EmitRecord(Code, NameVals, FSAbbrev);
4222 NameVals.clear();
4223}
4224
4225// Collect the global value references in the given variable's initializer,
4226// and emit them in a summary record.
4227void ModuleBitcodeWriterBase::writeModuleLevelReferences(
4228 const GlobalVariable &V, SmallVector<uint64_t, 64> &NameVals,
4229 unsigned FSModRefsAbbrev, unsigned FSModVTableRefsAbbrev) {
4230 auto VI = Index->getValueInfo(V.getGUID());
4231 if (!VI || VI.getSummaryList().empty()) {
4232 // Only declarations should not have a summary (a declaration might however
4233 // have a summary if the def was in module level asm).
4234 assert(V.isDeclaration());
4235 return;
4236 }
4237 auto *Summary = VI.getSummaryList()[0].get();
4238 NameVals.push_back(VE.getValueID(&V));
4239 GlobalVarSummary *VS = cast<GlobalVarSummary>(Summary);
4240 NameVals.push_back(getEncodedGVSummaryFlags(VS->flags()));
4241 NameVals.push_back(getEncodedGVarFlags(VS->varflags()));
4242
4243 auto VTableFuncs = VS->vTableFuncs();
4244 if (!VTableFuncs.empty())
4245 NameVals.push_back(VS->refs().size());
4246
4247 unsigned SizeBeforeRefs = NameVals.size();
4248 for (auto &RI : VS->refs())
4249 NameVals.push_back(VE.getValueID(RI.getValue()));
4250 // Sort the refs for determinism output, the vector returned by FS->refs() has
4251 // been initialized from a DenseSet.
4252 llvm::sort(drop_begin(NameVals, SizeBeforeRefs));
4253
4254 if (VTableFuncs.empty())
4256 FSModRefsAbbrev);
4257 else {
4258 // VTableFuncs pairs should already be sorted by offset.
4259 for (auto &P : VTableFuncs) {
4260 NameVals.push_back(VE.getValueID(P.FuncVI.getValue()));
4261 NameVals.push_back(P.VTableOffset);
4262 }
4263
4265 FSModVTableRefsAbbrev);
4266 }
4267 NameVals.clear();
4268}
4269
4270/// Emit the per-module summary section alongside the rest of
4271/// the module's bitcode.
4272void ModuleBitcodeWriterBase::writePerModuleGlobalValueSummary() {
4273 // By default we compile with ThinLTO if the module has a summary, but the
4274 // client can request full LTO with a module flag.
4275 bool IsThinLTO = true;
4276 if (auto *MD =
4277 mdconst::extract_or_null<ConstantInt>(M.getModuleFlag("ThinLTO")))
4278 IsThinLTO = MD->getZExtValue();
4281 4);
4282
4283 Stream.EmitRecord(
4286
4287 // Write the index flags.
4288 uint64_t Flags = 0;
4289 // Bits 1-3 are set only in the combined index, skip them.
4290 if (Index->enableSplitLTOUnit())
4291 Flags |= 0x8;
4292 if (Index->hasUnifiedLTO())
4293 Flags |= 0x200;
4294
4296
4297 if (Index->begin() == Index->end()) {
4298 Stream.ExitBlock();
4299 return;
4300 }
4301
4302 for (const auto &GVI : valueIds()) {
4304 ArrayRef<uint64_t>{GVI.second, GVI.first});
4305 }
4306
4307 if (!Index->stackIds().empty()) {
4308 auto StackIdAbbv = std::make_shared<BitCodeAbbrev>();
4309 StackIdAbbv->Add(BitCodeAbbrevOp(bitc::FS_STACK_IDS));
4310 // numids x stackid
4311 StackIdAbbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
4312 StackIdAbbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
4313 unsigned StackIdAbbvId = Stream.EmitAbbrev(std::move(StackIdAbbv));
4314 Stream.EmitRecord(bitc::FS_STACK_IDS, Index->stackIds(), StackIdAbbvId);
4315 }
4316
4317 // Abbrev for FS_PERMODULE_PROFILE.
4318 auto Abbv = std::make_shared<BitCodeAbbrev>();
4320 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
4321 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // flags
4322 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
4323 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // fflags
4324 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
4325 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // rorefcnt
4326 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // worefcnt
4327 // numrefs x valueid, n x (valueid, hotness+tailcall flags)
4330 unsigned FSCallsProfileAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4331
4332 // Abbrev for FS_PERMODULE_RELBF.
4333 Abbv = std::make_shared<BitCodeAbbrev>();
4335 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
4336 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
4337 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
4338 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // fflags
4339 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
4340 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // rorefcnt
4341 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // worefcnt
4342 // numrefs x valueid, n x (valueid, rel_block_freq+tailcall])
4345 unsigned FSCallsRelBFAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4346
4347 // Abbrev for FS_PERMODULE_GLOBALVAR_INIT_REFS.
4348 Abbv = std::make_shared<BitCodeAbbrev>();
4350 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
4351 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
4352 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); // valueids
4354 unsigned FSModRefsAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4355
4356 // Abbrev for FS_PERMODULE_VTABLE_GLOBALVAR_INIT_REFS.
4357 Abbv = std::make_shared<BitCodeAbbrev>();
4359 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
4360 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
4361 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
4362 // numrefs x valueid, n x (valueid , offset)
4365 unsigned FSModVTableRefsAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4366
4367 // Abbrev for FS_ALIAS.
4368 Abbv = std::make_shared<BitCodeAbbrev>();
4369 Abbv->Add(BitCodeAbbrevOp(bitc::FS_ALIAS));
4370 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
4371 Abbv->Add(BitCodeAbbrevOp(