xref: /llvm-project/llvm/include/llvm/ProfileData/InstrProf.h (revision ff281f7d37ead15bdbdbfccb4b82ea93013b1a00)
1 //===- InstrProf.h - Instrumented profiling format support ------*- C++ -*-===//
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 // Instrumentation-based profiling data is generated by instrumented
10 // binaries through library functions in compiler-rt, and read by the clang
11 // frontend to feed PGO.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_PROFILEDATA_INSTRPROF_H
16 #define LLVM_PROFILEDATA_INSTRPROF_H
17 
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/BitmaskEnum.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/IntervalMap.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/ADT/StringSet.h"
25 #include "llvm/IR/GlobalValue.h"
26 #include "llvm/IR/ProfileSummary.h"
27 #include "llvm/ProfileData/InstrProfData.inc"
28 #include "llvm/Support/BalancedPartitioning.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Support/Compiler.h"
31 #include "llvm/Support/Error.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/MD5.h"
34 #include "llvm/Support/MathExtras.h"
35 #include "llvm/Support/raw_ostream.h"
36 #include "llvm/TargetParser/Host.h"
37 #include "llvm/TargetParser/Triple.h"
38 #include <algorithm>
39 #include <cassert>
40 #include <cstddef>
41 #include <cstdint>
42 #include <cstring>
43 #include <list>
44 #include <memory>
45 #include <string>
46 #include <system_error>
47 #include <utility>
48 #include <vector>
49 
50 namespace llvm {
51 
52 class Function;
53 class GlobalVariable;
54 struct InstrProfRecord;
55 class InstrProfSymtab;
56 class Instruction;
57 class MDNode;
58 class Module;
59 
60 enum InstrProfSectKind {
61 #define INSTR_PROF_SECT_ENTRY(Kind, SectNameCommon, SectNameCoff, Prefix) Kind,
62 #include "llvm/ProfileData/InstrProfData.inc"
63 };
64 
65 /// Return the max count value. We reserver a few large values for special use.
66 inline uint64_t getInstrMaxCountValue() {
67   return std::numeric_limits<uint64_t>::max() - 2;
68 }
69 
70 /// Return the name of the profile section corresponding to \p IPSK.
71 ///
72 /// The name of the section depends on the object format type \p OF. If
73 /// \p AddSegmentInfo is true, a segment prefix and additional linker hints may
74 /// be added to the section name (this is the default).
75 std::string getInstrProfSectionName(InstrProfSectKind IPSK,
76                                     Triple::ObjectFormatType OF,
77                                     bool AddSegmentInfo = true);
78 
79 /// Return the name profile runtime entry point to do value profiling
80 /// for a given site.
81 inline StringRef getInstrProfValueProfFuncName() {
82   return INSTR_PROF_VALUE_PROF_FUNC_STR;
83 }
84 
85 /// Return the name profile runtime entry point to do memop size value
86 /// profiling.
87 inline StringRef getInstrProfValueProfMemOpFuncName() {
88   return INSTR_PROF_VALUE_PROF_MEMOP_FUNC_STR;
89 }
90 
91 /// Return the name prefix of variables containing instrumented function names.
92 inline StringRef getInstrProfNameVarPrefix() { return "__profn_"; }
93 
94 /// Return the name prefix of variables containing virtual table profile data.
95 inline StringRef getInstrProfVTableVarPrefix() { return "__profvt_"; }
96 
97 /// Return the name prefix of variables containing per-function control data.
98 inline StringRef getInstrProfDataVarPrefix() { return "__profd_"; }
99 
100 /// Return the name prefix of profile counter variables.
101 inline StringRef getInstrProfCountersVarPrefix() { return "__profc_"; }
102 
103 /// Return the name prefix of profile bitmap variables.
104 inline StringRef getInstrProfBitmapVarPrefix() { return "__profbm_"; }
105 
106 /// Return the name prefix of value profile variables.
107 inline StringRef getInstrProfValuesVarPrefix() { return "__profvp_"; }
108 
109 /// Return the name of value profile node array variables:
110 inline StringRef getInstrProfVNodesVarName() { return "__llvm_prf_vnodes"; }
111 
112 /// Return the name of the variable holding the strings (possibly compressed)
113 /// of all function's PGO names.
114 inline StringRef getInstrProfNamesVarName() { return "__llvm_prf_nm"; }
115 
116 inline StringRef getInstrProfVTableNamesVarName() { return "__llvm_prf_vnm"; }
117 
118 /// Return the name of a covarage mapping variable (internal linkage)
119 /// for each instrumented source module. Such variables are allocated
120 /// in the __llvm_covmap section.
121 inline StringRef getCoverageMappingVarName() {
122   return "__llvm_coverage_mapping";
123 }
124 
125 /// Return the name of the internal variable recording the array
126 /// of PGO name vars referenced by the coverage mapping. The owning
127 /// functions of those names are not emitted by FE (e.g, unused inline
128 /// functions.)
129 inline StringRef getCoverageUnusedNamesVarName() {
130   return "__llvm_coverage_names";
131 }
132 
133 /// Return the name of function that registers all the per-function control
134 /// data at program startup time by calling __llvm_register_function. This
135 /// function has internal linkage and is called by  __llvm_profile_init
136 /// runtime method. This function is not generated for these platforms:
137 /// Darwin, Linux, and FreeBSD.
138 inline StringRef getInstrProfRegFuncsName() {
139   return "__llvm_profile_register_functions";
140 }
141 
142 /// Return the name of the runtime interface that registers per-function control
143 /// data for one instrumented function.
144 inline StringRef getInstrProfRegFuncName() {
145   return "__llvm_profile_register_function";
146 }
147 
148 /// Return the name of the runtime interface that registers the PGO name
149 /// strings.
150 inline StringRef getInstrProfNamesRegFuncName() {
151   return "__llvm_profile_register_names_function";
152 }
153 
154 /// Return the name of the runtime initialization method that is generated by
155 /// the compiler. The function calls __llvm_profile_register_functions and
156 /// __llvm_profile_override_default_filename functions if needed. This function
157 /// has internal linkage and invoked at startup time via init_array.
158 inline StringRef getInstrProfInitFuncName() { return "__llvm_profile_init"; }
159 
160 /// Return the name of the hook variable defined in profile runtime library.
161 /// A reference to the variable causes the linker to link in the runtime
162 /// initialization module (which defines the hook variable).
163 inline StringRef getInstrProfRuntimeHookVarName() {
164   return INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_RUNTIME_VAR);
165 }
166 
167 /// Return the name of the compiler generated function that references the
168 /// runtime hook variable. The function is a weak global.
169 inline StringRef getInstrProfRuntimeHookVarUseFuncName() {
170   return "__llvm_profile_runtime_user";
171 }
172 
173 inline StringRef getInstrProfCounterBiasVarName() {
174   return INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_COUNTER_BIAS_VAR);
175 }
176 
177 inline StringRef getInstrProfBitmapBiasVarName() {
178   return INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_BITMAP_BIAS_VAR);
179 }
180 
181 /// Return the marker used to separate PGO names during serialization.
182 inline StringRef getInstrProfNameSeparator() { return "\01"; }
183 
184 /// Determines whether module targets a GPU eligable for PGO
185 /// instrumentation
186 bool isGPUProfTarget(const Module &M);
187 
188 /// Please use getIRPGOFuncName for LLVM IR instrumentation. This function is
189 /// for front-end (Clang, etc) instrumentation.
190 /// Return the modified name for function \c F suitable to be
191 /// used the key for profile lookup. Variable \c InLTO indicates if this
192 /// is called in LTO optimization passes.
193 std::string getPGOFuncName(const Function &F, bool InLTO = false,
194                            uint64_t Version = INSTR_PROF_INDEX_VERSION);
195 
196 /// Return the modified name for a function suitable to be
197 /// used the key for profile lookup. The function's original
198 /// name is \c RawFuncName and has linkage of type \c Linkage.
199 /// The function is defined in module \c FileName.
200 std::string getPGOFuncName(StringRef RawFuncName,
201                            GlobalValue::LinkageTypes Linkage,
202                            StringRef FileName,
203                            uint64_t Version = INSTR_PROF_INDEX_VERSION);
204 
205 /// \return the modified name for function \c F suitable to be
206 /// used as the key for IRPGO profile lookup. \c InLTO indicates if this is
207 /// called from LTO optimization passes.
208 std::string getIRPGOFuncName(const Function &F, bool InLTO = false);
209 
210 /// \return the filename and the function name parsed from the output of
211 /// \c getIRPGOFuncName()
212 std::pair<StringRef, StringRef> getParsedIRPGOName(StringRef IRPGOName);
213 
214 /// Return the name of the global variable used to store a function
215 /// name in PGO instrumentation. \c FuncName is the IRPGO function name
216 /// (returned by \c getIRPGOFuncName) for LLVM IR instrumentation and PGO
217 /// function name (returned by \c getPGOFuncName) for front-end instrumentation.
218 std::string getPGOFuncNameVarName(StringRef FuncName,
219                                   GlobalValue::LinkageTypes Linkage);
220 
221 /// Create and return the global variable for function name used in PGO
222 /// instrumentation. \c FuncName is the IRPGO function name (returned by
223 /// \c getIRPGOFuncName) for LLVM IR instrumentation and PGO function name
224 /// (returned by \c getPGOFuncName) for front-end instrumentation.
225 GlobalVariable *createPGOFuncNameVar(Function &F, StringRef PGOFuncName);
226 
227 /// Create and return the global variable for function name used in PGO
228 /// instrumentation. \c FuncName is the IRPGO function name (returned by
229 /// \c getIRPGOFuncName) for LLVM IR instrumentation and PGO function name
230 /// (returned by \c getPGOFuncName) for front-end instrumentation.
231 GlobalVariable *createPGOFuncNameVar(Module &M,
232                                      GlobalValue::LinkageTypes Linkage,
233                                      StringRef PGOFuncName);
234 
235 /// Return the initializer in string of the PGO name var \c NameVar.
236 StringRef getPGOFuncNameVarInitializer(GlobalVariable *NameVar);
237 
238 /// Given a PGO function name, remove the filename prefix and return
239 /// the original (static) function name.
240 StringRef getFuncNameWithoutPrefix(StringRef PGOFuncName,
241                                    StringRef FileName = "<unknown>");
242 
243 /// Given a vector of strings (names of global objects like functions or,
244 /// virtual tables) \c NameStrs, the method generates a combined string \c
245 /// Result that is ready to be serialized.  The \c Result string is comprised of
246 /// three fields: The first field is the length of the uncompressed strings, and
247 /// the the second field is the length of the zlib-compressed string. Both
248 /// fields are encoded in ULEB128.  If \c doCompress is false, the
249 ///  third field is the uncompressed strings; otherwise it is the
250 /// compressed string. When the string compression is off, the
251 /// second field will have value zero.
252 Error collectGlobalObjectNameStrings(ArrayRef<std::string> NameStrs,
253                                      bool doCompression, std::string &Result);
254 
255 /// Produce \c Result string with the same format described above. The input
256 /// is vector of PGO function name variables that are referenced.
257 /// The global variable element in 'NameVars' is a string containing the pgo
258 /// name of a function. See `createPGOFuncNameVar` that creates these global
259 /// variables.
260 Error collectPGOFuncNameStrings(ArrayRef<GlobalVariable *> NameVars,
261                                 std::string &Result, bool doCompression = true);
262 
263 Error collectVTableStrings(ArrayRef<GlobalVariable *> VTables,
264                            std::string &Result, bool doCompression);
265 
266 /// Check if INSTR_PROF_RAW_VERSION_VAR is defined. This global is only being
267 /// set in IR PGO compilation.
268 bool isIRPGOFlagSet(const Module *M);
269 
270 /// Check if we can safely rename this Comdat function. Instances of the same
271 /// comdat function may have different control flows thus can not share the
272 /// same counter variable.
273 bool canRenameComdatFunc(const Function &F, bool CheckAddressTaken = false);
274 
275 enum InstrProfValueKind : uint32_t {
276 #define VALUE_PROF_KIND(Enumerator, Value, Descr) Enumerator = Value,
277 #include "llvm/ProfileData/InstrProfData.inc"
278 };
279 
280 /// Get the value profile data for value site \p SiteIdx from \p InstrProfR
281 /// and annotate the instruction \p Inst with the value profile meta data.
282 /// Annotate up to \p MaxMDCount (default 3) number of records per value site.
283 void annotateValueSite(Module &M, Instruction &Inst,
284                        const InstrProfRecord &InstrProfR,
285                        InstrProfValueKind ValueKind, uint32_t SiteIndx,
286                        uint32_t MaxMDCount = 3);
287 
288 /// Same as the above interface but using an ArrayRef, as well as \p Sum.
289 /// This function will not annotate !prof metadata on the instruction if the
290 /// referenced array is empty.
291 void annotateValueSite(Module &M, Instruction &Inst,
292                        ArrayRef<InstrProfValueData> VDs, uint64_t Sum,
293                        InstrProfValueKind ValueKind, uint32_t MaxMDCount);
294 
295 // TODO: Unify metadata name 'PGOFuncName' and 'PGOName', by supporting read
296 // of this metadata for backward compatibility and generating 'PGOName' only.
297 /// Extract the value profile data from \p Inst and returns them if \p Inst is
298 /// annotated with value profile data. Returns an empty vector otherwise.
299 SmallVector<InstrProfValueData, 4>
300 getValueProfDataFromInst(const Instruction &Inst, InstrProfValueKind ValueKind,
301                          uint32_t MaxNumValueData, uint64_t &TotalC,
302                          bool GetNoICPValue = false);
303 
304 inline StringRef getPGOFuncNameMetadataName() { return "PGOFuncName"; }
305 
306 inline StringRef getPGONameMetadataName() { return "PGOName"; }
307 
308 /// Return the PGOFuncName meta data associated with a function.
309 MDNode *getPGOFuncNameMetadata(const Function &F);
310 
311 std::string getPGOName(const GlobalVariable &V, bool InLTO = false);
312 
313 /// Create the PGOFuncName meta data if PGOFuncName is different from
314 /// function's raw name. This should only apply to internal linkage functions
315 /// declared by users only.
316 /// TODO: Update all callers to 'createPGONameMetadata' and deprecate this
317 /// function.
318 void createPGOFuncNameMetadata(Function &F, StringRef PGOFuncName);
319 
320 /// Create the PGOName metadata if a global object's PGO name is different from
321 /// its mangled name. This should apply to local-linkage global objects only.
322 void createPGONameMetadata(GlobalObject &GO, StringRef PGOName);
323 
324 /// Check if we can use Comdat for profile variables. This will eliminate
325 /// the duplicated profile variables for Comdat functions.
326 bool needsComdatForCounter(const GlobalObject &GV, const Module &M);
327 
328 /// An enum describing the attributes of an instrumented profile.
329 enum class InstrProfKind {
330   Unknown = 0x0,
331   // A frontend clang profile, incompatible with other attrs.
332   FrontendInstrumentation = 0x1,
333   // An IR-level profile (default when -fprofile-generate is used).
334   IRInstrumentation = 0x2,
335   // A profile with entry basic block instrumentation.
336   FunctionEntryInstrumentation = 0x4,
337   // A context sensitive IR-level profile.
338   ContextSensitive = 0x8,
339   // Use single byte probes for coverage.
340   SingleByteCoverage = 0x10,
341   // Only instrument the function entry basic block.
342   FunctionEntryOnly = 0x20,
343   // A memory profile collected using -fprofile=memory.
344   MemProf = 0x40,
345   // A temporal profile.
346   TemporalProfile = 0x80,
347   // A profile with loop entry basic blocks instrumentation.
348   LoopEntriesInstrumentation = 0x100,
349   LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/LoopEntriesInstrumentation)
350 };
351 
352 const std::error_category &instrprof_category();
353 
354 enum class instrprof_error {
355   success = 0,
356   eof,
357   unrecognized_format,
358   bad_magic,
359   bad_header,
360   unsupported_version,
361   unsupported_hash_type,
362   too_large,
363   truncated,
364   malformed,
365   missing_correlation_info,
366   unexpected_correlation_info,
367   unable_to_correlate_profile,
368   unknown_function,
369   invalid_prof,
370   hash_mismatch,
371   count_mismatch,
372   bitmap_mismatch,
373   counter_overflow,
374   value_site_count_mismatch,
375   compress_failed,
376   uncompress_failed,
377   empty_raw_profile,
378   zlib_unavailable,
379   raw_profile_version_mismatch,
380   counter_value_too_large,
381 };
382 
383 /// An ordered list of functions identified by their NameRef found in
384 /// INSTR_PROF_DATA
385 struct TemporalProfTraceTy {
386   std::vector<uint64_t> FunctionNameRefs;
387   uint64_t Weight;
388   TemporalProfTraceTy(std::initializer_list<uint64_t> Trace = {},
389                       uint64_t Weight = 1)
390       : FunctionNameRefs(Trace), Weight(Weight) {}
391 
392   /// Use a set of temporal profile traces to create a list of balanced
393   /// partitioning function nodes used by BalancedPartitioning to generate a
394   /// function order that reduces page faults during startup
395   static void createBPFunctionNodes(ArrayRef<TemporalProfTraceTy> Traces,
396                                     std::vector<BPFunctionNode> &Nodes,
397                                     bool RemoveOutlierUNs = true);
398 };
399 
400 inline std::error_code make_error_code(instrprof_error E) {
401   return std::error_code(static_cast<int>(E), instrprof_category());
402 }
403 
404 class InstrProfError : public ErrorInfo<InstrProfError> {
405 public:
406   InstrProfError(instrprof_error Err, const Twine &ErrStr = Twine())
407       : Err(Err), Msg(ErrStr.str()) {
408     assert(Err != instrprof_error::success && "Not an error");
409   }
410 
411   std::string message() const override;
412 
413   void log(raw_ostream &OS) const override { OS << message(); }
414 
415   std::error_code convertToErrorCode() const override {
416     return make_error_code(Err);
417   }
418 
419   instrprof_error get() const { return Err; }
420   const std::string &getMessage() const { return Msg; }
421 
422   /// Consume an Error and return the raw enum value contained within it, and
423   /// the optional error message. The Error must either be a success value, or
424   /// contain a single InstrProfError.
425   static std::pair<instrprof_error, std::string> take(Error E) {
426     auto Err = instrprof_error::success;
427     std::string Msg = "";
428     handleAllErrors(std::move(E), [&Err, &Msg](const InstrProfError &IPE) {
429       assert(Err == instrprof_error::success && "Multiple errors encountered");
430       Err = IPE.get();
431       Msg = IPE.getMessage();
432     });
433     return {Err, Msg};
434   }
435 
436   static char ID;
437 
438 private:
439   instrprof_error Err;
440   std::string Msg;
441 };
442 
443 namespace object {
444 
445 class SectionRef;
446 
447 } // end namespace object
448 
449 namespace IndexedInstrProf {
450 
451 uint64_t ComputeHash(StringRef K);
452 
453 } // end namespace IndexedInstrProf
454 
455 /// A symbol table used for function [IR]PGO name look-up with keys
456 /// (such as pointers, md5hash values) to the function. A function's
457 /// [IR]PGO name or name's md5hash are used in retrieving the profile
458 /// data of the function. See \c getIRPGOFuncName() and \c getPGOFuncName
459 /// methods for details how [IR]PGO name is formed.
460 class InstrProfSymtab {
461 public:
462   using AddrHashMap = std::vector<std::pair<uint64_t, uint64_t>>;
463 
464 private:
465   using AddrIntervalMap =
466       IntervalMap<uint64_t, uint64_t, 4, IntervalMapHalfOpenInfo<uint64_t>>;
467   StringRef Data;
468   uint64_t Address = 0;
469   // Unique name strings. Used to ensure entries in MD5NameMap (a vector that's
470   // going to be sorted) has unique MD5 keys in the first place.
471   StringSet<> NameTab;
472   // Records the unique virtual table names. This is used by InstrProfWriter to
473   // write out an on-disk chained hash table of virtual table names.
474   // InstrProfWriter stores per function profile data (keyed by function names)
475   // so it doesn't use a StringSet for function names.
476   StringSet<> VTableNames;
477   // A map from MD5 keys to function name strings.
478   std::vector<std::pair<uint64_t, StringRef>> MD5NameMap;
479   // A map from MD5 keys to function define. We only populate this map
480   // when build the Symtab from a Module.
481   std::vector<std::pair<uint64_t, Function *>> MD5FuncMap;
482   // A map from MD5 to the global variable. This map is only populated when
483   // building the symtab from a module. Use separate container instances for
484   // `MD5FuncMap` and `MD5VTableMap`.
485   // TODO: Unify the container type and the lambda function 'mapName' inside
486   // add{Func,VTable}WithName.
487   DenseMap<uint64_t, GlobalVariable *> MD5VTableMap;
488   // A map from function runtime address to function name MD5 hash.
489   // This map is only populated and used by raw instr profile reader.
490   AddrHashMap AddrToMD5Map;
491 
492   AddrIntervalMap::Allocator VTableAddrMapAllocator;
493   // This map is only populated and used by raw instr profile reader.
494   AddrIntervalMap VTableAddrMap;
495   bool Sorted = false;
496 
497   static StringRef getExternalSymbol() { return "** External Symbol **"; }
498 
499   // Returns the canonial name of the given PGOName. In a canonical name, all
500   // suffixes that begins with "." except ".__uniq." are stripped.
501   // FIXME: Unify this with `FunctionSamples::getCanonicalFnName`.
502   static StringRef getCanonicalName(StringRef PGOName);
503 
504   // Add the function into the symbol table, by creating the following
505   // map entries:
506   // name-set = {PGOFuncName} union {getCanonicalName(PGOFuncName)}
507   // - In MD5NameMap: <MD5Hash(name), name> for name in name-set
508   // - In MD5FuncMap: <MD5Hash(name), &F> for name in name-set
509   // The canonical name is only added if \c AddCanonical is true.
510   Error addFuncWithName(Function &F, StringRef PGOFuncName, bool AddCanonical);
511 
512   // Add the vtable into the symbol table, by creating the following
513   // map entries:
514   // name-set = {PGOName} union {getCanonicalName(PGOName)}
515   // - In MD5NameMap:  <MD5Hash(name), name> for name in name-set
516   // - In MD5VTableMap: <MD5Hash(name), name> for name in name-set
517   Error addVTableWithName(GlobalVariable &V, StringRef PGOVTableName);
518 
519   // If the symtab is created by a series of calls to \c addFuncName, \c
520   // finalizeSymtab needs to be called before looking up function names.
521   // This is required because the underlying map is a vector (for space
522   // efficiency) which needs to be sorted.
523   inline void finalizeSymtab();
524 
525 public:
526   InstrProfSymtab() : VTableAddrMap(VTableAddrMapAllocator) {}
527 
528   // Not copyable or movable.
529   // Consider std::unique_ptr for move.
530   InstrProfSymtab(const InstrProfSymtab &) = delete;
531   InstrProfSymtab &operator=(const InstrProfSymtab &) = delete;
532   InstrProfSymtab(InstrProfSymtab &&) = delete;
533   InstrProfSymtab &operator=(InstrProfSymtab &&) = delete;
534 
535   /// Create InstrProfSymtab from an object file section which
536   /// contains function PGO names. When section may contain raw
537   /// string data or string data in compressed form. This method
538   /// only initialize the symtab with reference to the data and
539   /// the section base address. The decompression will be delayed
540   /// until before it is used. See also \c create(StringRef) method.
541   Error create(object::SectionRef &Section);
542 
543   /// \c NameStrings is a string composed of one of more sub-strings
544   ///  encoded in the format described in \c collectPGOFuncNameStrings.
545   /// This method is a wrapper to \c readAndDecodeStrings method.
546   Error create(StringRef NameStrings);
547 
548   /// Initialize symtab states with function names and vtable names. \c
549   /// FuncNameStrings is a string composed of one or more encoded function name
550   /// strings, and \c VTableNameStrings composes of one or more encoded vtable
551   /// names. This interface is solely used by raw profile reader.
552   Error create(StringRef FuncNameStrings, StringRef VTableNameStrings);
553 
554   /// Initialize 'this' with the set of vtable names encoded in
555   /// \c CompressedVTableNames.
556   Error initVTableNamesFromCompressedStrings(StringRef CompressedVTableNames);
557 
558   /// This interface is used by reader of CoverageMapping test
559   /// format.
560   inline Error create(StringRef D, uint64_t BaseAddr);
561 
562   /// A wrapper interface to populate the PGO symtab with functions
563   /// decls from module \c M. This interface is used by transformation
564   /// passes such as indirect function call promotion. Variable \c InLTO
565   /// indicates if this is called from LTO optimization passes.
566   /// A canonical name, removing non-__uniq suffixes, is added if
567   /// \c AddCanonical is true.
568   Error create(Module &M, bool InLTO = false, bool AddCanonical = true);
569 
570   /// Create InstrProfSymtab from a set of names iteratable from
571   /// \p IterRange. This interface is used by IndexedProfReader.
572   template <typename NameIterRange>
573   Error create(const NameIterRange &IterRange);
574 
575   /// Create InstrProfSymtab from a set of function names and vtable
576   /// names iteratable from \p IterRange. This interface is used by
577   /// IndexedProfReader.
578   template <typename FuncNameIterRange, typename VTableNameIterRange>
579   Error create(const FuncNameIterRange &FuncIterRange,
580                const VTableNameIterRange &VTableIterRange);
581 
582   // Map the MD5 of the symbol name to the name.
583   Error addSymbolName(StringRef SymbolName) {
584     if (SymbolName.empty())
585       return make_error<InstrProfError>(instrprof_error::malformed,
586                                         "symbol name is empty");
587 
588     // Insert into NameTab so that MD5NameMap (a vector that will be sorted)
589     // won't have duplicated entries in the first place.
590     auto Ins = NameTab.insert(SymbolName);
591     if (Ins.second) {
592       MD5NameMap.push_back(std::make_pair(
593           IndexedInstrProf::ComputeHash(SymbolName), Ins.first->getKey()));
594       Sorted = false;
595     }
596     return Error::success();
597   }
598 
599   /// The method name is kept since there are many callers.
600   /// It just forwards to 'addSymbolName'.
601   Error addFuncName(StringRef FuncName) { return addSymbolName(FuncName); }
602 
603   /// Adds VTableName as a known symbol, and inserts it to a map that
604   /// tracks all vtable names.
605   Error addVTableName(StringRef VTableName) {
606     if (Error E = addSymbolName(VTableName))
607       return E;
608 
609     // Record VTableName. InstrProfWriter uses this set. The comment around
610     // class member explains why.
611     VTableNames.insert(VTableName);
612     return Error::success();
613   }
614 
615   const StringSet<> &getVTableNames() const { return VTableNames; }
616 
617   /// Map a function address to its name's MD5 hash. This interface
618   /// is only used by the raw profiler reader.
619   void mapAddress(uint64_t Addr, uint64_t MD5Val) {
620     AddrToMD5Map.push_back(std::make_pair(Addr, MD5Val));
621   }
622 
623   /// Map the address range (i.e., [start_address, end_address)) of a variable
624   /// to  its names' MD5 hash. This interface is only used by the raw profile
625   /// reader.
626   void mapVTableAddress(uint64_t StartAddr, uint64_t EndAddr, uint64_t MD5Val) {
627     VTableAddrMap.insert(StartAddr, EndAddr, MD5Val);
628   }
629 
630   /// Return a function's hash, or 0, if the function isn't in this SymTab.
631   uint64_t getFunctionHashFromAddress(uint64_t Address);
632 
633   /// Return a vtable's hash, or 0 if the vtable doesn't exist in this SymTab.
634   uint64_t getVTableHashFromAddress(uint64_t Address);
635 
636   /// Return function's PGO name from the function name's symbol
637   /// address in the object file. If an error occurs, return
638   /// an empty string.
639   StringRef getFuncName(uint64_t FuncNameAddress, size_t NameSize);
640 
641   /// Return name of functions or global variables from the name's md5 hash
642   /// value. If not found, return an empty string.
643   inline StringRef getFuncOrVarName(uint64_t ValMD5Hash);
644 
645   /// Just like getFuncOrVarName, except that it will return literal string
646   /// 'External Symbol' if the function or global variable is external to
647   /// this symbol table.
648   inline StringRef getFuncOrVarNameIfDefined(uint64_t ValMD5Hash);
649 
650   /// True if Symbol is the value used to represent external symbols.
651   static bool isExternalSymbol(const StringRef &Symbol) {
652     return Symbol == InstrProfSymtab::getExternalSymbol();
653   }
654 
655   /// Return function from the name's md5 hash. Return nullptr if not found.
656   inline Function *getFunction(uint64_t FuncMD5Hash);
657 
658   /// Return the global variable corresponding to md5 hash. Return nullptr if
659   /// not found.
660   inline GlobalVariable *getGlobalVariable(uint64_t MD5Hash);
661 
662   /// Return the name section data.
663   inline StringRef getNameData() const { return Data; }
664 
665   /// Dump the symbols in this table.
666   void dumpNames(raw_ostream &OS) const;
667 };
668 
669 Error InstrProfSymtab::create(StringRef D, uint64_t BaseAddr) {
670   Data = D;
671   Address = BaseAddr;
672   return Error::success();
673 }
674 
675 template <typename NameIterRange>
676 Error InstrProfSymtab::create(const NameIterRange &IterRange) {
677   for (auto Name : IterRange)
678     if (Error E = addFuncName(Name))
679       return E;
680 
681   finalizeSymtab();
682   return Error::success();
683 }
684 
685 template <typename FuncNameIterRange, typename VTableNameIterRange>
686 Error InstrProfSymtab::create(const FuncNameIterRange &FuncIterRange,
687                               const VTableNameIterRange &VTableIterRange) {
688   // Iterate elements by StringRef rather than by const reference.
689   // StringRef is small enough, so the loop is efficient whether
690   // element in the range is std::string or StringRef.
691   for (StringRef Name : FuncIterRange)
692     if (Error E = addFuncName(Name))
693       return E;
694 
695   for (StringRef VTableName : VTableIterRange)
696     if (Error E = addVTableName(VTableName))
697       return E;
698 
699   finalizeSymtab();
700   return Error::success();
701 }
702 
703 void InstrProfSymtab::finalizeSymtab() {
704   if (Sorted)
705     return;
706   llvm::sort(MD5NameMap, less_first());
707   llvm::sort(MD5FuncMap, less_first());
708   llvm::sort(AddrToMD5Map, less_first());
709   AddrToMD5Map.erase(llvm::unique(AddrToMD5Map), AddrToMD5Map.end());
710   Sorted = true;
711 }
712 
713 StringRef InstrProfSymtab::getFuncOrVarNameIfDefined(uint64_t MD5Hash) {
714   StringRef ret = getFuncOrVarName(MD5Hash);
715   if (ret.empty())
716     return InstrProfSymtab::getExternalSymbol();
717   return ret;
718 }
719 
720 StringRef InstrProfSymtab::getFuncOrVarName(uint64_t MD5Hash) {
721   finalizeSymtab();
722   auto Result = llvm::lower_bound(MD5NameMap, MD5Hash,
723                                   [](const std::pair<uint64_t, StringRef> &LHS,
724                                      uint64_t RHS) { return LHS.first < RHS; });
725   if (Result != MD5NameMap.end() && Result->first == MD5Hash)
726     return Result->second;
727   return StringRef();
728 }
729 
730 Function* InstrProfSymtab::getFunction(uint64_t FuncMD5Hash) {
731   finalizeSymtab();
732   auto Result = llvm::lower_bound(MD5FuncMap, FuncMD5Hash,
733                                   [](const std::pair<uint64_t, Function *> &LHS,
734                                      uint64_t RHS) { return LHS.first < RHS; });
735   if (Result != MD5FuncMap.end() && Result->first == FuncMD5Hash)
736     return Result->second;
737   return nullptr;
738 }
739 
740 GlobalVariable *InstrProfSymtab::getGlobalVariable(uint64_t MD5Hash) {
741   return MD5VTableMap.lookup(MD5Hash);
742 }
743 
744 // To store the sums of profile count values, or the percentage of
745 // the sums of the total count values.
746 struct CountSumOrPercent {
747   uint64_t NumEntries = 0;
748   double CountSum = 0.0f;
749   std::array<double, IPVK_Last - IPVK_First + 1> ValueCounts = {};
750   CountSumOrPercent() = default;
751   void reset() {
752     NumEntries = 0;
753     CountSum = 0.0f;
754     ValueCounts.fill(0.0f);
755   }
756 };
757 
758 // Function level or program level overlap information.
759 struct OverlapStats {
760   enum OverlapStatsLevel { ProgramLevel, FunctionLevel };
761   // Sum of the total count values for the base profile.
762   CountSumOrPercent Base;
763   // Sum of the total count values for the test profile.
764   CountSumOrPercent Test;
765   // Overlap lap score. Should be in range of [0.0f to 1.0f].
766   CountSumOrPercent Overlap;
767   CountSumOrPercent Mismatch;
768   CountSumOrPercent Unique;
769   OverlapStatsLevel Level;
770   const std::string *BaseFilename = nullptr;
771   const std::string *TestFilename = nullptr;
772   StringRef FuncName;
773   uint64_t FuncHash = 0;
774   bool Valid = false;
775 
776   OverlapStats(OverlapStatsLevel L = ProgramLevel) : Level(L) {}
777 
778   void dump(raw_fd_ostream &OS) const;
779 
780   void setFuncInfo(StringRef Name, uint64_t Hash) {
781     FuncName = Name;
782     FuncHash = Hash;
783   }
784 
785   Error accumulateCounts(const std::string &BaseFilename,
786                          const std::string &TestFilename, bool IsCS);
787   void addOneMismatch(const CountSumOrPercent &MismatchFunc);
788   void addOneUnique(const CountSumOrPercent &UniqueFunc);
789 
790   static inline double score(uint64_t Val1, uint64_t Val2, double Sum1,
791                              double Sum2) {
792     if (Sum1 < 1.0f || Sum2 < 1.0f)
793       return 0.0f;
794     return std::min(Val1 / Sum1, Val2 / Sum2);
795   }
796 };
797 
798 // This is used to filter the functions whose overlap information
799 // to be output.
800 struct OverlapFuncFilters {
801   uint64_t ValueCutoff;
802   const std::string NameFilter;
803 };
804 
805 struct InstrProfValueSiteRecord {
806   /// Value profiling data pairs at a given value site.
807   std::vector<InstrProfValueData> ValueData;
808 
809   InstrProfValueSiteRecord() = default;
810   InstrProfValueSiteRecord(std::vector<InstrProfValueData> &&VD)
811       : ValueData(VD) {}
812 
813   /// Sort ValueData ascending by Value
814   void sortByTargetValues() {
815     llvm::sort(ValueData,
816                [](const InstrProfValueData &L, const InstrProfValueData &R) {
817                  return L.Value < R.Value;
818                });
819   }
820   /// Sort ValueData Descending by Count
821   inline void sortByCount();
822 
823   /// Merge data from another InstrProfValueSiteRecord
824   /// Optionally scale merged counts by \p Weight.
825   void merge(InstrProfValueSiteRecord &Input, uint64_t Weight,
826              function_ref<void(instrprof_error)> Warn);
827   /// Scale up value profile data counts by N (Numerator) / D (Denominator).
828   void scale(uint64_t N, uint64_t D, function_ref<void(instrprof_error)> Warn);
829 
830   /// Compute the overlap b/w this record and Input record.
831   void overlap(InstrProfValueSiteRecord &Input, uint32_t ValueKind,
832                OverlapStats &Overlap, OverlapStats &FuncLevelOverlap);
833 };
834 
835 /// Profiling information for a single function.
836 struct InstrProfRecord {
837   std::vector<uint64_t> Counts;
838   std::vector<uint8_t> BitmapBytes;
839 
840   InstrProfRecord() = default;
841   InstrProfRecord(std::vector<uint64_t> Counts) : Counts(std::move(Counts)) {}
842   InstrProfRecord(std::vector<uint64_t> Counts,
843                   std::vector<uint8_t> BitmapBytes)
844       : Counts(std::move(Counts)), BitmapBytes(std::move(BitmapBytes)) {}
845   InstrProfRecord(InstrProfRecord &&) = default;
846   InstrProfRecord(const InstrProfRecord &RHS)
847       : Counts(RHS.Counts), BitmapBytes(RHS.BitmapBytes),
848         ValueData(RHS.ValueData
849                       ? std::make_unique<ValueProfData>(*RHS.ValueData)
850                       : nullptr) {}
851   InstrProfRecord &operator=(InstrProfRecord &&) = default;
852   InstrProfRecord &operator=(const InstrProfRecord &RHS) {
853     Counts = RHS.Counts;
854     BitmapBytes = RHS.BitmapBytes;
855     if (!RHS.ValueData) {
856       ValueData = nullptr;
857       return *this;
858     }
859     if (!ValueData)
860       ValueData = std::make_unique<ValueProfData>(*RHS.ValueData);
861     else
862       *ValueData = *RHS.ValueData;
863     return *this;
864   }
865 
866   /// Return the number of value profile kinds with non-zero number
867   /// of profile sites.
868   inline uint32_t getNumValueKinds() const;
869   /// Return the number of instrumented sites for ValueKind.
870   inline uint32_t getNumValueSites(uint32_t ValueKind) const;
871 
872   /// Return the total number of ValueData for ValueKind.
873   inline uint32_t getNumValueData(uint32_t ValueKind) const;
874 
875   /// Return the array of profiled values at \p Site.
876   inline ArrayRef<InstrProfValueData> getValueArrayForSite(uint32_t ValueKind,
877                                                            uint32_t Site) const;
878 
879   /// Reserve space for NumValueSites sites.
880   inline void reserveSites(uint32_t ValueKind, uint32_t NumValueSites);
881 
882   /// Add ValueData for ValueKind at value Site.  We do not support adding sites
883   /// out of order.  Site must go up from 0 one by one.
884   void addValueData(uint32_t ValueKind, uint32_t Site,
885                     ArrayRef<InstrProfValueData> VData,
886                     InstrProfSymtab *SymTab);
887 
888   /// Merge the counts in \p Other into this one.
889   /// Optionally scale merged counts by \p Weight.
890   void merge(InstrProfRecord &Other, uint64_t Weight,
891              function_ref<void(instrprof_error)> Warn);
892 
893   /// Scale up profile counts (including value profile data) by
894   /// a factor of (N / D).
895   void scale(uint64_t N, uint64_t D, function_ref<void(instrprof_error)> Warn);
896 
897   /// Sort value profile data (per site) by count.
898   void sortValueData() {
899     for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
900       for (auto &SR : getValueSitesForKind(Kind))
901         SR.sortByCount();
902   }
903 
904   /// Clear value data entries and edge counters.
905   void Clear() {
906     Counts.clear();
907     clearValueData();
908   }
909 
910   /// Clear value data entries
911   void clearValueData() { ValueData = nullptr; }
912 
913   /// Compute the sums of all counts and store in Sum.
914   void accumulateCounts(CountSumOrPercent &Sum) const;
915 
916   /// Compute the overlap b/w this IntrprofRecord and Other.
917   void overlap(InstrProfRecord &Other, OverlapStats &Overlap,
918                OverlapStats &FuncLevelOverlap, uint64_t ValueCutoff);
919 
920   /// Compute the overlap of value profile counts.
921   void overlapValueProfData(uint32_t ValueKind, InstrProfRecord &Src,
922                             OverlapStats &Overlap,
923                             OverlapStats &FuncLevelOverlap);
924 
925   enum CountPseudoKind {
926     NotPseudo = 0,
927     PseudoHot,
928     PseudoWarm,
929   };
930   enum PseudoCountVal {
931     HotFunctionVal = -1,
932     WarmFunctionVal = -2,
933   };
934   CountPseudoKind getCountPseudoKind() const {
935     uint64_t FirstCount = Counts[0];
936     if (FirstCount == (uint64_t)HotFunctionVal)
937       return PseudoHot;
938     if (FirstCount == (uint64_t)WarmFunctionVal)
939       return PseudoWarm;
940     return NotPseudo;
941   }
942   void setPseudoCount(CountPseudoKind Kind) {
943     if (Kind == PseudoHot)
944       Counts[0] = (uint64_t)HotFunctionVal;
945     else if (Kind == PseudoWarm)
946       Counts[0] = (uint64_t)WarmFunctionVal;
947   }
948 
949 private:
950   using ValueProfData = std::array<std::vector<InstrProfValueSiteRecord>,
951                                    IPVK_Last - IPVK_First + 1>;
952   std::unique_ptr<ValueProfData> ValueData;
953 
954   MutableArrayRef<InstrProfValueSiteRecord>
955   getValueSitesForKind(uint32_t ValueKind) {
956     // Cast to /add/ const (should be an implicit_cast, ideally, if that's ever
957     // implemented in LLVM) to call the const overload of this function, then
958     // cast away the constness from the result.
959     auto AR = const_cast<const InstrProfRecord *>(this)->getValueSitesForKind(
960         ValueKind);
961     return MutableArrayRef(
962         const_cast<InstrProfValueSiteRecord *>(AR.data()), AR.size());
963   }
964   ArrayRef<InstrProfValueSiteRecord>
965   getValueSitesForKind(uint32_t ValueKind) const {
966     if (!ValueData)
967       return {};
968     assert(IPVK_First <= ValueKind && ValueKind <= IPVK_Last &&
969            "Unknown value kind!");
970     return (*ValueData)[ValueKind - IPVK_First];
971   }
972 
973   std::vector<InstrProfValueSiteRecord> &
974   getOrCreateValueSitesForKind(uint32_t ValueKind) {
975     if (!ValueData)
976       ValueData = std::make_unique<ValueProfData>();
977     assert(IPVK_First <= ValueKind && ValueKind <= IPVK_Last &&
978            "Unknown value kind!");
979     return (*ValueData)[ValueKind - IPVK_First];
980   }
981 
982   // Map indirect call target name hash to name string.
983   uint64_t remapValue(uint64_t Value, uint32_t ValueKind,
984                       InstrProfSymtab *SymTab);
985 
986   // Merge Value Profile data from Src record to this record for ValueKind.
987   // Scale merged value counts by \p Weight.
988   void mergeValueProfData(uint32_t ValkeKind, InstrProfRecord &Src,
989                           uint64_t Weight,
990                           function_ref<void(instrprof_error)> Warn);
991 
992   // Scale up value profile data count by N (Numerator) / D (Denominator).
993   void scaleValueProfData(uint32_t ValueKind, uint64_t N, uint64_t D,
994                           function_ref<void(instrprof_error)> Warn);
995 };
996 
997 struct NamedInstrProfRecord : InstrProfRecord {
998   StringRef Name;
999   uint64_t Hash;
1000 
1001   // We reserve this bit as the flag for context sensitive profile record.
1002   static const int CS_FLAG_IN_FUNC_HASH = 60;
1003 
1004   NamedInstrProfRecord() = default;
1005   NamedInstrProfRecord(StringRef Name, uint64_t Hash,
1006                        std::vector<uint64_t> Counts)
1007       : InstrProfRecord(std::move(Counts)), Name(Name), Hash(Hash) {}
1008   NamedInstrProfRecord(StringRef Name, uint64_t Hash,
1009                        std::vector<uint64_t> Counts,
1010                        std::vector<uint8_t> BitmapBytes)
1011       : InstrProfRecord(std::move(Counts), std::move(BitmapBytes)), Name(Name),
1012         Hash(Hash) {}
1013 
1014   static bool hasCSFlagInHash(uint64_t FuncHash) {
1015     return ((FuncHash >> CS_FLAG_IN_FUNC_HASH) & 1);
1016   }
1017   static void setCSFlagInHash(uint64_t &FuncHash) {
1018     FuncHash |= ((uint64_t)1 << CS_FLAG_IN_FUNC_HASH);
1019   }
1020 };
1021 
1022 uint32_t InstrProfRecord::getNumValueKinds() const {
1023   uint32_t NumValueKinds = 0;
1024   for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
1025     NumValueKinds += !(getValueSitesForKind(Kind).empty());
1026   return NumValueKinds;
1027 }
1028 
1029 uint32_t InstrProfRecord::getNumValueData(uint32_t ValueKind) const {
1030   uint32_t N = 0;
1031   for (const auto &SR : getValueSitesForKind(ValueKind))
1032     N += SR.ValueData.size();
1033   return N;
1034 }
1035 
1036 uint32_t InstrProfRecord::getNumValueSites(uint32_t ValueKind) const {
1037   return getValueSitesForKind(ValueKind).size();
1038 }
1039 
1040 ArrayRef<InstrProfValueData>
1041 InstrProfRecord::getValueArrayForSite(uint32_t ValueKind, uint32_t Site) const {
1042   return getValueSitesForKind(ValueKind)[Site].ValueData;
1043 }
1044 
1045 void InstrProfRecord::reserveSites(uint32_t ValueKind, uint32_t NumValueSites) {
1046   if (!NumValueSites)
1047     return;
1048   getOrCreateValueSitesForKind(ValueKind).reserve(NumValueSites);
1049 }
1050 
1051 // Include definitions for value profile data
1052 #define INSTR_PROF_VALUE_PROF_DATA
1053 #include "llvm/ProfileData/InstrProfData.inc"
1054 
1055 void InstrProfValueSiteRecord::sortByCount() {
1056   llvm::stable_sort(
1057       ValueData, [](const InstrProfValueData &L, const InstrProfValueData &R) {
1058         return L.Count > R.Count;
1059       });
1060   // Now truncate
1061   size_t max_s = INSTR_PROF_MAX_NUM_VAL_PER_SITE;
1062   if (ValueData.size() > max_s)
1063     ValueData.resize(max_s);
1064 }
1065 
1066 namespace IndexedInstrProf {
1067 
1068 enum class HashT : uint32_t {
1069   MD5,
1070   Last = MD5
1071 };
1072 
1073 inline uint64_t ComputeHash(HashT Type, StringRef K) {
1074   switch (Type) {
1075   case HashT::MD5:
1076     return MD5Hash(K);
1077   }
1078   llvm_unreachable("Unhandled hash type");
1079 }
1080 
1081 const uint64_t Magic = 0x8169666f72706cff; // "\xfflprofi\x81"
1082 
1083 enum ProfVersion {
1084   // Version 1 is the first version. In this version, the value of
1085   // a key/value pair can only include profile data of a single function.
1086   // Due to this restriction, the number of block counters for a given
1087   // function is not recorded but derived from the length of the value.
1088   Version1 = 1,
1089   // The version 2 format supports recording profile data of multiple
1090   // functions which share the same key in one value field. To support this,
1091   // the number block counters is recorded as an uint64_t field right after the
1092   // function structural hash.
1093   Version2 = 2,
1094   // Version 3 supports value profile data. The value profile data is expected
1095   // to follow the block counter profile data.
1096   Version3 = 3,
1097   // In this version, profile summary data \c IndexedInstrProf::Summary is
1098   // stored after the profile header.
1099   Version4 = 4,
1100   // In this version, the frontend PGO stable hash algorithm defaults to V2.
1101   Version5 = 5,
1102   // In this version, the frontend PGO stable hash algorithm got fixed and
1103   // may produce hashes different from Version5.
1104   Version6 = 6,
1105   // An additional counter is added around logical operators.
1106   Version7 = 7,
1107   // An additional (optional) memory profile type is added.
1108   Version8 = 8,
1109   // Binary ids are added.
1110   Version9 = 9,
1111   // An additional (optional) temporal profile traces section is added.
1112   Version10 = 10,
1113   // An additional field is used for bitmap bytes.
1114   Version11 = 11,
1115   // VTable profiling, decision record and bitmap are modified for mcdc.
1116   Version12 = 12,
1117   // The current version is 12.
1118   CurrentVersion = INSTR_PROF_INDEX_VERSION
1119 };
1120 const uint64_t Version = ProfVersion::CurrentVersion;
1121 
1122 const HashT HashType = HashT::MD5;
1123 
1124 inline uint64_t ComputeHash(StringRef K) { return ComputeHash(HashType, K); }
1125 
1126 // This structure defines the file header of the LLVM profile
1127 // data file in indexed-format. Please update llvm/docs/InstrProfileFormat.rst
1128 // as appropriate when updating the indexed profile format.
1129 struct Header {
1130   uint64_t Magic = IndexedInstrProf::Magic;
1131   // The lower 32 bits specify the version of the indexed profile.
1132   // The most significant 32 bits are reserved to specify the variant types of
1133   // the profile.
1134   uint64_t Version = 0;
1135   uint64_t Unused = 0; // Becomes unused since version 4
1136   uint64_t HashType = static_cast<uint64_t>(IndexedInstrProf::HashType);
1137   // This field records the offset of this hash table's metadata (i.e., the
1138   // number of buckets and entries), which follows right after the payload of
1139   // the entire hash table.
1140   uint64_t HashOffset = 0;
1141   uint64_t MemProfOffset = 0;
1142   uint64_t BinaryIdOffset = 0;
1143   uint64_t TemporalProfTracesOffset = 0;
1144   uint64_t VTableNamesOffset = 0;
1145   // New fields should only be added at the end to ensure that the size
1146   // computation is correct. The methods below need to be updated to ensure that
1147   // the new field is read correctly.
1148 
1149   // Reads a header struct from the buffer. Header fields are in machine native
1150   // endianness.
1151   static Expected<Header> readFromBuffer(const unsigned char *Buffer);
1152 
1153   // Returns the size of the header in bytes for all valid fields based on the
1154   // version. I.e a older version header will return a smaller size.
1155   size_t size() const;
1156 
1157   // Return the indexed profile version, i.e., the least significant 32 bits
1158   // in Header.Version.
1159   uint64_t getIndexedProfileVersion() const;
1160 };
1161 
1162 // Profile summary data recorded in the profile data file in indexed
1163 // format. It is introduced in version 4. The summary data follows
1164 // right after the profile file header.
1165 struct Summary {
1166   struct Entry {
1167     uint64_t Cutoff; ///< The required percentile of total execution count.
1168     uint64_t
1169         MinBlockCount;  ///< The minimum execution count for this percentile.
1170     uint64_t NumBlocks; ///< Number of blocks >= the minumum execution count.
1171   };
1172   // The field kind enumerator to assigned value mapping should remain
1173   // unchanged  when a new kind is added or an old kind gets deleted in
1174   // the future.
1175   enum SummaryFieldKind {
1176     /// The total number of functions instrumented.
1177     TotalNumFunctions = 0,
1178     /// Total number of instrumented blocks/edges.
1179     TotalNumBlocks = 1,
1180     /// The maximal execution count among all functions.
1181     /// This field does not exist for profile data from IR based
1182     /// instrumentation.
1183     MaxFunctionCount = 2,
1184     /// Max block count of the program.
1185     MaxBlockCount = 3,
1186     /// Max internal block count of the program (excluding entry blocks).
1187     MaxInternalBlockCount = 4,
1188     /// The sum of all instrumented block counts.
1189     TotalBlockCount = 5,
1190     NumKinds = TotalBlockCount + 1
1191   };
1192 
1193   // The number of summmary fields following the summary header.
1194   uint64_t NumSummaryFields;
1195   // The number of Cutoff Entries (Summary::Entry) following summary fields.
1196   uint64_t NumCutoffEntries;
1197 
1198   Summary() = delete;
1199   Summary(uint32_t Size) { memset(this, 0, Size); }
1200 
1201   void operator delete(void *ptr) { ::operator delete(ptr); }
1202 
1203   static uint32_t getSize(uint32_t NumSumFields, uint32_t NumCutoffEntries) {
1204     return sizeof(Summary) + NumCutoffEntries * sizeof(Entry) +
1205            NumSumFields * sizeof(uint64_t);
1206   }
1207 
1208   const uint64_t *getSummaryDataBase() const {
1209     return reinterpret_cast<const uint64_t *>(this + 1);
1210   }
1211 
1212   uint64_t *getSummaryDataBase() {
1213     return reinterpret_cast<uint64_t *>(this + 1);
1214   }
1215 
1216   const Entry *getCutoffEntryBase() const {
1217     return reinterpret_cast<const Entry *>(
1218         &getSummaryDataBase()[NumSummaryFields]);
1219   }
1220 
1221   Entry *getCutoffEntryBase() {
1222     return reinterpret_cast<Entry *>(&getSummaryDataBase()[NumSummaryFields]);
1223   }
1224 
1225   uint64_t get(SummaryFieldKind K) const {
1226     return getSummaryDataBase()[K];
1227   }
1228 
1229   void set(SummaryFieldKind K, uint64_t V) {
1230     getSummaryDataBase()[K] = V;
1231   }
1232 
1233   const Entry &getEntry(uint32_t I) const { return getCutoffEntryBase()[I]; }
1234 
1235   void setEntry(uint32_t I, const ProfileSummaryEntry &E) {
1236     Entry &ER = getCutoffEntryBase()[I];
1237     ER.Cutoff = E.Cutoff;
1238     ER.MinBlockCount = E.MinCount;
1239     ER.NumBlocks = E.NumCounts;
1240   }
1241 };
1242 
1243 inline std::unique_ptr<Summary> allocSummary(uint32_t TotalSize) {
1244   return std::unique_ptr<Summary>(new (::operator new(TotalSize))
1245                                       Summary(TotalSize));
1246 }
1247 
1248 } // end namespace IndexedInstrProf
1249 
1250 namespace RawInstrProf {
1251 
1252 // Version 1: First version
1253 // Version 2: Added value profile data section. Per-function control data
1254 // struct has more fields to describe value profile information.
1255 // Version 3: Compressed name section support. Function PGO name reference
1256 // from control data struct is changed from raw pointer to Name's MD5 value.
1257 // Version 4: ValueDataBegin and ValueDataSizes fields are removed from the
1258 // raw header.
1259 // Version 5: Bit 60 of FuncHash is reserved for the flag for the context
1260 // sensitive records.
1261 // Version 6: Added binary id.
1262 // Version 7: Reorder binary id and include version in signature.
1263 // Version 8: Use relative counter pointer.
1264 // Version 9: Added relative bitmap bytes pointer and count used by MC/DC.
1265 // Version 10: Added vtable, a new type of value profile data.
1266 const uint64_t Version = INSTR_PROF_RAW_VERSION;
1267 
1268 template <class IntPtrT> inline uint64_t getMagic();
1269 template <> inline uint64_t getMagic<uint64_t>() {
1270   return INSTR_PROF_RAW_MAGIC_64;
1271 }
1272 
1273 template <> inline uint64_t getMagic<uint32_t>() {
1274   return INSTR_PROF_RAW_MAGIC_32;
1275 }
1276 
1277 // Per-function profile data header/control structure.
1278 // The definition should match the structure defined in
1279 // compiler-rt/lib/profile/InstrProfiling.h.
1280 // It should also match the synthesized type in
1281 // Transforms/Instrumentation/InstrProfiling.cpp:getOrCreateRegionCounters.
1282 template <class IntPtrT> struct alignas(8) ProfileData {
1283 #define INSTR_PROF_DATA(Type, LLVMType, Name, Init) Type Name;
1284 #include "llvm/ProfileData/InstrProfData.inc"
1285 };
1286 
1287 template <class IntPtrT> struct alignas(8) VTableProfileData {
1288 #define INSTR_PROF_VTABLE_DATA(Type, LLVMType, Name, Init) Type Name;
1289 #include "llvm/ProfileData/InstrProfData.inc"
1290 };
1291 
1292 // File header structure of the LLVM profile data in raw format.
1293 // The definition should match the header referenced in
1294 // compiler-rt/lib/profile/InstrProfilingFile.c  and
1295 // InstrProfilingBuffer.c.
1296 struct Header {
1297 #define INSTR_PROF_RAW_HEADER(Type, Name, Init) const Type Name;
1298 #include "llvm/ProfileData/InstrProfData.inc"
1299 };
1300 
1301 } // end namespace RawInstrProf
1302 
1303 // Create the variable for the profile file name.
1304 void createProfileFileNameVar(Module &M, StringRef InstrProfileOutput);
1305 
1306 // Whether to compress function names in profile records, and filenames in
1307 // code coverage mappings. Used by the Instrumentation library and unit tests.
1308 extern cl::opt<bool> DoInstrProfNameCompression;
1309 
1310 } // end namespace llvm
1311 #endif // LLVM_PROFILEDATA_INSTRPROF_H
1312