31#include "llvm/Config/llvm-config.h"
89 cl::desc(
"Number of metadatas above which we emit an index "
90 "to enable lazy-loading"));
93 cl::desc(
"The threshold (unit M) for flushing LLVM bitcode."));
97 cl::desc(
"Write relative block frequency to function summary "));
115 VST_BBENTRY_6_ABBREV,
119 CONSTANTS_INTEGER_ABBREV,
120 CONSTANTS_CE_CAST_Abbrev,
121 CONSTANTS_NULL_Abbrev,
125 FUNCTION_INST_UNOP_ABBREV,
126 FUNCTION_INST_UNOP_FLAGS_ABBREV,
127 FUNCTION_INST_BINOP_ABBREV,
128 FUNCTION_INST_BINOP_FLAGS_ABBREV,
129 FUNCTION_INST_CAST_ABBREV,
130 FUNCTION_INST_CAST_FLAGS_ABBREV,
131 FUNCTION_INST_RET_VOID_ABBREV,
132 FUNCTION_INST_RET_VAL_ABBREV,
133 FUNCTION_INST_UNREACHABLE_ABBREV,
134 FUNCTION_INST_GEP_ABBREV,
135 FUNCTION_DEBUG_RECORD_VALUE_ABBREV,
140class BitcodeWriterBase {
151 : Stream(Stream), StrtabBuilder(StrtabBuilder) {}
154 void writeModuleVersion();
157void BitcodeWriterBase::writeModuleVersion() {
164class ModuleBitcodeWriterBase :
public BitcodeWriterBase {
178 std::map<GlobalValue::GUID, unsigned> GUIDToValueIdMap;
181 unsigned GlobalValueId;
192 bool ShouldPreserveUseListOrder,
194 : BitcodeWriterBase(Stream, StrtabBuilder),
M(
M),
204 for (
const auto &GUIDSummaryLists : *
Index)
206 for (
auto &Summary : GUIDSummaryLists.second.SummaryList)
207 if (
auto FS = dyn_cast<FunctionSummary>(
Summary.get())) {
212 for (
auto &CallEdge :
FS->calls())
213 if (!CallEdge.first.haveGVs() || !CallEdge.first.getValue())
214 assignValueId(CallEdge.first.getGUID());
220 for (
auto &RefEdge :
FS->refs())
221 if (!RefEdge.haveGVs() || !RefEdge.getValue())
222 assignValueId(RefEdge.getGUID());
227 void writePerModuleGlobalValueSummary();
230 void writePerModuleFunctionSummaryRecord(
232 unsigned ValueID,
unsigned FSCallsAbbrev,
unsigned FSCallsProfileAbbrev,
233 unsigned CallsiteAbbrev,
unsigned AllocAbbrev,
const Function &
F);
236 unsigned FSModRefsAbbrev,
237 unsigned FSModVTableRefsAbbrev);
240 GUIDToValueIdMap[ValGUID] = ++GlobalValueId;
244 const auto &VMI = GUIDToValueIdMap.find(ValGUID);
247 assert(VMI != GUIDToValueIdMap.end() &&
248 "GUID does not have assigned value Id");
254 if (!
VI.haveGVs() || !
VI.getValue())
255 return getValueId(
VI.getGUID());
259 std::map<GlobalValue::GUID, unsigned> &valueIds() {
return GUIDToValueIdMap; }
263class ModuleBitcodeWriter :
public ModuleBitcodeWriterBase {
283 : ModuleBitcodeWriterBase(
M, StrtabBuilder, Stream,
284 ShouldPreserveUseListOrder,
Index),
285 GenerateHash(GenerateHash), ModHash(ModHash),
286 BitcodeStartBit(Stream.GetCurrentBitNo()) {}
292 uint64_t bitcodeStartBit() {
return BitcodeStartBit; }
296 void writeAttributeGroupTable();
297 void writeAttributeTable();
298 void writeTypeTable();
300 void writeValueSymbolTableForwardDecl();
301 void writeModuleInfo();
306 unsigned createDILocationAbbrev();
309 unsigned createGenericDINodeAbbrev();
377 unsigned createNamedMetadataAbbrev();
379 unsigned createMetadataStringsAbbrev();
384 std::vector<unsigned> *MDAbbrevs =
nullptr,
385 std::vector<uint64_t> *IndexPos =
nullptr);
386 void writeModuleMetadata();
387 void writeFunctionMetadata(
const Function &
F);
388 void writeFunctionMetadataAttachment(
const Function &
F);
391 void writeModuleMetadataKinds();
392 void writeOperandBundleTags();
393 void writeSyncScopeNames();
394 void writeConstants(
unsigned FirstVal,
unsigned LastVal,
bool isGlobal);
395 void writeModuleConstants();
396 bool pushValueAndType(
const Value *V,
unsigned InstID,
398 void writeOperandBundles(
const CallBase &CB,
unsigned InstID);
399 void pushValue(
const Value *V,
unsigned InstID,
401 void pushValueSigned(
const Value *V,
unsigned InstID,
403 void writeInstruction(
const Instruction &
I,
unsigned InstID,
406 void writeGlobalValueSymbolTable(
409 void writeUseListBlock(
const Function *
F);
413 void writeBlockInfo();
420 unsigned getEncodedAlign(
MaybeAlign Alignment) {
return encode(Alignment); }
424class IndexBitcodeWriter :
public BitcodeWriterBase {
435 const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex;
439 std::map<GlobalValue::GUID, unsigned> GUIDToValueIdMap;
443 std::vector<uint64_t> StackIds;
451 unsigned GlobalValueId = 0;
467 const std::map<std::string, GVSummaryMapTy>
468 *ModuleToSummariesForIndex =
nullptr)
469 : BitcodeWriterBase(Stream, StrtabBuilder),
Index(
Index),
470 DecSummaries(DecSummaries),
471 ModuleToSummariesForIndex(ModuleToSummariesForIndex) {
475 auto RecordStackIdReference = [&](
unsigned StackIdIndex) {
480 StackIdIndicesToIndex.
insert({StackIdIndex, StackIds.size()});
482 StackIds.push_back(
Index.getStackIdAtIndex(StackIdIndex));
489 forEachSummary([&](GVInfo
I,
bool IsAliasee) {
490 GUIDToValueIdMap[
I.first] = ++GlobalValueId;
493 auto *
FS = dyn_cast<FunctionSummary>(
I.second);
499 for (
auto &CI :
FS->callsites()) {
510 if (CI.StackIdIndices.empty()) {
511 GUIDToValueIdMap[CI.Callee.getGUID()] = ++GlobalValueId;
514 for (
auto Idx : CI.StackIdIndices)
515 RecordStackIdReference(
Idx);
517 for (
auto &AI :
FS->allocs())
518 for (
auto &MIB : AI.MIBs)
519 for (
auto Idx : MIB.StackIdIndices)
520 RecordStackIdReference(
Idx);
525 using GVInfo = std::pair<GlobalValue::GUID, GlobalValueSummary *>;
530 template<
typename Functor>
531 void forEachSummary(Functor Callback) {
532 if (ModuleToSummariesForIndex) {
533 for (
auto &M : *ModuleToSummariesForIndex)
534 for (
auto &Summary :
M.second) {
535 Callback(Summary,
false);
539 if (
auto *AS = dyn_cast<AliasSummary>(
Summary.getSecond()))
540 Callback({AS->getAliaseeGUID(), &AS->getAliasee()},
true);
543 for (
auto &Summaries :
Index)
544 for (
auto &Summary : Summaries.second.SummaryList)
545 Callback({Summaries.first,
Summary.get()},
false);
553 template <
typename Functor>
void forEachModule(Functor Callback) {
554 if (ModuleToSummariesForIndex) {
555 for (
const auto &M : *ModuleToSummariesForIndex) {
556 const auto &MPI =
Index.modulePaths().find(
M.first);
557 if (MPI ==
Index.modulePaths().end()) {
561 assert(ModuleToSummariesForIndex->size() == 1);
571 std::vector<StringRef> ModulePaths;
572 for (
auto &[ModPath,
_] :
Index.modulePaths())
573 ModulePaths.push_back(ModPath);
574 llvm::sort(ModulePaths.begin(), ModulePaths.end());
575 for (
auto &ModPath : ModulePaths)
576 Callback(*
Index.modulePaths().find(ModPath));
584 void writeModStrings();
585 void writeCombinedGlobalValueSummary();
588 auto VMI = GUIDToValueIdMap.find(ValGUID);
589 if (VMI == GUIDToValueIdMap.end())
594 std::map<GlobalValue::GUID, unsigned> &valueIds() {
return GUIDToValueIdMap; }
628 case Instruction::Add:
630 case Instruction::Sub:
632 case Instruction::Mul:
635 case Instruction::FDiv:
638 case Instruction::FRem:
704 case Attribute::Alignment:
706 case Attribute::AllocAlign:
708 case Attribute::AllocSize:
710 case Attribute::AlwaysInline:
712 case Attribute::Builtin:
714 case Attribute::ByVal:
716 case Attribute::Convergent:
718 case Attribute::InAlloca:
720 case Attribute::Cold:
722 case Attribute::DisableSanitizerInstrumentation:
724 case Attribute::FnRetThunkExtern:
728 case Attribute::ElementType:
730 case Attribute::HybridPatchable:
732 case Attribute::InlineHint:
734 case Attribute::InReg:
736 case Attribute::JumpTable:
738 case Attribute::MinSize:
740 case Attribute::AllocatedPointer:
742 case Attribute::AllocKind:
744 case Attribute::Memory:
746 case Attribute::NoFPClass:
748 case Attribute::Naked:
750 case Attribute::Nest:
752 case Attribute::NoAlias:
754 case Attribute::NoBuiltin:
756 case Attribute::NoCallback:
758 case Attribute::NoCapture:
760 case Attribute::NoDuplicate:
762 case Attribute::NoFree:
764 case Attribute::NoImplicitFloat:
766 case Attribute::NoInline:
768 case Attribute::NoRecurse:
770 case Attribute::NoMerge:
772 case Attribute::NonLazyBind:
774 case Attribute::NonNull:
776 case Attribute::Dereferenceable:
778 case Attribute::DereferenceableOrNull:
780 case Attribute::NoRedZone:
782 case Attribute::NoReturn:
784 case Attribute::NoSync:
786 case Attribute::NoCfCheck:
788 case Attribute::NoProfile:
790 case Attribute::SkipProfile:
792 case Attribute::NoUnwind:
794 case Attribute::NoSanitizeBounds:
796 case Attribute::NoSanitizeCoverage:
798 case Attribute::NullPointerIsValid:
800 case Attribute::OptimizeForDebugging:
802 case Attribute::OptForFuzzing:
804 case Attribute::OptimizeForSize:
806 case Attribute::OptimizeNone:
808 case Attribute::ReadNone:
810 case Attribute::ReadOnly:
812 case Attribute::Returned:
814 case Attribute::ReturnsTwice:
816 case Attribute::SExt:
818 case Attribute::Speculatable:
820 case Attribute::StackAlignment:
822 case Attribute::StackProtect:
824 case Attribute::StackProtectReq:
826 case Attribute::StackProtectStrong:
828 case Attribute::SafeStack:
830 case Attribute::ShadowCallStack:
832 case Attribute::StrictFP:
834 case Attribute::StructRet:
836 case Attribute::SanitizeAddress:
838 case Attribute::SanitizeHWAddress:
840 case Attribute::SanitizeThread:
842 case Attribute::SanitizeMemory:
844 case Attribute::SanitizeNumericalStability:
846 case Attribute::SanitizeRealtime:
848 case Attribute::SpeculativeLoadHardening:
850 case Attribute::SwiftError:
852 case Attribute::SwiftSelf:
854 case Attribute::SwiftAsync:
856 case Attribute::UWTable:
858 case Attribute::VScaleRange:
860 case Attribute::WillReturn:
862 case Attribute::WriteOnly:
864 case Attribute::ZExt:
866 case Attribute::ImmArg:
868 case Attribute::SanitizeMemTag:
870 case Attribute::Preallocated:
872 case Attribute::NoUndef:
874 case Attribute::ByRef:
876 case Attribute::MustProgress:
878 case Attribute::PresplitCoroutine:
880 case Attribute::Writable:
882 case Attribute::CoroDestroyOnlyWhenComplete:
884 case Attribute::DeadOnUnwind:
886 case Attribute::Range:
888 case Attribute::Initializes:
914 unsigned NumWords =
A.getActiveWords();
915 const uint64_t *RawData =
A.getRawData();
916 for (
unsigned i = 0; i < NumWords; i++)
936void ModuleBitcodeWriter::writeAttributeGroupTable() {
937 const std::vector<ValueEnumerator::IndexAndAttrSet> &AttrGrps =
939 if (AttrGrps.empty())
return;
945 unsigned AttrListIndex = Pair.first;
948 Record.push_back(AttrListIndex);
951 if (Attr.isEnumAttribute()) {
954 }
else if (Attr.isIntAttribute()) {
957 Record.push_back(Attr.getValueAsInt());
958 }
else if (Attr.isStringAttribute()) {
969 }
else if (Attr.isTypeAttribute()) {
970 Type *Ty = Attr.getValueAsType();
971 Record.push_back(Ty ? 6 : 5);
975 }
else if (Attr.isConstantRangeAttribute()) {
981 assert(Attr.isConstantRangeListAttribute());
999void ModuleBitcodeWriter::writeAttributeTable() {
1001 if (
Attrs.empty())
return;
1007 for (
unsigned i :
AL.indexes()) {
1021void ModuleBitcodeWriter::writeTypeTable() {
1030 auto Abbv = std::make_shared<BitCodeAbbrev>();
1033 unsigned OpaquePtrAbbrev = Stream.
EmitAbbrev(std::move(Abbv));
1036 Abbv = std::make_shared<BitCodeAbbrev>();
1041 unsigned FunctionAbbrev = Stream.
EmitAbbrev(std::move(Abbv));
1044 Abbv = std::make_shared<BitCodeAbbrev>();
1049 unsigned StructAnonAbbrev = Stream.
EmitAbbrev(std::move(Abbv));
1052 Abbv = std::make_shared<BitCodeAbbrev>();
1056 unsigned StructNameAbbrev = Stream.
EmitAbbrev(std::move(Abbv));
1059 Abbv = std::make_shared<BitCodeAbbrev>();
1064 unsigned StructNamedAbbrev = Stream.
EmitAbbrev(std::move(Abbv));
1067 Abbv = std::make_shared<BitCodeAbbrev>();
1071 unsigned ArrayAbbrev = Stream.
EmitAbbrev(std::move(Abbv));
1079 for (
Type *
T : TypeList) {
1080 int AbbrevToUse = 0;
1083 switch (
T->getTypeID()) {
1110 AbbrevToUse = OpaquePtrAbbrev;
1119 for (
unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
1121 AbbrevToUse = FunctionAbbrev;
1129 for (
Type *ET :
ST->elements())
1132 if (
ST->isLiteral()) {
1134 AbbrevToUse = StructAnonAbbrev;
1136 if (
ST->isOpaque()) {
1140 AbbrevToUse = StructNamedAbbrev;
1144 if (!
ST->getName().empty())
1154 TypeVals.
push_back(AT->getNumElements());
1156 AbbrevToUse = ArrayAbbrev;
1165 TypeVals.
push_back(VT->getElementCount().getKnownMinValue());
1167 if (isa<ScalableVectorType>(VT))
1177 for (
Type *InnerTy :
TET->type_params())
1179 for (
unsigned IntParam :
TET->int_params())
1188 Stream.
EmitRecord(Code, TypeVals, AbbrevToUse);
1229 RawFlags |= Flags.ReadNone;
1230 RawFlags |= (Flags.ReadOnly << 1);
1231 RawFlags |= (Flags.NoRecurse << 2);
1232 RawFlags |= (Flags.ReturnDoesNotAlias << 3);
1233 RawFlags |= (Flags.NoInline << 4);
1234 RawFlags |= (Flags.AlwaysInline << 5);
1235 RawFlags |= (Flags.NoUnwind << 6);
1236 RawFlags |= (Flags.MayThrow << 7);
1237 RawFlags |= (Flags.HasUnknownCall << 8);
1238 RawFlags |= (Flags.MustBeUnreachable << 9);
1245 bool ImportAsDecl =
false) {
1248 RawFlags |= Flags.NotEligibleToImport;
1249 RawFlags |= (Flags.Live << 1);
1250 RawFlags |= (Flags.DSOLocal << 2);
1251 RawFlags |= (Flags.CanAutoHide << 3);
1256 RawFlags = (RawFlags << 4) | Flags.Linkage;
1258 RawFlags |= (Flags.Visibility << 8);
1260 unsigned ImportType = Flags.ImportType | ImportAsDecl;
1261 RawFlags |= (ImportType << 10);
1267 uint64_t RawFlags = Flags.MaybeReadOnly | (Flags.MaybeWriteOnly << 1) |
1268 (Flags.Constant << 2) | Flags.VCallVisibility << 3;
1310 case GlobalVariable::NotThreadLocal:
return 0;
1311 case GlobalVariable::GeneralDynamicTLSModel:
return 1;
1312 case GlobalVariable::LocalDynamicTLSModel:
return 2;
1313 case GlobalVariable::InitialExecTLSModel:
return 3;
1314 case GlobalVariable::LocalExecTLSModel:
return 4;
1320 switch (
C.getSelectionKind()) {
1337 case GlobalValue::UnnamedAddr::None:
return 0;
1338 case GlobalValue::UnnamedAddr::Local:
return 2;
1339 case GlobalValue::UnnamedAddr::Global:
return 1;
1344size_t ModuleBitcodeWriter::addToStrtab(
StringRef Str) {
1347 return StrtabBuilder.
add(Str);
1350void ModuleBitcodeWriter::writeComdats() {
1365void ModuleBitcodeWriter::writeValueSymbolTableForwardDecl() {
1370 auto Abbv = std::make_shared<BitCodeAbbrev>();
1376 unsigned VSTOffsetAbbrev = Stream.
EmitAbbrev(std::move(Abbv));
1392 bool isChar6 =
true;
1393 for (
char C : Str) {
1396 if ((
unsigned char)
C & 128)
1406 "Sanitizer Metadata is too large for naive serialization.");
1409 return Meta.NoAddress | (Meta.NoHWAddress << 1) |
1410 (Meta.Memtag << 2) | (Meta.IsDynInit << 3);
1416void ModuleBitcodeWriter::writeModuleInfo() {
1418 if (!
M.getTargetTriple().empty())
1421 const std::string &
DL =
M.getDataLayoutStr();
1424 if (!
M.getModuleInlineAsm().empty())
1430 std::map<std::string, unsigned> SectionMap;
1431 std::map<std::string, unsigned> GCMap;
1433 unsigned MaxGlobalType = 0;
1434 const auto UpdateMaxAlignment = [&MaxAlignment](
const MaybeAlign A) {
1436 MaxAlignment = !MaxAlignment ? *
A : std::max(*MaxAlignment, *
A);
1439 UpdateMaxAlignment(GV.getAlign());
1440 MaxGlobalType = std::max(MaxGlobalType, VE.
getTypeID(GV.getValueType()));
1441 if (GV.hasSection()) {
1443 unsigned &
Entry = SectionMap[std::string(GV.getSection())];
1447 Entry = SectionMap.size();
1452 UpdateMaxAlignment(
F.getAlign());
1453 if (
F.hasSection()) {
1455 unsigned &
Entry = SectionMap[std::string(
F.getSection())];
1459 Entry = SectionMap.size();
1464 unsigned &
Entry = GCMap[
F.getGC()];
1468 Entry = GCMap.size();
1474 unsigned SimpleGVarAbbrev = 0;
1475 if (!
M.global_empty()) {
1477 auto Abbv = std::make_shared<BitCodeAbbrev>();
1491 unsigned MaxEncAlignment = getEncodedAlign(MaxAlignment);
1495 if (SectionMap.empty())
1501 SimpleGVarAbbrev = Stream.
EmitAbbrev(std::move(Abbv));
1515 auto Abbv = std::make_shared<BitCodeAbbrev>();
1518 Abbv->Add(AbbrevOpToUse);
1519 unsigned FilenameAbbrev = Stream.
EmitAbbrev(std::move(Abbv));
1521 for (
const auto P :
M.getSourceFileName())
1531 unsigned AbbrevToUse = 0;
1537 Vals.
push_back(addToStrtab(GV.getName()));
1540 Vals.
push_back(GV.getType()->getAddressSpace() << 2 | 2 | GV.isConstant());
1544 Vals.
push_back(getEncodedAlign(GV.getAlign()));
1545 Vals.
push_back(GV.hasSection() ? SectionMap[std::string(GV.getSection())]
1547 if (GV.isThreadLocal() ||
1549 GV.getUnnamedAddr() != GlobalValue::UnnamedAddr::None ||
1550 GV.isExternallyInitialized() ||
1552 GV.hasComdat() || GV.hasAttributes() || GV.isDSOLocal() ||
1553 GV.hasPartition() || GV.hasSanitizerMetadata() || GV.getCodeModel()) {
1557 Vals.
push_back(GV.isExternallyInitialized());
1565 Vals.
push_back(addToStrtab(GV.getPartition()));
1566 Vals.
push_back(GV.getPartition().size());
1569 GV.getSanitizerMetadata())
1573 AbbrevToUse = SimpleGVarAbbrev;
1593 Vals.
push_back(getEncodedAlign(
F.getAlign()));
1594 Vals.
push_back(
F.hasSection() ? SectionMap[std::string(
F.getSection())]
1606 F.hasPersonalityFn() ? (VE.
getValueID(
F.getPersonalityFn()) + 1) : 0);
1610 Vals.
push_back(addToStrtab(
F.getPartition()));
1613 unsigned AbbrevToUse = 0;
1626 Vals.
push_back(
A.getType()->getAddressSpace());
1634 Vals.
push_back(addToStrtab(
A.getPartition()));
1637 unsigned AbbrevToUse = 0;
1649 Vals.
push_back(
I.getType()->getAddressSpace());
1654 Vals.
push_back(addToStrtab(
I.getPartition()));
1660 writeValueSymbolTableForwardDecl();
1666 if (
const auto *OBO = dyn_cast<OverflowingBinaryOperator>(V)) {
1667 if (OBO->hasNoSignedWrap())
1669 if (OBO->hasNoUnsignedWrap())
1671 }
else if (
const auto *PEO = dyn_cast<PossiblyExactOperator>(V)) {
1674 }
else if (
const auto *PDI = dyn_cast<PossiblyDisjointInst>(V)) {
1675 if (PDI->isDisjoint())
1677 }
else if (
const auto *FPMO = dyn_cast<FPMathOperator>(V)) {
1678 if (FPMO->hasAllowReassoc())
1680 if (FPMO->hasNoNaNs())
1682 if (FPMO->hasNoInfs())
1684 if (FPMO->hasNoSignedZeros())
1686 if (FPMO->hasAllowReciprocal())
1688 if (FPMO->hasAllowContract())
1690 if (FPMO->hasApproxFunc())
1692 }
else if (
const auto *NNI = dyn_cast<PossiblyNonNegInst>(V)) {
1693 if (NNI->hasNonNeg())
1695 }
else if (
const auto *TI = dyn_cast<TruncInst>(V)) {
1696 if (TI->hasNoSignedWrap())
1698 if (TI->hasNoUnsignedWrap())
1700 }
else if (
const auto *
GEP = dyn_cast<GEPOperator>(V)) {
1701 if (
GEP->isInBounds())
1703 if (
GEP->hasNoUnsignedSignedWrap())
1705 if (
GEP->hasNoUnsignedWrap())
1712void ModuleBitcodeWriter::writeValueAsMetadata(
1722void ModuleBitcodeWriter::writeMDTuple(
const MDTuple *
N,
1727 assert(!(MD && isa<LocalAsMetadata>(MD)) &&
1728 "Unexpected function-local metadata");
1737unsigned ModuleBitcodeWriter::createDILocationAbbrev() {
1740 auto Abbv = std::make_shared<BitCodeAbbrev>();
1751void ModuleBitcodeWriter::writeDILocation(
const DILocation *
N,
1755 Abbrev = createDILocationAbbrev();
1757 Record.push_back(
N->isDistinct());
1758 Record.push_back(
N->getLine());
1759 Record.push_back(
N->getColumn());
1762 Record.push_back(
N->isImplicitCode());
1768unsigned ModuleBitcodeWriter::createGenericDINodeAbbrev() {
1771 auto Abbv = std::make_shared<BitCodeAbbrev>();
1782void ModuleBitcodeWriter::writeGenericDINode(
const GenericDINode *
N,
1786 Abbrev = createGenericDINodeAbbrev();
1788 Record.push_back(
N->isDistinct());
1789 Record.push_back(
N->getTag());
1792 for (
auto &
I :
N->operands())
1799void ModuleBitcodeWriter::writeDISubrange(
const DISubrange *
N,
1813void ModuleBitcodeWriter::writeDIGenericSubrange(
1826void ModuleBitcodeWriter::writeDIEnumerator(
const DIEnumerator *
N,
1830 Record.push_back(IsBigInt | (
N->isUnsigned() << 1) |
N->isDistinct());
1831 Record.push_back(
N->getValue().getBitWidth());
1839void ModuleBitcodeWriter::writeDIBasicType(
const DIBasicType *
N,
1842 Record.push_back(
N->isDistinct());
1843 Record.push_back(
N->getTag());
1845 Record.push_back(
N->getSizeInBits());
1846 Record.push_back(
N->getAlignInBits());
1847 Record.push_back(
N->getEncoding());
1848 Record.push_back(
N->getFlags());
1854void ModuleBitcodeWriter::writeDIStringType(
const DIStringType *
N,
1857 Record.push_back(
N->isDistinct());
1858 Record.push_back(
N->getTag());
1863 Record.push_back(
N->getSizeInBits());
1864 Record.push_back(
N->getAlignInBits());
1865 Record.push_back(
N->getEncoding());
1871void ModuleBitcodeWriter::writeDIDerivedType(
const DIDerivedType *
N,
1874 Record.push_back(
N->isDistinct());
1875 Record.push_back(
N->getTag());
1878 Record.push_back(
N->getLine());
1881 Record.push_back(
N->getSizeInBits());
1882 Record.push_back(
N->getAlignInBits());
1883 Record.push_back(
N->getOffsetInBits());
1884 Record.push_back(
N->getFlags());
1889 if (
const auto &DWARFAddressSpace =
N->getDWARFAddressSpace())
1890 Record.push_back(*DWARFAddressSpace + 1);
1896 if (
auto PtrAuthData =
N->getPtrAuthData())
1897 Record.push_back(PtrAuthData->RawData);
1905void ModuleBitcodeWriter::writeDICompositeType(
1908 const unsigned IsNotUsedInOldTypeRef = 0x2;
1909 Record.push_back(IsNotUsedInOldTypeRef | (
unsigned)
N->isDistinct());
1910 Record.push_back(
N->getTag());
1913 Record.push_back(
N->getLine());
1916 Record.push_back(
N->getSizeInBits());
1917 Record.push_back(
N->getAlignInBits());
1918 Record.push_back(
N->getOffsetInBits());
1919 Record.push_back(
N->getFlags());
1921 Record.push_back(
N->getRuntimeLang());
1936void ModuleBitcodeWriter::writeDISubroutineType(
1939 const unsigned HasNoOldTypeRefs = 0x2;
1940 Record.push_back(HasNoOldTypeRefs | (
unsigned)
N->isDistinct());
1941 Record.push_back(
N->getFlags());
1949void ModuleBitcodeWriter::writeDIFile(
const DIFile *
N,
1952 Record.push_back(
N->isDistinct());
1955 if (
N->getRawChecksum()) {
1956 Record.push_back(
N->getRawChecksum()->Kind);
1964 auto Source =
N->getRawSource();
1972void ModuleBitcodeWriter::writeDICompileUnit(
const DICompileUnit *
N,
1975 assert(
N->isDistinct() &&
"Expected distinct compile units");
1977 Record.push_back(
N->getSourceLanguage());
1980 Record.push_back(
N->isOptimized());
1982 Record.push_back(
N->getRuntimeVersion());
1984 Record.push_back(
N->getEmissionKind());
1990 Record.push_back(
N->getDWOId());
1992 Record.push_back(
N->getSplitDebugInlining());
1993 Record.push_back(
N->getDebugInfoForProfiling());
1994 Record.push_back((
unsigned)
N->getNameTableKind());
1995 Record.push_back(
N->getRangesBaseAddress());
2003void ModuleBitcodeWriter::writeDISubprogram(
const DISubprogram *
N,
2006 const uint64_t HasUnitFlag = 1 << 1;
2007 const uint64_t HasSPFlagsFlag = 1 << 2;
2008 Record.push_back(
uint64_t(
N->isDistinct()) | HasUnitFlag | HasSPFlagsFlag);
2013 Record.push_back(
N->getLine());
2015 Record.push_back(
N->getScopeLine());
2017 Record.push_back(
N->getSPFlags());
2018 Record.push_back(
N->getVirtualIndex());
2019 Record.push_back(
N->getFlags());
2024 Record.push_back(
N->getThisAdjustment());
2036 Record.push_back(
N->isDistinct());
2039 Record.push_back(
N->getLine());
2040 Record.push_back(
N->getColumn());
2046void ModuleBitcodeWriter::writeDILexicalBlockFile(
2049 Record.push_back(
N->isDistinct());
2052 Record.push_back(
N->getDiscriminator());
2058void ModuleBitcodeWriter::writeDICommonBlock(
const DICommonBlock *
N,
2061 Record.push_back(
N->isDistinct());
2066 Record.push_back(
N->getLineNo());
2072void ModuleBitcodeWriter::writeDINamespace(
const DINamespace *
N,
2075 Record.push_back(
N->isDistinct() |
N->getExportSymbols() << 1);
2083void ModuleBitcodeWriter::writeDIMacro(
const DIMacro *
N,
2086 Record.push_back(
N->isDistinct());
2087 Record.push_back(
N->getMacinfoType());
2088 Record.push_back(
N->getLine());
2096void ModuleBitcodeWriter::writeDIMacroFile(
const DIMacroFile *
N,
2099 Record.push_back(
N->isDistinct());
2100 Record.push_back(
N->getMacinfoType());
2101 Record.push_back(
N->getLine());
2109void ModuleBitcodeWriter::writeDIArgList(
const DIArgList *
N,
2111 Record.reserve(
N->getArgs().size());
2119void ModuleBitcodeWriter::writeDIModule(
const DIModule *
N,
2122 Record.push_back(
N->isDistinct());
2123 for (
auto &
I :
N->operands())
2125 Record.push_back(
N->getLineNo());
2126 Record.push_back(
N->getIsDecl());
2132void ModuleBitcodeWriter::writeDIAssignID(
const DIAssignID *
N,
2136 Record.push_back(
N->isDistinct());
2141void ModuleBitcodeWriter::writeDITemplateTypeParameter(
2144 Record.push_back(
N->isDistinct());
2147 Record.push_back(
N->isDefault());
2153void ModuleBitcodeWriter::writeDITemplateValueParameter(
2156 Record.push_back(
N->isDistinct());
2157 Record.push_back(
N->getTag());
2160 Record.push_back(
N->isDefault());
2167void ModuleBitcodeWriter::writeDIGlobalVariable(
2176 Record.push_back(
N->getLine());
2178 Record.push_back(
N->isLocalToUnit());
2179 Record.push_back(
N->isDefinition());
2182 Record.push_back(
N->getAlignInBits());
2189void ModuleBitcodeWriter::writeDILocalVariable(
2205 const uint64_t HasAlignmentFlag = 1 << 1;
2210 Record.push_back(
N->getLine());
2212 Record.push_back(
N->getArg());
2213 Record.push_back(
N->getFlags());
2214 Record.push_back(
N->getAlignInBits());
2221void ModuleBitcodeWriter::writeDILabel(
2228 Record.push_back(
N->getLine());
2234void ModuleBitcodeWriter::writeDIExpression(
const DIExpression *
N,
2237 Record.reserve(
N->getElements().size() + 1);
2240 Record.append(
N->elements_begin(),
N->elements_end());
2246void ModuleBitcodeWriter::writeDIGlobalVariableExpression(
2249 Record.push_back(
N->isDistinct());
2260 Record.push_back(
N->isDistinct());
2263 Record.push_back(
N->getLine());
2266 Record.push_back(
N->getAttributes());
2273void ModuleBitcodeWriter::writeDIImportedEntity(
2276 Record.push_back(
N->isDistinct());
2277 Record.push_back(
N->getTag());
2280 Record.push_back(
N->getLine());
2289unsigned ModuleBitcodeWriter::createNamedMetadataAbbrev() {
2290 auto Abbv = std::make_shared<BitCodeAbbrev>();
2297void ModuleBitcodeWriter::writeNamedMetadata(
2299 if (
M.named_metadata_empty())
2302 unsigned Abbrev = createNamedMetadataAbbrev();
2306 Record.append(Str.bytes_begin(), Str.bytes_end());
2311 for (
const MDNode *
N : NMD.operands())
2318unsigned ModuleBitcodeWriter::createMetadataStringsAbbrev() {
2319 auto Abbv = std::make_shared<BitCodeAbbrev>();
2331void ModuleBitcodeWriter::writeMetadataStrings(
2333 if (Strings.empty())
2338 Record.push_back(Strings.size());
2345 W.EmitVBR(cast<MDString>(MD)->getLength(), 6);
2354 Blob.
append(cast<MDString>(MD)->getString());
2363#define HANDLE_MDNODE_LEAF(CLASS) CLASS##AbbrevID,
2364#include "llvm/IR/Metadata.def"
2368void ModuleBitcodeWriter::writeMetadataRecords(
2370 std::vector<unsigned> *MDAbbrevs, std::vector<uint64_t> *IndexPos) {
2375#define HANDLE_MDNODE_LEAF(CLASS) unsigned CLASS##Abbrev = 0;
2376#include "llvm/IR/Metadata.def"
2381 if (
const MDNode *
N = dyn_cast<MDNode>(MD)) {
2382 assert(
N->isResolved() &&
"Expected forward references to be resolved");
2384 switch (
N->getMetadataID()) {
2387#define HANDLE_MDNODE_LEAF(CLASS) \
2388 case Metadata::CLASS##Kind: \
2390 write##CLASS(cast<CLASS>(N), Record, \
2391 (*MDAbbrevs)[MetadataAbbrev::CLASS##AbbrevID]); \
2393 write##CLASS(cast<CLASS>(N), Record, CLASS##Abbrev); \
2395#include "llvm/IR/Metadata.def"
2398 if (
auto *
AL = dyn_cast<DIArgList>(MD)) {
2402 writeValueAsMetadata(cast<ValueAsMetadata>(MD),
Record);
2406void ModuleBitcodeWriter::writeModuleMetadata() {
2407 if (!VE.
hasMDs() &&
M.named_metadata_empty())
2415 std::vector<unsigned> MDAbbrevs;
2418 MDAbbrevs[MetadataAbbrev::DILocationAbbrevID] = createDILocationAbbrev();
2419 MDAbbrevs[MetadataAbbrev::GenericDINodeAbbrevID] =
2420 createGenericDINodeAbbrev();
2422 auto Abbv = std::make_shared<BitCodeAbbrev>();
2426 unsigned OffsetAbbrev = Stream.
EmitAbbrev(std::move(Abbv));
2428 Abbv = std::make_shared<BitCodeAbbrev>();
2432 unsigned IndexAbbrev = Stream.
EmitAbbrev(std::move(Abbv));
2454 std::vector<uint64_t> IndexPos;
2469 uint64_t PreviousValue = IndexOffsetRecordBitPos;
2470 for (
auto &Elt : IndexPos) {
2471 auto EltDelta = Elt - PreviousValue;
2472 PreviousValue = Elt;
2481 writeNamedMetadata(
Record);
2483 auto AddDeclAttachedMetadata = [&](
const GlobalObject &GO) {
2486 pushGlobalMetadataAttachment(
Record, GO);
2490 if (
F.isDeclaration() &&
F.hasMetadata())
2491 AddDeclAttachedMetadata(
F);
2495 if (GV.hasMetadata())
2496 AddDeclAttachedMetadata(GV);
2501void ModuleBitcodeWriter::writeFunctionMetadata(
const Function &
F) {
2512void ModuleBitcodeWriter::pushGlobalMetadataAttachment(
2517 for (
const auto &
I : MDs) {
2523void ModuleBitcodeWriter::writeFunctionMetadataAttachment(
const Function &
F) {
2528 if (
F.hasMetadata()) {
2529 pushGlobalMetadataAttachment(
Record,
F);
2540 I.getAllMetadataOtherThanDebugLoc(MDs);
2543 if (MDs.
empty())
continue;
2547 for (
unsigned i = 0, e = MDs.
size(); i != e; ++i) {
2548 Record.push_back(MDs[i].first);
2558void ModuleBitcodeWriter::writeModuleMetadataKinds() {
2564 M.getMDKindNames(Names);
2566 if (Names.
empty())
return;
2570 for (
unsigned MDKindID = 0, e = Names.
size(); MDKindID != e; ++MDKindID) {
2571 Record.push_back(MDKindID);
2582void ModuleBitcodeWriter::writeOperandBundleTags() {
2590 M.getOperandBundleTags(Tags);
2599 for (
auto Tag : Tags) {
2609void ModuleBitcodeWriter::writeSyncScopeNames() {
2611 M.getContext().getSyncScopeNames(SSNs);
2618 for (
auto SSN : SSNs) {
2619 Record.append(SSN.begin(), SSN.end());
2627void ModuleBitcodeWriter::writeConstants(
unsigned FirstVal,
unsigned LastVal,
2629 if (FirstVal == LastVal)
return;
2633 unsigned AggregateAbbrev = 0;
2634 unsigned String8Abbrev = 0;
2635 unsigned CString7Abbrev = 0;
2636 unsigned CString6Abbrev = 0;
2640 auto Abbv = std::make_shared<BitCodeAbbrev>();
2644 AggregateAbbrev = Stream.
EmitAbbrev(std::move(Abbv));
2647 Abbv = std::make_shared<BitCodeAbbrev>();
2651 String8Abbrev = Stream.
EmitAbbrev(std::move(Abbv));
2653 Abbv = std::make_shared<BitCodeAbbrev>();
2657 CString7Abbrev = Stream.
EmitAbbrev(std::move(Abbv));
2659 Abbv = std::make_shared<BitCodeAbbrev>();
2663 CString6Abbrev = Stream.
EmitAbbrev(std::move(Abbv));
2669 Type *LastTy =
nullptr;
2670 for (
unsigned i = FirstVal; i != LastVal; ++i) {
2671 const Value *
V = Vals[i].first;
2673 if (
V->getType() != LastTy) {
2674 LastTy =
V->getType();
2677 CONSTANTS_SETTYPE_ABBREV);
2681 if (
const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
2684 unsigned(
IA->hasSideEffects()) |
unsigned(
IA->isAlignStack()) << 1 |
2685 unsigned(
IA->getDialect() & 1) << 2 |
unsigned(
IA->canThrow()) << 3);
2688 const std::string &AsmStr =
IA->getAsmString();
2689 Record.push_back(AsmStr.size());
2690 Record.append(AsmStr.begin(), AsmStr.end());
2693 const std::string &ConstraintStr =
IA->getConstraintString();
2694 Record.push_back(ConstraintStr.size());
2695 Record.append(ConstraintStr.begin(), ConstraintStr.end());
2701 unsigned Code = -1U;
2702 unsigned AbbrevToUse = 0;
2703 if (
C->isNullValue()) {
2705 }
else if (isa<PoisonValue>(
C)) {
2707 }
else if (isa<UndefValue>(
C)) {
2710 if (
IV->getBitWidth() <= 64) {
2714 AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
2719 }
else if (
const ConstantFP *CFP = dyn_cast<ConstantFP>(
C)) {
2724 Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
2728 APInt api = CFP->getValueAPF().bitcastToAPInt();
2730 Record.push_back((p[1] << 48) | (p[0] >> 16));
2731 Record.push_back(p[0] & 0xffffLL);
2733 APInt api = CFP->getValueAPF().bitcastToAPInt();
2738 assert(0 &&
"Unknown FP type!");
2740 }
else if (isa<ConstantDataSequential>(
C) &&
2741 cast<ConstantDataSequential>(
C)->isString()) {
2744 unsigned NumElts = Str->getNumElements();
2746 if (Str->isCString()) {
2751 AbbrevToUse = String8Abbrev;
2755 for (
unsigned i = 0; i != NumElts; ++i) {
2756 unsigned char V = Str->getElementAsInteger(i);
2758 isCStr7 &= (
V & 128) == 0;
2764 AbbrevToUse = CString6Abbrev;
2766 AbbrevToUse = CString7Abbrev;
2768 dyn_cast<ConstantDataSequential>(
C)) {
2770 Type *EltTy = CDS->getElementType();
2771 if (isa<IntegerType>(EltTy)) {
2772 for (
unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
2773 Record.push_back(CDS->getElementAsInteger(i));
2775 for (
unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
2777 CDS->getElementAsAPFloat(i).bitcastToAPInt().getLimitedValue());
2779 }
else if (isa<ConstantAggregate>(
C)) {
2781 for (
const Value *
Op :
C->operands())
2783 AbbrevToUse = AggregateAbbrev;
2784 }
else if (
const ConstantExpr *CE = dyn_cast<ConstantExpr>(
C)) {
2785 switch (
CE->getOpcode()) {
2792 AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
2794 assert(
CE->getNumOperands() == 2 &&
"Unknown constant expr!");
2804 case Instruction::FNeg: {
2805 assert(
CE->getNumOperands() == 1 &&
"Unknown constant expr!");
2814 case Instruction::GetElementPtr: {
2816 const auto *GO = cast<GEPOperator>(
C);
2819 if (std::optional<ConstantRange>
Range = GO->getInRange()) {
2823 for (
unsigned i = 0, e =
CE->getNumOperands(); i != e; ++i) {
2829 case Instruction::ExtractElement:
2836 case Instruction::InsertElement:
2843 case Instruction::ShuffleVector:
2848 if (
C->getType() ==
C->getOperand(0)->getType()) {
2859 }
else if (
const BlockAddress *BA = dyn_cast<BlockAddress>(
C)) {
2864 }
else if (
const auto *Equiv = dyn_cast<DSOLocalEquivalent>(
C)) {
2868 }
else if (
const auto *
NC = dyn_cast<NoCFIValue>(
C)) {
2872 }
else if (
const auto *CPA = dyn_cast<ConstantPtrAuth>(
C)) {
2891void ModuleBitcodeWriter::writeModuleConstants() {
2896 for (
unsigned i = 0, e = Vals.size(); i != e; ++i) {
2897 if (!isa<GlobalValue>(Vals[i].first)) {
2898 writeConstants(i, Vals.size(),
true);
2912bool ModuleBitcodeWriter::pushValueAndType(
const Value *V,
unsigned InstID,
2917 if (
ValID >= InstID) {
2924void ModuleBitcodeWriter::writeOperandBundles(
const CallBase &CS,
2931 Record.push_back(
C.getOperandBundleTagID(Bundle.getTagName()));
2933 for (
auto &Input : Bundle.Inputs)
2934 pushValueAndType(Input, InstID,
Record);
2943void ModuleBitcodeWriter::pushValue(
const Value *V,
unsigned InstID,
2949void ModuleBitcodeWriter::pushValueSigned(
const Value *V,
unsigned InstID,
2952 int64_t diff = ((int32_t)InstID - (int32_t)
ValID);
2957void ModuleBitcodeWriter::writeInstruction(
const Instruction &
I,
2961 unsigned AbbrevToUse = 0;
2963 switch (
I.getOpcode()) {
2967 if (!pushValueAndType(
I.getOperand(0), InstID, Vals))
2968 AbbrevToUse = FUNCTION_INST_CAST_ABBREV;
2973 if (AbbrevToUse == FUNCTION_INST_CAST_ABBREV)
2974 AbbrevToUse = FUNCTION_INST_CAST_FLAGS_ABBREV;
2978 assert(isa<BinaryOperator>(
I) &&
"Unknown instruction!");
2980 if (!pushValueAndType(
I.getOperand(0), InstID, Vals))
2981 AbbrevToUse = FUNCTION_INST_BINOP_ABBREV;
2982 pushValue(
I.getOperand(1), InstID, Vals);
2986 if (AbbrevToUse == FUNCTION_INST_BINOP_ABBREV)
2987 AbbrevToUse = FUNCTION_INST_BINOP_FLAGS_ABBREV;
2992 case Instruction::FNeg: {
2994 if (!pushValueAndType(
I.getOperand(0), InstID, Vals))
2995 AbbrevToUse = FUNCTION_INST_UNOP_ABBREV;
2999 if (AbbrevToUse == FUNCTION_INST_UNOP_ABBREV)
3000 AbbrevToUse = FUNCTION_INST_UNOP_FLAGS_ABBREV;
3005 case Instruction::GetElementPtr: {
3007 AbbrevToUse = FUNCTION_INST_GEP_ABBREV;
3008 auto &GEPInst = cast<GetElementPtrInst>(
I);
3011 for (
unsigned i = 0, e =
I.getNumOperands(); i != e; ++i)
3012 pushValueAndType(
I.getOperand(i), InstID, Vals);
3015 case Instruction::ExtractValue: {
3017 pushValueAndType(
I.getOperand(0), InstID, Vals);
3022 case Instruction::InsertValue: {
3024 pushValueAndType(
I.getOperand(0), InstID, Vals);
3025 pushValueAndType(
I.getOperand(1), InstID, Vals);
3030 case Instruction::Select: {
3032 pushValueAndType(
I.getOperand(1), InstID, Vals);
3033 pushValue(
I.getOperand(2), InstID, Vals);
3034 pushValueAndType(
I.getOperand(0), InstID, Vals);
3040 case Instruction::ExtractElement:
3042 pushValueAndType(
I.getOperand(0), InstID, Vals);
3043 pushValueAndType(
I.getOperand(1), InstID, Vals);
3045 case Instruction::InsertElement:
3047 pushValueAndType(
I.getOperand(0), InstID, Vals);
3048 pushValue(
I.getOperand(1), InstID, Vals);
3049 pushValueAndType(
I.getOperand(2), InstID, Vals);
3051 case Instruction::ShuffleVector:
3053 pushValueAndType(
I.getOperand(0), InstID, Vals);
3054 pushValue(
I.getOperand(1), InstID, Vals);
3055 pushValue(cast<ShuffleVectorInst>(
I).getShuffleMaskForBitcode(), InstID,
3058 case Instruction::ICmp:
3059 case Instruction::FCmp: {
3062 pushValueAndType(
I.getOperand(0), InstID, Vals);
3063 pushValue(
I.getOperand(1), InstID, Vals);
3071 case Instruction::Ret:
3074 unsigned NumOperands =
I.getNumOperands();
3075 if (NumOperands == 0)
3076 AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV;
3077 else if (NumOperands == 1) {
3078 if (!pushValueAndType(
I.getOperand(0), InstID, Vals))
3079 AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV;
3081 for (
unsigned i = 0, e = NumOperands; i !=
e; ++i)
3082 pushValueAndType(
I.getOperand(i), InstID, Vals);
3086 case Instruction::Br:
3091 if (
II.isConditional()) {
3093 pushValue(
II.getCondition(), InstID, Vals);
3097 case Instruction::Switch:
3102 pushValue(
SI.getCondition(), InstID, Vals);
3104 for (
auto Case :
SI.cases()) {
3110 case Instruction::IndirectBr:
3114 pushValue(
I.getOperand(0), InstID, Vals);
3115 for (
unsigned i = 1, e =
I.getNumOperands(); i != e; ++i)
3119 case Instruction::Invoke: {
3124 if (
II->hasOperandBundles())
3125 writeOperandBundles(*
II, InstID);
3134 pushValueAndType(Callee, InstID, Vals);
3137 for (
unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
3138 pushValue(
I.getOperand(i), InstID, Vals);
3141 if (FTy->isVarArg()) {
3142 for (
unsigned i = FTy->getNumParams(), e =
II->arg_size(); i != e; ++i)
3143 pushValueAndType(
I.getOperand(i), InstID, Vals);
3147 case Instruction::Resume:
3149 pushValueAndType(
I.getOperand(0), InstID, Vals);
3151 case Instruction::CleanupRet: {
3153 const auto &CRI = cast<CleanupReturnInst>(
I);
3154 pushValue(CRI.getCleanupPad(), InstID, Vals);
3155 if (CRI.hasUnwindDest())
3159 case Instruction::CatchRet: {
3161 const auto &CRI = cast<CatchReturnInst>(
I);
3162 pushValue(CRI.getCatchPad(), InstID, Vals);
3166 case Instruction::CleanupPad:
3167 case Instruction::CatchPad: {
3168 const auto &FuncletPad = cast<FuncletPadInst>(
I);
3171 pushValue(FuncletPad.getParentPad(), InstID, Vals);
3173 unsigned NumArgOperands = FuncletPad.arg_size();
3175 for (
unsigned Op = 0;
Op != NumArgOperands; ++
Op)
3176 pushValueAndType(FuncletPad.getArgOperand(
Op), InstID, Vals);
3179 case Instruction::CatchSwitch: {
3181 const auto &CatchSwitch = cast<CatchSwitchInst>(
I);
3183 pushValue(CatchSwitch.getParentPad(), InstID, Vals);
3185 unsigned NumHandlers = CatchSwitch.getNumHandlers();
3187 for (
const BasicBlock *CatchPadBB : CatchSwitch.handlers())
3190 if (CatchSwitch.hasUnwindDest())
3194 case Instruction::CallBr: {
3200 writeOperandBundles(*CBI, InstID);
3215 pushValueAndType(Callee, InstID, Vals);
3218 for (
unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
3219 pushValue(
I.getOperand(i), InstID, Vals);
3222 if (FTy->isVarArg()) {
3223 for (
unsigned i = FTy->getNumParams(), e = CBI->
arg_size(); i != e; ++i)
3224 pushValueAndType(
I.getOperand(i), InstID, Vals);
3228 case Instruction::Unreachable:
3230 AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV;
3233 case Instruction::PHI: {
3234 const PHINode &PN = cast<PHINode>(
I);
3251 Stream.
EmitRecord(Code, Vals64, AbbrevToUse);
3256 case Instruction::LandingPad: {
3267 pushValueAndType(LP.
getClause(
I), InstID, Vals);
3272 case Instruction::Alloca: {
3280 unsigned EncodedAlign = getEncodedAlign(AI.
getAlign());
3281 Bitfield::set<APV::AlignLower>(
3282 Record, EncodedAlign & ((1 << APV::AlignLower::Bits) - 1));
3283 Bitfield::set<APV::AlignUpper>(
Record,
3284 EncodedAlign >> APV::AlignLower::Bits);
3286 Bitfield::set<APV::ExplicitType>(
Record,
true);
3291 if (AS !=
M.getDataLayout().getAllocaAddrSpace())
3296 case Instruction::Load:
3297 if (cast<LoadInst>(
I).isAtomic()) {
3299 pushValueAndType(
I.getOperand(0), InstID, Vals);
3302 if (!pushValueAndType(
I.getOperand(0), InstID, Vals))
3303 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV;
3307 Vals.
push_back(cast<LoadInst>(
I).isVolatile());
3308 if (cast<LoadInst>(
I).isAtomic()) {
3310 Vals.
push_back(getEncodedSyncScopeID(cast<LoadInst>(
I).getSyncScopeID()));
3313 case Instruction::Store:
3314 if (cast<StoreInst>(
I).isAtomic())
3318 pushValueAndType(
I.getOperand(1), InstID, Vals);
3319 pushValueAndType(
I.getOperand(0), InstID, Vals);
3321 Vals.
push_back(cast<StoreInst>(
I).isVolatile());
3322 if (cast<StoreInst>(
I).isAtomic()) {
3325 getEncodedSyncScopeID(cast<StoreInst>(
I).getSyncScopeID()));
3328 case Instruction::AtomicCmpXchg:
3330 pushValueAndType(
I.getOperand(0), InstID, Vals);
3331 pushValueAndType(
I.getOperand(1), InstID, Vals);
3332 pushValue(
I.getOperand(2), InstID, Vals);
3333 Vals.
push_back(cast<AtomicCmpXchgInst>(
I).isVolatile());
3337 getEncodedSyncScopeID(cast<AtomicCmpXchgInst>(
I).getSyncScopeID()));
3340 Vals.
push_back(cast<AtomicCmpXchgInst>(
I).isWeak());
3343 case Instruction::AtomicRMW:
3345 pushValueAndType(
I.getOperand(0), InstID, Vals);
3346 pushValueAndType(
I.getOperand(1), InstID, Vals);
3349 Vals.
push_back(cast<AtomicRMWInst>(
I).isVolatile());
3352 getEncodedSyncScopeID(cast<AtomicRMWInst>(
I).getSyncScopeID()));
3355 case Instruction::Fence:
3358 Vals.
push_back(getEncodedSyncScopeID(cast<FenceInst>(
I).getSyncScopeID()));
3360 case Instruction::Call: {
3365 writeOperandBundles(CI, InstID);
3385 for (
unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
3387 if (FTy->getParamType(i)->isLabelTy())
3394 if (FTy->isVarArg()) {
3395 for (
unsigned i = FTy->getNumParams(), e = CI.
arg_size(); i != e; ++i)
3400 case Instruction::VAArg:
3403 pushValue(
I.getOperand(0), InstID, Vals);
3406 case Instruction::Freeze:
3408 pushValueAndType(
I.getOperand(0), InstID, Vals);
3418void ModuleBitcodeWriter::writeGlobalValueSymbolTable(
3424 VSTOffset -= bitcodeStartBit();
3425 assert((VSTOffset & 31) == 0 &&
"VST block not 32-bit aligned");
3429 Stream.
BackpatchWord(VSTOffsetPlaceholder, VSTOffset / 32 + 1);
3433 auto Abbv = std::make_shared<BitCodeAbbrev>();
3437 unsigned FnEntryAbbrev = Stream.
EmitAbbrev(std::move(Abbv));
3442 if (
F.isDeclaration())
3449 uint64_t BitcodeIndex = FunctionToBitcodeIndex[&
F] - bitcodeStartBit();
3450 assert((BitcodeIndex & 31) == 0 &&
"function block not 32-bit aligned");
3454 Record[1] = BitcodeIndex / 32 + 1;
3463void ModuleBitcodeWriter::writeFunctionLevelValueSymbolTable(
3478 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
3484 if (isa<BasicBlock>(
Name.getValue())) {
3487 AbbrevToUse = VST_BBENTRY_6_ABBREV;
3491 AbbrevToUse = VST_ENTRY_6_ABBREV;
3493 AbbrevToUse = VST_ENTRY_7_ABBREV;
3496 for (
const auto P :
Name.getKey())
3500 Stream.
EmitRecord(Code, NameVals, AbbrevToUse);
3507void ModuleBitcodeWriter::writeUseList(
UseListOrder &&Order) {
3508 assert(Order.Shuffle.size() >= 2 &&
"Shuffle too small");
3510 if (isa<BasicBlock>(Order.V))
3520void ModuleBitcodeWriter::writeUseListBlock(
const Function *
F) {
3522 "Expected to be preserving use-list order");
3524 auto hasMore = [&]() {
3540void ModuleBitcodeWriter::writeFunction(
3559 unsigned CstStart, CstEnd;
3561 writeConstants(CstStart, CstEnd,
false);
3564 writeFunctionMetadata(
F);
3567 unsigned InstID = CstEnd;
3569 bool NeedsMetadataAttachment =
F.hasMetadata();
3577 writeInstruction(
I, InstID, Vals);
3579 if (!
I.getType()->isVoidTy())
3583 NeedsMetadataAttachment |=
I.hasMetadataOtherThanDebugLoc();
3610 auto PushValueOrMetadata = [&Vals, InstID,
3613 "RawLocation unexpectedly null in DbgVariableRecord");
3619 if (!pushValueAndType(VAM->getValue(), InstID, ValAndType)) {
3633 for (
DbgRecord &DR :
I.DebugMarker->getDbgRecordRange()) {
3659 FUNCTION_DEBUG_RECORD_VALUE_ABBREV);
3681 while (!Worklist.empty()) {
3682 Value *
V = Worklist.pop_back_val();
3683 for (
User *U :
V->users()) {
3684 if (
auto *
I = dyn_cast<Instruction>(U)) {
3688 }
else if (isa<Constant>(U) && !isa<GlobalValue>(U) &&
3689 Visited.insert(U).second)
3690 Worklist.push_back(U);
3696 if (!BlockAddressUsers.
empty()) {
3705 if (
auto *Symtab =
F.getValueSymbolTable())
3706 writeFunctionLevelValueSymbolTable(*Symtab);
3708 if (NeedsMetadataAttachment)
3709 writeFunctionMetadataAttachment(
F);
3711 writeUseListBlock(&
F);
3717void ModuleBitcodeWriter::writeBlockInfo() {
3724 auto Abbv = std::make_shared<BitCodeAbbrev>();
3735 auto Abbv = std::make_shared<BitCodeAbbrev>();
3745 auto Abbv = std::make_shared<BitCodeAbbrev>();
3755 auto Abbv = std::make_shared<BitCodeAbbrev>();
3761 VST_BBENTRY_6_ABBREV)
3766 auto Abbv = std::make_shared<BitCodeAbbrev>();
3771 CONSTANTS_SETTYPE_ABBREV)
3776 auto Abbv = std::make_shared<BitCodeAbbrev>();
3780 CONSTANTS_INTEGER_ABBREV)
3785 auto Abbv = std::make_shared<BitCodeAbbrev>();
3793 CONSTANTS_CE_CAST_Abbrev)
3797 auto Abbv = std::make_shared<BitCodeAbbrev>();
3800 CONSTANTS_NULL_Abbrev)
3807 auto Abbv = std::make_shared<BitCodeAbbrev>();
3815 FUNCTION_INST_LOAD_ABBREV)
3819 auto Abbv = std::make_shared<BitCodeAbbrev>();
3824 FUNCTION_INST_UNOP_ABBREV)
3828 auto Abbv = std::make_shared<BitCodeAbbrev>();
3834 FUNCTION_INST_UNOP_FLAGS_ABBREV)
3838 auto Abbv = std::make_shared<BitCodeAbbrev>();
3844 FUNCTION_INST_BINOP_ABBREV)
3848 auto Abbv = std::make_shared<BitCodeAbbrev>();
3855 FUNCTION_INST_BINOP_FLAGS_ABBREV)
3859 auto Abbv = std::make_shared<BitCodeAbbrev>();
3866 FUNCTION_INST_CAST_ABBREV)
3870 auto Abbv = std::make_shared<BitCodeAbbrev>();
3878 FUNCTION_INST_CAST_FLAGS_ABBREV)
3883 auto Abbv = std::make_shared<BitCodeAbbrev>();
3886 FUNCTION_INST_RET_VOID_ABBREV)
3890 auto Abbv = std::make_shared<BitCodeAbbrev>();
3894 FUNCTION_INST_RET_VAL_ABBREV)
3898 auto Abbv = std::make_shared<BitCodeAbbrev>();
3901 FUNCTION_INST_UNREACHABLE_ABBREV)
3905 auto Abbv = std::make_shared<BitCodeAbbrev>();
3913 FUNCTION_INST_GEP_ABBREV)
3917 auto Abbv = std::make_shared<BitCodeAbbrev>();
3924 FUNCTION_DEBUG_RECORD_VALUE_ABBREV)
3932void IndexBitcodeWriter::writeModStrings() {
3938 auto Abbv = std::make_shared<BitCodeAbbrev>();
3943 unsigned Abbrev8Bit = Stream.
EmitAbbrev(std::move(Abbv));
3946 Abbv = std::make_shared<BitCodeAbbrev>();
3951 unsigned Abbrev7Bit = Stream.
EmitAbbrev(std::move(Abbv));
3954 Abbv = std::make_shared<BitCodeAbbrev>();
3959 unsigned Abbrev6Bit = Stream.
EmitAbbrev(std::move(Abbv));
3962 Abbv = std::make_shared<BitCodeAbbrev>();
3969 unsigned AbbrevHash = Stream.
EmitAbbrev(std::move(Abbv));
3974 const auto &Hash = MPSE.
getValue();
3976 unsigned AbbrevToUse = Abbrev8Bit;
3978 AbbrevToUse = Abbrev6Bit;
3980 AbbrevToUse = Abbrev7Bit;
3982 auto ModuleId = ModuleIdMap.
size();
3983 ModuleIdMap[
Key] = ModuleId;
3992 Vals.
assign(Hash.begin(), Hash.end());
4004template <
typename Fn>
4008 if (!FS->type_tests().empty())
4013 auto WriteVFuncIdVec = [&](
uint64_t Ty,
4018 for (
auto &VF : VFs) {
4019 Record.push_back(VF.GUID);
4020 Record.push_back(VF.Offset);
4026 FS->type_test_assume_vcalls());
4028 FS->type_checked_load_vcalls());
4030 auto WriteConstVCallVec = [&](
uint64_t Ty,
4032 for (
auto &VC : VCs) {
4034 Record.push_back(VC.VFunc.GUID);
4035 Record.push_back(VC.VFunc.Offset);
4042 FS->type_test_assume_const_vcalls());
4044 FS->type_checked_load_const_vcalls());
4054 if (!FS->paramAccesses().empty()) {
4056 for (
auto &Arg : FS->paramAccesses()) {
4057 size_t UndoSize =
Record.size();
4058 Record.push_back(Arg.ParamNo);
4059 WriteRange(Arg.Use);
4060 Record.push_back(Arg.Calls.size());
4061 for (
auto &Call : Arg.Calls) {
4062 Record.push_back(Call.ParamNo);
4063 std::optional<unsigned> ValueID = GetValueID(Call.Callee);
4070 Record.push_back(*ValueID);
4071 WriteRange(Call.Offsets);
4082 std::set<GlobalValue::GUID> &ReferencedTypeIds) {
4083 if (!FS->type_tests().empty())
4084 for (
auto &TT : FS->type_tests())
4085 ReferencedTypeIds.insert(TT);
4087 auto GetReferencedTypesFromVFuncIdVec =
4089 for (
auto &VF : VFs)
4090 ReferencedTypeIds.insert(VF.GUID);
4093 GetReferencedTypesFromVFuncIdVec(FS->type_test_assume_vcalls());
4094 GetReferencedTypesFromVFuncIdVec(FS->type_checked_load_vcalls());
4096 auto GetReferencedTypesFromConstVCallVec =
4098 for (
auto &VC : VCs)
4099 ReferencedTypeIds.insert(VC.VFunc.GUID);
4102 GetReferencedTypesFromConstVCallVec(FS->type_test_assume_const_vcalls());
4103 GetReferencedTypesFromConstVCallVec(FS->type_checked_load_const_vcalls());
4134 const std::string &Id,
4139 NameVals.
push_back(Summary.TTRes.TheKind);
4140 NameVals.
push_back(Summary.TTRes.SizeM1BitWidth);
4141 NameVals.
push_back(Summary.TTRes.AlignLog2);
4142 NameVals.
push_back(Summary.TTRes.SizeM1);
4143 NameVals.
push_back(Summary.TTRes.BitMask);
4144 NameVals.
push_back(Summary.TTRes.InlineBits);
4146 for (
auto &W : Summary.WPDRes)
4158 for (
auto &
P : Summary) {
4166 unsigned AllocAbbrev,
bool PerModule,
4167 std::function<
unsigned(
const ValueInfo &VI)> GetValueID,
4168 std::function<
unsigned(
unsigned)> GetStackIndex) {
4171 for (
auto &CI : FS->callsites()) {
4175 assert(!PerModule || (CI.Clones.size() == 1 && CI.Clones[0] == 0));
4176 Record.push_back(GetValueID(CI.Callee));
4178 Record.push_back(CI.StackIdIndices.size());
4179 Record.push_back(CI.Clones.size());
4181 for (
auto Id : CI.StackIdIndices)
4182 Record.push_back(GetStackIndex(Id));
4184 for (
auto V : CI.Clones)
4192 for (
auto &AI : FS->allocs()) {
4196 assert(!PerModule || (AI.Versions.size() == 1 && AI.Versions[0] == 0));
4197 Record.push_back(AI.MIBs.size());
4199 Record.push_back(AI.Versions.size());
4200 for (
auto &MIB : AI.MIBs) {
4201 Record.push_back((uint8_t)MIB.AllocType);
4202 Record.push_back(MIB.StackIdIndices.size());
4203 for (
auto Id : MIB.StackIdIndices)
4204 Record.push_back(GetStackIndex(Id));
4207 for (
auto V : AI.Versions)
4210 assert(AI.TotalSizes.empty() || AI.TotalSizes.size() == AI.MIBs.size());
4211 if (!AI.TotalSizes.empty()) {
4212 for (
auto Size : AI.TotalSizes)
4222void ModuleBitcodeWriterBase::writePerModuleFunctionSummaryRecord(
4224 unsigned ValueID,
unsigned FSCallsRelBFAbbrev,
4225 unsigned FSCallsProfileAbbrev,
unsigned CallsiteAbbrev,
4226 unsigned AllocAbbrev,
const Function &
F) {
4232 Stream, FS, [&](
const ValueInfo &VI) -> std::optional<unsigned> {
4237 Stream, FS, CallsiteAbbrev, AllocAbbrev,
4239 [&](
const ValueInfo &VI) {
return getValueId(VI); },
4240 [&](
unsigned I) {
return I; });
4242 auto SpecialRefCnts =
FS->specialRefCounts();
4247 NameVals.
push_back(SpecialRefCnts.first);
4248 NameVals.
push_back(SpecialRefCnts.second);
4250 for (
auto &RI :
FS->refs())
4253 const bool UseRelBFRecord =
4256 for (
auto &ECI :
FS->calls()) {
4257 NameVals.
push_back(getValueId(ECI.first));
4265 (UseRelBFRecord ? FSCallsRelBFAbbrev : FSCallsProfileAbbrev);
4276void ModuleBitcodeWriterBase::writeModuleLevelReferences(
4278 unsigned FSModRefsAbbrev,
unsigned FSModVTableRefsAbbrev) {
4279 auto VI =
Index->getValueInfo(
V.getGUID());
4280 if (!VI ||
VI.getSummaryList().empty()) {
4286 auto *
Summary =
VI.getSummaryList()[0].get();
4292 auto VTableFuncs =
VS->vTableFuncs();
4293 if (!VTableFuncs.empty())
4296 unsigned SizeBeforeRefs = NameVals.
size();
4297 for (
auto &RI :
VS->refs())
4303 if (VTableFuncs.empty())
4308 for (
auto &
P : VTableFuncs) {
4314 FSModVTableRefsAbbrev);
4321void ModuleBitcodeWriterBase::writePerModuleGlobalValueSummary() {
4324 bool IsThinLTO =
true;
4326 mdconst::extract_or_null<ConstantInt>(
M.getModuleFlag(
"ThinLTO")))
4327 IsThinLTO = MD->getZExtValue();
4339 if (
Index->enableSplitLTOUnit())
4341 if (
Index->hasUnifiedLTO())
4351 for (
const auto &GVI : valueIds()) {
4356 if (!
Index->stackIds().empty()) {
4357 auto StackIdAbbv = std::make_shared<BitCodeAbbrev>();
4362 unsigned StackIdAbbvId = Stream.
EmitAbbrev(std::move(StackIdAbbv));
4367 auto Abbv = std::make_shared<BitCodeAbbrev>();