Reverting merge from trunk
[official-gcc.git] / libgcc / libgcov-profiler.c
blob6ba1bcf365cb3f7f0b8db110f25f9c46dd015728
1 /* Routines required for instrumenting a program. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1989-2013 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
10 version.
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.
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/>. */
26 #include "tconfig.h"
27 #include "tsystem.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "libgcc_tm.h"
32 #if !defined(inhibit_libc)
33 #define IN_LIBGCOV 1
34 #include "gcov-io.h"
36 #ifdef L_gcov_interval_profiler
37 /* If VALUE is in interval <START, START + STEPS - 1>, then increases the
38 corresponding counter in COUNTERS. If the VALUE is above or below
39 the interval, COUNTERS[STEPS] or COUNTERS[STEPS + 1] is increased
40 instead. */
42 void
43 __gcov_interval_profiler (gcov_type *counters, gcov_type value,
44 int start, unsigned steps)
46 gcov_type delta = value - start;
47 if (delta < 0)
48 counters[steps + 1]++;
49 else if (delta >= steps)
50 counters[steps]++;
51 else
52 counters[delta]++;
54 #endif
56 #ifdef L_gcov_pow2_profiler
57 /* If VALUE is a power of two, COUNTERS[1] is incremented. Otherwise
58 COUNTERS[0] is incremented. */
60 void
61 __gcov_pow2_profiler (gcov_type *counters, gcov_type value)
63 if (value & (value - 1))
64 counters[0]++;
65 else
66 counters[1]++;
68 #endif
70 /* Tries to determine the most common value among its inputs. Checks if the
71 value stored in COUNTERS[0] matches VALUE. If this is the case, COUNTERS[1]
72 is incremented. If this is not the case and COUNTERS[1] is not zero,
73 COUNTERS[1] is decremented. Otherwise COUNTERS[1] is set to one and
74 VALUE is stored to COUNTERS[0]. This algorithm guarantees that if this
75 function is called more than 50% of the time with one value, this value
76 will be in COUNTERS[0] in the end.
78 In any case, COUNTERS[2] is incremented. */
80 static inline void
81 __gcov_one_value_profiler_body (gcov_type *counters, gcov_type value)
83 if (value == counters[0])
84 counters[1]++;
85 else if (counters[1] == 0)
87 counters[1] = 1;
88 counters[0] = value;
90 else
91 counters[1]--;
92 counters[2]++;
95 #ifdef L_gcov_one_value_profiler
96 void
97 __gcov_one_value_profiler (gcov_type *counters, gcov_type value)
99 __gcov_one_value_profiler_body (counters, value);
101 #endif
103 #ifdef L_gcov_indirect_call_profiler
104 /* This function exist only for workaround of binutils bug 14342.
105 Once this compatibility hack is obsolette, it can be removed. */
107 /* By default, the C++ compiler will use function addresses in the
108 vtable entries. Setting TARGET_VTABLE_USES_DESCRIPTORS to nonzero
109 tells the compiler to use function descriptors instead. The value
110 of this macro says how many words wide the descriptor is (normally 2),
111 but it may be dependent on target flags. Since we do not have access
112 to the target flags here we just check to see if it is set and use
113 that to set VTABLE_USES_DESCRIPTORS to 0 or 1.
115 It is assumed that the address of a function descriptor may be treated
116 as a pointer to a function. */
118 #ifdef TARGET_VTABLE_USES_DESCRIPTORS
119 #define VTABLE_USES_DESCRIPTORS 1
120 #else
121 #define VTABLE_USES_DESCRIPTORS 0
122 #endif
124 /* Tries to determine the most common value among its inputs. */
125 void
126 __gcov_indirect_call_profiler (gcov_type* counter, gcov_type value,
127 void* cur_func, void* callee_func)
129 /* If the C++ virtual tables contain function descriptors then one
130 function may have multiple descriptors and we need to dereference
131 the descriptors to see if they point to the same function. */
132 if (cur_func == callee_func
133 || (VTABLE_USES_DESCRIPTORS && callee_func
134 && *(void **) cur_func == *(void **) callee_func))
135 __gcov_one_value_profiler_body (counter, value);
138 #endif
139 #ifdef L_gcov_indirect_call_profiler_v2
141 /* These two variables are used to actually track caller and callee. Keep
142 them in TLS memory so races are not common (they are written to often).
143 The variables are set directly by GCC instrumented code, so declaration
144 here must match one in tree-profile.c */
146 #if defined(HAVE_CC_TLS) && !defined (USE_EMUTLS)
147 __thread
148 #endif
149 void * __gcov_indirect_call_callee;
150 #if defined(HAVE_CC_TLS) && !defined (USE_EMUTLS)
151 __thread
152 #endif
153 gcov_type * __gcov_indirect_call_counters;
155 /* By default, the C++ compiler will use function addresses in the
156 vtable entries. Setting TARGET_VTABLE_USES_DESCRIPTORS to nonzero
157 tells the compiler to use function descriptors instead. The value
158 of this macro says how many words wide the descriptor is (normally 2),
159 but it may be dependent on target flags. Since we do not have access
160 to the target flags here we just check to see if it is set and use
161 that to set VTABLE_USES_DESCRIPTORS to 0 or 1.
163 It is assumed that the address of a function descriptor may be treated
164 as a pointer to a function. */
166 #ifdef TARGET_VTABLE_USES_DESCRIPTORS
167 #define VTABLE_USES_DESCRIPTORS 1
168 #else
169 #define VTABLE_USES_DESCRIPTORS 0
170 #endif
172 /* Tries to determine the most common value among its inputs. */
173 void
174 __gcov_indirect_call_profiler_v2 (gcov_type value, void* cur_func)
176 /* If the C++ virtual tables contain function descriptors then one
177 function may have multiple descriptors and we need to dereference
178 the descriptors to see if they point to the same function. */
179 if (cur_func == __gcov_indirect_call_callee
180 || (VTABLE_USES_DESCRIPTORS && __gcov_indirect_call_callee
181 && *(void **) cur_func == *(void **) __gcov_indirect_call_callee))
182 __gcov_one_value_profiler_body (__gcov_indirect_call_counters, value);
184 #endif
186 #ifdef L_gcov_time_profiler
188 /* Counter for first visit of each function. */
189 static gcov_type function_counter;
191 /* Sets corresponding COUNTERS if there is no value. */
193 void
194 __gcov_time_profiler (gcov_type* counters)
196 if (!counters[0])
197 counters[0] = ++function_counter;
199 #endif
201 #ifdef L_gcov_average_profiler
202 /* Increase corresponding COUNTER by VALUE. FIXME: Perhaps we want
203 to saturate up. */
205 void
206 __gcov_average_profiler (gcov_type *counters, gcov_type value)
208 counters[0] += value;
209 counters[1] ++;
211 #endif
213 #ifdef L_gcov_ior_profiler
214 /* Bitwise-OR VALUE into COUNTER. */
216 void
217 __gcov_ior_profiler (gcov_type *counters, gcov_type value)
219 *counters |= value;
221 #endif
223 #endif /* inhibit_libc */