87c871f855b353a2efed9e38cda0baa63fac7b01
[oota-llvm.git] / include / llvm / ProfileData / SampleProfReader.h
1 //===- SampleProfReader.h - Read LLVM sample profile data -----------------===//
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 definitions needed for reading sample profiles.
11 //
12 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_PROFILEDATA_SAMPLEPROFREADER_H
14 #define LLVM_PROFILEDATA_SAMPLEPROFREADER_H
15
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/IR/DiagnosticInfo.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/LLVMContext.h"
23 #include "llvm/ProfileData/SampleProf.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/ErrorOr.h"
27 #include "llvm/Support/GCOV.h"
28 #include "llvm/Support/MemoryBuffer.h"
29 #include "llvm/Support/raw_ostream.h"
30
31 namespace llvm {
32
33 namespace sampleprof {
34
35 /// \brief Sample-based profile reader.
36 ///
37 /// Each profile contains sample counts for all the functions
38 /// executed. Inside each function, statements are annotated with the
39 /// collected samples on all the instructions associated with that
40 /// statement.
41 ///
42 /// For this to produce meaningful data, the program needs to be
43 /// compiled with some debug information (at minimum, line numbers:
44 /// -gline-tables-only). Otherwise, it will be impossible to match IR
45 /// instructions to the line numbers collected by the profiler.
46 ///
47 /// From the profile file, we are interested in collecting the
48 /// following information:
49 ///
50 /// * A list of functions included in the profile (mangled names).
51 ///
52 /// * For each function F:
53 ///   1. The total number of samples collected in F.
54 ///
55 ///   2. The samples collected at each line in F. To provide some
56 ///      protection against source code shuffling, line numbers should
57 ///      be relative to the start of the function.
58 ///
59 /// The reader supports two file formats: text and binary. The text format
60 /// is useful for debugging and testing, while the binary format is more
61 /// compact and I/O efficient. They can both be used interchangeably.
62 class SampleProfileReader {
63 public:
64   SampleProfileReader(std::unique_ptr<MemoryBuffer> B, LLVMContext &C)
65       : Profiles(0), Ctx(C), Buffer(std::move(B)) {}
66
67   virtual ~SampleProfileReader() {}
68
69   /// \brief Read and validate the file header.
70   virtual std::error_code readHeader() = 0;
71
72   /// \brief Read sample profiles from the associated file.
73   virtual std::error_code read() = 0;
74
75   /// \brief Print the profile for \p FName on stream \p OS.
76   void dumpFunctionProfile(StringRef FName, raw_ostream &OS = dbgs());
77
78   /// \brief Print all the profiles on stream \p OS.
79   void dump(raw_ostream &OS = dbgs());
80
81   /// \brief Return the samples collected for function \p F.
82   FunctionSamples *getSamplesFor(const Function &F) {
83     return &Profiles[F.getName()];
84   }
85
86   /// \brief Return all the profiles.
87   StringMap<FunctionSamples> &getProfiles() { return Profiles; }
88
89   /// \brief Report a parse error message.
90   void reportError(int64_t LineNumber, Twine Msg) const {
91     Ctx.diagnose(DiagnosticInfoSampleProfile(Buffer->getBufferIdentifier(),
92                                              LineNumber, Msg));
93   }
94
95   /// \brief Create a sample profile reader appropriate to the file format.
96   static ErrorOr<std::unique_ptr<SampleProfileReader>>
97   create(StringRef Filename, LLVMContext &C);
98
99 protected:
100   /// \brief Map every function to its associated profile.
101   ///
102   /// The profile of every function executed at runtime is collected
103   /// in the structure FunctionSamples. This maps function objects
104   /// to their corresponding profiles.
105   StringMap<FunctionSamples> Profiles;
106
107   /// \brief LLVM context used to emit diagnostics.
108   LLVMContext &Ctx;
109
110   /// \brief Memory buffer holding the profile file.
111   std::unique_ptr<MemoryBuffer> Buffer;
112 };
113
114 class SampleProfileReaderText : public SampleProfileReader {
115 public:
116   SampleProfileReaderText(std::unique_ptr<MemoryBuffer> B, LLVMContext &C)
117       : SampleProfileReader(std::move(B), C) {}
118
119   /// \brief Read and validate the file header.
120   std::error_code readHeader() override { return sampleprof_error::success; }
121
122   /// \brief Read sample profiles from the associated file.
123   std::error_code read() override;
124 };
125
126 class SampleProfileReaderBinary : public SampleProfileReader {
127 public:
128   SampleProfileReaderBinary(std::unique_ptr<MemoryBuffer> B, LLVMContext &C)
129       : SampleProfileReader(std::move(B), C), Data(nullptr), End(nullptr) {}
130
131   /// \brief Read and validate the file header.
132   std::error_code readHeader() override;
133
134   /// \brief Read sample profiles from the associated file.
135   std::error_code read() override;
136
137   /// \brief Return true if \p Buffer is in the format supported by this class.
138   static bool hasFormat(const MemoryBuffer &Buffer);
139
140 protected:
141   /// \brief Read a numeric value of type T from the profile.
142   ///
143   /// If an error occurs during decoding, a diagnostic message is emitted and
144   /// EC is set.
145   ///
146   /// \returns the read value.
147   template <typename T> ErrorOr<T> readNumber();
148
149   /// \brief Read a string from the profile.
150   ///
151   /// If an error occurs during decoding, a diagnostic message is emitted and
152   /// EC is set.
153   ///
154   /// \returns the read value.
155   ErrorOr<StringRef> readString();
156
157   /// \brief Return true if we've reached the end of file.
158   bool at_eof() const { return Data >= End; }
159
160   /// \brief Points to the current location in the buffer.
161   const uint8_t *Data;
162
163   /// \brief Points to the end of the buffer.
164   const uint8_t *End;
165 };
166
167 // Represents the source position in GCC sample profiles.
168 struct SourceInfo {
169   SourceInfo()
170       : FuncName(), DirName(), FileName(), StartLine(0), Line(0),
171         Discriminator(0) {}
172
173   SourceInfo(StringRef FuncName, StringRef DirName, StringRef FileName,
174              uint32_t StartLine, uint32_t Line, uint32_t Discriminator)
175       : FuncName(FuncName), DirName(DirName), FileName(FileName),
176         StartLine(StartLine), Line(Line), Discriminator(Discriminator) {}
177
178   bool operator<(const SourceInfo &p) const;
179
180   uint32_t Offset() const { return ((Line - StartLine) << 16) | Discriminator; }
181
182   bool Malformed() const { return Line < StartLine; }
183
184   StringRef FuncName;
185   StringRef DirName;
186   StringRef FileName;
187   uint32_t StartLine;
188   uint32_t Line;
189   uint32_t Discriminator;
190 };
191
192 typedef std::vector<SourceInfo> SourceStack;
193
194 // Supported histogram types in GCC.  Currently, we only need support for
195 // call target histograms.
196 enum HistType {
197   HIST_TYPE_INTERVAL,
198   HIST_TYPE_POW2,
199   HIST_TYPE_SINGLE_VALUE,
200   HIST_TYPE_CONST_DELTA,
201   HIST_TYPE_INDIR_CALL,
202   HIST_TYPE_AVERAGE,
203   HIST_TYPE_IOR,
204   HIST_TYPE_INDIR_CALL_TOPN
205 };
206
207 class SampleProfileReaderGCC : public SampleProfileReader {
208 public:
209   SampleProfileReaderGCC(std::unique_ptr<MemoryBuffer> B, LLVMContext &C)
210       : SampleProfileReader(std::move(B), C), GcovBuffer(Buffer.get()) {}
211
212   /// \brief Read and validate the file header.
213   std::error_code readHeader() override;
214
215   /// \brief Read sample profiles from the associated file.
216   std::error_code read() override;
217
218   /// \brief Return true if \p Buffer is in the format supported by this class.
219   static bool hasFormat(const MemoryBuffer &Buffer);
220
221 protected:
222   std::error_code readNameTable();
223   std::error_code addSourceCount(StringRef Name, const SourceStack &Src,
224                                  uint64_t Count);
225   std::error_code readOneFunctionProfile(const SourceStack &Stack, bool Update);
226   std::error_code readFunctionProfiles();
227   std::error_code skipNextWord();
228   template <typename T> ErrorOr<T> readNumber();
229   ErrorOr<StringRef> readString();
230
231   /// \brief Read the section tag and check that it's the same as \p Expected.
232   std::error_code readSectionTag(uint32_t Expected);
233
234   /// GCOV buffer containing the profile.
235   GCOVBuffer GcovBuffer;
236
237   /// Function names in this profile.
238   std::vector<std::string> Names;
239
240   /// GCOV tags used to separate sections in the profile file.
241   static const uint32_t GCOVTagAFDOFileNames = 0xaa000000;
242   static const uint32_t GCOVTagAFDOFunction = 0xac000000;
243 };
244
245 } // End namespace sampleprof
246
247 } // End namespace llvm
248
249 #endif // LLVM_PROFILEDATA_SAMPLEPROFREADER_H