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 std::string getInstrProfErrString(instrprof_error Err) {
32 case instrprof_error::success:
34 case instrprof_error::eof:
36 case instrprof_error::unrecognized_format:
37 return "Unrecognized instrumentation profile encoding format";
38 case instrprof_error::bad_magic:
39 return "Invalid instrumentation profile data (bad magic)";
40 case instrprof_error::bad_header:
41 return "Invalid instrumentation profile data (file header is corrupt)";
42 case instrprof_error::unsupported_version:
43 return "Unsupported instrumentation profile format version";
44 case instrprof_error::unsupported_hash_type:
45 return "Unsupported instrumentation profile hash type";
46 case instrprof_error::too_large:
47 return "Too much profile data";
48 case instrprof_error::truncated:
49 return "Truncated profile data";
50 case instrprof_error::malformed:
51 return "Malformed instrumentation profile data";
52 case instrprof_error::unknown_function:
53 return "No profile data available for function";
54 case instrprof_error::hash_mismatch:
55 return "Function control flow change detected (hash mismatch)";
56 case instrprof_error::count_mismatch:
57 return "Function basic block count change detected (counter mismatch)";
58 case instrprof_error::counter_overflow:
59 return "Counter overflow";
60 case instrprof_error::value_site_count_mismatch:
61 return "Function value site count change detected (counter mismatch)";
62 case instrprof_error::compress_failed:
63 return "Failed to compress data (zlib)";
64 case instrprof_error::uncompress_failed:
65 return "Failed to uncompress data (zlib)";
67 llvm_unreachable("A value of instrprof_error has no message.");
70 // FIXME: This class is only here to support the transition to llvm::Error. It
71 // will be removed once this transition is complete. Clients should prefer to
72 // deal with the Error value directly, rather than converting to error_code.
73 class InstrProfErrorCategoryType : public std::error_category {
74 const char *name() const LLVM_NOEXCEPT override { return "llvm.instrprof"; }
75 std::string message(int IE) const override {
76 return getInstrProfErrString(static_cast<instrprof_error>(IE));
79 } // end anonymous namespace
81 static ManagedStatic<InstrProfErrorCategoryType> ErrorCategory;
83 const std::error_category &llvm::instrprof_category() {
84 return *ErrorCategory;
89 void SoftInstrProfErrors::addError(instrprof_error IE) {
90 if (IE == instrprof_error::success)
93 if (FirstError == instrprof_error::success)
97 case instrprof_error::hash_mismatch:
100 case instrprof_error::count_mismatch:
101 ++NumCountMismatches;
103 case instrprof_error::counter_overflow:
104 ++NumCounterOverflows;
106 case instrprof_error::value_site_count_mismatch:
107 ++NumValueSiteCountMismatches;
110 llvm_unreachable("Not a soft error");
114 std::string InstrProfError::message() const {
115 return getInstrProfErrString(Err);
118 char InstrProfError::ID = 0;
120 std::string getPGOFuncName(StringRef RawFuncName,
121 GlobalValue::LinkageTypes Linkage,
123 uint64_t Version LLVM_ATTRIBUTE_UNUSED) {
124 return GlobalValue::getGlobalIdentifier(RawFuncName, Linkage, FileName);
127 // Return the PGOFuncName. This function has some special handling when called
128 // in LTO optimization. The following only applies when calling in LTO passes
129 // (when \c InLTO is true): LTO's internalization privatizes many global linkage
130 // symbols. This happens after value profile annotation, but those internal
131 // linkage functions should not have a source prefix.
132 // To differentiate compiler generated internal symbols from original ones,
133 // PGOFuncName meta data are created and attached to the original internal
134 // symbols in the value profile annotation step
135 // (PGOUseFunc::annotateIndirectCallSites). If a symbol does not have the meta
136 // data, its original linkage must be non-internal.
137 std::string getPGOFuncName(const Function &F, bool InLTO, uint64_t Version) {
139 return getPGOFuncName(F.getName(), F.getLinkage(), F.getParent()->getName(),
142 // In LTO mode (when InLTO is true), first check if there is a meta data.
143 if (MDNode *MD = getPGOFuncNameMetadata(F)) {
144 StringRef S = cast<MDString>(MD->getOperand(0))->getString();
148 // If there is no meta data, the function must be a global before the value
149 // profile annotation pass. Its current linkage may be internal if it is
150 // internalized in LTO mode.
151 return getPGOFuncName(F.getName(), GlobalValue::ExternalLinkage, "");
154 StringRef getFuncNameWithoutPrefix(StringRef PGOFuncName, StringRef FileName) {
155 if (FileName.empty())
157 // Drop the file name including ':'. See also getPGOFuncName.
158 if (PGOFuncName.startswith(FileName))
159 PGOFuncName = PGOFuncName.drop_front(FileName.size() + 1);
163 // \p FuncName is the string used as profile lookup key for the function. A
164 // symbol is created to hold the name. Return the legalized symbol name.
165 std::string getPGOFuncNameVarName(StringRef FuncName,
166 GlobalValue::LinkageTypes Linkage) {
167 std::string VarName = getInstrProfNameVarPrefix();
170 if (!GlobalValue::isLocalLinkage(Linkage))
173 // Now fix up illegal chars in local VarName that may upset the assembler.
174 const char *InvalidChars = "-:<>\"'";
175 size_t found = VarName.find_first_of(InvalidChars);
176 while (found != std::string::npos) {
177 VarName[found] = '_';
178 found = VarName.find_first_of(InvalidChars, found + 1);
183 GlobalVariable *createPGOFuncNameVar(Module &M,
184 GlobalValue::LinkageTypes Linkage,
185 StringRef PGOFuncName) {
187 // We generally want to match the function's linkage, but available_externally
188 // and extern_weak both have the wrong semantics, and anything that doesn't
189 // need to link across compilation units doesn't need to be visible at all.
190 if (Linkage == GlobalValue::ExternalWeakLinkage)
191 Linkage = GlobalValue::LinkOnceAnyLinkage;
192 else if (Linkage == GlobalValue::AvailableExternallyLinkage)
193 Linkage = GlobalValue::LinkOnceODRLinkage;
194 else if (Linkage == GlobalValue::InternalLinkage ||
195 Linkage == GlobalValue::ExternalLinkage)
196 Linkage = GlobalValue::PrivateLinkage;
199 ConstantDataArray::getString(M.getContext(), PGOFuncName, false);
201 new GlobalVariable(M, Value->getType(), true, Linkage, Value,
202 getPGOFuncNameVarName(PGOFuncName, Linkage));
204 // Hide the symbol so that we correctly get a copy for each executable.
205 if (!GlobalValue::isLocalLinkage(FuncNameVar->getLinkage()))
206 FuncNameVar->setVisibility(GlobalValue::HiddenVisibility);
211 GlobalVariable *createPGOFuncNameVar(Function &F, StringRef PGOFuncName) {
212 return createPGOFuncNameVar(*F.getParent(), F.getLinkage(), PGOFuncName);
215 void InstrProfSymtab::create(Module &M, bool InLTO) {
216 for (Function &F : M) {
217 // Function may not have a name: like using asm("") to overwrite the name.
218 // Ignore in this case.
221 const std::string &PGOFuncName = getPGOFuncName(F, InLTO);
222 addFuncName(PGOFuncName);
223 MD5FuncMap.emplace_back(Function::getGUID(PGOFuncName), &F);
229 Error collectPGOFuncNameStrings(const std::vector<std::string> &NameStrs,
230 bool doCompression, std::string &Result) {
231 assert(NameStrs.size() && "No name data to emit");
233 uint8_t Header[16], *P = Header;
234 std::string UncompressedNameStrings =
235 join(NameStrs.begin(), NameStrs.end(), getInstrProfNameSeparator());
237 assert(StringRef(UncompressedNameStrings)
238 .count(getInstrProfNameSeparator()) == (NameStrs.size() - 1) &&
239 "PGO name is invalid (contains separator token)");
241 unsigned EncLen = encodeULEB128(UncompressedNameStrings.length(), P);
244 auto WriteStringToResult = [&](size_t CompressedLen,
245 const std::string &InputStr) {
246 EncLen = encodeULEB128(CompressedLen, P);
248 char *HeaderStr = reinterpret_cast<char *>(&Header[0]);
249 unsigned HeaderLen = P - &Header[0];
250 Result.append(HeaderStr, HeaderLen);
252 return Error::success();
255 if (!doCompression) {
256 return WriteStringToResult(0, UncompressedNameStrings);
259 SmallVector<char, 128> CompressedNameStrings;
260 zlib::Status Success =
261 zlib::compress(StringRef(UncompressedNameStrings), CompressedNameStrings,
262 zlib::BestSizeCompression);
264 if (Success != zlib::StatusOK)
265 return make_error<InstrProfError>(instrprof_error::compress_failed);
267 return WriteStringToResult(
268 CompressedNameStrings.size(),
269 std::string(CompressedNameStrings.data(), CompressedNameStrings.size()));
272 StringRef getPGOFuncNameVarInitializer(GlobalVariable *NameVar) {
273 auto *Arr = cast<ConstantDataArray>(NameVar->getInitializer());
275 Arr->isCString() ? Arr->getAsCString() : Arr->getAsString();
279 Error collectPGOFuncNameStrings(const std::vector<GlobalVariable *> &NameVars,
280 std::string &Result, bool doCompression) {
281 std::vector<std::string> NameStrs;
282 for (auto *NameVar : NameVars) {
283 NameStrs.push_back(getPGOFuncNameVarInitializer(NameVar));
285 return collectPGOFuncNameStrings(
286 NameStrs, zlib::isAvailable() && doCompression, Result);
289 Error readPGOFuncNameStrings(StringRef NameStrings, InstrProfSymtab &Symtab) {
290 const uint8_t *P = reinterpret_cast<const uint8_t *>(NameStrings.data());
291 const uint8_t *EndP = reinterpret_cast<const uint8_t *>(NameStrings.data() +
295 uint64_t UncompressedSize = decodeULEB128(P, &N);
297 uint64_t CompressedSize = decodeULEB128(P, &N);
299 bool isCompressed = (CompressedSize != 0);
300 SmallString<128> UncompressedNameStrings;
301 StringRef NameStrings;
303 StringRef CompressedNameStrings(reinterpret_cast<const char *>(P),
305 if (zlib::uncompress(CompressedNameStrings, UncompressedNameStrings,
306 UncompressedSize) != zlib::StatusOK)
307 return make_error<InstrProfError>(instrprof_error::uncompress_failed);
309 NameStrings = StringRef(UncompressedNameStrings.data(),
310 UncompressedNameStrings.size());
313 StringRef(reinterpret_cast<const char *>(P), UncompressedSize);
314 P += UncompressedSize;
316 // Now parse the name strings.
317 SmallVector<StringRef, 0> Names;
318 NameStrings.split(Names, getInstrProfNameSeparator());
319 for (StringRef &Name : Names)
320 Symtab.addFuncName(Name);
322 while (P < EndP && *P == 0)
325 Symtab.finalizeSymtab();
326 return Error::success();
329 void InstrProfValueSiteRecord::merge(SoftInstrProfErrors &SIPE,
330 InstrProfValueSiteRecord &Input,
332 this->sortByTargetValues();
333 Input.sortByTargetValues();
334 auto I = ValueData.begin();
335 auto IE = ValueData.end();
336 for (auto J = Input.ValueData.begin(), JE = Input.ValueData.end(); J != JE;
338 while (I != IE && I->Value < J->Value)
340 if (I != IE && I->Value == J->Value) {
342 I->Count = SaturatingMultiplyAdd(J->Count, Weight, I->Count, &Overflowed);
344 SIPE.addError(instrprof_error::counter_overflow);
348 ValueData.insert(I, *J);
352 void InstrProfValueSiteRecord::scale(SoftInstrProfErrors &SIPE,
354 for (auto I = ValueData.begin(), IE = ValueData.end(); I != IE; ++I) {
356 I->Count = SaturatingMultiply(I->Count, Weight, &Overflowed);
358 SIPE.addError(instrprof_error::counter_overflow);
362 // Merge Value Profile data from Src record to this record for ValueKind.
363 // Scale merged value counts by \p Weight.
364 void InstrProfRecord::mergeValueProfData(uint32_t ValueKind,
365 InstrProfRecord &Src,
367 uint32_t ThisNumValueSites = getNumValueSites(ValueKind);
368 uint32_t OtherNumValueSites = Src.getNumValueSites(ValueKind);
369 if (ThisNumValueSites != OtherNumValueSites) {
370 SIPE.addError(instrprof_error::value_site_count_mismatch);
373 std::vector<InstrProfValueSiteRecord> &ThisSiteRecords =
374 getValueSitesForKind(ValueKind);
375 std::vector<InstrProfValueSiteRecord> &OtherSiteRecords =
376 Src.getValueSitesForKind(ValueKind);
377 for (uint32_t I = 0; I < ThisNumValueSites; I++)
378 ThisSiteRecords[I].merge(SIPE, OtherSiteRecords[I], Weight);
381 void InstrProfRecord::merge(InstrProfRecord &Other, uint64_t Weight) {
382 // If the number of counters doesn't match we either have bad data
383 // or a hash collision.
384 if (Counts.size() != Other.Counts.size()) {
385 SIPE.addError(instrprof_error::count_mismatch);
389 for (size_t I = 0, E = Other.Counts.size(); I < E; ++I) {
392 SaturatingMultiplyAdd(Other.Counts[I], Weight, Counts[I], &Overflowed);
394 SIPE.addError(instrprof_error::counter_overflow);
397 for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
398 mergeValueProfData(Kind, Other, Weight);
401 void InstrProfRecord::scaleValueProfData(uint32_t ValueKind, uint64_t Weight) {
402 uint32_t ThisNumValueSites = getNumValueSites(ValueKind);
403 std::vector<InstrProfValueSiteRecord> &ThisSiteRecords =
404 getValueSitesForKind(ValueKind);
405 for (uint32_t I = 0; I < ThisNumValueSites; I++)
406 ThisSiteRecords[I].scale(SIPE, Weight);
409 void InstrProfRecord::scale(uint64_t Weight) {
410 for (auto &Count : this->Counts) {
412 Count = SaturatingMultiply(Count, Weight, &Overflowed);
414 SIPE.addError(instrprof_error::counter_overflow);
416 for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
417 scaleValueProfData(Kind, Weight);
420 // Map indirect call target name hash to name string.
421 uint64_t InstrProfRecord::remapValue(uint64_t Value, uint32_t ValueKind,
422 ValueMapType *ValueMap) {
426 case IPVK_IndirectCallTarget: {
428 std::lower_bound(ValueMap->begin(), ValueMap->end(), Value,
429 [](const std::pair<uint64_t, uint64_t> &LHS,
430 uint64_t RHS) { return LHS.first < RHS; });
431 // Raw function pointer collected by value profiler may be from
432 // external functions that are not instrumented. They won't have
433 // mapping data to be used by the deserializer. Force the value to
434 // be 0 in this case.
435 if (Result != ValueMap->end() && Result->first == Value)
436 Value = (uint64_t)Result->second;
445 void InstrProfRecord::addValueData(uint32_t ValueKind, uint32_t Site,
446 InstrProfValueData *VData, uint32_t N,
447 ValueMapType *ValueMap) {
448 for (uint32_t I = 0; I < N; I++) {
449 VData[I].Value = remapValue(VData[I].Value, ValueKind, ValueMap);
451 std::vector<InstrProfValueSiteRecord> &ValueSites =
452 getValueSitesForKind(ValueKind);
454 ValueSites.emplace_back();
456 ValueSites.emplace_back(VData, VData + N);
459 #define INSTR_PROF_COMMON_API_IMPL
460 #include "llvm/ProfileData/InstrProfData.inc"
463 * \brief ValueProfRecordClosure Interface implementation for InstrProfRecord
464 * class. These C wrappers are used as adaptors so that C++ code can be
465 * invoked as callbacks.
467 uint32_t getNumValueKindsInstrProf(const void *Record) {
468 return reinterpret_cast<const InstrProfRecord *>(Record)->getNumValueKinds();
471 uint32_t getNumValueSitesInstrProf(const void *Record, uint32_t VKind) {
472 return reinterpret_cast<const InstrProfRecord *>(Record)
473 ->getNumValueSites(VKind);
476 uint32_t getNumValueDataInstrProf(const void *Record, uint32_t VKind) {
477 return reinterpret_cast<const InstrProfRecord *>(Record)
478 ->getNumValueData(VKind);
481 uint32_t getNumValueDataForSiteInstrProf(const void *R, uint32_t VK,
483 return reinterpret_cast<const InstrProfRecord *>(R)
484 ->getNumValueDataForSite(VK, S);
487 void getValueForSiteInstrProf(const void *R, InstrProfValueData *Dst,
488 uint32_t K, uint32_t S) {
489 reinterpret_cast<const InstrProfRecord *>(R)->getValueForSite(Dst, K, S);
492 ValueProfData *allocValueProfDataInstrProf(size_t TotalSizeInBytes) {
494 (ValueProfData *)(new (::operator new(TotalSizeInBytes)) ValueProfData());
495 memset(VD, 0, TotalSizeInBytes);
499 static ValueProfRecordClosure InstrProfRecordClosure = {
501 getNumValueKindsInstrProf,
502 getNumValueSitesInstrProf,
503 getNumValueDataInstrProf,
504 getNumValueDataForSiteInstrProf,
506 getValueForSiteInstrProf,
507 allocValueProfDataInstrProf};
509 // Wrapper implementation using the closure mechanism.
510 uint32_t ValueProfData::getSize(const InstrProfRecord &Record) {
511 InstrProfRecordClosure.Record = &Record;
512 return getValueProfDataSize(&InstrProfRecordClosure);
515 // Wrapper implementation using the closure mechanism.
516 std::unique_ptr<ValueProfData>
517 ValueProfData::serializeFrom(const InstrProfRecord &Record) {
518 InstrProfRecordClosure.Record = &Record;
520 std::unique_ptr<ValueProfData> VPD(
521 serializeValueProfDataFrom(&InstrProfRecordClosure, nullptr));
525 void ValueProfRecord::deserializeTo(InstrProfRecord &Record,
526 InstrProfRecord::ValueMapType *VMap) {
527 Record.reserveSites(Kind, NumValueSites);
529 InstrProfValueData *ValueData = getValueProfRecordValueData(this);
530 for (uint64_t VSite = 0; VSite < NumValueSites; ++VSite) {
531 uint8_t ValueDataCount = this->SiteCountArray[VSite];
532 Record.addValueData(Kind, VSite, ValueData, ValueDataCount, VMap);
533 ValueData += ValueDataCount;
537 // For writing/serializing, Old is the host endianness, and New is
538 // byte order intended on disk. For Reading/deserialization, Old
539 // is the on-disk source endianness, and New is the host endianness.
540 void ValueProfRecord::swapBytes(support::endianness Old,
541 support::endianness New) {
542 using namespace support;
546 if (getHostEndianness() != Old) {
547 sys::swapByteOrder<uint32_t>(NumValueSites);
548 sys::swapByteOrder<uint32_t>(Kind);
550 uint32_t ND = getValueProfRecordNumValueData(this);
551 InstrProfValueData *VD = getValueProfRecordValueData(this);
553 // No need to swap byte array: SiteCountArrray.
554 for (uint32_t I = 0; I < ND; I++) {
555 sys::swapByteOrder<uint64_t>(VD[I].Value);
556 sys::swapByteOrder<uint64_t>(VD[I].Count);
558 if (getHostEndianness() == Old) {
559 sys::swapByteOrder<uint32_t>(NumValueSites);
560 sys::swapByteOrder<uint32_t>(Kind);
564 void ValueProfData::deserializeTo(InstrProfRecord &Record,
565 InstrProfRecord::ValueMapType *VMap) {
566 if (NumValueKinds == 0)
569 ValueProfRecord *VR = getFirstValueProfRecord(this);
570 for (uint32_t K = 0; K < NumValueKinds; K++) {
571 VR->deserializeTo(Record, VMap);
572 VR = getValueProfRecordNext(VR);
577 static T swapToHostOrder(const unsigned char *&D, support::endianness Orig) {
578 using namespace support;
580 return endian::readNext<T, little, unaligned>(D);
582 return endian::readNext<T, big, unaligned>(D);
585 static std::unique_ptr<ValueProfData> allocValueProfData(uint32_t TotalSize) {
586 return std::unique_ptr<ValueProfData>(new (::operator new(TotalSize))
590 Error ValueProfData::checkIntegrity() {
591 if (NumValueKinds > IPVK_Last + 1)
592 return make_error<InstrProfError>(instrprof_error::malformed);
593 // Total size needs to be mulltiple of quadword size.
594 if (TotalSize % sizeof(uint64_t))
595 return make_error<InstrProfError>(instrprof_error::malformed);
597 ValueProfRecord *VR = getFirstValueProfRecord(this);
598 for (uint32_t K = 0; K < this->NumValueKinds; K++) {
599 if (VR->Kind > IPVK_Last)
600 return make_error<InstrProfError>(instrprof_error::malformed);
601 VR = getValueProfRecordNext(VR);
602 if ((char *)VR - (char *)this > (ptrdiff_t)TotalSize)
603 return make_error<InstrProfError>(instrprof_error::malformed);
605 return Error::success();
608 Expected<std::unique_ptr<ValueProfData>>
609 ValueProfData::getValueProfData(const unsigned char *D,
610 const unsigned char *const BufferEnd,
611 support::endianness Endianness) {
612 using namespace support;
613 if (D + sizeof(ValueProfData) > BufferEnd)
614 return make_error<InstrProfError>(instrprof_error::truncated);
616 const unsigned char *Header = D;
617 uint32_t TotalSize = swapToHostOrder<uint32_t>(Header, Endianness);
618 if (D + TotalSize > BufferEnd)
619 return make_error<InstrProfError>(instrprof_error::too_large);
621 std::unique_ptr<ValueProfData> VPD = allocValueProfData(TotalSize);
622 memcpy(VPD.get(), D, TotalSize);
624 VPD->swapBytesToHost(Endianness);
626 Error E = VPD->checkIntegrity();
630 return std::move(VPD);
633 void ValueProfData::swapBytesToHost(support::endianness Endianness) {
634 using namespace support;
635 if (Endianness == getHostEndianness())
638 sys::swapByteOrder<uint32_t>(TotalSize);
639 sys::swapByteOrder<uint32_t>(NumValueKinds);
641 ValueProfRecord *VR = getFirstValueProfRecord(this);
642 for (uint32_t K = 0; K < NumValueKinds; K++) {
643 VR->swapBytes(Endianness, getHostEndianness());
644 VR = getValueProfRecordNext(VR);
648 void ValueProfData::swapBytesFromHost(support::endianness Endianness) {
649 using namespace support;
650 if (Endianness == getHostEndianness())
653 ValueProfRecord *VR = getFirstValueProfRecord(this);
654 for (uint32_t K = 0; K < NumValueKinds; K++) {
655 ValueProfRecord *NVR = getValueProfRecordNext(VR);
656 VR->swapBytes(getHostEndianness(), Endianness);
659 sys::swapByteOrder<uint32_t>(TotalSize);
660 sys::swapByteOrder<uint32_t>(NumValueKinds);
663 void annotateValueSite(Module &M, Instruction &Inst,
664 const InstrProfRecord &InstrProfR,
665 InstrProfValueKind ValueKind, uint32_t SiteIdx,
666 uint32_t MaxMDCount) {
667 uint32_t NV = InstrProfR.getNumValueDataForSite(ValueKind, SiteIdx);
672 std::unique_ptr<InstrProfValueData[]> VD =
673 InstrProfR.getValueForSite(ValueKind, SiteIdx, &Sum);
675 ArrayRef<InstrProfValueData> VDs(VD.get(), NV);
676 annotateValueSite(M, Inst, VDs, Sum, ValueKind, MaxMDCount);
679 void annotateValueSite(Module &M, Instruction &Inst,
680 ArrayRef<InstrProfValueData> VDs,
681 uint64_t Sum, InstrProfValueKind ValueKind,
682 uint32_t MaxMDCount) {
683 LLVMContext &Ctx = M.getContext();
684 MDBuilder MDHelper(Ctx);
685 SmallVector<Metadata *, 3> Vals;
687 Vals.push_back(MDHelper.createString("VP"));
689 Vals.push_back(MDHelper.createConstant(
690 ConstantInt::get(Type::getInt32Ty(Ctx), ValueKind)));
693 MDHelper.createConstant(ConstantInt::get(Type::getInt64Ty(Ctx), Sum)));
695 // Value Profile Data
696 uint32_t MDCount = MaxMDCount;
697 for (auto &VD : VDs) {
698 Vals.push_back(MDHelper.createConstant(
699 ConstantInt::get(Type::getInt64Ty(Ctx), VD.Value)));
700 Vals.push_back(MDHelper.createConstant(
701 ConstantInt::get(Type::getInt64Ty(Ctx), VD.Count)));
705 Inst.setMetadata(LLVMContext::MD_prof, MDNode::get(Ctx, Vals));
708 bool getValueProfDataFromInst(const Instruction &Inst,
709 InstrProfValueKind ValueKind,
710 uint32_t MaxNumValueData,
711 InstrProfValueData ValueData[],
712 uint32_t &ActualNumValueData, uint64_t &TotalC) {
713 MDNode *MD = Inst.getMetadata(LLVMContext::MD_prof);
717 unsigned NOps = MD->getNumOperands();
722 // Operand 0 is a string tag "VP":
723 MDString *Tag = cast<MDString>(MD->getOperand(0));
727 if (!Tag->getString().equals("VP"))
731 ConstantInt *KindInt = mdconst::dyn_extract<ConstantInt>(MD->getOperand(1));
734 if (KindInt->getZExtValue() != ValueKind)
738 ConstantInt *TotalCInt = mdconst::dyn_extract<ConstantInt>(MD->getOperand(2));
741 TotalC = TotalCInt->getZExtValue();
743 ActualNumValueData = 0;
745 for (unsigned I = 3; I < NOps; I += 2) {
746 if (ActualNumValueData >= MaxNumValueData)
748 ConstantInt *Value = mdconst::dyn_extract<ConstantInt>(MD->getOperand(I));
750 mdconst::dyn_extract<ConstantInt>(MD->getOperand(I + 1));
751 if (!Value || !Count)
753 ValueData[ActualNumValueData].Value = Value->getZExtValue();
754 ValueData[ActualNumValueData].Count = Count->getZExtValue();
755 ActualNumValueData++;
760 MDNode *getPGOFuncNameMetadata(const Function &F) {
761 return F.getMetadata(getPGOFuncNameMetadataName());
764 void createPGOFuncNameMetadata(Function &F, const std::string &PGOFuncName) {
765 // Only for internal linkage functions.
766 if (PGOFuncName == F.getName())
768 // Don't create duplicated meta-data.
769 if (getPGOFuncNameMetadata(F))
771 LLVMContext &C = F.getContext();
772 MDNode *N = MDNode::get(C, MDString::get(C, PGOFuncName.c_str()));
773 F.setMetadata(getPGOFuncNameMetadataName(), N);
776 } // end namespace llvm