OSDN Git Service

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