gcc/
[official-gcc.git] / gcc / statistics.c
blob233926d53a61f00d22719aa66f594b3ba53fe1bb
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
10 version.
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
15 for more details.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tree-pass.h"
25 #include "tree-dump.h"
26 #include "tm.h"
27 #include "hard-reg-set.h"
28 #include "function.h"
29 #include "context.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
37 and value. */
39 typedef struct statistics_counter_s {
40 const char *id;
41 int val;
42 bool histogram_p;
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. */
61 inline hashval_t
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. */
69 inline bool
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. */
78 inline void
79 stats_counter_hasher::remove (statistics_counter_t *v)
81 free (CONST_CAST (char *, v->id));
82 free (v);
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
92 statistics. */
94 static stats_counter_table_type *
95 curr_statistics_hash (void)
97 unsigned idx;
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;
130 if (count == 0)
131 return 1;
132 if (counter->histogram_p)
133 fprintf (dump_file, "%s == %d: " HOST_WIDE_INT_PRINT_DEC "\n",
134 counter->id, counter->val, count);
135 else
136 fprintf (dump_file, "%s: " HOST_WIDE_INT_PRINT_DEC "\n",
137 counter->id, count);
138 counter->prev_dumped_count = counter->count;
139 return 1;
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;
151 if (count == 0)
152 return 1;
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,
158 current_pass->name,
159 counter->id, counter->val,
160 current_function_name (),
161 count);
162 else
163 fprintf (statistics_dump_file,
164 "%d %s \"%s\" \"%s\" " HOST_WIDE_INT_PRINT_DEC "\n",
165 current_pass->static_pass_number,
166 current_pass->name,
167 counter->id,
168 current_function_name (),
169 count);
170 counter->prev_dumped_count = counter->count;
171 return 1;
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;
182 return 1;
185 /* Dump the current statistics incrementally. */
187 void
188 statistics_fini_pass (void)
190 if (current_pass->static_pass_number == -1)
191 return;
193 if (dump_file
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)
219 return 1;
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,
224 pass->name,
225 counter->id, counter->val,
226 counter->count);
227 else
228 fprintf (statistics_dump_file,
229 "%d %s \"%s\" " HOST_WIDE_INT_PRINT_DEC "\n",
230 pass->static_pass_number,
231 pass->name,
232 counter->id,
233 counter->count);
234 return 1;
237 /* Finish the statistics and dump summary information. */
239 void
240 statistics_fini (void)
242 gcc::pass_manager *passes = g->get_passes ();
243 if (!statistics_dump_file)
244 return;
246 if (statistics_dump_flags & TDF_STATS)
248 unsigned i;
249 for (i = 0; i < nr_statistics_hashes; ++i)
250 if (statistics_hashes[i]
251 && passes->get_pass_for_id (i) != NULL)
252 statistics_hashes[i]
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. */
262 void
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,
268 OPTGROUP_NONE,
269 false);
272 /* Init the statistics. */
274 void
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
283 and HISTOGRAM_P. */
285 static statistics_counter_t *
286 lookup_or_add_counter (stats_counter_table_type *hash, const char *id, int val,
287 bool histogram_p)
289 statistics_counter_t **counter;
290 statistics_counter_t c;
291 c.id = id;
292 c.val = val;
293 counter = hash->find_slot (&c, INSERT);
294 if (!*counter)
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;
303 return *counter;
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. */
310 void
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)
317 || incr == 0)
318 return;
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))
329 return;
331 fprintf (statistics_dump_file,
332 "%d %s \"%s\" \"%s\" %d\n",
333 current_pass->static_pass_number,
334 current_pass->name,
336 function_name (fn),
337 incr);
340 /* Add statistics information about event ID in function FN with the
341 histogram value VAL.
342 It will dump the event to the global statistics file if requested. */
344 void
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)
351 return;
353 counter = lookup_or_add_counter (curr_statistics_hash (), id, val, true);
354 gcc_assert (counter->histogram_p);
355 counter->count += 1;
357 if (!statistics_dump_file
358 || !(statistics_dump_flags & TDF_DETAILS))
359 return;
361 fprintf (statistics_dump_file,
362 "%d %s \"%s == %d\" \"%s\" 1\n",
363 current_pass->static_pass_number,
364 current_pass->name,
365 id, val,
366 function_name (fn));