LLVM 20.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::SanitizeRealtime:
848 case Attribute::SpeculativeLoadHardening:
850 case Attribute::SwiftError:
852 case Attribute::SwiftSelf:
854 case Attribute::SwiftAsync:
856 case Attribute::UWTable:
858 case Attribute::VScaleRange:
860 case Attribute::WillReturn:
862 case Attribute::WriteOnly:
864 case Attribute::ZExt:
866 case Attribute::ImmArg:
868 case Attribute::SanitizeMemTag:
870 case Attribute::Preallocated:
872 case Attribute::NoUndef:
874 case Attribute::ByRef:
876 case Attribute::MustProgress:
878 case Attribute::PresplitCoroutine:
880 case Attribute::Writable:
882 case Attribute::CoroDestroyOnlyWhenComplete:
884 case Attribute::DeadOnUnwind:
886 case Attribute::Range:
888 case Attribute::Initializes:
891 llvm_unreachable("Can not encode end-attribute kinds marker.");
892 case Attribute::None:
893 llvm_unreachable("Can not encode none-attribute.");
896 llvm_unreachable("Trying to encode EmptyKey/TombstoneKey");
897 }
898
899 llvm_unreachable("Trying to encode unknown attribute");
900}
901
903 if ((int64_t)V >= 0)
904 Vals.push_back(V << 1);
905 else
906 Vals.push_back((-V << 1) | 1);
907}
908
909static void emitWideAPInt(SmallVectorImpl<uint64_t> &Vals, const APInt &A) {
910 // We have an arbitrary precision integer value to write whose
911 // bit width is > 64. However, in canonical unsigned integer
912 // format it is likely that the high bits are going to be zero.
913 // So, we only write the number of active words.
914 unsigned NumWords = A.getActiveWords();
915 const uint64_t *RawData = A.getRawData();
916 for (unsigned i = 0; i < NumWords; i++)
917 emitSignedInt64(Vals, RawData[i]);
918}
919
921 const ConstantRange &CR, bool EmitBitWidth) {
922 unsigned BitWidth = CR.getBitWidth();
923 if (EmitBitWidth)
924 Record.push_back(BitWidth);
925 if (BitWidth > 64) {
926 Record.push_back(CR.getLower().getActiveWords() |
927 (uint64_t(CR.getUpper().getActiveWords()) << 32));
930 } else {
933 }
934}
935
936void ModuleBitcodeWriter::writeAttributeGroupTable() {
937 const std::vector<ValueEnumerator::IndexAndAttrSet> &AttrGrps =
939 if (AttrGrps.empty()) return;
940
942
944 for (ValueEnumerator::IndexAndAttrSet Pair : AttrGrps) {
945 unsigned AttrListIndex = Pair.first;
946 AttributeSet AS = Pair.second;
947 Record.push_back(VE.getAttributeGroupID(Pair));
948 Record.push_back(AttrListIndex);
949
950 for (Attribute Attr : AS) {
951 if (Attr.isEnumAttribute()) {
952 Record.push_back(0);
953 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
954 } else if (Attr.isIntAttribute()) {
955 Record.push_back(1);
956 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
957 Record.push_back(Attr.getValueAsInt());
958 } else if (Attr.isStringAttribute()) {
959 StringRef Kind = Attr.getKindAsString();
960 StringRef Val = Attr.getValueAsString();
961
962 Record.push_back(Val.empty() ? 3 : 4);
963 Record.append(Kind.begin(), Kind.end());
964 Record.push_back(0);
965 if (!Val.empty()) {
966 Record.append(Val.begin(), Val.end());
967 Record.push_back(0);
968 }
969 } else if (Attr.isTypeAttribute()) {
970 Type *Ty = Attr.getValueAsType();
971 Record.push_back(Ty ? 6 : 5);
972 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
973 if (Ty)
974 Record.push_back(VE.getTypeID(Attr.getValueAsType()));
975 } else if (Attr.isConstantRangeAttribute()) {
976 Record.push_back(7);
977 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
978 emitConstantRange(Record, Attr.getValueAsConstantRange(),
979 /*EmitBitWidth=*/true);
980 } else {
981 assert(Attr.isConstantRangeListAttribute());
982 Record.push_back(8);
983 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
984 ArrayRef<ConstantRange> Val = Attr.getValueAsConstantRangeList();
985 Record.push_back(Val.size());
986 Record.push_back(Val[0].getBitWidth());
987 for (auto &CR : Val)
988 emitConstantRange(Record, CR, /*EmitBitWidth=*/false);
989 }
990 }
991
993 Record.clear();
994 }
995
996 Stream.ExitBlock();
997}
998
999void ModuleBitcodeWriter::writeAttributeTable() {
1000 const std::vector<AttributeList> &Attrs = VE.getAttributeLists();
1001 if (Attrs.empty()) return;
1002
1004
1006 for (const AttributeList &AL : Attrs) {
1007 for (unsigned i : AL.indexes()) {
1008 AttributeSet AS = AL.getAttributes(i);
1009 if (AS.hasAttributes())
1010 Record.push_back(VE.getAttributeGroupID({i, AS}));
1011 }
1012
1014 Record.clear();
1015 }
1016
1017 Stream.ExitBlock();
1018}
1019
1020/// WriteTypeTable - Write out the type table for a module.
1021void ModuleBitcodeWriter::writeTypeTable() {
1022 const ValueEnumerator::TypeList &TypeList = VE.getTypes();
1023
1024 Stream.EnterSubblock(bitc::TYPE_BLOCK_ID_NEW, 4 /*count from # abbrevs */);
1026
1028
1029 // Abbrev for TYPE_CODE_OPAQUE_POINTER.
1030 auto Abbv = std::make_shared<BitCodeAbbrev>();
1032 Abbv->Add(BitCodeAbbrevOp(0)); // Addrspace = 0
1033 unsigned OpaquePtrAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1034
1035 // Abbrev for TYPE_CODE_FUNCTION.
1036 Abbv = std::make_shared<BitCodeAbbrev>();
1038 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg
1040 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
1041 unsigned FunctionAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1042
1043 // Abbrev for TYPE_CODE_STRUCT_ANON.
1044 Abbv = std::make_shared<BitCodeAbbrev>();
1046 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
1048 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
1049 unsigned StructAnonAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1050
1051 // Abbrev for TYPE_CODE_STRUCT_NAME.
1052 Abbv = std::make_shared<BitCodeAbbrev>();
1056 unsigned StructNameAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1057
1058 // Abbrev for TYPE_CODE_STRUCT_NAMED.
1059 Abbv = std::make_shared<BitCodeAbbrev>();
1061 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
1063 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
1064 unsigned StructNamedAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1065
1066 // Abbrev for TYPE_CODE_ARRAY.
1067 Abbv = std::make_shared<BitCodeAbbrev>();
1069 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size
1070 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
1071 unsigned ArrayAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1072
1073 // Emit an entry count so the reader can reserve space.
1074 TypeVals.push_back(TypeList.size());
1075 Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
1076 TypeVals.clear();
1077
1078 // Loop over all of the types, emitting each in turn.
1079 for (Type *T : TypeList) {
1080 int AbbrevToUse = 0;
1081 unsigned Code = 0;
1082
1083 switch (T->getTypeID()) {
1093 case Type::MetadataTyID:
1095 break;
1098 case Type::IntegerTyID:
1099 // INTEGER: [width]
1101 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
1102 break;
1103 case Type::PointerTyID: {
1104 PointerType *PTy = cast<PointerType>(T);
1105 unsigned AddressSpace = PTy->getAddressSpace();
1106 // OPAQUE_POINTER: [address space]
1108 TypeVals.push_back(AddressSpace);
1109 if (AddressSpace == 0)
1110 AbbrevToUse = OpaquePtrAbbrev;
1111 break;
1112 }
1113 case Type::FunctionTyID: {
1114 FunctionType *FT = cast<FunctionType>(T);
1115 // FUNCTION: [isvararg, retty, paramty x N]
1117 TypeVals.push_back(FT->isVarArg());
1118 TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
1119 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
1120 TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
1121 AbbrevToUse = FunctionAbbrev;
1122 break;
1123 }
1124 case Type::StructTyID: {
1125 StructType *ST = cast<StructType>(T);
1126 // STRUCT: [ispacked, eltty x N]
1127 TypeVals.push_back(ST->isPacked());
1128 // Output all of the element types.
1129 for (Type *ET : ST->elements())
1130 TypeVals.push_back(VE.getTypeID(ET));
1131
1132 if (ST->isLiteral()) {
1134 AbbrevToUse = StructAnonAbbrev;
1135 } else {
1136 if (ST->isOpaque()) {
1138 } else {
1140 AbbrevToUse = StructNamedAbbrev;
1141 }
1142
1143 // Emit the name if it is present.
1144 if (!ST->getName().empty())
1146 StructNameAbbrev);
1147 }
1148 break;
1149 }
1150 case Type::ArrayTyID: {
1151 ArrayType *AT = cast<ArrayType>(T);
1152 // ARRAY: [numelts, eltty]
1154 TypeVals.push_back(AT->getNumElements());
1155 TypeVals.push_back(VE.getTypeID(AT->getElementType()));
1156 AbbrevToUse = ArrayAbbrev;
1157 break;
1158 }
1161 VectorType *VT = cast<VectorType>(T);
1162 // VECTOR [numelts, eltty] or
1163 // [numelts, eltty, scalable]
1165 TypeVals.push_back(VT->getElementCount().getKnownMinValue());
1166 TypeVals.push_back(VE.getTypeID(VT->getElementType()));
1167 if (isa<ScalableVectorType>(VT))
1168 TypeVals.push_back(true);
1169 break;
1170 }
1171 case Type::TargetExtTyID: {
1172 TargetExtType *TET = cast<TargetExtType>(T);
1175 StructNameAbbrev);
1176 TypeVals.push_back(TET->getNumTypeParameters());
1177 for (Type *InnerTy : TET->type_params())
1178 TypeVals.push_back(VE.getTypeID(InnerTy));
1179 for (unsigned IntParam : TET->int_params())
1180 TypeVals.push_back(IntParam);
1181 break;
1182 }
1184 llvm_unreachable("Typed pointers cannot be added to IR modules");
1185 }
1186
1187 // Emit the finished record.
1188 Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
1189 TypeVals.clear();
1190 }
1191
1192 Stream.ExitBlock();
1193}
1194
1195static unsigned getEncodedLinkage(const GlobalValue::LinkageTypes Linkage) {
1196 switch (Linkage) {
1198 return 0;
1200 return 16;
1202 return 2;
1204 return 3;
1206 return 18;
1208 return 7;
1210 return 8;
1212 return 9;
1214 return 17;
1216 return 19;
1218 return 12;
1219 }
1220 llvm_unreachable("Invalid linkage");
1221}
1222
1223static unsigned getEncodedLinkage(const GlobalValue &GV) {
1224 return getEncodedLinkage(GV.getLinkage());
1225}
1226
1228 uint64_t RawFlags = 0;
1229 RawFlags |= Flags.ReadNone;
1230 RawFlags |= (Flags.ReadOnly << 1);
1231 RawFlags |= (Flags.NoRecurse << 2);
1232 RawFlags |= (Flags.ReturnDoesNotAlias << 3);
1233 RawFlags |= (Flags.NoInline << 4);
1234 RawFlags |= (Flags.AlwaysInline << 5);
1235 RawFlags |= (Flags.NoUnwind << 6);
1236 RawFlags |= (Flags.MayThrow << 7);
1237 RawFlags |= (Flags.HasUnknownCall << 8);
1238 RawFlags |= (Flags.MustBeUnreachable << 9);
1239 return RawFlags;
1240}
1241
1242// Decode the flags for GlobalValue in the summary. See getDecodedGVSummaryFlags
1243// in BitcodeReader.cpp.
1245 bool ImportAsDecl = false) {
1246 uint64_t RawFlags = 0;
1247
1248 RawFlags |= Flags.NotEligibleToImport; // bool
1249 RawFlags |= (Flags.Live << 1);
1250 RawFlags |= (Flags.DSOLocal << 2);
1251 RawFlags |= (Flags.CanAutoHide << 3);
1252
1253 // Linkage don't need to be remapped at that time for the summary. Any future
1254 // change to the getEncodedLinkage() function will need to be taken into
1255 // account here as well.
1256 RawFlags = (RawFlags << 4) | Flags.Linkage; // 4 bits
1257
1258 RawFlags |= (Flags.Visibility << 8); // 2 bits
1259
1260 unsigned ImportType = Flags.ImportType | ImportAsDecl;
1261 RawFlags |= (ImportType << 10); // 1 bit
1262
1263 return RawFlags;
1264}
1265
1267 uint64_t RawFlags = Flags.MaybeReadOnly | (Flags.MaybeWriteOnly << 1) |
1268 (Flags.Constant << 2) | Flags.VCallVisibility << 3;
1269 return RawFlags;
1270}
1271
1273 uint64_t RawFlags = 0;
1274
1275 RawFlags |= CI.Hotness; // 3 bits
1276 RawFlags |= (CI.HasTailCall << 3); // 1 bit
1277
1278 return RawFlags;
1279}
1280
1282 uint64_t RawFlags = 0;
1283
1284 RawFlags |= CI.RelBlockFreq; // CalleeInfo::RelBlockFreqBits bits
1285 RawFlags |= (CI.HasTailCall << CalleeInfo::RelBlockFreqBits); // 1 bit
1286
1287 return RawFlags;
1288}
1289
1290static unsigned getEncodedVisibility(const GlobalValue &GV) {
1291 switch (GV.getVisibility()) {
1292 case GlobalValue::DefaultVisibility: return 0;
1293 case GlobalValue::HiddenVisibility: return 1;
1294 case GlobalValue::ProtectedVisibility: return 2;
1295 }
1296 llvm_unreachable("Invalid visibility");
1297}
1298
1299static unsigned getEncodedDLLStorageClass(const GlobalValue &GV) {
1300 switch (GV.getDLLStorageClass()) {
1301 case GlobalValue::DefaultStorageClass: return 0;
1304 }
1305 llvm_unreachable("Invalid DLL storage class");
1306}
1307
1308static unsigned getEncodedThreadLocalMode(const GlobalValue &GV) {
1309 switch (GV.getThreadLocalMode()) {
1310 case GlobalVariable::NotThreadLocal: return 0;
1311 case GlobalVariable::GeneralDynamicTLSModel: return 1;
1312 case GlobalVariable::LocalDynamicTLSModel: return 2;
1313 case GlobalVariable::InitialExecTLSModel: return 3;
1314 case GlobalVariable::LocalExecTLSModel: return 4;
1315 }
1316 llvm_unreachable("Invalid TLS model");
1317}
1318
1319static unsigned getEncodedComdatSelectionKind(const Comdat &C) {
1320 switch (C.getSelectionKind()) {
1321 case Comdat::Any:
1323 case Comdat::ExactMatch:
1325 case Comdat::Largest:
1329 case Comdat::SameSize:
1331 }
1332 llvm_unreachable("Invalid selection kind");
1333}
1334
1335static unsigned getEncodedUnnamedAddr(const GlobalValue &GV) {
1336 switch (GV.getUnnamedAddr()) {
1337 case GlobalValue::UnnamedAddr::None: return 0;
1338 case GlobalValue::UnnamedAddr::Local: return 2;
1339 case GlobalValue::UnnamedAddr::Global: return 1;
1340 }
1341 llvm_unreachable("Invalid unnamed_addr");
1342}
1343
1344size_t ModuleBitcodeWriter::addToStrtab(StringRef Str) {
1345 if (GenerateHash)
1346 Hasher.update(Str);
1347 return StrtabBuilder.add(Str);
1348}
1349
1350void ModuleBitcodeWriter::writeComdats() {
1352 for (const Comdat *C : VE.getComdats()) {
1353 // COMDAT: [strtab offset, strtab size, selection_kind]
1354 Vals.push_back(addToStrtab(C->getName()));
1355 Vals.push_back(C->getName().size());
1357 Stream.EmitRecord(bitc::MODULE_CODE_COMDAT, Vals, /*AbbrevToUse=*/0);
1358 Vals.clear();
1359 }
1360}
1361
1362/// Write a record that will eventually hold the word offset of the
1363/// module-level VST. For now the offset is 0, which will be backpatched
1364/// after the real VST is written. Saves the bit offset to backpatch.
1365void ModuleBitcodeWriter::writeValueSymbolTableForwardDecl() {
1366 // Write a placeholder value in for the offset of the real VST,
1367 // which is written after the function blocks so that it can include
1368 // the offset of each function. The placeholder offset will be
1369 // updated when the real VST is written.
1370 auto Abbv = std::make_shared<BitCodeAbbrev>();
1372 // Blocks are 32-bit aligned, so we can use a 32-bit word offset to
1373 // hold the real VST offset. Must use fixed instead of VBR as we don't
1374 // know how many VBR chunks to reserve ahead of time.
1376 unsigned VSTOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1377
1378 // Emit the placeholder
1380 Stream.EmitRecordWithAbbrev(VSTOffsetAbbrev, Vals);
1381
1382 // Compute and save the bit offset to the placeholder, which will be
1383 // patched when the real VST is written. We can simply subtract the 32-bit
1384 // fixed size from the current bit number to get the location to backpatch.
1385 VSTOffsetPlaceholder = Stream.GetCurrentBitNo() - 32;
1386}
1387
1389
1390/// Determine the encoding to use for the given string name and length.
1392 bool isChar6 = true;
1393 for (char C : Str) {
1394 if (isChar6)
1395 isChar6 = BitCodeAbbrevOp::isChar6(C);
1396 if ((unsigned char)C & 128)
1397 // don't bother scanning the rest.
1398 return SE_Fixed8;
1399 }
1400 if (isChar6)
1401 return SE_Char6;
1402 return SE_Fixed7;
1403}
1404
1405static_assert(sizeof(GlobalValue::SanitizerMetadata) <= sizeof(unsigned),
1406 "Sanitizer Metadata is too large for naive serialization.");
1407static unsigned
1409 return Meta.NoAddress | (Meta.NoHWAddress << 1) |
1410 (Meta.Memtag << 2) | (Meta.IsDynInit << 3);
1411}
1412
1413/// Emit top-level description of module, including target triple, inline asm,
1414/// descriptors for global variables, and function prototype info.
1415/// Returns the bit offset to backpatch with the location of the real VST.
1416void ModuleBitcodeWriter::writeModuleInfo() {
1417 // Emit various pieces of data attached to a module.
1418 if (!M.getTargetTriple().empty())
1419 writeStringRecord(Stream, bitc::MODULE_CODE_TRIPLE, M.getTargetTriple(),
1420 0 /*TODO*/);
1421 const std::string &DL = M.getDataLayoutStr();
1422 if (!DL.empty())
1424 if (!M.getModuleInlineAsm().empty())
1425 writeStringRecord(Stream, bitc::MODULE_CODE_ASM, M.getModuleInlineAsm(),
1426 0 /*TODO*/);
1427
1428 // Emit information about sections and GC, computing how many there are. Also
1429 // compute the maximum alignment value.
1430 std::map<std::string, unsigned> SectionMap;
1431 std::map<std::string, unsigned> GCMap;
1432 MaybeAlign MaxAlignment;
1433 unsigned MaxGlobalType = 0;
1434 const auto UpdateMaxAlignment = [&MaxAlignment](const MaybeAlign A) {
1435 if (A)
1436 MaxAlignment = !MaxAlignment ? *A : std::max(*MaxAlignment, *A);
1437 };
1438 for (const GlobalVariable &GV : M.globals()) {
1439 UpdateMaxAlignment(GV.getAlign());
1440 MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV.getValueType()));
1441 if (GV.hasSection()) {
1442 // Give section names unique ID's.
1443 unsigned &Entry = SectionMap[std::string(GV.getSection())];
1444 if (!Entry) {
1445 writeStringRecord(Stream, bitc::MODULE_CODE_SECTIONNAME, GV.getSection(),
1446 0 /*TODO*/);
1447 Entry = SectionMap.size();
1448 }
1449 }
1450 }
1451 for (const Function &F : M) {
1452 UpdateMaxAlignment(F.getAlign());
1453 if (F.hasSection()) {
1454 // Give section names unique ID's.
1455 unsigned &Entry = SectionMap[std::string(F.getSection())];
1456 if (!Entry) {
1458 0 /*TODO*/);
1459 Entry = SectionMap.size();
1460 }
1461 }
1462 if (F.hasGC()) {
1463 // Same for GC names.
1464 unsigned &Entry = GCMap[F.getGC()];
1465 if (!Entry) {
1467 0 /*TODO*/);
1468 Entry = GCMap.size();
1469 }
1470 }
1471 }
1472
1473 // Emit abbrev for globals, now that we know # sections and max alignment.
1474 unsigned SimpleGVarAbbrev = 0;
1475 if (!M.global_empty()) {
1476 // Add an abbrev for common globals with no visibility or thread localness.
1477 auto Abbv = std::make_shared<BitCodeAbbrev>();
1482 Log2_32_Ceil(MaxGlobalType+1)));
1483 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // AddrSpace << 2
1484 //| explicitType << 1
1485 //| constant
1486 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer.
1487 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // Linkage.
1488 if (!MaxAlignment) // Alignment.
1489 Abbv->Add(BitCodeAbbrevOp(0));
1490 else {
1491 unsigned MaxEncAlignment = getEncodedAlign(MaxAlignment);
1493 Log2_32_Ceil(MaxEncAlignment+1)));
1494 }
1495 if (SectionMap.empty()) // Section.
1496 Abbv->Add(BitCodeAbbrevOp(0));
1497 else
1499 Log2_32_Ceil(SectionMap.size()+1)));
1500 // Don't bother emitting vis + thread local.
1501 SimpleGVarAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1502 }
1503
1505 // Emit the module's source file name.
1506 {
1507 StringEncoding Bits = getStringEncoding(M.getSourceFileName());
1509 if (Bits == SE_Char6)
1510 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Char6);
1511 else if (Bits == SE_Fixed7)
1512 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7);
1513
1514 // MODULE_CODE_SOURCE_FILENAME: [namechar x N]
1515 auto Abbv = std::make_shared<BitCodeAbbrev>();
1518 Abbv->Add(AbbrevOpToUse);
1519 unsigned FilenameAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1520
1521 for (const auto P : M.getSourceFileName())
1522 Vals.push_back((unsigned char)P);
1523
1524 // Emit the finished record.
1525 Stream.EmitRecord(bitc::MODULE_CODE_SOURCE_FILENAME, Vals, FilenameAbbrev);
1526 Vals.clear();
1527 }
1528
1529 // Emit the global variable information.
1530 for (const GlobalVariable &GV : M.globals()) {
1531 unsigned AbbrevToUse = 0;
1532
1533 // GLOBALVAR: [strtab offset, strtab size, type, isconst, initid,
1534 // linkage, alignment, section, visibility, threadlocal,
1535 // unnamed_addr, externally_initialized, dllstorageclass,
1536 // comdat, attributes, DSO_Local, GlobalSanitizer, code_model]
1537 Vals.push_back(addToStrtab(GV.getName()));
1538 Vals.push_back(GV.getName().size());
1539 Vals.push_back(VE.getTypeID(GV.getValueType()));
1540 Vals.push_back(GV.getType()->getAddressSpace() << 2 | 2 | GV.isConstant());
1541 Vals.push_back(GV.isDeclaration() ? 0 :
1542 (VE.getValueID(GV.getInitializer()) + 1));
1543 Vals.push_back(getEncodedLinkage(GV));
1544 Vals.push_back(getEncodedAlign(GV.getAlign()));
1545 Vals.push_back(GV.hasSection() ? SectionMap[std::string(GV.getSection())]
1546 : 0);
1547 if (GV.isThreadLocal() ||
1548 GV.getVisibility() != GlobalValue::DefaultVisibility ||
1549 GV.getUnnamedAddr() != GlobalValue::UnnamedAddr::None ||
1550 GV.isExternallyInitialized() ||
1551 GV.getDLLStorageClass() != GlobalValue::DefaultStorageClass ||
1552 GV.hasComdat() || GV.hasAttributes() || GV.isDSOLocal() ||
1553 GV.hasPartition() || GV.hasSanitizerMetadata() || GV.getCodeModel()) {
1557 Vals.push_back(GV.isExternallyInitialized());
1559 Vals.push_back(GV.hasComdat() ? VE.getComdatID(GV.getComdat()) : 0);
1560
1561 auto AL = GV.getAttributesAsList(AttributeList::FunctionIndex);
1562 Vals.push_back(VE.getAttributeListID(AL));
1563
1564 Vals.push_back(GV.isDSOLocal());
1565 Vals.push_back(addToStrtab(GV.getPartition()));
1566 Vals.push_back(GV.getPartition().size());
1567
1568 Vals.push_back((GV.hasSanitizerMetadata() ? serializeSanitizerMetadata(
1569 GV.getSanitizerMetadata())
1570 : 0));
1571 Vals.push_back(GV.getCodeModelRaw());
1572 } else {
1573 AbbrevToUse = SimpleGVarAbbrev;
1574 }
1575
1576 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
1577 Vals.clear();
1578 }
1579
1580 // Emit the function proto information.
1581 for (const Function &F : M) {
1582 // FUNCTION: [strtab offset, strtab size, type, callingconv, isproto,
1583 // linkage, paramattrs, alignment, section, visibility, gc,
1584 // unnamed_addr, prologuedata, dllstorageclass, comdat,
1585 // prefixdata, personalityfn, DSO_Local, addrspace]
1586 Vals.push_back(addToStrtab(F.getName()));
1587 Vals.push_back(F.getName().size());
1588 Vals.push_back(VE.getTypeID(F.getFunctionType()));
1589 Vals.push_back(F.getCallingConv());
1590 Vals.push_back(F.isDeclaration());
1592 Vals.push_back(VE.getAttributeListID(F.getAttributes()));
1593 Vals.push_back(getEncodedAlign(F.getAlign()));
1594 Vals.push_back(F.hasSection() ? SectionMap[std::string(F.getSection())]
1595 : 0);
1597 Vals.push_back(F.hasGC() ? GCMap[F.getGC()] : 0);
1599 Vals.push_back(F.hasPrologueData() ? (VE.getValueID(F.getPrologueData()) + 1)
1600 : 0);
1602 Vals.push_back(F.hasComdat() ? VE.getComdatID(F.getComdat()) : 0);
1603 Vals.push_back(F.hasPrefixData() ? (VE.getValueID(F.getPrefixData()) + 1)
1604 : 0);
1605 Vals.push_back(
1606 F.hasPersonalityFn() ? (VE.getValueID(F.getPersonalityFn()) + 1) : 0);
1607
1608 Vals.push_back(F.isDSOLocal());
1609 Vals.push_back(F.getAddressSpace());
1610 Vals.push_back(addToStrtab(F.getPartition()));
1611 Vals.push_back(F.getPartition().size());
1612
1613 unsigned AbbrevToUse = 0;
1614 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
1615 Vals.clear();
1616 }
1617
1618 // Emit the alias information.
1619 for (const GlobalAlias &A : M.aliases()) {
1620 // ALIAS: [strtab offset, strtab size, alias type, aliasee val#, linkage,
1621 // visibility, dllstorageclass, threadlocal, unnamed_addr,
1622 // DSO_Local]
1623 Vals.push_back(addToStrtab(A.getName()));
1624 Vals.push_back(A.getName().size());
1625 Vals.push_back(VE.getTypeID(A.getValueType()));
1626 Vals.push_back(A.getType()->getAddressSpace());
1627 Vals.push_back(VE.getValueID(A.getAliasee()));
1633 Vals.push_back(A.isDSOLocal());
1634 Vals.push_back(addToStrtab(A.getPartition()));
1635 Vals.push_back(A.getPartition().size());
1636
1637 unsigned AbbrevToUse = 0;
1638 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
1639 Vals.clear();
1640 }
1641
1642 // Emit the ifunc information.
1643 for (const GlobalIFunc &I : M.ifuncs()) {
1644 // IFUNC: [strtab offset, strtab size, ifunc type, address space, resolver
1645 // val#, linkage, visibility, DSO_Local]
1646 Vals.push_back(addToStrtab(I.getName()));
1647 Vals.push_back(I.getName().size());
1648 Vals.push_back(VE.getTypeID(I.getValueType()));
1649 Vals.push_back(I.getType()->getAddressSpace());
1650 Vals.push_back(VE.getValueID(I.getResolver()));
1653 Vals.push_back(I.isDSOLocal());
1654 Vals.push_back(addToStrtab(I.getPartition()));
1655 Vals.push_back(I.getPartition().size());
1656 Stream.EmitRecord(bitc::MODULE_CODE_IFUNC, Vals);
1657 Vals.clear();
1658 }
1659
1660 writeValueSymbolTableForwardDecl();
1661}
1662
1664 uint64_t Flags = 0;
1665
1666 if (const auto *OBO = dyn_cast<OverflowingBinaryOperator>(V)) {
1667 if (OBO->hasNoSignedWrap())
1668 Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP;
1669 if (OBO->hasNoUnsignedWrap())
1670 Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP;
1671 } else if (const auto *PEO = dyn_cast<PossiblyExactOperator>(V)) {
1672 if (PEO->isExact())
1673 Flags |= 1 << bitc::PEO_EXACT;
1674 } else if (const auto *PDI = dyn_cast<PossiblyDisjointInst>(V)) {
1675 if (PDI->isDisjoint())
1676 Flags |= 1 << bitc::PDI_DISJOINT;
1677 } else if (const auto *FPMO = dyn_cast<FPMathOperator>(V)) {
1678 if (FPMO->hasAllowReassoc())
1679 Flags |= bitc::AllowReassoc;
1680 if (FPMO->hasNoNaNs())
1681 Flags |= bitc::NoNaNs;
1682 if (FPMO->hasNoInfs())
1683 Flags |= bitc::NoInfs;
1684 if (FPMO->hasNoSignedZeros())
1685 Flags |= bitc::NoSignedZeros;
1686 if (FPMO->hasAllowReciprocal())
1687 Flags |= bitc::AllowReciprocal;
1688 if (FPMO->hasAllowContract())
1689 Flags |= bitc::AllowContract;
1690 if (FPMO->hasApproxFunc())
1691 Flags |= bitc::ApproxFunc;
1692 } else if (const auto *NNI = dyn_cast<PossiblyNonNegInst>(V)) {
1693 if (NNI->hasNonNeg())
1694 Flags |= 1 << bitc::PNNI_NON_NEG;
1695 } else if (const auto *TI = dyn_cast<TruncInst>(V)) {
1696 if (TI->hasNoSignedWrap())
1697 Flags |= 1 << bitc::TIO_NO_SIGNED_WRAP;
1698 if (TI->hasNoUnsignedWrap())
1699 Flags |= 1 << bitc::TIO_NO_UNSIGNED_WRAP;
1700 } else if (const auto *GEP = dyn_cast<GEPOperator>(V)) {
1701 if (GEP->isInBounds())
1702 Flags |= 1 << bitc::GEP_INBOUNDS;
1703 if (GEP->hasNoUnsignedSignedWrap())
1704 Flags |= 1 << bitc::GEP_NUSW;
1705 if (GEP->hasNoUnsignedWrap())
1706 Flags |= 1 << bitc::GEP_NUW;
1707 }
1708
1709 return Flags;
1710}
1711
1712void ModuleBitcodeWriter::writeValueAsMetadata(
1714 // Mimic an MDNode with a value as one operand.
1715 Value *V = MD->getValue();
1716 Record.push_back(VE.getTypeID(V->getType()));
1717 Record.push_back(VE.getValueID(V));
1719 Record.clear();
1720}
1721
1722void ModuleBitcodeWriter::writeMDTuple(const MDTuple *N,
1724 unsigned Abbrev) {
1725 for (const MDOperand &MDO : N->operands()) {
1726 Metadata *MD = MDO;
1727 assert(!(MD && isa<LocalAsMetadata>(MD)) &&
1728 "Unexpected function-local metadata");
1729 Record.push_back(VE.getMetadataOrNullID(MD));
1730 }
1731 Stream.EmitRecord(N->isDistinct() ? bitc::METADATA_DISTINCT_NODE
1733 Record, Abbrev);
1734 Record.clear();
1735}
1736
1737unsigned ModuleBitcodeWriter::createDILocationAbbrev() {
1738 // Assume the column is usually under 128, and always output the inlined-at
1739 // location (it's never more expensive than building an array size 1).
1740 auto Abbv = std::make_shared<BitCodeAbbrev>();
1748 return Stream.EmitAbbrev(std::move(Abbv));
1749}
1750
1751void ModuleBitcodeWriter::writeDILocation(const DILocation *N,
1753 unsigned &Abbrev) {
1754 if (!Abbrev)
1755 Abbrev = createDILocationAbbrev();
1756
1757 Record.push_back(N->isDistinct());
1758 Record.push_back(N->getLine());
1759 Record.push_back(N->getColumn());
1760 Record.push_back(VE.getMetadataID(N->getScope()));
1761 Record.push_back(VE.getMetadataOrNullID(N->getInlinedAt()));
1762 Record.push_back(N->isImplicitCode());
1763
1764 Stream.EmitRecord(bitc::METADATA_LOCATION, Record, Abbrev);
1765 Record.clear();
1766}
1767
1768unsigned ModuleBitcodeWriter::createGenericDINodeAbbrev() {
1769 // Assume the column is usually under 128, and always output the inlined-at
1770 // location (it's never more expensive than building an array size 1).
1771 auto Abbv = std::make_shared<BitCodeAbbrev>();
1779 return Stream.EmitAbbrev(std::move(Abbv));
1780}
1781
1782void ModuleBitcodeWriter::writeGenericDINode(const GenericDINode *N,
1784 unsigned &Abbrev) {
1785 if (!Abbrev)
1786 Abbrev = createGenericDINodeAbbrev();
1787
1788 Record.push_back(N->isDistinct());
1789 Record.push_back(N->getTag());
1790 Record.push_back(0); // Per-tag version field; unused for now.
1791
1792 for (auto &I : N->operands())
1793 Record.push_back(VE.getMetadataOrNullID(I));
1794
1796 Record.clear();
1797}
1798
1799void ModuleBitcodeWriter::writeDISubrange(const DISubrange *N,
1801 unsigned Abbrev) {
1802 const uint64_t Version = 2 << 1;
1803 Record.push_back((uint64_t)N->isDistinct() | Version);
1804 Record.push_back(VE.getMetadataOrNullID(N->getRawCountNode()));
1805 Record.push_back(VE.getMetadataOrNullID(N->getRawLowerBound()));
1806 Record.push_back(VE.getMetadataOrNullID(N->getRawUpperBound()));
1807 Record.push_back(VE.getMetadataOrNullID(N->getRawStride()));
1808
1809 Stream.EmitRecord(bitc::METADATA_SUBRANGE, Record, Abbrev);
1810 Record.clear();
1811}
1812
1813void ModuleBitcodeWriter::writeDIGenericSubrange(
1815 unsigned Abbrev) {
1816 Record.push_back((uint64_t)N->isDistinct());
1817 Record.push_back(VE.getMetadataOrNullID(N->getRawCountNode()));
1818 Record.push_back(VE.getMetadataOrNullID(N->getRawLowerBound()));
1819 Record.push_back(VE.getMetadataOrNullID(N->getRawUpperBound()));
1820 Record.push_back(VE.getMetadataOrNullID(N->getRawStride()));
1821
1823 Record.clear();
1824}
1825
1826void ModuleBitcodeWriter::writeDIEnumerator(const DIEnumerator *N,
1828 unsigned Abbrev) {
1829 const uint64_t IsBigInt = 1 << 2;
1830 Record.push_back(IsBigInt | (N->isUnsigned() << 1) | N->isDistinct());
1831 Record.push_back(N->getValue().getBitWidth());
1832 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1833 emitWideAPInt(Record, N->getValue());
1834
1836 Record.clear();
1837}
1838
1839void ModuleBitcodeWriter::writeDIBasicType(const DIBasicType *N,
1841 unsigned Abbrev) {
1842 Record.push_back(N->isDistinct());
1843 Record.push_back(N->getTag());
1844 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1845 Record.push_back(N->getSizeInBits());
1846 Record.push_back(N->getAlignInBits());
1847 Record.push_back(N->getEncoding());
1848 Record.push_back(N->getFlags());
1849
1851 Record.clear();
1852}
1853
1854void ModuleBitcodeWriter::writeDIStringType(const DIStringType *N,
1856 unsigned Abbrev) {
1857 Record.push_back(N->isDistinct());
1858 Record.push_back(N->getTag());
1859 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1860 Record.push_back(VE.getMetadataOrNullID(N->getStringLength()));
1861 Record.push_back(VE.getMetadataOrNullID(N->getStringLengthExp()));
1862 Record.push_back(VE.getMetadataOrNullID(N->getStringLocationExp()));
1863 Record.push_back(N->getSizeInBits());
1864 Record.push_back(N->getAlignInBits());
1865 Record.push_back(N->getEncoding());
1866
1868 Record.clear();
1869}
1870
1871void ModuleBitcodeWriter::writeDIDerivedType(const DIDerivedType *N,
1873 unsigned Abbrev) {
1874 Record.push_back(N->isDistinct());
1875 Record.push_back(N->getTag());
1876 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1877 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1878 Record.push_back(N->getLine());
1879 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1880 Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
1881 Record.push_back(N->getSizeInBits());
1882 Record.push_back(N->getAlignInBits());
1883 Record.push_back(N->getOffsetInBits());
1884 Record.push_back(N->getFlags());
1885 Record.push_back(VE.getMetadataOrNullID(N->getExtraData()));
1886
1887 // DWARF address space is encoded as N->getDWARFAddressSpace() + 1. 0 means
1888 // that there is no DWARF address space associated with DIDerivedType.
1889 if (const auto &DWARFAddressSpace = N->getDWARFAddressSpace())
1890 Record.push_back(*DWARFAddressSpace + 1);
1891 else
1892 Record.push_back(0);
1893
1894 Record.push_back(VE.getMetadataOrNullID(N->getAnnotations().get()));
1895
1896 if (auto PtrAuthData = N->getPtrAuthData())
1897 Record.push_back(PtrAuthData->RawData);
1898 else
1899 Record.push_back(0);
1900
1902 Record.clear();
1903}
1904
1905void ModuleBitcodeWriter::writeDICompositeType(
1907 unsigned Abbrev) {
1908 const unsigned IsNotUsedInOldTypeRef = 0x2;
1909 Record.push_back(IsNotUsedInOldTypeRef | (unsigned)N->isDistinct());
1910 Record.push_back(N->getTag());
1911 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1912 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1913 Record.push_back(N->getLine());
1914 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1915 Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
1916 Record.push_back(N->getSizeInBits());
1917 Record.push_back(N->getAlignInBits());
1918 Record.push_back(N->getOffsetInBits());
1919 Record.push_back(N->getFlags());
1920 Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
1921 Record.push_back(N->getRuntimeLang());
1922 Record.push_back(VE.getMetadataOrNullID(N->getVTableHolder()));
1923 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
1924 Record.push_back(VE.getMetadataOrNullID(N->getRawIdentifier()));
1925 Record.push_back(VE.getMetadataOrNullID(N->getDiscriminator()));
1926 Record.push_back(VE.getMetadataOrNullID(N->getRawDataLocation()));
1927 Record.push_back(VE.getMetadataOrNullID(N->getRawAssociated()));
1928 Record.push_back(VE.getMetadataOrNullID(N->getRawAllocated()));
1929 Record.push_back(VE.getMetadataOrNullID(N->getRawRank()));
1930 Record.push_back(VE.getMetadataOrNullID(N->getAnnotations().get()));
1931
1933 Record.clear();
1934}
1935
1936void ModuleBitcodeWriter::writeDISubroutineType(
1938 unsigned Abbrev) {
1939 const unsigned HasNoOldTypeRefs = 0x2;
1940 Record.push_back(HasNoOldTypeRefs | (unsigned)N->isDistinct());
1941 Record.push_back(N->getFlags());
1942 Record.push_back(VE.getMetadataOrNullID(N->getTypeArray().get()));
1943 Record.push_back(N->getCC());
1944
1946 Record.clear();
1947}
1948
1949void ModuleBitcodeWriter::writeDIFile(const DIFile *N,
1951 unsigned Abbrev) {
1952 Record.push_back(N->isDistinct());
1953 Record.push_back(VE.getMetadataOrNullID(N->getRawFilename()));
1954 Record.push_back(VE.getMetadataOrNullID(N->getRawDirectory()));
1955 if (N->getRawChecksum()) {
1956 Record.push_back(N->getRawChecksum()->Kind);
1957 Record.push_back(VE.getMetadataOrNullID(N->getRawChecksum()->Value));
1958 } else {
1959 // Maintain backwards compatibility with the old internal representation of
1960 // CSK_None in ChecksumKind by writing nulls here when Checksum is None.
1961 Record.push_back(0);
1962 Record.push_back(VE.getMetadataOrNullID(nullptr));
1963 }
1964 auto Source = N->getRawSource();
1965 if (Source)
1966 Record.push_back(VE.getMetadataOrNullID(Source));
1967
1968 Stream.EmitRecord(bitc::METADATA_FILE, Record, Abbrev);
1969 Record.clear();
1970}
1971
1972void ModuleBitcodeWriter::writeDICompileUnit(const DICompileUnit *N,
1974 unsigned Abbrev) {
1975 assert(N->isDistinct() && "Expected distinct compile units");
1976 Record.push_back(/* IsDistinct */ true);
1977 Record.push_back(N->getSourceLanguage());
1978 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1979 Record.push_back(VE.getMetadataOrNullID(N->getRawProducer()));
1980 Record.push_back(N->isOptimized());
1981 Record.push_back(VE.getMetadataOrNullID(N->getRawFlags()));
1982 Record.push_back(N->getRuntimeVersion());
1983 Record.push_back(VE.getMetadataOrNullID(N->getRawSplitDebugFilename()));
1984 Record.push_back(N->getEmissionKind());
1985 Record.push_back(VE.getMetadataOrNullID(N->getEnumTypes().get()));
1986 Record.push_back(VE.getMetadataOrNullID(N->getRetainedTypes().get()));
1987 Record.push_back(/* subprograms */ 0);
1988 Record.push_back(VE.getMetadataOrNullID(N->getGlobalVariables().get()));
1989 Record.push_back(VE.getMetadataOrNullID(N->getImportedEntities().get()));
1990 Record.push_back(N->getDWOId());
1991 Record.push_back(VE.getMetadataOrNullID(N->getMacros().get()));
1992 Record.push_back(N->getSplitDebugInlining());
1993 Record.push_back(N->getDebugInfoForProfiling());
1994 Record.push_back((unsigned)N->getNameTableKind());
1995 Record.push_back(N->getRangesBaseAddress());
1996 Record.push_back(VE.getMetadataOrNullID(N->getRawSysRoot()));
1997 Record.push_back(VE.getMetadataOrNullID(N->getRawSDK()));
1998
2000 Record.clear();
2001}
2002
2003void ModuleBitcodeWriter::writeDISubprogram(const DISubprogram *N,
2005 unsigned Abbrev) {
2006 const uint64_t HasUnitFlag = 1 << 1;
2007 const uint64_t HasSPFlagsFlag = 1 << 2;
2008 Record.push_back(uint64_t(N->isDistinct()) | HasUnitFlag | HasSPFlagsFlag);
2009 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
2010 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2011 Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
2012 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2013 Record.push_back(N->getLine());
2014 Record.push_back(VE.getMetadataOrNullID(N->getType()));
2015 Record.push_back(N->getScopeLine());
2016 Record.push_back(VE.getMetadataOrNullID(N->getContainingType()));
2017 Record.push_back(N->getSPFlags());
2018 Record.push_back(N->getVirtualIndex());
2019 Record.push_back(N->getFlags());
2020 Record.push_back(VE.getMetadataOrNullID(N->getRawUnit()));
2021 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
2022 Record.push_back(VE.getMetadataOrNullID(N->getDeclaration()));
2023 Record.push_back(VE.getMetadataOrNullID(N->getRetainedNodes().get()));
2024 Record.push_back(N->getThisAdjustment());
2025 Record.push_back(VE.getMetadataOrNullID(N->getThrownTypes().get()));
2026 Record.push_back(VE.getMetadataOrNullID(N->getAnnotations().get()));
2027 Record.push_back(VE.getMetadataOrNullID(N->getRawTargetFuncName()));
2028
2030 Record.clear();
2031}
2032
2033void ModuleBitcodeWriter::writeDILexicalBlock(const DILexicalBlock *N,
2035 unsigned Abbrev) {
2036 Record.push_back(N->isDistinct());
2037 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
2038 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2039 Record.push_back(N->getLine());
2040 Record.push_back(N->getColumn());
2041
2043 Record.clear();
2044}
2045
2046void ModuleBitcodeWriter::writeDILexicalBlockFile(
2048 unsigned Abbrev) {
2049 Record.push_back(N->isDistinct());
2050 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
2051 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2052 Record.push_back(N->getDiscriminator());
2053
2055 Record.clear();
2056}
2057
2058void ModuleBitcodeWriter::writeDICommonBlock(const DICommonBlock *N,
2060 unsigned Abbrev) {
2061 Record.push_back(N->isDistinct());
2062 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
2063 Record.push_back(VE.getMetadataOrNullID(N->getDecl()));
2064 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2065 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2066 Record.push_back(N->getLineNo());
2067
2069 Record.clear();
2070}
2071
2072void ModuleBitcodeWriter::writeDINamespace(const DINamespace *N,
2074 unsigned Abbrev) {
2075 Record.push_back(N->isDistinct() | N->getExportSymbols() << 1);
2076 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
2077 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2078
2080 Record.clear();
2081}
2082
2083void ModuleBitcodeWriter::writeDIMacro(const DIMacro *N,
2085 unsigned Abbrev) {
2086 Record.push_back(N->isDistinct());
2087 Record.push_back(N->getMacinfoType());
2088 Record.push_back(N->getLine());
2089 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2090 Record.push_back(VE.getMetadataOrNullID(N->getRawValue()));
2091
2092 Stream.EmitRecord(bitc::METADATA_MACRO, Record, Abbrev);
2093 Record.clear();
2094}
2095
2096void ModuleBitcodeWriter::writeDIMacroFile(const DIMacroFile *N,
2098 unsigned Abbrev) {
2099 Record.push_back(N->isDistinct());
2100 Record.push_back(N->getMacinfoType());
2101 Record.push_back(N->getLine());
2102 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2103 Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
2104
2106 Record.clear();
2107}
2108
2109void ModuleBitcodeWriter::writeDIArgList(const DIArgList *N,
2111 Record.reserve(N->getArgs().size());
2112 for (ValueAsMetadata *MD : N->getArgs())
2113 Record.push_back(VE.getMetadataID(MD));
2114
2116 Record.clear();
2117}
2118
2119void ModuleBitcodeWriter::writeDIModule(const DIModule *N,
2121 unsigned Abbrev) {
2122 Record.push_back(N->isDistinct());
2123 for (auto &I : N->operands())
2124 Record.push_back(VE.getMetadataOrNullID(I));
2125 Record.push_back(N->getLineNo());
2126 Record.push_back(N->getIsDecl());
2127
2128 Stream.EmitRecord(bitc::METADATA_MODULE, Record, Abbrev);
2129 Record.clear();
2130}
2131
2132void ModuleBitcodeWriter::writeDIAssignID(const DIAssignID *N,
2134 unsigned Abbrev) {
2135 // There are no arguments for this metadata type.
2136 Record.push_back(N->isDistinct());
2138 Record.clear();
2139}
2140
2141void ModuleBitcodeWriter::writeDITemplateTypeParameter(
2143 unsigned Abbrev) {
2144 Record.push_back(N->isDistinct());
2145 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2146 Record.push_back(VE.getMetadataOrNullID(N->getType()));
2147 Record.push_back(N->isDefault());
2148
2150 Record.clear();
2151}
2152
2153void ModuleBitcodeWriter::writeDITemplateValueParameter(
2155 unsigned Abbrev) {
2156 Record.push_back(N->isDistinct());
2157 Record.push_back(N->getTag());
2158 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2159 Record.push_back(VE.getMetadataOrNullID(N->getType()));
2160 Record.push_back(N->isDefault());
2161 Record.push_back(VE.getMetadataOrNullID(N->getValue()));
2162
2164 Record.clear();
2165}
2166
2167void ModuleBitcodeWriter::writeDIGlobalVariable(
2169 unsigned Abbrev) {
2170 const uint64_t Version = 2 << 1;
2171 Record.push_back((uint64_t)N->isDistinct() | Version);
2172 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
2173 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2174 Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
2175 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2176 Record.push_back(N->getLine());
2177 Record.push_back(VE.getMetadataOrNullID(N->getType()));
2178 Record.push_back(N->isLocalToUnit());
2179 Record.push_back(N->isDefinition());
2180 Record.push_back(VE.getMetadataOrNullID(N->getStaticDataMemberDeclaration()));
2181 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams()));
2182 Record.push_back(N->getAlignInBits());
2183 Record.push_back(VE.getMetadataOrNullID(N->getAnnotations().get()));
2184
2186 Record.clear();
2187}
2188
2189void ModuleBitcodeWriter::writeDILocalVariable(
2191 unsigned Abbrev) {
2192 // In order to support all possible bitcode formats in BitcodeReader we need
2193 // to distinguish the following cases:
2194 // 1) Record has no artificial tag (Record[1]),
2195 // has no obsolete inlinedAt field (Record[9]).
2196 // In this case Record size will be 8, HasAlignment flag is false.
2197 // 2) Record has artificial tag (Record[1]),
2198 // has no obsolete inlignedAt field (Record[9]).
2199 // In this case Record size will be 9, HasAlignment flag is false.
2200 // 3) Record has both artificial tag (Record[1]) and
2201 // obsolete inlignedAt field (Record[9]).
2202 // In this case Record size will be 10, HasAlignment flag is false.
2203 // 4) Record has neither artificial tag, nor inlignedAt field, but
2204 // HasAlignment flag is true and Record[8] contains alignment value.
2205 const uint64_t HasAlignmentFlag = 1 << 1;
2206 Record.push_back((uint64_t)N->isDistinct() | HasAlignmentFlag);
2207 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
2208 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2209 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2210 Record.push_back(N->getLine());
2211 Record.push_back(VE.getMetadataOrNullID(N->getType()));
2212 Record.push_back(N->getArg());
2213 Record.push_back(N->getFlags());
2214 Record.push_back(N->getAlignInBits());
2215 Record.push_back(VE.getMetadataOrNullID(N->getAnnotations().get()));
2216
2218 Record.clear();
2219}
2220
2221void ModuleBitcodeWriter::writeDILabel(
2223 unsigned Abbrev) {
2224 Record.push_back((uint64_t)N->isDistinct());
2225 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
2226 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2227 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2228 Record.push_back(N->getLine());
2229
2230 Stream.EmitRecord(bitc::METADATA_LABEL, Record, Abbrev);
2231 Record.clear();
2232}
2233
2234void ModuleBitcodeWriter::writeDIExpression(const DIExpression *N,
2236 unsigned Abbrev) {
2237 Record.reserve(N->getElements().size() + 1);
2238 const uint64_t Version = 3 << 1;
2239 Record.push_back((uint64_t)N->isDistinct() | Version);
2240 Record.append(N->elements_begin(), N->elements_end());
2241
2243 Record.clear();
2244}
2245
2246void ModuleBitcodeWriter::writeDIGlobalVariableExpression(
2248 unsigned Abbrev) {
2249 Record.push_back(N->isDistinct());
2250 Record.push_back(VE.getMetadataOrNullID(N->getVariable()));
2251 Record.push_back(VE.getMetadataOrNullID(N->getExpression()));
2252
2254 Record.clear();
2255}
2256
2257void ModuleBitcodeWriter::writeDIObjCProperty(const DIObjCProperty *N,
2259 unsigned Abbrev) {
2260 Record.push_back(N->isDistinct());
2261 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2262 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2263 Record.push_back(N->getLine());
2264 Record.push_back(VE.getMetadataOrNullID(N->getRawSetterName()));
2265 Record.push_back(VE.getMetadataOrNullID(N->getRawGetterName()));
2266 Record.push_back(N->getAttributes());
2267 Record.push_back(VE.getMetadataOrNullID(N->getType()));
2268
2270 Record.clear();
2271}
2272
2273void ModuleBitcodeWriter::writeDIImportedEntity(
2275 unsigned Abbrev) {
2276 Record.push_back(N->isDistinct());
2277 Record.push_back(N->getTag());
2278 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
2279 Record.push_back(VE.getMetadataOrNullID(N->getEntity()));
2280 Record.push_back(N->getLine());
2281 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2282 Record.push_back(VE.getMetadataOrNullID(N->getRawFile()));
2283 Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
2284
2286 Record.clear();
2287}
2288
2289unsigned ModuleBitcodeWriter::createNamedMetadataAbbrev() {
2290 auto Abbv = std::make_shared<BitCodeAbbrev>();
2294 return Stream.EmitAbbrev(std::move(Abbv));
2295}
2296
2297void ModuleBitcodeWriter::writeNamedMetadata(
2299 if (M.named_metadata_empty())
2300 return;
2301
2302 unsigned Abbrev = createNamedMetadataAbbrev();
2303 for (const NamedMDNode &NMD : M.named_metadata()) {
2304 // Write name.
2305 StringRef Str = NMD.getName();
2306 Record.append(Str.bytes_begin(), Str.bytes_end());
2307 Stream.EmitRecord(bitc::METADATA_NAME, Record, Abbrev);
2308 Record.clear();
2309
2310 // Write named metadata operands.
2311 for (const MDNode *N : NMD.operands())
2312 Record.push_back(VE.getMetadataID(N));
2314 Record.clear();
2315 }
2316}
2317
2318unsigned ModuleBitcodeWriter::createMetadataStringsAbbrev() {
2319 auto Abbv = std::make_shared<BitCodeAbbrev>();
2321 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of strings
2322 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // offset to chars
2324 return Stream.EmitAbbrev(std::move(Abbv));
2325}
2326
2327/// Write out a record for MDString.
2328///
2329/// All the metadata strings in a metadata block are emitted in a single
2330/// record. The sizes and strings themselves are shoved into a blob.
2331void ModuleBitcodeWriter::writeMetadataStrings(
2333 if (Strings.empty())
2334 return;
2335
2336 // Start the record with the number of strings.
2337 Record.push_back(bitc::METADATA_STRINGS);
2338 Record.push_back(Strings.size());
2339
2340 // Emit the sizes of the strings in the blob.
2341 SmallString<256> Blob;
2342 {
2343 BitstreamWriter W(Blob);
2344 for (const Metadata *MD : Strings)
2345 W.EmitVBR(cast<MDString>(MD)->getLength(), 6);
2346 W.FlushToWord();
2347 }
2348
2349 // Add the offset to the strings to the record.
2350 Record.push_back(Blob.size());
2351
2352 // Add the strings to the blob.
2353 for (const Metadata *MD : Strings)
2354 Blob.append(cast<MDString>(MD)->getString());
2355
2356 // Emit the final record.
2357 Stream.EmitRecordWithBlob(createMetadataStringsAbbrev(), Record, Blob);
2358 Record.clear();
2359}
2360
2361// Generates an enum to use as an index in the Abbrev array of Metadata record.
2362enum MetadataAbbrev : unsigned {
2363#define HANDLE_MDNODE_LEAF(CLASS) CLASS##AbbrevID,
2364#include "llvm/IR/Metadata.def"
2367
2368void ModuleBitcodeWriter::writeMetadataRecords(
2370 std::vector<unsigned> *MDAbbrevs, std::vector<uint64_t> *IndexPos) {
2371 if (MDs.empty())
2372 return;
2373
2374 // Initialize MDNode abbreviations.
2375#define HANDLE_MDNODE_LEAF(CLASS) unsigned CLASS##Abbrev = 0;
2376#include "llvm/IR/Metadata.def"
2377
2378 for (const Metadata *MD : MDs) {
2379 if (IndexPos)
2380 IndexPos->push_back(Stream.GetCurrentBitNo());
2381 if (const MDNode *N = dyn_cast<MDNode>(MD)) {
2382 assert(N->isResolved() && "Expected forward references to be resolved");
2383
2384 switch (N->getMetadataID()) {
2385 default:
2386 llvm_unreachable("Invalid MDNode subclass");
2387#define HANDLE_MDNODE_LEAF(CLASS) \
2388 case Metadata::CLASS##Kind: \
2389 if (MDAbbrevs) \
2390 write##CLASS(cast<CLASS>(N), Record, \
2391 (*MDAbbrevs)[MetadataAbbrev::CLASS##AbbrevID]); \
2392 else \
2393 write##CLASS(cast<CLASS>(N), Record, CLASS##Abbrev); \
2394 continue;
2395#include "llvm/IR/Metadata.def"
2396 }
2397 }
2398 if (auto *AL = dyn_cast<DIArgList>(MD)) {
2400 continue;
2401 }
2402 writeValueAsMetadata(cast<ValueAsMetadata>(MD), Record);
2403 }
2404}
2405
2406void ModuleBitcodeWriter::writeModuleMetadata() {
2407 if (!VE.hasMDs() && M.named_metadata_empty())
2408 return;
2409
2412
2413 // Emit all abbrevs upfront, so that the reader can jump in the middle of the
2414 // block and load any metadata.
2415 std::vector<unsigned> MDAbbrevs;
2416
2417 MDAbbrevs.resize(MetadataAbbrev::LastPlusOne);
2418 MDAbbrevs[MetadataAbbrev::DILocationAbbrevID] = createDILocationAbbrev();
2419 MDAbbrevs[MetadataAbbrev::GenericDINodeAbbrevID] =
2420 createGenericDINodeAbbrev();
2421
2422 auto Abbv = std::make_shared<BitCodeAbbrev>();
2426 unsigned OffsetAbbrev = Stream.EmitAbbrev(std::move(Abbv));
2427
2428 Abbv = std::make_shared<BitCodeAbbrev>();
2432 unsigned IndexAbbrev = Stream.EmitAbbrev(std::move(Abbv));
2433
2434 // Emit MDStrings together upfront.
2435 writeMetadataStrings(VE.getMDStrings(), Record);
2436
2437 // We only emit an index for the metadata record if we have more than a given
2438 // (naive) threshold of metadatas, otherwise it is not worth it.
2439 if (VE.getNonMDStrings().size() > IndexThreshold) {
2440 // Write a placeholder value in for the offset of the metadata index,
2441 // which is written after the records, so that it can include
2442 // the offset of each entry. The placeholder offset will be
2443 // updated after all records are emitted.
2444 uint64_t Vals[] = {0, 0};
2445 Stream.EmitRecord(bitc::METADATA_INDEX_OFFSET, Vals, OffsetAbbrev);
2446 }
2447
2448 // Compute and save the bit offset to the current position, which will be
2449 // patched when we emit the index later. We can simply subtract the 64-bit
2450 // fixed size from the current bit number to get the location to backpatch.
2451 uint64_t IndexOffsetRecordBitPos = Stream.GetCurrentBitNo();
2452
2453 // This index will contain the bitpos for each individual record.
2454 std::vector<uint64_t> IndexPos;
2455 IndexPos.reserve(VE.getNonMDStrings().size());
2456
2457 // Write all the records
2458 writeMetadataRecords(VE.getNonMDStrings(), Record, &MDAbbrevs, &IndexPos);
2459
2460 if (VE.getNonMDStrings().size() > IndexThreshold) {
2461 // Now that we have emitted all the records we will emit the index. But
2462 // first
2463 // backpatch the forward reference so that the reader can skip the records
2464 // efficiently.
2465 Stream.BackpatchWord64(IndexOffsetRecordBitPos - 64,
2466 Stream.GetCurrentBitNo() - IndexOffsetRecordBitPos);
2467
2468 // Delta encode the index.
2469 uint64_t PreviousValue = IndexOffsetRecordBitPos;
2470 for (auto &Elt : IndexPos) {
2471 auto EltDelta = Elt - PreviousValue;
2472 PreviousValue = Elt;
2473 Elt = EltDelta;
2474 }
2475 // Emit the index record.
2476 Stream.EmitRecord(bitc::METADATA_INDEX, IndexPos, IndexAbbrev);
2477 IndexPos.clear();
2478 }
2479
2480 // Write the named metadata now.
2481 writeNamedMetadata(Record);
2482
2483 auto AddDeclAttachedMetadata = [&](const GlobalObject &GO) {
2485 Record.push_back(VE.getValueID(&GO));
2486 pushGlobalMetadataAttachment(Record, GO);
2488 };
2489 for (const Function &F : M)
2490 if (F.isDeclaration() && F.hasMetadata())
2491 AddDeclAttachedMetadata(F);
2492 // FIXME: Only store metadata for declarations here, and move data for global
2493 // variable definitions to a separate block (PR28134).
2494 for (const GlobalVariable &GV : M.globals())
2495 if (GV.hasMetadata())
2496 AddDeclAttachedMetadata(GV);
2497
2498 Stream.ExitBlock();
2499}
2500
2501void ModuleBitcodeWriter::writeFunctionMetadata(const Function &F) {
2502 if (!VE.hasMDs())
2503 return;
2504
2507 writeMetadataStrings(VE.getMDStrings(), Record);
2508 writeMetadataRecords(VE.getNonMDStrings(), Record);
2509 Stream.ExitBlock();
2510}
2511
2512void ModuleBitcodeWriter::pushGlobalMetadataAttachment(
2514 // [n x [id, mdnode]]
2516 GO.getAllMetadata(MDs);
2517 for (const auto &I : MDs) {
2518 Record.push_back(I.first);
2519 Record.push_back(VE.getMetadataID(I.second));
2520 }
2521}
2522
2523void ModuleBitcodeWriter::writeFunctionMetadataAttachment(const Function &F) {
2525
2527
2528 if (F.hasMetadata()) {
2529 pushGlobalMetadataAttachment(Record, F);
2531 Record.clear();
2532 }
2533
2534 // Write metadata attachments
2535 // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]]
2537 for (const BasicBlock &BB : F)
2538 for (const Instruction &I : BB) {
2539 MDs.clear();
2540 I.getAllMetadataOtherThanDebugLoc(MDs);
2541
2542 // If no metadata, ignore instruction.
2543 if (MDs.empty()) continue;
2544
2545 Record.push_back(VE.getInstructionID(&I));
2546
2547 for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
2548 Record.push_back(MDs[i].first);
2549 Record.push_back(VE.getMetadataID(MDs[i].second));
2550 }
2552 Record.clear();
2553 }
2554
2555 Stream.ExitBlock();
2556}
2557
2558void ModuleBitcodeWriter::writeModuleMetadataKinds() {
2560
2561 // Write metadata kinds
2562 // METADATA_KIND - [n x [id, name]]
2564 M.getMDKindNames(Names);
2565
2566 if (Names.empty()) return;
2567
2569
2570 for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) {
2571 Record.push_back(MDKindID);
2572 StringRef KName = Names[MDKindID];
2573 Record.append(KName.begin(), KName.end());
2574
2576 Record.clear();
2577 }
2578
2579 Stream.ExitBlock();
2580}
2581
2582void ModuleBitcodeWriter::writeOperandBundleTags() {
2583 // Write metadata kinds
2584 //
2585 // OPERAND_BUNDLE_TAGS_BLOCK_ID : N x OPERAND_BUNDLE_TAG
2586 //
2587 // OPERAND_BUNDLE_TAG - [strchr x N]
2588
2590 M.getOperandBundleTags(Tags);
2591
2592 if (Tags.empty())
2593 return;
2594
2596
2598
2599 for (auto Tag : Tags) {
2600 Record.append(Tag.begin(), Tag.end());
2601
2603 Record.clear();
2604 }
2605
2606 Stream.ExitBlock();
2607}
2608
2609void ModuleBitcodeWriter::writeSyncScopeNames() {
2611 M.getContext().getSyncScopeNames(SSNs);
2612 if (SSNs.empty())
2613 return;
2614
2616
2618 for (auto SSN : SSNs) {
2619 Record.append(SSN.begin(), SSN.end());
2621 Record.clear();
2622 }
2623
2624 Stream.ExitBlock();
2625}
2626
2627void ModuleBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal,
2628 bool isGlobal) {
2629 if (FirstVal == LastVal) return;
2630
2632
2633 unsigned AggregateAbbrev = 0;
2634 unsigned String8Abbrev = 0;
2635 unsigned CString7Abbrev = 0;
2636 unsigned CString6Abbrev = 0;
2637 // If this is a constant pool for the module, emit module-specific abbrevs.
2638 if (isGlobal) {
2639 // Abbrev for CST_CODE_AGGREGATE.
2640 auto Abbv = std::make_shared<BitCodeAbbrev>();
2643 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
2644 AggregateAbbrev = Stream.EmitAbbrev(std::move(Abbv));
2645
2646 // Abbrev for CST_CODE_STRING.
2647 Abbv = std::make_shared<BitCodeAbbrev>();
2651 String8Abbrev = Stream.EmitAbbrev(std::move(Abbv));
2652 // Abbrev for CST_CODE_CSTRING.
2653 Abbv = std::make_shared<BitCodeAbbrev>();
2657 CString7Abbrev = Stream.EmitAbbrev(std::move(Abbv));
2658 // Abbrev for CST_CODE_CSTRING.
2659 Abbv = std::make_shared<BitCodeAbbrev>();
2663 CString6Abbrev = Stream.EmitAbbrev(std::move(Abbv));
2664 }
2665
2667
2668 const ValueEnumerator::ValueList &Vals = VE.getValues();
2669 Type *LastTy = nullptr;
2670 for (unsigned i = FirstVal; i != LastVal; ++i) {
2671 const Value *V = Vals[i].first;
2672 // If we need to switch types, do so now.
2673 if (V->getType() != LastTy) {
2674 LastTy = V->getType();
2675 Record.push_back(VE.getTypeID(LastTy));
2677 CONSTANTS_SETTYPE_ABBREV);
2678 Record.clear();
2679 }
2680
2681 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
2682 Record.push_back(VE.getTypeID(IA->getFunctionType()));
2683 Record.push_back(
2684 unsigned(IA->hasSideEffects()) | unsigned(IA->isAlignStack()) << 1 |
2685 unsigned(IA->getDialect() & 1) << 2 | unsigned(IA->canThrow()) << 3);
2686
2687 // Add the asm string.
2688 const std::string &AsmStr = IA->getAsmString();
2689 Record.push_back(AsmStr.size());
2690 Record.append(AsmStr.begin(), AsmStr.end());
2691
2692 // Add the constraint string.
2693 const std::string &ConstraintStr = IA->getConstraintString();
2694 Record.push_back(ConstraintStr.size());
2695 Record.append(ConstraintStr.begin(), ConstraintStr.end());
2697 Record.clear();
2698 continue;
2699 }
2700 const Constant *C = cast<Constant>(V);
2701 unsigned Code = -1U;
2702 unsigned AbbrevToUse = 0;
2703 if (C->isNullValue()) {
2705 } else if (isa<PoisonValue>(C)) {
2707 } else if (isa<UndefValue>(C)) {
2709 } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
2710 if (IV->getBitWidth() <= 64) {
2711 uint64_t V = IV->getSExtValue();
2714 AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
2715 } else { // Wide integers, > 64 bits in size.
2716 emitWideAPInt(Record, IV->getValue());
2718 }
2719 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
2721 Type *Ty = CFP->getType()->getScalarType();
2722 if (Ty->isHalfTy() || Ty->isBFloatTy() || Ty->isFloatTy() ||
2723 Ty->isDoubleTy()) {
2724 Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
2725 } else if (Ty->isX86_FP80Ty()) {
2726 // api needed to prevent premature destruction
2727 // bits are not in the same order as a normal i80 APInt, compensate.
2728 APInt api = CFP->getValueAPF().bitcastToAPInt();
2729 const uint64_t *p = api.getRawData();
2730 Record.push_back((p[1] << 48) | (p[0] >> 16));
2731 Record.push_back(p[0] & 0xffffLL);
2732 } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) {
2733 APInt api = CFP->getValueAPF().bitcastToAPInt();
2734 const uint64_t *p = api.getRawData();
2735 Record.push_back(p[0]);
2736 Record.push_back(p[1]);
2737 } else {
2738 assert(0 && "Unknown FP type!");
2739 }
2740 } else if (isa<ConstantDataSequential>(C) &&
2741 cast<ConstantDataSequential>(C)->isString()) {
2742 const ConstantDataSequential *Str = cast<ConstantDataSequential>(C);
2743 // Emit constant strings specially.
2744 unsigned NumElts = Str->getNumElements();
2745 // If this is a null-terminated string, use the denser CSTRING encoding.
2746 if (Str->isCString()) {
2748 --NumElts; // Don't encode the null, which isn't allowed by char6.
2749 } else {
2751 AbbrevToUse = String8Abbrev;
2752 }
2753 bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
2754 bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
2755 for (unsigned i = 0; i != NumElts; ++i) {
2756 unsigned char V = Str->getElementAsInteger(i);
2757 Record.push_back(V);
2758 isCStr7 &= (V & 128) == 0;
2759 if (isCStrChar6)
2760 isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
2761 }
2762
2763 if (isCStrChar6)
2764 AbbrevToUse = CString6Abbrev;
2765 else if (isCStr7)
2766 AbbrevToUse = CString7Abbrev;
2767 } else if (const ConstantDataSequential *CDS =
2768 dyn_cast<ConstantDataSequential>(C)) {
2770 Type *EltTy = CDS->getElementType();
2771 if (isa<IntegerType>(EltTy)) {
2772 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
2773 Record.push_back(CDS->getElementAsInteger(i));
2774 } else {
2775 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
2776 Record.push_back(
2777 CDS->getElementAsAPFloat(i).bitcastToAPInt().getLimitedValue());
2778 }
2779 } else if (isa<ConstantAggregate>(C)) {
2781 for (const Value *Op : C->operands())
2782 Record.push_back(VE.getValueID(Op));
2783 AbbrevToUse = AggregateAbbrev;
2784 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
2785 switch (CE->getOpcode()) {
2786 default:
2787 if (Instruction::isCast(CE->getOpcode())) {
2789 Record.push_back(getEncodedCastOpcode(CE->getOpcode()));
2790 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
2791 Record.push_back(VE.getValueID(C->getOperand(0)));
2792 AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
2793 } else {
2794 assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
2796 Record.push_back(getEncodedBinaryOpcode(CE->getOpcode()));
2797 Record.push_back(VE.getValueID(C->getOperand(0)));
2798 Record.push_back(VE.getValueID(C->getOperand(1)));
2800 if (Flags != 0)
2801 Record.push_back(Flags);
2802 }
2803 break;
2804 case Instruction::FNeg: {
2805 assert(CE->getNumOperands() == 1 && "Unknown constant expr!");
2807 Record.push_back(getEncodedUnaryOpcode(CE->getOpcode()));
2808 Record.push_back(VE.getValueID(C->getOperand(0)));
2810 if (Flags != 0)
2811 Record.push_back(Flags);
2812 break;
2813 }
2814 case Instruction::GetElementPtr: {
2816 const auto *GO = cast<GEPOperator>(C);
2817 Record.push_back(VE.getTypeID(GO->getSourceElementType()));
2818 Record.push_back(getOptimizationFlags(GO));
2819 if (std::optional<ConstantRange> Range = GO->getInRange()) {
2821 emitConstantRange(Record, *Range, /*EmitBitWidth=*/true);
2822 }
2823 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
2824 Record.push_back(VE.getTypeID(C->getOperand(i)->getType()));
2825 Record.push_back(VE.getValueID(C->getOperand(i)));
2826 }
2827 break;
2828 }
2829 case Instruction::ExtractElement:
2831 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
2832 Record.push_back(VE.getValueID(C->getOperand(0)));
2833 Record.push_back(VE.getTypeID(C->getOperand(1)->getType()));
2834 Record.push_back(VE.getValueID(C->getOperand(1)));
2835 break;
2836 case Instruction::InsertElement:
2838 Record.push_back(VE.getValueID(C->getOperand(0)));
2839 Record.push_back(VE.getValueID(C->getOperand(1)));
2840 Record.push_back(VE.getTypeID(C->getOperand(2)->getType()));
2841 Record.push_back(VE.getValueID(C->getOperand(2)));
2842 break;
2843 case Instruction::ShuffleVector:
2844 // If the return type and argument types are the same, this is a
2845 // standard shufflevector instruction. If the types are different,
2846 // then the shuffle is widening or truncating the input vectors, and
2847 // the argument type must also be encoded.
2848 if (C->getType() == C->getOperand(0)->getType()) {
2850 } else {
2852 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
2853 }
2854 Record.push_back(VE.getValueID(C->getOperand(0)));
2855 Record.push_back(VE.getValueID(C->getOperand(1)));
2856 Record.push_back(VE.getValueID(CE->getShuffleMaskForBitcode()));
2857 break;
2858 }
2859 } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
2861 Record.push_back(VE.getTypeID(BA->getFunction()->getType()));
2862 Record.push_back(VE.getValueID(BA->getFunction()));
2863 Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock()));
2864 } else if (const auto *Equiv = dyn_cast<DSOLocalEquivalent>(C)) {
2866 Record.push_back(VE.getTypeID(Equiv->getGlobalValue()->getType()));
2867 Record.push_back(VE.getValueID(Equiv->getGlobalValue()));
2868 } else if (const auto *NC = dyn_cast<NoCFIValue>(C)) {
2870 Record.push_back(VE.getTypeID(NC->getGlobalValue()->getType()));
2871 Record.push_back(VE.getValueID(NC->getGlobalValue()));
2872 } else if (const auto *CPA = dyn_cast<ConstantPtrAuth>(C)) {
2874 Record.push_back(VE.getValueID(CPA->getPointer()));
2875 Record.push_back(VE.getValueID(CPA->getKey()));
2876 Record.push_back(VE.getValueID(CPA->getDiscriminator()));
2877 Record.push_back(VE.getValueID(CPA->getAddrDiscriminator()));
2878 } else {
2879#ifndef NDEBUG
2880 C->dump();
2881#endif
2882 llvm_unreachable("Unknown constant!");
2883 }
2884 Stream.EmitRecord(Code, Record, AbbrevToUse);
2885 Record.clear();
2886 }
2887
2888 Stream.ExitBlock();
2889}
2890
2891void ModuleBitcodeWriter::writeModuleConstants() {
2892 const ValueEnumerator::ValueList &Vals = VE.getValues();
2893
2894 // Find the first constant to emit, which is the first non-globalvalue value.
2895 // We know globalvalues have been emitted by WriteModuleInfo.
2896 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
2897 if (!isa<GlobalValue>(Vals[i].first)) {
2898 writeConstants(i, Vals.size(), true);
2899 return;
2900 }
2901 }
2902}
2903
2904/// pushValueAndType - The file has to encode both the value and type id for
2905/// many values, because we need to know what type to create for forward
2906/// references. However, most operands are not forward references, so this type
2907/// field is not needed.
2908///
2909/// This function adds V's value ID to Vals. If the value ID is higher than the
2910/// instruction ID, then it is a forward reference, and it also includes the
2911/// type ID. The value ID that is written is encoded relative to the InstID.
2912bool ModuleBitcodeWriter::pushValueAndType(const Value *V, unsigned InstID,
2914 unsigned ValID = VE.getValueID(V);
2915 // Make encoding relative to the InstID.
2916 Vals.push_back(InstID - ValID);
2917 if (ValID >= InstID) {
2918 Vals.push_back(VE.getTypeID(V->getType()));
2919 return true;
2920 }
2921 return false;
2922}
2923
2924void ModuleBitcodeWriter::writeOperandBundles(const CallBase &CS,
2925 unsigned InstID) {
2927 LLVMContext &C = CS.getContext();
2928
2929 for (unsigned i = 0, e = CS.getNumOperandBundles(); i != e; ++i) {
2930 const auto &Bundle = CS.getOperandBundleAt(i);
2931 Record.push_back(C.getOperandBundleTagID(Bundle.getTagName()));
2932
2933 for (auto &Input : Bundle.Inputs)
2934 pushValueAndType(Input, InstID, Record);
2935
2937 Record.clear();
2938 }
2939}
2940
2941/// pushValue - Like pushValueAndType, but where the type of the value is
2942/// omitted (perhaps it was already encoded in an earlier operand).
2943void ModuleBitcodeWriter::pushValue(const Value *V, unsigned InstID,
2945 unsigned ValID = VE.getValueID(V);
2946 Vals.push_back(InstID - ValID);
2947}
2948
2949void ModuleBitcodeWriter::pushValueSigned(const Value *V, unsigned InstID,
2951 unsigned ValID = VE.getValueID(V);
2952 int64_t diff = ((int32_t)InstID - (int32_t)ValID);
2953 emitSignedInt64(Vals, diff);
2954}
2955
2956/// WriteInstruction - Emit an instruction to the specified stream.
2957void ModuleBitcodeWriter::writeInstruction(const Instruction &I,
2958 unsigned InstID,
2960 unsigned Code = 0;
2961 unsigned AbbrevToUse = 0;
2962 VE.setInstructionID(&I);
2963 switch (I.getOpcode()) {
2964 default:
2965 if (Instruction::isCast(I.getOpcode())) {
2967 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
2968 AbbrevToUse = FUNCTION_INST_CAST_ABBREV;
2969 Vals.push_back(VE.getTypeID(I.getType()));
2970 Vals.push_back(getEncodedCastOpcode(I.getOpcode()));
2972 if (Flags != 0) {
2973 if (AbbrevToUse == FUNCTION_INST_CAST_ABBREV)
2974 AbbrevToUse = FUNCTION_INST_CAST_FLAGS_ABBREV;
2975 Vals.push_back(Flags);
2976 }
2977 } else {
2978 assert(isa<BinaryOperator>(I) && "Unknown instruction!");
2980 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
2981 AbbrevToUse = FUNCTION_INST_BINOP_ABBREV;
2982 pushValue(I.getOperand(1), InstID, Vals);
2983 Vals.push_back(getEncodedBinaryOpcode(I.getOpcode()));
2985 if (Flags != 0) {
2986 if (AbbrevToUse == FUNCTION_INST_BINOP_ABBREV)
2987 AbbrevToUse = FUNCTION_INST_BINOP_FLAGS_ABBREV;
2988 Vals.push_back(Flags);
2989 }
2990 }
2991 break;
2992 case Instruction::FNeg: {
2994 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
2995 AbbrevToUse = FUNCTION_INST_UNOP_ABBREV;
2996 Vals.push_back(getEncodedUnaryOpcode(I.getOpcode()));
2998 if (Flags != 0) {
2999 if (AbbrevToUse == FUNCTION_INST_UNOP_ABBREV)
3000 AbbrevToUse = FUNCTION_INST_UNOP_FLAGS_ABBREV;
3001 Vals.push_back(Flags);
3002 }
3003 break;
3004 }
3005 case Instruction::GetElementPtr: {
3007 AbbrevToUse = FUNCTION_INST_GEP_ABBREV;
3008 auto &GEPInst = cast<GetElementPtrInst>(I);
3010 Vals.push_back(VE.getTypeID(GEPInst.getSourceElementType()));
3011 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
3012 pushValueAndType(I.getOperand(i), InstID, Vals);
3013 break;
3014 }
3015 case Instruction::ExtractValue: {
3017 pushValueAndType(I.getOperand(0), InstID, Vals);
3018 const ExtractValueInst *EVI = cast<ExtractValueInst>(&I);
3019 Vals.append(EVI->idx_begin(), EVI->idx_end());
3020 break;
3021 }
3022 case Instruction::InsertValue: {
3024 pushValueAndType(I.getOperand(0), InstID, Vals);
3025 pushValueAndType(I.getOperand(1), InstID, Vals);
3026 const InsertValueInst *IVI = cast<InsertValueInst>(&I);
3027 Vals.append(IVI->idx_begin(), IVI->idx_end());
3028 break;
3029 }
3030 case Instruction::Select: {
3032 pushValueAndType(I.getOperand(1), InstID, Vals);
3033 pushValue(I.getOperand(2), InstID, Vals);
3034 pushValueAndType(I.getOperand(0), InstID, Vals);
3036 if (Flags != 0)
3037 Vals.push_back(Flags);
3038 break;
3039 }
3040 case Instruction::ExtractElement:
3042 pushValueAndType(I.getOperand(0), InstID, Vals);
3043 pushValueAndType(I.getOperand(1), InstID, Vals);
3044 break;
3045 case Instruction::InsertElement:
3047 pushValueAndType(I.getOperand(0), InstID, Vals);
3048 pushValue(I.getOperand(1), InstID, Vals);
3049 pushValueAndType(I.getOperand(2), InstID, Vals);
3050 break;
3051 case Instruction::ShuffleVector:
3053 pushValueAndType(I.getOperand(0), InstID, Vals);
3054 pushValue(I.getOperand(1), InstID, Vals);
3055 pushValue(cast<ShuffleVectorInst>(I).getShuffleMaskForBitcode(), InstID,
3056 Vals);
3057 break;
3058 case Instruction::ICmp:
3059 case Instruction::FCmp: {
3060 // compare returning Int1Ty or vector of Int1Ty
3062 pushValueAndType(I.getOperand(0), InstID, Vals);
3063 pushValue(I.getOperand(1), InstID, Vals);
3064 Vals.push_back(cast<CmpInst>(I).getPredicate());
3066 if (Flags != 0)
3067 Vals.push_back(Flags);
3068 break;
3069 }
3070
3071 case Instruction::Ret:
3072 {
3074 unsigned NumOperands = I.getNumOperands();
3075 if (NumOperands == 0)
3076 AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV;
3077 else if (NumOperands == 1) {
3078 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
3079 AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV;
3080 } else {
3081 for (unsigned i = 0, e = NumOperands; i != e; ++i)
3082 pushValueAndType(I.getOperand(i), InstID, Vals);
3083 }
3084 }
3085 break;
3086 case Instruction::Br:
3087 {
3089 const BranchInst &II = cast<BranchInst>(I);
3090 Vals.push_back(VE.getValueID(II.getSuccessor(0)));
3091 if (II.isConditional()) {
3092 Vals.push_back(VE.getValueID(II.getSuccessor(1)));
3093 pushValue(II.getCondition(), InstID, Vals);
3094 }
3095 }
3096 break;
3097 case Instruction::Switch:
3098 {
3100 const SwitchInst &SI = cast<SwitchInst>(I);
3101 Vals.push_back(VE.getTypeID(SI.getCondition()->getType()));
3102 pushValue(SI.getCondition(), InstID, Vals);
3103 Vals.push_back(VE.getValueID(SI.getDefaultDest()));
3104 for (auto Case : SI.cases()) {
3105 Vals.push_back(VE.getValueID(Case.getCaseValue()));
3106 Vals.push_back(VE.getValueID(Case.getCaseSuccessor()));
3107 }
3108 }
3109 break;
3110 case Instruction::IndirectBr:
3112 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
3113 // Encode the address operand as relative, but not the basic blocks.
3114 pushValue(I.getOperand(0), InstID, Vals);
3115 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i)
3116 Vals.push_back(VE.getValueID(I.getOperand(i)));
3117 break;
3118
3119 case Instruction::Invoke: {
3120 const InvokeInst *II = cast<InvokeInst>(&I);
3121 const Value *Callee = II->getCalledOperand();
3122 FunctionType *FTy = II->getFunctionType();
3123
3124 if (II->hasOperandBundles())
3125 writeOperandBundles(*II, InstID);
3126
3128
3129 Vals.push_back(VE.getAttributeListID(II->getAttributes()));
3130 Vals.push_back(II->getCallingConv() | 1 << 13);
3131 Vals.push_back(VE.getValueID(II->getNormalDest()));
3132 Vals.push_back(VE.getValueID(II->getUnwindDest()));
3133 Vals.push_back(VE.getTypeID(FTy));
3134 pushValueAndType(Callee, InstID, Vals);
3135
3136 // Emit value #'s for the fixed parameters.
3137 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
3138 pushValue(I.getOperand(i), InstID, Vals); // fixed param.
3139
3140 // Emit type/value pairs for varargs params.
3141 if (FTy->isVarArg()) {
3142 for (unsigned i = FTy->getNumParams(), e = II->arg_size(); i != e; ++i)
3143 pushValueAndType(I.getOperand(i), InstID, Vals); // vararg
3144 }
3145 break;
3146 }
3147 case Instruction::Resume:
3149 pushValueAndType(I.getOperand(0), InstID, Vals);
3150 break;
3151 case Instruction::CleanupRet: {
3153 const auto &CRI = cast<CleanupReturnInst>(I);
3154 pushValue(CRI.getCleanupPad(), InstID, Vals);
3155 if (CRI.hasUnwindDest())
3156 Vals.push_back(VE.getValueID(CRI.getUnwindDest()));
3157 break;
3158 }
3159 case Instruction::CatchRet: {
3161 const auto &CRI = cast<CatchReturnInst>(I);
3162 pushValue(CRI.getCatchPad(), InstID, Vals);
3163 Vals.push_back(VE.getValueID(CRI.getSuccessor()));
3164 break;
3165 }
3166 case Instruction::CleanupPad:
3167 case Instruction::CatchPad: {
3168 const auto &FuncletPad = cast<FuncletPadInst>(I);
3169 Code = isa<CatchPadInst>(FuncletPad) ? bitc::FUNC_CODE_INST_CATCHPAD
3171 pushValue(FuncletPad.getParentPad(), InstID, Vals);
3172
3173 unsigned NumArgOperands = FuncletPad.arg_size();
3174 Vals.push_back(NumArgOperands);
3175 for (unsigned Op = 0; Op != NumArgOperands; ++Op)
3176 pushValueAndType(FuncletPad.getArgOperand(Op), InstID, Vals);
3177 break;
3178 }
3179 case Instruction::CatchSwitch: {
3181 const auto &CatchSwitch = cast<CatchSwitchInst>(I);
3182
3183 pushValue(CatchSwitch.getParentPad(), InstID, Vals);
3184
3185 unsigned NumHandlers = CatchSwitch.getNumHandlers();
3186 Vals.push_back(NumHandlers);
3187 for (const BasicBlock *CatchPadBB : CatchSwitch.handlers())
3188 Vals.push_back(VE.getValueID(CatchPadBB));
3189
3190 if (CatchSwitch.hasUnwindDest())
3191 Vals.push_back(VE.getValueID(CatchSwitch.getUnwindDest()));
3192 break;
3193 }
3194 case Instruction::CallBr: {
3195 const CallBrInst *CBI = cast<CallBrInst>(&I);
3196 const Value *Callee = CBI->getCalledOperand();
3197 FunctionType *FTy = CBI->getFunctionType();
3198
3199 if (CBI->hasOperandBundles())
3200 writeOperandBundles(*CBI, InstID);
3201
3203
3205
3208
3209 Vals.push_back(VE.getValueID(CBI->getDefaultDest()));
3210 Vals.push_back(CBI->getNumIndirectDests());
3211 for (unsigned i = 0, e = CBI->getNumIndirectDests(); i != e; ++i)
3212 Vals.push_back(VE.getValueID(CBI->getIndirectDest(i)));
3213
3214 Vals.push_back(VE.getTypeID(FTy));
3215 pushValueAndType(Callee, InstID, Vals);
3216
3217 // Emit value #'s for the fixed parameters.
3218 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
3219 pushValue(I.getOperand(i), InstID, Vals); // fixed param.
3220
3221 // Emit type/value pairs for varargs params.
3222 if (FTy->isVarArg()) {
3223 for (unsigned i = FTy->getNumParams(), e = CBI->arg_size(); i != e; ++i)
3224 pushValueAndType(I.getOperand(i), InstID, Vals); // vararg
3225 }
3226 break;
3227 }
3228 case Instruction::Unreachable:
3230 AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV;
3231 break;
3232
3233 case Instruction::PHI: {
3234 const PHINode &PN = cast<PHINode>(I);
3236 // With the newer instruction encoding, forward references could give
3237 // negative valued IDs. This is most common for PHIs, so we use
3238 // signed VBRs.
3240 Vals64.push_back(VE.getTypeID(PN.getType()));
3241 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
3242 pushValueSigned(PN.getIncomingValue(i), InstID, Vals64);
3243 Vals64.push_back(VE.getValueID(PN.getIncomingBlock(i)));
3244 }
3245
3247 if (Flags != 0)
3248 Vals64.push_back(Flags);
3249
3250 // Emit a Vals64 vector and exit.
3251 Stream.EmitRecord(Code, Vals64, AbbrevToUse);
3252 Vals64.clear();
3253 return;
3254 }
3255
3256 case Instruction::LandingPad: {
3257 const LandingPadInst &LP = cast<LandingPadInst>(I);
3259 Vals.push_back(VE.getTypeID(LP.getType()));
3260 Vals.push_back(LP.isCleanup());
3261 Vals.push_back(LP.getNumClauses());
3262 for (unsigned I = 0, E = LP.getNumClauses(); I != E; ++I) {
3263 if (LP.isCatch(I))
3265 else
3267 pushValueAndType(LP.getClause(I), InstID, Vals);
3268 }
3269 break;
3270 }
3271
3272 case Instruction::Alloca: {
3274 const AllocaInst &AI = cast<AllocaInst>(I);
3275 Vals.push_back(VE.getTypeID(AI.getAllocatedType()));
3276 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
3277 Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
3278 using APV = AllocaPackedValues;
3279 unsigned Record = 0;
3280 unsigned EncodedAlign = getEncodedAlign(AI.getAlign());
3281 Bitfield::set<APV::AlignLower>(
3282 Record, EncodedAlign & ((1 << APV::AlignLower::Bits) - 1));
3283 Bitfield::set<APV::AlignUpper>(Record,
3284 EncodedAlign >> APV::AlignLower::Bits);
3285 Bitfield::set<APV::UsedWithInAlloca>(Record, AI.isUsedWithInAlloca());
3286 Bitfield::set<APV::ExplicitType>(Record, true);
3287 Bitfield::set<APV::SwiftError>(Record, AI.isSwiftError());
3288 Vals.push_back(Record);
3289
3290 unsigned AS = AI.getAddressSpace();
3291 if (AS != M.getDataLayout().getAllocaAddrSpace())
3292 Vals.push_back(AS);
3293 break;
3294 }
3295
3296 case Instruction::Load:
3297 if (cast<LoadInst>(I).isAtomic()) {
3299 pushValueAndType(I.getOperand(0), InstID, Vals);
3300 } else {
3302 if (!pushValueAndType(I.getOperand(0), InstID, Vals)) // ptr
3303 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV;
3304 }
3305 Vals.push_back(VE.getTypeID(I.getType()));
3306 Vals.push_back(getEncodedAlign(cast<LoadInst>(I).getAlign()));
3307 Vals.push_back(cast<LoadInst>(I).isVolatile());
3308 if (cast<LoadInst>(I).isAtomic()) {
3309 Vals.push_back(getEncodedOrdering(cast<LoadInst>(I).getOrdering()));
3310 Vals.push_back(getEncodedSyncScopeID(cast<LoadInst>(I).getSyncScopeID()));
3311 }
3312 break;
3313 case Instruction::Store:
3314 if (cast<StoreInst>(I).isAtomic())
3316 else
3318 pushValueAndType(I.getOperand(1), InstID, Vals); // ptrty + ptr
3319 pushValueAndType(I.getOperand(0), InstID, Vals); // valty + val
3320 Vals.push_back(getEncodedAlign(cast<StoreInst>(I).getAlign()));
3321 Vals.push_back(cast<StoreInst>(I).isVolatile());
3322 if (cast<StoreInst>(I).isAtomic()) {
3323 Vals.push_back(getEncodedOrdering(cast<StoreInst>(I).getOrdering()));
3324 Vals.push_back(
3325 getEncodedSyncScopeID(cast<StoreInst>(I).getSyncScopeID()));
3326 }
3327 break;
3328 case Instruction::AtomicCmpXchg:
3330 pushValueAndType(I.getOperand(0), InstID, Vals); // ptrty + ptr
3331 pushValueAndType(I.getOperand(1), InstID, Vals); // cmp.
3332 pushValue(I.getOperand(2), InstID, Vals); // newval.
3333 Vals.push_back(cast<AtomicCmpXchgInst>(I).isVolatile());
3334 Vals.push_back(
3335 getEncodedOrdering(cast<AtomicCmpXchgInst>(I).getSuccessOrdering()));
3336 Vals.push_back(
3337 getEncodedSyncScopeID(cast<AtomicCmpXchgInst>(I).getSyncScopeID()));
3338 Vals.push_back(
3339 getEncodedOrdering(cast<AtomicCmpXchgInst>(I).getFailureOrdering()));
3340 Vals.push_back(cast<AtomicCmpXchgInst>(I).isWeak());
3341 Vals.push_back(getEncodedAlign(cast<AtomicCmpXchgInst>(I).getAlign()));
3342 break;
3343 case Instruction::AtomicRMW:
3345 pushValueAndType(I.getOperand(0), InstID, Vals); // ptrty + ptr
3346 pushValueAndType(I.getOperand(1), InstID, Vals); // valty + val
3347 Vals.push_back(
3348 getEncodedRMWOperation(cast<AtomicRMWInst>(I).getOperation()));
3349 Vals.push_back(cast<AtomicRMWInst>(I).isVolatile());
3350 Vals.push_back(getEncodedOrdering(cast<AtomicRMWInst>(I).getOrdering()));
3351 Vals.push_back(
3352 getEncodedSyncScopeID(cast<AtomicRMWInst>(I).getSyncScopeID()));
3353 Vals.push_back(getEncodedAlign(cast<AtomicRMWInst>(I).getAlign()));
3354 break;
3355 case Instruction::Fence:
3357 Vals.push_back(getEncodedOrdering(cast<FenceInst>(I).getOrdering()));
3358 Vals.push_back(getEncodedSyncScopeID(cast<FenceInst>(I).getSyncScopeID()));
3359 break;
3360 case Instruction::Call: {
3361 const CallInst &CI = cast<CallInst>(I);
3362 FunctionType *FTy = CI.getFunctionType();
3363
3364 if (CI.hasOperandBundles())
3365 writeOperandBundles(CI, InstID);
3366
3368
3370
3371 unsigned Flags = getOptimizationFlags(&I);
3373 unsigned(CI.isTailCall()) << bitc::CALL_TAIL |
3374 unsigned(CI.isMustTailCall()) << bitc::CALL_MUSTTAIL |
3376 unsigned(CI.isNoTailCall()) << bitc::CALL_NOTAIL |
3377 unsigned(Flags != 0) << bitc::CALL_FMF);
3378 if (Flags != 0)
3379 Vals.push_back(Flags);
3380
3381 Vals.push_back(VE.getTypeID(FTy));
3382 pushValueAndType(CI.getCalledOperand(), InstID, Vals); // Callee
3383
3384 // Emit value #'s for the fixed parameters.
3385 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
3386 // Check for labels (can happen with asm labels).
3387 if (FTy->getParamType(i)->isLabelTy())
3388 Vals.push_back(VE.getValueID(CI.getArgOperand(i)));
3389 else
3390 pushValue(CI.getArgOperand(i), InstID, Vals); // fixed param.
3391 }
3392
3393 // Emit type/value pairs for varargs params.
3394 if (FTy->isVarArg()) {
3395 for (unsigned i = FTy->getNumParams(), e = CI.arg_size(); i != e; ++i)
3396 pushValueAndType(CI.getArgOperand(i), InstID, Vals); // varargs
3397 }
3398 break;
3399 }
3400 case Instruction::VAArg:
3402 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty
3403 pushValue(I.getOperand(0), InstID, Vals); // valist.
3404 Vals.push_back(VE.getTypeID(I.getType())); // restype.
3405 break;
3406 case Instruction::Freeze:
3408 pushValueAndType(I.getOperand(0), InstID, Vals);
3409 break;
3410 }
3411
3412 Stream.EmitRecord(Code, Vals, AbbrevToUse);
3413 Vals.clear();
3414}
3415
3416/// Write a GlobalValue VST to the module. The purpose of this data structure is
3417/// to allow clients to efficiently find the function body.
3418void ModuleBitcodeWriter::writeGlobalValueSymbolTable(
3419 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex) {
3420 // Get the offset of the VST we are writing, and backpatch it into
3421 // the VST forward declaration record.
3422 uint64_t VSTOffset = Stream.GetCurrentBitNo();
3423 // The BitcodeStartBit was the stream offset of the identification block.
3424 VSTOffset -= bitcodeStartBit();
3425 assert((VSTOffset & 31) == 0 && "VST block not 32-bit aligned");
3426 // Note that we add 1 here because the offset is relative to one word
3427 // before the start of the identification block, which was historically
3428 // always the start of the regular bitcode header.
3429 Stream.BackpatchWord(VSTOffsetPlaceholder, VSTOffset / 32 + 1);
3430
3432
3433 auto Abbv = std::make_shared<BitCodeAbbrev>();
3435 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
3436 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
3437 unsigned FnEntryAbbrev = Stream.EmitAbbrev(std::move(Abbv));
3438
3439 for (const Function &F : M) {
3440 uint64_t Record[2];
3441
3442 if (F.isDeclaration())
3443 continue;
3444
3445 Record[0] = VE.getValueID(&F);
3446
3447 // Save the word offset of the function (from the start of the
3448 // actual bitcode written to the stream).
3449 uint64_t BitcodeIndex = FunctionToBitcodeIndex[&F] - bitcodeStartBit();
3450 assert((BitcodeIndex & 31) == 0 && "function block not 32-bit aligned");
3451 // Note that we add 1 here because the offset is relative to one word
3452 // before the start of the identification block, which was historically
3453 // always the start of the regular bitcode header.
3454 Record[1] = BitcodeIndex / 32 + 1;
3455
3456 Stream.EmitRecord(bitc::VST_CODE_FNENTRY, Record, FnEntryAbbrev);
3457 }
3458
3459 Stream.ExitBlock();
3460}
3461
3462/// Emit names for arguments, instructions and basic blocks in a function.
3463void ModuleBitcodeWriter::writeFunctionLevelValueSymbolTable(
3464 const ValueSymbolTable &VST) {
3465 if (VST.empty())
3466 return;
3467
3469
3470 // FIXME: Set up the abbrev, we know how many values there are!
3471 // FIXME: We know if the type names can use 7-bit ascii.
3473
3474 for (const ValueName &Name : VST) {
3475 // Figure out the encoding to use for the name.
3477
3478 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
3479 NameVals.push_back(VE.getValueID(Name.getValue()));
3480
3481 // VST_CODE_ENTRY: [valueid, namechar x N]
3482 // VST_CODE_BBENTRY: [bbid, namechar x N]
3483 unsigned Code;
3484 if (isa<BasicBlock>(Name.getValue())) {
3486 if (Bits == SE_Char6)
3487 AbbrevToUse = VST_BBENTRY_6_ABBREV;
3488 } else {
3490 if (Bits == SE_Char6)
3491 AbbrevToUse = VST_ENTRY_6_ABBREV;
3492 else if (Bits == SE_Fixed7)
3493 AbbrevToUse = VST_ENTRY_7_ABBREV;
3494 }
3495
3496 for (const auto P : Name.getKey())
3497 NameVals.push_back((unsigned char)P);
3498
3499 // Emit the finished record.
3500 Stream.EmitRecord(Code, NameVals, AbbrevToUse);
3501 NameVals.clear();
3502 }
3503
3504 Stream.ExitBlock();
3505}
3506
3507void ModuleBitcodeWriter::writeUseList(UseListOrder &&Order) {
3508 assert(Order.Shuffle.size() >= 2 && "Shuffle too small");
3509 unsigned Code;
3510 if (isa<BasicBlock>(Order.V))
3512 else
3514
3515 SmallVector<uint64_t, 64> Record(Order.Shuffle.begin(), Order.Shuffle.end());
3516 Record.push_back(VE.getValueID(Order.V));
3517 Stream.EmitRecord(Code, Record);
3518}
3519
3520void ModuleBitcodeWriter::writeUseListBlock(const Function *F) {
3522 "Expected to be preserving use-list order");
3523
3524 auto hasMore = [&]() {
3525 return !VE.UseListOrders.empty() && VE.UseListOrders.back().F == F;
3526 };
3527 if (!hasMore())
3528 // Nothing to do.
3529 return;
3530
3532 while (hasMore()) {
3533 writeUseList(std::move(VE.UseListOrders.back()));
3534 VE.UseListOrders.pop_back();
3535 }
3536 Stream.ExitBlock();
3537}
3538
3539/// Emit a function body to the module stream.
3540void ModuleBitcodeWriter::writeFunction(
3541 const Function &F,
3542 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex) {
3543 // Save the bitcode index of the start of this function block for recording
3544 // in the VST.
3545 FunctionToBitcodeIndex[&F] = Stream.GetCurrentBitNo();
3546
3549
3551
3552 // Emit the number of basic blocks, so the reader can create them ahead of
3553 // time.
3554 Vals.push_back(VE.getBasicBlocks().size());
3556 Vals.clear();
3557
3558 // If there are function-local constants, emit them now.
3559 unsigned CstStart, CstEnd;
3560 VE.getFunctionConstantRange(CstStart, CstEnd);
3561 writeConstants(CstStart, CstEnd, false);
3562
3563 // If there is function-local metadata, emit it now.
3564 writeFunctionMetadata(F);
3565
3566 // Keep a running idea of what the instruction ID is.
3567 unsigned InstID = CstEnd;
3568
3569 bool NeedsMetadataAttachment = F.hasMetadata();
3570
3571 DILocation *LastDL = nullptr;
3572 SmallSetVector<Function *, 4> BlockAddressUsers;
3573
3574 // Finally, emit all the instructions, in order.
3575 for (const BasicBlock &BB : F) {
3576 for (const Instruction &I : BB) {
3577 writeInstruction(I, InstID, Vals);
3578
3579 if (!I.getType()->isVoidTy())
3580 ++InstID;
3581
3582 // If the instruction has metadata, write a metadata attachment later.
3583 NeedsMetadataAttachment |= I.hasMetadataOtherThanDebugLoc();
3584
3585 // If the instruction has a debug location, emit it.
3586 if (DILocation *DL = I.getDebugLoc()) {
3587 if (DL == LastDL) {
3588 // Just repeat the same debug loc as last time.
3590 } else {
3591 Vals.push_back(DL->getLine());
3592 Vals.push_back(DL->getColumn());
3593 Vals.push_back(VE.getMetadataOrNullID(DL->getScope()));
3594 Vals.push_back(VE.getMetadataOrNullID(DL->getInlinedAt()));
3595 Vals.push_back(DL->isImplicitCode());
3597 Vals.clear();
3598 LastDL = DL;
3599 }
3600 }
3601
3602 // If the instruction has DbgRecords attached to it, emit them. Note that
3603 // they come after the instruction so that it's easy to attach them again
3604 // when reading the bitcode, even though conceptually the debug locations
3605 // start "before" the instruction.
3606 if (I.hasDbgRecords() && WriteNewDbgInfoFormatToBitcode) {
3607 /// Try to push the value only (unwrapped), otherwise push the
3608 /// metadata wrapped value. Returns true if the value was pushed
3609 /// without the ValueAsMetadata wrapper.
3610 auto PushValueOrMetadata = [&Vals, InstID,
3611 this](Metadata *RawLocation) {
3612 assert(RawLocation &&
3613 "RawLocation unexpectedly null in DbgVariableRecord");
3614 if (ValueAsMetadata *VAM = dyn_cast<ValueAsMetadata>(RawLocation)) {
3615 SmallVector<unsigned, 2> ValAndType;
3616 // If the value is a fwd-ref the type is also pushed. We don't
3617 // want the type, so fwd-refs are kept wrapped (pushValueAndType
3618 // returns false if the value is pushed without type).
3619 if (!pushValueAndType(VAM->getValue(), InstID, ValAndType)) {
3620 Vals.push_back(ValAndType[0]);
3621 return true;
3622 }
3623 }
3624 // The metadata is a DIArgList, or ValueAsMetadata wrapping a
3625 // fwd-ref. Push the metadata ID.
3626 Vals.push_back(VE.getMetadataID(RawLocation));
3627 return false;
3628 };
3629
3630 // Write out non-instruction debug information attached to this
3631 // instruction. Write it after the instruction so that it's easy to
3632 // re-attach to the instruction reading the records in.
3633 for (DbgRecord &DR : I.DebugMarker->getDbgRecordRange()) {
3634 if (DbgLabelRecord *DLR = dyn_cast<DbgLabelRecord>(&DR)) {
3635 Vals.push_back(VE.getMetadataID(&*DLR->getDebugLoc()));
3636 Vals.push_back(VE.getMetadataID(DLR->getLabel()));
3638 Vals.clear();
3639 continue;
3640 }
3641
3642 // First 3 fields are common to all kinds:
3643 // DILocation, DILocalVariable, DIExpression
3644 // dbg_value (FUNC_CODE_DEBUG_RECORD_VALUE)
3645 // ..., LocationMetadata
3646 // dbg_value (FUNC_CODE_DEBUG_RECORD_VALUE_SIMPLE - abbrev'd)
3647 // ..., Value
3648 // dbg_declare (FUNC_CODE_DEBUG_RECORD_DECLARE)
3649 // ..., LocationMetadata
3650 // dbg_assign (FUNC_CODE_DEBUG_RECORD_ASSIGN)
3651 // ..., LocationMetadata, DIAssignID, DIExpression, LocationMetadata
3652 DbgVariableRecord &DVR = cast<DbgVariableRecord>(DR);
3653 Vals.push_back(VE.getMetadataID(&*DVR.getDebugLoc()));
3654 Vals.push_back(VE.getMetadataID(DVR.getVariable()));
3655 Vals.push_back(VE.getMetadataID(DVR.getExpression()));
3656 if (DVR.isDbgValue()) {
3657 if (PushValueOrMetadata(DVR.getRawLocation()))
3659 FUNCTION_DEBUG_RECORD_VALUE_ABBREV);
3660 else
3662 } else if (DVR.isDbgDeclare()) {
3663 Vals.push_back(VE.getMetadataID(DVR.getRawLocation()));
3665 } else {
3666 assert(DVR.isDbgAssign() && "Unexpected DbgRecord kind");
3667 Vals.push_back(VE.getMetadataID(DVR.getRawLocation()));
3668 Vals.push_back(VE.getMetadataID(DVR.getAssignID()));
3670 Vals.push_back(VE.getMetadataID(DVR.getRawAddress()));
3672 }
3673 Vals.clear();
3674 }
3675 }
3676 }
3677
3678 if (BlockAddress *BA = BlockAddress::lookup(&BB)) {
3679 SmallVector<Value *> Worklist{BA};
3680 SmallPtrSet<Value *, 8> Visited{BA};
3681 while (!Worklist.empty()) {
3682 Value *V = Worklist.pop_back_val();
3683 for (User *U : V->users()) {
3684 if (auto *I = dyn_cast<Instruction>(U)) {
3685 Function *P = I->getFunction();
3686 if (P != &F)
3687 BlockAddressUsers.insert(P);
3688 } else if (isa<Constant>(U) && !isa<GlobalValue>(U) &&
3689 Visited.insert(U).second)
3690 Worklist.push_back(U);
3691 }
3692 }
3693 }
3694 }
3695
3696 if (!BlockAddressUsers.empty()) {
3697 Vals.resize(BlockAddressUsers.size());
3698 for (auto I : llvm::enumerate(BlockAddressUsers))
3699 Vals[I.index()] = VE.getValueID(I.value());
3701 Vals.clear();
3702 }
3703
3704 // Emit names for all the instructions etc.
3705 if (auto *Symtab = F.getValueSymbolTable())
3706 writeFunctionLevelValueSymbolTable(*Symtab);
3707
3708 if (NeedsMetadataAttachment)
3709 writeFunctionMetadataAttachment(F);
3711 writeUseListBlock(&F);
3712 VE.purgeFunction();
3713 Stream.ExitBlock();
3714}
3715
3716// Emit blockinfo, which defines the standard abbreviations etc.
3717void ModuleBitcodeWriter::writeBlockInfo() {
3718 // We only want to emit block info records for blocks that have multiple
3719 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK.
3720 // Other blocks can define their abbrevs inline.
3721 Stream.EnterBlockInfoBlock();
3722
3723 { // 8-bit fixed-width VST_CODE_ENTRY/VST_CODE_BBENTRY strings.
3724 auto Abbv = std::make_shared<BitCodeAbbrev>();
3730 VST_ENTRY_8_ABBREV)
3731 llvm_unreachable("Unexpected abbrev ordering!");
3732 }
3733
3734 { // 7-bit fixed width VST_CODE_ENTRY strings.
3735 auto Abbv = std::make_shared<BitCodeAbbrev>();
3741 VST_ENTRY_7_ABBREV)
3742 llvm_unreachable("Unexpected abbrev ordering!");
3743 }
3744 { // 6-bit char6 VST_CODE_ENTRY strings.
3745 auto Abbv = std::make_shared<BitCodeAbbrev>();
3751 VST_ENTRY_6_ABBREV)
3752 llvm_unreachable("Unexpected abbrev ordering!");
3753 }
3754 { // 6-bit char6 VST_CODE_BBENTRY strings.
3755 auto Abbv = std::make_shared<BitCodeAbbrev>();
3761 VST_BBENTRY_6_ABBREV)
3762 llvm_unreachable("Unexpected abbrev ordering!");
3763 }
3764
3765 { // SETTYPE abbrev for CONSTANTS_BLOCK.
3766 auto Abbv = std::make_shared<BitCodeAbbrev>();
3771 CONSTANTS_SETTYPE_ABBREV)
3772 llvm_unreachable("Unexpected abbrev ordering!");
3773 }
3774
3775 { // INTEGER abbrev for CONSTANTS_BLOCK.
3776 auto Abbv = std::make_shared<BitCodeAbbrev>();
3780 CONSTANTS_INTEGER_ABBREV)
3781 llvm_unreachable("Unexpected abbrev ordering!");
3782 }
3783
3784 { // CE_CAST abbrev for CONSTANTS_BLOCK.
3785 auto Abbv = std::make_shared<BitCodeAbbrev>();
3787 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc
3788 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid
3790 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
3791
3793 CONSTANTS_CE_CAST_Abbrev)
3794 llvm_unreachable("Unexpected abbrev ordering!");
3795 }
3796 { // NULL abbrev for CONSTANTS_BLOCK.
3797 auto Abbv = std::make_shared<BitCodeAbbrev>();
3800 CONSTANTS_NULL_Abbrev)
3801 llvm_unreachable("Unexpected abbrev ordering!");
3802 }
3803
3804 // FIXME: This should only use space for first class types!
3805
3806 { // INST_LOAD abbrev for FUNCTION_BLOCK.
3807 auto Abbv = std::make_shared<BitCodeAbbrev>();
3809 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr
3810 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
3812 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align
3813 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
3814 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3815 FUNCTION_INST_LOAD_ABBREV)
3816 llvm_unreachable("Unexpected abbrev ordering!");
3817 }
3818 { // INST_UNOP abbrev for FUNCTION_BLOCK.
3819 auto Abbv = std::make_shared<BitCodeAbbrev>();
3821 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
3822 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
3823 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3824 FUNCTION_INST_UNOP_ABBREV)
3825 llvm_unreachable("Unexpected abbrev ordering!");
3826 }
3827 { // INST_UNOP_FLAGS abbrev for FUNCTION_BLOCK.
3828 auto Abbv = std::make_shared<BitCodeAbbrev>();
3830 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
3831 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
3832 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); // flags
3833 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3834 FUNCTION_INST_UNOP_FLAGS_ABBREV)
3835 llvm_unreachable("Unexpected abbrev ordering!");
3836 }
3837 { // INST_BINOP abbrev for FUNCTION_BLOCK.
3838 auto Abbv = std::make_shared<BitCodeAbbrev>();
3840 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
3841 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
3842 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
3843 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3844 FUNCTION_INST_BINOP_ABBREV)
3845 llvm_unreachable("Unexpected abbrev ordering!");
3846 }
3847 { // INST_BINOP_FLAGS abbrev for FUNCTION_BLOCK.
3848 auto Abbv = std::make_shared<BitCodeAbbrev>();
3850 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
3851 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
3852 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
3853 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); // flags
3854 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3855 FUNCTION_INST_BINOP_FLAGS_ABBREV)
3856 llvm_unreachable("Unexpected abbrev ordering!");
3857 }
3858 { // INST_CAST abbrev for FUNCTION_BLOCK.
3859 auto Abbv = std::make_shared<BitCodeAbbrev>();
3861 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal
3862 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
3864 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
3865 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3866 FUNCTION_INST_CAST_ABBREV)
3867 llvm_unreachable("Unexpected abbrev ordering!");
3868 }
3869 { // INST_CAST_FLAGS abbrev for FUNCTION_BLOCK.
3870 auto Abbv = std::make_shared<BitCodeAbbrev>();
3872 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal
3873 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
3875 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
3876 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); // flags
3877 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3878 FUNCTION_INST_CAST_FLAGS_ABBREV)
3879 llvm_unreachable("Unexpected abbrev ordering!");
3880 }
3881
3882 { // INST_RET abbrev for FUNCTION_BLOCK.
3883 auto Abbv = std::make_shared<BitCodeAbbrev>();
3885 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3886 FUNCTION_INST_RET_VOID_ABBREV)
3887 llvm_unreachable("Unexpected abbrev ordering!");
3888 }
3889 { // INST_RET abbrev for FUNCTION_BLOCK.
3890 auto Abbv = std::make_shared<BitCodeAbbrev>();
3892 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID
3893 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3894 FUNCTION_INST_RET_VAL_ABBREV)
3895 llvm_unreachable("Unexpected abbrev ordering!");
3896 }
3897 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
3898 auto Abbv = std::make_shared<BitCodeAbbrev>();
3900 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3901 FUNCTION_INST_UNREACHABLE_ABBREV)
3902 llvm_unreachable("Unexpected abbrev ordering!");
3903 }
3904 {
3905 auto Abbv = std::make_shared<BitCodeAbbrev>();
3908 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
3909 Log2_32_Ceil(VE.getTypes().size() + 1)));
3912 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3913 FUNCTION_INST_GEP_ABBREV)
3914 llvm_unreachable("Unexpected abbrev ordering!");
3915 }
3916 {
3917 auto Abbv = std::make_shared<BitCodeAbbrev>();
3919 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 7)); // dbgloc
3920 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 7)); // var
3921 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 7)); // expr
3922 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // val
3923 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3924 FUNCTION_DEBUG_RECORD_VALUE_ABBREV)
3925 llvm_unreachable("Unexpected abbrev ordering! 1");
3926 }
3927 Stream.ExitBlock();
3928}
3929
3930/// Write the module path strings, currently only used when generating
3931/// a combined index file.
3932void IndexBitcodeWriter::writeModStrings() {
3934
3935 // TODO: See which abbrev sizes we actually need to emit
3936
3937 // 8-bit fixed-width MST_ENTRY strings.
3938 auto Abbv = std::make_shared<BitCodeAbbrev>();
3943 unsigned Abbrev8Bit = Stream.EmitAbbrev(std::move(Abbv));
3944
3945 // 7-bit fixed width MST_ENTRY strings.
3946 Abbv = std::make_shared<BitCodeAbbrev>();
3951 unsigned Abbrev7Bit = Stream.EmitAbbrev(std::move(Abbv));
3952
3953 // 6-bit char6 MST_ENTRY strings.
3954 Abbv = std::make_shared<BitCodeAbbrev>();
3959 unsigned Abbrev6Bit = Stream.EmitAbbrev(std::move(Abbv));
3960
3961 // Module Hash, 160 bits SHA1. Optionally, emitted after each MST_CODE_ENTRY.
3962 Abbv = std::make_shared<BitCodeAbbrev>();
3969 unsigned AbbrevHash = Stream.EmitAbbrev(std::move(Abbv));
3970
3972 forEachModule([&](const StringMapEntry<ModuleHash> &MPSE) {
3973 StringRef Key = MPSE.getKey();
3974 const auto &Hash = MPSE.getValue();
3976 unsigned AbbrevToUse = Abbrev8Bit;
3977 if (Bits == SE_Char6)
3978 AbbrevToUse = Abbrev6Bit;
3979 else if (Bits == SE_Fixed7)
3980 AbbrevToUse = Abbrev7Bit;
3981
3982 auto ModuleId = ModuleIdMap.size();
3983 ModuleIdMap[Key] = ModuleId;
3984 Vals.push_back(ModuleId);
3985 Vals.append(Key.begin(), Key.end());
3986
3987 // Emit the finished record.
3988 Stream.EmitRecord(bitc::MST_CODE_ENTRY, Vals, AbbrevToUse);
3989
3990 // Emit an optional hash for the module now
3991 if (llvm::any_of(Hash, [](uint32_t H) { return H; })) {
3992 Vals.assign(Hash.begin(), Hash.end());
3993 // Emit the hash record.
3994 Stream.EmitRecord(bitc::MST_CODE_HASH, Vals, AbbrevHash);
3995 }
3996
3997 Vals.clear();
3998 });
3999 Stream.ExitBlock();
4000}
4001
4002/// Write the function type metadata related records that need to appear before
4003/// a function summary entry (whether per-module or combined).
4004template <typename Fn>
4006 FunctionSummary *FS,
4007 Fn GetValueID) {
4008 if (!FS->type_tests().empty())
4009 Stream.EmitRecord(bitc::FS_TYPE_TESTS, FS->type_tests());
4010
4012
4013 auto WriteVFuncIdVec = [&](uint64_t Ty,
4015 if (VFs.empty())
4016 return;
4017 Record.clear();
4018 for (auto &VF : VFs) {
4019 Record.push_back(VF.GUID);
4020 Record.push_back(VF.Offset);
4021 }
4022 Stream.EmitRecord(Ty, Record);
4023 };
4024
4025 WriteVFuncIdVec(bitc::FS_TYPE_TEST_ASSUME_VCALLS,
4026 FS->type_test_assume_vcalls());
4027 WriteVFuncIdVec(bitc::FS_TYPE_CHECKED_LOAD_VCALLS,
4028 FS->type_checked_load_vcalls());
4029
4030 auto WriteConstVCallVec = [&](uint64_t Ty,
4032 for (auto &VC : VCs) {
4033 Record.clear();
4034 Record.push_back(VC.VFunc.GUID);
4035 Record.push_back(VC.VFunc.Offset);
4036 llvm::append_range(Record, VC.Args);
4037 Stream.EmitRecord(Ty, Record);
4038 }
4039 };
4040
4041 WriteConstVCallVec(bitc::FS_TYPE_TEST_ASSUME_CONST_VCALL,
4042 FS->type_test_assume_const_vcalls());
4043 WriteConstVCallVec(bitc::FS_TYPE_CHECKED_LOAD_CONST_VCALL,
4044 FS->type_checked_load_const_vcalls());
4045
4046 auto WriteRange = [&](ConstantRange Range) {
4048 assert(Range.getLower().getNumWords() == 1);
4049 assert(Range.getUpper().getNumWords() == 1);
4052 };
4053
4054 if (!FS->paramAccesses().empty()) {
4055 Record.clear();
4056 for (auto &Arg : FS->paramAccesses()) {
4057 size_t UndoSize = Record.size();
4058 Record.push_back(Arg.ParamNo);
4059 WriteRange(Arg.Use);
4060 Record.push_back(Arg.Calls.size());
4061 for (auto &Call : Arg.Calls) {
4062 Record.push_back(Call.ParamNo);
4063 std::optional<unsigned> ValueID = GetValueID(Call.Callee);
4064 if (!ValueID) {
4065 // If ValueID is unknown we can't drop just this call, we must drop
4066 // entire parameter.
4067 Record.resize(UndoSize);
4068 break;
4069 }
4070 Record.push_back(*ValueID);
4071 WriteRange(Call.Offsets);
4072 }
4073 }
4074 if (!Record.empty())
4076 }
4077}
4078
4079/// Collect type IDs from type tests used by function.
4080static void
4082 std::set<GlobalValue::GUID> &ReferencedTypeIds) {
4083 if (!FS->type_tests().empty())
4084 for (auto &TT : FS->type_tests())
4085 ReferencedTypeIds.insert(TT);
4086
4087 auto GetReferencedTypesFromVFuncIdVec =
4089 for (auto &VF : VFs)
4090 ReferencedTypeIds.insert(VF.GUID);
4091 };
4092
4093 GetReferencedTypesFromVFuncIdVec(FS->type_test_assume_vcalls());
4094 GetReferencedTypesFromVFuncIdVec(FS->type_checked_load_vcalls());
4095
4096 auto GetReferencedTypesFromConstVCallVec =
4098 for (auto &VC : VCs)
4099 ReferencedTypeIds.insert(VC.VFunc.GUID);
4100 };
4101
4102 GetReferencedTypesFromConstVCallVec(FS->type_test_assume_const_vcalls());
4103 GetReferencedTypesFromConstVCallVec(FS->type_checked_load_const_vcalls());
4104}
4105
4107 SmallVector<uint64_t, 64> &NameVals, const std::vector<uint64_t> &args,
4109 NameVals.push_back(args.size());
4110 llvm::append_range(NameVals, args);
4111
4112 NameVals.push_back(ByArg.TheKind);
4113 NameVals.push_back(ByArg.Info);
4114 NameVals.push_back(ByArg.Byte);
4115 NameVals.push_back(ByArg.Bit);
4116}
4117
4119 SmallVector<uint64_t, 64> &NameVals, StringTableBuilder &StrtabBuilder,
4120 uint64_t Id, const WholeProgramDevirtResolution &Wpd) {
4121 NameVals.push_back(Id);
4122
4123 NameVals.push_back(Wpd.TheKind);
4124 NameVals.push_back(StrtabBuilder.add(Wpd.SingleImplName));
4125 NameVals.push_back(Wpd.SingleImplName.size());
4126
4127 NameVals.push_back(Wpd.ResByArg.size());
4128 for (auto &A : Wpd.ResByArg)
4129 writeWholeProgramDevirtResolutionByArg(NameVals, A.first, A.second);
4130}
4131
4133 StringTableBuilder &StrtabBuilder,
4134 const std::string &Id,
4135 const TypeIdSummary &Summary) {
4136 NameVals.push_back(StrtabBuilder.add(Id));
4137 NameVals.push_back(Id.size());
4138
4139 NameVals.push_back(Summary.TTRes.TheKind);
4140 NameVals.push_back(Summary.TTRes.SizeM1BitWidth);
4141 NameVals.push_back(Summary.TTRes.AlignLog2);
4142 NameVals.push_back(Summary.TTRes.SizeM1);
4143 NameVals.push_back(Summary.TTRes.BitMask);
4144 NameVals.push_back(Summary.TTRes.InlineBits);
4145
4146 for (auto &W : Summary.WPDRes)
4147 writeWholeProgramDevirtResolution(NameVals, StrtabBuilder, W.first,
4148 W.second);
4149}
4150
4152 SmallVector<uint64_t, 64> &NameVals, StringTableBuilder &StrtabBuilder,
4153 const std::string &Id, const TypeIdCompatibleVtableInfo &Summary,
4154 ValueEnumerator &VE) {
4155 NameVals.push_back(StrtabBuilder.add(Id));
4156 NameVals.push_back(Id.size());
4157
4158 for (auto &P : Summary) {
4159 NameVals.push_back(P.AddressPointOffset);
4160 NameVals.push_back(VE.getValueID(P.VTableVI.getValue()));
4161 }
4162}
4163
4165 BitstreamWriter &Stream, FunctionSummary *FS, unsigned CallsiteAbbrev,
4166 unsigned AllocAbbrev, bool PerModule,
4167 std::function<unsigned(const ValueInfo &VI)> GetValueID,
4168 std::function<unsigned(unsigned)> GetStackIndex) {
4170
4171 for (auto &CI : FS->callsites()) {
4172 Record.clear();
4173 // Per module callsite clones should always have a single entry of
4174 // value 0.
4175 assert(!PerModule || (CI.Clones.size() == 1 && CI.Clones[0] == 0));
4176 Record.push_back(GetValueID(CI.Callee));
4177 if (!PerModule) {
4178 Record.push_back(CI.StackIdIndices.size());
4179 Record.push_back(CI.Clones.size());
4180 }
4181 for (auto Id : CI.StackIdIndices)
4182 Record.push_back(GetStackIndex(Id));
4183 if (!PerModule) {
4184 for (auto V : CI.Clones)
4185 Record.push_back(V);
4186 }
4189 Record, CallsiteAbbrev);
4190 }
4191
4192 for (auto &AI : FS->allocs()) {
4193 Record.clear();
4194 // Per module alloc versions should always have a single entry of
4195 // value 0.
4196 assert(!PerModule || (AI.Versions.size() == 1 && AI.Versions[0] == 0));
4197 Record.push_back(AI.MIBs.size());
4198 if (!PerModule)
4199 Record.push_back(AI.Versions.size());
4200 for (auto &MIB : AI.MIBs) {
4201 Record.push_back((uint8_t)MIB.AllocType);
4202 Record.push_back(MIB.StackIdIndices.size());
4203 for (auto Id : MIB.StackIdIndices)
4204 Record.push_back(GetStackIndex(Id));
4205 }
4206 if (!PerModule) {
4207 for (auto V : AI.Versions)
4208 Record.push_back(V);
4209 }
4210 assert(AI.TotalSizes.empty() || AI.TotalSizes.size() == AI.MIBs.size());
4211 if (!AI.TotalSizes.empty()) {
4212 for (auto Size : AI.TotalSizes)
4213 Record.push_back(Size);
4214 }
4215 Stream.EmitRecord(PerModule ? bitc::FS_PERMODULE_ALLOC_INFO
4217 Record, AllocAbbrev);
4218 }
4219}
4220
4221// Helper to emit a single function summary record.
4222void ModuleBitcodeWriterBase::writePerModuleFunctionSummaryRecord(
4224 unsigned ValueID, unsigned FSCallsRelBFAbbrev,
4225 unsigned FSCallsProfileAbbrev, unsigned CallsiteAbbrev,
4226 unsigned AllocAbbrev, const Function &F) {
4227 NameVals.push_back(ValueID);
4228
4229 FunctionSummary *FS = cast<FunctionSummary>(Summary);
4230
4232 Stream, FS, [&](const ValueInfo &VI) -> std::optional<unsigned> {
4233 return {VE.getValueID(VI.getValue())};
4234 });
4235
4237 Stream, FS, CallsiteAbbrev, AllocAbbrev,
4238 /*PerModule*/ true,
4239 /*GetValueId*/ [&](const ValueInfo &VI) { return getValueId(VI); },
4240 /*GetStackIndex*/ [&](unsigned I) { return I; });
4241
4242 auto SpecialRefCnts = FS->specialRefCounts();
4243 NameVals.push_back(getEncodedGVSummaryFlags(FS->flags()));
4244 NameVals.push_back(FS->instCount());
4245 NameVals.push_back(getEncodedFFlags(FS->fflags()));
4246 NameVals.push_back(FS->refs().size());
4247 NameVals.push_back(SpecialRefCnts.first); // rorefcnt
4248 NameVals.push_back(SpecialRefCnts.second); // worefcnt
4249
4250 for (auto &RI : FS->refs())
4251 NameVals.push_back(getValueId(RI));
4252
4253 const bool UseRelBFRecord =
4254 WriteRelBFToSummary && !F.hasProfileData() &&
4256 for (auto &ECI : FS->calls()) {
4257 NameVals.push_back(getValueId(ECI.first));
4258 if (UseRelBFRecord)
4259 NameVals.push_back(getEncodedRelBFCallEdgeInfo(ECI.second));
4260 else
4261 NameVals.push_back(getEncodedHotnessCallEdgeInfo(ECI.second));
4262 }
4263
4264 unsigned FSAbbrev =
4265 (UseRelBFRecord ? FSCallsRelBFAbbrev : FSCallsProfileAbbrev);
4266 unsigned Code =
4268
4269 // Emit the finished record.
4270 Stream.EmitRecord(Code, NameVals, FSAbbrev);
4271 NameVals.clear();
4272}
4273
4274// Collect the global value references in the given variable's initializer,
4275// and emit them in a summary record.
4276void ModuleBitcodeWriterBase::writeModuleLevelReferences(
4277 const GlobalVariable &V, SmallVector<uint64_t, 64> &NameVals,
4278 unsigned FSModRefsAbbrev, unsigned FSModVTableRefsAbbrev) {
4279 auto VI = Index->getValueInfo(V.getGUID());
4280 if (!VI || VI.getSummaryList().empty()) {
4281 // Only declarations should not have a summary (a declaration might however
4282 // have a summary if the def was in module level asm).
4283 assert(V.isDeclaration());
4284 return;
4285 }
4286 auto *Summary = VI.getSummaryList()[0].get();
4287 NameVals.push_back(VE.getValueID(&V));
4288 GlobalVarSummary *VS = cast<GlobalVarSummary>(Summary);
4289 NameVals.push_back(getEncodedGVSummaryFlags(VS->flags()));
4290 NameVals.push_back(getEncodedGVarFlags(VS->varflags()));
4291
4292 auto VTableFuncs = VS->vTableFuncs();
4293 if (!VTableFuncs.empty())
4294 NameVals.push_back(VS->refs().size());
4295
4296 unsigned SizeBeforeRefs = NameVals.size();
4297 for (auto &RI : VS->refs())
4298 NameVals.push_back(VE.getValueID(RI.getValue()));
4299 // Sort the refs for determinism output, the vector returned by FS->refs() has
4300 // been initialized from a DenseSet.
4301 llvm::sort(drop_begin(NameVals, SizeBeforeRefs));
4302
4303 if (VTableFuncs.empty())
4305 FSModRefsAbbrev);
4306 else {
4307 // VTableFuncs pairs should already be sorted by offset.
4308 for (auto &P : VTableFuncs) {
4309 NameVals.push_back(VE.getValueID(P.FuncVI.getValue()));
4310 NameVals.push_back(P.VTableOffset);
4311 }
4312
4314 FSModVTableRefsAbbrev);
4315 }
4316 NameVals.clear();
4317}
4318
4319/// Emit the per-module summary section alongside the rest of
4320/// the module's bitcode.
4321void ModuleBitcodeWriterBase::writePerModuleGlobalValueSummary() {
4322 // By default we compile with ThinLTO if the module has a summary, but the
4323 // client can request full LTO with a module flag.
4324 bool IsThinLTO = true;
4325 if (auto *MD =
4326 mdconst::extract_or_null<ConstantInt>(M.getModuleFlag("ThinLTO")))
4327 IsThinLTO = MD->getZExtValue();
4330 4);
4331
4332 Stream.EmitRecord(
4335
4336 // Write the index flags.
4337 uint64_t Flags = 0;
4338 // Bits 1-3 are set only in the combined index, skip them.
4339 if (Index->enableSplitLTOUnit())
4340 Flags |= 0x8;
4341 if (Index->hasUnifiedLTO())
4342 Flags |= 0x200;
4343
4345
4346 if (Index->begin() == Index->end()) {
4347 Stream.ExitBlock();
4348 return;
4349 }
4350
4351 for (const auto &GVI : valueIds()) {
4353 ArrayRef<uint64_t>{GVI.second, GVI.first});
4354 }
4355
4356 if (!Index->stackIds().empty()) {
4357 auto StackIdAbbv = std::make_shared<BitCodeAbbrev>();
4358 StackIdAbbv->Add(BitCodeAbbrevOp(bitc::FS_STACK_IDS));
4359 // numids x stackid
4360 StackIdAbbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
4361 StackIdAbbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
4362 unsigned StackIdAbbvId = Stream.EmitAbbrev(std::move(StackIdAbbv));
4363 Stream.EmitRecord(bitc::FS_STACK_IDS, Index->stackIds(), StackIdAbbvId);
4364 }
4365
4366 // Abbrev for FS_PERMODULE_PROFILE.
4367 auto Abbv = std::make_shared<BitCodeAbbrev>();
4369 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
4370 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // flags
4371 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
4372 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // fflags
4373 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
4374 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4));