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 (const Value *Op : CE->operands()) {
2824 Record.push_back(VE.getTypeID(Op->getType()));
2825 Record.push_back(VE.getValueID(Op));
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 (const Value *Op : I.operands())
3012 pushValueAndType(Op, 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 (const Value *Op : I.operands())
3082 pushValueAndType(Op, 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 (const Value *Op : drop_begin(I.operands()))
3116 Vals.push_back(VE.getValueID(Op));
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)); // rorefcnt
4375 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // worefcnt
4376 // numrefs x valueid, n x (valueid, hotness+tailcall flags)
4379 unsigned FSCallsProfileAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4380
4381 // Abbrev for FS_PERMODULE_RELBF.
4382 Abbv = std::make_shared<BitCodeAbbrev>();
4384 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
4385 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
4386 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
4387 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // fflags
4388 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
4389 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // rorefcnt
4390 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // worefcnt
4391 // numrefs x valueid, n x (valueid, rel_block_freq+tailcall])
4394 unsigned FSCallsRelBFAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4395
4396 // Abbrev for FS_PERMODULE_GLOBALVAR_INIT_REFS.
4397 Abbv = std::make_shared<BitCodeAbbrev>();
4399 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
4400 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
4401 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); // valueids
4403 unsigned FSModRefsAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4404
4405 // Abbrev for FS_PERMODULE_VTABLE_GLOBALVAR_INIT_REFS.
4406 Abbv = std::make_shared<BitCodeAbbrev>();
4408 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
4409 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
4410 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
4411 // numrefs x valueid, n x (valueid , offset)
4414 unsigned FSModVTableRefsAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4415
4416 // Abbrev for FS_ALIAS.
4417 Abbv = std::make_shared<BitCodeAbbrev>();
4418 Abbv->Add(BitCodeAbbrevOp(bitc::FS_ALIAS));
4419 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
4420 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
4421 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
4422 unsigned FSAliasAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4423
4424 // Abbrev for FS_TYPE_ID_METADATA
4425 Abbv = std::make_shared<BitCodeAbbrev>();
4427 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // typeid strtab index
4428 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // typeid length
4429 // n x (valueid , offset)
4432 unsigned TypeIdCompatibleVtableAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4433
4434 Abbv = std::make_shared<BitCodeAbbrev>();
4436 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
4437 // n x stackidindex
4440 unsigned CallsiteAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4441
4442 Abbv = std::make_shared<BitCodeAbbrev>();
4444 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // nummib
4445 // n x (alloc type, numstackids, numstackids x stackidindex)
4446 // optional: nummib x total size
4449 unsigned AllocAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4450
4452 // Iterate over the list of functions instead of the Index to
4453 // ensure the ordering is stable.
4454 for (const Function &F : M) {
4455 // Summary emission does not support anonymous functions, they have to
4456 // renamed using the anonymous function renaming pass.
4457 if (!F.hasName())
4458 report_fatal_error("Unexpected anonymous function when writing summary");
4459
4460 ValueInfo VI = Index->getValueInfo(F.getGUID());
4461 if (!VI || VI.getSummaryList().empty()) {
4462 // Only declarations should not have a summary (a declaration might
4463 // however have a summary if the def was in module level asm).
4464 assert(F.isDeclaration());
4465 continue;
4466 }
4467 auto *Summary = VI.getSummaryList()[0].get();
4468 writePerModuleFunctionSummaryRecord(
4469 NameVals, Summary, VE.getValueID(&F), FSCallsRelBFAbbrev,
4470 FSCallsProfileAbbrev, CallsiteAbbrev, AllocAbbrev, F);
4471 }
4472
4473 // Capture references from GlobalVariable initializers, which are outside
4474 // of a function scope.
4475 for (const GlobalVariable &G : M.globals())
4476 writeModuleLevelReferences(G, NameVals, FSModRefsAbbrev,
4477 FSModVTableRefsAbbrev);
4478
4479 for (const GlobalAlias &A : M.aliases()) {
4480 auto *Aliasee = A.getAliaseeObject();
4481 // Skip ifunc and nameless functions which don't have an entry in the
4482 // summary.
4483 if (!Aliasee->hasName() || isa<GlobalIFunc>(Aliasee))
4484 continue;
4485 auto AliasId = VE.getValueID(&A);
4486 auto AliaseeId = VE.getValueID(Aliasee);
4487 NameVals.push_back(AliasId);
4488 auto *Summary = Index->getGlobalValueSummary(A);
4489 AliasSummary *AS = cast<AliasSummary>(Summary);
4490 NameVals.push_back(getEncodedGVSummaryFlags(AS->flags()));
4491 NameVals.push_back(AliaseeId);
4492 Stream.EmitRecord(bitc::FS_ALIAS, NameVals, FSAliasAbbrev);
4493 NameVals.clear();
4494 }
4495
4496 for (auto &S : Index->typeIdCompatibleVtableMap()) {
4497 writeTypeIdCompatibleVtableSummaryRecord(NameVals, StrtabBuilder, S.first,
4498 S.second, VE);
4499 Stream.EmitRecord(bitc::FS_TYPE_ID_METADATA, NameVals,
4500 TypeIdCompatibleVtableAbbrev);
4501 NameVals.clear();
4502 }
4503
4504 if (Index->getBlockCount())
4506 ArrayRef<uint64_t>{Index->getBlockCount()});
4507
4508 Stream.ExitBlock();
4509}
4510
4511/// Emit the combined summary section into the combined index file.
4512void IndexBitcodeWriter::writeCombinedGlobalValueSummary() {
4514 Stream.EmitRecord(
4517
4518 // Write the index flags.
4520
4521 for (const auto &GVI : valueIds()) {
4523 ArrayRef<uint64_t>{GVI.second, GVI.first});
4524 }
4525
4526 // Write the stack ids used by this index, which will be a subset of those in
4527 // the full index in the case of distributed indexes.
4528 if (!StackIds.empty()) {
4529 auto StackIdAbbv = std::make_shared<BitCodeAbbrev>();
4530 StackIdAbbv->Add(BitCodeAbbrevOp(bitc::FS_STACK_IDS));
4531 // numids x stackid
4532 StackIdAbbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
4533 StackIdAbbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
4534 unsigned StackIdAbbvId = Stream.EmitAbbrev(std::move(StackIdAbbv));
4535 Stream.EmitRecord(bitc::FS_STACK_IDS, StackIds, StackIdAbbvId);
4536 }
4537
4538 // Abbrev for FS_COMBINED_PROFILE.
4539 auto Abbv = std::make_shared<BitCodeAbbrev>();
4541 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
4542 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
4543 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
4544 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
4545 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // fflags
4546 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // entrycount
4547 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
4548 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // rorefcnt
4549 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // worefcnt
4550 // numrefs x valueid, n x (valueid, hotness+tailcall flags)
4553 unsigned FSCallsProfileAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4554
4555 // Abbrev for FS_COMBINED_GLOBALVAR_INIT_REFS.
4556 Abbv = std::make_shared<BitCodeAbbrev>();
4558 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
4559 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
4560 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
4561 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); // valueids
4563 unsigned FSModRefsAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4564
4565 // Abbrev for FS_COMBINED_ALIAS.
4566 Abbv = std::make_shared<BitCodeAbbrev>();
4568 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
4569 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
4570 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
4571 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
4572 unsigned FSAliasAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4573
4574 Abbv = std::make_shared<BitCodeAbbrev>();
4576 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
4577 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numstackindices
4578 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numver
4579 // numstackindices x stackidindex, numver x version
4582 unsigned CallsiteAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4583
4584 Abbv = std::make_shared<BitCodeAbbrev>();
4586 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // nummib
4587 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numver
4588 // nummib x (alloc type, numstackids, numstackids x stackidindex),
4589 // numver x version
4590 // optional: nummib x total size
4593 unsigned AllocAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4594
4595 auto shouldImportValueAsDecl = [&](GlobalValueSummary *GVS) -> bool {
4596 if (DecSummaries == nullptr)
4597 return false;
4598 return DecSummaries->count(GVS);
4599 };
4600
4601 // The aliases are emitted as a post-pass, and will point to the value
4602 // id of the aliasee. Save them in a vector for post-processing.
4604
4605 // Save the value id for each summary for alias emission.
4607
4609
4610 // Set that will be populated during call to writeFunctionTypeMetadataRecords
4611 // with the type ids referenced by this index file.
4612 std::set<GlobalValue::GUID> ReferencedTypeIds;
4613
4614 // For local linkage, we also emit the original name separately
4615 // immediately after the record.
4616 auto MaybeEmitOriginalName = [&](GlobalValueSummary &S) {
4617 // We don't need to emit the original name if we are writing the index for
4618 // distributed backends (in which case ModuleToSummariesForIndex is
4619 // non-null). The original name is only needed during the thin link, since
4620 // for SamplePGO the indirect call targets for local functions have
4621 // have the original name annotated in profile.
4622 // Continue to emit it when writing out the entire combined index, which is
4623 // used in testing the thin link via llvm-lto.
4624 if (ModuleToSummariesForIndex || !GlobalValue::isLocalLinkage(S.linkage()))
4625 return;
4626 NameVals.push_back(S.getOriginalName());
4628 NameVals.clear();
4629 };
4630
4631 std::set<GlobalValue::GUID> DefOrUseGUIDs;
4632 forEachSummary([&](GVInfo I, bool IsAliasee) {
4633 GlobalValueSummary *S = I.second;
4634 assert(S);
4635 DefOrUseGUIDs.insert(I.first);
4636 for (const ValueInfo &VI : S->refs())
4637 DefOrUseGUIDs.insert(VI.getGUID());
4638
4639 auto ValueId = getValueId(I.first);
4640 assert(ValueId);
4641 SummaryToValueIdMap[S] = *ValueId;
4642
4643 // If this is invoked for an aliasee, we want to record the above
4644 // mapping, but then not emit a summary entry (if the aliasee is
4645 // to be imported, we will invoke this separately with IsAliasee=false).
4646 if (IsAliasee)
4647 return;
4648
4649 if (auto *AS = dyn_cast<AliasSummary>(S)) {
4650 // Will process aliases as a post-pass because the reader wants all
4651 // global to be loaded first.
4652 Aliases.push_back(AS);
4653 return;
4654 }
4655
4656 if (auto *VS = dyn_cast<GlobalVarSummary>(S)) {
4657 NameVals.push_back(*ValueId);
4658 assert(ModuleIdMap.count(VS->modulePath()));
4659 NameVals.push_back(ModuleIdMap[VS->modulePath()]);
4660 NameVals.push_back(getEncodedGVSummaryFlags(VS->flags()));
4661 NameVals.push_back(getEncodedGVarFlags(VS->varflags()));
4662 for (auto &RI : VS->refs()) {
4663 auto RefValueId = getValueId(RI.getGUID());
4664 if (!RefValueId)
4665 continue;
4666 NameVals.push_back(*RefValueId);
4667 }
4668
4669 // Emit the finished record.
4671 FSModRefsAbbrev);
4672 NameVals.clear();
4673 MaybeEmitOriginalName(*S);
4674 return;
4675 }
4676
4677 auto GetValueId = [&](const ValueInfo &VI) -> std::optional<unsigned> {
4678 if (!VI)
4679 return std::nullopt;
4680 return getValueId(VI.getGUID());
4681 };
4682
4683 auto *FS = cast<FunctionSummary>(S);
4684 writeFunctionTypeMetadataRecords(Stream, FS, GetValueId);
4685 getReferencedTypeIds(FS, ReferencedTypeIds);
4686
4688 Stream, FS, CallsiteAbbrev, AllocAbbrev,
4689 /*PerModule*/ false,
4690 /*GetValueId*/
4691 [&](const ValueInfo &VI) -> unsigned {
4692 std::optional<unsigned> ValueID = GetValueId(VI);
4693 // This can happen in shared index files for distributed ThinLTO if
4694 // the callee function summary is not included. Record 0 which we
4695 // will have to deal with conservatively when doing any kind of
4696 // validation in the ThinLTO backends.
4697 if (!ValueID)
4698 return 0;
4699 return *ValueID;
4700 },
4701 /*GetStackIndex*/
4702 [&](unsigned I) {
4703 // Get the corresponding index into the list of StackIds actually
4704 // being written for this combined index (which may be a subset in
4705 // the case of distributed indexes).
4706 assert(StackIdIndicesToIndex.contains(I));
4707 return StackIdIndicesToIndex[I];
4708 });
4709
4710 NameVals.push_back(*ValueId);
4711 assert(ModuleIdMap.count(FS->modulePath()));
4712 NameVals.push_back(ModuleIdMap[FS->modulePath()]);
4713 NameVals.push_back(
4714 getEncodedGVSummaryFlags(FS->flags(), shouldImportValueAsDecl(FS)));
4715 NameVals.push_back(FS->instCount());
4716 NameVals.push_back(getEncodedFFlags(FS->fflags()));
4717 NameVals.push_back(FS->entryCount());
4718
4719 // Fill in below
4720 NameVals.push_back(0); // numrefs
4721 NameVals.push_back(0); // rorefcnt
4722 NameVals.push_back(0); // worefcnt
4723
4724 unsigned Count = 0, RORefCnt = 0, WORefCnt = 0;
4725 for (auto &RI : FS->refs()) {
4726 auto RefValueId = getValueId(RI.getGUID());
4727 if (!RefValueId)
4728 continue;
4729 NameVals.push_back(*RefValueId);
4730 if (RI.isReadOnly())
4731 RORefCnt++;
4732 else if (RI.isWriteOnly())
4733 WORefCnt++;
4734 Count++;
4735 }
4736 NameVals[6] = Count;
4737 NameVals[7] = RORefCnt;
4738 NameVals[8] = WORefCnt;
4739
4740 for (auto &EI : FS->calls()) {
4741 // If this GUID doesn't have a value id, it doesn't have a function
4742 // summary and we don't need to record any calls to it.
4743 std::optional<unsigned> CallValueId = GetValueId(EI.first);
4744 if (!CallValueId)
4745 continue;
4746 NameVals.push_back(*CallValueId);
4747 NameVals.push_back(getEncodedHotnessCallEdgeInfo(EI.second));
4748 }
4749
4750 // Emit the finished record.
4751 Stream.EmitRecord(bitc::FS_COMBINED_PROFILE, NameVals,
4752 FSCallsProfileAbbrev);
4753 NameVals.clear();
4754 MaybeEmitOriginalName(*S);
4755 });
4756
4757 for (auto *AS : Aliases) {
4758 auto AliasValueId = SummaryToValueIdMap[AS];
4759 assert(AliasValueId);
4760 NameVals.push_back(AliasValueId);
4761 assert(ModuleIdMap.count(AS->modulePath()));
4762 NameVals.push_back(ModuleIdMap[AS->modulePath()]);
4763 NameVals.push_back(
4764 getEncodedGVSummaryFlags(AS->flags(), shouldImportValueAsDecl(AS)));
4765 auto AliaseeValueId = SummaryToValueIdMap[&AS->getAliasee()];
4766 assert(AliaseeValueId);
4767 NameVals.push_back(AliaseeValueId);
4768
4769 // Emit the finished record.
4770 Stream.EmitRecord(bitc::FS_COMBINED_ALIAS, NameVals, FSAliasAbbrev);
4771 NameVals.clear();
4772 MaybeEmitOriginalName(*AS);
4773
4774 if (auto *FS = dyn_cast<FunctionSummary>(&AS->getAliasee()))
4775 getReferencedTypeIds(FS, ReferencedTypeIds);
4776 }
4777
4778 if (!Index.cfiFunctionDefs().empty()) {
4779 for (auto &S : Index.cfiFunctionDefs()) {
4780 if (DefOrUseGUIDs.count(
4782 NameVals.push_back(StrtabBuilder.add(S));
4783 NameVals.push_back(S.size());
4784 }
4785 }
4786 if (!NameVals.empty()) {
4787 Stream.EmitRecord(bitc::FS_CFI_FUNCTION_DEFS, NameVals);
4788 NameVals.clear();
4789 }
4790 }
4791
4792 if (!Index.cfiFunctionDecls().empty()) {
4793 for (auto &S : Index.cfiFunctionDecls()) {
4794 if (DefOrUseGUIDs.count(
4796 NameVals.push_back(StrtabBuilder.add(S));
4797 NameVals.push_back(S.size());
4798 }
4799 }
4800 if (!NameVals.empty()) {
4801 Stream.EmitRecord(bitc::FS_CFI_FUNCTION_DECLS, NameVals);
4802 NameVals.clear();
4803 }
4804 }
4805
4806 // Walk the GUIDs that were referenced, and write the
4807 // corresponding type id records.
4808 for (auto &T : ReferencedTypeIds) {
4809 auto TidIter = Index.typeIds().equal_range(T);
4810 for (auto It = TidIter.first; It != TidIter.second; ++It) {
4811 writeTypeIdSummaryRecord(NameVals, StrtabBuilder, It->second.first,
4812 It->second.second);
4813 Stream.EmitRecord(bitc::FS_TYPE_ID, NameVals);
4814 NameVals.clear();
4815 }
4816 }
4817
4818 if (Index.getBlockCount())
4820 ArrayRef<uint64_t>{Index.getBlockCount()});
4821
4822 Stream.ExitBlock();
4823}
4824
4825/// Create the "IDENTIFICATION_BLOCK_ID" containing a single string with the
4826/// current llvm version, and a record for the epoch number.
4829
4830 // Write the "user readable" string identifying the bitcode producer
4831 auto Abbv = std::make_shared<BitCodeAbbrev>();
4835 auto StringAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4837 "LLVM" LLVM_VERSION_STRING, StringAbbrev);
4838
4839 // Write the epoch version
4840 Abbv = std::make_shared<BitCodeAbbrev>();
4843 auto EpochAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4844 constexpr std::array<unsigned, 1> Vals = {{bitc::BITCODE_CURRENT_EPOCH}};
4845 Stream.EmitRecord(bitc::IDENTIFICATION_CODE_EPOCH, Vals, EpochAbbrev);
4846 Stream.ExitBlock();
4847}
4848
4849void ModuleBitcodeWriter::writeModuleHash(StringRef View) {
4850 // Emit the module's hash.
4851 // MODULE_CODE_HASH: [5*i32]
4852 if (GenerateHash) {
4853 uint32_t Vals[5];
4854 Hasher.update(ArrayRef<uint8_t>(
4855 reinterpret_cast<const uint8_t *>(View.data()), View.size()));
4856 std::array<uint8_t, 20> Hash = Hasher.result();
4857 for (int Pos = 0; Pos < 20; Pos += 4) {
4858 Vals[Pos / 4] = support::endian::read32be(Hash.data() + Pos);
4859 }
4860
4861 // Emit the finished record.
4862 Stream.EmitRecord(bitc::MODULE_CODE_HASH, Vals);
4863
4864 if (ModHash)
4865 // Save the written hash value.
4866 llvm::copy(Vals, std::begin(*ModHash));
4867 }
4868}
4869
4870void ModuleBitcodeWriter::write() {
4872
4874 // We will want to write the module hash at this point. Block any flushing so
4875 // we can have access to the whole underlying data later.
4876 Stream.markAndBlockFlushing();
4877
4878 writeModuleVersion();
4879
4880 // Emit blockinfo, which defines the standard abbreviations etc.
4881 writeBlockInfo();
4882
4883 // Emit information describing all of the types in the module.
4884 writeTypeTable();
4885
4886 // Emit information about attribute groups.
4887 writeAttributeGroupTable();
4888
4889 // Emit information about parameter attributes.
4890 writeAttributeTable();
4891
4892 writeComdats();
4893
4894 // Emit top-level description of module, including target triple, inline asm,
4895 // descriptors for global variables, and function prototype info.
4896 writeModuleInfo();
4897
4898 // Emit constants.
4899 writeModuleConstants();
4900
4901 // Emit metadata kind names.
4902 writeModuleMetadataKinds();
4903
4904 // Emit metadata.
4905 writeModuleMetadata();
4906
4907 // Emit module-level use-lists.
4909 writeUseListBlock(nullptr);
4910
4911 writeOperandBundleTags();
4912 writeSyncScopeNames();
4913
4914 // Emit function bodies.
4915 DenseMap<const Function *, uint64_t> FunctionToBitcodeIndex;
4916 for (const Function &F : M)
4917 if (!F.isDeclaration())
4918 writeFunction(F, FunctionToBitcodeIndex);
4919
4920 // Need to write after the above call to WriteFunction which populates
4921 // the summary information in the index.
4922 if (Index)
4923 writePerModuleGlobalValueSummary();
4924
4925 writeGlobalValueSymbolTable(FunctionToBitcodeIndex);
4926
4927 writeModuleHash(Stream.getMarkedBufferAndResumeFlushing());
4928
4929 Stream.ExitBlock();
4930}
4931
4933 uint32_t &Position) {
4934 support::endian::write32le(&Buffer[Position], Value);
4935 Position += 4;
4936}
4937
4938/// If generating a bc file on darwin, we have to emit a
4939/// header and trailer to make it compatible with the system archiver. To do
4940/// this we emit the following header, and then emit a trailer that pads the
4941/// file out to be a multiple of 16 bytes.
4942///
4943/// struct bc_header {
4944/// uint32_t Magic; // 0x0B17C0DE
4945/// uint32_t Version; // Version, currently always 0.
4946/// uint32_t BitcodeOffset; // Offset to traditional bitcode file.
4947/// uint32_t BitcodeSize; // Size of traditional bitcode file.
4948/// uint32_t CPUType; // CPU specifier.
4949/// ... potentially more later ...
4950/// };
4952 const Triple &TT) {
4953 unsigned CPUType = ~0U;
4954
4955 // Match x86_64-*, i[3-9]86-*, powerpc-*, powerpc64-*, arm-*, thumb-*,
4956 // armv[0-9]-*, thumbv[0-9]-*, armv5te-*, or armv6t2-*. The CPUType is a magic
4957 // number from /usr/include/mach/machine.h. It is ok to reproduce the
4958 // specific constants here because they are implicitly part of the Darwin ABI.
4959 enum {
4960 DARWIN_CPU_ARCH_ABI64 = 0x01000000,
4961 DARWIN_CPU_TYPE_X86 = 7,
4962 DARWIN_CPU_TYPE_ARM = 12,
4963 DARWIN_CPU_TYPE_POWERPC = 18
4964 };
4965
4966 Triple::ArchType Arch = TT.getArch();
4967 if (Arch == Triple::x86_64)
4968 CPUType = DARWIN_CPU_TYPE_X86 | DARWIN_CPU_ARCH_ABI64;
4969 else if (Arch == Triple::x86)
4970 CPUType = DARWIN_CPU_TYPE_X86;
4971 else if (Arch == Triple::ppc)
4972 CPUType = DARWIN_CPU_TYPE_POWERPC;
4973 else if (Arch == Triple::ppc64)
4974 CPUType = DARWIN_CPU_TYPE_POWERPC | DARWIN_CPU_ARCH_ABI64;
4975 else if (Arch == Triple::arm || Arch == Triple::thumb)
4976 CPUType = DARWIN_CPU_TYPE_ARM;
4977
4978 // Traditional Bitcode starts after header.
4979 assert(Buffer.size() >= BWH_HeaderSize &&
4980 "Expected header size to be reserved");
4981 unsigned BCOffset = BWH_HeaderSize;
4982 unsigned BCSize = Buffer.size() - BWH_HeaderSize;
4983
4984 // Write the magic and version.
4985 unsigned Position = 0;
4986 writeInt32ToBuffer(0x0B17C0DE, Buffer, Position);
4987 writeInt32ToBuffer(0, Buffer, Position); // Version.
4988 writeInt32ToBuffer(BCOffset, Buffer, Position);
4989 writeInt32ToBuffer(BCSize, Buffer, Position);
4990 writeInt32ToBuffer(CPUType, Buffer, Position);
4991
4992 // If the file is not a multiple of 16 bytes, insert dummy padding.
4993 while (Buffer.size() & 15)
4994 Buffer.push_back(0);
4995}
4996
4997/// Helper to write the header common to all bitcode files.
4999 // Emit the file header.
5000 Stream.Emit((unsigned)'B', 8);
5001 Stream.Emit((unsigned)'C', 8);
5002 Stream.Emit(0x0, 4);
5003 Stream.Emit(0xC, 4);
5004 Stream.Emit(0xE, 4);
5005 Stream.Emit(0xD, 4);
5006}
5007
5009 : Stream(new BitstreamWriter(Buffer)) {
5010 writeBitcodeHeader(*Stream);
5011}
5012
5014 : Stream(new BitstreamWriter(FS, FlushThreshold)) {
5015 writeBitcodeHeader(*Stream);
5016}
5017
5019
5020void BitcodeWriter::writeBlob(unsigned Block, unsigned Record, StringRef Blob) {
5021 Stream->EnterSubblock(Block, 3);
5022
5023 auto Abbv = std::make_shared<BitCodeAbbrev>();
5024 Abbv->Add(BitCodeAbbrevOp(Record));
5026 auto AbbrevNo = Stream->EmitAbbrev(std::move(Abbv));
5027
5028 Stream->EmitRecordWithBlob(AbbrevNo, ArrayRef<uint64_t>{Record}, Blob);
5029
5030 Stream->ExitBlock();
5031}
5032
5034 assert(!WroteStrtab && !WroteSymtab);
5035
5036 // If any module has module-level inline asm, we will require a registered asm
5037 // parser for the target so that we can create an accurate symbol table for
5038 // the module.
5039 for (Module *M : Mods) {
5040 if (M->getModuleInlineAsm().empty())
5041 continue;
5042
5043 std::string Err;
5044 const Triple TT(M->getTargetTriple());
5045 const Target *T = TargetRegistry::lookupTarget(TT.str(), Err);
5046 if (!T || !T->hasMCAsmParser())
5047 return;
5048 }
5049
5050 WroteSymtab = true;
5051 SmallVector<char, 0> Symtab;
5052 // The irsymtab::build function may be unable to create a symbol table if the
5053 // module is malformed (e.g. it contains an invalid alias). Writing a symbol
5054 // table is not required for correctness, but we still want to be able to
5055 // write malformed modules to bitcode files, so swallow the error.
5056 if (Error E = irsymtab::build(Mods, Symtab, StrtabBuilder, Alloc)) {
5057 consumeError(std::move(E));
5058 return;
5059 }
5060
5062 {Symtab.data(), Symtab.size()});
5063}
5064
5066 assert(!WroteStrtab);
5067
5068 std::vector<char> Strtab;
5069 StrtabBuilder.finalizeInOrder();
5070 Strtab.resize(StrtabBuilder.getSize());
5071 StrtabBuilder.write((uint8_t *)Strtab.data());
5072
5074 {Strtab.data(), Strtab.size()});
5075
5076 WroteStrtab = true;
5077}
5078
5080 writeBlob(bitc::STRTAB_BLOCK_ID, bitc::STRTAB_BLOB, Strtab);
5081 WroteStrtab = true;
5082}
5083
5085 bool ShouldPreserveUseListOrder,
5087 bool GenerateHash, ModuleHash *ModHash) {
5088 assert(!WroteStrtab);
5089
5090 // The Mods vector is used by irsymtab::build, which requires non-const
5091 // Modules in case it needs to materialize metadata. But the bitcode writer
5092 // requires that the module is materialized, so we can cast to non-const here,
5093 // after checking that it is in fact materialized.
5094 assert(M.isMaterialized());
5095 Mods.push_back(const_cast<Module *>(&M));
5096
5097 ModuleBitcodeWriter ModuleWriter(M, StrtabBuilder, *Stream,
5098 ShouldPreserveUseListOrder, Index,
5099 GenerateHash, ModHash);
5100 ModuleWriter.write();
5101}
5102
5105 const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex,
5106 const GVSummaryPtrSet *DecSummaries) {
5107 IndexBitcodeWriter IndexWriter(*Stream, StrtabBuilder, *Index, DecSummaries,
5108 ModuleToSummariesForIndex);
5109 IndexWriter.write();
5110}
5111
5112/// Write the specified module to the specified output stream.
5114 bool ShouldPreserveUseListOrder,
5116 bool GenerateHash, ModuleHash *ModHash) {
5117 auto Write = [&](BitcodeWriter &Writer) {
5118 Writer.writeModule(M, ShouldPreserveUseListOrder, Index, GenerateHash,
5119 ModHash);
5120 Writer.writeSymtab();
5121 Writer.writeStrtab();
5122 };
5123 Triple TT(M.getTargetTriple());
5124 if (TT.isOSDarwin() || TT.isOSBinFormatMachO()) {
5125 // If this is darwin or another generic macho target, reserve space for the
5126 // header. Note that the header is computed *after* the output is known, so
5127 // we currently explicitly use a buffer, write to it, and then subsequently
5128 // flush to Out.
5129 SmallVector<char, 0> Buffer;
5130 Buffer.reserve(256 * 1024);
5131 Buffer.insert(Buffer.begin(), BWH_HeaderSize, 0);
5132 BitcodeWriter Writer(Buffer);
5133 Write(Writer);
5134 emitDarwinBCHeaderAndTrailer(Buffer, TT);
5135 Out.write(Buffer.data(), Buffer.size());
5136 } else {
5137 BitcodeWriter Writer(Out);
5138 Write(Writer);
5139 }
5140}
5141
5142void IndexBitcodeWriter::write() {
5144
5145 writeModuleVersion();
5146
5147 // Write the module paths in the combined index.
5148 writeModStrings();
5149
5150 // Write the summary combined index records.
5151 writeCombinedGlobalValueSummary();
5152
5153 Stream.ExitBlock();
5154}
5155
5156// Write the specified module summary index to the given raw output stream,
5157// where it will be written in a new bitcode block. This is used when
5158// writing the combined index file for ThinLTO. When writing a subset of the
5159// index for a distributed backend, provide a \p ModuleToSummariesForIndex map.
5162 const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex,
5163 const GVSummaryPtrSet *DecSummaries) {
5164 SmallVector<char, 0> Buffer;
5165 Buffer.reserve(256 * 1024);
5166
5167 BitcodeWriter Writer(Buffer);
5168 Writer.writeIndex(&Index, ModuleToSummariesForIndex, DecSummaries);
5169 Writer.writeStrtab();
5170
5171 Out.write((char *)&Buffer.front(), Buffer.size());
5172}
5173
5174namespace {
5175
5176/// Class to manage the bitcode writing for a thin link bitcode file.
5177class ThinLinkBitcodeWriter : public ModuleBitcodeWriterBase {
5178 /// ModHash is for use in ThinLTO incremental build, generated while writing
5179 /// the module bitcode file.
5180 const ModuleHash *ModHash;
5181
5182public:
5183 ThinLinkBitcodeWriter(const Module &M, StringTableBuilder &StrtabBuilder,
5184 BitstreamWriter &Stream,
5186 const ModuleHash &ModHash)
5187 : ModuleBitcodeWriterBase(M, StrtabBuilder, Stream,
5188 /*ShouldPreserveUseListOrder=*/false, &Index),
5189 ModHash(&ModHash) {}
5190
5191 void write();
5192
5193private:
5194 void writeSimplifiedModuleInfo();
5195};
5196
5197} // end anonymous namespace
5198
5199// This function writes a simpilified module info for thin link bitcode file.
5200// It only contains the source file name along with the name(the offset and
5201// size in strtab) and linkage for global values. For the global value info
5202// entry, in order to keep linkage at offset 5, there are three zeros used
5203// as padding.
5204void ThinLinkBitcodeWriter::writeSimplifiedModuleInfo() {
5206 // Emit the module's source file name.
5207 {
5208 StringEncoding Bits = getStringEncoding(M.getSourceFileName());
5210 if (Bits == SE_Char6)
5211 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Char6);
5212 else if (Bits == SE_Fixed7)
5213 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7);
5214
5215 // MODULE_CODE_SOURCE_FILENAME: [namechar x N]
5216 auto Abbv = std::make_shared<BitCodeAbbrev>();
5219 Abbv->Add(AbbrevOpToUse);
5220 unsigned FilenameAbbrev = Stream.EmitAbbrev(std::move(Abbv));
5221
5222 for (const auto P : M.getSourceFileName())
5223 Vals.push_back((unsigned char)P);
5224
5225 Stream.EmitRecord(bitc::MODULE_CODE_SOURCE_FILENAME, Vals, FilenameAbbrev);
5226 Vals.clear();
5227 }
5228
5229 // Emit the global variable information.
5230 for (const GlobalVariable &GV : M.globals()) {
5231 // GLOBALVAR: [strtab offset, strtab size, 0, 0, 0, linkage]
5232 Vals.push_back(StrtabBuilder.add(GV.getName()));
5233 Vals.push_back(GV.getName().size());
5234 Vals.push_back(0);
5235 Vals.push_back(0);
5236 Vals.push_back(0);
5237 Vals.push_back(getEncodedLinkage(GV));
5238
5240 Vals.clear();
5241 }
5242
5243 // Emit the function proto information.
5244 for (const Function &F : M) {
5245 // FUNCTION: [strtab offset, strtab size, 0, 0, 0, linkage]
5246 Vals.push_back(StrtabBuilder.add(F.getName()));
5247 Vals.push_back(F.getName().size());
5248 Vals.push_back(0);
5249 Vals.push_back(0);
5250 Vals.push_back(0);
5252
5254 Vals.clear();
5255 }
5256
5257 // Emit the alias information.
5258 for (const GlobalAlias &A : M.aliases()) {
5259 // ALIAS: [strtab offset, strtab size, 0, 0, 0, linkage]
5260 Vals.push_back(StrtabBuilder.add(A.getName()));
5261 Vals.push_back(A.getName().size());
5262 Vals.push_back(0);
5263 Vals.push_back(0);
5264 Vals.push_back(0);
5266
5267 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals);
5268 Vals.clear();
5269 }
5270
5271 // Emit the ifunc information.
5272 for (const GlobalIFunc &I : M.ifuncs()) {
5273 // IFUNC: [strtab offset, strtab size, 0, 0, 0, linkage]
5274 Vals.push_back(StrtabBuilder.add(I.getName()));
5275 Vals.push_back(I.getName().size());
5276 Vals.push_back(0);
5277 Vals.push_back(0);
5278 Vals.push_back(0);
5280
5281 Stream.EmitRecord(bitc::MODULE_CODE_IFUNC, Vals);
5282 Vals.clear();
5283 }
5284}
5285
5286void ThinLinkBitcodeWriter::write() {
5288
5289 writeModuleVersion();
5290
5291 writeSimplifiedModuleInfo();
5292
5293 writePerModuleGlobalValueSummary();
5294
5295 // Write module hash.
5297
5298 Stream.ExitBlock();
5299}
5300
5303 const ModuleHash &ModHash) {
5304 assert(!WroteStrtab);
5305
5306 // The Mods vector is used by irsymtab::build, which requires non-const
5307 // Modules in case it needs to materialize metadata. But the bitcode writer
5308 // requires that the module is materialized, so we can cast to non-const here,
5309 // after checking that it is in fact materialized.
5310 assert(M.isMaterialized());
5311 Mods.push_back(const_cast<Module *>(&M));
5312
5313 ThinLinkBitcodeWriter ThinLinkWriter(M, StrtabBuilder, *Stream, Index,
5314 ModHash);
5315 ThinLinkWriter.write();
5316}
5317
5318// Write the specified thin link bitcode file to the given raw output stream,
5319// where it will be written in a new bitcode block. This is used when
5320// writing the per-module index file for ThinLTO.
5323 const ModuleHash &ModHash) {
5324 SmallVector<char, 0> Buffer;
5325 Buffer.reserve(256 * 1024);
5326
5327 BitcodeWriter Writer(Buffer);
5328 Writer.writeThinLinkBitcode(M, Index, ModHash);
5329 Writer.writeSymtab();
5330 Writer.writeStrtab();
5331
5332 Out.write((char *)&Buffer.front(), Buffer.size());
5333}
5334
5335static const char *getSectionNameForBitcode(const Triple &T) {
5336 switch (T.getObjectFormat()) {
5337 case Triple::MachO:
5338 return "__LLVM,__bitcode";
5339 case Triple::COFF:
5340 case Triple::ELF:
5341 case Triple::Wasm:
5343 return ".llvmbc";
5344 case Triple::GOFF:
5345 llvm_unreachable("GOFF is not yet implemented");
5346 break;
5347 case Triple::SPIRV:
5348 if (T.getVendor() == Triple::AMD)
5349 return ".llvmbc";
5350 llvm_unreachable("SPIRV is not yet implemented");
5351 break;
5352 case Triple::XCOFF:
5353 llvm_unreachable("XCOFF is not yet implemented");
5354 break;
5356 llvm_unreachable("DXContainer is not yet implemented");
5357 break;
5358 }
5359 llvm_unreachable("Unimplemented ObjectFormatType");
5360}
5361
5362static const char *getSectionNameForCommandline(const Triple &T) {
5363 switch (T.getObjectFormat()) {
5364 case Triple::MachO:
5365 return "__LLVM,__cmdline";
5366 case Triple::COFF:
5367 case Triple::ELF:
5368 case Triple::Wasm:
5370 return ".llvmcmd";
5371 case Triple::GOFF:
5372 llvm_unreachable("GOFF is not yet implemented");
5373 break;
5374 case Triple::SPIRV:
5375 if (T.getVendor() == Triple::AMD)
5376 return ".llvmcmd";
5377 llvm_unreachable("SPIRV is not yet implemented");
5378 break;
5379 case Triple::XCOFF:
5380 llvm_unreachable("XCOFF is not yet implemented");
5381 break;
5383 llvm_unreachable("DXC is not yet implemented");
5384 break;
5385 }
5386 llvm_unreachable("Unimplemented ObjectFormatType");
5387}
5388
5390 bool EmbedBitcode, bool EmbedCmdline,
5391 const std::vector<uint8_t> &CmdArgs) {
5392 // Save llvm.compiler.used and remove it.
5395 Type *UsedElementType = PointerType::getUnqual(M.getContext());
5396 GlobalVariable *Used = collectUsedGlobalVariables(M, UsedGlobals, true);
5397 for (auto *GV : UsedGlobals) {
5398 if (GV->getName() != "llvm.embedded.module" &&
5399 GV->getName() != "llvm.cmdline")
5400 UsedArray.push_back(
5402 }
5403 if (Used)
5404 Used->eraseFromParent();
5405
5406 // Embed the bitcode for the llvm module.
5407 std::string Data;
5408 ArrayRef<uint8_t> ModuleData;
5409 Triple T(M.getTargetTriple());
5410
5411 if (EmbedBitcode) {
5412 if (Buf.getBufferSize() == 0 ||
5413 !isBitcode((const unsigned char *)Buf.getBufferStart(),
5414 (const unsigned char *)Buf.getBufferEnd())) {
5415 // If the input is LLVM Assembly, bitcode is produced by serializing
5416 // the module. Use-lists order need to be preserved in this case.
5418 llvm::WriteBitcodeToFile(M, OS, /* ShouldPreserveUseListOrder */ true);
5419 ModuleData =
5420 ArrayRef<uint8_t>((const uint8_t *)OS.str().data(), OS.str().size());
5421 } else
5422 // If the input is LLVM bitcode, write the input byte stream directly.
5423 ModuleData = ArrayRef<uint8_t>((const uint8_t *)Buf.getBufferStart(),
5424 Buf.getBufferSize());
5425 }
5426 llvm::Constant *ModuleConstant =
5427 llvm::ConstantDataArray::get(M.getContext(), ModuleData);
5429 M, ModuleConstant->getType(), true, llvm::GlobalValue::PrivateLinkage,
5430 ModuleConstant);
5432 // Set alignment to 1 to prevent padding between two contributions from input
5433 // sections after linking.
5434 GV->setAlignment(Align(1));
5435 UsedArray.push_back(
5437 if (llvm::GlobalVariable *Old =
5438 M.getGlobalVariable("llvm.embedded.module", true)) {
5439 assert(Old->hasZeroLiveUses() &&
5440 "llvm.embedded.module can only be used once in llvm.compiler.used");
5441 GV->takeName(Old);
5442 Old->eraseFromParent();
5443 } else {
5444 GV->setName("llvm.embedded.module");
5445 }
5446
5447 // Skip if only bitcode needs to be embedded.
5448 if (EmbedCmdline) {
5449 // Embed command-line options.
5450 ArrayRef<uint8_t> CmdData(const_cast<uint8_t *>(CmdArgs.data()),
5451 CmdArgs.size());
5452 llvm::Constant *CmdConstant =
5453 llvm::ConstantDataArray::get(M.getContext(), CmdData);
5454 GV = new llvm::GlobalVariable(M, CmdConstant->getType(), true,
5456 CmdConstant);
5458 GV->setAlignment(Align(1));
5459 UsedArray.push_back(
5461 if (llvm::GlobalVariable *Old = M.getGlobalVariable("llvm.cmdline", true)) {
5462 assert(Old->hasZeroLiveUses() &&
5463 "llvm.cmdline can only be used once in llvm.compiler.used");
5464 GV->takeName(Old);
5465 Old->eraseFromParent();
5466 } else {
5467 GV->setName("llvm.cmdline");
5468 }
5469 }
5470
5471 if (UsedArray.empty())
5472 return;
5473
5474 // Recreate llvm.compiler.used.
5475 ArrayType *ATy = ArrayType::get(UsedElementType, UsedArray.size());
5476 auto *NewUsed = new GlobalVariable(
5478 llvm::ConstantArray::get(ATy, UsedArray), "llvm.compiler.used");
5479 NewUsed->setSection("llvm.metadata");
5480}
This file defines the StringMap class.
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static void writeDIMacro(raw_ostream &Out, const DIMacro *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2353
static void writeDIGlobalVariableExpression(raw_ostream &Out, const DIGlobalVariableExpression *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2500
static void writeDICompositeType(raw_ostream &Out, const DICompositeType *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2191
static void writeDIStringType(raw_ostream &Out, const DIStringType *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2140
static void writeDIGlobalVariable(raw_ostream &Out, const DIGlobalVariable *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2414
static void writeDIModule(raw_ostream &Out, const DIModule *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2374
static void writeDIFile(raw_ostream &Out, const DIFile *N, AsmWriterContext &)
Definition: AsmWriter.cpp:2235
static void writeDISubroutineType(raw_ostream &Out, const DISubroutineType *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2224
static void writeDILabel(raw_ostream &Out, const DILabel *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2449
static void writeDIDerivedType(raw_ostream &Out, const DIDerivedType *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2158
static void writeDIImportedEntity(raw_ostream &Out, const DIImportedEntity *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2524
static void writeDIObjCProperty(raw_ostream &Out, const DIObjCProperty *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2510
static void writeDISubprogram(raw_ostream &Out, const DISubprogram *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2280
static void writeDILocation(raw_ostream &Out, const DILocation *DL, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2001
static void writeDINamespace(raw_ostream &Out, const DINamespace *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2331
static void writeDICommonBlock(raw_ostream &Out, const DICommonBlock *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2341
static void writeDIBasicType(raw_ostream &Out, const DIBasicType *N, AsmWriterContext &)
Definition: AsmWriter.cpp:2125
static void writeGenericDINode(raw_ostream &Out, const GenericDINode *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:1983
static void writeDILocalVariable(raw_ostream &Out, const DILocalVariable *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2433
static void writeDITemplateTypeParameter(raw_ostream &Out, const DITemplateTypeParameter *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2389
static void writeDICompileUnit(raw_ostream &Out, const DICompileUnit *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2250
static void writeDIGenericSubrange(raw_ostream &Out, const DIGenericSubrange *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2062
static void writeDISubrange(raw_ostream &Out, const DISubrange *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2021
static void writeDILexicalBlockFile(raw_ostream &Out, const DILexicalBlockFile *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2319
static void writeDIEnumerator(raw_ostream &Out, const DIEnumerator *N, AsmWriterContext &)
Definition: AsmWriter.cpp:2113
static void writeMDTuple(raw_ostream &Out, const MDTuple *Node, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:1762
static void writeDIExpression(raw_ostream &Out, const DIExpression *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2460
static void writeDIAssignID(raw_ostream &Out, const DIAssignID *DL, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2015
static void writeDILexicalBlock(raw_ostream &Out, const DILexicalBlock *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2308
static void writeDIArgList(raw_ostream &Out, const DIArgList *N, AsmWriterContext &WriterCtx, bool FromValue=false)
Definition: AsmWriter.cpp:2485
static void writeDITemplateValueParameter(raw_ostream &Out, const DITemplateValueParameter *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2400
static void writeDIMacroFile(raw_ostream &Out, const DIMacroFile *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2364
Atomic ordering constants.
This file contains the simple types necessary to represent the attributes associated with functions a...
static unsigned serializeSanitizerMetadata(const GlobalValue::SanitizerMetadata &Meta)
static void getReferencedTypeIds(FunctionSummary *FS, std::set< GlobalValue::GUID > &ReferencedTypeIds)
Collect type IDs from type tests used by function.
static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind)
static unsigned getEncodedUnaryOpcode(unsigned Opcode)
static void emitSignedInt64(SmallVectorImpl< uint64_t > &Vals, uint64_t V)
StringEncoding
@ SE_Char6
@ SE_Fixed7
@ SE_Fixed8
static unsigned getEncodedVisibility(const GlobalValue &GV)
static uint64_t getOptimizationFlags(const Value *V)
static unsigned getEncodedLinkage(const GlobalValue::LinkageTypes Linkage)
static unsigned getEncodedRMWOperation(AtomicRMWInst::BinOp Op)
static unsigned getEncodedThreadLocalMode(const GlobalValue &GV)
static void writeIdentificationBlock(BitstreamWriter &Stream)
Create the "IDENTIFICATION_BLOCK_ID" containing a single string with the current llvm version,...
static unsigned getEncodedCastOpcode(unsigned Opcode)
static cl::opt< bool > WriteRelBFToSummary("write-relbf-to-summary", cl::Hidden, cl::init(false), cl::desc("Write relative block frequency to function summary "))
static cl::opt< uint32_t > FlushThreshold("bitcode-flush-threshold", cl::Hidden, cl::init(512), cl::desc("The threshold (unit M) for flushing LLVM bitcode."))
static unsigned getEncodedOrdering(AtomicOrdering Ordering)
static unsigned getEncodedUnnamedAddr(const GlobalValue &GV)
bool WriteNewDbgInfoFormatToBitcode
Definition: BasicBlock.cpp:47
static unsigned getEncodedComdatSelectionKind(const Comdat &C)
static uint64_t getEncodedGVSummaryFlags(GlobalValueSummary::GVFlags Flags, bool ImportAsDecl=false)
static void emitDarwinBCHeaderAndTrailer(SmallVectorImpl< char > &Buffer, const Triple &TT)
If generating a bc file on darwin, we have to emit a header and trailer to make it compatible with th...
static void writeBitcodeHeader(BitstreamWriter &Stream)
Helper to write the header common to all bitcode files.
llvm::cl::opt< bool > UseNewDbgInfoFormat
static uint64_t getEncodedRelBFCallEdgeInfo(const CalleeInfo &CI)
static void writeWholeProgramDevirtResolutionByArg(SmallVector< uint64_t, 64 > &NameVals, const std::vector< uint64_t > &args, const WholeProgramDevirtResolution::ByArg &ByArg)
static void emitConstantRange(SmallVectorImpl< uint64_t > &Record, const ConstantRange &CR, bool EmitBitWidth)
static StringEncoding getStringEncoding(StringRef Str)
Determine the encoding to use for the given string name and length.
static uint64_t getEncodedGVarFlags(GlobalVarSummary::GVarFlags Flags)
static const char * getSectionNameForCommandline(const Triple &T)
static cl::opt< unsigned > IndexThreshold("bitcode-mdindex-threshold", cl::Hidden, cl::init(25), cl::desc("Number of metadatas above which we emit an index " "to enable lazy-loading"))
static void writeFunctionTypeMetadataRecords(BitstreamWriter &Stream, FunctionSummary *FS, Fn GetValueID)
Write the function type metadata related records that need to appear before a function summary entry ...
static uint64_t getEncodedHotnessCallEdgeInfo(const CalleeInfo &CI)
static void emitWideAPInt(SmallVectorImpl< uint64_t > &Vals, const APInt &A)
static void writeStringRecord(BitstreamWriter &Stream, unsigned Code, StringRef Str, unsigned AbbrevToUse)
static void writeWholeProgramDevirtResolution(SmallVector< uint64_t, 64 > &NameVals, StringTableBuilder &StrtabBuilder, uint64_t Id, const WholeProgramDevirtResolution &Wpd)
static unsigned getEncodedDLLStorageClass(const GlobalValue &GV)
static void writeTypeIdCompatibleVtableSummaryRecord(SmallVector< uint64_t, 64 > &NameVals, StringTableBuilder &StrtabBuilder, const std::string &Id, const TypeIdCompatibleVtableInfo &Summary, ValueEnumerator &VE)
static void writeInt32ToBuffer(uint32_t Value, SmallVectorImpl< char > &Buffer, uint32_t &Position)
MetadataAbbrev
@ LastPlusOne
static const char * getSectionNameForBitcode(const Triple &T)
static void writeFunctionHeapProfileRecords(BitstreamWriter &Stream, FunctionSummary *FS, unsigned CallsiteAbbrev, unsigned AllocAbbrev, bool PerModule, std::function< unsigned(const ValueInfo &VI)> GetValueID, std::function< unsigned(unsigned)> GetStackIndex)
static void writeTypeIdSummaryRecord(SmallVector< uint64_t, 64 > &NameVals, StringTableBuilder &StrtabBuilder, const std::string &Id, const TypeIdSummary &Summary)
static unsigned getEncodedBinaryOpcode(unsigned Opcode)
static uint64_t getEncodedFFlags(FunctionSummary::FFlags Flags)
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
This file defines the DenseMap class.
std::string Name
uint64_t Size
This file contains the declaration of the GlobalIFunc class, which represents a single indirect funct...
Hexagon Common GEP
#define _
static MaybeAlign getAlign(Value *Ptr)
Definition: IRBuilder.cpp:531
static cl::opt< LTOBitcodeEmbedding > EmbedBitcode("lto-embed-bitcode", cl::init(LTOBitcodeEmbedding::DoNotEmbed), cl::values(clEnumValN(LTOBitcodeEmbedding::DoNotEmbed, "none", "Do not embed"), clEnumValN(LTOBitcodeEmbedding::EmbedOptimized, "optimized", "Embed after all optimization passes"), clEnumValN(LTOBitcodeEmbedding::EmbedPostMergePreOptimized, "post-merge-pre-opt", "Embed post merge, but before optimizations")), cl::desc("Embed LLVM bitcode in object files produced by LTO"))
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
#define G(x, y, z)
Definition: MD5.cpp:56
#define H(x, y, z)
Definition: MD5.cpp:57
This file contains the declarations for metadata subclasses.
ModuleSummaryIndex.h This file contains the declarations the classes that hold the module index and s...
Module.h This file contains the declarations for the Module class.
nvptx lower args
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
#define P(N)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
raw_pwrite_stream & OS
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallPtrSet class.
This file defines the SmallString class.
This file defines the SmallVector class.
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type.
static const uint32_t IV[8]
Definition: blake3_impl.h:78
Class for arbitrary precision integers.
Definition: APInt.h:78
unsigned getNumWords() const
Get the number of words.
Definition: APInt.h:1455
unsigned getActiveWords() const
Compute the number of active words in the value of this APInt.
Definition: APInt.h:1478
const uint64_t * getRawData() const
This function returns a pointer to the internal storage of the APInt.
Definition: APInt.h:549
int64_t getSExtValue() const
Get sign extended value.
Definition: APInt.h:1522
Alias summary information.
const GlobalValueSummary & getAliasee() const
an instruction to allocate memory on the stack
Definition: Instructions.h:61
bool isSwiftError() const
Return true if this alloca is used as a swifterror argument to a call.
Definition: Instructions.h:147
Align getAlign() const
Return the alignment of the memory that is being allocated by the instruction.
Definition: Instructions.h:122
Type * getAllocatedType() const
Return the type that is being allocated by the instruction.
Definition: Instructions.h:115
bool isUsedWithInAlloca() const
Return true if this alloca is used as an inalloca argument to a call.
Definition: Instructions.h:137
unsigned getAddressSpace() const
Return the address space for the allocation.
Definition: Instructions.h:102
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:160
Class to represent array types.
Definition: DerivedTypes.h:371
static ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
Definition: Type.cpp:635
BinOp
This enumeration lists the possible modifications atomicrmw can make.
Definition: Instructions.h:708
@ Add
*p = old + v
Definition: Instructions.h:712
@ FAdd
*p = old + v
Definition: Instructions.h:733
@ Min
*p = old <signed v ? old : v
Definition: Instructions.h:726
@ Or
*p = old | v
Definition: Instructions.h:720
@ Sub
*p = old - v
Definition: Instructions.h:714
@ And
*p = old & v
Definition: Instructions.h:716
@ Xor
*p = old ^ v
Definition: Instructions.h:722
@ FSub
*p = old - v
Definition: Instructions.h:736
@ UIncWrap
Increment one up to a maximum value.
Definition: Instructions.h:748
@ Max
*p = old >signed v ? old : v
Definition: Instructions.h:724
@ UMin
*p = old <unsigned v ? old : v
Definition: Instructions.h:730
@ FMin
*p = minnum(old, v) minnum matches the behavior of llvm.minnum.
Definition: Instructions.h:744
@ UMax
*p = old >unsigned v ? old : v
Definition: Instructions.h:728
@ FMax
*p = maxnum(old, v) maxnum matches the behavior of llvm.maxnum.
Definition: Instructions.h:740
@ UDecWrap
Decrement one until a minimum value or zero.
Definition: Instructions.h:752
@ Nand
*p = ~(old & v)
Definition: Instructions.h:718
bool hasAttributes() const
Return true if attributes exists in this set.
Definition: Attributes.h:390
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results,...
Definition: Attributes.h:86
@ TombstoneKey
Use as Tombstone key for DenseMap of AttrKind.
Definition: Attributes.h:93
@ None
No attributes have been set.
Definition: Attributes.h:88
@ EmptyKey
Use as Empty key for DenseMap of AttrKind.
Definition: Attributes.h:92
@ EndAttrKinds
Sentinel value useful for loops.
Definition: Attributes.h:91
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
BitCodeAbbrevOp - This describes one or more operands in an abbreviation.
Definition: BitCodes.h:33
static bool isChar6(char C)
isChar6 - Return true if this character is legal in the Char6 encoding.
Definition: BitCodes.h:82
void writeThinLinkBitcode(const Module &M, const ModuleSummaryIndex &Index, const ModuleHash &ModHash)
Write the specified thin link bitcode file (i.e., the minimized bitcode file) to the buffer specified...
void copyStrtab(StringRef Strtab)
Copy the string table for another module into this bitcode file.
void writeStrtab()
Write the bitcode file's string table.
void writeSymtab()
Attempt to write a symbol table to the bitcode file.
void writeIndex(const ModuleSummaryIndex *Index, const std::map< std::string, GVSummaryMapTy > *ModuleToSummariesForIndex, const GVSummaryPtrSet *DecSummaries)
void writeModule(const Module &M, bool ShouldPreserveUseListOrder=false, const ModuleSummaryIndex *Index=nullptr, bool GenerateHash=false, ModuleHash *ModHash=nullptr)
Write the specified module to the buffer specified at construction time.
BitcodeWriter(SmallVectorImpl< char > &Buffer)
Create a BitcodeWriter that writes to Buffer.
unsigned EmitAbbrev(std::shared_ptr< BitCodeAbbrev > Abbv)
Emits the abbreviation Abbv to the stream.
void markAndBlockFlushing()
For scenarios where the user wants to access a section of the stream to (for example) compute some ch...
StringRef getMarkedBufferAndResumeFlushing()
resumes flushing, but does not flush, and returns the section in the internal buffer starting from th...
void EmitRecord(unsigned Code, const Container &Vals, unsigned Abbrev=0)
EmitRecord - Emit the specified record to the stream, using an abbrev if we have one to compress the ...
void Emit(uint32_t Val, unsigned NumBits)
void EmitRecordWithBlob(unsigned Abbrev, const Container &Vals, StringRef Blob)
EmitRecordWithBlob - Emit the specified record to the stream, using an abbrev that includes a blob at...
unsigned EmitBlockInfoAbbrev(unsigned BlockID, std::shared_ptr< BitCodeAbbrev > Abbv)
EmitBlockInfoAbbrev - Emit a DEFINE_ABBREV record for the specified BlockID.
void EnterBlockInfoBlock()
EnterBlockInfoBlock - Start emitting the BLOCKINFO_BLOCK.
void BackpatchWord(uint64_t BitNo, unsigned Val)
void BackpatchWord64(uint64_t BitNo, uint64_t Val)
void EnterSubblock(unsigned BlockID, unsigned CodeLen)
uint64_t GetCurrentBitNo() const
Retrieve the current position in the stream, in bits.
void EmitRecordWithAbbrev(unsigned Abbrev, const Container &Vals)
EmitRecordWithAbbrev - Emit a record with the specified abbreviation.
The address of a basic block.
Definition: Constants.h:890
static BlockAddress * lookup(const BasicBlock *BB)
Lookup an existing BlockAddress constant for the given BasicBlock.
Definition: Constants.cpp:1889
Conditional or Unconditional Branch instruction.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1236
OperandBundleUse getOperandBundleAt(unsigned Index) const
Return the operand bundle at a specific index.
Definition: InstrTypes.h:2112
unsigned getNumOperandBundles() const
Return the number of operand bundles associated with this User.
Definition: InstrTypes.h:2056
CallingConv::ID getCallingConv() const
Definition: InstrTypes.h:1523
Value * getCalledOperand() const
Definition: InstrTypes.h:1458
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1410
FunctionType * getFunctionType() const
Definition: InstrTypes.h:1323
unsigned arg_size() const
Definition: InstrTypes.h:1408
AttributeList getAttributes() const
Return the parameter attributes for this call.
Definition: InstrTypes.h:1542
bool hasOperandBundles() const
Return true if this User has any operand bundles.
Definition: InstrTypes.h:2061
CallBr instruction, tracking function calls that may not return control but instead transfer it to a ...
BasicBlock * getIndirectDest(unsigned i) const
BasicBlock * getDefaultDest() const
unsigned getNumIndirectDests() const
Return the number of callbr indirect dest labels.
This class represents a function call, abstracting a target machine's calling convention.
bool isNoTailCall() const
bool isTailCall() const
bool isMustTailCall() const
@ Largest
The linker will choose the largest COMDAT.
Definition: Comdat.h:38
@ SameSize
The data referenced by the COMDAT must be the same size.
Definition: Comdat.h:40
@ Any
The linker may choose any COMDAT.
Definition: Comdat.h:36
@ NoDeduplicate
No deduplication is performed.
Definition: Comdat.h:39
@ ExactMatch
The data referenced by the COMDAT must be the same.
Definition: Comdat.h:37
static Constant * get(ArrayType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:1292
static Constant * get(LLVMContext &Context, ArrayRef< ElementTy > Elts)
get() constructor - Return a constant with array type with an element count and element type matching...
Definition: Constants.h:706
ConstantDataSequential - A vector or array constant whose element type is a simple 1/2/4/8-byte integ...
Definition: Constants.h:584
A constant value that is initialized with an expression using other constant values.
Definition: Constants.h:1097
static Constant * getPointerBitCastOrAddrSpaceCast(Constant *C, Type *Ty)
Create a BitCast or AddrSpaceCast for a pointer type depending on the address space.
Definition: Constants.cpp:2242
ConstantFP - Floating Point Values [float, double].
Definition: Constants.h:269
This is the shared class of boolean and integer constants.
Definition: Constants.h:81
This class represents a range of values.
Definition: ConstantRange.h:47
const APInt & getLower() const
Return the lower value for this range.
const APInt & getUpper() const
Return the upper value for this range.
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
ConstantRange sextOrTrunc(uint32_t BitWidth) const
Make this range have the bit width given by BitWidth.
This is an important base class in LLVM.
Definition: Constant.h:42
List of ValueAsMetadata, to be used as an argument to a dbg.value intrinsic.
Assignment ID.
Basic type, like 'int' or 'float'.
Debug common block.
Enumeration value.
DWARF expression.
A pair of DIGlobalVariable and DIExpression.
An imported module (C++ using directive or similar).
Debug lexical block.
Debug location.
Represents a module in the programming language, for example, a Clang module, or a Fortran module.
Debug lexical block.
String type, Fortran CHARACTER(n)
Subprogram description.
Array subrange.
Type array for a subprogram.
This class represents an Operation in the Expression.
Records a position in IR for a source label (DILabel).
Base class for non-instruction debug metadata records that have positions within IR.
DebugLoc getDebugLoc() const
Record of a variable value-assignment, aka a non instruction representation of the dbg....
DIExpression * getExpression() const
DILocalVariable * getVariable() const
Metadata * getRawLocation() const
Returns the metadata operand for the first location description.
DIExpression * getAddressExpression() const
unsigned size() const
Definition: DenseMap.h:99
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition: DenseMap.h:151
bool contains(const_arg_type_t< KeyT > Val) const
Return true if the specified key is in the map, false otherwise.
Definition: DenseMap.h:146
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:211
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
This instruction extracts a struct member or array element value from an aggregate value.
idx_iterator idx_end() const
idx_iterator idx_begin() const
Function summary information to aid decisions and implementation of importing.
ForceSummaryHotnessType
Types for -force-summary-edges-cold debugging option.
Generic tagged DWARF-like metadata node.
void getAllMetadata(SmallVectorImpl< std::pair< unsigned, MDNode * > > &MDs) const
Appends all metadata attached to this value to MDs, sorting by KindID.
Definition: Metadata.cpp:1484
void setAlignment(Align Align)
Sets the alignment attribute of the GlobalObject.
Definition: Globals.cpp:137
void setSection(StringRef S)
Change the section for this global.
Definition: Globals.cpp:267
Function and variable summary information to aid decisions and implementation of importing.
GVFlags flags() const
Get the flags for this GlobalValue (see struct GVFlags).
StringRef modulePath() const
Get the path to the module containing this function.
ArrayRef< ValueInfo > refs() const
Return the list of values referenced by this global value definition.
VisibilityTypes getVisibility() const
Definition: GlobalValue.h:248
static bool isLocalLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:409
LinkageTypes getLinkage() const
Definition: GlobalValue.h:546
static StringRef dropLLVMManglingEscape(StringRef Name)
If the given string begins with the GlobalValue name mangling escape character '\1',...
Definition: GlobalValue.h:567
ThreadLocalMode getThreadLocalMode() const
Definition: GlobalValue.h:271
@ DLLExportStorageClass
Function to be accessible from DLL.
Definition: GlobalValue.h:76
@ DLLImportStorageClass
Function to be imported from DLL.
Definition: GlobalValue.h:75
GUID getGUID() const
Return a 64-bit global unique ID constructed from global value name (i.e.
Definition: GlobalValue.h:595
@ DefaultVisibility
The GV is visible.
Definition: GlobalValue.h:67
@ HiddenVisibility
The GV is hidden.
Definition: GlobalValue.h:68
@ ProtectedVisibility
The GV is protected.
Definition: GlobalValue.h:69
UnnamedAddr getUnnamedAddr() const
Definition: GlobalValue.h:228
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:51
@ PrivateLinkage
Like Internal, but omit from symbol table.
Definition: GlobalValue.h:60
@ CommonLinkage
Tentative definitions.
Definition: GlobalValue.h:62
@ InternalLinkage
Rename collisions when linking (static functions).
Definition: GlobalValue.h:59
@ LinkOnceAnyLinkage
Keep one copy of function when linking (inline)
Definition: GlobalValue.h:54
@ WeakODRLinkage
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:57
@ ExternalLinkage
Externally visible function.
Definition: GlobalValue.h:52
@ WeakAnyLinkage
Keep one copy of named function when linking (weak)
Definition: GlobalValue.h:56
@ AppendingLinkage
Special purpose, only applies to global arrays.
Definition: GlobalValue.h:58
@ AvailableExternallyLinkage
Available for inspection, not emission.
Definition: GlobalValue.h:53
@ ExternalWeakLinkage
ExternalWeak linkage description.
Definition: GlobalValue.h:61
@ LinkOnceODRLinkage
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:55
DLLStorageClassTypes getDLLStorageClass() const
Definition: GlobalValue.h:275
Global variable summary information to aid decisions and implementation of importing.
This instruction inserts a struct field of array element value into an aggregate value.
idx_iterator idx_end() const
idx_iterator idx_begin() const
bool isCast() const
Definition: Instruction.h:282
Invoke instruction.
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
The landingpad instruction holds all of the information necessary to generate correct exception handl...
bool isCleanup() const
Return 'true' if this landingpad instruction is a cleanup.
unsigned getNumClauses() const
Get the number of clauses for this landing pad.
bool isCatch(unsigned Idx) const
Return 'true' if the clause and index Idx is a catch clause.
Constant * getClause(unsigned Idx) const
Get the value of the clause at index Idx.
Metadata node.
Definition: Metadata.h:1069
Tracking metadata reference owned by Metadata.
Definition: Metadata.h:891
Tuple of metadata.
Definition: Metadata.h:1472
size_t getBufferSize() const
const char * getBufferStart() const
const char * getBufferEnd() const
Root of the metadata hierarchy.
Definition: Metadata.h:62
Class to hold module path string table and global value map, and encapsulate methods for operating on...
static constexpr uint64_t BitcodeSummaryVersion
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
A tuple of MDNodes.
Definition: Metadata.h:1730
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
Definition: DerivedTypes.h:662
A class that wrap the SHA1 algorithm.
Definition: SHA1.h:26
size_type size() const
Determine the number of elements in the SetVector.
Definition: SetVector.h:98
bool empty() const
Determine if the SetVector is empty or not.
Definition: SetVector.h:93
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:162
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:503
A SetVector that performs no allocations if smaller than a certain size.
Definition: SetVector.h:370
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
void append(StringRef RHS)
Append from a StringRef.
Definition: SmallString.h:68
bool empty() const
Definition: SmallVector.h:95
size_t size() const
Definition: SmallVector.h:92
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:587
void assign(size_type NumElts, ValueParamT Elt)
Definition: SmallVector.h:718
void reserve(size_type N)
Definition: SmallVector.h:677
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:697
iterator insert(iterator I, T &&Elt)
Definition: SmallVector.h:819
void resize(size_type N)
Definition: SmallVector.h:652
void push_back(const T &Elt)
Definition: SmallVector.h:427
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:300
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1210
const ValueTy & getValue() const
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
StringRef getKey() const
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
iterator begin() const
Definition: StringRef.h:111
iterator end() const
Definition: StringRef.h:113
Utility for building string tables with deduplicated suffixes.
void finalizeInOrder()
Finalize the string table without reording it.
void write(raw_ostream &OS) const
size_t add(CachedHashStringRef S)
Add a string to the builder.
Class to represent struct types.
Definition: DerivedTypes.h:216
Multiway switch.
Class to represent target extensions types, which are generally unintrospectable from target-independ...
Definition: DerivedTypes.h:720
Target - Wrapper for Target specific information.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
@ DXContainer
Definition: Triple.h:301
@ UnknownObjectFormat
Definition: Triple.h:298
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
bool isX86_FP80Ty() const
Return true if this is x86 long double.
Definition: Type.h:159
bool isFloatTy() const
Return true if this is 'float', a 32-bit IEEE fp type.
Definition: Type.h:153
bool isBFloatTy() const
Return true if this is 'bfloat', a 16-bit bfloat type.
Definition: Type.h:145
@ X86_AMXTyID
AMX vectors (8192 bits, X86 specific)
Definition: Type.h:66
@ FunctionTyID
Functions.
Definition: Type.h:71
@ ArrayTyID
Arrays.
Definition: Type.h:74
@ TypedPointerTyID
Typed pointer used by some GPU targets.
Definition: Type.h:77
@ HalfTyID
16-bit floating point type
Definition: Type.h:56
@ TargetExtTyID
Target extension type.
Definition: Type.h:78
@ VoidTyID
type with no size
Definition: Type.h:63
@ ScalableVectorTyID
Scalable SIMD vector type.
Definition: Type.h:76
@ LabelTyID
Labels.
Definition: Type.h:64
@ FloatTyID
32-bit floating point type
Definition: Type.h:58
@ StructTyID
Structures.
Definition: Type.h:73
@ IntegerTyID
Arbitrary bit width integers.
Definition: Type.h:70
@ FixedVectorTyID
Fixed width SIMD vector type.
Definition: Type.h:75
@ BFloatTyID
16-bit floating point type (7-bit significand)
Definition: Type.h:57
@ DoubleTyID
64-bit floating point type
Definition: Type.h:59
@ X86_FP80TyID
80-bit floating point type (X87)
Definition: Type.h:60
@ PPC_FP128TyID
128-bit floating point type (two 64-bits, PowerPC)
Definition: Type.h:62
@ MetadataTyID
Metadata.
Definition: Type.h:65
@ TokenTyID
Tokens.
Definition: Type.h:67
@ PointerTyID
Pointers.
Definition: Type.h:72
@ FP128TyID
128-bit floating point type (112-bit significand)
Definition: Type.h:61
bool isPPC_FP128Ty() const
Return true if this is powerpc long double.
Definition: Type.h:165
bool isFP128Ty() const
Return true if this is 'fp128'.
Definition: Type.h:162
bool isHalfTy() const
Return true if this is 'half', a 16-bit IEEE fp type.
Definition: Type.h:142
bool isDoubleTy() const
Return true if this is 'double', a 64-bit IEEE fp type.
Definition: Type.h:156
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition: Type.h:343
Value wrapper in the Metadata hierarchy.
Definition: Metadata.h:450
Value * getValue() const
Definition: Metadata.h:490
unsigned getTypeID(Type *T) const
unsigned getMetadataID(const Metadata *MD) const
UseListOrderStack UseListOrders
std::vector< std::pair< const Value *, unsigned > > ValueList
ArrayRef< const Metadata * > getNonMDStrings() const
Get the non-MDString metadata for this block.
unsigned getInstructionID(const Instruction *I) const
unsigned getAttributeListID(AttributeList PAL) const
void incorporateFunction(const Function &F)
incorporateFunction/purgeFunction - If you'd like to deal with a function, use these two methods to g...
void getFunctionConstantRange(unsigned &Start, unsigned &End) const
getFunctionConstantRange - Return the range of values that corresponds to function-local constants.
unsigned getAttributeGroupID(IndexAndAttrSet Group) const
bool hasMDs() const
Check whether the current block has any metadata to emit.
unsigned getComdatID(const Comdat *C) const
uint64_t computeBitsRequiredForTypeIndices() const
unsigned getValueID(const Value *V) const
unsigned getMetadataOrNullID(const Metadata *MD) const
const std::vector< IndexAndAttrSet > & getAttributeGroups() const
const ValueList & getValues() const
unsigned getGlobalBasicBlockID(const BasicBlock *BB) const
getGlobalBasicBlockID - This returns the function-specific ID for the specified basic block.
void setInstructionID(const Instruction *I)
const std::vector< const BasicBlock * > & getBasicBlocks() const
const std::vector< AttributeList > & getAttributeLists() const
bool shouldPreserveUseListOrder() const
const ComdatSetType & getComdats() const
std::vector< Type * > TypeList
ArrayRef< const Metadata * > getMDStrings() const
Get the MDString metadata for this block.
std::pair< unsigned, AttributeSet > IndexAndAttrSet
Attribute groups as encoded in bitcode are almost AttributeSets, but they include the AttributeList i...
const TypeList & getTypes() const
This class provides a symbol table of name/value pairs.
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
void setName(const Twine &Name)
Change the name of the value.
Definition: Value.cpp:377
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:1075
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:383
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
raw_ostream & write(unsigned char C)
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:661
This file contains the declaration of the Comdat class, which represents a single COMDAT in LLVM.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
Key
PAL metadata keys.
@ Entry
Definition: COFF.h:826
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
const uint64_t Version
Definition: InstrProf.h:1111
Predicate getPredicate(unsigned Condition, unsigned Hint)
Return predicate consisting of specified condition and hint bits.
Definition: PPCPredicates.h:87
@ CE
Windows NT (Windows on ARM)
@ FS
Definition: X86.h:210
@ BITCODE_CURRENT_EPOCH
Definition: LLVMBitCodes.h:81
@ TYPE_CODE_METADATA
Definition: LLVMBitCodes.h:162
@ TYPE_CODE_PPC_FP128
Definition: LLVMBitCodes.h:160
@ TYPE_CODE_TARGET_TYPE
Definition: LLVMBitCodes.h:179
@ TYPE_CODE_STRUCT_ANON
Definition: LLVMBitCodes.h:166
@ TYPE_CODE_STRUCT_NAME
Definition: LLVMBitCodes.h:167
@ TYPE_CODE_X86_FP80
Definition: LLVMBitCodes.h:158
@ TYPE_CODE_OPAQUE_POINTER
Definition: LLVMBitCodes.h:177
@ TYPE_CODE_FUNCTION
Definition: LLVMBitCodes.h:170
@ TYPE_CODE_NUMENTRY
Definition: LLVMBitCodes.h:136
@ TYPE_CODE_STRUCT_NAMED
Definition: LLVMBitCodes.h:168
@ METADATA_NAMESPACE
Definition: LLVMBitCodes.h:349
@ METADATA_COMMON_BLOCK
Definition: LLVMBitCodes.h:369
@ METADATA_STRING_TYPE
Definition: LLVMBitCodes.h:366
@ METADATA_MACRO_FILE
Definition: LLVMBitCodes.h:359
@ METADATA_TEMPLATE_VALUE
Definition: LLVMBitCodes.h:351
@ METADATA_LEXICAL_BLOCK_FILE
Definition: LLVMBitCodes.h:348
@ METADATA_INDEX_OFFSET
Definition: LLVMBitCodes.h:363
@ METADATA_LEXICAL_BLOCK
Definition: LLVMBitCodes.h:347
@ METADATA_SUBPROGRAM
Definition: LLVMBitCodes.h:346
@ METADATA_SUBROUTINE_TYPE
Definition: LLVMBitCodes.h:344
@ METADATA_GLOBAL_DECL_ATTACHMENT
Definition: LLVMBitCodes.h:361
@ METADATA_LOCAL_VAR
Definition: LLVMBitCodes.h:353
@ METADATA_GLOBAL_VAR
Definition: LLVMBitCodes.h:352
@ METADATA_EXPRESSION
Definition: LLVMBitCodes.h:354
@ METADATA_ATTACHMENT
Definition: LLVMBitCodes.h:336
@ METADATA_OBJC_PROPERTY
Definition: LLVMBitCodes.h:355
@ METADATA_NAMED_NODE
Definition: LLVMBitCodes.h:335
@ METADATA_IMPORTED_ENTITY
Definition: LLVMBitCodes.h:356
@ METADATA_GENERIC_SUBRANGE
Definition: LLVMBitCodes.h:370
@ METADATA_ASSIGN_ID
Definition: LLVMBitCodes.h:372
@ METADATA_COMPILE_UNIT
Definition: LLVMBitCodes.h:345
@ METADATA_COMPOSITE_TYPE
Definition: LLVMBitCodes.h:343
@ METADATA_ENUMERATOR
Definition: LLVMBitCodes.h:339
@ METADATA_DERIVED_TYPE
Definition: LLVMBitCodes.h:342
@ METADATA_TEMPLATE_TYPE
Definition: LLVMBitCodes.h:350
@ METADATA_GLOBAL_VAR_EXPR
Definition: LLVMBitCodes.h:362
@ METADATA_BASIC_TYPE
Definition: LLVMBitCodes.h:340
@ METADATA_DISTINCT_NODE
Definition: LLVMBitCodes.h:330
@ METADATA_GENERIC_DEBUG
Definition: LLVMBitCodes.h:337
@ FS_CFI_FUNCTION_DEFS
Definition: LLVMBitCodes.h:263
@ FS_PERMODULE_RELBF
Definition: LLVMBitCodes.h:272
@ FS_COMBINED_GLOBALVAR_INIT_REFS
Definition: LLVMBitCodes.h:225
@ FS_TYPE_CHECKED_LOAD_VCALLS
Definition: LLVMBitCodes.h:247
@ FS_COMBINED_PROFILE
Definition: LLVMBitCodes.h:223
@ FS_COMBINED_ORIGINAL_NAME
Definition: LLVMBitCodes.h:231
@ FS_TYPE_ID_METADATA
Definition: LLVMBitCodes.h:294
@ FS_PERMODULE_VTABLE_GLOBALVAR_INIT_REFS
Definition: LLVMBitCodes.h:300
@ FS_TYPE_TEST_ASSUME_CONST_VCALL
Definition: LLVMBitCodes.h:251
@ FS_PERMODULE_GLOBALVAR_INIT_REFS
Definition: LLVMBitCodes.h:216
@ FS_TYPE_TEST_ASSUME_VCALLS
Definition: LLVMBitCodes.h:242
@ FS_CFI_FUNCTION_DECLS
Definition: LLVMBitCodes.h:267
@ FS_COMBINED_CALLSITE_INFO
Definition: LLVMBitCodes.h:316
@ FS_COMBINED_ALLOC_INFO
Definition: LLVMBitCodes.h:321
@ FS_PERMODULE_PROFILE
Definition: LLVMBitCodes.h:214
@ FS_PERMODULE_CALLSITE_INFO
Definition: LLVMBitCodes.h:308
@ FS_PERMODULE_ALLOC_INFO
Definition: LLVMBitCodes.h:312
@ FS_TYPE_CHECKED_LOAD_CONST_VCALL
Definition: LLVMBitCodes.h:255
@ IDENTIFICATION_CODE_EPOCH
Definition: LLVMBitCodes.h:72
@ IDENTIFICATION_CODE_STRING
Definition: LLVMBitCodes.h:71
@ CST_CODE_BLOCKADDRESS
Definition: LLVMBitCodes.h:399
@ CST_CODE_NO_CFI_VALUE
Definition: LLVMBitCodes.h:410
@ CST_CODE_CE_SHUFVEC_EX
Definition: LLVMBitCodes.h:397
@ CST_CODE_CE_EXTRACTELT
Definition: LLVMBitCodes.h:391
@ CST_CODE_CE_SHUFFLEVEC
Definition: LLVMBitCodes.h:393
@ CST_CODE_WIDE_INTEGER
Definition: LLVMBitCodes.h:382
@ CST_CODE_DSO_LOCAL_EQUIVALENT
Definition: LLVMBitCodes.h:406
@ CST_CODE_AGGREGATE
Definition: LLVMBitCodes.h:384
@ CST_CODE_CE_INSERTELT
Definition: LLVMBitCodes.h:392
@ CST_CODE_CE_GEP_WITH_INRANGE
Definition: LLVMBitCodes.h:415
@ CST_CODE_INLINEASM
Definition: LLVMBitCodes.h:411
@ CALL_EXPLICIT_TYPE
Definition: LLVMBitCodes.h:554
@ COMDAT_SELECTION_KIND_LARGEST
Definition: LLVMBitCodes.h:767
@ COMDAT_SELECTION_KIND_ANY
Definition: LLVMBitCodes.h:765
@ COMDAT_SELECTION_KIND_SAME_SIZE
Definition: LLVMBitCodes.h:769
@ COMDAT_SELECTION_KIND_EXACT_MATCH
Definition: LLVMBitCodes.h:766
@ COMDAT_SELECTION_KIND_NO_DUPLICATES
Definition: LLVMBitCodes.h:768
@ ATTR_KIND_STACK_PROTECT
Definition: LLVMBitCodes.h:691
@ ATTR_KIND_NO_UNWIND
Definition: LLVMBitCodes.h:683
@ ATTR_KIND_STACK_PROTECT_STRONG
Definition: LLVMBitCodes.h:693
@ ATTR_KIND_SANITIZE_MEMORY
Definition: LLVMBitCodes.h:697
@ ATTR_KIND_SAFESTACK
Definition: LLVMBitCodes.h:709
@ ATTR_KIND_OPTIMIZE_FOR_SIZE
Definition: LLVMBitCodes.h:684
@ ATTR_KIND_SWIFT_ERROR
Definition: LLVMBitCodes.h:712
@ ATTR_KIND_STRUCT_RET
Definition: LLVMBitCodes.h:694
@ ATTR_KIND_MIN_SIZE
Definition: LLVMBitCodes.h:671
@ ATTR_KIND_NO_CALLBACK
Definition: LLVMBitCodes.h:736
@ ATTR_KIND_NO_CAPTURE
Definition: LLVMBitCodes.h:676
@ ATTR_KIND_FNRETTHUNK_EXTERN
Definition: LLVMBitCodes.h:749
@ ATTR_KIND_SANITIZE_ADDRESS
Definition: LLVMBitCodes.h:695
@ ATTR_KIND_NO_IMPLICIT_FLOAT
Definition: LLVMBitCodes.h:678
@ ATTR_KIND_NO_BUILTIN
Definition: LLVMBitCodes.h:675
@ ATTR_KIND_NO_RECURSE
Definition: LLVMBitCodes.h:713
@ ATTR_KIND_DEAD_ON_UNWIND
Definition: LLVMBitCodes.h:756
@ ATTR_KIND_CONVERGENT
Definition: LLVMBitCodes.h:708
@ ATTR_KIND_RETURNED
Definition: LLVMBitCodes.h:687
@ ATTR_KIND_STACK_ALIGNMENT
Definition: LLVMBitCodes.h:690
@ ATTR_KIND_SWIFT_SELF
Definition: LLVMBitCodes.h:711
@ ATTR_KIND_ALLOC_SIZE
Definition: LLVMBitCodes.h:716
@ ATTR_KIND_STACK_PROTECT_REQ
Definition: LLVMBitCodes.h:692
@ ATTR_KIND_INLINE_HINT
Definition: LLVMBitCodes.h:669
@ ATTR_KIND_NON_NULL
Definition: LLVMBitCodes.h:704
@ ATTR_KIND_NULL_POINTER_IS_VALID
Definition: LLVMBitCodes.h:732
@ ATTR_KIND_SANITIZE_HWADDRESS
Definition: LLVMBitCodes.h:720
@ ATTR_KIND_NO_RETURN
Definition: LLVMBitCodes.h:682
@ ATTR_KIND_MUSTPROGRESS
Definition: LLVMBitCodes.h:735
@ ATTR_KIND_RETURNS_TWICE
Definition: LLVMBitCodes.h:688
@ ATTR_KIND_SHADOWCALLSTACK
Definition: LLVMBitCodes.h:723
@ ATTR_KIND_OPT_FOR_FUZZING
Definition: LLVMBitCodes.h:722
@ ATTR_KIND_WRITABLE
Definition: LLVMBitCodes.h:754
@ ATTR_KIND_SANITIZE_NUMERICAL_STABILITY
Definition: LLVMBitCodes.h:758
@ ATTR_KIND_INITIALIZES
Definition: LLVMBitCodes.h:759
@ ATTR_KIND_ALLOCATED_POINTER
Definition: LLVMBitCodes.h:746
@ ATTR_KIND_DISABLE_SANITIZER_INSTRUMENTATION
Definition: LLVMBitCodes.h:743
@ ATTR_KIND_SKIP_PROFILE
Definition: LLVMBitCodes.h:750
@ ATTR_KIND_ELEMENTTYPE
Definition: LLVMBitCodes.h:742
@ ATTR_KIND_ALLOC_KIND
Definition: LLVMBitCodes.h:747
@ ATTR_KIND_NO_MERGE
Definition: LLVMBitCodes.h:731
@ ATTR_KIND_STRICT_FP
Definition: LLVMBitCodes.h:719
@ ATTR_KIND_NO_DUPLICATE
Definition: LLVMBitCodes.h:677
@ ATTR_KIND_ALLOC_ALIGN
Definition: LLVMBitCodes.h:745
@ ATTR_KIND_NON_LAZY_BIND
Definition: LLVMBitCodes.h:680
@ ATTR_KIND_DEREFERENCEABLE
Definition: LLVMBitCodes.h:706
@ ATTR_KIND_READ_NONE
Definition: LLVMBitCodes.h:685
@ ATTR_KIND_UW_TABLE
Definition: LLVMBitCodes.h:698
@ ATTR_KIND_OPTIMIZE_NONE
Definition: LLVMBitCodes.h:702
@ ATTR_KIND_WRITEONLY
Definition: LLVMBitCodes.h:717
@ ATTR_KIND_HYBRID_PATCHABLE
Definition: LLVMBitCodes.h:760
@ ATTR_KIND_NO_RED_ZONE
Definition: LLVMBitCodes.h:681
@ ATTR_KIND_NOCF_CHECK
Definition: LLVMBitCodes.h:721
@ ATTR_KIND_NO_PROFILE
Definition: LLVMBitCodes.h:738
@ ATTR_KIND_DEREFERENCEABLE_OR_NULL
Definition: LLVMBitCodes.h:707
@ ATTR_KIND_SANITIZE_REALTIME
Definition: LLVMBitCodes.h:761
@ ATTR_KIND_IN_ALLOCA
Definition: LLVMBitCodes.h:703
@ ATTR_KIND_READ_ONLY
Definition: LLVMBitCodes.h:686
@ ATTR_KIND_ALIGNMENT
Definition: LLVMBitCodes.h:666
@ ATTR_KIND_SPECULATIVE_LOAD_HARDENING
Definition: LLVMBitCodes.h:724
@ ATTR_KIND_ALWAYS_INLINE
Definition: LLVMBitCodes.h:667
@ ATTR_KIND_NOFPCLASS
Definition: LLVMBitCodes.h:752
@ ATTR_KIND_WILLRETURN
Definition: LLVMBitCodes.h:726
@ ATTR_KIND_PRESPLIT_COROUTINE
Definition: LLVMBitCodes.h:748
@ ATTR_KIND_NO_ALIAS
Definition: LLVMBitCodes.h:674
@ ATTR_KIND_VSCALE_RANGE
Definition: LLVMBitCodes.h:739
@ ATTR_KIND_NO_SANITIZE_COVERAGE
Definition: LLVMBitCodes.h:741
@ ATTR_KIND_JUMP_TABLE
Definition: LLVMBitCodes.h:705
@ ATTR_KIND_SPECULATABLE
Definition: LLVMBitCodes.h:718
@ ATTR_KIND_NO_INLINE
Definition: LLVMBitCodes.h:679
@ ATTR_KIND_NO_SANITIZE_BOUNDS
Definition: LLVMBitCodes.h:744
@ ATTR_KIND_SANITIZE_MEMTAG
Definition: LLVMBitCodes.h:729
@ ATTR_KIND_CORO_ONLY_DESTROY_WHEN_COMPLETE
Definition: LLVMBitCodes.h:755
@ ATTR_KIND_SANITIZE_THREAD
Definition: LLVMBitCodes.h:696
@ ATTR_KIND_OPTIMIZE_FOR_DEBUGGING
Definition: LLVMBitCodes.h:753
@ ATTR_KIND_PREALLOCATED
Definition: LLVMBitCodes.h:730
@ ATTR_KIND_SWIFT_ASYNC
Definition: LLVMBitCodes.h:740
@ OBO_NO_SIGNED_WRAP
Definition: LLVMBitCodes.h:495
@ OBO_NO_UNSIGNED_WRAP
Definition: LLVMBitCodes.h:494
@ TIO_NO_UNSIGNED_WRAP
Definition: LLVMBitCodes.h:501
@ TIO_NO_SIGNED_WRAP
Definition: LLVMBitCodes.h:502
@ USELIST_CODE_DEFAULT
Definition: LLVMBitCodes.h:660
@ SYNC_SCOPE_NAMES_BLOCK_ID
Definition: LLVMBitCodes.h:65
@ PARAMATTR_BLOCK_ID
Definition: LLVMBitCodes.h:33
@ TYPE_BLOCK_ID_NEW
Definition: LLVMBitCodes.h:48
@ CONSTANTS_BLOCK_ID
Definition: LLVMBitCodes.h:36
@ PARAMATTR_GROUP_BLOCK_ID
Definition: LLVMBitCodes.h:34
@ METADATA_KIND_BLOCK_ID
Definition: LLVMBitCodes.h:57
@ IDENTIFICATION_BLOCK_ID
Definition: LLVMBitCodes.h:42
@ GLOBALVAL_SUMMARY_BLOCK_ID
Definition: LLVMBitCodes.h:53
@ METADATA_ATTACHMENT_ID
Definition: LLVMBitCodes.h:46
@ METADATA_BLOCK_ID
Definition: LLVMBitCodes.h:45
@ FUNCTION_BLOCK_ID
Definition: LLVMBitCodes.h:37
@ FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID
Definition: LLVMBitCodes.h:61
@ MODULE_STRTAB_BLOCK_ID
Definition: LLVMBitCodes.h:52
@ VALUE_SYMTAB_BLOCK_ID
Definition: LLVMBitCodes.h:44
@ OPERAND_BUNDLE_TAGS_BLOCK_ID
Definition: LLVMBitCodes.h:55
@ USELIST_BLOCK_ID
Definition: LLVMBitCodes.h:50
@ CAST_ADDRSPACECAST
Definition: LLVMBitCodes.h:437
@ MODULE_CODE_FUNCTION
Definition: LLVMBitCodes.h:100
@ MODULE_CODE_VERSION
Definition: LLVMBitCodes.h:85
@ MODULE_CODE_SOURCE_FILENAME
Definition: LLVMBitCodes.h:116
@ MODULE_CODE_SECTIONNAME
Definition: LLVMBitCodes.h:89
@ MODULE_CODE_TRIPLE
Definition: LLVMBitCodes.h:86
@ MODULE_CODE_DATALAYOUT
Definition: LLVMBitCodes.h:87
@ MODULE_CODE_GLOBALVAR
Definition: LLVMBitCodes.h:96
@ MODULE_CODE_VSTOFFSET
Definition: LLVMBitCodes.h:108
@ MODULE_CODE_GCNAME
Definition: LLVMBitCodes.h:105
@ MODULE_CODE_COMDAT
Definition: LLVMBitCodes.h:106
@ FUNC_CODE_INST_CATCHRET
Definition: LLVMBitCodes.h:630
@ FUNC_CODE_INST_LANDINGPAD
Definition: LLVMBitCodes.h:628
@ FUNC_CODE_INST_EXTRACTVAL
Definition: LLVMBitCodes.h:593
@ FUNC_CODE_INST_CATCHPAD
Definition: LLVMBitCodes.h:631
@ FUNC_CODE_INST_RESUME
Definition: LLVMBitCodes.h:615
@ FUNC_CODE_INST_CMP2
Definition: LLVMBitCodes.h:597
@ FUNC_CODE_INST_FENCE
Definition: LLVMBitCodes.h:608
@ FUNC_CODE_INST_CALLBR
Definition: LLVMBitCodes.h:639
@ FUNC_CODE_INST_CATCHSWITCH
Definition: LLVMBitCodes.h:633
@ FUNC_CODE_INST_VSELECT
Definition: LLVMBitCodes.h:599
@ FUNC_CODE_INST_GEP
Definition: LLVMBitCodes.h:622
@ FUNC_CODE_INST_CLEANUPRET
Definition: LLVMBitCodes.h:629
@ FUNC_CODE_DEBUG_RECORD_VALUE
Definition: LLVMBitCodes.h:647
@ FUNC_CODE_INST_LOADATOMIC
Definition: LLVMBitCodes.h:618
@ FUNC_CODE_DEBUG_RECORD_ASSIGN
Definition: LLVMBitCodes.h:651
@ FUNC_CODE_INST_LOAD
Definition: LLVMBitCodes.h:584
@ FUNC_CODE_INST_STOREATOMIC
Definition: LLVMBitCodes.h:624
@ FUNC_CODE_INST_ATOMICRMW
Definition: LLVMBitCodes.h:642
@ FUNC_CODE_INST_BINOP
Definition: LLVMBitCodes.h:564
@ FUNC_CODE_INST_STORE
Definition: LLVMBitCodes.h:623
@ FUNC_CODE_DEBUG_LOC_AGAIN
Definition: LLVMBitCodes.h:603
@ FUNC_CODE_INST_EXTRACTELT
Definition: LLVMBitCodes.h:568
@ FUNC_CODE_INST_INDIRECTBR
Definition: LLVMBitCodes.h:601
@ FUNC_CODE_INST_INVOKE
Definition: LLVMBitCodes.h:576
@ FUNC_CODE_DEBUG_RECORD_VALUE_SIMPLE
Definition: LLVMBitCodes.h:654
@ FUNC_CODE_INST_INSERTVAL
Definition: LLVMBitCodes.h:594
@ FUNC_CODE_DECLAREBLOCKS
Definition: LLVMBitCodes.h:562
@ FUNC_CODE_DEBUG_RECORD_LABEL
Definition: LLVMBitCodes.h:656
@ FUNC_CODE_INST_SWITCH
Definition: LLVMBitCodes.h:575
@ FUNC_CODE_INST_PHI
Definition: LLVMBitCodes.h:580
@ FUNC_CODE_INST_RET
Definition: LLVMBitCodes.h:573
@ FUNC_CODE_INST_CALL
Definition: LLVMBitCodes.h:605
@ FUNC_CODE_INST_ALLOCA
Definition: LLVMBitCodes.h:583
@ FUNC_CODE_INST_INSERTELT
Definition: LLVMBitCodes.h:569
@ FUNC_CODE_BLOCKADDR_USERS
Definition: LLVMBitCodes.h:645
@ FUNC_CODE_INST_CLEANUPPAD
Definition: LLVMBitCodes.h:632
@ FUNC_CODE_INST_SHUFFLEVEC
Definition: LLVMBitCodes.h:570
@ FUNC_CODE_INST_UNOP
Definition: LLVMBitCodes.h:638
@ FUNC_CODE_INST_VAARG
Definition: LLVMBitCodes.h:587
@ FUNC_CODE_INST_FREEZE
Definition: LLVMBitCodes.h:641
@ FUNC_CODE_INST_CMPXCHG
Definition: LLVMBitCodes.h:625
@ FUNC_CODE_INST_UNREACHABLE
Definition: LLVMBitCodes.h:578
@ FUNC_CODE_INST_CAST
Definition: LLVMBitCodes.h:565
@ FUNC_CODE_DEBUG_LOC
Definition: LLVMBitCodes.h:607
@ FUNC_CODE_DEBUG_RECORD_DECLARE
Definition: LLVMBitCodes.h:649
@ FUNC_CODE_OPERAND_BUNDLE
Definition: LLVMBitCodes.h:637
@ FIRST_APPLICATION_ABBREV
Definition: BitCodeEnums.h:60
@ OPERAND_BUNDLE_TAG
Definition: LLVMBitCodes.h:183
@ PARAMATTR_GRP_CODE_ENTRY
Definition: LLVMBitCodes.h:131
@ PARAMATTR_CODE_ENTRY
Definition: LLVMBitCodes.h:130
@ ORDERING_NOTATOMIC
Definition: LLVMBitCodes.h:540
@ ORDERING_UNORDERED
Definition: LLVMBitCodes.h:541
@ ORDERING_MONOTONIC
Definition: LLVMBitCodes.h:542
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
Error build(ArrayRef< Module * > Mods, SmallVector< char, 0 > &Symtab, StringTableBuilder &StrtabBuilder, BumpPtrAllocator &Alloc)
Fills in Symtab and StrtabBuilder with a valid symbol and string table for Mods.
Definition: IRSymtab.cpp:378
NodeAddr< CodeNode * > Code
Definition: RDFGraph.h:388
void write32le(void *P, uint32_t V)
Definition: Endian.h:468
uint32_t read32be(const void *P)
Definition: Endian.h:434
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition: STLExtras.h:329
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
Definition: MathExtras.h:353
unsigned encode(MaybeAlign A)
Returns a representation of the alignment that encodes undefined as 0.
Definition: Alignment.h:217
void WriteBitcodeToFile(const Module &M, raw_ostream &Out, bool ShouldPreserveUseListOrder=false, const ModuleSummaryIndex *Index=nullptr, bool GenerateHash=false, ModuleHash *ModHash=nullptr)
Write the specified module to the specified raw output stream.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition: STLExtras.h:2406
AddressSpace
Definition: NVPTXBaseInfo.h:21
void writeThinLinkBitcodeToFile(const Module &M, raw_ostream &Out, const ModuleSummaryIndex &Index, const ModuleHash &ModHash)
Write the specified thin link bitcode file (i.e., the minimized bitcode file) to the given raw output...
@ BWH_HeaderSize
Definition: BitCodeEnums.h:32
FunctionSummary::ForceSummaryHotnessType ForceSummaryEdgesCold
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition: STLExtras.h:2073
void embedBitcodeInModule(Module &M, MemoryBufferRef Buf, bool EmbedBitcode, bool EmbedCmdline, const std::vector< uint8_t > &CmdArgs)
If EmbedBitcode is set, save a copy of the llvm IR as data in the __LLVM,__bitcode section (....
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1729
Error write(MCStreamer &Out, ArrayRef< std::string > Inputs, OnCuIndexOverflow OverflowOptValue)
Definition: DWP.cpp:625
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1647
void writeIndexToFile(const ModuleSummaryIndex &Index, raw_ostream &Out, const std::map< std::string, GVSummaryMapTy > *ModuleToSummariesForIndex=nullptr, const GVSummaryPtrSet *DecSummaries=nullptr)
Write the specified module summary index to the given raw output stream, where it will be written in ...
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167
AtomicOrdering
Atomic ordering for LLVM's memory model.
std::unordered_set< GlobalValueSummary * > GVSummaryPtrSet
A set of global value summary pointers.
DWARFExpression::Operation Op
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:1824
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:191
std::array< uint32_t, 5 > ModuleHash
160 bits SHA1
bool isBitcode(const unsigned char *BufPtr, const unsigned char *BufEnd)
isBitcode - Return true if the given bytes are the magic bytes for LLVM IR bitcode,...
std::vector< TypeIdOffsetVtableInfo > TypeIdCompatibleVtableInfo
List of vtable definitions decorated by a particular type identifier, and their corresponding offsets...
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1069
GlobalVariable * collectUsedGlobalVariables(const Module &M, SmallVectorImpl< GlobalValue * > &Vec, bool CompilerUsed)
Given "llvm.used" or "llvm.compiler.used" as a global name, collect the initializer elements of that ...
Definition: Module.cpp:830
#define N
#define NC
Definition: regutils.h:42
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
Class to accumulate and hold information about a callee.
static constexpr unsigned RelBlockFreqBits
The value stored in RelBlockFreq has to be interpreted as the digits of a scaled number with a scale ...
Flags specific to function summaries.
static constexpr uint32_t RangeWidth
Group flags (Linkage, NotEligibleToImport, etc.) as a bitfield.
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition: Alignment.h:117
static const Target * lookupTarget(StringRef Triple, std::string &Error)
lookupTarget - Lookup a target based on a target triple.
Structure to hold a use-list order.
Definition: UseListOrder.h:26
ValID - Represents a reference of a definition of some sort with no type.
Definition: LLParser.h:53
Struct that holds a reference to a particular GUID in a global value summary.
uint64_t Info
Additional information for the resolution:
enum llvm::WholeProgramDevirtResolution::ByArg::Kind TheKind
enum llvm::WholeProgramDevirtResolution::Kind TheKind
std::map< std::vector< uint64_t >, ByArg > ResByArg
Resolutions for calls with all constant integer arguments (excluding the first argument,...