ef782c11043d3005df352d201f9597a7ae383b8a
[oota-llvm.git] / include / llvm / ProfileData / SampleProf.h
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/Support/raw_ostream.h"
20
21 #include <system_error>
22
23 namespace llvm {
24
25 const std::error_category &sampleprof_category();
26
27 enum class sampleprof_error {
28   success = 0,
29   bad_magic,
30   unsupported_version,
31   too_large,
32   truncated,
33   malformed
34 };
35
36 inline std::error_code make_error_code(sampleprof_error E) {
37   return std::error_code(static_cast<int>(E), sampleprof_category());
38 }
39
40 } // end namespace llvm
41
42 namespace std {
43 template <>
44 struct is_error_code_enum<llvm::sampleprof_error> : std::true_type {};
45 }
46
47 namespace llvm {
48
49 namespace sampleprof {
50
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);
56 }
57
58 static inline uint64_t SPVersion() { return 100; }
59
60 /// \brief Represents the relative location of an instruction.
61 ///
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.
65 ///
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++;").
69 struct LineLocation {
70   LineLocation(int L, unsigned D) : LineOffset(L), Discriminator(D) {}
71   int LineOffset;
72   unsigned Discriminator;
73 };
74
75 } // End namespace sampleprof
76
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());
83   }
84   static inline sampleprof::LineLocation getTombstoneKey() {
85     return sampleprof::LineLocation(OffsetInfo::getTombstoneKey(),
86                                     DiscriminatorInfo::getTombstoneKey());
87   }
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));
91   }
92   static inline bool isEqual(sampleprof::LineLocation LHS,
93                              sampleprof::LineLocation RHS) {
94     return LHS.LineOffset == RHS.LineOffset &&
95            LHS.Discriminator == RHS.Discriminator;
96   }
97 };
98
99 namespace sampleprof {
100
101 /// \brief Representation of a single sample record.
102 ///
103 /// A sample record is represented by a positive integer value, which
104 /// indicates how frequently was the associated line location executed.
105 ///
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.
111 class SampleRecord {
112 public:
113   typedef SmallVector<std::pair<std::string, unsigned>, 8> CallTargetList;
114
115   SampleRecord() : NumSamples(0), CallTargets() {}
116
117   /// \brief Increment the number of samples for this record by \p S.
118   void addSamples(unsigned S) { NumSamples += S; }
119
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));
123   }
124
125   /// \brief Return true if this sample record contains function calls.
126   bool hasCalls() const { return CallTargets.size() > 0; }
127
128   unsigned getSamples() const { return NumSamples; }
129   const CallTargetList &getCallTargets() const { return CallTargets; }
130
131 private:
132   unsigned NumSamples;
133   CallTargetList CallTargets;
134 };
135
136 typedef DenseMap<LineLocation, SampleRecord> BodySampleMap;
137
138 /// \brief Representation of the samples collected for a function.
139 ///
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 {
144 public:
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.
155     if (Num == 0)
156       Num = 1;
157     BodySamples[LineLocation(LineOffset, Discriminator)].addSamples(Num);
158   }
159   void addCalledTargetSamples(int LineOffset, unsigned Discriminator,
160                               std::string FName, unsigned Num) {
161     assert(LineOffset >= 0);
162     BodySamples[LineLocation(LineOffset, Discriminator)].addCalledTarget(FName,
163                                                                          Num);
164   }
165
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();
170   }
171
172   bool empty() const { return BodySamples.empty(); }
173
174   /// \brief Return the total number of samples collected inside the function.
175   unsigned getTotalSamples() const { return TotalSamples; }
176
177   /// \brief Return the total number of samples collected at the head of the
178   /// function.
179   unsigned getHeadSamples() const { return TotalHeadSamples; }
180
181   /// \brief Return all the samples collected in the body of the function.
182   const BodySampleMap &getBodySamples() const { return BodySamples; }
183
184 private:
185   /// \brief Total number of samples collected inside this function.
186   ///
187   /// Samples are cumulative, they include all the samples collected
188   /// inside this function and all its inlined callees.
189   unsigned TotalSamples;
190
191   /// \brief Total number of samples collected at the head of the function.
192   unsigned TotalHeadSamples;
193
194   /// \brief Map instruction locations to collected samples.
195   ///
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;
200 };
201
202 } // End namespace sampleprof
203
204 } // End namespace llvm
205
206 #endif // LLVM_PROFILEDATA_SAMPLEPROF_H_