* lib/ubsan-dg.exp (check_effective_target_fsanitize_undefined):
[official-gcc.git] / gcc / statistics.c
blob0ceb2e9649a684bd48bd2ebc3c170c7a59e3ae6c
1 /* Optimization statistics functions.
2 Copyright (C) 2008-2014 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 "hashtab.h"
29 #include "hash-set.h"
30 #include "vec.h"
31 #include "machmode.h"
32 #include "tm.h"
33 #include "hard-reg-set.h"
34 #include "input.h"
35 #include "function.h"
36 #include "context.h"
37 #include "pass_manager.h"
39 static int statistics_dump_nr;
40 static int statistics_dump_flags;
41 static FILE *statistics_dump_file;
43 /* Statistics entry. A integer counter associated to a string ID
44 and value. */
46 typedef struct statistics_counter_s {
47 const char *id;
48 int val;
49 bool histogram_p;
50 unsigned HOST_WIDE_INT count;
51 unsigned HOST_WIDE_INT prev_dumped_count;
52 } statistics_counter_t;
54 /* Hashtable helpers. */
56 struct stats_counter_hasher
58 typedef statistics_counter_t value_type;
59 typedef statistics_counter_t compare_type;
60 static inline hashval_t hash (const value_type *);
61 static inline bool equal (const value_type *, const compare_type *);
62 static inline void remove (value_type *);
65 /* Hash a statistic counter by its string ID. */
67 inline hashval_t
68 stats_counter_hasher::hash (const value_type *c)
70 return htab_hash_string (c->id) + c->val;
73 /* Compare two statistic counters by their string IDs. */
75 inline bool
76 stats_counter_hasher::equal (const value_type *c1, const compare_type *c2)
78 return c1->val == c2->val && strcmp (c1->id, c2->id) == 0;
81 /* Free a statistics entry. */
83 inline void
84 stats_counter_hasher::remove (value_type *v)
86 free (CONST_CAST (char *, v->id));
87 free (v);
90 typedef hash_table<stats_counter_hasher> stats_counter_table_type;
92 /* Array of statistic hashes, indexed by pass id. */
93 static stats_counter_table_type **statistics_hashes;
94 static unsigned nr_statistics_hashes;
96 /* Return the current hashtable to be used for recording or printing
97 statistics. */
99 static stats_counter_table_type *
100 curr_statistics_hash (void)
102 unsigned idx;
104 gcc_assert (current_pass->static_pass_number >= 0);
105 idx = current_pass->static_pass_number;
107 if (idx < nr_statistics_hashes
108 && statistics_hashes[idx])
109 return statistics_hashes[idx];
111 if (idx >= nr_statistics_hashes)
113 statistics_hashes = XRESIZEVEC (stats_counter_table_type *,
114 statistics_hashes, idx+1);
115 memset (statistics_hashes + nr_statistics_hashes, 0,
116 (idx + 1 - nr_statistics_hashes)
117 * sizeof (stats_counter_table_type *));
118 nr_statistics_hashes = idx + 1;
121 statistics_hashes[idx] = new stats_counter_table_type (15);
123 return statistics_hashes[idx];
126 /* Helper for statistics_fini_pass. Print the counter difference
127 since the last dump for the pass dump files. */
130 statistics_fini_pass_1 (statistics_counter_t **slot,
131 void *data ATTRIBUTE_UNUSED)
133 statistics_counter_t *counter = *slot;
134 unsigned HOST_WIDE_INT count = counter->count - counter->prev_dumped_count;
135 if (count == 0)
136 return 1;
137 if (counter->histogram_p)
138 fprintf (dump_file, "%s == %d: " HOST_WIDE_INT_PRINT_DEC "\n",
139 counter->id, counter->val, count);
140 else
141 fprintf (dump_file, "%s: " HOST_WIDE_INT_PRINT_DEC "\n",
142 counter->id, count);
143 counter->prev_dumped_count = counter->count;
144 return 1;
147 /* Helper for statistics_fini_pass. Print the counter difference
148 since the last dump for the statistics dump. */
151 statistics_fini_pass_2 (statistics_counter_t **slot,
152 void *data ATTRIBUTE_UNUSED)
154 statistics_counter_t *counter = *slot;
155 unsigned HOST_WIDE_INT count = counter->count - counter->prev_dumped_count;
156 if (count == 0)
157 return 1;
158 counter->prev_dumped_count = counter->count;
159 if (counter->histogram_p)
160 fprintf (statistics_dump_file,
161 "%d %s \"%s == %d\" \"%s\" " HOST_WIDE_INT_PRINT_DEC "\n",
162 current_pass->static_pass_number,
163 current_pass->name,
164 counter->id, counter->val,
165 current_function_name (),
166 count);
167 else
168 fprintf (statistics_dump_file,
169 "%d %s \"%s\" \"%s\" " HOST_WIDE_INT_PRINT_DEC "\n",
170 current_pass->static_pass_number,
171 current_pass->name,
172 counter->id,
173 current_function_name (),
174 count);
175 counter->prev_dumped_count = counter->count;
176 return 1;
179 /* Helper for statistics_fini_pass, reset the counters. */
182 statistics_fini_pass_3 (statistics_counter_t **slot,
183 void *data ATTRIBUTE_UNUSED)
185 statistics_counter_t *counter = *slot;
186 counter->prev_dumped_count = counter->count;
187 return 1;
190 /* Dump the current statistics incrementally. */
192 void
193 statistics_fini_pass (void)
195 if (current_pass->static_pass_number == -1)
196 return;
198 if (dump_file
199 && dump_flags & TDF_STATS)
201 fprintf (dump_file, "\n");
202 fprintf (dump_file, "Pass statistics:\n");
203 fprintf (dump_file, "----------------\n");
204 curr_statistics_hash ()
205 ->traverse_noresize <void *, statistics_fini_pass_1> (NULL);
206 fprintf (dump_file, "\n");
208 if (statistics_dump_file
209 && !(statistics_dump_flags & TDF_STATS
210 || statistics_dump_flags & TDF_DETAILS))
211 curr_statistics_hash ()
212 ->traverse_noresize <void *, statistics_fini_pass_2> (NULL);
213 curr_statistics_hash ()
214 ->traverse_noresize <void *, statistics_fini_pass_3> (NULL);
217 /* Helper for printing summary information. */
220 statistics_fini_1 (statistics_counter_t **slot, opt_pass *pass)
222 statistics_counter_t *counter = *slot;
223 if (counter->count == 0)
224 return 1;
225 if (counter->histogram_p)
226 fprintf (statistics_dump_file,
227 "%d %s \"%s == %d\" " HOST_WIDE_INT_PRINT_DEC "\n",
228 pass->static_pass_number,
229 pass->name,
230 counter->id, counter->val,
231 counter->count);
232 else
233 fprintf (statistics_dump_file,
234 "%d %s \"%s\" " HOST_WIDE_INT_PRINT_DEC "\n",
235 pass->static_pass_number,
236 pass->name,
237 counter->id,
238 counter->count);
239 return 1;
242 /* Finish the statistics and dump summary information. */
244 void
245 statistics_fini (void)
247 gcc::pass_manager *passes = g->get_passes ();
248 if (!statistics_dump_file)
249 return;
251 if (statistics_dump_flags & TDF_STATS)
253 unsigned i;
254 for (i = 0; i < nr_statistics_hashes; ++i)
255 if (statistics_hashes[i]
256 && passes->get_pass_for_id (i) != NULL)
257 statistics_hashes[i]
258 ->traverse_noresize <opt_pass *, statistics_fini_1>
259 (passes->get_pass_for_id (i));
262 dump_end (statistics_dump_nr, statistics_dump_file);
265 /* Register the statistics dump file. */
267 void
268 statistics_early_init (void)
270 gcc::dump_manager *dumps = g->get_dumps ();
271 statistics_dump_nr = dumps->dump_register (".statistics", "statistics",
272 "statistics", TDF_TREE,
273 OPTGROUP_NONE,
274 false);
277 /* Init the statistics. */
279 void
280 statistics_init (void)
282 gcc::dump_manager *dumps = g->get_dumps ();
283 statistics_dump_file = dump_begin (statistics_dump_nr, NULL);
284 statistics_dump_flags = dumps->get_dump_file_info (statistics_dump_nr)->pflags;
287 /* Lookup or add a statistics counter in the hashtable HASH with ID, VAL
288 and HISTOGRAM_P. */
290 static statistics_counter_t *
291 lookup_or_add_counter (stats_counter_table_type *hash, const char *id, int val,
292 bool histogram_p)
294 statistics_counter_t **counter;
295 statistics_counter_t c;
296 c.id = id;
297 c.val = val;
298 counter = hash->find_slot (&c, INSERT);
299 if (!*counter)
301 *counter = XNEW (struct statistics_counter_s);
302 (*counter)->id = xstrdup (id);
303 (*counter)->val = val;
304 (*counter)->histogram_p = histogram_p;
305 (*counter)->prev_dumped_count = 0;
306 (*counter)->count = 0;
308 return *counter;
311 /* Add statistics information about event ID in function FN.
312 This will increment the counter associated with ID by INCR.
313 It will also dump the event to the global statistics file if requested. */
315 void
316 statistics_counter_event (struct function *fn, const char *id, int incr)
318 statistics_counter_t *counter;
320 if ((!(dump_flags & TDF_STATS)
321 && !statistics_dump_file)
322 || incr == 0)
323 return;
325 if (current_pass->static_pass_number != -1)
327 counter = lookup_or_add_counter (curr_statistics_hash (), id, 0, false);
328 gcc_assert (!counter->histogram_p);
329 counter->count += incr;
332 if (!statistics_dump_file
333 || !(statistics_dump_flags & TDF_DETAILS))
334 return;
336 fprintf (statistics_dump_file,
337 "%d %s \"%s\" \"%s\" %d\n",
338 current_pass->static_pass_number,
339 current_pass->name,
341 function_name (fn),
342 incr);
345 /* Add statistics information about event ID in function FN with the
346 histogram value VAL.
347 It will dump the event to the global statistics file if requested. */
349 void
350 statistics_histogram_event (struct function *fn, const char *id, int val)
352 statistics_counter_t *counter;
354 if (!(dump_flags & TDF_STATS)
355 && !statistics_dump_file)
356 return;
358 counter = lookup_or_add_counter (curr_statistics_hash (), id, val, true);
359 gcc_assert (counter->histogram_p);
360 counter->count += 1;
362 if (!statistics_dump_file
363 || !(statistics_dump_flags & TDF_DETAILS))
364 return;
366 fprintf (statistics_dump_file,
367 "%d %s \"%s == %d\" \"%s\" 1\n",
368 current_pass->static_pass_number,
369 current_pass->name,
370 id, val,
371 function_name (fn));