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
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
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/>. */
23 #include "coretypes.h"
25 #include "tree-pass.h"
27 #include "pass_manager.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
37 struct statistics_counter
{
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. */
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. */
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. */
75 stats_counter_hasher::remove (statistics_counter
*v
)
77 free (CONST_CAST (char *, v
->id
));
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
90 static stats_counter_table_type
*
91 curr_statistics_hash (void)
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. */
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
);
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
;
144 if (counter
->histogram_p
)
145 fprintf (dump_file
, "%s == %d: " HOST_WIDE_INT_PRINT_DEC
"\n",
146 counter
->id
, counter
->val
, count
);
148 fprintf (dump_file
, "%s: " HOST_WIDE_INT_PRINT_DEC
"\n",
150 counter
->prev_dumped_count
= counter
->count
;
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
;
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
,
171 counter
->id
, counter
->val
,
172 get_function_name (cfun
),
175 fprintf (statistics_dump_file
,
176 "%d %s \"%s\" \"%s\" " HOST_WIDE_INT_PRINT_DEC
"\n",
177 current_pass
->static_pass_number
,
180 get_function_name (cfun
),
182 counter
->prev_dumped_count
= counter
->count
;
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
;
197 /* Dump the current statistics incrementally. */
200 statistics_fini_pass (void)
202 if (current_pass
->static_pass_number
== -1)
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)
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
,
237 counter
->id
, counter
->val
,
240 fprintf (statistics_dump_file
,
241 "%d %s \"%s\" " HOST_WIDE_INT_PRINT_DEC
"\n",
242 pass
->static_pass_number
,
249 /* Finish the statistics and dump summary information. */
252 statistics_fini (void)
254 gcc::pass_manager
*passes
= g
->get_passes ();
255 if (!statistics_dump_file
)
258 if (statistics_dump_flags
& TDF_STATS
)
261 for (i
= 0; i
< nr_statistics_hashes
; ++i
)
262 if (statistics_hashes
[i
]
263 && passes
->get_pass_for_id (i
) != NULL
)
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. */
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
,
284 /* Init the statistics. */
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
297 static statistics_counter
*
298 lookup_or_add_counter (stats_counter_table_type
*hash
, const char *id
, int val
,
301 statistics_counter
**counter
;
302 statistics_counter c
;
305 counter
= hash
->find_slot (&c
, INSERT
);
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;
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. */
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
)
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
))
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
),
353 /* Add statistics information about event ID in function FN with the
355 It will dump the event to the global statistics file if requested. */
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
)
366 counter
= lookup_or_add_counter (curr_statistics_hash (), id
, val
, true);
367 gcc_assert (counter
->histogram_p
);
370 if (!statistics_dump_file
371 || !(statistics_dump_flags
& TDF_DETAILS
))
374 fprintf (statistics_dump_file
,
375 "%d %s \"%s == %d\" \"%s\" 1\n",
376 current_pass
->static_pass_number
,
379 get_function_name (fn
));