LLVM  3.7.0
SampleProf.h
Go to the documentation of this file.
1 //=-- SampleProf.h - Sampling profiling format support --------------------===//
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 common definitions used in the reading and writing of
11 // sample profile data.
12 //
13 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_PROFILEDATA_SAMPLEPROF_H_
15 #define LLVM_PROFILEDATA_SAMPLEPROF_H_
16 
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/Support/Debug.h"
22 #include <system_error>
23 
24 namespace llvm {
25 
27 
28 enum class sampleprof_error {
29  success = 0,
30  bad_magic,
32  too_large,
33  truncated,
34  malformed,
36 };
37 
38 inline std::error_code make_error_code(sampleprof_error E) {
39  return std::error_code(static_cast<int>(E), sampleprof_category());
40 }
41 
42 } // end namespace llvm
43 
44 namespace std {
45 template <>
46 struct is_error_code_enum<llvm::sampleprof_error> : std::true_type {};
47 }
48 
49 namespace llvm {
50 
51 namespace sampleprof {
52 
53 static inline uint64_t SPMagic() {
54  return uint64_t('S') << (64 - 8) | uint64_t('P') << (64 - 16) |
55  uint64_t('R') << (64 - 24) | uint64_t('O') << (64 - 32) |
56  uint64_t('F') << (64 - 40) | uint64_t('4') << (64 - 48) |
57  uint64_t('2') << (64 - 56) | uint64_t(0xff);
58 }
59 
60 static inline uint64_t SPVersion() { return 100; }
61 
62 /// Represents the relative location of an instruction.
63 ///
64 /// Instruction locations are specified by the line offset from the
65 /// beginning of the function (marked by the line where the function
66 /// header is) and the discriminator value within that line.
67 ///
68 /// The discriminator value is useful to distinguish instructions
69 /// that are on the same line but belong to different basic blocks
70 /// (e.g., the two post-increment instructions in "if (p) x++; else y++;").
71 struct LineLocation {
72  LineLocation(int L, unsigned D) : LineOffset(L), Discriminator(D) {}
74  unsigned Discriminator;
75 };
76 
77 } // End namespace sampleprof
78 
79 template <> struct DenseMapInfo<sampleprof::LineLocation> {
83  return sampleprof::LineLocation(OffsetInfo::getEmptyKey(),
84  DiscriminatorInfo::getEmptyKey());
85  }
87  return sampleprof::LineLocation(OffsetInfo::getTombstoneKey(),
88  DiscriminatorInfo::getTombstoneKey());
89  }
90  static inline unsigned getHashValue(sampleprof::LineLocation Val) {
91  return DenseMapInfo<std::pair<int, unsigned>>::getHashValue(
92  std::pair<int, unsigned>(Val.LineOffset, Val.Discriminator));
93  }
94  static inline bool isEqual(sampleprof::LineLocation LHS,
96  return LHS.LineOffset == RHS.LineOffset &&
97  LHS.Discriminator == RHS.Discriminator;
98  }
99 };
100 
101 namespace sampleprof {
102 
103 /// Representation of a single sample record.
104 ///
105 /// A sample record is represented by a positive integer value, which
106 /// indicates how frequently was the associated line location executed.
107 ///
108 /// Additionally, if the associated location contains a function call,
109 /// the record will hold a list of all the possible called targets. For
110 /// direct calls, this will be the exact function being invoked. For
111 /// indirect calls (function pointers, virtual table dispatch), this
112 /// will be a list of one or more functions.
114 public:
116 
117  SampleRecord() : NumSamples(0), CallTargets() {}
118 
119  /// Increment the number of samples for this record by \p S.
120  ///
121  /// Sample counts accumulate using saturating arithmetic, to avoid wrapping
122  /// around unsigned integers.
123  void addSamples(unsigned S) {
124  if (NumSamples <= std::numeric_limits<unsigned>::max() - S)
125  NumSamples += S;
126  else
127  NumSamples = std::numeric_limits<unsigned>::max();
128  }
129 
130  /// Add called function \p F with samples \p S.
131  ///
132  /// Sample counts accumulate using saturating arithmetic, to avoid wrapping
133  /// around unsigned integers.
134  void addCalledTarget(StringRef F, unsigned S) {
135  unsigned &TargetSamples = CallTargets[F];
136  if (TargetSamples <= std::numeric_limits<unsigned>::max() - S)
137  TargetSamples += S;
138  else
139  TargetSamples = std::numeric_limits<unsigned>::max();
140  }
141 
142  /// Return true if this sample record contains function calls.
143  bool hasCalls() const { return CallTargets.size() > 0; }
144 
145  unsigned getSamples() const { return NumSamples; }
146  const CallTargetMap &getCallTargets() const { return CallTargets; }
147 
148  /// Merge the samples in \p Other into this record.
149  void merge(const SampleRecord &Other) {
150  addSamples(Other.getSamples());
151  for (const auto &I : Other.getCallTargets())
152  addCalledTarget(I.first(), I.second);
153  }
154 
155 private:
156  unsigned NumSamples;
157  CallTargetMap CallTargets;
158 };
159 
161 
162 /// Representation of the samples collected for a function.
163 ///
164 /// This data structure contains all the collected samples for the body
165 /// of a function. Each sample corresponds to a LineLocation instance
166 /// within the body of the function.
168 public:
169  FunctionSamples() : TotalSamples(0), TotalHeadSamples(0) {}
170  void print(raw_ostream &OS = dbgs());
171  void addTotalSamples(unsigned Num) { TotalSamples += Num; }
172  void addHeadSamples(unsigned Num) { TotalHeadSamples += Num; }
173  void addBodySamples(int LineOffset, unsigned Discriminator, unsigned Num) {
174  assert(LineOffset >= 0);
175  // When dealing with instruction weights, we use the value
176  // zero to indicate the absence of a sample. If we read an
177  // actual zero from the profile file, use the value 1 to
178  // avoid the confusion later on.
179  if (Num == 0)
180  Num = 1;
181  BodySamples[LineLocation(LineOffset, Discriminator)].addSamples(Num);
182  }
183  void addCalledTargetSamples(int LineOffset, unsigned Discriminator,
184  std::string FName, unsigned Num) {
185  assert(LineOffset >= 0);
186  BodySamples[LineLocation(LineOffset, Discriminator)].addCalledTarget(FName,
187  Num);
188  }
189 
190  /// Return the sample record at the given location.
191  /// Each location is specified by \p LineOffset and \p Discriminator.
193  return BodySamples[Loc];
194  }
195 
196  /// Return the number of samples collected at the given location.
197  /// Each location is specified by \p LineOffset and \p Discriminator.
198  unsigned samplesAt(int LineOffset, unsigned Discriminator) {
199  return sampleRecordAt(LineLocation(LineOffset, Discriminator)).getSamples();
200  }
201 
202  bool empty() const { return BodySamples.empty(); }
203 
204  /// Return the total number of samples collected inside the function.
205  unsigned getTotalSamples() const { return TotalSamples; }
206 
207  /// Return the total number of samples collected at the head of the
208  /// function.
209  unsigned getHeadSamples() const { return TotalHeadSamples; }
210 
211  /// Return all the samples collected in the body of the function.
212  const BodySampleMap &getBodySamples() const { return BodySamples; }
213 
214  /// Merge the samples in \p Other into this one.
215  void merge(const FunctionSamples &Other) {
218  for (const auto &I : Other.getBodySamples()) {
219  const LineLocation &Loc = I.first;
220  const SampleRecord &Rec = I.second;
221  sampleRecordAt(Loc).merge(Rec);
222  }
223  }
224 
225 private:
226  /// Total number of samples collected inside this function.
227  ///
228  /// Samples are cumulative, they include all the samples collected
229  /// inside this function and all its inlined callees.
230  unsigned TotalSamples;
231 
232  /// Total number of samples collected at the head of the function.
233  /// This is an approximation of the number of calls made to this function
234  /// at runtime.
235  unsigned TotalHeadSamples;
236 
237  /// Map instruction locations to collected samples.
238  ///
239  /// Each entry in this map contains the number of samples
240  /// collected at the corresponding line offset. All line locations
241  /// are an offset from the start of the function.
242  BodySampleMap BodySamples;
243 };
244 
245 } // End namespace sampleprof
246 
247 } // End namespace llvm
248 
249 #endif // LLVM_PROFILEDATA_SAMPLEPROF_H_
void addBodySamples(int LineOffset, unsigned Discriminator, unsigned Num)
Definition: SampleProf.h:173
unsigned getHeadSamples() const
Return the total number of samples collected at the head of the function.
Definition: SampleProf.h:209
const BodySampleMap & getBodySamples() const
Return all the samples collected in the body of the function.
Definition: SampleProf.h:212
static sampleprof::LineLocation getTombstoneKey()
Definition: SampleProf.h:86
void print(raw_ostream &OS=dbgs())
Print the samples collected for a function on stream OS.
void addTotalSamples(unsigned Num)
Definition: SampleProf.h:171
F(f)
const CallTargetMap & getCallTargets() const
Definition: SampleProf.h:146
Representation of the samples collected for a function.
Definition: SampleProf.h:167
unsigned samplesAt(int LineOffset, unsigned Discriminator)
Return the number of samples collected at the given location.
Definition: SampleProf.h:198
std::error_code make_error_code(BitcodeError E)
Definition: ReaderWriter.h:150
ELFYAML::ELF_STO Other
Definition: ELFYAML.cpp:591
Representation of a single sample record.
Definition: SampleProf.h:113
void addHeadSamples(unsigned Num)
Definition: SampleProf.h:172
static uint64_t SPVersion()
Definition: SampleProf.h:60
sampleprof_error
Definition: SampleProf.h:28
unsigned size() const
Definition: StringMap.h:99
static ManagedStatic< _object_error_category > error_category
static unsigned getHashValue(sampleprof::LineLocation Val)
Definition: SampleProf.h:90
LineLocation(int L, unsigned D)
Definition: SampleProf.h:72
unsigned getTotalSamples() const
Return the total number of samples collected inside the function.
Definition: SampleProf.h:205
static bool isEqual(sampleprof::LineLocation LHS, sampleprof::LineLocation RHS)
Definition: SampleProf.h:94
DenseMap< LineLocation, SampleRecord > BodySampleMap
Definition: SampleProf.h:160
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:123
const std::error_category & sampleprof_category()
Definition: SampleProf.cpp:49
DenseMapInfo< unsigned > DiscriminatorInfo
Definition: SampleProf.h:81
bool hasCalls() const
Return true if this sample record contains function calls.
Definition: SampleProf.h:143
StringMap< unsigned > CallTargetMap
Definition: SampleProf.h:115
static uint64_t SPMagic()
Definition: SampleProf.h:53
Represents the relative location of an instruction.
Definition: SampleProf.h:71
#define I(x, y, z)
Definition: MD5.cpp:54
unsigned getSamples() const
Definition: SampleProf.h:145
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
Definition: DenseMap.h:79
void addSamples(unsigned S)
Increment the number of samples for this record by S.
Definition: SampleProf.h:123
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:38
void addCalledTarget(StringRef F, unsigned S)
Add called function F with samples S.
Definition: SampleProf.h:134
static sampleprof::LineLocation getEmptyKey()
Definition: SampleProf.h:82
SampleRecord & sampleRecordAt(const LineLocation &Loc)
Return the sample record at the given location.
Definition: SampleProf.h:192
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
void merge(const FunctionSamples &Other)
Merge the samples in Other into this one.
Definition: SampleProf.h:215
void merge(const SampleRecord &Other)
Merge the samples in Other into this record.
Definition: SampleProf.h:149
void addCalledTargetSamples(int LineOffset, unsigned Discriminator, std::string FName, unsigned Num)
Definition: SampleProf.h:183