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#include "InstrProfilingPort.h"
14
15#define INSTR_PROF_VISIBILITY COMPILER_RT_VISIBILITY
16#include "InstrProfData.inc"
17
18enum ValueKind {
19#define VALUE_PROF_KIND(Enumerator, Value) Enumerator = Value,
20#include "InstrProfData.inc"
21};
22
23typedef void *IntPtrT;
24typedef struct COMPILER_RT_ALIGNAS(INSTR_PROF_DATA_ALIGNMENT)
25    __llvm_profile_data {
26#define INSTR_PROF_DATA(Type, LLVMType, Name, Initializer) Type Name;
27#include "InstrProfData.inc"
28} __llvm_profile_data;
29
30typedef struct __llvm_profile_header {
31#define INSTR_PROF_RAW_HEADER(Type, Name, Initializer) Type Name;
32#include "InstrProfData.inc"
33} __llvm_profile_header;
34
35typedef struct ValueProfNode * PtrToNodeT;
36typedef struct ValueProfNode {
37#define INSTR_PROF_VALUE_NODE(Type, LLVMType, Name, Initializer) Type Name;
38#include "InstrProfData.inc"
39} ValueProfNode;
40
41/*!
42 * \brief Get number of bytes necessary to pad the argument to eight
43 * byte boundary.
44 */
45uint8_t __llvm_profile_get_num_padding_bytes(uint64_t SizeInBytes);
46
47/*!
48 * \brief Get required size for profile buffer.
49 */
50uint64_t __llvm_profile_get_size_for_buffer(void);
51
52/*!
53 * \brief Write instrumentation data to the given buffer.
54 *
55 * \pre \c Buffer is the start of a buffer at least as big as \a
56 * __llvm_profile_get_size_for_buffer().
57 */
58int __llvm_profile_write_buffer(char *Buffer);
59
60const __llvm_profile_data *__llvm_profile_begin_data(void);
61const __llvm_profile_data *__llvm_profile_end_data(void);
62const char *__llvm_profile_begin_names(void);
63const char *__llvm_profile_end_names(void);
64uint64_t *__llvm_profile_begin_counters(void);
65uint64_t *__llvm_profile_end_counters(void);
66ValueProfNode *__llvm_profile_begin_vnodes();
67ValueProfNode *__llvm_profile_end_vnodes();
68
69/*!
70 * \brief Clear profile counters to zero.
71 *
72 */
73void __llvm_profile_reset_counters(void);
74
75/*!
76 * \brief Merge profile data from buffer.
77 *
78 * Read profile data form buffer \p Profile  and merge with
79 * in-process profile counters. The client is expected to
80 * have checked or already knows the profile data in the
81 * buffer matches the in-process counter structure before
82 * calling it.
83 */
84void __llvm_profile_merge_from_buffer(const char *Profile, uint64_t Size);
85
86/*! \brief Check if profile in buffer matches the current binary.
87 *
88 *  Returns 0 (success) if the profile data in buffer \p Profile with size
89 *  \p Size was generated by the same binary and therefore matches
90 *  structurally the in-process counters. If the profile data in buffer is
91 *  not compatible, the interface returns 1 (failure).
92 */
93int __llvm_profile_check_compatibility(const char *Profile,
94                                       uint64_t Size);
95
96/*!
97 * \brief Counts the number of times a target value is seen.
98 *
99 * Records the target value for the CounterIndex if not seen before. Otherwise,
100 * increments the counter associated w/ the target value.
101 * void __llvm_profile_instrument_target(uint64_t TargetValue, void *Data,
102 *                                       uint32_t CounterIndex);
103 */
104void INSTR_PROF_VALUE_PROF_FUNC(
105#define VALUE_PROF_FUNC_PARAM(ArgType, ArgName, ArgLLVMType) ArgType ArgName
106#include "InstrProfData.inc"
107    );
108
109/*!
110 * \brief Write instrumentation data to the current file.
111 *
112 * Writes to the file with the last name given to \a *
113 * __llvm_profile_set_filename(),
114 * or if it hasn't been called, the \c LLVM_PROFILE_FILE environment variable,
115 * or if that's not set, the last name given to
116 * \a __llvm_profile_override_default_filename(), or if that's not set,
117 * \c "default.profraw".
118 */
119int __llvm_profile_write_file(void);
120
121/*!
122 * \brief Set the filename for writing instrumentation data.
123 *
124 * Sets the filename to be used for subsequent calls to
125 * \a __llvm_profile_write_file().
126 *
127 * \c Name is not copied, so it must remain valid.  Passing NULL resets the
128 * filename logic to the default behaviour.
129 */
130void __llvm_profile_set_filename(const char *Name);
131
132/*!
133 * \brief Set the filename for writing instrumentation data, unless the
134 * \c LLVM_PROFILE_FILE environment variable was set.
135 *
136 * Unless overridden, sets the filename to be used for subsequent calls to
137 * \a __llvm_profile_write_file().
138 *
139 * \c Name is not copied, so it must remain valid.  Passing NULL resets the
140 * filename logic to the default behaviour (unless the \c LLVM_PROFILE_FILE
141 * was set in which case it has no effect).
142 */
143void __llvm_profile_override_default_filename(const char *Name);
144
145/*! \brief Register to write instrumentation data to file at exit. */
146int __llvm_profile_register_write_file_atexit(void);
147
148/*! \brief Initialize file handling. */
149void __llvm_profile_initialize_file(void);
150
151/*! \brief Get the magic token for the file format. */
152uint64_t __llvm_profile_get_magic(void);
153
154/*! \brief Get the version of the file format. */
155uint64_t __llvm_profile_get_version(void);
156
157/*! \brief Get the number of entries in the profile data section. */
158uint64_t __llvm_profile_get_data_size(const __llvm_profile_data *Begin,
159                                      const __llvm_profile_data *End);
160
161/*!
162 * This variable is defined in InstrProfilingRuntime.cc as a hidden
163 * symbol. Its main purpose is to enable profile runtime user to
164 * bypass runtime initialization code -- if the client code explicitly
165 * define this variable, then InstProfileRuntime.o won't be linked in.
166 * Note that this variable's visibility needs to be hidden so that the
167 * definition of this variable in an instrumented shared library won't
168 * affect runtime initialization decision of the main program.
169 */
170COMPILER_RT_VISIBILITY extern int __llvm_profile_runtime;
171
172/*!
173 * This variable is defined in InstrProfiling.c. Its main purpose is to
174 * encode the raw profile version value and other format related information
175 * such as whether the profile is from IR based instrumentation. The variable
176 * is defined as weak so that compiler can emit an overriding definition
177 * depending on user option.  Since we don't support mixing FE and IR based
178 * data in the same raw profile data file (in other words, shared libs and
179 * main program are expected to be instrumented in the same way), there is
180 * no need for this variable to be hidden.
181 */
182extern uint64_t __llvm_profile_raw_version;
183
184#endif /* PROFILE_INSTRPROFILING_H_ */
185