1 //=-- InstrProf.cpp - Instrumented profiling format support -----------------=//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains support for clang's instrumentation based PGO and
11 // coverage.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/ProfileData/InstrProf.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/ADT/Triple.h"
18 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/GlobalVariable.h"
21 #include "llvm/IR/MDBuilder.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/Support/Compression.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/LEB128.h"
26 #include "llvm/Support/ManagedStatic.h"
27 #include "llvm/Support/Path.h"
28 
29 using namespace llvm;
30 
31 static cl::opt<bool> StaticFuncFullModulePrefix(
32     "static-func-full-module-prefix", cl::init(false),
33     cl::desc("Use full module build paths in the profile counter names for "
34              "static functions."));
35 
36 namespace {
37 std::string getInstrProfErrString(instrprof_error Err) {
38   switch (Err) {
39   case instrprof_error::success:
40     return "Success";
41   case instrprof_error::eof:
42     return "End of File";
43   case instrprof_error::unrecognized_format:
44     return "Unrecognized instrumentation profile encoding format";
45   case instrprof_error::bad_magic:
46     return "Invalid instrumentation profile data (bad magic)";
47   case instrprof_error::bad_header:
48     return "Invalid instrumentation profile data (file header is corrupt)";
49   case instrprof_error::unsupported_version:
50     return "Unsupported instrumentation profile format version";
51   case instrprof_error::unsupported_hash_type:
52     return "Unsupported instrumentation profile hash type";
53   case instrprof_error::too_large:
54     return "Too much profile data";
55   case instrprof_error::truncated:
56     return "Truncated profile data";
57   case instrprof_error::malformed:
58     return "Malformed instrumentation profile data";
59   case instrprof_error::unknown_function:
60     return "No profile data available for function";
61   case instrprof_error::hash_mismatch:
62     return "Function control flow change detected (hash mismatch)";
63   case instrprof_error::count_mismatch:
64     return "Function basic block count change detected (counter mismatch)";
65   case instrprof_error::counter_overflow:
66     return "Counter overflow";
67   case instrprof_error::value_site_count_mismatch:
68     return "Function value site count change detected (counter mismatch)";
69   case instrprof_error::compress_failed:
70     return "Failed to compress data (zlib)";
71   case instrprof_error::uncompress_failed:
72     return "Failed to uncompress data (zlib)";
73   }
74   llvm_unreachable("A value of instrprof_error has no message.");
75 }
76 
77 // FIXME: This class is only here to support the transition to llvm::Error. It
78 // will be removed once this transition is complete. Clients should prefer to
79 // deal with the Error value directly, rather than converting to error_code.
80 class InstrProfErrorCategoryType : public std::error_category {
81   const char *name() const LLVM_NOEXCEPT override { return "llvm.instrprof"; }
82   std::string message(int IE) const override {
83     return getInstrProfErrString(static_cast<instrprof_error>(IE));
84   }
85 };
86 } // end anonymous namespace
87 
88 static ManagedStatic<InstrProfErrorCategoryType> ErrorCategory;
89 
90 const std::error_category &llvm::instrprof_category() {
91   return *ErrorCategory;
92 }
93 
94 namespace llvm {
95 
96 void SoftInstrProfErrors::addError(instrprof_error IE) {
97   if (IE == instrprof_error::success)
98     return;
99 
100   if (FirstError == instrprof_error::success)
101     FirstError = IE;
102 
103   switch (IE) {
104   case instrprof_error::hash_mismatch:
105     ++NumHashMismatches;
106     break;
107   case instrprof_error::count_mismatch:
108     ++NumCountMismatches;
109     break;
110   case instrprof_error::counter_overflow:
111     ++NumCounterOverflows;
112     break;
113   case instrprof_error::value_site_count_mismatch:
114     ++NumValueSiteCountMismatches;
115     break;
116   default:
117     llvm_unreachable("Not a soft error");
118   }
119 }
120 
121 std::string InstrProfError::message() const {
122   return getInstrProfErrString(Err);
123 }
124 
125 char InstrProfError::ID = 0;
126 
127 std::string getPGOFuncName(StringRef RawFuncName,
128                            GlobalValue::LinkageTypes Linkage,
129                            StringRef FileName,
130                            uint64_t Version LLVM_ATTRIBUTE_UNUSED) {
131   return GlobalValue::getGlobalIdentifier(RawFuncName, Linkage, FileName);
132 }
133 
134 // Return the PGOFuncName. This function has some special handling when called
135 // in LTO optimization. The following only applies when calling in LTO passes
136 // (when \c InLTO is true): LTO's internalization privatizes many global linkage
137 // symbols. This happens after value profile annotation, but those internal
138 // linkage functions should not have a source prefix.
139 // To differentiate compiler generated internal symbols from original ones,
140 // PGOFuncName meta data are created and attached to the original internal
141 // symbols in the value profile annotation step
142 // (PGOUseFunc::annotateIndirectCallSites). If a symbol does not have the meta
143 // data, its original linkage must be non-internal.
144 std::string getPGOFuncName(const Function &F, bool InLTO, uint64_t Version) {
145   if (!InLTO) {
146     StringRef FileName = (StaticFuncFullModulePrefix
147                               ? F.getParent()->getName()
148                               : sys::path::filename(F.getParent()->getName()));
149     return getPGOFuncName(F.getName(), F.getLinkage(), FileName, Version);
150   }
151 
152   // In LTO mode (when InLTO is true), first check if there is a meta data.
153   if (MDNode *MD = getPGOFuncNameMetadata(F)) {
154     StringRef S = cast<MDString>(MD->getOperand(0))->getString();
155     return S.str();
156   }
157 
158   // If there is no meta data, the function must be a global before the value
159   // profile annotation pass. Its current linkage may be internal if it is
160   // internalized in LTO mode.
161   return getPGOFuncName(F.getName(), GlobalValue::ExternalLinkage, "");
162 }
163 
164 StringRef getFuncNameWithoutPrefix(StringRef PGOFuncName, StringRef FileName) {
165   if (FileName.empty())
166     return PGOFuncName;
167   // Drop the file name including ':'. See also getPGOFuncName.
168   if (PGOFuncName.startswith(FileName))
169     PGOFuncName = PGOFuncName.drop_front(FileName.size() + 1);
170   return PGOFuncName;
171 }
172 
173 // \p FuncName is the string used as profile lookup key for the function. A
174 // symbol is created to hold the name. Return the legalized symbol name.
175 std::string getPGOFuncNameVarName(StringRef FuncName,
176                                   GlobalValue::LinkageTypes Linkage) {
177   std::string VarName = getInstrProfNameVarPrefix();
178   VarName += FuncName;
179 
180   if (!GlobalValue::isLocalLinkage(Linkage))
181     return VarName;
182 
183   // Now fix up illegal chars in local VarName that may upset the assembler.
184   const char *InvalidChars = "-:<>/\"'";
185   size_t found = VarName.find_first_of(InvalidChars);
186   while (found != std::string::npos) {
187     VarName[found] = '_';
188     found = VarName.find_first_of(InvalidChars, found + 1);
189   }
190   return VarName;
191 }
192 
193 GlobalVariable *createPGOFuncNameVar(Module &M,
194                                      GlobalValue::LinkageTypes Linkage,
195                                      StringRef PGOFuncName) {
196 
197   // We generally want to match the function's linkage, but available_externally
198   // and extern_weak both have the wrong semantics, and anything that doesn't
199   // need to link across compilation units doesn't need to be visible at all.
200   if (Linkage == GlobalValue::ExternalWeakLinkage)
201     Linkage = GlobalValue::LinkOnceAnyLinkage;
202   else if (Linkage == GlobalValue::AvailableExternallyLinkage)
203     Linkage = GlobalValue::LinkOnceODRLinkage;
204   else if (Linkage == GlobalValue::InternalLinkage ||
205            Linkage == GlobalValue::ExternalLinkage)
206     Linkage = GlobalValue::PrivateLinkage;
207 
208   auto *Value =
209       ConstantDataArray::getString(M.getContext(), PGOFuncName, false);
210   auto FuncNameVar =
211       new GlobalVariable(M, Value->getType(), true, Linkage, Value,
212                          getPGOFuncNameVarName(PGOFuncName, Linkage));
213 
214   // Hide the symbol so that we correctly get a copy for each executable.
215   if (!GlobalValue::isLocalLinkage(FuncNameVar->getLinkage()))
216     FuncNameVar->setVisibility(GlobalValue::HiddenVisibility);
217 
218   return FuncNameVar;
219 }
220 
221 GlobalVariable *createPGOFuncNameVar(Function &F, StringRef PGOFuncName) {
222   return createPGOFuncNameVar(*F.getParent(), F.getLinkage(), PGOFuncName);
223 }
224 
225 void InstrProfSymtab::create(Module &M, bool InLTO) {
226   for (Function &F : M) {
227     // Function may not have a name: like using asm("") to overwrite the name.
228     // Ignore in this case.
229     if (!F.hasName())
230       continue;
231     const std::string &PGOFuncName = getPGOFuncName(F, InLTO);
232     addFuncName(PGOFuncName);
233     MD5FuncMap.emplace_back(Function::getGUID(PGOFuncName), &F);
234   }
235 
236   finalizeSymtab();
237 }
238 
239 Error collectPGOFuncNameStrings(const std::vector<std::string> &NameStrs,
240                                 bool doCompression, std::string &Result) {
241   assert(NameStrs.size() && "No name data to emit");
242 
243   uint8_t Header[16], *P = Header;
244   std::string UncompressedNameStrings =
245       join(NameStrs.begin(), NameStrs.end(), getInstrProfNameSeparator());
246 
247   assert(StringRef(UncompressedNameStrings)
248                  .count(getInstrProfNameSeparator()) == (NameStrs.size() - 1) &&
249          "PGO name is invalid (contains separator token)");
250 
251   unsigned EncLen = encodeULEB128(UncompressedNameStrings.length(), P);
252   P += EncLen;
253 
254   auto WriteStringToResult = [&](size_t CompressedLen, StringRef InputStr) {
255     EncLen = encodeULEB128(CompressedLen, P);
256     P += EncLen;
257     char *HeaderStr = reinterpret_cast<char *>(&Header[0]);
258     unsigned HeaderLen = P - &Header[0];
259     Result.append(HeaderStr, HeaderLen);
260     Result += InputStr;
261     return Error::success();
262   };
263 
264   if (!doCompression) {
265     return WriteStringToResult(0, UncompressedNameStrings);
266   }
267 
268   SmallString<128> CompressedNameStrings;
269   zlib::Status Success =
270       zlib::compress(StringRef(UncompressedNameStrings), CompressedNameStrings,
271                      zlib::BestSizeCompression);
272 
273   if (Success != zlib::StatusOK)
274     return make_error<InstrProfError>(instrprof_error::compress_failed);
275 
276   return WriteStringToResult(CompressedNameStrings.size(),
277                              CompressedNameStrings);
278 }
279 
280 StringRef getPGOFuncNameVarInitializer(GlobalVariable *NameVar) {
281   auto *Arr = cast<ConstantDataArray>(NameVar->getInitializer());
282   StringRef NameStr =
283       Arr->isCString() ? Arr->getAsCString() : Arr->getAsString();
284   return NameStr;
285 }
286 
287 Error collectPGOFuncNameStrings(const std::vector<GlobalVariable *> &NameVars,
288                                 std::string &Result, bool doCompression) {
289   std::vector<std::string> NameStrs;
290   for (auto *NameVar : NameVars) {
291     NameStrs.push_back(getPGOFuncNameVarInitializer(NameVar));
292   }
293   return collectPGOFuncNameStrings(
294       NameStrs, zlib::isAvailable() && doCompression, Result);
295 }
296 
297 Error readPGOFuncNameStrings(StringRef NameStrings, InstrProfSymtab &Symtab) {
298   const uint8_t *P = reinterpret_cast<const uint8_t *>(NameStrings.data());
299   const uint8_t *EndP = reinterpret_cast<const uint8_t *>(NameStrings.data() +
300                                                           NameStrings.size());
301   while (P < EndP) {
302     uint32_t N;
303     uint64_t UncompressedSize = decodeULEB128(P, &N);
304     P += N;
305     uint64_t CompressedSize = decodeULEB128(P, &N);
306     P += N;
307     bool isCompressed = (CompressedSize != 0);
308     SmallString<128> UncompressedNameStrings;
309     StringRef NameStrings;
310     if (isCompressed) {
311       StringRef CompressedNameStrings(reinterpret_cast<const char *>(P),
312                                       CompressedSize);
313       if (zlib::uncompress(CompressedNameStrings, UncompressedNameStrings,
314                            UncompressedSize) != zlib::StatusOK)
315         return make_error<InstrProfError>(instrprof_error::uncompress_failed);
316       P += CompressedSize;
317       NameStrings = StringRef(UncompressedNameStrings.data(),
318                               UncompressedNameStrings.size());
319     } else {
320       NameStrings =
321           StringRef(reinterpret_cast<const char *>(P), UncompressedSize);
322       P += UncompressedSize;
323     }
324     // Now parse the name strings.
325     SmallVector<StringRef, 0> Names;
326     NameStrings.split(Names, getInstrProfNameSeparator());
327     for (StringRef &Name : Names)
328       Symtab.addFuncName(Name);
329 
330     while (P < EndP && *P == 0)
331       P++;
332   }
333   Symtab.finalizeSymtab();
334   return Error::success();
335 }
336 
337 void InstrProfValueSiteRecord::merge(SoftInstrProfErrors &SIPE,
338                                      InstrProfValueSiteRecord &Input,
339                                      uint64_t Weight) {
340   this->sortByTargetValues();
341   Input.sortByTargetValues();
342   auto I = ValueData.begin();
343   auto IE = ValueData.end();
344   for (auto J = Input.ValueData.begin(), JE = Input.ValueData.end(); J != JE;
345        ++J) {
346     while (I != IE && I->Value < J->Value)
347       ++I;
348     if (I != IE && I->Value == J->Value) {
349       bool Overflowed;
350       I->Count = SaturatingMultiplyAdd(J->Count, Weight, I->Count, &Overflowed);
351       if (Overflowed)
352         SIPE.addError(instrprof_error::counter_overflow);
353       ++I;
354       continue;
355     }
356     ValueData.insert(I, *J);
357   }
358 }
359 
360 void InstrProfValueSiteRecord::scale(SoftInstrProfErrors &SIPE,
361                                      uint64_t Weight) {
362   for (auto I = ValueData.begin(), IE = ValueData.end(); I != IE; ++I) {
363     bool Overflowed;
364     I->Count = SaturatingMultiply(I->Count, Weight, &Overflowed);
365     if (Overflowed)
366       SIPE.addError(instrprof_error::counter_overflow);
367   }
368 }
369 
370 // Merge Value Profile data from Src record to this record for ValueKind.
371 // Scale merged value counts by \p Weight.
372 void InstrProfRecord::mergeValueProfData(uint32_t ValueKind,
373                                          InstrProfRecord &Src,
374                                          uint64_t Weight) {
375   uint32_t ThisNumValueSites = getNumValueSites(ValueKind);
376   uint32_t OtherNumValueSites = Src.getNumValueSites(ValueKind);
377   if (ThisNumValueSites != OtherNumValueSites) {
378     SIPE.addError(instrprof_error::value_site_count_mismatch);
379     return;
380   }
381   std::vector<InstrProfValueSiteRecord> &ThisSiteRecords =
382       getValueSitesForKind(ValueKind);
383   std::vector<InstrProfValueSiteRecord> &OtherSiteRecords =
384       Src.getValueSitesForKind(ValueKind);
385   for (uint32_t I = 0; I < ThisNumValueSites; I++)
386     ThisSiteRecords[I].merge(SIPE, OtherSiteRecords[I], Weight);
387 }
388 
389 void InstrProfRecord::merge(InstrProfRecord &Other, uint64_t Weight) {
390   // If the number of counters doesn't match we either have bad data
391   // or a hash collision.
392   if (Counts.size() != Other.Counts.size()) {
393     SIPE.addError(instrprof_error::count_mismatch);
394     return;
395   }
396 
397   for (size_t I = 0, E = Other.Counts.size(); I < E; ++I) {
398     bool Overflowed;
399     Counts[I] =
400         SaturatingMultiplyAdd(Other.Counts[I], Weight, Counts[I], &Overflowed);
401     if (Overflowed)
402       SIPE.addError(instrprof_error::counter_overflow);
403   }
404 
405   for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
406     mergeValueProfData(Kind, Other, Weight);
407 }
408 
409 void InstrProfRecord::scaleValueProfData(uint32_t ValueKind, uint64_t Weight) {
410   uint32_t ThisNumValueSites = getNumValueSites(ValueKind);
411   std::vector<InstrProfValueSiteRecord> &ThisSiteRecords =
412       getValueSitesForKind(ValueKind);
413   for (uint32_t I = 0; I < ThisNumValueSites; I++)
414     ThisSiteRecords[I].scale(SIPE, Weight);
415 }
416 
417 void InstrProfRecord::scale(uint64_t Weight) {
418   for (auto &Count : this->Counts) {
419     bool Overflowed;
420     Count = SaturatingMultiply(Count, Weight, &Overflowed);
421     if (Overflowed)
422       SIPE.addError(instrprof_error::counter_overflow);
423   }
424   for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
425     scaleValueProfData(Kind, Weight);
426 }
427 
428 // Map indirect call target name hash to name string.
429 uint64_t InstrProfRecord::remapValue(uint64_t Value, uint32_t ValueKind,
430                                      ValueMapType *ValueMap) {
431   if (!ValueMap)
432     return Value;
433   switch (ValueKind) {
434   case IPVK_IndirectCallTarget: {
435     auto Result =
436         std::lower_bound(ValueMap->begin(), ValueMap->end(), Value,
437                          [](const std::pair<uint64_t, uint64_t> &LHS,
438                             uint64_t RHS) { return LHS.first < RHS; });
439    // Raw function pointer collected by value profiler may be from
440    // external functions that are not instrumented. They won't have
441    // mapping data to be used by the deserializer. Force the value to
442    // be 0 in this case.
443     if (Result != ValueMap->end() && Result->first == Value)
444       Value = (uint64_t)Result->second;
445     else
446       Value = 0;
447     break;
448   }
449   }
450   return Value;
451 }
452 
453 void InstrProfRecord::addValueData(uint32_t ValueKind, uint32_t Site,
454                                    InstrProfValueData *VData, uint32_t N,
455                                    ValueMapType *ValueMap) {
456   for (uint32_t I = 0; I < N; I++) {
457     VData[I].Value = remapValue(VData[I].Value, ValueKind, ValueMap);
458   }
459   std::vector<InstrProfValueSiteRecord> &ValueSites =
460       getValueSitesForKind(ValueKind);
461   if (N == 0)
462     ValueSites.emplace_back();
463   else
464     ValueSites.emplace_back(VData, VData + N);
465 }
466 
467 #define INSTR_PROF_COMMON_API_IMPL
468 #include "llvm/ProfileData/InstrProfData.inc"
469 
470 /*!
471  * \brief ValueProfRecordClosure Interface implementation for  InstrProfRecord
472  *  class. These C wrappers are used as adaptors so that C++ code can be
473  *  invoked as callbacks.
474  */
475 uint32_t getNumValueKindsInstrProf(const void *Record) {
476   return reinterpret_cast<const InstrProfRecord *>(Record)->getNumValueKinds();
477 }
478 
479 uint32_t getNumValueSitesInstrProf(const void *Record, uint32_t VKind) {
480   return reinterpret_cast<const InstrProfRecord *>(Record)
481       ->getNumValueSites(VKind);
482 }
483 
484 uint32_t getNumValueDataInstrProf(const void *Record, uint32_t VKind) {
485   return reinterpret_cast<const InstrProfRecord *>(Record)
486       ->getNumValueData(VKind);
487 }
488 
489 uint32_t getNumValueDataForSiteInstrProf(const void *R, uint32_t VK,
490                                          uint32_t S) {
491   return reinterpret_cast<const InstrProfRecord *>(R)
492       ->getNumValueDataForSite(VK, S);
493 }
494 
495 void getValueForSiteInstrProf(const void *R, InstrProfValueData *Dst,
496                               uint32_t K, uint32_t S) {
497   reinterpret_cast<const InstrProfRecord *>(R)->getValueForSite(Dst, K, S);
498 }
499 
500 ValueProfData *allocValueProfDataInstrProf(size_t TotalSizeInBytes) {
501   ValueProfData *VD =
502       (ValueProfData *)(new (::operator new(TotalSizeInBytes)) ValueProfData());
503   memset(VD, 0, TotalSizeInBytes);
504   return VD;
505 }
506 
507 static ValueProfRecordClosure InstrProfRecordClosure = {
508     nullptr,
509     getNumValueKindsInstrProf,
510     getNumValueSitesInstrProf,
511     getNumValueDataInstrProf,
512     getNumValueDataForSiteInstrProf,
513     nullptr,
514     getValueForSiteInstrProf,
515     allocValueProfDataInstrProf};
516 
517 // Wrapper implementation using the closure mechanism.
518 uint32_t ValueProfData::getSize(const InstrProfRecord &Record) {
519   InstrProfRecordClosure.Record = &Record;
520   return getValueProfDataSize(&InstrProfRecordClosure);
521 }
522 
523 // Wrapper implementation using the closure mechanism.
524 std::unique_ptr<ValueProfData>
525 ValueProfData::serializeFrom(const InstrProfRecord &Record) {
526   InstrProfRecordClosure.Record = &Record;
527 
528   std::unique_ptr<ValueProfData> VPD(
529       serializeValueProfDataFrom(&InstrProfRecordClosure, nullptr));
530   return VPD;
531 }
532 
533 void ValueProfRecord::deserializeTo(InstrProfRecord &Record,
534                                     InstrProfRecord::ValueMapType *VMap) {
535   Record.reserveSites(Kind, NumValueSites);
536 
537   InstrProfValueData *ValueData = getValueProfRecordValueData(this);
538   for (uint64_t VSite = 0; VSite < NumValueSites; ++VSite) {
539     uint8_t ValueDataCount = this->SiteCountArray[VSite];
540     Record.addValueData(Kind, VSite, ValueData, ValueDataCount, VMap);
541     ValueData += ValueDataCount;
542   }
543 }
544 
545 // For writing/serializing,  Old is the host endianness, and  New is
546 // byte order intended on disk. For Reading/deserialization, Old
547 // is the on-disk source endianness, and New is the host endianness.
548 void ValueProfRecord::swapBytes(support::endianness Old,
549                                 support::endianness New) {
550   using namespace support;
551   if (Old == New)
552     return;
553 
554   if (getHostEndianness() != Old) {
555     sys::swapByteOrder<uint32_t>(NumValueSites);
556     sys::swapByteOrder<uint32_t>(Kind);
557   }
558   uint32_t ND = getValueProfRecordNumValueData(this);
559   InstrProfValueData *VD = getValueProfRecordValueData(this);
560 
561   // No need to swap byte array: SiteCountArrray.
562   for (uint32_t I = 0; I < ND; I++) {
563     sys::swapByteOrder<uint64_t>(VD[I].Value);
564     sys::swapByteOrder<uint64_t>(VD[I].Count);
565   }
566   if (getHostEndianness() == Old) {
567     sys::swapByteOrder<uint32_t>(NumValueSites);
568     sys::swapByteOrder<uint32_t>(Kind);
569   }
570 }
571 
572 void ValueProfData::deserializeTo(InstrProfRecord &Record,
573                                   InstrProfRecord::ValueMapType *VMap) {
574   if (NumValueKinds == 0)
575     return;
576 
577   ValueProfRecord *VR = getFirstValueProfRecord(this);
578   for (uint32_t K = 0; K < NumValueKinds; K++) {
579     VR->deserializeTo(Record, VMap);
580     VR = getValueProfRecordNext(VR);
581   }
582 }
583 
584 template <class T>
585 static T swapToHostOrder(const unsigned char *&D, support::endianness Orig) {
586   using namespace support;
587   if (Orig == little)
588     return endian::readNext<T, little, unaligned>(D);
589   else
590     return endian::readNext<T, big, unaligned>(D);
591 }
592 
593 static std::unique_ptr<ValueProfData> allocValueProfData(uint32_t TotalSize) {
594   return std::unique_ptr<ValueProfData>(new (::operator new(TotalSize))
595                                             ValueProfData());
596 }
597 
598 Error ValueProfData::checkIntegrity() {
599   if (NumValueKinds > IPVK_Last + 1)
600     return make_error<InstrProfError>(instrprof_error::malformed);
601   // Total size needs to be mulltiple of quadword size.
602   if (TotalSize % sizeof(uint64_t))
603     return make_error<InstrProfError>(instrprof_error::malformed);
604 
605   ValueProfRecord *VR = getFirstValueProfRecord(this);
606   for (uint32_t K = 0; K < this->NumValueKinds; K++) {
607     if (VR->Kind > IPVK_Last)
608       return make_error<InstrProfError>(instrprof_error::malformed);
609     VR = getValueProfRecordNext(VR);
610     if ((char *)VR - (char *)this > (ptrdiff_t)TotalSize)
611       return make_error<InstrProfError>(instrprof_error::malformed);
612   }
613   return Error::success();
614 }
615 
616 Expected<std::unique_ptr<ValueProfData>>
617 ValueProfData::getValueProfData(const unsigned char *D,
618                                 const unsigned char *const BufferEnd,
619                                 support::endianness Endianness) {
620   using namespace support;
621   if (D + sizeof(ValueProfData) > BufferEnd)
622     return make_error<InstrProfError>(instrprof_error::truncated);
623 
624   const unsigned char *Header = D;
625   uint32_t TotalSize = swapToHostOrder<uint32_t>(Header, Endianness);
626   if (D + TotalSize > BufferEnd)
627     return make_error<InstrProfError>(instrprof_error::too_large);
628 
629   std::unique_ptr<ValueProfData> VPD = allocValueProfData(TotalSize);
630   memcpy(VPD.get(), D, TotalSize);
631   // Byte swap.
632   VPD->swapBytesToHost(Endianness);
633 
634   Error E = VPD->checkIntegrity();
635   if (E)
636     return std::move(E);
637 
638   return std::move(VPD);
639 }
640 
641 void ValueProfData::swapBytesToHost(support::endianness Endianness) {
642   using namespace support;
643   if (Endianness == getHostEndianness())
644     return;
645 
646   sys::swapByteOrder<uint32_t>(TotalSize);
647   sys::swapByteOrder<uint32_t>(NumValueKinds);
648 
649   ValueProfRecord *VR = getFirstValueProfRecord(this);
650   for (uint32_t K = 0; K < NumValueKinds; K++) {
651     VR->swapBytes(Endianness, getHostEndianness());
652     VR = getValueProfRecordNext(VR);
653   }
654 }
655 
656 void ValueProfData::swapBytesFromHost(support::endianness Endianness) {
657   using namespace support;
658   if (Endianness == getHostEndianness())
659     return;
660 
661   ValueProfRecord *VR = getFirstValueProfRecord(this);
662   for (uint32_t K = 0; K < NumValueKinds; K++) {
663     ValueProfRecord *NVR = getValueProfRecordNext(VR);
664     VR->swapBytes(getHostEndianness(), Endianness);
665     VR = NVR;
666   }
667   sys::swapByteOrder<uint32_t>(TotalSize);
668   sys::swapByteOrder<uint32_t>(NumValueKinds);
669 }
670 
671 void annotateValueSite(Module &M, Instruction &Inst,
672                        const InstrProfRecord &InstrProfR,
673                        InstrProfValueKind ValueKind, uint32_t SiteIdx,
674                        uint32_t MaxMDCount) {
675   uint32_t NV = InstrProfR.getNumValueDataForSite(ValueKind, SiteIdx);
676   if (!NV)
677     return;
678 
679   uint64_t Sum = 0;
680   std::unique_ptr<InstrProfValueData[]> VD =
681       InstrProfR.getValueForSite(ValueKind, SiteIdx, &Sum);
682 
683   ArrayRef<InstrProfValueData> VDs(VD.get(), NV);
684   annotateValueSite(M, Inst, VDs, Sum, ValueKind, MaxMDCount);
685 }
686 
687 void annotateValueSite(Module &M, Instruction &Inst,
688                        ArrayRef<InstrProfValueData> VDs,
689                        uint64_t Sum, InstrProfValueKind ValueKind,
690                        uint32_t MaxMDCount) {
691   LLVMContext &Ctx = M.getContext();
692   MDBuilder MDHelper(Ctx);
693   SmallVector<Metadata *, 3> Vals;
694   // Tag
695   Vals.push_back(MDHelper.createString("VP"));
696   // Value Kind
697   Vals.push_back(MDHelper.createConstant(
698       ConstantInt::get(Type::getInt32Ty(Ctx), ValueKind)));
699   // Total Count
700   Vals.push_back(
701       MDHelper.createConstant(ConstantInt::get(Type::getInt64Ty(Ctx), Sum)));
702 
703   // Value Profile Data
704   uint32_t MDCount = MaxMDCount;
705   for (auto &VD : VDs) {
706     Vals.push_back(MDHelper.createConstant(
707         ConstantInt::get(Type::getInt64Ty(Ctx), VD.Value)));
708     Vals.push_back(MDHelper.createConstant(
709         ConstantInt::get(Type::getInt64Ty(Ctx), VD.Count)));
710     if (--MDCount == 0)
711       break;
712   }
713   Inst.setMetadata(LLVMContext::MD_prof, MDNode::get(Ctx, Vals));
714 }
715 
716 bool getValueProfDataFromInst(const Instruction &Inst,
717                               InstrProfValueKind ValueKind,
718                               uint32_t MaxNumValueData,
719                               InstrProfValueData ValueData[],
720                               uint32_t &ActualNumValueData, uint64_t &TotalC) {
721   MDNode *MD = Inst.getMetadata(LLVMContext::MD_prof);
722   if (!MD)
723     return false;
724 
725   unsigned NOps = MD->getNumOperands();
726 
727   if (NOps < 5)
728     return false;
729 
730   // Operand 0 is a string tag "VP":
731   MDString *Tag = cast<MDString>(MD->getOperand(0));
732   if (!Tag)
733     return false;
734 
735   if (!Tag->getString().equals("VP"))
736     return false;
737 
738   // Now check kind:
739   ConstantInt *KindInt = mdconst::dyn_extract<ConstantInt>(MD->getOperand(1));
740   if (!KindInt)
741     return false;
742   if (KindInt->getZExtValue() != ValueKind)
743     return false;
744 
745   // Get total count
746   ConstantInt *TotalCInt = mdconst::dyn_extract<ConstantInt>(MD->getOperand(2));
747   if (!TotalCInt)
748     return false;
749   TotalC = TotalCInt->getZExtValue();
750 
751   ActualNumValueData = 0;
752 
753   for (unsigned I = 3; I < NOps; I += 2) {
754     if (ActualNumValueData >= MaxNumValueData)
755       break;
756     ConstantInt *Value = mdconst::dyn_extract<ConstantInt>(MD->getOperand(I));
757     ConstantInt *Count =
758         mdconst::dyn_extract<ConstantInt>(MD->getOperand(I + 1));
759     if (!Value || !Count)
760       return false;
761     ValueData[ActualNumValueData].Value = Value->getZExtValue();
762     ValueData[ActualNumValueData].Count = Count->getZExtValue();
763     ActualNumValueData++;
764   }
765   return true;
766 }
767 
768 MDNode *getPGOFuncNameMetadata(const Function &F) {
769   return F.getMetadata(getPGOFuncNameMetadataName());
770 }
771 
772 void createPGOFuncNameMetadata(Function &F, StringRef PGOFuncName) {
773   // Only for internal linkage functions.
774   if (PGOFuncName == F.getName())
775       return;
776   // Don't create duplicated meta-data.
777   if (getPGOFuncNameMetadata(F))
778     return;
779   LLVMContext &C = F.getContext();
780   MDNode *N = MDNode::get(C, MDString::get(C, PGOFuncName));
781   F.setMetadata(getPGOFuncNameMetadataName(), N);
782 }
783 
784 bool needsComdatForCounter(const Function &F, const Module &M) {
785   if (F.hasComdat())
786     return true;
787 
788   Triple TT(M.getTargetTriple());
789   if (!TT.isOSBinFormatELF())
790     return false;
791 
792   // See createPGOFuncNameVar for more details. To avoid link errors, profile
793   // counters for function with available_externally linkage needs to be changed
794   // to linkonce linkage. On ELF based systems, this leads to weak symbols to be
795   // created. Without using comdat, duplicate entries won't be removed by the
796   // linker leading to increased data segement size and raw profile size. Even
797   // worse, since the referenced counter from profile per-function data object
798   // will be resolved to the common strong definition, the profile counts for
799   // available_externally functions will end up being duplicated in raw profile
800   // data. This can result in distorted profile as the counts of those dups
801   // will be accumulated by the profile merger.
802   GlobalValue::LinkageTypes Linkage = F.getLinkage();
803   if (Linkage != GlobalValue::ExternalWeakLinkage &&
804       Linkage != GlobalValue::AvailableExternallyLinkage)
805     return false;
806 
807   return true;
808 }
809 } // end namespace llvm
810