blob: f1d49cc16630e0b926e55b2690b33dab980a7503 [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:
45 enum Kind { PSK_Instr, PSK_Sample };
46
47private:
48 const Kind PSK;
49 static const char *KindStr[2];
50 SummaryEntryVector DetailedSummary;
51 uint64_t TotalCount, MaxCount, MaxInternalCount, MaxFunctionCount;
52 uint32_t NumCounts, NumFunctions;
Andrew Scullcdfcccc2018-10-05 20:58:37 +010053 /// Return detailed summary as metadata.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010054 Metadata *getDetailedSummaryMD(LLVMContext &Context);
55
56public:
57 static const int Scale = 1000000;
58
59 ProfileSummary(Kind K, SummaryEntryVector DetailedSummary,
60 uint64_t TotalCount, uint64_t MaxCount,
61 uint64_t MaxInternalCount, uint64_t MaxFunctionCount,
62 uint32_t NumCounts, uint32_t NumFunctions)
63 : PSK(K), DetailedSummary(std::move(DetailedSummary)),
64 TotalCount(TotalCount), MaxCount(MaxCount),
65 MaxInternalCount(MaxInternalCount), MaxFunctionCount(MaxFunctionCount),
66 NumCounts(NumCounts), NumFunctions(NumFunctions) {}
67
68 Kind getKind() const { return PSK; }
Andrew Scullcdfcccc2018-10-05 20:58:37 +010069 /// Return summary information as metadata.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010070 Metadata *getMD(LLVMContext &Context);
Andrew Scullcdfcccc2018-10-05 20:58:37 +010071 /// Construct profile summary from metdata.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010072 static ProfileSummary *getFromMD(Metadata *MD);
73 SummaryEntryVector &getDetailedSummary() { return DetailedSummary; }
74 uint32_t getNumFunctions() { return NumFunctions; }
75 uint64_t getMaxFunctionCount() { return MaxFunctionCount; }
76 uint32_t getNumCounts() { return NumCounts; }
77 uint64_t getTotalCount() { return TotalCount; }
78 uint64_t getMaxCount() { return MaxCount; }
79 uint64_t getMaxInternalCount() { return MaxInternalCount; }
80};
81
82} // end namespace llvm
83
84#endif // LLVM_IR_PROFILESUMMARY_H