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