xref: /freebsd-src/contrib/llvm-project/llvm/lib/ProfileData/InstrProf.cpp (revision 5e801ac66d24704442eba426ed13c3effb8a34e7)
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/Config/config.h"
22 #include "llvm/IR/Constant.h"
23 #include "llvm/IR/Constants.h"
24 #include "llvm/IR/Function.h"
25 #include "llvm/IR/GlobalValue.h"
26 #include "llvm/IR/GlobalVariable.h"
27 #include "llvm/IR/Instruction.h"
28 #include "llvm/IR/LLVMContext.h"
29 #include "llvm/IR/MDBuilder.h"
30 #include "llvm/IR/Metadata.h"
31 #include "llvm/IR/Module.h"
32 #include "llvm/IR/Type.h"
33 #include "llvm/ProfileData/InstrProfReader.h"
34 #include "llvm/Support/Casting.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Support/Compiler.h"
37 #include "llvm/Support/Compression.h"
38 #include "llvm/Support/Endian.h"
39 #include "llvm/Support/Error.h"
40 #include "llvm/Support/ErrorHandling.h"
41 #include "llvm/Support/LEB128.h"
42 #include "llvm/Support/ManagedStatic.h"
43 #include "llvm/Support/MathExtras.h"
44 #include "llvm/Support/Path.h"
45 #include "llvm/Support/SwapByteOrder.h"
46 #include <algorithm>
47 #include <cassert>
48 #include <cstddef>
49 #include <cstdint>
50 #include <cstring>
51 #include <memory>
52 #include <string>
53 #include <system_error>
54 #include <utility>
55 #include <vector>
56 
57 using namespace llvm;
58 
59 static cl::opt<bool> StaticFuncFullModulePrefix(
60     "static-func-full-module-prefix", cl::init(true), cl::Hidden,
61     cl::desc("Use full module build paths in the profile counter names for "
62              "static functions."));
63 
64 // This option is tailored to users that have different top-level directory in
65 // profile-gen and profile-use compilation. Users need to specific the number
66 // of levels to strip. A value larger than the number of directories in the
67 // source file will strip all the directory names and only leave the basename.
68 //
69 // Note current ThinLTO module importing for the indirect-calls assumes
70 // the source directory name not being stripped. A non-zero option value here
71 // can potentially prevent some inter-module indirect-call-promotions.
72 static cl::opt<unsigned> StaticFuncStripDirNamePrefix(
73     "static-func-strip-dirname-prefix", cl::init(0), cl::Hidden,
74     cl::desc("Strip specified level of directory name from source path in "
75              "the profile counter name for static functions."));
76 
77 static std::string getInstrProfErrString(instrprof_error Err,
78                                          const std::string &ErrMsg = "") {
79   std::string Msg;
80   raw_string_ostream OS(Msg);
81 
82   switch (Err) {
83   case instrprof_error::success:
84     OS << "success";
85     break;
86   case instrprof_error::eof:
87     OS << "end of File";
88     break;
89   case instrprof_error::unrecognized_format:
90     OS << "unrecognized instrumentation profile encoding format";
91     break;
92   case instrprof_error::bad_magic:
93     OS << "invalid instrumentation profile data (bad magic)";
94     break;
95   case instrprof_error::bad_header:
96     OS << "invalid instrumentation profile data (file header is corrupt)";
97     break;
98   case instrprof_error::unsupported_version:
99     OS << "unsupported instrumentation profile format version";
100     break;
101   case instrprof_error::unsupported_hash_type:
102     OS << "unsupported instrumentation profile hash type";
103     break;
104   case instrprof_error::too_large:
105     OS << "too much profile data";
106     break;
107   case instrprof_error::truncated:
108     OS << "truncated profile data";
109     break;
110   case instrprof_error::malformed:
111     OS << "malformed instrumentation profile data";
112     break;
113   case instrprof_error::invalid_prof:
114     OS << "invalid profile created. Please file a bug "
115           "at: " BUG_REPORT_URL
116           " and include the profraw files that caused this error.";
117     break;
118   case instrprof_error::unknown_function:
119     OS << "no profile data available for function";
120     break;
121   case instrprof_error::hash_mismatch:
122     OS << "function control flow change detected (hash mismatch)";
123     break;
124   case instrprof_error::count_mismatch:
125     OS << "function basic block count change detected (counter mismatch)";
126     break;
127   case instrprof_error::counter_overflow:
128     OS << "counter overflow";
129     break;
130   case instrprof_error::value_site_count_mismatch:
131     OS << "function value site count change detected (counter mismatch)";
132     break;
133   case instrprof_error::compress_failed:
134     OS << "failed to compress data (zlib)";
135     break;
136   case instrprof_error::uncompress_failed:
137     OS << "failed to uncompress data (zlib)";
138     break;
139   case instrprof_error::empty_raw_profile:
140     OS << "empty raw profile file";
141     break;
142   case instrprof_error::zlib_unavailable:
143     OS << "profile uses zlib compression but the profile reader was built "
144           "without zlib support";
145     break;
146   }
147 
148   // If optional error message is not empty, append it to the message.
149   if (!ErrMsg.empty())
150     OS << ": " << ErrMsg;
151 
152   return OS.str();
153 }
154 
155 namespace {
156 
157 // FIXME: This class is only here to support the transition to llvm::Error. It
158 // will be removed once this transition is complete. Clients should prefer to
159 // deal with the Error value directly, rather than converting to error_code.
160 class InstrProfErrorCategoryType : public std::error_category {
161   const char *name() const noexcept override { return "llvm.instrprof"; }
162 
163   std::string message(int IE) const override {
164     return getInstrProfErrString(static_cast<instrprof_error>(IE));
165   }
166 };
167 
168 } // end anonymous namespace
169 
170 static ManagedStatic<InstrProfErrorCategoryType> ErrorCategory;
171 
172 const std::error_category &llvm::instrprof_category() {
173   return *ErrorCategory;
174 }
175 
176 namespace {
177 
178 const char *InstrProfSectNameCommon[] = {
179 #define INSTR_PROF_SECT_ENTRY(Kind, SectNameCommon, SectNameCoff, Prefix)      \
180   SectNameCommon,
181 #include "llvm/ProfileData/InstrProfData.inc"
182 };
183 
184 const char *InstrProfSectNameCoff[] = {
185 #define INSTR_PROF_SECT_ENTRY(Kind, SectNameCommon, SectNameCoff, Prefix)      \
186   SectNameCoff,
187 #include "llvm/ProfileData/InstrProfData.inc"
188 };
189 
190 const char *InstrProfSectNamePrefix[] = {
191 #define INSTR_PROF_SECT_ENTRY(Kind, SectNameCommon, SectNameCoff, Prefix)      \
192   Prefix,
193 #include "llvm/ProfileData/InstrProfData.inc"
194 };
195 
196 } // namespace
197 
198 namespace llvm {
199 
200 cl::opt<bool> DoInstrProfNameCompression(
201     "enable-name-compression",
202     cl::desc("Enable name/filename string compression"), cl::init(true));
203 
204 std::string getInstrProfSectionName(InstrProfSectKind IPSK,
205                                     Triple::ObjectFormatType OF,
206                                     bool AddSegmentInfo) {
207   std::string SectName;
208 
209   if (OF == Triple::MachO && AddSegmentInfo)
210     SectName = InstrProfSectNamePrefix[IPSK];
211 
212   if (OF == Triple::COFF)
213     SectName += InstrProfSectNameCoff[IPSK];
214   else
215     SectName += InstrProfSectNameCommon[IPSK];
216 
217   if (OF == Triple::MachO && IPSK == IPSK_data && AddSegmentInfo)
218     SectName += ",regular,live_support";
219 
220   return SectName;
221 }
222 
223 void SoftInstrProfErrors::addError(instrprof_error IE) {
224   if (IE == instrprof_error::success)
225     return;
226 
227   if (FirstError == instrprof_error::success)
228     FirstError = IE;
229 
230   switch (IE) {
231   case instrprof_error::hash_mismatch:
232     ++NumHashMismatches;
233     break;
234   case instrprof_error::count_mismatch:
235     ++NumCountMismatches;
236     break;
237   case instrprof_error::counter_overflow:
238     ++NumCounterOverflows;
239     break;
240   case instrprof_error::value_site_count_mismatch:
241     ++NumValueSiteCountMismatches;
242     break;
243   default:
244     llvm_unreachable("Not a soft error");
245   }
246 }
247 
248 std::string InstrProfError::message() const {
249   return getInstrProfErrString(Err, Msg);
250 }
251 
252 char InstrProfError::ID = 0;
253 
254 std::string getPGOFuncName(StringRef RawFuncName,
255                            GlobalValue::LinkageTypes Linkage,
256                            StringRef FileName,
257                            uint64_t Version LLVM_ATTRIBUTE_UNUSED) {
258   return GlobalValue::getGlobalIdentifier(RawFuncName, Linkage, FileName);
259 }
260 
261 // Strip NumPrefix level of directory name from PathNameStr. If the number of
262 // directory separators is less than NumPrefix, strip all the directories and
263 // leave base file name only.
264 static StringRef stripDirPrefix(StringRef PathNameStr, uint32_t NumPrefix) {
265   uint32_t Count = NumPrefix;
266   uint32_t Pos = 0, LastPos = 0;
267   for (auto & CI : PathNameStr) {
268     ++Pos;
269     if (llvm::sys::path::is_separator(CI)) {
270       LastPos = Pos;
271       --Count;
272     }
273     if (Count == 0)
274       break;
275   }
276   return PathNameStr.substr(LastPos);
277 }
278 
279 // Return the PGOFuncName. This function has some special handling when called
280 // in LTO optimization. The following only applies when calling in LTO passes
281 // (when \c InLTO is true): LTO's internalization privatizes many global linkage
282 // symbols. This happens after value profile annotation, but those internal
283 // linkage functions should not have a source prefix.
284 // Additionally, for ThinLTO mode, exported internal functions are promoted
285 // and renamed. We need to ensure that the original internal PGO name is
286 // used when computing the GUID that is compared against the profiled GUIDs.
287 // To differentiate compiler generated internal symbols from original ones,
288 // PGOFuncName meta data are created and attached to the original internal
289 // symbols in the value profile annotation step
290 // (PGOUseFunc::annotateIndirectCallSites). If a symbol does not have the meta
291 // data, its original linkage must be non-internal.
292 std::string getPGOFuncName(const Function &F, bool InLTO, uint64_t Version) {
293   if (!InLTO) {
294     StringRef FileName(F.getParent()->getSourceFileName());
295     uint32_t StripLevel = StaticFuncFullModulePrefix ? 0 : (uint32_t)-1;
296     if (StripLevel < StaticFuncStripDirNamePrefix)
297       StripLevel = StaticFuncStripDirNamePrefix;
298     if (StripLevel)
299       FileName = stripDirPrefix(FileName, StripLevel);
300     return getPGOFuncName(F.getName(), F.getLinkage(), FileName, Version);
301   }
302 
303   // In LTO mode (when InLTO is true), first check if there is a meta data.
304   if (MDNode *MD = getPGOFuncNameMetadata(F)) {
305     StringRef S = cast<MDString>(MD->getOperand(0))->getString();
306     return S.str();
307   }
308 
309   // If there is no meta data, the function must be a global before the value
310   // profile annotation pass. Its current linkage may be internal if it is
311   // internalized in LTO mode.
312   return getPGOFuncName(F.getName(), GlobalValue::ExternalLinkage, "");
313 }
314 
315 StringRef getFuncNameWithoutPrefix(StringRef PGOFuncName, StringRef FileName) {
316   if (FileName.empty())
317     return PGOFuncName;
318   // Drop the file name including ':'. See also getPGOFuncName.
319   if (PGOFuncName.startswith(FileName))
320     PGOFuncName = PGOFuncName.drop_front(FileName.size() + 1);
321   return PGOFuncName;
322 }
323 
324 // \p FuncName is the string used as profile lookup key for the function. A
325 // symbol is created to hold the name. Return the legalized symbol name.
326 std::string getPGOFuncNameVarName(StringRef FuncName,
327                                   GlobalValue::LinkageTypes Linkage) {
328   std::string VarName = std::string(getInstrProfNameVarPrefix());
329   VarName += FuncName;
330 
331   if (!GlobalValue::isLocalLinkage(Linkage))
332     return VarName;
333 
334   // Now fix up illegal chars in local VarName that may upset the assembler.
335   const char *InvalidChars = "-:<>/\"'";
336   size_t found = VarName.find_first_of(InvalidChars);
337   while (found != std::string::npos) {
338     VarName[found] = '_';
339     found = VarName.find_first_of(InvalidChars, found + 1);
340   }
341   return VarName;
342 }
343 
344 GlobalVariable *createPGOFuncNameVar(Module &M,
345                                      GlobalValue::LinkageTypes Linkage,
346                                      StringRef PGOFuncName) {
347   // We generally want to match the function's linkage, but available_externally
348   // and extern_weak both have the wrong semantics, and anything that doesn't
349   // need to link across compilation units doesn't need to be visible at all.
350   if (Linkage == GlobalValue::ExternalWeakLinkage)
351     Linkage = GlobalValue::LinkOnceAnyLinkage;
352   else if (Linkage == GlobalValue::AvailableExternallyLinkage)
353     Linkage = GlobalValue::LinkOnceODRLinkage;
354   else if (Linkage == GlobalValue::InternalLinkage ||
355            Linkage == GlobalValue::ExternalLinkage)
356     Linkage = GlobalValue::PrivateLinkage;
357 
358   auto *Value =
359       ConstantDataArray::getString(M.getContext(), PGOFuncName, false);
360   auto FuncNameVar =
361       new GlobalVariable(M, Value->getType(), true, Linkage, Value,
362                          getPGOFuncNameVarName(PGOFuncName, Linkage));
363 
364   // Hide the symbol so that we correctly get a copy for each executable.
365   if (!GlobalValue::isLocalLinkage(FuncNameVar->getLinkage()))
366     FuncNameVar->setVisibility(GlobalValue::HiddenVisibility);
367 
368   return FuncNameVar;
369 }
370 
371 GlobalVariable *createPGOFuncNameVar(Function &F, StringRef PGOFuncName) {
372   return createPGOFuncNameVar(*F.getParent(), F.getLinkage(), PGOFuncName);
373 }
374 
375 Error InstrProfSymtab::create(Module &M, bool InLTO) {
376   for (Function &F : M) {
377     // Function may not have a name: like using asm("") to overwrite the name.
378     // Ignore in this case.
379     if (!F.hasName())
380       continue;
381     const std::string &PGOFuncName = getPGOFuncName(F, InLTO);
382     if (Error E = addFuncName(PGOFuncName))
383       return E;
384     MD5FuncMap.emplace_back(Function::getGUID(PGOFuncName), &F);
385     // In ThinLTO, local function may have been promoted to global and have
386     // suffix ".llvm." added to the function name. We need to add the
387     // stripped function name to the symbol table so that we can find a match
388     // from profile.
389     //
390     // We may have other suffixes similar as ".llvm." which are needed to
391     // be stripped before the matching, but ".__uniq." suffix which is used
392     // to differentiate internal linkage functions in different modules
393     // should be kept. Now this is the only suffix with the pattern ".xxx"
394     // which is kept before matching.
395     const std::string UniqSuffix = ".__uniq.";
396     auto pos = PGOFuncName.find(UniqSuffix);
397     // Search '.' after ".__uniq." if ".__uniq." exists, otherwise
398     // search '.' from the beginning.
399     if (pos != std::string::npos)
400       pos += UniqSuffix.length();
401     else
402       pos = 0;
403     pos = PGOFuncName.find('.', pos);
404     if (pos != std::string::npos && pos != 0) {
405       const std::string &OtherFuncName = PGOFuncName.substr(0, pos);
406       if (Error E = addFuncName(OtherFuncName))
407         return E;
408       MD5FuncMap.emplace_back(Function::getGUID(OtherFuncName), &F);
409     }
410   }
411   Sorted = false;
412   finalizeSymtab();
413   return Error::success();
414 }
415 
416 uint64_t InstrProfSymtab::getFunctionHashFromAddress(uint64_t Address) {
417   finalizeSymtab();
418   auto It = partition_point(AddrToMD5Map, [=](std::pair<uint64_t, uint64_t> A) {
419     return A.first < Address;
420   });
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 (It != AddrToMD5Map.end() && It->first == Address)
426     return (uint64_t)It->second;
427   return 0;
428 }
429 
430 Error collectPGOFuncNameStrings(ArrayRef<std::string> NameStrs,
431                                 bool doCompression, std::string &Result) {
432   assert(!NameStrs.empty() && "No name data to emit");
433 
434   uint8_t Header[16], *P = Header;
435   std::string UncompressedNameStrings =
436       join(NameStrs.begin(), NameStrs.end(), getInstrProfNameSeparator());
437 
438   assert(StringRef(UncompressedNameStrings)
439                  .count(getInstrProfNameSeparator()) == (NameStrs.size() - 1) &&
440          "PGO name is invalid (contains separator token)");
441 
442   unsigned EncLen = encodeULEB128(UncompressedNameStrings.length(), P);
443   P += EncLen;
444 
445   auto WriteStringToResult = [&](size_t CompressedLen, StringRef InputStr) {
446     EncLen = encodeULEB128(CompressedLen, P);
447     P += EncLen;
448     char *HeaderStr = reinterpret_cast<char *>(&Header[0]);
449     unsigned HeaderLen = P - &Header[0];
450     Result.append(HeaderStr, HeaderLen);
451     Result += InputStr;
452     return Error::success();
453   };
454 
455   if (!doCompression) {
456     return WriteStringToResult(0, UncompressedNameStrings);
457   }
458 
459   SmallString<128> CompressedNameStrings;
460   Error E = zlib::compress(StringRef(UncompressedNameStrings),
461                            CompressedNameStrings, zlib::BestSizeCompression);
462   if (E) {
463     consumeError(std::move(E));
464     return make_error<InstrProfError>(instrprof_error::compress_failed);
465   }
466 
467   return WriteStringToResult(CompressedNameStrings.size(),
468                              CompressedNameStrings);
469 }
470 
471 StringRef getPGOFuncNameVarInitializer(GlobalVariable *NameVar) {
472   auto *Arr = cast<ConstantDataArray>(NameVar->getInitializer());
473   StringRef NameStr =
474       Arr->isCString() ? Arr->getAsCString() : Arr->getAsString();
475   return NameStr;
476 }
477 
478 Error collectPGOFuncNameStrings(ArrayRef<GlobalVariable *> NameVars,
479                                 std::string &Result, bool doCompression) {
480   std::vector<std::string> NameStrs;
481   for (auto *NameVar : NameVars) {
482     NameStrs.push_back(std::string(getPGOFuncNameVarInitializer(NameVar)));
483   }
484   return collectPGOFuncNameStrings(
485       NameStrs, zlib::isAvailable() && doCompression, Result);
486 }
487 
488 Error readPGOFuncNameStrings(StringRef NameStrings, InstrProfSymtab &Symtab) {
489   const uint8_t *P = NameStrings.bytes_begin();
490   const uint8_t *EndP = NameStrings.bytes_end();
491   while (P < EndP) {
492     uint32_t N;
493     uint64_t UncompressedSize = decodeULEB128(P, &N);
494     P += N;
495     uint64_t CompressedSize = decodeULEB128(P, &N);
496     P += N;
497     bool isCompressed = (CompressedSize != 0);
498     SmallString<128> UncompressedNameStrings;
499     StringRef NameStrings;
500     if (isCompressed) {
501       if (!llvm::zlib::isAvailable())
502         return make_error<InstrProfError>(instrprof_error::zlib_unavailable);
503 
504       StringRef CompressedNameStrings(reinterpret_cast<const char *>(P),
505                                       CompressedSize);
506       if (Error E =
507               zlib::uncompress(CompressedNameStrings, UncompressedNameStrings,
508                                UncompressedSize)) {
509         consumeError(std::move(E));
510         return make_error<InstrProfError>(instrprof_error::uncompress_failed);
511       }
512       P += CompressedSize;
513       NameStrings = StringRef(UncompressedNameStrings.data(),
514                               UncompressedNameStrings.size());
515     } else {
516       NameStrings =
517           StringRef(reinterpret_cast<const char *>(P), UncompressedSize);
518       P += UncompressedSize;
519     }
520     // Now parse the name strings.
521     SmallVector<StringRef, 0> Names;
522     NameStrings.split(Names, getInstrProfNameSeparator());
523     for (StringRef &Name : Names)
524       if (Error E = Symtab.addFuncName(Name))
525         return E;
526 
527     while (P < EndP && *P == 0)
528       P++;
529   }
530   return Error::success();
531 }
532 
533 void InstrProfRecord::accumulateCounts(CountSumOrPercent &Sum) const {
534   uint64_t FuncSum = 0;
535   Sum.NumEntries += Counts.size();
536   for (size_t F = 0, E = Counts.size(); F < E; ++F)
537     FuncSum += Counts[F];
538   Sum.CountSum += FuncSum;
539 
540   for (uint32_t VK = IPVK_First; VK <= IPVK_Last; ++VK) {
541     uint64_t KindSum = 0;
542     uint32_t NumValueSites = getNumValueSites(VK);
543     for (size_t I = 0; I < NumValueSites; ++I) {
544       uint32_t NV = getNumValueDataForSite(VK, I);
545       std::unique_ptr<InstrProfValueData[]> VD = getValueForSite(VK, I);
546       for (uint32_t V = 0; V < NV; V++)
547         KindSum += VD[V].Count;
548     }
549     Sum.ValueCounts[VK] += KindSum;
550   }
551 }
552 
553 void InstrProfValueSiteRecord::overlap(InstrProfValueSiteRecord &Input,
554                                        uint32_t ValueKind,
555                                        OverlapStats &Overlap,
556                                        OverlapStats &FuncLevelOverlap) {
557   this->sortByTargetValues();
558   Input.sortByTargetValues();
559   double Score = 0.0f, FuncLevelScore = 0.0f;
560   auto I = ValueData.begin();
561   auto IE = ValueData.end();
562   auto J = Input.ValueData.begin();
563   auto JE = Input.ValueData.end();
564   while (I != IE && J != JE) {
565     if (I->Value == J->Value) {
566       Score += OverlapStats::score(I->Count, J->Count,
567                                    Overlap.Base.ValueCounts[ValueKind],
568                                    Overlap.Test.ValueCounts[ValueKind]);
569       FuncLevelScore += OverlapStats::score(
570           I->Count, J->Count, FuncLevelOverlap.Base.ValueCounts[ValueKind],
571           FuncLevelOverlap.Test.ValueCounts[ValueKind]);
572       ++I;
573     } else if (I->Value < J->Value) {
574       ++I;
575       continue;
576     }
577     ++J;
578   }
579   Overlap.Overlap.ValueCounts[ValueKind] += Score;
580   FuncLevelOverlap.Overlap.ValueCounts[ValueKind] += FuncLevelScore;
581 }
582 
583 // Return false on mismatch.
584 void InstrProfRecord::overlapValueProfData(uint32_t ValueKind,
585                                            InstrProfRecord &Other,
586                                            OverlapStats &Overlap,
587                                            OverlapStats &FuncLevelOverlap) {
588   uint32_t ThisNumValueSites = getNumValueSites(ValueKind);
589   assert(ThisNumValueSites == Other.getNumValueSites(ValueKind));
590   if (!ThisNumValueSites)
591     return;
592 
593   std::vector<InstrProfValueSiteRecord> &ThisSiteRecords =
594       getOrCreateValueSitesForKind(ValueKind);
595   MutableArrayRef<InstrProfValueSiteRecord> OtherSiteRecords =
596       Other.getValueSitesForKind(ValueKind);
597   for (uint32_t I = 0; I < ThisNumValueSites; I++)
598     ThisSiteRecords[I].overlap(OtherSiteRecords[I], ValueKind, Overlap,
599                                FuncLevelOverlap);
600 }
601 
602 void InstrProfRecord::overlap(InstrProfRecord &Other, OverlapStats &Overlap,
603                               OverlapStats &FuncLevelOverlap,
604                               uint64_t ValueCutoff) {
605   // FuncLevel CountSum for other should already computed and nonzero.
606   assert(FuncLevelOverlap.Test.CountSum >= 1.0f);
607   accumulateCounts(FuncLevelOverlap.Base);
608   bool Mismatch = (Counts.size() != Other.Counts.size());
609 
610   // Check if the value profiles mismatch.
611   if (!Mismatch) {
612     for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind) {
613       uint32_t ThisNumValueSites = getNumValueSites(Kind);
614       uint32_t OtherNumValueSites = Other.getNumValueSites(Kind);
615       if (ThisNumValueSites != OtherNumValueSites) {
616         Mismatch = true;
617         break;
618       }
619     }
620   }
621   if (Mismatch) {
622     Overlap.addOneMismatch(FuncLevelOverlap.Test);
623     return;
624   }
625 
626   // Compute overlap for value counts.
627   for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
628     overlapValueProfData(Kind, Other, Overlap, FuncLevelOverlap);
629 
630   double Score = 0.0;
631   uint64_t MaxCount = 0;
632   // Compute overlap for edge counts.
633   for (size_t I = 0, E = Other.Counts.size(); I < E; ++I) {
634     Score += OverlapStats::score(Counts[I], Other.Counts[I],
635                                  Overlap.Base.CountSum, Overlap.Test.CountSum);
636     MaxCount = std::max(Other.Counts[I], MaxCount);
637   }
638   Overlap.Overlap.CountSum += Score;
639   Overlap.Overlap.NumEntries += 1;
640 
641   if (MaxCount >= ValueCutoff) {
642     double FuncScore = 0.0;
643     for (size_t I = 0, E = Other.Counts.size(); I < E; ++I)
644       FuncScore += OverlapStats::score(Counts[I], Other.Counts[I],
645                                        FuncLevelOverlap.Base.CountSum,
646                                        FuncLevelOverlap.Test.CountSum);
647     FuncLevelOverlap.Overlap.CountSum = FuncScore;
648     FuncLevelOverlap.Overlap.NumEntries = Other.Counts.size();
649     FuncLevelOverlap.Valid = true;
650   }
651 }
652 
653 void InstrProfValueSiteRecord::merge(InstrProfValueSiteRecord &Input,
654                                      uint64_t Weight,
655                                      function_ref<void(instrprof_error)> Warn) {
656   this->sortByTargetValues();
657   Input.sortByTargetValues();
658   auto I = ValueData.begin();
659   auto IE = ValueData.end();
660   for (auto J = Input.ValueData.begin(), JE = Input.ValueData.end(); J != JE;
661        ++J) {
662     while (I != IE && I->Value < J->Value)
663       ++I;
664     if (I != IE && I->Value == J->Value) {
665       bool Overflowed;
666       I->Count = SaturatingMultiplyAdd(J->Count, Weight, I->Count, &Overflowed);
667       if (Overflowed)
668         Warn(instrprof_error::counter_overflow);
669       ++I;
670       continue;
671     }
672     ValueData.insert(I, *J);
673   }
674 }
675 
676 void InstrProfValueSiteRecord::scale(uint64_t N, uint64_t D,
677                                      function_ref<void(instrprof_error)> Warn) {
678   for (auto I = ValueData.begin(), IE = ValueData.end(); I != IE; ++I) {
679     bool Overflowed;
680     I->Count = SaturatingMultiply(I->Count, N, &Overflowed) / D;
681     if (Overflowed)
682       Warn(instrprof_error::counter_overflow);
683   }
684 }
685 
686 // Merge Value Profile data from Src record to this record for ValueKind.
687 // Scale merged value counts by \p Weight.
688 void InstrProfRecord::mergeValueProfData(
689     uint32_t ValueKind, InstrProfRecord &Src, uint64_t Weight,
690     function_ref<void(instrprof_error)> Warn) {
691   uint32_t ThisNumValueSites = getNumValueSites(ValueKind);
692   uint32_t OtherNumValueSites = Src.getNumValueSites(ValueKind);
693   if (ThisNumValueSites != OtherNumValueSites) {
694     Warn(instrprof_error::value_site_count_mismatch);
695     return;
696   }
697   if (!ThisNumValueSites)
698     return;
699   std::vector<InstrProfValueSiteRecord> &ThisSiteRecords =
700       getOrCreateValueSitesForKind(ValueKind);
701   MutableArrayRef<InstrProfValueSiteRecord> OtherSiteRecords =
702       Src.getValueSitesForKind(ValueKind);
703   for (uint32_t I = 0; I < ThisNumValueSites; I++)
704     ThisSiteRecords[I].merge(OtherSiteRecords[I], Weight, Warn);
705 }
706 
707 void InstrProfRecord::merge(InstrProfRecord &Other, uint64_t Weight,
708                             function_ref<void(instrprof_error)> Warn) {
709   // If the number of counters doesn't match we either have bad data
710   // or a hash collision.
711   if (Counts.size() != Other.Counts.size()) {
712     Warn(instrprof_error::count_mismatch);
713     return;
714   }
715 
716   for (size_t I = 0, E = Other.Counts.size(); I < E; ++I) {
717     bool Overflowed;
718     Counts[I] =
719         SaturatingMultiplyAdd(Other.Counts[I], Weight, Counts[I], &Overflowed);
720     if (Overflowed)
721       Warn(instrprof_error::counter_overflow);
722   }
723 
724   for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
725     mergeValueProfData(Kind, Other, Weight, Warn);
726 }
727 
728 void InstrProfRecord::scaleValueProfData(
729     uint32_t ValueKind, uint64_t N, uint64_t D,
730     function_ref<void(instrprof_error)> Warn) {
731   for (auto &R : getValueSitesForKind(ValueKind))
732     R.scale(N, D, Warn);
733 }
734 
735 void InstrProfRecord::scale(uint64_t N, uint64_t D,
736                             function_ref<void(instrprof_error)> Warn) {
737   assert(D != 0 && "D cannot be 0");
738   for (auto &Count : this->Counts) {
739     bool Overflowed;
740     Count = SaturatingMultiply(Count, N, &Overflowed) / D;
741     if (Overflowed)
742       Warn(instrprof_error::counter_overflow);
743   }
744   for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
745     scaleValueProfData(Kind, N, D, Warn);
746 }
747 
748 // Map indirect call target name hash to name string.
749 uint64_t InstrProfRecord::remapValue(uint64_t Value, uint32_t ValueKind,
750                                      InstrProfSymtab *SymTab) {
751   if (!SymTab)
752     return Value;
753 
754   if (ValueKind == IPVK_IndirectCallTarget)
755     return SymTab->getFunctionHashFromAddress(Value);
756 
757   return Value;
758 }
759 
760 void InstrProfRecord::addValueData(uint32_t ValueKind, uint32_t Site,
761                                    InstrProfValueData *VData, uint32_t N,
762                                    InstrProfSymtab *ValueMap) {
763   for (uint32_t I = 0; I < N; I++) {
764     VData[I].Value = remapValue(VData[I].Value, ValueKind, ValueMap);
765   }
766   std::vector<InstrProfValueSiteRecord> &ValueSites =
767       getOrCreateValueSitesForKind(ValueKind);
768   if (N == 0)
769     ValueSites.emplace_back();
770   else
771     ValueSites.emplace_back(VData, VData + N);
772 }
773 
774 #define INSTR_PROF_COMMON_API_IMPL
775 #include "llvm/ProfileData/InstrProfData.inc"
776 
777 /*!
778  * ValueProfRecordClosure Interface implementation for  InstrProfRecord
779  *  class. These C wrappers are used as adaptors so that C++ code can be
780  *  invoked as callbacks.
781  */
782 uint32_t getNumValueKindsInstrProf(const void *Record) {
783   return reinterpret_cast<const InstrProfRecord *>(Record)->getNumValueKinds();
784 }
785 
786 uint32_t getNumValueSitesInstrProf(const void *Record, uint32_t VKind) {
787   return reinterpret_cast<const InstrProfRecord *>(Record)
788       ->getNumValueSites(VKind);
789 }
790 
791 uint32_t getNumValueDataInstrProf(const void *Record, uint32_t VKind) {
792   return reinterpret_cast<const InstrProfRecord *>(Record)
793       ->getNumValueData(VKind);
794 }
795 
796 uint32_t getNumValueDataForSiteInstrProf(const void *R, uint32_t VK,
797                                          uint32_t S) {
798   return reinterpret_cast<const InstrProfRecord *>(R)
799       ->getNumValueDataForSite(VK, S);
800 }
801 
802 void getValueForSiteInstrProf(const void *R, InstrProfValueData *Dst,
803                               uint32_t K, uint32_t S) {
804   reinterpret_cast<const InstrProfRecord *>(R)->getValueForSite(Dst, K, S);
805 }
806 
807 ValueProfData *allocValueProfDataInstrProf(size_t TotalSizeInBytes) {
808   ValueProfData *VD =
809       (ValueProfData *)(new (::operator new(TotalSizeInBytes)) ValueProfData());
810   memset(VD, 0, TotalSizeInBytes);
811   return VD;
812 }
813 
814 static ValueProfRecordClosure InstrProfRecordClosure = {
815     nullptr,
816     getNumValueKindsInstrProf,
817     getNumValueSitesInstrProf,
818     getNumValueDataInstrProf,
819     getNumValueDataForSiteInstrProf,
820     nullptr,
821     getValueForSiteInstrProf,
822     allocValueProfDataInstrProf};
823 
824 // Wrapper implementation using the closure mechanism.
825 uint32_t ValueProfData::getSize(const InstrProfRecord &Record) {
826   auto Closure = InstrProfRecordClosure;
827   Closure.Record = &Record;
828   return getValueProfDataSize(&Closure);
829 }
830 
831 // Wrapper implementation using the closure mechanism.
832 std::unique_ptr<ValueProfData>
833 ValueProfData::serializeFrom(const InstrProfRecord &Record) {
834   InstrProfRecordClosure.Record = &Record;
835 
836   std::unique_ptr<ValueProfData> VPD(
837       serializeValueProfDataFrom(&InstrProfRecordClosure, nullptr));
838   return VPD;
839 }
840 
841 void ValueProfRecord::deserializeTo(InstrProfRecord &Record,
842                                     InstrProfSymtab *SymTab) {
843   Record.reserveSites(Kind, NumValueSites);
844 
845   InstrProfValueData *ValueData = getValueProfRecordValueData(this);
846   for (uint64_t VSite = 0; VSite < NumValueSites; ++VSite) {
847     uint8_t ValueDataCount = this->SiteCountArray[VSite];
848     Record.addValueData(Kind, VSite, ValueData, ValueDataCount, SymTab);
849     ValueData += ValueDataCount;
850   }
851 }
852 
853 // For writing/serializing,  Old is the host endianness, and  New is
854 // byte order intended on disk. For Reading/deserialization, Old
855 // is the on-disk source endianness, and New is the host endianness.
856 void ValueProfRecord::swapBytes(support::endianness Old,
857                                 support::endianness New) {
858   using namespace support;
859 
860   if (Old == New)
861     return;
862 
863   if (getHostEndianness() != Old) {
864     sys::swapByteOrder<uint32_t>(NumValueSites);
865     sys::swapByteOrder<uint32_t>(Kind);
866   }
867   uint32_t ND = getValueProfRecordNumValueData(this);
868   InstrProfValueData *VD = getValueProfRecordValueData(this);
869 
870   // No need to swap byte array: SiteCountArrray.
871   for (uint32_t I = 0; I < ND; I++) {
872     sys::swapByteOrder<uint64_t>(VD[I].Value);
873     sys::swapByteOrder<uint64_t>(VD[I].Count);
874   }
875   if (getHostEndianness() == Old) {
876     sys::swapByteOrder<uint32_t>(NumValueSites);
877     sys::swapByteOrder<uint32_t>(Kind);
878   }
879 }
880 
881 void ValueProfData::deserializeTo(InstrProfRecord &Record,
882                                   InstrProfSymtab *SymTab) {
883   if (NumValueKinds == 0)
884     return;
885 
886   ValueProfRecord *VR = getFirstValueProfRecord(this);
887   for (uint32_t K = 0; K < NumValueKinds; K++) {
888     VR->deserializeTo(Record, SymTab);
889     VR = getValueProfRecordNext(VR);
890   }
891 }
892 
893 template <class T>
894 static T swapToHostOrder(const unsigned char *&D, support::endianness Orig) {
895   using namespace support;
896 
897   if (Orig == little)
898     return endian::readNext<T, little, unaligned>(D);
899   else
900     return endian::readNext<T, big, unaligned>(D);
901 }
902 
903 static std::unique_ptr<ValueProfData> allocValueProfData(uint32_t TotalSize) {
904   return std::unique_ptr<ValueProfData>(new (::operator new(TotalSize))
905                                             ValueProfData());
906 }
907 
908 Error ValueProfData::checkIntegrity() {
909   if (NumValueKinds > IPVK_Last + 1)
910     return make_error<InstrProfError>(
911         instrprof_error::malformed, "number of value profile kinds is invalid");
912   // Total size needs to be multiple of quadword size.
913   if (TotalSize % sizeof(uint64_t))
914     return make_error<InstrProfError>(
915         instrprof_error::malformed, "total size is not multiples of quardword");
916 
917   ValueProfRecord *VR = getFirstValueProfRecord(this);
918   for (uint32_t K = 0; K < this->NumValueKinds; K++) {
919     if (VR->Kind > IPVK_Last)
920       return make_error<InstrProfError>(instrprof_error::malformed,
921                                         "value kind is invalid");
922     VR = getValueProfRecordNext(VR);
923     if ((char *)VR - (char *)this > (ptrdiff_t)TotalSize)
924       return make_error<InstrProfError>(
925           instrprof_error::malformed,
926           "value profile address is greater than total size");
927   }
928   return Error::success();
929 }
930 
931 Expected<std::unique_ptr<ValueProfData>>
932 ValueProfData::getValueProfData(const unsigned char *D,
933                                 const unsigned char *const BufferEnd,
934                                 support::endianness Endianness) {
935   using namespace support;
936 
937   if (D + sizeof(ValueProfData) > BufferEnd)
938     return make_error<InstrProfError>(instrprof_error::truncated);
939 
940   const unsigned char *Header = D;
941   uint32_t TotalSize = swapToHostOrder<uint32_t>(Header, Endianness);
942   if (D + TotalSize > BufferEnd)
943     return make_error<InstrProfError>(instrprof_error::too_large);
944 
945   std::unique_ptr<ValueProfData> VPD = allocValueProfData(TotalSize);
946   memcpy(VPD.get(), D, TotalSize);
947   // Byte swap.
948   VPD->swapBytesToHost(Endianness);
949 
950   Error E = VPD->checkIntegrity();
951   if (E)
952     return std::move(E);
953 
954   return std::move(VPD);
955 }
956 
957 void ValueProfData::swapBytesToHost(support::endianness Endianness) {
958   using namespace support;
959 
960   if (Endianness == getHostEndianness())
961     return;
962 
963   sys::swapByteOrder<uint32_t>(TotalSize);
964   sys::swapByteOrder<uint32_t>(NumValueKinds);
965 
966   ValueProfRecord *VR = getFirstValueProfRecord(this);
967   for (uint32_t K = 0; K < NumValueKinds; K++) {
968     VR->swapBytes(Endianness, getHostEndianness());
969     VR = getValueProfRecordNext(VR);
970   }
971 }
972 
973 void ValueProfData::swapBytesFromHost(support::endianness Endianness) {
974   using namespace support;
975 
976   if (Endianness == getHostEndianness())
977     return;
978 
979   ValueProfRecord *VR = getFirstValueProfRecord(this);
980   for (uint32_t K = 0; K < NumValueKinds; K++) {
981     ValueProfRecord *NVR = getValueProfRecordNext(VR);
982     VR->swapBytes(getHostEndianness(), Endianness);
983     VR = NVR;
984   }
985   sys::swapByteOrder<uint32_t>(TotalSize);
986   sys::swapByteOrder<uint32_t>(NumValueKinds);
987 }
988 
989 void annotateValueSite(Module &M, Instruction &Inst,
990                        const InstrProfRecord &InstrProfR,
991                        InstrProfValueKind ValueKind, uint32_t SiteIdx,
992                        uint32_t MaxMDCount) {
993   uint32_t NV = InstrProfR.getNumValueDataForSite(ValueKind, SiteIdx);
994   if (!NV)
995     return;
996 
997   uint64_t Sum = 0;
998   std::unique_ptr<InstrProfValueData[]> VD =
999       InstrProfR.getValueForSite(ValueKind, SiteIdx, &Sum);
1000 
1001   ArrayRef<InstrProfValueData> VDs(VD.get(), NV);
1002   annotateValueSite(M, Inst, VDs, Sum, ValueKind, MaxMDCount);
1003 }
1004 
1005 void annotateValueSite(Module &M, Instruction &Inst,
1006                        ArrayRef<InstrProfValueData> VDs,
1007                        uint64_t Sum, InstrProfValueKind ValueKind,
1008                        uint32_t MaxMDCount) {
1009   LLVMContext &Ctx = M.getContext();
1010   MDBuilder MDHelper(Ctx);
1011   SmallVector<Metadata *, 3> Vals;
1012   // Tag
1013   Vals.push_back(MDHelper.createString("VP"));
1014   // Value Kind
1015   Vals.push_back(MDHelper.createConstant(
1016       ConstantInt::get(Type::getInt32Ty(Ctx), ValueKind)));
1017   // Total Count
1018   Vals.push_back(
1019       MDHelper.createConstant(ConstantInt::get(Type::getInt64Ty(Ctx), Sum)));
1020 
1021   // Value Profile Data
1022   uint32_t MDCount = MaxMDCount;
1023   for (auto &VD : VDs) {
1024     Vals.push_back(MDHelper.createConstant(
1025         ConstantInt::get(Type::getInt64Ty(Ctx), VD.Value)));
1026     Vals.push_back(MDHelper.createConstant(
1027         ConstantInt::get(Type::getInt64Ty(Ctx), VD.Count)));
1028     if (--MDCount == 0)
1029       break;
1030   }
1031   Inst.setMetadata(LLVMContext::MD_prof, MDNode::get(Ctx, Vals));
1032 }
1033 
1034 bool getValueProfDataFromInst(const Instruction &Inst,
1035                               InstrProfValueKind ValueKind,
1036                               uint32_t MaxNumValueData,
1037                               InstrProfValueData ValueData[],
1038                               uint32_t &ActualNumValueData, uint64_t &TotalC,
1039                               bool GetNoICPValue) {
1040   MDNode *MD = Inst.getMetadata(LLVMContext::MD_prof);
1041   if (!MD)
1042     return false;
1043 
1044   unsigned NOps = MD->getNumOperands();
1045 
1046   if (NOps < 5)
1047     return false;
1048 
1049   // Operand 0 is a string tag "VP":
1050   MDString *Tag = cast<MDString>(MD->getOperand(0));
1051   if (!Tag)
1052     return false;
1053 
1054   if (!Tag->getString().equals("VP"))
1055     return false;
1056 
1057   // Now check kind:
1058   ConstantInt *KindInt = mdconst::dyn_extract<ConstantInt>(MD->getOperand(1));
1059   if (!KindInt)
1060     return false;
1061   if (KindInt->getZExtValue() != ValueKind)
1062     return false;
1063 
1064   // Get total count
1065   ConstantInt *TotalCInt = mdconst::dyn_extract<ConstantInt>(MD->getOperand(2));
1066   if (!TotalCInt)
1067     return false;
1068   TotalC = TotalCInt->getZExtValue();
1069 
1070   ActualNumValueData = 0;
1071 
1072   for (unsigned I = 3; I < NOps; I += 2) {
1073     if (ActualNumValueData >= MaxNumValueData)
1074       break;
1075     ConstantInt *Value = mdconst::dyn_extract<ConstantInt>(MD->getOperand(I));
1076     ConstantInt *Count =
1077         mdconst::dyn_extract<ConstantInt>(MD->getOperand(I + 1));
1078     if (!Value || !Count)
1079       return false;
1080     uint64_t CntValue = Count->getZExtValue();
1081     if (!GetNoICPValue && (CntValue == NOMORE_ICP_MAGICNUM))
1082       continue;
1083     ValueData[ActualNumValueData].Value = Value->getZExtValue();
1084     ValueData[ActualNumValueData].Count = CntValue;
1085     ActualNumValueData++;
1086   }
1087   return true;
1088 }
1089 
1090 MDNode *getPGOFuncNameMetadata(const Function &F) {
1091   return F.getMetadata(getPGOFuncNameMetadataName());
1092 }
1093 
1094 void createPGOFuncNameMetadata(Function &F, StringRef PGOFuncName) {
1095   // Only for internal linkage functions.
1096   if (PGOFuncName == F.getName())
1097       return;
1098   // Don't create duplicated meta-data.
1099   if (getPGOFuncNameMetadata(F))
1100     return;
1101   LLVMContext &C = F.getContext();
1102   MDNode *N = MDNode::get(C, MDString::get(C, PGOFuncName));
1103   F.setMetadata(getPGOFuncNameMetadataName(), N);
1104 }
1105 
1106 bool needsComdatForCounter(const Function &F, const Module &M) {
1107   if (F.hasComdat())
1108     return true;
1109 
1110   if (!Triple(M.getTargetTriple()).supportsCOMDAT())
1111     return false;
1112 
1113   // See createPGOFuncNameVar for more details. To avoid link errors, profile
1114   // counters for function with available_externally linkage needs to be changed
1115   // to linkonce linkage. On ELF based systems, this leads to weak symbols to be
1116   // created. Without using comdat, duplicate entries won't be removed by the
1117   // linker leading to increased data segement size and raw profile size. Even
1118   // worse, since the referenced counter from profile per-function data object
1119   // will be resolved to the common strong definition, the profile counts for
1120   // available_externally functions will end up being duplicated in raw profile
1121   // data. This can result in distorted profile as the counts of those dups
1122   // will be accumulated by the profile merger.
1123   GlobalValue::LinkageTypes Linkage = F.getLinkage();
1124   if (Linkage != GlobalValue::ExternalWeakLinkage &&
1125       Linkage != GlobalValue::AvailableExternallyLinkage)
1126     return false;
1127 
1128   return true;
1129 }
1130 
1131 // Check if INSTR_PROF_RAW_VERSION_VAR is defined.
1132 bool isIRPGOFlagSet(const Module *M) {
1133   auto IRInstrVar =
1134       M->getNamedGlobal(INSTR_PROF_QUOTE(INSTR_PROF_RAW_VERSION_VAR));
1135   if (!IRInstrVar || IRInstrVar->hasLocalLinkage())
1136     return false;
1137 
1138   // For CSPGO+LTO, this variable might be marked as non-prevailing and we only
1139   // have the decl.
1140   if (IRInstrVar->isDeclaration())
1141     return true;
1142 
1143   // Check if the flag is set.
1144   if (!IRInstrVar->hasInitializer())
1145     return false;
1146 
1147   auto *InitVal = dyn_cast_or_null<ConstantInt>(IRInstrVar->getInitializer());
1148   if (!InitVal)
1149     return false;
1150   return (InitVal->getZExtValue() & VARIANT_MASK_IR_PROF) != 0;
1151 }
1152 
1153 // Check if we can safely rename this Comdat function.
1154 bool canRenameComdatFunc(const Function &F, bool CheckAddressTaken) {
1155   if (F.getName().empty())
1156     return false;
1157   if (!needsComdatForCounter(F, *(F.getParent())))
1158     return false;
1159   // Unsafe to rename the address-taken function (which can be used in
1160   // function comparison).
1161   if (CheckAddressTaken && F.hasAddressTaken())
1162     return false;
1163   // Only safe to do if this function may be discarded if it is not used
1164   // in the compilation unit.
1165   if (!GlobalValue::isDiscardableIfUnused(F.getLinkage()))
1166     return false;
1167 
1168   // For AvailableExternallyLinkage functions.
1169   if (!F.hasComdat()) {
1170     assert(F.getLinkage() == GlobalValue::AvailableExternallyLinkage);
1171     return true;
1172   }
1173   return true;
1174 }
1175 
1176 // Create a COMDAT variable INSTR_PROF_RAW_VERSION_VAR to make the runtime
1177 // aware this is an ir_level profile so it can set the version flag.
1178 GlobalVariable *createIRLevelProfileFlagVar(Module &M, bool IsCS,
1179                                             bool InstrEntryBBEnabled) {
1180   const StringRef VarName(INSTR_PROF_QUOTE(INSTR_PROF_RAW_VERSION_VAR));
1181   Type *IntTy64 = Type::getInt64Ty(M.getContext());
1182   uint64_t ProfileVersion = (INSTR_PROF_RAW_VERSION | VARIANT_MASK_IR_PROF);
1183   if (IsCS)
1184     ProfileVersion |= VARIANT_MASK_CSIR_PROF;
1185   if (InstrEntryBBEnabled)
1186     ProfileVersion |= VARIANT_MASK_INSTR_ENTRY;
1187   auto IRLevelVersionVariable = new GlobalVariable(
1188       M, IntTy64, true, GlobalValue::WeakAnyLinkage,
1189       Constant::getIntegerValue(IntTy64, APInt(64, ProfileVersion)), VarName);
1190   IRLevelVersionVariable->setVisibility(GlobalValue::DefaultVisibility);
1191   Triple TT(M.getTargetTriple());
1192   if (TT.supportsCOMDAT()) {
1193     IRLevelVersionVariable->setLinkage(GlobalValue::ExternalLinkage);
1194     IRLevelVersionVariable->setComdat(M.getOrInsertComdat(VarName));
1195   }
1196   return IRLevelVersionVariable;
1197 }
1198 
1199 // Create the variable for the profile file name.
1200 void createProfileFileNameVar(Module &M, StringRef InstrProfileOutput) {
1201   if (InstrProfileOutput.empty())
1202     return;
1203   Constant *ProfileNameConst =
1204       ConstantDataArray::getString(M.getContext(), InstrProfileOutput, true);
1205   GlobalVariable *ProfileNameVar = new GlobalVariable(
1206       M, ProfileNameConst->getType(), true, GlobalValue::WeakAnyLinkage,
1207       ProfileNameConst, INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_NAME_VAR));
1208   Triple TT(M.getTargetTriple());
1209   if (TT.supportsCOMDAT()) {
1210     ProfileNameVar->setLinkage(GlobalValue::ExternalLinkage);
1211     ProfileNameVar->setComdat(M.getOrInsertComdat(
1212         StringRef(INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_NAME_VAR))));
1213   }
1214 }
1215 
1216 Error OverlapStats::accumulateCounts(const std::string &BaseFilename,
1217                                      const std::string &TestFilename,
1218                                      bool IsCS) {
1219   auto getProfileSum = [IsCS](const std::string &Filename,
1220                               CountSumOrPercent &Sum) -> Error {
1221     auto ReaderOrErr = InstrProfReader::create(Filename);
1222     if (Error E = ReaderOrErr.takeError()) {
1223       return E;
1224     }
1225     auto Reader = std::move(ReaderOrErr.get());
1226     Reader->accumulateCounts(Sum, IsCS);
1227     return Error::success();
1228   };
1229   auto Ret = getProfileSum(BaseFilename, Base);
1230   if (Ret)
1231     return Ret;
1232   Ret = getProfileSum(TestFilename, Test);
1233   if (Ret)
1234     return Ret;
1235   this->BaseFilename = &BaseFilename;
1236   this->TestFilename = &TestFilename;
1237   Valid = true;
1238   return Error::success();
1239 }
1240 
1241 void OverlapStats::addOneMismatch(const CountSumOrPercent &MismatchFunc) {
1242   Mismatch.NumEntries += 1;
1243   Mismatch.CountSum += MismatchFunc.CountSum / Test.CountSum;
1244   for (unsigned I = 0; I < IPVK_Last - IPVK_First + 1; I++) {
1245     if (Test.ValueCounts[I] >= 1.0f)
1246       Mismatch.ValueCounts[I] +=
1247           MismatchFunc.ValueCounts[I] / Test.ValueCounts[I];
1248   }
1249 }
1250 
1251 void OverlapStats::addOneUnique(const CountSumOrPercent &UniqueFunc) {
1252   Unique.NumEntries += 1;
1253   Unique.CountSum += UniqueFunc.CountSum / Test.CountSum;
1254   for (unsigned I = 0; I < IPVK_Last - IPVK_First + 1; I++) {
1255     if (Test.ValueCounts[I] >= 1.0f)
1256       Unique.ValueCounts[I] += UniqueFunc.ValueCounts[I] / Test.ValueCounts[I];
1257   }
1258 }
1259 
1260 void OverlapStats::dump(raw_fd_ostream &OS) const {
1261   if (!Valid)
1262     return;
1263 
1264   const char *EntryName =
1265       (Level == ProgramLevel ? "functions" : "edge counters");
1266   if (Level == ProgramLevel) {
1267     OS << "Profile overlap infomation for base_profile: " << *BaseFilename
1268        << " and test_profile: " << *TestFilename << "\nProgram level:\n";
1269   } else {
1270     OS << "Function level:\n"
1271        << "  Function: " << FuncName << " (Hash=" << FuncHash << ")\n";
1272   }
1273 
1274   OS << "  # of " << EntryName << " overlap: " << Overlap.NumEntries << "\n";
1275   if (Mismatch.NumEntries)
1276     OS << "  # of " << EntryName << " mismatch: " << Mismatch.NumEntries
1277        << "\n";
1278   if (Unique.NumEntries)
1279     OS << "  # of " << EntryName
1280        << " only in test_profile: " << Unique.NumEntries << "\n";
1281 
1282   OS << "  Edge profile overlap: " << format("%.3f%%", Overlap.CountSum * 100)
1283      << "\n";
1284   if (Mismatch.NumEntries)
1285     OS << "  Mismatched count percentage (Edge): "
1286        << format("%.3f%%", Mismatch.CountSum * 100) << "\n";
1287   if (Unique.NumEntries)
1288     OS << "  Percentage of Edge profile only in test_profile: "
1289        << format("%.3f%%", Unique.CountSum * 100) << "\n";
1290   OS << "  Edge profile base count sum: " << format("%.0f", Base.CountSum)
1291      << "\n"
1292      << "  Edge profile test count sum: " << format("%.0f", Test.CountSum)
1293      << "\n";
1294 
1295   for (unsigned I = 0; I < IPVK_Last - IPVK_First + 1; I++) {
1296     if (Base.ValueCounts[I] < 1.0f && Test.ValueCounts[I] < 1.0f)
1297       continue;
1298     char ProfileKindName[20];
1299     switch (I) {
1300     case IPVK_IndirectCallTarget:
1301       strncpy(ProfileKindName, "IndirectCall", 19);
1302       break;
1303     case IPVK_MemOPSize:
1304       strncpy(ProfileKindName, "MemOP", 19);
1305       break;
1306     default:
1307       snprintf(ProfileKindName, 19, "VP[%d]", I);
1308       break;
1309     }
1310     OS << "  " << ProfileKindName
1311        << " profile overlap: " << format("%.3f%%", Overlap.ValueCounts[I] * 100)
1312        << "\n";
1313     if (Mismatch.NumEntries)
1314       OS << "  Mismatched count percentage (" << ProfileKindName
1315          << "): " << format("%.3f%%", Mismatch.ValueCounts[I] * 100) << "\n";
1316     if (Unique.NumEntries)
1317       OS << "  Percentage of " << ProfileKindName
1318          << " profile only in test_profile: "
1319          << format("%.3f%%", Unique.ValueCounts[I] * 100) << "\n";
1320     OS << "  " << ProfileKindName
1321        << " profile base count sum: " << format("%.0f", Base.ValueCounts[I])
1322        << "\n"
1323        << "  " << ProfileKindName
1324        << " profile test count sum: " << format("%.0f", Test.ValueCounts[I])
1325        << "\n";
1326   }
1327 }
1328 
1329 } // end namespace llvm
1330