Implement TARGET_IRA_CHANGE_PSEUDO_ALLOCNO_CLASS hook.
[official-gcc.git] / gcc / statistics.c
blobe76ad95189ec324e59f1ddf9bf4aea1b4a5d07c7
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 "input.h"
29 #include "function.h"
30 #include "context.h"
31 #include "pass_manager.h"
33 static int statistics_dump_nr;
34 static int statistics_dump_flags;
35 static FILE *statistics_dump_file;
37 /* Statistics entry. A integer counter associated to a string ID
38 and value. */
40 typedef struct statistics_counter_s {
41 const char *id;
42 int val;
43 bool histogram_p;
44 unsigned HOST_WIDE_INT count;
45 unsigned HOST_WIDE_INT prev_dumped_count;
46 } statistics_counter_t;
48 /* Hashtable helpers. */
50 struct stats_counter_hasher
52 typedef statistics_counter_t *value_type;
53 typedef statistics_counter_t *compare_type;
54 static inline hashval_t hash (const statistics_counter_t *);
55 static inline bool equal (const statistics_counter_t *,
56 const statistics_counter_t *);
57 static inline void remove (statistics_counter_t *);
60 /* Hash a statistic counter by its string ID. */
62 inline hashval_t
63 stats_counter_hasher::hash (const statistics_counter_t *c)
65 return htab_hash_string (c->id) + c->val;
68 /* Compare two statistic counters by their string IDs. */
70 inline bool
71 stats_counter_hasher::equal (const statistics_counter_t *c1,
72 const statistics_counter_t *c2)
74 return c1->val == c2->val && strcmp (c1->id, c2->id) == 0;
77 /* Free a statistics entry. */
79 inline void
80 stats_counter_hasher::remove (statistics_counter_t *v)
82 free (CONST_CAST (char *, v->id));
83 free (v);
86 typedef hash_table<stats_counter_hasher> stats_counter_table_type;
88 /* Array of statistic hashes, indexed by pass id. */
89 static stats_counter_table_type **statistics_hashes;
90 static unsigned nr_statistics_hashes;
92 /* Return the current hashtable to be used for recording or printing
93 statistics. */
95 static stats_counter_table_type *
96 curr_statistics_hash (void)
98 unsigned idx;
100 gcc_assert (current_pass->static_pass_number >= 0);
101 idx = current_pass->static_pass_number;
103 if (idx < nr_statistics_hashes
104 && statistics_hashes[idx])
105 return statistics_hashes[idx];
107 if (idx >= nr_statistics_hashes)
109 statistics_hashes = XRESIZEVEC (stats_counter_table_type *,
110 statistics_hashes, idx+1);
111 memset (statistics_hashes + nr_statistics_hashes, 0,
112 (idx + 1 - nr_statistics_hashes)
113 * sizeof (stats_counter_table_type *));
114 nr_statistics_hashes = idx + 1;
117 statistics_hashes[idx] = new stats_counter_table_type (15);
119 return statistics_hashes[idx];
122 /* Helper for statistics_fini_pass. Print the counter difference
123 since the last dump for the pass dump files. */
126 statistics_fini_pass_1 (statistics_counter_t **slot,
127 void *data ATTRIBUTE_UNUSED)
129 statistics_counter_t *counter = *slot;
130 unsigned HOST_WIDE_INT count = counter->count - counter->prev_dumped_count;
131 if (count == 0)
132 return 1;
133 if (counter->histogram_p)
134 fprintf (dump_file, "%s == %d: " HOST_WIDE_INT_PRINT_DEC "\n",
135 counter->id, counter->val, count);
136 else
137 fprintf (dump_file, "%s: " HOST_WIDE_INT_PRINT_DEC "\n",
138 counter->id, count);
139 counter->prev_dumped_count = counter->count;
140 return 1;
143 /* Helper for statistics_fini_pass. Print the counter difference
144 since the last dump for the statistics dump. */
147 statistics_fini_pass_2 (statistics_counter_t **slot,
148 void *data ATTRIBUTE_UNUSED)
150 statistics_counter_t *counter = *slot;
151 unsigned HOST_WIDE_INT count = counter->count - counter->prev_dumped_count;
152 if (count == 0)
153 return 1;
154 counter->prev_dumped_count = counter->count;
155 if (counter->histogram_p)
156 fprintf (statistics_dump_file,
157 "%d %s \"%s == %d\" \"%s\" " HOST_WIDE_INT_PRINT_DEC "\n",
158 current_pass->static_pass_number,
159 current_pass->name,
160 counter->id, counter->val,
161 current_function_name (),
162 count);
163 else
164 fprintf (statistics_dump_file,
165 "%d %s \"%s\" \"%s\" " HOST_WIDE_INT_PRINT_DEC "\n",
166 current_pass->static_pass_number,
167 current_pass->name,
168 counter->id,
169 current_function_name (),
170 count);
171 counter->prev_dumped_count = counter->count;
172 return 1;
175 /* Helper for statistics_fini_pass, reset the counters. */
178 statistics_fini_pass_3 (statistics_counter_t **slot,
179 void *data ATTRIBUTE_UNUSED)
181 statistics_counter_t *counter = *slot;
182 counter->prev_dumped_count = counter->count;
183 return 1;
186 /* Dump the current statistics incrementally. */
188 void
189 statistics_fini_pass (void)
191 if (current_pass->static_pass_number == -1)
192 return;
194 if (dump_file
195 && dump_flags & TDF_STATS)
197 fprintf (dump_file, "\n");
198 fprintf (dump_file, "Pass statistics of \"%s\": ", current_pass->name);
199 fprintf (dump_file, "----------------\n");
200 curr_statistics_hash ()
201 ->traverse_noresize <void *, statistics_fini_pass_1> (NULL);
202 fprintf (dump_file, "\n");
204 if (statistics_dump_file
205 && !(statistics_dump_flags & TDF_STATS
206 || statistics_dump_flags & TDF_DETAILS))
207 curr_statistics_hash ()
208 ->traverse_noresize <void *, statistics_fini_pass_2> (NULL);
209 curr_statistics_hash ()
210 ->traverse_noresize <void *, statistics_fini_pass_3> (NULL);
213 /* Helper for printing summary information. */
216 statistics_fini_1 (statistics_counter_t **slot, opt_pass *pass)
218 statistics_counter_t *counter = *slot;
219 if (counter->count == 0)
220 return 1;
221 if (counter->histogram_p)
222 fprintf (statistics_dump_file,
223 "%d %s \"%s == %d\" " HOST_WIDE_INT_PRINT_DEC "\n",
224 pass->static_pass_number,
225 pass->name,
226 counter->id, counter->val,
227 counter->count);
228 else
229 fprintf (statistics_dump_file,
230 "%d %s \"%s\" " HOST_WIDE_INT_PRINT_DEC "\n",
231 pass->static_pass_number,
232 pass->name,
233 counter->id,
234 counter->count);
235 return 1;
238 /* Finish the statistics and dump summary information. */
240 void
241 statistics_fini (void)
243 gcc::pass_manager *passes = g->get_passes ();
244 if (!statistics_dump_file)
245 return;
247 if (statistics_dump_flags & TDF_STATS)
249 unsigned i;
250 for (i = 0; i < nr_statistics_hashes; ++i)
251 if (statistics_hashes[i]
252 && passes->get_pass_for_id (i) != NULL)
253 statistics_hashes[i]
254 ->traverse_noresize <opt_pass *, statistics_fini_1>
255 (passes->get_pass_for_id (i));
258 dump_end (statistics_dump_nr, statistics_dump_file);
261 /* Register the statistics dump file. */
263 void
264 statistics_early_init (void)
266 gcc::dump_manager *dumps = g->get_dumps ();
267 statistics_dump_nr = dumps->dump_register (".statistics", "statistics",
268 "statistics", TDF_TREE,
269 OPTGROUP_NONE,
270 false);
273 /* Init the statistics. */
275 void
276 statistics_init (void)
278 gcc::dump_manager *dumps = g->get_dumps ();
279 statistics_dump_file = dump_begin (statistics_dump_nr, NULL);
280 statistics_dump_flags = dumps->get_dump_file_info (statistics_dump_nr)->pflags;
283 /* Lookup or add a statistics counter in the hashtable HASH with ID, VAL
284 and HISTOGRAM_P. */
286 static statistics_counter_t *
287 lookup_or_add_counter (stats_counter_table_type *hash, const char *id, int val,
288 bool histogram_p)
290 statistics_counter_t **counter;
291 statistics_counter_t c;
292 c.id = id;
293 c.val = val;
294 counter = hash->find_slot (&c, INSERT);
295 if (!*counter)
297 *counter = XNEW (struct statistics_counter_s);
298 (*counter)->id = xstrdup (id);
299 (*counter)->val = val;
300 (*counter)->histogram_p = histogram_p;
301 (*counter)->prev_dumped_count = 0;
302 (*counter)->count = 0;
304 return *counter;
307 /* Add statistics information about event ID in function FN.
308 This will increment the counter associated with ID by INCR.
309 It will also dump the event to the global statistics file if requested. */
311 void
312 statistics_counter_event (struct function *fn, const char *id, int incr)
314 statistics_counter_t *counter;
316 if ((!(dump_flags & TDF_STATS)
317 && !statistics_dump_file)
318 || incr == 0)
319 return;
321 if (current_pass->static_pass_number != -1)
323 counter = lookup_or_add_counter (curr_statistics_hash (), id, 0, false);
324 gcc_assert (!counter->histogram_p);
325 counter->count += incr;
328 if (!statistics_dump_file
329 || !(statistics_dump_flags & TDF_DETAILS))
330 return;
332 fprintf (statistics_dump_file,
333 "%d %s \"%s\" \"%s\" %d\n",
334 current_pass->static_pass_number,
335 current_pass->name,
337 function_name (fn),
338 incr);
341 /* Add statistics information about event ID in function FN with the
342 histogram value VAL.
343 It will dump the event to the global statistics file if requested. */
345 void
346 statistics_histogram_event (struct function *fn, const char *id, int val)
348 statistics_counter_t *counter;
350 if (!(dump_flags & TDF_STATS)
351 && !statistics_dump_file)
352 return;
354 counter = lookup_or_add_counter (curr_statistics_hash (), id, val, true);
355 gcc_assert (counter->histogram_p);
356 counter->count += 1;
358 if (!statistics_dump_file
359 || !(statistics_dump_flags & TDF_DETAILS))
360 return;
362 fprintf (statistics_dump_file,
363 "%d %s \"%s == %d\" \"%s\" 1\n",
364 current_pass->static_pass_number,
365 current_pass->name,
366 id, val,
367 function_name (fn));