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/ADT/StringMap.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/ErrorOr.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include <system_error>
27 const std::error_category &sampleprof_category();
29 enum class sampleprof_error {
40 inline std::error_code make_error_code(sampleprof_error E) {
41 return std::error_code(static_cast<int>(E), sampleprof_category());
44 } // end namespace llvm
48 struct is_error_code_enum<llvm::sampleprof_error> : std::true_type {};
53 namespace sampleprof {
55 static inline uint64_t SPMagic() {
56 return uint64_t('S') << (64 - 8) | uint64_t('P') << (64 - 16) |
57 uint64_t('R') << (64 - 24) | uint64_t('O') << (64 - 32) |
58 uint64_t('F') << (64 - 40) | uint64_t('4') << (64 - 48) |
59 uint64_t('2') << (64 - 56) | uint64_t(0xff);
62 static inline uint64_t SPVersion() { return 100; }
64 /// Represents the relative location of an instruction.
66 /// Instruction locations are specified by the line offset from the
67 /// beginning of the function (marked by the line where the function
68 /// header is) and the discriminator value within that line.
70 /// The discriminator value is useful to distinguish instructions
71 /// that are on the same line but belong to different basic blocks
72 /// (e.g., the two post-increment instructions in "if (p) x++; else y++;").
74 LineLocation(int L, unsigned D) : LineOffset(L), Discriminator(D) {}
76 unsigned Discriminator;
79 /// Represents the relative location of a callsite.
81 /// Callsite locations are specified by the line offset from the
82 /// beginning of the function (marked by the line where the function
83 /// head is), the discriminator value within that line, and the callee
85 struct CallsiteLocation : public LineLocation {
86 CallsiteLocation(int L, unsigned D, StringRef N)
87 : LineLocation(L, D), CalleeName(N) {}
91 } // End namespace sampleprof
93 template <> struct DenseMapInfo<sampleprof::LineLocation> {
94 typedef DenseMapInfo<int> OffsetInfo;
95 typedef DenseMapInfo<unsigned> DiscriminatorInfo;
96 static inline sampleprof::LineLocation getEmptyKey() {
97 return sampleprof::LineLocation(OffsetInfo::getEmptyKey(),
98 DiscriminatorInfo::getEmptyKey());
100 static inline sampleprof::LineLocation getTombstoneKey() {
101 return sampleprof::LineLocation(OffsetInfo::getTombstoneKey(),
102 DiscriminatorInfo::getTombstoneKey());
104 static inline unsigned getHashValue(sampleprof::LineLocation Val) {
105 return DenseMapInfo<std::pair<int, unsigned>>::getHashValue(
106 std::pair<int, unsigned>(Val.LineOffset, Val.Discriminator));
108 static inline bool isEqual(sampleprof::LineLocation LHS,
109 sampleprof::LineLocation RHS) {
110 return LHS.LineOffset == RHS.LineOffset &&
111 LHS.Discriminator == RHS.Discriminator;
115 template <> struct DenseMapInfo<sampleprof::CallsiteLocation> {
116 typedef DenseMapInfo<int> OffsetInfo;
117 typedef DenseMapInfo<unsigned> DiscriminatorInfo;
118 typedef DenseMapInfo<StringRef> CalleeNameInfo;
119 static inline sampleprof::CallsiteLocation getEmptyKey() {
120 return sampleprof::CallsiteLocation(OffsetInfo::getEmptyKey(),
121 DiscriminatorInfo::getEmptyKey(), "");
123 static inline sampleprof::CallsiteLocation getTombstoneKey() {
124 return sampleprof::CallsiteLocation(OffsetInfo::getTombstoneKey(),
125 DiscriminatorInfo::getTombstoneKey(),
128 static inline unsigned getHashValue(sampleprof::CallsiteLocation Val) {
129 return DenseMapInfo<std::pair<int, unsigned>>::getHashValue(
130 std::pair<int, unsigned>(Val.LineOffset, Val.Discriminator));
132 static inline bool isEqual(sampleprof::CallsiteLocation LHS,
133 sampleprof::CallsiteLocation RHS) {
134 return LHS.LineOffset == RHS.LineOffset &&
135 LHS.Discriminator == RHS.Discriminator &&
136 LHS.CalleeName.equals(RHS.CalleeName);
140 namespace sampleprof {
142 /// Representation of a single sample record.
144 /// A sample record is represented by a positive integer value, which
145 /// indicates how frequently was the associated line location executed.
147 /// Additionally, if the associated location contains a function call,
148 /// the record will hold a list of all the possible called targets. For
149 /// direct calls, this will be the exact function being invoked. For
150 /// indirect calls (function pointers, virtual table dispatch), this
151 /// will be a list of one or more functions.
154 typedef StringMap<unsigned> CallTargetMap;
156 SampleRecord() : NumSamples(0), CallTargets() {}
158 /// Increment the number of samples for this record by \p S.
160 /// Sample counts accumulate using saturating arithmetic, to avoid wrapping
161 /// around unsigned integers.
162 void addSamples(unsigned S) {
163 if (NumSamples <= std::numeric_limits<unsigned>::max() - S)
166 NumSamples = std::numeric_limits<unsigned>::max();
169 /// Add called function \p F with samples \p S.
171 /// Sample counts accumulate using saturating arithmetic, to avoid wrapping
172 /// around unsigned integers.
173 void addCalledTarget(StringRef F, unsigned S) {
174 unsigned &TargetSamples = CallTargets[F];
175 if (TargetSamples <= std::numeric_limits<unsigned>::max() - S)
178 TargetSamples = std::numeric_limits<unsigned>::max();
181 /// Return true if this sample record contains function calls.
182 bool hasCalls() const { return CallTargets.size() > 0; }
184 unsigned getSamples() const { return NumSamples; }
185 const CallTargetMap &getCallTargets() const { return CallTargets; }
187 /// Merge the samples in \p Other into this record.
188 void merge(const SampleRecord &Other) {
189 addSamples(Other.getSamples());
190 for (const auto &I : Other.getCallTargets())
191 addCalledTarget(I.first(), I.second);
196 CallTargetMap CallTargets;
199 typedef DenseMap<LineLocation, SampleRecord> BodySampleMap;
200 class FunctionSamples;
201 typedef DenseMap<CallsiteLocation, FunctionSamples> CallsiteSampleMap;
203 /// Representation of the samples collected for a function.
205 /// This data structure contains all the collected samples for the body
206 /// of a function. Each sample corresponds to a LineLocation instance
207 /// within the body of the function.
208 class FunctionSamples {
210 FunctionSamples() : TotalSamples(0), TotalHeadSamples(0) {}
211 void print(raw_ostream &OS = dbgs());
212 void addTotalSamples(unsigned Num) { TotalSamples += Num; }
213 void addHeadSamples(unsigned Num) { TotalHeadSamples += Num; }
214 void addBodySamples(int LineOffset, unsigned Discriminator, unsigned Num) {
215 assert(LineOffset >= 0);
216 // When dealing with instruction weights, we use the value
217 // zero to indicate the absence of a sample. If we read an
218 // actual zero from the profile file, use the value 1 to
219 // avoid the confusion later on.
222 BodySamples[LineLocation(LineOffset, Discriminator)].addSamples(Num);
224 void addCalledTargetSamples(int LineOffset, unsigned Discriminator,
225 std::string FName, unsigned Num) {
226 assert(LineOffset >= 0);
227 BodySamples[LineLocation(LineOffset, Discriminator)].addCalledTarget(FName,
231 /// Return the number of samples collected at the given location.
232 /// Each location is specified by \p LineOffset and \p Discriminator.
233 /// If the location is not found in profile, return error.
234 ErrorOr<unsigned> findSamplesAt(int LineOffset,
235 unsigned Discriminator) const {
236 const auto &ret = BodySamples.find(LineLocation(LineOffset, Discriminator));
237 if (ret == BodySamples.end())
238 return std::error_code();
240 return ret->second.getSamples();
243 /// Return the function samples at the given callsite location.
244 FunctionSamples &functionSamplesAt(const CallsiteLocation &Loc) {
245 return CallsiteSamples[Loc];
248 /// Return a pointer to function samples at the given callsite location.
249 const FunctionSamples *
250 findFunctionSamplesAt(const CallsiteLocation &Loc) const {
251 auto iter = CallsiteSamples.find(Loc);
252 if (iter == CallsiteSamples.end()) {
255 return &iter->second;
259 bool empty() const { return TotalSamples == 0; }
261 /// Return the total number of samples collected inside the function.
262 unsigned getTotalSamples() const { return TotalSamples; }
264 /// Return the total number of samples collected at the head of the
266 unsigned getHeadSamples() const { return TotalHeadSamples; }
268 /// Return all the samples collected in the body of the function.
269 const BodySampleMap &getBodySamples() const { return BodySamples; }
271 /// Return all the callsite samples collected in the body of the function.
272 const CallsiteSampleMap &getCallsiteSamples() const {
273 return CallsiteSamples;
276 /// Merge the samples in \p Other into this one.
277 void merge(const FunctionSamples &Other) {
278 addTotalSamples(Other.getTotalSamples());
279 addHeadSamples(Other.getHeadSamples());
280 for (const auto &I : Other.getBodySamples()) {
281 const LineLocation &Loc = I.first;
282 const SampleRecord &Rec = I.second;
283 BodySamples[Loc].merge(Rec);
285 for (const auto &I : Other.getCallsiteSamples()) {
286 const CallsiteLocation &Loc = I.first;
287 const FunctionSamples &Rec = I.second;
288 functionSamplesAt(Loc).merge(Rec);
293 /// Total number of samples collected inside this function.
295 /// Samples are cumulative, they include all the samples collected
296 /// inside this function and all its inlined callees.
297 unsigned TotalSamples;
299 /// Total number of samples collected at the head of the function.
300 /// This is an approximation of the number of calls made to this function
302 unsigned TotalHeadSamples;
304 /// Map instruction locations to collected samples.
306 /// Each entry in this map contains the number of samples
307 /// collected at the corresponding line offset. All line locations
308 /// are an offset from the start of the function.
309 BodySampleMap BodySamples;
311 CallsiteSampleMap CallsiteSamples;
314 } // End namespace sampleprof
316 } // End namespace llvm
318 #endif // LLVM_PROFILEDATA_SAMPLEPROF_H_