Implement N disk counters for single value and indirect call counters.
[gcc.git] / libgcc / libgcov-merge.c
1 /* Routines required for instrumenting a program. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1989-2019 Free Software Foundation, Inc.
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
26 #include "libgcov.h"
27
28 #if defined(inhibit_libc)
29 /* If libc and its header files are not available, provide dummy functions. */
30
31 #ifdef L_gcov_merge_add
32 void __gcov_merge_add (gcov_type *counters __attribute__ ((unused)),
33 unsigned n_counters __attribute__ ((unused))) {}
34 #endif
35
36 #ifdef L_gcov_merge_single
37 void __gcov_merge_single (gcov_type *counters __attribute__ ((unused)))
38 {
39 }
40 #endif
41
42 #else
43
44 #ifdef L_gcov_merge_add
45 /* The profile merging function that just adds the counters. It is given
46 an array COUNTERS of N_COUNTERS old counters and it reads the same number
47 of counters from the gcov file. */
48 void
49 __gcov_merge_add (gcov_type *counters, unsigned n_counters)
50 {
51 for (; n_counters; counters++, n_counters--)
52 *counters += gcov_get_counter ();
53 }
54 #endif /* L_gcov_merge_add */
55
56 #ifdef L_gcov_merge_ior
57 /* The profile merging function that just adds the counters. It is given
58 an array COUNTERS of N_COUNTERS old counters and it reads the same number
59 of counters from the gcov file. */
60 void
61 __gcov_merge_ior (gcov_type *counters, unsigned n_counters)
62 {
63 for (; n_counters; counters++, n_counters--)
64 *counters |= gcov_get_counter_target ();
65 }
66 #endif
67
68 #ifdef L_gcov_merge_time_profile
69 /* Time profiles are merged so that minimum from all valid (greater than zero)
70 is stored. There could be a fork that creates new counters. To have
71 the profile stable, we chosen to pick the smallest function visit time. */
72 void
73 __gcov_merge_time_profile (gcov_type *counters, unsigned n_counters)
74 {
75 unsigned int i;
76 gcov_type value;
77
78 for (i = 0; i < n_counters; i++)
79 {
80 value = gcov_get_counter_target ();
81
82 if (value && (!counters[i] || value < counters[i]))
83 counters[i] = value;
84 }
85 }
86 #endif /* L_gcov_merge_time_profile */
87
88 #ifdef L_gcov_merge_single
89
90 static void
91 merge_single_value_set (gcov_type *counters)
92 {
93 unsigned j;
94 gcov_type value, counter;
95
96 /* First value is number of total executions of the profiler. */
97 gcov_type all = gcov_get_counter_ignore_scaling (-1);
98 counters[0] += all;
99 ++counters;
100
101 for (unsigned i = 0; i < GCOV_DISK_SINGLE_VALUES; i++)
102 {
103 value = gcov_get_counter_target ();
104 counter = gcov_get_counter_ignore_scaling (-1);
105
106 if (counter == -1)
107 {
108 counters[1] = -1;
109 /* We can't return as we need to read all counters. */
110 continue;
111 }
112 else if (counter == 0 || counters[1] == -1)
113 {
114 /* We can't return as we need to read all counters. */
115 continue;
116 }
117
118 for (j = 0; j < GCOV_DISK_SINGLE_VALUES; j++)
119 {
120 if (counters[2 * j] == value)
121 {
122 counters[2 * j + 1] += counter;
123 break;
124 }
125 else if (counters[2 * j + 1] == 0)
126 {
127 counters[2 * j] = value;
128 counters[2 * j + 1] = counter;
129 break;
130 }
131 }
132
133 /* We haven't found a free slot for the value, mark overflow. */
134 if (j == GCOV_DISK_SINGLE_VALUES)
135 counters[1] = -1;
136 }
137 }
138
139 /* The profile merging function for choosing the most common value.
140 It is given an array COUNTERS of N_COUNTERS old counters and it
141 reads the same number of counters from the gcov file. The counters
142 are split into pairs where the members of the tuple have
143 meanings:
144
145 -- the stored candidate on the most common value of the measured entity
146 -- counter
147 */
148 void
149 __gcov_merge_single (gcov_type *counters, unsigned n_counters)
150 {
151 gcc_assert (!(n_counters % GCOV_SINGLE_VALUE_COUNTERS));
152
153 for (unsigned i = 0; i < (n_counters / GCOV_SINGLE_VALUE_COUNTERS); i++)
154 merge_single_value_set (counters + (i * GCOV_SINGLE_VALUE_COUNTERS));
155 }
156 #endif /* L_gcov_merge_single */
157
158 #endif /* inhibit_libc */