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