LLVM: lib/Bitcode/Writer/BitcodeWriter.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
33#include "llvm/Config/llvm-config.h"
78#include
79#include
80#include
81#include
82#include
83#include
84#include
85#include
86#include
87#include
88#include
89
90using namespace llvm;
92
95 cl::desc("Number of metadatas above which we emit an index "
96 "to enable lazy-loading"));
99 cl::desc("The threshold (unit M) for flushing LLVM bitcode."));
100
103 cl::desc("Write relative block frequency to function summary "));
104
105
106
107
108
109
110
111
116#else
118#endif
120
123 cl::desc("Preserve use-list order when writing LLVM bitcode."));
124
125namespace llvm {
127}
128
129namespace {
130
131
132
133enum {
134
136 VST_ENTRY_7_ABBREV,
137 VST_ENTRY_6_ABBREV,
138 VST_BBENTRY_6_ABBREV,
139
140
142 CONSTANTS_INTEGER_ABBREV,
143 CONSTANTS_CE_CAST_Abbrev,
144 CONSTANTS_NULL_Abbrev,
145
146
148 FUNCTION_INST_STORE_ABBREV,
149 FUNCTION_INST_UNOP_ABBREV,
150 FUNCTION_INST_UNOP_FLAGS_ABBREV,
151 FUNCTION_INST_BINOP_ABBREV,
152 FUNCTION_INST_BINOP_FLAGS_ABBREV,
153 FUNCTION_INST_CAST_ABBREV,
154 FUNCTION_INST_CAST_FLAGS_ABBREV,
155 FUNCTION_INST_RET_VOID_ABBREV,
156 FUNCTION_INST_RET_VAL_ABBREV,
157 FUNCTION_INST_BR_UNCOND_ABBREV,
158 FUNCTION_INST_BR_COND_ABBREV,
159 FUNCTION_INST_UNREACHABLE_ABBREV,
160 FUNCTION_INST_GEP_ABBREV,
161 FUNCTION_INST_CMP_ABBREV,
162 FUNCTION_INST_CMP_FLAGS_ABBREV,
163 FUNCTION_DEBUG_RECORD_VALUE_ABBREV,
164 FUNCTION_DEBUG_LOC_ABBREV,
165};
166
167
168
169class BitcodeWriterBase {
170protected:
171
172 BitstreamWriter &Stream;
173
174 StringTableBuilder &StrtabBuilder;
175
176public:
177
178
179 BitcodeWriterBase(BitstreamWriter &Stream, StringTableBuilder &StrtabBuilder)
180 : Stream(Stream), StrtabBuilder(StrtabBuilder) {}
181
182protected:
183 void writeModuleVersion();
184};
185
186void BitcodeWriterBase::writeModuleVersion() {
187
189}
190
191
192
193class ModuleBitcodeWriterBase : public BitcodeWriterBase {
194protected:
195
197
198
199 ValueEnumerator VE;
200
201
202 const ModuleSummaryIndex *Index;
203
204
205
206
207 std::map<GlobalValue::GUID, unsigned> GUIDToValueIdMap;
208
209
210 unsigned GlobalValueId;
211
212
213
214 uint64_t VSTOffsetPlaceholder = 0;
215
216public:
217
218
219 ModuleBitcodeWriterBase(const Module &M, StringTableBuilder &StrtabBuilder,
220 BitstreamWriter &Stream,
221 bool ShouldPreserveUseListOrder,
222 const ModuleSummaryIndex *Index)
223 : BitcodeWriterBase(Stream, StrtabBuilder), M(M),
226 : ShouldPreserveUseListOrder),
228
229
230
231
232
234 if (!Index)
235 return;
236 for (const auto &GUIDSummaryLists : *Index)
237
238 for (auto &Summary : GUIDSummaryLists.second.getSummaryList())
240
241
242
243
244 for (auto &CallEdge : FS->calls())
245 if (!CallEdge.first.haveGVs() || !CallEdge.first.getValue())
246 assignValueId(CallEdge.first.getGUID());
247
248
249
250
251
252 for (auto &RefEdge : FS->refs())
253 if (!RefEdge.haveGVs() || !RefEdge.getValue())
254 assignValueId(RefEdge.getGUID());
255 }
256 }
257
258protected:
259 void writePerModuleGlobalValueSummary();
260
261private:
262 void writePerModuleFunctionSummaryRecord(
263 SmallVector<uint64_t, 64> &NameVals, GlobalValueSummary *Summary,
264 unsigned ValueID, unsigned FSCallsAbbrev, unsigned FSCallsProfileAbbrev,
265 unsigned CallsiteAbbrev, unsigned AllocAbbrev, unsigned ContextIdAbbvId,
266 const Function &F, DenseMap<CallStackId, LinearCallStackId> &CallStackPos,
268 void writeModuleLevelReferences(const GlobalVariable &V,
269 SmallVector<uint64_t, 64> &NameVals,
270 unsigned FSModRefsAbbrev,
271 unsigned FSModVTableRefsAbbrev);
272
274 GUIDToValueIdMap[ValGUID] = ++GlobalValueId;
275 }
276
278 const auto &VMI = GUIDToValueIdMap.find(ValGUID);
279
280
281 assert(VMI != GUIDToValueIdMap.end() &&
282 "GUID does not have assigned value Id");
283 return VMI->second;
284 }
285
286
287 unsigned getValueId(ValueInfo VI) {
288 if (.haveGVs() ||
.getValue())
289 return getValueId(VI.getGUID());
291 }
292
293 std::map<GlobalValue::GUID, unsigned> &valueIds() { return GUIDToValueIdMap; }
294};
295
296
297class ModuleBitcodeWriter : public ModuleBitcodeWriterBase {
298
299 bool GenerateHash;
300
301
302
304
306
307
308 uint64_t BitcodeStartBit;
309
310public:
311
312
313 ModuleBitcodeWriter(const Module &M, StringTableBuilder &StrtabBuilder,
314 BitstreamWriter &Stream, bool ShouldPreserveUseListOrder,
315 const ModuleSummaryIndex *Index, bool GenerateHash,
317 : ModuleBitcodeWriterBase(M, StrtabBuilder, Stream,
318 ShouldPreserveUseListOrder, Index),
319 GenerateHash(GenerateHash), ModHash(ModHash),
320 BitcodeStartBit(Stream.GetCurrentBitNo()) {}
321
322
324
325private:
326 uint64_t bitcodeStartBit() { return BitcodeStartBit; }
327
328 size_t addToStrtab(StringRef Str);
329
330 void writeAttributeGroupTable();
331 void writeAttributeTable();
332 void writeTypeTable();
333 void writeComdats();
334 void writeValueSymbolTableForwardDecl();
335 void writeModuleInfo();
336 void writeValueAsMetadata(const ValueAsMetadata *MD,
337 SmallVectorImpl<uint64_t> &Record);
338 void writeMDTuple(const MDTuple *N, SmallVectorImpl<uint64_t> &Record,
339 unsigned Abbrev);
340 unsigned createDILocationAbbrev();
341 void writeDILocation(const DILocation *N, SmallVectorImpl<uint64_t> &Record,
342 unsigned &Abbrev);
343 unsigned createGenericDINodeAbbrev();
345 SmallVectorImpl<uint64_t> &Record, unsigned &Abbrev);
346 void writeDISubrange(const DISubrange *N, SmallVectorImpl<uint64_t> &Record,
347 unsigned Abbrev);
349 SmallVectorImpl<uint64_t> &Record,
350 unsigned Abbrev);
352 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
353 void writeDIBasicType(const DIBasicType *N, SmallVectorImpl<uint64_t> &Record,
354 unsigned Abbrev);
356 SmallVectorImpl<uint64_t> &Record,
357 unsigned Abbrev);
359 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
361 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
363 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
365 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
367 SmallVectorImpl<uint64_t> &Record,
368 unsigned Abbrev);
369 void writeDIFile(const DIFile *N, SmallVectorImpl<uint64_t> &Record,
370 unsigned Abbrev);
372 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
374 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
376 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
378 SmallVectorImpl<uint64_t> &Record,
379 unsigned Abbrev);
381 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
382 void writeDINamespace(const DINamespace *N, SmallVectorImpl<uint64_t> &Record,
383 unsigned Abbrev);
384 void writeDIMacro(const DIMacro *N, SmallVectorImpl<uint64_t> &Record,
385 unsigned Abbrev);
386 void writeDIMacroFile(const DIMacroFile *N, SmallVectorImpl<uint64_t> &Record,
387 unsigned Abbrev);
388 void writeDIArgList(const DIArgList *N, SmallVectorImpl<uint64_t> &Record);
389 void writeDIModule(const DIModule *N, SmallVectorImpl<uint64_t> &Record,
390 unsigned Abbrev);
391 void writeDIAssignID(const DIAssignID *N, SmallVectorImpl<uint64_t> &Record,
392 unsigned Abbrev);
394 SmallVectorImpl<uint64_t> &Record,
395 unsigned Abbrev);
397 SmallVectorImpl<uint64_t> &Record,
398 unsigned Abbrev);
400 SmallVectorImpl<uint64_t> &Record,
401 unsigned Abbrev);
403 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
405 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
407 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
409 SmallVectorImpl<uint64_t> &Record,
410 unsigned Abbrev);
412 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
414 SmallVectorImpl<uint64_t> &Record,
415 unsigned Abbrev);
416 unsigned createNamedMetadataAbbrev();
417 void writeNamedMetadata(SmallVectorImpl<uint64_t> &Record);
418 unsigned createMetadataStringsAbbrev();
420 SmallVectorImpl<uint64_t> &Record);
422 SmallVectorImpl<uint64_t> &Record,
423 std::vector *MDAbbrevs = nullptr,
424 std::vector<uint64_t> *IndexPos = nullptr);
425 void writeModuleMetadata();
426 void writeFunctionMetadata(const Function &F);
427 void writeFunctionMetadataAttachment(const Function &F);
428 void pushGlobalMetadataAttachment(SmallVectorImpl<uint64_t> &Record,
429 const GlobalObject &GO);
430 void writeModuleMetadataKinds();
431 void writeOperandBundleTags();
432 void writeSyncScopeNames();
433 void writeConstants(unsigned FirstVal, unsigned LastVal, bool isGlobal);
434 void writeModuleConstants();
435 bool pushValueAndType(const Value *V, unsigned InstID,
436 SmallVectorImpl &Vals);
437 bool pushValueOrMetadata(const Value *V, unsigned InstID,
438 SmallVectorImpl &Vals);
439 void writeOperandBundles(const CallBase &CB, unsigned InstID);
440 void pushValue(const Value *V, unsigned InstID,
441 SmallVectorImpl &Vals);
442 void pushValueSigned(const Value *V, unsigned InstID,
443 SmallVectorImpl<uint64_t> &Vals);
444 void writeInstruction(const Instruction &I, unsigned InstID,
445 SmallVectorImpl &Vals);
446 void writeFunctionLevelValueSymbolTable(const ValueSymbolTable &VST);
447 void writeGlobalValueSymbolTable(
448 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex);
449 void writeUseList(UseListOrder &&Order);
450 void writeUseListBlock(const Function *F);
451 void
452 writeFunction(const Function &F,
453 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex);
454 void writeBlockInfo();
455 void writeModuleHash(StringRef View);
456
457 unsigned getEncodedSyncScopeID(SyncScope::ID SSID) {
458 return unsigned(SSID);
459 }
460
461 unsigned getEncodedAlign(MaybeAlign Alignment) { return encode(Alignment); }
462};
463
464
465class IndexBitcodeWriter : public BitcodeWriterBase {
466
467 const ModuleSummaryIndex &Index;
468
469
470
471
473
474
475
477
478
479
480 std::map<GlobalValue::GUID, unsigned> GUIDToValueIdMap;
481
482
483
484 std::vector<uint64_t> StackIds;
485
486
487
488
489 DenseMap<unsigned, unsigned> StackIdIndicesToIndex;
490
491
492 unsigned GlobalValueId = 0;
493
494
495
496 DenseMap<StringRef, uint64_t> ModuleIdMap;
497
498public:
499
500
501
502
503
504
505 IndexBitcodeWriter(
506 BitstreamWriter &Stream, StringTableBuilder &StrtabBuilder,
507 const ModuleSummaryIndex &Index,
510 : BitcodeWriterBase(Stream, StrtabBuilder), Index(Index),
511 DecSummaries(DecSummaries),
512 ModuleToSummariesForIndex(ModuleToSummariesForIndex) {
513
514
515
516 auto RecordStackIdReference = [&](unsigned StackIdIndex) {
517
518
519
521 StackIdIndicesToIndex.insert({StackIdIndex, StackIds.size()});
523 StackIds.push_back(Index.getStackIdAtIndex(StackIdIndex));
524 };
525
526
527
528
529
530 forEachSummary([&](GVInfo I, bool IsAliasee) {
531 GUIDToValueIdMap[I.first] = ++GlobalValueId;
532
533
534
535 if (IsAliasee)
536 return;
538 if (!FS)
539 return;
540
541
542
543 for (auto &CI : FS->callsites()) {
544
545
546
547
548
549
550
551
552
553
554 if (CI.StackIdIndices.empty()) {
555 GUIDToValueIdMap[CI.Callee.getGUID()] = ++GlobalValueId;
556 continue;
557 }
558 for (auto Idx : CI.StackIdIndices)
559 RecordStackIdReference(Idx);
560 }
562 for (auto &AI : FS->allocs())
563 for (auto &MIB : AI.MIBs)
564 for (auto Idx : MIB.StackIdIndices)
565 RecordStackIdReference(Idx);
566 }
567 });
568 }
569
570
571 using GVInfo = std::pair<GlobalValue::GUID, GlobalValueSummary *>;
572
573
574
575
576 template
577 void forEachSummary(Functor Callback) {
578 if (ModuleToSummariesForIndex) {
579 for (auto &M : *ModuleToSummariesForIndex)
580 for (auto &Summary : M.second) {
582
583
584
586 Callback({AS->getAliaseeGUID(), &AS->getAliasee()}, true);
587 }
588 } else {
589 for (auto &Summaries : Index)
590 for (auto &Summary : Summaries.second.getSummaryList())
592 }
593 }
594
595
596
597
598
599 template void forEachModule(Functor Callback) {
600 if (ModuleToSummariesForIndex) {
601 for (const auto &M : *ModuleToSummariesForIndex) {
602 const auto &MPI = Index.modulePaths().find(M.first);
603 if (MPI == Index.modulePaths().end()) {
604
605
606
607 assert(ModuleToSummariesForIndex->size() == 1);
608 continue;
609 }
611 }
612 } else {
613
614
615
616
617 std::vector ModulePaths;
618 for (auto &[ModPath, _] : Index.modulePaths())
619 ModulePaths.push_back(ModPath);
621 for (auto &ModPath : ModulePaths)
623 }
624 }
625
626
628
629private:
630 void writeModStrings();
631 void writeCombinedGlobalValueSummary();
632
634 auto VMI = GUIDToValueIdMap.find(ValGUID);
635 if (VMI == GUIDToValueIdMap.end())
636 return std::nullopt;
637 return VMI->second;
638 }
639
640 std::map<GlobalValue::GUID, unsigned> &valueIds() { return GUIDToValueIdMap; }
641};
642
643}
644
646 switch (Opcode) {
662 }
663}
664
666 switch (Opcode) {
669 }
670}
671
673 switch (Opcode) {
675 case Instruction::Add:
677 case Instruction::Sub:
679 case Instruction::Mul:
682 case Instruction::FDiv:
685 case Instruction::FRem:
693 }
694}
695
697 switch (Op) {
726 }
727}
728
730 switch (Ordering) {
738 }
740}
741
743 StringRef Str, unsigned AbbrevToUse) {
745
746
747 for (char C : Str) {
749 AbbrevToUse = 0;
751 }
752
753
754 Stream.EmitRecord(Code, Vals, AbbrevToUse);
755}
756
758 switch (Kind) {
759 case Attribute::Alignment:
761 case Attribute::AllocAlign:
763 case Attribute::AllocSize:
765 case Attribute::AlwaysInline:
767 case Attribute::Builtin:
769 case Attribute::ByVal:
771 case Attribute::Convergent:
773 case Attribute::InAlloca:
775 case Attribute::Cold:
777 case Attribute::DisableSanitizerInstrumentation:
779 case Attribute::FnRetThunkExtern:
781 case Attribute::Hot:
783 case Attribute::ElementType:
785 case Attribute::HybridPatchable:
787 case Attribute::InlineHint:
789 case Attribute::InReg:
791 case Attribute::JumpTable:
793 case Attribute::MinSize:
795 case Attribute::AllocatedPointer:
797 case Attribute::AllocKind:
799 case Attribute::Memory:
801 case Attribute::NoFPClass:
803 case Attribute::Naked:
805 case Attribute::Nest:
807 case Attribute::NoAlias:
809 case Attribute::NoBuiltin:
811 case Attribute::NoCallback:
813 case Attribute::NoDivergenceSource:
815 case Attribute::NoDuplicate:
817 case Attribute::NoFree:
819 case Attribute::NoImplicitFloat:
821 case Attribute::NoInline:
823 case Attribute::NoRecurse:
825 case Attribute::NoMerge:
827 case Attribute::NonLazyBind:
829 case Attribute::NonNull:
831 case Attribute::Dereferenceable:
833 case Attribute::DereferenceableOrNull:
835 case Attribute::NoRedZone:
837 case Attribute::NoReturn:
839 case Attribute::NoSync:
841 case Attribute::NoCfCheck:
843 case Attribute::NoProfile:
845 case Attribute::SkipProfile:
847 case Attribute::NoUnwind:
849 case Attribute::NoSanitizeBounds:
851 case Attribute::NoSanitizeCoverage:
853 case Attribute::NullPointerIsValid:
855 case Attribute::OptimizeForDebugging:
857 case Attribute::OptForFuzzing:
859 case Attribute::OptimizeForSize:
861 case Attribute::OptimizeNone:
863 case Attribute::ReadNone:
865 case Attribute::ReadOnly:
867 case Attribute::Returned:
869 case Attribute::ReturnsTwice:
871 case Attribute::SExt:
873 case Attribute::Speculatable:
875 case Attribute::StackAlignment:
877 case Attribute::StackProtect:
879 case Attribute::StackProtectReq:
881 case Attribute::StackProtectStrong:
883 case Attribute::SafeStack:
885 case Attribute::ShadowCallStack:
887 case Attribute::StrictFP:
889 case Attribute::StructRet:
891 case Attribute::SanitizeAddress:
893 case Attribute::SanitizeAllocToken:
895 case Attribute::SanitizeHWAddress:
897 case Attribute::SanitizeThread:
899 case Attribute::SanitizeType:
901 case Attribute::SanitizeMemory:
903 case Attribute::SanitizeNumericalStability:
905 case Attribute::SanitizeRealtime:
907 case Attribute::SanitizeRealtimeBlocking:
909 case Attribute::SpeculativeLoadHardening:
911 case Attribute::SwiftError:
913 case Attribute::SwiftSelf:
915 case Attribute::SwiftAsync:
917 case Attribute::UWTable:
919 case Attribute::VScaleRange:
921 case Attribute::WillReturn:
923 case Attribute::WriteOnly:
925 case Attribute::ZExt:
927 case Attribute::ImmArg:
929 case Attribute::SanitizeMemTag:
931 case Attribute::Preallocated:
933 case Attribute::NoUndef:
935 case Attribute::ByRef:
937 case Attribute::MustProgress:
939 case Attribute::PresplitCoroutine:
941 case Attribute::Writable:
943 case Attribute::CoroDestroyOnlyWhenComplete:
945 case Attribute::CoroElideSafe:
947 case Attribute::DeadOnUnwind:
949 case Attribute::Range:
951 case Attribute::Initializes:
953 case Attribute::NoExt:
955 case Attribute::Captures:
957 case Attribute::DeadOnReturn:
959 case Attribute::NoCreateUndefOrPoison:
962 llvm_unreachable("Can not encode end-attribute kinds marker.");
968 }
969
971}
972
974 if ((int64_t)V >= 0)
976 else
978}
979
981
982
983
984
985 unsigned NumWords = A.getActiveWords();
986 const uint64_t *RawData = A.getRawData();
987 for (unsigned i = 0; i < NumWords; i++)
989}
990
994 if (EmitBitWidth)
1001 } else {
1004 }
1005}
1006
1007void ModuleBitcodeWriter::writeAttributeGroupTable() {
1008 const std::vectorValueEnumerator::IndexAndAttrSet &AttrGrps =
1010 if (AttrGrps.empty()) return;
1011
1013
1014 SmallVector<uint64_t, 64> Record;
1016 unsigned AttrListIndex = Pair.first;
1017 AttributeSet AS = Pair.second;
1019 Record.push_back(AttrListIndex);
1020
1022 if (Attr.isEnumAttribute()) {
1025 } else if (Attr.isIntAttribute()) {
1027 Attribute::AttrKind Kind = Attr.getKindAsEnum();
1029 if (Kind == Attribute::Memory) {
1030
1031 const uint64_t Version = 1;
1033 } else {
1034 Record.push_back(Attr.getValueAsInt());
1035 }
1036 } else if (Attr.isStringAttribute()) {
1037 StringRef Kind = Attr.getKindAsString();
1038 StringRef Val = Attr.getValueAsString();
1039
1043 if (!Val.empty()) {
1046 }
1047 } else if (Attr.isTypeAttribute()) {
1048 Type *Ty = Attr.getValueAsType();
1051 if (Ty)
1053 } else if (Attr.isConstantRangeAttribute()) {
1057 true);
1058 } else {
1059 assert(Attr.isConstantRangeListAttribute());
1065 for (auto &CR : Val)
1067 }
1068 }
1069
1072 }
1073
1075}
1076
1077void ModuleBitcodeWriter::writeAttributeTable() {
1079 if (Attrs.empty()) return;
1080
1082
1083 SmallVector<uint64_t, 64> Record;
1084 for (const AttributeList &AL : Attrs) {
1085 for (unsigned i : AL.indexes()) {
1086 AttributeSet AS = AL.getAttributes(i);
1089 }
1090
1093 }
1094
1096}
1097
1098
1099void ModuleBitcodeWriter::writeTypeTable() {
1101
1103 SmallVector<uint64_t, 64> TypeVals;
1104
1106
1107
1108 auto Abbv = std::make_shared();
1110 Abbv->Add(BitCodeAbbrevOp(0));
1111 unsigned OpaquePtrAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1112
1113
1114 Abbv = std::make_shared();
1119 unsigned FunctionAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1120
1121
1122 Abbv = std::make_shared();
1127 unsigned StructAnonAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1128
1129
1130 Abbv = std::make_shared();
1134 unsigned StructNameAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1135
1136
1137 Abbv = std::make_shared();
1142 unsigned StructNamedAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1143
1144
1145 Abbv = std::make_shared();
1149 unsigned ArrayAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1150
1151
1152 TypeVals.push_back(TypeList.size());
1154 TypeVals.clear();
1155
1156
1157 for (Type *T : TypeList) {
1158 int AbbrevToUse = 0;
1159 unsigned Code = 0;
1160
1161 switch (T->getTypeID()) {
1171 case Type::MetadataTyID:
1173 break;
1176 case Type::IntegerTyID:
1177
1180 break;
1181 case Type::PointerTyID: {
1183 unsigned AddressSpace = PTy->getAddressSpace();
1184
1188 AbbrevToUse = OpaquePtrAbbrev;
1189 break;
1190 }
1191 case Type::FunctionTyID: {
1193
1195 TypeVals.push_back(FT->isVarArg());
1197 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
1199 AbbrevToUse = FunctionAbbrev;
1200 break;
1201 }
1202 case Type::StructTyID: {
1204
1206
1207 for (Type *ET : ST->elements())
1209
1210 if (ST->isLiteral()) {
1212 AbbrevToUse = StructAnonAbbrev;
1213 } else {
1214 if (ST->isOpaque()) {
1216 } else {
1218 AbbrevToUse = StructNamedAbbrev;
1219 }
1220
1221
1222 if (->getName().empty())
1224 StructNameAbbrev);
1225 }
1226 break;
1227 }
1228 case Type::ArrayTyID: {
1230
1232 TypeVals.push_back(AT->getNumElements());
1234 AbbrevToUse = ArrayAbbrev;
1235 break;
1236 }
1237 case Type::FixedVectorTyID:
1238 case Type::ScalableVectorTyID: {
1240
1241
1243 TypeVals.push_back(VT->getElementCount().getKnownMinValue());
1247 break;
1248 }
1249 case Type::TargetExtTyID: {
1253 StructNameAbbrev);
1254 TypeVals.push_back(TET->getNumTypeParameters());
1255 for (Type *InnerTy : TET->type_params())
1258 break;
1259 }
1260 case Type::TypedPointerTyID:
1261 llvm_unreachable("Typed pointers cannot be added to IR modules");
1262 }
1263
1264
1265 Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
1266 TypeVals.clear();
1267 }
1268
1270}
1271
1275 return 0;
1277 return 16;
1279 return 2;
1281 return 3;
1283 return 18;
1285 return 7;
1287 return 8;
1289 return 9;
1291 return 17;
1293 return 19;
1295 return 12;
1296 }
1298}
1299
1303
1306 RawFlags |= Flags.ReadNone;
1307 RawFlags |= (Flags.ReadOnly << 1);
1308 RawFlags |= (Flags.NoRecurse << 2);
1309 RawFlags |= (Flags.ReturnDoesNotAlias << 3);
1310 RawFlags |= (Flags.NoInline << 4);
1311 RawFlags |= (Flags.AlwaysInline << 5);
1312 RawFlags |= (Flags.NoUnwind << 6);
1313 RawFlags |= (Flags.MayThrow << 7);
1314 RawFlags |= (Flags.HasUnknownCall << 8);
1315 RawFlags |= (Flags.MustBeUnreachable << 9);
1316 return RawFlags;
1317}
1318
1319
1320
1322 bool ImportAsDecl = false) {
1324
1325 RawFlags |= Flags.NotEligibleToImport;
1326 RawFlags |= (Flags.Live << 1);
1327 RawFlags |= (Flags.DSOLocal << 2);
1328 RawFlags |= (Flags.CanAutoHide << 3);
1329
1330
1331
1332
1333 RawFlags = (RawFlags << 4) | Flags.Linkage;
1334
1335 RawFlags |= (Flags.Visibility << 8);
1336
1337 unsigned ImportType = Flags.ImportType | ImportAsDecl;
1338 RawFlags |= (ImportType << 10);
1339
1340 return RawFlags;
1341}
1342
1344 uint64_t RawFlags = Flags.MaybeReadOnly | (Flags.MaybeWriteOnly << 1) |
1345 (Flags.Constant << 2) | Flags.VCallVisibility << 3;
1346 return RawFlags;
1347}
1348
1351
1352 RawFlags |= CI.Hotness;
1353 RawFlags |= (CI.HasTailCall << 3);
1354
1355 return RawFlags;
1356}
1357
1360
1361 RawFlags |= CI.RelBlockFreq;
1363
1364 return RawFlags;
1365}
1366
1375
1384
1395
1397 switch (C.getSelectionKind()) {
1408 }
1410}
1411
1420
1421size_t ModuleBitcodeWriter::addToStrtab(StringRef Str) {
1422 if (GenerateHash)
1424 return StrtabBuilder.add(Str);
1425}
1426
1427void ModuleBitcodeWriter::writeComdats() {
1429 for (const Comdat *C : VE.getComdats()) {
1430
1431 Vals.push_back(addToStrtab(C->getName()));
1432 Vals.push_back(C->getName().size());
1436 }
1437}
1438
1439
1440
1441
1442void ModuleBitcodeWriter::writeValueSymbolTableForwardDecl() {
1443
1444
1445
1446
1447 auto Abbv = std::make_shared();
1449
1450
1451
1453 unsigned VSTOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1454
1455
1458
1459
1460
1461
1463}
1464
1466
1467
1469 bool isChar6 = true;
1470 for (char C : Str) {
1471 if (isChar6)
1473 if ((unsigned char)C & 128)
1474
1476 }
1477 if (isChar6)
1480}
1481
1482static_assert(sizeof(GlobalValue::SanitizerMetadata) <= sizeof(unsigned),
1483 "Sanitizer Metadata is too large for naive serialization.");
1484static unsigned
1486 return Meta.NoAddress | (Meta.NoHWAddress << 1) |
1487 (Meta.Memtag << 2) | (Meta.IsDynInit << 3);
1488}
1489
1490
1491
1492
1493void ModuleBitcodeWriter::writeModuleInfo() {
1494
1495 if (.getTargetTriple().empty())
1497 M.getTargetTriple().str(), 0 );
1498 const std::string &DL = M.getDataLayoutStr();
1501 if (.getModuleInlineAsm().empty())
1503 0 );
1504
1505
1506
1507 std::map<std::string, unsigned> SectionMap;
1508 std::map<std::string, unsigned> GCMap;
1509 MaybeAlign MaxGVarAlignment;
1510 unsigned MaxGlobalType = 0;
1511 for (const GlobalVariable &GV : M.globals()) {
1512 if (MaybeAlign A = GV.getAlign())
1513 MaxGVarAlignment = !MaxGVarAlignment ? *A : std::max(*MaxGVarAlignment, *A);
1514 MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV.getValueType()));
1515 if (GV.hasSection()) {
1516
1517 unsigned &Entry = SectionMap[std::string(GV.getSection())];
1518 if (!Entry) {
1520 0 );
1521 Entry = SectionMap.size();
1522 }
1523 }
1524 }
1525 for (const Function &F : M) {
1526 if (F.hasSection()) {
1527
1528 unsigned &Entry = SectionMap[std::string(F.getSection())];
1529 if (!Entry) {
1531 0 );
1532 Entry = SectionMap.size();
1533 }
1534 }
1535 if (F.hasGC()) {
1536
1537 unsigned &Entry = GCMap[F.getGC()];
1538 if (!Entry) {
1540 0 );
1541 Entry = GCMap.size();
1542 }
1543 }
1544 }
1545
1546
1547 unsigned SimpleGVarAbbrev = 0;
1548 if (.global_empty()) {
1549
1550 auto Abbv = std::make_shared();
1557
1558
1561 if (!MaxGVarAlignment)
1562 Abbv->Add(BitCodeAbbrevOp(0));
1563 else {
1564 unsigned MaxEncAlignment = getEncodedAlign(MaxGVarAlignment);
1567 }
1568 if (SectionMap.empty())
1569 Abbv->Add(BitCodeAbbrevOp(0));
1570 else
1573
1574 SimpleGVarAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1575 }
1576
1578
1579 {
1586
1587
1588 auto Abbv = std::make_shared();
1591 Abbv->Add(AbbrevOpToUse);
1592 unsigned FilenameAbbrev = Stream.EmitAbbrev(std::move(Abbv));
1593
1594 for (const auto P : M.getSourceFileName())
1596
1597
1600 }
1601
1602
1603 for (const GlobalVariable &GV : M.globals()) {
1604 unsigned AbbrevToUse = 0;
1605
1606
1607
1608
1609
1610 Vals.push_back(addToStrtab(GV.getName()));
1611 Vals.push_back(GV.getName().size());
1613 Vals.push_back(GV.getType()->getAddressSpace() << 2 | 2 | GV.isConstant());
1614 Vals.push_back(GV.isDeclaration() ? 0 :
1615 (VE.getValueID(GV.getInitializer()) + 1));
1617 Vals.push_back(getEncodedAlign(GV.getAlign()));
1618 Vals.push_back(GV.hasSection() ? SectionMap[std::string(GV.getSection())]
1619 : 0);
1620 if (GV.isThreadLocal() ||
1622 GV.getUnnamedAddr() != GlobalValue::UnnamedAddr::None ||
1623 GV.isExternallyInitialized() ||
1625 GV.hasComdat() || GV.hasAttributes() || GV.isDSOLocal() ||
1626 GV.hasPartition() || GV.hasSanitizerMetadata() || GV.getCodeModel()) {
1630 Vals.push_back(GV.isExternallyInitialized());
1633
1634 auto AL = GV.getAttributesAsList(AttributeList::FunctionIndex);
1636
1637 Vals.push_back(GV.isDSOLocal());
1638 Vals.push_back(addToStrtab(GV.getPartition()));
1639 Vals.push_back(GV.getPartition().size());
1640
1642 GV.getSanitizerMetadata())
1643 : 0));
1644 Vals.push_back(GV.getCodeModelRaw());
1645 } else {
1646 AbbrevToUse = SimpleGVarAbbrev;
1647 }
1648
1651 }
1652
1653
1654 for (const Function &F : M) {
1655
1656
1657
1658
1659 Vals.push_back(addToStrtab(F.getName()));
1666 Vals.push_back(getEncodedAlign(F.getAlign()));
1667 Vals.push_back(F.hasSection() ? SectionMap[std::string(F.getSection())]
1668 : 0);
1670 Vals.push_back(F.hasGC() ? GCMap[F.getGC()] : 0);
1673 : 0);
1677 : 0);
1679 F.hasPersonalityFn() ? (VE.getValueID(F.getPersonalityFn()) + 1) : 0);
1680
1682 Vals.push_back(F.getAddressSpace());
1683 Vals.push_back(addToStrtab(F.getPartition()));
1684 Vals.push_back(F.getPartition().size());
1685
1686 unsigned AbbrevToUse = 0;
1689 }
1690
1691
1692 for (const GlobalAlias &A : M.aliases()) {
1693
1694
1695
1696 Vals.push_back(addToStrtab(A.getName()));
1699 Vals.push_back(A.getType()->getAddressSpace());
1707 Vals.push_back(addToStrtab(A.getPartition()));
1708 Vals.push_back(A.getPartition().size());
1709
1710 unsigned AbbrevToUse = 0;
1713 }
1714
1715
1716 for (const GlobalIFunc &I : M.ifuncs()) {
1717
1718
1719 Vals.push_back(addToStrtab(I.getName()));
1722 Vals.push_back(I.getType()->getAddressSpace());
1727 Vals.push_back(addToStrtab(I.getPartition()));
1728 Vals.push_back(I.getPartition().size());
1731 }
1732
1733 writeValueSymbolTableForwardDecl();
1734}
1735
1738
1740 if (OBO->hasNoSignedWrap())
1742 if (OBO->hasNoUnsignedWrap())
1745 if (PEO->isExact())
1748 if (PDI->isDisjoint())
1751 if (FPMO->hasAllowReassoc())
1753 if (FPMO->hasNoNaNs())
1755 if (FPMO->hasNoInfs())
1757 if (FPMO->hasNoSignedZeros())
1759 if (FPMO->hasAllowReciprocal())
1761 if (FPMO->hasAllowContract())
1763 if (FPMO->hasApproxFunc())
1766 if (NNI->hasNonNeg())
1769 if (TI->hasNoSignedWrap())
1771 if (TI->hasNoUnsignedWrap())
1774 if (GEP->isInBounds())
1776 if (GEP->hasNoUnsignedSignedWrap())
1778 if (GEP->hasNoUnsignedWrap())
1781 if (ICmp->hasSameSign())
1783 }
1784
1785 return Flags;
1786}
1787
1788void ModuleBitcodeWriter::writeValueAsMetadata(
1789 const ValueAsMetadata *MD, SmallVectorImpl<uint64_t> &Record) {
1790
1796}
1797
1798void ModuleBitcodeWriter::writeMDTuple(const MDTuple *N,
1799 SmallVectorImpl<uint64_t> &Record,
1800 unsigned Abbrev) {
1801 for (const MDOperand &MDO : N->operands()) {
1804 "Unexpected function-local metadata");
1806 }
1809 Record, Abbrev);
1811}
1812
1813unsigned ModuleBitcodeWriter::createDILocationAbbrev() {
1814
1815
1816 auto Abbv = std::make_shared();
1826 return Stream.EmitAbbrev(std::move(Abbv));
1827}
1828
1829void ModuleBitcodeWriter::writeDILocation(const DILocation *N,
1830 SmallVectorImpl<uint64_t> &Record,
1831 unsigned &Abbrev) {
1832 if (!Abbrev)
1833 Abbrev = createDILocationAbbrev();
1834
1840 Record.push_back(N->isImplicitCode());
1841 Record.push_back(N->getAtomGroup());
1845}
1846
1847unsigned ModuleBitcodeWriter::createGenericDINodeAbbrev() {
1848
1849
1850 auto Abbv = std::make_shared();
1858 return Stream.EmitAbbrev(std::move(Abbv));
1859}
1860
1861void ModuleBitcodeWriter::writeGenericDINode(const GenericDINode *N,
1862 SmallVectorImpl<uint64_t> &Record,
1863 unsigned &Abbrev) {
1864 if (!Abbrev)
1865 Abbrev = createGenericDINodeAbbrev();
1866
1869 Record.push_back(0);
1870
1871 for (auto &I : N->operands())
1873
1876}
1877
1878void ModuleBitcodeWriter::writeDISubrange(const DISubrange *N,
1879 SmallVectorImpl<uint64_t> &Record,
1880 unsigned Abbrev) {
1881 const uint64_t Version = 2 << 1;
1887
1890}
1891
1892void ModuleBitcodeWriter::writeDIGenericSubrange(
1893 const DIGenericSubrange *N, SmallVectorImpl<uint64_t> &Record,
1894 unsigned Abbrev) {
1895 Record.push_back((uint64_t)N->isDistinct());
1900
1903}
1904
1905void ModuleBitcodeWriter::writeDIEnumerator(const DIEnumerator *N,
1906 SmallVectorImpl<uint64_t> &Record,
1907 unsigned Abbrev) {
1908 const uint64_t IsBigInt = 1 << 2;
1909 Record.push_back(IsBigInt | (N->isUnsigned() << 1) | N->isDistinct());
1910 Record.push_back(N->getValue().getBitWidth());
1913
1916}
1917
1918void ModuleBitcodeWriter::writeDIBasicType(const DIBasicType *N,
1919 SmallVectorImpl<uint64_t> &Record,
1920 unsigned Abbrev) {
1921 const unsigned SizeIsMetadata = 0x2;
1922 Record.push_back(SizeIsMetadata | (unsigned)N->isDistinct());
1926 Record.push_back(N->getAlignInBits());
1929 Record.push_back(N->getNumExtraInhabitants());
1930 Record.push_back(N->getDataSizeInBits());
1931
1934}
1935
1936void ModuleBitcodeWriter::writeDIFixedPointType(
1937 const DIFixedPointType *N, SmallVectorImpl<uint64_t> &Record,
1938 unsigned Abbrev) {
1939 const unsigned SizeIsMetadata = 0x2;
1940 Record.push_back(SizeIsMetadata | (unsigned)N->isDistinct());
1944 Record.push_back(N->getAlignInBits());
1948 Record.push_back(N->getFactorRaw());
1949
1950 auto WriteWideInt = [&](const APInt &Value) {
1951
1952
1953 uint64_t NumWords = Value.getActiveWords();
1954 uint64_t Encoded = (NumWords << 32) | Value.getBitWidth();
1957 };
1958
1959 WriteWideInt(N->getNumeratorRaw());
1960 WriteWideInt(N->getDenominatorRaw());
1961
1964}
1965
1966void ModuleBitcodeWriter::writeDIStringType(const DIStringType *N,
1967 SmallVectorImpl<uint64_t> &Record,
1968 unsigned Abbrev) {
1969 const unsigned SizeIsMetadata = 0x2;
1970 Record.push_back(SizeIsMetadata | (unsigned)N->isDistinct());
1977 Record.push_back(N->getAlignInBits());
1979
1982}
1983
1984void ModuleBitcodeWriter::writeDIDerivedType(const DIDerivedType *N,
1985 SmallVectorImpl<uint64_t> &Record,
1986 unsigned Abbrev) {
1987 const unsigned SizeIsMetadata = 0x2;
1988 Record.push_back(SizeIsMetadata | (unsigned)N->isDistinct());
1996 Record.push_back(N->getAlignInBits());
2000
2001
2002
2003 if (const auto &DWARFAddressSpace = N->getDWARFAddressSpace())
2004 Record.push_back(*DWARFAddressSpace + 1);
2005 else
2007
2009
2010 if (auto PtrAuthData = N->getPtrAuthData())
2011 Record.push_back(PtrAuthData->RawData);
2012 else
2014
2017}
2018
2019void ModuleBitcodeWriter::writeDISubrangeType(const DISubrangeType *N,
2020 SmallVectorImpl<uint64_t> &Record,
2021 unsigned Abbrev) {
2022 const unsigned SizeIsMetadata = 0x2;
2023 Record.push_back(SizeIsMetadata | (unsigned)N->isDistinct());
2029 Record.push_back(N->getAlignInBits());
2036
2039}
2040
2041void ModuleBitcodeWriter::writeDICompositeType(
2042 const DICompositeType *N, SmallVectorImpl<uint64_t> &Record,
2043 unsigned Abbrev) {
2044 const unsigned IsNotUsedInOldTypeRef = 0x2;
2045 const unsigned SizeIsMetadata = 0x4;
2046 Record.push_back(SizeIsMetadata | IsNotUsedInOldTypeRef |
2047 (unsigned)N->isDistinct());
2055 Record.push_back(N->getAlignInBits());
2059 Record.push_back(N->getRuntimeLang());
2069 Record.push_back(N->getNumExtraInhabitants());
2074
2077}
2078
2079void ModuleBitcodeWriter::writeDISubroutineType(
2080 const DISubroutineType *N, SmallVectorImpl<uint64_t> &Record,
2081 unsigned Abbrev) {
2082 const unsigned HasNoOldTypeRefs = 0x2;
2083 Record.push_back(HasNoOldTypeRefs | (unsigned)N->isDistinct());
2087
2090}
2091
2092void ModuleBitcodeWriter::writeDIFile(const DIFile *N,
2093 SmallVectorImpl<uint64_t> &Record,
2094 unsigned Abbrev) {
2098 if (N->getRawChecksum()) {
2099 Record.push_back(N->getRawChecksum()->Kind);
2101 } else {
2102
2103
2106 }
2107 auto Source = N->getRawSource();
2108 if (Source)
2110
2113}
2114
2115void ModuleBitcodeWriter::writeDICompileUnit(const DICompileUnit *N,
2116 SmallVectorImpl<uint64_t> &Record,
2117 unsigned Abbrev) {
2118 assert(N->isDistinct() && "Expected distinct compile units");
2119 Record.push_back( true);
2120
2121 auto Lang = N->getSourceLanguage();
2122 Record.push_back(Lang.getName());
2123
2124
2125 if (Lang.hasVersionedName())
2126 Record.back() ^= (uint64_t(1) << 63);
2127
2132 Record.push_back(N->getRuntimeVersion());
2134 Record.push_back(N->getEmissionKind());
2137 Record.push_back( 0);
2142 Record.push_back(N->getSplitDebugInlining());
2143 Record.push_back(N->getDebugInfoForProfiling());
2144 Record.push_back((unsigned)N->getNameTableKind());
2145 Record.push_back(N->getRangesBaseAddress());
2148 Record.push_back(Lang.hasVersionedName() ? Lang.getVersion() : 0);
2149
2152}
2153
2154void ModuleBitcodeWriter::writeDISubprogram(const DISubprogram *N,
2155 SmallVectorImpl<uint64_t> &Record,
2156 unsigned Abbrev) {
2157 const uint64_t HasUnitFlag = 1 << 1;
2158 const uint64_t HasSPFlagsFlag = 1 << 2;
2159 Record.push_back(uint64_t(N->isDistinct()) | HasUnitFlag | HasSPFlagsFlag);
2166 Record.push_back(N->getScopeLine());
2169 Record.push_back(N->getVirtualIndex());
2175 Record.push_back(N->getThisAdjustment());
2179 Record.push_back(N->getKeyInstructionsEnabled());
2180
2183}
2184
2185void ModuleBitcodeWriter::writeDILexicalBlock(const DILexicalBlock *N,
2186 SmallVectorImpl<uint64_t> &Record,
2187 unsigned Abbrev) {
2193
2196}
2197
2198void ModuleBitcodeWriter::writeDILexicalBlockFile(
2199 const DILexicalBlockFile *N, SmallVectorImpl<uint64_t> &Record,
2200 unsigned Abbrev) {
2204 Record.push_back(N->getDiscriminator());
2205
2208}
2209
2210void ModuleBitcodeWriter::writeDICommonBlock(const DICommonBlock *N,
2211 SmallVectorImpl<uint64_t> &Record,
2212 unsigned Abbrev) {
2219
2222}
2223
2224void ModuleBitcodeWriter::writeDINamespace(const DINamespace *N,
2225 SmallVectorImpl<uint64_t> &Record,
2226 unsigned Abbrev) {
2227 Record.push_back(N->isDistinct() | N->getExportSymbols() << 1);
2230
2233}
2234
2235void ModuleBitcodeWriter::writeDIMacro(const DIMacro *N,
2236 SmallVectorImpl<uint64_t> &Record,
2237 unsigned Abbrev) {
2239 Record.push_back(N->getMacinfoType());
2243
2246}
2247
2248void ModuleBitcodeWriter::writeDIMacroFile(const DIMacroFile *N,
2249 SmallVectorImpl<uint64_t> &Record,
2250 unsigned Abbrev) {
2252 Record.push_back(N->getMacinfoType());
2256
2259}
2260
2261void ModuleBitcodeWriter::writeDIArgList(const DIArgList *N,
2262 SmallVectorImpl<uint64_t> &Record) {
2263 Record.reserve(N->getArgs().size());
2264 for (ValueAsMetadata *MD : N->getArgs())
2266
2269}
2270
2271void ModuleBitcodeWriter::writeDIModule(const DIModule *N,
2272 SmallVectorImpl<uint64_t> &Record,
2273 unsigned Abbrev) {
2275 for (auto &I : N->operands())
2279
2282}
2283
2284void ModuleBitcodeWriter::writeDIAssignID(const DIAssignID *N,
2285 SmallVectorImpl<uint64_t> &Record,
2286 unsigned Abbrev) {
2287
2291}
2292
2293void ModuleBitcodeWriter::writeDITemplateTypeParameter(
2294 const DITemplateTypeParameter *N, SmallVectorImpl<uint64_t> &Record,
2295 unsigned Abbrev) {
2300
2303}
2304
2305void ModuleBitcodeWriter::writeDITemplateValueParameter(
2306 const DITemplateValueParameter *N, SmallVectorImpl<uint64_t> &Record,
2307 unsigned Abbrev) {
2314
2317}
2318
2319void ModuleBitcodeWriter::writeDIGlobalVariable(
2320 const DIGlobalVariable *N, SmallVectorImpl<uint64_t> &Record,
2321 unsigned Abbrev) {
2322 const uint64_t Version = 2 << 1;
2330 Record.push_back(N->isLocalToUnit());
2331 Record.push_back(N->isDefinition());
2334 Record.push_back(N->getAlignInBits());
2336
2339}
2340
2341void ModuleBitcodeWriter::writeDILocalVariable(
2342 const DILocalVariable *N, SmallVectorImpl<uint64_t> &Record,
2343 unsigned Abbrev) {
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357 const uint64_t HasAlignmentFlag = 1 << 1;
2358 Record.push_back((uint64_t)N->isDistinct() | HasAlignmentFlag);
2366 Record.push_back(N->getAlignInBits());
2368
2371}
2372
2373void ModuleBitcodeWriter::writeDILabel(
2374 const DILabel *N, SmallVectorImpl<uint64_t> &Record,
2375 unsigned Abbrev) {
2376 uint64_t IsArtificialFlag = uint64_t(N->isArtificial()) << 1;
2377 Record.push_back((uint64_t)N->isDistinct() | IsArtificialFlag);
2383 Record.push_back(N->getCoroSuspendIdx().has_value()
2384 ? (uint64_t)N->getCoroSuspendIdx().value()
2385 : std::numeric_limits<uint64_t>::max());
2386
2389}
2390
2391void ModuleBitcodeWriter::writeDIExpression(const DIExpression *N,
2392 SmallVectorImpl<uint64_t> &Record,
2393 unsigned Abbrev) {
2394 Record.reserve(N->getElements().size() + 1);
2395 const uint64_t Version = 3 << 1;
2397 Record.append(N->elements_begin(), N->elements_end());
2398
2401}
2402
2403void ModuleBitcodeWriter::writeDIGlobalVariableExpression(
2404 const DIGlobalVariableExpression *N, SmallVectorImpl<uint64_t> &Record,
2405 unsigned Abbrev) {
2409
2412}
2413
2414void ModuleBitcodeWriter::writeDIObjCProperty(const DIObjCProperty *N,
2415 SmallVectorImpl<uint64_t> &Record,
2416 unsigned Abbrev) {
2423 Record.push_back(N->getAttributes());
2425
2428}
2429
2430void ModuleBitcodeWriter::writeDIImportedEntity(
2431 const DIImportedEntity *N, SmallVectorImpl<uint64_t> &Record,
2432 unsigned Abbrev) {
2441
2444}
2445
2446unsigned ModuleBitcodeWriter::createNamedMetadataAbbrev() {
2447 auto Abbv = std::make_shared();
2451 return Stream.EmitAbbrev(std::move(Abbv));
2452}
2453
2454void ModuleBitcodeWriter::writeNamedMetadata(
2455 SmallVectorImpl<uint64_t> &Record) {
2456 if (M.named_metadata_empty())
2457 return;
2458
2459 unsigned Abbrev = createNamedMetadataAbbrev();
2460 for (const NamedMDNode &NMD : M.named_metadata()) {
2461
2462 StringRef Str = NMD.getName();
2463 Record.append(Str.bytes_begin(), Str.bytes_end());
2466
2467
2468 for (const MDNode *N : NMD.operands())
2472 }
2473}
2474
2475unsigned ModuleBitcodeWriter::createMetadataStringsAbbrev() {
2476 auto Abbv = std::make_shared();
2481 return Stream.EmitAbbrev(std::move(Abbv));
2482}
2483
2484
2485
2486
2487
2488void ModuleBitcodeWriter::writeMetadataStrings(
2490 if (Strings.empty())
2491 return;
2492
2493
2496
2497
2498 SmallString<256> Blob;
2499 {
2500 BitstreamWriter W(Blob);
2501 for (const Metadata *MD : Strings)
2503 W.FlushToWord();
2504 }
2505
2506
2508
2509
2510 for (const Metadata *MD : Strings)
2512
2513
2514 Stream.EmitRecordWithBlob(createMetadataStringsAbbrev(), Record, Blob);
2516}
2517
2518
2520#define HANDLE_MDNODE_LEAF(CLASS) CLASS##AbbrevID,
2521#include "llvm/IR/Metadata.def"
2523};
2524
2525void ModuleBitcodeWriter::writeMetadataRecords(
2527 std::vector *MDAbbrevs, std::vector<uint64_t> *IndexPos) {
2528 if (MDs.empty())
2529 return;
2530
2531
2532#define HANDLE_MDNODE_LEAF(CLASS) unsigned CLASS##Abbrev = 0;
2533#include "llvm/IR/Metadata.def"
2534
2535 for (const Metadata *MD : MDs) {
2536 if (IndexPos)
2539 assert(N->isResolved() && "Expected forward references to be resolved");
2540
2541 switch (N->getMetadataID()) {
2542 default:
2544#define HANDLE_MDNODE_LEAF(CLASS) \
2545 case Metadata::CLASS##Kind: \
2546 if (MDAbbrevs) \
2547 write##CLASS(cast(N), Record, \
2548 (*MDAbbrevs)[MetadataAbbrev::CLASS##AbbrevID]); \
2549 else \
2550 write##CLASS(cast(N), Record, CLASS##Abbrev); \
2551 continue;
2552#include "llvm/IR/Metadata.def"
2553 }
2554 }
2557 continue;
2558 }
2560 }
2561}
2562
2563void ModuleBitcodeWriter::writeModuleMetadata() {
2564 if (!VE.hasMDs() && M.named_metadata_empty())
2565 return;
2566
2568 SmallVector<uint64_t, 64> Record;
2569
2570
2571
2572 std::vector MDAbbrevs;
2573
2575 MDAbbrevs[MetadataAbbrev::DILocationAbbrevID] = createDILocationAbbrev();
2576 MDAbbrevs[MetadataAbbrev::GenericDINodeAbbrevID] =
2577 createGenericDINodeAbbrev();
2578
2579 auto Abbv = std::make_shared();
2583 unsigned OffsetAbbrev = Stream.EmitAbbrev(std::move(Abbv));
2584
2585 Abbv = std::make_shared();
2589 unsigned IndexAbbrev = Stream.EmitAbbrev(std::move(Abbv));
2590
2591
2592 writeMetadataStrings(VE.getMDStrings(), Record);
2593
2594
2595
2597
2598
2599
2600
2601 uint64_t Vals[] = {0, 0};
2603 }
2604
2605
2606
2607
2608 uint64_t IndexOffsetRecordBitPos = Stream.GetCurrentBitNo();
2609
2610
2611 std::vector<uint64_t> IndexPos;
2613
2614
2615 writeMetadataRecords(VE.getNonMDStrings(), Record, &MDAbbrevs, &IndexPos);
2616
2618
2619
2620
2621
2624
2625
2626 uint64_t PreviousValue = IndexOffsetRecordBitPos;
2627 for (auto &Elt : IndexPos) {
2628 auto EltDelta = Elt - PreviousValue;
2629 PreviousValue = Elt;
2630 Elt = EltDelta;
2631 }
2632
2634 IndexPos.clear();
2635 }
2636
2637
2638 writeNamedMetadata(Record);
2639
2640 auto AddDeclAttachedMetadata = [&](const GlobalObject &GO) {
2641 SmallVector<uint64_t, 4> Record;
2643 pushGlobalMetadataAttachment(Record, GO);
2645 };
2646 for (const Function &F : M)
2647 if (F.isDeclaration() && F.hasMetadata())
2648 AddDeclAttachedMetadata(F);
2649 for (const GlobalIFunc &GI : M.ifuncs())
2650 if (GI.hasMetadata())
2651 AddDeclAttachedMetadata(GI);
2652
2653
2654 for (const GlobalVariable &GV : M.globals())
2655 if (GV.hasMetadata())
2656 AddDeclAttachedMetadata(GV);
2657
2659}
2660
2661void ModuleBitcodeWriter::writeFunctionMetadata(const Function &F) {
2663 return;
2664
2666 SmallVector<uint64_t, 64> Record;
2667 writeMetadataStrings(VE.getMDStrings(), Record);
2670}
2671
2672void ModuleBitcodeWriter::pushGlobalMetadataAttachment(
2673 SmallVectorImpl<uint64_t> &Record, const GlobalObject &GO) {
2674
2677 for (const auto &I : MDs) {
2680 }
2681}
2682
2683void ModuleBitcodeWriter::writeFunctionMetadataAttachment(const Function &F) {
2685
2686 SmallVector<uint64_t, 64> Record;
2687
2688 if (F.hasMetadata()) {
2689 pushGlobalMetadataAttachment(Record, F);
2692 }
2693
2694
2695
2697 for (const BasicBlock &BB : F)
2698 for (const Instruction &I : BB) {
2700 I.getAllMetadataOtherThanDebugLoc(MDs);
2701
2702
2703 if (MDs.empty()) continue;
2704
2706
2707 for (const auto &[ID, MD] : MDs) {
2710 }
2713 }
2714
2716}
2717
2718void ModuleBitcodeWriter::writeModuleMetadataKinds() {
2719 SmallVector<uint64_t, 64> Record;
2720
2721
2722
2724 M.getMDKindNames(Names);
2725
2726 if (Names.empty()) return;
2727
2729
2730 for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) {
2732 StringRef KName = Names[MDKindID];
2734
2737 }
2738
2740}
2741
2742void ModuleBitcodeWriter::writeOperandBundleTags() {
2743
2744
2745
2746
2747
2748
2750 M.getOperandBundleTags(Tags);
2751
2752 if (Tags.empty())
2753 return;
2754
2756
2757 SmallVector<uint64_t, 64> Record;
2758
2759 for (auto Tag : Tags) {
2761
2764 }
2765
2767}
2768
2769void ModuleBitcodeWriter::writeSyncScopeNames() {
2771 M.getContext().getSyncScopeNames(SSNs);
2772 if (SSNs.empty())
2773 return;
2774
2776
2777 SmallVector<uint64_t, 64> Record;
2778 for (auto SSN : SSNs) {
2779 Record.append(SSN.begin(), SSN.end());
2782 }
2783
2785}
2786
2787void ModuleBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal,
2788 bool isGlobal) {
2789 if (FirstVal == LastVal) return;
2790
2792
2793 unsigned AggregateAbbrev = 0;
2794 unsigned String8Abbrev = 0;
2795 unsigned CString7Abbrev = 0;
2796 unsigned CString6Abbrev = 0;
2797
2798 if (isGlobal) {
2799
2800 auto Abbv = std::make_shared();
2804 AggregateAbbrev = Stream.EmitAbbrev(std::move(Abbv));
2805
2806
2807 Abbv = std::make_shared();
2811 String8Abbrev = Stream.EmitAbbrev(std::move(Abbv));
2812
2813 Abbv = std::make_shared();
2817 CString7Abbrev = Stream.EmitAbbrev(std::move(Abbv));
2818
2819 Abbv = std::make_shared();
2823 CString6Abbrev = Stream.EmitAbbrev(std::move(Abbv));
2824 }
2825
2826 SmallVector<uint64_t, 64> Record;
2827
2829 Type *LastTy = nullptr;
2830 for (unsigned i = FirstVal; i != LastVal; ++i) {
2831 const Value *V = Vals[i].first;
2832
2833 if (V->getType() != LastTy) {
2834 LastTy = V->getType();
2837 CONSTANTS_SETTYPE_ABBREV);
2839 }
2840
2844 unsigned(IA->hasSideEffects()) | unsigned(IA->isAlignStack()) << 1 |
2845 unsigned(IA->getDialect() & 1) << 2 | unsigned(IA->canThrow()) << 3);
2846
2847
2848 StringRef AsmStr = IA->getAsmString();
2851
2852
2853 StringRef ConstraintStr = IA->getConstraintString();
2855 Record.append(ConstraintStr.begin(), ConstraintStr.end());
2858 continue;
2859 }
2861 unsigned Code = -1U;
2862 unsigned AbbrevToUse = 0;
2863 if (C->isNullValue()) {
2870 if (IV->getBitWidth() <= 64) {
2871 uint64_t V = IV->getSExtValue();
2874 AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
2875 } else {
2878 }
2884 Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
2886
2887
2888 APInt api = CFP->getValueAPF().bitcastToAPInt();
2890 Record.push_back((p[1] << 48) | (p[0] >> 16));
2891 Record.push_back(p[0] & 0xffffLL);
2893 APInt api = CFP->getValueAPF().bitcastToAPInt();
2897 } else {
2898 assert(0 && "Unknown FP type!");
2899 }
2903
2904 uint64_t NumElts = Str->getNumElements();
2905
2906 if (Str->isCString()) {
2908 --NumElts;
2909 } else {
2911 AbbrevToUse = String8Abbrev;
2912 }
2915 for (uint64_t i = 0; i != NumElts; ++i) {
2916 unsigned char V = Str->getElementAsInteger(i);
2918 isCStr7 &= (V & 128) == 0;
2919 if (isCStrChar6)
2921 }
2922
2923 if (isCStrChar6)
2924 AbbrevToUse = CString6Abbrev;
2925 else if (isCStr7)
2926 AbbrevToUse = CString7Abbrev;
2927 } else if (const ConstantDataSequential *CDS =
2930 Type *EltTy = CDS->getElementType();
2932 for (uint64_t i = 0, e = CDS->getNumElements(); i != e; ++i)
2933 Record.push_back(CDS->getElementAsInteger(i));
2934 } else {
2935 for (uint64_t i = 0, e = CDS->getNumElements(); i != e; ++i)
2937 CDS->getElementAsAPFloat(i).bitcastToAPInt().getLimitedValue());
2938 }
2941 for (const Value *Op : C->operands())
2943 AbbrevToUse = AggregateAbbrev;
2945 switch (CE->getOpcode()) {
2946 default:
2952 AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
2953 } else {
2954 assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
2960 if (Flags != 0)
2962 }
2963 break;
2964 case Instruction::FNeg: {
2965 assert(CE->getNumOperands() == 1 && "Unknown constant expr!");
2970 if (Flags != 0)
2972 break;
2973 }
2974 case Instruction::GetElementPtr: {
2979 if (std::optional Range = GO->getInRange()) {
2982 }
2983 for (const Value *Op : CE->operands()) {
2986 }
2987 break;
2988 }
2989 case Instruction::ExtractElement:
2995 break;
2996 case Instruction::InsertElement:
3002 break;
3003 case Instruction::ShuffleVector:
3004
3005
3006
3007
3008 if (C->getType() == C->getOperand(0)->getType()) {
3010 } else {
3013 }
3017 break;
3018 }
3039 } else {
3040#ifndef NDEBUG
3041 C->dump();
3042#endif
3044 }
3045 Stream.EmitRecord(Code, Record, AbbrevToUse);
3047 }
3048
3050}
3051
3052void ModuleBitcodeWriter::writeModuleConstants() {
3054
3055
3056
3057 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
3059 writeConstants(i, Vals.size(), true);
3060 return;
3061 }
3062 }
3063}
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073bool ModuleBitcodeWriter::pushValueAndType(const Value *V, unsigned InstID,
3074 SmallVectorImpl &Vals) {
3076
3078 if (ValID >= InstID) {
3080 return true;
3081 }
3082 return false;
3083}
3084
3085bool ModuleBitcodeWriter::pushValueOrMetadata(const Value *V, unsigned InstID,
3086 SmallVectorImpl &Vals) {
3087 bool IsMetadata = V->getType()->isMetadataTy();
3088 if (IsMetadata) {
3093 return false;
3094 }
3095 return pushValueAndType(V, InstID, Vals);
3096}
3097
3098void ModuleBitcodeWriter::writeOperandBundles(const CallBase &CS,
3099 unsigned InstID) {
3102
3105 Record.push_back(C.getOperandBundleTagID(Bundle.getTagName()));
3106
3107 for (auto &Input : Bundle.Inputs)
3108 pushValueOrMetadata(Input, InstID, Record);
3109
3112 }
3113}
3114
3115
3116
3117void ModuleBitcodeWriter::pushValue(const Value *V, unsigned InstID,
3118 SmallVectorImpl &Vals) {
3121}
3122
3123void ModuleBitcodeWriter::pushValueSigned(const Value *V, unsigned InstID,
3124 SmallVectorImpl<uint64_t> &Vals) {
3126 int64_t diff = ((int32_t)InstID - (int32_t)ValID);
3128}
3129
3130
3131void ModuleBitcodeWriter::writeInstruction(const Instruction &I,
3132 unsigned InstID,
3133 SmallVectorImpl &Vals) {
3134 unsigned Code = 0;
3135 unsigned AbbrevToUse = 0;
3137 switch (I.getOpcode()) {
3138 default:
3141 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
3142 AbbrevToUse = FUNCTION_INST_CAST_ABBREV;
3146 if (Flags != 0) {
3147 if (AbbrevToUse == FUNCTION_INST_CAST_ABBREV)
3148 AbbrevToUse = FUNCTION_INST_CAST_FLAGS_ABBREV;
3150 }
3151 } else {
3154 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
3155 AbbrevToUse = FUNCTION_INST_BINOP_ABBREV;
3156 pushValue(I.getOperand(1), InstID, Vals);
3159 if (Flags != 0) {
3160 if (AbbrevToUse == FUNCTION_INST_BINOP_ABBREV)
3161 AbbrevToUse = FUNCTION_INST_BINOP_FLAGS_ABBREV;
3163 }
3164 }
3165 break;
3166 case Instruction::FNeg: {
3168 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
3169 AbbrevToUse = FUNCTION_INST_UNOP_ABBREV;
3172 if (Flags != 0) {
3173 if (AbbrevToUse == FUNCTION_INST_UNOP_ABBREV)
3174 AbbrevToUse = FUNCTION_INST_UNOP_FLAGS_ABBREV;
3176 }
3177 break;
3178 }
3179 case Instruction::GetElementPtr: {
3181 AbbrevToUse = FUNCTION_INST_GEP_ABBREV;
3185 for (const Value *Op : I.operands())
3186 pushValueAndType(Op, InstID, Vals);
3187 break;
3188 }
3189 case Instruction::ExtractValue: {
3191 pushValueAndType(I.getOperand(0), InstID, Vals);
3194 break;
3195 }
3196 case Instruction::InsertValue: {
3198 pushValueAndType(I.getOperand(0), InstID, Vals);
3199 pushValueAndType(I.getOperand(1), InstID, Vals);
3202 break;
3203 }
3204 case Instruction::Select: {
3206 pushValueAndType(I.getOperand(1), InstID, Vals);
3207 pushValue(I.getOperand(2), InstID, Vals);
3208 pushValueAndType(I.getOperand(0), InstID, Vals);
3210 if (Flags != 0)
3212 break;
3213 }
3214 case Instruction::ExtractElement:
3216 pushValueAndType(I.getOperand(0), InstID, Vals);
3217 pushValueAndType(I.getOperand(1), InstID, Vals);
3218 break;
3219 case Instruction::InsertElement:
3221 pushValueAndType(I.getOperand(0), InstID, Vals);
3222 pushValue(I.getOperand(1), InstID, Vals);
3223 pushValueAndType(I.getOperand(2), InstID, Vals);
3224 break;
3225 case Instruction::ShuffleVector:
3227 pushValueAndType(I.getOperand(0), InstID, Vals);
3228 pushValue(I.getOperand(1), InstID, Vals);
3230 Vals);
3231 break;
3232 case Instruction::ICmp:
3233 case Instruction::FCmp: {
3234
3236 AbbrevToUse = FUNCTION_INST_CMP_ABBREV;
3237 if (pushValueAndType(I.getOperand(0), InstID, Vals))
3238 AbbrevToUse = 0;
3239 pushValue(I.getOperand(1), InstID, Vals);
3242 if (Flags != 0) {
3244 if (AbbrevToUse)
3245 AbbrevToUse = FUNCTION_INST_CMP_FLAGS_ABBREV;
3246 }
3247 break;
3248 }
3249
3250 case Instruction::Ret:
3251 {
3253 unsigned NumOperands = I.getNumOperands();
3254 if (NumOperands == 0)
3255 AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV;
3256 else if (NumOperands == 1) {
3257 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
3258 AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV;
3259 } else {
3260 for (const Value *Op : I.operands())
3261 pushValueAndType(Op, InstID, Vals);
3262 }
3263 }
3264 break;
3265 case Instruction::Br:
3266 {
3268 AbbrevToUse = FUNCTION_INST_BR_UNCOND_ABBREV;
3271 if (II.isConditional()) {
3273 pushValue(II.getCondition(), InstID, Vals);
3274 AbbrevToUse = FUNCTION_INST_BR_COND_ABBREV;
3275 }
3276 }
3277 break;
3278 case Instruction::Switch:
3279 {
3283 pushValue(SI.getCondition(), InstID, Vals);
3285 for (auto Case : SI.cases()) {
3288 }
3289 }
3290 break;
3291 case Instruction::IndirectBr:
3294
3295 pushValue(I.getOperand(0), InstID, Vals);
3298 break;
3299
3300 case Instruction::Invoke: {
3303 FunctionType *FTy = II->getFunctionType();
3304
3305 if (II->hasOperandBundles())
3306 writeOperandBundles(*II, InstID);
3307
3309
3311 Vals.push_back(II->getCallingConv() | 1 << 13);
3315 pushValueAndType(Callee, InstID, Vals);
3316
3317
3318 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
3319 pushValue(I.getOperand(i), InstID, Vals);
3320
3321
3322 if (FTy->isVarArg()) {
3323 for (unsigned i = FTy->getNumParams(), e = II->arg_size(); i != e; ++i)
3324 pushValueAndType(I.getOperand(i), InstID, Vals);
3325 }
3326 break;
3327 }
3328 case Instruction::Resume:
3330 pushValueAndType(I.getOperand(0), InstID, Vals);
3331 break;
3332 case Instruction::CleanupRet: {
3335 pushValue(CRI.getCleanupPad(), InstID, Vals);
3336 if (CRI.hasUnwindDest())
3338 break;
3339 }
3340 case Instruction::CatchRet: {
3343 pushValue(CRI.getCatchPad(), InstID, Vals);
3345 break;
3346 }
3347 case Instruction::CleanupPad:
3348 case Instruction::CatchPad: {
3352 pushValue(FuncletPad.getParentPad(), InstID, Vals);
3353
3354 unsigned NumArgOperands = FuncletPad.arg_size();
3356 for (unsigned Op = 0; Op != NumArgOperands; ++Op)
3357 pushValueAndType(FuncletPad.getArgOperand(Op), InstID, Vals);
3358 break;
3359 }
3360 case Instruction::CatchSwitch: {
3363
3364 pushValue(CatchSwitch.getParentPad(), InstID, Vals);
3365
3366 unsigned NumHandlers = CatchSwitch.getNumHandlers();
3368 for (const BasicBlock *CatchPadBB : CatchSwitch.handlers())
3370
3371 if (CatchSwitch.hasUnwindDest())
3373 break;
3374 }
3375 case Instruction::CallBr: {
3379
3381 writeOperandBundles(*CBI, InstID);
3382
3384
3386
3389
3394
3396 pushValueAndType(Callee, InstID, Vals);
3397
3398
3399 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
3400 pushValue(I.getOperand(i), InstID, Vals);
3401
3402
3403 if (FTy->isVarArg()) {
3404 for (unsigned i = FTy->getNumParams(), e = CBI->arg_size(); i != e; ++i)
3405 pushValueAndType(I.getOperand(i), InstID, Vals);
3406 }
3407 break;
3408 }
3409 case Instruction::Unreachable:
3411 AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV;
3412 break;
3413
3414 case Instruction::PHI: {
3417
3418
3419
3425 }
3426
3428 if (Flags != 0)
3430
3431
3432 Stream.EmitRecord(Code, Vals64, AbbrevToUse);
3434 return;
3435 }
3436
3437 case Instruction::LandingPad: {
3446 else
3448 pushValueAndType(LP.getClause(I), InstID, Vals);
3449 }
3450 break;
3451 }
3452
3453 case Instruction::Alloca: {
3459 using APV = AllocaPackedValues;
3460 unsigned Record = 0;
3461 unsigned EncodedAlign = getEncodedAlign(AI.getAlign());
3463 Record, EncodedAlign & ((1 << APV::AlignLower::Bits) - 1));
3465 EncodedAlign >> APV::AlignLower::Bits);
3470
3472 if (AS != M.getDataLayout().getAllocaAddrSpace())
3474 break;
3475 }
3476
3477 case Instruction::Load:
3480 pushValueAndType(I.getOperand(0), InstID, Vals);
3481 } else {
3483 if (!pushValueAndType(I.getOperand(0), InstID, Vals))
3484 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV;
3485 }
3492 }
3493 break;
3494 case Instruction::Store:
3497 } else {
3499 AbbrevToUse = FUNCTION_INST_STORE_ABBREV;
3500 }
3501 if (pushValueAndType(I.getOperand(1), InstID, Vals))
3502 AbbrevToUse = 0;
3503 if (pushValueAndType(I.getOperand(0), InstID, Vals))
3504 AbbrevToUse = 0;
3510 getEncodedSyncScopeID(cast(I).getSyncScopeID()));
3511 }
3512 break;
3513 case Instruction::AtomicCmpXchg:
3515 pushValueAndType(I.getOperand(0), InstID, Vals);
3516 pushValueAndType(I.getOperand(1), InstID, Vals);
3517 pushValue(I.getOperand(2), InstID, Vals);
3527 break;
3528 case Instruction::AtomicRMW:
3530 pushValueAndType(I.getOperand(0), InstID, Vals);
3531 pushValueAndType(I.getOperand(1), InstID, Vals);
3539 break;
3540 case Instruction::Fence:
3544 break;
3545 case Instruction::Call: {
3548
3550 writeOperandBundles(CI, InstID);
3551
3553
3555
3563 if (Flags != 0)
3565
3567 pushValueAndType(CI.getCalledOperand(), InstID, Vals);
3568
3569
3570 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
3571 pushValue(CI.getArgOperand(i), InstID, Vals);
3572
3573
3574 if (FTy->isVarArg()) {
3575 for (unsigned i = FTy->getNumParams(), e = CI.arg_size(); i != e; ++i)
3576 pushValueAndType(CI.getArgOperand(i), InstID, Vals);
3577 }
3578 break;
3579 }
3580 case Instruction::VAArg:
3583 pushValue(I.getOperand(0), InstID, Vals);
3585 break;
3586 case Instruction::Freeze:
3588 pushValueAndType(I.getOperand(0), InstID, Vals);
3589 break;
3590 }
3591
3592 Stream.EmitRecord(Code, Vals, AbbrevToUse);
3594}
3595
3596
3597
3598void ModuleBitcodeWriter::writeGlobalValueSymbolTable(
3599 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex) {
3600
3601
3603
3604 VSTOffset -= bitcodeStartBit();
3605 assert((VSTOffset & 31) == 0 && "VST block not 32-bit aligned");
3606
3607
3608
3609 Stream.BackpatchWord(VSTOffsetPlaceholder, VSTOffset / 32 + 1);
3610
3612
3613 auto Abbv = std::make_shared();
3617 unsigned FnEntryAbbrev = Stream.EmitAbbrev(std::move(Abbv));
3618
3619 for (const Function &F : M) {
3620 uint64_t Record[2];
3621
3622 if (F.isDeclaration())
3623 continue;
3624
3626
3627
3628
3629 uint64_t BitcodeIndex = FunctionToBitcodeIndex[&F] - bitcodeStartBit();
3630 assert((BitcodeIndex & 31) == 0 && "function block not 32-bit aligned");
3631
3632
3633
3634 Record[1] = BitcodeIndex / 32 + 1;
3635
3637 }
3638
3640}
3641
3642
3643void ModuleBitcodeWriter::writeFunctionLevelValueSymbolTable(
3644 const ValueSymbolTable &VST) {
3645 if (VST.empty())
3646 return;
3647
3649
3650
3651
3652 SmallVector<uint64_t, 64> NameVals;
3653
3654 for (const ValueName &Name : VST) {
3655
3657
3658 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
3660
3661
3662
3663 unsigned Code;
3667 AbbrevToUse = VST_BBENTRY_6_ABBREV;
3668 } else {
3671 AbbrevToUse = VST_ENTRY_6_ABBREV;
3673 AbbrevToUse = VST_ENTRY_7_ABBREV;
3674 }
3675
3676 for (const auto P : Name.getKey())
3677 NameVals.push_back((unsigned char)P);
3678
3679
3680 Stream.EmitRecord(Code, NameVals, AbbrevToUse);
3681 NameVals.clear();
3682 }
3683
3685}
3686
3687void ModuleBitcodeWriter::writeUseList(UseListOrder &&Order) {
3688 assert(Order.Shuffle.size() >= 2 && "Shuffle too small");
3689 unsigned Code;
3692 else
3694
3695 SmallVector<uint64_t, 64> Record(Order.Shuffle.begin(), Order.Shuffle.end());
3696 Record.push_back(VE.getValueID(Order.V));
3698}
3699
3700void ModuleBitcodeWriter::writeUseListBlock(const Function *F) {
3702 "Expected to be preserving use-list order");
3703
3704 auto hasMore = [&]() {
3706 };
3707 if (!hasMore())
3708
3709 return;
3710
3712 while (hasMore()) {
3713 writeUseList(std::move(VE.UseListOrders.back()));
3715 }
3717}
3718
3719
3720void ModuleBitcodeWriter::writeFunction(
3721 const Function &F,
3722 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex) {
3723
3724
3726
3729
3731
3732
3733
3737
3738
3739 unsigned CstStart, CstEnd;
3741 writeConstants(CstStart, CstEnd, false);
3742
3743
3744 writeFunctionMetadata(F);
3745
3746
3747 unsigned InstID = CstEnd;
3748
3749 bool NeedsMetadataAttachment = F.hasMetadata();
3750
3751 DILocation *LastDL = nullptr;
3752 SmallSetVector<Function *, 4> BlockAddressUsers;
3753
3754
3755 for (const BasicBlock &BB : F) {
3756 for (const Instruction &I : BB) {
3757 writeInstruction(I, InstID, Vals);
3758
3759 if (.getType()->isVoidTy())
3760 ++InstID;
3761
3762
3763 NeedsMetadataAttachment |= I.hasMetadataOtherThanDebugLoc();
3764
3765
3766 if (DILocation *DL = I.getDebugLoc()) {
3767 if (DL == LastDL) {
3768
3770 } else {
3779 FUNCTION_DEBUG_LOC_ABBREV);
3781 LastDL = DL;
3782 }
3783 }
3784
3785
3786
3787
3788
3789 if (I.hasDbgRecords()) {
3790
3791
3792
3793 auto PushValueOrMetadata = [&Vals, InstID,
3794 this](Metadata *RawLocation) {
3795 assert(RawLocation &&
3796 "RawLocation unexpectedly null in DbgVariableRecord");
3798 SmallVector<unsigned, 2> ValAndType;
3799
3800
3801
3802 if (!pushValueAndType(VAM->getValue(), InstID, ValAndType)) {
3804 return true;
3805 }
3806 }
3807
3808
3810 return false;
3811 };
3812
3813
3814
3815
3816 for (DbgRecord &DR : I.DebugMarker->getDbgRecordRange()) {
3822 continue;
3823 }
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3842 FUNCTION_DEBUG_RECORD_VALUE_ABBREV);
3843 else
3851 } else {
3858 }
3860 }
3861 }
3862 }
3863
3866 SmallPtrSet<Value *, 8> Visited{BA};
3867 while (!Worklist.empty()) {
3869 for (User *U : V->users()) {
3873 BlockAddressUsers.insert(P);
3875 Visited.insert(U).second)
3877 }
3878 }
3879 }
3880 }
3881
3882 if (!BlockAddressUsers.empty()) {
3883 Vals.resize(BlockAddressUsers.size());
3888 }
3889
3890
3891 if (auto *Symtab = F.getValueSymbolTable())
3892 writeFunctionLevelValueSymbolTable(*Symtab);
3893
3894 if (NeedsMetadataAttachment)
3895 writeFunctionMetadataAttachment(F);
3897 writeUseListBlock(&F);
3900}
3901
3902
3903void ModuleBitcodeWriter::writeBlockInfo() {
3904
3905
3906
3908
3909
3912
3914
3915 {
3916 auto Abbv = std::make_shared();
3922 VST_ENTRY_8_ABBREV)
3924 }
3925
3926 {
3927 auto Abbv = std::make_shared();
3933 VST_ENTRY_7_ABBREV)
3935 }
3936 {
3937 auto Abbv = std::make_shared();
3943 VST_ENTRY_6_ABBREV)
3945 }
3946 {
3947 auto Abbv = std::make_shared();
3953 VST_BBENTRY_6_ABBREV)
3955 }
3956
3957 {
3958 auto Abbv = std::make_shared();
3960 Abbv->Add(TypeAbbrevOp);
3962 CONSTANTS_SETTYPE_ABBREV)
3964 }
3965
3966 {
3967 auto Abbv = std::make_shared();
3971 CONSTANTS_INTEGER_ABBREV)
3973 }
3974
3975 {
3976 auto Abbv = std::make_shared();
3982
3984 CONSTANTS_CE_CAST_Abbrev)
3986 }
3987 {
3988 auto Abbv = std::make_shared();
3991 CONSTANTS_NULL_Abbrev)
3993 }
3994
3995
3996
3997 {
3998 auto Abbv = std::make_shared();
4000 Abbv->Add(ValAbbrevOp);
4001 Abbv->Add(TypeAbbrevOp);
4005 FUNCTION_INST_LOAD_ABBREV)
4007 }
4008 {
4009 auto Abbv = std::make_shared();
4011 Abbv->Add(ValAbbrevOp);
4012 Abbv->Add(ValAbbrevOp);
4016 FUNCTION_INST_STORE_ABBREV)
4018 }
4019 {
4020 auto Abbv = std::make_shared();
4022 Abbv->Add(ValAbbrevOp);
4025 FUNCTION_INST_UNOP_ABBREV)
4027 }
4028 {
4029 auto Abbv = std::make_shared();
4031 Abbv->Add(ValAbbrevOp);
4035 FUNCTION_INST_UNOP_FLAGS_ABBREV)
4037 }
4038 {
4039 auto Abbv = std::make_shared();
4041 Abbv->Add(ValAbbrevOp);
4042 Abbv->Add(ValAbbrevOp);
4045 FUNCTION_INST_BINOP_ABBREV)
4047 }
4048 {
4049 auto Abbv = std::make_shared();
4051 Abbv->Add(ValAbbrevOp);
4052 Abbv->Add(ValAbbrevOp);
4056 FUNCTION_INST_BINOP_FLAGS_ABBREV)
4058 }
4059 {
4060 auto Abbv = std::make_shared();
4062 Abbv->Add(ValAbbrevOp);
4063 Abbv->Add(TypeAbbrevOp);
4066 FUNCTION_INST_CAST_ABBREV)
4068 }
4069 {
4070 auto Abbv = std::make_shared();
4072 Abbv->Add(ValAbbrevOp);
4073 Abbv->Add(TypeAbbrevOp);
4077 FUNCTION_INST_CAST_FLAGS_ABBREV)
4079 }
4080
4081 {
4082 auto Abbv = std::make_shared();
4085 FUNCTION_INST_RET_VOID_ABBREV)
4087 }
4088 {
4089 auto Abbv = std::make_shared();
4091 Abbv->Add(ValAbbrevOp);
4093 FUNCTION_INST_RET_VAL_ABBREV)
4095 }
4096 {
4097 auto Abbv = std::make_shared();
4099
4100 Abbv->Add(ValAbbrevOp);
4102 FUNCTION_INST_BR_UNCOND_ABBREV)
4104 }
4105 {
4106 auto Abbv = std::make_shared();
4108
4109 Abbv->Add(ValAbbrevOp);
4110 Abbv->Add(ValAbbrevOp);
4111 Abbv->Add(ValAbbrevOp);
4113 FUNCTION_INST_BR_COND_ABBREV)
4115 }
4116 {
4117 auto Abbv = std::make_shared();
4120 FUNCTION_INST_UNREACHABLE_ABBREV)
4122 }
4123 {
4124 auto Abbv = std::make_shared();
4127 Abbv->Add(TypeAbbrevOp);
4129 Abbv->Add(ValAbbrevOp);
4131 FUNCTION_INST_GEP_ABBREV)
4133 }
4134 {
4135 auto Abbv = std::make_shared();
4137 Abbv->Add(ValAbbrevOp);
4138 Abbv->Add(ValAbbrevOp);
4141 FUNCTION_INST_CMP_ABBREV)
4143 }
4144 {
4145 auto Abbv = std::make_shared();
4147 Abbv->Add(ValAbbrevOp);
4148 Abbv->Add(ValAbbrevOp);
4152 FUNCTION_INST_CMP_FLAGS_ABBREV)
4154 }
4155 {
4156 auto Abbv = std::make_shared();
4161 Abbv->Add(ValAbbrevOp);
4163 FUNCTION_DEBUG_RECORD_VALUE_ABBREV)
4165 }
4166 {
4167 auto Abbv = std::make_shared();
4169
4178 FUNCTION_DEBUG_LOC_ABBREV)
4180 }
4182}
4183
4184
4185
4186void IndexBitcodeWriter::writeModStrings() {
4188
4189
4190
4191
4192 auto Abbv = std::make_shared();
4197 unsigned Abbrev8Bit = Stream.EmitAbbrev(std::move(Abbv));
4198
4199
4200 Abbv = std::make_shared();
4205 unsigned Abbrev7Bit = Stream.EmitAbbrev(std::move(Abbv));
4206
4207
4208 Abbv = std::make_shared();
4213 unsigned Abbrev6Bit = Stream.EmitAbbrev(std::move(Abbv));
4214
4215
4216 Abbv = std::make_shared();
4223 unsigned AbbrevHash = Stream.EmitAbbrev(std::move(Abbv));
4224
4226 forEachModule([&](const StringMapEntry &MPSE) {
4228 const auto &Hash = MPSE.getValue();
4230 unsigned AbbrevToUse = Abbrev8Bit;
4232 AbbrevToUse = Abbrev6Bit;
4234 AbbrevToUse = Abbrev7Bit;
4235
4236 auto ModuleId = ModuleIdMap.size();
4237 ModuleIdMap[Key] = ModuleId;
4240
4241
4243
4244
4245 if (llvm::any_of(Hash, [](uint32_t H) { return H; })) {
4246 Vals.assign(Hash.begin(), Hash.end());
4247
4249 }
4250
4252 });
4254}
4255
4256
4257
4258template
4261 Fn GetValueID) {
4262 if (!FS->type_tests().empty())
4264
4266
4267 auto WriteVFuncIdVec = [&](uint64_t Ty,
4269 if (VFs.empty())
4270 return;
4272 for (auto &VF : VFs) {
4273 Record.push_back(VF.GUID);
4274 Record.push_back(VF.Offset);
4275 }
4277 };
4278
4280 FS->type_test_assume_vcalls());
4282 FS->type_checked_load_vcalls());
4283
4284 auto WriteConstVCallVec = [&](uint64_t Ty,
4286 for (auto &VC : VCs) {
4288 Record.push_back(VC.VFunc.GUID);
4289 Record.push_back(VC.VFunc.Offset);
4292 }
4293 };
4294
4296 FS->type_test_assume_const_vcalls());
4298 FS->type_checked_load_const_vcalls());
4299
4302 assert(Range.getLower().getNumWords() == 1);
4303 assert(Range.getUpper().getNumWords() == 1);
4306 };
4307
4308 if (!FS->paramAccesses().empty()) {
4310 for (auto &Arg : FS->paramAccesses()) {
4311 size_t UndoSize = Record.size();
4312 Record.push_back(Arg.ParamNo);
4313 WriteRange(Arg.Use);
4314 Record.push_back(Arg.Calls.size());
4315 for (auto &Call : Arg.Calls) {
4317 std::optional ValueID = GetValueID(Call.Callee);
4318 if (!ValueID) {
4319
4320
4321 Record.resize(UndoSize);
4322 break;
4323 }
4324 Record.push_back(*ValueID);
4325 WriteRange(Call.Offsets);
4326 }
4327 }
4328 if (.empty())
4330 }
4331}
4332
4333
4334static void
4336 std::setGlobalValue::GUID &ReferencedTypeIds) {
4337 if (!FS->type_tests().empty())
4338 for (auto &TT : FS->type_tests())
4339 ReferencedTypeIds.insert(TT);
4340
4341 auto GetReferencedTypesFromVFuncIdVec =
4343 for (auto &VF : VFs)
4344 ReferencedTypeIds.insert(VF.GUID);
4345 };
4346
4347 GetReferencedTypesFromVFuncIdVec(FS->type_test_assume_vcalls());
4348 GetReferencedTypesFromVFuncIdVec(FS->type_checked_load_vcalls());
4349
4350 auto GetReferencedTypesFromConstVCallVec =
4352 for (auto &VC : VCs)
4353 ReferencedTypeIds.insert(VC.VFunc.GUID);
4354 };
4355
4356 GetReferencedTypesFromConstVCallVec(FS->type_test_assume_const_vcalls());
4357 GetReferencedTypesFromConstVCallVec(FS->type_checked_load_const_vcalls());
4358}
4359
4371
4385
4392
4393 NameVals.push_back(Summary.TTRes.TheKind);
4394 NameVals.push_back(Summary.TTRes.SizeM1BitWidth);
4395 NameVals.push_back(Summary.TTRes.AlignLog2);
4396 NameVals.push_back(Summary.TTRes.SizeM1);
4397 NameVals.push_back(Summary.TTRes.BitMask);
4398 NameVals.push_back(Summary.TTRes.InlineBits);
4399
4400 for (auto &W : Summary.WPDRes)
4402 W.second);
4403}
4404
4411
4412 for (auto &P : Summary) {
4413 NameVals.push_back(P.AddressPointOffset);
4414 NameVals.push_back(VE.getValueID(P.VTableVI.getValue()));
4415 }
4416}
4417
4418
4419
4420
4421
4425
4426
4427
4428 static_assert(std::is_same_v<LinearFrameId, unsigned>);
4429 for (auto &AI : FS->allocs()) {
4430 for (auto &MIB : AI.MIBs) {
4432 StackIdIndices.reserve(MIB.StackIdIndices.size());
4433 for (auto Id : MIB.StackIdIndices)
4434 StackIdIndices.push_back(GetStackIndex(Id));
4435
4436 CallStacks.insert({CallStacks.size(), StackIdIndices});
4437 }
4438 }
4439}
4440
4441
4442
4443
4444
4445
4449 assert(!CallStacks.empty());
4453
4454
4455 Builder.build(std::move(CallStacks), nullptr,
4456 FrameHistogram);
4458 RadixAbbrev);
4459 return Builder.takeCallStackPos();
4460}
4461
4464 unsigned AllocAbbrev, unsigned ContextIdAbbvId, bool PerModule,
4465 std::function<unsigned(const ValueInfo &VI)> GetValueID,
4466 std::function<unsigned(unsigned)> GetStackIndex,
4467 bool WriteContextSizeInfoIndex,
4471
4472 for (auto &CI : FS->callsites()) {
4474
4475
4476 assert(!PerModule || (CI.Clones.size() == 1 && CI.Clones[0] == 0));
4477 Record.push_back(GetValueID(CI.Callee));
4478 if (!PerModule) {
4479 Record.push_back(CI.StackIdIndices.size());
4480 Record.push_back(CI.Clones.size());
4481 }
4482 for (auto Id : CI.StackIdIndices)
4483 Record.push_back(GetStackIndex(Id));
4484 if (!PerModule)
4488 Record, CallsiteAbbrev);
4489 }
4490
4491 for (auto &AI : FS->allocs()) {
4493
4494
4495 assert(!PerModule || (AI.Versions.size() == 1 && AI.Versions[0] == 0));
4496 Record.push_back(AI.MIBs.size());
4497 if (!PerModule)
4498 Record.push_back(AI.Versions.size());
4499 for (auto &MIB : AI.MIBs) {
4501
4502
4503
4505
4506 assert(CallStackCount <= CallStackPos.size());
4507 Record.push_back(CallStackPos[CallStackCount++]);
4508 }
4509 }
4510 if (!PerModule)
4512 assert(AI.ContextSizeInfos.empty() ||
4513 AI.ContextSizeInfos.size() == AI.MIBs.size());
4514
4515 if (WriteContextSizeInfoIndex && !AI.ContextSizeInfos.empty()) {
4516
4517
4518
4519 assert(ContextIdAbbvId);
4521
4522
4523 ContextIds.reserve(AI.ContextSizeInfos.size() * 2);
4524 for (auto &Infos : AI.ContextSizeInfos) {
4525 Record.push_back(Infos.size());
4526 for (auto [FullStackId, TotalSize] : Infos) {
4527
4528
4529
4530
4533 Record.push_back(TotalSize);
4534 }
4535 }
4536
4537
4539 ContextIdAbbvId);
4540 }
4546 Record, AllocAbbrev);
4547 }
4548}
4549
4550
4551void ModuleBitcodeWriterBase::writePerModuleFunctionSummaryRecord(
4552 SmallVector<uint64_t, 64> &NameVals, GlobalValueSummary *Summary,
4553 unsigned ValueID, unsigned FSCallsRelBFAbbrev,
4554 unsigned FSCallsProfileAbbrev, unsigned CallsiteAbbrev,
4555 unsigned AllocAbbrev, unsigned ContextIdAbbvId, const Function &F,
4556 DenseMap<CallStackId, LinearCallStackId> &CallStackPos,
4559
4561
4563 Stream, FS, [&](const ValueInfo &VI) -> std::optional {
4565 });
4566
4568 Stream, FS, CallsiteAbbrev, AllocAbbrev, ContextIdAbbvId,
4569 true,
4570 [&](const ValueInfo &VI) { return getValueId(VI); },
4571 [&](unsigned I) { return I; },
4572 true, CallStackPos, CallStackCount);
4573
4574 auto SpecialRefCnts = FS->specialRefCounts();
4579 NameVals.push_back(SpecialRefCnts.first);
4580 NameVals.push_back(SpecialRefCnts.second);
4581
4582 for (auto &RI : FS->refs())
4583 NameVals.push_back(getValueId(RI));
4584
4585 const bool UseRelBFRecord =
4588 for (auto &ECI : FS->calls()) {
4589 NameVals.push_back(getValueId(ECI.first));
4590 if (UseRelBFRecord)
4592 else
4594 }
4595
4596 unsigned FSAbbrev =
4597 (UseRelBFRecord ? FSCallsRelBFAbbrev : FSCallsProfileAbbrev);
4598 unsigned Code =
4600
4601
4602 Stream.EmitRecord(Code, NameVals, FSAbbrev);
4603 NameVals.clear();
4604}
4605
4606
4607
4608void ModuleBitcodeWriterBase::writeModuleLevelReferences(
4609 const GlobalVariable &V, SmallVector<uint64_t, 64> &NameVals,
4610 unsigned FSModRefsAbbrev, unsigned FSModVTableRefsAbbrev) {
4611 auto VI = Index->getValueInfo(V.getGUID());
4612 if (!VI || VI.getSummaryList().empty()) {
4613
4614
4615 assert(V.isDeclaration());
4616 return;
4617 }
4618 auto *Summary = VI.getSummaryList()[0].get();
4623
4624 auto VTableFuncs = VS->vTableFuncs();
4625 if (!VTableFuncs.empty())
4627
4628 unsigned SizeBeforeRefs = NameVals.size();
4629 for (auto &RI : VS->refs())
4631
4632
4634
4635 if (VTableFuncs.empty())
4637 FSModRefsAbbrev);
4638 else {
4639
4640 for (auto &P : VTableFuncs) {
4643 }
4644
4646 FSModVTableRefsAbbrev);
4647 }
4648 NameVals.clear();
4649}
4650
4651
4652
4653void ModuleBitcodeWriterBase::writePerModuleGlobalValueSummary() {
4654
4655
4656 bool IsThinLTO = true;
4657 if (auto *MD =
4659 IsThinLTO = MD->getZExtValue();
4662 4);
4663
4667
4668
4669 uint64_t Flags = 0;
4670
4671 if (Index->enableSplitLTOUnit())
4673 if (Index->hasUnifiedLTO())
4675
4677
4678 if (Index->begin() == Index->end()) {
4680 return;
4681 }
4682
4683 auto Abbv = std::make_shared();
4686
4689 unsigned ValueGuidAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4690
4691 for (const auto &GVI : valueIds()) {
4693 ArrayRef<uint32_t>{GVI.second,
4694 static_cast<uint32_t>(GVI.first >> 32),
4695 static_cast<uint32_t>(GVI.first)},
4696 ValueGuidAbbrev);
4697 }
4698
4699 if (->stackIds().empty()) {
4700 auto StackIdAbbv = std::make_shared();
4702
4704
4705
4707 unsigned StackIdAbbvId = Stream.EmitAbbrev(std::move(StackIdAbbv));
4708 SmallVector<uint32_t> Vals;
4710 for (auto Id : Index->stackIds()) {
4711 Vals.push_back(static_cast<uint32_t>(Id >> 32));
4712 Vals.push_back(static_cast<uint32_t>(Id));
4713 }
4715 }
4716
4717 unsigned ContextIdAbbvId = 0;
4719
4720 auto ContextIdAbbv = std::make_shared();
4723
4724
4725
4726
4727
4730 else
4732 ContextIdAbbvId = Stream.EmitAbbrev(std::move(ContextIdAbbv));
4733 }
4734
4735
4736 Abbv = std::make_shared();
4745
4748 unsigned FSCallsProfileAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4749
4750
4751 Abbv = std::make_shared();
4760
4763 unsigned FSCallsRelBFAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4764
4765
4766 Abbv = std::make_shared();
4772 unsigned FSModRefsAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4773
4774
4775 Abbv = std::make_shared();
4780
4783 unsigned FSModVTableRefsAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4784
4785
4786 Abbv = std::make_shared();
4791 unsigned FSAliasAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4792
4793
4794 Abbv = std::make_shared();
4798
4801 unsigned TypeIdCompatibleVtableAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4802
4803 Abbv = std::make_shared();
4806
4809 unsigned CallsiteAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4810
4811 Abbv = std::make_shared();
4814
4815
4818 unsigned AllocAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4819
4820 Abbv = std::make_shared();
4822
4825 unsigned RadixAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4826
4827
4828
4829
4830
4831
4832 MapVector<CallStackId, llvm::SmallVector> CallStacks;
4833 for (const Function &F : M) {
4834
4835
4836 if (.hasName())
4837 report_fatal_error("Unexpected anonymous function when writing summary");
4838
4839 ValueInfo VI = Index->getValueInfo(F.getGUID());
4840 if (!VI || VI.getSummaryList().empty()) {
4841
4842
4843 assert(F.isDeclaration());
4844 continue;
4845 }
4846 auto *Summary = VI.getSummaryList()[0].get();
4849 FS, [](unsigned I) { return I; }, CallStacks);
4850 }
4851
4852
4853 DenseMap<CallStackId, LinearCallStackId> CallStackPos;
4854 if (!CallStacks.empty()) {
4855 CallStackPos =
4857 }
4858
4859
4861
4862 SmallVector<uint64_t, 64> NameVals;
4863
4864
4865 for (const Function &F : M) {
4866
4867
4868 if (.hasName())
4869 report_fatal_error("Unexpected anonymous function when writing summary");
4870
4871 ValueInfo VI = Index->getValueInfo(F.getGUID());
4872 if (!VI || VI.getSummaryList().empty()) {
4873
4874
4875 assert(F.isDeclaration());
4876 continue;
4877 }
4878 auto *Summary = VI.getSummaryList()[0].get();
4879 writePerModuleFunctionSummaryRecord(
4880 NameVals, Summary, VE.getValueID(&F), FSCallsRelBFAbbrev,
4881 FSCallsProfileAbbrev, CallsiteAbbrev, AllocAbbrev, ContextIdAbbvId, F,
4882 CallStackPos, CallStackCount);
4883 }
4884
4885
4886
4887 for (const GlobalVariable &G : M.globals())
4888 writeModuleLevelReferences(G, NameVals, FSModRefsAbbrev,
4889 FSModVTableRefsAbbrev);
4890
4891 for (const GlobalAlias &A : M.aliases()) {
4892 auto *Aliasee = A.getAliaseeObject();
4893
4894
4896 continue;
4898 auto AliaseeId = VE.getValueID(Aliasee);
4900 auto *Summary = Index->getGlobalValueSummary(A);
4905 NameVals.clear();
4906 }
4907
4908 for (auto &S : Index->typeIdCompatibleVtableMap()) {
4910 S.second, VE);
4912 TypeIdCompatibleVtableAbbrev);
4913 NameVals.clear();
4914 }
4915
4916 if (Index->getBlockCount())
4918 ArrayRef<uint64_t>{Index->getBlockCount()});
4919
4921}
4922
4923
4924void IndexBitcodeWriter::writeCombinedGlobalValueSummary() {
4929
4930
4932
4933 auto Abbv = std::make_shared();
4936
4939 unsigned ValueGuidAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4940
4941 for (const auto &GVI : valueIds()) {
4943 ArrayRef<uint32_t>{GVI.second,
4944 static_cast<uint32_t>(GVI.first >> 32),
4945 static_cast<uint32_t>(GVI.first)},
4946 ValueGuidAbbrev);
4947 }
4948
4949
4950
4951 if (!StackIds.empty()) {
4952 auto StackIdAbbv = std::make_shared();
4954
4956
4957
4959 unsigned StackIdAbbvId = Stream.EmitAbbrev(std::move(StackIdAbbv));
4960 SmallVector<uint32_t> Vals;
4961 Vals.reserve(StackIds.size() * 2);
4962 for (auto Id : StackIds) {
4963 Vals.push_back(static_cast<uint32_t>(Id >> 32));
4964 Vals.push_back(static_cast<uint32_t>(Id));
4965 }
4967 }
4968
4969
4970 Abbv = std::make_shared();
4981
4984 unsigned FSCallsProfileAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4985
4986
4987 Abbv = std::make_shared();
4994 unsigned FSModRefsAbbrev = Stream.EmitAbbrev(std::move(Abbv));
4995
4996
4997 Abbv = std::make_shared();
5003 unsigned FSAliasAbbrev = Stream.EmitAbbrev(std::move(Abbv));
5004
5005 Abbv = std::make_shared();
5010
5013 unsigned CallsiteAbbrev = Stream.EmitAbbrev(std::move(Abbv));
5014
5015 Abbv = std::make_shared();
5021
5022
5023
5026 unsigned AllocAbbrev = Stream.EmitAbbrev(std::move(Abbv));
5027
5028 auto shouldImportValueAsDecl = [&](GlobalValueSummary *GVS) -> bool {
5029 if (DecSummaries == nullptr)
5030 return false;
5031 return DecSummaries->count(GVS);
5032 };
5033
5034
5035
5037
5038
5039 DenseMap<const GlobalValueSummary *, unsigned> SummaryToValueIdMap;
5040
5041 SmallVector<uint64_t, 64> NameVals;
5042
5043
5044
5045 std::setGlobalValue::GUID ReferencedTypeIds;
5046
5047
5048
5049 auto MaybeEmitOriginalName = [&](GlobalValueSummary &S) {
5050
5051
5052
5053
5054
5055
5056
5058 return;
5059 NameVals.push_back(S.getOriginalName());
5061 NameVals.clear();
5062 };
5063
5064 DenseMap<CallStackId, LinearCallStackId> CallStackPos;
5066 Abbv = std::make_shared();
5068
5071 unsigned RadixAbbrev = Stream.EmitAbbrev(std::move(Abbv));
5072
5073
5074
5075
5076
5077
5078 MapVector<CallStackId, llvm::SmallVector> CallStacks;
5079 forEachSummary([&](GVInfo I, bool IsAliasee) {
5080
5081
5082
5083 if (IsAliasee)
5084 return;
5085 GlobalValueSummary *S = I.second;
5088 if (!FS)
5089 return;
5091 FS,
5092
5093 [&](unsigned I) {
5094
5095
5096
5098 return StackIdIndicesToIndex[I];
5099 },
5100 CallStacks);
5101 });
5102
5103
5104 if (!CallStacks.empty()) {
5106 RadixAbbrev);
5107 }
5108 }
5109
5110
5111
5113
5114 DenseSetGlobalValue::GUID DefOrUseGUIDs;
5115 forEachSummary([&](GVInfo I, bool IsAliasee) {
5116 GlobalValueSummary *S = I.second;
5118 DefOrUseGUIDs.insert(I.first);
5119 for (const ValueInfo &VI : S->refs())
5120 DefOrUseGUIDs.insert(VI.getGUID());
5121
5122 auto ValueId = getValueId(I.first);
5124 SummaryToValueIdMap[S] = *ValueId;
5125
5126
5127
5128
5129 if (IsAliasee)
5130 return;
5131
5133
5134
5136 return;
5137 }
5138
5142 NameVals.push_back(ModuleIdMap[VS->modulePath()]);
5146 for (auto &RI : VS->refs()) {
5147 auto RefValueId = getValueId(RI.getGUID());
5148 if (!RefValueId)
5149 continue;
5150 NameVals.push_back(*RefValueId);
5151 }
5152
5153
5155 FSModRefsAbbrev);
5156 NameVals.clear();
5157 MaybeEmitOriginalName(*S);
5158 return;
5159 }
5160
5161 auto GetValueId = [&](const ValueInfo &VI) -> std::optional {
5162 if (!VI)
5163 return std::nullopt;
5164 return getValueId(VI.getGUID());
5165 };
5166
5170
5172 Stream, FS, CallsiteAbbrev, AllocAbbrev, 0,
5173 false,
5174
5175 [&](const ValueInfo &VI) -> unsigned {
5176 std::optional ValueID = GetValueId(VI);
5177
5178
5179
5180
5181 if (!ValueID)
5182 return 0;
5183 return *ValueID;
5184 },
5185
5186 [&](unsigned I) {
5187
5188
5189
5191 return StackIdIndicesToIndex[I];
5192 },
5193 false, CallStackPos, CallStackCount);
5194
5197 NameVals.push_back(ModuleIdMap[FS->modulePath()]);
5202
5203 NameVals.push_back(0 );
5204
5205
5206 NameVals.push_back(0);
5207 NameVals.push_back(0);
5208 NameVals.push_back(0);
5209
5210 unsigned Count = 0, RORefCnt = 0, WORefCnt = 0;
5211 for (auto &RI : FS->refs()) {
5212 auto RefValueId = getValueId(RI.getGUID());
5213 if (!RefValueId)
5214 continue;
5215 NameVals.push_back(*RefValueId);
5216 if (RI.isReadOnly())
5217 RORefCnt++;
5218 else if (RI.isWriteOnly())
5219 WORefCnt++;
5221 }
5222 NameVals[6] = Count;
5223 NameVals[7] = RORefCnt;
5224 NameVals[8] = WORefCnt;
5225
5226 for (auto &EI : FS->calls()) {
5227
5228
5229 std::optional CallValueId = GetValueId(EI.first);
5230 if (!CallValueId)
5231 continue;
5232 NameVals.push_back(*CallValueId);
5234 }
5235
5236
5238 FSCallsProfileAbbrev);
5239 NameVals.clear();
5240 MaybeEmitOriginalName(*S);
5241 });
5242
5243 for (auto *AS : Aliases) {
5244 auto AliasValueId = SummaryToValueIdMap[AS];
5245 assert(AliasValueId);
5246 NameVals.push_back(AliasValueId);
5251
5252
5253 auto AliaseeValueId =
5255 NameVals.push_back(AliaseeValueId);
5256
5257
5259 NameVals.clear();
5260 MaybeEmitOriginalName(*AS);
5261
5265 }
5266
5268 auto EmitCfiFunctions = [&](const CfiFunctionIndex &CfiIndex,
5270 if (CfiIndex.empty())
5271 return;
5273 auto Defs = CfiIndex.forGuid(GUID);
5275 }
5276 if (Functions.empty())
5277 return;
5279 for (const auto &S : Functions) {
5282 }
5284 NameVals.clear();
5285 Functions.clear();
5286 };
5287
5290
5291
5292
5293 for (auto &T : ReferencedTypeIds) {
5294 auto TidIter = Index.typeIds().equal_range(T);
5295 for (const auto &[GUID, TypeIdPair] : make_range(TidIter)) {
5297 TypeIdPair.second);
5299 NameVals.clear();
5300 }
5301 }
5302
5303 if (Index.getBlockCount())
5305 ArrayRef<uint64_t>{Index.getBlockCount()});
5306
5308}
5309
5310
5311
5314
5315
5316 auto Abbv = std::make_shared();
5320 auto StringAbbrev = Stream.EmitAbbrev(std::move(Abbv));
5322 "LLVM" LLVM_VERSION_STRING, StringAbbrev);
5323
5324
5325 Abbv = std::make_shared();
5328 auto EpochAbbrev = Stream.EmitAbbrev(std::move(Abbv));
5332}
5333
5334void ModuleBitcodeWriter::writeModuleHash(StringRef View) {
5335
5336
5337 if (GenerateHash) {
5338 uint32_t Vals[5];
5339 Hasher.update(ArrayRef<uint8_t>(
5340 reinterpret_cast<const uint8_t *>(View.data()), View.size()));
5341 std::array<uint8_t, 20> Hash = Hasher.result();
5342 for (int Pos = 0; Pos < 20; Pos += 4) {
5344 }
5345
5346
5348
5349 if (ModHash)
5350
5351 llvm::copy(Vals, std::begin(*ModHash));
5352 }
5353}
5354
5355void ModuleBitcodeWriter::write() {
5357
5359
5360
5362
5363 writeModuleVersion();
5364
5365
5366 writeBlockInfo();
5367
5368
5369 writeTypeTable();
5370
5371
5372 writeAttributeGroupTable();
5373
5374
5375 writeAttributeTable();
5376
5377 writeComdats();
5378
5379
5380
5381 writeModuleInfo();
5382
5383
5384 writeModuleConstants();
5385
5386
5387 writeModuleMetadataKinds();
5388
5389
5390 writeModuleMetadata();
5391
5392
5394 writeUseListBlock(nullptr);
5395
5396 writeOperandBundleTags();
5397 writeSyncScopeNames();
5398
5399
5400 DenseMap<const Function *, uint64_t> FunctionToBitcodeIndex;
5401 for (const Function &F : M)
5402 if (.isDeclaration())
5403 writeFunction(F, FunctionToBitcodeIndex);
5404
5405
5406
5407 if (Index)
5408 writePerModuleGlobalValueSummary();
5409
5410 writeGlobalValueSymbolTable(FunctionToBitcodeIndex);
5411
5413
5415}
5416
5420 Position += 4;
5421}
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5438 unsigned CPUType = ~0U;
5439
5440
5441
5442
5443
5444 enum {
5445 DARWIN_CPU_ARCH_ABI64 = 0x01000000,
5446 DARWIN_CPU_TYPE_X86 = 7,
5447 DARWIN_CPU_TYPE_ARM = 12,
5448 DARWIN_CPU_TYPE_POWERPC = 18
5449 };
5450
5453 CPUType = DARWIN_CPU_TYPE_X86 | DARWIN_CPU_ARCH_ABI64;
5455 CPUType = DARWIN_CPU_TYPE_X86;
5457 CPUType = DARWIN_CPU_TYPE_POWERPC;
5459 CPUType = DARWIN_CPU_TYPE_POWERPC | DARWIN_CPU_ARCH_ABI64;
5461 CPUType = DARWIN_CPU_TYPE_ARM;
5462
5463
5465 "Expected header size to be reserved");
5468
5469
5470 unsigned Position = 0;
5476
5477
5478 while (Buffer.size() & 15)
5480}
5481
5482
5484
5485 Stream.Emit((unsigned)'B', 8);
5486 Stream.Emit((unsigned)'C', 8);
5487 Stream.Emit(0x0, 4);
5488 Stream.Emit(0xC, 4);
5489 Stream.Emit(0xE, 4);
5490 Stream.Emit(0xD, 4);
5491}
5492
5497
5502
5504
5505void BitcodeWriter::writeBlob(unsigned Block, unsigned Record, StringRef Blob) {
5507
5508 auto Abbv = std::make_shared();
5511 auto AbbrevNo = Stream->EmitAbbrev(std::move(Abbv));
5512
5514
5516}
5517
5519 assert(!WroteStrtab && !WroteSymtab);
5520
5521
5522
5523
5524 for (Module *M : Mods) {
5525 if (M->getModuleInlineAsm().empty())
5526 continue;
5527
5528 std::string Err;
5529 const Triple TT(M->getTargetTriple());
5531 if ( ||
->hasMCAsmParser())
5532 return;
5533 }
5534
5535 WroteSymtab = true;
5537
5538
5539
5540
5543 return;
5544 }
5545
5547 {Symtab.data(), Symtab.size()});
5548}
5549
5551 assert(!WroteStrtab);
5552
5553 std::vector Strtab;
5554 StrtabBuilder.finalizeInOrder();
5555 Strtab.resize(StrtabBuilder.getSize());
5556 StrtabBuilder.write((uint8_t *)Strtab.data());
5557
5559 {Strtab.data(), Strtab.size()});
5560
5561 WroteStrtab = true;
5562}
5563
5566 WroteStrtab = true;
5567}
5568
5570 bool ShouldPreserveUseListOrder,
5572 bool GenerateHash, ModuleHash *ModHash) {
5573 assert(!WroteStrtab);
5574
5575
5576
5577
5578
5579 assert(M.isMaterialized());
5580 Mods.push_back(const_cast<Module *>(&M));
5581
5582 ModuleBitcodeWriter ModuleWriter(M, StrtabBuilder, *Stream,
5583 ShouldPreserveUseListOrder, Index,
5584 GenerateHash, ModHash);
5585 ModuleWriter.write();
5586}
5587
5592 IndexBitcodeWriter IndexWriter(*Stream, StrtabBuilder, *Index, DecSummaries,
5593 ModuleToSummariesForIndex);
5594 IndexWriter.write();
5595}
5596
5597
5599 bool ShouldPreserveUseListOrder,
5601 bool GenerateHash, ModuleHash *ModHash) {
5603 Writer.writeModule(M, ShouldPreserveUseListOrder, Index, GenerateHash,
5604 ModHash);
5605 Writer.writeSymtab();
5606 Writer.writeStrtab();
5607 };
5608 Triple TT(M.getTargetTriple());
5609 if (TT.isOSDarwin() || TT.isOSBinFormatMachO()) {
5610
5611
5612
5613
5615 Buffer.reserve(256 * 1024);
5621 } else {
5624 }
5625}
5626
5627void IndexBitcodeWriter::write() {
5629
5630 writeModuleVersion();
5631
5632
5633 writeModStrings();
5634
5635
5636 writeCombinedGlobalValueSummary();
5637
5639}
5640
5641
5642
5643
5644
5650 Buffer.reserve(256 * 1024);
5651
5653 Writer.writeIndex(&Index, ModuleToSummariesForIndex, DecSummaries);
5655
5657}
5658
5659namespace {
5660
5661
5662class ThinLinkBitcodeWriter : public ModuleBitcodeWriterBase {
5663
5664
5666
5667public:
5672 : ModuleBitcodeWriterBase(M, StrtabBuilder, Stream,
5673 false, &Index),
5674 ModHash(&ModHash) {}
5675
5677
5678private:
5679 void writeSimplifiedModuleInfo();
5680};
5681
5682}
5683
5684
5685
5686
5687
5688
5689void ThinLinkBitcodeWriter::writeSimplifiedModuleInfo() {
5691
5692 {
5699
5700
5701 auto Abbv = std::make_shared();
5704 Abbv->Add(AbbrevOpToUse);
5705 unsigned FilenameAbbrev = Stream.EmitAbbrev(std::move(Abbv));
5706
5707 for (const auto P : M.getSourceFileName())
5709
5712 }
5713
5714
5716
5717 Vals.push_back(StrtabBuilder.add(GV.getName()));
5718 Vals.push_back(GV.getName().size());
5723
5726 }
5727
5728
5730
5737
5740 }
5741
5742
5744
5751
5754 }
5755
5756
5758
5765
5768 }
5769}
5770
5771void ThinLinkBitcodeWriter::write() {
5773
5774 writeModuleVersion();
5775
5776 writeSimplifiedModuleInfo();
5777
5778 writePerModuleGlobalValueSummary();
5779
5780
5782
5784}
5785
5789 assert(!WroteStrtab);
5790
5791
5792
5793
5794
5795 assert(M.isMaterialized());
5796 Mods.push_back(const_cast<Module *>(&M));
5797
5798 ThinLinkBitcodeWriter ThinLinkWriter(M, StrtabBuilder, *Stream, Index,
5799 ModHash);
5800 ThinLinkWriter.write();
5801}
5802
5803
5804
5805
5810 Buffer.reserve(256 * 1024);
5811
5816
5818}
5819
5821 switch (T.getObjectFormat()) {
5823 return "__LLVM,__bitcode";
5828 return ".llvmbc";
5831 break;
5834 return ".llvmbc";
5836 break;
5839 break;
5842 break;
5843 }
5845}
5846
5848 switch (T.getObjectFormat()) {
5850 return "__LLVM,__cmdline";
5855 return ".llvmcmd";
5858 break;
5861 return ".llvmcmd";
5863 break;
5866 break;
5869 break;
5870 }
5872}
5873
5876 const std::vector<uint8_t> &CmdArgs) {
5877
5881 Type *UsedElementType = Used ? Used->getValueType()->getArrayElementType()
5883 for (auto *GV : UsedGlobals) {
5884 if (GV->getName() != "llvm.embedded.module" &&
5885 GV->getName() != "llvm.cmdline")
5888 }
5889 if (Used)
5890 Used->eraseFromParent();
5891
5892
5893 std::string Data;
5895 Triple T(M.getTargetTriple());
5896
5900 (const unsigned char *)Buf.getBufferEnd())) {
5901
5902
5905 ModuleData =
5907 } else
5908
5911 }
5916 ModuleConstant);
5918
5919
5924 M.getGlobalVariable("llvm.embedded.module", true)) {
5925 assert(Old->hasZeroLiveUses() &&
5926 "llvm.embedded.module can only be used once in llvm.compiler.used");
5928 Old->eraseFromParent();
5929 } else {
5930 GV->setName("llvm.embedded.module");
5931 }
5932
5933
5934 if (EmbedCmdline) {
5935
5937 CmdArgs.size());
5942 CmdConstant);
5947 if (llvm::GlobalVariable *Old = M.getGlobalVariable("llvm.cmdline", true)) {
5948 assert(Old->hasZeroLiveUses() &&
5949 "llvm.cmdline can only be used once in llvm.compiler.used");
5951 Old->eraseFromParent();
5952 } else {
5953 GV->setName("llvm.cmdline");
5954 }
5955 }
5956
5957 if (UsedArray.empty())
5958 return;
5959
5960
5965 NewUsed->setSection("llvm.metadata");
5966}
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)
Definition BitcodeWriter.cpp:4462
static unsigned serializeSanitizerMetadata(const GlobalValue::SanitizerMetadata &Meta)
Definition BitcodeWriter.cpp:1485
static void writeTypeIdCompatibleVtableSummaryRecord(SmallVector< uint64_t, 64 > &NameVals, StringTableBuilder &StrtabBuilder, StringRef Id, const TypeIdCompatibleVtableInfo &Summary, ValueEnumerator &VE)
Definition BitcodeWriter.cpp:4405
static void getReferencedTypeIds(FunctionSummary *FS, std::set< GlobalValue::GUID > &ReferencedTypeIds)
Collect type IDs from type tests used by function.
Definition BitcodeWriter.cpp:4335
static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind)
Definition BitcodeWriter.cpp:757
static void collectMemProfCallStacks(FunctionSummary *FS, std::function< LinearFrameId(unsigned)> GetStackIndex, MapVector< CallStackId, llvm::SmallVector< LinearFrameId > > &CallStacks)
Definition BitcodeWriter.cpp:4422
static unsigned getEncodedUnaryOpcode(unsigned Opcode)
Definition BitcodeWriter.cpp:665
static void emitSignedInt64(SmallVectorImpl< uint64_t > &Vals, uint64_t V)
Definition BitcodeWriter.cpp:973
StringEncoding
Definition BitcodeWriter.cpp:1465
@ SE_Char6
Definition BitcodeWriter.cpp:1465
@ SE_Fixed7
Definition BitcodeWriter.cpp:1465
@ SE_Fixed8
Definition BitcodeWriter.cpp:1465
static unsigned getEncodedVisibility(const GlobalValue &GV)
Definition BitcodeWriter.cpp:1367
static uint64_t getOptimizationFlags(const Value *V)
Definition BitcodeWriter.cpp:1736
static unsigned getEncodedLinkage(const GlobalValue::LinkageTypes Linkage)
Definition BitcodeWriter.cpp:1272
static cl::opt< bool > PreserveBitcodeUseListOrder("preserve-bc-uselistorder", cl::Hidden, cl::init(true), cl::desc("Preserve use-list order when writing LLVM bitcode."))
static unsigned getEncodedRMWOperation(AtomicRMWInst::BinOp Op)
Definition BitcodeWriter.cpp:696
static unsigned getEncodedThreadLocalMode(const GlobalValue &GV)
Definition BitcodeWriter.cpp:1385
static DenseMap< CallStackId, LinearCallStackId > writeMemoryProfileRadixTree(MapVector< CallStackId, llvm::SmallVector< LinearFrameId > > &&CallStacks, BitstreamWriter &Stream, unsigned RadixAbbrev)
Definition BitcodeWriter.cpp:4446
static void writeIdentificationBlock(BitstreamWriter &Stream)
Create the "IDENTIFICATION_BLOCK_ID" containing a single string with the current llvm version,...
Definition BitcodeWriter.cpp:5312
static unsigned getEncodedCastOpcode(unsigned Opcode)
Definition BitcodeWriter.cpp:645
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)
Definition BitcodeWriter.cpp:729
static unsigned getEncodedUnnamedAddr(const GlobalValue &GV)
Definition BitcodeWriter.cpp:1412
static unsigned getEncodedComdatSelectionKind(const Comdat &C)
Definition BitcodeWriter.cpp:1396
static uint64_t getEncodedGVSummaryFlags(GlobalValueSummary::GVFlags Flags, bool ImportAsDecl=false)
Definition BitcodeWriter.cpp:1321
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...
Definition BitcodeWriter.cpp:5436
static void writeBitcodeHeader(BitstreamWriter &Stream)
Helper to write the header common to all bitcode files.
Definition BitcodeWriter.cpp:5483
static uint64_t getEncodedRelBFCallEdgeInfo(const CalleeInfo &CI)
Definition BitcodeWriter.cpp:1358
static void writeWholeProgramDevirtResolutionByArg(SmallVector< uint64_t, 64 > &NameVals, const std::vector< uint64_t > &args, const WholeProgramDevirtResolution::ByArg &ByArg)
Definition BitcodeWriter.cpp:4360
static void emitConstantRange(SmallVectorImpl< uint64_t > &Record, const ConstantRange &CR, bool EmitBitWidth)
Definition BitcodeWriter.cpp:991
static StringEncoding getStringEncoding(StringRef Str)
Determine the encoding to use for the given string name and length.
Definition BitcodeWriter.cpp:1468
static uint64_t getEncodedGVarFlags(GlobalVarSummary::GVarFlags Flags)
Definition BitcodeWriter.cpp:1343
static const char * getSectionNameForCommandline(const Triple &T)
Definition BitcodeWriter.cpp:5847
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)
Definition BitcodeWriter.cpp:4386
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 ...
Definition BitcodeWriter.cpp:4259
static uint64_t getEncodedHotnessCallEdgeInfo(const CalleeInfo &CI)
Definition BitcodeWriter.cpp:1349
static void emitWideAPInt(SmallVectorImpl< uint64_t > &Vals, const APInt &A)
Definition BitcodeWriter.cpp:980
static void writeStringRecord(BitstreamWriter &Stream, unsigned Code, StringRef Str, unsigned AbbrevToUse)
Definition BitcodeWriter.cpp:742
static void writeWholeProgramDevirtResolution(SmallVector< uint64_t, 64 > &NameVals, StringTableBuilder &StrtabBuilder, uint64_t Id, const WholeProgramDevirtResolution &Wpd)
Definition BitcodeWriter.cpp:4372
static unsigned getEncodedDLLStorageClass(const GlobalValue &GV)
Definition BitcodeWriter.cpp:1376
static void writeInt32ToBuffer(uint32_t Value, SmallVectorImpl< char > &Buffer, uint32_t &Position)
Definition BitcodeWriter.cpp:5417
MetadataAbbrev
Definition BitcodeWriter.cpp:2519
@ LastPlusOne
Definition BitcodeWriter.cpp:2522
static const char * getSectionNameForBitcode(const Triple &T)
Definition BitcodeWriter.cpp:5820
static cl::opt< bool > CombinedIndexMemProfContext("combined-index-memprof-context", cl::Hidden, cl::init(true), cl::desc(""))
static unsigned getEncodedBinaryOpcode(unsigned Opcode)
Definition BitcodeWriter.cpp:672
static uint64_t getEncodedFFlags(FunctionSummary::FFlags Flags)
Definition BitcodeWriter.cpp:1304
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...
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...
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"))
Machine Check Debug Module
ModuleSummaryIndex.h This file contains the declarations the classes that hold the module index and s...
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
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]
Class for arbitrary precision integers.
unsigned getActiveWords() const
Compute the number of active words in the value of this APInt.
const uint64_t * getRawData() const
This function returns a pointer to the internal storage of the APInt.
int64_t getSExtValue() const
Get sign extended value.
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),...
size_t size() const
size - Get the array size.
bool empty() const
empty - Check if the array is empty.
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.
@ 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
@ 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.
@ 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.
bool hasAttributes() const
Return true if attributes exists in this set.
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results,...
@ TombstoneKey
Use as Tombstone key for DenseMap of AttrKind.
@ None
No attributes have been set.
@ EmptyKey
Use as Empty key for DenseMap of AttrKind.
@ EndAttrKinds
Sentinel value useful for loops.
BitCodeAbbrevOp - This describes one or more operands in an abbreviation.
static bool isChar6(char C)
isChar6 - Return true if this character is legal in the Char6 encoding.
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...
Definition BitcodeWriter.cpp:5786
LLVM_ABI void writeIndex(const ModuleSummaryIndex *Index, const ModuleToSummariesForIndexTy *ModuleToSummariesForIndex, const GVSummaryPtrSet *DecSummaries)
Definition BitcodeWriter.cpp:5588
LLVM_ABI void copyStrtab(StringRef Strtab)
Copy the string table for another module into this bitcode file.
Definition BitcodeWriter.cpp:5564
LLVM_ABI void writeStrtab()
Write the bitcode file's string table.
Definition BitcodeWriter.cpp:5550
LLVM_ABI ~BitcodeWriter()
Definition BitcodeWriter.cpp:5503
LLVM_ABI void writeSymtab()
Attempt to write a symbol table to the bitcode file.
Definition BitcodeWriter.cpp:5518
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.
Definition BitcodeWriter.cpp:5569
LLVM_ABI BitcodeWriter(SmallVectorImpl< char > &Buffer)
Create a BitcodeWriter that writes to Buffer.
Definition BitcodeWriter.cpp:5493
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 isMustTailCall() const
iterator_range< NestedIterator > forGuid(GlobalValue::GUID GUID) const
@ Largest
The linker will choose the largest COMDAT.
@ SameSize
The data referenced by the COMDAT must be the same size.
@ Any
The linker may choose any COMDAT.
@ NoDeduplicate
No deduplication is performed.
@ ExactMatch
The data referenced by the COMDAT must be the same.
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...
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.
DebugLoc getDebugLoc() const
LLVM_ABI DIAssignID * getAssignID() const
DIExpression * getExpression() const
DILocalVariable * getVariable() const
bool isDbgDeclareValue() const
Metadata * getRawLocation() const
Returns the metadata operand for the first location description.
bool isDbgDeclare() const
Metadata * getRawAddress() const
DIExpression * getAddressExpression() const
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
bool contains(const_arg_type_t< KeyT > Val) const
Return true if the specified key is in the map, false otherwise.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Lightweight error class with error context and mandatory checking.
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.
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.
@ DLLImportStorageClass
Function to be imported from DLL.
@ DefaultVisibility
The GV is visible.
@ HiddenVisibility
The GV is hidden.
@ ProtectedVisibility
The GV is protected.
UnnamedAddr getUnnamedAddr() const
LinkageTypes
An enumeration for the kinds of linkage for global values.
@ PrivateLinkage
Like Internal, but omit from symbol table.
@ CommonLinkage
Tentative definitions.
@ InternalLinkage
Rename collisions when linking (static functions).
@ LinkOnceAnyLinkage
Keep one copy of function when linking (inline)
@ WeakODRLinkage
Same, but only replaced by something equivalent.
@ ExternalLinkage
Externally visible function.
@ WeakAnyLinkage
Keep one copy of named function when linking (weak)
@ AppendingLinkage
Special purpose, only applies to global arrays.
@ AvailableExternallyLinkage
Available for inspection, not emission.
@ ExternalWeakLinkage
ExternalWeak linkage description.
@ LinkOnceODRLinkage
Same, but only replaced by something equivalent.
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 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.
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.
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.
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().
size_type size() const
Determine the number of elements in the SetVector.
bool empty() const
Determine if the SetVector is empty or not.
bool insert(const value_type &X)
Insert a new element into the SetVector.
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.
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 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 - Represent a constant reference to a string, i.e.
constexpr bool empty() const
empty - Check if the string is empty.
constexpr size_t size() const
size - Get the string size.
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.
The instances of the Type class are immutable: once they are created, they are never changed.
bool isX86_FP80Ty() const
Return true if this is x86 long double.
bool isFloatTy() const
Return true if this is 'float', a 32-bit IEEE fp type.
bool isBFloatTy() const
Return true if this is 'bfloat', a 16-bit bfloat type.
bool isPPC_FP128Ty() const
Return true if this is powerpc long double.
bool isFP128Ty() const
Return true if this is 'fp128'.
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
bool isHalfTy() const
Return true if this is 'half', a 16-bit IEEE fp type.
bool isDoubleTy() const
Return true if this is 'double', a 64-bit IEEE fp type.
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.
Type * getType() const
All values are typed, get the type of this value.
LLVM_ABI void setName(const Twine &Name)
Change the name of the value.
LLVM_ABI LLVMContext & getContext() const
All values hold a context through their type.
LLVM_ABI void takeName(Value *V)
Transfer the name from V to this value.
std::pair< iterator, bool > insert(const ValueT &V)
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.
raw_ostream & write(unsigned char C)
A raw_ostream that writes to an std::string.
std::string & str()
Returns the string's reference.
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.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
@ C
The default llvm calling convention, compatible with C.
Predicate getPredicate(unsigned Condition, unsigned Hint)
Return predicate consisting of specified condition and hint bits.
@ CE
Windows NT (Windows on ARM)
@ TYPE_CODE_OPAQUE_POINTER
@ METADATA_TEMPLATE_VALUE
@ METADATA_LEXICAL_BLOCK_FILE
@ METADATA_SUBROUTINE_TYPE
@ METADATA_GLOBAL_DECL_ATTACHMENT
@ METADATA_IMPORTED_ENTITY
@ METADATA_GENERIC_SUBRANGE
@ METADATA_COMPOSITE_TYPE
@ METADATA_FIXED_POINT_TYPE
@ METADATA_GLOBAL_VAR_EXPR
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_COMBINED_CALLSITE_INFO
@ FS_PERMODULE_CALLSITE_INFO
@ FS_PERMODULE_ALLOC_INFO
@ FS_TYPE_CHECKED_LOAD_CONST_VCALL
@ IDENTIFICATION_CODE_EPOCH
@ IDENTIFICATION_CODE_STRING
@ CST_CODE_DSO_LOCAL_EQUIVALENT
@ 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_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_NULL_POINTER_IS_VALID
@ ATTR_KIND_SANITIZE_HWADDRESS
@ ATTR_KIND_RETURNS_TWICE
@ ATTR_KIND_SHADOWCALLSTACK
@ ATTR_KIND_OPT_FOR_FUZZING
@ ATTR_KIND_SANITIZE_NUMERICAL_STABILITY
@ ATTR_KIND_ALLOCATED_POINTER
@ ATTR_KIND_DISABLE_SANITIZER_INSTRUMENTATION
@ ATTR_KIND_CORO_ELIDE_SAFE
@ ATTR_KIND_NON_LAZY_BIND
@ ATTR_KIND_DEREFERENCEABLE
@ ATTR_KIND_OPTIMIZE_NONE
@ ATTR_KIND_HYBRID_PATCHABLE
@ 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_SANITIZE_ALLOC_TOKEN
@ ATTR_KIND_NO_SANITIZE_COVERAGE
@ ATTR_KIND_NO_CREATE_UNDEF_OR_POISON
@ 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
@ SYNC_SCOPE_NAMES_BLOCK_ID
@ PARAMATTR_GROUP_BLOCK_ID
@ IDENTIFICATION_BLOCK_ID
@ GLOBALVAL_SUMMARY_BLOCK_ID
@ FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID
@ OPERAND_BUNDLE_TAGS_BLOCK_ID
@ MODULE_CODE_SOURCE_FILENAME
@ MODULE_CODE_SECTIONNAME
@ FUNC_CODE_INST_CATCHRET
@ FUNC_CODE_INST_LANDINGPAD
@ FUNC_CODE_INST_EXTRACTVAL
@ FUNC_CODE_INST_CATCHPAD
@ FUNC_CODE_INST_CATCHSWITCH
@ 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_RECORD_DECLARE_VALUE
@ FUNC_CODE_DEBUG_LOC_AGAIN
@ FUNC_CODE_INST_EXTRACTELT
@ FUNC_CODE_INST_INDIRECTBR
@ FUNC_CODE_DEBUG_RECORD_VALUE_SIMPLE
@ FUNC_CODE_INST_INSERTVAL
@ FUNC_CODE_DECLAREBLOCKS
@ FUNC_CODE_DEBUG_RECORD_LABEL
@ FUNC_CODE_INST_INSERTELT
@ FUNC_CODE_BLOCKADDR_USERS
@ FUNC_CODE_INST_CLEANUPPAD
@ FUNC_CODE_INST_SHUFFLEVEC
@ 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.
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.
llvm::unique_function< void(llvm::Expected< T >)> Callback
A Callback is a void function that accepts Expected.
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract_or_null(Y &&MD)
Extract a Value from Metadata, allowing null.
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)...
NodeAddr< CodeNode * > Code
void write32le(void *P, uint32_t V)
uint32_t read32be(const void *P)
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.
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
FunctionAddr VTableAddr Value
StringMapEntry< Value * > ValueName
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.
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.
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.
Definition BitcodeWriter.cpp:5598
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
decltype(auto) dyn_cast(const From &Val)
dyn_cast - Return the argument parameter cast to the specified type.
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...
Definition BitcodeWriter.cpp:5806
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
FunctionSummary::ForceSummaryHotnessType ForceSummaryEdgesCold
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
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 ...
Definition BitcodeWriter.cpp:5645
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 (....
Definition BitcodeWriter.cpp:5874
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
FunctionAddr VTableAddr uintptr_t uintptr_t Version
void sort(IteratorTy Start, IteratorTy End)
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
FunctionAddr VTableAddr Count
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 - Return true if the parameter to the template is an instance of one of the template type argu...
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
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
OutputIt copy(R &&Range, OutputIt Out)
constexpr unsigned BitWidth
LLVM_ABI Error write(MCStreamer &Out, ArrayRef< std::string > Inputs, OnCuIndexOverflow OverflowOptValue, Dwarf64StrOffsetsPromotion StrOffsetsOptValue)
decltype(auto) cast(const From &Val)
cast - Return the argument parameter cast to the specified type.
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.
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 ...
This struct is a compact representation of a valid (non-zero power of two) alignment.
static void set(StorageType &Packed, typename Bitfield::Type Value)
Sets the typed value in the provided Packed value.
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,...
std::string SingleImplName