1 /* Optimization statistics functions.
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"
32 static int statistics_dump_nr
;
33 static int statistics_dump_flags
;
34 static FILE *statistics_dump_file
;
36 /* Statistics entry. A integer counter associated to a string ID
39 typedef struct statistics_counter_s
{
43 unsigned HOST_WIDE_INT count
;
44 unsigned HOST_WIDE_INT prev_dumped_count
;
45 } statistics_counter_t
;
47 /* Array of statistic hashes, indexed by pass id. */
48 static htab_t
*statistics_hashes
;
49 static unsigned nr_statistics_hashes
;
51 /* Hash a statistic counter by its string ID. */
54 hash_statistics_hash (const void *p
)
56 const statistics_counter_t
*const c
= (const statistics_counter_t
*)p
;
57 return htab_hash_string (c
->id
) + c
->val
;
60 /* Compare two statistic counters by their string IDs. */
63 hash_statistics_eq (const void *p
, const void *q
)
65 const statistics_counter_t
*const c1
= (const statistics_counter_t
*)p
;
66 const statistics_counter_t
*const c2
= (const statistics_counter_t
*)q
;
67 return c1
->val
== c2
->val
&& strcmp (c1
->id
, c2
->id
) == 0;
70 /* Free a statistics entry. */
73 hash_statistics_free (void *p
)
75 free (CONST_CAST(char *, ((statistics_counter_t
*)p
)->id
));
79 /* Return the current hashtable to be used for recording or printing
83 curr_statistics_hash (void)
85 unsigned idx
= current_pass
->static_pass_number
;
87 if (idx
< nr_statistics_hashes
88 && statistics_hashes
[idx
] != NULL
)
89 return statistics_hashes
[idx
];
91 if (idx
>= nr_statistics_hashes
)
93 statistics_hashes
= XRESIZEVEC (struct htab
*, statistics_hashes
, idx
+1);
94 memset (statistics_hashes
+ nr_statistics_hashes
, 0,
95 (idx
+ 1 - nr_statistics_hashes
) * sizeof (htab_t
));
96 nr_statistics_hashes
= idx
+ 1;
99 statistics_hashes
[idx
] = htab_create (15, hash_statistics_hash
,
101 hash_statistics_free
);
103 return statistics_hashes
[idx
];
106 /* Helper for statistics_fini_pass. Print the counter difference
107 since the last dump for the pass dump files. */
110 statistics_fini_pass_1 (void **slot
, void *data ATTRIBUTE_UNUSED
)
112 statistics_counter_t
*counter
= (statistics_counter_t
*)*slot
;
113 unsigned HOST_WIDE_INT count
= counter
->count
- counter
->prev_dumped_count
;
116 if (counter
->histogram_p
)
117 fprintf (dump_file
, "%s == %d: " HOST_WIDE_INT_PRINT_DEC
"\n",
118 counter
->id
, counter
->val
, count
);
120 fprintf (dump_file
, "%s: " HOST_WIDE_INT_PRINT_DEC
"\n",
122 counter
->prev_dumped_count
= counter
->count
;
126 /* Helper for statistics_fini_pass. Print the counter difference
127 since the last dump for the statistics dump. */
130 statistics_fini_pass_2 (void **slot
, void *data ATTRIBUTE_UNUSED
)
132 statistics_counter_t
*counter
= (statistics_counter_t
*)*slot
;
133 unsigned HOST_WIDE_INT count
= counter
->count
- counter
->prev_dumped_count
;
136 counter
->prev_dumped_count
= counter
->count
;
137 if (counter
->histogram_p
)
138 fprintf (statistics_dump_file
,
139 "%d %s \"%s == %d\" \"%s\" " HOST_WIDE_INT_PRINT_DEC
"\n",
140 current_pass
->static_pass_number
,
142 counter
->id
, counter
->val
,
143 cfun
? IDENTIFIER_POINTER (DECL_NAME (cfun
->decl
)) : "(nofn)",
146 fprintf (statistics_dump_file
,
147 "%d %s \"%s\" \"%s\" " HOST_WIDE_INT_PRINT_DEC
"\n",
148 current_pass
->static_pass_number
,
151 cfun
? IDENTIFIER_POINTER (DECL_NAME (cfun
->decl
)) : "(nofn)",
153 counter
->prev_dumped_count
= counter
->count
;
157 /* Helper for statistics_fini_pass, reset the counters. */
160 statistics_fini_pass_3 (void **slot
, void *data ATTRIBUTE_UNUSED
)
162 statistics_counter_t
*counter
= (statistics_counter_t
*)*slot
;
163 counter
->prev_dumped_count
= counter
->count
;
167 /* Dump the current statistics incrementally. */
170 statistics_fini_pass (void)
172 if (current_pass
->static_pass_number
== -1)
176 && dump_flags
& TDF_STATS
)
178 fprintf (dump_file
, "\n");
179 fprintf (dump_file
, "Pass statistics:\n");
180 fprintf (dump_file
, "----------------\n");
181 htab_traverse_noresize (curr_statistics_hash (),
182 statistics_fini_pass_1
, NULL
);
183 fprintf (dump_file
, "\n");
185 if (statistics_dump_file
186 && !(statistics_dump_flags
& TDF_STATS
187 || statistics_dump_flags
& TDF_DETAILS
))
188 htab_traverse_noresize (curr_statistics_hash (),
189 statistics_fini_pass_2
, NULL
);
190 htab_traverse_noresize (curr_statistics_hash (),
191 statistics_fini_pass_3
, NULL
);
194 /* Helper for printing summary information. */
197 statistics_fini_1 (void **slot
, void *data
)
199 struct opt_pass
*pass
= (struct opt_pass
*)data
;
200 statistics_counter_t
*counter
= (statistics_counter_t
*)*slot
;
201 if (counter
->count
== 0)
203 if (counter
->histogram_p
)
204 fprintf (statistics_dump_file
,
205 "%d %s \"%s == %d\" " HOST_WIDE_INT_PRINT_DEC
"\n",
206 pass
->static_pass_number
,
208 counter
->id
, counter
->val
,
211 fprintf (statistics_dump_file
,
212 "%d %s \"%s\" " HOST_WIDE_INT_PRINT_DEC
"\n",
213 pass
->static_pass_number
,
220 /* Finish the statistics and dump summary information. */
223 statistics_fini (void)
225 if (!statistics_dump_file
)
228 if (statistics_dump_flags
& TDF_STATS
)
231 for (i
= 0; i
< nr_statistics_hashes
; ++i
)
232 if (statistics_hashes
[i
] != NULL
233 && get_pass_for_id (i
) != NULL
)
234 htab_traverse_noresize (statistics_hashes
[i
],
235 statistics_fini_1
, get_pass_for_id (i
));
238 dump_end (statistics_dump_nr
, statistics_dump_file
);
241 /* Register the statistics dump file. */
244 statistics_early_init (void)
246 statistics_dump_nr
= dump_register (".statistics", "statistics",
247 "statistics", TDF_TREE
);
250 /* Init the statistics. */
253 statistics_init (void)
255 statistics_dump_file
= dump_begin (statistics_dump_nr
, NULL
);
256 statistics_dump_flags
= get_dump_file_info (statistics_dump_nr
)->flags
;
259 /* Lookup or add a statistics counter in the hashtable HASH with ID, VAL
262 static statistics_counter_t
*
263 lookup_or_add_counter (htab_t hash
, const char *id
, int val
,
266 statistics_counter_t
**counter
;
267 statistics_counter_t c
;
270 counter
= (statistics_counter_t
**) htab_find_slot (hash
, &c
, INSERT
);
273 *counter
= XNEW (struct statistics_counter_s
);
274 (*counter
)->id
= xstrdup (id
);
275 (*counter
)->val
= val
;
276 (*counter
)->histogram_p
= histogram_p
;
277 (*counter
)->prev_dumped_count
= 0;
278 (*counter
)->count
= 0;
283 /* Add statistics information about event ID in function FN.
284 This will increment the counter associated with ID by INCR.
285 It will also dump the event to the global statistics file if requested. */
288 statistics_counter_event (struct function
*fn
, const char *id
, int incr
)
290 statistics_counter_t
*counter
;
292 if ((!(dump_flags
& TDF_STATS
)
293 && !statistics_dump_file
)
297 counter
= lookup_or_add_counter (curr_statistics_hash (), id
, 0, false);
298 gcc_assert (!counter
->histogram_p
);
299 counter
->count
+= incr
;
301 if (!statistics_dump_file
302 || !(statistics_dump_flags
& TDF_DETAILS
))
305 fprintf (statistics_dump_file
,
306 "%d %s \"%s\" \"%s\" %d\n",
307 current_pass
->static_pass_number
,
310 fn
? IDENTIFIER_POINTER (DECL_NAME (fn
->decl
)) : "(nofn)",
314 /* Add statistics information about event ID in function FN with the
316 It will dump the event to the global statistics file if requested. */
319 statistics_histogram_event (struct function
*fn
, const char *id
, int val
)
321 statistics_counter_t
*counter
;
323 if (!(dump_flags
& TDF_STATS
)
324 && !statistics_dump_file
)
327 counter
= lookup_or_add_counter (curr_statistics_hash (), id
, val
, true);
328 gcc_assert (counter
->histogram_p
);
331 if (!statistics_dump_file
332 || !(statistics_dump_flags
& TDF_DETAILS
))
335 fprintf (statistics_dump_file
,
336 "%d %s \"%s == %d\" \"%s\" 1\n",
337 current_pass
->static_pass_number
,
340 fn
? IDENTIFIER_POINTER (DECL_NAME (fn
->decl
)) : "(nofn)");