1 /* Optimization statistics functions.
2 Copyright (C) 2008-2018 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"
25 #include "tree-pass.h"
27 #include "pass_manager.h"
29 static int statistics_dump_nr
;
30 static dump_flags_t statistics_dump_flags
;
31 static FILE *statistics_dump_file
;
33 /* Statistics entry. A integer counter associated to a string ID
36 struct statistics_counter
{
40 unsigned HOST_WIDE_INT count
;
41 unsigned HOST_WIDE_INT prev_dumped_count
;
44 /* Hashtable helpers. */
46 struct stats_counter_hasher
: pointer_hash
<statistics_counter
>
48 static inline hashval_t
hash (const statistics_counter
*);
49 static inline bool equal (const statistics_counter
*,
50 const statistics_counter
*);
51 static inline void remove (statistics_counter
*);
54 /* Hash a statistic counter by its string ID. */
57 stats_counter_hasher::hash (const statistics_counter
*c
)
59 return htab_hash_string (c
->id
) + c
->val
;
62 /* Compare two statistic counters by their string IDs. */
65 stats_counter_hasher::equal (const statistics_counter
*c1
,
66 const statistics_counter
*c2
)
68 return c1
->val
== c2
->val
&& strcmp (c1
->id
, c2
->id
) == 0;
71 /* Free a statistics entry. */
74 stats_counter_hasher::remove (statistics_counter
*v
)
76 free (CONST_CAST (char *, v
->id
));
80 typedef hash_table
<stats_counter_hasher
> stats_counter_table_type
;
82 /* Array of statistic hashes, indexed by pass id. */
83 static stats_counter_table_type
**statistics_hashes
;
84 static unsigned nr_statistics_hashes
;
86 /* Return the current hashtable to be used for recording or printing
89 static stats_counter_table_type
*
90 curr_statistics_hash (void)
94 gcc_assert (current_pass
->static_pass_number
>= 0);
95 idx
= current_pass
->static_pass_number
;
97 if (idx
< nr_statistics_hashes
98 && statistics_hashes
[idx
])
99 return statistics_hashes
[idx
];
101 if (idx
>= nr_statistics_hashes
)
103 statistics_hashes
= XRESIZEVEC (stats_counter_table_type
*,
104 statistics_hashes
, idx
+1);
105 memset (statistics_hashes
+ nr_statistics_hashes
, 0,
106 (idx
+ 1 - nr_statistics_hashes
)
107 * sizeof (stats_counter_table_type
*));
108 nr_statistics_hashes
= idx
+ 1;
111 statistics_hashes
[idx
] = new stats_counter_table_type (15);
113 return statistics_hashes
[idx
];
116 /* Helper for statistics_fini_pass. Print the counter difference
117 since the last dump for the pass dump files. */
120 statistics_fini_pass_1 (statistics_counter
**slot
,
121 void *data ATTRIBUTE_UNUSED
)
123 statistics_counter
*counter
= *slot
;
124 unsigned HOST_WIDE_INT count
= counter
->count
- counter
->prev_dumped_count
;
127 if (counter
->histogram_p
)
128 fprintf (dump_file
, "%s == %d: " HOST_WIDE_INT_PRINT_DEC
"\n",
129 counter
->id
, counter
->val
, count
);
131 fprintf (dump_file
, "%s: " HOST_WIDE_INT_PRINT_DEC
"\n",
133 counter
->prev_dumped_count
= counter
->count
;
137 /* Helper for statistics_fini_pass. Print the counter difference
138 since the last dump for the statistics dump. */
141 statistics_fini_pass_2 (statistics_counter
**slot
,
142 void *data ATTRIBUTE_UNUSED
)
144 statistics_counter
*counter
= *slot
;
145 unsigned HOST_WIDE_INT count
= counter
->count
- counter
->prev_dumped_count
;
148 counter
->prev_dumped_count
= counter
->count
;
149 if (counter
->histogram_p
)
150 fprintf (statistics_dump_file
,
151 "%d %s \"%s == %d\" \"%s\" " HOST_WIDE_INT_PRINT_DEC
"\n",
152 current_pass
->static_pass_number
,
154 counter
->id
, counter
->val
,
155 current_function_name (),
158 fprintf (statistics_dump_file
,
159 "%d %s \"%s\" \"%s\" " HOST_WIDE_INT_PRINT_DEC
"\n",
160 current_pass
->static_pass_number
,
163 current_function_name (),
165 counter
->prev_dumped_count
= counter
->count
;
169 /* Helper for statistics_fini_pass, reset the counters. */
172 statistics_fini_pass_3 (statistics_counter
**slot
,
173 void *data ATTRIBUTE_UNUSED
)
175 statistics_counter
*counter
= *slot
;
176 counter
->prev_dumped_count
= counter
->count
;
180 /* Dump the current statistics incrementally. */
183 statistics_fini_pass (void)
185 if (current_pass
->static_pass_number
== -1)
189 && dump_flags
& TDF_STATS
)
191 fprintf (dump_file
, "\n");
192 fprintf (dump_file
, "Pass statistics of \"%s\": ", current_pass
->name
);
193 fprintf (dump_file
, "----------------\n");
194 curr_statistics_hash ()
195 ->traverse_noresize
<void *, statistics_fini_pass_1
> (NULL
);
196 fprintf (dump_file
, "\n");
198 if (statistics_dump_file
199 && !(statistics_dump_flags
& TDF_STATS
200 || statistics_dump_flags
& TDF_DETAILS
))
201 curr_statistics_hash ()
202 ->traverse_noresize
<void *, statistics_fini_pass_2
> (NULL
);
203 curr_statistics_hash ()
204 ->traverse_noresize
<void *, statistics_fini_pass_3
> (NULL
);
207 /* Helper for printing summary information. */
210 statistics_fini_1 (statistics_counter
**slot
, opt_pass
*pass
)
212 statistics_counter
*counter
= *slot
;
213 if (counter
->count
== 0)
215 if (counter
->histogram_p
)
216 fprintf (statistics_dump_file
,
217 "%d %s \"%s == %d\" " HOST_WIDE_INT_PRINT_DEC
"\n",
218 pass
->static_pass_number
,
220 counter
->id
, counter
->val
,
223 fprintf (statistics_dump_file
,
224 "%d %s \"%s\" " HOST_WIDE_INT_PRINT_DEC
"\n",
225 pass
->static_pass_number
,
232 /* Finish the statistics and dump summary information. */
235 statistics_fini (void)
237 gcc::pass_manager
*passes
= g
->get_passes ();
238 if (!statistics_dump_file
)
241 if (statistics_dump_flags
& TDF_STATS
)
244 for (i
= 0; i
< nr_statistics_hashes
; ++i
)
245 if (statistics_hashes
[i
]
246 && passes
->get_pass_for_id (i
) != NULL
)
248 ->traverse_noresize
<opt_pass
*, statistics_fini_1
>
249 (passes
->get_pass_for_id (i
));
252 dump_end (statistics_dump_nr
, statistics_dump_file
);
255 /* Register the statistics dump file. */
258 statistics_early_init (void)
260 gcc::dump_manager
*dumps
= g
->get_dumps ();
261 statistics_dump_nr
= dumps
->dump_register (".statistics", "statistics",
262 "statistics", DK_tree
,
267 /* Init the statistics. */
270 statistics_init (void)
272 gcc::dump_manager
*dumps
= g
->get_dumps ();
273 statistics_dump_file
= dump_begin (statistics_dump_nr
, NULL
);
274 statistics_dump_flags
= dumps
->get_dump_file_info (statistics_dump_nr
)->pflags
;
277 /* Lookup or add a statistics counter in the hashtable HASH with ID, VAL
280 static statistics_counter
*
281 lookup_or_add_counter (stats_counter_table_type
*hash
, const char *id
, int val
,
284 statistics_counter
**counter
;
285 statistics_counter c
;
288 counter
= hash
->find_slot (&c
, INSERT
);
291 *counter
= XNEW (statistics_counter
);
292 (*counter
)->id
= xstrdup (id
);
293 (*counter
)->val
= val
;
294 (*counter
)->histogram_p
= histogram_p
;
295 (*counter
)->prev_dumped_count
= 0;
296 (*counter
)->count
= 0;
301 /* Add statistics information about event ID in function FN.
302 This will increment the counter associated with ID by INCR.
303 It will also dump the event to the global statistics file if requested. */
306 statistics_counter_event (struct function
*fn
, const char *id
, int incr
)
308 statistics_counter
*counter
;
310 if ((!(dump_flags
& TDF_STATS
)
311 && !statistics_dump_file
)
316 && current_pass
->static_pass_number
!= -1)
318 counter
= lookup_or_add_counter (curr_statistics_hash (), id
, 0, false);
319 gcc_assert (!counter
->histogram_p
);
320 counter
->count
+= incr
;
323 if (!statistics_dump_file
324 || !(statistics_dump_flags
& TDF_DETAILS
))
327 fprintf (statistics_dump_file
,
328 "%d %s \"%s\" \"%s\" %d\n",
329 current_pass
? current_pass
->static_pass_number
: -1,
330 current_pass
? current_pass
->name
: "none",
336 /* Add statistics information about event ID in function FN with the
338 It will dump the event to the global statistics file if requested. */
341 statistics_histogram_event (struct function
*fn
, const char *id
, int val
)
343 statistics_counter
*counter
;
345 if (!(dump_flags
& TDF_STATS
)
346 && !statistics_dump_file
)
349 counter
= lookup_or_add_counter (curr_statistics_hash (), id
, val
, true);
350 gcc_assert (counter
->histogram_p
);
353 if (!statistics_dump_file
354 || !(statistics_dump_flags
& TDF_DETAILS
))
357 fprintf (statistics_dump_file
,
358 "%d %s \"%s == %d\" \"%s\" 1\n",
359 current_pass
->static_pass_number
,