Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 1 | /*===- 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 Smith | f1c9361 | 2014-03-21 18:47:23 +0000 | [diff] [blame] | 10 | #ifndef PROFILE_INSTRPROFILING_H_ |
| 11 | #define PROFILE_INSTRPROFILING_H_ |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 12 | |
Betul Buyukkurt | 808385f | 2015-11-18 18:12:35 +0000 | [diff] [blame^] | 13 | #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 Smith | 8d02a2a | 2014-03-21 20:59:08 +0000 | [diff] [blame] | 21 | #if defined(__FreeBSD__) && defined(__i386__) |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 22 | |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 23 | /* 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" |
| 27 | typedef unsigned int uint32_t; |
| 28 | typedef unsigned long long uint64_t; |
Viktor Kutuzov | 9068dfa | 2014-03-26 12:00:44 +0000 | [diff] [blame] | 29 | typedef uint32_t uintptr_t; |
Duncan P. N. Exon Smith | 8d02a2a | 2014-03-21 20:59:08 +0000 | [diff] [blame] | 30 | |
| 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 Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 37 | |
Betul Buyukkurt | 808385f | 2015-11-18 18:12:35 +0000 | [diff] [blame^] | 38 | typedef 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 Smith | 812dcae | 2014-03-21 18:29:24 +0000 | [diff] [blame] | 43 | |
Betul Buyukkurt | 808385f | 2015-11-18 18:12:35 +0000 | [diff] [blame^] | 44 | typedef struct __llvm_profile_value_data { |
| 45 | uint64_t TargetValue; |
| 46 | uint64_t NumTaken; |
| 47 | } __llvm_profile_value_data; |
| 48 | |
| 49 | typedef struct __llvm_profile_value_node { |
| 50 | __llvm_profile_value_data VData; |
| 51 | struct __llvm_profile_value_node *Next; |
| 52 | } __llvm_profile_value_node; |
| 53 | |
| 54 | typedef struct LLVM_ALIGNAS(8) __llvm_profile_data { |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 55 | const uint32_t NameSize; |
| 56 | const uint32_t NumCounters; |
| 57 | const uint64_t FuncHash; |
Xinliang David Li | 470bfa9 | 2015-11-08 18:00:13 +0000 | [diff] [blame] | 58 | const char *const NamePtr; |
| 59 | uint64_t *const CounterPtr; |
Betul Buyukkurt | 808385f | 2015-11-18 18:12:35 +0000 | [diff] [blame^] | 60 | const uint8_t *FunctionPointer; |
| 61 | __llvm_profile_value_node **ValueCounters; |
| 62 | const uint16_t NumValueSites[VK_LAST + 1]; |
Duncan P. N. Exon Smith | 9edbae0 | 2014-03-20 20:00:44 +0000 | [diff] [blame] | 63 | } __llvm_profile_data; |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 64 | |
Xinliang David Li | 4da5de9 | 2015-10-16 22:21:56 +0000 | [diff] [blame] | 65 | typedef 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 Buyukkurt | 808385f | 2015-11-18 18:12:35 +0000 | [diff] [blame^] | 73 | uint64_t ValueKindLast; |
| 74 | uint64_t ValueDataSize; |
| 75 | uint64_t ValueDataDelta; |
Xinliang David Li | 4da5de9 | 2015-10-16 22:21:56 +0000 | [diff] [blame] | 76 | } __llvm_profile_header; |
| 77 | |
Betul Buyukkurt | 808385f | 2015-11-18 18:12:35 +0000 | [diff] [blame^] | 78 | /*! |
| 79 | * \brief Get number of bytes necessary to pad the argument to eight |
| 80 | * byte boundary. |
| 81 | */ |
| 82 | uint8_t __llvm_profile_get_num_padding_bytes(uint64_t SizeInBytes); |
Xinliang David Li | 4da5de9 | 2015-10-16 22:21:56 +0000 | [diff] [blame] | 83 | |
Duncan P. N. Exon Smith | cf4bb96 | 2014-03-21 18:29:19 +0000 | [diff] [blame] | 84 | /*! |
| 85 | * \brief Get required size for profile buffer. |
| 86 | */ |
| 87 | uint64_t __llvm_profile_get_size_for_buffer(void); |
Duncan P. N. Exon Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 88 | |
| 89 | /*! |
| 90 | * \brief Write instrumentation data to the given buffer. |
| 91 | * |
Duncan P. N. Exon Smith | cf4bb96 | 2014-03-21 18:29:19 +0000 | [diff] [blame] | 92 | * \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 Smith | 8353a26 | 2014-03-19 22:10:27 +0000 | [diff] [blame] | 94 | */ |
Duncan P. N. Exon Smith | cf4bb96 | 2014-03-21 18:29:19 +0000 | [diff] [blame] | 95 | int __llvm_profile_write_buffer(char *Buffer); |
Duncan P. N. Exon Smith | da0de8a2 | 2014-03-20 03:23:10 +0000 | [diff] [blame] | 96 | |
Justin Bogner | cc0d7ee | 2014-09-04 15:45:31 +0000 | [diff] [blame] | 97 | const __llvm_profile_data *__llvm_profile_begin_data(void); |
| 98 | const __llvm_profile_data *__llvm_profile_end_data(void); |
| 99 | const char *__llvm_profile_begin_names(void); |
| 100 | const char *__llvm_profile_end_names(void); |
| 101 | uint64_t *__llvm_profile_begin_counters(void); |
| 102 | uint64_t *__llvm_profile_end_counters(void); |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 103 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 104 | /*! |
Betul Buyukkurt | 808385f | 2015-11-18 18:12:35 +0000 | [diff] [blame^] | 105 | * \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 | */ |
| 110 | void __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 | */ |
| 119 | uint64_t __llvm_profile_gather_value_data(uint8_t **DataArray); |
| 120 | |
| 121 | /*! |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 122 | * \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 Christopher | d641b53 | 2015-04-28 22:54:51 +0000 | [diff] [blame] | 126 | * 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 Novillo | eae9514 | 2015-07-09 17:21:52 +0000 | [diff] [blame] | 128 | * \c "default.profraw". |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 129 | */ |
| 130 | int __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 | */ |
| 141 | void __llvm_profile_set_filename(const char *Name); |
| 142 | |
Eric Christopher | d641b53 | 2015-04-28 22:54:51 +0000 | [diff] [blame] | 143 | /*! |
| 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 | */ |
| 154 | void __llvm_profile_override_default_filename(const char *Name); |
| 155 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 156 | /*! \brief Register to write instrumentation data to file at exit. */ |
| 157 | int __llvm_profile_register_write_file_atexit(void); |
| 158 | |
Duncan P. N. Exon Smith | 55e4d66 | 2014-05-17 01:27:30 +0000 | [diff] [blame] | 159 | /*! \brief Initialize file handling. */ |
| 160 | void __llvm_profile_initialize_file(void); |
Duncan P. N. Exon Smith | 0843988 | 2014-05-16 01:30:24 +0000 | [diff] [blame] | 161 | |
Duncan P. N. Exon Smith | be0a5e1 | 2014-03-21 18:29:15 +0000 | [diff] [blame] | 162 | /*! \brief Get the magic token for the file format. */ |
| 163 | uint64_t __llvm_profile_get_magic(void); |
| 164 | |
| 165 | /*! \brief Get the version of the file format. */ |
| 166 | uint64_t __llvm_profile_get_version(void); |
| 167 | |
Duncan P. N. Exon Smith | f1c9361 | 2014-03-21 18:47:23 +0000 | [diff] [blame] | 168 | #endif /* PROFILE_INSTRPROFILING_H_ */ |