1 //===-- ProfileCommon.h - Common profiling APIs. ----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains data structures and functions common to both instrumented
11 // and sample profiling.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_PROFILEDATA_PROFILE_COMMON_H
16 #define LLVM_PROFILEDATA_PROFILE_COMMON_H
24 #include "llvm/IR/ProfileSummary.h"
25 #include "llvm/Support/Error.h"
26 #include "llvm/ADT/ArrayRef.h"
30 namespace IndexedInstrProf {
33 namespace sampleprof {
34 class FunctionSamples;
36 struct InstrProfRecord;
42 inline const char *getHotSectionPrefix() { return ".hot"; }
43 inline const char *getUnlikelySectionPrefix() { return ".unlikely"; }
45 class ProfileSummaryBuilder {
48 // We keep track of the number of times a count (block count or samples)
49 // appears in the profile. The map is kept sorted in the descending order of
51 std::map<uint64_t, uint32_t, std::greater<uint64_t>> CountFrequencies;
52 std::vector<uint32_t> DetailedSummaryCutoffs;
55 SummaryEntryVector DetailedSummary;
56 ProfileSummaryBuilder(std::vector<uint32_t> Cutoffs)
57 : DetailedSummaryCutoffs(std::move(Cutoffs)), TotalCount(0), MaxCount(0),
58 MaxFunctionCount(0), NumCounts(0), NumFunctions(0) {}
59 inline void addCount(uint64_t Count);
60 ~ProfileSummaryBuilder() = default;
61 void computeDetailedSummary();
62 uint64_t TotalCount, MaxCount, MaxFunctionCount;
63 uint32_t NumCounts, NumFunctions;
66 /// \brief A vector of useful cutoff values for detailed summary.
67 static const ArrayRef<uint32_t> DefaultCutoffs;
70 class InstrProfSummaryBuilder final : public ProfileSummaryBuilder {
71 uint64_t MaxInternalBlockCount;
72 inline void addEntryCount(uint64_t Count);
73 inline void addInternalCount(uint64_t Count);
76 InstrProfSummaryBuilder(std::vector<uint32_t> Cutoffs)
77 : ProfileSummaryBuilder(std::move(Cutoffs)), MaxInternalBlockCount(0) {}
78 void addRecord(const InstrProfRecord &);
79 std::unique_ptr<ProfileSummary> getSummary();
82 class SampleProfileSummaryBuilder final : public ProfileSummaryBuilder {
85 void addRecord(const sampleprof::FunctionSamples &FS);
86 SampleProfileSummaryBuilder(std::vector<uint32_t> Cutoffs)
87 : ProfileSummaryBuilder(std::move(Cutoffs)) {}
88 std::unique_ptr<ProfileSummary> getSummary();
91 // This is called when a count is seen in the profile.
92 void ProfileSummaryBuilder::addCount(uint64_t Count) {
97 CountFrequencies[Count]++;
101 } // end namespace llvm