Fix gnat.dg/opt39.adb on hppa.
[official-gcc.git] / gcc / statistics.cc
blob1708e0d3aacf3b73e8a0c3adccc4d099d9c7b2f0
1 /* Optimization statistics functions.
2 Copyright (C) 2008-2023 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 (void)
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 (idx >= nr_statistics_hashes)
104 statistics_hashes = XRESIZEVEC (stats_counter_table_type *,
105 statistics_hashes, idx+1);
106 memset (statistics_hashes + nr_statistics_hashes, 0,
107 (idx + 1 - nr_statistics_hashes)
108 * sizeof (stats_counter_table_type *));
109 nr_statistics_hashes = idx + 1;
112 statistics_hashes[idx] = new stats_counter_table_type (15);
114 return statistics_hashes[idx];
117 /* Helper function to return asmname or name of FN
118 depending on whether asmname option is set. */
120 static const char *
121 get_function_name (struct function *fn)
123 if ((statistics_dump_flags & TDF_ASMNAME)
124 && fn && DECL_ASSEMBLER_NAME_SET_P (fn->decl))
126 tree asmname = decl_assembler_name (fn->decl);
127 if (asmname)
128 return IDENTIFIER_POINTER (asmname);
130 return function_name (fn);
133 /* Helper for statistics_fini_pass. Print the counter difference
134 since the last dump for the pass dump files. */
137 statistics_fini_pass_1 (statistics_counter **slot,
138 void *data ATTRIBUTE_UNUSED)
140 statistics_counter *counter = *slot;
141 unsigned HOST_WIDE_INT count = counter->count - counter->prev_dumped_count;
142 if (count == 0)
143 return 1;
144 if (counter->histogram_p)
145 fprintf (dump_file, "%s == %d: " HOST_WIDE_INT_PRINT_DEC "\n",
146 counter->id, counter->val, count);
147 else
148 fprintf (dump_file, "%s: " HOST_WIDE_INT_PRINT_DEC "\n",
149 counter->id, count);
150 counter->prev_dumped_count = counter->count;
151 return 1;
154 /* Helper for statistics_fini_pass. Print the counter difference
155 since the last dump for the statistics dump. */
158 statistics_fini_pass_2 (statistics_counter **slot,
159 void *data ATTRIBUTE_UNUSED)
161 statistics_counter *counter = *slot;
162 unsigned HOST_WIDE_INT count = counter->count - counter->prev_dumped_count;
163 if (count == 0)
164 return 1;
165 counter->prev_dumped_count = counter->count;
166 if (counter->histogram_p)
167 fprintf (statistics_dump_file,
168 "%d %s \"%s == %d\" \"%s\" " HOST_WIDE_INT_PRINT_DEC "\n",
169 current_pass->static_pass_number,
170 current_pass->name,
171 counter->id, counter->val,
172 get_function_name (cfun),
173 count);
174 else
175 fprintf (statistics_dump_file,
176 "%d %s \"%s\" \"%s\" " HOST_WIDE_INT_PRINT_DEC "\n",
177 current_pass->static_pass_number,
178 current_pass->name,
179 counter->id,
180 get_function_name (cfun),
181 count);
182 counter->prev_dumped_count = counter->count;
183 return 1;
186 /* Helper for statistics_fini_pass, reset the counters. */
189 statistics_fini_pass_3 (statistics_counter **slot,
190 void *data ATTRIBUTE_UNUSED)
192 statistics_counter *counter = *slot;
193 counter->prev_dumped_count = counter->count;
194 return 1;
197 /* Dump the current statistics incrementally. */
199 void
200 statistics_fini_pass (void)
202 if (current_pass->static_pass_number == -1)
203 return;
205 if (dump_file
206 && dump_flags & TDF_STATS)
208 fprintf (dump_file, "\n");
209 fprintf (dump_file, "Pass statistics of \"%s\": ", current_pass->name);
210 fprintf (dump_file, "----------------\n");
211 curr_statistics_hash ()
212 ->traverse_noresize <void *, statistics_fini_pass_1> (NULL);
213 fprintf (dump_file, "\n");
215 if (statistics_dump_file
216 && !(statistics_dump_flags & TDF_STATS
217 || statistics_dump_flags & TDF_DETAILS))
218 curr_statistics_hash ()
219 ->traverse_noresize <void *, statistics_fini_pass_2> (NULL);
220 curr_statistics_hash ()
221 ->traverse_noresize <void *, statistics_fini_pass_3> (NULL);
224 /* Helper for printing summary information. */
227 statistics_fini_1 (statistics_counter **slot, opt_pass *pass)
229 statistics_counter *counter = *slot;
230 if (counter->count == 0)
231 return 1;
232 if (counter->histogram_p)
233 fprintf (statistics_dump_file,
234 "%d %s \"%s == %d\" " HOST_WIDE_INT_PRINT_DEC "\n",
235 pass->static_pass_number,
236 pass->name,
237 counter->id, counter->val,
238 counter->count);
239 else
240 fprintf (statistics_dump_file,
241 "%d %s \"%s\" " HOST_WIDE_INT_PRINT_DEC "\n",
242 pass->static_pass_number,
243 pass->name,
244 counter->id,
245 counter->count);
246 return 1;
249 /* Finish the statistics and dump summary information. */
251 void
252 statistics_fini (void)
254 gcc::pass_manager *passes = g->get_passes ();
255 if (!statistics_dump_file)
256 return;
258 if (statistics_dump_flags & TDF_STATS)
260 unsigned i;
261 for (i = 0; i < nr_statistics_hashes; ++i)
262 if (statistics_hashes[i]
263 && passes->get_pass_for_id (i) != NULL)
264 statistics_hashes[i]
265 ->traverse_noresize <opt_pass *, statistics_fini_1>
266 (passes->get_pass_for_id (i));
269 dump_end (statistics_dump_nr, statistics_dump_file);
272 /* Register the statistics dump file. */
274 void
275 statistics_early_init (void)
277 gcc::dump_manager *dumps = g->get_dumps ();
278 statistics_dump_nr = dumps->dump_register (".statistics", "statistics",
279 "statistics", DK_tree,
280 OPTGROUP_NONE,
281 false);
284 /* Init the statistics. */
286 void
287 statistics_init (void)
289 gcc::dump_manager *dumps = g->get_dumps ();
290 statistics_dump_file = dump_begin (statistics_dump_nr, NULL);
291 statistics_dump_flags = dumps->get_dump_file_info (statistics_dump_nr)->pflags;
294 /* Lookup or add a statistics counter in the hashtable HASH with ID, VAL
295 and HISTOGRAM_P. */
297 static statistics_counter *
298 lookup_or_add_counter (stats_counter_table_type *hash, const char *id, int val,
299 bool histogram_p)
301 statistics_counter **counter;
302 statistics_counter c;
303 c.id = id;
304 c.val = val;
305 counter = hash->find_slot (&c, INSERT);
306 if (!*counter)
308 *counter = XNEW (statistics_counter);
309 (*counter)->id = xstrdup (id);
310 (*counter)->val = val;
311 (*counter)->histogram_p = histogram_p;
312 (*counter)->prev_dumped_count = 0;
313 (*counter)->count = 0;
315 return *counter;
318 /* Add statistics information about event ID in function FN.
319 This will increment the counter associated with ID by INCR.
320 It will also dump the event to the global statistics file if requested. */
322 void
323 statistics_counter_event (struct function *fn, const char *id, int incr)
325 statistics_counter *counter;
327 if ((!(dump_flags & TDF_STATS)
328 && !statistics_dump_file)
329 || incr == 0)
330 return;
332 if (current_pass
333 && current_pass->static_pass_number != -1)
335 counter = lookup_or_add_counter (curr_statistics_hash (), id, 0, false);
336 gcc_assert (!counter->histogram_p);
337 counter->count += incr;
340 if (!statistics_dump_file
341 || !(statistics_dump_flags & TDF_DETAILS))
342 return;
344 fprintf (statistics_dump_file,
345 "%d %s \"%s\" \"%s\" %d\n",
346 current_pass ? current_pass->static_pass_number : -1,
347 current_pass ? current_pass->name : "none",
349 get_function_name (fn),
350 incr);
353 /* Add statistics information about event ID in function FN with the
354 histogram value VAL.
355 It will dump the event to the global statistics file if requested. */
357 void
358 statistics_histogram_event (struct function *fn, const char *id, int val)
360 statistics_counter *counter;
362 if (!(dump_flags & TDF_STATS)
363 && !statistics_dump_file)
364 return;
366 counter = lookup_or_add_counter (curr_statistics_hash (), id, val, true);
367 gcc_assert (counter->histogram_p);
368 counter->count += 1;
370 if (!statistics_dump_file
371 || !(statistics_dump_flags & TDF_DETAILS))
372 return;
374 fprintf (statistics_dump_file,
375 "%d %s \"%s == %d\" \"%s\" 1\n",
376 current_pass->static_pass_number,
377 current_pass->name,
378 id, val,
379 get_function_name (fn));