blob: e8a340dc2a3aa5a4445b1755a3b062ecea33c453 [file] [log] [blame]
Rong Xud6d3f032013-11-13 00:24:49 +00001/* Routines required for instrumenting a program. */
2/* Compile this one with gcc. */
Richard Sandifordac1dca32014-01-02 22:25:22 +00003/* Copyright (C) 1989-2014 Free Software Foundation, Inc.
Rong Xud6d3f032013-11-13 00:24:49 +00004
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 3, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17Under Section 7 of GPL version 3, you are granted additional
18permissions described in the GCC Runtime Library Exception, version
193.1, as published by the Free Software Foundation.
20
21You should have received a copy of the GNU General Public License and
22a copy of the GCC Runtime Library Exception along with this program;
23see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24<http://www.gnu.org/licenses/>. */
25
26#include "tconfig.h"
27#include "tsystem.h"
28#include "coretypes.h"
29#include "tm.h"
30#include "libgcc_tm.h"
31
32#if !defined(inhibit_libc)
33#define IN_LIBGCOV 1
34#include "gcov-io.h"
35
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. */
41
42void
43__gcov_interval_profiler (gcov_type *counters, gcov_type value,
44 int start, unsigned steps)
45{
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]++;
53}
54#endif
55
56#ifdef L_gcov_pow2_profiler
57/* If VALUE is a power of two, COUNTERS[1] is incremented. Otherwise
58 COUNTERS[0] is incremented. */
59
60void
61__gcov_pow2_profiler (gcov_type *counters, gcov_type value)
62{
63 if (value & (value - 1))
64 counters[0]++;
65 else
66 counters[1]++;
67}
68#endif
69
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.
77
78 In any case, COUNTERS[2] is incremented. */
79
80static inline void
81__gcov_one_value_profiler_body (gcov_type *counters, gcov_type value)
82{
83 if (value == counters[0])
84 counters[1]++;
85 else if (counters[1] == 0)
86 {
87 counters[1] = 1;
88 counters[0] = value;
89 }
90 else
91 counters[1]--;
92 counters[2]++;
93}
94
95#ifdef L_gcov_one_value_profiler
96void
97__gcov_one_value_profiler (gcov_type *counters, gcov_type value)
98{
99 __gcov_one_value_profiler_body (counters, value);
100}
101#endif
102
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. */
106
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.
114
115 It is assumed that the address of a function descriptor may be treated
116 as a pointer to a function. */
117
118#ifdef TARGET_VTABLE_USES_DESCRIPTORS
119#define VTABLE_USES_DESCRIPTORS 1
120#else
121#define VTABLE_USES_DESCRIPTORS 0
122#endif
123
124/* Tries to determine the most common value among its inputs. */
125void
126__gcov_indirect_call_profiler (gcov_type* counter, gcov_type value,
127 void* cur_func, void* callee_func)
128{
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);
136}
137
138#endif
139#ifdef L_gcov_indirect_call_profiler_v2
140
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 */
145
146#if defined(HAVE_CC_TLS) && !defined (USE_EMUTLS)
147__thread
148#endif
149void * __gcov_indirect_call_callee;
150#if defined(HAVE_CC_TLS) && !defined (USE_EMUTLS)
151__thread
152#endif
153gcov_type * __gcov_indirect_call_counters;
154
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.
162
163 It is assumed that the address of a function descriptor may be treated
164 as a pointer to a function. */
165
166#ifdef TARGET_VTABLE_USES_DESCRIPTORS
167#define VTABLE_USES_DESCRIPTORS 1
168#else
169#define VTABLE_USES_DESCRIPTORS 0
170#endif
171
172/* Tries to determine the most common value among its inputs. */
173void
174__gcov_indirect_call_profiler_v2 (gcov_type value, void* cur_func)
175{
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);
183}
184#endif
185
186#ifdef L_gcov_time_profiler
187
188/* Counter for first visit of each function. */
189static gcov_type function_counter;
190
191/* Sets corresponding COUNTERS if there is no value. */
192
193void
194__gcov_time_profiler (gcov_type* counters)
195{
196 if (!counters[0])
197 counters[0] = ++function_counter;
198}
199#endif
200
201#ifdef L_gcov_average_profiler
202/* Increase corresponding COUNTER by VALUE. FIXME: Perhaps we want
203 to saturate up. */
204
205void
206__gcov_average_profiler (gcov_type *counters, gcov_type value)
207{
208 counters[0] += value;
209 counters[1] ++;
210}
211#endif
212
213#ifdef L_gcov_ior_profiler
214/* Bitwise-OR VALUE into COUNTER. */
215
216void
217__gcov_ior_profiler (gcov_type *counters, gcov_type value)
218{
219 *counters |= value;
220}
221#endif
222
223#endif /* inhibit_libc */