Daily bump.
[official-gcc.git] / gcc / statistics.c
blob3077cc0c36ce5e7dd8751259054f08be20ad6bdc
1 /* Optimization statistics functions.
2 Copyright (C) 2008-2013 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 "statistics.h"
27 #include "hash-table.h"
28 #include "function.h"
30 static int statistics_dump_nr;
31 static int statistics_dump_flags;
32 static FILE *statistics_dump_file;
34 /* Statistics entry. A integer counter associated to a string ID
35 and value. */
37 typedef struct statistics_counter_s {
38 const char *id;
39 int val;
40 bool histogram_p;
41 unsigned HOST_WIDE_INT count;
42 unsigned HOST_WIDE_INT prev_dumped_count;
43 } statistics_counter_t;
45 /* Hashtable helpers. */
47 struct stats_counter_hasher
49 typedef statistics_counter_t value_type;
50 typedef statistics_counter_t compare_type;
51 static inline hashval_t hash (const value_type *);
52 static inline bool equal (const value_type *, const compare_type *);
53 static inline void remove (value_type *);
56 /* Hash a statistic counter by its string ID. */
58 inline hashval_t
59 stats_counter_hasher::hash (const value_type *c)
61 return htab_hash_string (c->id) + c->val;
64 /* Compare two statistic counters by their string IDs. */
66 inline bool
67 stats_counter_hasher::equal (const value_type *c1, const compare_type *c2)
69 return c1->val == c2->val && strcmp (c1->id, c2->id) == 0;
72 /* Free a statistics entry. */
74 inline void
75 stats_counter_hasher::remove (value_type *v)
77 free (CONST_CAST(char *, v->id));
78 free (v);
81 typedef hash_table <stats_counter_hasher> stats_counter_table_type;
83 /* Array of statistic hashes, indexed by pass id. */
84 static stats_counter_table_type *statistics_hashes;
85 static unsigned nr_statistics_hashes;
87 /* Return the current hashtable to be used for recording or printing
88 statistics. */
90 static stats_counter_table_type
91 curr_statistics_hash (void)
93 unsigned idx;
95 gcc_assert (current_pass->static_pass_number >= 0);
96 idx = current_pass->static_pass_number;
98 if (idx < nr_statistics_hashes
99 && statistics_hashes[idx].is_created ())
100 return statistics_hashes[idx];
102 if (idx >= nr_statistics_hashes)
104 statistics_hashes = XRESIZEVEC (stats_counter_table_type,
105 statistics_hashes, idx+1);
106 memset (statistics_hashes + nr_statistics_hashes, 0,
107 (idx + 1 - nr_statistics_hashes)
108 * sizeof (stats_counter_table_type));
109 nr_statistics_hashes = idx + 1;
112 statistics_hashes[idx].create (15);
114 return statistics_hashes[idx];
117 /* Helper for statistics_fini_pass. Print the counter difference
118 since the last dump for the pass dump files. */
121 statistics_fini_pass_1 (statistics_counter_t **slot,
122 void *data ATTRIBUTE_UNUSED)
124 statistics_counter_t *counter = *slot;
125 unsigned HOST_WIDE_INT count = counter->count - counter->prev_dumped_count;
126 if (count == 0)
127 return 1;
128 if (counter->histogram_p)
129 fprintf (dump_file, "%s == %d: " HOST_WIDE_INT_PRINT_DEC "\n",
130 counter->id, counter->val, count);
131 else
132 fprintf (dump_file, "%s: " HOST_WIDE_INT_PRINT_DEC "\n",
133 counter->id, count);
134 counter->prev_dumped_count = counter->count;
135 return 1;
138 /* Helper for statistics_fini_pass. Print the counter difference
139 since the last dump for the statistics dump. */
142 statistics_fini_pass_2 (statistics_counter_t **slot,
143 void *data ATTRIBUTE_UNUSED)
145 statistics_counter_t *counter = *slot;
146 unsigned HOST_WIDE_INT count = counter->count - counter->prev_dumped_count;
147 if (count == 0)
148 return 1;
149 counter->prev_dumped_count = counter->count;
150 if (counter->histogram_p)
151 fprintf (statistics_dump_file,
152 "%d %s \"%s == %d\" \"%s\" " HOST_WIDE_INT_PRINT_DEC "\n",
153 current_pass->static_pass_number,
154 current_pass->name,
155 counter->id, counter->val,
156 current_function_name (),
157 count);
158 else
159 fprintf (statistics_dump_file,
160 "%d %s \"%s\" \"%s\" " HOST_WIDE_INT_PRINT_DEC "\n",
161 current_pass->static_pass_number,
162 current_pass->name,
163 counter->id,
164 current_function_name (),
165 count);
166 counter->prev_dumped_count = counter->count;
167 return 1;
170 /* Helper for statistics_fini_pass, reset the counters. */
173 statistics_fini_pass_3 (statistics_counter_t **slot,
174 void *data ATTRIBUTE_UNUSED)
176 statistics_counter_t *counter = *slot;
177 counter->prev_dumped_count = counter->count;
178 return 1;
181 /* Dump the current statistics incrementally. */
183 void
184 statistics_fini_pass (void)
186 if (current_pass->static_pass_number == -1)
187 return;
189 if (dump_file
190 && dump_flags & TDF_STATS)
192 fprintf (dump_file, "\n");
193 fprintf (dump_file, "Pass statistics:\n");
194 fprintf (dump_file, "----------------\n");
195 curr_statistics_hash ()
196 .traverse_noresize <void *, statistics_fini_pass_1> (NULL);
197 fprintf (dump_file, "\n");
199 if (statistics_dump_file
200 && !(statistics_dump_flags & TDF_STATS
201 || statistics_dump_flags & TDF_DETAILS))
202 curr_statistics_hash ()
203 .traverse_noresize <void *, statistics_fini_pass_2> (NULL);
204 curr_statistics_hash ()
205 .traverse_noresize <void *, statistics_fini_pass_3> (NULL);
208 /* Helper for printing summary information. */
211 statistics_fini_1 (statistics_counter_t **slot, opt_pass *pass)
213 statistics_counter_t *counter = *slot;
214 if (counter->count == 0)
215 return 1;
216 if (counter->histogram_p)
217 fprintf (statistics_dump_file,
218 "%d %s \"%s == %d\" " HOST_WIDE_INT_PRINT_DEC "\n",
219 pass->static_pass_number,
220 pass->name,
221 counter->id, counter->val,
222 counter->count);
223 else
224 fprintf (statistics_dump_file,
225 "%d %s \"%s\" " HOST_WIDE_INT_PRINT_DEC "\n",
226 pass->static_pass_number,
227 pass->name,
228 counter->id,
229 counter->count);
230 return 1;
233 /* Finish the statistics and dump summary information. */
235 void
236 statistics_fini (void)
238 if (!statistics_dump_file)
239 return;
241 if (statistics_dump_flags & TDF_STATS)
243 unsigned i;
244 for (i = 0; i < nr_statistics_hashes; ++i)
245 if (statistics_hashes[i].is_created ()
246 && get_pass_for_id (i) != NULL)
247 statistics_hashes[i]
248 .traverse_noresize <opt_pass *, statistics_fini_1>
249 (get_pass_for_id (i));
252 dump_end (statistics_dump_nr, statistics_dump_file);
255 /* Register the statistics dump file. */
257 void
258 statistics_early_init (void)
260 statistics_dump_nr = dump_register (".statistics", "statistics",
261 "statistics", TDF_TREE, OPTGROUP_NONE);
264 /* Init the statistics. */
266 void
267 statistics_init (void)
269 statistics_dump_file = dump_begin (statistics_dump_nr, NULL);
270 statistics_dump_flags = get_dump_file_info (statistics_dump_nr)->pflags;
273 /* Lookup or add a statistics counter in the hashtable HASH with ID, VAL
274 and HISTOGRAM_P. */
276 static statistics_counter_t *
277 lookup_or_add_counter (stats_counter_table_type hash, const char *id, int val,
278 bool histogram_p)
280 statistics_counter_t **counter;
281 statistics_counter_t c;
282 c.id = id;
283 c.val = val;
284 counter = hash.find_slot (&c, INSERT);
285 if (!*counter)
287 *counter = XNEW (struct statistics_counter_s);
288 (*counter)->id = xstrdup (id);
289 (*counter)->val = val;
290 (*counter)->histogram_p = histogram_p;
291 (*counter)->prev_dumped_count = 0;
292 (*counter)->count = 0;
294 return *counter;
297 /* Add statistics information about event ID in function FN.
298 This will increment the counter associated with ID by INCR.
299 It will also dump the event to the global statistics file if requested. */
301 void
302 statistics_counter_event (struct function *fn, const char *id, int incr)
304 statistics_counter_t *counter;
306 if ((!(dump_flags & TDF_STATS)
307 && !statistics_dump_file)
308 || incr == 0)
309 return;
311 if (current_pass->static_pass_number != -1)
313 counter = lookup_or_add_counter (curr_statistics_hash (), id, 0, false);
314 gcc_assert (!counter->histogram_p);
315 counter->count += incr;
318 if (!statistics_dump_file
319 || !(statistics_dump_flags & TDF_DETAILS))
320 return;
322 fprintf (statistics_dump_file,
323 "%d %s \"%s\" \"%s\" %d\n",
324 current_pass->static_pass_number,
325 current_pass->name,
327 function_name (fn),
328 incr);
331 /* Add statistics information about event ID in function FN with the
332 histogram value VAL.
333 It will dump the event to the global statistics file if requested. */
335 void
336 statistics_histogram_event (struct function *fn, const char *id, int val)
338 statistics_counter_t *counter;
340 if (!(dump_flags & TDF_STATS)
341 && !statistics_dump_file)
342 return;
344 counter = lookup_or_add_counter (curr_statistics_hash (), id, val, true);
345 gcc_assert (counter->histogram_p);
346 counter->count += 1;
348 if (!statistics_dump_file
349 || !(statistics_dump_flags & TDF_DETAILS))
350 return;
352 fprintf (statistics_dump_file,
353 "%d %s \"%s == %d\" \"%s\" 1\n",
354 current_pass->static_pass_number,
355 current_pass->name,
356 id, val,
357 function_name (fn));