1 /* Optimization statistics functions.
2 Copyright (C) 2008-2013 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"
24 #include "tree-pass.h"
25 #include "tree-dump.h"
26 #include "statistics.h"
27 #include "hash-table.h"
30 static int statistics_dump_nr
;
31 static int statistics_dump_flags
;
32 static FILE *statistics_dump_file
;
34 /* Statistics entry. A integer counter associated to a string ID
37 typedef struct statistics_counter_s
{
41 unsigned HOST_WIDE_INT count
;
42 unsigned HOST_WIDE_INT prev_dumped_count
;
43 } statistics_counter_t
;
45 /* Hashtable helpers. */
47 struct stats_counter_hasher
49 typedef statistics_counter_t value_type
;
50 typedef statistics_counter_t compare_type
;
51 static inline hashval_t
hash (const value_type
*);
52 static inline bool equal (const value_type
*, const compare_type
*);
53 static inline void remove (value_type
*);
56 /* Hash a statistic counter by its string ID. */
59 stats_counter_hasher::hash (const value_type
*c
)
61 return htab_hash_string (c
->id
) + c
->val
;
64 /* Compare two statistic counters by their string IDs. */
67 stats_counter_hasher::equal (const value_type
*c1
, const compare_type
*c2
)
69 return c1
->val
== c2
->val
&& strcmp (c1
->id
, c2
->id
) == 0;
72 /* Free a statistics entry. */
75 stats_counter_hasher::remove (value_type
*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
].is_created ())
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
].create (15);
114 return statistics_hashes
[idx
];
117 /* Helper for statistics_fini_pass. Print the counter difference
118 since the last dump for the pass dump files. */
121 statistics_fini_pass_1 (statistics_counter_t
**slot
,
122 void *data ATTRIBUTE_UNUSED
)
124 statistics_counter_t
*counter
= *slot
;
125 unsigned HOST_WIDE_INT count
= counter
->count
- counter
->prev_dumped_count
;
128 if (counter
->histogram_p
)
129 fprintf (dump_file
, "%s == %d: " HOST_WIDE_INT_PRINT_DEC
"\n",
130 counter
->id
, counter
->val
, count
);
132 fprintf (dump_file
, "%s: " HOST_WIDE_INT_PRINT_DEC
"\n",
134 counter
->prev_dumped_count
= counter
->count
;
138 /* Helper for statistics_fini_pass. Print the counter difference
139 since the last dump for the statistics dump. */
142 statistics_fini_pass_2 (statistics_counter_t
**slot
,
143 void *data ATTRIBUTE_UNUSED
)
145 statistics_counter_t
*counter
= *slot
;
146 unsigned HOST_WIDE_INT count
= counter
->count
- counter
->prev_dumped_count
;
149 counter
->prev_dumped_count
= counter
->count
;
150 if (counter
->histogram_p
)
151 fprintf (statistics_dump_file
,
152 "%d %s \"%s == %d\" \"%s\" " HOST_WIDE_INT_PRINT_DEC
"\n",
153 current_pass
->static_pass_number
,
155 counter
->id
, counter
->val
,
156 current_function_name (),
159 fprintf (statistics_dump_file
,
160 "%d %s \"%s\" \"%s\" " HOST_WIDE_INT_PRINT_DEC
"\n",
161 current_pass
->static_pass_number
,
164 current_function_name (),
166 counter
->prev_dumped_count
= counter
->count
;
170 /* Helper for statistics_fini_pass, reset the counters. */
173 statistics_fini_pass_3 (statistics_counter_t
**slot
,
174 void *data ATTRIBUTE_UNUSED
)
176 statistics_counter_t
*counter
= *slot
;
177 counter
->prev_dumped_count
= counter
->count
;
181 /* Dump the current statistics incrementally. */
184 statistics_fini_pass (void)
186 if (current_pass
->static_pass_number
== -1)
190 && dump_flags
& TDF_STATS
)
192 fprintf (dump_file
, "\n");
193 fprintf (dump_file
, "Pass statistics:\n");
194 fprintf (dump_file
, "----------------\n");
195 curr_statistics_hash ()
196 .traverse_noresize
<void *, statistics_fini_pass_1
> (NULL
);
197 fprintf (dump_file
, "\n");
199 if (statistics_dump_file
200 && !(statistics_dump_flags
& TDF_STATS
201 || statistics_dump_flags
& TDF_DETAILS
))
202 curr_statistics_hash ()
203 .traverse_noresize
<void *, statistics_fini_pass_2
> (NULL
);
204 curr_statistics_hash ()
205 .traverse_noresize
<void *, statistics_fini_pass_3
> (NULL
);
208 /* Helper for printing summary information. */
211 statistics_fini_1 (statistics_counter_t
**slot
, opt_pass
*pass
)
213 statistics_counter_t
*counter
= *slot
;
214 if (counter
->count
== 0)
216 if (counter
->histogram_p
)
217 fprintf (statistics_dump_file
,
218 "%d %s \"%s == %d\" " HOST_WIDE_INT_PRINT_DEC
"\n",
219 pass
->static_pass_number
,
221 counter
->id
, counter
->val
,
224 fprintf (statistics_dump_file
,
225 "%d %s \"%s\" " HOST_WIDE_INT_PRINT_DEC
"\n",
226 pass
->static_pass_number
,
233 /* Finish the statistics and dump summary information. */
236 statistics_fini (void)
238 if (!statistics_dump_file
)
241 if (statistics_dump_flags
& TDF_STATS
)
244 for (i
= 0; i
< nr_statistics_hashes
; ++i
)
245 if (statistics_hashes
[i
].is_created ()
246 && get_pass_for_id (i
) != NULL
)
248 .traverse_noresize
<opt_pass
*, statistics_fini_1
>
249 (get_pass_for_id (i
));
252 dump_end (statistics_dump_nr
, statistics_dump_file
);
255 /* Register the statistics dump file. */
258 statistics_early_init (void)
260 statistics_dump_nr
= dump_register (".statistics", "statistics",
261 "statistics", TDF_TREE
, OPTGROUP_NONE
);
264 /* Init the statistics. */
267 statistics_init (void)
269 statistics_dump_file
= dump_begin (statistics_dump_nr
, NULL
);
270 statistics_dump_flags
= get_dump_file_info (statistics_dump_nr
)->pflags
;
273 /* Lookup or add a statistics counter in the hashtable HASH with ID, VAL
276 static statistics_counter_t
*
277 lookup_or_add_counter (stats_counter_table_type hash
, const char *id
, int val
,
280 statistics_counter_t
**counter
;
281 statistics_counter_t c
;
284 counter
= hash
.find_slot (&c
, INSERT
);
287 *counter
= XNEW (struct statistics_counter_s
);
288 (*counter
)->id
= xstrdup (id
);
289 (*counter
)->val
= val
;
290 (*counter
)->histogram_p
= histogram_p
;
291 (*counter
)->prev_dumped_count
= 0;
292 (*counter
)->count
= 0;
297 /* Add statistics information about event ID in function FN.
298 This will increment the counter associated with ID by INCR.
299 It will also dump the event to the global statistics file if requested. */
302 statistics_counter_event (struct function
*fn
, const char *id
, int incr
)
304 statistics_counter_t
*counter
;
306 if ((!(dump_flags
& TDF_STATS
)
307 && !statistics_dump_file
)
311 if (current_pass
->static_pass_number
!= -1)
313 counter
= lookup_or_add_counter (curr_statistics_hash (), id
, 0, false);
314 gcc_assert (!counter
->histogram_p
);
315 counter
->count
+= incr
;
318 if (!statistics_dump_file
319 || !(statistics_dump_flags
& TDF_DETAILS
))
322 fprintf (statistics_dump_file
,
323 "%d %s \"%s\" \"%s\" %d\n",
324 current_pass
->static_pass_number
,
331 /* Add statistics information about event ID in function FN with the
333 It will dump the event to the global statistics file if requested. */
336 statistics_histogram_event (struct function
*fn
, const char *id
, int val
)
338 statistics_counter_t
*counter
;
340 if (!(dump_flags
& TDF_STATS
)
341 && !statistics_dump_file
)
344 counter
= lookup_or_add_counter (curr_statistics_hash (), id
, val
, true);
345 gcc_assert (counter
->histogram_p
);
348 if (!statistics_dump_file
349 || !(statistics_dump_flags
& TDF_DETAILS
))
352 fprintf (statistics_dump_file
,
353 "%d %s \"%s == %d\" \"%s\" 1\n",
354 current_pass
->static_pass_number
,