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(), unsigned Indent = 0) const;
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 BodySamples[LineLocation(LineOffset, Discriminator)].addSamples(Num);
218 void addCalledTargetSamples(int LineOffset, unsigned Discriminator,
219 std::string FName, unsigned Num) {
220 assert(LineOffset >= 0);
221 BodySamples[LineLocation(LineOffset, Discriminator)].addCalledTarget(FName,
225 /// Return the number of samples collected at the given location.
226 /// Each location is specified by \p LineOffset and \p Discriminator.
227 /// If the location is not found in profile, return error.
228 ErrorOr<unsigned> findSamplesAt(int LineOffset,
229 unsigned Discriminator) const {
230 const auto &ret = BodySamples.find(LineLocation(LineOffset, Discriminator));
231 if (ret == BodySamples.end())
232 return std::error_code();
234 return ret->second.getSamples();
237 /// Return the function samples at the given callsite location.
238 FunctionSamples &functionSamplesAt(const CallsiteLocation &Loc) {
239 return CallsiteSamples[Loc];
242 /// Return a pointer to function samples at the given callsite location.
243 const FunctionSamples *
244 findFunctionSamplesAt(const CallsiteLocation &Loc) const {
245 auto iter = CallsiteSamples.find(Loc);
246 if (iter == CallsiteSamples.end()) {
249 return &iter->second;
253 bool empty() const { return TotalSamples == 0; }
255 /// Return the total number of samples collected inside the function.
256 unsigned getTotalSamples() const { return TotalSamples; }
258 /// Return the total number of samples collected at the head of the
260 unsigned getHeadSamples() const { return TotalHeadSamples; }
262 /// Return all the samples collected in the body of the function.
263 const BodySampleMap &getBodySamples() const { return BodySamples; }
265 /// Return all the callsite samples collected in the body of the function.
266 const CallsiteSampleMap &getCallsiteSamples() const {
267 return CallsiteSamples;
270 /// Merge the samples in \p Other into this one.
271 void merge(const FunctionSamples &Other) {
272 addTotalSamples(Other.getTotalSamples());
273 addHeadSamples(Other.getHeadSamples());
274 for (const auto &I : Other.getBodySamples()) {
275 const LineLocation &Loc = I.first;
276 const SampleRecord &Rec = I.second;
277 BodySamples[Loc].merge(Rec);
279 for (const auto &I : Other.getCallsiteSamples()) {
280 const CallsiteLocation &Loc = I.first;
281 const FunctionSamples &Rec = I.second;
282 functionSamplesAt(Loc).merge(Rec);
287 /// Total number of samples collected inside this function.
289 /// Samples are cumulative, they include all the samples collected
290 /// inside this function and all its inlined callees.
291 unsigned TotalSamples;
293 /// Total number of samples collected at the head of the function.
294 /// This is an approximation of the number of calls made to this function
296 unsigned TotalHeadSamples;
298 /// Map instruction locations to collected samples.
300 /// Each entry in this map contains the number of samples
301 /// collected at the corresponding line offset. All line locations
302 /// are an offset from the start of the function.
303 BodySampleMap BodySamples;
305 /// Map call sites to collected samples for the called function.
307 /// Each entry in this map corresponds to all the samples
308 /// collected for the inlined function call at the given
309 /// location. For example, given:
317 /// If the bar() and baz() calls were inlined inside foo(), this
318 /// map will contain two entries. One for all the samples collected
319 /// in the call to bar() at line offset 1, the other for all the samples
320 /// collected in the call to baz() at line offset 8.
321 CallsiteSampleMap CallsiteSamples;
324 } // End namespace sampleprof
326 } // End namespace llvm
328 #endif // LLVM_PROFILEDATA_SAMPLEPROF_H_