1 /* Optimization statistics functions.
2 Copyright (C) 2008-2015 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"
27 #include "hard-reg-set.h"
30 #include "pass_manager.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 /* Hashtable helpers. */
49 struct stats_counter_hasher
: pointer_hash
<statistics_counter_t
>
51 static inline hashval_t
hash (const statistics_counter_t
*);
52 static inline bool equal (const statistics_counter_t
*,
53 const statistics_counter_t
*);
54 static inline void remove (statistics_counter_t
*);
57 /* Hash a statistic counter by its string ID. */
60 stats_counter_hasher::hash (const statistics_counter_t
*c
)
62 return htab_hash_string (c
->id
) + c
->val
;
65 /* Compare two statistic counters by their string IDs. */
68 stats_counter_hasher::equal (const statistics_counter_t
*c1
,
69 const statistics_counter_t
*c2
)
71 return c1
->val
== c2
->val
&& strcmp (c1
->id
, c2
->id
) == 0;
74 /* Free a statistics entry. */
77 stats_counter_hasher::remove (statistics_counter_t
*v
)
79 free (CONST_CAST (char *, v
->id
));
83 typedef hash_table
<stats_counter_hasher
> stats_counter_table_type
;
85 /* Array of statistic hashes, indexed by pass id. */
86 static stats_counter_table_type
**statistics_hashes
;
87 static unsigned nr_statistics_hashes
;
89 /* Return the current hashtable to be used for recording or printing
92 static stats_counter_table_type
*
93 curr_statistics_hash (void)
97 gcc_assert (current_pass
->static_pass_number
>= 0);
98 idx
= current_pass
->static_pass_number
;
100 if (idx
< nr_statistics_hashes
101 && statistics_hashes
[idx
])
102 return statistics_hashes
[idx
];
104 if (idx
>= nr_statistics_hashes
)
106 statistics_hashes
= XRESIZEVEC (stats_counter_table_type
*,
107 statistics_hashes
, idx
+1);
108 memset (statistics_hashes
+ nr_statistics_hashes
, 0,
109 (idx
+ 1 - nr_statistics_hashes
)
110 * sizeof (stats_counter_table_type
*));
111 nr_statistics_hashes
= idx
+ 1;
114 statistics_hashes
[idx
] = new stats_counter_table_type (15);
116 return statistics_hashes
[idx
];
119 /* Helper for statistics_fini_pass. Print the counter difference
120 since the last dump for the pass dump files. */
123 statistics_fini_pass_1 (statistics_counter_t
**slot
,
124 void *data ATTRIBUTE_UNUSED
)
126 statistics_counter_t
*counter
= *slot
;
127 unsigned HOST_WIDE_INT count
= counter
->count
- counter
->prev_dumped_count
;
130 if (counter
->histogram_p
)
131 fprintf (dump_file
, "%s == %d: " HOST_WIDE_INT_PRINT_DEC
"\n",
132 counter
->id
, counter
->val
, count
);
134 fprintf (dump_file
, "%s: " HOST_WIDE_INT_PRINT_DEC
"\n",
136 counter
->prev_dumped_count
= counter
->count
;
140 /* Helper for statistics_fini_pass. Print the counter difference
141 since the last dump for the statistics dump. */
144 statistics_fini_pass_2 (statistics_counter_t
**slot
,
145 void *data ATTRIBUTE_UNUSED
)
147 statistics_counter_t
*counter
= *slot
;
148 unsigned HOST_WIDE_INT count
= counter
->count
- counter
->prev_dumped_count
;
151 counter
->prev_dumped_count
= counter
->count
;
152 if (counter
->histogram_p
)
153 fprintf (statistics_dump_file
,
154 "%d %s \"%s == %d\" \"%s\" " HOST_WIDE_INT_PRINT_DEC
"\n",
155 current_pass
->static_pass_number
,
157 counter
->id
, counter
->val
,
158 current_function_name (),
161 fprintf (statistics_dump_file
,
162 "%d %s \"%s\" \"%s\" " HOST_WIDE_INT_PRINT_DEC
"\n",
163 current_pass
->static_pass_number
,
166 current_function_name (),
168 counter
->prev_dumped_count
= counter
->count
;
172 /* Helper for statistics_fini_pass, reset the counters. */
175 statistics_fini_pass_3 (statistics_counter_t
**slot
,
176 void *data ATTRIBUTE_UNUSED
)
178 statistics_counter_t
*counter
= *slot
;
179 counter
->prev_dumped_count
= counter
->count
;
183 /* Dump the current statistics incrementally. */
186 statistics_fini_pass (void)
188 if (current_pass
->static_pass_number
== -1)
192 && dump_flags
& TDF_STATS
)
194 fprintf (dump_file
, "\n");
195 fprintf (dump_file
, "Pass statistics of \"%s\": ", current_pass
->name
);
196 fprintf (dump_file
, "----------------\n");
197 curr_statistics_hash ()
198 ->traverse_noresize
<void *, statistics_fini_pass_1
> (NULL
);
199 fprintf (dump_file
, "\n");
201 if (statistics_dump_file
202 && !(statistics_dump_flags
& TDF_STATS
203 || statistics_dump_flags
& TDF_DETAILS
))
204 curr_statistics_hash ()
205 ->traverse_noresize
<void *, statistics_fini_pass_2
> (NULL
);
206 curr_statistics_hash ()
207 ->traverse_noresize
<void *, statistics_fini_pass_3
> (NULL
);
210 /* Helper for printing summary information. */
213 statistics_fini_1 (statistics_counter_t
**slot
, opt_pass
*pass
)
215 statistics_counter_t
*counter
= *slot
;
216 if (counter
->count
== 0)
218 if (counter
->histogram_p
)
219 fprintf (statistics_dump_file
,
220 "%d %s \"%s == %d\" " HOST_WIDE_INT_PRINT_DEC
"\n",
221 pass
->static_pass_number
,
223 counter
->id
, counter
->val
,
226 fprintf (statistics_dump_file
,
227 "%d %s \"%s\" " HOST_WIDE_INT_PRINT_DEC
"\n",
228 pass
->static_pass_number
,
235 /* Finish the statistics and dump summary information. */
238 statistics_fini (void)
240 gcc::pass_manager
*passes
= g
->get_passes ();
241 if (!statistics_dump_file
)
244 if (statistics_dump_flags
& TDF_STATS
)
247 for (i
= 0; i
< nr_statistics_hashes
; ++i
)
248 if (statistics_hashes
[i
]
249 && passes
->get_pass_for_id (i
) != NULL
)
251 ->traverse_noresize
<opt_pass
*, statistics_fini_1
>
252 (passes
->get_pass_for_id (i
));
255 dump_end (statistics_dump_nr
, statistics_dump_file
);
258 /* Register the statistics dump file. */
261 statistics_early_init (void)
263 gcc::dump_manager
*dumps
= g
->get_dumps ();
264 statistics_dump_nr
= dumps
->dump_register (".statistics", "statistics",
265 "statistics", TDF_TREE
,
270 /* Init the statistics. */
273 statistics_init (void)
275 gcc::dump_manager
*dumps
= g
->get_dumps ();
276 statistics_dump_file
= dump_begin (statistics_dump_nr
, NULL
);
277 statistics_dump_flags
= dumps
->get_dump_file_info (statistics_dump_nr
)->pflags
;
280 /* Lookup or add a statistics counter in the hashtable HASH with ID, VAL
283 static statistics_counter_t
*
284 lookup_or_add_counter (stats_counter_table_type
*hash
, const char *id
, int val
,
287 statistics_counter_t
**counter
;
288 statistics_counter_t c
;
291 counter
= hash
->find_slot (&c
, INSERT
);
294 *counter
= XNEW (struct statistics_counter_s
);
295 (*counter
)->id
= xstrdup (id
);
296 (*counter
)->val
= val
;
297 (*counter
)->histogram_p
= histogram_p
;
298 (*counter
)->prev_dumped_count
= 0;
299 (*counter
)->count
= 0;
304 /* Add statistics information about event ID in function FN.
305 This will increment the counter associated with ID by INCR.
306 It will also dump the event to the global statistics file if requested. */
309 statistics_counter_event (struct function
*fn
, const char *id
, int incr
)
311 statistics_counter_t
*counter
;
313 if ((!(dump_flags
& TDF_STATS
)
314 && !statistics_dump_file
)
318 if (current_pass
->static_pass_number
!= -1)
320 counter
= lookup_or_add_counter (curr_statistics_hash (), id
, 0, false);
321 gcc_assert (!counter
->histogram_p
);
322 counter
->count
+= incr
;
325 if (!statistics_dump_file
326 || !(statistics_dump_flags
& TDF_DETAILS
))
329 fprintf (statistics_dump_file
,
330 "%d %s \"%s\" \"%s\" %d\n",
331 current_pass
->static_pass_number
,
338 /* Add statistics information about event ID in function FN with the
340 It will dump the event to the global statistics file if requested. */
343 statistics_histogram_event (struct function
*fn
, const char *id
, int val
)
345 statistics_counter_t
*counter
;
347 if (!(dump_flags
& TDF_STATS
)
348 && !statistics_dump_file
)
351 counter
= lookup_or_add_counter (curr_statistics_hash (), id
, val
, true);
352 gcc_assert (counter
->histogram_p
);
355 if (!statistics_dump_file
356 || !(statistics_dump_flags
& TDF_DETAILS
))
359 fprintf (statistics_dump_file
,
360 "%d %s \"%s == %d\" \"%s\" 1\n",
361 current_pass
->static_pass_number
,