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
51 typedef statistics_counter_t
*value_type
;
52 typedef statistics_counter_t
*compare_type
;
53 static inline hashval_t
hash (const statistics_counter_t
*);
54 static inline bool equal (const statistics_counter_t
*,
55 const statistics_counter_t
*);
56 static inline void remove (statistics_counter_t
*);
59 /* Hash a statistic counter by its string ID. */
62 stats_counter_hasher::hash (const statistics_counter_t
*c
)
64 return htab_hash_string (c
->id
) + c
->val
;
67 /* Compare two statistic counters by their string IDs. */
70 stats_counter_hasher::equal (const statistics_counter_t
*c1
,
71 const statistics_counter_t
*c2
)
73 return c1
->val
== c2
->val
&& strcmp (c1
->id
, c2
->id
) == 0;
76 /* Free a statistics entry. */
79 stats_counter_hasher::remove (statistics_counter_t
*v
)
81 free (CONST_CAST (char *, v
->id
));
85 typedef hash_table
<stats_counter_hasher
> stats_counter_table_type
;
87 /* Array of statistic hashes, indexed by pass id. */
88 static stats_counter_table_type
**statistics_hashes
;
89 static unsigned nr_statistics_hashes
;
91 /* Return the current hashtable to be used for recording or printing
94 static stats_counter_table_type
*
95 curr_statistics_hash (void)
99 gcc_assert (current_pass
->static_pass_number
>= 0);
100 idx
= current_pass
->static_pass_number
;
102 if (idx
< nr_statistics_hashes
103 && statistics_hashes
[idx
])
104 return statistics_hashes
[idx
];
106 if (idx
>= nr_statistics_hashes
)
108 statistics_hashes
= XRESIZEVEC (stats_counter_table_type
*,
109 statistics_hashes
, idx
+1);
110 memset (statistics_hashes
+ nr_statistics_hashes
, 0,
111 (idx
+ 1 - nr_statistics_hashes
)
112 * sizeof (stats_counter_table_type
*));
113 nr_statistics_hashes
= idx
+ 1;
116 statistics_hashes
[idx
] = new stats_counter_table_type (15);
118 return statistics_hashes
[idx
];
121 /* Helper for statistics_fini_pass. Print the counter difference
122 since the last dump for the pass dump files. */
125 statistics_fini_pass_1 (statistics_counter_t
**slot
,
126 void *data ATTRIBUTE_UNUSED
)
128 statistics_counter_t
*counter
= *slot
;
129 unsigned HOST_WIDE_INT count
= counter
->count
- counter
->prev_dumped_count
;
132 if (counter
->histogram_p
)
133 fprintf (dump_file
, "%s == %d: " HOST_WIDE_INT_PRINT_DEC
"\n",
134 counter
->id
, counter
->val
, count
);
136 fprintf (dump_file
, "%s: " HOST_WIDE_INT_PRINT_DEC
"\n",
138 counter
->prev_dumped_count
= counter
->count
;
142 /* Helper for statistics_fini_pass. Print the counter difference
143 since the last dump for the statistics dump. */
146 statistics_fini_pass_2 (statistics_counter_t
**slot
,
147 void *data ATTRIBUTE_UNUSED
)
149 statistics_counter_t
*counter
= *slot
;
150 unsigned HOST_WIDE_INT count
= counter
->count
- counter
->prev_dumped_count
;
153 counter
->prev_dumped_count
= counter
->count
;
154 if (counter
->histogram_p
)
155 fprintf (statistics_dump_file
,
156 "%d %s \"%s == %d\" \"%s\" " HOST_WIDE_INT_PRINT_DEC
"\n",
157 current_pass
->static_pass_number
,
159 counter
->id
, counter
->val
,
160 current_function_name (),
163 fprintf (statistics_dump_file
,
164 "%d %s \"%s\" \"%s\" " HOST_WIDE_INT_PRINT_DEC
"\n",
165 current_pass
->static_pass_number
,
168 current_function_name (),
170 counter
->prev_dumped_count
= counter
->count
;
174 /* Helper for statistics_fini_pass, reset the counters. */
177 statistics_fini_pass_3 (statistics_counter_t
**slot
,
178 void *data ATTRIBUTE_UNUSED
)
180 statistics_counter_t
*counter
= *slot
;
181 counter
->prev_dumped_count
= counter
->count
;
185 /* Dump the current statistics incrementally. */
188 statistics_fini_pass (void)
190 if (current_pass
->static_pass_number
== -1)
194 && dump_flags
& TDF_STATS
)
196 fprintf (dump_file
, "\n");
197 fprintf (dump_file
, "Pass statistics of \"%s\": ", current_pass
->name
);
198 fprintf (dump_file
, "----------------\n");
199 curr_statistics_hash ()
200 ->traverse_noresize
<void *, statistics_fini_pass_1
> (NULL
);
201 fprintf (dump_file
, "\n");
203 if (statistics_dump_file
204 && !(statistics_dump_flags
& TDF_STATS
205 || statistics_dump_flags
& TDF_DETAILS
))
206 curr_statistics_hash ()
207 ->traverse_noresize
<void *, statistics_fini_pass_2
> (NULL
);
208 curr_statistics_hash ()
209 ->traverse_noresize
<void *, statistics_fini_pass_3
> (NULL
);
212 /* Helper for printing summary information. */
215 statistics_fini_1 (statistics_counter_t
**slot
, opt_pass
*pass
)
217 statistics_counter_t
*counter
= *slot
;
218 if (counter
->count
== 0)
220 if (counter
->histogram_p
)
221 fprintf (statistics_dump_file
,
222 "%d %s \"%s == %d\" " HOST_WIDE_INT_PRINT_DEC
"\n",
223 pass
->static_pass_number
,
225 counter
->id
, counter
->val
,
228 fprintf (statistics_dump_file
,
229 "%d %s \"%s\" " HOST_WIDE_INT_PRINT_DEC
"\n",
230 pass
->static_pass_number
,
237 /* Finish the statistics and dump summary information. */
240 statistics_fini (void)
242 gcc::pass_manager
*passes
= g
->get_passes ();
243 if (!statistics_dump_file
)
246 if (statistics_dump_flags
& TDF_STATS
)
249 for (i
= 0; i
< nr_statistics_hashes
; ++i
)
250 if (statistics_hashes
[i
]
251 && passes
->get_pass_for_id (i
) != NULL
)
253 ->traverse_noresize
<opt_pass
*, statistics_fini_1
>
254 (passes
->get_pass_for_id (i
));
257 dump_end (statistics_dump_nr
, statistics_dump_file
);
260 /* Register the statistics dump file. */
263 statistics_early_init (void)
265 gcc::dump_manager
*dumps
= g
->get_dumps ();
266 statistics_dump_nr
= dumps
->dump_register (".statistics", "statistics",
267 "statistics", TDF_TREE
,
272 /* Init the statistics. */
275 statistics_init (void)
277 gcc::dump_manager
*dumps
= g
->get_dumps ();
278 statistics_dump_file
= dump_begin (statistics_dump_nr
, NULL
);
279 statistics_dump_flags
= dumps
->get_dump_file_info (statistics_dump_nr
)->pflags
;
282 /* Lookup or add a statistics counter in the hashtable HASH with ID, VAL
285 static statistics_counter_t
*
286 lookup_or_add_counter (stats_counter_table_type
*hash
, const char *id
, int val
,
289 statistics_counter_t
**counter
;
290 statistics_counter_t c
;
293 counter
= hash
->find_slot (&c
, INSERT
);
296 *counter
= XNEW (struct statistics_counter_s
);
297 (*counter
)->id
= xstrdup (id
);
298 (*counter
)->val
= val
;
299 (*counter
)->histogram_p
= histogram_p
;
300 (*counter
)->prev_dumped_count
= 0;
301 (*counter
)->count
= 0;
306 /* Add statistics information about event ID in function FN.
307 This will increment the counter associated with ID by INCR.
308 It will also dump the event to the global statistics file if requested. */
311 statistics_counter_event (struct function
*fn
, const char *id
, int incr
)
313 statistics_counter_t
*counter
;
315 if ((!(dump_flags
& TDF_STATS
)
316 && !statistics_dump_file
)
320 if (current_pass
->static_pass_number
!= -1)
322 counter
= lookup_or_add_counter (curr_statistics_hash (), id
, 0, false);
323 gcc_assert (!counter
->histogram_p
);
324 counter
->count
+= incr
;
327 if (!statistics_dump_file
328 || !(statistics_dump_flags
& TDF_DETAILS
))
331 fprintf (statistics_dump_file
,
332 "%d %s \"%s\" \"%s\" %d\n",
333 current_pass
->static_pass_number
,
340 /* Add statistics information about event ID in function FN with the
342 It will dump the event to the global statistics file if requested. */
345 statistics_histogram_event (struct function
*fn
, const char *id
, int val
)
347 statistics_counter_t
*counter
;
349 if (!(dump_flags
& TDF_STATS
)
350 && !statistics_dump_file
)
353 counter
= lookup_or_add_counter (curr_statistics_hash (), id
, val
, true);
354 gcc_assert (counter
->histogram_p
);
357 if (!statistics_dump_file
358 || !(statistics_dump_flags
& TDF_DETAILS
))
361 fprintf (statistics_dump_file
,
362 "%d %s \"%s == %d\" \"%s\" 1\n",
363 current_pass
->static_pass_number
,