1 //===-- ProfileCommon.h - Common profiling APIs. ----------------*- 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 contains data structures and functions common to both instrumented
11 // and sample profiling.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_PROFILEDATA_PROFILE_COMMON_H
16 #define LLVM_PROFILEDATA_PROFILE_COMMON_H
17
18 #include <cstdint>
19 #include <functional>
20 #include <map>
21 #include <utility>
22 #include <vector>
23
24 #include "llvm/IR/ProfileSummary.h"
25 #include "llvm/Support/Error.h"
26 #include "llvm/ADT/ArrayRef.h"
27
28 namespace llvm {
29 class Function;
30 namespace IndexedInstrProf {
31 struct Summary;
32 }
33 namespace sampleprof {
34 class FunctionSamples;
35 }
36 struct InstrProfRecord;
37 class LLVMContext;
38 class Metadata;
39 class MDTuple;
40 class MDNode;
41
getHotSectionPrefix()42 inline const char *getHotSectionPrefix() { return ".hot"; }
getUnlikelySectionPrefix()43 inline const char *getUnlikelySectionPrefix() { return ".unlikely"; }
44
45 class ProfileSummaryBuilder {
46
47 private:
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
50 // counts.
51 std::map<uint64_t, uint32_t, std::greater<uint64_t>> CountFrequencies;
52 std::vector<uint32_t> DetailedSummaryCutoffs;
53
54 protected:
55 SummaryEntryVector DetailedSummary;
ProfileSummaryBuilder(std::vector<uint32_t> Cutoffs)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;
64
65 public:
66 /// \brief A vector of useful cutoff values for detailed summary.
67 static const ArrayRef<uint32_t> DefaultCutoffs;
68 };
69
70 class InstrProfSummaryBuilder final : public ProfileSummaryBuilder {
71 uint64_t MaxInternalBlockCount;
72 inline void addEntryCount(uint64_t Count);
73 inline void addInternalCount(uint64_t Count);
74
75 public:
InstrProfSummaryBuilder(std::vector<uint32_t> Cutoffs)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();
80 };
81
82 class SampleProfileSummaryBuilder final : public ProfileSummaryBuilder {
83
84 public:
85 void addRecord(const sampleprof::FunctionSamples &FS);
SampleProfileSummaryBuilder(std::vector<uint32_t> Cutoffs)86 SampleProfileSummaryBuilder(std::vector<uint32_t> Cutoffs)
87 : ProfileSummaryBuilder(std::move(Cutoffs)) {}
88 std::unique_ptr<ProfileSummary> getSummary();
89 };
90
91 // This is called when a count is seen in the profile.
addCount(uint64_t Count)92 void ProfileSummaryBuilder::addCount(uint64_t Count) {
93 TotalCount += Count;
94 if (Count > MaxCount)
95 MaxCount = Count;
96 NumCounts++;
97 CountFrequencies[Count]++;
98 }
99
100
101 } // end namespace llvm
102 #endif
103