2013-12-09 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc.git] / libgcc / libgcov-merge.c
blob45cd48cc4c4e290e9e8ebc8450bc167135248744
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 #else
35 #define IN_LIBGCOV 1
36 #endif
38 #include "gcov-io.h"
40 #if defined(inhibit_libc)
41 /* If libc and its header files are not available, provide dummy functions. */
43 #ifdef L_gcov_merge_add
44 void __gcov_merge_add (gcov_type *counters __attribute__ ((unused)),
45 unsigned n_counters __attribute__ ((unused))) {}
46 #endif
48 #ifdef L_gcov_merge_single
49 void __gcov_merge_single (gcov_type *counters __attribute__ ((unused)),
50 unsigned n_counters __attribute__ ((unused))) {}
51 #endif
53 #ifdef L_gcov_merge_delta
54 void __gcov_merge_delta (gcov_type *counters __attribute__ ((unused)),
55 unsigned n_counters __attribute__ ((unused))) {}
56 #endif
58 #else
60 #ifdef L_gcov_merge_add
61 /* The profile merging function that just adds the counters. It is given
62 an array COUNTERS of N_COUNTERS old counters and it reads the same number
63 of counters from the gcov file. */
64 void
65 __gcov_merge_add (gcov_type *counters, unsigned n_counters)
67 for (; n_counters; counters++, n_counters--)
68 *counters += gcov_read_counter ();
70 #endif /* L_gcov_merge_add */
72 #ifdef L_gcov_merge_ior
73 /* The profile merging function that just adds the counters. It is given
74 an array COUNTERS of N_COUNTERS old counters and it reads the same number
75 of counters from the gcov file. */
76 void
77 __gcov_merge_ior (gcov_type *counters, unsigned n_counters)
79 for (; n_counters; counters++, n_counters--)
80 *counters |= gcov_read_counter ();
82 #endif
84 #ifdef L_gcov_merge_time_profile
85 /* Time profiles are merged so that minimum from all valid (greater than zero)
86 is stored. There could be a fork that creates new counters. To have
87 the profile stable, we chosen to pick the smallest function visit time. */
88 void
89 __gcov_merge_time_profile (gcov_type *counters, unsigned n_counters)
91 unsigned int i;
92 gcov_type value;
94 for (i = 0; i < n_counters; i++)
96 value = gcov_read_counter ();
98 if (value && (!counters[i] || value < counters[i]))
99 counters[i] = value;
102 #endif /* L_gcov_merge_time_profile */
104 #ifdef L_gcov_merge_single
105 /* The profile merging function for choosing the most common value.
106 It is given an array COUNTERS of N_COUNTERS old counters and it
107 reads the same number of counters from the gcov file. The counters
108 are split into 3-tuples where the members of the tuple have
109 meanings:
111 -- the stored candidate on the most common value of the measured entity
112 -- counter
113 -- total number of evaluations of the value */
114 void
115 __gcov_merge_single (gcov_type *counters, unsigned n_counters)
117 unsigned i, n_measures;
118 gcov_type value, counter, all;
120 gcc_assert (!(n_counters % 3));
121 n_measures = n_counters / 3;
122 for (i = 0; i < n_measures; i++, counters += 3)
124 value = gcov_read_counter ();
125 counter = gcov_read_counter ();
126 all = gcov_read_counter ();
128 if (counters[0] == value)
129 counters[1] += counter;
130 else if (counter > counters[1])
132 counters[0] = value;
133 counters[1] = counter - counters[1];
135 else
136 counters[1] -= counter;
137 counters[2] += all;
140 #endif /* L_gcov_merge_single */
142 #ifdef L_gcov_merge_delta
143 /* The profile merging function for choosing the most common
144 difference between two consecutive evaluations of the value. It is
145 given an array COUNTERS of N_COUNTERS old counters and it reads the
146 same number of counters from the gcov file. The counters are split
147 into 4-tuples where the members of the tuple have meanings:
149 -- the last value of the measured entity
150 -- the stored candidate on the most common difference
151 -- counter
152 -- total number of evaluations of the value */
153 void
154 __gcov_merge_delta (gcov_type *counters, unsigned n_counters)
156 unsigned i, n_measures;
157 gcov_type value, counter, all;
159 gcc_assert (!(n_counters % 4));
160 n_measures = n_counters / 4;
161 for (i = 0; i < n_measures; i++, counters += 4)
163 /* last = */ gcov_read_counter ();
164 value = gcov_read_counter ();
165 counter = gcov_read_counter ();
166 all = gcov_read_counter ();
168 if (counters[1] == value)
169 counters[2] += counter;
170 else if (counter > counters[2])
172 counters[1] = value;
173 counters[2] = counter - counters[2];
175 else
176 counters[2] -= counter;
177 counters[3] += all;
180 #endif /* L_gcov_merge_delta */
181 #endif /* inhibit_libc */