]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/ProfileData/ProfileCommon.h
Upgrade our copies of clang, llvm, lld, lldb, compiler-rt and libc++ to
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / ProfileData / ProfileCommon.h
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
42 inline const char *getHotSectionPrefix() { return ".hot"; }
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;
56   ProfileSummaryBuilder(std::vector<uint32_t> Cutoffs)
57       : DetailedSummaryCutoffs(std::move(Cutoffs)) {}
58   inline void addCount(uint64_t Count);
59   ~ProfileSummaryBuilder() = default;
60   void computeDetailedSummary();
61   uint64_t TotalCount = 0, MaxCount = 0, MaxFunctionCount = 0;
62   uint32_t NumCounts = 0, NumFunctions = 0;
63
64 public:
65   /// \brief A vector of useful cutoff values for detailed summary.
66   static const ArrayRef<uint32_t> DefaultCutoffs;
67 };
68
69 class InstrProfSummaryBuilder final : public ProfileSummaryBuilder {
70   uint64_t MaxInternalBlockCount = 0;
71   inline void addEntryCount(uint64_t Count);
72   inline void addInternalCount(uint64_t Count);
73
74 public:
75   InstrProfSummaryBuilder(std::vector<uint32_t> Cutoffs)
76       : ProfileSummaryBuilder(std::move(Cutoffs)) {}
77   void addRecord(const InstrProfRecord &);
78   std::unique_ptr<ProfileSummary> getSummary();
79 };
80
81 class SampleProfileSummaryBuilder final : public ProfileSummaryBuilder {
82
83 public:
84   void addRecord(const sampleprof::FunctionSamples &FS);
85   SampleProfileSummaryBuilder(std::vector<uint32_t> Cutoffs)
86       : ProfileSummaryBuilder(std::move(Cutoffs)) {}
87   std::unique_ptr<ProfileSummary> getSummary();
88 };
89
90 /// This is called when a count is seen in the profile.
91 void ProfileSummaryBuilder::addCount(uint64_t Count) {
92   TotalCount += Count;
93   if (Count > MaxCount)
94     MaxCount = Count;
95   NumCounts++;
96   CountFrequencies[Count]++;
97 }
98
99
100 } // end namespace llvm
101 #endif