]>
Commit | Line | Data |
---|---|---|
d6d3f033 RX |
1 | /* Routines required for instrumenting a program. */ |
2 | /* Compile this one with gcc. */ | |
8d9254fc | 3 | /* Copyright (C) 1989-2020 Free Software Foundation, Inc. |
d6d3f033 RX |
4 | |
5 | This file is part of GCC. | |
6 | ||
7 | GCC is free software; you can redistribute it and/or modify it under | |
8 | the terms of the GNU General Public License as published by the Free | |
9 | Software Foundation; either version 3, or (at your option) any later | |
10 | version. | |
11 | ||
12 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 | for more details. | |
16 | ||
17 | Under Section 7 of GPL version 3, you are granted additional | |
18 | permissions described in the GCC Runtime Library Exception, version | |
19 | 3.1, as published by the Free Software Foundation. | |
20 | ||
21 | You should have received a copy of the GNU General Public License and | |
22 | a copy of the GCC Runtime Library Exception along with this program; | |
23 | see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
24 | <http://www.gnu.org/licenses/>. */ | |
25 | ||
40d6b753 | 26 | #include "libgcov.h" |
d6d3f033 | 27 | #if !defined(inhibit_libc) |
d6d3f033 | 28 | |
7fe76f6a ML |
29 | /* Detect whether target can support atomic update of profilers. */ |
30 | #if __SIZEOF_LONG_LONG__ == 4 && __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4 | |
31 | #define GCOV_SUPPORTS_ATOMIC 1 | |
32 | #else | |
33 | #if __SIZEOF_LONG_LONG__ == 8 && __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8 | |
34 | #define GCOV_SUPPORTS_ATOMIC 1 | |
35 | #else | |
36 | #define GCOV_SUPPORTS_ATOMIC 0 | |
37 | #endif | |
38 | #endif | |
39 | ||
d6d3f033 RX |
40 | #ifdef L_gcov_interval_profiler |
41 | /* If VALUE is in interval <START, START + STEPS - 1>, then increases the | |
42 | corresponding counter in COUNTERS. If the VALUE is above or below | |
43 | the interval, COUNTERS[STEPS] or COUNTERS[STEPS + 1] is increased | |
44 | instead. */ | |
45 | ||
46 | void | |
47 | __gcov_interval_profiler (gcov_type *counters, gcov_type value, | |
48 | int start, unsigned steps) | |
49 | { | |
50 | gcov_type delta = value - start; | |
51 | if (delta < 0) | |
52 | counters[steps + 1]++; | |
53 | else if (delta >= steps) | |
54 | counters[steps]++; | |
55 | else | |
56 | counters[delta]++; | |
57 | } | |
58 | #endif | |
59 | ||
7fe76f6a | 60 | #if defined(L_gcov_interval_profiler_atomic) && GCOV_SUPPORTS_ATOMIC |
a266236e ML |
61 | /* If VALUE is in interval <START, START + STEPS - 1>, then increases the |
62 | corresponding counter in COUNTERS. If the VALUE is above or below | |
63 | the interval, COUNTERS[STEPS] or COUNTERS[STEPS + 1] is increased | |
64 | instead. Function is thread-safe. */ | |
65 | ||
66 | void | |
67 | __gcov_interval_profiler_atomic (gcov_type *counters, gcov_type value, | |
68 | int start, unsigned steps) | |
69 | { | |
70 | gcov_type delta = value - start; | |
71 | if (delta < 0) | |
4d0cdd0c | 72 | __atomic_fetch_add (&counters[steps + 1], 1, __ATOMIC_RELAXED); |
a266236e | 73 | else if (delta >= steps) |
4d0cdd0c | 74 | __atomic_fetch_add (&counters[steps], 1, __ATOMIC_RELAXED); |
a266236e | 75 | else |
4d0cdd0c | 76 | __atomic_fetch_add (&counters[delta], 1, __ATOMIC_RELAXED); |
a266236e ML |
77 | } |
78 | #endif | |
79 | ||
d6d3f033 RX |
80 | #ifdef L_gcov_pow2_profiler |
81 | /* If VALUE is a power of two, COUNTERS[1] is incremented. Otherwise | |
82 | COUNTERS[0] is incremented. */ | |
83 | ||
84 | void | |
85 | __gcov_pow2_profiler (gcov_type *counters, gcov_type value) | |
86 | { | |
dcb1e137 | 87 | if (value == 0 || (value & (value - 1))) |
d6d3f033 RX |
88 | counters[0]++; |
89 | else | |
90 | counters[1]++; | |
91 | } | |
92 | #endif | |
93 | ||
7fe76f6a | 94 | #if defined(L_gcov_pow2_profiler_atomic) && GCOV_SUPPORTS_ATOMIC |
a266236e ML |
95 | /* If VALUE is a power of two, COUNTERS[1] is incremented. Otherwise |
96 | COUNTERS[0] is incremented. Function is thread-safe. */ | |
97 | ||
98 | void | |
99 | __gcov_pow2_profiler_atomic (gcov_type *counters, gcov_type value) | |
100 | { | |
101 | if (value == 0 || (value & (value - 1))) | |
4d0cdd0c | 102 | __atomic_fetch_add (&counters[0], 1, __ATOMIC_RELAXED); |
a266236e | 103 | else |
4d0cdd0c | 104 | __atomic_fetch_add (&counters[1], 1, __ATOMIC_RELAXED); |
a266236e ML |
105 | } |
106 | #endif | |
107 | ||
108 | ||
596341c7 | 109 | /* Tries to determine N most commons value among its inputs. */ |
d6d3f033 RX |
110 | |
111 | static inline void | |
596341c7 ML |
112 | __gcov_topn_values_profiler_body (gcov_type *counters, gcov_type value, |
113 | int use_atomic) | |
d6d3f033 | 114 | { |
1b309ca5 ML |
115 | if (use_atomic) |
116 | __atomic_fetch_add (&counters[0], 1, __ATOMIC_RELAXED); | |
117 | else | |
118 | counters[0]++; | |
119 | ||
120 | ++counters; | |
121 | ||
596341c7 | 122 | /* We have GCOV_TOPN_VALUES as we can keep multiple values |
1b309ca5 ML |
123 | next to each other. */ |
124 | unsigned sindex = 0; | |
125 | ||
596341c7 | 126 | for (unsigned i = 0; i < GCOV_TOPN_VALUES; i++) |
d6d3f033 | 127 | { |
1b309ca5 ML |
128 | if (value == counters[2 * i]) |
129 | { | |
130 | if (use_atomic) | |
131 | __atomic_fetch_add (&counters[2 * i + 1], 1, __ATOMIC_RELAXED); | |
132 | else | |
133 | counters[2 * i + 1]++; | |
134 | return; | |
135 | } | |
136 | else if (counters[2 * i + 1] == 0) | |
137 | { | |
138 | /* We found an empty slot. */ | |
139 | counters[2 * i] = value; | |
140 | counters[2 * i + 1] = 1; | |
141 | return; | |
142 | } | |
143 | ||
144 | if (counters[2 * i + 1] < counters[2 * sindex + 1]) | |
145 | sindex = i; | |
d6d3f033 | 146 | } |
a266236e | 147 | |
1b309ca5 | 148 | /* We haven't found an empty slot, then decrement the smallest. */ |
a266236e | 149 | if (use_atomic) |
1b309ca5 | 150 | __atomic_fetch_sub (&counters[2 * sindex + 1], 1, __ATOMIC_RELAXED); |
a266236e | 151 | else |
1b309ca5 | 152 | counters[2 * sindex + 1]--; |
d6d3f033 RX |
153 | } |
154 | ||
596341c7 | 155 | #ifdef L_gcov_topn_values_profiler |
d6d3f033 | 156 | void |
596341c7 | 157 | __gcov_topn_values_profiler (gcov_type *counters, gcov_type value) |
d6d3f033 | 158 | { |
596341c7 | 159 | __gcov_topn_values_profiler_body (counters, value, 0); |
a266236e ML |
160 | } |
161 | #endif | |
162 | ||
596341c7 | 163 | #if defined(L_gcov_topn_values_profiler_atomic) && GCOV_SUPPORTS_ATOMIC |
a266236e ML |
164 | |
165 | /* Update one value profilers (COUNTERS) for a given VALUE. | |
166 | ||
167 | CAVEAT: Following function is not thread-safe, only total number | |
168 | of executions (COUNTERS[2]) is update with an atomic instruction. | |
169 | Problem is that one cannot atomically update two counters | |
170 | (COUNTERS[0] and COUNTERS[1]), for more information please read | |
171 | following email thread: | |
172 | https://gcc.gnu.org/ml/gcc-patches/2016-08/msg00024.html. */ | |
173 | ||
174 | void | |
596341c7 | 175 | __gcov_topn_values_profiler_atomic (gcov_type *counters, gcov_type value) |
a266236e | 176 | { |
596341c7 | 177 | __gcov_topn_values_profiler_body (counters, value, 1); |
d6d3f033 RX |
178 | } |
179 | #endif | |
180 | ||
92d41717 | 181 | #ifdef L_gcov_indirect_call_profiler_v4 |
d6d3f033 RX |
182 | |
183 | /* These two variables are used to actually track caller and callee. Keep | |
184 | them in TLS memory so races are not common (they are written to often). | |
185 | The variables are set directly by GCC instrumented code, so declaration | |
186 | here must match one in tree-profile.c */ | |
187 | ||
188 | #if defined(HAVE_CC_TLS) && !defined (USE_EMUTLS) | |
189 | __thread | |
190 | #endif | |
3edbcdbe | 191 | struct indirect_call_tuple __gcov_indirect_call; |
d6d3f033 RX |
192 | |
193 | /* By default, the C++ compiler will use function addresses in the | |
194 | vtable entries. Setting TARGET_VTABLE_USES_DESCRIPTORS to nonzero | |
195 | tells the compiler to use function descriptors instead. The value | |
53d68b9f | 196 | of this macro says how many words wide the descriptor is (normally 2). |
d6d3f033 RX |
197 | |
198 | It is assumed that the address of a function descriptor may be treated | |
199 | as a pointer to a function. */ | |
200 | ||
d6d3f033 RX |
201 | /* Tries to determine the most common value among its inputs. */ |
202 | void | |
92d41717 | 203 | __gcov_indirect_call_profiler_v4 (gcov_type value, void* cur_func) |
d6d3f033 RX |
204 | { |
205 | /* If the C++ virtual tables contain function descriptors then one | |
206 | function may have multiple descriptors and we need to dereference | |
207 | the descriptors to see if they point to the same function. */ | |
3edbcdbe | 208 | if (cur_func == __gcov_indirect_call.callee |
fd2e1dcd | 209 | || (__LIBGCC_VTABLE_USES_DESCRIPTORS__ |
3edbcdbe | 210 | && *(void **) cur_func == *(void **) __gcov_indirect_call.callee)) |
596341c7 | 211 | __gcov_topn_values_profiler_body (__gcov_indirect_call.counters, value, 0); |
4f751c54 | 212 | |
3edbcdbe | 213 | __gcov_indirect_call.callee = NULL; |
d6d3f033 RX |
214 | } |
215 | #endif | |
216 | ||
217 | #ifdef L_gcov_time_profiler | |
218 | ||
219 | /* Counter for first visit of each function. */ | |
3239d726 | 220 | gcov_type __gcov_time_profiler_counter ATTRIBUTE_HIDDEN; |
d6d3f033 | 221 | |
d6d3f033 | 222 | #endif |
a266236e | 223 | |
d6d3f033 RX |
224 | #ifdef L_gcov_average_profiler |
225 | /* Increase corresponding COUNTER by VALUE. FIXME: Perhaps we want | |
226 | to saturate up. */ | |
227 | ||
228 | void | |
229 | __gcov_average_profiler (gcov_type *counters, gcov_type value) | |
230 | { | |
231 | counters[0] += value; | |
232 | counters[1] ++; | |
233 | } | |
234 | #endif | |
235 | ||
7fe76f6a | 236 | #if defined(L_gcov_average_profiler_atomic) && GCOV_SUPPORTS_ATOMIC |
a266236e ML |
237 | /* Increase corresponding COUNTER by VALUE. FIXME: Perhaps we want |
238 | to saturate up. Function is thread-safe. */ | |
239 | ||
240 | void | |
241 | __gcov_average_profiler_atomic (gcov_type *counters, gcov_type value) | |
242 | { | |
4d0cdd0c TP |
243 | __atomic_fetch_add (&counters[0], value, __ATOMIC_RELAXED); |
244 | __atomic_fetch_add (&counters[1], 1, __ATOMIC_RELAXED); | |
a266236e ML |
245 | } |
246 | #endif | |
247 | ||
d6d3f033 RX |
248 | #ifdef L_gcov_ior_profiler |
249 | /* Bitwise-OR VALUE into COUNTER. */ | |
250 | ||
251 | void | |
252 | __gcov_ior_profiler (gcov_type *counters, gcov_type value) | |
253 | { | |
254 | *counters |= value; | |
255 | } | |
256 | #endif | |
257 | ||
7fe76f6a | 258 | #if defined(L_gcov_ior_profiler_atomic) && GCOV_SUPPORTS_ATOMIC |
a266236e ML |
259 | /* Bitwise-OR VALUE into COUNTER. Function is thread-safe. */ |
260 | ||
261 | void | |
262 | __gcov_ior_profiler_atomic (gcov_type *counters, gcov_type value) | |
263 | { | |
4d0cdd0c | 264 | __atomic_fetch_or (&counters[0], value, __ATOMIC_RELAXED); |
a266236e ML |
265 | } |
266 | #endif | |
267 | ||
268 | ||
d6d3f033 | 269 | #endif /* inhibit_libc */ |