* config/sh/sh.md (prologue, epilogue): Use braced strings.
[official-gcc.git] / gcc / statistics.c
blob55ec981f3bb504294fa08d44fcc7c8abe93ec20c
1 /* Optimization statistics functions.
2 Copyright (C) 2008, 2010
3 Free Software Foundation, Inc.
4 Contributed by Richard Guenther <rguenther@suse.de>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tree-pass.h"
26 #include "tree-dump.h"
27 #include "statistics.h"
28 #include "hashtab.h"
29 #include "function.h"
31 static int statistics_dump_nr;
32 static int statistics_dump_flags;
33 static FILE *statistics_dump_file;
35 /* Statistics entry. A integer counter associated to a string ID
36 and value. */
38 typedef struct statistics_counter_s {
39 const char *id;
40 int val;
41 bool histogram_p;
42 unsigned HOST_WIDE_INT count;
43 unsigned HOST_WIDE_INT prev_dumped_count;
44 } statistics_counter_t;
46 /* Array of statistic hashes, indexed by pass id. */
47 static htab_t *statistics_hashes;
48 static unsigned nr_statistics_hashes;
50 /* Hash a statistic counter by its string ID. */
52 static hashval_t
53 hash_statistics_hash (const void *p)
55 const statistics_counter_t *const c = (const statistics_counter_t *)p;
56 return htab_hash_string (c->id) + c->val;
59 /* Compare two statistic counters by their string IDs. */
61 static int
62 hash_statistics_eq (const void *p, const void *q)
64 const statistics_counter_t *const c1 = (const statistics_counter_t *)p;
65 const statistics_counter_t *const c2 = (const statistics_counter_t *)q;
66 return c1->val == c2->val && strcmp (c1->id, c2->id) == 0;
69 /* Free a statistics entry. */
71 static void
72 hash_statistics_free (void *p)
74 free (CONST_CAST(char *, ((statistics_counter_t *)p)->id));
75 free (p);
78 /* Return the current hashtable to be used for recording or printing
79 statistics. */
81 static htab_t
82 curr_statistics_hash (void)
84 unsigned idx;
86 gcc_assert (current_pass->static_pass_number >= 0);
87 idx = current_pass->static_pass_number;
89 if (idx < nr_statistics_hashes
90 && statistics_hashes[idx] != NULL)
91 return statistics_hashes[idx];
93 if (idx >= nr_statistics_hashes)
95 statistics_hashes = XRESIZEVEC (struct htab *, statistics_hashes, idx+1);
96 memset (statistics_hashes + nr_statistics_hashes, 0,
97 (idx + 1 - nr_statistics_hashes) * sizeof (htab_t));
98 nr_statistics_hashes = idx + 1;
101 statistics_hashes[idx] = htab_create (15, hash_statistics_hash,
102 hash_statistics_eq,
103 hash_statistics_free);
105 return statistics_hashes[idx];
108 /* Helper for statistics_fini_pass. Print the counter difference
109 since the last dump for the pass dump files. */
111 static int
112 statistics_fini_pass_1 (void **slot, void *data ATTRIBUTE_UNUSED)
114 statistics_counter_t *counter = (statistics_counter_t *)*slot;
115 unsigned HOST_WIDE_INT count = counter->count - counter->prev_dumped_count;
116 if (count == 0)
117 return 1;
118 if (counter->histogram_p)
119 fprintf (dump_file, "%s == %d: " HOST_WIDE_INT_PRINT_DEC "\n",
120 counter->id, counter->val, count);
121 else
122 fprintf (dump_file, "%s: " HOST_WIDE_INT_PRINT_DEC "\n",
123 counter->id, count);
124 counter->prev_dumped_count = counter->count;
125 return 1;
128 /* Helper for statistics_fini_pass. Print the counter difference
129 since the last dump for the statistics dump. */
131 static int
132 statistics_fini_pass_2 (void **slot, void *data ATTRIBUTE_UNUSED)
134 statistics_counter_t *counter = (statistics_counter_t *)*slot;
135 unsigned HOST_WIDE_INT count = counter->count - counter->prev_dumped_count;
136 if (count == 0)
137 return 1;
138 counter->prev_dumped_count = counter->count;
139 if (counter->histogram_p)
140 fprintf (statistics_dump_file,
141 "%d %s \"%s == %d\" \"%s\" " HOST_WIDE_INT_PRINT_DEC "\n",
142 current_pass->static_pass_number,
143 current_pass->name,
144 counter->id, counter->val,
145 current_function_name (),
146 count);
147 else
148 fprintf (statistics_dump_file,
149 "%d %s \"%s\" \"%s\" " HOST_WIDE_INT_PRINT_DEC "\n",
150 current_pass->static_pass_number,
151 current_pass->name,
152 counter->id,
153 current_function_name (),
154 count);
155 counter->prev_dumped_count = counter->count;
156 return 1;
159 /* Helper for statistics_fini_pass, reset the counters. */
161 static int
162 statistics_fini_pass_3 (void **slot, void *data ATTRIBUTE_UNUSED)
164 statistics_counter_t *counter = (statistics_counter_t *)*slot;
165 counter->prev_dumped_count = counter->count;
166 return 1;
169 /* Dump the current statistics incrementally. */
171 void
172 statistics_fini_pass (void)
174 if (current_pass->static_pass_number == -1)
175 return;
177 if (dump_file
178 && dump_flags & TDF_STATS)
180 fprintf (dump_file, "\n");
181 fprintf (dump_file, "Pass statistics:\n");
182 fprintf (dump_file, "----------------\n");
183 htab_traverse_noresize (curr_statistics_hash (),
184 statistics_fini_pass_1, NULL);
185 fprintf (dump_file, "\n");
187 if (statistics_dump_file
188 && !(statistics_dump_flags & TDF_STATS
189 || statistics_dump_flags & TDF_DETAILS))
190 htab_traverse_noresize (curr_statistics_hash (),
191 statistics_fini_pass_2, NULL);
192 htab_traverse_noresize (curr_statistics_hash (),
193 statistics_fini_pass_3, NULL);
196 /* Helper for printing summary information. */
198 static int
199 statistics_fini_1 (void **slot, void *data)
201 struct opt_pass *pass = (struct opt_pass *)data;
202 statistics_counter_t *counter = (statistics_counter_t *)*slot;
203 if (counter->count == 0)
204 return 1;
205 if (counter->histogram_p)
206 fprintf (statistics_dump_file,
207 "%d %s \"%s == %d\" " HOST_WIDE_INT_PRINT_DEC "\n",
208 pass->static_pass_number,
209 pass->name,
210 counter->id, counter->val,
211 counter->count);
212 else
213 fprintf (statistics_dump_file,
214 "%d %s \"%s\" " HOST_WIDE_INT_PRINT_DEC "\n",
215 pass->static_pass_number,
216 pass->name,
217 counter->id,
218 counter->count);
219 return 1;
222 /* Finish the statistics and dump summary information. */
224 void
225 statistics_fini (void)
227 if (!statistics_dump_file)
228 return;
230 if (statistics_dump_flags & TDF_STATS)
232 unsigned i;
233 for (i = 0; i < nr_statistics_hashes; ++i)
234 if (statistics_hashes[i] != NULL
235 && get_pass_for_id (i) != NULL)
236 htab_traverse_noresize (statistics_hashes[i],
237 statistics_fini_1, get_pass_for_id (i));
240 dump_end (statistics_dump_nr, statistics_dump_file);
243 /* Register the statistics dump file. */
245 void
246 statistics_early_init (void)
248 statistics_dump_nr = dump_register (".statistics", "statistics",
249 "statistics", TDF_TREE);
252 /* Init the statistics. */
254 void
255 statistics_init (void)
257 statistics_dump_file = dump_begin (statistics_dump_nr, NULL);
258 statistics_dump_flags = get_dump_file_info (statistics_dump_nr)->flags;
261 /* Lookup or add a statistics counter in the hashtable HASH with ID, VAL
262 and HISTOGRAM_P. */
264 static statistics_counter_t *
265 lookup_or_add_counter (htab_t hash, const char *id, int val,
266 bool histogram_p)
268 statistics_counter_t **counter;
269 statistics_counter_t c;
270 c.id = id;
271 c.val = val;
272 counter = (statistics_counter_t **) htab_find_slot (hash, &c, INSERT);
273 if (!*counter)
275 *counter = XNEW (struct statistics_counter_s);
276 (*counter)->id = xstrdup (id);
277 (*counter)->val = val;
278 (*counter)->histogram_p = histogram_p;
279 (*counter)->prev_dumped_count = 0;
280 (*counter)->count = 0;
282 return *counter;
285 /* Add statistics information about event ID in function FN.
286 This will increment the counter associated with ID by INCR.
287 It will also dump the event to the global statistics file if requested. */
289 void
290 statistics_counter_event (struct function *fn, const char *id, int incr)
292 statistics_counter_t *counter;
294 if ((!(dump_flags & TDF_STATS)
295 && !statistics_dump_file)
296 || incr == 0)
297 return;
299 if (current_pass->static_pass_number != -1)
301 counter = lookup_or_add_counter (curr_statistics_hash (), id, 0, false);
302 gcc_assert (!counter->histogram_p);
303 counter->count += incr;
306 if (!statistics_dump_file
307 || !(statistics_dump_flags & TDF_DETAILS))
308 return;
310 fprintf (statistics_dump_file,
311 "%d %s \"%s\" \"%s\" %d\n",
312 current_pass->static_pass_number,
313 current_pass->name,
315 function_name (fn),
316 incr);
319 /* Add statistics information about event ID in function FN with the
320 histogram value VAL.
321 It will dump the event to the global statistics file if requested. */
323 void
324 statistics_histogram_event (struct function *fn, const char *id, int val)
326 statistics_counter_t *counter;
328 if (!(dump_flags & TDF_STATS)
329 && !statistics_dump_file)
330 return;
332 counter = lookup_or_add_counter (curr_statistics_hash (), id, val, true);
333 gcc_assert (counter->histogram_p);
334 counter->count += 1;
336 if (!statistics_dump_file
337 || !(statistics_dump_flags & TDF_DETAILS))
338 return;
340 fprintf (statistics_dump_file,
341 "%d %s \"%s == %d\" \"%s\" 1\n",
342 current_pass->static_pass_number,
343 current_pass->name,
344 id, val,
345 function_name (fn));