Fix PR 93568 (thinko)
[official-gcc.git] / libgcc / libgcov-merge.c
blob19b8ee72ae99f9434fce806922558f17c0114df6
1 /* Routines required for instrumenting a program. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1989-2020 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 "libgcov.h"
28 #if defined(inhibit_libc)
29 /* If libc and its header files are not available, provide dummy functions. */
31 #ifdef L_gcov_merge_add
32 void __gcov_merge_add (gcov_type *counters __attribute__ ((unused)),
33 unsigned n_counters __attribute__ ((unused))) {}
34 #endif
36 #ifdef L_gcov_merge_topn
37 void __gcov_merge_topn (gcov_type *counters __attribute__ ((unused)),
38 unsigned n_counters __attribute__ ((unused))) {}
39 #endif
41 #else
43 #ifdef L_gcov_merge_add
44 /* The profile merging function that just adds the counters. It is given
45 an array COUNTERS of N_COUNTERS old counters and it reads the same number
46 of counters from the gcov file. */
47 void
48 __gcov_merge_add (gcov_type *counters, unsigned n_counters)
50 for (; n_counters; counters++, n_counters--)
51 *counters += gcov_get_counter ();
53 #endif /* L_gcov_merge_add */
55 #ifdef L_gcov_merge_ior
56 /* The profile merging function that just adds the counters. It is given
57 an array COUNTERS of N_COUNTERS old counters and it reads the same number
58 of counters from the gcov file. */
59 void
60 __gcov_merge_ior (gcov_type *counters, unsigned n_counters)
62 for (; n_counters; counters++, n_counters--)
63 *counters |= gcov_get_counter_target ();
65 #endif
67 #ifdef L_gcov_merge_time_profile
68 /* Time profiles are merged so that minimum from all valid (greater than zero)
69 is stored. There could be a fork that creates new counters. To have
70 the profile stable, we chosen to pick the smallest function visit time. */
71 void
72 __gcov_merge_time_profile (gcov_type *counters, unsigned n_counters)
74 unsigned int i;
75 gcov_type value;
77 for (i = 0; i < n_counters; i++)
79 value = gcov_get_counter_target ();
81 if (value && (!counters[i] || value < counters[i]))
82 counters[i] = value;
85 #endif /* L_gcov_merge_time_profile */
87 #ifdef L_gcov_merge_topn
89 static void
90 merge_topn_values_set (gcov_type *counters)
92 /* First value is number of total executions of the profiler. */
93 gcov_type all = gcov_get_counter_ignore_scaling (-1);
94 counters[0] += all;
95 ++counters;
97 /* Read all part values. */
98 gcov_type read_counters[2 * GCOV_TOPN_VALUES];
100 for (unsigned i = 0; i < GCOV_TOPN_VALUES; i++)
102 read_counters[2 * i] = gcov_get_counter_target ();
103 read_counters[2 * i + 1] = gcov_get_counter_ignore_scaling (-1);
106 if (read_counters[1] == -1)
108 counters[1] = -1;
109 return;
112 for (unsigned i = 0; i < GCOV_TOPN_VALUES; i++)
114 if (read_counters[2 * i + 1] == 0)
115 continue;
117 unsigned j;
118 int slot = -1;
120 for (j = 0; j < GCOV_TOPN_VALUES; j++)
122 if (counters[2 * j] == read_counters[2 * i])
124 counters[2 * j + 1] += read_counters[2 * i + 1];
125 break;
127 else if (counters[2 * j + 1] == 0)
128 slot = j;
131 if (j == GCOV_TOPN_VALUES)
133 if (slot > 0)
135 /* If we found empty slot, add the value. */
136 counters[2 * slot] = read_counters[2 * i];
137 counters[2 * slot + 1] = read_counters[2 * i + 1];
139 else
141 /* We haven't found a slot, bail out. */
142 counters[1] = -1;
143 return;
149 /* The profile merging function for choosing the most common value.
150 It is given an array COUNTERS of N_COUNTERS old counters and it
151 reads the same number of counters from the gcov file. The counters
152 are split into pairs where the members of the tuple have
153 meanings:
155 -- the stored candidate on the most common value of the measured entity
156 -- counter
158 void
159 __gcov_merge_topn (gcov_type *counters, unsigned n_counters)
161 gcc_assert (!(n_counters % GCOV_TOPN_VALUES_COUNTERS));
163 for (unsigned i = 0; i < (n_counters / GCOV_TOPN_VALUES_COUNTERS); i++)
164 merge_topn_values_set (counters + (i * GCOV_TOPN_VALUES_COUNTERS));
166 #endif /* L_gcov_merge_topn */
168 #endif /* inhibit_libc */