1/* Routines required for instrumenting a program. */ 2/* Compile this one with gcc. */ 3/* Copyright (C) 1989-2020 Free Software Foundation, Inc. 4 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 "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 32void __gcov_merge_add (gcov_type *counters __attribute__ ((unused)), 33 unsigned n_counters __attribute__ ((unused))) {} 34#endif 35 36#ifdef L_gcov_merge_topn 37void __gcov_merge_topn (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. */ 47void 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. */ 59void 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. */ 71void 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_topn 88 89/* To merging of TOPN profiles. 90 counters[0] is the number of executions 91 for i in 0 ... TOPN-1 92 counters[2 * i + 1] is target 93 counters[2 * i + 2] is corresponding hitrate counter. 94 95 Because we prune counters only those with probability >= 1/TOPN are 96 present now. 97 98 We use sign of counters[0] to track whether the number of different 99 targets exceeds TOPN. */ 100 101static void 102merge_topn_values_set (gcov_type *counters) 103{ 104 /* First value is number of total executions of the profiler. */ 105 gcov_type all = gcov_get_counter (); 106 gcov_type *total = &counters[0]; 107 ++counters; 108 109 /* Negative value means that counter is missing some of values. */ 110 if (all < 0) 111 *total = -(*total); 112 113 *total += all; 114 115 /* Read all part values. */ 116 gcov_type read_counters[2 * GCOV_TOPN_VALUES]; 117 for (unsigned i = 0; i < GCOV_TOPN_VALUES; i++) 118 { 119 read_counters[2 * i] = gcov_get_counter_target (); 120 read_counters[2 * i + 1] = gcov_get_counter_ignore_scaling (-1); 121 } 122 123 for (unsigned i = 0; i < GCOV_TOPN_VALUES; i++) 124 { 125 if (read_counters[2 * i + 1] == 0) 126 continue; 127 128 unsigned j; 129 int slot = 0; 130 131 for (j = 0; j < GCOV_TOPN_VALUES; j++) 132 { 133 if (counters[2 * j] == read_counters[2 * i]) 134 { 135 counters[2 * j + 1] += read_counters[2 * i + 1]; 136 break; 137 } 138 else if (counters[2 * j + 1] < counters[2 * slot + 1]) 139 slot = j; 140 } 141 142 if (j == GCOV_TOPN_VALUES) 143 { 144 gcov_type slot_count = counters[2 * slot + 1]; 145 /* We found an empty slot. */ 146 if (slot_count == 0) 147 { 148 /* If we found empty slot, add the value. */ 149 counters[2 * slot] = read_counters[2 * i]; 150 counters[2 * slot + 1] = read_counters[2 * i + 1]; 151 } 152 else 153 { 154 /* Here we are loosing some values. */ 155 if (*total >= 0) 156 *total = -(*total); 157 if (read_counters[2 * i + 1] > slot_count) 158 { 159 counters[2 * slot] = read_counters[2 * i]; 160 counters[2 * slot + 1] = read_counters[2 * i + 1]; 161 } 162 else 163 counters[2 * slot + 1] -= read_counters[2 * i + 1]; 164 } 165 } 166 } 167} 168 169/* The profile merging function for choosing the most common value. 170 It is given an array COUNTERS of N_COUNTERS old counters and it 171 reads the same number of counters from the gcov file. The counters 172 are split into pairs where the members of the tuple have 173 meanings: 174 175 -- the stored candidate on the most common value of the measured entity 176 -- counter 177 */ 178void 179__gcov_merge_topn (gcov_type *counters, unsigned n_counters) 180{ 181 gcc_assert (!(n_counters % GCOV_TOPN_VALUES_COUNTERS)); 182 183 for (unsigned i = 0; i < (n_counters / GCOV_TOPN_VALUES_COUNTERS); i++) 184 merge_topn_values_set (counters + (i * GCOV_TOPN_VALUES_COUNTERS)); 185} 186#endif /* L_gcov_merge_topn */ 187 188#endif /* inhibit_libc */ 189