f425c6e13ed30343b4538c936715b50329451539
[oota-llvm.git] / include / llvm / ProfileData / InstrProf.h
1 //=-- InstrProf.h - Instrumented profiling format support ---------*- C++ -*-=//
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 // Instrumentation-based profiling data is generated by instrumented
11 // binaries through library functions in compiler-rt, and read by the clang
12 // frontend to feed PGO.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_PROFILEDATA_INSTRPROF_H_
17 #define LLVM_PROFILEDATA_INSTRPROF_H_
18
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/StringSet.h"
21 #include "llvm/Support/Endian.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/MD5.h"
24 #include <cstdint>
25 #include <list>
26 #include <system_error>
27 #include <vector>
28
29 namespace llvm {
30 const std::error_category &instrprof_category();
31
32 enum class instrprof_error {
33   success = 0,
34   eof,
35   bad_magic,
36   bad_header,
37   unsupported_version,
38   unsupported_hash_type,
39   too_large,
40   truncated,
41   malformed,
42   unknown_function,
43   hash_mismatch,
44   count_mismatch,
45   counter_overflow,
46   value_site_count_mismatch
47 };
48
49 inline std::error_code make_error_code(instrprof_error E) {
50   return std::error_code(static_cast<int>(E), instrprof_category());
51 }
52
53 enum InstrProfValueKind : uint32_t {
54   IPVK_IndirectCallTarget = 0,
55
56   IPVK_First = IPVK_IndirectCallTarget,
57   IPVK_Last = IPVK_IndirectCallTarget
58 };
59
60 struct InstrProfStringTable {
61   // Set of string values in profiling data.
62   StringSet<> StringValueSet;
63   InstrProfStringTable() { StringValueSet.clear(); }
64   // Get a pointer to internal storage of a string in set
65   const char *getStringData(StringRef Str) {
66     auto Result = StringValueSet.find(Str);
67     return (Result == StringValueSet.end()) ? nullptr : Result->first().data();
68   }
69   // Insert a string to StringTable
70   const char *insertString(StringRef Str) {
71     auto Result = StringValueSet.insert(Str);
72     return Result.first->first().data();
73   }
74 };
75
76 struct InstrProfValueSiteRecord {
77   /// Typedef for a single TargetValue-NumTaken pair.
78   typedef std::pair<uint64_t, uint64_t> ValueDataPair;
79   /// Value profiling data pairs at a given value site.
80   std::list<ValueDataPair> ValueData;
81
82   InstrProfValueSiteRecord() { ValueData.clear(); }
83
84   /// Sort ValueData ascending by TargetValue
85   void sortByTargetValues() {
86     ValueData.sort([](const ValueDataPair &left, const ValueDataPair &right) {
87       return left.first < right.first;
88     });
89   }
90
91   /// Merge data from another InstrProfValueSiteRecord
92   void mergeValueData(InstrProfValueSiteRecord &Input) {
93     this->sortByTargetValues();
94     Input.sortByTargetValues();
95     auto I = ValueData.begin();
96     auto IE = ValueData.end();
97     for (auto J = Input.ValueData.begin(), JE = Input.ValueData.end(); J != JE;
98          ++J) {
99       while (I != IE && I->first < J->first)
100         ++I;
101       if (I != IE && I->first == J->first) {
102         I->second += J->second;
103         ++I;
104         continue;
105       }
106       ValueData.insert(I, *J);
107     }
108   }
109 };
110
111 /// Profiling information for a single function.
112 struct InstrProfRecord {
113   InstrProfRecord() {}
114   InstrProfRecord(StringRef Name, uint64_t Hash, std::vector<uint64_t> Counts)
115       : Name(Name), Hash(Hash), Counts(std::move(Counts)) {}
116   StringRef Name;
117   uint64_t Hash;
118   std::vector<uint64_t> Counts;
119   std::vector<InstrProfValueSiteRecord> IndirectCallSites;
120
121   const std::vector<InstrProfValueSiteRecord> &
122   getValueSitesForKind(uint32_t ValueKind) const {
123     switch (ValueKind) {
124     case IPVK_IndirectCallTarget:
125       return IndirectCallSites;
126     }
127     llvm_unreachable("Unknown value kind!");
128   }
129
130   std::vector<InstrProfValueSiteRecord> &
131   getValueSitesForKind(uint32_t ValueKind) {
132     return const_cast<std::vector<InstrProfValueSiteRecord> &>(
133         const_cast<const InstrProfRecord *>(this)
134             ->getValueSitesForKind(ValueKind));
135   }
136 };
137
138 namespace IndexedInstrProf {
139 enum class HashT : uint32_t {
140   MD5,
141
142   Last = MD5
143 };
144
145 static inline uint64_t MD5Hash(StringRef Str) {
146   MD5 Hash;
147   Hash.update(Str);
148   llvm::MD5::MD5Result Result;
149   Hash.final(Result);
150   // Return the least significant 8 bytes. Our MD5 implementation returns the
151   // result in little endian, so we may need to swap bytes.
152   using namespace llvm::support;
153   return endian::read<uint64_t, little, unaligned>(Result);
154 }
155
156 static inline uint64_t ComputeHash(HashT Type, StringRef K) {
157   switch (Type) {
158     case HashT::MD5:
159       return IndexedInstrProf::MD5Hash(K);
160   }
161   llvm_unreachable("Unhandled hash type");
162 }
163
164 const uint64_t Magic = 0x8169666f72706cff;  // "\xfflprofi\x81"
165 const uint64_t Version = 3;
166 const HashT HashType = HashT::MD5;
167
168 struct Header {
169   uint64_t Magic;
170   uint64_t Version;
171   uint64_t MaxFunctionCount;
172   uint64_t HashType;
173   uint64_t HashOffset;
174 };
175
176 }  // end namespace IndexedInstrProf
177
178 namespace RawInstrProf {
179
180 const uint64_t Version = 1;
181
182 // Magic number to detect file format and endianness.
183 // Use 255 at one end, since no UTF-8 file can use that character.  Avoid 0,
184 // so that utilities, like strings, don't grab it as a string.  129 is also
185 // invalid UTF-8, and high enough to be interesting.
186 // Use "lprofr" in the centre to stand for "LLVM Profile Raw", or "lprofR"
187 // for 32-bit platforms.
188 // The magic and version need to be kept in sync with
189 // projects/compiler-rt/lib/profile/InstrProfiling.c
190
191 template <class IntPtrT>
192 inline uint64_t getMagic();
193 template <>
194 inline uint64_t getMagic<uint64_t>() {
195   return uint64_t(255) << 56 | uint64_t('l') << 48 | uint64_t('p') << 40 |
196          uint64_t('r') << 32 | uint64_t('o') << 24 | uint64_t('f') << 16 |
197          uint64_t('r') << 8 | uint64_t(129);
198 }
199
200 template <>
201 inline uint64_t getMagic<uint32_t>() {
202   return uint64_t(255) << 56 | uint64_t('l') << 48 | uint64_t('p') << 40 |
203          uint64_t('r') << 32 | uint64_t('o') << 24 | uint64_t('f') << 16 |
204          uint64_t('R') << 8 | uint64_t(129);
205 }
206
207 // The definition should match the structure defined in
208 // compiler-rt/lib/profile/InstrProfiling.h.
209 // It should also match the synthesized type in
210 // Transforms/Instrumentation/InstrProfiling.cpp:getOrCreateRegionCounters.
211
212 template <class IntPtrT>
213 struct ProfileData {
214   const uint32_t NameSize;
215   const uint32_t NumCounters;
216   const uint64_t FuncHash;
217   const IntPtrT NamePtr;
218   const IntPtrT CounterPtr;
219 };
220
221 // The definition should match the header referenced in
222 // compiler-rt/lib/profile/InstrProfilingFile.c  and
223 // InstrProfilingBuffer.c.
224
225 struct Header {
226   const uint64_t Magic;
227   const uint64_t Version;
228   const uint64_t DataSize;
229   const uint64_t CountersSize;
230   const uint64_t NamesSize;
231   const uint64_t CountersDelta;
232   const uint64_t NamesDelta;
233 };
234
235 }  // end namespace RawInstrProf
236
237 } // end namespace llvm
238
239 namespace std {
240 template <>
241 struct is_error_code_enum<llvm::instrprof_error> : std::true_type {};
242 }
243
244 #endif // LLVM_PROFILEDATA_INSTRPROF_H_