1 /* Optimization statistics functions.
2 Copyright (C) 2008, 2010
3 Free Software Foundation, Inc.
4 Contributed by Richard Guenther <rguenther@suse.de>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
25 #include "tree-pass.h"
26 #include "tree-dump.h"
27 #include "statistics.h"
31 static int statistics_dump_nr
;
32 static int statistics_dump_flags
;
33 static FILE *statistics_dump_file
;
35 /* Statistics entry. A integer counter associated to a string ID
38 typedef struct statistics_counter_s
{
42 unsigned HOST_WIDE_INT count
;
43 unsigned HOST_WIDE_INT prev_dumped_count
;
44 } statistics_counter_t
;
46 /* Array of statistic hashes, indexed by pass id. */
47 static htab_t
*statistics_hashes
;
48 static unsigned nr_statistics_hashes
;
50 /* Hash a statistic counter by its string ID. */
53 hash_statistics_hash (const void *p
)
55 const statistics_counter_t
*const c
= (const statistics_counter_t
*)p
;
56 return htab_hash_string (c
->id
) + c
->val
;
59 /* Compare two statistic counters by their string IDs. */
62 hash_statistics_eq (const void *p
, const void *q
)
64 const statistics_counter_t
*const c1
= (const statistics_counter_t
*)p
;
65 const statistics_counter_t
*const c2
= (const statistics_counter_t
*)q
;
66 return c1
->val
== c2
->val
&& strcmp (c1
->id
, c2
->id
) == 0;
69 /* Free a statistics entry. */
72 hash_statistics_free (void *p
)
74 free (CONST_CAST(char *, ((statistics_counter_t
*)p
)->id
));
78 /* Return the current hashtable to be used for recording or printing
82 curr_statistics_hash (void)
86 gcc_assert (current_pass
->static_pass_number
>= 0);
87 idx
= current_pass
->static_pass_number
;
89 if (idx
< nr_statistics_hashes
90 && statistics_hashes
[idx
] != NULL
)
91 return statistics_hashes
[idx
];
93 if (idx
>= nr_statistics_hashes
)
95 statistics_hashes
= XRESIZEVEC (struct htab
*, statistics_hashes
, idx
+1);
96 memset (statistics_hashes
+ nr_statistics_hashes
, 0,
97 (idx
+ 1 - nr_statistics_hashes
) * sizeof (htab_t
));
98 nr_statistics_hashes
= idx
+ 1;
101 statistics_hashes
[idx
] = htab_create (15, hash_statistics_hash
,
103 hash_statistics_free
);
105 return statistics_hashes
[idx
];
108 /* Helper for statistics_fini_pass. Print the counter difference
109 since the last dump for the pass dump files. */
112 statistics_fini_pass_1 (void **slot
, void *data ATTRIBUTE_UNUSED
)
114 statistics_counter_t
*counter
= (statistics_counter_t
*)*slot
;
115 unsigned HOST_WIDE_INT count
= counter
->count
- counter
->prev_dumped_count
;
118 if (counter
->histogram_p
)
119 fprintf (dump_file
, "%s == %d: " HOST_WIDE_INT_PRINT_DEC
"\n",
120 counter
->id
, counter
->val
, count
);
122 fprintf (dump_file
, "%s: " HOST_WIDE_INT_PRINT_DEC
"\n",
124 counter
->prev_dumped_count
= counter
->count
;
128 /* Helper for statistics_fini_pass. Print the counter difference
129 since the last dump for the statistics dump. */
132 statistics_fini_pass_2 (void **slot
, void *data ATTRIBUTE_UNUSED
)
134 statistics_counter_t
*counter
= (statistics_counter_t
*)*slot
;
135 unsigned HOST_WIDE_INT count
= counter
->count
- counter
->prev_dumped_count
;
138 counter
->prev_dumped_count
= counter
->count
;
139 if (counter
->histogram_p
)
140 fprintf (statistics_dump_file
,
141 "%d %s \"%s == %d\" \"%s\" " HOST_WIDE_INT_PRINT_DEC
"\n",
142 current_pass
->static_pass_number
,
144 counter
->id
, counter
->val
,
145 current_function_name (),
148 fprintf (statistics_dump_file
,
149 "%d %s \"%s\" \"%s\" " HOST_WIDE_INT_PRINT_DEC
"\n",
150 current_pass
->static_pass_number
,
153 current_function_name (),
155 counter
->prev_dumped_count
= counter
->count
;
159 /* Helper for statistics_fini_pass, reset the counters. */
162 statistics_fini_pass_3 (void **slot
, void *data ATTRIBUTE_UNUSED
)
164 statistics_counter_t
*counter
= (statistics_counter_t
*)*slot
;
165 counter
->prev_dumped_count
= counter
->count
;
169 /* Dump the current statistics incrementally. */
172 statistics_fini_pass (void)
174 if (current_pass
->static_pass_number
== -1)
178 && dump_flags
& TDF_STATS
)
180 fprintf (dump_file
, "\n");
181 fprintf (dump_file
, "Pass statistics:\n");
182 fprintf (dump_file
, "----------------\n");
183 htab_traverse_noresize (curr_statistics_hash (),
184 statistics_fini_pass_1
, NULL
);
185 fprintf (dump_file
, "\n");
187 if (statistics_dump_file
188 && !(statistics_dump_flags
& TDF_STATS
189 || statistics_dump_flags
& TDF_DETAILS
))
190 htab_traverse_noresize (curr_statistics_hash (),
191 statistics_fini_pass_2
, NULL
);
192 htab_traverse_noresize (curr_statistics_hash (),
193 statistics_fini_pass_3
, NULL
);
196 /* Helper for printing summary information. */
199 statistics_fini_1 (void **slot
, void *data
)
201 struct opt_pass
*pass
= (struct opt_pass
*)data
;
202 statistics_counter_t
*counter
= (statistics_counter_t
*)*slot
;
203 if (counter
->count
== 0)
205 if (counter
->histogram_p
)
206 fprintf (statistics_dump_file
,
207 "%d %s \"%s == %d\" " HOST_WIDE_INT_PRINT_DEC
"\n",
208 pass
->static_pass_number
,
210 counter
->id
, counter
->val
,
213 fprintf (statistics_dump_file
,
214 "%d %s \"%s\" " HOST_WIDE_INT_PRINT_DEC
"\n",
215 pass
->static_pass_number
,
222 /* Finish the statistics and dump summary information. */
225 statistics_fini (void)
227 if (!statistics_dump_file
)
230 if (statistics_dump_flags
& TDF_STATS
)
233 for (i
= 0; i
< nr_statistics_hashes
; ++i
)
234 if (statistics_hashes
[i
] != NULL
235 && get_pass_for_id (i
) != NULL
)
236 htab_traverse_noresize (statistics_hashes
[i
],
237 statistics_fini_1
, get_pass_for_id (i
));
240 dump_end (statistics_dump_nr
, statistics_dump_file
);
243 /* Register the statistics dump file. */
246 statistics_early_init (void)
248 statistics_dump_nr
= dump_register (".statistics", "statistics",
249 "statistics", TDF_TREE
, OPTGROUP_NONE
);
252 /* Init the statistics. */
255 statistics_init (void)
257 statistics_dump_file
= dump_begin (statistics_dump_nr
, NULL
);
258 statistics_dump_flags
= get_dump_file_info (statistics_dump_nr
)->pflags
;
261 /* Lookup or add a statistics counter in the hashtable HASH with ID, VAL
264 static statistics_counter_t
*
265 lookup_or_add_counter (htab_t hash
, const char *id
, int val
,
268 statistics_counter_t
**counter
;
269 statistics_counter_t c
;
272 counter
= (statistics_counter_t
**) htab_find_slot (hash
, &c
, INSERT
);
275 *counter
= XNEW (struct statistics_counter_s
);
276 (*counter
)->id
= xstrdup (id
);
277 (*counter
)->val
= val
;
278 (*counter
)->histogram_p
= histogram_p
;
279 (*counter
)->prev_dumped_count
= 0;
280 (*counter
)->count
= 0;
285 /* Add statistics information about event ID in function FN.
286 This will increment the counter associated with ID by INCR.
287 It will also dump the event to the global statistics file if requested. */
290 statistics_counter_event (struct function
*fn
, const char *id
, int incr
)
292 statistics_counter_t
*counter
;
294 if ((!(dump_flags
& TDF_STATS
)
295 && !statistics_dump_file
)
299 if (current_pass
->static_pass_number
!= -1)
301 counter
= lookup_or_add_counter (curr_statistics_hash (), id
, 0, false);
302 gcc_assert (!counter
->histogram_p
);
303 counter
->count
+= incr
;
306 if (!statistics_dump_file
307 || !(statistics_dump_flags
& TDF_DETAILS
))
310 fprintf (statistics_dump_file
,
311 "%d %s \"%s\" \"%s\" %d\n",
312 current_pass
->static_pass_number
,
319 /* Add statistics information about event ID in function FN with the
321 It will dump the event to the global statistics file if requested. */
324 statistics_histogram_event (struct function
*fn
, const char *id
, int val
)
326 statistics_counter_t
*counter
;
328 if (!(dump_flags
& TDF_STATS
)
329 && !statistics_dump_file
)
332 counter
= lookup_or_add_counter (curr_statistics_hash (), id
, val
, true);
333 gcc_assert (counter
->histogram_p
);
336 if (!statistics_dump_file
337 || !(statistics_dump_flags
& TDF_DETAILS
))
340 fprintf (statistics_dump_file
,
341 "%d %s \"%s == %d\" \"%s\" 1\n",
342 current_pass
->static_pass_number
,