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 (bool alloc
= true)
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
];
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. */
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
);
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
;
147 if (counter
->histogram_p
)
148 fprintf (dump_file
, "%s == %d: " HOST_WIDE_INT_PRINT_DEC
"\n",
149 counter
->id
, counter
->val
, count
);
151 fprintf (dump_file
, "%s: " HOST_WIDE_INT_PRINT_DEC
"\n",
153 counter
->prev_dumped_count
= counter
->count
;
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
;
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
,
174 counter
->id
, counter
->val
,
175 get_function_name (cfun
),
178 fprintf (statistics_dump_file
,
179 "%d %s \"%s\" \"%s\" " HOST_WIDE_INT_PRINT_DEC
"\n",
180 current_pass
->static_pass_number
,
183 get_function_name (cfun
),
185 counter
->prev_dumped_count
= counter
->count
;
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
;
200 /* Dump the current statistics incrementally. */
203 statistics_fini_pass (void)
205 if (current_pass
->static_pass_number
== -1)
208 stats_counter_table_type
*stat_hash
= curr_statistics_hash (false);
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");
217 stat_hash
->traverse_noresize
<void *, statistics_fini_pass_1
> (NULL
);
218 fprintf (dump_file
, "\n");
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)
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
,
244 counter
->id
, counter
->val
,
247 fprintf (statistics_dump_file
,
248 "%d %s \"%s\" " HOST_WIDE_INT_PRINT_DEC
"\n",
249 pass
->static_pass_number
,
256 /* Finish the statistics and dump summary information. */
259 statistics_fini (void)
261 gcc::pass_manager
*passes
= g
->get_passes ();
262 if (!statistics_dump_file
)
265 if (statistics_dump_flags
& TDF_STATS
)
268 for (i
= 0; i
< nr_statistics_hashes
; ++i
)
269 if (statistics_hashes
[i
]
270 && passes
->get_pass_for_id (i
) != NULL
)
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. */
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
,
291 /* Init the statistics. */
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
304 static statistics_counter
*
305 lookup_or_add_counter (stats_counter_table_type
*hash
, const char *id
, int val
,
308 statistics_counter
**counter
;
309 statistics_counter c
;
312 counter
= hash
->find_slot (&c
, INSERT
);
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;
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. */
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
)
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
))
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
),
360 /* Add statistics information about event ID in function FN with the
362 It will dump the event to the global statistics file if requested. */
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
)
373 counter
= lookup_or_add_counter (curr_statistics_hash (), id
, val
, true);
374 gcc_assert (counter
->histogram_p
);
377 if (!statistics_dump_file
378 || !(statistics_dump_flags
& TDF_DETAILS
))
381 fprintf (statistics_dump_file
,
382 "%d %s \"%s == %d\" \"%s\" 1\n",
383 current_pass
->static_pass_number
,
386 get_function_name (fn
));