1 //=-- InstrProf.cpp - Instrumented profiling format support -----------------=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains support for clang's instrumentation based PGO and
13 //===----------------------------------------------------------------------===//
15 #include "llvm/ProfileData/InstrProf.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/IR/GlobalVariable.h"
20 #include "llvm/IR/MDBuilder.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/Support/Compression.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/LEB128.h"
25 #include "llvm/Support/ManagedStatic.h"
30 class InstrProfErrorCategoryType : public std::error_category {
31 const char *name() const LLVM_NOEXCEPT override { return "llvm.instrprof"; }
32 std::string message(int IE) const override {
33 instrprof_error E = static_cast<instrprof_error>(IE);
35 case instrprof_error::success:
37 case instrprof_error::eof:
39 case instrprof_error::unrecognized_format:
40 return "Unrecognized instrumentation profile encoding format";
41 case instrprof_error::bad_magic:
42 return "Invalid instrumentation profile data (bad magic)";
43 case instrprof_error::bad_header:
44 return "Invalid instrumentation profile data (file header is corrupt)";
45 case instrprof_error::unsupported_version:
46 return "Unsupported instrumentation profile format version";
47 case instrprof_error::unsupported_hash_type:
48 return "Unsupported instrumentation profile hash type";
49 case instrprof_error::too_large:
50 return "Too much profile data";
51 case instrprof_error::truncated:
52 return "Truncated profile data";
53 case instrprof_error::malformed:
54 return "Malformed instrumentation profile data";
55 case instrprof_error::unknown_function:
56 return "No profile data available for function";
57 case instrprof_error::hash_mismatch:
58 return "Function control flow change detected (hash mismatch)";
59 case instrprof_error::count_mismatch:
60 return "Function basic block count change detected (counter mismatch)";
61 case instrprof_error::counter_overflow:
62 return "Counter overflow";
63 case instrprof_error::value_site_count_mismatch:
64 return "Function value site count change detected (counter mismatch)";
65 case instrprof_error::compress_failed:
66 return "Failed to compress data (zlib)";
67 case instrprof_error::uncompress_failed:
68 return "Failed to uncompress data (zlib)";
70 llvm_unreachable("A value of instrprof_error has no message.");
73 } // end anonymous namespace
75 static ManagedStatic<InstrProfErrorCategoryType> ErrorCategory;
77 const std::error_category &llvm::instrprof_category() {
78 return *ErrorCategory;
83 void SoftInstrProfErrors::addError(instrprof_error IE) {
84 if (IE == instrprof_error::success)
87 if (FirstError == instrprof_error::success)
91 case instrprof_error::hash_mismatch:
94 case instrprof_error::count_mismatch:
97 case instrprof_error::counter_overflow:
98 ++NumCounterOverflows;
100 case instrprof_error::value_site_count_mismatch:
101 ++NumValueSiteCountMismatches;
104 llvm_unreachable("Not a soft error");
108 std::string getPGOFuncName(StringRef RawFuncName,
109 GlobalValue::LinkageTypes Linkage,
111 uint64_t Version LLVM_ATTRIBUTE_UNUSED) {
112 return GlobalValue::getGlobalIdentifier(RawFuncName, Linkage, FileName);
115 // Return the PGOFuncName. This function has some special handling when called
116 // in LTO optimization. The following only applies when calling in LTO passes
117 // (when \c InLTO is true): LTO's internalization privatizes many global linkage
118 // symbols. This happens after value profile annotation, but those internal
119 // linkage functions should not have a source prefix.
120 // To differentiate compiler generated internal symbols from original ones,
121 // PGOFuncName meta data are created and attached to the original internal
122 // symbols in the value profile annotation step
123 // (PGOUseFunc::annotateIndirectCallSites). If a symbol does not have the meta
124 // data, its original linkage must be non-internal.
125 std::string getPGOFuncName(const Function &F, bool InLTO, uint64_t Version) {
127 return getPGOFuncName(F.getName(), F.getLinkage(), F.getParent()->getName(),
130 // In LTO mode (when InLTO is true), first check if there is a meta data.
131 if (MDNode *MD = getPGOFuncNameMetadata(F)) {
132 StringRef S = cast<MDString>(MD->getOperand(0))->getString();
136 // If there is no meta data, the function must be a global before the value
137 // profile annotation pass. Its current linkage may be internal if it is
138 // internalized in LTO mode.
139 return getPGOFuncName(F.getName(), GlobalValue::ExternalLinkage, "");
142 StringRef getFuncNameWithoutPrefix(StringRef PGOFuncName, StringRef FileName) {
143 if (FileName.empty())
145 // Drop the file name including ':'. See also getPGOFuncName.
146 if (PGOFuncName.startswith(FileName))
147 PGOFuncName = PGOFuncName.drop_front(FileName.size() + 1);
151 // \p FuncName is the string used as profile lookup key for the function. A
152 // symbol is created to hold the name. Return the legalized symbol name.
153 std::string getPGOFuncNameVarName(StringRef FuncName,
154 GlobalValue::LinkageTypes Linkage) {
155 std::string VarName = getInstrProfNameVarPrefix();
158 if (!GlobalValue::isLocalLinkage(Linkage))
161 // Now fix up illegal chars in local VarName that may upset the assembler.
162 const char *InvalidChars = "-:<>\"'";
163 size_t found = VarName.find_first_of(InvalidChars);
164 while (found != std::string::npos) {
165 VarName[found] = '_';
166 found = VarName.find_first_of(InvalidChars, found + 1);
171 GlobalVariable *createPGOFuncNameVar(Module &M,
172 GlobalValue::LinkageTypes Linkage,
173 StringRef PGOFuncName) {
175 // We generally want to match the function's linkage, but available_externally
176 // and extern_weak both have the wrong semantics, and anything that doesn't
177 // need to link across compilation units doesn't need to be visible at all.
178 if (Linkage == GlobalValue::ExternalWeakLinkage)
179 Linkage = GlobalValue::LinkOnceAnyLinkage;
180 else if (Linkage == GlobalValue::AvailableExternallyLinkage)
181 Linkage = GlobalValue::LinkOnceODRLinkage;
182 else if (Linkage == GlobalValue::InternalLinkage ||
183 Linkage == GlobalValue::ExternalLinkage)
184 Linkage = GlobalValue::PrivateLinkage;
187 ConstantDataArray::getString(M.getContext(), PGOFuncName, false);
189 new GlobalVariable(M, Value->getType(), true, Linkage, Value,
190 getPGOFuncNameVarName(PGOFuncName, Linkage));
192 // Hide the symbol so that we correctly get a copy for each executable.
193 if (!GlobalValue::isLocalLinkage(FuncNameVar->getLinkage()))
194 FuncNameVar->setVisibility(GlobalValue::HiddenVisibility);
199 GlobalVariable *createPGOFuncNameVar(Function &F, StringRef PGOFuncName) {
200 return createPGOFuncNameVar(*F.getParent(), F.getLinkage(), PGOFuncName);
203 void InstrProfSymtab::create(Module &M, bool InLTO) {
204 for (Function &F : M) {
205 // Function may not have a name: like using asm("") to overwrite the name.
206 // Ignore in this case.
209 const std::string &PGOFuncName = getPGOFuncName(F, InLTO);
210 addFuncName(PGOFuncName);
211 MD5FuncMap.emplace_back(Function::getGUID(PGOFuncName), &F);
218 collectPGOFuncNameStrings(const std::vector<std::string> &NameStrs,
219 bool doCompression, std::string &Result) {
220 assert(NameStrs.size() && "No name data to emit");
222 uint8_t Header[16], *P = Header;
223 std::string UncompressedNameStrings =
224 join(NameStrs.begin(), NameStrs.end(), getInstrProfNameSeparator());
226 assert(StringRef(UncompressedNameStrings)
227 .count(getInstrProfNameSeparator()) == (NameStrs.size() - 1) &&
228 "PGO name is invalid (contains separator token)");
230 unsigned EncLen = encodeULEB128(UncompressedNameStrings.length(), P);
233 auto WriteStringToResult = [&](size_t CompressedLen,
234 const std::string &InputStr) {
235 EncLen = encodeULEB128(CompressedLen, P);
237 char *HeaderStr = reinterpret_cast<char *>(&Header[0]);
238 unsigned HeaderLen = P - &Header[0];
239 Result.append(HeaderStr, HeaderLen);
241 return make_error_code(instrprof_error::success);
245 return WriteStringToResult(0, UncompressedNameStrings);
247 SmallVector<char, 128> CompressedNameStrings;
248 zlib::Status Success =
249 zlib::compress(StringRef(UncompressedNameStrings), CompressedNameStrings,
250 zlib::BestSizeCompression);
252 if (Success != zlib::StatusOK)
253 return make_error_code(instrprof_error::compress_failed);
255 return WriteStringToResult(
256 CompressedNameStrings.size(),
257 std::string(CompressedNameStrings.data(), CompressedNameStrings.size()));
260 StringRef getPGOFuncNameVarInitializer(GlobalVariable *NameVar) {
261 auto *Arr = cast<ConstantDataArray>(NameVar->getInitializer());
263 Arr->isCString() ? Arr->getAsCString() : Arr->getAsString();
268 collectPGOFuncNameStrings(const std::vector<GlobalVariable *> &NameVars,
269 std::string &Result, bool doCompression) {
270 std::vector<std::string> NameStrs;
271 for (auto *NameVar : NameVars) {
272 NameStrs.push_back(getPGOFuncNameVarInitializer(NameVar));
274 return collectPGOFuncNameStrings(
275 NameStrs, zlib::isAvailable() && doCompression, Result);
278 std::error_code readPGOFuncNameStrings(StringRef NameStrings,
279 InstrProfSymtab &Symtab) {
280 const uint8_t *P = reinterpret_cast<const uint8_t *>(NameStrings.data());
281 const uint8_t *EndP = reinterpret_cast<const uint8_t *>(NameStrings.data() +
285 uint64_t UncompressedSize = decodeULEB128(P, &N);
287 uint64_t CompressedSize = decodeULEB128(P, &N);
289 bool isCompressed = (CompressedSize != 0);
290 SmallString<128> UncompressedNameStrings;
291 StringRef NameStrings;
293 StringRef CompressedNameStrings(reinterpret_cast<const char *>(P),
295 if (zlib::uncompress(CompressedNameStrings, UncompressedNameStrings,
296 UncompressedSize) != zlib::StatusOK)
297 return make_error_code(instrprof_error::uncompress_failed);
299 NameStrings = StringRef(UncompressedNameStrings.data(),
300 UncompressedNameStrings.size());
303 StringRef(reinterpret_cast<const char *>(P), UncompressedSize);
304 P += UncompressedSize;
306 // Now parse the name strings.
307 SmallVector<StringRef, 0> Names;
308 NameStrings.split(Names, getInstrProfNameSeparator());
309 for (StringRef &Name : Names)
310 Symtab.addFuncName(Name);
312 while (P < EndP && *P == 0)
315 Symtab.finalizeSymtab();
316 return make_error_code(instrprof_error::success);
319 void InstrProfValueSiteRecord::merge(SoftInstrProfErrors &SIPE,
320 InstrProfValueSiteRecord &Input,
322 this->sortByTargetValues();
323 Input.sortByTargetValues();
324 auto I = ValueData.begin();
325 auto IE = ValueData.end();
326 for (auto J = Input.ValueData.begin(), JE = Input.ValueData.end(); J != JE;
328 while (I != IE && I->Value < J->Value)
330 if (I != IE && I->Value == J->Value) {
332 I->Count = SaturatingMultiplyAdd(J->Count, Weight, I->Count, &Overflowed);
334 SIPE.addError(instrprof_error::counter_overflow);
338 ValueData.insert(I, *J);
342 void InstrProfValueSiteRecord::scale(SoftInstrProfErrors &SIPE,
344 for (auto I = ValueData.begin(), IE = ValueData.end(); I != IE; ++I) {
346 I->Count = SaturatingMultiply(I->Count, Weight, &Overflowed);
348 SIPE.addError(instrprof_error::counter_overflow);
352 // Merge Value Profile data from Src record to this record for ValueKind.
353 // Scale merged value counts by \p Weight.
354 void InstrProfRecord::mergeValueProfData(uint32_t ValueKind,
355 InstrProfRecord &Src,
357 uint32_t ThisNumValueSites = getNumValueSites(ValueKind);
358 uint32_t OtherNumValueSites = Src.getNumValueSites(ValueKind);
359 if (ThisNumValueSites != OtherNumValueSites) {
360 SIPE.addError(instrprof_error::value_site_count_mismatch);
363 std::vector<InstrProfValueSiteRecord> &ThisSiteRecords =
364 getValueSitesForKind(ValueKind);
365 std::vector<InstrProfValueSiteRecord> &OtherSiteRecords =
366 Src.getValueSitesForKind(ValueKind);
367 for (uint32_t I = 0; I < ThisNumValueSites; I++)
368 ThisSiteRecords[I].merge(SIPE, OtherSiteRecords[I], Weight);
371 void InstrProfRecord::merge(InstrProfRecord &Other, uint64_t Weight) {
372 // If the number of counters doesn't match we either have bad data
373 // or a hash collision.
374 if (Counts.size() != Other.Counts.size()) {
375 SIPE.addError(instrprof_error::count_mismatch);
379 for (size_t I = 0, E = Other.Counts.size(); I < E; ++I) {
382 SaturatingMultiplyAdd(Other.Counts[I], Weight, Counts[I], &Overflowed);
384 SIPE.addError(instrprof_error::counter_overflow);
387 for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
388 mergeValueProfData(Kind, Other, Weight);
391 void InstrProfRecord::scaleValueProfData(uint32_t ValueKind, uint64_t Weight) {
392 uint32_t ThisNumValueSites = getNumValueSites(ValueKind);
393 std::vector<InstrProfValueSiteRecord> &ThisSiteRecords =
394 getValueSitesForKind(ValueKind);
395 for (uint32_t I = 0; I < ThisNumValueSites; I++)
396 ThisSiteRecords[I].scale(SIPE, Weight);
399 void InstrProfRecord::scale(uint64_t Weight) {
400 for (auto &Count : this->Counts) {
402 Count = SaturatingMultiply(Count, Weight, &Overflowed);
404 SIPE.addError(instrprof_error::counter_overflow);
406 for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
407 scaleValueProfData(Kind, Weight);
410 // Map indirect call target name hash to name string.
411 uint64_t InstrProfRecord::remapValue(uint64_t Value, uint32_t ValueKind,
412 ValueMapType *ValueMap) {
416 case IPVK_IndirectCallTarget: {
418 std::lower_bound(ValueMap->begin(), ValueMap->end(), Value,
419 [](const std::pair<uint64_t, uint64_t> &LHS,
420 uint64_t RHS) { return LHS.first < RHS; });
421 // Raw function pointer collected by value profiler may be from
422 // external functions that are not instrumented. They won't have
423 // mapping data to be used by the deserializer. Force the value to
424 // be 0 in this case.
425 if (Result != ValueMap->end() && Result->first == Value)
426 Value = (uint64_t)Result->second;
435 void InstrProfRecord::addValueData(uint32_t ValueKind, uint32_t Site,
436 InstrProfValueData *VData, uint32_t N,
437 ValueMapType *ValueMap) {
438 for (uint32_t I = 0; I < N; I++) {
439 VData[I].Value = remapValue(VData[I].Value, ValueKind, ValueMap);
441 std::vector<InstrProfValueSiteRecord> &ValueSites =
442 getValueSitesForKind(ValueKind);
444 ValueSites.emplace_back();
446 ValueSites.emplace_back(VData, VData + N);
449 #define INSTR_PROF_COMMON_API_IMPL
450 #include "llvm/ProfileData/InstrProfData.inc"
453 * \brief ValueProfRecordClosure Interface implementation for InstrProfRecord
454 * class. These C wrappers are used as adaptors so that C++ code can be
455 * invoked as callbacks.
457 uint32_t getNumValueKindsInstrProf(const void *Record) {
458 return reinterpret_cast<const InstrProfRecord *>(Record)->getNumValueKinds();
461 uint32_t getNumValueSitesInstrProf(const void *Record, uint32_t VKind) {
462 return reinterpret_cast<const InstrProfRecord *>(Record)
463 ->getNumValueSites(VKind);
466 uint32_t getNumValueDataInstrProf(const void *Record, uint32_t VKind) {
467 return reinterpret_cast<const InstrProfRecord *>(Record)
468 ->getNumValueData(VKind);
471 uint32_t getNumValueDataForSiteInstrProf(const void *R, uint32_t VK,
473 return reinterpret_cast<const InstrProfRecord *>(R)
474 ->getNumValueDataForSite(VK, S);
477 void getValueForSiteInstrProf(const void *R, InstrProfValueData *Dst,
478 uint32_t K, uint32_t S) {
479 reinterpret_cast<const InstrProfRecord *>(R)->getValueForSite(Dst, K, S);
482 ValueProfData *allocValueProfDataInstrProf(size_t TotalSizeInBytes) {
484 (ValueProfData *)(new (::operator new(TotalSizeInBytes)) ValueProfData());
485 memset(VD, 0, TotalSizeInBytes);
489 static ValueProfRecordClosure InstrProfRecordClosure = {
491 getNumValueKindsInstrProf,
492 getNumValueSitesInstrProf,
493 getNumValueDataInstrProf,
494 getNumValueDataForSiteInstrProf,
496 getValueForSiteInstrProf,
497 allocValueProfDataInstrProf};
499 // Wrapper implementation using the closure mechanism.
500 uint32_t ValueProfData::getSize(const InstrProfRecord &Record) {
501 InstrProfRecordClosure.Record = &Record;
502 return getValueProfDataSize(&InstrProfRecordClosure);
505 // Wrapper implementation using the closure mechanism.
506 std::unique_ptr<ValueProfData>
507 ValueProfData::serializeFrom(const InstrProfRecord &Record) {
508 InstrProfRecordClosure.Record = &Record;
510 std::unique_ptr<ValueProfData> VPD(
511 serializeValueProfDataFrom(&InstrProfRecordClosure, nullptr));
515 void ValueProfRecord::deserializeTo(InstrProfRecord &Record,
516 InstrProfRecord::ValueMapType *VMap) {
517 Record.reserveSites(Kind, NumValueSites);
519 InstrProfValueData *ValueData = getValueProfRecordValueData(this);
520 for (uint64_t VSite = 0; VSite < NumValueSites; ++VSite) {
521 uint8_t ValueDataCount = this->SiteCountArray[VSite];
522 Record.addValueData(Kind, VSite, ValueData, ValueDataCount, VMap);
523 ValueData += ValueDataCount;
527 // For writing/serializing, Old is the host endianness, and New is
528 // byte order intended on disk. For Reading/deserialization, Old
529 // is the on-disk source endianness, and New is the host endianness.
530 void ValueProfRecord::swapBytes(support::endianness Old,
531 support::endianness New) {
532 using namespace support;
536 if (getHostEndianness() != Old) {
537 sys::swapByteOrder<uint32_t>(NumValueSites);
538 sys::swapByteOrder<uint32_t>(Kind);
540 uint32_t ND = getValueProfRecordNumValueData(this);
541 InstrProfValueData *VD = getValueProfRecordValueData(this);
543 // No need to swap byte array: SiteCountArrray.
544 for (uint32_t I = 0; I < ND; I++) {
545 sys::swapByteOrder<uint64_t>(VD[I].Value);
546 sys::swapByteOrder<uint64_t>(VD[I].Count);
548 if (getHostEndianness() == Old) {
549 sys::swapByteOrder<uint32_t>(NumValueSites);
550 sys::swapByteOrder<uint32_t>(Kind);
554 void ValueProfData::deserializeTo(InstrProfRecord &Record,
555 InstrProfRecord::ValueMapType *VMap) {
556 if (NumValueKinds == 0)
559 ValueProfRecord *VR = getFirstValueProfRecord(this);
560 for (uint32_t K = 0; K < NumValueKinds; K++) {
561 VR->deserializeTo(Record, VMap);
562 VR = getValueProfRecordNext(VR);
567 static T swapToHostOrder(const unsigned char *&D, support::endianness Orig) {
568 using namespace support;
570 return endian::readNext<T, little, unaligned>(D);
572 return endian::readNext<T, big, unaligned>(D);
575 static std::unique_ptr<ValueProfData> allocValueProfData(uint32_t TotalSize) {
576 return std::unique_ptr<ValueProfData>(new (::operator new(TotalSize))
580 instrprof_error ValueProfData::checkIntegrity() {
581 if (NumValueKinds > IPVK_Last + 1)
582 return instrprof_error::malformed;
583 // Total size needs to be mulltiple of quadword size.
584 if (TotalSize % sizeof(uint64_t))
585 return instrprof_error::malformed;
587 ValueProfRecord *VR = getFirstValueProfRecord(this);
588 for (uint32_t K = 0; K < this->NumValueKinds; K++) {
589 if (VR->Kind > IPVK_Last)
590 return instrprof_error::malformed;
591 VR = getValueProfRecordNext(VR);
592 if ((char *)VR - (char *)this > (ptrdiff_t)TotalSize)
593 return instrprof_error::malformed;
595 return instrprof_error::success;
598 ErrorOr<std::unique_ptr<ValueProfData>>
599 ValueProfData::getValueProfData(const unsigned char *D,
600 const unsigned char *const BufferEnd,
601 support::endianness Endianness) {
602 using namespace support;
603 if (D + sizeof(ValueProfData) > BufferEnd)
604 return instrprof_error::truncated;
606 const unsigned char *Header = D;
607 uint32_t TotalSize = swapToHostOrder<uint32_t>(Header, Endianness);
608 if (D + TotalSize > BufferEnd)
609 return instrprof_error::too_large;
611 std::unique_ptr<ValueProfData> VPD = allocValueProfData(TotalSize);
612 memcpy(VPD.get(), D, TotalSize);
614 VPD->swapBytesToHost(Endianness);
616 instrprof_error EC = VPD->checkIntegrity();
617 if (EC != instrprof_error::success)
620 return std::move(VPD);
623 void ValueProfData::swapBytesToHost(support::endianness Endianness) {
624 using namespace support;
625 if (Endianness == getHostEndianness())
628 sys::swapByteOrder<uint32_t>(TotalSize);
629 sys::swapByteOrder<uint32_t>(NumValueKinds);
631 ValueProfRecord *VR = getFirstValueProfRecord(this);
632 for (uint32_t K = 0; K < NumValueKinds; K++) {
633 VR->swapBytes(Endianness, getHostEndianness());
634 VR = getValueProfRecordNext(VR);
638 void ValueProfData::swapBytesFromHost(support::endianness Endianness) {
639 using namespace support;
640 if (Endianness == getHostEndianness())
643 ValueProfRecord *VR = getFirstValueProfRecord(this);
644 for (uint32_t K = 0; K < NumValueKinds; K++) {
645 ValueProfRecord *NVR = getValueProfRecordNext(VR);
646 VR->swapBytes(getHostEndianness(), Endianness);
649 sys::swapByteOrder<uint32_t>(TotalSize);
650 sys::swapByteOrder<uint32_t>(NumValueKinds);
653 void annotateValueSite(Module &M, Instruction &Inst,
654 const InstrProfRecord &InstrProfR,
655 InstrProfValueKind ValueKind, uint32_t SiteIdx,
656 uint32_t MaxMDCount) {
657 uint32_t NV = InstrProfR.getNumValueDataForSite(ValueKind, SiteIdx);
662 std::unique_ptr<InstrProfValueData[]> VD =
663 InstrProfR.getValueForSite(ValueKind, SiteIdx, &Sum);
665 ArrayRef<InstrProfValueData> VDs(VD.get(), NV);
666 annotateValueSite(M, Inst, VDs, Sum, ValueKind, MaxMDCount);
669 void annotateValueSite(Module &M, Instruction &Inst,
670 ArrayRef<InstrProfValueData> VDs,
671 uint64_t Sum, InstrProfValueKind ValueKind,
672 uint32_t MaxMDCount) {
673 LLVMContext &Ctx = M.getContext();
674 MDBuilder MDHelper(Ctx);
675 SmallVector<Metadata *, 3> Vals;
677 Vals.push_back(MDHelper.createString("VP"));
679 Vals.push_back(MDHelper.createConstant(
680 ConstantInt::get(Type::getInt32Ty(Ctx), ValueKind)));
683 MDHelper.createConstant(ConstantInt::get(Type::getInt64Ty(Ctx), Sum)));
685 // Value Profile Data
686 uint32_t MDCount = MaxMDCount;
687 for (auto &VD : VDs) {
688 Vals.push_back(MDHelper.createConstant(
689 ConstantInt::get(Type::getInt64Ty(Ctx), VD.Value)));
690 Vals.push_back(MDHelper.createConstant(
691 ConstantInt::get(Type::getInt64Ty(Ctx), VD.Count)));
695 Inst.setMetadata(LLVMContext::MD_prof, MDNode::get(Ctx, Vals));
698 bool getValueProfDataFromInst(const Instruction &Inst,
699 InstrProfValueKind ValueKind,
700 uint32_t MaxNumValueData,
701 InstrProfValueData ValueData[],
702 uint32_t &ActualNumValueData, uint64_t &TotalC) {
703 MDNode *MD = Inst.getMetadata(LLVMContext::MD_prof);
707 unsigned NOps = MD->getNumOperands();
712 // Operand 0 is a string tag "VP":
713 MDString *Tag = cast<MDString>(MD->getOperand(0));
717 if (!Tag->getString().equals("VP"))
721 ConstantInt *KindInt = mdconst::dyn_extract<ConstantInt>(MD->getOperand(1));
724 if (KindInt->getZExtValue() != ValueKind)
728 ConstantInt *TotalCInt = mdconst::dyn_extract<ConstantInt>(MD->getOperand(2));
731 TotalC = TotalCInt->getZExtValue();
733 ActualNumValueData = 0;
735 for (unsigned I = 3; I < NOps; I += 2) {
736 if (ActualNumValueData >= MaxNumValueData)
738 ConstantInt *Value = mdconst::dyn_extract<ConstantInt>(MD->getOperand(I));
740 mdconst::dyn_extract<ConstantInt>(MD->getOperand(I + 1));
741 if (!Value || !Count)
743 ValueData[ActualNumValueData].Value = Value->getZExtValue();
744 ValueData[ActualNumValueData].Count = Count->getZExtValue();
745 ActualNumValueData++;
750 MDNode *getPGOFuncNameMetadata(const Function &F) {
751 return F.getMetadata(getPGOFuncNameMetadataName());
754 void createPGOFuncNameMetadata(Function &F, const std::string &PGOFuncName) {
755 // Only for internal linkage functions.
756 if (PGOFuncName == F.getName())
758 // Don't create duplicated meta-data.
759 if (getPGOFuncNameMetadata(F))
761 LLVMContext &C = F.getContext();
762 MDNode *N = MDNode::get(C, MDString::get(C, PGOFuncName.c_str()));
763 F.setMetadata(getPGOFuncNameMetadataName(), N);
766 } // end namespace llvm