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 <cassert>
18#include <cstdint>
19#include <vector>
20
21namespace llvm {
22
23class LLVMContext;
24class Metadata;
25class raw_ostream;
26
27// The profile summary is one or more (Cutoff, MinCount, NumCounts) triplets.
28// The semantics of counts depend on the type of profile. For instrumentation
29// profile, counts are block counts and for sample profile, counts are
30// per-line samples. Given a target counts percentile, we compute the minimum
31// number of counts needed to reach this target and the minimum among these
32// counts.
33struct ProfileSummaryEntry {
34 uint32_t Cutoff; ///< The required percentile of counts.
35 uint64_t MinCount; ///< The minimum count for this percentile.
36 uint64_t NumCounts; ///< Number of counts >= the minimum count.
37
38 ProfileSummaryEntry(uint32_t TheCutoff, uint64_t TheMinCount,
39 uint64_t TheNumCounts)
40 : Cutoff(TheCutoff), MinCount(TheMinCount), NumCounts(TheNumCounts) {}
41};
42
43using SummaryEntryVector = std::vector<ProfileSummaryEntry>;
44
45class ProfileSummary {
46public:
47 enum Kind { PSK_Instr, PSK_CSInstr, PSK_Sample };
48
49private:
50 const Kind PSK;
51 SummaryEntryVector DetailedSummary;
52 uint64_t TotalCount, MaxCount, MaxInternalCount, MaxFunctionCount;
53 uint32_t NumCounts, NumFunctions;
54 /// If 'Partial' is false, it means the profile being used to optimize
55 /// a target is collected from the same target.
56 /// If 'Partial' is true, it means the profile is for common/shared
57 /// code. The common profile is usually merged from profiles collected
58 /// from running other targets.
59 bool Partial = false;
60 /// This approximately represents the ratio of the number of profile counters
61 /// of the program being built to the number of profile counters in the
62 /// partial sample profile. When 'Partial' is false, it is undefined. This is
63 /// currently only available under thin LTO mode.
64 double PartialProfileRatio = 0;
65 /// Return detailed summary as metadata.
66 Metadata *getDetailedSummaryMD(LLVMContext &Context);
67
68public:
69 static const int Scale = 1000000;
70
71 ProfileSummary(Kind K, SummaryEntryVector DetailedSummary,
72 uint64_t TotalCount, uint64_t MaxCount,
73 uint64_t MaxInternalCount, uint64_t MaxFunctionCount,
74 uint32_t NumCounts, uint32_t NumFunctions,
75 bool Partial = false, double PartialProfileRatio = 0)
76 : PSK(K), DetailedSummary(std::move(DetailedSummary)),
77 TotalCount(TotalCount), MaxCount(MaxCount),
78 MaxInternalCount(MaxInternalCount), MaxFunctionCount(MaxFunctionCount),
79 NumCounts(NumCounts), NumFunctions(NumFunctions), Partial(Partial),
80 PartialProfileRatio(PartialProfileRatio) {}
81
82 Kind getKind() const { return PSK; }
83 /// Return summary information as metadata.
84 Metadata *getMD(LLVMContext &Context, bool AddPartialField = true,
85 bool AddPartialProfileRatioField = true);
86 /// Construct profile summary from metdata.
87 static ProfileSummary *getFromMD(Metadata *MD);
88 SummaryEntryVector &getDetailedSummary() { return DetailedSummary; }
89 uint32_t getNumFunctions() { return NumFunctions; }
90 uint64_t getMaxFunctionCount() { return MaxFunctionCount; }
91 uint32_t getNumCounts() { return NumCounts; }
92 uint64_t getTotalCount() { return TotalCount; }
93 uint64_t getMaxCount() { return MaxCount; }
94 uint64_t getMaxInternalCount() { return MaxInternalCount; }
95 void setPartialProfile(bool PP) { Partial = PP; }
96 bool isPartialProfile() { return Partial; }
97 double getPartialProfileRatio() { return PartialProfileRatio; }
98 void setPartialProfileRatio(double R) {
99 assert(isPartialProfile() && "Unexpected when not partial profile");
100 PartialProfileRatio = R;
101 }
102 void printSummary(raw_ostream &OS);
103 void printDetailedSummary(raw_ostream &OS);
104};
105
106} // end namespace llvm
107
108#endif // LLVM_IR_PROFILESUMMARY_H
109