decl.c (value_annotation_hasher::handle_cache_entry): Delete.
[gcc.git] / gcc / statistics.c
1 /* Optimization statistics functions.
2 Copyright (C) 2008-2015 Free Software Foundation, Inc.
3 Contributed by Richard Guenther <rguenther@suse.de>
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
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"
31
32 static int statistics_dump_nr;
33 static int statistics_dump_flags;
34 static FILE *statistics_dump_file;
35
36 /* Statistics entry. A integer counter associated to a string ID
37 and value. */
38
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;
46
47 /* Hashtable helpers. */
48
49 struct stats_counter_hasher
50 {
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 *);
57 };
58
59 /* Hash a statistic counter by its string ID. */
60
61 inline hashval_t
62 stats_counter_hasher::hash (const statistics_counter_t *c)
63 {
64 return htab_hash_string (c->id) + c->val;
65 }
66
67 /* Compare two statistic counters by their string IDs. */
68
69 inline bool
70 stats_counter_hasher::equal (const statistics_counter_t *c1,
71 const statistics_counter_t *c2)
72 {
73 return c1->val == c2->val && strcmp (c1->id, c2->id) == 0;
74 }
75
76 /* Free a statistics entry. */
77
78 inline void
79 stats_counter_hasher::remove (statistics_counter_t *v)
80 {
81 free (CONST_CAST (char *, v->id));
82 free (v);
83 }
84
85 typedef hash_table<stats_counter_hasher> stats_counter_table_type;
86
87 /* Array of statistic hashes, indexed by pass id. */
88 static stats_counter_table_type **statistics_hashes;
89 static unsigned nr_statistics_hashes;
90
91 /* Return the current hashtable to be used for recording or printing
92 statistics. */
93
94 static stats_counter_table_type *
95 curr_statistics_hash (void)
96 {
97 unsigned idx;
98
99 gcc_assert (current_pass->static_pass_number >= 0);
100 idx = current_pass->static_pass_number;
101
102 if (idx < nr_statistics_hashes
103 && statistics_hashes[idx])
104 return statistics_hashes[idx];
105
106 if (idx >= nr_statistics_hashes)
107 {
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;
114 }
115
116 statistics_hashes[idx] = new stats_counter_table_type (15);
117
118 return statistics_hashes[idx];
119 }
120
121 /* Helper for statistics_fini_pass. Print the counter difference
122 since the last dump for the pass dump files. */
123
124 int
125 statistics_fini_pass_1 (statistics_counter_t **slot,
126 void *data ATTRIBUTE_UNUSED)
127 {
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;
140 }
141
142 /* Helper for statistics_fini_pass. Print the counter difference
143 since the last dump for the statistics dump. */
144
145 int
146 statistics_fini_pass_2 (statistics_counter_t **slot,
147 void *data ATTRIBUTE_UNUSED)
148 {
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;
172 }
173
174 /* Helper for statistics_fini_pass, reset the counters. */
175
176 int
177 statistics_fini_pass_3 (statistics_counter_t **slot,
178 void *data ATTRIBUTE_UNUSED)
179 {
180 statistics_counter_t *counter = *slot;
181 counter->prev_dumped_count = counter->count;
182 return 1;
183 }
184
185 /* Dump the current statistics incrementally. */
186
187 void
188 statistics_fini_pass (void)
189 {
190 if (current_pass->static_pass_number == -1)
191 return;
192
193 if (dump_file
194 && dump_flags & TDF_STATS)
195 {
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");
202 }
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);
210 }
211
212 /* Helper for printing summary information. */
213
214 int
215 statistics_fini_1 (statistics_counter_t **slot, opt_pass *pass)
216 {
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;
235 }
236
237 /* Finish the statistics and dump summary information. */
238
239 void
240 statistics_fini (void)
241 {
242 gcc::pass_manager *passes = g->get_passes ();
243 if (!statistics_dump_file)
244 return;
245
246 if (statistics_dump_flags & TDF_STATS)
247 {
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));
255 }
256
257 dump_end (statistics_dump_nr, statistics_dump_file);
258 }
259
260 /* Register the statistics dump file. */
261
262 void
263 statistics_early_init (void)
264 {
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);
270 }
271
272 /* Init the statistics. */
273
274 void
275 statistics_init (void)
276 {
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;
280 }
281
282 /* Lookup or add a statistics counter in the hashtable HASH with ID, VAL
283 and HISTOGRAM_P. */
284
285 static statistics_counter_t *
286 lookup_or_add_counter (stats_counter_table_type *hash, const char *id, int val,
287 bool histogram_p)
288 {
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)
295 {
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;
302 }
303 return *counter;
304 }
305
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. */
309
310 void
311 statistics_counter_event (struct function *fn, const char *id, int incr)
312 {
313 statistics_counter_t *counter;
314
315 if ((!(dump_flags & TDF_STATS)
316 && !statistics_dump_file)
317 || incr == 0)
318 return;
319
320 if (current_pass->static_pass_number != -1)
321 {
322 counter = lookup_or_add_counter (curr_statistics_hash (), id, 0, false);
323 gcc_assert (!counter->histogram_p);
324 counter->count += incr;
325 }
326
327 if (!statistics_dump_file
328 || !(statistics_dump_flags & TDF_DETAILS))
329 return;
330
331 fprintf (statistics_dump_file,
332 "%d %s \"%s\" \"%s\" %d\n",
333 current_pass->static_pass_number,
334 current_pass->name,
335 id,
336 function_name (fn),
337 incr);
338 }
339
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. */
343
344 void
345 statistics_histogram_event (struct function *fn, const char *id, int val)
346 {
347 statistics_counter_t *counter;
348
349 if (!(dump_flags & TDF_STATS)
350 && !statistics_dump_file)
351 return;
352
353 counter = lookup_or_add_counter (curr_statistics_hash (), id, val, true);
354 gcc_assert (counter->histogram_p);
355 counter->count += 1;
356
357 if (!statistics_dump_file
358 || !(statistics_dump_flags & TDF_DETAILS))
359 return;
360
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));
367 }