]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/profile/InstrProfData.inc
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / profile / InstrProfData.inc
1 /*===-- InstrProfData.inc - instr profiling runtime structures -*- 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  * This is the master file that defines all the data structure, signature,
11  * constant literals that are shared across profiling runtime library,
12  * compiler (instrumentation), and host tools (reader/writer). The entities
13  * defined in this file affect the profile runtime ABI, the raw profile format,
14  * or both.
15  *
16  * The file has two identical copies. The master copy lives in LLVM and
17  * the other one  sits in compiler-rt/lib/profile directory. To make changes
18  * in this file, first modify the master copy and copy it over to compiler-rt.
19  * Testing of any change in this file can start only after the two copies are
20  * synced up.
21  *
22  * The first part of the file includes macros that defines types, names, and
23  * initializers for the member fields of the core data structures. The field
24  * declarations for one structure is enabled by defining the field activation
25  * macro associated with that structure. Only one field activation record
26  * can be defined at one time and the rest definitions will be filtered out by
27  * the preprocessor.
28  *
29  * Examples of how the template is used to instantiate structure definition:
30  * 1. To declare a structure:
31  *
32  * struct ProfData {
33  * #define INSTR_PROF_DATA(Type, LLVMType, Name, Initializer) \
34  *    Type Name;
35  * #include "llvm/ProfileData/InstrProfData.inc"
36  * };
37  *
38  * 2. To construct LLVM type arrays for the struct type:
39  *
40  * Type *DataTypes[] = {
41  * #define INSTR_PROF_DATA(Type, LLVMType, Name, Initializer) \
42  *   LLVMType,
43  * #include "llvm/ProfileData/InstrProfData.inc"
44  * };
45  *
46  * 4. To construct constant array for the initializers:
47  * #define INSTR_PROF_DATA(Type, LLVMType, Name, Initializer) \
48  *   Initializer,
49  * Constant *ConstantVals[] = {
50  * #include "llvm/ProfileData/InstrProfData.inc"
51  * };
52  *
53  *
54  * The second part of the file includes definitions all other entities that
55  * are related to runtime ABI and format. When no field activation macro is
56  * defined, this file can be included to introduce the definitions.
57  *
58 \*===----------------------------------------------------------------------===*/
59
60 /* Functions marked with INSTR_PROF_VISIBILITY must have hidden visibility in
61  * the compiler runtime. */
62 #ifndef INSTR_PROF_VISIBILITY
63 #define INSTR_PROF_VISIBILITY
64 #endif
65
66 /* INSTR_PROF_DATA start. */
67 /* Definition of member fields of the per-function control structure. */
68 #ifndef INSTR_PROF_DATA
69 #define INSTR_PROF_DATA(Type, LLVMType, Name, Initializer)
70 #else
71 #define INSTR_PROF_DATA_DEFINED
72 #endif
73 INSTR_PROF_DATA(const uint64_t, llvm::Type::getInt64Ty(Ctx), NameRef, \
74                 ConstantInt::get(llvm::Type::getInt64Ty(Ctx), \
75                 IndexedInstrProf::ComputeHash(getPGOFuncNameVarInitializer(Inc->getName()))))
76 INSTR_PROF_DATA(const uint64_t, llvm::Type::getInt64Ty(Ctx), FuncHash, \
77                 ConstantInt::get(llvm::Type::getInt64Ty(Ctx), \
78                 Inc->getHash()->getZExtValue()))
79 INSTR_PROF_DATA(const IntPtrT, llvm::Type::getInt64PtrTy(Ctx), CounterPtr, \
80                 ConstantExpr::getBitCast(CounterPtr, \
81                 llvm::Type::getInt64PtrTy(Ctx)))
82 /* This is used to map function pointers for the indirect call targets to
83  * function name hashes during the conversion from raw to merged profile
84  * data.
85  */
86 INSTR_PROF_DATA(const IntPtrT, llvm::Type::getInt8PtrTy(Ctx), FunctionPointer, \
87                 FunctionAddr)
88 INSTR_PROF_DATA(IntPtrT, llvm::Type::getInt8PtrTy(Ctx), Values, \
89                 ValuesPtrExpr)
90 INSTR_PROF_DATA(const uint32_t, llvm::Type::getInt32Ty(Ctx), NumCounters, \
91                 ConstantInt::get(llvm::Type::getInt32Ty(Ctx), NumCounters))
92 INSTR_PROF_DATA(const uint16_t, Int16ArrayTy, NumValueSites[IPVK_Last+1], \
93                 ConstantArray::get(Int16ArrayTy, Int16ArrayVals))
94 #undef INSTR_PROF_DATA
95 /* INSTR_PROF_DATA end. */
96
97
98 /* This is an internal data structure used by value profiler. It
99  * is defined here to allow serialization code sharing by LLVM
100  * to be used in unit test.
101  *
102  * typedef struct ValueProfNode {
103  *   // InstrProfValueData VData;
104  *   uint64_t Value;
105  *   uint64_t Count;
106  *   struct ValueProfNode *Next;
107  * } ValueProfNode;
108  */
109 /* INSTR_PROF_VALUE_NODE start. */
110 #ifndef INSTR_PROF_VALUE_NODE
111 #define INSTR_PROF_VALUE_NODE(Type, LLVMType, Name, Initializer)
112 #else
113 #define INSTR_PROF_DATA_DEFINED
114 #endif
115 INSTR_PROF_VALUE_NODE(uint64_t, llvm::Type::getInt64Ty(Ctx), Value, \
116                       ConstantInt::get(llvm::Type::GetInt64Ty(Ctx), 0))
117 INSTR_PROF_VALUE_NODE(uint64_t, llvm::Type::getInt64Ty(Ctx), Count, \
118                       ConstantInt::get(llvm::Type::GetInt64Ty(Ctx), 0))
119 INSTR_PROF_VALUE_NODE(PtrToNodeT, llvm::Type::getInt8PtrTy(Ctx), Next, \
120                       ConstantInt::get(llvm::Type::GetInt8PtrTy(Ctx), 0))
121 #undef INSTR_PROF_VALUE_NODE
122 /* INSTR_PROF_VALUE_NODE end. */
123
124 /* INSTR_PROF_RAW_HEADER  start */
125 /* Definition of member fields of the raw profile header data structure. */
126 #ifndef INSTR_PROF_RAW_HEADER
127 #define INSTR_PROF_RAW_HEADER(Type, Name, Initializer)
128 #else
129 #define INSTR_PROF_DATA_DEFINED
130 #endif
131 INSTR_PROF_RAW_HEADER(uint64_t, Magic, __llvm_profile_get_magic())
132 INSTR_PROF_RAW_HEADER(uint64_t, Version, __llvm_profile_get_version())
133 INSTR_PROF_RAW_HEADER(uint64_t, DataSize, DataSize)
134 INSTR_PROF_RAW_HEADER(uint64_t, CountersSize, CountersSize)
135 INSTR_PROF_RAW_HEADER(uint64_t, NamesSize,  NamesSize)
136 INSTR_PROF_RAW_HEADER(uint64_t, CountersDelta, (uintptr_t)CountersBegin)
137 INSTR_PROF_RAW_HEADER(uint64_t, NamesDelta, (uintptr_t)NamesBegin)
138 INSTR_PROF_RAW_HEADER(uint64_t, ValueKindLast, IPVK_Last)
139 #undef INSTR_PROF_RAW_HEADER
140 /* INSTR_PROF_RAW_HEADER  end */
141
142 /* VALUE_PROF_FUNC_PARAM start */
143 /* Definition of parameter types of the runtime API used to do value profiling
144  * for a given value site.
145  */
146 #ifndef VALUE_PROF_FUNC_PARAM
147 #define VALUE_PROF_FUNC_PARAM(ArgType, ArgName, ArgLLVMType)
148 #define INSTR_PROF_COMMA
149 #else
150 #define INSTR_PROF_DATA_DEFINED
151 #define INSTR_PROF_COMMA ,
152 #endif
153 VALUE_PROF_FUNC_PARAM(uint64_t, TargetValue, Type::getInt64Ty(Ctx)) \
154                       INSTR_PROF_COMMA
155 VALUE_PROF_FUNC_PARAM(void *, Data, Type::getInt8PtrTy(Ctx)) INSTR_PROF_COMMA
156 #ifndef VALUE_RANGE_PROF
157 VALUE_PROF_FUNC_PARAM(uint32_t, CounterIndex, Type::getInt32Ty(Ctx))
158 #else /* VALUE_RANGE_PROF */
159 VALUE_PROF_FUNC_PARAM(uint32_t, CounterIndex, Type::getInt32Ty(Ctx)) \
160                       INSTR_PROF_COMMA
161 VALUE_PROF_FUNC_PARAM(uint64_t, PreciseRangeStart, Type::getInt64Ty(Ctx)) \
162                       INSTR_PROF_COMMA
163 VALUE_PROF_FUNC_PARAM(uint64_t, PreciseRangeLast, Type::getInt64Ty(Ctx)) \
164                       INSTR_PROF_COMMA
165 VALUE_PROF_FUNC_PARAM(uint64_t, LargeValue, Type::getInt64Ty(Ctx))
166 #endif /*VALUE_RANGE_PROF */
167 #undef VALUE_PROF_FUNC_PARAM
168 #undef INSTR_PROF_COMMA
169 /* VALUE_PROF_FUNC_PARAM end */
170
171 /* VALUE_PROF_KIND start */
172 #ifndef VALUE_PROF_KIND
173 #define VALUE_PROF_KIND(Enumerator, Value)
174 #else
175 #define INSTR_PROF_DATA_DEFINED
176 #endif
177 /* For indirect function call value profiling, the addresses of the target
178  * functions are profiled by the instrumented code. The target addresses are
179  * written in the raw profile data and converted to target function name's MD5
180  * hash by the profile reader during deserialization.  Typically, this happens
181  * when the raw profile data is read during profile merging.
182  *
183  * For this remapping the ProfData is used.  ProfData contains both the function
184  * name hash and the function address.
185  */
186 VALUE_PROF_KIND(IPVK_IndirectCallTarget, 0)
187 /* For memory intrinsic functions size profiling. */
188 VALUE_PROF_KIND(IPVK_MemOPSize, 1)
189 /* These two kinds must be the last to be
190  * declared. This is to make sure the string
191  * array created with the template can be
192  * indexed with the kind value.
193  */
194 VALUE_PROF_KIND(IPVK_First, IPVK_IndirectCallTarget)
195 VALUE_PROF_KIND(IPVK_Last, IPVK_MemOPSize)
196
197 #undef VALUE_PROF_KIND
198 /* VALUE_PROF_KIND end */
199
200 /* COVMAP_FUNC_RECORD start */
201 /* Definition of member fields of the function record structure in coverage
202  * map.
203  */
204 #ifndef COVMAP_FUNC_RECORD
205 #define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Initializer)
206 #else
207 #define INSTR_PROF_DATA_DEFINED
208 #endif
209 #ifdef COVMAP_V1
210 COVMAP_FUNC_RECORD(const IntPtrT, llvm::Type::getInt8PtrTy(Ctx), \
211                    NamePtr, llvm::ConstantExpr::getBitCast(NamePtr, \
212                    llvm::Type::getInt8PtrTy(Ctx)))
213 COVMAP_FUNC_RECORD(const uint32_t, llvm::Type::getInt32Ty(Ctx), NameSize, \
214                    llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), \
215                    NameValue.size()))
216 #else
217 COVMAP_FUNC_RECORD(const int64_t, llvm::Type::getInt64Ty(Ctx), NameRef, \
218                    llvm::ConstantInt::get(llvm::Type::getInt64Ty(Ctx), \
219                    llvm::IndexedInstrProf::ComputeHash(NameValue)))
220 #endif
221 COVMAP_FUNC_RECORD(const uint32_t, llvm::Type::getInt32Ty(Ctx), DataSize, \
222                    llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx),\
223                    CoverageMapping.size()))
224 COVMAP_FUNC_RECORD(const uint64_t, llvm::Type::getInt64Ty(Ctx), FuncHash, \
225                    llvm::ConstantInt::get(llvm::Type::getInt64Ty(Ctx), FuncHash))
226 #undef COVMAP_FUNC_RECORD
227 /* COVMAP_FUNC_RECORD end.  */
228
229 /* COVMAP_HEADER start */
230 /* Definition of member fields of coverage map header.
231  */
232 #ifndef COVMAP_HEADER
233 #define COVMAP_HEADER(Type, LLVMType, Name, Initializer)
234 #else
235 #define INSTR_PROF_DATA_DEFINED
236 #endif
237 COVMAP_HEADER(uint32_t, Int32Ty, NRecords, \
238               llvm::ConstantInt::get(Int32Ty,  FunctionRecords.size()))
239 COVMAP_HEADER(uint32_t, Int32Ty, FilenamesSize, \
240               llvm::ConstantInt::get(Int32Ty, FilenamesSize))
241 COVMAP_HEADER(uint32_t, Int32Ty, CoverageSize, \
242               llvm::ConstantInt::get(Int32Ty, CoverageMappingSize))
243 COVMAP_HEADER(uint32_t, Int32Ty, Version, \
244               llvm::ConstantInt::get(Int32Ty, CovMapVersion::CurrentVersion))
245 #undef COVMAP_HEADER
246 /* COVMAP_HEADER end.  */
247
248
249 #ifdef INSTR_PROF_SECT_ENTRY
250 #define INSTR_PROF_DATA_DEFINED
251 INSTR_PROF_SECT_ENTRY(IPSK_data, \
252                       INSTR_PROF_QUOTE(INSTR_PROF_DATA_COMMON), \
253                       INSTR_PROF_QUOTE(INSTR_PROF_DATA_COFF), "__DATA,")
254 INSTR_PROF_SECT_ENTRY(IPSK_cnts, \
255                       INSTR_PROF_QUOTE(INSTR_PROF_CNTS_COMMON), \
256                       INSTR_PROF_QUOTE(INSTR_PROF_CNTS_COFF), "__DATA,")
257 INSTR_PROF_SECT_ENTRY(IPSK_name, \
258                       INSTR_PROF_QUOTE(INSTR_PROF_NAME_COMMON), \
259                       INSTR_PROF_QUOTE(INSTR_PROF_NAME_COFF), "__DATA,")
260 INSTR_PROF_SECT_ENTRY(IPSK_vals, \
261                       INSTR_PROF_QUOTE(INSTR_PROF_VALS_COMMON), \
262                       INSTR_PROF_QUOTE(INSTR_PROF_VALS_COFF), "__DATA,")
263 INSTR_PROF_SECT_ENTRY(IPSK_vnodes, \
264                       INSTR_PROF_QUOTE(INSTR_PROF_VNODES_COMMON), \
265                       INSTR_PROF_QUOTE(INSTR_PROF_VNODES_COFF), "__DATA,")
266 INSTR_PROF_SECT_ENTRY(IPSK_covmap, \
267                       INSTR_PROF_QUOTE(INSTR_PROF_COVMAP_COMMON), \
268                       INSTR_PROF_QUOTE(INSTR_PROF_COVMAP_COFF), "__LLVM_COV,")
269
270 #undef INSTR_PROF_SECT_ENTRY
271 #endif
272
273
274 #ifdef INSTR_PROF_VALUE_PROF_DATA
275 #define INSTR_PROF_DATA_DEFINED
276
277 #define INSTR_PROF_MAX_NUM_VAL_PER_SITE 255
278 /*!
279  * This is the header of the data structure that defines the on-disk
280  * layout of the value profile data of a particular kind for one function.
281  */
282 typedef struct ValueProfRecord {
283   /* The kind of the value profile record. */
284   uint32_t Kind;
285   /*
286    * The number of value profile sites. It is guaranteed to be non-zero;
287    * otherwise the record for this kind won't be emitted.
288    */
289   uint32_t NumValueSites;
290   /*
291    * The first element of the array that stores the number of profiled
292    * values for each value site. The size of the array is NumValueSites.
293    * Since NumValueSites is greater than zero, there is at least one
294    * element in the array.
295    */
296   uint8_t SiteCountArray[1];
297
298   /*
299    * The fake declaration is for documentation purpose only.
300    * Align the start of next field to be on 8 byte boundaries.
301   uint8_t Padding[X];
302    */
303
304   /* The array of value profile data. The size of the array is the sum
305    * of all elements in SiteCountArray[].
306   InstrProfValueData ValueData[];
307    */
308
309 #ifdef __cplusplus
310   /*!
311    * \brief Return the number of value sites.
312    */
313   uint32_t getNumValueSites() const { return NumValueSites; }
314   /*!
315    * \brief Read data from this record and save it to Record.
316    */
317   void deserializeTo(InstrProfRecord &Record,
318                      InstrProfRecord::ValueMapType *VMap);
319   /*
320    * In-place byte swap:
321    * Do byte swap for this instance. \c Old is the original order before
322    * the swap, and \c New is the New byte order.
323    */
324   void swapBytes(support::endianness Old, support::endianness New);
325 #endif
326 } ValueProfRecord;
327
328 /*!
329  * Per-function header/control data structure for value profiling
330  * data in indexed format.
331  */
332 typedef struct ValueProfData {
333   /*
334    * Total size in bytes including this field. It must be a multiple
335    * of sizeof(uint64_t).
336    */
337   uint32_t TotalSize;
338   /*
339    *The number of value profile kinds that has value profile data.
340    * In this implementation, a value profile kind is considered to
341    * have profile data if the number of value profile sites for the
342    * kind is not zero. More aggressively, the implementation can
343    * choose to check the actual data value: if none of the value sites
344    * has any profiled values, the kind can be skipped.
345    */
346   uint32_t NumValueKinds;
347
348   /*
349    * Following are a sequence of variable length records. The prefix/header
350    * of each record is defined by ValueProfRecord type. The number of
351    * records is NumValueKinds.
352    * ValueProfRecord Record_1;
353    * ValueProfRecord Record_N;
354    */
355
356 #if __cplusplus
357   /*!
358    * Return the total size in bytes of the on-disk value profile data
359    * given the data stored in Record.
360    */
361   static uint32_t getSize(const InstrProfRecord &Record);
362   /*!
363    * Return a pointer to \c ValueProfData instance ready to be streamed.
364    */
365   static std::unique_ptr<ValueProfData>
366   serializeFrom(const InstrProfRecord &Record);
367   /*!
368    * Check the integrity of the record.
369    */
370   Error checkIntegrity();
371   /*!
372    * Return a pointer to \c ValueProfileData instance ready to be read.
373    * All data in the instance are properly byte swapped. The input
374    * data is assumed to be in little endian order.
375    */
376   static Expected<std::unique_ptr<ValueProfData>>
377   getValueProfData(const unsigned char *SrcBuffer,
378                    const unsigned char *const SrcBufferEnd,
379                    support::endianness SrcDataEndianness);
380   /*!
381    * Swap byte order from \c Endianness order to host byte order.
382    */
383   void swapBytesToHost(support::endianness Endianness);
384   /*!
385    * Swap byte order from host byte order to \c Endianness order.
386    */
387   void swapBytesFromHost(support::endianness Endianness);
388   /*!
389    * Return the total size of \c ValueProfileData.
390    */
391   uint32_t getSize() const { return TotalSize; }
392   /*!
393    * Read data from this data and save it to \c Record.
394    */
395   void deserializeTo(InstrProfRecord &Record,
396                      InstrProfRecord::ValueMapType *VMap);
397   void operator delete(void *ptr) { ::operator delete(ptr); }
398 #endif
399 } ValueProfData;
400
401 /*
402  * The closure is designed to abstact away two types of value profile data:
403  * - InstrProfRecord which is the primary data structure used to
404  *   represent profile data in host tools (reader, writer, and profile-use)
405  * - value profile runtime data structure suitable to be used by C
406  *   runtime library.
407  *
408  * Both sources of data need to serialize to disk/memory-buffer in common
409  * format: ValueProfData. The abstraction allows compiler-rt's raw profiler
410  * writer to share the same format and code with indexed profile writer.
411  *
412  * For documentation of the member methods below, refer to corresponding methods
413  * in class InstrProfRecord.
414  */
415 typedef struct ValueProfRecordClosure {
416   const void *Record;
417   uint32_t (*GetNumValueKinds)(const void *Record);
418   uint32_t (*GetNumValueSites)(const void *Record, uint32_t VKind);
419   uint32_t (*GetNumValueData)(const void *Record, uint32_t VKind);
420   uint32_t (*GetNumValueDataForSite)(const void *R, uint32_t VK, uint32_t S);
421
422   /*
423    * After extracting the value profile data from the value profile record,
424    * this method is used to map the in-memory value to on-disk value. If
425    * the method is null, value will be written out untranslated.
426    */
427   uint64_t (*RemapValueData)(uint32_t, uint64_t Value);
428   void (*GetValueForSite)(const void *R, InstrProfValueData *Dst, uint32_t K,
429                           uint32_t S);
430   ValueProfData *(*AllocValueProfData)(size_t TotalSizeInBytes);
431 } ValueProfRecordClosure;
432
433 INSTR_PROF_VISIBILITY ValueProfRecord *
434 getFirstValueProfRecord(ValueProfData *VPD);
435 INSTR_PROF_VISIBILITY ValueProfRecord *
436 getValueProfRecordNext(ValueProfRecord *VPR);
437 INSTR_PROF_VISIBILITY InstrProfValueData *
438 getValueProfRecordValueData(ValueProfRecord *VPR);
439 INSTR_PROF_VISIBILITY uint32_t
440 getValueProfRecordHeaderSize(uint32_t NumValueSites);
441
442 #undef INSTR_PROF_VALUE_PROF_DATA
443 #endif  /* INSTR_PROF_VALUE_PROF_DATA */
444
445
446 #ifdef INSTR_PROF_COMMON_API_IMPL
447 #define INSTR_PROF_DATA_DEFINED
448 #ifdef __cplusplus
449 #define INSTR_PROF_INLINE inline
450 #define INSTR_PROF_NULLPTR nullptr
451 #else
452 #define INSTR_PROF_INLINE
453 #define INSTR_PROF_NULLPTR NULL
454 #endif
455
456 #ifndef offsetof
457 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
458 #endif
459
460 /*!
461  * \brief Return the \c ValueProfRecord header size including the
462  * padding bytes.
463  */
464 INSTR_PROF_VISIBILITY INSTR_PROF_INLINE
465 uint32_t getValueProfRecordHeaderSize(uint32_t NumValueSites) {
466   uint32_t Size = offsetof(ValueProfRecord, SiteCountArray) +
467                   sizeof(uint8_t) * NumValueSites;
468   /* Round the size to multiple of 8 bytes. */
469   Size = (Size + 7) & ~7;
470   return Size;
471 }
472
473 /*!
474  * \brief Return the total size of the value profile record including the
475  * header and the value data.
476  */
477 INSTR_PROF_VISIBILITY INSTR_PROF_INLINE
478 uint32_t getValueProfRecordSize(uint32_t NumValueSites,
479                                 uint32_t NumValueData) {
480   return getValueProfRecordHeaderSize(NumValueSites) +
481          sizeof(InstrProfValueData) * NumValueData;
482 }
483
484 /*!
485  * \brief Return the pointer to the start of value data array.
486  */
487 INSTR_PROF_VISIBILITY INSTR_PROF_INLINE
488 InstrProfValueData *getValueProfRecordValueData(ValueProfRecord *This) {
489   return (InstrProfValueData *)((char *)This + getValueProfRecordHeaderSize(
490                                                    This->NumValueSites));
491 }
492
493 /*!
494  * \brief Return the total number of value data for \c This record.
495  */
496 INSTR_PROF_VISIBILITY INSTR_PROF_INLINE
497 uint32_t getValueProfRecordNumValueData(ValueProfRecord *This) {
498   uint32_t NumValueData = 0;
499   uint32_t I;
500   for (I = 0; I < This->NumValueSites; I++)
501     NumValueData += This->SiteCountArray[I];
502   return NumValueData;
503 }
504
505 /*!
506  * \brief Use this method to advance to the next \c This \c ValueProfRecord.
507  */
508 INSTR_PROF_VISIBILITY INSTR_PROF_INLINE
509 ValueProfRecord *getValueProfRecordNext(ValueProfRecord *This) {
510   uint32_t NumValueData = getValueProfRecordNumValueData(This);
511   return (ValueProfRecord *)((char *)This +
512                              getValueProfRecordSize(This->NumValueSites,
513                                                     NumValueData));
514 }
515
516 /*!
517  * \brief Return the first \c ValueProfRecord instance.
518  */
519 INSTR_PROF_VISIBILITY INSTR_PROF_INLINE
520 ValueProfRecord *getFirstValueProfRecord(ValueProfData *This) {
521   return (ValueProfRecord *)((char *)This + sizeof(ValueProfData));
522 }
523
524 /* Closure based interfaces.  */
525
526 /*!
527  * Return the total size in bytes of the on-disk value profile data
528  * given the data stored in Record.
529  */
530 INSTR_PROF_VISIBILITY uint32_t
531 getValueProfDataSize(ValueProfRecordClosure *Closure) {
532   uint32_t Kind;
533   uint32_t TotalSize = sizeof(ValueProfData);
534   const void *Record = Closure->Record;
535
536   for (Kind = IPVK_First; Kind <= IPVK_Last; Kind++) {
537     uint32_t NumValueSites = Closure->GetNumValueSites(Record, Kind);
538     if (!NumValueSites)
539       continue;
540     TotalSize += getValueProfRecordSize(NumValueSites,
541                                         Closure->GetNumValueData(Record, Kind));
542   }
543   return TotalSize;
544 }
545
546 /*!
547  * Extract value profile data of a function for the profile kind \c ValueKind
548  * from the \c Closure and serialize the data into \c This record instance.
549  */
550 INSTR_PROF_VISIBILITY void
551 serializeValueProfRecordFrom(ValueProfRecord *This,
552                              ValueProfRecordClosure *Closure,
553                              uint32_t ValueKind, uint32_t NumValueSites) {
554   uint32_t S;
555   const void *Record = Closure->Record;
556   This->Kind = ValueKind;
557   This->NumValueSites = NumValueSites;
558   InstrProfValueData *DstVD = getValueProfRecordValueData(This);
559
560   for (S = 0; S < NumValueSites; S++) {
561     uint32_t ND = Closure->GetNumValueDataForSite(Record, ValueKind, S);
562     This->SiteCountArray[S] = ND;
563     Closure->GetValueForSite(Record, DstVD, ValueKind, S);
564     DstVD += ND;
565   }
566 }
567
568 /*!
569  * Extract value profile data of a function  from the \c Closure
570  * and serialize the data into \c DstData if it is not NULL or heap
571  * memory allocated by the \c Closure's allocator method. If \c
572  * DstData is not null, the caller is expected to set the TotalSize
573  * in DstData.
574  */
575 INSTR_PROF_VISIBILITY ValueProfData *
576 serializeValueProfDataFrom(ValueProfRecordClosure *Closure,
577                            ValueProfData *DstData) {
578   uint32_t Kind;
579   uint32_t TotalSize =
580       DstData ? DstData->TotalSize : getValueProfDataSize(Closure);
581
582   ValueProfData *VPD =
583       DstData ? DstData : Closure->AllocValueProfData(TotalSize);
584
585   VPD->TotalSize = TotalSize;
586   VPD->NumValueKinds = Closure->GetNumValueKinds(Closure->Record);
587   ValueProfRecord *VR = getFirstValueProfRecord(VPD);
588   for (Kind = IPVK_First; Kind <= IPVK_Last; Kind++) {
589     uint32_t NumValueSites = Closure->GetNumValueSites(Closure->Record, Kind);
590     if (!NumValueSites)
591       continue;
592     serializeValueProfRecordFrom(VR, Closure, Kind, NumValueSites);
593     VR = getValueProfRecordNext(VR);
594   }
595   return VPD;
596 }
597
598 #undef INSTR_PROF_COMMON_API_IMPL
599 #endif /* INSTR_PROF_COMMON_API_IMPL */
600
601 /*============================================================================*/
602
603 #ifndef INSTR_PROF_DATA_DEFINED
604
605 #ifndef INSTR_PROF_DATA_INC
606 #define INSTR_PROF_DATA_INC
607
608 /* Helper macros.  */
609 #define INSTR_PROF_SIMPLE_QUOTE(x) #x
610 #define INSTR_PROF_QUOTE(x) INSTR_PROF_SIMPLE_QUOTE(x)
611 #define INSTR_PROF_SIMPLE_CONCAT(x,y) x ## y
612 #define INSTR_PROF_CONCAT(x,y) INSTR_PROF_SIMPLE_CONCAT(x,y)
613
614 /* Magic number to detect file format and endianness.
615  * Use 255 at one end, since no UTF-8 file can use that character.  Avoid 0,
616  * so that utilities, like strings, don't grab it as a string.  129 is also
617  * invalid UTF-8, and high enough to be interesting.
618  * Use "lprofr" in the centre to stand for "LLVM Profile Raw", or "lprofR"
619  * for 32-bit platforms.
620  */
621 #define INSTR_PROF_RAW_MAGIC_64 (uint64_t)255 << 56 | (uint64_t)'l' << 48 | \
622        (uint64_t)'p' << 40 | (uint64_t)'r' << 32 | (uint64_t)'o' << 24 |  \
623         (uint64_t)'f' << 16 | (uint64_t)'r' << 8 | (uint64_t)129
624 #define INSTR_PROF_RAW_MAGIC_32 (uint64_t)255 << 56 | (uint64_t)'l' << 48 | \
625        (uint64_t)'p' << 40 | (uint64_t)'r' << 32 | (uint64_t)'o' << 24 |  \
626         (uint64_t)'f' << 16 | (uint64_t)'R' << 8 | (uint64_t)129
627
628 /* Raw profile format version (start from 1). */
629 #define INSTR_PROF_RAW_VERSION 4
630 /* Indexed profile format version (start from 1). */
631 #define INSTR_PROF_INDEX_VERSION 5
632 /* Coverage mapping format vresion (start from 0). */
633 #define INSTR_PROF_COVMAP_VERSION 2
634
635 /* Profile version is always of type uint64_t. Reserve the upper 8 bits in the
636  * version for other variants of profile. We set the lowest bit of the upper 8
637  * bits (i.e. bit 56) to 1 to indicate if this is an IR-level instrumentaiton
638  * generated profile, and 0 if this is a Clang FE generated profile.
639  */
640 #define VARIANT_MASKS_ALL 0xff00000000000000ULL
641 #define GET_VERSION(V) ((V) & ~VARIANT_MASKS_ALL)
642 #define VARIANT_MASK_IR_PROF (0x1ULL << 56)
643 #define INSTR_PROF_RAW_VERSION_VAR __llvm_profile_raw_version
644 #define INSTR_PROF_PROFILE_RUNTIME_VAR __llvm_profile_runtime
645
646 /* The variable that holds the name of the profile data
647  * specified via command line. */
648 #define INSTR_PROF_PROFILE_NAME_VAR __llvm_profile_filename
649
650 /* section name strings common to all targets other
651    than WIN32 */
652 #define INSTR_PROF_DATA_COMMON __llvm_prf_data
653 #define INSTR_PROF_NAME_COMMON __llvm_prf_names
654 #define INSTR_PROF_CNTS_COMMON __llvm_prf_cnts
655 #define INSTR_PROF_VALS_COMMON __llvm_prf_vals
656 #define INSTR_PROF_VNODES_COMMON __llvm_prf_vnds
657 #define INSTR_PROF_COVMAP_COMMON __llvm_covmap
658 /* Win32 */
659 #define INSTR_PROF_DATA_COFF .lprfd
660 #define INSTR_PROF_NAME_COFF .lprfn
661 #define INSTR_PROF_CNTS_COFF .lprfc
662 #define INSTR_PROF_VALS_COFF .lprfv
663 #define INSTR_PROF_VNODES_COFF .lprfnd
664 #define INSTR_PROF_COVMAP_COFF .lcovmap
665
666 #ifdef _WIN32
667 /* Runtime section names and name strings.  */
668 #define INSTR_PROF_DATA_SECT_NAME INSTR_PROF_DATA_COFF
669 #define INSTR_PROF_NAME_SECT_NAME INSTR_PROF_NAME_COFF
670 #define INSTR_PROF_CNTS_SECT_NAME INSTR_PROF_CNTS_COFF
671 /* Array of pointers. Each pointer points to a list
672  * of value nodes associated with one value site.
673  */
674 #define INSTR_PROF_VALS_SECT_NAME INSTR_PROF_VALS_COFF
675 /* Value profile nodes section. */
676 #define INSTR_PROF_VNODES_SECT_NAME INSTR_PROF_VNODES_COFF
677 #define INSTR_PROF_COVMAP_SECT_NAME INSTR_PROF_COVMAP_COFF
678 #else
679 /* Runtime section names and name strings.  */
680 #define INSTR_PROF_DATA_SECT_NAME INSTR_PROF_DATA_COMMON
681 #define INSTR_PROF_NAME_SECT_NAME INSTR_PROF_NAME_COMMON
682 #define INSTR_PROF_CNTS_SECT_NAME INSTR_PROF_CNTS_COMMON
683 /* Array of pointers. Each pointer points to a list
684  * of value nodes associated with one value site.
685  */
686 #define INSTR_PROF_VALS_SECT_NAME INSTR_PROF_VALS_COMMON
687 /* Value profile nodes section. */
688 #define INSTR_PROF_VNODES_SECT_NAME INSTR_PROF_VNODES_COMMON
689 #define INSTR_PROF_COVMAP_SECT_NAME INSTR_PROF_COVMAP_COMMON
690 #endif
691
692 #define INSTR_PROF_DATA_SECT_NAME_STR                                          \
693   INSTR_PROF_QUOTE(INSTR_PROF_DATA_SECT_NAME)
694 #define INSTR_PROF_NAME_SECT_NAME_STR                                          \
695   INSTR_PROF_QUOTE(INSTR_PROF_NAME_SECT_NAME)
696 #define INSTR_PROF_CNTS_SECT_NAME_STR                                          \
697   INSTR_PROF_QUOTE(INSTR_PROF_CNTS_SECT_NAME)
698 #define INSTR_PROF_COVMAP_SECT_NAME_STR                                        \
699   INSTR_PROF_QUOTE(INSTR_PROF_COVMAP_SECT_NAME)
700 #define INSTR_PROF_VALS_SECT_NAME_STR                                          \
701   INSTR_PROF_QUOTE(INSTR_PROF_VALS_SECT_NAME)
702 #define INSTR_PROF_VNODES_SECT_NAME_STR                                        \
703   INSTR_PROF_QUOTE(INSTR_PROF_VNODES_SECT_NAME)
704
705 /* Macros to define start/stop section symbol for a given
706  * section on Linux. For instance
707  * INSTR_PROF_SECT_START(INSTR_PROF_DATA_SECT_NAME) will
708  * expand to __start___llvm_prof_data
709  */
710 #define INSTR_PROF_SECT_START(Sect) \
711         INSTR_PROF_CONCAT(__start_,Sect)
712 #define INSTR_PROF_SECT_STOP(Sect) \
713         INSTR_PROF_CONCAT(__stop_,Sect)
714
715 /* Value Profiling API linkage name.  */
716 #define INSTR_PROF_VALUE_PROF_FUNC __llvm_profile_instrument_target
717 #define INSTR_PROF_VALUE_PROF_FUNC_STR \
718         INSTR_PROF_QUOTE(INSTR_PROF_VALUE_PROF_FUNC)
719 #define INSTR_PROF_VALUE_RANGE_PROF_FUNC __llvm_profile_instrument_range
720 #define INSTR_PROF_VALUE_RANGE_PROF_FUNC_STR \
721         INSTR_PROF_QUOTE(INSTR_PROF_VALUE_RANGE_PROF_FUNC)
722
723 /* InstrProfile per-function control data alignment.  */
724 #define INSTR_PROF_DATA_ALIGNMENT 8
725
726 /* The data structure that represents a tracked value by the
727  * value profiler.
728  */
729 typedef struct InstrProfValueData {
730   /* Profiled value. */
731   uint64_t Value;
732   /* Number of times the value appears in the training run. */
733   uint64_t Count;
734 } InstrProfValueData;
735
736 #endif /* INSTR_PROF_DATA_INC */
737
738 #else
739 #undef INSTR_PROF_DATA_DEFINED
740 #endif