1 //===- InstrProf.cpp - Instrumented profiling format support --------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains support for clang's instrumentation based PGO and
10 // coverage.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/ProfileData/InstrProf.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/Triple.h"
21 #include "llvm/IR/Constant.h"
22 #include "llvm/IR/Constants.h"
23 #include "llvm/IR/Function.h"
24 #include "llvm/IR/GlobalValue.h"
25 #include "llvm/IR/GlobalVariable.h"
26 #include "llvm/IR/Instruction.h"
27 #include "llvm/IR/LLVMContext.h"
28 #include "llvm/IR/MDBuilder.h"
29 #include "llvm/IR/Metadata.h"
30 #include "llvm/IR/Module.h"
31 #include "llvm/IR/Type.h"
32 #include "llvm/Support/Casting.h"
33 #include "llvm/Support/CommandLine.h"
34 #include "llvm/Support/Compiler.h"
35 #include "llvm/Support/Compression.h"
36 #include "llvm/Support/Endian.h"
37 #include "llvm/Support/Error.h"
38 #include "llvm/Support/ErrorHandling.h"
39 #include "llvm/Support/LEB128.h"
40 #include "llvm/Support/ManagedStatic.h"
41 #include "llvm/Support/MathExtras.h"
42 #include "llvm/Support/Path.h"
43 #include "llvm/Support/SwapByteOrder.h"
44 #include <algorithm>
45 #include <cassert>
46 #include <cstddef>
47 #include <cstdint>
48 #include <cstring>
49 #include <memory>
50 #include <string>
51 #include <system_error>
52 #include <utility>
53 #include <vector>
54 
55 using namespace llvm;
56 
57 static cl::opt<bool> StaticFuncFullModulePrefix(
58     "static-func-full-module-prefix", cl::init(true), cl::Hidden,
59     cl::desc("Use full module build paths in the profile counter names for "
60              "static functions."));
61 
62 // This option is tailored to users that have different top-level directory in
63 // profile-gen and profile-use compilation. Users need to specific the number
64 // of levels to strip. A value larger than the number of directories in the
65 // source file will strip all the directory names and only leave the basename.
66 //
67 // Note current ThinLTO module importing for the indirect-calls assumes
68 // the source directory name not being stripped. A non-zero option value here
69 // can potentially prevent some inter-module indirect-call-promotions.
70 static cl::opt<unsigned> StaticFuncStripDirNamePrefix(
71     "static-func-strip-dirname-prefix", cl::init(0), cl::Hidden,
72     cl::desc("Strip specified level of directory name from source path in "
73              "the profile counter name for static functions."));
74 
75 static std::string getInstrProfErrString(instrprof_error Err) {
76   switch (Err) {
77   case instrprof_error::success:
78     return "Success";
79   case instrprof_error::eof:
80     return "End of File";
81   case instrprof_error::unrecognized_format:
82     return "Unrecognized instrumentation profile encoding format";
83   case instrprof_error::bad_magic:
84     return "Invalid instrumentation profile data (bad magic)";
85   case instrprof_error::bad_header:
86     return "Invalid instrumentation profile data (file header is corrupt)";
87   case instrprof_error::unsupported_version:
88     return "Unsupported instrumentation profile format version";
89   case instrprof_error::unsupported_hash_type:
90     return "Unsupported instrumentation profile hash type";
91   case instrprof_error::too_large:
92     return "Too much profile data";
93   case instrprof_error::truncated:
94     return "Truncated profile data";
95   case instrprof_error::malformed:
96     return "Malformed instrumentation profile data";
97   case instrprof_error::unknown_function:
98     return "No profile data available for function";
99   case instrprof_error::hash_mismatch:
100     return "Function control flow change detected (hash mismatch)";
101   case instrprof_error::count_mismatch:
102     return "Function basic block count change detected (counter mismatch)";
103   case instrprof_error::counter_overflow:
104     return "Counter overflow";
105   case instrprof_error::value_site_count_mismatch:
106     return "Function value site count change detected (counter mismatch)";
107   case instrprof_error::compress_failed:
108     return "Failed to compress data (zlib)";
109   case instrprof_error::uncompress_failed:
110     return "Failed to uncompress data (zlib)";
111   case instrprof_error::empty_raw_profile:
112     return "Empty raw profile file";
113   case instrprof_error::zlib_unavailable:
114     return "Profile uses zlib compression but the profile reader was built without zlib support";
115   }
116   llvm_unreachable("A value of instrprof_error has no message.");
117 }
118 
119 namespace {
120 
121 // FIXME: This class is only here to support the transition to llvm::Error. It
122 // will be removed once this transition is complete. Clients should prefer to
123 // deal with the Error value directly, rather than converting to error_code.
124 class InstrProfErrorCategoryType : public std::error_category {
125   const char *name() const noexcept override { return "llvm.instrprof"; }
126 
127   std::string message(int IE) const override {
128     return getInstrProfErrString(static_cast<instrprof_error>(IE));
129   }
130 };
131 
132 } // end anonymous namespace
133 
134 static ManagedStatic<InstrProfErrorCategoryType> ErrorCategory;
135 
136 const std::error_category &llvm::instrprof_category() {
137   return *ErrorCategory;
138 }
139 
140 namespace {
141 
142 const char *InstrProfSectNameCommon[] = {
143 #define INSTR_PROF_SECT_ENTRY(Kind, SectNameCommon, SectNameCoff, Prefix)      \
144   SectNameCommon,
145 #include "llvm/ProfileData/InstrProfData.inc"
146 };
147 
148 const char *InstrProfSectNameCoff[] = {
149 #define INSTR_PROF_SECT_ENTRY(Kind, SectNameCommon, SectNameCoff, Prefix)      \
150   SectNameCoff,
151 #include "llvm/ProfileData/InstrProfData.inc"
152 };
153 
154 const char *InstrProfSectNamePrefix[] = {
155 #define INSTR_PROF_SECT_ENTRY(Kind, SectNameCommon, SectNameCoff, Prefix)      \
156   Prefix,
157 #include "llvm/ProfileData/InstrProfData.inc"
158 };
159 
160 } // namespace
161 
162 namespace llvm {
163 
164 std::string getInstrProfSectionName(InstrProfSectKind IPSK,
165                                     Triple::ObjectFormatType OF,
166                                     bool AddSegmentInfo) {
167   std::string SectName;
168 
169   if (OF == Triple::MachO && AddSegmentInfo)
170     SectName = InstrProfSectNamePrefix[IPSK];
171 
172   if (OF == Triple::COFF)
173     SectName += InstrProfSectNameCoff[IPSK];
174   else
175     SectName += InstrProfSectNameCommon[IPSK];
176 
177   if (OF == Triple::MachO && IPSK == IPSK_data && AddSegmentInfo)
178     SectName += ",regular,live_support";
179 
180   return SectName;
181 }
182 
183 void SoftInstrProfErrors::addError(instrprof_error IE) {
184   if (IE == instrprof_error::success)
185     return;
186 
187   if (FirstError == instrprof_error::success)
188     FirstError = IE;
189 
190   switch (IE) {
191   case instrprof_error::hash_mismatch:
192     ++NumHashMismatches;
193     break;
194   case instrprof_error::count_mismatch:
195     ++NumCountMismatches;
196     break;
197   case instrprof_error::counter_overflow:
198     ++NumCounterOverflows;
199     break;
200   case instrprof_error::value_site_count_mismatch:
201     ++NumValueSiteCountMismatches;
202     break;
203   default:
204     llvm_unreachable("Not a soft error");
205   }
206 }
207 
208 std::string InstrProfError::message() const {
209   return getInstrProfErrString(Err);
210 }
211 
212 char InstrProfError::ID = 0;
213 
214 std::string getPGOFuncName(StringRef RawFuncName,
215                            GlobalValue::LinkageTypes Linkage,
216                            StringRef FileName,
217                            uint64_t Version LLVM_ATTRIBUTE_UNUSED) {
218   return GlobalValue::getGlobalIdentifier(RawFuncName, Linkage, FileName);
219 }
220 
221 // Strip NumPrefix level of directory name from PathNameStr. If the number of
222 // directory separators is less than NumPrefix, strip all the directories and
223 // leave base file name only.
224 static StringRef stripDirPrefix(StringRef PathNameStr, uint32_t NumPrefix) {
225   uint32_t Count = NumPrefix;
226   uint32_t Pos = 0, LastPos = 0;
227   for (auto & CI : PathNameStr) {
228     ++Pos;
229     if (llvm::sys::path::is_separator(CI)) {
230       LastPos = Pos;
231       --Count;
232     }
233     if (Count == 0)
234       break;
235   }
236   return PathNameStr.substr(LastPos);
237 }
238 
239 // Return the PGOFuncName. This function has some special handling when called
240 // in LTO optimization. The following only applies when calling in LTO passes
241 // (when \c InLTO is true): LTO's internalization privatizes many global linkage
242 // symbols. This happens after value profile annotation, but those internal
243 // linkage functions should not have a source prefix.
244 // Additionally, for ThinLTO mode, exported internal functions are promoted
245 // and renamed. We need to ensure that the original internal PGO name is
246 // used when computing the GUID that is compared against the profiled GUIDs.
247 // To differentiate compiler generated internal symbols from original ones,
248 // PGOFuncName meta data are created and attached to the original internal
249 // symbols in the value profile annotation step
250 // (PGOUseFunc::annotateIndirectCallSites). If a symbol does not have the meta
251 // data, its original linkage must be non-internal.
252 std::string getPGOFuncName(const Function &F, bool InLTO, uint64_t Version) {
253   if (!InLTO) {
254     StringRef FileName(F.getParent()->getSourceFileName());
255     uint32_t StripLevel = StaticFuncFullModulePrefix ? 0 : (uint32_t)-1;
256     if (StripLevel < StaticFuncStripDirNamePrefix)
257       StripLevel = StaticFuncStripDirNamePrefix;
258     if (StripLevel)
259       FileName = stripDirPrefix(FileName, StripLevel);
260     return getPGOFuncName(F.getName(), F.getLinkage(), FileName, Version);
261   }
262 
263   // In LTO mode (when InLTO is true), first check if there is a meta data.
264   if (MDNode *MD = getPGOFuncNameMetadata(F)) {
265     StringRef S = cast<MDString>(MD->getOperand(0))->getString();
266     return S.str();
267   }
268 
269   // If there is no meta data, the function must be a global before the value
270   // profile annotation pass. Its current linkage may be internal if it is
271   // internalized in LTO mode.
272   return getPGOFuncName(F.getName(), GlobalValue::ExternalLinkage, "");
273 }
274 
275 StringRef getFuncNameWithoutPrefix(StringRef PGOFuncName, StringRef FileName) {
276   if (FileName.empty())
277     return PGOFuncName;
278   // Drop the file name including ':'. See also getPGOFuncName.
279   if (PGOFuncName.startswith(FileName))
280     PGOFuncName = PGOFuncName.drop_front(FileName.size() + 1);
281   return PGOFuncName;
282 }
283 
284 // \p FuncName is the string used as profile lookup key for the function. A
285 // symbol is created to hold the name. Return the legalized symbol name.
286 std::string getPGOFuncNameVarName(StringRef FuncName,
287                                   GlobalValue::LinkageTypes Linkage) {
288   std::string VarName = getInstrProfNameVarPrefix();
289   VarName += FuncName;
290 
291   if (!GlobalValue::isLocalLinkage(Linkage))
292     return VarName;
293 
294   // Now fix up illegal chars in local VarName that may upset the assembler.
295   const char *InvalidChars = "-:<>/\"'";
296   size_t found = VarName.find_first_of(InvalidChars);
297   while (found != std::string::npos) {
298     VarName[found] = '_';
299     found = VarName.find_first_of(InvalidChars, found + 1);
300   }
301   return VarName;
302 }
303 
304 GlobalVariable *createPGOFuncNameVar(Module &M,
305                                      GlobalValue::LinkageTypes Linkage,
306                                      StringRef PGOFuncName) {
307   // We generally want to match the function's linkage, but available_externally
308   // and extern_weak both have the wrong semantics, and anything that doesn't
309   // need to link across compilation units doesn't need to be visible at all.
310   if (Linkage == GlobalValue::ExternalWeakLinkage)
311     Linkage = GlobalValue::LinkOnceAnyLinkage;
312   else if (Linkage == GlobalValue::AvailableExternallyLinkage)
313     Linkage = GlobalValue::LinkOnceODRLinkage;
314   else if (Linkage == GlobalValue::InternalLinkage ||
315            Linkage == GlobalValue::ExternalLinkage)
316     Linkage = GlobalValue::PrivateLinkage;
317 
318   auto *Value =
319       ConstantDataArray::getString(M.getContext(), PGOFuncName, false);
320   auto FuncNameVar =
321       new GlobalVariable(M, Value->getType(), true, Linkage, Value,
322                          getPGOFuncNameVarName(PGOFuncName, Linkage));
323 
324   // Hide the symbol so that we correctly get a copy for each executable.
325   if (!GlobalValue::isLocalLinkage(FuncNameVar->getLinkage()))
326     FuncNameVar->setVisibility(GlobalValue::HiddenVisibility);
327 
328   return FuncNameVar;
329 }
330 
331 GlobalVariable *createPGOFuncNameVar(Function &F, StringRef PGOFuncName) {
332   return createPGOFuncNameVar(*F.getParent(), F.getLinkage(), PGOFuncName);
333 }
334 
335 Error InstrProfSymtab::create(Module &M, bool InLTO) {
336   for (Function &F : M) {
337     // Function may not have a name: like using asm("") to overwrite the name.
338     // Ignore in this case.
339     if (!F.hasName())
340       continue;
341     const std::string &PGOFuncName = getPGOFuncName(F, InLTO);
342     if (Error E = addFuncName(PGOFuncName))
343       return E;
344     MD5FuncMap.emplace_back(Function::getGUID(PGOFuncName), &F);
345     // In ThinLTO, local function may have been promoted to global and have
346     // suffix added to the function name. We need to add the stripped function
347     // name to the symbol table so that we can find a match from profile.
348     if (InLTO) {
349       auto pos = PGOFuncName.find('.');
350       if (pos != std::string::npos) {
351         const std::string &OtherFuncName = PGOFuncName.substr(0, pos);
352         if (Error E = addFuncName(OtherFuncName))
353           return E;
354         MD5FuncMap.emplace_back(Function::getGUID(OtherFuncName), &F);
355       }
356     }
357   }
358   Sorted = false;
359   finalizeSymtab();
360   return Error::success();
361 }
362 
363 uint64_t InstrProfSymtab::getFunctionHashFromAddress(uint64_t Address) {
364   finalizeSymtab();
365   auto Result =
366       std::lower_bound(AddrToMD5Map.begin(), AddrToMD5Map.end(), Address,
367                        [](const std::pair<uint64_t, uint64_t> &LHS,
368                           uint64_t RHS) { return LHS.first < RHS; });
369   // Raw function pointer collected by value profiler may be from
370   // external functions that are not instrumented. They won't have
371   // mapping data to be used by the deserializer. Force the value to
372   // be 0 in this case.
373   if (Result != AddrToMD5Map.end() && Result->first == Address)
374     return (uint64_t)Result->second;
375   return 0;
376 }
377 
378 Error collectPGOFuncNameStrings(ArrayRef<std::string> NameStrs,
379                                 bool doCompression, std::string &Result) {
380   assert(!NameStrs.empty() && "No name data to emit");
381 
382   uint8_t Header[16], *P = Header;
383   std::string UncompressedNameStrings =
384       join(NameStrs.begin(), NameStrs.end(), getInstrProfNameSeparator());
385 
386   assert(StringRef(UncompressedNameStrings)
387                  .count(getInstrProfNameSeparator()) == (NameStrs.size() - 1) &&
388          "PGO name is invalid (contains separator token)");
389 
390   unsigned EncLen = encodeULEB128(UncompressedNameStrings.length(), P);
391   P += EncLen;
392 
393   auto WriteStringToResult = [&](size_t CompressedLen, StringRef InputStr) {
394     EncLen = encodeULEB128(CompressedLen, P);
395     P += EncLen;
396     char *HeaderStr = reinterpret_cast<char *>(&Header[0]);
397     unsigned HeaderLen = P - &Header[0];
398     Result.append(HeaderStr, HeaderLen);
399     Result += InputStr;
400     return Error::success();
401   };
402 
403   if (!doCompression) {
404     return WriteStringToResult(0, UncompressedNameStrings);
405   }
406 
407   SmallString<128> CompressedNameStrings;
408   Error E = zlib::compress(StringRef(UncompressedNameStrings),
409                            CompressedNameStrings, zlib::BestSizeCompression);
410   if (E) {
411     consumeError(std::move(E));
412     return make_error<InstrProfError>(instrprof_error::compress_failed);
413   }
414 
415   return WriteStringToResult(CompressedNameStrings.size(),
416                              CompressedNameStrings);
417 }
418 
419 StringRef getPGOFuncNameVarInitializer(GlobalVariable *NameVar) {
420   auto *Arr = cast<ConstantDataArray>(NameVar->getInitializer());
421   StringRef NameStr =
422       Arr->isCString() ? Arr->getAsCString() : Arr->getAsString();
423   return NameStr;
424 }
425 
426 Error collectPGOFuncNameStrings(ArrayRef<GlobalVariable *> NameVars,
427                                 std::string &Result, bool doCompression) {
428   std::vector<std::string> NameStrs;
429   for (auto *NameVar : NameVars) {
430     NameStrs.push_back(getPGOFuncNameVarInitializer(NameVar));
431   }
432   return collectPGOFuncNameStrings(
433       NameStrs, zlib::isAvailable() && doCompression, Result);
434 }
435 
436 Error readPGOFuncNameStrings(StringRef NameStrings, InstrProfSymtab &Symtab) {
437   const uint8_t *P = NameStrings.bytes_begin();
438   const uint8_t *EndP = NameStrings.bytes_end();
439   while (P < EndP) {
440     uint32_t N;
441     uint64_t UncompressedSize = decodeULEB128(P, &N);
442     P += N;
443     uint64_t CompressedSize = decodeULEB128(P, &N);
444     P += N;
445     bool isCompressed = (CompressedSize != 0);
446     SmallString<128> UncompressedNameStrings;
447     StringRef NameStrings;
448     if (isCompressed) {
449       if (!llvm::zlib::isAvailable())
450         return make_error<InstrProfError>(instrprof_error::zlib_unavailable);
451 
452       StringRef CompressedNameStrings(reinterpret_cast<const char *>(P),
453                                       CompressedSize);
454       if (Error E =
455               zlib::uncompress(CompressedNameStrings, UncompressedNameStrings,
456                                UncompressedSize)) {
457         consumeError(std::move(E));
458         return make_error<InstrProfError>(instrprof_error::uncompress_failed);
459       }
460       P += CompressedSize;
461       NameStrings = StringRef(UncompressedNameStrings.data(),
462                               UncompressedNameStrings.size());
463     } else {
464       NameStrings =
465           StringRef(reinterpret_cast<const char *>(P), UncompressedSize);
466       P += UncompressedSize;
467     }
468     // Now parse the name strings.
469     SmallVector<StringRef, 0> Names;
470     NameStrings.split(Names, getInstrProfNameSeparator());
471     for (StringRef &Name : Names)
472       if (Error E = Symtab.addFuncName(Name))
473         return E;
474 
475     while (P < EndP && *P == 0)
476       P++;
477   }
478   return Error::success();
479 }
480 
481 void InstrProfValueSiteRecord::merge(InstrProfValueSiteRecord &Input,
482                                      uint64_t Weight,
483                                      function_ref<void(instrprof_error)> Warn) {
484   this->sortByTargetValues();
485   Input.sortByTargetValues();
486   auto I = ValueData.begin();
487   auto IE = ValueData.end();
488   for (auto J = Input.ValueData.begin(), JE = Input.ValueData.end(); J != JE;
489        ++J) {
490     while (I != IE && I->Value < J->Value)
491       ++I;
492     if (I != IE && I->Value == J->Value) {
493       bool Overflowed;
494       I->Count = SaturatingMultiplyAdd(J->Count, Weight, I->Count, &Overflowed);
495       if (Overflowed)
496         Warn(instrprof_error::counter_overflow);
497       ++I;
498       continue;
499     }
500     ValueData.insert(I, *J);
501   }
502 }
503 
504 void InstrProfValueSiteRecord::scale(uint64_t Weight,
505                                      function_ref<void(instrprof_error)> Warn) {
506   for (auto I = ValueData.begin(), IE = ValueData.end(); I != IE; ++I) {
507     bool Overflowed;
508     I->Count = SaturatingMultiply(I->Count, Weight, &Overflowed);
509     if (Overflowed)
510       Warn(instrprof_error::counter_overflow);
511   }
512 }
513 
514 // Merge Value Profile data from Src record to this record for ValueKind.
515 // Scale merged value counts by \p Weight.
516 void InstrProfRecord::mergeValueProfData(
517     uint32_t ValueKind, InstrProfRecord &Src, uint64_t Weight,
518     function_ref<void(instrprof_error)> Warn) {
519   uint32_t ThisNumValueSites = getNumValueSites(ValueKind);
520   uint32_t OtherNumValueSites = Src.getNumValueSites(ValueKind);
521   if (ThisNumValueSites != OtherNumValueSites) {
522     Warn(instrprof_error::value_site_count_mismatch);
523     return;
524   }
525   if (!ThisNumValueSites)
526     return;
527   std::vector<InstrProfValueSiteRecord> &ThisSiteRecords =
528       getOrCreateValueSitesForKind(ValueKind);
529   MutableArrayRef<InstrProfValueSiteRecord> OtherSiteRecords =
530       Src.getValueSitesForKind(ValueKind);
531   for (uint32_t I = 0; I < ThisNumValueSites; I++)
532     ThisSiteRecords[I].merge(OtherSiteRecords[I], Weight, Warn);
533 }
534 
535 void InstrProfRecord::merge(InstrProfRecord &Other, uint64_t Weight,
536                             function_ref<void(instrprof_error)> Warn) {
537   // If the number of counters doesn't match we either have bad data
538   // or a hash collision.
539   if (Counts.size() != Other.Counts.size()) {
540     Warn(instrprof_error::count_mismatch);
541     return;
542   }
543 
544   for (size_t I = 0, E = Other.Counts.size(); I < E; ++I) {
545     bool Overflowed;
546     Counts[I] =
547         SaturatingMultiplyAdd(Other.Counts[I], Weight, Counts[I], &Overflowed);
548     if (Overflowed)
549       Warn(instrprof_error::counter_overflow);
550   }
551 
552   for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
553     mergeValueProfData(Kind, Other, Weight, Warn);
554 }
555 
556 void InstrProfRecord::scaleValueProfData(
557     uint32_t ValueKind, uint64_t Weight,
558     function_ref<void(instrprof_error)> Warn) {
559   for (auto &R : getValueSitesForKind(ValueKind))
560     R.scale(Weight, Warn);
561 }
562 
563 void InstrProfRecord::scale(uint64_t Weight,
564                             function_ref<void(instrprof_error)> Warn) {
565   for (auto &Count : this->Counts) {
566     bool Overflowed;
567     Count = SaturatingMultiply(Count, Weight, &Overflowed);
568     if (Overflowed)
569       Warn(instrprof_error::counter_overflow);
570   }
571   for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
572     scaleValueProfData(Kind, Weight, Warn);
573 }
574 
575 // Map indirect call target name hash to name string.
576 uint64_t InstrProfRecord::remapValue(uint64_t Value, uint32_t ValueKind,
577                                      InstrProfSymtab *SymTab) {
578   if (!SymTab)
579     return Value;
580 
581   if (ValueKind == IPVK_IndirectCallTarget)
582     return SymTab->getFunctionHashFromAddress(Value);
583 
584   return Value;
585 }
586 
587 void InstrProfRecord::addValueData(uint32_t ValueKind, uint32_t Site,
588                                    InstrProfValueData *VData, uint32_t N,
589                                    InstrProfSymtab *ValueMap) {
590   for (uint32_t I = 0; I < N; I++) {
591     VData[I].Value = remapValue(VData[I].Value, ValueKind, ValueMap);
592   }
593   std::vector<InstrProfValueSiteRecord> &ValueSites =
594       getOrCreateValueSitesForKind(ValueKind);
595   if (N == 0)
596     ValueSites.emplace_back();
597   else
598     ValueSites.emplace_back(VData, VData + N);
599 }
600 
601 #define INSTR_PROF_COMMON_API_IMPL
602 #include "llvm/ProfileData/InstrProfData.inc"
603 
604 /*!
605  * ValueProfRecordClosure Interface implementation for  InstrProfRecord
606  *  class. These C wrappers are used as adaptors so that C++ code can be
607  *  invoked as callbacks.
608  */
609 uint32_t getNumValueKindsInstrProf(const void *Record) {
610   return reinterpret_cast<const InstrProfRecord *>(Record)->getNumValueKinds();
611 }
612 
613 uint32_t getNumValueSitesInstrProf(const void *Record, uint32_t VKind) {
614   return reinterpret_cast<const InstrProfRecord *>(Record)
615       ->getNumValueSites(VKind);
616 }
617 
618 uint32_t getNumValueDataInstrProf(const void *Record, uint32_t VKind) {
619   return reinterpret_cast<const InstrProfRecord *>(Record)
620       ->getNumValueData(VKind);
621 }
622 
623 uint32_t getNumValueDataForSiteInstrProf(const void *R, uint32_t VK,
624                                          uint32_t S) {
625   return reinterpret_cast<const InstrProfRecord *>(R)
626       ->getNumValueDataForSite(VK, S);
627 }
628 
629 void getValueForSiteInstrProf(const void *R, InstrProfValueData *Dst,
630                               uint32_t K, uint32_t S) {
631   reinterpret_cast<const InstrProfRecord *>(R)->getValueForSite(Dst, K, S);
632 }
633 
634 ValueProfData *allocValueProfDataInstrProf(size_t TotalSizeInBytes) {
635   ValueProfData *VD =
636       (ValueProfData *)(new (::operator new(TotalSizeInBytes)) ValueProfData());
637   memset(VD, 0, TotalSizeInBytes);
638   return VD;
639 }
640 
641 static ValueProfRecordClosure InstrProfRecordClosure = {
642     nullptr,
643     getNumValueKindsInstrProf,
644     getNumValueSitesInstrProf,
645     getNumValueDataInstrProf,
646     getNumValueDataForSiteInstrProf,
647     nullptr,
648     getValueForSiteInstrProf,
649     allocValueProfDataInstrProf};
650 
651 // Wrapper implementation using the closure mechanism.
652 uint32_t ValueProfData::getSize(const InstrProfRecord &Record) {
653   auto Closure = InstrProfRecordClosure;
654   Closure.Record = &Record;
655   return getValueProfDataSize(&Closure);
656 }
657 
658 // Wrapper implementation using the closure mechanism.
659 std::unique_ptr<ValueProfData>
660 ValueProfData::serializeFrom(const InstrProfRecord &Record) {
661   InstrProfRecordClosure.Record = &Record;
662 
663   std::unique_ptr<ValueProfData> VPD(
664       serializeValueProfDataFrom(&InstrProfRecordClosure, nullptr));
665   return VPD;
666 }
667 
668 void ValueProfRecord::deserializeTo(InstrProfRecord &Record,
669                                     InstrProfSymtab *SymTab) {
670   Record.reserveSites(Kind, NumValueSites);
671 
672   InstrProfValueData *ValueData = getValueProfRecordValueData(this);
673   for (uint64_t VSite = 0; VSite < NumValueSites; ++VSite) {
674     uint8_t ValueDataCount = this->SiteCountArray[VSite];
675     Record.addValueData(Kind, VSite, ValueData, ValueDataCount, SymTab);
676     ValueData += ValueDataCount;
677   }
678 }
679 
680 // For writing/serializing,  Old is the host endianness, and  New is
681 // byte order intended on disk. For Reading/deserialization, Old
682 // is the on-disk source endianness, and New is the host endianness.
683 void ValueProfRecord::swapBytes(support::endianness Old,
684                                 support::endianness New) {
685   using namespace support;
686 
687   if (Old == New)
688     return;
689 
690   if (getHostEndianness() != Old) {
691     sys::swapByteOrder<uint32_t>(NumValueSites);
692     sys::swapByteOrder<uint32_t>(Kind);
693   }
694   uint32_t ND = getValueProfRecordNumValueData(this);
695   InstrProfValueData *VD = getValueProfRecordValueData(this);
696 
697   // No need to swap byte array: SiteCountArrray.
698   for (uint32_t I = 0; I < ND; I++) {
699     sys::swapByteOrder<uint64_t>(VD[I].Value);
700     sys::swapByteOrder<uint64_t>(VD[I].Count);
701   }
702   if (getHostEndianness() == Old) {
703     sys::swapByteOrder<uint32_t>(NumValueSites);
704     sys::swapByteOrder<uint32_t>(Kind);
705   }
706 }
707 
708 void ValueProfData::deserializeTo(InstrProfRecord &Record,
709                                   InstrProfSymtab *SymTab) {
710   if (NumValueKinds == 0)
711     return;
712 
713   ValueProfRecord *VR = getFirstValueProfRecord(this);
714   for (uint32_t K = 0; K < NumValueKinds; K++) {
715     VR->deserializeTo(Record, SymTab);
716     VR = getValueProfRecordNext(VR);
717   }
718 }
719 
720 template <class T>
721 static T swapToHostOrder(const unsigned char *&D, support::endianness Orig) {
722   using namespace support;
723 
724   if (Orig == little)
725     return endian::readNext<T, little, unaligned>(D);
726   else
727     return endian::readNext<T, big, unaligned>(D);
728 }
729 
730 static std::unique_ptr<ValueProfData> allocValueProfData(uint32_t TotalSize) {
731   return std::unique_ptr<ValueProfData>(new (::operator new(TotalSize))
732                                             ValueProfData());
733 }
734 
735 Error ValueProfData::checkIntegrity() {
736   if (NumValueKinds > IPVK_Last + 1)
737     return make_error<InstrProfError>(instrprof_error::malformed);
738   // Total size needs to be mulltiple of quadword size.
739   if (TotalSize % sizeof(uint64_t))
740     return make_error<InstrProfError>(instrprof_error::malformed);
741 
742   ValueProfRecord *VR = getFirstValueProfRecord(this);
743   for (uint32_t K = 0; K < this->NumValueKinds; K++) {
744     if (VR->Kind > IPVK_Last)
745       return make_error<InstrProfError>(instrprof_error::malformed);
746     VR = getValueProfRecordNext(VR);
747     if ((char *)VR - (char *)this > (ptrdiff_t)TotalSize)
748       return make_error<InstrProfError>(instrprof_error::malformed);
749   }
750   return Error::success();
751 }
752 
753 Expected<std::unique_ptr<ValueProfData>>
754 ValueProfData::getValueProfData(const unsigned char *D,
755                                 const unsigned char *const BufferEnd,
756                                 support::endianness Endianness) {
757   using namespace support;
758 
759   if (D + sizeof(ValueProfData) > BufferEnd)
760     return make_error<InstrProfError>(instrprof_error::truncated);
761 
762   const unsigned char *Header = D;
763   uint32_t TotalSize = swapToHostOrder<uint32_t>(Header, Endianness);
764   if (D + TotalSize > BufferEnd)
765     return make_error<InstrProfError>(instrprof_error::too_large);
766 
767   std::unique_ptr<ValueProfData> VPD = allocValueProfData(TotalSize);
768   memcpy(VPD.get(), D, TotalSize);
769   // Byte swap.
770   VPD->swapBytesToHost(Endianness);
771 
772   Error E = VPD->checkIntegrity();
773   if (E)
774     return std::move(E);
775 
776   return std::move(VPD);
777 }
778 
779 void ValueProfData::swapBytesToHost(support::endianness Endianness) {
780   using namespace support;
781 
782   if (Endianness == getHostEndianness())
783     return;
784 
785   sys::swapByteOrder<uint32_t>(TotalSize);
786   sys::swapByteOrder<uint32_t>(NumValueKinds);
787 
788   ValueProfRecord *VR = getFirstValueProfRecord(this);
789   for (uint32_t K = 0; K < NumValueKinds; K++) {
790     VR->swapBytes(Endianness, getHostEndianness());
791     VR = getValueProfRecordNext(VR);
792   }
793 }
794 
795 void ValueProfData::swapBytesFromHost(support::endianness Endianness) {
796   using namespace support;
797 
798   if (Endianness == getHostEndianness())
799     return;
800 
801   ValueProfRecord *VR = getFirstValueProfRecord(this);
802   for (uint32_t K = 0; K < NumValueKinds; K++) {
803     ValueProfRecord *NVR = getValueProfRecordNext(VR);
804     VR->swapBytes(getHostEndianness(), Endianness);
805     VR = NVR;
806   }
807   sys::swapByteOrder<uint32_t>(TotalSize);
808   sys::swapByteOrder<uint32_t>(NumValueKinds);
809 }
810 
811 void annotateValueSite(Module &M, Instruction &Inst,
812                        const InstrProfRecord &InstrProfR,
813                        InstrProfValueKind ValueKind, uint32_t SiteIdx,
814                        uint32_t MaxMDCount) {
815   uint32_t NV = InstrProfR.getNumValueDataForSite(ValueKind, SiteIdx);
816   if (!NV)
817     return;
818 
819   uint64_t Sum = 0;
820   std::unique_ptr<InstrProfValueData[]> VD =
821       InstrProfR.getValueForSite(ValueKind, SiteIdx, &Sum);
822 
823   ArrayRef<InstrProfValueData> VDs(VD.get(), NV);
824   annotateValueSite(M, Inst, VDs, Sum, ValueKind, MaxMDCount);
825 }
826 
827 void annotateValueSite(Module &M, Instruction &Inst,
828                        ArrayRef<InstrProfValueData> VDs,
829                        uint64_t Sum, InstrProfValueKind ValueKind,
830                        uint32_t MaxMDCount) {
831   LLVMContext &Ctx = M.getContext();
832   MDBuilder MDHelper(Ctx);
833   SmallVector<Metadata *, 3> Vals;
834   // Tag
835   Vals.push_back(MDHelper.createString("VP"));
836   // Value Kind
837   Vals.push_back(MDHelper.createConstant(
838       ConstantInt::get(Type::getInt32Ty(Ctx), ValueKind)));
839   // Total Count
840   Vals.push_back(
841       MDHelper.createConstant(ConstantInt::get(Type::getInt64Ty(Ctx), Sum)));
842 
843   // Value Profile Data
844   uint32_t MDCount = MaxMDCount;
845   for (auto &VD : VDs) {
846     Vals.push_back(MDHelper.createConstant(
847         ConstantInt::get(Type::getInt64Ty(Ctx), VD.Value)));
848     Vals.push_back(MDHelper.createConstant(
849         ConstantInt::get(Type::getInt64Ty(Ctx), VD.Count)));
850     if (--MDCount == 0)
851       break;
852   }
853   Inst.setMetadata(LLVMContext::MD_prof, MDNode::get(Ctx, Vals));
854 }
855 
856 bool getValueProfDataFromInst(const Instruction &Inst,
857                               InstrProfValueKind ValueKind,
858                               uint32_t MaxNumValueData,
859                               InstrProfValueData ValueData[],
860                               uint32_t &ActualNumValueData, uint64_t &TotalC) {
861   MDNode *MD = Inst.getMetadata(LLVMContext::MD_prof);
862   if (!MD)
863     return false;
864 
865   unsigned NOps = MD->getNumOperands();
866 
867   if (NOps < 5)
868     return false;
869 
870   // Operand 0 is a string tag "VP":
871   MDString *Tag = cast<MDString>(MD->getOperand(0));
872   if (!Tag)
873     return false;
874 
875   if (!Tag->getString().equals("VP"))
876     return false;
877 
878   // Now check kind:
879   ConstantInt *KindInt = mdconst::dyn_extract<ConstantInt>(MD->getOperand(1));
880   if (!KindInt)
881     return false;
882   if (KindInt->getZExtValue() != ValueKind)
883     return false;
884 
885   // Get total count
886   ConstantInt *TotalCInt = mdconst::dyn_extract<ConstantInt>(MD->getOperand(2));
887   if (!TotalCInt)
888     return false;
889   TotalC = TotalCInt->getZExtValue();
890 
891   ActualNumValueData = 0;
892 
893   for (unsigned I = 3; I < NOps; I += 2) {
894     if (ActualNumValueData >= MaxNumValueData)
895       break;
896     ConstantInt *Value = mdconst::dyn_extract<ConstantInt>(MD->getOperand(I));
897     ConstantInt *Count =
898         mdconst::dyn_extract<ConstantInt>(MD->getOperand(I + 1));
899     if (!Value || !Count)
900       return false;
901     ValueData[ActualNumValueData].Value = Value->getZExtValue();
902     ValueData[ActualNumValueData].Count = Count->getZExtValue();
903     ActualNumValueData++;
904   }
905   return true;
906 }
907 
908 MDNode *getPGOFuncNameMetadata(const Function &F) {
909   return F.getMetadata(getPGOFuncNameMetadataName());
910 }
911 
912 void createPGOFuncNameMetadata(Function &F, StringRef PGOFuncName) {
913   // Only for internal linkage functions.
914   if (PGOFuncName == F.getName())
915       return;
916   // Don't create duplicated meta-data.
917   if (getPGOFuncNameMetadata(F))
918     return;
919   LLVMContext &C = F.getContext();
920   MDNode *N = MDNode::get(C, MDString::get(C, PGOFuncName));
921   F.setMetadata(getPGOFuncNameMetadataName(), N);
922 }
923 
924 bool needsComdatForCounter(const Function &F, const Module &M) {
925   if (F.hasComdat())
926     return true;
927 
928   if (!Triple(M.getTargetTriple()).supportsCOMDAT())
929     return false;
930 
931   // See createPGOFuncNameVar for more details. To avoid link errors, profile
932   // counters for function with available_externally linkage needs to be changed
933   // to linkonce linkage. On ELF based systems, this leads to weak symbols to be
934   // created. Without using comdat, duplicate entries won't be removed by the
935   // linker leading to increased data segement size and raw profile size. Even
936   // worse, since the referenced counter from profile per-function data object
937   // will be resolved to the common strong definition, the profile counts for
938   // available_externally functions will end up being duplicated in raw profile
939   // data. This can result in distorted profile as the counts of those dups
940   // will be accumulated by the profile merger.
941   GlobalValue::LinkageTypes Linkage = F.getLinkage();
942   if (Linkage != GlobalValue::ExternalWeakLinkage &&
943       Linkage != GlobalValue::AvailableExternallyLinkage)
944     return false;
945 
946   return true;
947 }
948 
949 // Check if INSTR_PROF_RAW_VERSION_VAR is defined.
950 bool isIRPGOFlagSet(const Module *M) {
951   auto IRInstrVar =
952       M->getNamedGlobal(INSTR_PROF_QUOTE(INSTR_PROF_RAW_VERSION_VAR));
953   if (!IRInstrVar || IRInstrVar->isDeclaration() ||
954       IRInstrVar->hasLocalLinkage())
955     return false;
956 
957   // Check if the flag is set.
958   if (!IRInstrVar->hasInitializer())
959     return false;
960 
961   const Constant *InitVal = IRInstrVar->getInitializer();
962   if (!InitVal)
963     return false;
964 
965   return (dyn_cast<ConstantInt>(InitVal)->getZExtValue() &
966           VARIANT_MASK_IR_PROF) != 0;
967 }
968 
969 // Check if we can safely rename this Comdat function.
970 bool canRenameComdatFunc(const Function &F, bool CheckAddressTaken) {
971   if (F.getName().empty())
972     return false;
973   if (!needsComdatForCounter(F, *(F.getParent())))
974     return false;
975   // Unsafe to rename the address-taken function (which can be used in
976   // function comparison).
977   if (CheckAddressTaken && F.hasAddressTaken())
978     return false;
979   // Only safe to do if this function may be discarded if it is not used
980   // in the compilation unit.
981   if (!GlobalValue::isDiscardableIfUnused(F.getLinkage()))
982     return false;
983 
984   // For AvailableExternallyLinkage functions.
985   if (!F.hasComdat()) {
986     assert(F.getLinkage() == GlobalValue::AvailableExternallyLinkage);
987     return true;
988   }
989   return true;
990 }
991 
992 // Parse the value profile options.
993 void getMemOPSizeRangeFromOption(StringRef MemOPSizeRange, int64_t &RangeStart,
994                                  int64_t &RangeLast) {
995   static const int64_t DefaultMemOPSizeRangeStart = 0;
996   static const int64_t DefaultMemOPSizeRangeLast = 8;
997   RangeStart = DefaultMemOPSizeRangeStart;
998   RangeLast = DefaultMemOPSizeRangeLast;
999 
1000   if (!MemOPSizeRange.empty()) {
1001     auto Pos = MemOPSizeRange.find(':');
1002     if (Pos != std::string::npos) {
1003       if (Pos > 0)
1004         MemOPSizeRange.substr(0, Pos).getAsInteger(10, RangeStart);
1005       if (Pos < MemOPSizeRange.size() - 1)
1006         MemOPSizeRange.substr(Pos + 1).getAsInteger(10, RangeLast);
1007     } else
1008       MemOPSizeRange.getAsInteger(10, RangeLast);
1009   }
1010   assert(RangeLast >= RangeStart);
1011 }
1012 
1013 // Create a COMDAT variable INSTR_PROF_RAW_VERSION_VAR to make the runtime
1014 // aware this is an ir_level profile so it can set the version flag.
1015 void createIRLevelProfileFlagVar(Module &M, bool IsCS) {
1016   const StringRef VarName(INSTR_PROF_QUOTE(INSTR_PROF_RAW_VERSION_VAR));
1017   Type *IntTy64 = Type::getInt64Ty(M.getContext());
1018   uint64_t ProfileVersion = (INSTR_PROF_RAW_VERSION | VARIANT_MASK_IR_PROF);
1019   if (IsCS)
1020     ProfileVersion |= VARIANT_MASK_CSIR_PROF;
1021   auto IRLevelVersionVariable = new GlobalVariable(
1022       M, IntTy64, true, GlobalValue::WeakAnyLinkage,
1023       Constant::getIntegerValue(IntTy64, APInt(64, ProfileVersion)), VarName);
1024   IRLevelVersionVariable->setVisibility(GlobalValue::DefaultVisibility);
1025   Triple TT(M.getTargetTriple());
1026   if (TT.supportsCOMDAT()) {
1027     IRLevelVersionVariable->setLinkage(GlobalValue::ExternalLinkage);
1028     IRLevelVersionVariable->setComdat(M.getOrInsertComdat(VarName));
1029   }
1030 }
1031 
1032 // Create the variable for the profile file name.
1033 void createProfileFileNameVar(Module &M, StringRef InstrProfileOutput) {
1034   if (InstrProfileOutput.empty())
1035     return;
1036   Constant *ProfileNameConst =
1037       ConstantDataArray::getString(M.getContext(), InstrProfileOutput, true);
1038   GlobalVariable *ProfileNameVar = new GlobalVariable(
1039       M, ProfileNameConst->getType(), true, GlobalValue::WeakAnyLinkage,
1040       ProfileNameConst, INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_NAME_VAR));
1041   Triple TT(M.getTargetTriple());
1042   if (TT.supportsCOMDAT()) {
1043     ProfileNameVar->setLinkage(GlobalValue::ExternalLinkage);
1044     ProfileNameVar->setComdat(M.getOrInsertComdat(
1045         StringRef(INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_NAME_VAR))));
1046   }
1047 }
1048 
1049 } // end namespace llvm
1050