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
28namespace llvm {
29class Function;
30namespace IndexedInstrProf {
31struct Summary;
32}
33namespace sampleprof {
34class FunctionSamples;
35}
36struct InstrProfRecord;
37class LLVMContext;
38class Metadata;
39class MDTuple;
40class MDNode;
41
42inline const char *getHotSectionPrefix() { return ".hot"; }
43inline const char *getUnlikelySectionPrefix() { return ".unlikely"; }
44
45class ProfileSummaryBuilder {
46
47private:
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
54protected:
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;
64
65public:
66  /// \brief A vector of useful cutoff values for detailed summary.
67  static const ArrayRef<uint32_t> DefaultCutoffs;
68};
69
70class InstrProfSummaryBuilder final : public ProfileSummaryBuilder {
71  uint64_t MaxInternalBlockCount;
72  inline void addEntryCount(uint64_t Count);
73  inline void addInternalCount(uint64_t Count);
74
75public:
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
82class SampleProfileSummaryBuilder final : public ProfileSummaryBuilder {
83
84public:
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();
89};
90
91// This is called when a count is seen in the profile.
92void 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