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
73INSTR_PROF_DATA(const uint64_t, llvm::Type::getInt64Ty(Ctx), NameRef, \
74                ConstantInt::get(llvm::Type::getInt64Ty(Ctx), \
75		IndexedInstrProf::ComputeHash(getPGOFuncNameVarInitializer(Inc->getName()))))
76INSTR_PROF_DATA(const uint64_t, llvm::Type::getInt64Ty(Ctx), FuncHash, \
77                ConstantInt::get(llvm::Type::getInt64Ty(Ctx), \
78                Inc->getHash()->getZExtValue()))
79INSTR_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 */
86INSTR_PROF_DATA(const IntPtrT, llvm::Type::getInt8PtrTy(Ctx), FunctionPointer, \
87                FunctionAddr)
88INSTR_PROF_DATA(IntPtrT, llvm::Type::getInt8PtrTy(Ctx), Values, \
89                ValuesPtrExpr)
90INSTR_PROF_DATA(const uint32_t, llvm::Type::getInt32Ty(Ctx), NumCounters, \
91                ConstantInt::get(llvm::Type::getInt32Ty(Ctx), NumCounters))
92INSTR_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
115INSTR_PROF_VALUE_NODE(uint64_t, llvm::Type::getInt64Ty(Ctx), Value, \
116                      ConstantInt::get(llvm::Type::GetInt64Ty(Ctx), 0))
117INSTR_PROF_VALUE_NODE(uint64_t, llvm::Type::getInt64Ty(Ctx), Count, \
118                      ConstantInt::get(llvm::Type::GetInt64Ty(Ctx), 0))
119INSTR_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
131INSTR_PROF_RAW_HEADER(uint64_t, Magic, __llvm_profile_get_magic())
132INSTR_PROF_RAW_HEADER(uint64_t, Version, __llvm_profile_get_version())
133INSTR_PROF_RAW_HEADER(uint64_t, DataSize, DataSize)
134INSTR_PROF_RAW_HEADER(uint64_t, CountersSize, CountersSize)
135INSTR_PROF_RAW_HEADER(uint64_t, NamesSize,  NamesSize)
136INSTR_PROF_RAW_HEADER(uint64_t, CountersDelta, (uintptr_t)CountersBegin)
137INSTR_PROF_RAW_HEADER(uint64_t, NamesDelta, (uintptr_t)NamesBegin)
138INSTR_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
153VALUE_PROF_FUNC_PARAM(uint64_t, TargetValue, Type::getInt64Ty(Ctx)) \
154                      INSTR_PROF_COMMA
155VALUE_PROF_FUNC_PARAM(void *, Data, Type::getInt8PtrTy(Ctx)) INSTR_PROF_COMMA
156VALUE_PROF_FUNC_PARAM(uint32_t, CounterIndex, Type::getInt32Ty(Ctx))
157#undef VALUE_PROF_FUNC_PARAM
158#undef INSTR_PROF_COMMA
159/* VALUE_PROF_FUNC_PARAM end */
160
161/* VALUE_PROF_KIND start */
162#ifndef VALUE_PROF_KIND
163#define VALUE_PROF_KIND(Enumerator, Value)
164#else
165#define INSTR_PROF_DATA_DEFINED
166#endif
167/* For indirect function call value profiling, the addresses of the target
168 * functions are profiled by the instrumented code. The target addresses are
169 * written in the raw profile data and converted to target function name's MD5
170 * hash by the profile reader during deserialization.  Typically, this happens
171 * when the the raw profile data is read during profile merging.
172 *
173 * For this remapping the ProfData is used.  ProfData contains both the function
174 * name hash and the function address.
175 */
176VALUE_PROF_KIND(IPVK_IndirectCallTarget, 0)
177/* These two kinds must be the last to be
178 * declared. This is to make sure the string
179 * array created with the template can be
180 * indexed with the kind value.
181 */
182VALUE_PROF_KIND(IPVK_First, IPVK_IndirectCallTarget)
183VALUE_PROF_KIND(IPVK_Last, IPVK_IndirectCallTarget)
184
185#undef VALUE_PROF_KIND
186/* VALUE_PROF_KIND end */
187
188/* COVMAP_FUNC_RECORD start */
189/* Definition of member fields of the function record structure in coverage
190 * map.
191 */
192#ifndef COVMAP_FUNC_RECORD
193#define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Initializer)
194#else
195#define INSTR_PROF_DATA_DEFINED
196#endif
197#ifdef COVMAP_V1
198COVMAP_FUNC_RECORD(const IntPtrT, llvm::Type::getInt8PtrTy(Ctx), \
199                   NamePtr, llvm::ConstantExpr::getBitCast(NamePtr, \
200                   llvm::Type::getInt8PtrTy(Ctx)))
201COVMAP_FUNC_RECORD(const uint32_t, llvm::Type::getInt32Ty(Ctx), NameSize, \
202                   llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), \
203                   NameValue.size()))
204#else
205COVMAP_FUNC_RECORD(const int64_t, llvm::Type::getInt64Ty(Ctx), NameRef, \
206                   llvm::ConstantInt::get(llvm::Type::getInt64Ty(Ctx), \
207	           llvm::IndexedInstrProf::ComputeHash(NameValue)))
208#endif
209COVMAP_FUNC_RECORD(const uint32_t, llvm::Type::getInt32Ty(Ctx), DataSize, \
210                   llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx),\
211                   CoverageMapping.size()))
212COVMAP_FUNC_RECORD(const uint64_t, llvm::Type::getInt64Ty(Ctx), FuncHash, \
213                   llvm::ConstantInt::get(llvm::Type::getInt64Ty(Ctx), FuncHash))
214#undef COVMAP_FUNC_RECORD
215/* COVMAP_FUNC_RECORD end.  */
216
217/* COVMAP_HEADER start */
218/* Definition of member fields of coverage map header.
219 */
220#ifndef COVMAP_HEADER
221#define COVMAP_HEADER(Type, LLVMType, Name, Initializer)
222#else
223#define INSTR_PROF_DATA_DEFINED
224#endif
225COVMAP_HEADER(uint32_t, Int32Ty, NRecords, \
226              llvm::ConstantInt::get(Int32Ty,  FunctionRecords.size()))
227COVMAP_HEADER(uint32_t, Int32Ty, FilenamesSize, \
228              llvm::ConstantInt::get(Int32Ty, FilenamesSize))
229COVMAP_HEADER(uint32_t, Int32Ty, CoverageSize, \
230              llvm::ConstantInt::get(Int32Ty, CoverageMappingSize))
231COVMAP_HEADER(uint32_t, Int32Ty, Version, \
232              llvm::ConstantInt::get(Int32Ty, CovMapVersion::CurrentVersion))
233#undef COVMAP_HEADER
234/* COVMAP_HEADER end.  */
235
236
237#ifdef INSTR_PROF_VALUE_PROF_DATA
238#define INSTR_PROF_DATA_DEFINED
239
240#define INSTR_PROF_MAX_NUM_VAL_PER_SITE 255
241/*!
242 * This is the header of the data structure that defines the on-disk
243 * layout of the value profile data of a particular kind for one function.
244 */
245typedef struct ValueProfRecord {
246  /* The kind of the value profile record. */
247  uint32_t Kind;
248  /*
249   * The number of value profile sites. It is guaranteed to be non-zero;
250   * otherwise the record for this kind won't be emitted.
251   */
252  uint32_t NumValueSites;
253  /*
254   * The first element of the array that stores the number of profiled
255   * values for each value site. The size of the array is NumValueSites.
256   * Since NumValueSites is greater than zero, there is at least one
257   * element in the array.
258   */
259  uint8_t SiteCountArray[1];
260
261  /*
262   * The fake declaration is for documentation purpose only.
263   * Align the start of next field to be on 8 byte boundaries.
264  uint8_t Padding[X];
265   */
266
267  /* The array of value profile data. The size of the array is the sum
268   * of all elements in SiteCountArray[].
269  InstrProfValueData ValueData[];
270   */
271
272#ifdef __cplusplus
273  /*!
274   * \brief Return the number of value sites.
275   */
276  uint32_t getNumValueSites() const { return NumValueSites; }
277  /*!
278   * \brief Read data from this record and save it to Record.
279   */
280  void deserializeTo(InstrProfRecord &Record,
281                     InstrProfRecord::ValueMapType *VMap);
282  /*
283   * In-place byte swap:
284   * Do byte swap for this instance. \c Old is the original order before
285   * the swap, and \c New is the New byte order.
286   */
287  void swapBytes(support::endianness Old, support::endianness New);
288#endif
289} ValueProfRecord;
290
291/*!
292 * Per-function header/control data structure for value profiling
293 * data in indexed format.
294 */
295typedef struct ValueProfData {
296  /*
297   * Total size in bytes including this field. It must be a multiple
298   * of sizeof(uint64_t).
299   */
300  uint32_t TotalSize;
301  /*
302   *The number of value profile kinds that has value profile data.
303   * In this implementation, a value profile kind is considered to
304   * have profile data if the number of value profile sites for the
305   * kind is not zero. More aggressively, the implementation can
306   * choose to check the actual data value: if none of the value sites
307   * has any profiled values, the kind can be skipped.
308   */
309  uint32_t NumValueKinds;
310
311  /*
312   * Following are a sequence of variable length records. The prefix/header
313   * of each record is defined by ValueProfRecord type. The number of
314   * records is NumValueKinds.
315   * ValueProfRecord Record_1;
316   * ValueProfRecord Record_N;
317   */
318
319#if __cplusplus
320  /*!
321   * Return the total size in bytes of the on-disk value profile data
322   * given the data stored in Record.
323   */
324  static uint32_t getSize(const InstrProfRecord &Record);
325  /*!
326   * Return a pointer to \c ValueProfData instance ready to be streamed.
327   */
328  static std::unique_ptr<ValueProfData>
329  serializeFrom(const InstrProfRecord &Record);
330  /*!
331   * Check the integrity of the record.
332   */
333  Error checkIntegrity();
334  /*!
335   * Return a pointer to \c ValueProfileData instance ready to be read.
336   * All data in the instance are properly byte swapped. The input
337   * data is assumed to be in little endian order.
338   */
339  static Expected<std::unique_ptr<ValueProfData>>
340  getValueProfData(const unsigned char *SrcBuffer,
341                   const unsigned char *const SrcBufferEnd,
342                   support::endianness SrcDataEndianness);
343  /*!
344   * Swap byte order from \c Endianness order to host byte order.
345   */
346  void swapBytesToHost(support::endianness Endianness);
347  /*!
348   * Swap byte order from host byte order to \c Endianness order.
349   */
350  void swapBytesFromHost(support::endianness Endianness);
351  /*!
352   * Return the total size of \c ValueProfileData.
353   */
354  uint32_t getSize() const { return TotalSize; }
355  /*!
356   * Read data from this data and save it to \c Record.
357   */
358  void deserializeTo(InstrProfRecord &Record,
359                     InstrProfRecord::ValueMapType *VMap);
360  void operator delete(void *ptr) { ::operator delete(ptr); }
361#endif
362} ValueProfData;
363
364/*
365 * The closure is designed to abstact away two types of value profile data:
366 * - InstrProfRecord which is the primary data structure used to
367 *   represent profile data in host tools (reader, writer, and profile-use)
368 * - value profile runtime data structure suitable to be used by C
369 *   runtime library.
370 *
371 * Both sources of data need to serialize to disk/memory-buffer in common
372 * format: ValueProfData. The abstraction allows compiler-rt's raw profiler
373 * writer to share the same format and code with indexed profile writer.
374 *
375 * For documentation of the member methods below, refer to corresponding methods
376 * in class InstrProfRecord.
377 */
378typedef struct ValueProfRecordClosure {
379  const void *Record;
380  uint32_t (*GetNumValueKinds)(const void *Record);
381  uint32_t (*GetNumValueSites)(const void *Record, uint32_t VKind);
382  uint32_t (*GetNumValueData)(const void *Record, uint32_t VKind);
383  uint32_t (*GetNumValueDataForSite)(const void *R, uint32_t VK, uint32_t S);
384
385  /*
386   * After extracting the value profile data from the value profile record,
387   * this method is used to map the in-memory value to on-disk value. If
388   * the method is null, value will be written out untranslated.
389   */
390  uint64_t (*RemapValueData)(uint32_t, uint64_t Value);
391  void (*GetValueForSite)(const void *R, InstrProfValueData *Dst, uint32_t K,
392                          uint32_t S);
393  ValueProfData *(*AllocValueProfData)(size_t TotalSizeInBytes);
394} ValueProfRecordClosure;
395
396INSTR_PROF_VISIBILITY ValueProfRecord *
397getFirstValueProfRecord(ValueProfData *VPD);
398INSTR_PROF_VISIBILITY ValueProfRecord *
399getValueProfRecordNext(ValueProfRecord *VPR);
400INSTR_PROF_VISIBILITY InstrProfValueData *
401getValueProfRecordValueData(ValueProfRecord *VPR);
402INSTR_PROF_VISIBILITY uint32_t
403getValueProfRecordHeaderSize(uint32_t NumValueSites);
404
405#undef INSTR_PROF_VALUE_PROF_DATA
406#endif  /* INSTR_PROF_VALUE_PROF_DATA */
407
408
409#ifdef INSTR_PROF_COMMON_API_IMPL
410#define INSTR_PROF_DATA_DEFINED
411#ifdef __cplusplus
412#define INSTR_PROF_INLINE inline
413#define INSTR_PROF_NULLPTR nullptr
414#else
415#define INSTR_PROF_INLINE
416#define INSTR_PROF_NULLPTR NULL
417#endif
418
419#ifndef offsetof
420#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
421#endif
422
423/*!
424 * \brief Return the \c ValueProfRecord header size including the
425 * padding bytes.
426 */
427INSTR_PROF_VISIBILITY INSTR_PROF_INLINE
428uint32_t getValueProfRecordHeaderSize(uint32_t NumValueSites) {
429  uint32_t Size = offsetof(ValueProfRecord, SiteCountArray) +
430                  sizeof(uint8_t) * NumValueSites;
431  /* Round the size to multiple of 8 bytes. */
432  Size = (Size + 7) & ~7;
433  return Size;
434}
435
436/*!
437 * \brief Return the total size of the value profile record including the
438 * header and the value data.
439 */
440INSTR_PROF_VISIBILITY INSTR_PROF_INLINE
441uint32_t getValueProfRecordSize(uint32_t NumValueSites,
442                                uint32_t NumValueData) {
443  return getValueProfRecordHeaderSize(NumValueSites) +
444         sizeof(InstrProfValueData) * NumValueData;
445}
446
447/*!
448 * \brief Return the pointer to the start of value data array.
449 */
450INSTR_PROF_VISIBILITY INSTR_PROF_INLINE
451InstrProfValueData *getValueProfRecordValueData(ValueProfRecord *This) {
452  return (InstrProfValueData *)((char *)This + getValueProfRecordHeaderSize(
453                                                   This->NumValueSites));
454}
455
456/*!
457 * \brief Return the total number of value data for \c This record.
458 */
459INSTR_PROF_VISIBILITY INSTR_PROF_INLINE
460uint32_t getValueProfRecordNumValueData(ValueProfRecord *This) {
461  uint32_t NumValueData = 0;
462  uint32_t I;
463  for (I = 0; I < This->NumValueSites; I++)
464    NumValueData += This->SiteCountArray[I];
465  return NumValueData;
466}
467
468/*!
469 * \brief Use this method to advance to the next \c This \c ValueProfRecord.
470 */
471INSTR_PROF_VISIBILITY INSTR_PROF_INLINE
472ValueProfRecord *getValueProfRecordNext(ValueProfRecord *This) {
473  uint32_t NumValueData = getValueProfRecordNumValueData(This);
474  return (ValueProfRecord *)((char *)This +
475                             getValueProfRecordSize(This->NumValueSites,
476                                                    NumValueData));
477}
478
479/*!
480 * \brief Return the first \c ValueProfRecord instance.
481 */
482INSTR_PROF_VISIBILITY INSTR_PROF_INLINE
483ValueProfRecord *getFirstValueProfRecord(ValueProfData *This) {
484  return (ValueProfRecord *)((char *)This + sizeof(ValueProfData));
485}
486
487/* Closure based interfaces.  */
488
489/*!
490 * Return the total size in bytes of the on-disk value profile data
491 * given the data stored in Record.
492 */
493INSTR_PROF_VISIBILITY uint32_t
494getValueProfDataSize(ValueProfRecordClosure *Closure) {
495  uint32_t Kind;
496  uint32_t TotalSize = sizeof(ValueProfData);
497  const void *Record = Closure->Record;
498
499  for (Kind = IPVK_First; Kind <= IPVK_Last; Kind++) {
500    uint32_t NumValueSites = Closure->GetNumValueSites(Record, Kind);
501    if (!NumValueSites)
502      continue;
503    TotalSize += getValueProfRecordSize(NumValueSites,
504                                        Closure->GetNumValueData(Record, Kind));
505  }
506  return TotalSize;
507}
508
509/*!
510 * Extract value profile data of a function for the profile kind \c ValueKind
511 * from the \c Closure and serialize the data into \c This record instance.
512 */
513INSTR_PROF_VISIBILITY void
514serializeValueProfRecordFrom(ValueProfRecord *This,
515                             ValueProfRecordClosure *Closure,
516                             uint32_t ValueKind, uint32_t NumValueSites) {
517  uint32_t S;
518  const void *Record = Closure->Record;
519  This->Kind = ValueKind;
520  This->NumValueSites = NumValueSites;
521  InstrProfValueData *DstVD = getValueProfRecordValueData(This);
522
523  for (S = 0; S < NumValueSites; S++) {
524    uint32_t ND = Closure->GetNumValueDataForSite(Record, ValueKind, S);
525    This->SiteCountArray[S] = ND;
526    Closure->GetValueForSite(Record, DstVD, ValueKind, S);
527    DstVD += ND;
528  }
529}
530
531/*!
532 * Extract value profile data of a function  from the \c Closure
533 * and serialize the data into \c DstData if it is not NULL or heap
534 * memory allocated by the \c Closure's allocator method. If \c
535 * DstData is not null, the caller is expected to set the TotalSize
536 * in DstData.
537 */
538INSTR_PROF_VISIBILITY ValueProfData *
539serializeValueProfDataFrom(ValueProfRecordClosure *Closure,
540                           ValueProfData *DstData) {
541  uint32_t Kind;
542  uint32_t TotalSize =
543      DstData ? DstData->TotalSize : getValueProfDataSize(Closure);
544
545  ValueProfData *VPD =
546      DstData ? DstData : Closure->AllocValueProfData(TotalSize);
547
548  VPD->TotalSize = TotalSize;
549  VPD->NumValueKinds = Closure->GetNumValueKinds(Closure->Record);
550  ValueProfRecord *VR = getFirstValueProfRecord(VPD);
551  for (Kind = IPVK_First; Kind <= IPVK_Last; Kind++) {
552    uint32_t NumValueSites = Closure->GetNumValueSites(Closure->Record, Kind);
553    if (!NumValueSites)
554      continue;
555    serializeValueProfRecordFrom(VR, Closure, Kind, NumValueSites);
556    VR = getValueProfRecordNext(VR);
557  }
558  return VPD;
559}
560
561#undef INSTR_PROF_COMMON_API_IMPL
562#endif /* INSTR_PROF_COMMON_API_IMPL */
563
564/*============================================================================*/
565
566#ifndef INSTR_PROF_DATA_DEFINED
567
568#ifndef INSTR_PROF_DATA_INC
569#define INSTR_PROF_DATA_INC
570
571/* Helper macros.  */
572#define INSTR_PROF_SIMPLE_QUOTE(x) #x
573#define INSTR_PROF_QUOTE(x) INSTR_PROF_SIMPLE_QUOTE(x)
574#define INSTR_PROF_SIMPLE_CONCAT(x,y) x ## y
575#define INSTR_PROF_CONCAT(x,y) INSTR_PROF_SIMPLE_CONCAT(x,y)
576
577/* Magic number to detect file format and endianness.
578 * Use 255 at one end, since no UTF-8 file can use that character.  Avoid 0,
579 * so that utilities, like strings, don't grab it as a string.  129 is also
580 * invalid UTF-8, and high enough to be interesting.
581 * Use "lprofr" in the centre to stand for "LLVM Profile Raw", or "lprofR"
582 * for 32-bit platforms.
583 */
584#define INSTR_PROF_RAW_MAGIC_64 (uint64_t)255 << 56 | (uint64_t)'l' << 48 | \
585       (uint64_t)'p' << 40 | (uint64_t)'r' << 32 | (uint64_t)'o' << 24 |  \
586        (uint64_t)'f' << 16 | (uint64_t)'r' << 8 | (uint64_t)129
587#define INSTR_PROF_RAW_MAGIC_32 (uint64_t)255 << 56 | (uint64_t)'l' << 48 | \
588       (uint64_t)'p' << 40 | (uint64_t)'r' << 32 | (uint64_t)'o' << 24 |  \
589        (uint64_t)'f' << 16 | (uint64_t)'R' << 8 | (uint64_t)129
590
591/* Raw profile format version (start from 1). */
592#define INSTR_PROF_RAW_VERSION 4
593/* Indexed profile format version (start from 1). */
594#define INSTR_PROF_INDEX_VERSION 4
595/* Coverage mapping format vresion (start from 0). */
596#define INSTR_PROF_COVMAP_VERSION 1
597
598/* Profile version is always of type uint64_t. Reserve the upper 8 bits in the
599 * version for other variants of profile. We set the lowest bit of the upper 8
600 * bits (i.e. bit 56) to 1 to indicate if this is an IR-level instrumentaiton
601 * generated profile, and 0 if this is a Clang FE generated profile.
602 */
603#define VARIANT_MASKS_ALL 0xff00000000000000ULL
604#define GET_VERSION(V) ((V) & ~VARIANT_MASKS_ALL)
605#define VARIANT_MASK_IR_PROF (0x1ULL << 56)
606#define IR_LEVEL_PROF_VERSION_VAR __llvm_profile_raw_version
607
608/* Runtime section names and name strings.  */
609#define INSTR_PROF_DATA_SECT_NAME __llvm_prf_data
610#define INSTR_PROF_NAME_SECT_NAME __llvm_prf_names
611#define INSTR_PROF_CNTS_SECT_NAME __llvm_prf_cnts
612/* Array of pointers. Each pointer points to a list
613 * of value nodes associated with one value site.
614 */
615#define INSTR_PROF_VALS_SECT_NAME __llvm_prf_vals
616/* Value profile nodes section. */
617#define INSTR_PROF_VNODES_SECT_NAME __llvm_prf_vnds
618#define INSTR_PROF_COVMAP_SECT_NAME __llvm_covmap
619
620#define INSTR_PROF_DATA_SECT_NAME_STR                                          \
621  INSTR_PROF_QUOTE(INSTR_PROF_DATA_SECT_NAME)
622#define INSTR_PROF_NAME_SECT_NAME_STR                                          \
623  INSTR_PROF_QUOTE(INSTR_PROF_NAME_SECT_NAME)
624#define INSTR_PROF_CNTS_SECT_NAME_STR                                          \
625  INSTR_PROF_QUOTE(INSTR_PROF_CNTS_SECT_NAME)
626#define INSTR_PROF_COVMAP_SECT_NAME_STR                                        \
627  INSTR_PROF_QUOTE(INSTR_PROF_COVMAP_SECT_NAME)
628#define INSTR_PROF_VALS_SECT_NAME_STR                                          \
629  INSTR_PROF_QUOTE(INSTR_PROF_VALS_SECT_NAME)
630#define INSTR_PROF_VNODES_SECT_NAME_STR                                        \
631  INSTR_PROF_QUOTE(INSTR_PROF_VNODES_SECT_NAME)
632
633/* Macros to define start/stop section symbol for a given
634 * section on Linux. For instance
635 * INSTR_PROF_SECT_START(INSTR_PROF_DATA_SECT_NAME) will
636 * expand to __start___llvm_prof_data
637 */
638#define INSTR_PROF_SECT_START(Sect) \
639        INSTR_PROF_CONCAT(__start_,Sect)
640#define INSTR_PROF_SECT_STOP(Sect) \
641        INSTR_PROF_CONCAT(__stop_,Sect)
642
643/* Value Profiling API linkage name.  */
644#define INSTR_PROF_VALUE_PROF_FUNC __llvm_profile_instrument_target
645#define INSTR_PROF_VALUE_PROF_FUNC_STR \
646        INSTR_PROF_QUOTE(INSTR_PROF_VALUE_PROF_FUNC)
647
648/* InstrProfile per-function control data alignment.  */
649#define INSTR_PROF_DATA_ALIGNMENT 8
650
651/* The data structure that represents a tracked value by the
652 * value profiler.
653 */
654typedef struct InstrProfValueData {
655  /* Profiled value. */
656  uint64_t Value;
657  /* Number of times the value appears in the training run. */
658  uint64_t Count;
659} InstrProfValueData;
660
661#endif /* INSTR_PROF_DATA_INC */
662
663#else
664#undef INSTR_PROF_DATA_DEFINED
665#endif
666