OSDN Git Service

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