Thanks to visit codestin.com
Credit goes to llvm.org

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