blob: 78635ec4386c3e873cd89082554624b9dc63ea6e [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- ProfileSummary.h - Profile summary data structure. -------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
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
20namespace llvm {
21
22class LLVMContext;
23class 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.
31struct 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
41using SummaryEntryVector = std::vector<ProfileSummaryEntry>;
42
43class ProfileSummary {
44public:
Andrew Walbran3d2c1972020-04-07 12:24:26 +010045 enum Kind { PSK_Instr, PSK_CSInstr, PSK_Sample };
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010046
47private:
48 const Kind PSK;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010049 SummaryEntryVector DetailedSummary;
50 uint64_t TotalCount, MaxCount, MaxInternalCount, MaxFunctionCount;
51 uint32_t NumCounts, NumFunctions;
Andrew Scullcdfcccc2018-10-05 20:58:37 +010052 /// Return detailed summary as metadata.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010053 Metadata *getDetailedSummaryMD(LLVMContext &Context);
54
55public:
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; }
Andrew Scullcdfcccc2018-10-05 20:58:37 +010068 /// Return summary information as metadata.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010069 Metadata *getMD(LLVMContext &Context);
Andrew Scullcdfcccc2018-10-05 20:58:37 +010070 /// Construct profile summary from metdata.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010071 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