1 /* Routines required for instrumenting a program. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1989-2019 Free Software Foundation, Inc.
5 This file is part of GCC.
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
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
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.
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/>. */
27 #if !defined(inhibit_libc)
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
33 #if __SIZEOF_LONG_LONG__ == 8 && __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8
34 #define GCOV_SUPPORTS_ATOMIC 1
36 #define GCOV_SUPPORTS_ATOMIC 0
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
47 __gcov_interval_profiler (gcov_type
*counters
, gcov_type value
,
48 int start
, unsigned steps
)
50 gcov_type delta
= value
- start
;
52 counters
[steps
+ 1]++;
53 else if (delta
>= steps
)
60 #if defined(L_gcov_interval_profiler_atomic) && GCOV_SUPPORTS_ATOMIC
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. */
67 __gcov_interval_profiler_atomic (gcov_type
*counters
, gcov_type value
,
68 int start
, unsigned steps
)
70 gcov_type delta
= value
- start
;
72 __atomic_fetch_add (&counters
[steps
+ 1], 1, __ATOMIC_RELAXED
);
73 else if (delta
>= steps
)
74 __atomic_fetch_add (&counters
[steps
], 1, __ATOMIC_RELAXED
);
76 __atomic_fetch_add (&counters
[delta
], 1, __ATOMIC_RELAXED
);
80 #ifdef L_gcov_pow2_profiler
81 /* If VALUE is a power of two, COUNTERS[1] is incremented. Otherwise
82 COUNTERS[0] is incremented. */
85 __gcov_pow2_profiler (gcov_type
*counters
, gcov_type value
)
87 if (value
== 0 || (value
& (value
- 1)))
94 #if defined(L_gcov_pow2_profiler_atomic) && GCOV_SUPPORTS_ATOMIC
95 /* If VALUE is a power of two, COUNTERS[1] is incremented. Otherwise
96 COUNTERS[0] is incremented. Function is thread-safe. */
99 __gcov_pow2_profiler_atomic (gcov_type
*counters
, gcov_type value
)
101 if (value
== 0 || (value
& (value
- 1)))
102 __atomic_fetch_add (&counters
[0], 1, __ATOMIC_RELAXED
);
104 __atomic_fetch_add (&counters
[1], 1, __ATOMIC_RELAXED
);
109 /* Tries to determine the most common value among its inputs. Checks if the
110 value stored in COUNTERS[0] matches VALUE. If this is the case, COUNTERS[1]
111 is incremented. If this is not the case and COUNTERS[1] is not zero,
112 COUNTERS[1] is decremented. Otherwise COUNTERS[1] is set to one and
113 VALUE is stored to COUNTERS[0]. This algorithm guarantees that if this
114 function is called more than 50% of the time with one value, this value
115 will be in COUNTERS[0] in the end. */
118 __gcov_one_value_profiler_body (gcov_type
*counters
, gcov_type value
,
121 if (value
== counters
[1])
123 else if (counters
[2] == 0)
132 __atomic_fetch_add (&counters
[0], 1, __ATOMIC_RELAXED
);
137 #ifdef L_gcov_one_value_profiler_v2
139 __gcov_one_value_profiler_v2 (gcov_type
*counters
, gcov_type value
)
141 __gcov_one_value_profiler_body (counters
, value
, 0);
145 #if defined(L_gcov_one_value_profiler_v2_atomic) && GCOV_SUPPORTS_ATOMIC
147 /* Update one value profilers (COUNTERS) for a given VALUE.
149 CAVEAT: Following function is not thread-safe, only total number
150 of executions (COUNTERS[2]) is update with an atomic instruction.
151 Problem is that one cannot atomically update two counters
152 (COUNTERS[0] and COUNTERS[1]), for more information please read
153 following email thread:
154 https://gcc.gnu.org/ml/gcc-patches/2016-08/msg00024.html. */
157 __gcov_one_value_profiler_v2_atomic (gcov_type
*counters
, gcov_type value
)
159 __gcov_one_value_profiler_body (counters
, value
, 1);
163 #ifdef L_gcov_indirect_call_profiler_v4
165 /* These two variables are used to actually track caller and callee. Keep
166 them in TLS memory so races are not common (they are written to often).
167 The variables are set directly by GCC instrumented code, so declaration
168 here must match one in tree-profile.c */
170 #if defined(HAVE_CC_TLS) && !defined (USE_EMUTLS)
173 struct indirect_call_tuple __gcov_indirect_call
;
175 /* By default, the C++ compiler will use function addresses in the
176 vtable entries. Setting TARGET_VTABLE_USES_DESCRIPTORS to nonzero
177 tells the compiler to use function descriptors instead. The value
178 of this macro says how many words wide the descriptor is (normally 2).
180 It is assumed that the address of a function descriptor may be treated
181 as a pointer to a function. */
183 /* Tries to determine the most common value among its inputs. */
185 __gcov_indirect_call_profiler_v4 (gcov_type value
, void* cur_func
)
187 /* If the C++ virtual tables contain function descriptors then one
188 function may have multiple descriptors and we need to dereference
189 the descriptors to see if they point to the same function. */
190 if (cur_func
== __gcov_indirect_call
.callee
191 || (__LIBGCC_VTABLE_USES_DESCRIPTORS__
192 && *(void **) cur_func
== *(void **) __gcov_indirect_call
.callee
))
193 __gcov_one_value_profiler_body (__gcov_indirect_call
.counters
, value
, 0);
195 __gcov_indirect_call
.callee
= NULL
;
199 #ifdef L_gcov_time_profiler
201 /* Counter for first visit of each function. */
202 gcov_type __gcov_time_profiler_counter ATTRIBUTE_HIDDEN
;
206 #ifdef L_gcov_average_profiler
207 /* Increase corresponding COUNTER by VALUE. FIXME: Perhaps we want
211 __gcov_average_profiler (gcov_type
*counters
, gcov_type value
)
213 counters
[0] += value
;
218 #if defined(L_gcov_average_profiler_atomic) && GCOV_SUPPORTS_ATOMIC
219 /* Increase corresponding COUNTER by VALUE. FIXME: Perhaps we want
220 to saturate up. Function is thread-safe. */
223 __gcov_average_profiler_atomic (gcov_type
*counters
, gcov_type value
)
225 __atomic_fetch_add (&counters
[0], value
, __ATOMIC_RELAXED
);
226 __atomic_fetch_add (&counters
[1], 1, __ATOMIC_RELAXED
);
230 #ifdef L_gcov_ior_profiler
231 /* Bitwise-OR VALUE into COUNTER. */
234 __gcov_ior_profiler (gcov_type
*counters
, gcov_type value
)
240 #if defined(L_gcov_ior_profiler_atomic) && GCOV_SUPPORTS_ATOMIC
241 /* Bitwise-OR VALUE into COUNTER. Function is thread-safe. */
244 __gcov_ior_profiler_atomic (gcov_type
*counters
, gcov_type value
)
246 __atomic_fetch_or (&counters
[0], value
, __ATOMIC_RELAXED
);
251 #endif /* inhibit_libc */