1 //===- ProfileSummary.h - Profile summary data structure. -------*- 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 // This file defines the profile summary data structure.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_IR_PROFILESUMMARY_H
14 #define LLVM_IR_PROFILESUMMARY_H
15 
16 #include <algorithm>
17 #include <cstdint>
18 #include <vector>
19 
20 namespace llvm {
21 
22 class LLVMContext;
23 class Metadata;
24 
25 // The profile summary is one or more (Cutoff, MinCount, NumCounts) triplets.
26 // The semantics of counts depend on the type of profile. For instrumentation
27 // profile, counts are block counts and for sample profile, counts are
28 // per-line samples. Given a target counts percentile, we compute the minimum
29 // number of counts needed to reach this target and the minimum among these
30 // counts.
31 struct ProfileSummaryEntry {
32   uint32_t Cutoff;    ///< The required percentile of counts.
33   uint64_t MinCount;  ///< The minimum count for this percentile.
34   uint64_t NumCounts; ///< Number of counts >= the minimum count.
35 
36   ProfileSummaryEntry(uint32_t TheCutoff, uint64_t TheMinCount,
37                       uint64_t TheNumCounts)
38       : Cutoff(TheCutoff), MinCount(TheMinCount), NumCounts(TheNumCounts) {}
39 };
40 
41 using SummaryEntryVector = std::vector<ProfileSummaryEntry>;
42 
43 class ProfileSummary {
44 public:
45   enum Kind { PSK_Instr, PSK_CSInstr, PSK_Sample };
46 
47 private:
48   const Kind PSK;
49   SummaryEntryVector DetailedSummary;
50   uint64_t TotalCount, MaxCount, MaxInternalCount, MaxFunctionCount;
51   uint32_t NumCounts, NumFunctions;
52   /// Return detailed summary as metadata.
53   Metadata *getDetailedSummaryMD(LLVMContext &Context);
54 
55 public:
56   static const int Scale = 1000000;
57 
58   ProfileSummary(Kind K, SummaryEntryVector DetailedSummary,
59                  uint64_t TotalCount, uint64_t MaxCount,
60                  uint64_t MaxInternalCount, uint64_t MaxFunctionCount,
61                  uint32_t NumCounts, uint32_t NumFunctions)
62       : PSK(K), DetailedSummary(std::move(DetailedSummary)),
63         TotalCount(TotalCount), MaxCount(MaxCount),
64         MaxInternalCount(MaxInternalCount), MaxFunctionCount(MaxFunctionCount),
65         NumCounts(NumCounts), NumFunctions(NumFunctions) {}
66 
67   Kind getKind() const { return PSK; }
68   /// Return summary information as metadata.
69   Metadata *getMD(LLVMContext &Context);
70   /// Construct profile summary from metdata.
71   static ProfileSummary *getFromMD(Metadata *MD);
72   SummaryEntryVector &getDetailedSummary() { return DetailedSummary; }
73   uint32_t getNumFunctions() { return NumFunctions; }
74   uint64_t getMaxFunctionCount() { return MaxFunctionCount; }
75   uint32_t getNumCounts() { return NumCounts; }
76   uint64_t getTotalCount() { return TotalCount; }
77   uint64_t getMaxCount() { return MaxCount; }
78   uint64_t getMaxInternalCount() { return MaxInternalCount; }
79 };
80 
81 } // end namespace llvm
82 
83 #endif // LLVM_IR_PROFILESUMMARY_H
84