LLVM 19.0.0git
BitcodeWriter.cpp
Go to the documentation of this file.
1//===- Bitcode/Writer/BitcodeWriter.cpp - Bitcode Writer ------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Bitcode writer implementation.
10//
11//===----------------------------------------------------------------------===//
12
14#include "ValueEnumerator.h"
15#include "llvm/ADT/APFloat.h"
16#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/SetVector.h"
24#include "llvm/ADT/StringMap.h"
25#include "llvm/ADT/StringRef.h"
31#include "llvm/Config/llvm-config.h"
32#include "llvm/IR/Attributes.h"
33#include "llvm/IR/BasicBlock.h"
34#include "llvm/IR/Comdat.h"
35#include "llvm/IR/Constant.h"
37#include "llvm/IR/Constants.h"
39#include "llvm/IR/DebugLoc.h"
41#include "llvm/IR/Function.h"
42#include "llvm/IR/GlobalAlias.h"
43#include "llvm/IR/GlobalIFunc.h"
45#include "llvm/IR/GlobalValue.h"
47#include "llvm/IR/InlineAsm.h"
48#include "llvm/IR/InstrTypes.h"
49#include "llvm/IR/Instruction.h"
51#include "llvm/IR/LLVMContext.h"
52#include "llvm/IR/Metadata.h"
53#include "llvm/IR/Module.h"
55#include "llvm/IR/Operator.h"
56#include "llvm/IR/Type.h"
58#include "llvm/IR/Value.h"
66#include "llvm/Support/Endian.h"
67#include "llvm/Support/Error.h"
70#include "llvm/Support/SHA1.h"
73#include <algorithm>
74#include <cassert>
75#include <cstddef>
76#include <cstdint>
77#include <iterator>
78#include <map>
79#include <memory>
80#include <optional>
81#include <string>
82#include <utility>
83#include <vector>
84
85using namespace llvm;
86
88 IndexThreshold("bitcode-mdindex-threshold", cl::Hidden, cl::init(25),
89 cl::desc("Number of metadatas above which we emit an index "
90 "to enable lazy-loading"));
92 "bitcode-flush-threshold", cl::Hidden, cl::init(512),
93 cl::desc("The threshold (unit M) for flushing LLVM bitcode."));
94
96 "write-relbf-to-summary", cl::Hidden, cl::init(false),
97 cl::desc("Write relative block frequency to function summary "));
98
99namespace llvm {
101}
102
105
106namespace {
107
108/// These are manifest constants used by the bitcode writer. They do not need to
109/// be kept in sync with the reader, but need to be consistent within this file.
110enum {
111 // VALUE_SYMTAB_BLOCK abbrev id's.
112 VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
113 VST_ENTRY_7_ABBREV,
114 VST_ENTRY_6_ABBREV,
115 VST_BBENTRY_6_ABBREV,
116
117 // CONSTANTS_BLOCK abbrev id's.
118 CONSTANTS_SETTYPE_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
119 CONSTANTS_INTEGER_ABBREV,
120 CONSTANTS_CE_CAST_Abbrev,
121 CONSTANTS_NULL_Abbrev,
122
123 // FUNCTION_BLOCK abbrev id's.
124 FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
125 FUNCTION_INST_UNOP_ABBREV,
126 FUNCTION_INST_UNOP_FLAGS_ABBREV,
127 FUNCTION_INST_BINOP_ABBREV,
128 FUNCTION_INST_BINOP_FLAGS_ABBREV,
129 FUNCTION_INST_CAST_ABBREV,
130 FUNCTION_INST_CAST_FLAGS_ABBREV,
131 FUNCTION_INST_RET_VOID_ABBREV,
132 FUNCTION_INST_RET_VAL_ABBREV,
133 FUNCTION_INST_UNREACHABLE_ABBREV,
134 FUNCTION_INST_GEP_ABBREV,
135 FUNCTION_DEBUG_RECORD_VALUE_ABBREV,
136};
137
138/// Abstract class to manage the bitcode writing, subclassed for each bitcode
139/// file type.
140class BitcodeWriterBase {
141protected:
142 /// The stream created and owned by the client.
143 BitstreamWriter &Stream;
144
145 StringTableBuilder &StrtabBuilder;
146
147public:
148 /// Constructs a BitcodeWriterBase object that writes to the provided
149 /// \p Stream.
150 BitcodeWriterBase(BitstreamWriter &Stream, StringTableBuilder &StrtabBuilder)
151 : Stream(Stream), StrtabBuilder(StrtabBuilder) {}
152
153protected:
154 void writeModuleVersion();
155};
156
157void BitcodeWriterBase::writeModuleVersion() {
158 // VERSION: [version#]
159 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, ArrayRef<uint64_t>{2});
160}
161
162/// Base class to manage the module bitcode writing, currently subclassed for
163/// ModuleBitcodeWriter and ThinLinkBitcodeWriter.
164class ModuleBitcodeWriterBase : public BitcodeWriterBase {
165protected:
166 /// The Module to write to bitcode.
167 const Module &M;
168
169 /// Enumerates ids for all values in the module.
171
172 /// Optional per-module index to write for ThinLTO.
174
175 /// Map that holds the correspondence between GUIDs in the summary index,
176 /// that came from indirect call profiles, and a value id generated by this
177 /// class to use in the VST and summary block records.
178 std::map<GlobalValue::GUID, unsigned> GUIDToValueIdMap;
179
180 /// Tracks the last value id recorded in the GUIDToValueMap.
181 unsigned GlobalValueId;
182
183 /// Saves the offset of the VSTOffset record that must eventually be
184 /// backpatched with the offset of the actual VST.
185 uint64_t VSTOffsetPlaceholder = 0;
186
187public:
188 /// Constructs a ModuleBitcodeWriterBase object for the given Module,
189 /// writing to the provided \p Buffer.
190 ModuleBitcodeWriterBase(const Module &M, StringTableBuilder &StrtabBuilder,
191 BitstreamWriter &Stream,
192 bool ShouldPreserveUseListOrder,
194 : BitcodeWriterBase(Stream, StrtabBuilder), M(M),
195 VE(M, ShouldPreserveUseListOrder), Index(Index) {
196 // Assign ValueIds to any callee values in the index that came from
197 // indirect call profiles and were recorded as a GUID not a Value*
198 // (which would have been assigned an ID by the ValueEnumerator).
199 // The starting ValueId is just after the number of values in the
200 // ValueEnumerator, so that they can be emitted in the VST.
201 GlobalValueId = VE.getValues().size();
202 if (!Index)
203 return;
204 for (const auto &GUIDSummaryLists : *Index)
205 // Examine all summaries for this GUID.
206 for (auto &Summary : GUIDSummaryLists.second.SummaryList)
207 if (auto FS = dyn_cast<FunctionSummary>(Summary.get())) {
208 // For each call in the function summary, see if the call
209 // is to a GUID (which means it is for an indirect call,
210 // otherwise we would have a Value for it). If so, synthesize
211 // a value id.
212 for (auto &CallEdge : FS->calls())
213 if (!CallEdge.first.haveGVs() || !CallEdge.first.getValue())
214 assignValueId(CallEdge.first.getGUID());
215
216 // For each referenced variables in the function summary, see if the
217 // variable is represented by a GUID (as opposed to a symbol to
218 // declarations or definitions in the module). If so, synthesize a
219 // value id.
220 for (auto &RefEdge : FS->refs())
221 if (!RefEdge.haveGVs() || !RefEdge.getValue())
222 assignValueId(RefEdge.getGUID());
223 }
224 }
225
226protected:
227 void writePerModuleGlobalValueSummary();
228
229private:
230 void writePerModuleFunctionSummaryRecord(
232 unsigned ValueID, unsigned FSCallsAbbrev, unsigned FSCallsProfileAbbrev,
233 unsigned CallsiteAbbrev, unsigned AllocAbbrev, const Function &F);
234 void writeModuleLevelReferences(const GlobalVariable &V,
236 unsigned FSModRefsAbbrev,
237 unsigned FSModVTableRefsAbbrev);
238
239 void assignValueId(GlobalValue::GUID ValGUID) {
240 GUIDToValueIdMap[ValGUID] = ++GlobalValueId;
241 }
242
243 unsigned getValueId(GlobalValue::GUID ValGUID) {
244 const auto &VMI = GUIDToValueIdMap.find(ValGUID);
245 // Expect that any GUID value had a value Id assigned by an
246 // earlier call to assignValueId.
247 assert(VMI != GUIDToValueIdMap.end() &&
248 "GUID does not have assigned value Id");
249 return VMI->second;
250 }
251
252 // Helper to get the valueId for the type of value recorded in VI.
253 unsigned getValueId(ValueInfo VI) {
254 if (!VI.haveGVs() || !VI.getValue())
255 return getValueId(VI.getGUID());
256 return VE.getValueID(VI.getValue());
257 }
258
259 std::map<GlobalValue::GUID, unsigned> &valueIds() { return GUIDToValueIdMap; }
260};
261
262/// Class to manage the bitcode writing for a module.
263class ModuleBitcodeWriter : public ModuleBitcodeWriterBase {
264 /// True if a module hash record should be written.
265 bool GenerateHash;
266
267 /// If non-null, when GenerateHash is true, the resulting hash is written
268 /// into ModHash.
269 ModuleHash *ModHash;
270
271 SHA1 Hasher;
272
273 /// The start bit of the identification block.
274 uint64_t BitcodeStartBit;
275
276public:
277 /// Constructs a ModuleBitcodeWriter object for the given Module,
278 /// writing to the provided \p Buffer.
279 ModuleBitcodeWriter(const Module &M, StringTableBuilder &StrtabBuilder,
280 BitstreamWriter &Stream, bool ShouldPreserveUseListOrder,
281 const ModuleSummaryIndex *Index, bool GenerateHash,
282 ModuleHash *ModHash = nullptr)
283 : ModuleBitcodeWriterBase(M, StrtabBuilder, Stream,
284 ShouldPreserveUseListOrder, Index),
285 GenerateHash(GenerateHash), ModHash(ModHash),
286 BitcodeStartBit(Stream.GetCurrentBitNo()) {}
287
288 /// Emit the current module to the bitstream.
289 void write();
290
291private:
292 uint64_t bitcodeStartBit() { return BitcodeStartBit; }
293
294 size_t addToStrtab(StringRef Str);
295
296 void writeAttributeGroupTable();
297 void writeAttributeTable();
298 void writeTypeTable();
299 void writeComdats();
300 void writeValueSymbolTableForwardDecl();
301 void writeModuleInfo();
302 void writeValueAsMetadata(const ValueAsMetadata *MD,
305 unsigned Abbrev);
306 unsigned createDILocationAbbrev();
308 unsigned &Abbrev);
309 unsigned createGenericDINodeAbbrev();
311 SmallVectorImpl<uint64_t> &Record, unsigned &Abbrev);
313 unsigned Abbrev);
316 unsigned Abbrev);
317 void writeDIEnumerator(const DIEnumerator *N,
318 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
320 unsigned Abbrev);
321 void writeDIStringType(const DIStringType *N,
322 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
324 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
326 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
329 unsigned Abbrev);
331 unsigned Abbrev);
333 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
334 void writeDISubprogram(const DISubprogram *N,
335 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
337 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
340 unsigned Abbrev);
342 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
344 unsigned Abbrev);
346 unsigned Abbrev);
348 unsigned Abbrev);
351 unsigned Abbrev);
353 unsigned Abbrev);
356 unsigned Abbrev);
359 unsigned Abbrev);
362 unsigned Abbrev);
364 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
365 void writeDILabel(const DILabel *N,
366 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
367 void writeDIExpression(const DIExpression *N,
368 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
371 unsigned Abbrev);
373 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
376 unsigned Abbrev);
377 unsigned createNamedMetadataAbbrev();
378 void writeNamedMetadata(SmallVectorImpl<uint64_t> &Record);
379 unsigned createMetadataStringsAbbrev();
380 void writeMetadataStrings(ArrayRef<const Metadata *> Strings,
382 void writeMetadataRecords(ArrayRef<const Metadata *> MDs,
384 std::vector<unsigned> *MDAbbrevs = nullptr,
385 std::vector<uint64_t> *IndexPos = nullptr);
386 void writeModuleMetadata();
387 void writeFunctionMetadata(const Function &F);
388 void writeFunctionMetadataAttachment(const Function &F);
389 void pushGlobalMetadataAttachment(SmallVectorImpl<uint64_t> &Record,
390 const GlobalObject &GO);
391 void writeModuleMetadataKinds();
392 void writeOperandBundleTags();
393 void writeSyncScopeNames();
394 void writeConstants(unsigned FirstVal, unsigned LastVal, bool isGlobal);
395 void writeModuleConstants();
396 bool pushValueAndType(const Value *V, unsigned InstID,
398 void writeOperandBundles(const CallBase &CB, unsigned InstID);
399 void pushValue(const Value *V, unsigned InstID,
401 void pushValueSigned(const Value *V, unsigned InstID,
403 void writeInstruction(const Instruction &I, unsigned InstID,
405 void writeFunctionLevelValueSymbolTable(const ValueSymbolTable &VST);
406 void writeGlobalValueSymbolTable(
407 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex);
408 void writeUseList(UseListOrder &&Order);
409 void writeUseListBlock(const Function *F);
410 void
411 writeFunction(const Function &F,
412 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex);
413 void writeBlockInfo();
414 void writeModuleHash(StringRef View);
415
416 unsigned getEncodedSyncScopeID(SyncScope::ID SSID) {
417 return unsigned(SSID);
418 }
419
420 unsigned getEncodedAlign(MaybeAlign Alignment) { return encode(Alignment); }
421};
422
423/// Class to manage the bitcode writing for a combined index.
424class IndexBitcodeWriter : public BitcodeWriterBase {
425 /// The combined index to write to bitcode.
427
428 /// When writing combined summaries, provides the set of global value
429 /// summaries for which the value (function, function alias, etc) should be
430 /// imported as a declaration.
431 const GVSummaryPtrSet *DecSummaries = nullptr;
432
433 /// When writing a subset of the index for distributed backends, client
434 /// provides a map of modules to the corresponding GUIDs/summaries to write.
435 const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex;
436
437 /// Map that holds the correspondence between the GUID used in the combined
438 /// index and a value id generated by this class to use in references.
439 std::map<GlobalValue::GUID, unsigned> GUIDToValueIdMap;
440
441 // The stack ids used by this index, which will be a subset of those in
442 // the full index in the case of distributed indexes.
443 std::vector<uint64_t> StackIds;
444
445 // Keep a map of the stack id indices used by records being written for this
446 // index to the index of the corresponding stack id in the above StackIds
447 // vector. Ensures we write each referenced stack id once.
448 DenseMap<unsigned, unsigned> StackIdIndicesToIndex;
449
450 /// Tracks the last value id recorded in the GUIDToValueMap.
451 unsigned GlobalValueId = 0;
452
453 /// Tracks the assignment of module paths in the module path string table to
454 /// an id assigned for use in summary references to the module path.
456
457public:
458 /// Constructs a IndexBitcodeWriter object for the given combined index,
459 /// writing to the provided \p Buffer. When writing a subset of the index
460 /// for a distributed backend, provide a \p ModuleToSummariesForIndex map.
461 /// If provided, \p DecSummaries specifies the set of summaries for which
462 /// the corresponding functions or aliased functions should be imported as a
463 /// declaration (but not definition) for each module.
464 IndexBitcodeWriter(BitstreamWriter &Stream, StringTableBuilder &StrtabBuilder,
466 const GVSummaryPtrSet *DecSummaries = nullptr,
467 const std::map<std::string, GVSummaryMapTy>
468 *ModuleToSummariesForIndex = nullptr)
469 : BitcodeWriterBase(Stream, StrtabBuilder), Index(Index),
470 DecSummaries(DecSummaries),
471 ModuleToSummariesForIndex(ModuleToSummariesForIndex) {
472
473 // See if the StackIdIndex was already added to the StackId map and
474 // vector. If not, record it.
475 auto RecordStackIdReference = [&](unsigned StackIdIndex) {
476 // If the StackIdIndex is not yet in the map, the below insert ensures
477 // that it will point to the new StackIds vector entry we push to just
478 // below.
479 auto Inserted =
480 StackIdIndicesToIndex.insert({StackIdIndex, StackIds.size()});
481 if (Inserted.second)
482 StackIds.push_back(Index.getStackIdAtIndex(StackIdIndex));
483 };
484
485 // Assign unique value ids to all summaries to be written, for use
486 // in writing out the call graph edges. Save the mapping from GUID
487 // to the new global value id to use when writing those edges, which
488 // are currently saved in the index in terms of GUID.
489 forEachSummary([&](GVInfo I, bool IsAliasee) {
490 GUIDToValueIdMap[I.first] = ++GlobalValueId;
491 if (IsAliasee)
492 return;
493 auto *FS = dyn_cast<FunctionSummary>(I.second);
494 if (!FS)
495 return;
496 // Record all stack id indices actually used in the summary entries being
497 // written, so that we can compact them in the case of distributed ThinLTO
498 // indexes.
499 for (auto &CI : FS->callsites()) {
500 // If the stack id list is empty, this callsite info was synthesized for
501 // a missing tail call frame. Ensure that the callee's GUID gets a value
502 // id. Normally we only generate these for defined summaries, which in
503 // the case of distributed ThinLTO is only the functions already defined
504 // in the module or that we want to import. We don't bother to include
505 // all the callee symbols as they aren't normally needed in the backend.
506 // However, for the synthesized callsite infos we do need the callee
507 // GUID in the backend so that we can correlate the identified callee
508 // with this callsite info (which for non-tail calls is done by the
509 // ordering of the callsite infos and verified via stack ids).
510 if (CI.StackIdIndices.empty()) {
511 GUIDToValueIdMap[CI.Callee.getGUID()] = ++GlobalValueId;
512 continue;
513 }
514 for (auto Idx : CI.StackIdIndices)
515 RecordStackIdReference(Idx);
516 }
517 for (auto &AI : FS->allocs())
518 for (auto &MIB : AI.MIBs)
519 for (auto Idx : MIB.StackIdIndices)
520 RecordStackIdReference(Idx);
521 });
522 }
523
524 /// The below iterator returns the GUID and associated summary.
525 using GVInfo = std::pair<GlobalValue::GUID, GlobalValueSummary *>;
526
527 /// Calls the callback for each value GUID and summary to be written to
528 /// bitcode. This hides the details of whether they are being pulled from the
529 /// entire index or just those in a provided ModuleToSummariesForIndex map.
530 template<typename Functor>
531 void forEachSummary(Functor Callback) {
532 if (ModuleToSummariesForIndex) {
533 for (auto &M : *ModuleToSummariesForIndex)
534 for (auto &Summary : M.second) {
535 Callback(Summary, false);
536 // Ensure aliasee is handled, e.g. for assigning a valueId,
537 // even if we are not importing the aliasee directly (the
538 // imported alias will contain a copy of aliasee).
539 if (auto *AS = dyn_cast<AliasSummary>(Summary.getSecond()))
540 Callback({AS->getAliaseeGUID(), &AS->getAliasee()}, true);
541 }
542 } else {
543 for (auto &Summaries : Index)
544 for (auto &Summary : Summaries.second.SummaryList)
545 Callback({Summaries.first, Summary.get()}, false);
546 }
547 }
548
549 /// Calls the callback for each entry in the modulePaths StringMap that
550 /// should be written to the module path string table. This hides the details
551 /// of whether they are being pulled from the entire index or just those in a
552 /// provided ModuleToSummariesForIndex map.
553 template <typename Functor> void forEachModule(Functor Callback) {
554 if (ModuleToSummariesForIndex) {
555 for (const auto &M : *ModuleToSummariesForIndex) {
556 const auto &MPI = Index.modulePaths().find(M.first);
557 if (MPI == Index.modulePaths().end()) {
558 // This should only happen if the bitcode file was empty, in which
559 // case we shouldn't be importing (the ModuleToSummariesForIndex
560 // would only include the module we are writing and index for).
561 assert(ModuleToSummariesForIndex->size() == 1);
562 continue;
563 }
564 Callback(*MPI);
565 }
566 } else {
567 // Since StringMap iteration order isn't guaranteed, order by path string
568 // first.
569 // FIXME: Make this a vector of StringMapEntry instead to avoid the later
570 // map lookup.
571 std::vector<StringRef> ModulePaths;
572 for (auto &[ModPath, _] : Index.modulePaths())
573 ModulePaths.push_back(ModPath);
574 llvm::sort(ModulePaths.begin(), ModulePaths.end());
575 for (auto &ModPath : ModulePaths)
576 Callback(*Index.modulePaths().find(ModPath));
577 }
578 }
579
580 /// Main entry point for writing a combined index to bitcode.
581 void write();
582
583private:
584 void writeModStrings();
585 void writeCombinedGlobalValueSummary();
586
587 std::optional<unsigned> getValueId(GlobalValue::GUID ValGUID) {
588 auto VMI = GUIDToValueIdMap.find(ValGUID);
589 if (VMI == GUIDToValueIdMap.end())
590 return std::nullopt;
591 return VMI->second;
592 }
593
594 std::map<GlobalValue::GUID, unsigned> &valueIds() { return GUIDToValueIdMap; }
595};
596
597} // end anonymous namespace
598
599static unsigned getEncodedCastOpcode(unsigned Opcode) {
600 switch (Opcode) {
601 default: llvm_unreachable("Unknown cast instruction!");
602 case Instruction::Trunc : return bitc::CAST_TRUNC;
603 case Instruction::ZExt : return bitc::CAST_ZEXT;
604 case Instruction::SExt : return bitc::CAST_SEXT;
605 case Instruction::FPToUI : return bitc::CAST_FPTOUI;
606 case Instruction::FPToSI : return bitc::CAST_FPTOSI;
607 case Instruction::UIToFP : return bitc::CAST_UITOFP;
608 case Instruction::SIToFP : return bitc::CAST_SITOFP;
609 case Instruction::FPTrunc : return bitc::CAST_FPTRUNC;
610 case Instruction::FPExt : return bitc::CAST_FPEXT;
611 case Instruction::PtrToInt: return bitc::CAST_PTRTOINT;
612 case Instruction::IntToPtr: return bitc::CAST_INTTOPTR;
613 case Instruction::BitCast : return bitc::CAST_BITCAST;
614 case Instruction::AddrSpaceCast: return bitc::CAST_ADDRSPACECAST;
615 }
616}
617
618static unsigned getEncodedUnaryOpcode(unsigned Opcode) {
619 switch (Opcode) {
620 default: llvm_unreachable("Unknown binary instruction!");
621 case Instruction::FNeg: return bitc::UNOP_FNEG;
622 }
623}
624
625static unsigned getEncodedBinaryOpcode(unsigned Opcode) {
626 switch (Opcode) {
627 default: llvm_unreachable("Unknown binary instruction!");
628 case Instruction::Add:
629 case Instruction::FAdd: return bitc::BINOP_ADD;
630 case Instruction::Sub:
631 case Instruction::FSub: return bitc::BINOP_SUB;
632 case Instruction::Mul:
633 case Instruction::FMul: return bitc::BINOP_MUL;
634 case Instruction::UDiv: return bitc::BINOP_UDIV;
635 case Instruction::FDiv:
636 case Instruction::SDiv: return bitc::BINOP_SDIV;
637 case Instruction::URem: return bitc::BINOP_UREM;
638 case Instruction::FRem:
639 case Instruction::SRem: return bitc::BINOP_SREM;
640 case Instruction::Shl: return bitc::BINOP_SHL;
641 case Instruction::LShr: return bitc::BINOP_LSHR;
642 case Instruction::AShr: return bitc::BINOP_ASHR;
643 case Instruction::And: return bitc::BINOP_AND;
644 case Instruction::Or: return bitc::BINOP_OR;
645 case Instruction::Xor: return bitc::BINOP_XOR;
646 }
647}
648
650 switch (Op) {
651 default: llvm_unreachable("Unknown RMW operation!");
657 case AtomicRMWInst::Or: return bitc::RMW_OR;
668 return bitc::RMW_UINC_WRAP;
670 return bitc::RMW_UDEC_WRAP;
671 }
672}
673
674static unsigned getEncodedOrdering(AtomicOrdering Ordering) {
675 switch (Ordering) {
676 case AtomicOrdering::NotAtomic: return bitc::ORDERING_NOTATOMIC;
677 case AtomicOrdering::Unordered: return bitc::ORDERING_UNORDERED;
678 case AtomicOrdering::Monotonic: return bitc::ORDERING_MONOTONIC;
679 case AtomicOrdering::Acquire: return bitc::ORDERING_ACQUIRE;
680 case AtomicOrdering::Release: return bitc::ORDERING_RELEASE;
681 case AtomicOrdering::AcquireRelease: return bitc::ORDERING_ACQREL;
682 case AtomicOrdering::SequentiallyConsistent: return bitc::ORDERING_SEQCST;
683 }
684 llvm_unreachable("Invalid ordering");
685}
686
687static void writeStringRecord(BitstreamWriter &Stream, unsigned Code,
688 StringRef Str, unsigned AbbrevToUse) {
690
691 // Code: [strchar x N]
692 for (char C : Str) {
693 if (AbbrevToUse && !BitCodeAbbrevOp::isChar6(C))
694 AbbrevToUse = 0;
695 Vals.push_back(C);
696 }
697
698 // Emit the finished record.
699 Stream.EmitRecord(Code, Vals, AbbrevToUse);
700}
701
703 switch (Kind) {
704 case Attribute::Alignment:
706 case Attribute::AllocAlign:
708 case Attribute::AllocSize:
710 case Attribute::AlwaysInline:
712 case Attribute::Builtin:
714 case Attribute::ByVal:
716 case Attribute::Convergent:
718 case Attribute::InAlloca:
720 case Attribute::Cold:
722 case Attribute::DisableSanitizerInstrumentation:
724 case Attribute::FnRetThunkExtern:
726 case Attribute::Hot:
727 return bitc::ATTR_KIND_HOT;
728 case Attribute::ElementType:
730 case Attribute::HybridPatchable:
732 case Attribute::InlineHint:
734 case Attribute::InReg:
736 case Attribute::JumpTable:
738 case Attribute::MinSize:
740 case Attribute::AllocatedPointer:
742 case Attribute::AllocKind:
744 case Attribute::Memory:
746 case Attribute::NoFPClass:
748 case Attribute::Naked:
750 case Attribute::Nest:
752 case Attribute::NoAlias:
754 case Attribute::NoBuiltin:
756 case Attribute::NoCallback:
758 case Attribute::NoCapture:
760 case Attribute::NoDuplicate:
762 case Attribute::NoFree:
764 case Attribute::NoImplicitFloat:
766 case Attribute::NoInline:
768 case Attribute::NoRecurse:
770 case Attribute::NoMerge:
772 case Attribute::NonLazyBind:
774 case Attribute::NonNull:
776 case Attribute::Dereferenceable:
778 case Attribute::DereferenceableOrNull:
780 case Attribute::NoRedZone:
782 case Attribute::NoReturn:
784 case Attribute::NoSync:
786 case Attribute::NoCfCheck:
788 case Attribute::NoProfile:
790 case Attribute::SkipProfile:
792 case Attribute::NoUnwind:
794 case Attribute::NoSanitizeBounds:
796 case Attribute::NoSanitizeCoverage:
798 case Attribute::NullPointerIsValid:
800 case Attribute::OptimizeForDebugging:
802 case Attribute::OptForFuzzing:
804 case Attribute::OptimizeForSize:
806 case Attribute::OptimizeNone:
808 case Attribute::ReadNone:
810 case Attribute::ReadOnly:
812 case Attribute::Returned:
814 case Attribute::ReturnsTwice:
816 case Attribute::SExt:
818 case Attribute::Speculatable:
820 case Attribute::StackAlignment:
822 case Attribute::StackProtect:
824 case Attribute::StackProtectReq:
826 case Attribute::StackProtectStrong:
828 case Attribute::SafeStack:
830 case Attribute::ShadowCallStack:
832 case Attribute::StrictFP:
834 case Attribute::StructRet:
836 case Attribute::SanitizeAddress:
838 case Attribute::SanitizeHWAddress:
840 case Attribute::SanitizeThread:
842 case Attribute::SanitizeMemory:
844 case Attribute::SanitizeNumericalStability:
846 case Attribute::SpeculativeLoadHardening:
848 case Attribute::SwiftError:
850 case Attribute::SwiftSelf:
852 case Attribute::SwiftAsync:
854 case Attribute::UWTable:
856 case Attribute::VScaleRange:
858 case Attribute::WillReturn:
860 case Attribute::WriteOnly:
862 case Attribute::ZExt:
864 case Attribute::ImmArg:
866 case Attribute::SanitizeMemTag:
868 case Attribute::Preallocated:
870 case Attribute::NoUndef:
872 case Attribute::ByRef:
874 case Attribute::MustProgress:
876 case Attribute::PresplitCoroutine:
878 case Attribute::Writable:
880 case Attribute::CoroDestroyOnlyWhenComplete:
882 case Attribute::DeadOnUnwind:
884 case Attribute::Range:
886 case Attribute::Initializes:
889 llvm_unreachable("Can not encode end-attribute kinds marker.");
890 case Attribute::None:
891 llvm_unreachable("Can not encode none-attribute.");
894 llvm_unreachable("Trying to encode EmptyKey/TombstoneKey");
895 }
896
897 llvm_unreachable("Trying to encode unknown attribute");
898}
899
901 if ((int64_t)V >= 0)
902 Vals.push_back(V << 1);
903 else
904 Vals.push_back((-V << 1) | 1);
905}
906
907static void emitWideAPInt(SmallVectorImpl<uint64_t> &Vals, const APInt &A) {
908 // We have an arbitrary precision integer value to write whose
909 // bit width is > 64. However, in canonical unsigned integer
910 // format it is likely that the high bits are going to be zero.
911 // So, we only write the number of active words.
912 unsigned NumWords = A.getActiveWords();
913 const uint64_t *RawData = A.getRawData();
914 for (unsigned i = 0; i < NumWords; i++)
915 emitSignedInt64(Vals, RawData[i]);
916}
917
919 const ConstantRange &CR, bool EmitBitWidth) {
920 unsigned BitWidth = CR.getBitWidth();
921 if (EmitBitWidth)
922 Record.push_back(BitWidth);
923 if (BitWidth > 64) {
924 Record.push_back(CR.getLower().getActiveWords() |
925 (uint64_t(CR.getUpper().getActiveWords()) << 32));
928 } else {
931 }
932}
933
934void ModuleBitcodeWriter::writeAttributeGroupTable() {
935 const std::vector<ValueEnumerator::IndexAndAttrSet> &AttrGrps =
937 if (AttrGrps.empty()) return;
938
940
942 for (ValueEnumerator::IndexAndAttrSet Pair : AttrGrps) {
943 unsigned AttrListIndex = Pair.first;
944 AttributeSet AS = Pair.second;
945 Record.push_back(VE.getAttributeGroupID(Pair));
946 Record.push_back(AttrListIndex);
947
948 for (Attribute Attr : AS) {
949 if (Attr.isEnumAttribute()) {
950 Record.push_back(0);
951 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
952 } else if (Attr.isIntAttribute()) {
953 Record.push_back(1);
954 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
955 Record.push_back(Attr.getValueAsInt());
956 } else if (Attr.isStringAttribute()) {
957 StringRef Kind = Attr.getKindAsString();
958 StringRef Val = Attr.getValueAsString();
959
960 Record.push_back(Val.empty() ? 3 : 4);
961 Record.append(Kind.begin(), Kind.end());
962 Record.push_back(0);
963 if (!Val.empty()) {
964 Record.append(Val.begin(), Val.end());
965 Record.push_back(0);
966 }
967 } else if (Attr.isTypeAttribute()) {
968 Type *Ty = Attr.getValueAsType();
969 Record.push_back(Ty ? 6 : 5);
970 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
971 if (Ty)
972 Record.push_back(VE.getTypeID(Attr.getValueAsType()));
973 } else if (Attr.isConstantRangeAttribute()) {
974 Record.push_back(7);
975 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
976 emitConstantRange(Record, Attr.getValueAsConstantRange(),
977 /*EmitBitWidth=*/true);
978 } else {
979 assert(Attr.isConstantRangeListAttribute());
980 Record.push_back(8);
981 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
982 ArrayRef<ConstantRange> Val = Attr.getValueAsConstantRangeList();
983 Record.push_back(Val.size());
984 Record.push_back(Val[0].getBitWidth());
985 for (auto &CR : Val)
986 emitConstantRange(Record, CR, /*EmitBitWidth=*/false);
987 }
988 }
989
991 Record.clear();
992 }
993
994 Stream.ExitBlock();
995}
996
997void ModuleBitcodeWriter::writeAttributeTable() {
998 const std::vector<AttributeList> &Attrs = VE.getAttributeLists();
999 if (Attrs.empty()) return;
1000
1002
1004 for (const AttributeList &AL : Attrs) {
1005 for (unsigned i : AL.indexes()) {
1006 AttributeSet AS = AL.getAttributes(i);
1007 if (AS.hasAttributes())
1008 Record.push_back(VE.getAttributeGroupID({i, AS}));
1009 }
1010
1012 Record.clear();
1013 }
1014
1015 Stream.ExitBlock();
1016}
1017
1018/// WriteTypeTable - Write out the type table for a module.
1019void ModuleBitcodeWriter::writeTypeTable() {
1020 const ValueEnumerator::TypeList &TypeList = VE.getTypes();
1021
1022 Stream.EnterSubblock(bitc::TYPE_BLOCK_ID_NEW, 4 /*count from # abbrevs */);
1024
1026
1027 // Abbrev for TYPE_CODE_OPAQUE_POINTER.
1028 auto Abbv = std::make_shared<BitCodeAbbrev>();
1030 Abbv->Add(BitCodeAbbrevOp(0)); // Addrspace = 0
1031 unsigned OpaquePtrAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1032
1033 // Abbrev for TYPE_CODE_FUNCTION.
1034 Abbv = std::make_shared<BitCodeAbbrev>();
1036 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg
1038 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
1039 unsigned FunctionAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1040
1041 // Abbrev for TYPE_CODE_STRUCT_ANON.
1042 Abbv = std::make_shared<BitCodeAbbrev>();
1044 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
1046 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
1047 unsigned StructAnonAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1048
1049 // Abbrev for TYPE_CODE_STRUCT_NAME.
1050 Abbv = std::make_shared<BitCodeAbbrev>();
1054 unsigned StructNameAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1055
1056 // Abbrev for TYPE_CODE_STRUCT_NAMED.
1057 Abbv = std::make_shared<BitCodeAbbrev>();
1059 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
1061 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
1062 unsigned StructNamedAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1063
1064 // Abbrev for TYPE_CODE_ARRAY.
1065 Abbv = std::make_shared<BitCodeAbbrev>();
1067 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size
1068 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
1069 unsigned ArrayAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1070
1071 // Emit an entry count so the reader can reserve space.
1072 TypeVals.push_back(TypeList.size());
1073 Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
1074 TypeVals.clear();
1075
1076 // Loop over all of the types, emitting each in turn.
1077 for (Type *T : TypeList) {
1078 int AbbrevToUse = 0;
1079 unsigned Code = 0;
1080
1081 switch (T->getTypeID()) {
1095 case Type::IntegerTyID:
1096 // INTEGER: [width]
1098 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
1099 break;
1100 case Type::PointerTyID: {
1101 PointerType *PTy = cast<PointerType>(T);
1102 unsigned AddressSpace = PTy->getAddressSpace();
1103 // OPAQUE_POINTER: [address space]
1105 TypeVals.push_back(AddressSpace);
1106 if (AddressSpace == 0)
1107 AbbrevToUse = OpaquePtrAbbrev;
1108 break;
1109 }
1110 case Type::FunctionTyID: {
1111 FunctionType *FT = cast<FunctionType>(T);
1112 // FUNCTION: [isvararg, retty, paramty x N]
1114 TypeVals.push_back(FT->isVarArg());
1115 TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
1116 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
1117 TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
1118 AbbrevToUse = FunctionAbbrev;
1119 break;
1120 }
1121 case Type::StructTyID: {
1122 StructType *ST = cast<StructType>(T);
1123 // STRUCT: [ispacked, eltty x N]
1124 TypeVals.push_back(ST->isPacked());
1125 // Output all of the element types.
1126 for (Type *ET : ST->elements())
1127 TypeVals.push_back(VE.getTypeID(ET));
1128
1129 if (ST->isLiteral()) {
1131 AbbrevToUse = StructAnonAbbrev;
1132 } else {
1133 if (ST->isOpaque()) {
1135 } else {
1137 AbbrevToUse = StructNamedAbbrev;
1138 }
1139
1140 // Emit the name if it is present.
1141 if (!ST->getName().empty())
1143 StructNameAbbrev);
1144 }
1145 break;
1146 }
1147 case Type::ArrayTyID: {
1148 ArrayType *AT = cast<ArrayType>(T);
1149 // ARRAY: [numelts, eltty]
1151 TypeVals.push_back(AT->getNumElements());
1152 TypeVals.push_back(VE.getTypeID(AT->getElementType()));
1153 AbbrevToUse = ArrayAbbrev;
1154 break;
1155 }
1158 VectorType *VT = cast<VectorType>(T);
1159 // VECTOR [numelts, eltty] or
1160 // [numelts, eltty, scalable]
1162 TypeVals.push_back(VT->getElementCount().getKnownMinValue());
1163 TypeVals.push_back(VE.getTypeID(VT->getElementType()));
1164 if (isa<ScalableVectorType>(VT))
1165 TypeVals.push_back(true);
1166 break;
1167 }
1168 case Type::TargetExtTyID: {
1169 TargetExtType *TET = cast<TargetExtType>(T);
1172 StructNameAbbrev);
1173 TypeVals.push_back(TET->getNumTypeParameters());
1174 for (Type *InnerTy : TET->type_params())
1175 TypeVals.push_back(VE.getTypeID(InnerTy));
1176 for (unsigned IntParam : TET->int_params())
1177 TypeVals.push_back(IntParam);
1178 break;
1179 }
1181 llvm_unreachable("Typed pointers cannot be added to IR modules");
1182 }
1183
1184 // Emit the finished record.
1185 Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
1186 TypeVals.clear();
1187 }
1188
1189 Stream.ExitBlock();
1190}
1191
1192static unsigned getEncodedLinkage(const GlobalValue::LinkageTypes Linkage) {
1193 switch (Linkage) {
1195 return 0;
1197 return 16;
1199 return 2;
1201 return 3;
1203 return 18;
1205 return 7;
1207 return 8;
1209 return 9;
1211 return 17;
1213 return 19;
1215 return 12;
1216 }
1217 llvm_unreachable("Invalid linkage");
1218}
1219
1220static unsigned getEncodedLinkage(const GlobalValue &GV) {
1221 return getEncodedLinkage(GV.getLinkage());
1222}
1223
1225 uint64_t RawFlags = 0;
1226 RawFlags |= Flags.ReadNone;
1227 RawFlags |= (Flags.ReadOnly << 1);
1228 RawFlags |= (Flags.NoRecurse << 2);
1229 RawFlags |= (Flags.ReturnDoesNotAlias << 3);
1230 RawFlags |= (Flags.NoInline << 4);
1231 RawFlags |= (Flags.AlwaysInline << 5);
1232 RawFlags |= (Flags.NoUnwind << 6);
1233 RawFlags |= (Flags.MayThrow << 7);
1234 RawFlags |= (Flags.HasUnknownCall << 8);
1235 RawFlags |= (Flags.MustBeUnreachable << 9);
1236 return RawFlags;
1237}
1238
1239// Decode the flags for GlobalValue in the summary. See getDecodedGVSummaryFlags
1240// in BitcodeReader.cpp.
1242 bool ImportAsDecl = false) {
1243 uint64_t RawFlags = 0;
1244
1245 RawFlags |= Flags.NotEligibleToImport; // bool
1246 RawFlags |= (Flags.Live << 1);
1247 RawFlags |= (Flags.DSOLocal << 2);
1248 RawFlags |= (Flags.CanAutoHide << 3);
1249
1250 // Linkage don't need to be remapped at that time for the summary. Any future
1251 // change to the getEncodedLinkage() function will need to be taken into
1252 // account here as well.
1253 RawFlags = (RawFlags << 4) | Flags.Linkage; // 4 bits
1254
1255 RawFlags |= (Flags.Visibility << 8); // 2 bits
1256
1257 unsigned ImportType = Flags.ImportType | ImportAsDecl;
1258 RawFlags |= (ImportType << 10); // 1 bit
1259
1260 return RawFlags;
1261}
1262
1264 uint64_t RawFlags = Flags.MaybeReadOnly | (Flags.MaybeWriteOnly << 1) |
1265 (Flags.Constant << 2) | Flags.VCallVisibility << 3;
1266 return RawFlags;
1267}
1268
1270 uint64_t RawFlags = 0;
1271
1272 RawFlags |= CI.Hotness; // 3 bits
1273 RawFlags |= (CI.HasTailCall << 3); // 1 bit
1274
1275 return RawFlags;
1276}
1277
1279 uint64_t RawFlags = 0;
1280
1281 RawFlags |= CI.RelBlockFreq; // CalleeInfo::RelBlockFreqBits bits
1282 RawFlags |= (CI.HasTailCall << CalleeInfo::RelBlockFreqBits); // 1 bit
1283
1284 return RawFlags;
1285}
1286
1287static unsigned getEncodedVisibility(const GlobalValue &GV) {
1288 switch (GV.getVisibility()) {
1289 case GlobalValue::DefaultVisibility: return 0;
1290 case GlobalValue::HiddenVisibility: return 1;
1291 case GlobalValue::ProtectedVisibility: return 2;
1292 }
1293 llvm_unreachable("Invalid visibility");
1294}
1295
1296static unsigned getEncodedDLLStorageClass(const GlobalValue &GV) {
1297 switch (GV.getDLLStorageClass()) {
1298 case GlobalValue::DefaultStorageClass: return 0;
1301 }
1302 llvm_unreachable("Invalid DLL storage class");
1303}
1304
1305static unsigned getEncodedThreadLocalMode(const GlobalValue &GV) {
1306 switch (GV.getThreadLocalMode()) {
1307 case GlobalVariable::NotThreadLocal: return 0;
1308 case GlobalVariable::GeneralDynamicTLSModel: return 1;
1309 case GlobalVariable::LocalDynamicTLSModel: return 2;
1310 case GlobalVariable::InitialExecTLSModel: return 3;
1311 case GlobalVariable::LocalExecTLSModel: return 4;
1312 }
1313 llvm_unreachable("Invalid TLS model");
1314}
1315
1316static unsigned getEncodedComdatSelectionKind(const Comdat &C) {
1317 switch (C.getSelectionKind()) {
1318 case Comdat::Any:
1320 case Comdat::ExactMatch:
1322 case Comdat::Largest:
1326 case Comdat::SameSize:
1328 }
1329 llvm_unreachable("Invalid selection kind");
1330}
1331
1332static unsigned getEncodedUnnamedAddr(const GlobalValue &GV) {
1333 switch (GV.getUnnamedAddr()) {
1334 case GlobalValue::UnnamedAddr::None: return 0;
1335 case GlobalValue::UnnamedAddr::Local: return 2;
1336 case GlobalValue::UnnamedAddr::Global: return 1;
1337 }
1338 llvm_unreachable("Invalid unnamed_addr");
1339}
1340
1341size_t ModuleBitcodeWriter::addToStrtab(StringRef Str) {
1342 if (GenerateHash)
1343 Hasher.update(Str);
1344 return StrtabBuilder.add(Str);
1345}
1346
1347void ModuleBitcodeWriter::writeComdats() {
1349 for (const Comdat *C : VE.getComdats()) {
1350 // COMDAT: [strtab offset, strtab size, selection_kind]
1351 Vals.push_back(addToStrtab(C->getName()));
1352 Vals.push_back(C->getName().size());
1354 Stream.EmitRecord(bitc::MODULE_CODE_COMDAT, Vals, /*AbbrevToUse=*/0);
1355 Vals.clear();
1356 }
1357}
1358
1359/// Write a record that will eventually hold the word offset of the
1360/// module-level VST. For now the offset is 0, which will be backpatched
1361/// after the real VST is written. Saves the bit offset to backpatch.
1362void ModuleBitcodeWriter::writeValueSymbolTableForwardDecl() {
1363 // Write a placeholder value in for the offset of the real VST,
1364 // which is written after the function blocks so that it can include
1365 // the offset of each function. The placeholder offset will be
1366 // updated when the real VST is written.
1367 auto Abbv = std::make_shared<BitCodeAbbrev>();
1369 // Blocks are 32-bit aligned, so we can use a 32-bit word offset to
1370 // hold the real VST offset. Must use fixed instead of VBR as we don't
1371 // know how many VBR chunks to reserve ahead of time.
1373 unsigned VSTOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1374
1375 // Emit the placeholder
1377 Stream.EmitRecordWithAbbrev(VSTOffsetAbbrev, Vals);
1378
1379 // Compute and save the bit offset to the placeholder, which will be
1380 // patched when the real VST is written. We can simply subtract the 32-bit
1381 // fixed size from the current bit number to get the location to backpatch.
1382 VSTOffsetPlaceholder = Stream.GetCurrentBitNo() - 32;
1383}
1384
1386
1387/// Determine the encoding to use for the given string name and length.
1389 bool isChar6 = true;
1390 for (char C : Str) {
1391 if (isChar6)
1392 isChar6 = BitCodeAbbrevOp::isChar6(C);
1393 if ((unsigned char)C & 128)
1394 // don't bother scanning the rest.
1395 return SE_Fixed8;
1396 }
1397 if (isChar6)
1398 return SE_Char6;
1399 return SE_Fixed7;
1400}
1401
1402static_assert(sizeof(GlobalValue::SanitizerMetadata) <= sizeof(unsigned),
1403 "Sanitizer Metadata is too large for naive serialization.");
1404static unsigned
1406 return Meta.NoAddress | (Meta.NoHWAddress << 1) |
1407 (Meta.Memtag << 2) | (Meta.IsDynInit << 3);
1408}
1409
1410/// Emit top-level description of module, including target triple, inline asm,
1411/// descriptors for global variables, and function prototype info.
1412/// Returns the bit offset to backpatch with the location of the real VST.
1413void ModuleBitcodeWriter::writeModuleInfo() {
1414 // Emit various pieces of data attached to a module.
1415 if (!M.getTargetTriple().empty())
1416 writeStringRecord(Stream, bitc::MODULE_CODE_TRIPLE, M.getTargetTriple(),
1417 0 /*TODO*/);
1418 const std::string &DL = M.getDataLayoutStr();
1419 if (!DL.empty())
1421 if (!M.getModuleInlineAsm().empty())
1422 writeStringRecord(Stream, bitc::MODULE_CODE_ASM, M.getModuleInlineAsm(),
1423 0 /*TODO*/);
1424
1425 // Emit information about sections and GC, computing how many there are. Also
1426 // compute the maximum alignment value.
1427 std::map<std::string, unsigned> SectionMap;
1428 std::map<std::string, unsigned> GCMap;
1429 MaybeAlign MaxAlignment;
1430 unsigned MaxGlobalType = 0;
1431 const auto UpdateMaxAlignment = [&MaxAlignment](const MaybeAlign A) {
1432 if (A)
1433 MaxAlignment = !MaxAlignment ? *A : std::max(*MaxAlignment, *A);
1434 };
1435 for (const GlobalVariable &GV : M.globals()) {
1436 UpdateMaxAlignment(GV.getAlign());
1437 MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV.getValueType()));
1438 if (GV.hasSection()) {
1439 // Give section names unique ID's.
1440 unsigned &Entry = SectionMap[std::string(GV.getSection())];
1441 if (!Entry) {
1442 writeStringRecord(Stream, bitc::MODULE_CODE_SECTIONNAME, GV.getSection(),
1443 0 /*TODO*/);
1444 Entry = SectionMap.size();
1445 }
1446 }
1447 }
1448 for (const Function &F : M) {
1449 UpdateMaxAlignment(F.getAlign());
1450 if (F.hasSection()) {
1451 // Give section names unique ID's.
1452 unsigned &Entry = SectionMap[std::string(F.getSection())];
1453 if (!Entry) {
1455 0 /*TODO*/);
1456 Entry = SectionMap.size();
1457 }
1458 }
1459 if (F.hasGC()) {
1460 // Same for GC names.
1461 unsigned &Entry = GCMap[F.getGC()];
1462 if (!Entry) {
1464 0 /*TODO*/);
1465 Entry = GCMap.size();
1466 }
1467 }
1468 }
1469
1470 // Emit abbrev for globals, now that we know # sections and max alignment.
1471 unsigned SimpleGVarAbbrev = 0;
1472 if (!M.global_empty()) {
1473 // Add an abbrev for common globals with no visibility or thread localness.
1474 auto Abbv = std::make_shared<BitCodeAbbrev>();
1479 Log2_32_Ceil(MaxGlobalType+1)));
1480 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // AddrSpace << 2
1481 //| explicitType << 1
1482 //| constant
1483 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer.
1484 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // Linkage.
1485 if (!MaxAlignment) // Alignment.
1486 Abbv->Add(BitCodeAbbrevOp(0));
1487 else {
1488 unsigned MaxEncAlignment = getEncodedAlign(MaxAlignment);
1490 Log2_32_Ceil(MaxEncAlignment+1)));
1491 }
1492 if (SectionMap.empty()) // Section.
1493 Abbv->Add(BitCodeAbbrevOp(0));
1494 else
1496 Log2_32_Ceil(SectionMap.size()+1)));
1497 // Don't bother emitting vis + thread local.
1498 SimpleGVarAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1499 }
1500
1502 // Emit the module's source file name.
1503 {
1504 StringEncoding Bits = getStringEncoding(M.getSourceFileName());
1506 if (Bits == SE_Char6)
1507 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Char6);
1508 else if (Bits == SE_Fixed7)
1509 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7);
1510
1511 // MODULE_CODE_SOURCE_FILENAME: [namechar x N]
1512 auto Abbv = std::make_shared<BitCodeAbbrev>();
1515 Abbv->Add(AbbrevOpToUse);
1516 unsigned FilenameAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1517
1518 for (const auto P : M.getSourceFileName())
1519 Vals.push_back((unsigned char)P);
1520
1521 // Emit the finished record.
1522 Stream.EmitRecord(bitc::MODULE_CODE_SOURCE_FILENAME, Vals, FilenameAbbrev);
1523 Vals.clear();
1524 }
1525
1526 // Emit the global variable information.
1527 for (const GlobalVariable &GV : M.globals()) {
1528 unsigned AbbrevToUse = 0;
1529
1530 // GLOBALVAR: [strtab offset, strtab size, type, isconst, initid,
1531 // linkage, alignment, section, visibility, threadlocal,
1532 // unnamed_addr, externally_initialized, dllstorageclass,
1533 // comdat, attributes, DSO_Local, GlobalSanitizer, code_model]
1534 Vals.push_back(addToStrtab(GV.getName()));
1535 Vals.push_back(GV.getName().size());
1536 Vals.push_back(VE.getTypeID(GV.getValueType()));
1537 Vals.push_back(GV.getType()->getAddressSpace() << 2 | 2 | GV.isConstant());
1538 Vals.push_back(GV.isDeclaration() ? 0 :
1539 (VE.getValueID(GV.getInitializer()) + 1));
1540 Vals.push_back(getEncodedLinkage(GV));
1541 Vals.push_back(getEncodedAlign(GV.getAlign()));
1542 Vals.push_back(GV.hasSection() ? SectionMap[std::string(GV.getSection())]
1543 : 0);
1544 if (GV.isThreadLocal() ||
1545 GV.getVisibility() != GlobalValue::DefaultVisibility ||
1546 GV.getUnnamedAddr() != GlobalValue::UnnamedAddr::None ||
1547 GV.isExternallyInitialized() ||
1548 GV.getDLLStorageClass() != GlobalValue::DefaultStorageClass ||
1549 GV.hasComdat() || GV.hasAttributes() || GV.isDSOLocal() ||
1550 GV.hasPartition() || GV.hasSanitizerMetadata() || GV.getCodeModel()) {
1554 Vals.push_back(GV.isExternallyInitialized());
1556 Vals.push_back(GV.hasComdat() ? VE.getComdatID(GV.getComdat()) : 0);
1557
1558 auto AL = GV.getAttributesAsList(AttributeList::FunctionIndex);
1559 Vals.push_back(VE.getAttributeListID(AL));
1560
1561 Vals.push_back(GV.isDSOLocal());
1562 Vals.push_back(addToStrtab(GV.getPartition()));
1563 Vals.push_back(GV.getPartition().size());
1564
1565 Vals.push_back((GV.hasSanitizerMetadata() ? serializeSanitizerMetadata(
1566 GV.getSanitizerMetadata())
1567 : 0));
1568 Vals.push_back(GV.getCodeModelRaw());
1569 } else {
1570 AbbrevToUse = SimpleGVarAbbrev;
1571 }
1572
1573 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
1574 Vals.clear();
1575 }
1576
1577 // Emit the function proto information.
1578 for (const Function &F : M) {
1579 // FUNCTION: [strtab offset, strtab size, type, callingconv, isproto,
1580 // linkage, paramattrs, alignment, section, visibility, gc,
1581 // unnamed_addr, prologuedata, dllstorageclass, comdat,
1582 // prefixdata, personalityfn, DSO_Local, addrspace]
1583 Vals.push_back(addToStrtab(F.getName()));
1584 Vals.push_back(F.getName().size());
1585 Vals.push_back(VE.getTypeID(F.getFunctionType()));
1586 Vals.push_back(F.getCallingConv());
1587 Vals.push_back(F.isDeclaration());
1589 Vals.push_back(VE.getAttributeListID(F.getAttributes()));
1590 Vals.push_back(getEncodedAlign(F.getAlign()));
1591 Vals.push_back(F.hasSection() ? SectionMap[std::string(F.getSection())]
1592 : 0);
1594 Vals.push_back(F.hasGC() ? GCMap[F.getGC()] : 0);
1596 Vals.push_back(F.hasPrologueData() ? (VE.getValueID(F.getPrologueData()) + 1)
1597 : 0);
1599 Vals.push_back(F.hasComdat() ? VE.getComdatID(F.getComdat()) : 0);
1600 Vals.push_back(F.hasPrefixData() ? (VE.getValueID(F.getPrefixData()) + 1)
1601 : 0);
1602 Vals.push_back(
1603 F.hasPersonalityFn() ? (VE.getValueID(F.getPersonalityFn()) + 1) : 0);
1604
1605 Vals.push_back(F.isDSOLocal());
1606 Vals.push_back(F.getAddressSpace());
1607 Vals.push_back(addToStrtab(F.getPartition()));
1608 Vals.push_back(F.getPartition().size());
1609
1610 unsigned AbbrevToUse = 0;
1611 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
1612 Vals.clear();
1613 }
1614
1615 // Emit the alias information.
1616 for (const GlobalAlias &A : M.aliases()) {
1617 // ALIAS: [strtab offset, strtab size, alias type, aliasee val#, linkage,
1618 // visibility, dllstorageclass, threadlocal, unnamed_addr,
1619 // DSO_Local]
1620 Vals.push_back(addToStrtab(A.getName()));
1621 Vals.push_back(A.getName().size());
1622 Vals.push_back(VE.getTypeID(A.getValueType()));
1623 Vals.push_back(A.getType()->getAddressSpace());
1624 Vals.push_back(VE.getValueID(A.getAliasee()));
1630 Vals.push_back(A.isDSOLocal());
1631 Vals.push_back(addToStrtab(A.getPartition()));
1632 Vals.push_back(A.getPartition().size());
1633
1634 unsigned AbbrevToUse = 0;
1635 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
1636 Vals.clear();
1637 }
1638
1639 // Emit the ifunc information.
1640 for (const GlobalIFunc &I : M.ifuncs()) {
1641 // IFUNC: [strtab offset, strtab size, ifunc type, address space, resolver
1642 // val#, linkage, visibility, DSO_Local]
1643 Vals.push_back(addToStrtab(I.getName()));
1644 Vals.push_back(I.getName().size());
1645 Vals.push_back(VE.getTypeID(I.getValueType()));
1646 Vals.push_back(I.getType()->getAddressSpace());
1647 Vals.push_back(VE.getValueID(I.getResolver()));
1650 Vals.push_back(I.isDSOLocal());
1651 Vals.push_back(addToStrtab(I.getPartition()));
1652 Vals.push_back(I.getPartition().size());
1653 Stream.EmitRecord(bitc::MODULE_CODE_IFUNC, Vals);
1654 Vals.clear();
1655 }
1656
1657 writeValueSymbolTableForwardDecl();
1658}
1659
1661 uint64_t Flags = 0;
1662
1663 if (const auto *OBO = dyn_cast<OverflowingBinaryOperator>(V)) {
1664 if (OBO->hasNoSignedWrap())
1665 Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP;
1666 if (OBO->hasNoUnsignedWrap())
1667 Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP;
1668 } else if (const auto *PEO = dyn_cast<PossiblyExactOperator>(V)) {
1669 if (PEO->isExact())
1670 Flags |= 1 << bitc::PEO_EXACT;
1671 } else if (const auto *PDI = dyn_cast<PossiblyDisjointInst>(V)) {
1672 if (PDI->isDisjoint())
1673 Flags |= 1 << bitc::PDI_DISJOINT;
1674 } else if (const auto *FPMO = dyn_cast<FPMathOperator>(V)) {
1675 if (FPMO->hasAllowReassoc())
1676 Flags |= bitc::AllowReassoc;
1677 if (FPMO->hasNoNaNs())
1678 Flags |= bitc::NoNaNs;
1679 if (FPMO->hasNoInfs())
1680 Flags |= bitc::NoInfs;
1681 if (FPMO->hasNoSignedZeros())
1682 Flags |= bitc::NoSignedZeros;
1683 if (FPMO->hasAllowReciprocal())
1684 Flags |= bitc::AllowReciprocal;
1685 if (FPMO->hasAllowContract())
1686 Flags |= bitc::AllowContract;
1687 if (FPMO->hasApproxFunc())
1688 Flags |= bitc::ApproxFunc;
1689 } else if (const auto *NNI = dyn_cast<PossiblyNonNegInst>(V)) {
1690 if (NNI->hasNonNeg())
1691 Flags |= 1 << bitc::PNNI_NON_NEG;
1692 } else if (const auto *TI = dyn_cast<TruncInst>(V)) {
1693 if (TI->hasNoSignedWrap())
1694 Flags |= 1 << bitc::TIO_NO_SIGNED_WRAP;
1695 if (TI->hasNoUnsignedWrap())
1696 Flags |= 1 << bitc::TIO_NO_UNSIGNED_WRAP;
1697 } else if (const auto *GEP = dyn_cast<GEPOperator>(V)) {
1698 if (GEP->isInBounds())
1699 Flags |= 1 << bitc::GEP_INBOUNDS;
1700 if (GEP->hasNoUnsignedSignedWrap())
1701 Flags |= 1 << bitc::GEP_NUSW;
1702 if (GEP->hasNoUnsignedWrap())
1703 Flags |= 1 << bitc::GEP_NUW;
1704 }
1705
1706 return Flags;
1707}
1708
1709void ModuleBitcodeWriter::writeValueAsMetadata(
1711 // Mimic an MDNode with a value as one operand.
1712 Value *V = MD->getValue();
1713 Record.push_back(VE.getTypeID(V->getType()));
1714 Record.push_back(VE.getValueID(V));
1716 Record.clear();
1717}
1718
1719void ModuleBitcodeWriter::writeMDTuple(const MDTuple *N,
1721 unsigned Abbrev) {
1722 for (const MDOperand &MDO : N->operands()) {
1723 Metadata *MD = MDO;
1724 assert(!(MD && isa<LocalAsMetadata>(MD)) &&
1725 "Unexpected function-local metadata");
1726 Record.push_back(VE.getMetadataOrNullID(MD));
1727 }
1728 Stream.EmitRecord(N->isDistinct() ? bitc::METADATA_DISTINCT_NODE
1730 Record, Abbrev);
1731 Record.clear();
1732}
1733
1734unsigned ModuleBitcodeWriter::createDILocationAbbrev() {
1735 // Assume the column is usually under 128, and always output the inlined-at
1736 // location (it's never more expensive than building an array size 1).
1737 auto Abbv = std::make_shared<BitCodeAbbrev>();
1745 return Stream.EmitAbbrev(std::move(Abbv));
1746}
1747
1748void ModuleBitcodeWriter::writeDILocation(const DILocation *N,
1750 unsigned &Abbrev) {
1751 if (!Abbrev)
1752 Abbrev = createDILocationAbbrev();
1753
1754 Record.push_back(N->isDistinct());
1755 Record.push_back(N->getLine());
1756 Record.push_back(N->getColumn());
1757 Record.push_back(VE.getMetadataID(N->getScope()));
1758 Record.push_back(VE.getMetadataOrNullID(N->getInlinedAt()));
1759 Record.push_back(N->isImplicitCode());
1760
1761 Stream.EmitRecord(bitc::METADATA_LOCATION, Record, Abbrev);
1762 Record.clear();
1763}
1764
1765unsigned ModuleBitcodeWriter::createGenericDINodeAbbrev() {
1766 // Assume the column is usually under 128, and always output the inlined-at
1767 // location (it's never more expensive than building an array size 1).
1768 auto Abbv = std::make_shared<BitCodeAbbrev>();
1776 return Stream.EmitAbbrev(std::move(Abbv));
1777}
1778
1779void ModuleBitcodeWriter::writeGenericDINode(const GenericDINode *N,
1781 unsigned &Abbrev) {
1782 if (!Abbrev)
1783 Abbrev = createGenericDINodeAbbrev();
1784
1785 Record.push_back(N->isDistinct());
1786 Record.push_back(N->getTag());
1787 Record.push_back(0); // Per-tag version field; unused for now.
1788
1789 for (auto &I : N->operands())
1790 Record.push_back(VE.getMetadataOrNullID(I));
1791
1793 Record.clear();
1794}
1795
1796void ModuleBitcodeWriter::writeDISubrange(const DISubrange *N,
1798 unsigned Abbrev) {
1799 const uint64_t Version = 2 << 1;
1800 Record.push_back((uint64_t)N->isDistinct() | Version);
1801 Record.push_back(VE.getMetadataOrNullID(N->getRawCountNode()));
1802 Record.push_back(VE.getMetadataOrNullID(N->getRawLowerBound()));
1803 Record.push_back(VE.getMetadataOrNullID(N->getRawUpperBound()));
1804 Record.push_back(VE.getMetadataOrNullID(N->getRawStride()));
1805
1806 Stream.EmitRecord(bitc::METADATA_SUBRANGE, Record, Abbrev);
1807 Record.clear();
1808}
1809
1810void ModuleBitcodeWriter::writeDIGenericSubrange(
1812 unsigned Abbrev) {
1813 Record.push_back((uint64_t)N->isDistinct());
1814 Record.push_back(VE.getMetadataOrNullID(N->getRawCountNode()));
1815 Record.push_back(VE.getMetadataOrNullID(N->getRawLowerBound()));
1816 Record.push_back(VE.getMetadataOrNullID(N->getRawUpperBound()));
1817 Record.push_back(VE.getMetadataOrNullID(N->getRawStride()));
1818
1820 Record.clear();
1821}
1822
1823void ModuleBitcodeWriter::writeDIEnumerator(const DIEnumerator *N,
1825 unsigned Abbrev) {
1826 const uint64_t IsBigInt = 1 << 2;
1827 Record.push_back(IsBigInt | (N->isUnsigned() << 1) | N->isDistinct());
1828 Record.push_back(N->getValue().getBitWidth());
1829 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1830 emitWideAPInt(Record, N->getValue());
1831
1833 Record.clear();
1834}
1835
1836void ModuleBitcodeWriter::writeDIBasicType(const DIBasicType *N,
1838 unsigned Abbrev) {
1839 Record.push_back(N->isDistinct());
1840 Record.push_back(N->getTag());
1841 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1842 Record.push_back(N->getSizeInBits());
1843 Record.push_back(N->getAlignInBits());
1844 Record.push_back(N->getEncoding());
1845 Record.push_back(N->getFlags());
1846
1848 Record.clear();
1849}
1850
1851void ModuleBitcodeWriter::writeDIStringType(const DIStringType *N,
1853 unsigned Abbrev) {
1854 Record.push_back(N->isDistinct());
1855 Record.push_back(N->getTag());
1856 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1857 Record.push_back(VE.getMetadataOrNullID(N->getStringLength()));
1858 Record.push_back(VE.getMetadataOrNullID(N->getStringLengthExp()));
1859 Record.push_back(VE.getMetadataOrNullID(N->getStringLocationExp()));
1860 Record.push_back(N->getSizeInBits());
1861 Record.push_back(N->getAlignInBits());
1862 Record.push_back(N->getEncoding());
1863
1865 Record.clear();
1866}
1867
1868void ModuleBitcodeWriter::writeDIDerivedType(const DIDerivedType *N,
1870 unsigned Abbrev) {
1871 Record.push_back(N->isDistinct());
1872 Record.push_back(N->getTag());
1873 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1874 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1875 Record.push_back(N->getLine());
1876 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1877 Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
1878 Record.push_back(N->getSizeInBits());
1879 Record.push_back(N->getAlignInBits());
1880 Record.push_back(N->getOffsetInBits());
1881 Record.push_back(N->getFlags());
1882 Record.push_back(VE.getMetadataOrNullID(N->getExtraData()));
1883
1884 // DWARF address space is encoded as N->getDWARFAddressSpace() + 1. 0 means
1885 // that there is no DWARF address space associated with DIDerivedType.
1886 if (const auto &DWARFAddressSpace = N->getDWARFAddressSpace())
1887 Record.push_back(*DWARFAddressSpace + 1);
1888 else
1889 Record.push_back(0);
1890
1891 Record.push_back(VE.getMetadataOrNullID(N->getAnnotations().get()));
1892
1893 if (auto PtrAuthData = N->getPtrAuthData())
1894 Record.push_back(PtrAuthData->RawData);
1895 else
1896 Record.push_back(0);
1897
1899 Record.clear();
1900}
1901
1902void ModuleBitcodeWriter::writeDICompositeType(
1904 unsigned Abbrev) {
1905 const unsigned IsNotUsedInOldTypeRef = 0x2;
1906 Record.push_back(IsNotUsedInOldTypeRef | (unsigned)N->isDistinct());
1907 Record.push_back(N->getTag());
1908 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1909 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1910 Record.push_back(N->getLine());
1911 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1912 Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
1913 Record.push_back(N->getSizeInBits());
1914 Record.push_back(N->getAlignInBits());
1915 Record.push_back(N->getOffsetInBits());
1916 Record.push_back(N->getFlags());
1917 Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
1918 Record.push_back(N->getRuntimeLang());
1919 Record.push_back(VE.getMetadataOrNullID(N->getVTableHolder()));
1920 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
1921 Record.push_back(VE.getMetadataOrNullID(N->getRawIdentifier()));
1922 Record.push_back(VE.getMetadataOrNullID(N->getDiscriminator()));
1923 Record.push_back(VE.getMetadataOrNullID(N->getRawDataLocation()));
1924 Record.push_back(VE.getMetadataOrNullID(N->getRawAssociated()));
1925 Record.push_back(VE.getMetadataOrNullID(N->getRawAllocated()));
1926 Record.push_back(VE.getMetadataOrNullID(N->getRawRank()));
1927 Record.push_back(VE.getMetadataOrNullID(N->getAnnotations().get()));
1928
1930 Record.clear();
1931}
1932
1933void ModuleBitcodeWriter::writeDISubroutineType(
1935 unsigned Abbrev) {
1936 const unsigned HasNoOldTypeRefs = 0x2;
1937 Record.push_back(HasNoOldTypeRefs | (unsigned)N->isDistinct());
1938 Record.push_back(N->getFlags());
1939 Record.push_back(VE.getMetadataOrNullID(N->getTypeArray().get()));
1940 Record.push_back(N->getCC());
1941
1943 Record.clear();
1944}
1945
1946void ModuleBitcodeWriter::writeDIFile(const DIFile *N,
1948 unsigned Abbrev) {
1949 Record.push_back(N->isDistinct());
1950 Record.push_back(VE.getMetadataOrNullID(N->getRawFilename()));
1951 Record.push_back(VE.getMetadataOrNullID(N->getRawDirectory()));
1952 if (N->getRawChecksum()) {
1953 Record.push_back(N->getRawChecksum()->Kind);
1954 Record.push_back(VE.getMetadataOrNullID(N->getRawChecksum()->Value));
1955 } else {
1956 // Maintain backwards compatibility with the old internal representation of
1957 // CSK_None in ChecksumKind by writing nulls here when Checksum is None.
1958 Record.push_back(0);
1959 Record.push_back(VE.getMetadataOrNullID(nullptr));
1960 }
1961 auto Source = N->getRawSource();
1962 if (Source)
1963 Record.push_back(VE.getMetadataOrNullID(Source));
1964
1965 Stream.EmitRecord(bitc::METADATA_FILE, Record, Abbrev);
1966 Record.clear();
1967}
1968
1969void ModuleBitcodeWriter::writeDICompileUnit(const DICompileUnit *N,
1971 unsigned Abbrev) {
1972 assert(N->isDistinct() && "Expected distinct compile units");
1973 Record.push_back(/* IsDistinct */ true);
1974 Record.push_back(N->getSourceLanguage());
1975 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1976 Record.push_back(VE.getMetadataOrNullID(N->getRawProducer()));
1977 Record.push_back(N->isOptimized());
1978 Record.push_back(VE.getMetadataOrNullID(N->getRawFlags()));
1979 Record.push_back(N->getRuntimeVersion());
1980 Record.push_back(VE.getMetadataOrNullID(N->getRawSplitDebugFilename()));
1981 Record.push_back(N->getEmissionKind());
1982 Record.push_back(VE.getMetadataOrNullID(N->getEnumTypes().get()));
1983 Record.push_back(VE.getMetadataOrNullID(N->getRetainedTypes().get()));
1984 Record.push_back(/* subprograms */ 0);
1985 Record.push_back(VE.getMetadataOrNullID(N->getGlobalVariables().get()));
1986 Record.push_back(VE.getMetadataOrNullID(N->getImportedEntities().get()));
1987 Record.push_back(N->getDWOId());
1988 Record.push_back(VE.getMetadataOrNullID(N->getMacros().get()));
1989 Record.push_back(N->getSplitDebugInlining());
1990 Record.push_back(N->getDebugInfoForProfiling());
1991 Record.push_back((unsigned)N->getNameTableKind());
1992 Record.push_back(N->getRangesBaseAddress());
1993 Record.push_back(VE.getMetadataOrNullID(N->getRawSysRoot()));
1994 Record.push_back(VE.getMetadataOrNullID(N->getRawSDK()));
1995
1997 Record.clear();
1998}
1999
2000void ModuleBitcodeWriter::writeDISubprogram(const DISubprogram *N,
2002 unsigned Abbrev) {
2003 const uint64_t HasUnitFlag = 1 << 1;
2004 const uint64_t HasSPFlagsFlag = 1 << 2;
2005 Record.push_back(uint64_t(N->isDistinct()) | HasUnitFlag | HasSPFlagsFlag);
2006 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
2007 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2008 Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
2009 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2010 Record.push_back(N->getLine());
2011 Record.push_back(VE.getMetadataOrNullID(N->getType()));
2012 Record.push_back(N->getScopeLine());
2013 Record.push_back(VE.getMetadataOrNullID(N->getContainingType()));
2014 Record.push_back(N->getSPFlags());
2015 Record.push_back(N->getVirtualIndex());
2016 Record.push_back(N->getFlags());
2017 Record.push_back(VE.getMetadataOrNullID(N->getRawUnit()));
2018 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
2019 Record.push_back(VE.getMetadataOrNullID(N->getDeclaration()));
2020 Record.push_back(VE.getMetadataOrNullID(N->getRetainedNodes().get()));
2021 Record.push_back(N->getThisAdjustment());
2022 Record.push_back(VE.getMetadataOrNullID(N->getThrownTypes().get()));
2023 Record.push_back(VE.getMetadataOrNullID(N->getAnnotations().get()));
2024 Record.push_back(VE.getMetadataOrNullID(N->getRawTargetFuncName()));
2025
2027 Record.clear();
2028}
2029
2030void ModuleBitcodeWriter::writeDILexicalBlock(const DILexicalBlock *N,
2032 unsigned Abbrev) {
2033 Record.push_back(N->isDistinct());
2034 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
2035 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2036 Record.push_back(N->getLine());
2037 Record.push_back(N->getColumn());
2038
2040 Record.clear();
2041}
2042
2043void ModuleBitcodeWriter::writeDILexicalBlockFile(
2045 unsigned Abbrev) {
2046 Record.push_back(N->isDistinct());
2047 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
2048 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2049 Record.push_back(N->getDiscriminator());
2050
2052 Record.clear();
2053}
2054
2055void ModuleBitcodeWriter::writeDICommonBlock(const DICommonBlock *N,
2057 unsigned Abbrev) {
2058 Record.push_back(N->isDistinct());
2059 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
2060 Record.push_back(VE.getMetadataOrNullID(N->getDecl()));
2061 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2062 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2063 Record.push_back(N->getLineNo());
2064
2066 Record.clear();
2067}
2068
2069void ModuleBitcodeWriter::writeDINamespace(const DINamespace *N,
2071 unsigned Abbrev) {
2072 Record.push_back(N->isDistinct() | N->getExportSymbols() << 1);
2073 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
2074 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2075
2077 Record.clear();
2078}
2079
2080void ModuleBitcodeWriter::writeDIMacro(const DIMacro *N,
2082 unsigned Abbrev) {
2083 Record.push_back(N->isDistinct());
2084 Record.push_back(N->getMacinfoType());
2085 Record.push_back(N->getLine());
2086 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2087 Record.push_back(VE.getMetadataOrNullID(N->getRawValue()));
2088
2089 Stream.EmitRecord(bitc::METADATA_MACRO, Record, Abbrev);
2090 Record.clear();
2091}
2092
2093void ModuleBitcodeWriter::writeDIMacroFile(const DIMacroFile *N,
2095 unsigned Abbrev) {
2096 Record.push_back(N->isDistinct());
2097 Record.push_back(N->getMacinfoType());
2098 Record.push_back(N->getLine());
2099 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2100 Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
2101
2103 Record.clear();
2104}
2105
2106void ModuleBitcodeWriter::writeDIArgList(const DIArgList *N,
2108 Record.reserve(N->getArgs().size());
2109 for (ValueAsMetadata *MD : N->getArgs())
2110 Record.push_back(VE.getMetadataID(MD));
2111
2113 Record.clear();
2114}
2115
2116void ModuleBitcodeWriter::writeDIModule(const DIModule *N,
2118 unsigned Abbrev) {
2119 Record.push_back(N->isDistinct());
2120 for (auto &I : N->operands())
2121 Record.push_back(VE.getMetadataOrNullID(I));
2122 Record.push_back(N->getLineNo());
2123 Record.push_back(N->getIsDecl());
2124
2125 Stream.EmitRecord(bitc::METADATA_MODULE, Record, Abbrev);
2126 Record.clear();
2127}
2128
2129void ModuleBitcodeWriter::writeDIAssignID(const DIAssignID *N,
2131 unsigned Abbrev) {
2132 // There are no arguments for this metadata type.
2133 Record.push_back(N->isDistinct());
2135 Record.clear();
2136}
2137
2138void ModuleBitcodeWriter::writeDITemplateTypeParameter(
2140 unsigned Abbrev) {
2141 Record.push_back(N->isDistinct());
2142 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2143 Record.push_back(VE.getMetadataOrNullID(N->getType()));
2144 Record.push_back(N->isDefault());
2145
2147 Record.clear();
2148}
2149
2150void ModuleBitcodeWriter::writeDITemplateValueParameter(
2152 unsigned Abbrev) {
2153 Record.push_back(N->isDistinct());
2154 Record.push_back(N->getTag());
2155 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2156 Record.push_back(VE.getMetadataOrNullID(N->getType()));
2157 Record.push_back(N->isDefault());
2158 Record.push_back(VE.getMetadataOrNullID(N->getValue()));
2159
2161 Record.clear();
2162}
2163
2164void ModuleBitcodeWriter::writeDIGlobalVariable(
2166 unsigned Abbrev) {
2167 const uint64_t Version = 2 << 1;
2168 Record.push_back((uint64_t)N->isDistinct() | Version);
2169 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
2170 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2171 Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
2172 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2173 Record.push_back(N->getLine());
2174 Record.push_back(VE.getMetadataOrNullID(N->getType()));
2175 Record.push_back(N->isLocalToUnit());
2176 Record.push_back(N->isDefinition());
2177 Record.push_back(VE.getMetadataOrNullID(N->getStaticDataMemberDeclaration()));
2178 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams()));
2179 Record.push_back(N->getAlignInBits());
2180 Record.push_back(VE.getMetadataOrNullID(N->getAnnotations().get()));
2181
2183 Record.clear();
2184}
2185
2186void ModuleBitcodeWriter::writeDILocalVariable(
2188 unsigned Abbrev) {
2189 // In order to support all possible bitcode formats in BitcodeReader we need
2190 // to distinguish the following cases:
2191 // 1) Record has no artificial tag (Record[1]),
2192 // has no obsolete inlinedAt field (Record[9]).
2193 // In this case Record size will be 8, HasAlignment flag is false.
2194 // 2) Record has artificial tag (Record[1]),
2195 // has no obsolete inlignedAt field (Record[9]).
2196 // In this case Record size will be 9, HasAlignment flag is false.
2197 // 3) Record has both artificial tag (Record[1]) and
2198 // obsolete inlignedAt field (Record[9]).
2199 // In this case Record size will be 10, HasAlignment flag is false.
2200 // 4) Record has neither artificial tag, nor inlignedAt field, but
2201 // HasAlignment flag is true and Record[8] contains alignment value.
2202 const uint64_t HasAlignmentFlag = 1 << 1;
2203 Record.push_back((uint64_t)N->isDistinct() | HasAlignmentFlag);
2204 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
2205 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2206 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2207 Record.push_back(N->getLine());
2208 Record.push_back(VE.getMetadataOrNullID(N->getType()));
2209 Record.push_back(N->getArg());
2210 Record.push_back(N->getFlags());
2211 Record.push_back(N->getAlignInBits());
2212 Record.push_back(VE.getMetadataOrNullID(N->getAnnotations().get()));
2213
2215 Record.clear();
2216}
2217
2218void ModuleBitcodeWriter::writeDILabel(
2220 unsigned Abbrev) {
2221 Record.push_back((uint64_t)N->isDistinct());
2222 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
2223 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2224 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2225 Record.push_back(N->getLine());
2226
2227 Stream.EmitRecord(bitc::METADATA_LABEL, Record, Abbrev);
2228 Record.clear();
2229}
2230
2231void ModuleBitcodeWriter::writeDIExpression(const DIExpression *N,
2233 unsigned Abbrev) {
2234 Record.reserve(N->getElements().size() + 1);
2235 const uint64_t Version = 3 << 1;
2236 Record.push_back((uint64_t)N->isDistinct() | Version);
2237 Record.append(N->elements_begin(), N->elements_end());
2238
2240 Record.clear();
2241}
2242
2243void ModuleBitcodeWriter::writeDIGlobalVariableExpression(
2245 unsigned Abbrev) {
2246 Record.push_back(N->isDistinct());
2247 Record.push_back(VE.getMetadataOrNullID(N->getVariable()));
2248 Record.push_back(VE.getMetadataOrNullID(N->getExpression()));
2249
2251 Record.clear();
2252}
2253
2254void ModuleBitcodeWriter::writeDIObjCProperty(const DIObjCProperty *N,
2256 unsigned Abbrev) {
2257 Record.push_back(N->isDistinct());
2258 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2259 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2260 Record.push_back(N->getLine());
2261 Record.push_back(VE.getMetadataOrNullID(N->getRawSetterName()));
2262 Record.push_back(VE.getMetadataOrNullID(N->getRawGetterName()));
2263 Record.push_back(N->getAttributes());
2264 Record.push_back(VE.getMetadataOrNullID(N->getType()));
2265
2267 Record.clear();
2268}
2269
2270void ModuleBitcodeWriter::writeDIImportedEntity(
2272 unsigned Abbrev) {
2273 Record.push_back(N->isDistinct());
2274 Record.push_back(N->getTag());
2275 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
2276 Record.push_back(VE.getMetadataOrNullID(N->getEntity()));
2277 Record.push_back(N->getLine());
2278 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2279 Record.push_back(VE.getMetadataOrNullID(N->getRawFile()));
2280 Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
2281
2283 Record.clear();
2284}
2285
2286unsigned ModuleBitcodeWriter::createNamedMetadataAbbrev() {
2287 auto Abbv = std::make_shared<BitCodeAbbrev>();
2291 return Stream.EmitAbbrev(std::move(Abbv));
2292}
2293
2294void ModuleBitcodeWriter::writeNamedMetadata(
2296 if (M.named_metadata_empty())
2297 return;
2298
2299 unsigned Abbrev = createNamedMetadataAbbrev();
2300 for (const NamedMDNode &NMD : M.named_metadata()) {
2301 // Write name.
2302 StringRef Str = NMD.getName();
2303 Record.append(Str.bytes_begin(), Str.bytes_end());
2304 Stream.EmitRecord(bitc::METADATA_NAME, Record, Abbrev);
2305 Record.clear();
2306
2307 // Write named metadata operands.
2308 for (const MDNode *N : NMD.operands())
2309 Record.push_back(VE.getMetadataID(N));
2311 Record.clear();
2312 }
2313}
2314
2315unsigned ModuleBitcodeWriter::createMetadataStringsAbbrev() {
2316 auto Abbv = std::make_shared<BitCodeAbbrev>();
2318 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of strings
2319 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // offset to chars
2321 return Stream.EmitAbbrev(std::move(Abbv));
2322}
2323
2324/// Write out a record for MDString.
2325///
2326/// All the metadata strings in a metadata block are emitted in a single
2327/// record. The sizes and strings themselves are shoved into a blob.
2328void ModuleBitcodeWriter::writeMetadataStrings(
2330 if (Strings.empty())
2331 return;
2332
2333 // Start the record with the number of strings.
2334 Record.push_back(bitc::METADATA_STRINGS);
2335 Record.push_back(Strings.size());
2336
2337 // Emit the sizes of the strings in the blob.
2338 SmallString<256> Blob;
2339 {
2340 BitstreamWriter W(Blob);
2341 for (const Metadata *MD : Strings)
2342 W.EmitVBR(cast<MDString>(MD)->getLength(), 6);
2343 W.FlushToWord();
2344 }
2345
2346 // Add the offset to the strings to the record.
2347 Record.push_back(Blob.size());
2348
2349 // Add the strings to the blob.
2350 for (const Metadata *MD : Strings)
2351 Blob.append(cast<MDString>(MD)->getString());
2352
2353 // Emit the final record.
2354 Stream.EmitRecordWithBlob(createMetadataStringsAbbrev(), Record, Blob);
2355 Record.clear();
2356}
2357
2358// Generates an enum to use as an index in the Abbrev array of Metadata record.
2359enum MetadataAbbrev : unsigned {
2360#define HANDLE_MDNODE_LEAF(CLASS) CLASS##AbbrevID,
2361#include "llvm/IR/Metadata.def"
2364
2365void ModuleBitcodeWriter::writeMetadataRecords(
2367 std::vector<unsigned> *MDAbbrevs, std::vector<uint64_t> *IndexPos) {
2368 if (MDs.empty())
2369 return;
2370
2371 // Initialize MDNode abbreviations.
2372#define HANDLE_MDNODE_LEAF(CLASS) unsigned CLASS##Abbrev = 0;
2373#include "llvm/IR/Metadata.def"
2374
2375 for (const Metadata *MD : MDs) {
2376 if (IndexPos)
2377 IndexPos->push_back(Stream.GetCurrentBitNo());
2378 if (const MDNode *N = dyn_cast<MDNode>(MD)) {
2379 assert(N->isResolved() && "Expected forward references to be resolved");
2380
2381 switch (N->getMetadataID()) {
2382 default:
2383 llvm_unreachable("Invalid MDNode subclass");
2384#define HANDLE_MDNODE_LEAF(CLASS) \
2385 case Metadata::CLASS##Kind: \
2386 if (MDAbbrevs) \
2387 write##CLASS(cast<CLASS>(N), Record, \
2388 (*MDAbbrevs)[MetadataAbbrev::CLASS##AbbrevID]); \
2389 else \
2390 write##CLASS(cast<CLASS>(N), Record, CLASS##Abbrev); \
2391 continue;
2392#include "llvm/IR/Metadata.def"
2393 }
2394 }
2395 if (auto *AL = dyn_cast<DIArgList>(MD)) {
2397 continue;
2398 }
2399 writeValueAsMetadata(cast<ValueAsMetadata>(MD), Record);
2400 }
2401}
2402
2403void ModuleBitcodeWriter::writeModuleMetadata() {
2404 if (!VE.hasMDs() && M.named_metadata_empty())
2405 return;
2406
2409
2410 // Emit all abbrevs upfront, so that the reader can jump in the middle of the
2411 // block and load any metadata.
2412 std::vector<unsigned> MDAbbrevs;
2413
2414 MDAbbrevs.resize(MetadataAbbrev::LastPlusOne);
2415 MDAbbrevs[MetadataAbbrev::DILocationAbbrevID] = createDILocationAbbrev();
2416 MDAbbrevs[MetadataAbbrev::GenericDINodeAbbrevID] =
2417 createGenericDINodeAbbrev();
2418
2419 auto Abbv = std::make_shared<BitCodeAbbrev>();
2423 unsigned OffsetAbbrev = Stream.EmitAbbrev(std::move(Abbv));
2424
2425 Abbv = std::make_shared<BitCodeAbbrev>();
2429 unsigned IndexAbbrev = Stream.EmitAbbrev(std::move(Abbv));
2430
2431 // Emit MDStrings together upfront.
2432 writeMetadataStrings(VE.getMDStrings(), Record);
2433
2434 // We only emit an index for the metadata record if we have more than a given
2435 // (naive) threshold of metadatas, otherwise it is not worth it.
2436 if (VE.getNonMDStrings().size() > IndexThreshold) {
2437 // Write a placeholder value in for the offset of the metadata index,
2438 // which is written after the records, so that it can include
2439 // the offset of each entry. The placeholder offset will be
2440 // updated after all records are emitted.
2441 uint64_t Vals[] = {0, 0};
2442 Stream.EmitRecord(bitc::METADATA_INDEX_OFFSET, Vals, OffsetAbbrev);
2443 }
2444
2445 // Compute and save the bit offset to the current position, which will be
2446 // patched when we emit the index later. We can simply subtract the 64-bit
2447 // fixed size from the current bit number to get the location to backpatch.
2448 uint64_t IndexOffsetRecordBitPos = Stream.GetCurrentBitNo();
2449
2450 // This index will contain the bitpos for each individual record.
2451 std::vector<uint64_t> IndexPos;
2452 IndexPos.reserve(VE.getNonMDStrings().size());
2453
2454 // Write all the records
2455 writeMetadataRecords(VE.getNonMDStrings(), Record, &MDAbbrevs, &IndexPos);
2456
2457 if (VE.getNonMDStrings().size() > IndexThreshold) {
2458 // Now that we have emitted all the records we will emit the index. But
2459 // first
2460 // backpatch the forward reference so that the reader can skip the records
2461 // efficiently.
2462 Stream.BackpatchWord64(IndexOffsetRecordBitPos - 64,
2463 Stream.GetCurrentBitNo() - IndexOffsetRecordBitPos);
2464
2465 // Delta encode the index.
2466 uint64_t PreviousValue = IndexOffsetRecordBitPos;
2467 for (auto &Elt : IndexPos) {
2468 auto EltDelta = Elt - PreviousValue;
2469 PreviousValue = Elt;
2470 Elt = EltDelta;
2471 }
2472 // Emit the index record.
2473 Stream.EmitRecord(bitc::METADATA_INDEX, IndexPos, IndexAbbrev);
2474 IndexPos.clear();
2475 }
2476
2477 // Write the named metadata now.
2478 writeNamedMetadata(Record);
2479
2480 auto AddDeclAttachedMetadata = [&](const GlobalObject &GO) {
2482 Record.push_back(VE.getValueID(&GO));
2483 pushGlobalMetadataAttachment(Record, GO);
2485 };
2486 for (const Function &F : M)
2487 if (F.isDeclaration() && F.hasMetadata())
2488 AddDeclAttachedMetadata(F);
2489 // FIXME: Only store metadata for declarations here, and move data for global
2490 // variable definitions to a separate block (PR28134).
2491 for (const GlobalVariable &GV : M.globals())
2492 if (GV.hasMetadata())
2493 AddDeclAttachedMetadata(GV);
2494
2495 Stream.ExitBlock();
2496}
2497
2498void ModuleBitcodeWriter::writeFunctionMetadata(const Function &F) {
2499 if (!VE.hasMDs())
2500 return;
2501
2504 writeMetadataStrings(VE.getMDStrings(), Record);
2505 writeMetadataRecords(VE.getNonMDStrings(), Record);
2506 Stream.ExitBlock();
2507}
2508
2509void ModuleBitcodeWriter::pushGlobalMetadataAttachment(
2511 // [n x [id, mdnode]]
2513 GO.getAllMetadata(MDs);
2514 for (const auto &I : MDs) {
2515 Record.push_back(I.first);
2516 Record.push_back(VE.getMetadataID(I.second));
2517 }
2518}
2519
2520void ModuleBitcodeWriter::writeFunctionMetadataAttachment(const Function &F) {
2522
2524
2525 if (F.hasMetadata()) {
2526 pushGlobalMetadataAttachment(Record, F);
2528 Record.clear();
2529 }
2530
2531 // Write metadata attachments
2532 // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]]
2534 for (const BasicBlock &BB : F)
2535 for (const Instruction &I : BB) {
2536 MDs.clear();
2537 I.getAllMetadataOtherThanDebugLoc(MDs);
2538
2539 // If no metadata, ignore instruction.
2540 if (MDs.empty()) continue;
2541
2542 Record.push_back(VE.getInstructionID(&I));
2543
2544 for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
2545 Record.push_back(MDs[i].first);
2546 Record.push_back(VE.getMetadataID(MDs[i].second));
2547 }
2549 Record.clear();
2550 }
2551
2552 Stream.ExitBlock();
2553}
2554
2555void ModuleBitcodeWriter::writeModuleMetadataKinds() {
2557
2558 // Write metadata kinds
2559 // METADATA_KIND - [n x [id, name]]
2561 M.getMDKindNames(Names);
2562
2563 if (Names.empty()) return;
2564
2566
2567 for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) {
2568 Record.push_back(MDKindID);
2569 StringRef KName = Names[MDKindID];
2570 Record.append(KName.begin(), KName.end());
2571
2573 Record.clear();
2574 }
2575
2576 Stream.ExitBlock();
2577}
2578
2579void ModuleBitcodeWriter::writeOperandBundleTags() {
2580 // Write metadata kinds
2581 //
2582 // OPERAND_BUNDLE_TAGS_BLOCK_ID : N x OPERAND_BUNDLE_TAG
2583 //
2584 // OPERAND_BUNDLE_TAG - [strchr x N]
2585
2587 M.getOperandBundleTags(Tags);
2588
2589 if (Tags.empty())
2590 return;
2591
2593
2595
2596 for (auto Tag : Tags) {
2597 Record.append(Tag.begin(), Tag.end());
2598
2600 Record.clear();
2601 }
2602
2603 Stream.ExitBlock();
2604}
2605
2606void ModuleBitcodeWriter::writeSyncScopeNames() {
2608 M.getContext().getSyncScopeNames(SSNs);
2609 if (SSNs.empty())
2610 return;
2611
2613
2615 for (auto SSN : SSNs) {
2616 Record.append(SSN.begin(), SSN.end());
2618 Record.clear();
2619 }
2620
2621 Stream.ExitBlock();
2622}
2623
2624void ModuleBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal,
2625 bool isGlobal) {
2626 if (FirstVal == LastVal) return;
2627
2629
2630 unsigned AggregateAbbrev = 0;
2631 unsigned String8Abbrev = 0;
2632 unsigned CString7Abbrev = 0;
2633 unsigned CString6Abbrev = 0;
2634 // If this is a constant pool for the module, emit module-specific abbrevs.
2635 if (isGlobal) {
2636 // Abbrev for CST_CODE_AGGREGATE.
2637 auto Abbv = std::make_shared<BitCodeAbbrev>();
2640 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
2641 AggregateAbbrev = Stream.EmitAbbrev(std::move(Abbv));
2642
2643 // Abbrev for CST_CODE_STRING.
2644 Abbv = std::make_shared<BitCodeAbbrev>();
2648 String8Abbrev = Stream.EmitAbbrev(std::move(Abbv));
2649 // Abbrev for CST_CODE_CSTRING.
2650 Abbv = std::make_shared<BitCodeAbbrev>();
2654 CString7Abbrev = Stream.EmitAbbrev(std::move(Abbv));
2655 // Abbrev for CST_CODE_CSTRING.
2656 Abbv = std::make_shared<BitCodeAbbrev>();
2660 CString6Abbrev = Stream.EmitAbbrev(std::move(Abbv));
2661 }
2662
2664
2665 const ValueEnumerator::ValueList &Vals = VE.getValues();
2666 Type *LastTy = nullptr;
2667 for (unsigned i = FirstVal; i != LastVal; ++i) {
2668 const Value *V = Vals[i].first;
2669 // If we need to switch types, do so now.
2670 if (V->getType() != LastTy) {
2671 LastTy = V->getType();
2672 Record.push_back(VE.getTypeID(LastTy));
2674 CONSTANTS_SETTYPE_ABBREV);
2675 Record.clear();
2676 }
2677
2678 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
2679 Record.push_back(VE.getTypeID(IA->getFunctionType()));
2680 Record.push_back(
2681 unsigned(IA->hasSideEffects()) | unsigned(IA->isAlignStack()) << 1 |
2682 unsigned(IA->getDialect() & 1) << 2 | unsigned(IA->canThrow()) << 3);
2683
2684 // Add the asm string.
2685 const std::string &AsmStr = IA->getAsmString();
2686 Record.push_back(AsmStr.size());
2687 Record.append(AsmStr.begin(), AsmStr.end());
2688
2689 // Add the constraint string.
2690 const std::string &ConstraintStr = IA->getConstraintString();
2691 Record.push_back(ConstraintStr.size());
2692 Record.append(ConstraintStr.begin(), ConstraintStr.end());
2694 Record.clear();
2695 continue;
2696 }
2697 const Constant *C = cast<Constant>(V);
2698 unsigned Code = -1U;
2699 unsigned AbbrevToUse = 0;
2700 if (C->isNullValue()) {
2702 } else if (isa<PoisonValue>(C)) {
2704 } else if (isa<UndefValue>(C)) {
2706 } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
2707 if (IV->getBitWidth() <= 64) {
2708 uint64_t V = IV->getSExtValue();
2711 AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
2712 } else { // Wide integers, > 64 bits in size.
2713 emitWideAPInt(Record, IV->getValue());
2715 }
2716 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
2718 Type *Ty = CFP->getType()->getScalarType();
2719 if (Ty->isHalfTy() || Ty->isBFloatTy() || Ty->isFloatTy() ||
2720 Ty->isDoubleTy()) {
2721 Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
2722 } else if (Ty->isX86_FP80Ty()) {
2723 // api needed to prevent premature destruction
2724 // bits are not in the same order as a normal i80 APInt, compensate.
2725 APInt api = CFP->getValueAPF().bitcastToAPInt();
2726 const uint64_t *p = api.getRawData();
2727 Record.push_back((p[1] << 48) | (p[0] >> 16));
2728 Record.push_back(p[0] & 0xffffLL);
2729 } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) {
2730 APInt api = CFP->getValueAPF().bitcastToAPInt();
2731 const uint64_t *p = api.getRawData();
2732 Record.push_back(p[0]);
2733 Record.push_back(p[1]);
2734 } else {
2735 assert(0 && "Unknown FP type!");
2736 }
2737 } else if (isa<ConstantDataSequential>(C) &&
2738 cast<ConstantDataSequential>(C)->isString()) {
2739 const ConstantDataSequential *Str = cast<ConstantDataSequential>(C);
2740 // Emit constant strings specially.
2741 unsigned NumElts = Str->getNumElements();
2742 // If this is a null-terminated string, use the denser CSTRING encoding.
2743 if (Str->isCString()) {
2745 --NumElts; // Don't encode the null, which isn't allowed by char6.
2746 } else {
2748 AbbrevToUse = String8Abbrev;
2749 }
2750 bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
2751 bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
2752 for (unsigned i = 0; i != NumElts; ++i) {
2753 unsigned char V = Str->getElementAsInteger(i);
2754 Record.push_back(V);
2755 isCStr7 &= (V & 128) == 0;
2756 if (isCStrChar6)
2757 isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
2758 }
2759
2760 if (isCStrChar6)
2761 AbbrevToUse = CString6Abbrev;
2762 else if (isCStr7)
2763 AbbrevToUse = CString7Abbrev;
2764 } else if (const ConstantDataSequential *CDS =
2765 dyn_cast<ConstantDataSequential>(C)) {
2767 Type *EltTy = CDS->getElementType();
2768 if (isa<IntegerType>(EltTy)) {
2769 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
2770 Record.push_back(CDS->getElementAsInteger(i));
2771 } else {
2772 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
2773 Record.push_back(
2774 CDS->getElementAsAPFloat(i).bitcastToAPInt().getLimitedValue());
2775 }
2776 } else if (isa<ConstantAggregate>(C)) {
2778 for (const Value *Op : C->operands())
2779 Record.push_back(VE.getValueID(Op));
2780 AbbrevToUse = AggregateAbbrev;
2781 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
2782 switch (CE->getOpcode()) {
2783 default:
2784 if (Instruction::isCast(CE->getOpcode())) {
2786 Record.push_back(getEncodedCastOpcode(CE->getOpcode()));
2787 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
2788 Record.push_back(VE.getValueID(C->getOperand(0)));
2789 AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
2790 } else {
2791 assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
2793 Record.push_back(getEncodedBinaryOpcode(CE->getOpcode()));
2794 Record.push_back(VE.getValueID(C->getOperand(0)));
2795 Record.push_back(VE.getValueID(C->getOperand(1)));
2797 if (Flags != 0)
2798 Record.push_back(Flags);
2799 }
2800 break;
2801 case Instruction::FNeg: {
2802 assert(CE->getNumOperands() == 1 && "Unknown constant expr!");
2804 Record.push_back(getEncodedUnaryOpcode(CE->getOpcode()));
2805 Record.push_back(VE.getValueID(C->getOperand(0)));
2807 if (Flags != 0)
2808 Record.push_back(Flags);
2809 break;
2810 }
2811 case Instruction::GetElementPtr: {
2813 const auto *GO = cast<GEPOperator>(C);
2814 Record.push_back(VE.getTypeID(GO->getSourceElementType()));
2815 Record.push_back(getOptimizationFlags(GO));
2816 if (std::optional<ConstantRange> Range = GO->getInRange()) {
2818 emitConstantRange(Record, *Range, /*EmitBitWidth=*/true);
2819 }
2820 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
2821 Record.push_back(VE.getTypeID(C->getOperand(i)->getType()));
2822 Record.push_back(VE.getValueID(C->getOperand(i)));
2823 }
2824 break;
2825 }
2826 case Instruction::ExtractElement:
2828 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
2829 Record.push_back(VE.getValueID(C->getOperand(0)));
2830 Record.push_back(VE.getTypeID(C->getOperand(1)->getType()));
2831 Record.push_back(VE.getValueID(C->getOperand(1)));
2832 break;
2833 case Instruction::InsertElement:
2835 Record.push_back(VE.getValueID(C->getOperand(0)));
2836 Record.push_back(VE.getValueID(C->getOperand(1)));
2837 Record.push_back(VE.getTypeID(C->getOperand(2)->getType()));
2838 Record.push_back(VE.getValueID(C->getOperand(2)));
2839 break;
2840 case Instruction::ShuffleVector:
2841 // If the return type and argument types are the same, this is a
2842 // standard shufflevector instruction. If the types are different,
2843 // then the shuffle is widening or truncating the input vectors, and
2844 // the argument type must also be encoded.
2845 if (C->getType() == C->getOperand(0)->getType()) {
2847 } else {
2849 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
2850 }
2851 Record.push_back(VE.getValueID(C->getOperand(0)));
2852 Record.push_back(VE.getValueID(C->getOperand(1)));
2853 Record.push_back(VE.getValueID(CE->getShuffleMaskForBitcode()));
2854 break;
2855 }
2856 } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
2858 Record.push_back(VE.getTypeID(BA->getFunction()->getType()));
2859 Record.push_back(VE.getValueID(BA->getFunction()));
2860 Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock()));
2861 } else if (const auto *Equiv = dyn_cast<DSOLocalEquivalent>(C)) {
2863 Record.push_back(VE.getTypeID(Equiv->getGlobalValue()->getType()));
2864 Record.push_back(VE.getValueID(Equiv->getGlobalValue()));
2865 } else if (const auto *NC = dyn_cast<NoCFIValue>(C)) {
2867 Record.push_back(VE.getTypeID(NC->getGlobalValue()->getType()));
2868 Record.push_back(VE.getValueID(NC->getGlobalValue()));
2869 } else if (const auto *CPA = dyn_cast<ConstantPtrAuth>(C)) {
2871 Record.push_back(VE.getValueID(CPA->getPointer()));
2872 Record.push_back(VE.getValueID(CPA->getKey()));
2873 Record.push_back(VE.getValueID(CPA->getDiscriminator()));
2874 Record.push_back(VE.getValueID(CPA->getAddrDiscriminator()));
2875 } else {
2876#ifndef NDEBUG
2877 C->dump();
2878#endif
2879 llvm_unreachable("Unknown constant!");
2880 }
2881 Stream.EmitRecord(Code, Record, AbbrevToUse);
2882 Record.clear();
2883 }
2884
2885 Stream.ExitBlock();
2886}
2887
2888void ModuleBitcodeWriter::writeModuleConstants() {
2889 const ValueEnumerator::ValueList &Vals = VE.getValues();
2890
2891 // Find the first constant to emit, which is the first non-globalvalue value.
2892 // We know globalvalues have been emitted by WriteModuleInfo.
2893 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
2894 if (!isa<GlobalValue>(Vals[i].first)) {
2895 writeConstants(i, Vals.size(), true);
2896 return;
2897 }
2898 }
2899}
2900
2901/// pushValueAndType - The file has to encode both the value and type id for
2902/// many values, because we need to know what type to create for forward
2903/// references. However, most operands are not forward references, so this type
2904/// field is not needed.
2905///
2906/// This function adds V's value ID to Vals. If the value ID is higher than the
2907/// instruction ID, then it is a forward reference, and it also includes the
2908/// type ID. The value ID that is written is encoded relative to the InstID.
2909bool ModuleBitcodeWriter::pushValueAndType(const Value *V, unsigned InstID,
2911 unsigned ValID = VE.getValueID(V);
2912 // Make encoding relative to the InstID.
2913 Vals.push_back(InstID - ValID);
2914 if (ValID >= InstID) {
2915 Vals.push_back(VE.getTypeID(V->getType()));
2916 return true;
2917 }
2918 return false;
2919}
2920
2921void ModuleBitcodeWriter::writeOperandBundles(const CallBase &CS,
2922 unsigned InstID) {
2924 LLVMContext &C = CS.getContext();
2925
2926 for (unsigned i = 0, e = CS.getNumOperandBundles(); i != e; ++i) {
2927 const auto &Bundle = CS.getOperandBundleAt(i);
2928 Record.push_back(C.getOperandBundleTagID(Bundle.getTagName()));
2929
2930 for (auto &Input : Bundle.Inputs)
2931 pushValueAndType(Input, InstID, Record);
2932
2934 Record.clear();
2935 }
2936}
2937
2938/// pushValue - Like pushValueAndType, but where the type of the value is
2939/// omitted (perhaps it was already encoded in an earlier operand).
2940void ModuleBitcodeWriter::pushValue(const Value *V, unsigned InstID,
2942 unsigned ValID = VE.getValueID(V);
2943 Vals.push_back(InstID - ValID);
2944}
2945
2946void ModuleBitcodeWriter::pushValueSigned(const Value *V, unsigned InstID,
2948 unsigned ValID = VE.getValueID(V);
2949 int64_t diff = ((int32_t)InstID - (int32_t)ValID);
2950 emitSignedInt64(Vals, diff);
2951}
2952
2953/// WriteInstruction - Emit an instruction to the specified stream.
2954void ModuleBitcodeWriter::writeInstruction(const Instruction &I,
2955 unsigned InstID,
2957 unsigned Code = 0;
2958 unsigned AbbrevToUse = 0;
2959 VE.setInstructionID(&I);
2960 switch (I.getOpcode()) {
2961 default:
2962 if (Instruction::isCast(I.getOpcode())) {
2964 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
2965 AbbrevToUse = FUNCTION_INST_CAST_ABBREV;
2966 Vals.push_back(VE.getTypeID(I.getType()));
2967 Vals.push_back(getEncodedCastOpcode(I.getOpcode()));
2969 if (Flags != 0) {
2970 if (AbbrevToUse == FUNCTION_INST_CAST_ABBREV)
2971 AbbrevToUse = FUNCTION_INST_CAST_FLAGS_ABBREV;
2972 Vals.push_back(Flags);
2973 }
2974 } else {
2975 assert(isa<BinaryOperator>(I) && "Unknown instruction!");
2977 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
2978 AbbrevToUse = FUNCTION_INST_BINOP_ABBREV;
2979 pushValue(I.getOperand(1), InstID, Vals);
2980 Vals.push_back(getEncodedBinaryOpcode(I.getOpcode()));
2982 if (Flags != 0) {
2983 if (AbbrevToUse == FUNCTION_INST_BINOP_ABBREV)
2984 AbbrevToUse = FUNCTION_INST_BINOP_FLAGS_ABBREV;
2985 Vals.push_back(Flags);
2986 }
2987 }
2988 break;
2989 case Instruction::FNeg: {
2991 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
2992 AbbrevToUse = FUNCTION_INST_UNOP_ABBREV;
2993 Vals.push_back(getEncodedUnaryOpcode(I.getOpcode()));
2995 if (Flags != 0) {
2996 if (AbbrevToUse == FUNCTION_INST_UNOP_ABBREV)
2997 AbbrevToUse = FUNCTION_INST_UNOP_FLAGS_ABBREV;
2998 Vals.push_back(Flags);
2999 }
3000 break;
3001 }
3002 case Instruction::GetElementPtr: {
3004 AbbrevToUse = FUNCTION_INST_GEP_ABBREV;
3005 auto &GEPInst = cast<GetElementPtrInst>(I);
3007 Vals.push_back(VE.getTypeID(GEPInst.getSourceElementType()));
3008 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
3009 pushValueAndType(I.getOperand(i), InstID, Vals);
3010 break;
3011 }
3012 case Instruction::ExtractValue: {
3014 pushValueAndType(I.getOperand(0), InstID, Vals);
3015 const ExtractValueInst *EVI = cast<ExtractValueInst>(&I);
3016 Vals.append(EVI->idx_begin(), EVI->idx_end());
3017 break;
3018 }
3019 case Instruction::InsertValue: {
3021 pushValueAndType(I.getOperand(0), InstID, Vals);
3022 pushValueAndType(I.getOperand(1), InstID, Vals);
3023 const InsertValueInst *IVI = cast<InsertValueInst>(&I);
3024 Vals.append(IVI->idx_begin(), IVI->idx_end());
3025 break;
3026 }
3027 case Instruction::Select: {
3029 pushValueAndType(I.getOperand(1), InstID, Vals);
3030 pushValue(I.getOperand(2), InstID, Vals);
3031 pushValueAndType(I.getOperand(0), InstID, Vals);
3033 if (Flags != 0)
3034 Vals.push_back(Flags);
3035 break;
3036 }
3037 case Instruction::ExtractElement:
3039 pushValueAndType(I.getOperand(0), InstID, Vals);
3040 pushValueAndType(I.getOperand(1), InstID, Vals);
3041 break;
3042 case Instruction::InsertElement:
3044 pushValueAndType(I.getOperand(0), InstID, Vals);
3045 pushValue(I.getOperand(1), InstID, Vals);
3046 pushValueAndType(I.getOperand(2), InstID, Vals);
3047 break;
3048 case Instruction::ShuffleVector:
3050 pushValueAndType(I.getOperand(0), InstID, Vals);
3051 pushValue(I.getOperand(1), InstID, Vals);
3052 pushValue(cast<ShuffleVectorInst>(I).getShuffleMaskForBitcode(), InstID,
3053 Vals);
3054 break;
3055 case Instruction::ICmp:
3056 case Instruction::FCmp: {
3057 // compare returning Int1Ty or vector of Int1Ty
3059 pushValueAndType(I.getOperand(0), InstID, Vals);
3060 pushValue(I.getOperand(1), InstID, Vals);
3061 Vals.push_back(cast<CmpInst>(I).getPredicate());
3063 if (Flags != 0)
3064 Vals.push_back(Flags);
3065 break;
3066 }
3067
3068 case Instruction::Ret:
3069 {
3071 unsigned NumOperands = I.getNumOperands();
3072 if (NumOperands == 0)
3073 AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV;
3074 else if (NumOperands == 1) {
3075 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
3076 AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV;
3077 } else {
3078 for (unsigned i = 0, e = NumOperands; i != e; ++i)
3079 pushValueAndType(I.getOperand(i), InstID, Vals);
3080 }
3081 }
3082 break;
3083 case Instruction::Br:
3084 {
3086 const BranchInst &II = cast<BranchInst>(I);
3087 Vals.push_back(VE.getValueID(II.getSuccessor(0)));
3088 if (II.isConditional()) {
3089 Vals.push_back(VE.getValueID(II.getSuccessor(1)));
3090 pushValue(II.getCondition(), InstID, Vals);
3091 }
3092 }
3093 break;
3094 case Instruction::Switch:
3095 {
3097 const SwitchInst &SI = cast<SwitchInst>(I);
3098 Vals.push_back(VE.getTypeID(SI.getCondition()->getType()));
3099 pushValue(SI.getCondition(), InstID, Vals);
3100 Vals.push_back(VE.getValueID(SI.getDefaultDest()));
3101 for (auto Case : SI.cases()) {
3102 Vals.push_back(VE.getValueID(Case.getCaseValue()));
3103 Vals.push_back(VE.getValueID(Case.getCaseSuccessor()));
3104 }
3105 }
3106 break;
3107 case Instruction::IndirectBr:
3109 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
3110 // Encode the address operand as relative, but not the basic blocks.
3111 pushValue(I.getOperand(0), InstID, Vals);
3112 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i)
3113 Vals.push_back(VE.getValueID(I.getOperand(i)));
3114 break;
3115
3116 case Instruction::Invoke: {
3117 const InvokeInst *II = cast<InvokeInst>(&I);
3118 const Value *Callee = II->getCalledOperand();
3119 FunctionType *FTy = II->getFunctionType();
3120
3121 if (II->hasOperandBundles())
3122 writeOperandBundles(*II, InstID);
3123
3125
3126 Vals.push_back(VE.getAttributeListID(II->getAttributes()));
3127 Vals.push_back(II->getCallingConv() | 1 << 13);
3128 Vals.push_back(VE.getValueID(II->getNormalDest()));
3129 Vals.push_back(VE.getValueID(II->getUnwindDest()));
3130 Vals.push_back(VE.getTypeID(FTy));
3131 pushValueAndType(Callee, InstID, Vals);
3132
3133 // Emit value #'s for the fixed parameters.
3134 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
3135 pushValue(I.getOperand(i), InstID, Vals); // fixed param.
3136
3137 // Emit type/value pairs for varargs params.
3138 if (FTy->isVarArg()) {
3139 for (unsigned i = FTy->getNumParams(), e = II->arg_size(); i != e; ++i)
3140 pushValueAndType(I.getOperand(i), InstID, Vals); // vararg
3141 }
3142 break;
3143 }
3144 case Instruction::Resume:
3146 pushValueAndType(I.getOperand(0), InstID, Vals);
3147 break;
3148 case Instruction::CleanupRet: {
3150 const auto &CRI = cast<CleanupReturnInst>(I);
3151 pushValue(CRI.getCleanupPad(), InstID, Vals);
3152 if (CRI.hasUnwindDest())
3153 Vals.push_back(VE.getValueID(CRI.getUnwindDest()));
3154 break;
3155 }
3156 case Instruction::CatchRet: {
3158 const auto &CRI = cast<CatchReturnInst>(I);
3159 pushValue(CRI.getCatchPad(), InstID, Vals);
3160 Vals.push_back(VE.getValueID(CRI.getSuccessor()));
3161 break;
3162 }
3163 case Instruction::CleanupPad:
3164 case Instruction::CatchPad: {
3165 const auto &FuncletPad = cast<FuncletPadInst>(I);
3166 Code = isa<CatchPadInst>(FuncletPad) ? bitc::FUNC_CODE_INST_CATCHPAD
3168 pushValue(FuncletPad.getParentPad(), InstID, Vals);
3169
3170 unsigned NumArgOperands = FuncletPad.arg_size();
3171 Vals.push_back(NumArgOperands);
3172 for (unsigned Op = 0; Op != NumArgOperands; ++Op)
3173 pushValueAndType(FuncletPad.getArgOperand(Op), InstID, Vals);
3174 break;
3175 }
3176 case Instruction::CatchSwitch: {
3178 const auto &CatchSwitch = cast<CatchSwitchInst>(I);
3179
3180 pushValue(CatchSwitch.getParentPad(), InstID, Vals);
3181
3182 unsigned NumHandlers = CatchSwitch.getNumHandlers();
3183 Vals.push_back(NumHandlers);
3184 for (const BasicBlock *CatchPadBB : CatchSwitch.handlers())
3185 Vals.push_back(VE.getValueID(CatchPadBB));
3186
3187 if (CatchSwitch.hasUnwindDest())
3188 Vals.push_back(VE.getValueID(CatchSwitch.getUnwindDest()));
3189 break;
3190 }
3191 case Instruction::CallBr: {
3192 const CallBrInst *CBI = cast<CallBrInst>(&I);
3193 const Value *Callee = CBI->getCalledOperand();
3194 FunctionType *FTy = CBI->getFunctionType();
3195
3196 if (CBI->hasOperandBundles())
3197 writeOperandBundles(*CBI, InstID);
3198
3200
3202
3205
3206 Vals.push_back(VE.getValueID(CBI->getDefaultDest()));
3207 Vals.push_back(CBI->getNumIndirectDests());
3208 for (unsigned i = 0, e = CBI->getNumIndirectDests(); i != e; ++i)
3209 Vals.push_back(VE.getValueID(CBI->getIndirectDest(i)));
3210
3211 Vals.push_back(VE.getTypeID(FTy));
3212 pushValueAndType(Callee, InstID, Vals);
3213
3214 // Emit value #'s for the fixed parameters.
3215 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
3216 pushValue(I.getOperand(i), InstID, Vals); // fixed param.
3217
3218 // Emit type/value pairs for varargs params.
3219 if (FTy->isVarArg()) {
3220 for (unsigned i = FTy->getNumParams(), e = CBI->arg_size(); i != e; ++i)
3221 pushValueAndType(I.getOperand(i), InstID, Vals); // vararg
3222 }
3223 break;
3224 }
3225 case Instruction::Unreachable:
3227 AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV;
3228 break;
3229
3230 case Instruction::PHI: {
3231 const PHINode &PN = cast<PHINode>(I);
3233 // With the newer instruction encoding, forward references could give
3234 // negative valued IDs. This is most common for PHIs, so we use
3235 // signed VBRs.
3237 Vals64.push_back(VE.getTypeID(PN.getType()));
3238 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
3239 pushValueSigned(PN.getIncomingValue(i), InstID, Vals64);
3240 Vals64.push_back(VE.getValueID(PN.getIncomingBlock(i)));
3241 }
3242
3244 if (Flags != 0)
3245 Vals64.push_back(Flags);
3246
3247 // Emit a Vals64 vector and exit.
3248 Stream.EmitRecord(Code, Vals64, AbbrevToUse);
3249 Vals64.clear();
3250 return;
3251 }
3252
3253 case Instruction::LandingPad: {
3254 const LandingPadInst &LP = cast<LandingPadInst>(I);
3256 Vals.push_back(VE.getTypeID(LP.getType()));
3257 Vals.push_back(LP.isCleanup());
3258 Vals.push_back(LP.getNumClauses());
3259 for (unsigned I = 0, E = LP.getNumClauses(); I != E; ++I) {
3260 if (LP.isCatch(I))
3262 else
3264 pushValueAndType(LP.getClause(I), InstID, Vals);
3265 }
3266 break;
3267 }
3268
3269 case Instruction::Alloca: {
3271 const AllocaInst &AI = cast<AllocaInst>(I);
3272 Vals.push_back(VE.getTypeID(AI.getAllocatedType()));
3273 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
3274 Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
3275 using APV = AllocaPackedValues;
3276 unsigned Record = 0;
3277 unsigned EncodedAlign = getEncodedAlign(AI.getAlign());
3278 Bitfield::set<APV::AlignLower>(
3279 Record, EncodedAlign & ((1 << APV::AlignLower::Bits) - 1));
3280 Bitfield::set<APV::AlignUpper>(Record,
3281 EncodedAlign >> APV::AlignLower::Bits);
3282 Bitfield::set<APV::UsedWithInAlloca>(Record, AI.isUsedWithInAlloca());
3283 Bitfield::set<APV::ExplicitType>(Record, true);
3284 Bitfield::set<APV::SwiftError>(Record, AI.isSwiftError());
3285 Vals.push_back(Record);
3286
3287 unsigned AS = AI.getAddressSpace();
3288 if (AS != M.getDataLayout().getAllocaAddrSpace())
3289 Vals.push_back(AS);
3290 break;
3291 }
3292
3293 case Instruction::Load:
3294 if (cast<LoadInst>(I).isAtomic()) {
3296 pushValueAndType(I.getOperand(0), InstID, Vals);
3297 } else {
3299 if (!pushValueAndType(I.getOperand(0), InstID, Vals)) // ptr
3300 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV;
3301 }
3302 Vals.push_back(VE.getTypeID(I.getType()));
3303 Vals.push_back(getEncodedAlign(cast<LoadInst>(I).getAlign()));
3304 Vals.push_back(cast<LoadInst>(I).isVolatile());
3305 if (cast<LoadInst>(I).isAtomic()) {
3306 Vals.push_back(getEncodedOrdering(cast<LoadInst>(I).getOrdering()));
3307 Vals.push_back(getEncodedSyncScopeID(cast<LoadInst>(I).getSyncScopeID()));
3308 }
3309 break;
3310 case Instruction::Store:
3311 if (cast<StoreInst>(I).isAtomic())
3313 else
3315 pushValueAndType(I.getOperand(1), InstID, Vals); // ptrty + ptr
3316 pushValueAndType(I.getOperand(0), InstID, Vals); // valty + val
3317 Vals.push_back(getEncodedAlign(cast<StoreInst>(I).getAlign()));
3318 Vals.push_back(cast<StoreInst>(I).isVolatile());
3319 if (cast<StoreInst>(I).isAtomic()) {
3320 Vals.push_back(getEncodedOrdering(cast<StoreInst>(I).getOrdering()));
3321 Vals.push_back(
3322 getEncodedSyncScopeID(cast<StoreInst>(I).getSyncScopeID()));
3323 }
3324 break;
3325 case Instruction::AtomicCmpXchg:
3327 pushValueAndType(I.getOperand(0), InstID, Vals); // ptrty + ptr
3328 pushValueAndType(I.getOperand(1), InstID, Vals); // cmp.
3329 pushValue(I.getOperand(2), InstID, Vals); // newval.
3330 Vals.push_back(cast<AtomicCmpXchgInst>(I).isVolatile());
3331 Vals.push_back(
3332 getEncodedOrdering(cast<AtomicCmpXchgInst>(I).getSuccessOrdering()));
3333 Vals.push_back(
3334 getEncodedSyncScopeID(cast<AtomicCmpXchgInst>(I).getSyncScopeID()));
3335 Vals.push_back(
3336 getEncodedOrdering(cast<AtomicCmpXchgInst>(I).getFailureOrdering()));
3337 Vals.push_back(cast<AtomicCmpXchgInst>(I).isWeak());
3338 Vals.push_back(getEncodedAlign(cast<AtomicCmpXchgInst>(I).getAlign()));
3339 break;
3340 case Instruction::AtomicRMW:
3342 pushValueAndType(I.getOperand(0), InstID, Vals); // ptrty + ptr
3343 pushValueAndType(I.getOperand(1), InstID, Vals); // valty + val
3344 Vals.push_back(
3345 getEncodedRMWOperation(cast<AtomicRMWInst>(I).getOperation()));
3346 Vals.push_back(cast<AtomicRMWInst>(I).isVolatile());
3347 Vals.push_back(getEncodedOrdering(cast<AtomicRMWInst>(I).getOrdering()));
3348 Vals.push_back(
3349 getEncodedSyncScopeID(cast<AtomicRMWInst>(I).getSyncScopeID()));
3350 Vals.push_back(getEncodedAlign(cast<AtomicRMWInst>(I).getAlign()));
3351 break;
3352 case Instruction::Fence:
3354 Vals.push_back(getEncodedOrdering(cast<FenceInst>(I).getOrdering()));
3355 Vals.push_back(getEncodedSyncScopeID(cast<FenceInst>(I).getSyncScopeID()));
3356 break;
3357 case Instruction::Call: {
3358 const CallInst &CI = cast<CallInst>(I);
3359 FunctionType *FTy = CI.getFunctionType();
3360
3361 if (CI.hasOperandBundles())
3362 writeOperandBundles(CI, InstID);
3363
3365
3367
3368 unsigned Flags = getOptimizationFlags(&I);
3370 unsigned(CI.isTailCall()) << bitc::CALL_TAIL |
3371 unsigned(CI.isMustTailCall()) << bitc::CALL_MUSTTAIL |
3373 unsigned(CI.isNoTailCall()) << bitc::CALL_NOTAIL |
3374 unsigned(Flags != 0) << bitc::CALL_FMF);
3375 if (Flags != 0)
3376 Vals.push_back(Flags);
3377
3378 Vals.push_back(VE.getTypeID(FTy));
3379 pushValueAndType(CI.getCalledOperand(), InstID, Vals); // Callee
3380
3381 // Emit value #'s for the fixed parameters.
3382 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
3383 // Check for labels (can happen with asm labels).
3384 if (FTy->getParamType(i)->isLabelTy())
3385 Vals.push_back(VE.getValueID(CI.getArgOperand(i)));
3386 else
3387 pushValue(CI.getArgOperand(i), InstID, Vals); // fixed param.
3388 }
3389
3390 // Emit type/value pairs for varargs params.
3391 if (FTy->isVarArg()) {
3392 for (unsigned i = FTy->getNumParams(), e = CI.arg_size(); i != e; ++i)
3393 pushValueAndType(CI.getArgOperand(i), InstID, Vals); // varargs
3394 }
3395 break;
3396 }
3397 case Instruction::VAArg:
3399 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty
3400 pushValue(I.getOperand(0), InstID, Vals); // valist.
3401 Vals.push_back(VE.getTypeID(I.getType())); // restype.
3402 break;
3403 case Instruction::Freeze:
3405 pushValueAndType(I.getOperand(0), InstID, Vals);
3406 break;
3407 }
3408
3409 Stream.EmitRecord(Code, Vals, AbbrevToUse);
3410 Vals.clear();
3411}
3412
3413/// Write a GlobalValue VST to the module. The purpose of this data structure is
3414/// to allow clients to efficiently find the function body.
3415void ModuleBitcodeWriter::writeGlobalValueSymbolTable(
3416 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex) {
3417 // Get the offset of the VST we are writing, and backpatch it into
3418 // the VST forward declaration record.
3419 uint64_t VSTOffset = Stream.GetCurrentBitNo();
3420 // The BitcodeStartBit was the stream offset of the identification block.
3421 VSTOffset -= bitcodeStartBit();
3422 assert((VSTOffset & 31) == 0 && "VST block not 32-bit aligned");
3423 // Note that we add 1 here because the offset is relative to one word
3424 // before the start of the identification block, which was historically
3425 // always the start of the regular bitcode header.
3426 Stream.BackpatchWord(VSTOffsetPlaceholder, VSTOffset / 32 + 1);
3427
3429
3430 auto Abbv = std::make_shared<BitCodeAbbrev>();
3432 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
3433 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
3434 unsigned FnEntryAbbrev = Stream.EmitAbbrev(std::move(Abbv));
3435
3436 for (const Function &F : M) {
3437 uint64_t Record[2];
3438
3439 if (F.isDeclaration())
3440 continue;
3441
3442 Record[0] = VE.getValueID(&F);
3443
3444 // Save the word offset of the function (from the start of the
3445 // actual bitcode written to the stream).
3446 uint64_t BitcodeIndex = FunctionToBitcodeIndex[&F] - bitcodeStartBit();
3447 assert((BitcodeIndex & 31) == 0 && "function block not 32-bit aligned");
3448 // Note that we add 1 here because the offset is relative to one word
3449 // before the start of the identification block, which was historically
3450 // always the start of the regular bitcode header.
3451 Record[1] = BitcodeIndex / 32 + 1;
3452
3453 Stream.EmitRecord(bitc::VST_CODE_FNENTRY, Record, FnEntryAbbrev);
3454 }
3455
3456 Stream.ExitBlock();
3457}
3458
3459/// Emit names for arguments, instructions and basic blocks in a function.
3460void ModuleBitcodeWriter::writeFunctionLevelValueSymbolTable(
3461 const ValueSymbolTable &VST) {
3462 if (VST.empty())
3463 return;
3464
3466
3467 // FIXME: Set up the abbrev, we know how many values there are!
3468 // FIXME: We know if the type names can use 7-bit ascii.
3470
3471 for (const ValueName &Name : VST) {
3472 // Figure out the encoding to use for the name.
3474
3475 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
3476 NameVals.push_back(VE.getValueID(Name.getValue()));
3477
3478 // VST_CODE_ENTRY: [valueid, namechar x N]
3479 // VST_CODE_BBENTRY: [bbid, namechar x N]
3480 unsigned Code;
3481 if (isa<BasicBlock>(Name.getValue())) {
3483 if (Bits == SE_Char6)
3484 AbbrevToUse = VST_BBENTRY_6_ABBREV;
3485 } else {
3487 if (Bits == SE_Char6)
3488 AbbrevToUse = VST_ENTRY_6_ABBREV;
3489 else if (Bits == SE_Fixed7)
3490 AbbrevToUse = VST_ENTRY_7_ABBREV;
3491 }
3492
3493 for (const auto P : Name.getKey())
3494 NameVals.push_back((unsigned char)P);
3495
3496 // Emit the finished record.
3497 Stream.EmitRecord(Code, NameVals, AbbrevToUse);
3498 NameVals.clear();
3499 }
3500
3501 Stream.ExitBlock();
3502}
3503
3504void ModuleBitcodeWriter::writeUseList(UseListOrder &&Order) {
3505 assert(Order.Shuffle.size() >= 2 && "Shuffle too small");
3506 unsigned Code;
3507 if (isa<BasicBlock>(Order.V))
3509 else
3511
3512 SmallVector<uint64_t, 64> Record(Order.Shuffle.begin(), Order.Shuffle.end());
3513 Record.push_back(VE.getValueID(Order.V));
3514 Stream.EmitRecord(Code, Record);
3515}
3516
3517void ModuleBitcodeWriter::writeUseListBlock(const Function *F) {
3519 "Expected to be preserving use-list order");
3520
3521 auto hasMore = [&]() {
3522 return !VE.UseListOrders.empty() && VE.UseListOrders.back().F == F;
3523 };
3524 if (!hasMore())
3525 // Nothing to do.
3526 return;
3527
3529 while (hasMore()) {
3530 writeUseList(std::move(VE.UseListOrders.back()));
3531 VE.UseListOrders.pop_back();
3532 }
3533 Stream.ExitBlock();
3534}
3535
3536/// Emit a function body to the module stream.
3537void ModuleBitcodeWriter::writeFunction(
3538 const Function &F,
3539 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex) {
3540 // Save the bitcode index of the start of this function block for recording
3541 // in the VST.
3542 FunctionToBitcodeIndex[&F] = Stream.GetCurrentBitNo();
3543
3546
3548
3549 // Emit the number of basic blocks, so the reader can create them ahead of
3550 // time.
3551 Vals.push_back(VE.getBasicBlocks().size());
3553 Vals.clear();
3554
3555 // If there are function-local constants, emit them now.
3556 unsigned CstStart, CstEnd;
3557 VE.getFunctionConstantRange(CstStart, CstEnd);
3558 writeConstants(CstStart, CstEnd, false);
3559
3560 // If there is function-local metadata, emit it now.
3561 writeFunctionMetadata(F);
3562
3563 // Keep a running idea of what the instruction ID is.
3564 unsigned InstID = CstEnd;
3565
3566 bool NeedsMetadataAttachment = F.hasMetadata();
3567
3568 DILocation *LastDL = nullptr;
3569 SmallSetVector<Function *, 4> BlockAddressUsers;
3570
3571 // Finally, emit all the instructions, in order.
3572 for (const BasicBlock &BB : F) {
3573 for (const Instruction &I : BB) {
3574 writeInstruction(I, InstID, Vals);
3575
3576 if (!I.getType()->isVoidTy())
3577 ++InstID;
3578
3579 // If the instruction has metadata, write a metadata attachment later.
3580 NeedsMetadataAttachment |= I.hasMetadataOtherThanDebugLoc();
3581
3582 // If the instruction has a debug location, emit it.
3583 if (DILocation *DL = I.getDebugLoc()) {
3584 if (DL == LastDL) {
3585 // Just repeat the same debug loc as last time.
3587 } else {
3588 Vals.push_back(DL->getLine());
3589 Vals.push_back(DL->getColumn());
3590 Vals.push_back(VE.getMetadataOrNullID(DL->getScope()));
3591 Vals.push_back(VE.getMetadataOrNullID(DL->getInlinedAt()));
3592 Vals.push_back(DL->isImplicitCode());
3594 Vals.clear();
3595 LastDL = DL;
3596 }
3597 }
3598
3599 // If the instruction has DbgRecords attached to it, emit them. Note that
3600 // they come after the instruction so that it's easy to attach them again
3601 // when reading the bitcode, even though conceptually the debug locations
3602 // start "before" the instruction.
3603 if (I.hasDbgRecords() && WriteNewDbgInfoFormatToBitcode) {
3604 /// Try to push the value only (unwrapped), otherwise push the
3605 /// metadata wrapped value. Returns true if the value was pushed
3606 /// without the ValueAsMetadata wrapper.
3607 auto PushValueOrMetadata = [&Vals, InstID,
3608 this](Metadata *RawLocation) {
3609 assert(RawLocation &&
3610 "RawLocation unexpectedly null in DbgVariableRecord");
3611 if (ValueAsMetadata *VAM = dyn_cast<ValueAsMetadata>(RawLocation)) {
3612 SmallVector<unsigned, 2> ValAndType;
3613 // If the value is a fwd-ref the type is also pushed. We don't
3614 // want the type, so fwd-refs are kept wrapped (pushValueAndType
3615 // returns false if the value is pushed without type).
3616 if (!pushValueAndType(VAM->getValue(), InstID, ValAndType)) {
3617 Vals.push_back(ValAndType[0]);
3618 return true;
3619 }
3620 }
3621 // The metadata is a DIArgList, or ValueAsMetadata wrapping a
3622 // fwd-ref. Push the metadata ID.
3623 Vals.push_back(VE.getMetadataID(RawLocation));
3624 return false;
3625 };
3626
3627 // Write out non-instruction debug information attached to this
3628 // instruction. Write it after the instruction so that it's easy to
3629 // re-attach to the instruction reading the records in.
3630 for (DbgRecord &DR : I.DebugMarker->getDbgRecordRange()) {
3631 if (DbgLabelRecord *DLR = dyn_cast<DbgLabelRecord>(&DR)) {
3632 Vals.push_back(VE.getMetadataID(&*DLR->getDebugLoc()));
3633 Vals.push_back(VE.getMetadataID(DLR->getLabel()));
3635 Vals.clear();
3636 continue;
3637 }
3638
3639 // First 3 fields are common to all kinds:
3640 // DILocation, DILocalVariable, DIExpression
3641 // dbg_value (FUNC_CODE_DEBUG_RECORD_VALUE)
3642 // ..., LocationMetadata
3643 // dbg_value (FUNC_CODE_DEBUG_RECORD_VALUE_SIMPLE - abbrev'd)
3644 // ..., Value
3645 // dbg_declare (FUNC_CODE_DEBUG_RECORD_DECLARE)
3646 // ..., LocationMetadata
3647 // dbg_assign (FUNC_CODE_DEBUG_RECORD_ASSIGN)
3648 // ..., LocationMetadata, DIAssignID, DIExpression, LocationMetadata
3649 DbgVariableRecord &DVR = cast<DbgVariableRecord>(DR);
3650 Vals.push_back(VE.getMetadataID(&*DVR.getDebugLoc()));
3651 Vals.push_back(VE.getMetadataID(DVR.getVariable()));
3652 Vals.push_back(VE.getMetadataID(DVR.getExpression()));
3653 if (DVR.isDbgValue()) {
3654 if (PushValueOrMetadata(DVR.getRawLocation()))
3656 FUNCTION_DEBUG_RECORD_VALUE_ABBREV);
3657 else
3659 } else if (DVR.isDbgDeclare()) {
3660 Vals.push_back(VE.getMetadataID(DVR.getRawLocation()));
3662 } else {
3663 assert(DVR.isDbgAssign() && "Unexpected DbgRecord kind");
3664 Vals.push_back(VE.getMetadataID(DVR.getRawLocation()));
3665 Vals.push_back(VE.getMetadataID(DVR.getAssignID()));
3667 Vals.push_back(VE.getMetadataID(DVR.getRawAddress()));
3669 }
3670 Vals.clear();
3671 }
3672 }
3673 }
3674
3675 if (BlockAddress *BA = BlockAddress::lookup(&BB)) {
3676 SmallVector<Value *> Worklist{BA};
3677 SmallPtrSet<Value *, 8> Visited{BA};
3678 while (!Worklist.empty()) {
3679 Value *V = Worklist.pop_back_val();
3680 for (User *U : V->users()) {
3681 if (auto *I = dyn_cast<Instruction>(U)) {
3682 Function *P = I->getFunction();
3683 if (P != &F)
3684 BlockAddressUsers.insert(P);
3685 } else if (isa<Constant>(U) && !isa<GlobalValue>(U) &&
3686 Visited.insert(U).second)
3687 Worklist.push_back(U);
3688 }
3689 }
3690 }
3691 }
3692
3693 if (!BlockAddressUsers.empty()) {
3694 Vals.resize(BlockAddressUsers.size());
3695 for (auto I : llvm::enumerate(BlockAddressUsers))
3696 Vals[I.index()] = VE.getValueID(I.value());
3698 Vals.clear();
3699 }
3700
3701 // Emit names for all the instructions etc.
3702 if (auto *Symtab = F.getValueSymbolTable())
3703 writeFunctionLevelValueSymbolTable(*Symtab);
3704
3705 if (NeedsMetadataAttachment)
3706 writeFunctionMetadataAttachment(F);
3708 writeUseListBlock(&F);
3709 VE.purgeFunction();
3710 Stream.ExitBlock();
3711}
3712
3713// Emit blockinfo, which defines the standard abbreviations etc.
3714void ModuleBitcodeWriter::writeBlockInfo() {
3715 // We only want to emit block info records for blocks that have multiple
3716 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK.
3717 // Other blocks can define their abbrevs inline.
3718 Stream.EnterBlockInfoBlock();
3719
3720 { // 8-bit fixed-width VST_CODE_ENTRY/VST_CODE_BBENTRY strings.
3721 auto Abbv = std::make_shared<BitCodeAbbrev>();
3727 VST_ENTRY_8_ABBREV)
3728 llvm_unreachable("Unexpected abbrev ordering!");
3729 }
3730
3731 { // 7-bit fixed width VST_CODE_ENTRY strings.
3732 auto Abbv = std::make_shared<BitCodeAbbrev>();
3738 VST_ENTRY_7_ABBREV)
3739 llvm_unreachable("Unexpected abbrev ordering!");
3740 }
3741 { // 6-bit char6 VST_CODE_ENTRY strings.
3742 auto Abbv = std::make_shared<BitCodeAbbrev>();
3748 VST_ENTRY_6_ABBREV)
3749 llvm_unreachable("Unexpected abbrev ordering!");
3750 }
3751 { // 6-bit char6 VST_CODE_BBENTRY strings.
3752 auto Abbv = std::make_shared<BitCodeAbbrev>();
3758 VST_BBENTRY_6_ABBREV)
3759 llvm_unreachable("Unexpected abbrev ordering!");
3760 }
3761
3762 { // SETTYPE abbrev for CONSTANTS_BLOCK.
3763 auto Abbv = std::make_shared<BitCodeAbbrev>();
3768 CONSTANTS_SETTYPE_ABBREV)
3769 llvm_unreachable("Unexpected abbrev ordering!");
3770 }
3771
3772 { // INTEGER abbrev for CONSTANTS_BLOCK.
3773 auto Abbv = std::make_shared<BitCodeAbbrev>();
3777 CONSTANTS_INTEGER_ABBREV)
3778 llvm_unreachable("Unexpected abbrev ordering!");
3779 }
3780
3781 { // CE_CAST abbrev for CONSTANTS_BLOCK.
3782 auto Abbv = std::make_shared<BitCodeAbbrev>();
3784 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc
3785 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid
3787 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
3788
3790 CONSTANTS_CE_CAST_Abbrev)
3791 llvm_unreachable("Unexpected abbrev ordering!");
3792 }
3793 { // NULL abbrev for CONSTANTS_BLOCK.
3794 auto Abbv = std::make_shared<BitCodeAbbrev>();
3797 CONSTANTS_NULL_Abbrev)
3798 llvm_unreachable("Unexpected abbrev ordering!");
3799 }
3800
3801 // FIXME: This should only use space for first class types!
3802
3803 { // INST_LOAD abbrev for FUNCTION_BLOCK.
3804 auto Abbv = std::make_shared<BitCodeAbbrev>();
3806 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr
3807 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
3809 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align
3810 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
3811 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3812 FUNCTION_INST_LOAD_ABBREV)
3813 llvm_unreachable("Unexpected abbrev ordering!");
3814 }
3815 { // INST_UNOP abbrev for FUNCTION_BLOCK.
3816 auto Abbv = std::make_shared<BitCodeAbbrev>();
3818 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
3819 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
3820 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3821 FUNCTION_INST_UNOP_ABBREV)
3822 llvm_unreachable("Unexpected abbrev ordering!");
3823 }
3824 { // INST_UNOP_FLAGS abbrev for FUNCTION_BLOCK.
3825 auto Abbv = std::make_shared<BitCodeAbbrev>();
3827 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
3828 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
3829 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); // flags
3830 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3831 FUNCTION_INST_UNOP_FLAGS_ABBREV)
3832 llvm_unreachable("Unexpected abbrev ordering!");
3833 }
3834 { // INST_BINOP abbrev for FUNCTION_BLOCK.
3835 auto Abbv = std::make_shared<BitCodeAbbrev>();
3837 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
3838 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
3839 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
3840 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3841 FUNCTION_INST_BINOP_ABBREV)
3842 llvm_unreachable("Unexpected abbrev ordering!");
3843 }
3844 { // INST_BINOP_FLAGS abbrev for FUNCTION_BLOCK.
3845 auto Abbv = std::make_shared<BitCodeAbbrev>();
3847 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
3848 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
3849 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
3850 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); // flags
3851 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3852 FUNCTION_INST_BINOP_FLAGS_ABBREV)
3853 llvm_unreachable("Unexpected abbrev ordering!");
3854 }
3855 { // INST_CAST abbrev for FUNCTION_BLOCK.
3856 auto Abbv = std::make_shared<BitCodeAbbrev>();
3858 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal
3859 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
3861 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
3862 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3863 FUNCTION_INST_CAST_ABBREV)
3864 llvm_unreachable("Unexpected abbrev ordering!");
3865 }
3866 { // INST_CAST_FLAGS abbrev for FUNCTION_BLOCK.
3867 auto Abbv = std::make_shared<BitCodeAbbrev>();
3869 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal
3870 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
3872 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
3873 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); // flags
3874 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3875 FUNCTION_INST_CAST_FLAGS_ABBREV)
3876 llvm_unreachable("Unexpected abbrev ordering!");
3877 }
3878
3879 { // INST_RET abbrev for FUNCTION_BLOCK.
3880 auto Abbv = std::make_shared<BitCodeAbbrev>();
3882 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3883 FUNCTION_INST_RET_VOID_ABBREV)
3884 llvm_unreachable("Unexpected abbrev ordering!");
3885 }
3886 { // INST_RET abbrev for FUNCTION_BLOCK.
3887 auto Abbv = std::make_shared<BitCodeAbbrev>();
3889 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID
3890 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3891 FUNCTION_INST_RET_VAL_ABBREV)
3892 llvm_unreachable("Unexpected abbrev ordering!");
3893 }
3894 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
3895 auto Abbv = std::make_shared<BitCodeAbbrev>();
3897 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3898 FUNCTION_INST_UNREACHABLE_ABBREV)
3899 llvm_unreachable("Unexpected abbrev ordering!");
3900 }
3901 {
3902 auto Abbv = std::make_shared<BitCodeAbbrev>();
3905 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
3906 Log2_32_Ceil(VE.getTypes().size() + 1)));
3909 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3910 FUNCTION_INST_GEP_ABBREV)
3911 llvm_unreachable("Unexpected abbrev ordering!");
3912 }
3913 {
3914 auto Abbv = std::make_shared<BitCodeAbbrev>();
3916 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 7)); // dbgloc
3917 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 7)); // var
3918 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 7)); // expr
3919 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // val
3920 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3921 FUNCTION_DEBUG_RECORD_VALUE_ABBREV)
3922 llvm_unreachable("Unexpected abbrev ordering! 1");
3923 }
3924 Stream.ExitBlock();
3925}
3926
3927/// Write the module path strings, currently only used when generating
3928/// a combined index file.
3929void IndexBitcodeWriter::writeModStrings() {
3931
3932 // TODO: See which abbrev sizes we actually need to emit
3933
3934 // 8-bit fixed-width MST_ENTRY strings.
3935 auto Abbv = std::make_shared<BitCodeAbbrev>();
3940 unsigned Abbrev8Bit = Stream.EmitAbbrev(std::move(Abbv));
3941
3942 // 7-bit fixed width MST_ENTRY strings.
3943 Abbv = std::make_shared<BitCodeAbbrev>();
3948 unsigned Abbrev7Bit = Stream.EmitAbbrev(std::move(Abbv));
3949
3950 // 6-bit char6 MST_ENTRY strings.
3951 Abbv = std::make_shared<BitCodeAbbrev>();
3956 unsigned Abbrev6Bit = Stream.EmitAbbrev(std::move(Abbv));
3957
3958 // Module Hash, 160 bits SHA1. Optionally, emitted after each MST_CODE_ENTRY.
3959 Abbv = std::make_shared<BitCodeAbbrev>();
3966 unsigned AbbrevHash = Stream.EmitAbbrev(std::move(Abbv));
3967
3969 forEachModule([&](const StringMapEntry<ModuleHash> &MPSE) {
3970 StringRef Key = MPSE.getKey();
3971 const auto &Hash = MPSE.getValue();
3973 unsigned AbbrevToUse = Abbrev8Bit;
3974 if (Bits == SE_Char6)
3975 AbbrevToUse = Abbrev6Bit;
3976 else if (Bits == SE_Fixed7)
3977 AbbrevToUse = Abbrev7Bit;
3978
3979 auto ModuleId = ModuleIdMap.size();
3980 ModuleIdMap[Key] = ModuleId;
3981 Vals.push_back(ModuleId);
3982 Vals.append(Key.begin(), Key.end());
3983
3984 // Emit the finished record.
3985 Stream.EmitRecord(bitc::MST_CODE_ENTRY, Vals, AbbrevToUse);
3986
3987 // Emit an optional hash for the module now
3988 if (llvm::any_of(Hash, [](uint32_t H) { return H; })) {
3989 Vals.assign(Hash.begin(), Hash.end());
3990 // Emit the hash record.
3991 Stream.EmitRecord(bitc::MST_CODE_HASH, Vals, AbbrevHash);
3992 }
3993
3994 Vals.clear();
3995 });
3996 Stream.ExitBlock();
3997}
3998
3999/// Write the function type metadata related records that need to appear before
4000/// a function summary entry (whether per-module or combined).
4001template <typename Fn>
4003 FunctionSummary *FS,
4004 Fn GetValueID) {
4005 if (!FS->type_tests().empty())
4006 Stream.EmitRecord(bitc::FS_TYPE_TESTS, FS->type_tests());
4007
4009
4010 auto WriteVFuncIdVec = [&](uint64_t Ty,
4012 if (VFs.empty())
4013 return;
4014 Record.clear();
4015 for (auto &VF : VFs) {
4016 Record.push_back(VF.GUID);
4017 Record.push_back(VF.Offset);
4018 }
4019 Stream.EmitRecord(Ty, Record);
4020 };
4021
4022 WriteVFuncIdVec(bitc::FS_TYPE_TEST_ASSUME_VCALLS,
4023 FS->type_test_assume_vcalls());
4024 WriteVFuncIdVec(bitc::FS_TYPE_CHECKED_LOAD_VCALLS,
4025 FS->type_checked_load_vcalls());
4026
4027 auto WriteConstVCallVec = [&](uint64_t Ty,
4029 for (auto &VC : VCs) {
4030 Record.clear();
4031 Record.push_back(VC.VFunc.GUID);
4032 Record.push_back(VC.VFunc.Offset);
4033 llvm::append_range(Record, VC.Args);
4034 Stream.EmitRecord(Ty, Record);
4035 }
4036 };
4037
4038 WriteConstVCallVec(bitc::FS_TYPE_TEST_ASSUME_CONST_VCALL,
4039 FS->type_test_assume_const_vcalls());
4040 WriteConstVCallVec(bitc::FS_TYPE_CHECKED_LOAD_CONST_VCALL,
4041 FS->type_checked_load_const_vcalls());
4042
4043 auto WriteRange = [&](ConstantRange Range) {
4045 assert(Range.getLower().getNumWords() == 1);
4046 assert(Range.getUpper().getNumWords() == 1);
4049 };
4050
4051 if (!FS->paramAccesses().empty()) {
4052 Record.clear();
4053 for (auto &Arg : FS->paramAccesses()) {
4054 size_t UndoSize = Record.size();
4055 Record.push_back(Arg.ParamNo);
4056 WriteRange(Arg.Use);
4057 Record.push_back(Arg.Calls.size());
4058 for (auto &Call : Arg.Calls) {
4059 Record.push_back(Call.ParamNo);
4060 std::optional<unsigned> ValueID = GetValueID(Call.Callee);
4061 if (!ValueID) {
4062 // If ValueID is unknown we can't drop just this call, we must drop
4063 // entire parameter.
4064 Record.resize(UndoSize);
4065 break;
4066 }
4067 Record.push_back(*ValueID);
4068 WriteRange(Call.Offsets);
4069 }
4070 }
4071 if (!Record.empty())
4073 }
4074}
4075
4076/// Collect type IDs from type tests used by function.
4077static void
4079 std::set<GlobalValue::GUID> &ReferencedTypeIds) {
4080 if (!FS->type_tests().empty())
4081 for (auto &TT : FS->type_tests())
4082 ReferencedTypeIds.insert(TT);
4083
4084 auto GetReferencedTypesFromVFuncIdVec =
4086 for (auto &VF : VFs)
4087 ReferencedTypeIds.insert(VF.GUID);
4088 };
4089
4090 GetReferencedTypesFromVFuncIdVec(FS->type_test_assume_vcalls());
4091 GetReferencedTypesFromVFuncIdVec(FS->type_checked_load_vcalls());
4092
4093 auto GetReferencedTypesFromConstVCallVec =
4095 for (auto &VC : VCs)
4096 ReferencedTypeIds.insert(VC.VFunc.GUID);
4097 };
4098
4099 GetReferencedTypesFromConstVCallVec(FS->type_test_assume_const_vcalls());
4100 GetReferencedTypesFromConstVCallVec(FS->type_checked_load_const_vcalls());
4101}
4102
4104 SmallVector<uint64_t, 64> &NameVals, const std::vector<uint64_t> &args,
4106 NameVals.push_back(args.size());
4107 llvm::append_range(NameVals, args);
4108
4109 NameVals.push_back(ByArg.TheKind);
4110 NameVals.push_back(ByArg.Info);
4111 NameVals.push_back(ByArg.Byte);
4112 NameVals.push_back(ByArg.Bit);
4113}
4114
4116 SmallVector<uint64_t, 64> &NameVals, StringTableBuilder &StrtabBuilder,
4117 uint64_t Id, const WholeProgramDevirtResolution &Wpd) {
4118 NameVals.push_back(Id);
4119
4120 NameVals.push_back(Wpd.TheKind);
4121 NameVals.push_back(StrtabBuilder.add(Wpd.SingleImplName));
4122 NameVals.push_back(Wpd.SingleImplName.size());
4123
4124 NameVals.push_back(Wpd.ResByArg.size());
4125 for (auto &A : Wpd.ResByArg)
4126 writeWholeProgramDevirtResolutionByArg(NameVals, A.first, A.second);
4127}
4128
4130 StringTableBuilder &StrtabBuilder,
4131 const std::string &Id,
4132 const TypeIdSummary &Summary) {
4133 NameVals.push_back(StrtabBuilder.add(Id));
4134 NameVals.push_back(Id.size());
4135
4136 NameVals.push_back(Summary.TTRes.TheKind);
4137 NameVals.push_back(Summary.TTRes.SizeM1BitWidth);
4138 NameVals.push_back(Summary.TTRes.AlignLog2);
4139 NameVals.push_back(Summary.TTRes.SizeM1);
4140 NameVals.push_back(Summary.TTRes.BitMask);
4141 NameVals.push_back(Summary.TTRes.InlineBits);
4142
4143 for (auto &W : Summary.WPDRes)
4144 writeWholeProgramDevirtResolution(NameVals, StrtabBuilder, W.first,
4145 W.second);
4146}
4147
4149 SmallVector<uint64_t, 64> &NameVals, StringTableBuilder &StrtabBuilder,
4150 const std::string &Id, const TypeIdCompatibleVtableInfo &Summary,
4151 ValueEnumerator &VE) {
4152 NameVals.push_back(StrtabBuilder.add(Id));
4153 NameVals.push_back(Id.size());
4154
4155 for (auto &P : Summary) {
4156 NameVals.push_back(P.AddressPointOffset);
4157 NameVals.push_back(VE.getValueID(P.VTableVI.getValue()));
4158 }
4159}
4160
4162 BitstreamWriter &Stream, FunctionSummary *FS, unsigned CallsiteAbbrev,
4163 unsigned AllocAbbrev, bool PerModule,
4164 std::function<unsigned(const ValueInfo &VI)> GetValueID,
4165 std::function<unsigned(unsigned)> GetStackIndex) {
4167
4168 for (auto &CI : FS->callsites()) {
4169 Record.clear();
4170 // Per module callsite clones should always have a single entry of
4171 // value 0.
4172 assert(!PerModule || (CI.Clones.size() == 1 && CI.Clones[0] == 0));
4173 Record.push_back(GetValueID(CI.Callee));
4174 if (!PerModule) {
4175 Record.push_back(CI.StackIdIndices.size());
4176 Record.push_back(CI.Clones.size());
4177 }
4178 for (auto Id : CI.StackIdIndices)
4179 Record.push_back(GetStackIndex(Id));
4180 if (!PerModule) {
4181 for (auto V : CI.Clones)
4182 Record.push_back(V);
4183 }
4186 Record, CallsiteAbbrev);
4187 }
4188
4189 for (auto &AI : FS->allocs()) {
4190 Record.clear();
4191 // Per module alloc versions should always have a single entry of
4192 // value 0.
4193 assert(!PerModule || (AI.Versions.size() == 1 && AI.Versions[0] == 0));
4194 Record.push_back(AI.MIBs.size());
4195 if (!PerModule)
4196 Record.push_back(AI.Versions.size());
4197 for (auto &MIB : AI.MIBs) {
4198 Record.push_back((uint8_t)MIB.AllocType);
4199 Record.push_back(MIB.StackIdIndices.size());
4200 for (auto Id : MIB.StackIdIndices)
4201 Record.push_back(GetStackIndex(Id));
4202 }
4203 if (!PerModule) {
4204 for (auto V : AI.Versions)
4205 Record.push_back(V);
4206 }
4207 assert(AI.TotalSizes.empty() || AI.TotalSizes.size() == AI.MIBs.size());
4208 if (!AI.TotalSizes.empty()) {
4209 for (auto Size : AI.TotalSizes)
4210 Record.push_back(Size);
4211 }
4212 Stream.EmitRecord(PerModule ? bitc::FS_PERMODULE_ALLOC_INFO
4214 Record, AllocAbbrev);
4215 }
4216}
4217
4218// Helper to emit a single function summary record.
4219void ModuleBitcodeWriterBase::writePerModuleFunctionSummaryRecord(
4221 unsigned ValueID, unsigned FSCallsRelBFAbbrev,
4222 unsigned FSCallsProfileAbbrev, unsigned CallsiteAbbrev,
4223 unsigned AllocAbbrev, const Function &F) {
4224 NameVals.push_back(ValueID);
4225
4226 FunctionSummary *FS = cast<FunctionSummary>(Summary);
4227
4229 Stream, FS, [&](const ValueInfo &VI) -> std::optional<unsigned> {
4230 return {VE.getValueID(VI.getValue())};
4231 });
4232
4234 Stream, FS, CallsiteAbbrev, AllocAbbrev,
4235 /*PerModule*/ true,
4236 /*GetValueId*/ [&](const ValueInfo &VI) { return getValueId(VI); },
4237 /*GetStackIndex*/ [&](unsigned I) { return I; });
4238
4239 auto SpecialRefCnts = FS->specialRefCounts();
4240 NameVals.push_back(getEncodedGVSummaryFlags(FS->flags()));
4241 NameVals.push_back(FS->instCount());
4242 NameVals.push_back(getEncodedFFlags(FS->fflags()));
4243 NameVals.push_back(FS->refs().size());
4244 NameVals.push_back(SpecialRefCnts.first); // rorefcnt
4245 NameVals.push_back(SpecialRefCnts.second); // worefcnt
4246
4247 for (auto &RI : FS->refs())
4248 NameVals.push_back(getValueId(RI));
4249
4250 const bool UseRelBFRecord =
4251 WriteRelBFToSummary && !F.hasProfileData() &&
4253 for (auto &ECI : FS->calls()) {
4254 NameVals.push_back(getValueId(ECI.first));
4255 if (UseRelBFRecord)
4256 NameVals.push_back(getEncodedRelBFCallEdgeInfo(ECI.second));
4257 else
4258 NameVals.push_back(getEncodedHotnessCallEdgeInfo(ECI.second));
4259 }
4260
4261 unsigned FSAbbrev =
4262 (UseRelBFRecord ? FSCallsRelBFAbbrev : FSCallsProfileAbbrev);
4263 unsigned Code =
4265
4266 // Emit the finished record.
4267 Stream.EmitRecord(Code, NameVals, FSAbbrev);
4268 NameVals.clear();
4269}
4270
4271// Collect the global value references in the given variable's initializer,
4272// and emit them in a summary record.
4273void ModuleBitcodeWriterBase::writeModuleLevelReferences(
4274 const GlobalVariable &V, SmallVector<uint64_t, 64> &NameVals,
4275 unsigned FSModRefsAbbrev, unsigned FSModVTableRefsAbbrev) {
4276 auto VI = Index->getValueInfo(V.getGUID());
4277 if (!VI || VI.getSummaryList().empty()) {
4278 // Only declarations should not have a summary (a declaration might however
4279 // have a summary if the def was in module level asm).
4280 assert(V.isDeclaration());
4281 return;
4282 }
4283 auto *Summary = VI.getSummaryList()[0].get();
4284 NameVals.push_back(VE.getValueID(&V));
4285 GlobalVarSummary *VS = cast<GlobalVarSummary>(Summary);
4286 NameVals.push_back(getEncodedGVSummaryFlags(VS->flags()));
4287 NameVals.push_back(getEncodedGVarFlags(VS->varflags()));
4288
4289 auto VTableFuncs = VS->vTableFuncs();
4290 if (!VTableFuncs.empty())
4291 NameVals.push_back(VS->refs().size());
4292
4293 unsigned SizeBeforeRefs = NameVals.size();
4294 for (auto &RI : VS->refs())
4295 NameVals.push_back(VE.getValueID(RI.getValue()));
4296 // Sort the refs for determinism output, the vector returned by FS->refs() has
4297 // been initialized from a DenseSet.
4298 llvm::sort(drop_begin(NameVals, SizeBeforeRefs));
4299
4300 if (VTableFuncs.empty())
4302 FSModRefsAbbrev);
4303 else {
4304 // VTableFuncs pairs should already be sorted by offset.
4305 for (auto &P : VTableFuncs) {
4306 NameVals.push_back(VE.getValueID(P.FuncVI.getValue()));
4307 NameVals.push_back(P.VTableOffset);
4308 }
4309
4311 FSModVTableRefsAbbrev);
4312 }
4313 NameVals.clear();
4314}
4315
4316/// Emit the per-module summary section alongside the rest of
4317/// the module's bitcode.
4318void ModuleBitcodeWriterBase::writePerModuleGlobalValueSummary() {
4319 // By default we compile with ThinLTO if the module has a summary, but the
4320 // client can request full LTO with a module flag.
4321 bool IsThinLTO = true;
4322 if (auto *MD =
4323 mdconst::extract_or_null<ConstantInt>(M.getModuleFlag("ThinLTO")))
4324 IsThinLTO = MD->getZExtValue();
4327 4);
4328
4329 Stream.EmitRecord(
4332
4333 // Write the index flags.
4334 uint64_t Flags = 0;
4335 // Bits 1-3 are set only in the combined index, skip them.
4336 if (Index->enableSplitLTOUnit())
4337 Flags |= 0x8;
4338 if (Index->hasUnifiedLTO())
4339 Flags |= 0x200;
4340
4342
4343 if (Index->begin() == Index->end()) {
4344 Stream.ExitBlock();
4345 return;
4346 }
4347
4348 for (const auto &GVI : valueIds()) {
4350 ArrayRef<uint64_t>{GVI.second, GVI.first});
4351 }
4352
4353 if (!Index->stackIds().empty()) {
4354 auto StackIdAbbv = std::make_shared<BitCodeAbbrev>();
4355 StackIdAbbv->Add(BitCodeAbbrevOp(bitc::FS_STACK_IDS));
4356 // numids x stackid
4357 StackIdAbbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
4358 StackIdAbbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
4359 unsigned StackIdAbbvId = Stream.EmitAbbrev(std::move(StackIdAbbv));
4360 Stream.EmitRecord(bitc::FS_STACK_IDS, Index->stackIds(), StackIdAbbvId);
4361 }
4362
4363 // Abbrev for FS_PERMODULE_PROFILE.
4364 auto Abbv = std::make_shared<BitCodeAbbrev>();
4366 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
4367 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // flags
4368 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
4369 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // fflags
4370 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
4371 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // rorefcnt
4372 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // worefcnt
4373 // numrefs x valueid, n x (valueid, hotness+tailcall flags)
4376 unsigned FSCallsProfileAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4377
4378 // Abbrev for FS_PERMODULE_RELBF.
4379 Abbv = std::make_shared<BitCodeAbbrev>();
4381 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
4382 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
4383 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
4384 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // fflags
4385 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
4386 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // rorefcnt
4387 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // worefcnt
4388 // numrefs x valueid, n x (valueid, rel_block_freq+tailcall])
4391 unsigned FSCallsRelBFAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4392
4393 // Abbrev for FS_PERMODULE_GLOBALVAR_INIT_REFS.
4394 Abbv = std::make_shared<BitCodeAbbrev>();
4396 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
4397 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
4398 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); // valueids
4400 unsigned FSModRefsAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4401
4402 // Abbrev for FS_PERMODULE_VTABLE_GLOBALVAR_INIT_REFS.
4403 Abbv = std::make_shared<BitCodeAbbrev>();
4405 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
4406 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
4407 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
4408 // numrefs x valueid, n x (valueid , offset)
4411 unsigned FSModVTableRefsAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4412
4413 // Abbrev for FS_ALIAS.
4414 Abbv = std::make_shared<BitCodeAbbrev>();
4415 Abbv->Add(BitCodeAbbrevOp(bitc::FS_ALIAS));
4416 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
4417 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
4418 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
4419 unsigned FSAliasAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4420
4421 // Abbrev for FS_TYPE_ID_METADATA
4422 Abbv = std::make_shared<BitCodeAbbrev>();
4424 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // typeid strtab index
4425 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // typeid length
4426 // n x (valueid , offset)
4429 unsigned TypeIdCompatibleVtableAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4430
4431 Abbv = std::make_shared<BitCodeAbbrev>();
4433 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
4434 // n x stackidindex
4437 unsigned CallsiteAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4438
4439 Abbv = std::make_shared<BitCodeAbbrev>();
4441 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // nummib
4442 // n x (alloc type, numstackids, numstackids x stackidindex)
4443 // optional: nummib x total size
4446 unsigned AllocAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4447
4449 // Iterate over the list of functions instead of the Index to
4450 // ensure the ordering is stable.
4451 for (const Function &F : M) {
4452 // Summary emission does not support anonymous functions, they have to
4453 // renamed using the anonymous function renaming pass.
4454 if (!F.hasName())
4455 report_fatal_error("Unexpected anonymous function when writing summary");
4456
4457 ValueInfo VI = Index->getValueInfo(F.getGUID());
4458 if (!VI || VI.getSummaryList().empty()) {
4459 // Only declarations should not have a summary (a declaration might
4460 // however have a summary if the def was in module level asm).
4461 assert(F.isDeclaration());
4462 continue;
4463 }
4464 auto *Summary = VI.getSummaryList()[0].get();
4465 writePerModuleFunctionSummaryRecord(
4466 NameVals, Summary, VE.getValueID(&F), FSCallsRelBFAbbrev,
4467 FSCallsProfileAbbrev, CallsiteAbbrev, AllocAbbrev, F);
4468 }
4469
4470 // Capture references from GlobalVariable initializers, which are outside
4471 // of a function scope.
4472 for (const GlobalVariable &G : M.globals())
4473 writeModuleLevelReferences(G, NameVals, FSModRefsAbbrev,
4474 FSModVTableRefsAbbrev);
4475
4476 for (const GlobalAlias &A : M.aliases()) {
4477 auto *Aliasee = A.getAliaseeObject();
4478 // Skip ifunc and nameless functions which don't have an entry in the
4479 // summary.
4480 if (!Aliasee->hasName() || isa<GlobalIFunc>(Aliasee))
4481 continue;
4482 auto AliasId = VE.getValueID(&A);
4483 auto AliaseeId = VE.getValueID(Aliasee);
4484 NameVals.push_back(AliasId);
4485 auto *Summary = Index->getGlobalValueSummary(A);
4486 AliasSummary *AS = cast<AliasSummary>(Summary);
4487 NameVals.push_back(getEncodedGVSummaryFlags(AS->flags()));
4488 NameVals.push_back(AliaseeId);
4489 Stream.EmitRecord(bitc::FS_ALIAS, NameVals, FSAliasAbbrev);
4490 NameVals.clear();
4491 }
4492
4493 for (auto &S : Index->typeIdCompatibleVtableMap()) {
4494 writeTypeIdCompatibleVtableSummaryRecord(NameVals, StrtabBuilder, S.first,
4495 S.second, VE);
4496 Stream.EmitRecord(bitc::FS_TYPE_ID_METADATA, NameVals,
4497 TypeIdCompatibleVtableAbbrev);
4498 NameVals.clear();
4499 }
4500
4501 if (Index->getBlockCount())
4503 ArrayRef<uint64_t>{Index->getBlockCount()});
4504
4505 Stream.ExitBlock();
4506}
4507
4508/// Emit the combined summary section into the combined index file.
4509void IndexBitcodeWriter::writeCombinedGlobalValueSummary() {
4511 Stream.EmitRecord(
4514
4515 // Write the index flags.
4517
4518 for (const auto &GVI : valueIds()) {
4520 ArrayRef<uint64_t>{GVI.second, GVI.first});
4521 }
4522
4523 // Write the stack ids used by this index, which will be a subset of those in
4524 // the full index in the case of distributed indexes.
4525 if (!StackIds.empty()) {
4526 auto StackIdAbbv = std::make_shared<BitCodeAbbrev>();
4527 StackIdAbbv->Add(BitCodeAbbrevOp(bitc::FS_STACK_IDS));
4528 // numids x stackid
4529 StackIdAbbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
4530 StackIdAbbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
4531 unsigned StackIdAbbvId = Stream.EmitAbbrev(std::move(StackIdAbbv));
4532 Stream.EmitRecord(bitc::FS_STACK_IDS, StackIds, StackIdAbbvId);
4533 }
4534
4535 // Abbrev for FS_COMBINED_PROFILE.
4536 auto Abbv = std::make_shared<BitCodeAbbrev>();
4538 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
4539 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
4540 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
4541 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
4542 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // fflags
4543 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // entrycount
4544 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
4545 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // rorefcnt
4546 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // worefcnt
4547 // numrefs x valueid, n x (valueid, hotness+tailcall flags)
4550 unsigned FSCallsProfileAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4551
4552 // Abbrev for FS_COMBINED_GLOBALVAR_INIT_REFS.
4553 Abbv = std::make_shared<BitCodeAbbrev>();
4555 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
4556 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
4557 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
4558 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); // valueids
4560 unsigned FSModRefsAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4561
4562 // Abbrev for FS_COMBINED_ALIAS.
4563 Abbv = std::make_shared<BitCodeAbbrev>();
4565 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
4566 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
4567 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
4568 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
4569 unsigned FSAliasAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4570
4571 Abbv = std::make_shared<BitCodeAbbrev>();
4573 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
4574 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numstackindices
4575 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numver
4576 // numstackindices x stackidindex, numver x version
4579 unsigned CallsiteAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4580
4581 Abbv = std::make_shared<BitCodeAbbrev>();
4583 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // nummib
4584 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numver
4585 // nummib x (alloc type, numstackids, numstackids x stackidindex),
4586 // numver x version
4587 // optional: nummib x total size
4590 unsigned AllocAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4591
4592 auto shouldImportValueAsDecl = [&](GlobalValueSummary *GVS) -> bool {
4593 if (DecSummaries == nullptr)
4594 return false;
4595 return DecSummaries->count(GVS);
4596 };
4597
4598 // The aliases are emitted as a post-pass, and will point to the value
4599 // id of the aliasee. Save them in a vector for post-processing.
4601
4602 // Save the value id for each summary for alias emission.
4604
4606
4607 // Set that will be populated during call to writeFunctionTypeMetadataRecords
4608 // with the type ids referenced by this index file.
4609 std::set<GlobalValue::GUID> ReferencedTypeIds;
4610
4611 // For local linkage, we also emit the original name separately
4612 // immediately after the record.
4613 auto MaybeEmitOriginalName = [&](GlobalValueSummary &S) {
4614 // We don't need to emit the original name if we are writing the index for
4615 // distributed backends (in which case ModuleToSummariesForIndex is
4616 // non-null). The original name is only needed during the thin link, since
4617 // for SamplePGO the indirect call targets for local functions have
4618 // have the original name annotated in profile.
4619 // Continue to emit it when writing out the entire combined index, which is
4620 // used in testing the thin link via llvm-lto.
4621 if (ModuleToSummariesForIndex || !GlobalValue::isLocalLinkage(S.linkage()))
4622 return;
4623 NameVals.push_back(S.getOriginalName());
4625 NameVals.clear();
4626 };
4627
4628 std::set<GlobalValue::GUID> DefOrUseGUIDs;
4629 forEachSummary([&](GVInfo I, bool IsAliasee) {
4630 GlobalValueSummary *S = I.second;
4631 assert(S);
4632 DefOrUseGUIDs.insert(I.first);
4633 for (const ValueInfo &VI : S->refs())
4634 DefOrUseGUIDs.insert(VI.getGUID());
4635
4636 auto ValueId = getValueId(I.first);
4637 assert(ValueId);
4638 SummaryToValueIdMap[S] = *ValueId;
4639
4640 // If this is invoked for an aliasee, we want to record the above
4641 // mapping, but then not emit a summary entry (if the aliasee is
4642 // to be imported, we will invoke this separately with IsAliasee=false).
4643 if (IsAliasee)
4644 return;
4645
4646 if (auto *AS = dyn_cast<AliasSummary>(S)) {
4647 // Will process aliases as a post-pass because the reader wants all
4648 // global to be loaded first.
4649 Aliases.push_back(AS);
4650 return;
4651 }
4652
4653 if (auto *VS = dyn_cast<GlobalVarSummary>(S)) {
4654 NameVals.push_back(*ValueId);
4655 assert(ModuleIdMap.count(VS->modulePath()));
4656 NameVals.push_back(ModuleIdMap[VS->modulePath()]);
4657 NameVals.push_back(getEncodedGVSummaryFlags(VS->flags()));
4658 NameVals.push_back(getEncodedGVarFlags(VS->varflags()));
4659 for (auto &RI : VS->refs()) {
4660 auto RefValueId = getValueId(RI.getGUID());
4661 if (!RefValueId)
4662 continue;
4663 NameVals.push_back(*RefValueId);
4664 }
4665
4666 // Emit the finished record.
4668 FSModRefsAbbrev);
4669 NameVals.clear();
4670 MaybeEmitOriginalName(*S);
4671 return;
4672 }
4673
4674 auto GetValueId = [&](const ValueInfo &VI) -> std::optional<unsigned> {
4675 if (!VI)
4676 return std::nullopt;
4677 return getValueId(VI.getGUID());
4678 };
4679
4680 auto *FS = cast<FunctionSummary>(S);
4681 writeFunctionTypeMetadataRecords(Stream, FS, GetValueId);
4682 getReferencedTypeIds(FS, ReferencedTypeIds);
4683
4685 Stream, FS, CallsiteAbbrev, AllocAbbrev,
4686 /*PerModule*/ false,
4687 /*GetValueId*/
4688 [&](const ValueInfo &VI) -> unsigned {
4689 std::optional<unsigned> ValueID = GetValueId(VI);
4690 // This can happen in shared index files for distributed ThinLTO if
4691 // the callee function summary is not included. Record 0 which we
4692 // will have to deal with conservatively when doing any kind of
4693 // validation in the ThinLTO backends.
4694 if (!ValueID)
4695 return 0;
4696 return *ValueID;
4697 },
4698 /*GetStackIndex*/
4699 [&](unsigned I) {
4700 // Get the corresponding index into the list of StackIds actually
4701 // being written for this combined index (which may be a subset in
4702 // the case of distributed indexes).
4703 assert(StackIdIndicesToIndex.contains(I));
4704 return StackIdIndicesToIndex[I];
4705 });
4706
4707 NameVals.push_back(*ValueId);
4708 assert(ModuleIdMap.count(FS->modulePath()));
4709 NameVals.push_back(ModuleIdMap[FS->modulePath()]);
4710 NameVals.push_back(
4711 getEncodedGVSummaryFlags(FS->flags(), shouldImportValueAsDecl(FS)));
4712 NameVals.push_back(FS->instCount());
4713 NameVals.push_back(getEncodedFFlags(FS->fflags()));
4714 NameVals.push_back(FS->entryCount());
4715
4716 // Fill in below
4717 NameVals.push_back(0); // numrefs
4718 NameVals.push_back(0); // rorefcnt
4719 NameVals.push_back(0); // worefcnt
4720
4721 unsigned Count = 0, RORefCnt = 0, WORefCnt = 0;
4722 for (auto &RI : FS->refs()) {
4723 auto RefValueId = getValueId(RI.getGUID());
4724 if (!RefValueId)
4725 continue;
4726 NameVals.push_back(*RefValueId);
4727 if (RI.isReadOnly())
4728 RORefCnt++;
4729 else if (RI.isWriteOnly())
4730 WORefCnt++;
4731 Count++;
4732 }
4733 NameVals[6] = Count;
4734 NameVals[7] = RORefCnt;
4735 NameVals[8] = WORefCnt;
4736
4737 for (auto &EI : FS->calls()) {
4738 // If this GUID doesn't have a value id, it doesn't have a function
4739 // summary and we don't need to record any calls to it.
4740 std::optional<unsigned> CallValueId = GetValueId(EI.first);
4741 if (!CallValueId)
4742 continue;
4743 NameVals.push_back(*CallValueId);
4744 NameVals.push_back(getEncodedHotnessCallEdgeInfo(EI.second));
4745 }
4746
4747 // Emit the finished record.
4748 Stream.EmitRecord(bitc::FS_COMBINED_PROFILE, NameVals,
4749 FSCallsProfileAbbrev);
4750 NameVals.clear();
4751 MaybeEmitOriginalName(*S);
4752 });
4753
4754 for (auto *AS : Aliases) {
4755 auto AliasValueId = SummaryToValueIdMap[AS];
4756 assert(AliasValueId);
4757 NameVals.push_back(AliasValueId);
4758 assert(ModuleIdMap.count(AS->modulePath()));
4759 NameVals.push_back(ModuleIdMap[AS->modulePath()]);
4760 NameVals.push_back(
4761 getEncodedGVSummaryFlags(AS->flags(), shouldImportValueAsDecl(AS)));
4762 auto AliaseeValueId = SummaryToValueIdMap[&AS->getAliasee()];
4763 assert(AliaseeValueId);
4764 NameVals.push_back(AliaseeValueId);
4765
4766 // Emit the finished record.
4767 Stream.EmitRecord(bitc::FS_COMBINED_ALIAS, NameVals, FSAliasAbbrev);
4768 NameVals.clear();
4769 MaybeEmitOriginalName(*AS);
4770
4771 if (auto *FS = dyn_cast<FunctionSummary>(&AS->getAliasee()))
4772 getReferencedTypeIds(FS, ReferencedTypeIds);
4773 }
4774
4775 if (!Index.cfiFunctionDefs().empty()) {
4776 for (auto &S : Index.cfiFunctionDefs()) {
4777 if (DefOrUseGUIDs.count(
4779 NameVals.push_back(StrtabBuilder.add(S));
4780 NameVals.push_back(S.size());
4781 }
4782 }
4783 if (!NameVals.empty()) {
4784 Stream.EmitRecord(bitc::FS_CFI_FUNCTION_DEFS, NameVals);
4785 NameVals.clear();
4786 }
4787 }
4788
4789 if (!Index.cfiFunctionDecls().empty()) {
4790 for (auto &S : Index.cfiFunctionDecls()) {
4791 if (DefOrUseGUIDs.count(
4793 NameVals.push_back(StrtabBuilder.add(S));
4794 NameVals.push_back(S.size());
4795 }
4796 }
4797 if (!NameVals.empty()) {
4798 Stream.EmitRecord(bitc::FS_CFI_FUNCTION_DECLS, NameVals);
4799 NameVals.clear();
4800 }
4801 }
4802
4803 // Walk the GUIDs that were referenced, and write the
4804 // corresponding type id records.
4805 for (auto &T : ReferencedTypeIds) {
4806 auto TidIter = Index.typeIds().equal_range(T);
4807 for (auto It = TidIter.first; It != TidIter.second; ++It) {
4808 writeTypeIdSummaryRecord(NameVals, StrtabBuilder, It->second.first,
4809 It->second.second);
4810 Stream.EmitRecord(bitc::FS_TYPE_ID, NameVals);
4811 NameVals.clear();
4812 }
4813 }
4814
4815 if (Index.getBlockCount())
4817 ArrayRef<uint64_t>{Index.getBlockCount()});
4818
4819 Stream.ExitBlock();
4820}
4821
4822/// Create the "IDENTIFICATION_BLOCK_ID" containing a single string with the
4823/// current llvm version, and a record for the epoch number.
4826
4827 // Write the "user readable" string identifying the bitcode producer
4828 auto Abbv = std::make_shared<BitCodeAbbrev>();
4832 auto StringAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4834 "LLVM" LLVM_VERSION_STRING, StringAbbrev);
4835
4836 // Write the epoch version
4837 Abbv = std::make_shared<BitCodeAbbrev>();
4840 auto EpochAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4841 constexpr std::array<unsigned, 1> Vals = {{bitc::BITCODE_CURRENT_EPOCH}};
4842 Stream.EmitRecord(bitc::IDENTIFICATION_CODE_EPOCH, Vals, EpochAbbrev);
4843 Stream.ExitBlock();
4844}
4845
4846void ModuleBitcodeWriter::writeModuleHash(StringRef View) {
4847 // Emit the module's hash.
4848 // MODULE_CODE_HASH: [5*i32]
4849 if (GenerateHash) {
4850 uint32_t Vals[5];
4851 Hasher.update(ArrayRef<uint8_t>(
4852 reinterpret_cast<const uint8_t *>(View.data()), View.size()));
4853 std::array<uint8_t, 20> Hash = Hasher.result();
4854 for (int Pos = 0; Pos < 20; Pos += 4) {
4855 Vals[Pos / 4] = support::endian::read32be(Hash.data() + Pos);
4856 }
4857
4858 // Emit the finished record.
4859 Stream.EmitRecord(bitc::MODULE_CODE_HASH, Vals);
4860
4861 if (ModHash)
4862 // Save the written hash value.
4863 llvm::copy(Vals, std::begin(*ModHash));
4864 }
4865}
4866
4867void ModuleBitcodeWriter::write() {
4869
4871 // We will want to write the module hash at this point. Block any flushing so
4872 // we can have access to the whole underlying data later.
4873 Stream.markAndBlockFlushing();
4874
4875 writeModuleVersion();
4876
4877 // Emit blockinfo, which defines the standard abbreviations etc.
4878 writeBlockInfo();
4879
4880 // Emit information describing all of the types in the module.
4881 writeTypeTable();
4882
4883 // Emit information about attribute groups.
4884 writeAttributeGroupTable();
4885
4886 // Emit information about parameter attributes.
4887 writeAttributeTable();
4888
4889 writeComdats();
4890
4891 // Emit top-level description of module, including target triple, inline asm,
4892 // descriptors for global variables, and function prototype info.
4893 writeModuleInfo();
4894
4895 // Emit constants.
4896 writeModuleConstants();
4897
4898 // Emit metadata kind names.
4899 writeModuleMetadataKinds();
4900
4901 // Emit metadata.
4902 writeModuleMetadata();
4903
4904 // Emit module-level use-lists.
4906 writeUseListBlock(nullptr);
4907
4908 writeOperandBundleTags();
4909 writeSyncScopeNames();
4910
4911 // Emit function bodies.
4912 DenseMap<const Function *, uint64_t> FunctionToBitcodeIndex;
4913 for (const Function &F : M)
4914 if (!F.isDeclaration())
4915 writeFunction(F, FunctionToBitcodeIndex);
4916
4917 // Need to write after the above call to WriteFunction which populates
4918 // the summary information in the index.
4919 if (Index)
4920 writePerModuleGlobalValueSummary();
4921
4922 writeGlobalValueSymbolTable(FunctionToBitcodeIndex);
4923
4924 writeModuleHash(Stream.getMarkedBufferAndResumeFlushing());
4925
4926 Stream.ExitBlock();
4927}
4928
4930 uint32_t &Position) {
4931 support::endian::write32le(&Buffer[Position], Value);
4932 Position += 4;
4933}
4934
4935/// If generating a bc file on darwin, we have to emit a
4936/// header and trailer to make it compatible with the system archiver. To do
4937/// this we emit the following header, and then emit a trailer that pads the
4938/// file out to be a multiple of 16 bytes.
4939///
4940/// struct bc_header {
4941/// uint32_t Magic; // 0x0B17C0DE
4942/// uint32_t Version; // Version, currently always 0.
4943/// uint32_t BitcodeOffset; // Offset to traditional bitcode file.
4944/// uint32_t BitcodeSize; // Size of traditional bitcode file.
4945/// uint32_t CPUType; // CPU specifier.
4946/// ... potentially more later ...
4947/// };
4949 const Triple &TT) {
4950 unsigned CPUType = ~0U;
4951
4952 // Match x86_64-*, i[3-9]86-*, powerpc-*, powerpc64-*, arm-*, thumb-*,
4953 // armv[0-9]-*, thumbv[0-9]-*, armv5te-*, or armv6t2-*. The CPUType is a magic
4954 // number from /usr/include/mach/machine.h. It is ok to reproduce the
4955 // specific constants here because they are implicitly part of the Darwin ABI.
4956 enum {
4957 DARWIN_CPU_ARCH_ABI64 = 0x01000000,
4958 DARWIN_CPU_TYPE_X86 = 7,
4959 DARWIN_CPU_TYPE_ARM = 12,
4960 DARWIN_CPU_TYPE_POWERPC = 18
4961 };
4962
4963 Triple::ArchType Arch = TT.getArch();
4964 if (Arch == Triple::x86_64)
4965 CPUType = DARWIN_CPU_TYPE_X86 | DARWIN_CPU_ARCH_ABI64;
4966 else if (Arch == Triple::x86)
4967 CPUType = DARWIN_CPU_TYPE_X86;
4968 else if (Arch == Triple::ppc)
4969 CPUType = DARWIN_CPU_TYPE_POWERPC;
4970 else if (Arch == Triple::ppc64)
4971 CPUType = DARWIN_CPU_TYPE_POWERPC | DARWIN_CPU_ARCH_ABI64;
4972 else if (Arch == Triple::arm || Arch == Triple::thumb)
4973 CPUType = DARWIN_CPU_TYPE_ARM;
4974
4975 // Traditional Bitcode starts after header.
4976 assert(Buffer.size() >= BWH_HeaderSize &&
4977 "Expected header size to be reserved");
4978 unsigned BCOffset = BWH_HeaderSize;
4979 unsigned BCSize = Buffer.size() - BWH_HeaderSize;
4980
4981 // Write the magic and version.
4982 unsigned Position = 0;
4983 writeInt32ToBuffer(0x0B17C0DE, Buffer, Position);
4984 writeInt32ToBuffer(0, Buffer, Position); // Version.
4985 writeInt32ToBuffer(BCOffset, Buffer, Position);
4986 writeInt32ToBuffer(BCSize, Buffer, Position);
4987 writeInt32ToBuffer(CPUType, Buffer, Position);
4988
4989 // If the file is not a multiple of 16 bytes, insert dummy padding.
4990 while (Buffer.size() & 15)
4991 Buffer.push_back(0);
4992}
4993
4994/// Helper to write the header common to all bitcode files.
4996 // Emit the file header.
4997 Stream.Emit((unsigned)'B', 8);
4998 Stream.Emit((unsigned)'C', 8);
4999 Stream.Emit(0x0, 4);
5000 Stream.Emit(0xC, 4);
5001 Stream.Emit(0xE, 4);
5002 Stream.Emit(0xD, 4);
5003}
5004
5006 : Stream(new BitstreamWriter(Buffer)) {
5007 writeBitcodeHeader(*Stream);
5008}
5009
5011 : Stream(new BitstreamWriter(FS, FlushThreshold)) {
5012 writeBitcodeHeader(*Stream);
5013}
5014
5016
5017void BitcodeWriter::writeBlob(unsigned Block, unsigned Record, StringRef Blob) {
5018 Stream->EnterSubblock(Block, 3);
5019
5020 auto Abbv = std::make_shared<BitCodeAbbrev>();
5021 Abbv->Add(BitCodeAbbrevOp(Record));
5023 auto AbbrevNo = Stream->EmitAbbrev(std::move(Abbv));
5024
5025 Stream->EmitRecordWithBlob(AbbrevNo, ArrayRef<uint64_t>{Record}, Blob);
5026
5027 Stream->ExitBlock();
5028}
5029
5031 assert(!WroteStrtab && !WroteSymtab);
5032
5033 // If any module has module-level inline asm, we will require a registered asm
5034 // parser for the target so that we can create an accurate symbol table for
5035 // the module.
5036 for (Module *M : Mods) {
5037 if (M->getModuleInlineAsm().empty())
5038 continue;
5039
5040 std::string Err;
5041 const Triple TT(M->getTargetTriple());
5042 const Target *T = TargetRegistry::lookupTarget(TT.str(), Err);
5043 if (!T || !T->hasMCAsmParser())
5044 return;
5045 }
5046
5047 WroteSymtab = true;
5048 SmallVector<char, 0> Symtab;
5049 // The irsymtab::build function may be unable to create a symbol table if the
5050 // module is malformed (e.g. it contains an invalid alias). Writing a symbol
5051 // table is not required for correctness, but we still want to be able to
5052 // write malformed modules to bitcode files, so swallow the error.
5053 if (Error E = irsymtab::build(Mods, Symtab, StrtabBuilder, Alloc)) {
5054 consumeError(std::move(E));
5055 return;
5056 }
5057
5059 {Symtab.data(), Symtab.size()});
5060}
5061
5063 assert(!WroteStrtab);
5064
5065 std::vector<char> Strtab;
5066 StrtabBuilder.finalizeInOrder();
5067 Strtab.resize(StrtabBuilder.getSize());
5068 StrtabBuilder.write((uint8_t *)Strtab.data());
5069
5071 {Strtab.data(), Strtab.size()});
5072
5073 WroteStrtab = true;
5074}
5075
5077 writeBlob(bitc::STRTAB_BLOCK_ID, bitc::STRTAB_BLOB, Strtab);
5078 WroteStrtab = true;
5079}
5080
5082 bool ShouldPreserveUseListOrder,
5084 bool GenerateHash, ModuleHash *ModHash) {
5085 assert(!WroteStrtab);
5086
5087 // The Mods vector is used by irsymtab::build, which requires non-const
5088 // Modules in case it needs to materialize metadata. But the bitcode writer
5089 // requires that the module is materialized, so we can cast to non-const here,
5090 // after checking that it is in fact materialized.
5091 assert(M.isMaterialized());
5092 Mods.push_back(const_cast<Module *>(&M));
5093
5094 ModuleBitcodeWriter ModuleWriter(M, StrtabBuilder, *Stream,
5095 ShouldPreserveUseListOrder, Index,
5096 GenerateHash, ModHash);
5097 ModuleWriter.write();
5098}
5099
5102 const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex,
5103 const GVSummaryPtrSet *DecSummaries) {
5104 IndexBitcodeWriter IndexWriter(*Stream, StrtabBuilder, *Index, DecSummaries,
5105 ModuleToSummariesForIndex);
5106 IndexWriter.write();
5107}
5108
5109/// Write the specified module to the specified output stream.
5111 bool ShouldPreserveUseListOrder,
5113 bool GenerateHash, ModuleHash *ModHash) {
5114 auto Write = [&](BitcodeWriter &Writer) {
5115 Writer.writeModule(M, ShouldPreserveUseListOrder, Index, GenerateHash,
5116 ModHash);
5117 Writer.writeSymtab();
5118 Writer.writeStrtab();
5119 };
5120 Triple TT(M.getTargetTriple());
5121 if (TT.isOSDarwin() || TT.isOSBinFormatMachO()) {
5122 // If this is darwin or another generic macho target, reserve space for the
5123 // header. Note that the header is computed *after* the output is known, so
5124 // we currently explicitly use a buffer, write to it, and then subsequently
5125 // flush to Out.
5126 SmallVector<char, 0> Buffer;
5127 Buffer.reserve(256 * 1024);
5128 Buffer.insert(Buffer.begin(), BWH_HeaderSize, 0);
5129 BitcodeWriter Writer(Buffer);
5130 Write(Writer);
5131 emitDarwinBCHeaderAndTrailer(Buffer, TT);
5132 Out.write(Buffer.data(), Buffer.size());
5133 } else {
5134 BitcodeWriter Writer(Out);
5135 Write(Writer);
5136 }
5137}
5138
5139void IndexBitcodeWriter::write() {
5141
5142 writeModuleVersion();
5143
5144 // Write the module paths in the combined index.
5145 writeModStrings();
5146
5147 // Write the summary combined index records.
5148 writeCombinedGlobalValueSummary();
5149
5150 Stream.ExitBlock();
5151}
5152
5153// Write the specified module summary index to the given raw output stream,
5154// where it will be written in a new bitcode block. This is used when
5155// writing the combined index file for ThinLTO. When writing a subset of the
5156// index for a distributed backend, provide a \p ModuleToSummariesForIndex map.
5159 const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex,
5160 const GVSummaryPtrSet *DecSummaries) {
5161 SmallVector<char, 0> Buffer;
5162 Buffer.reserve(256 * 1024);
5163
5164 BitcodeWriter Writer(Buffer);
5165 Writer.writeIndex(&Index, ModuleToSummariesForIndex, DecSummaries);
5166 Writer.writeStrtab();
5167
5168 Out.write((char *)&Buffer.front(), Buffer.size());
5169}
5170
5171namespace {
5172
5173/// Class to manage the bitcode writing for a thin link bitcode file.
5174class ThinLinkBitcodeWriter : public ModuleBitcodeWriterBase {
5175 /// ModHash is for use in ThinLTO incremental build, generated while writing
5176 /// the module bitcode file.
5177 const ModuleHash *ModHash;
5178
5179public:
5180 ThinLinkBitcodeWriter(const Module &M, StringTableBuilder &StrtabBuilder,
5181 BitstreamWriter &Stream,
5183 const ModuleHash &ModHash)
5184 : ModuleBitcodeWriterBase(M, StrtabBuilder, Stream,
5185 /*ShouldPreserveUseListOrder=*/false, &Index),
5186 ModHash(&ModHash) {}
5187
5188 void write();
5189
5190private:
5191 void writeSimplifiedModuleInfo();
5192};
5193
5194} // end anonymous namespace
5195
5196// This function writes a simpilified module info for thin link bitcode file.
5197// It only contains the source file name along with the name(the offset and
5198// size in strtab) and linkage for global values. For the global value info
5199// entry, in order to keep linkage at offset 5, there are three zeros used
5200// as padding.
5201void ThinLinkBitcodeWriter::writeSimplifiedModuleInfo() {
5203 // Emit the module's source file name.
5204 {
5205 StringEncoding Bits = getStringEncoding(M.getSourceFileName());
5207 if (Bits == SE_Char6)
5208 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Char6);
5209 else if (Bits == SE_Fixed7)
5210 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7);
5211
5212 // MODULE_CODE_SOURCE_FILENAME: [namechar x N]
5213 auto Abbv = std::make_shared<BitCodeAbbrev>();
5216 Abbv->Add(AbbrevOpToUse);
5217 unsigned FilenameAbbrev = Stream.EmitAbbrev(std::move(Abbv));
5218
5219 for (const auto P : M.getSourceFileName())
5220 Vals.push_back((unsigned char)P);
5221
5222 Stream.EmitRecord(bitc::MODULE_CODE_SOURCE_FILENAME, Vals, FilenameAbbrev);
5223 Vals.clear();
5224 }
5225
5226 // Emit the global variable information.
5227 for (const GlobalVariable &GV : M.globals()) {
5228 // GLOBALVAR: [strtab offset, strtab size, 0, 0, 0, linkage]
5229 Vals.push_back(StrtabBuilder.add(GV.getName()));
5230 Vals.push_back(GV.getName().size());
5231 Vals.push_back(0);
5232 Vals.push_back(0);
5233 Vals.push_back(0);
5234 Vals.push_back(getEncodedLinkage(GV));
5235
5237 Vals.clear();
5238 }
5239
5240 // Emit the function proto information.
5241 for (const Function &F : M) {
5242 // FUNCTION: [strtab offset, strtab size, 0, 0, 0, linkage]
5243 Vals.push_back(StrtabBuilder.add(F.getName()));
5244 Vals.push_back(F.getName().size());
5245 Vals.push_back(0);
5246 Vals.push_back(0);
5247 Vals.push_back(0);
5249
5251 Vals.clear();
5252 }
5253
5254 // Emit the alias information.
5255 for (const GlobalAlias &A : M.aliases()) {
5256 // ALIAS: [strtab offset, strtab size, 0, 0, 0, linkage]
5257 Vals.push_back(StrtabBuilder.add(A.getName()));
5258 Vals.push_back(A.getName().size());
5259 Vals.push_back(0);
5260 Vals.push_back(0);
5261 Vals.push_back(0);
5263
5264 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals);
5265 Vals.clear();
5266 }
5267
5268 // Emit the ifunc information.
5269 for (const GlobalIFunc &I : M.ifuncs()) {
5270 // IFUNC: [strtab offset, strtab size, 0, 0, 0, linkage]
5271 Vals.push_back(StrtabBuilder.add(I.getName()));
5272 Vals.push_back(I.getName().size());
5273 Vals.push_back(0);
5274 Vals.push_back(0);
5275 Vals.push_back(0);
5277
5278 Stream.EmitRecord(bitc::MODULE_CODE_IFUNC, Vals);
5279 Vals.clear();
5280 }
5281}
5282
5283void ThinLinkBitcodeWriter::write() {
5285
5286 writeModuleVersion();
5287
5288 writeSimplifiedModuleInfo();
5289
5290 writePerModuleGlobalValueSummary();
5291
5292 // Write module hash.
5294
5295 Stream.ExitBlock();
5296}
5297
5300 const ModuleHash &ModHash) {
5301 assert(!WroteStrtab);
5302
5303 // The Mods vector is used by irsymtab::build, which requires non-const
5304 // Modules in case it needs to materialize metadata. But the bitcode writer
5305 // requires that the module is materialized, so we can cast to non-const here,
5306 // after checking that it is in fact materialized.
5307 assert(M.isMaterialized());
5308 Mods.push_back(const_cast<Module *>(&M));
5309
5310 ThinLinkBitcodeWriter ThinLinkWriter(M, StrtabBuilder, *Stream, Index,
5311 ModHash);
5312 ThinLinkWriter.write();
5313}
5314
5315// Write the specified thin link bitcode file to the given raw output stream,
5316// where it will be written in a new bitcode block. This is used when
5317// writing the per-module index file for ThinLTO.
5320 const ModuleHash &ModHash) {
5321 SmallVector<char, 0> Buffer;
5322 Buffer.reserve(256 * 1024);
5323
5324 BitcodeWriter Writer(Buffer);
5325 Writer.writeThinLinkBitcode(M, Index, ModHash);
5326 Writer.writeSymtab();
5327 Writer.writeStrtab();
5328
5329 Out.write((char *)&Buffer.front(), Buffer.size());
5330}
5331
5332static const char *getSectionNameForBitcode(const Triple &T) {
5333 switch (T.getObjectFormat()) {
5334 case Triple::MachO:
5335 return "__LLVM,__bitcode";
5336 case Triple::COFF:
5337 case Triple::ELF:
5338 case Triple::Wasm:
5340 return ".llvmbc";
5341 case Triple::GOFF:
5342 llvm_unreachable("GOFF is not yet implemented");
5343 break;
5344 case Triple::SPIRV:
5345 if (T.getVendor() == Triple::AMD)
5346 return ".llvmbc";
5347 llvm_unreachable("SPIRV is not yet implemented");
5348 break;
5349 case Triple::XCOFF:
5350 llvm_unreachable("XCOFF is not yet implemented");
5351 break;
5353 llvm_unreachable("DXContainer is not yet implemented");
5354 break;
5355 }
5356 llvm_unreachable("Unimplemented ObjectFormatType");
5357}
5358
5359static const char *getSectionNameForCommandline(const Triple &T) {
5360 switch (T.getObjectFormat()) {
5361 case Triple::MachO:
5362 return "__LLVM,__cmdline";
5363 case Triple::COFF:
5364 case Triple::ELF:
5365 case Triple::Wasm:
5367 return ".llvmcmd";
5368 case Triple::GOFF:
5369 llvm_unreachable("GOFF is not yet implemented");
5370 break;
5371 case Triple::SPIRV:
5372 if (T.getVendor() == Triple::AMD)
5373 return ".llvmcmd";
5374 llvm_unreachable("SPIRV is not yet implemented");
5375 break;
5376 case Triple::XCOFF:
5377 llvm_unreachable("XCOFF is not yet implemented");
5378 break;
5380 llvm_unreachable("DXC is not yet implemented");
5381 break;
5382 }
5383 llvm_unreachable("Unimplemented ObjectFormatType");
5384}
5385
5387 bool EmbedBitcode, bool EmbedCmdline,
5388 const std::vector<uint8_t> &CmdArgs) {
5389 // Save llvm.compiler.used and remove it.
5392 Type *UsedElementType = PointerType::getUnqual(M.getContext());
5393 GlobalVariable *Used = collectUsedGlobalVariables(M, UsedGlobals, true);
5394 for (auto *GV : UsedGlobals) {
5395 if (GV->getName() != "llvm.embedded.module" &&
5396 GV->getName() != "llvm.cmdline")
5397 UsedArray.push_back(
5399 }
5400 if (Used)
5401 Used->eraseFromParent();
5402
5403 // Embed the bitcode for the llvm module.
5404 std::string Data;
5405 ArrayRef<uint8_t> ModuleData;
5406 Triple T(M.getTargetTriple());
5407
5408 if (EmbedBitcode) {
5409 if (Buf.getBufferSize() == 0 ||
5410 !isBitcode((const unsigned char *)Buf.getBufferStart(),
5411 (const unsigned char *)Buf.getBufferEnd())) {
5412 // If the input is LLVM Assembly, bitcode is produced by serializing
5413 // the module. Use-lists order need to be preserved in this case.
5415 llvm::WriteBitcodeToFile(M, OS, /* ShouldPreserveUseListOrder */ true);
5416 ModuleData =
5417 ArrayRef<uint8_t>((const uint8_t *)OS.str().data(), OS.str().size());
5418 } else
5419 // If the input is LLVM bitcode, write the input byte stream directly.
5420 ModuleData = ArrayRef<uint8_t>((const uint8_t *)Buf.getBufferStart(),
5421 Buf.getBufferSize());
5422 }
5423 llvm::Constant *ModuleConstant =
5424 llvm::ConstantDataArray::get(M.getContext(), ModuleData);
5426 M, ModuleConstant->getType(), true, llvm::GlobalValue::PrivateLinkage,
5427 ModuleConstant);
5429 // Set alignment to 1 to prevent padding between two contributions from input
5430 // sections after linking.
5431 GV->setAlignment(Align(1));
5432 UsedArray.push_back(
5434 if (llvm::GlobalVariable *Old =
5435 M.getGlobalVariable("llvm.embedded.module", true)) {
5436 assert(Old->hasZeroLiveUses() &&
5437 "llvm.embedded.module can only be used once in llvm.compiler.used");
5438 GV->takeName(Old);
5439 Old->eraseFromParent();
5440 } else {
5441 GV->setName("llvm.embedded.module");
5442 }
5443
5444 // Skip if only bitcode needs to be embedded.
5445 if (EmbedCmdline) {
5446 // Embed command-line options.
5447 ArrayRef<uint8_t> CmdData(const_cast<uint8_t *>(CmdArgs.data()),
5448 CmdArgs.size());
5449 llvm::Constant *CmdConstant =
5450 llvm::ConstantDataArray::get(M.getContext(), CmdData);
5451 GV = new llvm::GlobalVariable(M, CmdConstant->getType(), true,
5453 CmdConstant);
5455 GV->setAlignment(Align(1));
5456 UsedArray.push_back(
5458 if (llvm::GlobalVariable *Old = M.getGlobalVariable("llvm.cmdline", true)) {
5459 assert(Old->hasZeroLiveUses() &&
5460 "llvm.cmdline can only be used once in llvm.compiler.used");
5461 GV->takeName(Old);
5462 Old->eraseFromParent();
5463 } else {
5464 GV->setName("llvm.cmdline");
5465 }
5466 }
5467
5468 if (UsedArray.empty())
5469 return;
5470
5471 // Recreate llvm.compiler.used.
5472 ArrayType *ATy = ArrayType::get(UsedElementType, UsedArray.size());
5473 auto *NewUsed = new GlobalVariable(
5475 llvm::ConstantArray::get(ATy, UsedArray), "llvm.compiler.used");
5476 NewUsed->setSection("llvm.metadata");
5477}
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:2352
static void writeDIGlobalVariableExpression(raw_ostream &Out, const DIGlobalVariableExpression *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2499
static void writeDICompositeType(raw_ostream &Out, const DICompositeType *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2190
static void writeDIStringType(raw_ostream &Out, const DIStringType *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2139
static void writeDIGlobalVariable(raw_ostream &Out, const DIGlobalVariable *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2413
static void writeDIModule(raw_ostream &Out, const DIModule *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2373
static void writeDIFile(raw_ostream &Out, const DIFile *N, AsmWriterContext &)
Definition: AsmWriter.cpp:2234
static void writeDISubroutineType(raw_ostream &Out, const DISubroutineType *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2223
static void writeDILabel(raw_ostream &Out, const DILabel *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2448
static void writeDIDerivedType(raw_ostream &Out, const DIDerivedType *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2157
static void writeDIImportedEntity(raw_ostream &Out, const DIImportedEntity *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2523
static void writeDIObjCProperty(raw_ostream &Out, const DIObjCProperty *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2509
static void writeDISubprogram(raw_ostream &Out, const DISubprogram *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2279
static void writeDILocation(raw_ostream &Out, const DILocation *DL, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2000
static void writeDINamespace(raw_ostream &Out, const DINamespace *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2330
static void writeDICommonBlock(raw_ostream &Out, const DICommonBlock *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2340
static void writeDIBasicType(raw_ostream &Out, const DIBasicType *N, AsmWriterContext &)
Definition: AsmWriter.cpp:2124
static void writeGenericDINode(raw_ostream &Out, const GenericDINode *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:1982
static void writeDILocalVariable(raw_ostream &Out, const DILocalVariable *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2432
static void writeDITemplateTypeParameter(raw_ostream &Out, const DITemplateTypeParameter *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2388
static void writeDICompileUnit(raw_ostream &Out, const DICompileUnit *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2249
static void writeDIGenericSubrange(raw_ostream &Out, const DIGenericSubrange *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2061
static void writeDISubrange(raw_ostream &Out, const DISubrange *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2020
static void writeDILexicalBlockFile(raw_ostream &Out, const DILexicalBlockFile *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2318
static void writeDIEnumerator(raw_ostream &Out, const DIEnumerator *N, AsmWriterContext &)
Definition: AsmWriter.cpp:2112
static void writeMDTuple(raw_ostream &Out, const MDTuple *Node, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:1761
static void writeDIExpression(raw_ostream &Out, const DIExpression *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2459
static void writeDIAssignID(raw_ostream &Out, const DIAssignID *DL, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2014
static void writeDILexicalBlock(raw_ostream &Out, const DILexicalBlock *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2307
static void writeDIArgList(raw_ostream &Out, const DIArgList *N, AsmWriterContext &WriterCtx, bool FromValue=false)
Definition: AsmWriter.cpp:2484
static void writeDITemplateValueParameter(raw_ostream &Out, const DITemplateValueParameter *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2399
static void writeDIMacroFile(raw_ostream &Out, const DIMacroFile *N, AsmWriterContext &WriterCtx)
Definition: AsmWriter.cpp:2363
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:647
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:1084
static Constant * getPointerBitCastOrAddrSpaceCast(Constant *C, Type *Ty)
Create a BitCast or AddrSpaceCast for a pointer type depending on the address space.
Definition: Constants.cpp:2230
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:145
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:220
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:1477
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:1067
Tracking metadata reference owned by Metadata.
Definition: Metadata.h:889
Tuple of metadata.
Definition: Metadata.h:1470
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:1729
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:479
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:94
size_t size() const
Definition: SmallVector.h:91
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
void assign(size_type NumElts, ValueParamT Elt)
Definition: SmallVector.h:717
void reserve(size_type N)
Definition: SmallVector.h:676
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:696
iterator insert(iterator I, T &&Elt)
Definition: SmallVector.h:818
void resize(size_type N)
Definition: SmallVector.h:651
void push_back(const T &Elt)
Definition: SmallVector.h:426
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:299
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
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:160
bool isFloatTy() const
Return true if this is 'float', a 32-bit IEEE fp type.
Definition: Type.h:154
bool isBFloatTy() const
Return true if this is 'bfloat', a 16-bit bfloat type.
Definition: Type.h:146
@ X86_MMXTyID
MMX vectors (64 bits, X86 specific)
Definition: Type.h:66
@ X86_AMXTyID
AMX vectors (8192 bits, X86 specific)
Definition: Type.h:67
@ FunctionTyID
Functions.
Definition: Type.h:72
@ ArrayTyID
Arrays.
Definition: Type.h:75
@ TypedPointerTyID
Typed pointer used by some GPU targets.
Definition: Type.h:78
@ HalfTyID
16-bit floating point type
Definition: Type.h:56
@ TargetExtTyID
Target extension type.
Definition: Type.h:79
@ VoidTyID
type with no size
Definition: Type.h:63
@ ScalableVectorTyID
Scalable SIMD vector type.
Definition: Type.h:77
@ LabelTyID
Labels.
Definition: Type.h:64
@ FloatTyID
32-bit floating point type
Definition: Type.h:58
@ StructTyID
Structures.
Definition: Type.h:74
@ IntegerTyID
Arbitrary bit width integers.
Definition: Type.h:71
@ FixedVectorTyID
Fixed width SIMD vector type.
Definition: Type.h:76
@ 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:68
@ PointerTyID
Pointers.
Definition: Type.h:73
@ 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:166
bool isFP128Ty() const
Return true if this is 'fp128'.
Definition: Type.h:163
bool isHalfTy() const
Return true if this is 'half', a 16-bit IEEE fp type.
Definition: Type.h:143
bool isDoubleTy() const
Return true if this is 'double', a 64-bit IEEE fp type.
Definition: Type.h:157
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition: Type.h:348
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:811
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
const uint64_t Version
Definition: InstrProf.h:1107
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
@ 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
@ BITCODE_CURRENT_EPOCH
Definition: LLVMBitCodes.h:81
@ 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:766
@ COMDAT_SELECTION_KIND_ANY
Definition: LLVMBitCodes.h:764
@ COMDAT_SELECTION_KIND_SAME_SIZE
Definition: LLVMBitCodes.h:768
@ COMDAT_SELECTION_KIND_EXACT_MATCH
Definition: LLVMBitCodes.h:765
@ COMDAT_SELECTION_KIND_NO_DUPLICATES
Definition: LLVMBitCodes.h:767
@ 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_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:372
constexpr double e
Definition: MathExtras.h:47
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 are tuples (A,...
Definition: STLExtras.h:2400
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:2067
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:853
#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,...