1 //=-- SampleProf.h - Sampling profiling format support --------------------===//
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 common definitions used in the reading and writing of
11 // sample profile data.
13 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_PROFILEDATA_SAMPLEPROF_H_
15 #define LLVM_PROFILEDATA_SAMPLEPROF_H_
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/Support/raw_ostream.h"
21 #include <system_error>
25 const std::error_category &sampleprof_category();
27 enum class sampleprof_error {
36 inline std::error_code make_error_code(sampleprof_error E) {
37 return std::error_code(static_cast<int>(E), sampleprof_category());
40 } // end namespace llvm
44 struct is_error_code_enum<llvm::sampleprof_error> : std::true_type {};
49 namespace sampleprof {
51 static inline uint64_t SPMagic() {
52 return uint64_t('S') << (64 - 8) | uint64_t('P') << (64 - 16) |
53 uint64_t('R') << (64 - 24) | uint64_t('O') << (64 - 32) |
54 uint64_t('F') << (64 - 40) | uint64_t('4') << (64 - 48) |
55 uint64_t('2') << (64 - 56) | uint64_t(0xff);
58 static inline uint64_t SPVersion() { return 100; }
60 /// \brief Represents the relative location of an instruction.
62 /// Instruction locations are specified by the line offset from the
63 /// beginning of the function (marked by the line where the function
64 /// header is) and the discriminator value within that line.
66 /// The discriminator value is useful to distinguish instructions
67 /// that are on the same line but belong to different basic blocks
68 /// (e.g., the two post-increment instructions in "if (p) x++; else y++;").
70 LineLocation(int L, unsigned D) : LineOffset(L), Discriminator(D) {}
72 unsigned Discriminator;
75 } // End namespace sampleprof
77 template <> struct DenseMapInfo<sampleprof::LineLocation> {
78 typedef DenseMapInfo<int> OffsetInfo;
79 typedef DenseMapInfo<unsigned> DiscriminatorInfo;
80 static inline sampleprof::LineLocation getEmptyKey() {
81 return sampleprof::LineLocation(OffsetInfo::getEmptyKey(),
82 DiscriminatorInfo::getEmptyKey());
84 static inline sampleprof::LineLocation getTombstoneKey() {
85 return sampleprof::LineLocation(OffsetInfo::getTombstoneKey(),
86 DiscriminatorInfo::getTombstoneKey());
88 static inline unsigned getHashValue(sampleprof::LineLocation Val) {
89 return DenseMapInfo<std::pair<int, unsigned>>::getHashValue(
90 std::pair<int, unsigned>(Val.LineOffset, Val.Discriminator));
92 static inline bool isEqual(sampleprof::LineLocation LHS,
93 sampleprof::LineLocation RHS) {
94 return LHS.LineOffset == RHS.LineOffset &&
95 LHS.Discriminator == RHS.Discriminator;
99 namespace sampleprof {
101 /// \brief Representation of a single sample record.
103 /// A sample record is represented by a positive integer value, which
104 /// indicates how frequently was the associated line location executed.
106 /// Additionally, if the associated location contains a function call,
107 /// the record will hold a list of all the possible called targets. For
108 /// direct calls, this will be the exact function being invoked. For
109 /// indirect calls (function pointers, virtual table dispatch), this
110 /// will be a list of one or more functions.
113 typedef SmallVector<std::pair<std::string, unsigned>, 8> CallTargetList;
115 SampleRecord() : NumSamples(0), CallTargets() {}
117 /// \brief Increment the number of samples for this record by \p S.
118 void addSamples(unsigned S) { NumSamples += S; }
120 /// \brief Add called function \p F with samples \p S.
121 void addCalledTarget(std::string F, unsigned S) {
122 CallTargets.push_back(std::make_pair(F, S));
125 /// \brief Return true if this sample record contains function calls.
126 bool hasCalls() const { return CallTargets.size() > 0; }
128 unsigned getSamples() const { return NumSamples; }
129 const CallTargetList &getCallTargets() const { return CallTargets; }
133 CallTargetList CallTargets;
136 typedef DenseMap<LineLocation, SampleRecord> BodySampleMap;
138 /// \brief Representation of the samples collected for a function.
140 /// This data structure contains all the collected samples for the body
141 /// of a function. Each sample corresponds to a LineLocation instance
142 /// within the body of the function.
143 class FunctionSamples {
145 FunctionSamples() : TotalSamples(0), TotalHeadSamples(0) {}
146 void print(raw_ostream &OS);
147 void addTotalSamples(unsigned Num) { TotalSamples += Num; }
148 void addHeadSamples(unsigned Num) { TotalHeadSamples += Num; }
149 void addBodySamples(int LineOffset, unsigned Discriminator, unsigned Num) {
150 assert(LineOffset >= 0);
151 // When dealing with instruction weights, we use the value
152 // zero to indicate the absence of a sample. If we read an
153 // actual zero from the profile file, use the value 1 to
154 // avoid the confusion later on.
157 BodySamples[LineLocation(LineOffset, Discriminator)].addSamples(Num);
159 void addCalledTargetSamples(int LineOffset, unsigned Discriminator,
160 std::string FName, unsigned Num) {
161 assert(LineOffset >= 0);
162 BodySamples[LineLocation(LineOffset, Discriminator)].addCalledTarget(FName,
166 /// \brief Return the number of samples collected at the given location.
167 /// Each location is specified by \p LineOffset and \p Discriminator.
168 unsigned samplesAt(int LineOffset, unsigned Discriminator) {
169 return BodySamples[LineLocation(LineOffset, Discriminator)].getSamples();
172 bool empty() const { return BodySamples.empty(); }
174 /// \brief Return the total number of samples collected inside the function.
175 unsigned getTotalSamples() const { return TotalSamples; }
177 /// \brief Return the total number of samples collected at the head of the
179 unsigned getHeadSamples() const { return TotalHeadSamples; }
181 /// \brief Return all the samples collected in the body of the function.
182 const BodySampleMap &getBodySamples() const { return BodySamples; }
185 /// \brief Total number of samples collected inside this function.
187 /// Samples are cumulative, they include all the samples collected
188 /// inside this function and all its inlined callees.
189 unsigned TotalSamples;
191 /// \brief Total number of samples collected at the head of the function.
192 unsigned TotalHeadSamples;
194 /// \brief Map instruction locations to collected samples.
196 /// Each entry in this map contains the number of samples
197 /// collected at the corresponding line offset. All line locations
198 /// are an offset from the start of the function.
199 BodySampleMap BodySamples;
202 } // End namespace sampleprof
204 } // End namespace llvm
206 #endif // LLVM_PROFILEDATA_SAMPLEPROF_H_