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 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_PROFILEDATA_SAMPLEPROF_H_
16 #define LLVM_PROFILEDATA_SAMPLEPROF_H_
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringMap.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/ErrorOr.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include <system_error>
28 const std::error_category &sampleprof_category();
30 enum class sampleprof_error {
38 unsupported_writing_format,
43 inline std::error_code make_error_code(sampleprof_error E) {
44 return std::error_code(static_cast<int>(E), sampleprof_category());
47 } // end namespace llvm
51 struct is_error_code_enum<llvm::sampleprof_error> : std::true_type {};
56 namespace sampleprof {
58 static inline uint64_t SPMagic() {
59 return uint64_t('S') << (64 - 8) | uint64_t('P') << (64 - 16) |
60 uint64_t('R') << (64 - 24) | uint64_t('O') << (64 - 32) |
61 uint64_t('F') << (64 - 40) | uint64_t('4') << (64 - 48) |
62 uint64_t('2') << (64 - 56) | uint64_t(0xff);
65 static inline uint64_t SPVersion() { return 102; }
67 /// Represents the relative location of an instruction.
69 /// Instruction locations are specified by the line offset from the
70 /// beginning of the function (marked by the line where the function
71 /// header is) and the discriminator value within that line.
73 /// The discriminator value is useful to distinguish instructions
74 /// that are on the same line but belong to different basic blocks
75 /// (e.g., the two post-increment instructions in "if (p) x++; else y++;").
77 LineLocation(uint32_t L, uint32_t D) : LineOffset(L), Discriminator(D) {}
79 uint32_t Discriminator;
82 /// Represents the relative location of a callsite.
84 /// Callsite locations are specified by the line offset from the
85 /// beginning of the function (marked by the line where the function
86 /// head is), the discriminator value within that line, and the callee
88 struct CallsiteLocation : public LineLocation {
89 CallsiteLocation(uint32_t L, uint32_t D, StringRef N)
90 : LineLocation(L, D), CalleeName(N) {}
94 } // End namespace sampleprof
96 template <> struct DenseMapInfo<sampleprof::LineLocation> {
97 typedef DenseMapInfo<uint32_t> OffsetInfo;
98 typedef DenseMapInfo<uint32_t> DiscriminatorInfo;
99 static inline sampleprof::LineLocation getEmptyKey() {
100 return sampleprof::LineLocation(OffsetInfo::getEmptyKey(),
101 DiscriminatorInfo::getEmptyKey());
103 static inline sampleprof::LineLocation getTombstoneKey() {
104 return sampleprof::LineLocation(OffsetInfo::getTombstoneKey(),
105 DiscriminatorInfo::getTombstoneKey());
107 static inline unsigned getHashValue(sampleprof::LineLocation Val) {
108 return DenseMapInfo<std::pair<uint32_t, uint32_t>>::getHashValue(
109 std::pair<uint32_t, uint32_t>(Val.LineOffset, Val.Discriminator));
111 static inline bool isEqual(sampleprof::LineLocation LHS,
112 sampleprof::LineLocation RHS) {
113 return LHS.LineOffset == RHS.LineOffset &&
114 LHS.Discriminator == RHS.Discriminator;
118 template <> struct DenseMapInfo<sampleprof::CallsiteLocation> {
119 typedef DenseMapInfo<uint32_t> OffsetInfo;
120 typedef DenseMapInfo<uint32_t> DiscriminatorInfo;
121 typedef DenseMapInfo<StringRef> CalleeNameInfo;
122 static inline sampleprof::CallsiteLocation getEmptyKey() {
123 return sampleprof::CallsiteLocation(OffsetInfo::getEmptyKey(),
124 DiscriminatorInfo::getEmptyKey(), "");
126 static inline sampleprof::CallsiteLocation getTombstoneKey() {
127 return sampleprof::CallsiteLocation(OffsetInfo::getTombstoneKey(),
128 DiscriminatorInfo::getTombstoneKey(),
131 static inline unsigned getHashValue(sampleprof::CallsiteLocation Val) {
132 return DenseMapInfo<std::pair<uint32_t, uint32_t>>::getHashValue(
133 std::pair<uint32_t, uint32_t>(Val.LineOffset, Val.Discriminator));
135 static inline bool isEqual(sampleprof::CallsiteLocation LHS,
136 sampleprof::CallsiteLocation RHS) {
137 return LHS.LineOffset == RHS.LineOffset &&
138 LHS.Discriminator == RHS.Discriminator &&
139 LHS.CalleeName.equals(RHS.CalleeName);
143 namespace sampleprof {
145 /// Representation of a single sample record.
147 /// A sample record is represented by a positive integer value, which
148 /// indicates how frequently was the associated line location executed.
150 /// Additionally, if the associated location contains a function call,
151 /// the record will hold a list of all the possible called targets. For
152 /// direct calls, this will be the exact function being invoked. For
153 /// indirect calls (function pointers, virtual table dispatch), this
154 /// will be a list of one or more functions.
157 typedef StringMap<uint64_t> CallTargetMap;
159 SampleRecord() : NumSamples(0), CallTargets() {}
161 /// Increment the number of samples for this record by \p S.
163 /// Sample counts accumulate using saturating arithmetic, to avoid wrapping
164 /// around unsigned integers.
165 void addSamples(uint64_t S) {
166 if (NumSamples <= std::numeric_limits<uint64_t>::max() - S)
169 NumSamples = std::numeric_limits<uint64_t>::max();
172 /// Add called function \p F with samples \p S.
174 /// Sample counts accumulate using saturating arithmetic, to avoid wrapping
175 /// around unsigned integers.
176 void addCalledTarget(StringRef F, uint64_t S) {
177 uint64_t &TargetSamples = CallTargets[F];
178 if (TargetSamples <= std::numeric_limits<uint64_t>::max() - S)
181 TargetSamples = std::numeric_limits<uint64_t>::max();
184 /// Return true if this sample record contains function calls.
185 bool hasCalls() const { return CallTargets.size() > 0; }
187 uint64_t getSamples() const { return NumSamples; }
188 const CallTargetMap &getCallTargets() const { return CallTargets; }
190 /// Merge the samples in \p Other into this record.
191 void merge(const SampleRecord &Other) {
192 addSamples(Other.getSamples());
193 for (const auto &I : Other.getCallTargets())
194 addCalledTarget(I.first(), I.second);
199 CallTargetMap CallTargets;
202 typedef DenseMap<LineLocation, SampleRecord> BodySampleMap;
203 class FunctionSamples;
204 typedef DenseMap<CallsiteLocation, FunctionSamples> CallsiteSampleMap;
206 /// Representation of the samples collected for a function.
208 /// This data structure contains all the collected samples for the body
209 /// of a function. Each sample corresponds to a LineLocation instance
210 /// within the body of the function.
211 class FunctionSamples {
213 FunctionSamples() : TotalSamples(0), TotalHeadSamples(0) {}
214 void print(raw_ostream &OS = dbgs(), unsigned Indent = 0) const;
215 void addTotalSamples(uint64_t Num) { TotalSamples += Num; }
216 void addHeadSamples(uint64_t Num) { TotalHeadSamples += Num; }
217 void addBodySamples(uint32_t LineOffset, uint32_t Discriminator,
219 BodySamples[LineLocation(LineOffset, Discriminator)].addSamples(Num);
221 void addCalledTargetSamples(uint32_t LineOffset, uint32_t Discriminator,
222 std::string FName, uint64_t Num) {
223 BodySamples[LineLocation(LineOffset, Discriminator)].addCalledTarget(FName,
227 /// Return the number of samples collected at the given location.
228 /// Each location is specified by \p LineOffset and \p Discriminator.
229 /// If the location is not found in profile, return error.
230 ErrorOr<uint64_t> findSamplesAt(uint32_t LineOffset,
231 uint32_t Discriminator) const {
232 const auto &ret = BodySamples.find(LineLocation(LineOffset, Discriminator));
233 if (ret == BodySamples.end())
234 return std::error_code();
236 return ret->second.getSamples();
239 /// Return the function samples at the given callsite location.
240 FunctionSamples &functionSamplesAt(const CallsiteLocation &Loc) {
241 return CallsiteSamples[Loc];
244 /// Return a pointer to function samples at the given callsite location.
245 const FunctionSamples *
246 findFunctionSamplesAt(const CallsiteLocation &Loc) const {
247 auto iter = CallsiteSamples.find(Loc);
248 if (iter == CallsiteSamples.end()) {
251 return &iter->second;
255 bool empty() const { return TotalSamples == 0; }
257 /// Return the total number of samples collected inside the function.
258 uint64_t getTotalSamples() const { return TotalSamples; }
260 /// Return the total number of samples collected at the head of the
262 uint64_t getHeadSamples() const { return TotalHeadSamples; }
264 /// Return all the samples collected in the body of the function.
265 const BodySampleMap &getBodySamples() const { return BodySamples; }
267 /// Return all the callsite samples collected in the body of the function.
268 const CallsiteSampleMap &getCallsiteSamples() const {
269 return CallsiteSamples;
272 /// Merge the samples in \p Other into this one.
273 void merge(const FunctionSamples &Other) {
274 addTotalSamples(Other.getTotalSamples());
275 addHeadSamples(Other.getHeadSamples());
276 for (const auto &I : Other.getBodySamples()) {
277 const LineLocation &Loc = I.first;
278 const SampleRecord &Rec = I.second;
279 BodySamples[Loc].merge(Rec);
281 for (const auto &I : Other.getCallsiteSamples()) {
282 const CallsiteLocation &Loc = I.first;
283 const FunctionSamples &Rec = I.second;
284 functionSamplesAt(Loc).merge(Rec);
289 /// Total number of samples collected inside this function.
291 /// Samples are cumulative, they include all the samples collected
292 /// inside this function and all its inlined callees.
293 uint64_t TotalSamples;
295 /// Total number of samples collected at the head of the function.
296 /// This is an approximation of the number of calls made to this function
298 uint64_t TotalHeadSamples;
300 /// Map instruction locations to collected samples.
302 /// Each entry in this map contains the number of samples
303 /// collected at the corresponding line offset. All line locations
304 /// are an offset from the start of the function.
305 BodySampleMap BodySamples;
307 /// Map call sites to collected samples for the called function.
309 /// Each entry in this map corresponds to all the samples
310 /// collected for the inlined function call at the given
311 /// location. For example, given:
319 /// If the bar() and baz() calls were inlined inside foo(), this
320 /// map will contain two entries. One for all the samples collected
321 /// in the call to bar() at line offset 1, the other for all the samples
322 /// collected in the call to baz() at line offset 8.
323 CallsiteSampleMap CallsiteSamples;
326 } // end namespace sampleprof
328 } // end namespace llvm
330 #endif // LLVM_PROFILEDATA_SAMPLEPROF_H_