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