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