1 /* Routines required for instrumenting a program. */ 2 /* Compile this one with gcc. */ 3 /* Copyright (C) 1989-2018 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 unsigned n_counters __attribute__ ((unused))) {} 39 #endif 40 41 #else 42 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) 49 { 50 for (; n_counters; counters++, n_counters--) 51 *counters += gcov_get_counter (); 52 } 53 #endif /* L_gcov_merge_add */ 54 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) 61 { 62 for (; n_counters; counters++, n_counters--) 63 *counters |= gcov_get_counter_target (); 64 } 65 #endif 66 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) 73 { 74 unsigned int i; 75 gcov_type value; 76 77 for (i = 0; i < n_counters; i++) 78 { 79 value = gcov_get_counter_target (); 80 81 if (value && (!counters[i] || value < counters[i])) 82 counters[i] = value; 83 } 84 } 85 #endif /* L_gcov_merge_time_profile */ 86 87 #ifdef L_gcov_merge_single 88 /* The profile merging function for choosing the most common value. 89 It is given an array COUNTERS of N_COUNTERS old counters and it 90 reads the same number of counters from the gcov file. The counters 91 are split into 3-tuples where the members of the tuple have 92 meanings: 93 94 -- the stored candidate on the most common value of the measured entity 95 -- counter 96 -- total number of evaluations of the value */ 97 void 98 __gcov_merge_single (gcov_type *counters, unsigned n_counters) 99 { 100 unsigned i, n_measures; 101 gcov_type value, counter, all; 102 103 gcc_assert (!(n_counters % 3)); 104 n_measures = n_counters / 3; 105 for (i = 0; i < n_measures; i++, counters += 3) 106 { 107 value = gcov_get_counter_target (); 108 counter = gcov_get_counter (); 109 all = gcov_get_counter (); 110 111 if (counters[0] == value) 112 counters[1] += counter; 113 else if (counter > counters[1]) 114 { 115 counters[0] = value; 116 counters[1] = counter - counters[1]; 117 } 118 else 119 counters[1] -= counter; 120 counters[2] += all; 121 } 122 } 123 #endif /* L_gcov_merge_single */ 124 125 #ifdef L_gcov_merge_icall_topn 126 /* The profile merging function used for merging indirect call counts 127 This function is given array COUNTERS of N_COUNTERS old counters and it 128 reads the same number of counters from the gcov file. */ 129 130 void 131 __gcov_merge_icall_topn (gcov_type *counters, unsigned n_counters) 132 { 133 unsigned i, j, k, m; 134 135 gcc_assert (!(n_counters % GCOV_ICALL_TOPN_NCOUNTS)); 136 for (i = 0; i < n_counters; i += GCOV_ICALL_TOPN_NCOUNTS) 137 { 138 gcov_type *value_array = &counters[i + 1]; 139 unsigned tmp_size = 2 * (GCOV_ICALL_TOPN_NCOUNTS - 1); 140 gcov_type *tmp_array 141 = (gcov_type *) alloca (tmp_size * sizeof (gcov_type)); 142 143 for (j = 0; j < tmp_size; j++) 144 tmp_array[j] = 0; 145 146 for (j = 0; j < GCOV_ICALL_TOPN_NCOUNTS - 1; j += 2) 147 { 148 tmp_array[j] = value_array[j]; 149 tmp_array[j + 1] = value_array [j + 1]; 150 } 151 152 /* Skip the number_of_eviction entry. */ 153 gcov_get_counter (); 154 for (k = 0; k < GCOV_ICALL_TOPN_NCOUNTS - 1; k += 2) 155 { 156 int found = 0; 157 gcov_type global_id = gcov_get_counter_target (); 158 gcov_type call_count = gcov_get_counter (); 159 for (m = 0; m < j; m += 2) 160 { 161 if (tmp_array[m] == global_id) 162 { 163 found = 1; 164 tmp_array[m + 1] += call_count; 165 break; 166 } 167 } 168 if (!found) 169 { 170 tmp_array[j] = global_id; 171 tmp_array[j + 1] = call_count; 172 j += 2; 173 } 174 } 175 /* Now sort the temp array */ 176 gcov_sort_n_vals (tmp_array, j); 177 178 /* Now copy back the top half of the temp array */ 179 for (k = 0; k < GCOV_ICALL_TOPN_NCOUNTS - 1; k += 2) 180 { 181 value_array[k] = tmp_array[k]; 182 value_array[k + 1] = tmp_array[k + 1]; 183 } 184 } 185 } 186 #endif /* L_gcov_merge_icall_topn */ 187 #endif /* inhibit_libc */ 188