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 
10 #ifndef PROFILE_INSTRPROFILING_H_
11 #define PROFILE_INSTRPROFILING_H_
12 
13 #ifdef _MSC_VER
14 # define LLVM_ALIGNAS(x) __declspec(align(x))
15 #elif __GNUC__
16 #define LLVM_ALIGNAS(x) __attribute__((aligned(x)))
17 #endif
18 
19 #define LLVM_LIBRARY_VISIBILITY __attribute__((visibility("hidden")))
20 #define LLVM_SECTION(Sect) __attribute__((section(Sect)))
21 
22 #define PROF_ERR(Format, ...) \
23  if (getenv("LLVM_PROFILE_VERBOSE_ERRORS")) \
24    fprintf(stderr, Format, __VA_ARGS__ );
25 
26 
27 #if defined(__FreeBSD__) && defined(__i386__)
28 
29 /* System headers define 'size_t' incorrectly on x64 FreeBSD (prior to
30  * FreeBSD 10, r232261) when compiled in 32-bit mode.
31  */
32 #define PRIu64 "llu"
33 typedef unsigned char uint8_t;
34 typedef unsigned short uint16_t;
35 typedef unsigned int uint32_t;
36 typedef unsigned long long uint64_t;
37 typedef uint32_t uintptr_t;
38 #elif defined(__FreeBSD__) && defined(__x86_64__)
39 #define PRIu64 "lu"
40 typedef unsigned char uint8_t;
41 typedef unsigned short uint16_t;
42 typedef unsigned int uint32_t;
43 typedef unsigned long long uint64_t;
44 typedef unsigned long int uintptr_t;
45 
46 #else /* defined(__FreeBSD__) && defined(__i386__) */
47 
48 #include <inttypes.h>
49 #include <stdint.h>
50 
51 #endif /* defined(__FreeBSD__) && defined(__i386__) */
52 
53 #include "InstrProfData.inc"
54 
55 enum ValueKind {
56 #define VALUE_PROF_KIND(Enumerator, Value) Enumerator = Value,
57 #include "InstrProfData.inc"
58 };
59 
60 typedef void *IntPtrT;
61 typedef struct LLVM_ALIGNAS(INSTR_PROF_DATA_ALIGNMENT) __llvm_profile_data {
62 #define INSTR_PROF_DATA(Type, LLVMType, Name, Initializer) Type Name;
63 #include "InstrProfData.inc"
64 } __llvm_profile_data;
65 
66 typedef struct __llvm_profile_header {
67 #define INSTR_PROF_RAW_HEADER(Type, Name, Initializer) Type Name;
68 #include "InstrProfData.inc"
69 } __llvm_profile_header;
70 
71 /*!
72  * \brief Get number of bytes necessary to pad the argument to eight
73  * byte boundary.
74  */
75 uint8_t __llvm_profile_get_num_padding_bytes(uint64_t SizeInBytes);
76 
77 /*!
78  * \brief Get required size for profile buffer.
79  */
80 uint64_t __llvm_profile_get_size_for_buffer(void);
81 
82 /*!
83  * \brief Write instrumentation data to the given buffer.
84  *
85  * \pre \c Buffer is the start of a buffer at least as big as \a
86  * __llvm_profile_get_size_for_buffer().
87  */
88 int __llvm_profile_write_buffer(char *Buffer);
89 
90 const __llvm_profile_data *__llvm_profile_begin_data(void);
91 const __llvm_profile_data *__llvm_profile_end_data(void);
92 const char *__llvm_profile_begin_names(void);
93 const char *__llvm_profile_end_names(void);
94 uint64_t *__llvm_profile_begin_counters(void);
95 uint64_t *__llvm_profile_end_counters(void);
96 
97 /*!
98  * \brief Clear profile counters to zero.
99  *
100  */
101 void __llvm_profile_reset_counters(void);
102 
103 /*!
104  * \brief Counts the number of times a target value is seen.
105  *
106  * Records the target value for the CounterIndex if not seen before. Otherwise,
107  * increments the counter associated w/ the target value.
108  * void __llvm_profile_instrument_target(uint64_t TargetValue, void *Data,
109  *                                       uint32_t CounterIndex);
110  */
111 void INSTR_PROF_VALUE_PROF_FUNC(
112 #define VALUE_PROF_FUNC_PARAM(ArgType, ArgName, ArgLLVMType) ArgType ArgName
113 #include "InstrProfData.inc"
114 );
115 
116 /*!
117  * \brief Prepares the value profiling data for output.
118  *
119  * Prepares a single __llvm_profile_value_data array out of the many
120  * ValueProfNode trees (one per instrumented function).
121  */
122 uint64_t __llvm_profile_gather_value_data(uint8_t **DataArray);
123 
124 /*!
125  * \brief Write instrumentation data to the current file.
126  *
127  * Writes to the file with the last name given to \a __llvm_profile_set_filename(),
128  * or if it hasn't been called, the \c LLVM_PROFILE_FILE environment variable,
129  * or if that's not set, the last name given to
130  * \a __llvm_profile_override_default_filename(), or if that's not set,
131  * \c "default.profraw".
132  */
133 int __llvm_profile_write_file(void);
134 
135 /*!
136  * \brief Set the filename for writing instrumentation data.
137  *
138  * Sets the filename to be used for subsequent calls to
139  * \a __llvm_profile_write_file().
140  *
141  * \c Name is not copied, so it must remain valid.  Passing NULL resets the
142  * filename logic to the default behaviour.
143  */
144 void __llvm_profile_set_filename(const char *Name);
145 
146 /*!
147  * \brief Set the filename for writing instrumentation data, unless the
148  * \c LLVM_PROFILE_FILE environment variable was set.
149  *
150  * Unless overridden, sets the filename to be used for subsequent calls to
151  * \a __llvm_profile_write_file().
152  *
153  * \c Name is not copied, so it must remain valid.  Passing NULL resets the
154  * filename logic to the default behaviour (unless the \c LLVM_PROFILE_FILE
155  * was set in which case it has no effect).
156  */
157 void __llvm_profile_override_default_filename(const char *Name);
158 
159 /*! \brief Register to write instrumentation data to file at exit. */
160 int __llvm_profile_register_write_file_atexit(void);
161 
162 /*! \brief Initialize file handling. */
163 void __llvm_profile_initialize_file(void);
164 
165 /*! \brief Get the magic token for the file format. */
166 uint64_t __llvm_profile_get_magic(void);
167 
168 /*! \brief Get the version of the file format. */
169 uint64_t __llvm_profile_get_version(void);
170 
171 #endif /* PROFILE_INSTRPROFILING_H_ */
172