LLVM 24.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 "BitWriterOptions.h"
15#include "ValueEnumerator.h"
16#include "llvm/ADT/APFloat.h"
17#include "llvm/ADT/APInt.h"
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/DenseMap.h"
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/SetVector.h"
25#include "llvm/ADT/StringMap.h"
26#include "llvm/ADT/StringRef.h"
34#include "llvm/Config/llvm-config.h"
35#include "llvm/IR/Attributes.h"
36#include "llvm/IR/BasicBlock.h"
37#include "llvm/IR/Comdat.h"
38#include "llvm/IR/Constant.h"
40#include "llvm/IR/Constants.h"
42#include "llvm/IR/DebugLoc.h"
44#include "llvm/IR/Function.h"
45#include "llvm/IR/GlobalAlias.h"
46#include "llvm/IR/GlobalIFunc.h"
48#include "llvm/IR/GlobalValue.h"
50#include "llvm/IR/InlineAsm.h"
51#include "llvm/IR/InstrTypes.h"
52#include "llvm/IR/Instruction.h"
54#include "llvm/IR/LLVMContext.h"
55#include "llvm/IR/Metadata.h"
56#include "llvm/IR/Module.h"
58#include "llvm/IR/Operator.h"
59#include "llvm/IR/Type.h"
61#include "llvm/IR/Value.h"
72#include "llvm/Support/Endian.h"
73#include "llvm/Support/Error.h"
76#include "llvm/Support/SHA1.h"
79#include <algorithm>
80#include <cassert>
81#include <cstddef>
82#include <cstdint>
83#include <iterator>
84#include <map>
85#include <memory>
86#include <optional>
87#include <string>
88#include <utility>
89#include <vector>
90
91using namespace llvm;
92using namespace llvm::memprof;
93
94// Since we only use the context information in the memprof summary records in
95// the LTO backends to do assertion checking, save time and space by only
96// serializing the context for non-NDEBUG builds.
97// TODO: Currently this controls writing context of the allocation info records,
98// which are larger and more expensive, but we should do this for the callsite
99// records as well.
100// FIXME: Convert to a const once this has undergone more sigificant testing.
101static cl::opt<bool>
102 CombinedIndexMemProfContext("combined-index-memprof-context", cl::Hidden,
103#ifdef NDEBUG
104 cl::init(false),
105#else
106 cl::init(true),
107#endif
108 cl::desc(""));
109
110namespace {
111
112/// These are manifest constants used by the bitcode writer. They do not need to
113/// be kept in sync with the reader, but need to be consistent within this file.
114enum {
115 // VALUE_SYMTAB_BLOCK abbrev id's.
116 VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
117 VST_ENTRY_7_ABBREV,
118 VST_ENTRY_6_ABBREV,
119 VST_BBENTRY_6_ABBREV,
120
121 // CONSTANTS_BLOCK abbrev id's.
122 CONSTANTS_SETTYPE_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
123 CONSTANTS_INTEGER_ABBREV,
124 CONSTANTS_BYTE_ABBREV,
125 CONSTANTS_CE_CAST_Abbrev,
126 CONSTANTS_NULL_Abbrev,
127
128 // FUNCTION_BLOCK abbrev id's.
129 FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
130 FUNCTION_INST_STORE_ABBREV,
131 FUNCTION_INST_UNOP_ABBREV,
132 FUNCTION_INST_UNOP_FLAGS_ABBREV,
133 FUNCTION_INST_BINOP_ABBREV,
134 FUNCTION_INST_BINOP_FLAGS_ABBREV,
135 FUNCTION_INST_CAST_ABBREV,
136 FUNCTION_INST_CAST_FLAGS_ABBREV,
137 FUNCTION_INST_RET_VOID_ABBREV,
138 FUNCTION_INST_RET_VAL_ABBREV,
139 FUNCTION_INST_BR_UNCOND_ABBREV,
140 FUNCTION_INST_BR_COND_ABBREV,
141 FUNCTION_INST_UNREACHABLE_ABBREV,
142 FUNCTION_INST_GEP_ABBREV,
143 FUNCTION_INST_CMP_ABBREV,
144 FUNCTION_INST_CMP_FLAGS_ABBREV,
145 FUNCTION_DEBUG_RECORD_VALUE_ABBREV,
146 FUNCTION_DEBUG_LOC_ABBREV,
147 FUNCTION_DEBUG_LOC_LAYERS_ABBREV,
148};
149
150/// Abstract class to manage the bitcode writing, subclassed for each bitcode
151/// file type.
152class BitcodeWriterBase {
153protected:
154 /// The stream created and owned by the client.
155 BitstreamWriter &Stream;
156
157 StringTableBuilder &StrtabBuilder;
158
159public:
160 /// Constructs a BitcodeWriterBase object that writes to the provided
161 /// \p Stream.
162 BitcodeWriterBase(BitstreamWriter &Stream, StringTableBuilder &StrtabBuilder)
163 : Stream(Stream), StrtabBuilder(StrtabBuilder) {}
164
165protected:
166 void writeModuleVersion();
167};
168
169void BitcodeWriterBase::writeModuleVersion() {
170 // VERSION: [version#]
171 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, ArrayRef<uint64_t>{2});
172}
173
174/// Base class to manage the module bitcode writing, currently subclassed for
175/// ModuleBitcodeWriter and ThinLinkBitcodeWriter.
176class ModuleBitcodeWriterBase : public BitcodeWriterBase {
177protected:
178 const BitWriterOptions &Opts;
179
180 /// The Module to write to bitcode.
181 const Module &M;
182
183 /// Enumerates ids for all values in the module.
184 ValueEnumerator VE;
185
186 /// Optional per-module index to write for ThinLTO.
187 const ModuleSummaryIndex *Index;
188
189 /// Map that holds the correspondence between GUIDs in the summary index,
190 /// that came from indirect call profiles, and a value id generated by this
191 /// class to use in the VST and summary block records.
192 std::map<GlobalValue::GUID, unsigned> GUIDToValueIdMap;
193
194 /// Tracks the last value id recorded in the GUIDToValueMap.
195 unsigned GlobalValueId;
196
197 /// Saves the offset of the VSTOffset record that must eventually be
198 /// backpatched with the offset of the actual VST.
199 uint64_t VSTOffsetPlaceholder = 0;
200
201public:
202 /// Constructs a ModuleBitcodeWriterBase object for the given Module,
203 /// writing to the provided \p Buffer.
204 ModuleBitcodeWriterBase(const Module &M, StringTableBuilder &StrtabBuilder,
205 BitstreamWriter &Stream,
206 bool ShouldPreserveUseListOrder,
207 const ModuleSummaryIndex *Index)
208 : BitcodeWriterBase(Stream, StrtabBuilder),
210 VE(M,
211 valueOr(Opts.preserve_bc_uselistorder, ShouldPreserveUseListOrder)),
212 Index(Index) {
213 // Assign ValueIds to any callee values in the index that came from
214 // indirect call profiles and were recorded as a GUID not a Value*
215 // (which would have been assigned an ID by the ValueEnumerator).
216 // The starting ValueId is just after the number of values in the
217 // ValueEnumerator, so that they can be emitted in the VST.
218 GlobalValueId = VE.getValues().size();
219 if (!Index)
220 return;
221 // Sort by GUID for deterministic value ID assignment.
222 for (const auto &GUIDSummaryLists :
223 Index->sortedGlobalValueSummariesRange())
224 // Examine all summaries for this GUID.
225 for (auto &Summary : GUIDSummaryLists.second.getSummaryList())
226 if (auto *FS = dyn_cast<FunctionSummary>(Summary.get())) {
227 // For each call in the function summary, see if the call
228 // is to a GUID (which means it is for an indirect call,
229 // otherwise we would have a Value for it). If so, synthesize
230 // a value id.
231 for (auto &CallEdge : FS->calls())
232 if (!CallEdge.first.haveGVs() || !CallEdge.first.getValue())
233 assignValueId(CallEdge.first.getGUID());
234
235 // For each referenced variables in the function summary, see if the
236 // variable is represented by a GUID (as opposed to a symbol to
237 // declarations or definitions in the module). If so, synthesize a
238 // value id.
239 for (auto &RefEdge : FS->refs())
240 if (!RefEdge.haveGVs() || !RefEdge.getValue())
241 assignValueId(RefEdge.getGUID());
242 }
243 }
244
245protected:
246 void writePerModuleGlobalValueSummary();
247 void writeGUIDList();
248
249private:
250 void writePerModuleFunctionSummaryRecord(
251 SmallVector<uint64_t, 64> &NameVals, GlobalValueSummary *Summary,
252 unsigned ValueID, unsigned FSCallsProfileAbbrev, unsigned CallsiteAbbrev,
253 unsigned AllocAbbrev, unsigned ContextIdAbbvId, const Function &F,
254 DenseMap<CallStackId, LinearCallStackId> &CallStackPos,
255 CallStackId &CallStackCount);
256 void writeModuleLevelReferences(const GlobalVariable &V,
257 SmallVector<uint64_t, 64> &NameVals,
258 unsigned FSModRefsAbbrev,
259 unsigned FSModVTableRefsAbbrev);
260
261 void assignValueId(GlobalValue::GUID ValGUID) {
262 GUIDToValueIdMap[ValGUID] = ++GlobalValueId;
263 }
264
265 unsigned getValueId(GlobalValue::GUID ValGUID) {
266 const auto &VMI = GUIDToValueIdMap.find(ValGUID);
267 // Expect that any GUID value had a value Id assigned by an
268 // earlier call to assignValueId.
269 assert(VMI != GUIDToValueIdMap.end() &&
270 "GUID does not have assigned value Id");
271 return VMI->second;
272 }
273
274 // Helper to get the valueId for the type of value recorded in VI.
275 unsigned getValueId(ValueInfo VI) {
276 if (!VI.haveGVs() || !VI.getValue())
277 return getValueId(VI.getGUID());
278 return VE.getValueID(VI.getValue());
279 }
280
281 std::map<GlobalValue::GUID, unsigned> &valueIds() { return GUIDToValueIdMap; }
282};
283
284/// Class to manage the bitcode writing for a module.
285class ModuleBitcodeWriter : public ModuleBitcodeWriterBase {
286 /// True if a module hash record should be written.
287 bool GenerateHash;
288
289 /// If non-null, when GenerateHash is true, the resulting hash is written
290 /// into ModHash.
291 ModuleHash *ModHash;
292
293 SHA1 Hasher;
294
295 /// The start bit of the identification block.
296 uint64_t BitcodeStartBit;
297
298 /// Abbrev for DILocations that carry an irlayers operand. Locations without
299 /// layers use the shorter abbrev threaded through writeDILocation's \p
300 /// Abbrev parameter, so they pay nothing for a field they do not use.
301 unsigned DILocationLayersAbbrev = 0;
302
303public:
304 /// Constructs a ModuleBitcodeWriter object for the given Module,
305 /// writing to the provided \p Buffer.
306 ModuleBitcodeWriter(const Module &M, StringTableBuilder &StrtabBuilder,
307 BitstreamWriter &Stream, bool ShouldPreserveUseListOrder,
308 const ModuleSummaryIndex *Index, bool GenerateHash,
309 ModuleHash *ModHash = nullptr)
310 : ModuleBitcodeWriterBase(M, StrtabBuilder, Stream,
311 ShouldPreserveUseListOrder, Index),
312 GenerateHash(GenerateHash), ModHash(ModHash),
313 BitcodeStartBit(Stream.GetCurrentBitNo()) {}
314
315 /// Emit the current module to the bitstream.
316 void write();
317
318private:
319 uint64_t bitcodeStartBit() { return BitcodeStartBit; }
320
321 size_t addToStrtab(StringRef Str);
322
323 void writeAttributeGroupTable();
324 void writeAttributeTable();
325 void writeTypeTable();
326 void writeComdats();
327 void writeValueSymbolTableForwardDecl();
328 void writeModuleInfo();
329 void writeValueAsMetadata(const ValueAsMetadata *MD,
330 SmallVectorImpl<uint64_t> &Record);
331 void writeMDTuple(const MDTuple *N, SmallVectorImpl<uint64_t> &Record,
332 unsigned Abbrev);
333 unsigned createDILocationAbbrev(bool WithIRLayers);
334 void writeDILocation(const DILocation *N, SmallVectorImpl<uint64_t> &Record,
335 unsigned &Abbrev);
336 void writeDILayerLoc(const DILayerLoc *N, SmallVectorImpl<uint64_t> &Record,
337 unsigned Abbrev);
338 void writeDILayerLocList(const DILayerLocList *N,
339 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
340 unsigned createGenericDINodeAbbrev();
341 void writeGenericDINode(const GenericDINode *N,
342 SmallVectorImpl<uint64_t> &Record, unsigned &Abbrev);
343 void writeDISubrange(const DISubrange *N, SmallVectorImpl<uint64_t> &Record,
344 unsigned Abbrev);
345 void writeDIGenericSubrange(const DIGenericSubrange *N,
346 SmallVectorImpl<uint64_t> &Record,
347 unsigned Abbrev);
348 void writeDIEnumerator(const DIEnumerator *N,
349 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
350 void writeDIBasicType(const DIBasicType *N, SmallVectorImpl<uint64_t> &Record,
351 unsigned Abbrev);
352 void writeDIFixedPointType(const DIFixedPointType *N,
353 SmallVectorImpl<uint64_t> &Record,
354 unsigned Abbrev);
355 void writeDIStringType(const DIStringType *N,
356 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
357 void writeDIDerivedType(const DIDerivedType *N,
358 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
359 void writeDISubrangeType(const DISubrangeType *N,
360 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
361 void writeDICompositeType(const DICompositeType *N,
362 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
363 void writeDISubroutineType(const DISubroutineType *N,
364 SmallVectorImpl<uint64_t> &Record,
365 unsigned Abbrev);
366 void writeDIFile(const DIFile *N, SmallVectorImpl<uint64_t> &Record,
367 unsigned Abbrev);
368 void writeDICompileUnit(const DICompileUnit *N,
369 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
370 void writeDISubprogram(const DISubprogram *N,
371 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
372 void writeDILexicalBlock(const DILexicalBlock *N,
373 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
374 void writeDILexicalBlockFile(const DILexicalBlockFile *N,
375 SmallVectorImpl<uint64_t> &Record,
376 unsigned Abbrev);
377 void writeDICommonBlock(const DICommonBlock *N,
378 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
379 void writeDINamespace(const DINamespace *N, SmallVectorImpl<uint64_t> &Record,
380 unsigned Abbrev);
381 void writeDIMacro(const DIMacro *N, SmallVectorImpl<uint64_t> &Record,
382 unsigned Abbrev);
383 void writeDIMacroFile(const DIMacroFile *N, SmallVectorImpl<uint64_t> &Record,
384 unsigned Abbrev);
385 void writeDIArgList(const DIArgList *N, SmallVectorImpl<uint64_t> &Record);
386 void writeDIModule(const DIModule *N, SmallVectorImpl<uint64_t> &Record,
387 unsigned Abbrev);
388 void writeDIAssignID(const DIAssignID *N, SmallVectorImpl<uint64_t> &Record,
389 unsigned Abbrev);
390 void writeDITemplateTypeParameter(const DITemplateTypeParameter *N,
391 SmallVectorImpl<uint64_t> &Record,
392 unsigned Abbrev);
393 void writeDITemplateValueParameter(const DITemplateValueParameter *N,
394 SmallVectorImpl<uint64_t> &Record,
395 unsigned Abbrev);
396 void writeDIGlobalVariable(const DIGlobalVariable *N,
397 SmallVectorImpl<uint64_t> &Record,
398 unsigned Abbrev);
399 void writeDILocalVariable(const DILocalVariable *N,
400 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
401 void writeDILabel(const DILabel *N,
402 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
403 void writeDIExpression(const DIExpression *N,
404 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
405 void writeDIGlobalVariableExpression(const DIGlobalVariableExpression *N,
406 SmallVectorImpl<uint64_t> &Record,
407 unsigned Abbrev);
408 void writeDIObjCProperty(const DIObjCProperty *N,
409 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
410 void writeDIProperty(const DIProperty *N, SmallVectorImpl<uint64_t> &Record,
411 unsigned Abbrev);
412 void writeDIImportedEntity(const DIImportedEntity *N,
413 SmallVectorImpl<uint64_t> &Record,
414 unsigned Abbrev);
415 unsigned createNamedMetadataAbbrev();
416 void writeNamedMetadata(SmallVectorImpl<uint64_t> &Record);
417 unsigned createMetadataStringsAbbrev();
418 void writeMetadataStrings(ArrayRef<const Metadata *> Strings,
419 SmallVectorImpl<uint64_t> &Record);
420 void writeMetadataRecords(ArrayRef<const Metadata *> MDs,
421 SmallVectorImpl<uint64_t> &Record,
422 std::vector<unsigned> *MDAbbrevs = nullptr,
423 std::vector<uint64_t> *IndexPos = nullptr);
424 void writeModuleMetadata();
425 void writeFunctionMetadata(const Function &F);
426 void writeFunctionMetadataAttachment(const Function &F);
427 void pushGlobalMetadataAttachment(SmallVectorImpl<uint64_t> &Record,
428 const GlobalObject &GO);
429 void writeModuleMetadataKinds();
430 void writeOperandBundleTags();
431 void writeSyncScopeNames();
432 void writeConstants(unsigned FirstVal, unsigned LastVal, bool isGlobal);
433 void writeModuleConstants();
434 bool pushValueAndType(const Value *V, unsigned InstID,
435 SmallVectorImpl<unsigned> &Vals);
436 bool pushValueOrMetadata(const Value *V, unsigned InstID,
437 SmallVectorImpl<unsigned> &Vals);
438 void writeOperandBundles(const CallBase &CB, unsigned InstID);
439 void pushValue(const Value *V, unsigned InstID,
440 SmallVectorImpl<unsigned> &Vals);
441 void pushValueSigned(const Value *V, unsigned InstID,
442 SmallVectorImpl<uint64_t> &Vals);
443 void writeInstruction(const Instruction &I, unsigned InstID,
444 SmallVectorImpl<unsigned> &Vals);
445 void writeFunctionLevelValueSymbolTable(const ValueSymbolTable &VST);
446 void writeGlobalValueSymbolTable(
447 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex);
448 void writeUseList(UseListOrder &&Order);
449 void writeUseListBlock(const Function *F);
450 void
451 writeFunction(const Function &F,
452 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex);
453 void writeBlockInfo();
454 void writeModuleHash(StringRef View);
455
456 unsigned getEncodedSyncScopeID(SyncScope::ID SSID) {
457 return unsigned(SSID);
458 }
459
460 unsigned getEncodedAlign(MaybeAlign Alignment) { return encode(Alignment); }
461};
462
463/// Class to manage the bitcode writing for a combined index.
464class IndexBitcodeWriter : public BitcodeWriterBase {
465 /// The combined index to write to bitcode.
466 const ModuleSummaryIndex &Index;
467
468 /// When writing combined summaries, provides the set of global value
469 /// summaries for which the value (function, function alias, etc) should be
470 /// imported as a declaration.
471 const GVSummaryPtrSet *DecSummaries = nullptr;
472
473 /// When writing a subset of the index for distributed backends, client
474 /// provides a map of modules to the corresponding GUIDs/summaries to write.
475 const ModuleToSummariesForIndexTy *ModuleToSummariesForIndex;
476
477 /// The summaries in ModuleToSummariesForIndex, sorted by GUID. The
478 /// per-module maps are DenseMaps whose iteration order depends on insertion
479 /// order, which can change with unrelated modules in the link. Writing in
480 /// this order makes logically identical indexes byte-identical.
482 SortedSummariesForIndex;
483
484 /// Map that holds the correspondence between the GUID used in the combined
485 /// index and a value id generated by this class to use in references.
486 std::map<GlobalValue::GUID, unsigned> GUIDToValueIdMap;
487
488 // The stack ids used by this index, which will be a subset of those in
489 // the full index in the case of distributed indexes.
490 std::vector<uint64_t> StackIds;
491
492 // Keep a map of the stack id indices used by records being written for this
493 // index to the index of the corresponding stack id in the above StackIds
494 // vector. Ensures we write each referenced stack id once.
495 DenseMap<unsigned, unsigned> StackIdIndicesToIndex;
496
497 /// Tracks the last value id recorded in the GUIDToValueMap.
498 unsigned GlobalValueId = 0;
499
500 /// Tracks the assignment of module paths in the module path string table to
501 /// an id assigned for use in summary references to the module path.
502 DenseMap<StringRef, uint64_t> ModuleIdMap;
503
504public:
505 /// Constructs a IndexBitcodeWriter object for the given combined index,
506 /// writing to the provided \p Buffer. When writing a subset of the index
507 /// for a distributed backend, provide a \p ModuleToSummariesForIndex map.
508 /// If provided, \p DecSummaries specifies the set of summaries for which
509 /// the corresponding functions or aliased functions should be imported as a
510 /// declaration (but not definition) for each module.
511 IndexBitcodeWriter(
512 BitstreamWriter &Stream, StringTableBuilder &StrtabBuilder,
513 const ModuleSummaryIndex &Index,
514 const GVSummaryPtrSet *DecSummaries = nullptr,
515 const ModuleToSummariesForIndexTy *ModuleToSummariesForIndex = nullptr)
516 : BitcodeWriterBase(Stream, StrtabBuilder), Index(Index),
517 DecSummaries(DecSummaries),
518 ModuleToSummariesForIndex(ModuleToSummariesForIndex) {
519 if (ModuleToSummariesForIndex) {
520 for (const auto &[ModPath, Summaries] : *ModuleToSummariesForIndex)
521 llvm::append_range(SortedSummariesForIndex, Summaries);
522 // Each module's summaries are in DenseMap order, which depends on
523 // insertion order. Sort by GUID for byte-identical output. A GUID may
524 // appear under more than one module; the stable sort keeps those in the
525 // std::map's module path order.
526 llvm::stable_sort(SortedSummariesForIndex, llvm::less_first());
527 }
528
529 // See if the StackIdIndex was already added to the StackId map and
530 // vector. If not, record it.
531 auto RecordStackIdReference = [&](unsigned StackIdIndex) {
532 // If the StackIdIndex is not yet in the map, the below insert ensures
533 // that it will point to the new StackIds vector entry we push to just
534 // below.
535 auto Inserted =
536 StackIdIndicesToIndex.insert({StackIdIndex, StackIds.size()});
537 if (Inserted.second)
538 StackIds.push_back(Index.getStackIdAtIndex(StackIdIndex));
539 };
540
541 // Assign unique value ids to all summaries to be written, for use
542 // in writing out the call graph edges. Save the mapping from GUID
543 // to the new global value id to use when writing those edges, which
544 // are currently saved in the index in terms of GUID.
545 forEachSummary([&](GVInfo I, bool IsAliasee) {
546 GUIDToValueIdMap[I.first] = ++GlobalValueId;
547 // If this is invoked for an aliasee, we want to record the above mapping,
548 // but not the information needed for its summary entry (if the aliasee is
549 // to be imported, we will invoke this separately with IsAliasee=false).
550 if (IsAliasee)
551 return;
552 auto *FS = dyn_cast<FunctionSummary>(I.second);
553 if (!FS)
554 return;
555 // Record all stack id indices actually used in the summary entries being
556 // written, so that we can compact them in the case of distributed ThinLTO
557 // indexes.
558 for (auto &CI : FS->callsites()) {
559 // If the stack id list is empty, this callsite info was synthesized for
560 // a missing tail call frame. Ensure that the callee's GUID gets a value
561 // id. Normally we only generate these for defined summaries, which in
562 // the case of distributed ThinLTO is only the functions already defined
563 // in the module or that we want to import. We don't bother to include
564 // all the callee symbols as they aren't normally needed in the backend.
565 // However, for the synthesized callsite infos we do need the callee
566 // GUID in the backend so that we can correlate the identified callee
567 // with this callsite info (which for non-tail calls is done by the
568 // ordering of the callsite infos and verified via stack ids).
569 if (CI.StackIdIndices.empty()) {
570 GUIDToValueIdMap[CI.Callee.getGUID()] = ++GlobalValueId;
571 continue;
572 }
573 for (auto Idx : CI.StackIdIndices)
574 RecordStackIdReference(Idx);
575 }
577 for (auto &AI : FS->allocs())
578 for (auto &MIB : AI.MIBs)
579 for (auto Idx : MIB.StackIdIndices)
580 RecordStackIdReference(Idx);
581 }
582 });
583 }
584
585 /// The below iterator returns the GUID and associated summary.
586 using GVInfo = std::pair<GlobalValue::GUID, GlobalValueSummary *>;
587
588 /// Calls the callback for each value GUID and summary to be written to
589 /// bitcode. This hides the details of whether they are being pulled from the
590 /// entire index or just those in a provided ModuleToSummariesForIndex map.
591 template<typename Functor>
592 void forEachSummary(Functor Callback) {
593 if (ModuleToSummariesForIndex) {
594 for (auto &[GUID, GVS] : SortedSummariesForIndex) {
595 Callback({GUID, GVS}, false);
596 // Ensure aliasee is handled, e.g. for assigning a valueId,
597 // even if we are not importing the aliasee directly (the
598 // imported alias will contain a copy of aliasee).
599 if (auto *AS = dyn_cast<AliasSummary>(GVS))
600 Callback({AS->getAliaseeGUID(), &AS->getAliasee()}, true);
601 }
602 } else {
603 // Sort by GUID for deterministic output.
604 for (const auto &Summaries : Index.sortedGlobalValueSummariesRange())
605 for (auto &Summary : Summaries.second.getSummaryList())
606 Callback({Summaries.first, Summary.get()}, false);
607 }
608 }
609
610 /// Calls the callback for each entry in the modulePaths StringMap that
611 /// should be written to the module path string table. This hides the details
612 /// of whether they are being pulled from the entire index or just those in a
613 /// provided ModuleToSummariesForIndex map.
614 template <typename Functor> void forEachModule(Functor Callback) {
615 if (ModuleToSummariesForIndex) {
616 for (const auto &M : *ModuleToSummariesForIndex) {
617 const auto &MPI = Index.modulePaths().find(M.first);
618 if (MPI == Index.modulePaths().end()) {
619 // This should only happen if the bitcode file was empty, in which
620 // case we shouldn't be importing (the ModuleToSummariesForIndex
621 // would only include the module we are writing and index for).
622 assert(ModuleToSummariesForIndex->size() == 1);
623 continue;
624 }
625 Callback(*MPI);
626 }
627 } else {
628 // Since StringMap iteration order isn't guaranteed, order by path string
629 // first.
630 // FIXME: Make this a vector of StringMapEntry instead to avoid the later
631 // map lookup.
632 std::vector<StringRef> ModulePaths;
633 for (auto &[ModPath, _] : Index.modulePaths())
634 ModulePaths.push_back(ModPath);
635 llvm::sort(ModulePaths);
636 for (auto &ModPath : ModulePaths)
637 Callback(*Index.modulePaths().find(ModPath));
638 }
639 }
640
641 /// Main entry point for writing a combined index to bitcode.
642 void write();
643
644private:
645 void writeModStrings();
646 void writeCombinedGlobalValueSummary();
647
648 std::optional<unsigned> getValueId(GlobalValue::GUID ValGUID) {
649 auto VMI = GUIDToValueIdMap.find(ValGUID);
650 if (VMI == GUIDToValueIdMap.end())
651 return std::nullopt;
652 return VMI->second;
653 }
654
655 std::map<GlobalValue::GUID, unsigned> &valueIds() { return GUIDToValueIdMap; }
656};
657
658} // end anonymous namespace
659
660static unsigned getEncodedCastOpcode(unsigned Opcode) {
661 switch (Opcode) {
662 default: llvm_unreachable("Unknown cast instruction!");
663 case Instruction::Trunc : return bitc::CAST_TRUNC;
664 case Instruction::ZExt : return bitc::CAST_ZEXT;
665 case Instruction::SExt : return bitc::CAST_SEXT;
666 case Instruction::FPToUI : return bitc::CAST_FPTOUI;
667 case Instruction::FPToSI : return bitc::CAST_FPTOSI;
668 case Instruction::UIToFP : return bitc::CAST_UITOFP;
669 case Instruction::SIToFP : return bitc::CAST_SITOFP;
670 case Instruction::FPTrunc : return bitc::CAST_FPTRUNC;
671 case Instruction::FPExt : return bitc::CAST_FPEXT;
672 case Instruction::PtrToAddr: return bitc::CAST_PTRTOADDR;
673 case Instruction::PtrToInt: return bitc::CAST_PTRTOINT;
674 case Instruction::IntToPtr: return bitc::CAST_INTTOPTR;
675 case Instruction::BitCast : return bitc::CAST_BITCAST;
676 case Instruction::AddrSpaceCast: return bitc::CAST_ADDRSPACECAST;
677 }
678}
679
680static unsigned getEncodedUnaryOpcode(unsigned Opcode) {
681 switch (Opcode) {
682 default: llvm_unreachable("Unknown binary instruction!");
683 case Instruction::FNeg: return bitc::UNOP_FNEG;
684 }
685}
686
687static unsigned getEncodedBinaryOpcode(unsigned Opcode) {
688 switch (Opcode) {
689 default: llvm_unreachable("Unknown binary instruction!");
690 case Instruction::Add:
691 case Instruction::FAdd: return bitc::BINOP_ADD;
692 case Instruction::Sub:
693 case Instruction::FSub: return bitc::BINOP_SUB;
694 case Instruction::Mul:
695 case Instruction::FMul: return bitc::BINOP_MUL;
696 case Instruction::UDiv: return bitc::BINOP_UDIV;
697 case Instruction::FDiv:
698 case Instruction::SDiv: return bitc::BINOP_SDIV;
699 case Instruction::URem: return bitc::BINOP_UREM;
700 case Instruction::FRem:
701 case Instruction::SRem: return bitc::BINOP_SREM;
702 case Instruction::Shl: return bitc::BINOP_SHL;
703 case Instruction::LShr: return bitc::BINOP_LSHR;
704 case Instruction::AShr: return bitc::BINOP_ASHR;
705 case Instruction::And: return bitc::BINOP_AND;
706 case Instruction::Or: return bitc::BINOP_OR;
707 case Instruction::Xor: return bitc::BINOP_XOR;
708 }
709}
710
711static unsigned getEncodedRMWOperation(const AtomicRMWInst &I) {
712 unsigned Encoding = 0;
713 switch (I.getOperation()) {
714 default: llvm_unreachable("Unknown RMW operation!");
716 Encoding = bitc::RMW_XCHG;
717 break;
719 Encoding = bitc::RMW_ADD;
720 break;
722 Encoding = bitc::RMW_SUB;
723 break;
725 Encoding = bitc::RMW_AND;
726 break;
728 Encoding = bitc::RMW_NAND;
729 break;
731 Encoding = bitc::RMW_OR;
732 break;
734 Encoding = bitc::RMW_XOR;
735 break;
737 Encoding = bitc::RMW_MAX;
738 break;
740 Encoding = bitc::RMW_MIN;
741 break;
743 Encoding = bitc::RMW_UMAX;
744 break;
746 Encoding = bitc::RMW_UMIN;
747 break;
749 Encoding = bitc::RMW_FADD;
750 break;
752 Encoding = bitc::RMW_FSUB;
753 break;
755 Encoding = bitc::RMW_FMAX;
756 break;
758 Encoding = bitc::RMW_FMIN;
759 break;
761 Encoding = bitc::RMW_FMAXIMUM;
762 break;
764 Encoding = bitc::RMW_FMINIMUM;
765 break;
767 Encoding = bitc::RMW_FMAXIMUMNUM;
768 break;
770 Encoding = bitc::RMW_FMINIMUMNUM;
771 break;
773 Encoding = bitc::RMW_UINC_WRAP;
774 break;
776 Encoding = bitc::RMW_UDEC_WRAP;
777 break;
779 Encoding = bitc::RMW_USUB_COND;
780 break;
782 Encoding = bitc::RMW_USUB_SAT;
783 break;
784 }
785
786 if (I.isElementwise())
787 Encoding |= bitc::RMW_ELEMENTWISE_FLAG;
788 return Encoding;
789}
790
803
804static void writeStringRecord(BitstreamWriter &Stream, unsigned Code,
805 StringRef Str, unsigned AbbrevToUse) {
807
808 // Code: [strchar x N]
809 for (char C : Str) {
810 if (AbbrevToUse && !BitCodeAbbrevOp::isChar6(C))
811 AbbrevToUse = 0;
812 Vals.push_back(C);
813 }
814
815 // Emit the finished record.
816 Stream.EmitRecord(Code, Vals, AbbrevToUse);
817}
818
820 switch (Kind) {
821 case Attribute::Alignment:
823 case Attribute::AllocAlign:
825 case Attribute::AllocSize:
827 case Attribute::AlwaysInline:
829 case Attribute::Builtin:
831 case Attribute::ByVal:
833 case Attribute::Convergent:
835 case Attribute::InAlloca:
837 case Attribute::Cold:
839 case Attribute::DisableSanitizerInstrumentation:
841 case Attribute::FnRetThunkExtern:
843 case Attribute::Flatten:
845 case Attribute::Hot:
846 return bitc::ATTR_KIND_HOT;
847 case Attribute::ElementType:
849 case Attribute::HybridPatchable:
851 case Attribute::InlineHint:
853 case Attribute::InReg:
855 case Attribute::JumpTable:
857 case Attribute::MinSize:
859 case Attribute::AllocatedPointer:
861 case Attribute::AllocKind:
863 case Attribute::Memory:
865 case Attribute::NoFPClass:
867 case Attribute::Naked:
869 case Attribute::Nest:
871 case Attribute::NoAlias:
873 case Attribute::NoBuiltin:
875 case Attribute::NoCallback:
877 case Attribute::NoDivergenceSource:
879 case Attribute::NoDuplicate:
881 case Attribute::NoFree:
883 case Attribute::NoFreeObj:
885 case Attribute::NoImplicitFloat:
887 case Attribute::NoInline:
889 case Attribute::NoRecurse:
891 case Attribute::NoMerge:
893 case Attribute::NonLazyBind:
895 case Attribute::NonNull:
897 case Attribute::Dereferenceable:
899 case Attribute::DereferenceableOrNull:
901 case Attribute::NoRedZone:
903 case Attribute::NoReturn:
905 case Attribute::NoSync:
907 case Attribute::NoCfCheck:
909 case Attribute::NoProfile:
911 case Attribute::SkipProfile:
913 case Attribute::NoUnwind:
915 case Attribute::NoSanitizeBounds:
917 case Attribute::NoSanitizeCoverage:
919 case Attribute::NullPointerIsValid:
921 case Attribute::OptimizeForDebugging:
923 case Attribute::OptForFuzzing:
925 case Attribute::OptimizeForSize:
927 case Attribute::OptimizeNone:
929 case Attribute::ReadNone:
931 case Attribute::ReadOnly:
933 case Attribute::Returned:
935 case Attribute::ReturnsTwice:
937 case Attribute::SExt:
939 case Attribute::Speculatable:
941 case Attribute::StackAlignment:
943 case Attribute::StackProtect:
945 case Attribute::StackProtectReq:
947 case Attribute::StackProtectStrong:
949 case Attribute::SafeStack:
951 case Attribute::ShadowCallStack:
953 case Attribute::StrictFP:
955 case Attribute::StructRet:
957 case Attribute::SanitizeAddress:
959 case Attribute::SanitizeAllocToken:
961 case Attribute::SanitizeHWAddress:
963 case Attribute::SanitizeThread:
965 case Attribute::SanitizeConcurrency:
967 case Attribute::SanitizeType:
969 case Attribute::SanitizeMemory:
971 case Attribute::SanitizeNumericalStability:
973 case Attribute::SanitizeRealtime:
975 case Attribute::SanitizeRealtimeBlocking:
977 case Attribute::SpeculativeLoadHardening:
979 case Attribute::SwiftError:
981 case Attribute::SwiftSelf:
983 case Attribute::SwiftAsync:
985 case Attribute::UWTable:
987 case Attribute::VScaleRange:
989 case Attribute::WillReturn:
991 case Attribute::WriteOnly:
993 case Attribute::ZExt:
995 case Attribute::ImmArg:
997 case Attribute::SanitizeMemTag:
999 case Attribute::Preallocated:
1001 case Attribute::NoUndef:
1003 case Attribute::ByRef:
1004 return bitc::ATTR_KIND_BYREF;
1005 case Attribute::MustProgress:
1007 case Attribute::PresplitCoroutine:
1009 case Attribute::Writable:
1011 case Attribute::CoroDestroyOnlyWhenComplete:
1013 case Attribute::CoroElideSafe:
1015 case Attribute::DeadOnUnwind:
1017 case Attribute::Range:
1018 return bitc::ATTR_KIND_RANGE;
1019 case Attribute::Initializes:
1021 case Attribute::NoExt:
1023 case Attribute::Captures:
1025 case Attribute::DeadOnReturn:
1027 case Attribute::NoCreateUndefOrPoison:
1029 case Attribute::DenormalFPEnv:
1031 case Attribute::NoOutline:
1033 case Attribute::NoIPA:
1034 return bitc::ATTR_KIND_NOIPA;
1035 case Attribute::LikelyModuleLocal:
1038 llvm_unreachable("Can not encode end-attribute kinds marker.");
1039 case Attribute::None:
1040 llvm_unreachable("Can not encode none-attribute.");
1043 llvm_unreachable("Trying to encode EmptyKey/TombstoneKey");
1044 }
1045
1046 llvm_unreachable("Trying to encode unknown attribute");
1047}
1048
1050 if ((int64_t)V >= 0)
1051 Vals.push_back(V << 1);
1052 else
1053 Vals.push_back((-V << 1) | 1);
1054}
1055
1057 // We have an arbitrary precision integer value to write whose
1058 // bit width is > 64. However, in canonical unsigned integer
1059 // format it is likely that the high bits are going to be zero.
1060 // So, we only write the number of active words.
1061 unsigned NumWords = A.getActiveWords();
1062 const uint64_t *RawData = A.getRawData();
1063 for (unsigned i = 0; i < NumWords; i++)
1064 emitSignedInt64(Vals, RawData[i]);
1065}
1066
1068 const ConstantRange &CR, bool EmitBitWidth) {
1069 unsigned BitWidth = CR.getBitWidth();
1070 if (EmitBitWidth)
1071 Record.push_back(BitWidth);
1072 if (BitWidth > 64) {
1073 Record.push_back(CR.getLower().getActiveWords() |
1074 (uint64_t(CR.getUpper().getActiveWords()) << 32));
1077 } else {
1080 }
1081}
1082
1083void ModuleBitcodeWriter::writeAttributeGroupTable() {
1084 const std::vector<ValueEnumerator::IndexAndAttrSet> &AttrGrps =
1085 VE.getAttributeGroups();
1086 if (AttrGrps.empty()) return;
1087
1089
1090 SmallVector<uint64_t, 64> Record;
1091 for (ValueEnumerator::IndexAndAttrSet Pair : AttrGrps) {
1092 unsigned AttrListIndex = Pair.first;
1093 AttributeSet AS = Pair.second;
1094 Record.push_back(VE.getAttributeGroupID(Pair));
1095 Record.push_back(AttrListIndex);
1096
1097 for (Attribute Attr : AS) {
1098 if (Attr.isEnumAttribute()) {
1099 Record.push_back(0);
1100 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
1101 } else if (Attr.isIntAttribute()) {
1102 Record.push_back(1);
1103 Attribute::AttrKind Kind = Attr.getKindAsEnum();
1104 Record.push_back(getAttrKindEncoding(Kind));
1105 if (Kind == Attribute::Memory) {
1106 // Version field for upgrading old memory effects.
1107 const uint64_t Version = 2;
1108 Record.push_back((Version << 56) | Attr.getValueAsInt());
1109 } else {
1110 Record.push_back(Attr.getValueAsInt());
1111 }
1112 } else if (Attr.isStringAttribute()) {
1113 StringRef Kind = Attr.getKindAsString();
1114 StringRef Val = Attr.getValueAsString();
1115
1116 Record.push_back(Val.empty() ? 3 : 4);
1117 Record.append(Kind.begin(), Kind.end());
1118 Record.push_back(0);
1119 if (!Val.empty()) {
1120 Record.append(Val.begin(), Val.end());
1121 Record.push_back(0);
1122 }
1123 } else if (Attr.isTypeAttribute()) {
1124 Type *Ty = Attr.getValueAsType();
1125 Record.push_back(Ty ? 6 : 5);
1126 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
1127 if (Ty)
1128 Record.push_back(VE.getTypeID(Attr.getValueAsType()));
1129 } else if (Attr.isConstantRangeAttribute()) {
1130 Record.push_back(7);
1131 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
1132 emitConstantRange(Record, Attr.getValueAsConstantRange(),
1133 /*EmitBitWidth=*/true);
1134 } else {
1135 assert(Attr.isConstantRangeListAttribute());
1136 Record.push_back(8);
1137 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
1138 ArrayRef<ConstantRange> Val = Attr.getValueAsConstantRangeList();
1139 Record.push_back(Val.size());
1140 Record.push_back(Val[0].getBitWidth());
1141 for (auto &CR : Val)
1142 emitConstantRange(Record, CR, /*EmitBitWidth=*/false);
1143 }
1144 }
1145
1147 Record.clear();
1148 }
1149
1150 Stream.ExitBlock();
1151}
1152
1153void ModuleBitcodeWriter::writeAttributeTable() {
1154 const std::vector<AttributeList> &Attrs = VE.getAttributeLists();
1155 if (Attrs.empty()) return;
1156
1158
1159 SmallVector<uint64_t, 64> Record;
1160 for (const AttributeList &AL : Attrs) {
1161 for (unsigned i : AL.indexes()) {
1162 AttributeSet AS = AL.getAttributes(i);
1163 if (AS.hasAttributes())
1164 Record.push_back(VE.getAttributeGroupID({i, AS}));
1165 }
1166
1167 Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record);
1168 Record.clear();
1169 }
1170
1171 Stream.ExitBlock();
1172}
1173
1174/// WriteTypeTable - Write out the type table for a module.
1175void ModuleBitcodeWriter::writeTypeTable() {
1176 const ValueEnumerator::TypeList &TypeList = VE.getTypes();
1177
1178 Stream.EnterSubblock(bitc::TYPE_BLOCK_ID_NEW, 4 /*count from # abbrevs */);
1179 SmallVector<uint64_t, 64> TypeVals;
1180
1182
1183 // Abbrev for TYPE_CODE_OPAQUE_POINTER.
1184 auto Abbv = std::make_shared<BitCodeAbbrev>();
1185 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_OPAQUE_POINTER));
1186 Abbv->Add(BitCodeAbbrevOp(0)); // Addrspace = 0
1187 unsigned OpaquePtrAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1188
1189 // Abbrev for TYPE_CODE_FUNCTION.
1190 Abbv = std::make_shared<BitCodeAbbrev>();
1191 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION));
1192 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg
1193 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1194 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
1195 unsigned FunctionAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1196
1197 // Abbrev for TYPE_CODE_STRUCT_ANON.
1198 Abbv = std::make_shared<BitCodeAbbrev>();
1199 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_ANON));
1200 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
1201 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1202 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
1203 unsigned StructAnonAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1204
1205 // Abbrev for TYPE_CODE_STRUCT_NAME.
1206 Abbv = std::make_shared<BitCodeAbbrev>();
1207 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAME));
1208 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1209 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
1210 unsigned StructNameAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1211
1212 // Abbrev for TYPE_CODE_STRUCT_NAMED.
1213 Abbv = std::make_shared<BitCodeAbbrev>();
1214 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAMED));
1215 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
1216 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1217 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
1218 unsigned StructNamedAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1219
1220 // Abbrev for TYPE_CODE_ARRAY.
1221 Abbv = std::make_shared<BitCodeAbbrev>();
1222 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY));
1223 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size
1224 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
1225 unsigned ArrayAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1226
1227 // Emit an entry count so the reader can reserve space.
1228 TypeVals.push_back(TypeList.size());
1229 Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
1230 TypeVals.clear();
1231
1232 // Loop over all of the types, emitting each in turn.
1233 for (Type *T : TypeList) {
1234 int AbbrevToUse = 0;
1235 unsigned Code = 0;
1236
1237 switch (T->getTypeID()) {
1238 case Type::VoidTyID: Code = bitc::TYPE_CODE_VOID; break;
1239 case Type::HalfTyID: Code = bitc::TYPE_CODE_HALF; break;
1240 case Type::BFloatTyID: Code = bitc::TYPE_CODE_BFLOAT; break;
1241 case Type::FloatTyID: Code = bitc::TYPE_CODE_FLOAT; break;
1242 case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break;
1243 case Type::X86_FP80TyID: Code = bitc::TYPE_CODE_X86_FP80; break;
1244 case Type::FP128TyID: Code = bitc::TYPE_CODE_FP128; break;
1245 case Type::PPC_FP128TyID: Code = bitc::TYPE_CODE_PPC_FP128; break;
1246 case Type::LabelTyID: Code = bitc::TYPE_CODE_LABEL; break;
1247 case Type::MetadataTyID:
1249 break;
1250 case Type::X86_AMXTyID: Code = bitc::TYPE_CODE_X86_AMX; break;
1251 case Type::TokenTyID: Code = bitc::TYPE_CODE_TOKEN; break;
1252 case Type::ByteTyID:
1253 // BYTE: [width]
1255 TypeVals.push_back(T->getByteBitWidth());
1256 break;
1257 case Type::IntegerTyID:
1258 // INTEGER: [width]
1261 break;
1262 case Type::PointerTyID: {
1264 unsigned AddressSpace = PTy->getAddressSpace();
1265 // OPAQUE_POINTER: [address space]
1267 TypeVals.push_back(AddressSpace);
1268 if (AddressSpace == 0)
1269 AbbrevToUse = OpaquePtrAbbrev;
1270 break;
1271 }
1272 case Type::FunctionTyID: {
1273 FunctionType *FT = cast<FunctionType>(T);
1274 // FUNCTION: [isvararg, retty, paramty x N]
1276 TypeVals.push_back(FT->isVarArg());
1277 TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
1278 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
1279 TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
1280 AbbrevToUse = FunctionAbbrev;
1281 break;
1282 }
1283 case Type::StructTyID: {
1284 StructType *ST = cast<StructType>(T);
1285 // STRUCT: [ispacked, eltty x N]
1286 TypeVals.push_back(ST->isPacked());
1287 // Output all of the element types.
1288 for (Type *ET : ST->elements())
1289 TypeVals.push_back(VE.getTypeID(ET));
1290
1291 if (ST->isLiteral()) {
1293 AbbrevToUse = StructAnonAbbrev;
1294 } else {
1295 if (ST->isOpaque()) {
1297 } else {
1299 AbbrevToUse = StructNamedAbbrev;
1300 }
1301
1302 // Emit the name if it is present.
1303 if (!ST->getName().empty())
1305 StructNameAbbrev);
1306 }
1307 break;
1308 }
1309 case Type::ArrayTyID: {
1311 // ARRAY: [numelts, eltty]
1313 TypeVals.push_back(AT->getNumElements());
1314 TypeVals.push_back(VE.getTypeID(AT->getElementType()));
1315 AbbrevToUse = ArrayAbbrev;
1316 break;
1317 }
1318 case Type::FixedVectorTyID:
1319 case Type::ScalableVectorTyID: {
1321 // VECTOR [numelts, eltty] or
1322 // [numelts, eltty, scalable]
1324 TypeVals.push_back(VT->getElementCount().getKnownMinValue());
1325 TypeVals.push_back(VE.getTypeID(VT->getElementType()));
1327 TypeVals.push_back(true);
1328 break;
1329 }
1330 case Type::TargetExtTyID: {
1331 TargetExtType *TET = cast<TargetExtType>(T);
1334 StructNameAbbrev);
1335 TypeVals.push_back(TET->getNumTypeParameters());
1336 for (Type *InnerTy : TET->type_params())
1337 TypeVals.push_back(VE.getTypeID(InnerTy));
1338 llvm::append_range(TypeVals, TET->int_params());
1339 break;
1340 }
1341 case Type::TypedPointerTyID:
1342 llvm_unreachable("Typed pointers cannot be added to IR modules");
1343 }
1344
1345 // Emit the finished record.
1346 Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
1347 TypeVals.clear();
1348 }
1349
1350 Stream.ExitBlock();
1351}
1352
1354 switch (Linkage) {
1356 return 0;
1358 return 16;
1360 return 2;
1362 return 3;
1364 return 18;
1366 return 7;
1368 return 8;
1370 return 9;
1372 return 17;
1374 return 19;
1376 return 12;
1377 }
1378 llvm_unreachable("Invalid linkage");
1379}
1380
1381static unsigned getEncodedLinkage(const GlobalValue &GV) {
1382 return getEncodedLinkage(GV.getLinkage());
1383}
1384
1386 uint64_t RawFlags = 0;
1387 RawFlags |= Flags.ReadNone;
1388 RawFlags |= (Flags.ReadOnly << 1);
1389 RawFlags |= (Flags.NoRecurse << 2);
1390 RawFlags |= (Flags.ReturnDoesNotAlias << 3);
1391 RawFlags |= (Flags.NoInline << 4);
1392 RawFlags |= (Flags.AlwaysInline << 5);
1393 RawFlags |= (Flags.NoUnwind << 6);
1394 RawFlags |= (Flags.MayThrow << 7);
1395 RawFlags |= (Flags.HasUnknownCall << 8);
1396 RawFlags |= (Flags.MustBeUnreachable << 9);
1397 return RawFlags;
1398}
1399
1400// Decode the flags for GlobalValue in the summary. See getDecodedGVSummaryFlags
1401// in BitcodeReader.cpp.
1403 bool ImportAsDecl = false) {
1404 uint64_t RawFlags = 0;
1405
1406 RawFlags |= Flags.NotEligibleToImport; // bool
1407 RawFlags |= (Flags.Live << 1);
1408 RawFlags |= (Flags.DSOLocal << 2);
1409 RawFlags |= (Flags.CanAutoHide << 3);
1410
1411 // Linkage don't need to be remapped at that time for the summary. Any future
1412 // change to the getEncodedLinkage() function will need to be taken into
1413 // account here as well.
1414 RawFlags = (RawFlags << 4) | Flags.Linkage; // 4 bits
1415
1416 RawFlags |= (Flags.Visibility << 8); // 2 bits
1417
1418 unsigned ImportType = Flags.ImportType | ImportAsDecl;
1419 RawFlags |= (ImportType << 10); // 1 bit
1420
1421 RawFlags |= (Flags.NoRenameOnPromotion << 11); // 1 bit
1422
1423 return RawFlags;
1424}
1425
1427 uint64_t RawFlags = Flags.MaybeReadOnly | (Flags.MaybeWriteOnly << 1) |
1428 (Flags.Constant << 2) | Flags.VCallVisibility << 3;
1429 return RawFlags;
1430}
1431
1433 uint64_t RawFlags = 0;
1434
1435 RawFlags |= CI.Hotness; // 3 bits
1436 RawFlags |= (CI.HasTailCall << 3); // 1 bit
1437 RawFlags |= (CI.HasIndirectCall << 4); // 1 bit
1438
1439 return RawFlags;
1440}
1441
1442static unsigned getEncodedVisibility(const GlobalValue &GV) {
1443 switch (GV.getVisibility()) {
1444 case GlobalValue::DefaultVisibility: return 0;
1445 case GlobalValue::HiddenVisibility: return 1;
1446 case GlobalValue::ProtectedVisibility: return 2;
1447 }
1448 llvm_unreachable("Invalid visibility");
1449}
1450
1451static unsigned getEncodedDLLStorageClass(const GlobalValue &GV) {
1452 switch (GV.getDLLStorageClass()) {
1453 case GlobalValue::DefaultStorageClass: return 0;
1456 }
1457 llvm_unreachable("Invalid DLL storage class");
1458}
1459
1460static unsigned getEncodedThreadLocalMode(const GlobalValue &GV) {
1461 switch (GV.getThreadLocalMode()) {
1462 case GlobalVariable::NotThreadLocal: return 0;
1466 case GlobalVariable::LocalExecTLSModel: return 4;
1467 }
1468 llvm_unreachable("Invalid TLS model");
1469}
1470
1471static unsigned getEncodedComdatSelectionKind(const Comdat &C) {
1472 switch (C.getSelectionKind()) {
1473 case Comdat::Any:
1475 case Comdat::ExactMatch:
1477 case Comdat::Largest:
1481 case Comdat::SameSize:
1483 }
1484 llvm_unreachable("Invalid selection kind");
1485}
1486
1487static unsigned getEncodedUnnamedAddr(const GlobalValue &GV) {
1488 switch (GV.getUnnamedAddr()) {
1489 case GlobalValue::UnnamedAddr::None: return 0;
1490 case GlobalValue::UnnamedAddr::Local: return 2;
1491 case GlobalValue::UnnamedAddr::Global: return 1;
1492 }
1493 llvm_unreachable("Invalid unnamed_addr");
1494}
1495
1496size_t ModuleBitcodeWriter::addToStrtab(StringRef Str) {
1497 if (GenerateHash)
1498 Hasher.update(Str);
1499 return StrtabBuilder.add(Str);
1500}
1501
1502void ModuleBitcodeWriter::writeComdats() {
1504 for (const Comdat *C : VE.getComdats()) {
1505 // COMDAT: [strtab offset, strtab size, selection_kind]
1506 Vals.push_back(addToStrtab(C->getName()));
1507 Vals.push_back(C->getName().size());
1509 Stream.EmitRecord(bitc::MODULE_CODE_COMDAT, Vals, /*AbbrevToUse=*/0);
1510 Vals.clear();
1511 }
1512}
1513
1514/// Write a record that will eventually hold the word offset of the
1515/// module-level VST. For now the offset is 0, which will be backpatched
1516/// after the real VST is written. Saves the bit offset to backpatch.
1517void ModuleBitcodeWriter::writeValueSymbolTableForwardDecl() {
1518 // Write a placeholder value in for the offset of the real VST,
1519 // which is written after the function blocks so that it can include
1520 // the offset of each function. The placeholder offset will be
1521 // updated when the real VST is written.
1522 auto Abbv = std::make_shared<BitCodeAbbrev>();
1523 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_VSTOFFSET));
1524 // Blocks are 32-bit aligned, so we can use a 32-bit word offset to
1525 // hold the real VST offset. Must use fixed instead of VBR as we don't
1526 // know how many VBR chunks to reserve ahead of time.
1527 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1528 unsigned VSTOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1529
1530 // Emit the placeholder
1532 Stream.EmitRecordWithAbbrev(VSTOffsetAbbrev, Vals);
1533
1534 // Compute and save the bit offset to the placeholder, which will be
1535 // patched when the real VST is written. We can simply subtract the 32-bit
1536 // fixed size from the current bit number to get the location to backpatch.
1537 VSTOffsetPlaceholder = Stream.GetCurrentBitNo() - 32;
1538}
1539
1541
1542/// Determine the encoding to use for the given string name and length.
1544 bool isChar6 = true;
1545 for (char C : Str) {
1546 if (isChar6)
1547 isChar6 = BitCodeAbbrevOp::isChar6(C);
1548 if ((unsigned char)C & 128)
1549 // don't bother scanning the rest.
1550 return SE_Fixed8;
1551 }
1552 if (isChar6)
1553 return SE_Char6;
1554 return SE_Fixed7;
1555}
1556
1557static_assert(sizeof(GlobalValue::SanitizerMetadata) <= sizeof(unsigned),
1558 "Sanitizer Metadata is too large for naive serialization.");
1559static unsigned
1561 return Meta.NoAddress | (Meta.NoHWAddress << 1) |
1562 (Meta.Memtag << 2) | (Meta.IsDynInit << 3);
1563}
1564
1565/// Emit top-level description of module, including target triple, inline asm,
1566/// descriptors for global variables, and function prototype info.
1567/// Returns the bit offset to backpatch with the location of the real VST.
1568void ModuleBitcodeWriter::writeModuleInfo() {
1569 // Emit various pieces of data attached to a module.
1570 if (!M.getTargetTriple().empty())
1572 M.getTargetTriple().str(), 0 /*TODO*/);
1573 const std::string &DL = M.getDataLayoutStr();
1574 if (!DL.empty())
1576
1577 for (const Module::GlobalAsmFragment &Frag : M.getModuleInlineAsm()) {
1579 Frag.Props.getAsStrings();
1580 for (auto [Key, Value] : Props) {
1582 Record.append(Key.begin(), Key.end());
1583 Record.push_back(0);
1584 Record.append(Value.begin(), Value.end());
1586 }
1587 writeStringRecord(Stream, bitc::MODULE_CODE_ASM, Frag.Asm, 0 /*TODO*/);
1588 }
1589
1590 // Emit information about sections and GC, computing how many there are. Also
1591 // compute the maximum alignment value.
1592 std::map<std::string, unsigned> SectionMap;
1593 std::map<std::string, unsigned> GCMap;
1594 MaybeAlign MaxGVarAlignment;
1595 unsigned MaxGlobalType = 0;
1596 for (const GlobalVariable &GV : M.globals()) {
1597 if (MaybeAlign A = GV.getAlign())
1598 MaxGVarAlignment = !MaxGVarAlignment ? *A : std::max(*MaxGVarAlignment, *A);
1599 MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV.getValueType()));
1600 if (GV.hasSection()) {
1601 // Give section names unique ID's.
1602 unsigned &Entry = SectionMap[std::string(GV.getSection())];
1603 if (!Entry) {
1604 writeStringRecord(Stream, bitc::MODULE_CODE_SECTIONNAME, GV.getSection(),
1605 0 /*TODO*/);
1606 Entry = SectionMap.size();
1607 }
1608 }
1609 }
1610 for (const Function &F : M) {
1611 if (F.hasSection()) {
1612 // Give section names unique ID's.
1613 unsigned &Entry = SectionMap[std::string(F.getSection())];
1614 if (!Entry) {
1616 0 /*TODO*/);
1617 Entry = SectionMap.size();
1618 }
1619 }
1620 if (F.hasGC()) {
1621 // Same for GC names.
1622 unsigned &Entry = GCMap[F.getGC()];
1623 if (!Entry) {
1625 0 /*TODO*/);
1626 Entry = GCMap.size();
1627 }
1628 }
1629 }
1630
1631 // Emit abbrev for globals, now that we know # sections and max alignment.
1632 unsigned SimpleGVarAbbrev = 0;
1633 if (!M.global_empty()) {
1634 // Add an abbrev for common globals with no visibility or thread localness.
1635 auto Abbv = std::make_shared<BitCodeAbbrev>();
1636 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
1637 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1638 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1639 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1640 Log2_32_Ceil(MaxGlobalType+1)));
1641 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // AddrSpace << 2
1642 //| explicitType << 1
1643 //| constant
1644 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer.
1645 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // Linkage.
1646 if (!MaxGVarAlignment) // Alignment.
1647 Abbv->Add(BitCodeAbbrevOp(0));
1648 else {
1649 unsigned MaxEncAlignment = getEncodedAlign(MaxGVarAlignment);
1650 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1651 Log2_32_Ceil(MaxEncAlignment+1)));
1652 }
1653 if (SectionMap.empty()) // Section.
1654 Abbv->Add(BitCodeAbbrevOp(0));
1655 else
1656 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1657 Log2_32_Ceil(SectionMap.size()+1)));
1658 // Don't bother emitting vis + thread local.
1659 SimpleGVarAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1660 }
1661
1663 // Emit the module's source file name.
1664 {
1665 StringEncoding Bits = getStringEncoding(M.getSourceFileName());
1666 BitCodeAbbrevOp AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8);
1667 if (Bits == SE_Char6)
1668 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Char6);
1669 else if (Bits == SE_Fixed7)
1670 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7);
1671
1672 // MODULE_CODE_SOURCE_FILENAME: [namechar x N]
1673 auto Abbv = std::make_shared<BitCodeAbbrev>();
1674 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_SOURCE_FILENAME));
1675 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1676 Abbv->Add(AbbrevOpToUse);
1677 unsigned FilenameAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1678
1679 for (const auto P : M.getSourceFileName())
1680 Vals.push_back((unsigned char)P);
1681
1682 // Emit the finished record.
1683 Stream.EmitRecord(bitc::MODULE_CODE_SOURCE_FILENAME, Vals, FilenameAbbrev);
1684 Vals.clear();
1685 }
1686
1687 writeGUIDList();
1688
1689 // Emit the global variable information.
1690 for (const GlobalVariable &GV : M.globals()) {
1691 unsigned AbbrevToUse = 0;
1692
1693 // GLOBALVAR: [strtab offset, strtab size, type, isconst, initid,
1694 // linkage, alignment, section, visibility, threadlocal,
1695 // unnamed_addr, externally_initialized, dllstorageclass,
1696 // comdat, attributes, DSO_Local, GlobalSanitizer, code_model]
1697 Vals.push_back(addToStrtab(GV.getName()));
1698 Vals.push_back(GV.getName().size());
1699 Vals.push_back(VE.getTypeID(GV.getValueType()));
1700 Vals.push_back(GV.getType()->getAddressSpace() << 2 | 2 | GV.isConstant());
1701 Vals.push_back(GV.isDeclaration() ? 0 :
1702 (VE.getValueID(GV.getInitializer()) + 1));
1703 Vals.push_back(getEncodedLinkage(GV));
1704 Vals.push_back(getEncodedAlign(GV.getAlign()));
1705 Vals.push_back(GV.hasSection() ? SectionMap[std::string(GV.getSection())]
1706 : 0);
1707 if (GV.isThreadLocal() ||
1708 GV.getVisibility() != GlobalValue::DefaultVisibility ||
1709 GV.getUnnamedAddr() != GlobalValue::UnnamedAddr::None ||
1710 GV.isExternallyInitialized() ||
1711 GV.getDLLStorageClass() != GlobalValue::DefaultStorageClass ||
1712 GV.hasComdat() || GV.hasAttributes() || GV.isDSOLocal() ||
1713 GV.hasPartition() || GV.hasSanitizerMetadata() || GV.getCodeModel()) {
1717 Vals.push_back(GV.isExternallyInitialized());
1719 Vals.push_back(GV.hasComdat() ? VE.getComdatID(GV.getComdat()) : 0);
1720
1721 auto AL = GV.getAttributesAsList(AttributeList::FunctionIndex);
1722 Vals.push_back(VE.getAttributeListID(AL));
1723
1724 Vals.push_back(GV.isDSOLocal());
1725 Vals.push_back(addToStrtab(GV.getPartition()));
1726 Vals.push_back(GV.getPartition().size());
1727
1728 Vals.push_back((GV.hasSanitizerMetadata() ? serializeSanitizerMetadata(
1729 GV.getSanitizerMetadata())
1730 : 0));
1731 Vals.push_back(GV.getCodeModelRaw());
1732 } else {
1733 AbbrevToUse = SimpleGVarAbbrev;
1734 }
1735
1736 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
1737 Vals.clear();
1738 }
1739
1740 // Emit the function proto information.
1741 for (const Function &F : M) {
1742 // FUNCTION: [strtab offset, strtab size, type, callingconv, isproto,
1743 // linkage, paramattrs, alignment, section, visibility, gc,
1744 // unnamed_addr, prologuedata, dllstorageclass, comdat,
1745 // prefixdata, personalityfn, DSO_Local, addrspace,
1746 // partition_strtab, partition_size, prefalign]
1747 Vals.push_back(addToStrtab(F.getName()));
1748 Vals.push_back(F.getName().size());
1749 Vals.push_back(VE.getTypeID(F.getFunctionType()));
1750 Vals.push_back(F.getCallingConv());
1751 Vals.push_back(F.isDeclaration());
1753 Vals.push_back(VE.getAttributeListID(F.getAttributes()));
1754 Vals.push_back(getEncodedAlign(F.getAlign()));
1755 Vals.push_back(F.hasSection() ? SectionMap[std::string(F.getSection())]
1756 : 0);
1758 Vals.push_back(F.hasGC() ? GCMap[F.getGC()] : 0);
1760 Vals.push_back(F.hasPrologueData() ? (VE.getValueID(F.getPrologueData()) + 1)
1761 : 0);
1763 Vals.push_back(F.hasComdat() ? VE.getComdatID(F.getComdat()) : 0);
1764 Vals.push_back(F.hasPrefixData() ? (VE.getValueID(F.getPrefixData()) + 1)
1765 : 0);
1766 Vals.push_back(
1767 F.hasPersonalityFn() ? (VE.getValueID(F.getPersonalityFn()) + 1) : 0);
1768
1769 Vals.push_back(F.isDSOLocal());
1770 Vals.push_back(F.getAddressSpace());
1771 Vals.push_back(addToStrtab(F.getPartition()));
1772 Vals.push_back(F.getPartition().size());
1773 Vals.push_back(getEncodedAlign(F.getPreferredAlignment()));
1774
1775 unsigned AbbrevToUse = 0;
1776 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
1777 Vals.clear();
1778 }
1779
1780 // Emit the alias information.
1781 for (const GlobalAlias &A : M.aliases()) {
1782 // ALIAS: [strtab offset, strtab size, alias type, aliasee val#, linkage,
1783 // visibility, dllstorageclass, threadlocal, unnamed_addr,
1784 // DSO_Local]
1785 Vals.push_back(addToStrtab(A.getName()));
1786 Vals.push_back(A.getName().size());
1787 Vals.push_back(VE.getTypeID(A.getValueType()));
1788 Vals.push_back(A.getType()->getAddressSpace());
1789 Vals.push_back(VE.getValueID(A.getAliasee()));
1795 Vals.push_back(A.isDSOLocal());
1796 Vals.push_back(addToStrtab(A.getPartition()));
1797 Vals.push_back(A.getPartition().size());
1798
1799 unsigned AbbrevToUse = 0;
1800 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
1801 Vals.clear();
1802 }
1803
1804 // Emit the ifunc information.
1805 for (const GlobalIFunc &I : M.ifuncs()) {
1806 // IFUNC: [strtab offset, strtab size, ifunc type, address space, resolver
1807 // val#, linkage, visibility, DSO_Local]
1808 Vals.push_back(addToStrtab(I.getName()));
1809 Vals.push_back(I.getName().size());
1810 Vals.push_back(VE.getTypeID(I.getValueType()));
1811 Vals.push_back(I.getType()->getAddressSpace());
1812 Vals.push_back(VE.getValueID(I.getResolver()));
1815 Vals.push_back(I.isDSOLocal());
1816 Vals.push_back(addToStrtab(I.getPartition()));
1817 Vals.push_back(I.getPartition().size());
1818 Stream.EmitRecord(bitc::MODULE_CODE_IFUNC, Vals);
1819 Vals.clear();
1820 }
1821
1822 writeValueSymbolTableForwardDecl();
1823}
1824
1826 uint64_t Flags = 0;
1827
1828 if (const auto *OBO = dyn_cast<OverflowingBinaryOperator>(V)) {
1829 if (OBO->hasNoSignedWrap())
1830 Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP;
1831 if (OBO->hasNoUnsignedWrap())
1832 Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP;
1833 } else if (const auto *PEO = dyn_cast<PossiblyExactOperator>(V)) {
1834 if (PEO->isExact())
1835 Flags |= 1 << bitc::PEO_EXACT;
1836 } else if (const auto *PDI = dyn_cast<PossiblyDisjointInst>(V)) {
1837 if (PDI->isDisjoint())
1838 Flags |= 1 << bitc::PDI_DISJOINT;
1839 } else if (const auto *FPMO = dyn_cast<FPMathOperator>(V)) {
1840 if (FPMO->hasAllowReassoc())
1841 Flags |= bitc::AllowReassoc;
1842 if (FPMO->hasNoNaNs())
1843 Flags |= bitc::NoNaNs;
1844 if (FPMO->hasNoInfs())
1845 Flags |= bitc::NoInfs;
1846 if (FPMO->hasNoSignedZeros())
1847 Flags |= bitc::NoSignedZeros;
1848 if (FPMO->hasAllowReciprocal())
1849 Flags |= bitc::AllowReciprocal;
1850 if (FPMO->hasAllowContract())
1851 Flags |= bitc::AllowContract;
1852 if (FPMO->hasApproxFunc())
1853 Flags |= bitc::ApproxFunc;
1854
1855 // Handle uitofp.
1856 if (const auto *NNI = dyn_cast<PossiblyNonNegInst>(V)) {
1857 Flags <<= 1;
1858 if (NNI->hasNonNeg())
1859 Flags |= 1 << bitc::PNNI_NON_NEG;
1860 }
1861 } else if (const auto *NNI = dyn_cast<PossiblyNonNegInst>(V)) {
1862 if (NNI->hasNonNeg())
1863 Flags |= 1 << bitc::PNNI_NON_NEG;
1864 } else if (const auto *TI = dyn_cast<TruncInst>(V)) {
1865 if (TI->hasNoSignedWrap())
1866 Flags |= 1 << bitc::TIO_NO_SIGNED_WRAP;
1867 if (TI->hasNoUnsignedWrap())
1868 Flags |= 1 << bitc::TIO_NO_UNSIGNED_WRAP;
1869 } else if (const auto *GEP = dyn_cast<GEPOperator>(V)) {
1870 if (GEP->isInBounds())
1871 Flags |= 1 << bitc::GEP_INBOUNDS;
1872 if (GEP->hasNoUnsignedSignedWrap())
1873 Flags |= 1 << bitc::GEP_NUSW;
1874 if (GEP->hasNoUnsignedWrap())
1875 Flags |= 1 << bitc::GEP_NUW;
1876 } else if (const auto *ICmp = dyn_cast<ICmpInst>(V)) {
1877 if (ICmp->hasSameSign())
1878 Flags |= 1 << bitc::ICMP_SAME_SIGN;
1879 } else if (const auto *ASC = dyn_cast<AddrSpaceCastInst>(V)) {
1880 if (ASC->hasNonNull())
1881 Flags |= 1 << bitc::ASCI_NON_NULL;
1882 }
1883
1884 return Flags;
1885}
1886
1887void ModuleBitcodeWriter::writeValueAsMetadata(
1888 const ValueAsMetadata *MD, SmallVectorImpl<uint64_t> &Record) {
1889 // Mimic an MDNode with a value as one operand.
1890 Value *V = MD->getValue();
1891 Record.push_back(VE.getTypeID(V->getType()));
1892 Record.push_back(VE.getValueID(V));
1893 Stream.EmitRecord(bitc::METADATA_VALUE, Record, 0);
1894 Record.clear();
1895}
1896
1897void ModuleBitcodeWriter::writeMDTuple(const MDTuple *N,
1898 SmallVectorImpl<uint64_t> &Record,
1899 unsigned Abbrev) {
1900 for (const MDOperand &MDO : N->operands()) {
1901 Metadata *MD = MDO;
1902 assert(!(MD && isa<LocalAsMetadata>(MD)) &&
1903 "Unexpected function-local metadata");
1904 Record.push_back(VE.getMetadataOrNullID(MD));
1905 }
1906 Stream.EmitRecord(N->isDistinct() ? bitc::METADATA_DISTINCT_NODE
1908 Record, Abbrev);
1909 Record.clear();
1910}
1911
1912unsigned ModuleBitcodeWriter::createDILocationAbbrev(bool WithIRLayers) {
1913 // Assume the column is usually under 128, and always output the inlined-at
1914 // location (it's never more expensive than building an array size 1).
1915 //
1916 // Separate abbrev so a location without layers does not spend a VBR chunk
1917 // encoding a zero; writeDILocation picks between the two per record.
1918 auto Abbv = std::make_shared<BitCodeAbbrev>();
1919 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_LOCATION));
1920 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isDistinct
1921 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // line
1922 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // column
1923 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // scope
1924 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // inlinedAt
1925 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImplicitCode
1926 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // atomGroup
1927 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // atomRank
1928 if (WithIRLayers)
1929 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // irlayers
1930 return Stream.EmitAbbrev(std::move(Abbv));
1931}
1932
1933void ModuleBitcodeWriter::writeDILocation(const DILocation *N,
1934 SmallVectorImpl<uint64_t> &Record,
1935 unsigned &Abbrev) {
1936 if (!Abbrev)
1937 Abbrev = createDILocationAbbrev(/*WithIRLayers=*/false);
1938
1939 Record.push_back(N->isDistinct());
1940 Record.push_back(N->getLine());
1941 Record.push_back(N->getColumn());
1942 Record.push_back(VE.getMetadataID(N->getScope()));
1943 Record.push_back(VE.getMetadataOrNullID(N->getInlinedAt()));
1944 Record.push_back(N->isImplicitCode());
1945 Record.push_back(N->getAtomGroup());
1946 Record.push_back(N->getAtomRank());
1947
1948 unsigned AbbrevToUse = Abbrev;
1949 if (DILayerLocList *IRLayers = N->getIRLayers()) {
1950 if (!DILocationLayersAbbrev)
1951 DILocationLayersAbbrev = createDILocationAbbrev(/*WithIRLayers=*/true);
1952 AbbrevToUse = DILocationLayersAbbrev;
1953 Record.push_back(VE.getMetadataOrNullID(IRLayers));
1954 }
1955
1956 Stream.EmitRecord(bitc::METADATA_LOCATION, Record, AbbrevToUse);
1957 Record.clear();
1958}
1959
1960void ModuleBitcodeWriter::writeDILayerLoc(const DILayerLoc *N,
1961 SmallVectorImpl<uint64_t> &Record,
1962 unsigned Abbrev) {
1963 Record.push_back(N->isDistinct());
1964 Record.push_back(N->getLine());
1965 Record.push_back(N->getColumn());
1966 Record.push_back(VE.getMetadataID(N->getRawFile()));
1967 Record.push_back(VE.getMetadataID(N->getRawKind()));
1968 Stream.EmitRecord(bitc::METADATA_LAYERLOC, Record, Abbrev);
1969 Record.clear();
1970}
1971
1972void ModuleBitcodeWriter::writeDILayerLocList(const DILayerLocList *N,
1973 SmallVectorImpl<uint64_t> &Record,
1974 unsigned Abbrev) {
1975 Record.push_back(N->isDistinct());
1976 for (const MDOperand &Op : N->layers())
1977 Record.push_back(VE.getMetadataID(Op.get()));
1978 Stream.EmitRecord(bitc::METADATA_LAYERLOCLIST, Record, Abbrev);
1979 Record.clear();
1980}
1981
1982unsigned ModuleBitcodeWriter::createGenericDINodeAbbrev() {
1983 // Assume the column is usually under 128, and always output the inlined-at
1984 // location (it's never more expensive than building an array size 1).
1985 auto Abbv = std::make_shared<BitCodeAbbrev>();
1986 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_GENERIC_DEBUG));
1987 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1988 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1989 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1990 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1991 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1992 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1993 return Stream.EmitAbbrev(std::move(Abbv));
1994}
1995
1996void ModuleBitcodeWriter::writeGenericDINode(const GenericDINode *N,
1997 SmallVectorImpl<uint64_t> &Record,
1998 unsigned &Abbrev) {
1999 if (!Abbrev)
2000 Abbrev = createGenericDINodeAbbrev();
2001
2002 Record.push_back(N->isDistinct());
2003 Record.push_back(N->getTag());
2004 Record.push_back(0); // Per-tag version field; unused for now.
2005
2006 for (auto &I : N->operands())
2007 Record.push_back(VE.getMetadataOrNullID(I));
2008
2009 Stream.EmitRecord(bitc::METADATA_GENERIC_DEBUG, Record, Abbrev);
2010 Record.clear();
2011}
2012
2013void ModuleBitcodeWriter::writeDISubrange(const DISubrange *N,
2014 SmallVectorImpl<uint64_t> &Record,
2015 unsigned Abbrev) {
2016 const uint64_t Version = 2 << 1;
2017 Record.push_back((uint64_t)N->isDistinct() | Version);
2018 Record.push_back(VE.getMetadataOrNullID(N->getRawCountNode()));
2019 Record.push_back(VE.getMetadataOrNullID(N->getRawLowerBound()));
2020 Record.push_back(VE.getMetadataOrNullID(N->getRawUpperBound()));
2021 Record.push_back(VE.getMetadataOrNullID(N->getRawStride()));
2022
2023 Stream.EmitRecord(bitc::METADATA_SUBRANGE, Record, Abbrev);
2024 Record.clear();
2025}
2026
2027void ModuleBitcodeWriter::writeDIGenericSubrange(
2028 const DIGenericSubrange *N, SmallVectorImpl<uint64_t> &Record,
2029 unsigned Abbrev) {
2030 Record.push_back((uint64_t)N->isDistinct());
2031 Record.push_back(VE.getMetadataOrNullID(N->getRawCountNode()));
2032 Record.push_back(VE.getMetadataOrNullID(N->getRawLowerBound()));
2033 Record.push_back(VE.getMetadataOrNullID(N->getRawUpperBound()));
2034 Record.push_back(VE.getMetadataOrNullID(N->getRawStride()));
2035
2036 Stream.EmitRecord(bitc::METADATA_GENERIC_SUBRANGE, Record, Abbrev);
2037 Record.clear();
2038}
2039
2040void ModuleBitcodeWriter::writeDIEnumerator(const DIEnumerator *N,
2041 SmallVectorImpl<uint64_t> &Record,
2042 unsigned Abbrev) {
2043 const uint64_t IsBigInt = 1 << 2;
2044 Record.push_back(IsBigInt | (N->isUnsigned() << 1) | N->isDistinct());
2045 Record.push_back(N->getValue().getBitWidth());
2046 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2047 emitWideAPInt(Record, N->getValue());
2048
2049 Stream.EmitRecord(bitc::METADATA_ENUMERATOR, Record, Abbrev);
2050 Record.clear();
2051}
2052
2053void ModuleBitcodeWriter::writeDIBasicType(const DIBasicType *N,
2054 SmallVectorImpl<uint64_t> &Record,
2055 unsigned Abbrev) {
2056 const unsigned SizeIsMetadata = 0x2;
2057 Record.push_back(SizeIsMetadata | (unsigned)N->isDistinct());
2058 Record.push_back(N->getTag());
2059 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2060 Record.push_back(VE.getMetadataOrNullID(N->getRawSizeInBits()));
2061 Record.push_back(N->getAlignInBits());
2062 Record.push_back(N->getEncoding());
2063 Record.push_back(N->getFlags());
2064 Record.push_back(N->getNumExtraInhabitants());
2065 Record.push_back(N->getDataSizeInBits());
2066 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2067 Record.push_back(N->getLine());
2068 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
2069
2070 Stream.EmitRecord(bitc::METADATA_BASIC_TYPE, Record, Abbrev);
2071 Record.clear();
2072}
2073
2074void ModuleBitcodeWriter::writeDIFixedPointType(
2075 const DIFixedPointType *N, SmallVectorImpl<uint64_t> &Record,
2076 unsigned Abbrev) {
2077 const unsigned SizeIsMetadata = 0x2;
2078 Record.push_back(SizeIsMetadata | (unsigned)N->isDistinct());
2079 Record.push_back(N->getTag());
2080 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2081 Record.push_back(VE.getMetadataOrNullID(N->getRawSizeInBits()));
2082 Record.push_back(N->getAlignInBits());
2083 Record.push_back(N->getEncoding());
2084 Record.push_back(N->getFlags());
2085 Record.push_back(N->getKind());
2086 Record.push_back(N->getFactorRaw());
2087
2088 auto WriteWideInt = [&](const APInt &Value) {
2089 // Write an encoded word that holds the number of active words and
2090 // the number of bits.
2091 uint64_t NumWords = Value.getActiveWords();
2092 uint64_t Encoded = (NumWords << 32) | Value.getBitWidth();
2093 Record.push_back(Encoded);
2094 emitWideAPInt(Record, Value);
2095 };
2096
2097 WriteWideInt(N->getNumeratorRaw());
2098 WriteWideInt(N->getDenominatorRaw());
2099
2100 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2101 Record.push_back(N->getLine());
2102 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
2103
2104 Stream.EmitRecord(bitc::METADATA_FIXED_POINT_TYPE, Record, Abbrev);
2105 Record.clear();
2106}
2107
2108void ModuleBitcodeWriter::writeDIStringType(const DIStringType *N,
2109 SmallVectorImpl<uint64_t> &Record,
2110 unsigned Abbrev) {
2111 const unsigned SizeIsMetadata = 0x2;
2112 Record.push_back(SizeIsMetadata | (unsigned)N->isDistinct());
2113 Record.push_back(N->getTag());
2114 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2115 Record.push_back(VE.getMetadataOrNullID(N->getStringLength()));
2116 Record.push_back(VE.getMetadataOrNullID(N->getStringLengthExp()));
2117 Record.push_back(VE.getMetadataOrNullID(N->getStringLocationExp()));
2118 Record.push_back(VE.getMetadataOrNullID(N->getRawSizeInBits()));
2119 Record.push_back(N->getAlignInBits());
2120 Record.push_back(N->getEncoding());
2121 Record.push_back(VE.getMetadataOrNullID(N->getRawCharType()));
2122
2123 Stream.EmitRecord(bitc::METADATA_STRING_TYPE, Record, Abbrev);
2124 Record.clear();
2125}
2126
2127void ModuleBitcodeWriter::writeDIDerivedType(const DIDerivedType *N,
2128 SmallVectorImpl<uint64_t> &Record,
2129 unsigned Abbrev) {
2130 const unsigned SizeIsMetadata = 0x2;
2131 Record.push_back(SizeIsMetadata | (unsigned)N->isDistinct());
2132 Record.push_back(N->getTag());
2133 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2134 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2135 Record.push_back(N->getLine());
2136 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
2137 Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
2138 Record.push_back(VE.getMetadataOrNullID(N->getRawSizeInBits()));
2139 Record.push_back(N->getAlignInBits());
2140 Record.push_back(VE.getMetadataOrNullID(N->getRawOffsetInBits()));
2141 Record.push_back(N->getFlags());
2142 Record.push_back(VE.getMetadataOrNullID(N->getExtraData()));
2143
2144 // DWARF address space is encoded as N->getDWARFAddressSpace() + 1. 0 means
2145 // that there is no DWARF address space associated with DIDerivedType.
2146 if (const auto &DWARFAddressSpace = N->getDWARFAddressSpace())
2147 Record.push_back(*DWARFAddressSpace + 1);
2148 else
2149 Record.push_back(0);
2150
2151 Record.push_back(VE.getMetadataOrNullID(N->getAnnotations().get()));
2152
2153 if (auto PtrAuthData = N->getPtrAuthData())
2154 Record.push_back(PtrAuthData->RawData);
2155 else
2156 Record.push_back(0);
2157
2158 Stream.EmitRecord(bitc::METADATA_DERIVED_TYPE, Record, Abbrev);
2159 Record.clear();
2160}
2161
2162void ModuleBitcodeWriter::writeDISubrangeType(const DISubrangeType *N,
2163 SmallVectorImpl<uint64_t> &Record,
2164 unsigned Abbrev) {
2165 const unsigned SizeIsMetadata = 0x2;
2166 Record.push_back(SizeIsMetadata | (unsigned)N->isDistinct());
2167 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2168 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2169 Record.push_back(N->getLine());
2170 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
2171 Record.push_back(VE.getMetadataOrNullID(N->getRawSizeInBits()));
2172 Record.push_back(N->getAlignInBits());
2173 Record.push_back(N->getFlags());
2174 Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
2175 Record.push_back(VE.getMetadataOrNullID(N->getRawLowerBound()));
2176 Record.push_back(VE.getMetadataOrNullID(N->getRawUpperBound()));
2177 Record.push_back(VE.getMetadataOrNullID(N->getRawStride()));
2178 Record.push_back(VE.getMetadataOrNullID(N->getRawBias()));
2179
2180 Stream.EmitRecord(bitc::METADATA_SUBRANGE_TYPE, Record, Abbrev);
2181 Record.clear();
2182}
2183
2184void ModuleBitcodeWriter::writeDICompositeType(
2185 const DICompositeType *N, SmallVectorImpl<uint64_t> &Record,
2186 unsigned Abbrev) {
2187 const unsigned IsNotUsedInOldTypeRef = 0x2;
2188 const unsigned SizeIsMetadata = 0x4;
2189 Record.push_back(SizeIsMetadata | IsNotUsedInOldTypeRef |
2190 (unsigned)N->isDistinct());
2191 Record.push_back(N->getTag());
2192 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2193 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2194 Record.push_back(N->getLine());
2195 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
2196 Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
2197 Record.push_back(VE.getMetadataOrNullID(N->getRawSizeInBits()));
2198 Record.push_back(N->getAlignInBits());
2199 Record.push_back(VE.getMetadataOrNullID(N->getRawOffsetInBits()));
2200 Record.push_back(N->getFlags());
2201 Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
2202 Record.push_back(N->getRuntimeLang());
2203 Record.push_back(VE.getMetadataOrNullID(N->getVTableHolder()));
2204 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
2205 Record.push_back(VE.getMetadataOrNullID(N->getRawIdentifier()));
2206 Record.push_back(VE.getMetadataOrNullID(N->getDiscriminator()));
2207 Record.push_back(VE.getMetadataOrNullID(N->getRawDataLocation()));
2208 Record.push_back(VE.getMetadataOrNullID(N->getRawAssociated()));
2209 Record.push_back(VE.getMetadataOrNullID(N->getRawAllocated()));
2210 Record.push_back(VE.getMetadataOrNullID(N->getRawRank()));
2211 Record.push_back(VE.getMetadataOrNullID(N->getAnnotations().get()));
2212 Record.push_back(N->getNumExtraInhabitants());
2213 Record.push_back(VE.getMetadataOrNullID(N->getRawSpecification()));
2214 Record.push_back(
2215 N->getEnumKind().value_or(dwarf::DW_APPLE_ENUM_KIND_invalid));
2216 Record.push_back(VE.getMetadataOrNullID(N->getRawBitStride()));
2217
2218 Stream.EmitRecord(bitc::METADATA_COMPOSITE_TYPE, Record, Abbrev);
2219 Record.clear();
2220}
2221
2222void ModuleBitcodeWriter::writeDISubroutineType(
2223 const DISubroutineType *N, SmallVectorImpl<uint64_t> &Record,
2224 unsigned Abbrev) {
2225 const unsigned HasNoOldTypeRefs = 0x2;
2226 Record.push_back(HasNoOldTypeRefs | (unsigned)N->isDistinct());
2227 Record.push_back(N->getFlags());
2228 Record.push_back(VE.getMetadataOrNullID(N->getTypeArray().get()));
2229 Record.push_back(N->getCC());
2230
2231 Stream.EmitRecord(bitc::METADATA_SUBROUTINE_TYPE, Record, Abbrev);
2232 Record.clear();
2233}
2234
2235void ModuleBitcodeWriter::writeDIFile(const DIFile *N,
2236 SmallVectorImpl<uint64_t> &Record,
2237 unsigned Abbrev) {
2238 Record.push_back(N->isDistinct());
2239 Record.push_back(VE.getMetadataOrNullID(N->getRawFilename()));
2240 Record.push_back(VE.getMetadataOrNullID(N->getRawDirectory()));
2241 if (N->getRawChecksum()) {
2242 Record.push_back(N->getRawChecksum()->Kind);
2243 Record.push_back(VE.getMetadataOrNullID(N->getRawChecksum()->Value));
2244 } else {
2245 // Maintain backwards compatibility with the old internal representation of
2246 // CSK_None in ChecksumKind by writing nulls here when Checksum is None.
2247 Record.push_back(0);
2248 Record.push_back(VE.getMetadataOrNullID(nullptr));
2249 }
2250 auto Source = N->getRawSource();
2251 if (Source)
2252 Record.push_back(VE.getMetadataOrNullID(Source));
2253
2254 Stream.EmitRecord(bitc::METADATA_FILE, Record, Abbrev);
2255 Record.clear();
2256}
2257
2258void ModuleBitcodeWriter::writeDICompileUnit(const DICompileUnit *N,
2259 SmallVectorImpl<uint64_t> &Record,
2260 unsigned Abbrev) {
2261 assert(N->isDistinct() && "Expected distinct compile units");
2262 Record.push_back(/* IsDistinct */ true);
2263
2264 auto Lang = N->getSourceLanguage();
2265 Record.push_back(Lang.getName());
2266 // Set bit so the MetadataLoader can distniguish between versioned and
2267 // unversioned names.
2268 if (Lang.hasVersionedName())
2269 Record.back() ^= (uint64_t(1) << 63);
2270
2271 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2272 Record.push_back(VE.getMetadataOrNullID(N->getRawProducer()));
2273 Record.push_back(N->isOptimized());
2274 Record.push_back(VE.getMetadataOrNullID(N->getRawFlags()));
2275 Record.push_back(N->getRuntimeVersion());
2276 Record.push_back(VE.getMetadataOrNullID(N->getRawSplitDebugFilename()));
2277 Record.push_back(N->getEmissionKind());
2278 Record.push_back(VE.getMetadataOrNullID(N->getEnumTypes().get()));
2279 Record.push_back(VE.getMetadataOrNullID(N->getRetainedTypes().get()));
2280 Record.push_back(/* subprograms */ 0);
2281 Record.push_back(VE.getMetadataOrNullID(N->getGlobalVariables().get()));
2282 Record.push_back(VE.getMetadataOrNullID(N->getImportedEntities().get()));
2283 Record.push_back(N->getDWOId());
2284 Record.push_back(VE.getMetadataOrNullID(N->getMacros().get()));
2285 Record.push_back(N->getSplitDebugInlining());
2286 Record.push_back(N->getDebugInfoForProfiling());
2287 Record.push_back((unsigned)N->getNameTableKind());
2288 Record.push_back(N->getRangesBaseAddress());
2289 Record.push_back(VE.getMetadataOrNullID(N->getRawSysRoot()));
2290 Record.push_back(VE.getMetadataOrNullID(N->getRawSDK()));
2291 Record.push_back(Lang.hasVersionedName() ? Lang.getVersion() : 0);
2292 Record.push_back(Lang.getDialect());
2293
2294 Stream.EmitRecord(bitc::METADATA_COMPILE_UNIT, Record, Abbrev);
2295 Record.clear();
2296}
2297
2298void ModuleBitcodeWriter::writeDISubprogram(const DISubprogram *N,
2299 SmallVectorImpl<uint64_t> &Record,
2300 unsigned Abbrev) {
2301 const uint64_t HasUnitFlag = 1 << 1;
2302 const uint64_t HasSPFlagsFlag = 1 << 2;
2303 Record.push_back(uint64_t(N->isDistinct()) | HasUnitFlag | HasSPFlagsFlag);
2304 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
2305 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2306 Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
2307 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2308 Record.push_back(N->getLine());
2309 Record.push_back(VE.getMetadataOrNullID(N->getType()));
2310 Record.push_back(N->getScopeLine());
2311 Record.push_back(VE.getMetadataOrNullID(N->getContainingType()));
2312 Record.push_back(N->getSPFlags());
2313 Record.push_back(N->getVirtualIndex());
2314 Record.push_back(N->getFlags());
2315 Record.push_back(VE.getMetadataOrNullID(N->getRawUnit()));
2316 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
2317 Record.push_back(VE.getMetadataOrNullID(N->getDeclaration()));
2318 Record.push_back(VE.getMetadataOrNullID(N->getRetainedNodes().get()));
2319 Record.push_back(N->getThisAdjustment());
2320 Record.push_back(VE.getMetadataOrNullID(N->getThrownTypes().get()));
2321 Record.push_back(VE.getMetadataOrNullID(N->getAnnotations().get()));
2322 Record.push_back(VE.getMetadataOrNullID(N->getRawTargetFuncName()));
2323 Record.push_back(N->getKeyInstructionsEnabled());
2324
2325 Stream.EmitRecord(bitc::METADATA_SUBPROGRAM, Record, Abbrev);
2326 Record.clear();
2327}
2328
2329void ModuleBitcodeWriter::writeDILexicalBlock(const DILexicalBlock *N,
2330 SmallVectorImpl<uint64_t> &Record,
2331 unsigned Abbrev) {
2332 Record.push_back(N->isDistinct());
2333 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
2334 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2335 Record.push_back(N->getLine());
2336 Record.push_back(N->getColumn());
2337
2338 Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK, Record, Abbrev);
2339 Record.clear();
2340}
2341
2342void ModuleBitcodeWriter::writeDILexicalBlockFile(
2343 const DILexicalBlockFile *N, SmallVectorImpl<uint64_t> &Record,
2344 unsigned Abbrev) {
2345 Record.push_back(N->isDistinct());
2346 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
2347 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2348 Record.push_back(N->getDiscriminator());
2349
2350 Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK_FILE, Record, Abbrev);
2351 Record.clear();
2352}
2353
2354void ModuleBitcodeWriter::writeDICommonBlock(const DICommonBlock *N,
2355 SmallVectorImpl<uint64_t> &Record,
2356 unsigned Abbrev) {
2357 Record.push_back(N->isDistinct());
2358 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
2359 Record.push_back(VE.getMetadataOrNullID(N->getDecl()));
2360 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2361 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2362 Record.push_back(N->getLineNo());
2363
2364 Stream.EmitRecord(bitc::METADATA_COMMON_BLOCK, Record, Abbrev);
2365 Record.clear();
2366}
2367
2368void ModuleBitcodeWriter::writeDINamespace(const DINamespace *N,
2369 SmallVectorImpl<uint64_t> &Record,
2370 unsigned Abbrev) {
2371 Record.push_back(N->isDistinct() | N->getExportSymbols() << 1);
2372 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
2373 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2374
2375 Stream.EmitRecord(bitc::METADATA_NAMESPACE, Record, Abbrev);
2376 Record.clear();
2377}
2378
2379void ModuleBitcodeWriter::writeDIMacro(const DIMacro *N,
2380 SmallVectorImpl<uint64_t> &Record,
2381 unsigned Abbrev) {
2382 Record.push_back(N->isDistinct());
2383 Record.push_back(N->getMacinfoType());
2384 Record.push_back(N->getLine());
2385 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2386 Record.push_back(VE.getMetadataOrNullID(N->getRawValue()));
2387
2388 Stream.EmitRecord(bitc::METADATA_MACRO, Record, Abbrev);
2389 Record.clear();
2390}
2391
2392void ModuleBitcodeWriter::writeDIMacroFile(const DIMacroFile *N,
2393 SmallVectorImpl<uint64_t> &Record,
2394 unsigned Abbrev) {
2395 Record.push_back(N->isDistinct());
2396 Record.push_back(N->getMacinfoType());
2397 Record.push_back(N->getLine());
2398 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2399 Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
2400
2401 Stream.EmitRecord(bitc::METADATA_MACRO_FILE, Record, Abbrev);
2402 Record.clear();
2403}
2404
2405void ModuleBitcodeWriter::writeDIArgList(const DIArgList *N,
2406 SmallVectorImpl<uint64_t> &Record) {
2407 Record.reserve(N->getArgs().size());
2408 for (ValueAsMetadata *MD : N->getArgs())
2409 Record.push_back(VE.getMetadataID(MD));
2410
2411 Stream.EmitRecord(bitc::METADATA_ARG_LIST, Record);
2412 Record.clear();
2413}
2414
2415void ModuleBitcodeWriter::writeDIModule(const DIModule *N,
2416 SmallVectorImpl<uint64_t> &Record,
2417 unsigned Abbrev) {
2418 Record.push_back(N->isDistinct());
2419 for (auto &I : N->operands())
2420 Record.push_back(VE.getMetadataOrNullID(I));
2421 Record.push_back(N->getLineNo());
2422 Record.push_back(N->getIsDecl());
2423
2424 Stream.EmitRecord(bitc::METADATA_MODULE, Record, Abbrev);
2425 Record.clear();
2426}
2427
2428void ModuleBitcodeWriter::writeDIAssignID(const DIAssignID *N,
2429 SmallVectorImpl<uint64_t> &Record,
2430 unsigned Abbrev) {
2431 // There are no arguments for this metadata type.
2432 Record.push_back(N->isDistinct());
2433 Stream.EmitRecord(bitc::METADATA_ASSIGN_ID, Record, Abbrev);
2434 Record.clear();
2435}
2436
2437void ModuleBitcodeWriter::writeDITemplateTypeParameter(
2438 const DITemplateTypeParameter *N, SmallVectorImpl<uint64_t> &Record,
2439 unsigned Abbrev) {
2440 Record.push_back(N->isDistinct());
2441 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2442 Record.push_back(VE.getMetadataOrNullID(N->getType()));
2443 Record.push_back(N->isDefault());
2444
2445 Stream.EmitRecord(bitc::METADATA_TEMPLATE_TYPE, Record, Abbrev);
2446 Record.clear();
2447}
2448
2449void ModuleBitcodeWriter::writeDITemplateValueParameter(
2450 const DITemplateValueParameter *N, SmallVectorImpl<uint64_t> &Record,
2451 unsigned Abbrev) {
2452 Record.push_back(N->isDistinct());
2453 Record.push_back(N->getTag());
2454 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2455 Record.push_back(VE.getMetadataOrNullID(N->getType()));
2456 Record.push_back(N->isDefault());
2457 Record.push_back(VE.getMetadataOrNullID(N->getValue()));
2458
2459 Stream.EmitRecord(bitc::METADATA_TEMPLATE_VALUE, Record, Abbrev);
2460 Record.clear();
2461}
2462
2463void ModuleBitcodeWriter::writeDIGlobalVariable(
2464 const DIGlobalVariable *N, SmallVectorImpl<uint64_t> &Record,
2465 unsigned Abbrev) {
2466 const uint64_t Version = 2 << 1;
2467 Record.push_back((uint64_t)N->isDistinct() | Version);
2468 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
2469 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2470 Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
2471 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2472 Record.push_back(N->getLine());
2473 Record.push_back(VE.getMetadataOrNullID(N->getType()));
2474 Record.push_back(N->isLocalToUnit());
2475 Record.push_back(N->isDefinition());
2476 Record.push_back(VE.getMetadataOrNullID(N->getStaticDataMemberDeclaration()));
2477 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams()));
2478 Record.push_back(N->getAlignInBits());
2479 Record.push_back(VE.getMetadataOrNullID(N->getAnnotations().get()));
2480
2481 Stream.EmitRecord(bitc::METADATA_GLOBAL_VAR, Record, Abbrev);
2482 Record.clear();
2483}
2484
2485void ModuleBitcodeWriter::writeDILocalVariable(
2486 const DILocalVariable *N, SmallVectorImpl<uint64_t> &Record,
2487 unsigned Abbrev) {
2488 // In order to support all possible bitcode formats in BitcodeReader we need
2489 // to distinguish the following cases:
2490 // 1) Record has no artificial tag (Record[1]),
2491 // has no obsolete inlinedAt field (Record[9]).
2492 // In this case Record size will be 8, HasAlignment flag is false.
2493 // 2) Record has artificial tag (Record[1]),
2494 // has no obsolete inlignedAt field (Record[9]).
2495 // In this case Record size will be 9, HasAlignment flag is false.
2496 // 3) Record has both artificial tag (Record[1]) and
2497 // obsolete inlignedAt field (Record[9]).
2498 // In this case Record size will be 10, HasAlignment flag is false.
2499 // 4) Record has neither artificial tag, nor inlignedAt field, but
2500 // HasAlignment flag is true and Record[8] contains alignment value.
2501 const uint64_t HasAlignmentFlag = 1 << 1;
2502 Record.push_back((uint64_t)N->isDistinct() | HasAlignmentFlag);
2503 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
2504 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2505 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2506 Record.push_back(N->getLine());
2507 Record.push_back(VE.getMetadataOrNullID(N->getType()));
2508 Record.push_back(N->getArg());
2509 Record.push_back(N->getFlags());
2510 Record.push_back(N->getAlignInBits());
2511 Record.push_back(VE.getMetadataOrNullID(N->getAnnotations().get()));
2512
2513 Stream.EmitRecord(bitc::METADATA_LOCAL_VAR, Record, Abbrev);
2514 Record.clear();
2515}
2516
2517void ModuleBitcodeWriter::writeDILabel(
2518 const DILabel *N, SmallVectorImpl<uint64_t> &Record,
2519 unsigned Abbrev) {
2520 uint64_t IsArtificialFlag = uint64_t(N->isArtificial()) << 1;
2521 Record.push_back((uint64_t)N->isDistinct() | IsArtificialFlag);
2522 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
2523 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2524 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2525 Record.push_back(N->getLine());
2526 Record.push_back(N->getColumn());
2527 Record.push_back(N->getCoroSuspendIdx().has_value()
2528 ? (uint64_t)N->getCoroSuspendIdx().value()
2529 : std::numeric_limits<uint64_t>::max());
2530
2531 Stream.EmitRecord(bitc::METADATA_LABEL, Record, Abbrev);
2532 Record.clear();
2533}
2534
2535void ModuleBitcodeWriter::writeDIExpression(const DIExpression *N,
2536 SmallVectorImpl<uint64_t> &Record,
2537 unsigned Abbrev) {
2538 Record.reserve(N->getElements().size() + 1);
2539 const uint64_t Version = 3 << 1;
2540 Record.push_back((uint64_t)N->isDistinct() | Version);
2541 Record.append(N->elements_begin(), N->elements_end());
2542
2543 Stream.EmitRecord(bitc::METADATA_EXPRESSION, Record, Abbrev);
2544 Record.clear();
2545}
2546
2547void ModuleBitcodeWriter::writeDIGlobalVariableExpression(
2548 const DIGlobalVariableExpression *N, SmallVectorImpl<uint64_t> &Record,
2549 unsigned Abbrev) {
2550 Record.push_back(N->isDistinct());
2551 Record.push_back(VE.getMetadataOrNullID(N->getVariable()));
2552 Record.push_back(VE.getMetadataOrNullID(N->getExpression()));
2553
2554 Stream.EmitRecord(bitc::METADATA_GLOBAL_VAR_EXPR, Record, Abbrev);
2555 Record.clear();
2556}
2557
2558void ModuleBitcodeWriter::writeDIObjCProperty(const DIObjCProperty *N,
2559 SmallVectorImpl<uint64_t> &Record,
2560 unsigned Abbrev) {
2561 Record.push_back(N->isDistinct());
2562 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2563 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2564 Record.push_back(N->getLine());
2565 Record.push_back(VE.getMetadataOrNullID(N->getRawSetterName()));
2566 Record.push_back(VE.getMetadataOrNullID(N->getRawGetterName()));
2567 Record.push_back(N->getAttributes());
2568 Record.push_back(VE.getMetadataOrNullID(N->getType()));
2569
2570 Stream.EmitRecord(bitc::METADATA_OBJC_PROPERTY, Record, Abbrev);
2571 Record.clear();
2572}
2573
2574void ModuleBitcodeWriter::writeDIProperty(const DIProperty *N,
2575 SmallVectorImpl<uint64_t> &Record,
2576 unsigned Abbrev) {
2577 Record.push_back(N->isDistinct());
2578 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2579 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
2580 Record.push_back(N->getLine());
2581 Record.push_back(VE.getMetadataOrNullID(N->getType()));
2582 Record.push_back(VE.getMetadataOrNullID(N->getBackingStorage()));
2583
2584 Stream.EmitRecord(bitc::METADATA_PROPERTY, Record, Abbrev);
2585 Record.clear();
2586}
2587
2588void ModuleBitcodeWriter::writeDIImportedEntity(
2589 const DIImportedEntity *N, SmallVectorImpl<uint64_t> &Record,
2590 unsigned Abbrev) {
2591 Record.push_back(N->isDistinct());
2592 Record.push_back(N->getTag());
2593 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
2594 Record.push_back(VE.getMetadataOrNullID(N->getEntity()));
2595 Record.push_back(N->getLine());
2596 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
2597 Record.push_back(VE.getMetadataOrNullID(N->getRawFile()));
2598 Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
2599
2600 Stream.EmitRecord(bitc::METADATA_IMPORTED_ENTITY, Record, Abbrev);
2601 Record.clear();
2602}
2603
2604unsigned ModuleBitcodeWriter::createNamedMetadataAbbrev() {
2605 auto Abbv = std::make_shared<BitCodeAbbrev>();
2606 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_NAME));
2607 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2608 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
2609 return Stream.EmitAbbrev(std::move(Abbv));
2610}
2611
2612void ModuleBitcodeWriter::writeNamedMetadata(
2613 SmallVectorImpl<uint64_t> &Record) {
2614 if (M.named_metadata_empty())
2615 return;
2616
2617 unsigned Abbrev = createNamedMetadataAbbrev();
2618 for (const NamedMDNode &NMD : M.named_metadata()) {
2619 // Write name.
2620 StringRef Str = NMD.getName();
2621 Record.append(Str.bytes_begin(), Str.bytes_end());
2622 Stream.EmitRecord(bitc::METADATA_NAME, Record, Abbrev);
2623 Record.clear();
2624
2625 // Write named metadata operands.
2626 for (const MDNode *N : NMD.operands())
2627 Record.push_back(VE.getMetadataID(N));
2628 Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0);
2629 Record.clear();
2630 }
2631}
2632
2633unsigned ModuleBitcodeWriter::createMetadataStringsAbbrev() {
2634 auto Abbv = std::make_shared<BitCodeAbbrev>();
2635 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRINGS));
2636 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of strings
2637 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // offset to chars
2638 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2639 return Stream.EmitAbbrev(std::move(Abbv));
2640}
2641
2642/// Write out a record for MDString.
2643///
2644/// All the metadata strings in a metadata block are emitted in a single
2645/// record. The sizes and strings themselves are shoved into a blob.
2646void ModuleBitcodeWriter::writeMetadataStrings(
2647 ArrayRef<const Metadata *> Strings, SmallVectorImpl<uint64_t> &Record) {
2648 if (Strings.empty())
2649 return;
2650
2651 // Start the record with the number of strings.
2652 Record.push_back(bitc::METADATA_STRINGS);
2653 Record.push_back(Strings.size());
2654
2655 // Emit the sizes of the strings in the blob.
2656 SmallString<256> Blob;
2657 {
2658 BitstreamWriter W(Blob);
2659 for (const Metadata *MD : Strings)
2660 W.EmitVBR(cast<MDString>(MD)->getLength(), 6);
2661 W.FlushToWord();
2662 }
2663
2664 // Add the offset to the strings to the record.
2665 Record.push_back(Blob.size());
2666
2667 // Add the strings to the blob.
2668 for (const Metadata *MD : Strings)
2669 Blob.append(cast<MDString>(MD)->getString());
2670
2671 // Emit the final record.
2672 Stream.EmitRecordWithBlob(createMetadataStringsAbbrev(), Record, Blob);
2673 Record.clear();
2674}
2675
2676// Generates an enum to use as an index in the Abbrev array of Metadata record.
2677enum MetadataAbbrev : unsigned {
2678#define HANDLE_MDNODE_LEAF(CLASS) CLASS##AbbrevID,
2679#include "llvm/IR/Metadata.def"
2681};
2682
2683void ModuleBitcodeWriter::writeMetadataRecords(
2684 ArrayRef<const Metadata *> MDs, SmallVectorImpl<uint64_t> &Record,
2685 std::vector<unsigned> *MDAbbrevs, std::vector<uint64_t> *IndexPos) {
2686 if (MDs.empty())
2687 return;
2688
2689 // Initialize MDNode abbreviations.
2690#define HANDLE_MDNODE_LEAF(CLASS) unsigned CLASS##Abbrev = 0;
2691#include "llvm/IR/Metadata.def"
2692
2693 for (const Metadata *MD : MDs) {
2694 if (IndexPos)
2695 IndexPos->push_back(Stream.GetCurrentBitNo());
2696 if (const MDNode *N = dyn_cast<MDNode>(MD)) {
2697 assert(N->isResolved() && "Expected forward references to be resolved");
2698
2699 switch (N->getMetadataID()) {
2700 default:
2701 llvm_unreachable("Invalid MDNode subclass");
2702#define HANDLE_MDNODE_LEAF(CLASS) \
2703 case Metadata::CLASS##Kind: \
2704 if (MDAbbrevs) \
2705 write##CLASS(cast<CLASS>(N), Record, \
2706 (*MDAbbrevs)[MetadataAbbrev::CLASS##AbbrevID]); \
2707 else \
2708 write##CLASS(cast<CLASS>(N), Record, CLASS##Abbrev); \
2709 continue;
2710#include "llvm/IR/Metadata.def"
2711 }
2712 }
2713 if (auto *AL = dyn_cast<DIArgList>(MD)) {
2715 continue;
2716 }
2717 writeValueAsMetadata(cast<ValueAsMetadata>(MD), Record);
2718 }
2719}
2720
2721void ModuleBitcodeWriter::writeModuleMetadata() {
2722 if (!VE.hasMDs() && M.named_metadata_empty())
2723 return;
2724
2726 SmallVector<uint64_t, 64> Record;
2727
2728 // Emit all abbrevs upfront, so that the reader can jump in the middle of the
2729 // block and load any metadata.
2730 std::vector<unsigned> MDAbbrevs;
2731
2732 MDAbbrevs.resize(MetadataAbbrev::LastPlusOne);
2733 MDAbbrevs[MetadataAbbrev::DILocationAbbrevID] =
2734 createDILocationAbbrev(/*WithIRLayers=*/false);
2735 DILocationLayersAbbrev = createDILocationAbbrev(/*WithIRLayers=*/true);
2736 MDAbbrevs[MetadataAbbrev::GenericDINodeAbbrevID] =
2737 createGenericDINodeAbbrev();
2738
2739 auto Abbv = std::make_shared<BitCodeAbbrev>();
2740 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_INDEX_OFFSET));
2741 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2742 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2743 unsigned OffsetAbbrev = Stream.EmitAbbrev(std::move(Abbv));
2744
2745 Abbv = std::make_shared<BitCodeAbbrev>();
2746 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_INDEX));
2747 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2748 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
2749 unsigned IndexAbbrev = Stream.EmitAbbrev(std::move(Abbv));
2750
2751 // Emit MDStrings together upfront.
2752 writeMetadataStrings(VE.getMDStrings(), Record);
2753
2754 // We only emit an index for the metadata record if we have more than a given
2755 // (naive) threshold of metadatas, otherwise it is not worth it.
2756 if (VE.getNonMDStrings().size() > Opts.bitcode_mdindex_threshold) {
2757 // Write a placeholder value in for the offset of the metadata index,
2758 // which is written after the records, so that it can include
2759 // the offset of each entry. The placeholder offset will be
2760 // updated after all records are emitted.
2761 uint64_t Vals[] = {0, 0};
2762 Stream.EmitRecord(bitc::METADATA_INDEX_OFFSET, Vals, OffsetAbbrev);
2763 }
2764
2765 // Compute and save the bit offset to the current position, which will be
2766 // patched when we emit the index later. We can simply subtract the 64-bit
2767 // fixed size from the current bit number to get the location to backpatch.
2768 uint64_t IndexOffsetRecordBitPos = Stream.GetCurrentBitNo();
2769
2770 // This index will contain the bitpos for each individual record.
2771 std::vector<uint64_t> IndexPos;
2772 IndexPos.reserve(VE.getNonMDStrings().size());
2773
2774 // Write all the records
2775 writeMetadataRecords(VE.getNonMDStrings(), Record, &MDAbbrevs, &IndexPos);
2776
2777 if (VE.getNonMDStrings().size() > Opts.bitcode_mdindex_threshold) {
2778 // Now that we have emitted all the records we will emit the index. But
2779 // first
2780 // backpatch the forward reference so that the reader can skip the records
2781 // efficiently.
2782 Stream.BackpatchWord64(IndexOffsetRecordBitPos - 64,
2783 Stream.GetCurrentBitNo() - IndexOffsetRecordBitPos);
2784
2785 // Delta encode the index.
2786 uint64_t PreviousValue = IndexOffsetRecordBitPos;
2787 for (auto &Elt : IndexPos) {
2788 auto EltDelta = Elt - PreviousValue;
2789 PreviousValue = Elt;
2790 Elt = EltDelta;
2791 }
2792 // Emit the index record.
2793 Stream.EmitRecord(bitc::METADATA_INDEX, IndexPos, IndexAbbrev);
2794 IndexPos.clear();
2795 }
2796
2797 // Write the named metadata now.
2798 writeNamedMetadata(Record);
2799
2800 auto AddDeclAttachedMetadata = [&](const GlobalObject &GO) {
2801 SmallVector<uint64_t, 4> Record;
2802 Record.push_back(VE.getValueID(&GO));
2803 pushGlobalMetadataAttachment(Record, GO);
2805 };
2806 for (const Function &F : M)
2807 if (F.isDeclaration() && F.hasMetadata())
2808 AddDeclAttachedMetadata(F);
2809 for (const GlobalIFunc &GI : M.ifuncs())
2810 if (GI.hasMetadata())
2811 AddDeclAttachedMetadata(GI);
2812 // FIXME: Only store metadata for declarations here, and move data for global
2813 // variable definitions to a separate block (PR28134).
2814 for (const GlobalVariable &GV : M.globals())
2815 if (GV.hasMetadata())
2816 AddDeclAttachedMetadata(GV);
2817
2818 Stream.ExitBlock();
2819}
2820
2821void ModuleBitcodeWriter::writeFunctionMetadata(const Function &F) {
2822 if (!VE.hasMDs())
2823 return;
2824
2826 // New block, new abbrev id space. Unlike the module block this one is not
2827 // randomly accessed, so the irlayers abbrev can be created on first use.
2828 DILocationLayersAbbrev = 0;
2829 SmallVector<uint64_t, 64> Record;
2830 writeMetadataStrings(VE.getMDStrings(), Record);
2831 writeMetadataRecords(VE.getNonMDStrings(), Record);
2832 Stream.ExitBlock();
2833}
2834
2835void ModuleBitcodeWriter::pushGlobalMetadataAttachment(
2836 SmallVectorImpl<uint64_t> &Record, const GlobalObject &GO) {
2837 // [n x [id, mdnode]]
2839 GO.getAllMetadata(MDs);
2840 for (const auto &I : MDs) {
2841 Record.push_back(I.first);
2842 Record.push_back(VE.getMetadataID(I.second));
2843 }
2844}
2845
2846void ModuleBitcodeWriter::writeFunctionMetadataAttachment(const Function &F) {
2848
2849 SmallVector<uint64_t, 64> Record;
2850
2851 if (F.hasMetadata()) {
2852 pushGlobalMetadataAttachment(Record, F);
2853 Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
2854 Record.clear();
2855 }
2856
2857 // Write metadata attachments
2858 // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]]
2860 for (const BasicBlock &BB : F)
2861 for (const Instruction &I : BB) {
2862 MDs.clear();
2863 I.getAllMetadataOtherThanDebugLoc(MDs);
2864
2865 // If no metadata, ignore instruction.
2866 if (MDs.empty()) continue;
2867
2868 Record.push_back(VE.getInstructionID(&I));
2869
2870 for (const auto &[ID, MD] : MDs) {
2871 Record.push_back(ID);
2872 Record.push_back(VE.getMetadataID(MD));
2873 }
2874 Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
2875 Record.clear();
2876 }
2877
2878 Stream.ExitBlock();
2879}
2880
2881void ModuleBitcodeWriter::writeModuleMetadataKinds() {
2882 SmallVector<uint64_t, 64> Record;
2883
2884 // Write metadata kinds
2885 // METADATA_KIND - [n x [id, name]]
2887 M.getMDKindNames(Names);
2888
2889 if (Names.empty()) return;
2890
2892
2893 for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) {
2894 Record.push_back(MDKindID);
2895 StringRef KName = Names[MDKindID];
2896 Record.append(KName.begin(), KName.end());
2897
2898 Stream.EmitRecord(bitc::METADATA_KIND, Record, 0);
2899 Record.clear();
2900 }
2901
2902 Stream.ExitBlock();
2903}
2904
2905void ModuleBitcodeWriter::writeOperandBundleTags() {
2906 // Write metadata kinds
2907 //
2908 // OPERAND_BUNDLE_TAGS_BLOCK_ID : N x OPERAND_BUNDLE_TAG
2909 //
2910 // OPERAND_BUNDLE_TAG - [strchr x N]
2911
2913 M.getOperandBundleTags(Tags);
2914
2915 if (Tags.empty())
2916 return;
2917
2919
2920 SmallVector<uint64_t, 64> Record;
2921
2922 for (auto Tag : Tags) {
2923 Record.append(Tag.begin(), Tag.end());
2924
2925 Stream.EmitRecord(bitc::OPERAND_BUNDLE_TAG, Record, 0);
2926 Record.clear();
2927 }
2928
2929 Stream.ExitBlock();
2930}
2931
2932void ModuleBitcodeWriter::writeSyncScopeNames() {
2934 M.getContext().getSyncScopeNames(SSNs);
2935 if (SSNs.empty())
2936 return;
2937
2939
2940 SmallVector<uint64_t, 64> Record;
2941 for (auto SSN : SSNs) {
2942 Record.append(SSN.begin(), SSN.end());
2943 Stream.EmitRecord(bitc::SYNC_SCOPE_NAME, Record, 0);
2944 Record.clear();
2945 }
2946
2947 Stream.ExitBlock();
2948}
2949
2950void ModuleBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal,
2951 bool isGlobal) {
2952 if (FirstVal == LastVal) return;
2953
2955
2956 unsigned AggregateAbbrev = 0;
2957 unsigned String8Abbrev = 0;
2958 unsigned CString7Abbrev = 0;
2959 unsigned CString6Abbrev = 0;
2960 // If this is a constant pool for the module, emit module-specific abbrevs.
2961 if (isGlobal) {
2962 // Abbrev for CST_CODE_AGGREGATE.
2963 auto Abbv = std::make_shared<BitCodeAbbrev>();
2964 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE));
2965 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2966 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
2967 AggregateAbbrev = Stream.EmitAbbrev(std::move(Abbv));
2968
2969 // Abbrev for CST_CODE_STRING.
2970 Abbv = std::make_shared<BitCodeAbbrev>();
2971 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
2972 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2973 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
2974 String8Abbrev = Stream.EmitAbbrev(std::move(Abbv));
2975 // Abbrev for CST_CODE_CSTRING.
2976 Abbv = std::make_shared<BitCodeAbbrev>();
2977 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
2978 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2979 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
2980 CString7Abbrev = Stream.EmitAbbrev(std::move(Abbv));
2981 // Abbrev for CST_CODE_CSTRING.
2982 Abbv = std::make_shared<BitCodeAbbrev>();
2983 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
2984 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2985 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
2986 CString6Abbrev = Stream.EmitAbbrev(std::move(Abbv));
2987 }
2988
2989 SmallVector<uint64_t, 64> Record;
2990
2991 const ValueEnumerator::ValueList &Vals = VE.getValues();
2992 Type *LastTy = nullptr;
2993 for (unsigned i = FirstVal; i != LastVal; ++i) {
2994 const Value *V = Vals[i].first;
2995 // If we need to switch types, do so now.
2996 if (V->getType() != LastTy) {
2997 LastTy = V->getType();
2998 Record.push_back(VE.getTypeID(LastTy));
2999 Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record,
3000 CONSTANTS_SETTYPE_ABBREV);
3001 Record.clear();
3002 }
3003
3004 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
3005 Record.push_back(VE.getTypeID(IA->getFunctionType()));
3006 Record.push_back(
3007 unsigned(IA->hasSideEffects()) | unsigned(IA->isAlignStack()) << 1 |
3008 unsigned(IA->getDialect() & 1) << 2 | unsigned(IA->canThrow()) << 3);
3009
3010 // Add the asm string.
3011 StringRef AsmStr = IA->getAsmString();
3012 Record.push_back(AsmStr.size());
3013 Record.append(AsmStr.begin(), AsmStr.end());
3014
3015 // Add the constraint string.
3016 StringRef ConstraintStr = IA->getConstraintString();
3017 Record.push_back(ConstraintStr.size());
3018 Record.append(ConstraintStr.begin(), ConstraintStr.end());
3019 Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record);
3020 Record.clear();
3021 continue;
3022 }
3023 const Constant *C = cast<Constant>(V);
3024 unsigned Code = -1U;
3025 unsigned AbbrevToUse = 0;
3026 if (C->isNullValue()) {
3028 } else if (isa<PoisonValue>(C)) {
3030 } else if (isa<UndefValue>(C)) {
3032 } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
3033 if (IV->getBitWidth() <= 64) {
3034 uint64_t V = IV->getSExtValue();
3035 emitSignedInt64(Record, V);
3037 AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
3038 } else { // Wide integers, > 64 bits in size.
3039 emitWideAPInt(Record, IV->getValue());
3041 }
3042 } else if (const ConstantByte *BV = dyn_cast<ConstantByte>(C)) {
3043 if (BV->getBitWidth() <= 64) {
3044 uint64_t V = BV->getSExtValue();
3045 emitSignedInt64(Record, V);
3047 AbbrevToUse = CONSTANTS_BYTE_ABBREV;
3048 } else { // Wide bytes, > 64 bits in size.
3049 emitWideAPInt(Record, BV->getValue());
3051 }
3052 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
3054 Type *Ty = CFP->getType()->getScalarType();
3055 if (Ty->isHalfTy() || Ty->isBFloatTy() || Ty->isFloatTy() ||
3056 Ty->isDoubleTy()) {
3057 Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
3058 } else if (Ty->isX86_FP80Ty()) {
3059 // api needed to prevent premature destruction
3060 // bits are not in the same order as a normal i80 APInt, compensate.
3061 APInt api = CFP->getValueAPF().bitcastToAPInt();
3062 const uint64_t *p = api.getRawData();
3063 Record.push_back((p[1] << 48) | (p[0] >> 16));
3064 Record.push_back(p[0] & 0xffffLL);
3065 } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) {
3066 APInt api = CFP->getValueAPF().bitcastToAPInt();
3067 const uint64_t *p = api.getRawData();
3068 Record.push_back(p[0]);
3069 Record.push_back(p[1]);
3070 } else {
3071 assert(0 && "Unknown FP type!");
3072 }
3073 } else if (isa<ConstantDataSequential>(C) &&
3074 cast<ConstantDataSequential>(C)->isString()) {
3075 const ConstantDataSequential *Str = cast<ConstantDataSequential>(C);
3076 // Emit constant strings specially.
3077 uint64_t NumElts = Str->getNumElements();
3078 // If this is a null-terminated string, use the denser CSTRING encoding.
3079 if (Str->isCString()) {
3081 --NumElts; // Don't encode the null, which isn't allowed by char6.
3082 } else {
3084 AbbrevToUse = String8Abbrev;
3085 }
3086 bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
3087 bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
3088 for (uint64_t i = 0; i != NumElts; ++i) {
3089 unsigned char V = Str->getElementAsInteger(i);
3090 Record.push_back(V);
3091 isCStr7 &= (V & 128) == 0;
3092 if (isCStrChar6)
3093 isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
3094 }
3095
3096 if (isCStrChar6)
3097 AbbrevToUse = CString6Abbrev;
3098 else if (isCStr7)
3099 AbbrevToUse = CString7Abbrev;
3100 } else if (const ConstantDataSequential *CDS =
3103 Type *EltTy = CDS->getElementType();
3104 if (isa<IntegerType>(EltTy) || isa<ByteType>(EltTy)) {
3105 for (uint64_t i = 0, e = CDS->getNumElements(); i != e; ++i)
3106 Record.push_back(CDS->getElementAsInteger(i));
3107 } else {
3108 for (uint64_t i = 0, e = CDS->getNumElements(); i != e; ++i)
3109 Record.push_back(
3110 CDS->getElementAsAPFloat(i).bitcastToAPInt().getLimitedValue());
3111 }
3112 } else if (isa<ConstantAggregate>(C)) {
3114 for (const Value *Op : C->operands())
3115 Record.push_back(VE.getValueID(Op));
3116 AbbrevToUse = AggregateAbbrev;
3117 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
3118 switch (CE->getOpcode()) {
3119 default:
3120 if (Instruction::isCast(CE->getOpcode())) {
3122 Record.push_back(getEncodedCastOpcode(CE->getOpcode()));
3123 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
3124 Record.push_back(VE.getValueID(C->getOperand(0)));
3125 AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
3126 } else {
3127 assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
3129 Record.push_back(getEncodedBinaryOpcode(CE->getOpcode()));
3130 Record.push_back(VE.getValueID(C->getOperand(0)));
3131 Record.push_back(VE.getValueID(C->getOperand(1)));
3133 if (Flags != 0)
3134 Record.push_back(Flags);
3135 }
3136 break;
3137 case Instruction::GetElementPtr: {
3139 const auto *GO = cast<GEPOperator>(C);
3140 Record.push_back(VE.getTypeID(GO->getSourceElementType()));
3141 Record.push_back(getOptimizationFlags(GO));
3142 if (std::optional<ConstantRange> Range = GO->getInRange()) {
3144 emitConstantRange(Record, *Range, /*EmitBitWidth=*/true);
3145 }
3146 for (const Value *Op : CE->operands()) {
3147 Record.push_back(VE.getTypeID(Op->getType()));
3148 Record.push_back(VE.getValueID(Op));
3149 }
3150 break;
3151 }
3152 case Instruction::ExtractElement:
3154 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
3155 Record.push_back(VE.getValueID(C->getOperand(0)));
3156 Record.push_back(VE.getTypeID(C->getOperand(1)->getType()));
3157 Record.push_back(VE.getValueID(C->getOperand(1)));
3158 break;
3159 case Instruction::InsertElement:
3161 Record.push_back(VE.getValueID(C->getOperand(0)));
3162 Record.push_back(VE.getValueID(C->getOperand(1)));
3163 Record.push_back(VE.getTypeID(C->getOperand(2)->getType()));
3164 Record.push_back(VE.getValueID(C->getOperand(2)));
3165 break;
3166 case Instruction::ShuffleVector:
3167 // If the return type and argument types are the same, this is a
3168 // standard shufflevector instruction. If the types are different,
3169 // then the shuffle is widening or truncating the input vectors, and
3170 // the argument type must also be encoded.
3171 if (C->getType() == C->getOperand(0)->getType()) {
3173 } else {
3175 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
3176 }
3177 Record.push_back(VE.getValueID(C->getOperand(0)));
3178 Record.push_back(VE.getValueID(C->getOperand(1)));
3179 Record.push_back(VE.getValueID(CE->getShuffleMaskForBitcode()));
3180 break;
3181 }
3182 } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
3184 Record.push_back(VE.getTypeID(BA->getFunction()->getType()));
3185 Record.push_back(VE.getValueID(BA->getFunction()));
3186 Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock()));
3187 } else if (const auto *Equiv = dyn_cast<DSOLocalEquivalent>(C)) {
3189 Record.push_back(VE.getTypeID(Equiv->getGlobalValue()->getType()));
3190 Record.push_back(VE.getValueID(Equiv->getGlobalValue()));
3191 } else if (const auto *NC = dyn_cast<NoCFIValue>(C)) {
3193 Record.push_back(VE.getTypeID(NC->getGlobalValue()->getType()));
3194 Record.push_back(VE.getValueID(NC->getGlobalValue()));
3195 } else if (const auto *CPA = dyn_cast<ConstantPtrAuth>(C)) {
3197 Record.push_back(VE.getValueID(CPA->getPointer()));
3198 Record.push_back(VE.getValueID(CPA->getKey()));
3199 Record.push_back(VE.getValueID(CPA->getDiscriminator()));
3200 Record.push_back(VE.getValueID(CPA->getAddrDiscriminator()));
3201 Record.push_back(VE.getValueID(CPA->getDeactivationSymbol()));
3202 } else {
3203#ifndef NDEBUG
3204 C->dump();
3205#endif
3206 llvm_unreachable("Unknown constant!");
3207 }
3208 Stream.EmitRecord(Code, Record, AbbrevToUse);
3209 Record.clear();
3210 }
3211
3212 Stream.ExitBlock();
3213}
3214
3215void ModuleBitcodeWriter::writeModuleConstants() {
3216 const ValueEnumerator::ValueList &Vals = VE.getValues();
3217
3218 // Find the first constant to emit, which is the first non-globalvalue value.
3219 // We know globalvalues have been emitted by WriteModuleInfo.
3220 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
3221 if (!isa<GlobalValue>(Vals[i].first)) {
3222 writeConstants(i, Vals.size(), true);
3223 return;
3224 }
3225 }
3226}
3227
3228/// pushValueAndType - The file has to encode both the value and type id for
3229/// many values, because we need to know what type to create for forward
3230/// references. However, most operands are not forward references, so this type
3231/// field is not needed.
3232///
3233/// This function adds V's value ID to Vals. If the value ID is higher than the
3234/// instruction ID, then it is a forward reference, and it also includes the
3235/// type ID. The value ID that is written is encoded relative to the InstID.
3236bool ModuleBitcodeWriter::pushValueAndType(const Value *V, unsigned InstID,
3237 SmallVectorImpl<unsigned> &Vals) {
3238 unsigned ValID = VE.getValueID(V);
3239 // Make encoding relative to the InstID.
3240 Vals.push_back(InstID - ValID);
3241 if (ValID >= InstID) {
3242 Vals.push_back(VE.getTypeID(V->getType()));
3243 return true;
3244 }
3245 return false;
3246}
3247
3248bool ModuleBitcodeWriter::pushValueOrMetadata(const Value *V, unsigned InstID,
3249 SmallVectorImpl<unsigned> &Vals) {
3250 bool IsMetadata = V->getType()->isMetadataTy();
3251 if (IsMetadata) {
3253 Metadata *MD = cast<MetadataAsValue>(V)->getMetadata();
3254 unsigned ValID = VE.getMetadataID(MD);
3255 Vals.push_back(InstID - ValID);
3256 return false;
3257 }
3258 return pushValueAndType(V, InstID, Vals);
3259}
3260
3261void ModuleBitcodeWriter::writeOperandBundles(const CallBase &CS,
3262 unsigned InstID) {
3264 LLVMContext &C = CS.getContext();
3265
3266 for (unsigned i = 0, e = CS.getNumOperandBundles(); i != e; ++i) {
3267 const auto &Bundle = CS.getOperandBundleAt(i);
3268 Record.push_back(C.getOperandBundleTagID(Bundle.getTagName()));
3269
3270 for (auto &Input : Bundle.Inputs)
3271 pushValueOrMetadata(Input, InstID, Record);
3272
3274 Record.clear();
3275 }
3276}
3277
3278/// pushValue - Like pushValueAndType, but where the type of the value is
3279/// omitted (perhaps it was already encoded in an earlier operand).
3280void ModuleBitcodeWriter::pushValue(const Value *V, unsigned InstID,
3281 SmallVectorImpl<unsigned> &Vals) {
3282 unsigned ValID = VE.getValueID(V);
3283 Vals.push_back(InstID - ValID);
3284}
3285
3286void ModuleBitcodeWriter::pushValueSigned(const Value *V, unsigned InstID,
3287 SmallVectorImpl<uint64_t> &Vals) {
3288 unsigned ValID = VE.getValueID(V);
3289 int64_t diff = ((int32_t)InstID - (int32_t)ValID);
3290 emitSignedInt64(Vals, diff);
3291}
3292
3293/// WriteInstruction - Emit an instruction to the specified stream.
3294void ModuleBitcodeWriter::writeInstruction(const Instruction &I,
3295 unsigned InstID,
3296 SmallVectorImpl<unsigned> &Vals) {
3297 unsigned Code = 0;
3298 unsigned AbbrevToUse = 0;
3299 VE.setInstructionID(&I);
3300 switch (I.getOpcode()) {
3301 default:
3302 if (Instruction::isCast(I.getOpcode())) {
3304 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
3305 AbbrevToUse = FUNCTION_INST_CAST_ABBREV;
3306 Vals.push_back(VE.getTypeID(I.getType()));
3307 Vals.push_back(getEncodedCastOpcode(I.getOpcode()));
3309 if (Flags != 0) {
3310 if (AbbrevToUse == FUNCTION_INST_CAST_ABBREV)
3311 AbbrevToUse = FUNCTION_INST_CAST_FLAGS_ABBREV;
3312 Vals.push_back(Flags);
3313 }
3314 } else {
3315 assert(isa<BinaryOperator>(I) && "Unknown instruction!");
3317 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
3318 AbbrevToUse = FUNCTION_INST_BINOP_ABBREV;
3319 pushValue(I.getOperand(1), InstID, Vals);
3320 Vals.push_back(getEncodedBinaryOpcode(I.getOpcode()));
3322 if (Flags != 0) {
3323 if (AbbrevToUse == FUNCTION_INST_BINOP_ABBREV)
3324 AbbrevToUse = FUNCTION_INST_BINOP_FLAGS_ABBREV;
3325 Vals.push_back(Flags);
3326 }
3327 }
3328 break;
3329 case Instruction::FNeg: {
3331 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
3332 AbbrevToUse = FUNCTION_INST_UNOP_ABBREV;
3333 Vals.push_back(getEncodedUnaryOpcode(I.getOpcode()));
3335 if (Flags != 0) {
3336 if (AbbrevToUse == FUNCTION_INST_UNOP_ABBREV)
3337 AbbrevToUse = FUNCTION_INST_UNOP_FLAGS_ABBREV;
3338 Vals.push_back(Flags);
3339 }
3340 break;
3341 }
3342 case Instruction::GetElementPtr: {
3344 AbbrevToUse = FUNCTION_INST_GEP_ABBREV;
3345 auto &GEPInst = cast<GetElementPtrInst>(I);
3347 Vals.push_back(VE.getTypeID(GEPInst.getSourceElementType()));
3348 for (const Value *Op : I.operands())
3349 pushValueAndType(Op, InstID, Vals);
3350 break;
3351 }
3352 case Instruction::ExtractValue: {
3354 pushValueAndType(I.getOperand(0), InstID, Vals);
3355 const ExtractValueInst *EVI = cast<ExtractValueInst>(&I);
3356 Vals.append(EVI->idx_begin(), EVI->idx_end());
3357 break;
3358 }
3359 case Instruction::InsertValue: {
3361 pushValueAndType(I.getOperand(0), InstID, Vals);
3362 pushValueAndType(I.getOperand(1), InstID, Vals);
3363 const InsertValueInst *IVI = cast<InsertValueInst>(&I);
3364 Vals.append(IVI->idx_begin(), IVI->idx_end());
3365 break;
3366 }
3367 case Instruction::Select: {
3369 pushValueAndType(I.getOperand(1), InstID, Vals);
3370 pushValue(I.getOperand(2), InstID, Vals);
3371 pushValueAndType(I.getOperand(0), InstID, Vals);
3373 if (Flags != 0)
3374 Vals.push_back(Flags);
3375 break;
3376 }
3377 case Instruction::ExtractElement:
3379 pushValueAndType(I.getOperand(0), InstID, Vals);
3380 pushValueAndType(I.getOperand(1), InstID, Vals);
3381 break;
3382 case Instruction::InsertElement:
3384 pushValueAndType(I.getOperand(0), InstID, Vals);
3385 pushValue(I.getOperand(1), InstID, Vals);
3386 pushValueAndType(I.getOperand(2), InstID, Vals);
3387 break;
3388 case Instruction::BitExtract:
3390 Vals.push_back(VE.getTypeID(I.getType()));
3391 pushValueAndType(I.getOperand(0), InstID, Vals);
3392 pushValueAndType(I.getOperand(1), InstID, Vals);
3393 break;
3394 case Instruction::BitInsert:
3396 pushValueAndType(I.getOperand(0), InstID, Vals);
3397 pushValueAndType(I.getOperand(1), InstID, Vals);
3398 pushValueAndType(I.getOperand(2), InstID, Vals);
3399 break;
3400 case Instruction::ShuffleVector:
3402 pushValueAndType(I.getOperand(0), InstID, Vals);
3403 pushValue(I.getOperand(1), InstID, Vals);
3404 pushValue(cast<ShuffleVectorInst>(I).getShuffleMaskForBitcode(), InstID,
3405 Vals);
3406 break;
3407 case Instruction::ICmp:
3408 case Instruction::FCmp: {
3409 // compare returning Int1Ty or vector of Int1Ty
3411 AbbrevToUse = FUNCTION_INST_CMP_ABBREV;
3412 if (pushValueAndType(I.getOperand(0), InstID, Vals))
3413 AbbrevToUse = 0;
3414 pushValue(I.getOperand(1), InstID, Vals);
3417 if (Flags != 0) {
3418 Vals.push_back(Flags);
3419 if (AbbrevToUse)
3420 AbbrevToUse = FUNCTION_INST_CMP_FLAGS_ABBREV;
3421 }
3422 break;
3423 }
3424
3425 case Instruction::Ret:
3426 {
3428 unsigned NumOperands = I.getNumOperands();
3429 if (NumOperands == 0)
3430 AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV;
3431 else if (NumOperands == 1) {
3432 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
3433 AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV;
3434 } else {
3435 for (const Value *Op : I.operands())
3436 pushValueAndType(Op, InstID, Vals);
3437 }
3438 }
3439 break;
3440 case Instruction::UncondBr: {
3442 AbbrevToUse = FUNCTION_INST_BR_UNCOND_ABBREV;
3443 const UncondBrInst &II = cast<UncondBrInst>(I);
3444 Vals.push_back(VE.getValueID(II.getSuccessor(0)));
3445 } break;
3446 case Instruction::CondBr: {
3448 AbbrevToUse = FUNCTION_INST_BR_COND_ABBREV;
3449 const CondBrInst &II = cast<CondBrInst>(I);
3450 Vals.push_back(VE.getValueID(II.getSuccessor(0)));
3451 Vals.push_back(VE.getValueID(II.getSuccessor(1)));
3452 pushValue(II.getCondition(), InstID, Vals);
3453 } break;
3454 case Instruction::Switch:
3455 {
3457 const SwitchInst &SI = cast<SwitchInst>(I);
3458 Vals.push_back(VE.getTypeID(SI.getCondition()->getType()));
3459 pushValue(SI.getCondition(), InstID, Vals);
3460 Vals.push_back(VE.getValueID(SI.getDefaultDest()));
3461 for (auto Case : SI.cases()) {
3462 Vals.push_back(VE.getValueID(Case.getCaseValue()));
3463 Vals.push_back(VE.getValueID(Case.getCaseSuccessor()));
3464 }
3465 }
3466 break;
3467 case Instruction::IndirectBr:
3469 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
3470 // Encode the address operand as relative, but not the basic blocks.
3471 pushValue(I.getOperand(0), InstID, Vals);
3472 for (const Value *Op : drop_begin(I.operands()))
3473 Vals.push_back(VE.getValueID(Op));
3474 break;
3475
3476 case Instruction::Invoke: {
3477 const InvokeInst *II = cast<InvokeInst>(&I);
3478 const Value *Callee = II->getCalledOperand();
3479 FunctionType *FTy = II->getFunctionType();
3480
3481 if (II->hasOperandBundles())
3482 writeOperandBundles(*II, InstID);
3483
3485
3486 Vals.push_back(VE.getAttributeListID(II->getAttributes()));
3487 Vals.push_back(II->getCallingConv() | 1 << 13);
3488 Vals.push_back(VE.getValueID(II->getNormalDest()));
3489 Vals.push_back(VE.getValueID(II->getUnwindDest()));
3490 Vals.push_back(VE.getTypeID(FTy));
3491 pushValueAndType(Callee, InstID, Vals);
3492
3493 // Emit value #'s for the fixed parameters.
3494 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
3495 pushValue(I.getOperand(i), InstID, Vals); // fixed param.
3496
3497 // Emit type/value pairs for varargs params.
3498 if (FTy->isVarArg()) {
3499 for (unsigned i = FTy->getNumParams(), e = II->arg_size(); i != e; ++i)
3500 pushValueAndType(I.getOperand(i), InstID, Vals); // vararg
3501 }
3502 break;
3503 }
3504 case Instruction::Resume:
3506 pushValueAndType(I.getOperand(0), InstID, Vals);
3507 break;
3508 case Instruction::CleanupRet: {
3510 const auto &CRI = cast<CleanupReturnInst>(I);
3511 pushValue(CRI.getCleanupPad(), InstID, Vals);
3512 if (CRI.hasUnwindDest())
3513 Vals.push_back(VE.getValueID(CRI.getUnwindDest()));
3514 break;
3515 }
3516 case Instruction::CatchRet: {
3518 const auto &CRI = cast<CatchReturnInst>(I);
3519 pushValue(CRI.getCatchPad(), InstID, Vals);
3520 Vals.push_back(VE.getValueID(CRI.getSuccessor()));
3521 break;
3522 }
3523 case Instruction::CleanupPad:
3524 case Instruction::CatchPad: {
3525 const auto &FuncletPad = cast<FuncletPadInst>(I);
3528 pushValue(FuncletPad.getParentPad(), InstID, Vals);
3529
3530 unsigned NumArgOperands = FuncletPad.arg_size();
3531 Vals.push_back(NumArgOperands);
3532 for (unsigned Op = 0; Op != NumArgOperands; ++Op)
3533 pushValueAndType(FuncletPad.getArgOperand(Op), InstID, Vals);
3534 break;
3535 }
3536 case Instruction::CatchSwitch: {
3538 const auto &CatchSwitch = cast<CatchSwitchInst>(I);
3539
3540 pushValue(CatchSwitch.getParentPad(), InstID, Vals);
3541
3542 unsigned NumHandlers = CatchSwitch.getNumHandlers();
3543 Vals.push_back(NumHandlers);
3544 for (const BasicBlock *CatchPadBB : CatchSwitch.handlers())
3545 Vals.push_back(VE.getValueID(CatchPadBB));
3546
3547 if (CatchSwitch.hasUnwindDest())
3548 Vals.push_back(VE.getValueID(CatchSwitch.getUnwindDest()));
3549 break;
3550 }
3551 case Instruction::CallBr: {
3552 const CallBrInst *CBI = cast<CallBrInst>(&I);
3553 const Value *Callee = CBI->getCalledOperand();
3554 FunctionType *FTy = CBI->getFunctionType();
3555
3556 if (CBI->hasOperandBundles())
3557 writeOperandBundles(*CBI, InstID);
3558
3560
3562
3565
3566 Vals.push_back(VE.getValueID(CBI->getDefaultDest()));
3567 Vals.push_back(CBI->getNumIndirectDests());
3568 for (unsigned i = 0, e = CBI->getNumIndirectDests(); i != e; ++i)
3569 Vals.push_back(VE.getValueID(CBI->getIndirectDest(i)));
3570
3571 Vals.push_back(VE.getTypeID(FTy));
3572 pushValueAndType(Callee, InstID, Vals);
3573
3574 // Emit value #'s for the fixed parameters.
3575 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
3576 pushValue(I.getOperand(i), InstID, Vals); // fixed param.
3577
3578 // Emit type/value pairs for varargs params.
3579 if (FTy->isVarArg()) {
3580 for (unsigned i = FTy->getNumParams(), e = CBI->arg_size(); i != e; ++i)
3581 pushValueAndType(I.getOperand(i), InstID, Vals); // vararg
3582 }
3583 break;
3584 }
3585 case Instruction::Unreachable:
3587 AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV;
3588 break;
3589
3590 case Instruction::PHI: {
3591 const PHINode &PN = cast<PHINode>(I);
3593 // With the newer instruction encoding, forward references could give
3594 // negative valued IDs. This is most common for PHIs, so we use
3595 // signed VBRs.
3597 Vals64.push_back(VE.getTypeID(PN.getType()));
3598 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
3599 pushValueSigned(PN.getIncomingValue(i), InstID, Vals64);
3600 Vals64.push_back(VE.getValueID(PN.getIncomingBlock(i)));
3601 }
3602
3604 if (Flags != 0)
3605 Vals64.push_back(Flags);
3606
3607 // Emit a Vals64 vector and exit.
3608 Stream.EmitRecord(Code, Vals64, AbbrevToUse);
3609 Vals64.clear();
3610 return;
3611 }
3612
3613 case Instruction::LandingPad: {
3614 const LandingPadInst &LP = cast<LandingPadInst>(I);
3616 Vals.push_back(VE.getTypeID(LP.getType()));
3617 Vals.push_back(LP.isCleanup());
3618 Vals.push_back(LP.getNumClauses());
3619 for (unsigned I = 0, E = LP.getNumClauses(); I != E; ++I) {
3620 if (LP.isCatch(I))
3622 else
3624 pushValueAndType(LP.getClause(I), InstID, Vals);
3625 }
3626 break;
3627 }
3628
3629 case Instruction::Alloca: {
3631 const AllocaInst &AI = cast<AllocaInst>(I);
3632 Vals.push_back(VE.getTypeID(AI.getAllocatedType()));
3633 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
3634 Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
3635 using APV = AllocaPackedValues;
3636 unsigned Record = 0;
3637 unsigned EncodedAlign = getEncodedAlign(AI.getAlign());
3639 Record, EncodedAlign & ((1 << APV::AlignLower::Bits) - 1));
3641 EncodedAlign >> APV::AlignLower::Bits);
3645 Vals.push_back(Record);
3646
3647 unsigned AS = AI.getAddressSpace();
3648 if (AS != M.getDataLayout().getAllocaAddrSpace())
3649 Vals.push_back(AS);
3650 break;
3651 }
3652
3653 case Instruction::Load: {
3654 const auto &LI = cast<LoadInst>(I);
3655 if (LI.isAtomic()) {
3657 pushValueAndType(LI.getOperand(0), InstID, Vals);
3658 } else {
3660 if (!pushValueAndType(LI.getOperand(0), InstID, Vals)) // ptr
3661 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV;
3662 }
3663 Vals.push_back(VE.getTypeID(LI.getType()));
3664 Vals.push_back(getEncodedAlign(LI.getAlign()));
3665 Vals.push_back(LI.isVolatile());
3666 if (LI.isAtomic()) {
3667 Vals.push_back(getEncodedOrdering(LI.getOrdering()));
3668 Vals.push_back(getEncodedSyncScopeID(LI.getSyncScopeID()));
3669 if (LI.isElementwise())
3670 Vals.push_back(1);
3671 }
3672 break;
3673 }
3674
3675 case Instruction::Store: {
3676 const auto &SI = cast<StoreInst>(I);
3677 if (SI.isAtomic()) {
3679 } else {
3681 AbbrevToUse = FUNCTION_INST_STORE_ABBREV;
3682 }
3683 if (pushValueAndType(I.getOperand(1), InstID, Vals)) // ptrty + ptr
3684 AbbrevToUse = 0;
3685 if (pushValueAndType(I.getOperand(0), InstID, Vals)) // valty + val
3686 AbbrevToUse = 0;
3687 Vals.push_back(getEncodedAlign(SI.getAlign()));
3688 Vals.push_back(SI.isVolatile());
3689 if (SI.isAtomic()) {
3690 Vals.push_back(getEncodedOrdering(SI.getOrdering()));
3691 Vals.push_back(getEncodedSyncScopeID(SI.getSyncScopeID()));
3692 if (SI.isElementwise())
3693 Vals.push_back(1);
3694 }
3695 break;
3696 }
3697
3698 case Instruction::AtomicCmpXchg:
3700 pushValueAndType(I.getOperand(0), InstID, Vals); // ptrty + ptr
3701 pushValueAndType(I.getOperand(1), InstID, Vals); // cmp.
3702 pushValue(I.getOperand(2), InstID, Vals); // newval.
3703 Vals.push_back(cast<AtomicCmpXchgInst>(I).isVolatile());
3704 Vals.push_back(
3705 getEncodedOrdering(cast<AtomicCmpXchgInst>(I).getSuccessOrdering()));
3706 Vals.push_back(
3707 getEncodedSyncScopeID(cast<AtomicCmpXchgInst>(I).getSyncScopeID()));
3708 Vals.push_back(
3709 getEncodedOrdering(cast<AtomicCmpXchgInst>(I).getFailureOrdering()));
3710 Vals.push_back(cast<AtomicCmpXchgInst>(I).isWeak());
3711 Vals.push_back(getEncodedAlign(cast<AtomicCmpXchgInst>(I).getAlign()));
3712 break;
3713 case Instruction::AtomicRMW:
3715 pushValueAndType(I.getOperand(0), InstID, Vals); // ptrty + ptr
3716 pushValueAndType(I.getOperand(1), InstID, Vals); // valty + val
3718 Vals.push_back(cast<AtomicRMWInst>(I).isVolatile());
3719 Vals.push_back(getEncodedOrdering(cast<AtomicRMWInst>(I).getOrdering()));
3720 Vals.push_back(
3721 getEncodedSyncScopeID(cast<AtomicRMWInst>(I).getSyncScopeID()));
3722 Vals.push_back(getEncodedAlign(cast<AtomicRMWInst>(I).getAlign()));
3723 break;
3724 case Instruction::Fence:
3726 Vals.push_back(getEncodedOrdering(cast<FenceInst>(I).getOrdering()));
3727 Vals.push_back(getEncodedSyncScopeID(cast<FenceInst>(I).getSyncScopeID()));
3728 break;
3729 case Instruction::Call: {
3730 const CallInst &CI = cast<CallInst>(I);
3731 FunctionType *FTy = CI.getFunctionType();
3732
3733 if (CI.hasOperandBundles())
3734 writeOperandBundles(CI, InstID);
3735
3737
3739
3740 unsigned Flags = getOptimizationFlags(&I);
3742 unsigned(CI.isTailCall()) << bitc::CALL_TAIL |
3743 unsigned(CI.isMustTailCall()) << bitc::CALL_MUSTTAIL |
3745 unsigned(CI.isNoTailCall()) << bitc::CALL_NOTAIL |
3746 unsigned(Flags != 0) << bitc::CALL_FMF);
3747 if (Flags != 0)
3748 Vals.push_back(Flags);
3749
3750 Vals.push_back(VE.getTypeID(FTy));
3751 pushValueAndType(CI.getCalledOperand(), InstID, Vals); // Callee
3752
3753 // Emit value #'s for the fixed parameters.
3754 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
3755 pushValue(CI.getArgOperand(i), InstID, Vals); // fixed param.
3756
3757 // Emit type/value pairs for varargs params.
3758 if (FTy->isVarArg()) {
3759 for (unsigned i = FTy->getNumParams(), e = CI.arg_size(); i != e; ++i)
3760 pushValueAndType(CI.getArgOperand(i), InstID, Vals); // varargs
3761 }
3762 break;
3763 }
3764 case Instruction::VAArg:
3766 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty
3767 pushValue(I.getOperand(0), InstID, Vals); // valist.
3768 Vals.push_back(VE.getTypeID(I.getType())); // restype.
3769 break;
3770 case Instruction::Freeze:
3772 pushValueAndType(I.getOperand(0), InstID, Vals);
3773 break;
3774 }
3775
3776 Stream.EmitRecord(Code, Vals, AbbrevToUse);
3777 Vals.clear();
3778}
3779
3780/// Write a GlobalValue VST to the module. The purpose of this data structure is
3781/// to allow clients to efficiently find the function body.
3782void ModuleBitcodeWriter::writeGlobalValueSymbolTable(
3783 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex) {
3784 // Get the offset of the VST we are writing, and backpatch it into
3785 // the VST forward declaration record.
3786 uint64_t VSTOffset = Stream.GetCurrentBitNo();
3787 // The BitcodeStartBit was the stream offset of the identification block.
3788 VSTOffset -= bitcodeStartBit();
3789 assert((VSTOffset & 31) == 0 && "VST block not 32-bit aligned");
3790 // Note that we add 1 here because the offset is relative to one word
3791 // before the start of the identification block, which was historically
3792 // always the start of the regular bitcode header.
3793 Stream.BackpatchWord(VSTOffsetPlaceholder, VSTOffset / 32 + 1);
3794
3796
3797 auto Abbv = std::make_shared<BitCodeAbbrev>();
3798 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_FNENTRY));
3799 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
3800 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
3801 unsigned FnEntryAbbrev = Stream.EmitAbbrev(std::move(Abbv));
3802
3803 for (const Function &F : M) {
3804 uint64_t Record[2];
3805
3806 if (F.isDeclaration())
3807 continue;
3808
3809 Record[0] = VE.getValueID(&F);
3810
3811 // Save the word offset of the function (from the start of the
3812 // actual bitcode written to the stream).
3813 uint64_t BitcodeIndex = FunctionToBitcodeIndex[&F] - bitcodeStartBit();
3814 assert((BitcodeIndex & 31) == 0 && "function block not 32-bit aligned");
3815 // Note that we add 1 here because the offset is relative to one word
3816 // before the start of the identification block, which was historically
3817 // always the start of the regular bitcode header.
3818 Record[1] = BitcodeIndex / 32 + 1;
3819
3820 Stream.EmitRecord(bitc::VST_CODE_FNENTRY, Record, FnEntryAbbrev);
3821 }
3822
3823 Stream.ExitBlock();
3824}
3825
3826/// Emit names for arguments, instructions and basic blocks in a function.
3827void ModuleBitcodeWriter::writeFunctionLevelValueSymbolTable(
3828 const ValueSymbolTable &VST) {
3829 if (VST.empty())
3830 return;
3831
3833
3834 // FIXME: Set up the abbrev, we know how many values there are!
3835 // FIXME: We know if the type names can use 7-bit ascii.
3836 SmallVector<uint64_t, 64> NameVals;
3837
3838 for (const ValueName &Name : VST) {
3839 // Figure out the encoding to use for the name.
3841
3842 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
3843 NameVals.push_back(VE.getValueID(Name.getValue()));
3844
3845 // VST_CODE_ENTRY: [valueid, namechar x N]
3846 // VST_CODE_BBENTRY: [bbid, namechar x N]
3847 unsigned Code;
3848 if (isa<BasicBlock>(Name.getValue())) {
3850 if (Bits == SE_Char6)
3851 AbbrevToUse = VST_BBENTRY_6_ABBREV;
3852 } else {
3854 if (Bits == SE_Char6)
3855 AbbrevToUse = VST_ENTRY_6_ABBREV;
3856 else if (Bits == SE_Fixed7)
3857 AbbrevToUse = VST_ENTRY_7_ABBREV;
3858 }
3859
3860 for (const auto P : Name.getKey())
3861 NameVals.push_back((unsigned char)P);
3862
3863 // Emit the finished record.
3864 Stream.EmitRecord(Code, NameVals, AbbrevToUse);
3865 NameVals.clear();
3866 }
3867
3868 Stream.ExitBlock();
3869}
3870
3871void ModuleBitcodeWriter::writeUseList(UseListOrder &&Order) {
3872 assert(Order.Shuffle.size() >= 2 && "Shuffle too small");
3873 unsigned Code;
3874 if (isa<BasicBlock>(Order.V))
3876 else
3878
3879 SmallVector<uint64_t, 64> Record(Order.Shuffle.begin(), Order.Shuffle.end());
3880 Record.push_back(VE.getValueID(Order.V));
3881 Stream.EmitRecord(Code, Record);
3882}
3883
3884void ModuleBitcodeWriter::writeUseListBlock(const Function *F) {
3886 "Expected to be preserving use-list order");
3887
3888 auto hasMore = [&]() {
3889 return !VE.UseListOrders.empty() && VE.UseListOrders.back().F == F;
3890 };
3891 if (!hasMore())
3892 // Nothing to do.
3893 return;
3894
3896 while (hasMore()) {
3897 writeUseList(std::move(VE.UseListOrders.back()));
3898 VE.UseListOrders.pop_back();
3899 }
3900 Stream.ExitBlock();
3901}
3902
3903/// Emit a function body to the module stream.
3904void ModuleBitcodeWriter::writeFunction(
3905 const Function &F,
3906 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex) {
3907 // Save the bitcode index of the start of this function block for recording
3908 // in the VST.
3909 FunctionToBitcodeIndex[&F] = Stream.GetCurrentBitNo();
3910
3913
3915
3916 // Emit the number of basic blocks, so the reader can create them ahead of
3917 // time.
3918 Vals.push_back(VE.getBasicBlocks().size());
3920 Vals.clear();
3921
3922 // If there are function-local constants, emit them now.
3923 unsigned CstStart, CstEnd;
3924 VE.getFunctionConstantRange(CstStart, CstEnd);
3925 writeConstants(CstStart, CstEnd, false);
3926
3927 // If there is function-local metadata, emit it now.
3928 writeFunctionMetadata(F);
3929
3930 // Keep a running idea of what the instruction ID is.
3931 unsigned InstID = CstEnd;
3932
3933 bool NeedsMetadataAttachment = F.hasMetadata();
3934
3935 DILocation *LastDL = nullptr;
3936 SmallSetVector<Function *, 4> BlockAddressUsers;
3937
3938 // Finally, emit all the instructions, in order.
3939 for (const BasicBlock &BB : F) {
3940 for (const Instruction &I : BB) {
3941 writeInstruction(I, InstID, Vals);
3942
3943 if (!I.getType()->isVoidTy())
3944 ++InstID;
3945
3946 // If the instruction has metadata, write a metadata attachment later.
3947 NeedsMetadataAttachment |= I.hasMetadataOtherThanDebugLoc();
3948
3949 // If the instruction has a debug location, emit it.
3950 if (DILocation *DL = I.getDebugLoc()) {
3951 if (DL == LastDL) {
3952 // Just repeat the same debug loc as last time.
3954 } else {
3955 Vals.push_back(DL->getLine());
3956 Vals.push_back(DL->getColumn());
3957 Vals.push_back(VE.getMetadataOrNullID(DL->getScope()));
3958 Vals.push_back(VE.getMetadataOrNullID(DL->getInlinedAt()));
3959 Vals.push_back(DL->isImplicitCode());
3960 Vals.push_back(DL->getAtomGroup());
3961 Vals.push_back(DL->getAtomRank());
3962
3963 unsigned DLAbbrev = FUNCTION_DEBUG_LOC_ABBREV;
3964 if (DILayerLocList *IRLayers = DL->getIRLayers()) {
3965 DLAbbrev = FUNCTION_DEBUG_LOC_LAYERS_ABBREV;
3966 Vals.push_back(VE.getMetadataOrNullID(IRLayers));
3967 }
3968
3969 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC, Vals, DLAbbrev);
3970 Vals.clear();
3971 LastDL = DL;
3972 }
3973 }
3974
3975 // If the instruction has DbgRecords attached to it, emit them. Note that
3976 // they come after the instruction so that it's easy to attach them again
3977 // when reading the bitcode, even though conceptually the debug locations
3978 // start "before" the instruction.
3979 if (I.hasDbgRecords()) {
3980 /// Try to push the value only (unwrapped), otherwise push the
3981 /// metadata wrapped value. Returns true if the value was pushed
3982 /// without the ValueAsMetadata wrapper.
3983 auto PushValueOrMetadata = [&Vals, InstID,
3984 this](Metadata *RawLocation) {
3985 assert(RawLocation &&
3986 "RawLocation unexpectedly null in DbgVariableRecord");
3987 if (ValueAsMetadata *VAM = dyn_cast<ValueAsMetadata>(RawLocation)) {
3988 SmallVector<unsigned, 2> ValAndType;
3989 // If the value is a fwd-ref the type is also pushed. We don't
3990 // want the type, so fwd-refs are kept wrapped (pushValueAndType
3991 // returns false if the value is pushed without type).
3992 if (!pushValueAndType(VAM->getValue(), InstID, ValAndType)) {
3993 Vals.push_back(ValAndType[0]);
3994 return true;
3995 }
3996 }
3997 // The metadata is a DIArgList, or ValueAsMetadata wrapping a
3998 // fwd-ref. Push the metadata ID.
3999 Vals.push_back(VE.getMetadataID(RawLocation));
4000 return false;
4001 };
4002
4003 // Write out non-instruction debug information attached to this
4004 // instruction. Write it after the instruction so that it's easy to
4005 // re-attach to the instruction reading the records in.
4006 for (DbgRecord &DR : I.getDbgMarker()->getDbgRecordRange()) {
4007 if (DbgLabelRecord *DLR = dyn_cast<DbgLabelRecord>(&DR)) {
4008 Vals.push_back(VE.getMetadataID(&*DLR->getDebugLoc()));
4009 Vals.push_back(VE.getMetadataID(DLR->getLabel()));
4011 Vals.clear();
4012 continue;
4013 }
4014
4015 // First 3 fields are common to all kinds:
4016 // DILocation, DILocalVariable, DIExpression
4017 // dbg_value (FUNC_CODE_DEBUG_RECORD_VALUE)
4018 // ..., LocationMetadata
4019 // dbg_value (FUNC_CODE_DEBUG_RECORD_VALUE_SIMPLE - abbrev'd)
4020 // ..., Value
4021 // dbg_declare (FUNC_CODE_DEBUG_RECORD_DECLARE)
4022 // ..., LocationMetadata
4023 // dbg_assign (FUNC_CODE_DEBUG_RECORD_ASSIGN)
4024 // ..., LocationMetadata, DIAssignID, DIExpression, LocationMetadata
4025 DbgVariableRecord &DVR = cast<DbgVariableRecord>(DR);
4026 Vals.push_back(VE.getMetadataID(&*DVR.getDebugLoc()));
4027 Vals.push_back(VE.getMetadataID(DVR.getVariable()));
4028 Vals.push_back(VE.getMetadataID(DVR.getExpression()));
4029 if (DVR.isDbgValue()) {
4030 if (PushValueOrMetadata(DVR.getRawLocation()))
4032 FUNCTION_DEBUG_RECORD_VALUE_ABBREV);
4033 else
4035 } else if (DVR.isDbgDeclare()) {
4036 Vals.push_back(VE.getMetadataID(DVR.getRawLocation()));
4038 } else if (DVR.isDbgDeclareValue()) {
4039 Vals.push_back(VE.getMetadataID(DVR.getRawLocation()));
4041 } else {
4042 assert(DVR.isDbgAssign() && "Unexpected DbgRecord kind");
4043 Vals.push_back(VE.getMetadataID(DVR.getRawLocation()));
4044 Vals.push_back(VE.getMetadataID(DVR.getAssignID()));
4046 Vals.push_back(VE.getMetadataID(DVR.getRawAddress()));
4048 }
4049 Vals.clear();
4050 }
4051 }
4052 }
4053
4054 if (BlockAddress *BA = BlockAddress::lookup(&BB)) {
4055 SmallVector<Value *> Worklist{BA};
4056 SmallPtrSet<Value *, 8> Visited{BA};
4057 while (!Worklist.empty()) {
4058 Value *V = Worklist.pop_back_val();
4059 for (User *U : V->users()) {
4060 if (auto *I = dyn_cast<Instruction>(U)) {
4061 Function *P = I->getFunction();
4062 if (P != &F)
4063 BlockAddressUsers.insert(P);
4064 } else if (isa<Constant>(U) && !isa<GlobalValue>(U) &&
4065 Visited.insert(U).second)
4066 Worklist.push_back(U);
4067 }
4068 }
4069 }
4070 }
4071
4072 if (!BlockAddressUsers.empty()) {
4073 Vals.resize(BlockAddressUsers.size());
4074 for (auto I : llvm::enumerate(BlockAddressUsers))
4075 Vals[I.index()] = VE.getValueID(I.value());
4077 Vals.clear();
4078 }
4079
4080 // Emit names for all the instructions etc.
4081 if (auto *Symtab = F.getValueSymbolTable())
4082 writeFunctionLevelValueSymbolTable(*Symtab);
4083
4084 if (NeedsMetadataAttachment)
4085 writeFunctionMetadataAttachment(F);
4087 writeUseListBlock(&F);
4088 VE.purgeFunction();
4089 Stream.ExitBlock();
4090}
4091
4092// Emit blockinfo, which defines the standard abbreviations etc.
4093void ModuleBitcodeWriter::writeBlockInfo() {
4094 // We only want to emit block info records for blocks that have multiple
4095 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK.
4096 // Other blocks can define their abbrevs inline.
4097 Stream.EnterBlockInfoBlock();
4098
4099 // Encode type indices using fixed size based on number of types.
4100 BitCodeAbbrevOp TypeAbbrevOp(BitCodeAbbrevOp::Fixed,
4102 // Encode value indices as 6-bit VBR.
4103 BitCodeAbbrevOp ValAbbrevOp(BitCodeAbbrevOp::VBR, 6);
4104
4105 { // 8-bit fixed-width VST_CODE_ENTRY/VST_CODE_BBENTRY strings.
4106 auto Abbv = std::make_shared<BitCodeAbbrev>();
4107 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));
4108 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
4109 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
4110 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
4112 VST_ENTRY_8_ABBREV)
4113 llvm_unreachable("Unexpected abbrev ordering!");
4114 }
4115
4116 { // 7-bit fixed width VST_CODE_ENTRY strings.
4117 auto Abbv = std::make_shared<BitCodeAbbrev>();
4118 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
4119 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
4120 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
4121 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
4123 VST_ENTRY_7_ABBREV)
4124 llvm_unreachable("Unexpected abbrev ordering!");
4125 }
4126 { // 6-bit char6 VST_CODE_ENTRY strings.
4127 auto Abbv = std::make_shared<BitCodeAbbrev>();
4128 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
4129 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
4130 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
4131 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
4133 VST_ENTRY_6_ABBREV)
4134 llvm_unreachable("Unexpected abbrev ordering!");
4135 }
4136 { // 6-bit char6 VST_CODE_BBENTRY strings.
4137 auto Abbv = std::make_shared<BitCodeAbbrev>();
4138 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY));
4139 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
4140 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
4141 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
4143 VST_BBENTRY_6_ABBREV)
4144 llvm_unreachable("Unexpected abbrev ordering!");
4145 }
4146
4147 { // SETTYPE abbrev for CONSTANTS_BLOCK.
4148 auto Abbv = std::make_shared<BitCodeAbbrev>();
4149 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE));
4150 Abbv->Add(TypeAbbrevOp);
4152 CONSTANTS_SETTYPE_ABBREV)
4153 llvm_unreachable("Unexpected abbrev ordering!");
4154 }
4155
4156 { // INTEGER abbrev for CONSTANTS_BLOCK.
4157 auto Abbv = std::make_shared<BitCodeAbbrev>();
4158 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER));
4159 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
4161 CONSTANTS_INTEGER_ABBREV)
4162 llvm_unreachable("Unexpected abbrev ordering!");
4163 }
4164
4165 { // BYTE abbrev for CONSTANTS_BLOCK.
4166 auto Abbv = std::make_shared<BitCodeAbbrev>();
4167 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_BYTE));
4168 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
4170 CONSTANTS_BYTE_ABBREV)
4171 llvm_unreachable("Unexpected abbrev ordering!");
4172 }
4173
4174 { // CE_CAST abbrev for CONSTANTS_BLOCK.
4175 auto Abbv = std::make_shared<BitCodeAbbrev>();
4176 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST));
4177 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc
4178 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid
4180 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
4181
4183 CONSTANTS_CE_CAST_Abbrev)
4184 llvm_unreachable("Unexpected abbrev ordering!");
4185 }
4186 { // NULL abbrev for CONSTANTS_BLOCK.
4187 auto Abbv = std::make_shared<BitCodeAbbrev>();
4188 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL));
4190 CONSTANTS_NULL_Abbrev)
4191 llvm_unreachable("Unexpected abbrev ordering!");
4192 }
4193
4194 // FIXME: This should only use space for first class types!
4195
4196 { // INST_LOAD abbrev for FUNCTION_BLOCK.
4197 auto Abbv = std::make_shared<BitCodeAbbrev>();
4198 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD));
4199 Abbv->Add(ValAbbrevOp); // Ptr
4200 Abbv->Add(TypeAbbrevOp); // dest ty
4201 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align
4202 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
4203 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
4204 FUNCTION_INST_LOAD_ABBREV)
4205 llvm_unreachable("Unexpected abbrev ordering!");
4206 }
4207 {
4208 auto Abbv = std::make_shared<BitCodeAbbrev>();
4209 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_STORE));
4210 Abbv->Add(ValAbbrevOp); // op1
4211 Abbv->Add(ValAbbrevOp); // op0
4212 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // align
4213 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
4214 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
4215 FUNCTION_INST_STORE_ABBREV)
4216 llvm_unreachable("Unexpected abbrev ordering!");
4217 }
4218 { // INST_UNOP abbrev for FUNCTION_BLOCK.
4219 auto Abbv = std::make_shared<BitCodeAbbrev>();
4220 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNOP));
4221 Abbv->Add(ValAbbrevOp); // LHS
4222 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
4223 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
4224 FUNCTION_INST_UNOP_ABBREV)
4225 llvm_unreachable("Unexpected abbrev ordering!");
4226 }
4227 { // INST_UNOP_FLAGS abbrev for FUNCTION_BLOCK.
4228 auto Abbv = std::make_shared<BitCodeAbbrev>();
4229 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNOP));
4230 Abbv->Add(ValAbbrevOp); // LHS
4231 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
4232 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); // flags
4233 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
4234 FUNCTION_INST_UNOP_FLAGS_ABBREV)
4235 llvm_unreachable("Unexpected abbrev ordering!");
4236 }
4237 { // INST_BINOP abbrev for FUNCTION_BLOCK.
4238 auto Abbv = std::make_shared<BitCodeAbbrev>();
4239 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
4240 Abbv->Add(ValAbbrevOp); // LHS
4241 Abbv->Add(ValAbbrevOp); // RHS
4242 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
4243 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
4244 FUNCTION_INST_BINOP_ABBREV)
4245 llvm_unreachable("Unexpected abbrev ordering!");
4246 }
4247 { // INST_BINOP_FLAGS abbrev for FUNCTION_BLOCK.
4248 auto Abbv = std::make_shared<BitCodeAbbrev>();
4249 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
4250 Abbv->Add(ValAbbrevOp); // LHS
4251 Abbv->Add(ValAbbrevOp); // RHS
4252 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
4253 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); // flags
4254 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
4255 FUNCTION_INST_BINOP_FLAGS_ABBREV)
4256 llvm_unreachable("Unexpected abbrev ordering!");
4257 }
4258 { // INST_CAST abbrev for FUNCTION_BLOCK.
4259 auto Abbv = std::make_shared<BitCodeAbbrev>();
4260 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST));
4261 Abbv->Add(ValAbbrevOp); // OpVal
4262 Abbv->Add(TypeAbbrevOp); // dest ty
4263 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
4264 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
4265 FUNCTION_INST_CAST_ABBREV)
4266 llvm_unreachable("Unexpected abbrev ordering!");
4267 }
4268 { // INST_CAST_FLAGS abbrev for FUNCTION_BLOCK.
4269 auto Abbv = std::make_shared<BitCodeAbbrev>();
4270 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST));
4271 Abbv->Add(ValAbbrevOp); // OpVal
4272 Abbv->Add(TypeAbbrevOp); // dest ty
4273 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
4274 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 9)); // flags
4275 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
4276 FUNCTION_INST_CAST_FLAGS_ABBREV)
4277 llvm_unreachable("Unexpected abbrev ordering!");
4278 }
4279
4280 { // INST_RET abbrev for FUNCTION_BLOCK.
4281 auto Abbv = std::make_shared<BitCodeAbbrev>();
4282 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
4283 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
4284 FUNCTION_INST_RET_VOID_ABBREV)
4285 llvm_unreachable("Unexpected abbrev ordering!");
4286 }
4287 { // INST_RET abbrev for FUNCTION_BLOCK.
4288 auto Abbv = std::make_shared<BitCodeAbbrev>();
4289 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
4290 Abbv->Add(ValAbbrevOp);
4291 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
4292 FUNCTION_INST_RET_VAL_ABBREV)
4293 llvm_unreachable("Unexpected abbrev ordering!");
4294 }
4295 {
4296 auto Abbv = std::make_shared<BitCodeAbbrev>();
4297 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BR));
4298 // TODO: Use different abbrev for absolute value reference (succ0)?
4299 Abbv->Add(ValAbbrevOp); // succ0
4300 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
4301 FUNCTION_INST_BR_UNCOND_ABBREV)
4302 llvm_unreachable("Unexpected abbrev ordering!");
4303 }
4304 {
4305 auto Abbv = std::make_shared<BitCodeAbbrev>();
4306 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BR));
4307 // TODO: Use different abbrev for absolute value references (succ0, succ1)?
4308 Abbv->Add(ValAbbrevOp); // succ0
4309 Abbv->Add(ValAbbrevOp); // succ1
4310 Abbv->Add(ValAbbrevOp); // cond
4311 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
4312 FUNCTION_INST_BR_COND_ABBREV)
4313 llvm_unreachable("Unexpected abbrev ordering!");
4314 }
4315 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
4316 auto Abbv = std::make_shared<BitCodeAbbrev>();
4317 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE));
4318 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
4319 FUNCTION_INST_UNREACHABLE_ABBREV)
4320 llvm_unreachable("Unexpected abbrev ordering!");
4321 }
4322 {
4323 auto Abbv = std::make_shared<BitCodeAbbrev>();
4324 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_GEP));
4325 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // flags
4326 Abbv->Add(TypeAbbrevOp); // dest ty
4327 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
4328 Abbv->Add(ValAbbrevOp);
4329 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
4330 FUNCTION_INST_GEP_ABBREV)
4331 llvm_unreachable("Unexpected abbrev ordering!");
4332 }
4333 {
4334 auto Abbv = std::make_shared<BitCodeAbbrev>();
4335 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CMP2));
4336 Abbv->Add(ValAbbrevOp); // op0
4337 Abbv->Add(ValAbbrevOp); // op1
4338 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 6)); // pred
4339 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
4340 FUNCTION_INST_CMP_ABBREV)
4341 llvm_unreachable("Unexpected abbrev ordering!");
4342 }
4343 {
4344 auto Abbv = std::make_shared<BitCodeAbbrev>();
4345 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CMP2));
4346 Abbv->Add(ValAbbrevOp); // op0
4347 Abbv->Add(ValAbbrevOp); // op1
4348 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 6)); // pred
4349 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); // flags
4350 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
4351 FUNCTION_INST_CMP_FLAGS_ABBREV)
4352 llvm_unreachable("Unexpected abbrev ordering!");
4353 }
4354 {
4355 auto Abbv = std::make_shared<BitCodeAbbrev>();
4356 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_DEBUG_RECORD_VALUE_SIMPLE));
4357 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 7)); // dbgloc
4358 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 7)); // var
4359 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 7)); // expr
4360 Abbv->Add(ValAbbrevOp); // val
4361 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
4362 FUNCTION_DEBUG_RECORD_VALUE_ABBREV)
4363 llvm_unreachable("Unexpected abbrev ordering! 1");
4364 }
4365 {
4366 auto Abbv = std::make_shared<BitCodeAbbrev>();
4367 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_DEBUG_LOC));
4368 // NOTE: No IsDistinct field for FUNC_CODE_DEBUG_LOC.
4369 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
4370 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
4371 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
4372 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
4373 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
4374 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Atom group.
4375 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3)); // Atom rank.
4376 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
4377 FUNCTION_DEBUG_LOC_ABBREV)
4378 llvm_unreachable("Unexpected abbrev ordering!");
4379 }
4380 { // DEBUG_LOC abbrev for FUNCTION_BLOCK, irlayers variant.
4381 // Separate abbrev so a location without layers does not spend a VBR chunk
4382 // encoding a zero; writeInstruction picks between the two per record.
4383 auto Abbv = std::make_shared<BitCodeAbbrev>();
4384 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_DEBUG_LOC));
4385 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
4386 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
4387 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
4388 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
4389 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
4390 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Atom group.
4391 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3)); // Atom rank.
4392 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // irlayers.
4393 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
4394 FUNCTION_DEBUG_LOC_LAYERS_ABBREV)
4395 llvm_unreachable("Unexpected abbrev ordering!");
4396 }
4397 Stream.ExitBlock();
4398}
4399
4400/// Write the module path strings, currently only used when generating
4401/// a combined index file.
4402void IndexBitcodeWriter::writeModStrings() {
4404
4405 // TODO: See which abbrev sizes we actually need to emit
4406
4407 // 8-bit fixed-width MST_ENTRY strings.
4408 auto Abbv = std::make_shared<BitCodeAbbrev>();
4409 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
4410 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
4411 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
4412 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
4413 unsigned Abbrev8Bit = Stream.EmitAbbrev(std::move(Abbv));
4414
4415 // 7-bit fixed width MST_ENTRY strings.
4416 Abbv = std::make_shared<BitCodeAbbrev>();
4417 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
4418 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
4419 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
4420 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
4421 unsigned Abbrev7Bit = Stream.EmitAbbrev(std::move(Abbv));
4422
4423 // 6-bit char6 MST_ENTRY strings.
4424 Abbv = std::make_shared<BitCodeAbbrev>();
4425 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
4426 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
4427 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
4428 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
4429 unsigned Abbrev6Bit = Stream.EmitAbbrev(std::move(Abbv));
4430
4431 // Module Hash, 160 bits SHA1. Optionally, emitted after each MST_CODE_ENTRY.
4432 Abbv = std::make_shared<BitCodeAbbrev>();
4433 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_HASH));
4434 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
4435 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
4436 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
4437 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
4438 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
4439 unsigned AbbrevHash = Stream.EmitAbbrev(std::move(Abbv));
4440
4442 forEachModule([&](const StringMapEntry<ModuleHash> &MPSE) {
4443 StringRef Key = MPSE.getKey();
4444 const auto &Hash = MPSE.getValue();
4446 unsigned AbbrevToUse = Abbrev8Bit;
4447 if (Bits == SE_Char6)
4448 AbbrevToUse = Abbrev6Bit;
4449 else if (Bits == SE_Fixed7)
4450 AbbrevToUse = Abbrev7Bit;
4451
4452 auto ModuleId = ModuleIdMap.size();
4453 ModuleIdMap[Key] = ModuleId;
4454 Vals.push_back(ModuleId);
4455 // Use bytes_begin/end() for unsigned char iteration.
4456 Vals.append(Key.bytes_begin(), Key.bytes_end());
4457
4458 // Emit the finished record.
4459 Stream.EmitRecord(bitc::MST_CODE_ENTRY, Vals, AbbrevToUse);
4460
4461 // Emit an optional hash for the module now
4462 if (llvm::any_of(Hash, [](uint32_t H) { return H; })) {
4463 Vals.assign(Hash.begin(), Hash.end());
4464 // Emit the hash record.
4465 Stream.EmitRecord(bitc::MST_CODE_HASH, Vals, AbbrevHash);
4466 }
4467
4468 Vals.clear();
4469 });
4470 Stream.ExitBlock();
4471}
4472
4473/// Write the function type metadata related records that need to appear before
4474/// a function summary entry (whether per-module or combined).
4475template <typename Fn>
4477 FunctionSummary *FS,
4478 Fn GetValueID) {
4479 if (!FS->type_tests().empty())
4480 Stream.EmitRecord(bitc::FS_TYPE_TESTS, FS->type_tests());
4481
4483
4484 auto WriteVFuncIdVec = [&](uint64_t Ty,
4486 if (VFs.empty())
4487 return;
4488 Record.clear();
4489 for (auto &VF : VFs) {
4490 Record.push_back(VF.GUID);
4491 Record.push_back(VF.Offset);
4492 }
4493 Stream.EmitRecord(Ty, Record);
4494 };
4495
4496 WriteVFuncIdVec(bitc::FS_TYPE_TEST_ASSUME_VCALLS,
4497 FS->type_test_assume_vcalls());
4498 WriteVFuncIdVec(bitc::FS_TYPE_CHECKED_LOAD_VCALLS,
4499 FS->type_checked_load_vcalls());
4500
4501 auto WriteConstVCallVec = [&](uint64_t Ty,
4503 for (auto &VC : VCs) {
4504 Record.clear();
4505 Record.push_back(VC.VFunc.GUID);
4506 Record.push_back(VC.VFunc.Offset);
4507 llvm::append_range(Record, VC.Args);
4508 Stream.EmitRecord(Ty, Record);
4509 }
4510 };
4511
4512 WriteConstVCallVec(bitc::FS_TYPE_TEST_ASSUME_CONST_VCALL,
4513 FS->type_test_assume_const_vcalls());
4514 WriteConstVCallVec(bitc::FS_TYPE_CHECKED_LOAD_CONST_VCALL,
4515 FS->type_checked_load_const_vcalls());
4516
4517 auto WriteRange = [&](ConstantRange Range) {
4519 assert(Range.getLower().getNumWords() == 1);
4520 assert(Range.getUpper().getNumWords() == 1);
4521 emitSignedInt64(Record, *Range.getLower().getRawData());
4522 emitSignedInt64(Record, *Range.getUpper().getRawData());
4523 };
4524
4525 if (!FS->paramAccesses().empty()) {
4526 Record.clear();
4527 for (auto &Arg : FS->paramAccesses()) {
4528 size_t UndoSize = Record.size();
4529 Record.push_back(Arg.ParamNo);
4530 WriteRange(Arg.Use);
4531 Record.push_back(Arg.Calls.size());
4532 for (auto &Call : Arg.Calls) {
4533 Record.push_back(Call.ParamNo);
4534 std::optional<unsigned> ValueID = GetValueID(Call.Callee);
4535 if (!ValueID) {
4536 // If ValueID is unknown we can't drop just this call, we must drop
4537 // entire parameter.
4538 Record.resize(UndoSize);
4539 break;
4540 }
4541 Record.push_back(*ValueID);
4542 WriteRange(Call.Offsets);
4543 }
4544 }
4545 if (!Record.empty())
4547 }
4548}
4549
4550/// Collect type IDs from type tests used by function.
4551static void
4553 std::set<GlobalValue::GUID> &ReferencedTypeIds) {
4554 if (!FS->type_tests().empty())
4555 for (auto &TT : FS->type_tests())
4556 ReferencedTypeIds.insert(TT);
4557
4558 auto GetReferencedTypesFromVFuncIdVec =
4560 for (auto &VF : VFs)
4561 ReferencedTypeIds.insert(VF.GUID);
4562 };
4563
4564 GetReferencedTypesFromVFuncIdVec(FS->type_test_assume_vcalls());
4565 GetReferencedTypesFromVFuncIdVec(FS->type_checked_load_vcalls());
4566
4567 auto GetReferencedTypesFromConstVCallVec =
4569 for (auto &VC : VCs)
4570 ReferencedTypeIds.insert(VC.VFunc.GUID);
4571 };
4572
4573 GetReferencedTypesFromConstVCallVec(FS->type_test_assume_const_vcalls());
4574 GetReferencedTypesFromConstVCallVec(FS->type_checked_load_const_vcalls());
4575}
4576
4578 SmallVector<uint64_t, 64> &NameVals, const std::vector<uint64_t> &args,
4580 NameVals.push_back(args.size());
4581 llvm::append_range(NameVals, args);
4582
4583 NameVals.push_back(ByArg.TheKind);
4584 NameVals.push_back(ByArg.Info);
4585 NameVals.push_back(ByArg.Byte);
4586 NameVals.push_back(ByArg.Bit);
4587}
4588
4590 SmallVector<uint64_t, 64> &NameVals, StringTableBuilder &StrtabBuilder,
4591 uint64_t Id, const WholeProgramDevirtResolution &Wpd) {
4592 NameVals.push_back(Id);
4593
4594 NameVals.push_back(Wpd.TheKind);
4595 NameVals.push_back(StrtabBuilder.add(Wpd.SingleImplName));
4596 NameVals.push_back(Wpd.SingleImplName.size());
4597
4598 NameVals.push_back(Wpd.ResByArg.size());
4599 for (auto &A : Wpd.ResByArg)
4600 writeWholeProgramDevirtResolutionByArg(NameVals, A.first, A.second);
4601}
4602
4604 StringTableBuilder &StrtabBuilder,
4605 StringRef Id,
4606 const TypeIdSummary &Summary) {
4607 NameVals.push_back(StrtabBuilder.add(Id));
4608 NameVals.push_back(Id.size());
4609
4610 NameVals.push_back(Summary.TTRes.TheKind);
4611 NameVals.push_back(Summary.TTRes.SizeM1BitWidth);
4612 NameVals.push_back(Summary.TTRes.AlignLog2);
4613 NameVals.push_back(Summary.TTRes.SizeM1);
4614 NameVals.push_back(Summary.TTRes.BitMask);
4615 NameVals.push_back(Summary.TTRes.InlineBits);
4616
4617 for (auto &W : Summary.WPDRes)
4618 writeWholeProgramDevirtResolution(NameVals, StrtabBuilder, W.first,
4619 W.second);
4620}
4621
4623 SmallVector<uint64_t, 64> &NameVals, StringTableBuilder &StrtabBuilder,
4624 StringRef Id, const TypeIdCompatibleVtableInfo &Summary,
4626 NameVals.push_back(StrtabBuilder.add(Id));
4627 NameVals.push_back(Id.size());
4628
4629 for (auto &P : Summary) {
4630 NameVals.push_back(P.AddressPointOffset);
4631 NameVals.push_back(VE.getValueID(P.VTableVI.getValue()));
4632 }
4633}
4634
4635// Adds the allocation contexts to the CallStacks map. We simply use the
4636// size at the time the context was added as the CallStackId. This works because
4637// when we look up the call stacks later on we process the function summaries
4638// and their allocation records in the same exact order.
4640 FunctionSummary *FS, std::function<LinearFrameId(unsigned)> GetStackIndex,
4642 // The interfaces in ProfileData/MemProf.h use a type alias for a stack frame
4643 // id offset into the index of the full stack frames. The ModuleSummaryIndex
4644 // currently uses unsigned. Make sure these stay in sync.
4645 static_assert(std::is_same_v<LinearFrameId, unsigned>);
4646 for (auto &AI : FS->allocs()) {
4647 for (auto &MIB : AI.MIBs) {
4648 SmallVector<unsigned> StackIdIndices;
4649 StackIdIndices.reserve(MIB.StackIdIndices.size());
4650 for (auto Id : MIB.StackIdIndices)
4651 StackIdIndices.push_back(GetStackIndex(Id));
4652 // The CallStackId is the size at the time this context was inserted.
4653 CallStacks.insert({CallStacks.size(), StackIdIndices});
4654 }
4655 }
4656}
4657
4658// Build the radix tree from the accumulated CallStacks, write out the resulting
4659// linearized radix tree array, and return the map of call stack positions into
4660// this array for use when writing the allocation records. The returned map is
4661// indexed by a CallStackId which in this case is implicitly determined by the
4662// order of function summaries and their allocation infos being written.
4665 BitstreamWriter &Stream, unsigned RadixAbbrev) {
4666 assert(!CallStacks.empty());
4667 DenseMap<unsigned, FrameStat> FrameHistogram =
4670 // We don't need a MemProfFrameIndexes map as we have already converted the
4671 // full stack id hash to a linear offset into the StackIds array.
4672 Builder.build(std::move(CallStacks), /*MemProfFrameIndexes=*/nullptr,
4673 FrameHistogram);
4674 Stream.EmitRecord(bitc::FS_CONTEXT_RADIX_TREE_ARRAY, Builder.getRadixArray(),
4675 RadixAbbrev);
4676 return Builder.takeCallStackPos();
4677}
4678
4680 BitstreamWriter &Stream, FunctionSummary *FS, unsigned CallsiteAbbrev,
4681 unsigned AllocAbbrev, unsigned ContextIdAbbvId, bool PerModule,
4682 std::function<unsigned(const ValueInfo &VI)> GetValueID,
4683 std::function<unsigned(unsigned)> GetStackIndex,
4684 bool WriteContextSizeInfoIndex,
4686 CallStackId &CallStackCount) {
4688
4689 for (auto &CI : FS->callsites()) {
4690 Record.clear();
4691 // Per module callsite clones should always have a single entry of
4692 // value 0.
4693 assert(!PerModule || (CI.Clones.size() == 1 && CI.Clones[0] == 0));
4694 Record.push_back(GetValueID(CI.Callee));
4695 if (!PerModule) {
4696 Record.push_back(CI.StackIdIndices.size());
4697 Record.push_back(CI.Clones.size());
4698 }
4699 for (auto Id : CI.StackIdIndices)
4700 Record.push_back(GetStackIndex(Id));
4701 if (!PerModule)
4702 llvm::append_range(Record, CI.Clones);
4705 Record, CallsiteAbbrev);
4706 }
4707
4708 for (auto &AI : FS->allocs()) {
4709 Record.clear();
4710 // Per module alloc versions should always have a single entry of
4711 // value 0.
4712 assert(!PerModule || (AI.Versions.size() == 1 && AI.Versions[0] == 0));
4713 Record.push_back(AI.MIBs.size());
4714 if (!PerModule)
4715 Record.push_back(AI.Versions.size());
4716 for (auto &MIB : AI.MIBs) {
4717 Record.push_back((uint8_t)MIB.AllocType);
4718 // The per-module summary always needs to include the alloc context, as we
4719 // use it during the thin link. For the combined index it is optional (see
4720 // comments where CombinedIndexMemProfContext is defined).
4721 if (PerModule || CombinedIndexMemProfContext) {
4722 // Record the index into the radix tree array for this context.
4723 assert(CallStackCount <= CallStackPos.size());
4724 Record.push_back(CallStackPos[CallStackCount++]);
4725 }
4726 }
4727 if (!PerModule)
4728 llvm::append_range(Record, AI.Versions);
4729 assert(AI.ContextSizeInfos.empty() ||
4730 AI.ContextSizeInfos.size() == AI.MIBs.size());
4731 // Optionally emit the context size information if it exists.
4732 if (WriteContextSizeInfoIndex && !AI.ContextSizeInfos.empty()) {
4733 // The abbreviation id for the context ids record should have been created
4734 // if we are emitting the per-module index, which is where we write this
4735 // info.
4736 assert(ContextIdAbbvId);
4737 SmallVector<uint32_t> ContextIds;
4738 // At least one context id per ContextSizeInfos entry (MIB), broken into 2
4739 // halves.
4740 ContextIds.reserve(AI.ContextSizeInfos.size() * 2);
4741 for (auto &Infos : AI.ContextSizeInfos) {
4742 Record.push_back(Infos.size());
4743 for (auto [FullStackId, TotalSize] : Infos) {
4744 // The context ids are emitted separately as a fixed width array,
4745 // which is more efficient than a VBR given that these hashes are
4746 // typically close to 64-bits. The max fixed width entry is 32 bits so
4747 // it is split into 2.
4748 ContextIds.push_back(static_cast<uint32_t>(FullStackId >> 32));
4749 ContextIds.push_back(static_cast<uint32_t>(FullStackId));
4750 Record.push_back(TotalSize);
4751 }
4752 }
4753 // The context ids are expected by the reader to immediately precede the
4754 // associated alloc info record.
4755 Stream.EmitRecord(bitc::FS_ALLOC_CONTEXT_IDS, ContextIds,
4756 ContextIdAbbvId);
4757 }
4758 Stream.EmitRecord(PerModule
4763 Record, AllocAbbrev);
4764 }
4765}
4766
4767// Helper to emit a single function summary record.
4768void ModuleBitcodeWriterBase::writePerModuleFunctionSummaryRecord(
4769 SmallVector<uint64_t, 64> &NameVals, GlobalValueSummary *Summary,
4770 unsigned ValueID, unsigned FSCallsProfileAbbrev, unsigned CallsiteAbbrev,
4771 unsigned AllocAbbrev, unsigned ContextIdAbbvId, const Function &F,
4772 DenseMap<CallStackId, LinearCallStackId> &CallStackPos,
4773 CallStackId &CallStackCount) {
4774 NameVals.push_back(ValueID);
4775
4776 FunctionSummary *FS = cast<FunctionSummary>(Summary);
4777
4779 Stream, FS, [&](const ValueInfo &VI) -> std::optional<unsigned> {
4780 return {VE.getValueID(VI.getValue())};
4781 });
4782
4783 auto SpecialRefCnts = FS->specialRefCounts();
4784 NameVals.push_back(getEncodedGVSummaryFlags(FS->flags()));
4785 NameVals.push_back(FS->instCount());
4786 NameVals.push_back(getEncodedFFlags(FS->fflags()));
4787 NameVals.push_back(FS->refs().size());
4788 NameVals.push_back(SpecialRefCnts.first); // rorefcnt
4789 NameVals.push_back(SpecialRefCnts.second); // worefcnt
4790
4791 for (auto &RI : FS->refs())
4792 NameVals.push_back(getValueId(RI));
4793
4794 for (auto &ECI : FS->calls()) {
4795 NameVals.push_back(getValueId(ECI.first));
4796 NameVals.push_back(getEncodedHotnessCallEdgeInfo(ECI.second));
4797 }
4798
4799 // Emit the finished record.
4800 Stream.EmitRecord(bitc::FS_PERMODULE_PROFILE, NameVals, FSCallsProfileAbbrev);
4801 NameVals.clear();
4802
4804 Stream, FS, CallsiteAbbrev, AllocAbbrev, ContextIdAbbvId,
4805 /*PerModule*/ true,
4806 /*GetValueId*/ [&](const ValueInfo &VI) { return getValueId(VI); },
4807 /*GetStackIndex*/ [&](unsigned I) { return I; },
4808 /*WriteContextSizeInfoIndex*/ true, CallStackPos, CallStackCount);
4809}
4810
4811// Collect the global value references in the given variable's initializer,
4812// and emit them in a summary record.
4813void ModuleBitcodeWriterBase::writeModuleLevelReferences(
4814 const GlobalVariable &V, SmallVector<uint64_t, 64> &NameVals,
4815 unsigned FSModRefsAbbrev, unsigned FSModVTableRefsAbbrev) {
4816 // Be a little lenient here, to accomodate older files without GUIDs
4817 // already computed and assigned as metadata.
4818 GlobalValue::GUID GUID = V.getGUIDOrFallback();
4819
4820 auto VI = Index->getValueInfo(GUID);
4821 if (!VI || VI.getSummaryList().empty()) {
4822 // Only declarations should not have a summary (a declaration might however
4823 // have a summary if the def was in module level asm).
4824 assert(V.isDeclaration());
4825 return;
4826 }
4827 auto *Summary = VI.getSummaryList()[0].get();
4828 NameVals.push_back(VE.getValueID(&V));
4829 GlobalVarSummary *VS = cast<GlobalVarSummary>(Summary);
4830 NameVals.push_back(getEncodedGVSummaryFlags(VS->flags()));
4831 NameVals.push_back(getEncodedGVarFlags(VS->varflags()));
4832
4833 auto VTableFuncs = VS->vTableFuncs();
4834 if (!VTableFuncs.empty())
4835 NameVals.push_back(VS->refs().size());
4836
4837 unsigned SizeBeforeRefs = NameVals.size();
4838 for (auto &RI : VS->refs())
4839 NameVals.push_back(VE.getValueID(RI.getValue()));
4840 // Sort the refs for determinism output, the vector returned by FS->refs() has
4841 // been initialized from a DenseSet.
4842 llvm::sort(drop_begin(NameVals, SizeBeforeRefs));
4843
4844 if (VTableFuncs.empty())
4846 FSModRefsAbbrev);
4847 else {
4848 // VTableFuncs pairs should already be sorted by offset.
4849 for (auto &P : VTableFuncs) {
4850 NameVals.push_back(VE.getValueID(P.FuncVI.getValue()));
4851 NameVals.push_back(P.VTableOffset);
4852 }
4853
4855 FSModVTableRefsAbbrev);
4856 }
4857 NameVals.clear();
4858}
4859
4860/// Emit the per-module summary section alongside the rest of
4861/// the module's bitcode.
4862void ModuleBitcodeWriterBase::writePerModuleGlobalValueSummary() {
4863 // By default we compile with ThinLTO if the module has a summary, but the
4864 // client can request full LTO with a module flag.
4865 bool IsThinLTO = true;
4866 if (auto *MD =
4867 mdconst::extract_or_null<ConstantInt>(M.getModuleFlag("ThinLTO")))
4868 IsThinLTO = MD->getZExtValue();
4871 4);
4872
4873 Stream.EmitRecord(
4875 ArrayRef<uint64_t>{ModuleSummaryIndex::BitcodeSummaryVersion});
4876
4877 // Write the index flags.
4878 uint64_t Flags = 0;
4879 // Bits 1-3 are set only in the combined index, skip them.
4880 if (Index->enableSplitLTOUnit())
4881 Flags |= 0x8;
4882 if (Index->hasUnifiedLTO())
4883 Flags |= 0x200;
4884
4885 Stream.EmitRecord(bitc::FS_FLAGS, ArrayRef<uint64_t>{Flags});
4886
4887 if (Index->begin() == Index->end()) {
4888 Stream.ExitBlock();
4889 return;
4890 }
4891
4892 auto Abbv = std::make_shared<BitCodeAbbrev>();
4893 Abbv->Add(BitCodeAbbrevOp(bitc::FS_VALUE_GUID));
4894 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
4895 // GUIDS often use up most of 64-bits, so encode as two Fixed 32.
4896 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
4897 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
4898 unsigned ValueGuidAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4899
4900 for (const auto &GVI : valueIds()) {
4902 ArrayRef<uint32_t>{GVI.second,
4903 static_cast<uint32_t>(GVI.first >> 32),
4904 static_cast<uint32_t>(GVI.first)},
4905 ValueGuidAbbrev);
4906 }
4907
4908 if (!Index->stackIds().empty()) {
4909 auto StackIdAbbv = std::make_shared<BitCodeAbbrev>();
4910 StackIdAbbv->Add(BitCodeAbbrevOp(bitc::FS_STACK_IDS));
4911 // numids x stackid
4912 StackIdAbbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
4913 // The stack ids are hashes that are close to 64 bits in size, so emitting
4914 // as a pair of 32-bit fixed-width values is more efficient than a VBR.
4915 StackIdAbbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
4916 unsigned StackIdAbbvId = Stream.EmitAbbrev(std::move(StackIdAbbv));
4917 SmallVector<uint32_t> Vals;
4918 Vals.reserve(Index->stackIds().size() * 2);
4919 for (auto Id : Index->stackIds()) {
4920 Vals.push_back(static_cast<uint32_t>(Id >> 32));
4921 Vals.push_back(static_cast<uint32_t>(Id));
4922 }
4923 Stream.EmitRecord(bitc::FS_STACK_IDS, Vals, StackIdAbbvId);
4924 }
4925
4926 unsigned ContextIdAbbvId = 0;
4928 // n x context id
4929 auto ContextIdAbbv = std::make_shared<BitCodeAbbrev>();
4930 ContextIdAbbv->Add(BitCodeAbbrevOp(bitc::FS_ALLOC_CONTEXT_IDS));
4931 ContextIdAbbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
4932 // The context ids are hashes that are close to 64 bits in size, so emitting
4933 // as a pair of 32-bit fixed-width values is more efficient than a VBR if we
4934 // are emitting them for all MIBs. Otherwise we use VBR to better compress 0
4935 // values that are expected to more frequently occur in an alloc's memprof
4936 // summary.
4938 ContextIdAbbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
4939 else
4940 ContextIdAbbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
4941 ContextIdAbbvId = Stream.EmitAbbrev(std::move(ContextIdAbbv));
4942 }
4943
4944 // Abbrev for FS_PERMODULE_PROFILE.
4945 Abbv = std::make_shared<BitCodeAbbrev>();
4946 Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE_PROFILE));
4947 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
4948 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // flags
4949 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
4950 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // fflags
4951 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
4952 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // rorefcnt
4953 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // worefcnt
4954 // numrefs x valueid, n x (valueid, hotness+tailcall+indirectcall flags)
4955 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
4956 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
4957 unsigned FSCallsProfileAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4958
4959 // Abbrev for FS_PERMODULE_GLOBALVAR_INIT_REFS.
4960 Abbv = std::make_shared<BitCodeAbbrev>();
4961 Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS));
4962 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
4963 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
4964 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); // valueids
4965 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
4966 unsigned FSModRefsAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4967
4968 // Abbrev for FS_PERMODULE_VTABLE_GLOBALVAR_INIT_REFS.
4969 Abbv = std::make_shared<BitCodeAbbrev>();
4970 Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE_VTABLE_GLOBALVAR_INIT_REFS));
4971 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
4972 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
4973 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
4974 // numrefs x valueid, n x (valueid , offset)
4975 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
4976 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
4977 unsigned FSModVTableRefsAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4978
4979 // Abbrev for FS_ALIAS.
4980 Abbv = std::make_shared<BitCodeAbbrev>();
4981 Abbv->Add(BitCodeAbbrevOp(bitc::FS_ALIAS));
4982 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
4983 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
4984 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
4985 unsigned FSAliasAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4986
4987 // Abbrev for FS_TYPE_ID_METADATA
4988 Abbv = std::make_shared<BitCodeAbbrev>();
4989 Abbv->Add(BitCodeAbbrevOp(bitc::FS_TYPE_ID_METADATA));
4990 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // typeid strtab index
4991 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // typeid length
4992 // n x (valueid , offset)
4993 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
4994 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
4995 unsigned TypeIdCompatibleVtableAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4996
4997 Abbv = std::make_shared<BitCodeAbbrev>();
4998 Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE_CALLSITE_INFO));
4999 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
5000 // n x stackidindex
5001 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
5002 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
5003 unsigned CallsiteAbbrev = Stream.EmitAbbrev(std::move(Abbv));
5004
5005 Abbv = std::make_shared<BitCodeAbbrev>();
5006 Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE_ALLOC_INFO));
5007 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // nummib
5008 // n x (alloc type, context radix tree index)
5009 // optional: nummib x (numcontext x total size)
5010 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
5011 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
5012 unsigned AllocAbbrev = Stream.EmitAbbrev(std::move(Abbv));
5013
5014 Abbv = std::make_shared<BitCodeAbbrev>();
5015 Abbv->Add(BitCodeAbbrevOp(bitc::FS_CONTEXT_RADIX_TREE_ARRAY));
5016 // n x entry
5017 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
5018 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
5019 unsigned RadixAbbrev = Stream.EmitAbbrev(std::move(Abbv));
5020
5021 // First walk through all the functions and collect the allocation contexts in
5022 // their associated summaries, for use in constructing a radix tree of
5023 // contexts. Note that we need to do this in the same order as the functions
5024 // are processed further below since the call stack positions in the resulting
5025 // radix tree array are identified based on this order.
5026 MapVector<CallStackId, llvm::SmallVector<LinearFrameId>> CallStacks;
5027 for (const Function &F : M) {
5028 // Summary emission does not support anonymous functions, they have to be
5029 // renamed using the anonymous function renaming pass.
5030 if (!F.hasName())
5031 report_fatal_error("Unexpected anonymous function when writing summary");
5032
5033 // Be a little lenient here, to accomodate older files without GUIDs
5034 // already computed and assigned as metadata.
5035 GlobalValue::GUID GUID = F.getGUIDOrFallback();
5036
5037 ValueInfo VI = Index->getValueInfo(GUID);
5038 if (!VI || VI.getSummaryList().empty()) {
5039 // Only declarations should not have a summary (a declaration might
5040 // however have a summary if the def was in module level asm).
5041 if (!F.isDeclaration())
5042 reportFatalUsageError("expected function definition " + F.getName() +
5043 " to have an associated value info.");
5044 continue;
5045 }
5046 auto *Summary = VI.getSummaryList()[0].get();
5047 FunctionSummary *FS = cast<FunctionSummary>(Summary);
5049 FS, /*GetStackIndex*/ [](unsigned I) { return I; }, CallStacks);
5050 }
5051 // Finalize the radix tree, write it out, and get the map of positions in the
5052 // linearized tree array.
5053 DenseMap<CallStackId, LinearCallStackId> CallStackPos;
5054 if (!CallStacks.empty()) {
5055 CallStackPos =
5056 writeMemoryProfileRadixTree(std::move(CallStacks), Stream, RadixAbbrev);
5057 }
5058
5059 // Keep track of the current index into the CallStackPos map.
5060 CallStackId CallStackCount = 0;
5061
5062 SmallVector<uint64_t, 64> NameVals;
5063 // Iterate over the list of functions instead of the Index to
5064 // ensure the ordering is stable.
5065 for (const Function &F : M) {
5066 // Summary emission does not support anonymous functions, they have to
5067 // renamed using the anonymous function renaming pass.
5068 if (!F.hasName())
5069 report_fatal_error("Unexpected anonymous function when writing summary");
5070
5071 GlobalValue::GUID GUID = F.getGUIDOrFallback();
5072
5073 ValueInfo VI = Index->getValueInfo(GUID);
5074 if (!VI || VI.getSummaryList().empty()) {
5075 // Only declarations should not have a summary (a declaration might
5076 // however have a summary if the def was in module level asm).
5077 assert(F.isDeclaration());
5078 continue;
5079 }
5080 auto *Summary = VI.getSummaryList()[0].get();
5081 writePerModuleFunctionSummaryRecord(NameVals, Summary, VE.getValueID(&F),
5082 FSCallsProfileAbbrev, CallsiteAbbrev,
5083 AllocAbbrev, ContextIdAbbvId, F,
5084 CallStackPos, CallStackCount);
5085 }
5086
5087 // Capture references from GlobalVariable initializers, which are outside
5088 // of a function scope.
5089 for (const GlobalVariable &G : M.globals())
5090 writeModuleLevelReferences(G, NameVals, FSModRefsAbbrev,
5091 FSModVTableRefsAbbrev);
5092
5093 for (const GlobalAlias &A : M.aliases()) {
5094 auto *Aliasee = A.getAliaseeObject();
5095 // Skip ifunc and nameless functions which don't have an entry in the
5096 // summary.
5097 if (!Aliasee->hasName() || isa<GlobalIFunc>(Aliasee))
5098 continue;
5099 auto AliasId = VE.getValueID(&A);
5100 auto AliaseeId = VE.getValueID(Aliasee);
5101 NameVals.push_back(AliasId);
5102 auto *Summary = Index->getGlobalValueSummary(A);
5103 AliasSummary *AS = cast<AliasSummary>(Summary);
5104 NameVals.push_back(getEncodedGVSummaryFlags(AS->flags()));
5105 NameVals.push_back(AliaseeId);
5106 Stream.EmitRecord(bitc::FS_ALIAS, NameVals, FSAliasAbbrev);
5107 NameVals.clear();
5108 }
5109
5110 for (auto &S : Index->typeIdCompatibleVtableMap()) {
5111 writeTypeIdCompatibleVtableSummaryRecord(NameVals, StrtabBuilder, S.first,
5112 S.second, VE);
5113 Stream.EmitRecord(bitc::FS_TYPE_ID_METADATA, NameVals,
5114 TypeIdCompatibleVtableAbbrev);
5115 NameVals.clear();
5116 }
5117
5118 if (Index->getBlockCount())
5120 ArrayRef<uint64_t>{Index->getBlockCount()});
5121
5122 Stream.ExitBlock();
5123}
5124
5125void ModuleBitcodeWriterBase::writeGUIDList() {
5127 for (const GlobalValue &GV : M.global_values()) {
5128 auto MaybeGUID = GV.getGUIDIfAssigned();
5129 if (!MaybeGUID || *MaybeGUID == 0)
5130 continue;
5131 const auto ValueID = VE.getValueID(&GV);
5132 if (ValueID >= GUIDs.size())
5133 GUIDs.resize(ValueID + 1);
5134 GUIDs[ValueID] = *MaybeGUID;
5135 }
5136
5137 auto Abbv = std::make_shared<BitCodeAbbrev>();
5138 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GUIDLIST));
5139 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
5140 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
5141 unsigned GUIDListAbbrev = Stream.EmitAbbrev(std::move(Abbv));
5142
5143 SmallVector<uint32_t> RecordVals;
5144 RecordVals.reserve(GUIDs.size() * 2);
5145 for (auto GUID : GUIDs) {
5146 RecordVals.push_back(static_cast<uint32_t>(GUID >> 32));
5147 RecordVals.push_back(static_cast<uint32_t>(GUID));
5148 }
5149
5150 Stream.EmitRecord(bitc::MODULE_CODE_GUIDLIST, RecordVals, GUIDListAbbrev);
5151}
5152
5153/// Emit the combined summary section into the combined index file.
5154void IndexBitcodeWriter::writeCombinedGlobalValueSummary() {
5156 Stream.EmitRecord(
5158 ArrayRef<uint64_t>{ModuleSummaryIndex::BitcodeSummaryVersion});
5159
5160 // Write the index flags.
5161 Stream.EmitRecord(bitc::FS_FLAGS, ArrayRef<uint64_t>{Index.getFlags()});
5162
5163 auto Abbv = std::make_shared<BitCodeAbbrev>();
5164 Abbv->Add(BitCodeAbbrevOp(bitc::FS_VALUE_GUID));
5165 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
5166 // GUIDS often use up most of 64-bits, so encode as two Fixed 32.
5167 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
5168 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
5169 unsigned ValueGuidAbbrev = Stream.EmitAbbrev(std::move(Abbv));
5170
5171 for (const auto &GVI : valueIds()) {
5173 ArrayRef<uint32_t>{GVI.second,
5174 static_cast<uint32_t>(GVI.first >> 32),
5175 static_cast<uint32_t>(GVI.first)},
5176 ValueGuidAbbrev);
5177 }
5178
5179 // Write the stack ids used by this index, which will be a subset of those in
5180 // the full index in the case of distributed indexes.
5181 if (!StackIds.empty()) {
5182 auto StackIdAbbv = std::make_shared<BitCodeAbbrev>();
5183 StackIdAbbv->Add(BitCodeAbbrevOp(bitc::FS_STACK_IDS));
5184 // numids x stackid
5185 StackIdAbbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
5186 // The stack ids are hashes that are close to 64 bits in size, so emitting
5187 // as a pair of 32-bit fixed-width values is more efficient than a VBR.
5188 StackIdAbbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
5189 unsigned StackIdAbbvId = Stream.EmitAbbrev(std::move(StackIdAbbv));
5190 SmallVector<uint32_t> Vals;
5191 Vals.reserve(StackIds.size() * 2);
5192 for (auto Id : StackIds) {
5193 Vals.push_back(static_cast<uint32_t>(Id >> 32));
5194 Vals.push_back(static_cast<uint32_t>(Id));
5195 }
5196 Stream.EmitRecord(bitc::FS_STACK_IDS, Vals, StackIdAbbvId);
5197 }
5198
5199 // Abbrev for FS_COMBINED_PROFILE.
5200 Abbv = std::make_shared<BitCodeAbbrev>();
5201 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED_PROFILE));
5202 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
5203 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
5204 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
5205 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
5206 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // fflags
5207 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // entrycount
5208 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs
5209 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // rorefcnt
5210 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // worefcnt
5211 // numrefs x valueid, n x (valueid, hotness+tailcall+indirectcall flags)
5212 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
5213 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
5214 unsigned FSCallsProfileAbbrev = Stream.EmitAbbrev(std::move(Abbv));
5215
5216 // Abbrev for FS_COMBINED_GLOBALVAR_INIT_REFS.
5217 Abbv = std::make_shared<BitCodeAbbrev>();
5218 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED_GLOBALVAR_INIT_REFS));
5219 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
5220 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
5221 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
5222 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); // valueids
5223 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
5224 unsigned FSModRefsAbbrev = Stream.EmitAbbrev(std::move(Abbv));
5225
5226 // Abbrev for FS_COMBINED_ALIAS.
5227 Abbv = std::make_shared<BitCodeAbbrev>();
5228 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED_ALIAS));
5229 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
5230 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
5231 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags
5232 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
5233 unsigned FSAliasAbbrev = Stream.EmitAbbrev(std::move(Abbv));
5234
5235 Abbv = std::make_shared<BitCodeAbbrev>();
5236 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED_CALLSITE_INFO));
5237 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
5238 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numstackindices
5239 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numver
5240 // numstackindices x stackidindex, numver x version
5241 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
5242 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
5243 unsigned CallsiteAbbrev = Stream.EmitAbbrev(std::move(Abbv));
5244
5245 Abbv = std::make_shared<BitCodeAbbrev>();
5246 Abbv->Add(BitCodeAbbrevOp(CombinedIndexMemProfContext
5249 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // nummib
5250 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numver
5251 // nummib x (alloc type, context radix tree index),
5252 // numver x version
5253 // optional: nummib x total size
5254 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
5255 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
5256 unsigned AllocAbbrev = Stream.EmitAbbrev(std::move(Abbv));
5257
5258 auto shouldImportValueAsDecl = [&](GlobalValueSummary *GVS) -> bool {
5259 if (DecSummaries == nullptr)
5260 return false;
5261 return DecSummaries->count(GVS);
5262 };
5263
5264 // The aliases are emitted as a post-pass, and will point to the value
5265 // id of the aliasee. Save them in a vector for post-processing.
5267
5268 // Save the value id for each summary for alias emission.
5269 DenseMap<const GlobalValueSummary *, unsigned> SummaryToValueIdMap;
5270
5271 SmallVector<uint64_t, 64> NameVals;
5272
5273 // Set that will be populated during call to writeFunctionTypeMetadataRecords
5274 // with the type ids referenced by this index file.
5275 std::set<GlobalValue::GUID> ReferencedTypeIds;
5276
5277 // For local linkage, we also emit the original name separately
5278 // immediately after the record.
5279 auto MaybeEmitOriginalName = [&](GlobalValueSummary &S) {
5280 // We don't need to emit the original name if we are writing the index for
5281 // distributed backends (in which case ModuleToSummariesForIndex is
5282 // non-null). The original name is only needed during the thin link, since
5283 // for SamplePGO the indirect call targets for local functions have
5284 // have the original name annotated in profile.
5285 // Continue to emit it when writing out the entire combined index, which is
5286 // used in testing the thin link via llvm-lto.
5287 if (ModuleToSummariesForIndex || !GlobalValue::isLocalLinkage(S.linkage()))
5288 return;
5289 NameVals.push_back(S.getOriginalName());
5291 NameVals.clear();
5292 };
5293
5294 DenseMap<CallStackId, LinearCallStackId> CallStackPos;
5296 Abbv = std::make_shared<BitCodeAbbrev>();
5297 Abbv->Add(BitCodeAbbrevOp(bitc::FS_CONTEXT_RADIX_TREE_ARRAY));
5298 // n x entry
5299 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
5300 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
5301 unsigned RadixAbbrev = Stream.EmitAbbrev(std::move(Abbv));
5302
5303 // First walk through all the functions and collect the allocation contexts
5304 // in their associated summaries, for use in constructing a radix tree of
5305 // contexts. Note that we need to do this in the same order as the functions
5306 // are processed further below since the call stack positions in the
5307 // resulting radix tree array are identified based on this order.
5308 MapVector<CallStackId, llvm::SmallVector<LinearFrameId>> CallStacks;
5309 forEachSummary([&](GVInfo I, bool IsAliasee) {
5310 // Don't collect this when invoked for an aliasee, as it is not needed for
5311 // the alias summary. If the aliasee is to be imported, we will invoke
5312 // this separately with IsAliasee=false.
5313 if (IsAliasee)
5314 return;
5315 GlobalValueSummary *S = I.second;
5316 assert(S);
5317 auto *FS = dyn_cast<FunctionSummary>(S);
5318 if (!FS)
5319 return;
5321 FS,
5322 /*GetStackIndex*/
5323 [&](unsigned I) {
5324 // Get the corresponding index into the list of StackIds actually
5325 // being written for this combined index (which may be a subset in
5326 // the case of distributed indexes).
5327 assert(StackIdIndicesToIndex.contains(I));
5328 return StackIdIndicesToIndex[I];
5329 },
5330 CallStacks);
5331 });
5332 // Finalize the radix tree, write it out, and get the map of positions in
5333 // the linearized tree array.
5334 if (!CallStacks.empty()) {
5335 CallStackPos = writeMemoryProfileRadixTree(std::move(CallStacks), Stream,
5336 RadixAbbrev);
5337 }
5338 }
5339
5340 // Keep track of the current index into the CallStackPos map. Not used if
5341 // CombinedIndexMemProfContext is false.
5342 CallStackId CallStackCount = 0;
5343
5344 DenseSet<GlobalValue::GUID> DefOrUseGUIDs;
5345 forEachSummary([&](GVInfo I, bool IsAliasee) {
5346 GlobalValueSummary *S = I.second;
5347 assert(S);
5348 DefOrUseGUIDs.insert(I.first);
5349 for (const ValueInfo &VI : S->refs())
5350 DefOrUseGUIDs.insert(VI.getGUID());
5351
5352 auto ValueId = getValueId(I.first);
5353 assert(ValueId);
5354 SummaryToValueIdMap[S] = *ValueId;
5355
5356 // If this is invoked for an aliasee, we want to record the above
5357 // mapping, but then not emit a summary entry (if the aliasee is
5358 // to be imported, we will invoke this separately with IsAliasee=false).
5359 if (IsAliasee)
5360 return;
5361
5362 if (auto *AS = dyn_cast<AliasSummary>(S)) {
5363 // Will process aliases as a post-pass because the reader wants all
5364 // global to be loaded first.
5365 Aliases.push_back(AS);
5366 return;
5367 }
5368
5369 if (auto *VS = dyn_cast<GlobalVarSummary>(S)) {
5370 NameVals.push_back(*ValueId);
5371 assert(ModuleIdMap.count(VS->modulePath()));
5372 NameVals.push_back(ModuleIdMap[VS->modulePath()]);
5373 NameVals.push_back(
5374 getEncodedGVSummaryFlags(VS->flags(), shouldImportValueAsDecl(VS)));
5375 NameVals.push_back(getEncodedGVarFlags(VS->varflags()));
5376 for (auto &RI : VS->refs()) {
5377 auto RefValueId = getValueId(RI.getGUID());
5378 if (!RefValueId)
5379 continue;
5380 NameVals.push_back(*RefValueId);
5381 }
5382
5383 // Emit the finished record.
5385 FSModRefsAbbrev);
5386 NameVals.clear();
5387 MaybeEmitOriginalName(*S);
5388 return;
5389 }
5390
5391 auto GetValueId = [&](const ValueInfo &VI) -> std::optional<unsigned> {
5392 if (!VI)
5393 return std::nullopt;
5394 return getValueId(VI.getGUID());
5395 };
5396
5397 auto *FS = cast<FunctionSummary>(S);
5398 writeFunctionTypeMetadataRecords(Stream, FS, GetValueId);
5399 getReferencedTypeIds(FS, ReferencedTypeIds);
5400
5401 NameVals.push_back(*ValueId);
5402 assert(ModuleIdMap.count(FS->modulePath()));
5403 NameVals.push_back(ModuleIdMap[FS->modulePath()]);
5404 NameVals.push_back(
5405 getEncodedGVSummaryFlags(FS->flags(), shouldImportValueAsDecl(FS)));
5406 NameVals.push_back(FS->instCount());
5407 NameVals.push_back(getEncodedFFlags(FS->fflags()));
5408 // TODO: Stop writing entry count and bump bitcode version.
5409 NameVals.push_back(0 /* EntryCount */);
5410
5411 // Fill in below
5412 NameVals.push_back(0); // numrefs
5413 NameVals.push_back(0); // rorefcnt
5414 NameVals.push_back(0); // worefcnt
5415
5416 unsigned Count = 0, RORefCnt = 0, WORefCnt = 0;
5417 for (auto &RI : FS->refs()) {
5418 auto RefValueId = getValueId(RI.getGUID());
5419 if (!RefValueId)
5420 continue;
5421 NameVals.push_back(*RefValueId);
5422 if (RI.isReadOnly())
5423 RORefCnt++;
5424 else if (RI.isWriteOnly())
5425 WORefCnt++;
5426 Count++;
5427 }
5428 NameVals[6] = Count;
5429 NameVals[7] = RORefCnt;
5430 NameVals[8] = WORefCnt;
5431
5432 for (auto &EI : FS->calls()) {
5433 // If this GUID doesn't have a value id, it doesn't have a function
5434 // summary and we don't need to record any calls to it.
5435 std::optional<unsigned> CallValueId = GetValueId(EI.first);
5436 if (!CallValueId)
5437 continue;
5438 NameVals.push_back(*CallValueId);
5439 NameVals.push_back(getEncodedHotnessCallEdgeInfo(EI.second));
5440 }
5441
5442 // Emit the finished record.
5443 Stream.EmitRecord(bitc::FS_COMBINED_PROFILE, NameVals,
5444 FSCallsProfileAbbrev);
5445 NameVals.clear();
5446
5448 Stream, FS, CallsiteAbbrev, AllocAbbrev, /*ContextIdAbbvId*/ 0,
5449 /*PerModule*/ false,
5450 /*GetValueId*/
5451 [&](const ValueInfo &VI) -> unsigned {
5452 std::optional<unsigned> ValueID = GetValueId(VI);
5453 // This can happen in shared index files for distributed ThinLTO if
5454 // the callee function summary is not included. Record 0 which we
5455 // will have to deal with conservatively when doing any kind of
5456 // validation in the ThinLTO backends.
5457 if (!ValueID)
5458 return 0;
5459 return *ValueID;
5460 },
5461 /*GetStackIndex*/
5462 [&](unsigned I) {
5463 // Get the corresponding index into the list of StackIds actually
5464 // being written for this combined index (which may be a subset in
5465 // the case of distributed indexes).
5466 assert(StackIdIndicesToIndex.contains(I));
5467 return StackIdIndicesToIndex[I];
5468 },
5469 /*WriteContextSizeInfoIndex*/ false, CallStackPos, CallStackCount);
5470
5471 MaybeEmitOriginalName(*S);
5472 });
5473
5474 for (auto *AS : Aliases) {
5475 auto AliasValueId = SummaryToValueIdMap[AS];
5476 assert(AliasValueId);
5477 NameVals.push_back(AliasValueId);
5478 assert(ModuleIdMap.count(AS->modulePath()));
5479 NameVals.push_back(ModuleIdMap[AS->modulePath()]);
5480 NameVals.push_back(
5481 getEncodedGVSummaryFlags(AS->flags(), shouldImportValueAsDecl(AS)));
5482 // Set value id to 0 when an alias is imported but the aliasee summary is
5483 // not contained in the index.
5484 auto AliaseeValueId =
5485 AS->hasAliasee() ? SummaryToValueIdMap[&AS->getAliasee()] : 0;
5486 NameVals.push_back(AliaseeValueId);
5487
5488 // Emit the finished record.
5489 Stream.EmitRecord(bitc::FS_COMBINED_ALIAS, NameVals, FSAliasAbbrev);
5490 NameVals.clear();
5491 MaybeEmitOriginalName(*AS);
5492
5493 if (AS->hasAliasee())
5494 if (auto *FS = dyn_cast<FunctionSummary>(&AS->getAliasee()))
5495 getReferencedTypeIds(FS, ReferencedTypeIds);
5496 }
5497
5499 auto EmitCfiFunctions = [&](const CfiFunctionIndex &CfiIndex,
5501 if (CfiIndex.empty())
5502 return;
5503 for (GlobalValue::GUID GUID : DefOrUseGUIDs) {
5504 auto Names = CfiIndex.getNamesForGUID(GUID);
5505 for (StringRef Name : Names)
5506 Functions.push_back({Name, GUID});
5507 }
5508 if (Functions.empty())
5509 return;
5510 llvm::sort(Functions);
5511 for (const auto &Record : Functions) {
5512 NameVals.push_back(Record.second);
5513 NameVals.push_back(StrtabBuilder.add(Record.first));
5514 NameVals.push_back(Record.first.size());
5515 }
5516 Stream.EmitRecord(Code, NameVals);
5517 NameVals.clear();
5518 Functions.clear();
5519 };
5520
5521 EmitCfiFunctions(Index.cfiFunctionDefs(), bitc::FS_CFI_FUNCTION_DEFS);
5522 EmitCfiFunctions(Index.cfiFunctionDecls(), bitc::FS_CFI_FUNCTION_DECLS);
5523
5524 // Walk the GUIDs that were referenced, and write the
5525 // corresponding type id records.
5526 for (auto &T : ReferencedTypeIds) {
5527 auto TidIter = Index.typeIds().equal_range(T);
5528 for (const auto &[GUID, TypeIdPair] : make_range(TidIter)) {
5529 writeTypeIdSummaryRecord(NameVals, StrtabBuilder, TypeIdPair.first,
5530 TypeIdPair.second);
5531 Stream.EmitRecord(bitc::FS_TYPE_ID, NameVals);
5532 NameVals.clear();
5533 }
5534 }
5535
5536 if (Index.getBlockCount())
5538 ArrayRef<uint64_t>{Index.getBlockCount()});
5539
5540 Stream.ExitBlock();
5541}
5542
5543/// Create the "IDENTIFICATION_BLOCK_ID" containing a single string with the
5544/// current llvm version, and a record for the epoch number.
5547
5548 // Write the "user readable" string identifying the bitcode producer
5549 auto Abbv = std::make_shared<BitCodeAbbrev>();
5553 auto StringAbbrev = Stream.EmitAbbrev(std::move(Abbv));
5555 "LLVM" LLVM_VERSION_STRING, StringAbbrev);
5556
5557 // Write the epoch version
5558 Abbv = std::make_shared<BitCodeAbbrev>();
5561 auto EpochAbbrev = Stream.EmitAbbrev(std::move(Abbv));
5562 constexpr std::array<unsigned, 1> Vals = {{bitc::BITCODE_CURRENT_EPOCH}};
5563 Stream.EmitRecord(bitc::IDENTIFICATION_CODE_EPOCH, Vals, EpochAbbrev);
5564 Stream.ExitBlock();
5565}
5566
5567void ModuleBitcodeWriter::writeModuleHash(StringRef View) {
5568 // Emit the module's hash.
5569 // MODULE_CODE_HASH: [5*i32]
5570 if (GenerateHash) {
5571 uint32_t Vals[5];
5572 Hasher.update(ArrayRef<uint8_t>(
5573 reinterpret_cast<const uint8_t *>(View.data()), View.size()));
5574 std::array<uint8_t, 20> Hash = Hasher.result();
5575 for (int Pos = 0; Pos < 20; Pos += 4) {
5576 Vals[Pos / 4] = support::endian::read32be(Hash.data() + Pos);
5577 }
5578
5579 // Emit the finished record.
5580 Stream.EmitRecord(bitc::MODULE_CODE_HASH, Vals);
5581
5582 if (ModHash)
5583 // Save the written hash value.
5584 llvm::copy(Vals, std::begin(*ModHash));
5585 }
5586}
5587
5588void ModuleBitcodeWriter::write() {
5590
5592 // We will want to write the module hash at this point. Block any flushing so
5593 // we can have access to the whole underlying data later.
5594 Stream.markAndBlockFlushing();
5595
5596 writeModuleVersion();
5597
5598 // Emit blockinfo, which defines the standard abbreviations etc.
5599 writeBlockInfo();
5600
5601 // Emit information describing all of the types in the module.
5602 writeTypeTable();
5603
5604 // Emit information about attribute groups.
5605 writeAttributeGroupTable();
5606
5607 // Emit information about parameter attributes.
5608 writeAttributeTable();
5609
5610 writeComdats();
5611
5612 // Emit top-level description of module, including target triple, inline asm,
5613 // descriptors for global variables, and function prototype info.
5614 writeModuleInfo();
5615
5616 // Emit constants.
5617 writeModuleConstants();
5618
5619 // Emit metadata kind names.
5620 writeModuleMetadataKinds();
5621
5622 // Emit metadata.
5623 writeModuleMetadata();
5624
5625 // Emit module-level use-lists.
5627 writeUseListBlock(nullptr);
5628
5629 writeOperandBundleTags();
5630 writeSyncScopeNames();
5631
5632 // Emit function bodies.
5633 DenseMap<const Function *, uint64_t> FunctionToBitcodeIndex;
5634 for (const Function &F : M)
5635 if (!F.isDeclaration())
5636 writeFunction(F, FunctionToBitcodeIndex);
5637
5638 // Need to write after the above call to WriteFunction which populates
5639 // the summary information in the index.
5640 if (Index)
5641 writePerModuleGlobalValueSummary();
5642
5643 writeGlobalValueSymbolTable(FunctionToBitcodeIndex);
5644
5645 writeModuleHash(Stream.getMarkedBufferAndResumeFlushing());
5646
5647 Stream.ExitBlock();
5648}
5649
5651 uint32_t &Position) {
5652 support::endian::write32le(&Buffer[Position], Value);
5653 Position += 4;
5654}
5655
5656/// If generating a bc file on darwin, we have to emit a
5657/// header and trailer to make it compatible with the system archiver. To do
5658/// this we emit the following header, and then emit a trailer that pads the
5659/// file out to be a multiple of 16 bytes.
5660///
5661/// struct bc_header {
5662/// uint32_t Magic; // 0x0B17C0DE
5663/// uint32_t Version; // Version, currently always 0.
5664/// uint32_t BitcodeOffset; // Offset to traditional bitcode file.
5665/// uint32_t BitcodeSize; // Size of traditional bitcode file.
5666/// uint32_t CPUType; // CPU specifier.
5667/// ... potentially more later ...
5668/// };
5670 const Triple &TT) {
5671 unsigned CPUType = ~0U;
5672
5673 // Match x86_64-*, i[3-9]86-*, powerpc-*, powerpc64-*, arm-*, thumb-*,
5674 // armv[0-9]-*, thumbv[0-9]-*, armv5te-*, or armv6t2-*. The CPUType is a magic
5675 // number from /usr/include/mach/machine.h. It is ok to reproduce the
5676 // specific constants here because they are implicitly part of the Darwin ABI.
5677 enum {
5678 DARWIN_CPU_ARCH_ABI64 = 0x01000000,
5679 DARWIN_CPU_TYPE_X86 = 7,
5680 DARWIN_CPU_TYPE_ARM = 12,
5681 DARWIN_CPU_TYPE_POWERPC = 18
5682 };
5683
5684 Triple::ArchType Arch = TT.getArch();
5685 if (Arch == Triple::x86_64)
5686 CPUType = DARWIN_CPU_TYPE_X86 | DARWIN_CPU_ARCH_ABI64;
5687 else if (Arch == Triple::x86)
5688 CPUType = DARWIN_CPU_TYPE_X86;
5689 else if (Arch == Triple::ppc)
5690 CPUType = DARWIN_CPU_TYPE_POWERPC;
5691 else if (Arch == Triple::ppc64)
5692 CPUType = DARWIN_CPU_TYPE_POWERPC | DARWIN_CPU_ARCH_ABI64;
5693 else if (Arch == Triple::arm || Arch == Triple::thumb)
5694 CPUType = DARWIN_CPU_TYPE_ARM;
5695
5696 // Traditional Bitcode starts after header.
5697 assert(Buffer.size() >= BWH_HeaderSize &&
5698 "Expected header size to be reserved");
5699 unsigned BCOffset = BWH_HeaderSize;
5700 unsigned BCSize = Buffer.size() - BWH_HeaderSize;
5701
5702 // Write the magic and version.
5703 unsigned Position = 0;
5704 writeInt32ToBuffer(0x0B17C0DE, Buffer, Position);
5705 writeInt32ToBuffer(0, Buffer, Position); // Version.
5706 writeInt32ToBuffer(BCOffset, Buffer, Position);
5707 writeInt32ToBuffer(BCSize, Buffer, Position);
5708 writeInt32ToBuffer(CPUType, Buffer, Position);
5709
5710 // If the file is not a multiple of 16 bytes, insert dummy padding.
5711 while (Buffer.size() & 15)
5712 Buffer.push_back(0);
5713}
5714
5715/// Helper to write the header common to all bitcode files.
5717 // Emit the file header.
5718 Stream.Emit((unsigned)'B', 8);
5719 Stream.Emit((unsigned)'C', 8);
5720 Stream.Emit(0x0, 4);
5721 Stream.Emit(0xC, 4);
5722 Stream.Emit(0xE, 4);
5723 Stream.Emit(0xD, 4);
5724}
5725
5727 : Stream(new BitstreamWriter(Buffer)) {
5728 writeBitcodeHeader(*Stream);
5729}
5730
5732 : Stream(new BitstreamWriter(FS, FlushThreshold)) {
5733 writeBitcodeHeader(*Stream);
5734}
5735
5737
5738void BitcodeWriter::writeBlob(unsigned Block, unsigned Record, StringRef Blob) {
5739 Stream->EnterSubblock(Block, 3);
5740
5741 auto Abbv = std::make_shared<BitCodeAbbrev>();
5742 Abbv->Add(BitCodeAbbrevOp(Record));
5744 auto AbbrevNo = Stream->EmitAbbrev(std::move(Abbv));
5745
5746 Stream->EmitRecordWithBlob(AbbrevNo, ArrayRef<uint64_t>{Record}, Blob);
5747
5748 Stream->ExitBlock();
5749}
5750
5752 assert(!WroteStrtab && !WroteSymtab);
5753
5754 // If any module has module-level inline asm, we will require a registered asm
5755 // parser for the target so that we can create an accurate symbol table for
5756 // the module.
5757 for (Module *M : Mods) {
5758 if (M->getModuleInlineAsm().empty())
5759 continue;
5760
5761 std::string Err;
5762 const Triple TT(M->getTargetTriple());
5763 const Target *T = TargetRegistry::lookupTarget(TT, Err);
5764 if (!T || !T->hasMCAsmParser())
5765 return;
5766 }
5767
5768 WroteSymtab = true;
5769 SmallVector<char, 0> Symtab;
5770 // The irsymtab::build function may be unable to create a symbol table if the
5771 // module is malformed (e.g. it contains an invalid alias). Writing a symbol
5772 // table is not required for correctness, but we still want to be able to
5773 // write malformed modules to bitcode files, so swallow the error.
5774 if (Error E = irsymtab::build(Mods, Symtab, StrtabBuilder, Alloc)) {
5775 consumeError(std::move(E));
5776 return;
5777 }
5778
5780 {Symtab.data(), Symtab.size()});
5781}
5782
5784 assert(!WroteStrtab);
5785
5786 std::vector<char> Strtab;
5787 StrtabBuilder.finalizeInOrder();
5788 Strtab.resize(StrtabBuilder.getSize());
5789 StrtabBuilder.write((uint8_t *)Strtab.data());
5790
5792 {Strtab.data(), Strtab.size()});
5793
5794 WroteStrtab = true;
5795}
5796
5798 writeBlob(bitc::STRTAB_BLOCK_ID, bitc::STRTAB_BLOB, Strtab);
5799 WroteStrtab = true;
5800}
5801
5803 bool ShouldPreserveUseListOrder,
5804 const ModuleSummaryIndex *Index,
5805 bool GenerateHash, ModuleHash *ModHash) {
5806 assert(!WroteStrtab);
5807
5808 // The Mods vector is used by irsymtab::build, which requires non-const
5809 // Modules in case it needs to materialize metadata. But the bitcode writer
5810 // requires that the module is materialized, so we can cast to non-const here,
5811 // after checking that it is in fact materialized.
5812 assert(M.isMaterialized());
5813 Mods.push_back(const_cast<Module *>(&M));
5814
5815 ModuleBitcodeWriter ModuleWriter(M, StrtabBuilder, *Stream,
5816 ShouldPreserveUseListOrder, Index,
5817 GenerateHash, ModHash);
5818 ModuleWriter.write();
5819}
5820
5822 const ModuleSummaryIndex *Index,
5823 const ModuleToSummariesForIndexTy *ModuleToSummariesForIndex,
5824 const GVSummaryPtrSet *DecSummaries) {
5825 IndexBitcodeWriter IndexWriter(*Stream, StrtabBuilder, *Index, DecSummaries,
5826 ModuleToSummariesForIndex);
5827 IndexWriter.write();
5828}
5829
5830/// Write the specified module to the specified output stream.
5832 bool ShouldPreserveUseListOrder,
5833 const ModuleSummaryIndex *Index,
5834 bool GenerateHash, ModuleHash *ModHash) {
5835 auto Write = [&](BitcodeWriter &Writer) {
5836 Writer.writeModule(M, ShouldPreserveUseListOrder, Index, GenerateHash,
5837 ModHash);
5838 Writer.writeSymtab();
5839 Writer.writeStrtab();
5840 };
5841 Triple TT(M.getTargetTriple());
5842 if (TT.isOSDarwin() || TT.isOSBinFormatMachO()) {
5843 // If this is darwin or another generic macho target, reserve space for the
5844 // header. Note that the header is computed *after* the output is known, so
5845 // we currently explicitly use a buffer, write to it, and then subsequently
5846 // flush to Out.
5847 SmallVector<char, 0> Buffer;
5848 Buffer.reserve(256 * 1024);
5849 Buffer.insert(Buffer.begin(), BWH_HeaderSize, 0);
5850 BitcodeWriter Writer(Buffer);
5851 Write(Writer);
5852 emitDarwinBCHeaderAndTrailer(Buffer, TT);
5853 Out.write(Buffer.data(), Buffer.size());
5854 } else {
5855 BitcodeWriter Writer(
5856 Out, getBitWriterOptions(M.getContext()).bitcode_flush_threshold);
5857 Write(Writer);
5858 }
5859}
5860
5861void IndexBitcodeWriter::write() {
5863
5864 writeModuleVersion();
5865
5866 // Write the module paths in the combined index.
5867 writeModStrings();
5868
5869 // Write the summary combined index records.
5870 writeCombinedGlobalValueSummary();
5871
5872 Stream.ExitBlock();
5873}
5874
5875// Write the specified module summary index to the given raw output stream,
5876// where it will be written in a new bitcode block. This is used when
5877// writing the combined index file for ThinLTO. When writing a subset of the
5878// index for a distributed backend, provide a \p ModuleToSummariesForIndex map.
5880 const ModuleSummaryIndex &Index, raw_ostream &Out,
5881 const ModuleToSummariesForIndexTy *ModuleToSummariesForIndex,
5882 const GVSummaryPtrSet *DecSummaries) {
5883 SmallVector<char, 0> Buffer;
5884 Buffer.reserve(256 * 1024);
5885
5886 BitcodeWriter Writer(Buffer);
5887 Writer.writeIndex(&Index, ModuleToSummariesForIndex, DecSummaries);
5888 Writer.writeStrtab();
5889
5890 Out.write((char *)&Buffer.front(), Buffer.size());
5891}
5892
5893namespace {
5894
5895/// Class to manage the bitcode writing for a thin link bitcode file.
5896class ThinLinkBitcodeWriter : public ModuleBitcodeWriterBase {
5897 /// ModHash is for use in ThinLTO incremental build, generated while writing
5898 /// the module bitcode file.
5899 const ModuleHash *ModHash;
5900
5901public:
5902 ThinLinkBitcodeWriter(const Module &M, StringTableBuilder &StrtabBuilder,
5903 BitstreamWriter &Stream,
5904 const ModuleSummaryIndex &Index,
5905 const ModuleHash &ModHash)
5906 : ModuleBitcodeWriterBase(M, StrtabBuilder, Stream,
5907 /*ShouldPreserveUseListOrder=*/false, &Index),
5908 ModHash(&ModHash) {}
5909
5910 void write();
5911
5912private:
5913 void writeSimplifiedModuleInfo();
5914};
5915
5916} // end anonymous namespace
5917
5918// This function writes a simpilified module info for thin link bitcode file.
5919// It only contains the source file name along with the name(the offset and
5920// size in strtab) and linkage for global values. For the global value info
5921// entry, in order to keep linkage at offset 5, there are three zeros used
5922// as padding.
5923void ThinLinkBitcodeWriter::writeSimplifiedModuleInfo() {
5925 // Emit the module's source file name.
5926 {
5927 StringEncoding Bits = getStringEncoding(M.getSourceFileName());
5929 if (Bits == SE_Char6)
5930 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Char6);
5931 else if (Bits == SE_Fixed7)
5932 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7);
5933
5934 // MODULE_CODE_SOURCE_FILENAME: [namechar x N]
5935 auto Abbv = std::make_shared<BitCodeAbbrev>();
5938 Abbv->Add(AbbrevOpToUse);
5939 unsigned FilenameAbbrev = Stream.EmitAbbrev(std::move(Abbv));
5940
5941 for (const auto P : M.getSourceFileName())
5942 Vals.push_back((unsigned char)P);
5943
5944 Stream.EmitRecord(bitc::MODULE_CODE_SOURCE_FILENAME, Vals, FilenameAbbrev);
5945 Vals.clear();
5946 }
5947
5948 writeGUIDList();
5949
5950 // Emit the global variable information.
5951 for (const GlobalVariable &GV : M.globals()) {
5952 // GLOBALVAR: [strtab offset, strtab size, 0, 0, 0, linkage]
5953 Vals.push_back(StrtabBuilder.add(GV.getName()));
5954 Vals.push_back(GV.getName().size());
5955 Vals.push_back(0);
5956 Vals.push_back(0);
5957 Vals.push_back(0);
5958 Vals.push_back(getEncodedLinkage(GV));
5959
5961 Vals.clear();
5962 }
5963
5964 // Emit the function proto information.
5965 for (const Function &F : M) {
5966 // FUNCTION: [strtab offset, strtab size, 0, 0, 0, linkage]
5967 Vals.push_back(StrtabBuilder.add(F.getName()));
5968 Vals.push_back(F.getName().size());
5969 Vals.push_back(0);
5970 Vals.push_back(0);
5971 Vals.push_back(0);
5973
5975 Vals.clear();
5976 }
5977
5978 // Emit the alias information.
5979 for (const GlobalAlias &A : M.aliases()) {
5980 // ALIAS: [strtab offset, strtab size, 0, 0, 0, linkage]
5981 Vals.push_back(StrtabBuilder.add(A.getName()));
5982 Vals.push_back(A.getName().size());
5983 Vals.push_back(0);
5984 Vals.push_back(0);
5985 Vals.push_back(0);
5987
5988 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals);
5989 Vals.clear();
5990 }
5991
5992 // Emit the ifunc information.
5993 for (const GlobalIFunc &I : M.ifuncs()) {
5994 // IFUNC: [strtab offset, strtab size, 0, 0, 0, linkage]
5995 Vals.push_back(StrtabBuilder.add(I.getName()));
5996 Vals.push_back(I.getName().size());
5997 Vals.push_back(0);
5998 Vals.push_back(0);
5999 Vals.push_back(0);
6001
6002 Stream.EmitRecord(bitc::MODULE_CODE_IFUNC, Vals);
6003 Vals.clear();
6004 }
6005}
6006
6007void ThinLinkBitcodeWriter::write() {
6009
6010 writeModuleVersion();
6011
6012 writeSimplifiedModuleInfo();
6013
6014 writePerModuleGlobalValueSummary();
6015
6016 // Write module hash.
6018
6019 Stream.ExitBlock();
6020}
6021
6023 const ModuleSummaryIndex &Index,
6024 const ModuleHash &ModHash) {
6025 assert(!WroteStrtab);
6026
6027 // The Mods vector is used by irsymtab::build, which requires non-const
6028 // Modules in case it needs to materialize metadata. But the bitcode writer
6029 // requires that the module is materialized, so we can cast to non-const here,
6030 // after checking that it is in fact materialized.
6031 assert(M.isMaterialized());
6032 Mods.push_back(const_cast<Module *>(&M));
6033
6034 ThinLinkBitcodeWriter ThinLinkWriter(M, StrtabBuilder, *Stream, Index,
6035 ModHash);
6036 ThinLinkWriter.write();
6037}
6038
6039// Write the specified thin link bitcode file to the given raw output stream,
6040// where it will be written in a new bitcode block. This is used when
6041// writing the per-module index file for ThinLTO.
6043 const ModuleSummaryIndex &Index,
6044 const ModuleHash &ModHash) {
6045 SmallVector<char, 0> Buffer;
6046 Buffer.reserve(256 * 1024);
6047
6048 BitcodeWriter Writer(Buffer);
6049 Writer.writeThinLinkBitcode(M, Index, ModHash);
6050 Writer.writeSymtab();
6051 Writer.writeStrtab();
6052
6053 Out.write((char *)&Buffer.front(), Buffer.size());
6054}
6055
6056static const char *getSectionNameForBitcode(const Triple &T) {
6057 switch (T.getObjectFormat()) {
6058 case Triple::MachO:
6059 return "__LLVM,__bitcode";
6060 case Triple::COFF:
6061 case Triple::ELF:
6062 case Triple::Wasm:
6064 return ".llvmbc";
6065 case Triple::GOFF:
6066 llvm_unreachable("GOFF is not yet implemented");
6067 break;
6068 case Triple::SPIRV:
6069 if (T.getVendor() == Triple::AMD)
6070 return ".llvmbc";
6071 llvm_unreachable("SPIRV is not yet implemented");
6072 break;
6073 case Triple::XCOFF:
6074 llvm_unreachable("XCOFF is not yet implemented");
6075 break;
6077 llvm_unreachable("DXContainer is not yet implemented");
6078 break;
6079 }
6080 llvm_unreachable("Unimplemented ObjectFormatType");
6081}
6082
6083static const char *getSectionNameForCommandline(const Triple &T) {
6084 switch (T.getObjectFormat()) {
6085 case Triple::MachO:
6086 return "__LLVM,__cmdline";
6087 case Triple::COFF:
6088 case Triple::ELF:
6089 case Triple::Wasm:
6091 return ".llvmcmd";
6092 case Triple::GOFF:
6093 llvm_unreachable("GOFF is not yet implemented");
6094 break;
6095 case Triple::SPIRV:
6096 if (T.getVendor() == Triple::AMD)
6097 return ".llvmcmd";
6098 llvm_unreachable("SPIRV is not yet implemented");
6099 break;
6100 case Triple::XCOFF:
6101 llvm_unreachable("XCOFF is not yet implemented");
6102 break;
6104 llvm_unreachable("DXC is not yet implemented");
6105 break;
6106 }
6107 llvm_unreachable("Unimplemented ObjectFormatType");
6108}
6109
6111 bool EmbedBitcode, bool EmbedCmdline,
6112 const std::vector<uint8_t> &CmdArgs) {
6113 // Save llvm.compiler.used and remove it.
6116 GlobalVariable *Used = collectUsedGlobalVariables(M, UsedGlobals, true);
6117 Type *UsedElementType = Used ? Used->getValueType()->getArrayElementType()
6118 : PointerType::getUnqual(M.getContext());
6119 for (auto *GV : UsedGlobals) {
6120 if (GV->getName() != "llvm.embedded.module" &&
6121 GV->getName() != "llvm.cmdline")
6122 UsedArray.push_back(
6124 }
6125 if (Used)
6126 Used->eraseFromParent();
6127
6128 // Embed the bitcode for the llvm module.
6129 std::string Data;
6130 ArrayRef<uint8_t> ModuleData;
6131 Triple T(M.getTargetTriple());
6132
6133 if (EmbedBitcode) {
6134 if (Buf.getBufferSize() == 0 ||
6135 !isBitcode((const unsigned char *)Buf.getBufferStart(),
6136 (const unsigned char *)Buf.getBufferEnd())) {
6137 // If the input is LLVM Assembly, bitcode is produced by serializing
6138 // the module. Use-lists order need to be preserved in this case.
6140 llvm::WriteBitcodeToFile(M, OS, /* ShouldPreserveUseListOrder */ true);
6141 ModuleData =
6142 ArrayRef<uint8_t>((const uint8_t *)OS.str().data(), OS.str().size());
6143 } else
6144 // If the input is LLVM bitcode, write the input byte stream directly.
6145 ModuleData = ArrayRef<uint8_t>((const uint8_t *)Buf.getBufferStart(),
6146 Buf.getBufferSize());
6147 }
6148 llvm::Constant *ModuleConstant =
6149 llvm::ConstantDataArray::get(M.getContext(), ModuleData);
6151 M, ModuleConstant->getType(), true, llvm::GlobalValue::PrivateLinkage,
6152 ModuleConstant);
6154 // Set alignment to 1 to prevent padding between two contributions from input
6155 // sections after linking.
6156 GV->setAlignment(Align(1));
6157 UsedArray.push_back(
6159 if (llvm::GlobalVariable *Old =
6160 M.getGlobalVariable("llvm.embedded.module", true)) {
6161 assert(Old->hasZeroLiveUses() &&
6162 "llvm.embedded.module can only be used once in llvm.compiler.used");
6163 GV->takeName(Old);
6164 Old->eraseFromParent();
6165 } else {
6166 GV->setName("llvm.embedded.module");
6167 }
6168
6169 // Skip if only bitcode needs to be embedded.
6170 if (EmbedCmdline) {
6171 // Embed command-line options.
6172 ArrayRef<uint8_t> CmdData(const_cast<uint8_t *>(CmdArgs.data()),
6173 CmdArgs.size());
6174 llvm::Constant *CmdConstant =
6175 llvm::ConstantDataArray::get(M.getContext(), CmdData);
6176 GV = new llvm::GlobalVariable(M, CmdConstant->getType(), true,
6178 CmdConstant);
6180 GV->setAlignment(Align(1));
6181 UsedArray.push_back(
6183 if (llvm::GlobalVariable *Old = M.getGlobalVariable("llvm.cmdline", true)) {
6184 assert(Old->hasZeroLiveUses() &&
6185 "llvm.cmdline can only be used once in llvm.compiler.used");
6186 GV->takeName(Old);
6187 Old->eraseFromParent();
6188 } else {
6189 GV->setName("llvm.cmdline");
6190 }
6191 }
6192
6193 if (UsedArray.empty())
6194 return;
6195
6196 // Recreate llvm.compiler.used.
6197 ArrayType *ATy = ArrayType::get(UsedElementType, UsedArray.size());
6198 auto *NewUsed = new GlobalVariable(
6200 llvm::ConstantArray::get(ATy, UsedArray), "llvm.compiler.used");
6201 NewUsed->setSection("llvm.metadata");
6202}
for(const MachineOperand &MO :llvm::drop_begin(OldMI.operands(), Desc.getNumOperands()))
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the StringMap class.
uint64_t uint64_t
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)
static void writeDIGlobalVariableExpression(raw_ostream &Out, const DIGlobalVariableExpression *N, AsmWriterContext &WriterCtx)
static void writeDICompositeType(raw_ostream &Out, const DICompositeType *N, AsmWriterContext &WriterCtx)
static void writeDIFixedPointType(raw_ostream &Out, const DIFixedPointType *N, AsmWriterContext &WriterCtx)
static void writeDISubrangeType(raw_ostream &Out, const DISubrangeType *N, AsmWriterContext &WriterCtx)
static void writeDIStringType(raw_ostream &Out, const DIStringType *N, AsmWriterContext &WriterCtx)
static void writeDILayerLocList(raw_ostream &Out, const DILayerLocList *N, AsmWriterContext &WriterCtx)
static void writeDIGlobalVariable(raw_ostream &Out, const DIGlobalVariable *N, AsmWriterContext &WriterCtx)
static void writeDIBasicType(raw_ostream &Out, const DIBasicType *N, AsmWriterContext &WriterCtx)
static void writeDILayerLoc(raw_ostream &Out, const DILayerLoc *N, AsmWriterContext &WriterCtx)
static void writeDIModule(raw_ostream &Out, const DIModule *N, AsmWriterContext &WriterCtx)
static void writeDIFile(raw_ostream &Out, const DIFile *N, AsmWriterContext &)
static void writeDISubroutineType(raw_ostream &Out, const DISubroutineType *N, AsmWriterContext &WriterCtx)
static void writeDILabel(raw_ostream &Out, const DILabel *N, AsmWriterContext &WriterCtx)
static void writeDIDerivedType(raw_ostream &Out, const DIDerivedType *N, AsmWriterContext &WriterCtx)
static void writeDIImportedEntity(raw_ostream &Out, const DIImportedEntity *N, AsmWriterContext &WriterCtx)
static void writeDIObjCProperty(raw_ostream &Out, const DIObjCProperty *N, AsmWriterContext &WriterCtx)
static void writeDISubprogram(raw_ostream &Out, const DISubprogram *N, AsmWriterContext &WriterCtx)
static void writeDILocation(raw_ostream &Out, const DILocation *DL, AsmWriterContext &WriterCtx)
static void writeDINamespace(raw_ostream &Out, const DINamespace *N, AsmWriterContext &WriterCtx)
static void writeDICommonBlock(raw_ostream &Out, const DICommonBlock *N, AsmWriterContext &WriterCtx)
static void writeGenericDINode(raw_ostream &Out, const GenericDINode *N, AsmWriterContext &WriterCtx)
static void writeDILocalVariable(raw_ostream &Out, const DILocalVariable *N, AsmWriterContext &WriterCtx)
static void writeDITemplateTypeParameter(raw_ostream &Out, const DITemplateTypeParameter *N, AsmWriterContext &WriterCtx)
static void writeDICompileUnit(raw_ostream &Out, const DICompileUnit *N, AsmWriterContext &WriterCtx)
static void writeDIGenericSubrange(raw_ostream &Out, const DIGenericSubrange *N, AsmWriterContext &WriterCtx)
static void writeDISubrange(raw_ostream &Out, const DISubrange *N, AsmWriterContext &WriterCtx)
static void writeDIProperty(raw_ostream &Out, const DIProperty *N, AsmWriterContext &WriterCtx)
static void writeDILexicalBlockFile(raw_ostream &Out, const DILexicalBlockFile *N, AsmWriterContext &WriterCtx)
static void writeDIEnumerator(raw_ostream &Out, const DIEnumerator *N, AsmWriterContext &)
static void writeMDTuple(raw_ostream &Out, const MDTuple *Node, AsmWriterContext &WriterCtx)
static void writeDIExpression(raw_ostream &Out, const DIExpression *N, AsmWriterContext &WriterCtx)
static void writeDIAssignID(raw_ostream &Out, const DIAssignID *DL, AsmWriterContext &WriterCtx)
static void writeDILexicalBlock(raw_ostream &Out, const DILexicalBlock *N, AsmWriterContext &WriterCtx)
static void writeDIArgList(raw_ostream &Out, const DIArgList *N, AsmWriterContext &WriterCtx, bool FromValue=false)
static void writeDITemplateValueParameter(raw_ostream &Out, const DITemplateValueParameter *N, AsmWriterContext &WriterCtx)
static void writeDIMacroFile(raw_ostream &Out, const DIMacroFile *N, AsmWriterContext &WriterCtx)
Atomic ordering constants.
This file contains the simple types necessary to represent the attributes associated with functions a...
static void writeFunctionHeapProfileRecords(BitstreamWriter &Stream, FunctionSummary *FS, unsigned CallsiteAbbrev, unsigned AllocAbbrev, unsigned ContextIdAbbvId, bool PerModule, std::function< unsigned(const ValueInfo &VI)> GetValueID, std::function< unsigned(unsigned)> GetStackIndex, bool WriteContextSizeInfoIndex, DenseMap< CallStackId, LinearCallStackId > &CallStackPos, CallStackId &CallStackCount)
static unsigned serializeSanitizerMetadata(const GlobalValue::SanitizerMetadata &Meta)
static void writeTypeIdCompatibleVtableSummaryRecord(SmallVector< uint64_t, 64 > &NameVals, StringTableBuilder &StrtabBuilder, StringRef Id, const TypeIdCompatibleVtableInfo &Summary, ValueEnumerator &VE)
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 void collectMemProfCallStacks(FunctionSummary *FS, std::function< LinearFrameId(unsigned)> GetStackIndex, MapVector< CallStackId, llvm::SmallVector< LinearFrameId > > &CallStacks)
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 getEncodedThreadLocalMode(const GlobalValue &GV)
static DenseMap< CallStackId, LinearCallStackId > writeMemoryProfileRadixTree(MapVector< CallStackId, llvm::SmallVector< LinearFrameId > > &&CallStacks, BitstreamWriter &Stream, unsigned RadixAbbrev)
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 unsigned getEncodedOrdering(AtomicOrdering Ordering)
static unsigned getEncodedUnnamedAddr(const GlobalValue &GV)
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.
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 void writeTypeIdSummaryRecord(SmallVector< uint64_t, 64 > &NameVals, StringTableBuilder &StrtabBuilder, StringRef Id, const TypeIdSummary &Summary)
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 unsigned getEncodedRMWOperation(const AtomicRMWInst &I)
static void writeWholeProgramDevirtResolution(SmallVector< uint64_t, 64 > &NameVals, StringTableBuilder &StrtabBuilder, uint64_t Id, const WholeProgramDevirtResolution &Wpd)
static unsigned getEncodedDLLStorageClass(const GlobalValue &GV)
static void writeInt32ToBuffer(uint32_t Value, SmallVectorImpl< char > &Buffer, uint32_t &Position)
MetadataAbbrev
@ LastPlusOne
static const char * getSectionNameForBitcode(const Triple &T)
static cl::opt< bool > CombinedIndexMemProfContext("combined-index-memprof-context", cl::Hidden, cl::init(true), cl::desc(""))
static unsigned getEncodedBinaryOpcode(unsigned Opcode)
static uint64_t getEncodedFFlags(FunctionSummary::FFlags Flags)
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
DXIL Finalize Linkage
dxil translate DXIL Translate Metadata
This file defines the DenseMap class.
This file contains constants used for implementing Dwarf debug support.
This file contains the declaration of the GlobalIFunc class, which represents a single indirect funct...
Hexagon Common GEP
#define _
static MaybeAlign getAlign(Value *Ptr)
Module.h This file contains the declarations for the Module class.
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:54
#define I(x, y, z)
Definition MD5.cpp:57
#define G(x, y, z)
Definition MD5.cpp:55
#define H(x, y, z)
Definition MD5.cpp:56
Machine Check Debug Module
This file contains the declarations for metadata subclasses.
#define T
ModuleSummaryIndex.h This file contains the declarations the classes that hold the module index and s...
nvptx lower args
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
#define P(N)
if(PassOpts->AAPipeline)
Func getContext().diagnose(DiagnosticInfoUnsupported(Func
This file contains some templates that are useful if you are working with the STL at all.
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:83
Class for arbitrary precision integers.
Definition APInt.h:78
unsigned getActiveWords() const
Compute the number of active words in the value of this APInt.
Definition APInt.h:1538
const uint64_t * getRawData() const
This function returns a pointer to the internal storage of the APInt.
Definition APInt.h:571
int64_t getSExtValue() const
Get sign extended value.
Definition APInt.h:1582
const GlobalValueSummary & getAliasee() const
bool isSwiftError() const
Return true if this alloca is used as a swifterror argument to a call.
Align getAlign() const
Return the alignment of the memory that is being allocated by the instruction.
Type * getAllocatedType() const
Return the type that is being allocated by the instruction.
bool isUsedWithInAlloca() const
Return true if this alloca is used as an inalloca argument to a call.
unsigned getAddressSpace() const
Return the address space for the allocation.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
Get the array size.
Definition ArrayRef.h:141
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
Class to represent array types.
static LLVM_ABI ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
an instruction that atomically reads a memory location, combines it with another value,...
@ Add
*p = old + v
@ FAdd
*p = old + v
@ USubCond
Subtract only if no unsigned overflow.
@ FMinimum
*p = minimum(old, v) minimum matches the behavior of llvm.minimum.
@ Min
*p = old <signed v ? old : v
@ Sub
*p = old - v
@ And
*p = old & v
@ Xor
*p = old ^ v
@ USubSat
*p = usub.sat(old, v) usub.sat matches the behavior of llvm.usub.sat.
@ FMaximum
*p = maximum(old, v) maximum matches the behavior of llvm.maximum.
@ FSub
*p = old - v
@ UIncWrap
Increment one up to a maximum value.
@ Max
*p = old >signed v ? old : v
@ UMin
*p = old <unsigned v ? old : v
@ FMin
*p = minnum(old, v) minnum matches the behavior of llvm.minnum.
@ UMax
*p = old >unsigned v ? old : v
@ FMaximumNum
*p = maximumnum(old, v) maximumnum matches the behavior of llvm.maximumnum.
@ FMax
*p = maxnum(old, v) maxnum matches the behavior of llvm.maxnum.
@ UDecWrap
Decrement one until a minimum value or zero.
@ FMinimumNum
*p = minimumnum(old, v) minimumnum matches the behavior of llvm.minimumnum.
@ Nand
*p = ~(old & v)
bool hasAttributes() const
Return true if attributes exists in this set.
Definition Attributes.h:484
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results,...
Definition Attributes.h:125
@ TombstoneKey
Use as Tombstone key for DenseMap of AttrKind.
Definition Attributes.h:132
@ None
No attributes have been set.
Definition Attributes.h:127
@ EmptyKey
Use as Empty key for DenseMap of AttrKind.
Definition Attributes.h:131
@ EndAttrKinds
Sentinel value useful for loops.
Definition Attributes.h:130
BitCodeAbbrevOp - This describes one or more operands in an abbreviation.
Definition BitCodes.h:34
static bool isChar6(char C)
isChar6 - Return true if this character is legal in the Char6 encoding.
Definition BitCodes.h:88
LLVM_ABI 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...
LLVM_ABI void writeIndex(const ModuleSummaryIndex *Index, const ModuleToSummariesForIndexTy *ModuleToSummariesForIndex, const GVSummaryPtrSet *DecSummaries)
LLVM_ABI void copyStrtab(StringRef Strtab)
Copy the string table for another module into this bitcode file.
LLVM_ABI void writeStrtab()
Write the bitcode file's string table.
LLVM_ABI void writeSymtab()
Attempt to write a symbol table to the bitcode file.
LLVM_ABI 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.
LLVM_ABI 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.
static LLVM_ABI BlockAddress * lookup(const BasicBlock *BB)
Lookup an existing BlockAddress constant for the given BasicBlock.
OperandBundleUse getOperandBundleAt(unsigned Index) const
Return the operand bundle at a specific index.
unsigned getNumOperandBundles() const
Return the number of operand bundles associated with this User.
CallingConv::ID getCallingConv() const
Value * getCalledOperand() const
Value * getArgOperand(unsigned i) const
FunctionType * getFunctionType() const
unsigned arg_size() const
AttributeList getAttributes() const
Return the attributes for this call.
bool hasOperandBundles() const
Return true if this User has any operand bundles.
BasicBlock * getIndirectDest(unsigned i) const
BasicBlock * getDefaultDest() const
unsigned getNumIndirectDests() const
Return the number of callbr indirect dest labels.
bool isNoTailCall() const
bool isTailCall() const
bool isMustTailCall() const
auto getNamesForGUID(GlobalValue::GUID GUID) const
get the name(s) associated with a given ThinLTO GUID.
@ Largest
The linker will choose the largest COMDAT.
Definition Comdat.h:39
@ SameSize
The data referenced by the COMDAT must be the same size.
Definition Comdat.h:41
@ Any
The linker may choose any COMDAT.
Definition Comdat.h:37
@ NoDeduplicate
No deduplication is performed.
Definition Comdat.h:40
@ ExactMatch
The data referenced by the COMDAT must be the same.
Definition Comdat.h:38
static LLVM_ABI Constant * get(ArrayType *T, ArrayRef< Constant * > V)
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:878
static LLVM_ABI Constant * getPointerBitCastOrAddrSpaceCast(Constant *C, Type *Ty)
Create a BitCast or AddrSpaceCast for a pointer type depending on the address space.
This class represents a range of values.
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.
This is an important base class in LLVM.
Definition Constant.h:44
DebugLoc getDebugLoc() const
LLVM_ABI DIAssignID * getAssignID() const
DIExpression * getExpression() const
DILocalVariable * getVariable() const
Metadata * getRawLocation() const
Returns the metadata operand for the first location description.
DIExpression * getAddressExpression() const
bool contains(const_arg_type_t< KeyT > Val) const
Return true if the specified key is in the map, false otherwise.
Definition DenseMap.h:757
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:762
unsigned size() const
Definition DenseMap.h:717
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:827
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
idx_iterator idx_end() const
idx_iterator idx_begin() const
Function summary information to aid decisions and implementation of importing.
LLVM_ABI void getAllMetadata(SmallVectorImpl< std::pair< unsigned, MDNode * > > &MDs) const
Appends all metadata attached to this value to MDs, sorting by KindID.
LLVM_ABI void setSection(StringRef S)
Change the section for this global.
Definition Globals.cpp:348
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
static bool isLocalLinkage(LinkageTypes Linkage)
LinkageTypes getLinkage() const
uint64_t GUID
Declare a type to represent a global unique identifier for a global value.
ThreadLocalMode getThreadLocalMode() const
@ DLLExportStorageClass
Function to be accessible from DLL.
Definition GlobalValue.h:77
@ DLLImportStorageClass
Function to be imported from DLL.
Definition GlobalValue.h:76
@ DefaultVisibility
The GV is visible.
Definition GlobalValue.h:68
@ HiddenVisibility
The GV is hidden.
Definition GlobalValue.h:69
@ ProtectedVisibility
The GV is protected.
Definition GlobalValue.h:70
UnnamedAddr getUnnamedAddr() const
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition GlobalValue.h:52
@ PrivateLinkage
Like Internal, but omit from symbol table.
Definition GlobalValue.h:61
@ CommonLinkage
Tentative definitions.
Definition GlobalValue.h:63
@ InternalLinkage
Rename collisions when linking (static functions).
Definition GlobalValue.h:60
@ LinkOnceAnyLinkage
Keep one copy of function when linking (inline)
Definition GlobalValue.h:55
@ WeakODRLinkage
Same, but only replaced by something equivalent.
Definition GlobalValue.h:58
@ ExternalLinkage
Externally visible function.
Definition GlobalValue.h:53
@ WeakAnyLinkage
Keep one copy of named function when linking (weak)
Definition GlobalValue.h:57
@ AppendingLinkage
Special purpose, only applies to global arrays.
Definition GlobalValue.h:59
@ AvailableExternallyLinkage
Available for inspection, not emission.
Definition GlobalValue.h:54
@ ExternalWeakLinkage
ExternalWeak linkage description.
Definition GlobalValue.h:62
@ LinkOnceODRLinkage
Same, but only replaced by something equivalent.
Definition GlobalValue.h:56
DLLStorageClassTypes getDLLStorageClass() const
void setAlignment(Align Align)
Sets the alignment attribute of the GlobalVariable.
idx_iterator idx_end() const
idx_iterator idx_begin() const
bool isCast() const
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.
This class implements a map that also provides access to all stored values in a deterministic order.
Definition MapVector.h:39
bool empty() const
Definition MapVector.h:80
size_t getBufferSize() const
const char * getBufferStart() const
const char * getBufferEnd() const
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:68
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(LLVMContext &C)
This constructs an opaque pointer to an object in the default address space (address space zero).
LLVM_ABI void update(ArrayRef< uint8_t > Data)
Digest more data.
Definition SHA1.cpp:208
LLVM_ABI std::array< uint8_t, 20 > result()
Return the current raw 160-bits SHA1 for the digested data since the last call to init().
Definition SHA1.cpp:288
size_type size() const
Determine the number of elements in the SetVector.
Definition SetVector.h:103
bool empty() const
Determine if the SetVector is empty or not.
Definition SetVector.h:100
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:157
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
void append(StringRef RHS)
Append from a StringRef.
Definition SmallString.h:68
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void assign(size_type NumElts, ValueParamT Elt)
void reserve(size_type N)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
iterator insert(iterator I, T &&Elt)
void resize(size_type N)
void push_back(const T &Elt)
pointer data()
Return a pointer to the vector's buffer, even if empty().
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
const ValueTy & getValue() const
StringRef getKey() const
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
iterator begin() const
Definition StringRef.h:114
constexpr size_t size() const
Get the string size.
Definition StringRef.h:144
iterator end() const
Definition StringRef.h:116
Utility for building string tables with deduplicated suffixes.
LLVM_ABI size_t add(CachedHashStringRef S, uint8_t Priority=0)
Add a string to the builder.
Target - Wrapper for Target specific information.
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:48
@ UnknownObjectFormat
Definition Triple.h:422
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
bool isX86_FP80Ty() const
Return true if this is x86 long double.
Definition Type.h:161
bool isFloatTy() const
Return true if this is 'float', a 32-bit IEEE fp type.
Definition Type.h:155
bool isBFloatTy() const
Return true if this is 'bfloat', a 16-bit bfloat type.
Definition Type.h:147
bool isPPC_FP128Ty() const
Return true if this is powerpc long double.
Definition Type.h:167
bool isFP128Ty() const
Return true if this is 'fp128'.
Definition Type.h:164
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:363
bool isHalfTy() const
Return true if this is 'half', a 16-bit IEEE fp type.
Definition Type.h:144
bool isDoubleTy() const
Return true if this is 'double', a 64-bit IEEE fp type.
Definition Type.h:158
Value * getValue() const
Definition Metadata.h:510
std::vector< std::pair< const Value *, unsigned > > ValueList
unsigned getTypeID(Type *T) const
unsigned getMetadataID(const Metadata *MD) const
UseListOrderStack UseListOrders
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
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:257
LLVM_ABI void setName(const Twine &Name)
Change the name of the value.
Definition Value.cpp:389
LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.h:260
LLVM_ABI void takeName(Value *V)
Transfer the name from V to this value.
Definition Value.cpp:395
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:210
void build(llvm::MapVector< CallStackId, llvm::SmallVector< FrameIdTy > > &&MemProfCallStackData, const llvm::DenseMap< FrameIdTy, LinearFrameId > *MemProfFrameIndexes, llvm::DenseMap< FrameIdTy, FrameStat > &FrameHistogram)
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
A raw_ostream that writes to an std::string.
std::string & str()
Returns the string's reference.
CallInst * Call
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.
@ Entry
Definition COFF.h:862
Predicate getPredicate(unsigned Condition, unsigned Hint)
Return predicate consisting of specified condition and hint bits.
@ CE
Windows NT (Windows on ARM)
Definition MCAsmInfo.h:51
@ TYPE_CODE_TARGET_TYPE
@ TYPE_CODE_STRUCT_ANON
@ TYPE_CODE_STRUCT_NAME
@ TYPE_CODE_OPAQUE_POINTER
@ TYPE_CODE_STRUCT_NAMED
@ METADATA_COMMON_BLOCK
@ METADATA_TEMPLATE_VALUE
@ METADATA_LEXICAL_BLOCK_FILE
@ METADATA_INDEX_OFFSET
@ METADATA_LEXICAL_BLOCK
@ METADATA_SUBROUTINE_TYPE
@ METADATA_GLOBAL_DECL_ATTACHMENT
@ METADATA_OBJC_PROPERTY
@ METADATA_IMPORTED_ENTITY
@ METADATA_GENERIC_SUBRANGE
@ METADATA_COMPILE_UNIT
@ METADATA_COMPOSITE_TYPE
@ METADATA_FIXED_POINT_TYPE
@ METADATA_LAYERLOCLIST
@ METADATA_DERIVED_TYPE
@ METADATA_SUBRANGE_TYPE
@ METADATA_TEMPLATE_TYPE
@ METADATA_GLOBAL_VAR_EXPR
@ METADATA_DISTINCT_NODE
@ METADATA_GENERIC_DEBUG
GlobalValueSummarySymtabCodes
@ FS_CONTEXT_RADIX_TREE_ARRAY
@ FS_COMBINED_GLOBALVAR_INIT_REFS
@ FS_TYPE_CHECKED_LOAD_VCALLS
@ FS_COMBINED_ORIGINAL_NAME
@ FS_PERMODULE_VTABLE_GLOBALVAR_INIT_REFS
@ FS_TYPE_TEST_ASSUME_CONST_VCALL
@ FS_PERMODULE_GLOBALVAR_INIT_REFS
@ FS_TYPE_TEST_ASSUME_VCALLS
@ FS_COMBINED_ALLOC_INFO_NO_CONTEXT
@ FS_CFI_FUNCTION_DECLS
@ FS_COMBINED_CALLSITE_INFO
@ FS_COMBINED_ALLOC_INFO
@ FS_PERMODULE_CALLSITE_INFO
@ FS_PERMODULE_ALLOC_INFO
@ FS_TYPE_CHECKED_LOAD_CONST_VCALL
@ BITCODE_CURRENT_EPOCH
@ IDENTIFICATION_CODE_EPOCH
@ IDENTIFICATION_CODE_STRING
@ CST_CODE_BLOCKADDRESS
@ CST_CODE_NO_CFI_VALUE
@ CST_CODE_CE_SHUFVEC_EX
@ CST_CODE_CE_EXTRACTELT
@ CST_CODE_CE_SHUFFLEVEC
@ CST_CODE_WIDE_INTEGER
@ CST_CODE_DSO_LOCAL_EQUIVALENT
@ CST_CODE_CE_INSERTELT
@ CST_CODE_CE_GEP_WITH_INRANGE
@ COMDAT_SELECTION_KIND_LARGEST
@ COMDAT_SELECTION_KIND_ANY
@ COMDAT_SELECTION_KIND_SAME_SIZE
@ COMDAT_SELECTION_KIND_EXACT_MATCH
@ COMDAT_SELECTION_KIND_NO_DUPLICATES
@ ATTR_KIND_STACK_PROTECT
@ ATTR_KIND_STACK_PROTECT_STRONG
@ ATTR_KIND_SANITIZE_MEMORY
@ ATTR_KIND_OPTIMIZE_FOR_SIZE
@ ATTR_KIND_SWIFT_ERROR
@ ATTR_KIND_NO_CALLBACK
@ ATTR_KIND_FNRETTHUNK_EXTERN
@ ATTR_KIND_NO_DIVERGENCE_SOURCE
@ ATTR_KIND_SANITIZE_ADDRESS
@ ATTR_KIND_NO_IMPLICIT_FLOAT
@ ATTR_KIND_DEAD_ON_UNWIND
@ ATTR_KIND_STACK_ALIGNMENT
@ ATTR_KIND_STACK_PROTECT_REQ
@ ATTR_KIND_INLINE_HINT
@ ATTR_KIND_NULL_POINTER_IS_VALID
@ ATTR_KIND_SANITIZE_HWADDRESS
@ ATTR_KIND_MUSTPROGRESS
@ ATTR_KIND_RETURNS_TWICE
@ ATTR_KIND_SHADOWCALLSTACK
@ ATTR_KIND_OPT_FOR_FUZZING
@ ATTR_KIND_DENORMAL_FPENV
@ ATTR_KIND_SANITIZE_NUMERICAL_STABILITY
@ ATTR_KIND_SANITIZE_CONCURRENCY
@ ATTR_KIND_INITIALIZES
@ ATTR_KIND_ALLOCATED_POINTER
@ ATTR_KIND_DISABLE_SANITIZER_INSTRUMENTATION
@ ATTR_KIND_SKIP_PROFILE
@ ATTR_KIND_ELEMENTTYPE
@ ATTR_KIND_CORO_ELIDE_SAFE
@ ATTR_KIND_NO_DUPLICATE
@ ATTR_KIND_ALLOC_ALIGN
@ ATTR_KIND_NON_LAZY_BIND
@ ATTR_KIND_DEREFERENCEABLE
@ ATTR_KIND_LIKELY_MODULE_LOCAL
@ ATTR_KIND_OPTIMIZE_NONE
@ ATTR_KIND_HYBRID_PATCHABLE
@ ATTR_KIND_NO_RED_ZONE
@ ATTR_KIND_DEREFERENCEABLE_OR_NULL
@ ATTR_KIND_SANITIZE_REALTIME
@ ATTR_KIND_SPECULATIVE_LOAD_HARDENING
@ ATTR_KIND_ALWAYS_INLINE
@ ATTR_KIND_SANITIZE_TYPE
@ ATTR_KIND_PRESPLIT_COROUTINE
@ ATTR_KIND_VSCALE_RANGE
@ ATTR_KIND_SANITIZE_ALLOC_TOKEN
@ ATTR_KIND_NO_SANITIZE_COVERAGE
@ ATTR_KIND_NO_CREATE_UNDEF_OR_POISON
@ ATTR_KIND_SPECULATABLE
@ ATTR_KIND_DEAD_ON_RETURN
@ ATTR_KIND_SANITIZE_REALTIME_BLOCKING
@ ATTR_KIND_NO_SANITIZE_BOUNDS
@ ATTR_KIND_SANITIZE_MEMTAG
@ ATTR_KIND_CORO_ONLY_DESTROY_WHEN_COMPLETE
@ ATTR_KIND_SANITIZE_THREAD
@ ATTR_KIND_OPTIMIZE_FOR_DEBUGGING
@ ATTR_KIND_PREALLOCATED
@ ATTR_KIND_SWIFT_ASYNC
@ SYNC_SCOPE_NAMES_BLOCK_ID
@ PARAMATTR_GROUP_BLOCK_ID
@ METADATA_KIND_BLOCK_ID
@ IDENTIFICATION_BLOCK_ID
@ GLOBALVAL_SUMMARY_BLOCK_ID
@ METADATA_ATTACHMENT_ID
@ FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID
@ MODULE_STRTAB_BLOCK_ID
@ VALUE_SYMTAB_BLOCK_ID
@ OPERAND_BUNDLE_TAGS_BLOCK_ID
@ MODULE_CODE_VERSION
@ MODULE_CODE_SOURCE_FILENAME
@ MODULE_CODE_SECTIONNAME
@ MODULE_CODE_DATALAYOUT
@ MODULE_CODE_GLOBALVAR
@ MODULE_CODE_VSTOFFSET
@ MODULE_CODE_ASM_PROPERTY
@ FUNC_CODE_INST_CATCHRET
@ FUNC_CODE_INST_LANDINGPAD
@ FUNC_CODE_INST_EXTRACTVAL
@ FUNC_CODE_INST_CATCHPAD
@ FUNC_CODE_INST_RESUME
@ FUNC_CODE_INST_CALLBR
@ FUNC_CODE_INST_CATCHSWITCH
@ FUNC_CODE_INST_VSELECT
@ FUNC_CODE_INST_CLEANUPRET
@ FUNC_CODE_DEBUG_RECORD_VALUE
@ FUNC_CODE_INST_LOADATOMIC
@ FUNC_CODE_DEBUG_RECORD_ASSIGN
@ FUNC_CODE_INST_STOREATOMIC
@ FUNC_CODE_INST_BITEXTRACT
@ FUNC_CODE_INST_ATOMICRMW
@ FUNC_CODE_DEBUG_RECORD_DECLARE_VALUE
@ FUNC_CODE_DEBUG_LOC_AGAIN
@ FUNC_CODE_INST_EXTRACTELT
@ FUNC_CODE_INST_INDIRECTBR
@ FUNC_CODE_INST_INVOKE
@ FUNC_CODE_DEBUG_RECORD_VALUE_SIMPLE
@ FUNC_CODE_INST_INSERTVAL
@ FUNC_CODE_DECLAREBLOCKS
@ FUNC_CODE_DEBUG_RECORD_LABEL
@ FUNC_CODE_INST_SWITCH
@ FUNC_CODE_INST_ALLOCA
@ FUNC_CODE_INST_INSERTELT
@ FUNC_CODE_BLOCKADDR_USERS
@ FUNC_CODE_INST_CLEANUPPAD
@ FUNC_CODE_INST_SHUFFLEVEC
@ FUNC_CODE_INST_FREEZE
@ FUNC_CODE_INST_CMPXCHG
@ FUNC_CODE_INST_UNREACHABLE
@ FUNC_CODE_INST_BITINSERT
@ FUNC_CODE_DEBUG_RECORD_DECLARE
@ FUNC_CODE_OPERAND_BUNDLE
@ FIRST_APPLICATION_ABBREV
@ PARAMATTR_GRP_CODE_ENTRY
initializer< Ty > init(const Ty &Val)
@ DW_APPLE_ENUM_KIND_invalid
Enum kind for invalid results.
Definition Dwarf.h:51
LLVM_ABI 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:348
llvm::unique_function< void(llvm::Expected< T >)> Callback
A Callback<T> is a void function that accepts Expected<T>.
Definition Transport.h:132
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract_or_null(Y &&MD)
Extract a Value from Metadata, allowing null.
Definition Metadata.h:694
LLVM_ABI bool metadataIncludesAllContextSizeInfo()
Whether the alloc memeprof metadata will include context size info for all MIBs.
template LLVM_ABI llvm::DenseMap< LinearFrameId, FrameStat > computeFrameHistogram< LinearFrameId >(llvm::MapVector< CallStackId, llvm::SmallVector< LinearFrameId > > &MemProfCallStackData)
LLVM_ABI bool metadataMayIncludeContextSizeInfo()
Whether the alloc memprof metadata may include context size info for some MIBs (but possibly not all)...
uint32_t LinearFrameId
Definition MemProf.h:236
uint64_t CallStackId
Definition MemProf.h:353
NodeAddr< CodeNode * > Code
Definition RDFGraph.h:388
void write32le(void *P, uint32_t V)
Definition Endian.h:455
uint32_t read32be(const void *P)
Definition Endian.h:421
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:316
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:339
void stable_sort(R &&Range)
Definition STLExtras.h:2132
StringMapEntry< Value * > ValueName
Definition Value.h:56
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1685
unsigned encode(MaybeAlign A)
Returns a representation of the alignment that encodes undefined as 0.
Definition Alignment.h:211
LLVM_ABI void WriteBitcodeToFile(const Module &M, raw_ostream &Out, bool ShouldPreserveUseListOrder=false, const ModuleSummaryIndex *Index=nullptr, bool GenerateHash=false, ModuleHash *ModHash=nullptr)
Write the specified module to the specified raw output stream.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2570
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
std::array< uint32_t, 5 > ModuleHash
160 bits SHA1
LLVM_ABI 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...
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
@ BWH_HeaderSize
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2224
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
LLVM_ABI void writeIndexToFile(const ModuleSummaryIndex &Index, raw_ostream &Out, const ModuleToSummariesForIndexTy *ModuleToSummariesForIndex=nullptr, const GVSummaryPtrSet *DecSummaries=nullptr)
Write the specified module summary index to the given raw output stream, where it will be written in ...
LLVM_ABI 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:1762
const BitWriterOptions & getBitWriterOptions(const LLVMContext &C)
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1652
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
std::map< std::string, GVSummaryMapTy, std::less<> > ModuleToSummariesForIndexTy
Map of a module name to the GUIDs and summaries we will import from that module.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
AtomicOrdering
Atomic ordering for LLVM's memory model.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
OutputIt copy(R &&Range, OutputIt Out)
Definition STLExtras.h:1901
constexpr unsigned BitWidth
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
constexpr bool valueOr(BoolOrDefault X, bool Default)
std::vector< TypeIdOffsetVtableInfo > TypeIdCompatibleVtableInfo
List of vtable definitions decorated by a particular type identifier, and their corresponding offsets...
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,...
SmallPtrSet< GlobalValueSummary *, 0 > GVSummaryPtrSet
A set of global value summary pointers.
void consumeError(Error Err)
Consume a Error without doing anything.
Definition Error.h:1106
LLVM_ABI Error write(DWPWriter &Out, ArrayRef< std::string > Inputs, OnCuIndexOverflow OverflowOptValue, Dwarf64StrOffsetsPromotion StrOffsetsOptValue, raw_pwrite_stream *OS=nullptr)
Definition DWP.cpp:746
LLVM_ABI 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:966
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition Error.cpp:177
#define N
#define NC
Definition regutils.h:42
#define NDEBUG
Definition regutils.h:48
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
static void set(StorageType &Packed, typename Bitfield::Type Value)
Sets the typed value in the provided Packed value.
Definition Bitfields.h:223
Class to accumulate and hold information about a callee.
Flags specific to function summaries.
static constexpr uint32_t RangeWidth
Group flags (Linkage, NotEligibleToImport, etc.) as a bitfield.
static LLVM_ABI const Target * lookupTarget(const Triple &TheTriple, std::string &Error)
lookupTarget - Lookup a target based on a target triple.
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,...