blob: 15a25abe98f3fe3fdd62bdf98afb1db1f18073d8 [file] [log] [blame]
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +00001/*===- InstrProfiling.h- Support library for PGO instrumentation ----------===*\
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
Duncan P. N. Exon Smithf1c93612014-03-21 18:47:23 +000010#ifndef PROFILE_INSTRPROFILING_H_
11#define PROFILE_INSTRPROFILING_H_
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +000012
Betul Buyukkurt808385f2015-11-18 18:12:35 +000013#ifdef _MSC_VER
14# define LLVM_ALIGNAS(x) __declspec(align(x))
15#elif __GNUC__ && !__has_feature(cxx_alignas)
16# define LLVM_ALIGNAS(x) __attribute__((aligned(x)))
17#else
18# define LLVM_ALIGNAS(x) alignas(x)
19#endif
20
Duncan P. N. Exon Smith8d02a2a2014-03-21 20:59:08 +000021#if defined(__FreeBSD__) && defined(__i386__)
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000022
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000023/* System headers define 'size_t' incorrectly on x64 FreeBSD (prior to
24 * FreeBSD 10, r232261) when compiled in 32-bit mode.
25 */
26#define PRIu64 "llu"
27typedef unsigned int uint32_t;
28typedef unsigned long long uint64_t;
Viktor Kutuzov9068dfa2014-03-26 12:00:44 +000029typedef uint32_t uintptr_t;
Duncan P. N. Exon Smith8d02a2a2014-03-21 20:59:08 +000030
31#else /* defined(__FreeBSD__) && defined(__i386__) */
32
33#include <inttypes.h>
34#include <stdint.h>
35
36#endif /* defined(__FreeBSD__) && defined(__i386__) */
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000037
Betul Buyukkurt808385f2015-11-18 18:12:35 +000038typedef enum ValueKind {
39 VK_IndirectCallTarget = 0,
40 VK_FIRST = VK_IndirectCallTarget,
41 VK_LAST = VK_IndirectCallTarget
42} __llvm_profile_value_kind;
Duncan P. N. Exon Smith812dcae2014-03-21 18:29:24 +000043
Betul Buyukkurt808385f2015-11-18 18:12:35 +000044typedef struct __llvm_profile_value_data {
45 uint64_t TargetValue;
46 uint64_t NumTaken;
47} __llvm_profile_value_data;
48
49typedef struct __llvm_profile_value_node {
50 __llvm_profile_value_data VData;
51 struct __llvm_profile_value_node *Next;
52} __llvm_profile_value_node;
53
54typedef struct LLVM_ALIGNAS(8) __llvm_profile_data {
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000055 const uint32_t NameSize;
56 const uint32_t NumCounters;
57 const uint64_t FuncHash;
Xinliang David Li470bfa92015-11-08 18:00:13 +000058 const char *const NamePtr;
59 uint64_t *const CounterPtr;
Betul Buyukkurt808385f2015-11-18 18:12:35 +000060 const uint8_t *FunctionPointer;
61 __llvm_profile_value_node **ValueCounters;
62 const uint16_t NumValueSites[VK_LAST + 1];
Duncan P. N. Exon Smith9edbae02014-03-20 20:00:44 +000063} __llvm_profile_data;
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000064
Xinliang David Li4da5de92015-10-16 22:21:56 +000065typedef struct __llvm_profile_header {
66 uint64_t Magic;
67 uint64_t Version;
68 uint64_t DataSize;
69 uint64_t CountersSize;
70 uint64_t NamesSize;
71 uint64_t CountersDelta;
72 uint64_t NamesDelta;
Betul Buyukkurt808385f2015-11-18 18:12:35 +000073 uint64_t ValueKindLast;
74 uint64_t ValueDataSize;
75 uint64_t ValueDataDelta;
Xinliang David Li4da5de92015-10-16 22:21:56 +000076} __llvm_profile_header;
77
Betul Buyukkurt808385f2015-11-18 18:12:35 +000078/*!
79 * \brief Get number of bytes necessary to pad the argument to eight
80 * byte boundary.
81 */
82uint8_t __llvm_profile_get_num_padding_bytes(uint64_t SizeInBytes);
Xinliang David Li4da5de92015-10-16 22:21:56 +000083
Duncan P. N. Exon Smithcf4bb962014-03-21 18:29:19 +000084/*!
85 * \brief Get required size for profile buffer.
86 */
87uint64_t __llvm_profile_get_size_for_buffer(void);
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000088
89/*!
90 * \brief Write instrumentation data to the given buffer.
91 *
Duncan P. N. Exon Smithcf4bb962014-03-21 18:29:19 +000092 * \pre \c Buffer is the start of a buffer at least as big as \a
93 * __llvm_profile_get_size_for_buffer().
Duncan P. N. Exon Smith8353a262014-03-19 22:10:27 +000094 */
Duncan P. N. Exon Smithcf4bb962014-03-21 18:29:19 +000095int __llvm_profile_write_buffer(char *Buffer);
Duncan P. N. Exon Smithda0de8a22014-03-20 03:23:10 +000096
Justin Bognercc0d7ee2014-09-04 15:45:31 +000097const __llvm_profile_data *__llvm_profile_begin_data(void);
98const __llvm_profile_data *__llvm_profile_end_data(void);
99const char *__llvm_profile_begin_names(void);
100const char *__llvm_profile_end_names(void);
101uint64_t *__llvm_profile_begin_counters(void);
102uint64_t *__llvm_profile_end_counters(void);
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000103
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000104/*!
Betul Buyukkurt808385f2015-11-18 18:12:35 +0000105 * \brief Counts the number of times a target value is seen.
106 *
107 * Records the target value for the CounterIndex if not seen before. Otherwise,
108 * increments the counter associated w/ the target value.
109 */
110void __llvm_profile_instrument_target(uint64_t TargetValue,
111 void *Data_, uint32_t CounterIndex);
112
113/*!
114 * \brief Prepares the value profiling data for output.
115 *
116 * Prepares a single __llvm_profile_value_data array out of the many
117 * __llvm_profile_value_node trees (one per instrumented function).
118 */
119uint64_t __llvm_profile_gather_value_data(uint8_t **DataArray);
120
121/*!
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000122 * \brief Write instrumentation data to the current file.
123 *
124 * Writes to the file with the last name given to \a __llvm_profile_set_filename(),
125 * or if it hasn't been called, the \c LLVM_PROFILE_FILE environment variable,
Eric Christopherd641b532015-04-28 22:54:51 +0000126 * or if that's not set, the last name given to
127 * \a __llvm_profile_override_default_filename(), or if that's not set,
Diego Novilloeae95142015-07-09 17:21:52 +0000128 * \c "default.profraw".
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000129 */
130int __llvm_profile_write_file(void);
131
132/*!
133 * \brief Set the filename for writing instrumentation data.
134 *
135 * Sets the filename to be used for subsequent calls to
136 * \a __llvm_profile_write_file().
137 *
138 * \c Name is not copied, so it must remain valid. Passing NULL resets the
139 * filename logic to the default behaviour.
140 */
141void __llvm_profile_set_filename(const char *Name);
142
Eric Christopherd641b532015-04-28 22:54:51 +0000143/*!
144 * \brief Set the filename for writing instrumentation data, unless the
145 * \c LLVM_PROFILE_FILE environment variable was set.
146 *
147 * Unless overridden, sets the filename to be used for subsequent calls to
148 * \a __llvm_profile_write_file().
149 *
150 * \c Name is not copied, so it must remain valid. Passing NULL resets the
151 * filename logic to the default behaviour (unless the \c LLVM_PROFILE_FILE
152 * was set in which case it has no effect).
153 */
154void __llvm_profile_override_default_filename(const char *Name);
155
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000156/*! \brief Register to write instrumentation data to file at exit. */
157int __llvm_profile_register_write_file_atexit(void);
158
Duncan P. N. Exon Smith55e4d662014-05-17 01:27:30 +0000159/*! \brief Initialize file handling. */
160void __llvm_profile_initialize_file(void);
Duncan P. N. Exon Smith08439882014-05-16 01:30:24 +0000161
Duncan P. N. Exon Smithbe0a5e12014-03-21 18:29:15 +0000162/*! \brief Get the magic token for the file format. */
163uint64_t __llvm_profile_get_magic(void);
164
165/*! \brief Get the version of the file format. */
166uint64_t __llvm_profile_get_version(void);
167
Duncan P. N. Exon Smithf1c93612014-03-21 18:47:23 +0000168#endif /* PROFILE_INSTRPROFILING_H_ */