1 /* Optimization statistics functions.
2 Copyright (C) 2008-2014 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"
33 #include "hard-reg-set.h"
37 #include "pass_manager.h"
39 static int statistics_dump_nr
;
40 static int statistics_dump_flags
;
41 static FILE *statistics_dump_file
;
43 /* Statistics entry. A integer counter associated to a string ID
46 typedef struct statistics_counter_s
{
50 unsigned HOST_WIDE_INT count
;
51 unsigned HOST_WIDE_INT prev_dumped_count
;
52 } statistics_counter_t
;
54 /* Hashtable helpers. */
56 struct stats_counter_hasher
58 typedef statistics_counter_t value_type
;
59 typedef statistics_counter_t compare_type
;
60 static inline hashval_t
hash (const value_type
*);
61 static inline bool equal (const value_type
*, const compare_type
*);
62 static inline void remove (value_type
*);
65 /* Hash a statistic counter by its string ID. */
68 stats_counter_hasher::hash (const value_type
*c
)
70 return htab_hash_string (c
->id
) + c
->val
;
73 /* Compare two statistic counters by their string IDs. */
76 stats_counter_hasher::equal (const value_type
*c1
, const compare_type
*c2
)
78 return c1
->val
== c2
->val
&& strcmp (c1
->id
, c2
->id
) == 0;
81 /* Free a statistics entry. */
84 stats_counter_hasher::remove (value_type
*v
)
86 free (CONST_CAST (char *, v
->id
));
90 typedef hash_table
<stats_counter_hasher
> stats_counter_table_type
;
92 /* Array of statistic hashes, indexed by pass id. */
93 static stats_counter_table_type
**statistics_hashes
;
94 static unsigned nr_statistics_hashes
;
96 /* Return the current hashtable to be used for recording or printing
99 static stats_counter_table_type
*
100 curr_statistics_hash (void)
104 gcc_assert (current_pass
->static_pass_number
>= 0);
105 idx
= current_pass
->static_pass_number
;
107 if (idx
< nr_statistics_hashes
108 && statistics_hashes
[idx
])
109 return statistics_hashes
[idx
];
111 if (idx
>= nr_statistics_hashes
)
113 statistics_hashes
= XRESIZEVEC (stats_counter_table_type
*,
114 statistics_hashes
, idx
+1);
115 memset (statistics_hashes
+ nr_statistics_hashes
, 0,
116 (idx
+ 1 - nr_statistics_hashes
)
117 * sizeof (stats_counter_table_type
*));
118 nr_statistics_hashes
= idx
+ 1;
121 statistics_hashes
[idx
] = new stats_counter_table_type (15);
123 return statistics_hashes
[idx
];
126 /* Helper for statistics_fini_pass. Print the counter difference
127 since the last dump for the pass dump files. */
130 statistics_fini_pass_1 (statistics_counter_t
**slot
,
131 void *data ATTRIBUTE_UNUSED
)
133 statistics_counter_t
*counter
= *slot
;
134 unsigned HOST_WIDE_INT count
= counter
->count
- counter
->prev_dumped_count
;
137 if (counter
->histogram_p
)
138 fprintf (dump_file
, "%s == %d: " HOST_WIDE_INT_PRINT_DEC
"\n",
139 counter
->id
, counter
->val
, count
);
141 fprintf (dump_file
, "%s: " HOST_WIDE_INT_PRINT_DEC
"\n",
143 counter
->prev_dumped_count
= counter
->count
;
147 /* Helper for statistics_fini_pass. Print the counter difference
148 since the last dump for the statistics dump. */
151 statistics_fini_pass_2 (statistics_counter_t
**slot
,
152 void *data ATTRIBUTE_UNUSED
)
154 statistics_counter_t
*counter
= *slot
;
155 unsigned HOST_WIDE_INT count
= counter
->count
- counter
->prev_dumped_count
;
158 counter
->prev_dumped_count
= counter
->count
;
159 if (counter
->histogram_p
)
160 fprintf (statistics_dump_file
,
161 "%d %s \"%s == %d\" \"%s\" " HOST_WIDE_INT_PRINT_DEC
"\n",
162 current_pass
->static_pass_number
,
164 counter
->id
, counter
->val
,
165 current_function_name (),
168 fprintf (statistics_dump_file
,
169 "%d %s \"%s\" \"%s\" " HOST_WIDE_INT_PRINT_DEC
"\n",
170 current_pass
->static_pass_number
,
173 current_function_name (),
175 counter
->prev_dumped_count
= counter
->count
;
179 /* Helper for statistics_fini_pass, reset the counters. */
182 statistics_fini_pass_3 (statistics_counter_t
**slot
,
183 void *data ATTRIBUTE_UNUSED
)
185 statistics_counter_t
*counter
= *slot
;
186 counter
->prev_dumped_count
= counter
->count
;
190 /* Dump the current statistics incrementally. */
193 statistics_fini_pass (void)
195 if (current_pass
->static_pass_number
== -1)
199 && dump_flags
& TDF_STATS
)
201 fprintf (dump_file
, "\n");
202 fprintf (dump_file
, "Pass statistics:\n");
203 fprintf (dump_file
, "----------------\n");
204 curr_statistics_hash ()
205 ->traverse_noresize
<void *, statistics_fini_pass_1
> (NULL
);
206 fprintf (dump_file
, "\n");
208 if (statistics_dump_file
209 && !(statistics_dump_flags
& TDF_STATS
210 || statistics_dump_flags
& TDF_DETAILS
))
211 curr_statistics_hash ()
212 ->traverse_noresize
<void *, statistics_fini_pass_2
> (NULL
);
213 curr_statistics_hash ()
214 ->traverse_noresize
<void *, statistics_fini_pass_3
> (NULL
);
217 /* Helper for printing summary information. */
220 statistics_fini_1 (statistics_counter_t
**slot
, opt_pass
*pass
)
222 statistics_counter_t
*counter
= *slot
;
223 if (counter
->count
== 0)
225 if (counter
->histogram_p
)
226 fprintf (statistics_dump_file
,
227 "%d %s \"%s == %d\" " HOST_WIDE_INT_PRINT_DEC
"\n",
228 pass
->static_pass_number
,
230 counter
->id
, counter
->val
,
233 fprintf (statistics_dump_file
,
234 "%d %s \"%s\" " HOST_WIDE_INT_PRINT_DEC
"\n",
235 pass
->static_pass_number
,
242 /* Finish the statistics and dump summary information. */
245 statistics_fini (void)
247 gcc::pass_manager
*passes
= g
->get_passes ();
248 if (!statistics_dump_file
)
251 if (statistics_dump_flags
& TDF_STATS
)
254 for (i
= 0; i
< nr_statistics_hashes
; ++i
)
255 if (statistics_hashes
[i
]
256 && passes
->get_pass_for_id (i
) != NULL
)
258 ->traverse_noresize
<opt_pass
*, statistics_fini_1
>
259 (passes
->get_pass_for_id (i
));
262 dump_end (statistics_dump_nr
, statistics_dump_file
);
265 /* Register the statistics dump file. */
268 statistics_early_init (void)
270 gcc::dump_manager
*dumps
= g
->get_dumps ();
271 statistics_dump_nr
= dumps
->dump_register (".statistics", "statistics",
272 "statistics", TDF_TREE
,
276 /* Init the statistics. */
279 statistics_init (void)
281 gcc::dump_manager
*dumps
= g
->get_dumps ();
282 statistics_dump_file
= dump_begin (statistics_dump_nr
, NULL
);
283 statistics_dump_flags
= dumps
->get_dump_file_info (statistics_dump_nr
)->pflags
;
286 /* Lookup or add a statistics counter in the hashtable HASH with ID, VAL
289 static statistics_counter_t
*
290 lookup_or_add_counter (stats_counter_table_type
*hash
, const char *id
, int val
,
293 statistics_counter_t
**counter
;
294 statistics_counter_t c
;
297 counter
= hash
->find_slot (&c
, INSERT
);
300 *counter
= XNEW (struct statistics_counter_s
);
301 (*counter
)->id
= xstrdup (id
);
302 (*counter
)->val
= val
;
303 (*counter
)->histogram_p
= histogram_p
;
304 (*counter
)->prev_dumped_count
= 0;
305 (*counter
)->count
= 0;
310 /* Add statistics information about event ID in function FN.
311 This will increment the counter associated with ID by INCR.
312 It will also dump the event to the global statistics file if requested. */
315 statistics_counter_event (struct function
*fn
, const char *id
, int incr
)
317 statistics_counter_t
*counter
;
319 if ((!(dump_flags
& TDF_STATS
)
320 && !statistics_dump_file
)
324 if (current_pass
->static_pass_number
!= -1)
326 counter
= lookup_or_add_counter (curr_statistics_hash (), id
, 0, false);
327 gcc_assert (!counter
->histogram_p
);
328 counter
->count
+= incr
;
331 if (!statistics_dump_file
332 || !(statistics_dump_flags
& TDF_DETAILS
))
335 fprintf (statistics_dump_file
,
336 "%d %s \"%s\" \"%s\" %d\n",
337 current_pass
->static_pass_number
,
344 /* Add statistics information about event ID in function FN with the
346 It will dump the event to the global statistics file if requested. */
349 statistics_histogram_event (struct function
*fn
, const char *id
, int val
)
351 statistics_counter_t
*counter
;
353 if (!(dump_flags
& TDF_STATS
)
354 && !statistics_dump_file
)
357 counter
= lookup_or_add_counter (curr_statistics_hash (), id
, val
, true);
358 gcc_assert (counter
->histogram_p
);
361 if (!statistics_dump_file
362 || !(statistics_dump_flags
& TDF_DETAILS
))
365 fprintf (statistics_dump_file
,
366 "%d %s \"%s == %d\" \"%s\" 1\n",
367 current_pass
->static_pass_number
,