c++: Implement C++26 P2573R2 - = delete("should have a reason"); [PR114458]
[official-gcc.git] / gcc / statistics.cc
blobf26889d4196af0e3a383c71e4f61fd084d0c9429
1 /* Optimization statistics functions.
2 Copyright (C) 2008-2024 Free Software Foundation, Inc.
3 Contributed by Richard Guenther <rguenther@suse.de>
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 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "function.h"
25 #include "tree-pass.h"
26 #include "context.h"
27 #include "pass_manager.h"
28 #include "tree.h"
30 static int statistics_dump_nr;
31 static dump_flags_t statistics_dump_flags;
32 static FILE *statistics_dump_file;
34 /* Statistics entry. A integer counter associated to a string ID
35 and value. */
37 struct statistics_counter {
38 const char *id;
39 int val;
40 bool histogram_p;
41 unsigned HOST_WIDE_INT count;
42 unsigned HOST_WIDE_INT prev_dumped_count;
45 /* Hashtable helpers. */
47 struct stats_counter_hasher : pointer_hash <statistics_counter>
49 static inline hashval_t hash (const statistics_counter *);
50 static inline bool equal (const statistics_counter *,
51 const statistics_counter *);
52 static inline void remove (statistics_counter *);
55 /* Hash a statistic counter by its string ID. */
57 inline hashval_t
58 stats_counter_hasher::hash (const statistics_counter *c)
60 return htab_hash_string (c->id) + c->val;
63 /* Compare two statistic counters by their string IDs. */
65 inline bool
66 stats_counter_hasher::equal (const statistics_counter *c1,
67 const statistics_counter *c2)
69 return c1->val == c2->val && strcmp (c1->id, c2->id) == 0;
72 /* Free a statistics entry. */
74 inline void
75 stats_counter_hasher::remove (statistics_counter *v)
77 free (CONST_CAST (char *, v->id));
78 free (v);
81 typedef hash_table<stats_counter_hasher> stats_counter_table_type;
83 /* Array of statistic hashes, indexed by pass id. */
84 static stats_counter_table_type **statistics_hashes;
85 static unsigned nr_statistics_hashes;
87 /* Return the current hashtable to be used for recording or printing
88 statistics. */
90 static stats_counter_table_type *
91 curr_statistics_hash (bool alloc = true)
93 unsigned idx;
95 gcc_assert (current_pass->static_pass_number >= 0);
96 idx = current_pass->static_pass_number;
98 if (idx < nr_statistics_hashes
99 && statistics_hashes[idx])
100 return statistics_hashes[idx];
102 if (!alloc)
103 return nullptr;
105 if (idx >= nr_statistics_hashes)
107 statistics_hashes = XRESIZEVEC (stats_counter_table_type *,
108 statistics_hashes, idx+1);
109 memset (statistics_hashes + nr_statistics_hashes, 0,
110 (idx + 1 - nr_statistics_hashes)
111 * sizeof (stats_counter_table_type *));
112 nr_statistics_hashes = idx + 1;
115 statistics_hashes[idx] = new stats_counter_table_type (15);
117 return statistics_hashes[idx];
120 /* Helper function to return asmname or name of FN
121 depending on whether asmname option is set. */
123 static const char *
124 get_function_name (struct function *fn)
126 if ((statistics_dump_flags & TDF_ASMNAME)
127 && fn && DECL_ASSEMBLER_NAME_SET_P (fn->decl))
129 tree asmname = decl_assembler_name (fn->decl);
130 if (asmname)
131 return IDENTIFIER_POINTER (asmname);
133 return function_name (fn);
136 /* Helper for statistics_fini_pass. Print the counter difference
137 since the last dump for the pass dump files. */
140 statistics_fini_pass_1 (statistics_counter **slot,
141 void *data ATTRIBUTE_UNUSED)
143 statistics_counter *counter = *slot;
144 unsigned HOST_WIDE_INT count = counter->count - counter->prev_dumped_count;
145 if (count == 0)
146 return 1;
147 if (counter->histogram_p)
148 fprintf (dump_file, "%s == %d: " HOST_WIDE_INT_PRINT_DEC "\n",
149 counter->id, counter->val, count);
150 else
151 fprintf (dump_file, "%s: " HOST_WIDE_INT_PRINT_DEC "\n",
152 counter->id, count);
153 counter->prev_dumped_count = counter->count;
154 return 1;
157 /* Helper for statistics_fini_pass. Print the counter difference
158 since the last dump for the statistics dump. */
161 statistics_fini_pass_2 (statistics_counter **slot,
162 void *data ATTRIBUTE_UNUSED)
164 statistics_counter *counter = *slot;
165 unsigned HOST_WIDE_INT count = counter->count - counter->prev_dumped_count;
166 if (count == 0)
167 return 1;
168 counter->prev_dumped_count = counter->count;
169 if (counter->histogram_p)
170 fprintf (statistics_dump_file,
171 "%d %s \"%s == %d\" \"%s\" " HOST_WIDE_INT_PRINT_DEC "\n",
172 current_pass->static_pass_number,
173 current_pass->name,
174 counter->id, counter->val,
175 get_function_name (cfun),
176 count);
177 else
178 fprintf (statistics_dump_file,
179 "%d %s \"%s\" \"%s\" " HOST_WIDE_INT_PRINT_DEC "\n",
180 current_pass->static_pass_number,
181 current_pass->name,
182 counter->id,
183 get_function_name (cfun),
184 count);
185 counter->prev_dumped_count = counter->count;
186 return 1;
189 /* Helper for statistics_fini_pass, reset the counters. */
192 statistics_fini_pass_3 (statistics_counter **slot,
193 void *data ATTRIBUTE_UNUSED)
195 statistics_counter *counter = *slot;
196 counter->prev_dumped_count = counter->count;
197 return 1;
200 /* Dump the current statistics incrementally. */
202 void
203 statistics_fini_pass (void)
205 if (current_pass->static_pass_number == -1)
206 return;
208 stats_counter_table_type *stat_hash = curr_statistics_hash (false);
210 if (dump_file
211 && dump_flags & TDF_STATS)
213 fprintf (dump_file, "\n");
214 fprintf (dump_file, "Pass statistics of \"%s\": ", current_pass->name);
215 fprintf (dump_file, "----------------\n");
216 if (stat_hash)
217 stat_hash->traverse_noresize <void *, statistics_fini_pass_1> (NULL);
218 fprintf (dump_file, "\n");
221 if (!stat_hash)
222 return;
224 if (statistics_dump_file
225 && !(statistics_dump_flags & TDF_STATS
226 || statistics_dump_flags & TDF_DETAILS))
227 stat_hash->traverse_noresize <void *, statistics_fini_pass_2> (NULL);
228 stat_hash->traverse_noresize <void *, statistics_fini_pass_3> (NULL);
231 /* Helper for printing summary information. */
234 statistics_fini_1 (statistics_counter **slot, opt_pass *pass)
236 statistics_counter *counter = *slot;
237 if (counter->count == 0)
238 return 1;
239 if (counter->histogram_p)
240 fprintf (statistics_dump_file,
241 "%d %s \"%s == %d\" " HOST_WIDE_INT_PRINT_DEC "\n",
242 pass->static_pass_number,
243 pass->name,
244 counter->id, counter->val,
245 counter->count);
246 else
247 fprintf (statistics_dump_file,
248 "%d %s \"%s\" " HOST_WIDE_INT_PRINT_DEC "\n",
249 pass->static_pass_number,
250 pass->name,
251 counter->id,
252 counter->count);
253 return 1;
256 /* Finish the statistics and dump summary information. */
258 void
259 statistics_fini (void)
261 gcc::pass_manager *passes = g->get_passes ();
262 if (!statistics_dump_file)
263 return;
265 if (statistics_dump_flags & TDF_STATS)
267 unsigned i;
268 for (i = 0; i < nr_statistics_hashes; ++i)
269 if (statistics_hashes[i]
270 && passes->get_pass_for_id (i) != NULL)
271 statistics_hashes[i]
272 ->traverse_noresize <opt_pass *, statistics_fini_1>
273 (passes->get_pass_for_id (i));
276 dump_end (statistics_dump_nr, statistics_dump_file);
279 /* Register the statistics dump file. */
281 void
282 statistics_early_init (void)
284 gcc::dump_manager *dumps = g->get_dumps ();
285 statistics_dump_nr = dumps->dump_register (".statistics", "statistics",
286 "statistics", DK_tree,
287 OPTGROUP_NONE,
288 false);
291 /* Init the statistics. */
293 void
294 statistics_init (void)
296 gcc::dump_manager *dumps = g->get_dumps ();
297 statistics_dump_file = dump_begin (statistics_dump_nr, NULL);
298 statistics_dump_flags = dumps->get_dump_file_info (statistics_dump_nr)->pflags;
301 /* Lookup or add a statistics counter in the hashtable HASH with ID, VAL
302 and HISTOGRAM_P. */
304 static statistics_counter *
305 lookup_or_add_counter (stats_counter_table_type *hash, const char *id, int val,
306 bool histogram_p)
308 statistics_counter **counter;
309 statistics_counter c;
310 c.id = id;
311 c.val = val;
312 counter = hash->find_slot (&c, INSERT);
313 if (!*counter)
315 *counter = XNEW (statistics_counter);
316 (*counter)->id = xstrdup (id);
317 (*counter)->val = val;
318 (*counter)->histogram_p = histogram_p;
319 (*counter)->prev_dumped_count = 0;
320 (*counter)->count = 0;
322 return *counter;
325 /* Add statistics information about event ID in function FN.
326 This will increment the counter associated with ID by INCR.
327 It will also dump the event to the global statistics file if requested. */
329 void
330 statistics_counter_event (struct function *fn, const char *id, int incr)
332 statistics_counter *counter;
334 if ((!(dump_flags & TDF_STATS)
335 && !statistics_dump_file)
336 || incr == 0)
337 return;
339 if (current_pass
340 && current_pass->static_pass_number != -1)
342 counter = lookup_or_add_counter (curr_statistics_hash (), id, 0, false);
343 gcc_assert (!counter->histogram_p);
344 counter->count += incr;
347 if (!statistics_dump_file
348 || !(statistics_dump_flags & TDF_DETAILS))
349 return;
351 fprintf (statistics_dump_file,
352 "%d %s \"%s\" \"%s\" %d\n",
353 current_pass ? current_pass->static_pass_number : -1,
354 current_pass ? current_pass->name : "none",
356 get_function_name (fn),
357 incr);
360 /* Add statistics information about event ID in function FN with the
361 histogram value VAL.
362 It will dump the event to the global statistics file if requested. */
364 void
365 statistics_histogram_event (struct function *fn, const char *id, int val)
367 statistics_counter *counter;
369 if (!(dump_flags & TDF_STATS)
370 && !statistics_dump_file)
371 return;
373 counter = lookup_or_add_counter (curr_statistics_hash (), id, val, true);
374 gcc_assert (counter->histogram_p);
375 counter->count += 1;
377 if (!statistics_dump_file
378 || !(statistics_dump_flags & TDF_DETAILS))
379 return;
381 fprintf (statistics_dump_file,
382 "%d %s \"%s == %d\" \"%s\" 1\n",
383 current_pass->static_pass_number,
384 current_pass->name,
385 id, val,
386 get_function_name (fn));