2014-11-18 Christophe Lyon <christophe.lyon@linaro.org>
[official-gcc.git] / gcc / coverage.c
blobbeaa02f6a30174a39f922aeefc0cb7c5fefbdbe1
1 /* Read and write coverage files, and associated functionality.
2 Copyright (C) 1990-2014 Free Software Foundation, Inc.
3 Contributed by James E. Wilson, UC Berkeley/Cygnus Support;
4 based on some ideas from Dain Samples of UC Berkeley.
5 Further mangling by Bob Manson, Cygnus Support.
6 Further mangled by Nathan Sidwell, CodeSourcery
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
25 #define GCOV_LINKAGE
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "rtl.h"
32 #include "tree.h"
33 #include "stringpool.h"
34 #include "stor-layout.h"
35 #include "flags.h"
36 #include "output.h"
37 #include "regs.h"
38 #include "expr.h"
39 #include "hashtab.h"
40 #include "hash-set.h"
41 #include "vec.h"
42 #include "machmode.h"
43 #include "hard-reg-set.h"
44 #include "input.h"
45 #include "function.h"
46 #include "predict.h"
47 #include "dominance.h"
48 #include "cfg.h"
49 #include "basic-block.h"
50 #include "toplev.h"
51 #include "tm_p.h"
52 #include "ggc.h"
53 #include "coverage.h"
54 #include "langhooks.h"
55 #include "hash-table.h"
56 #include "tree-iterator.h"
57 #include "context.h"
58 #include "pass_manager.h"
59 #include "tree-pass.h"
60 #include "hash-map.h"
61 #include "is-a.h"
62 #include "plugin-api.h"
63 #include "ipa-ref.h"
64 #include "cgraph.h"
65 #include "dumpfile.h"
66 #include "diagnostic-core.h"
67 #include "intl.h"
68 #include "filenames.h"
69 #include "target.h"
70 #include "params.h"
71 #include "auto-profile.h"
73 #include "gcov-io.h"
74 #include "gcov-io.c"
76 struct GTY((chain_next ("%h.next"))) coverage_data
78 struct coverage_data *next; /* next function */
79 unsigned ident; /* function ident */
80 unsigned lineno_checksum; /* function lineno checksum */
81 unsigned cfg_checksum; /* function cfg checksum */
82 tree fn_decl; /* the function decl */
83 tree ctr_vars[GCOV_COUNTERS]; /* counter variables. */
86 /* Counts information for a function. */
87 typedef struct counts_entry
89 /* We hash by */
90 unsigned ident;
91 unsigned ctr;
93 /* Store */
94 unsigned lineno_checksum;
95 unsigned cfg_checksum;
96 gcov_type *counts;
97 struct gcov_ctr_summary summary;
99 /* hash_table support. */
100 typedef counts_entry value_type;
101 typedef counts_entry compare_type;
102 static inline hashval_t hash (const value_type *);
103 static int equal (const value_type *, const compare_type *);
104 static void remove (value_type *);
105 } counts_entry_t;
107 static GTY(()) struct coverage_data *functions_head = 0;
108 static struct coverage_data **functions_tail = &functions_head;
109 static unsigned no_coverage = 0;
111 /* Cumulative counter information for whole program. */
112 static unsigned prg_ctr_mask; /* Mask of counter types generated. */
114 /* Counter information for current function. */
115 static unsigned fn_ctr_mask; /* Mask of counters used. */
116 static GTY(()) tree fn_v_ctrs[GCOV_COUNTERS]; /* counter variables. */
117 static unsigned fn_n_ctrs[GCOV_COUNTERS]; /* Counters allocated. */
118 static unsigned fn_b_ctrs[GCOV_COUNTERS]; /* Allocation base. */
120 /* Coverage info VAR_DECL and function info type nodes. */
121 static GTY(()) tree gcov_info_var;
122 static GTY(()) tree gcov_fn_info_type;
123 static GTY(()) tree gcov_fn_info_ptr_type;
125 /* Name of the notes (gcno) output file. The "bbg" prefix is for
126 historical reasons, when the notes file contained only the
127 basic block graph notes.
128 If this is NULL we're not writing to the notes file. */
129 static char *bbg_file_name;
131 /* File stamp for notes file. */
132 static unsigned bbg_file_stamp;
134 /* Name of the count data (gcda) file. */
135 static char *da_file_name;
137 /* The names of merge functions for counters. */
138 #define STR(str) #str
139 #define DEF_GCOV_COUNTER(COUNTER, NAME, FN_TYPE) STR(__gcov_merge ## FN_TYPE),
140 static const char *const ctr_merge_functions[GCOV_COUNTERS] = {
141 #include "gcov-counter.def"
143 #undef DEF_GCOV_COUNTER
144 #undef STR
146 #define DEF_GCOV_COUNTER(COUNTER, NAME, FN_TYPE) NAME,
147 static const char *const ctr_names[GCOV_COUNTERS] = {
148 #include "gcov-counter.def"
150 #undef DEF_GCOV_COUNTER
152 /* Forward declarations. */
153 static void read_counts_file (void);
154 static tree build_var (tree, tree, int);
155 static void build_fn_info_type (tree, unsigned, tree);
156 static void build_info_type (tree, tree);
157 static tree build_fn_info (const struct coverage_data *, tree, tree);
158 static tree build_info (tree, tree);
159 static bool coverage_obj_init (void);
160 static vec<constructor_elt, va_gc> *coverage_obj_fn
161 (vec<constructor_elt, va_gc> *, tree, struct coverage_data const *);
162 static void coverage_obj_finish (vec<constructor_elt, va_gc> *);
164 /* Return the type node for gcov_type. */
166 tree
167 get_gcov_type (void)
169 machine_mode mode = smallest_mode_for_size (GCOV_TYPE_SIZE, MODE_INT);
170 return lang_hooks.types.type_for_mode (mode, false);
173 /* Return the type node for gcov_unsigned_t. */
175 static tree
176 get_gcov_unsigned_t (void)
178 machine_mode mode = smallest_mode_for_size (32, MODE_INT);
179 return lang_hooks.types.type_for_mode (mode, true);
182 inline hashval_t
183 counts_entry::hash (const value_type *entry)
185 return entry->ident * GCOV_COUNTERS + entry->ctr;
188 inline int
189 counts_entry::equal (const value_type *entry1,
190 const compare_type *entry2)
192 return entry1->ident == entry2->ident && entry1->ctr == entry2->ctr;
195 inline void
196 counts_entry::remove (value_type *entry)
198 free (entry->counts);
199 free (entry);
202 /* Hash table of count data. */
203 static hash_table<counts_entry> *counts_hash;
205 /* Read in the counts file, if available. */
207 static void
208 read_counts_file (void)
210 gcov_unsigned_t fn_ident = 0;
211 struct gcov_summary summary;
212 unsigned new_summary = 1;
213 gcov_unsigned_t tag;
214 int is_error = 0;
215 unsigned lineno_checksum = 0;
216 unsigned cfg_checksum = 0;
218 if (!gcov_open (da_file_name, 1))
219 return;
221 if (!gcov_magic (gcov_read_unsigned (), GCOV_DATA_MAGIC))
223 warning (0, "%qs is not a gcov data file", da_file_name);
224 gcov_close ();
225 return;
227 else if ((tag = gcov_read_unsigned ()) != GCOV_VERSION)
229 char v[4], e[4];
231 GCOV_UNSIGNED2STRING (v, tag);
232 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
234 warning (0, "%qs is version %q.*s, expected version %q.*s",
235 da_file_name, 4, v, 4, e);
236 gcov_close ();
237 return;
240 /* Read the stamp, used for creating a generation count. */
241 tag = gcov_read_unsigned ();
242 bbg_file_stamp = crc32_unsigned (bbg_file_stamp, tag);
244 counts_hash = new hash_table<counts_entry> (10);
245 while ((tag = gcov_read_unsigned ()))
247 gcov_unsigned_t length;
248 gcov_position_t offset;
250 length = gcov_read_unsigned ();
251 offset = gcov_position ();
252 if (tag == GCOV_TAG_FUNCTION)
254 if (length)
256 fn_ident = gcov_read_unsigned ();
257 lineno_checksum = gcov_read_unsigned ();
258 cfg_checksum = gcov_read_unsigned ();
260 else
261 fn_ident = lineno_checksum = cfg_checksum = 0;
262 new_summary = 1;
264 else if (tag == GCOV_TAG_PROGRAM_SUMMARY)
266 struct gcov_summary sum;
267 unsigned ix;
269 if (new_summary)
270 memset (&summary, 0, sizeof (summary));
272 gcov_read_summary (&sum);
273 for (ix = 0; ix != GCOV_COUNTERS_SUMMABLE; ix++)
275 summary.ctrs[ix].runs += sum.ctrs[ix].runs;
276 summary.ctrs[ix].sum_all += sum.ctrs[ix].sum_all;
277 if (summary.ctrs[ix].run_max < sum.ctrs[ix].run_max)
278 summary.ctrs[ix].run_max = sum.ctrs[ix].run_max;
279 summary.ctrs[ix].sum_max += sum.ctrs[ix].sum_max;
281 if (new_summary)
282 memcpy (summary.ctrs[GCOV_COUNTER_ARCS].histogram,
283 sum.ctrs[GCOV_COUNTER_ARCS].histogram,
284 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
285 else
286 gcov_histogram_merge (summary.ctrs[GCOV_COUNTER_ARCS].histogram,
287 sum.ctrs[GCOV_COUNTER_ARCS].histogram);
288 new_summary = 0;
290 else if (GCOV_TAG_IS_COUNTER (tag) && fn_ident)
292 counts_entry_t **slot, *entry, elt;
293 unsigned n_counts = GCOV_TAG_COUNTER_NUM (length);
294 unsigned ix;
296 elt.ident = fn_ident;
297 elt.ctr = GCOV_COUNTER_FOR_TAG (tag);
299 slot = counts_hash->find_slot (&elt, INSERT);
300 entry = *slot;
301 if (!entry)
303 *slot = entry = XCNEW (counts_entry_t);
304 entry->ident = fn_ident;
305 entry->ctr = elt.ctr;
306 entry->lineno_checksum = lineno_checksum;
307 entry->cfg_checksum = cfg_checksum;
308 if (elt.ctr < GCOV_COUNTERS_SUMMABLE)
309 entry->summary = summary.ctrs[elt.ctr];
310 entry->summary.num = n_counts;
311 entry->counts = XCNEWVEC (gcov_type, n_counts);
313 else if (entry->lineno_checksum != lineno_checksum
314 || entry->cfg_checksum != cfg_checksum)
316 error ("Profile data for function %u is corrupted", fn_ident);
317 error ("checksum is (%x,%x) instead of (%x,%x)",
318 entry->lineno_checksum, entry->cfg_checksum,
319 lineno_checksum, cfg_checksum);
320 delete counts_hash;
321 counts_hash = NULL;
322 break;
324 else if (entry->summary.num != n_counts)
326 error ("Profile data for function %u is corrupted", fn_ident);
327 error ("number of counters is %d instead of %d", entry->summary.num, n_counts);
328 delete counts_hash;
329 counts_hash = NULL;
330 break;
332 else if (elt.ctr >= GCOV_COUNTERS_SUMMABLE)
334 error ("cannot merge separate %s counters for function %u",
335 ctr_names[elt.ctr], fn_ident);
336 goto skip_merge;
338 else
340 entry->summary.runs += summary.ctrs[elt.ctr].runs;
341 entry->summary.sum_all += summary.ctrs[elt.ctr].sum_all;
342 if (entry->summary.run_max < summary.ctrs[elt.ctr].run_max)
343 entry->summary.run_max = summary.ctrs[elt.ctr].run_max;
344 entry->summary.sum_max += summary.ctrs[elt.ctr].sum_max;
346 for (ix = 0; ix != n_counts; ix++)
347 entry->counts[ix] += gcov_read_counter ();
348 skip_merge:;
350 gcov_sync (offset, length);
351 if ((is_error = gcov_is_error ()))
353 error (is_error < 0 ? "%qs has overflowed" : "%qs is corrupted",
354 da_file_name);
355 delete counts_hash;
356 counts_hash = NULL;
357 break;
361 gcov_close ();
364 /* Returns the counters for a particular tag. */
366 gcov_type *
367 get_coverage_counts (unsigned counter, unsigned expected,
368 unsigned cfg_checksum, unsigned lineno_checksum,
369 const struct gcov_ctr_summary **summary)
371 counts_entry_t *entry, elt;
373 /* No hash table, no counts. */
374 if (!counts_hash)
376 static int warned = 0;
378 if (!warned++ && dump_enabled_p ())
379 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
380 (flag_guess_branch_prob
381 ? "file %s not found, execution counts estimated\n"
382 : "file %s not found, execution counts assumed to "
383 "be zero\n"),
384 da_file_name);
385 return NULL;
387 if (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID))
388 elt.ident = current_function_funcdef_no + 1;
389 else
391 gcc_assert (coverage_node_map_initialized_p ());
392 elt.ident = cgraph_node::get (cfun->decl)->profile_id;
394 elt.ctr = counter;
395 entry = counts_hash->find (&elt);
396 if (!entry || !entry->summary.num)
397 /* The function was not emitted, or is weak and not chosen in the
398 final executable. Silently fail, because there's nothing we
399 can do about it. */
400 return NULL;
402 if (entry->cfg_checksum != cfg_checksum
403 || entry->summary.num != expected)
405 static int warned = 0;
406 bool warning_printed = false;
407 tree id = DECL_ASSEMBLER_NAME (current_function_decl);
409 warning_printed =
410 warning_at (input_location, OPT_Wcoverage_mismatch,
411 "the control flow of function %qE does not match "
412 "its profile data (counter %qs)", id, ctr_names[counter]);
413 if (warning_printed && dump_enabled_p ())
415 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
416 "use -Wno-error=coverage-mismatch to tolerate "
417 "the mismatch but performance may drop if the "
418 "function is hot\n");
420 if (!seen_error ()
421 && !warned++)
423 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
424 "coverage mismatch ignored\n");
425 dump_printf (MSG_OPTIMIZED_LOCATIONS,
426 flag_guess_branch_prob
427 ? G_("execution counts estimated\n")
428 : G_("execution counts assumed to be zero\n"));
429 if (!flag_guess_branch_prob)
430 dump_printf (MSG_OPTIMIZED_LOCATIONS,
431 "this can result in poorly optimized code\n");
435 return NULL;
437 else if (entry->lineno_checksum != lineno_checksum)
439 warning (OPT_Wcoverage_mismatch,
440 "source locations for function %qE have changed,"
441 " the profile data may be out of date",
442 DECL_ASSEMBLER_NAME (current_function_decl));
445 if (summary)
446 *summary = &entry->summary;
448 return entry->counts;
451 /* Allocate NUM counters of type COUNTER. Returns nonzero if the
452 allocation succeeded. */
455 coverage_counter_alloc (unsigned counter, unsigned num)
457 if (no_coverage)
458 return 0;
460 if (!num)
461 return 1;
463 if (!fn_v_ctrs[counter])
465 tree array_type = build_array_type (get_gcov_type (), NULL_TREE);
467 fn_v_ctrs[counter]
468 = build_var (current_function_decl, array_type, counter);
471 fn_b_ctrs[counter] = fn_n_ctrs[counter];
472 fn_n_ctrs[counter] += num;
474 fn_ctr_mask |= 1 << counter;
475 return 1;
478 /* Generate a tree to access COUNTER NO. */
480 tree
481 tree_coverage_counter_ref (unsigned counter, unsigned no)
483 tree gcov_type_node = get_gcov_type ();
485 gcc_assert (no < fn_n_ctrs[counter] - fn_b_ctrs[counter]);
487 no += fn_b_ctrs[counter];
489 /* "no" here is an array index, scaled to bytes later. */
490 return build4 (ARRAY_REF, gcov_type_node, fn_v_ctrs[counter],
491 build_int_cst (integer_type_node, no), NULL, NULL);
494 /* Generate a tree to access the address of COUNTER NO. */
496 tree
497 tree_coverage_counter_addr (unsigned counter, unsigned no)
499 tree gcov_type_node = get_gcov_type ();
501 gcc_assert (no < fn_n_ctrs[counter] - fn_b_ctrs[counter]);
502 no += fn_b_ctrs[counter];
504 /* "no" here is an array index, scaled to bytes later. */
505 return build_fold_addr_expr (build4 (ARRAY_REF, gcov_type_node,
506 fn_v_ctrs[counter],
507 build_int_cst (integer_type_node, no),
508 NULL, NULL));
512 /* Generate a checksum for a string. CHKSUM is the current
513 checksum. */
515 static unsigned
516 coverage_checksum_string (unsigned chksum, const char *string)
518 int i;
519 char *dup = NULL;
521 /* Look for everything that looks if it were produced by
522 get_file_function_name and zero out the second part
523 that may result from flag_random_seed. This is not critical
524 as the checksums are used only for sanity checking. */
525 for (i = 0; string[i]; i++)
527 int offset = 0;
528 if (!strncmp (string + i, "_GLOBAL__N_", 11))
529 offset = 11;
530 if (!strncmp (string + i, "_GLOBAL__", 9))
531 offset = 9;
533 /* C++ namespaces do have scheme:
534 _GLOBAL__N_<filename>_<wrongmagicnumber>_<magicnumber>functionname
535 since filename might contain extra underscores there seems
536 to be no better chance then walk all possible offsets looking
537 for magicnumber. */
538 if (offset)
540 for (i = i + offset; string[i]; i++)
541 if (string[i]=='_')
543 int y;
545 for (y = 1; y < 9; y++)
546 if (!(string[i + y] >= '0' && string[i + y] <= '9')
547 && !(string[i + y] >= 'A' && string[i + y] <= 'F'))
548 break;
549 if (y != 9 || string[i + 9] != '_')
550 continue;
551 for (y = 10; y < 18; y++)
552 if (!(string[i + y] >= '0' && string[i + y] <= '9')
553 && !(string[i + y] >= 'A' && string[i + y] <= 'F'))
554 break;
555 if (y != 18)
556 continue;
557 if (!dup)
558 string = dup = xstrdup (string);
559 for (y = 10; y < 18; y++)
560 dup[i + y] = '0';
562 break;
566 chksum = crc32_string (chksum, string);
567 free (dup);
569 return chksum;
572 /* Compute checksum for the current function. We generate a CRC32. */
574 unsigned
575 coverage_compute_lineno_checksum (void)
577 expanded_location xloc
578 = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
579 unsigned chksum = xloc.line;
581 chksum = coverage_checksum_string (chksum, xloc.file);
582 chksum = coverage_checksum_string
583 (chksum, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl)));
585 return chksum;
588 /* Compute profile ID. This is better to be unique in whole program. */
590 unsigned
591 coverage_compute_profile_id (struct cgraph_node *n)
593 unsigned chksum;
595 /* Externally visible symbols have unique name. */
596 if (TREE_PUBLIC (n->decl) || DECL_EXTERNAL (n->decl) || n->unique_name)
598 chksum = coverage_checksum_string
599 (0, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n->decl)));
601 else
603 expanded_location xloc
604 = expand_location (DECL_SOURCE_LOCATION (n->decl));
605 bool use_name_only = (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID) == 0);
607 chksum = (use_name_only ? 0 : xloc.line);
608 chksum = coverage_checksum_string (chksum, xloc.file);
609 chksum = coverage_checksum_string
610 (chksum, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n->decl)));
611 if (!use_name_only && first_global_object_name)
612 chksum = coverage_checksum_string
613 (chksum, first_global_object_name);
614 chksum = coverage_checksum_string
615 (chksum, aux_base_name);
618 /* Non-negative integers are hopefully small enough to fit in all targets.
619 Gcov file formats wants non-zero function IDs. */
620 chksum = chksum & 0x7fffffff;
621 return chksum + (!chksum);
624 /* Compute cfg checksum for the function FN given as argument.
625 The checksum is calculated carefully so that
626 source code changes that doesn't affect the control flow graph
627 won't change the checksum.
628 This is to make the profile data useable across source code change.
629 The downside of this is that the compiler may use potentially
630 wrong profile data - that the source code change has non-trivial impact
631 on the validity of profile data (e.g. the reversed condition)
632 but the compiler won't detect the change and use the wrong profile data. */
634 unsigned
635 coverage_compute_cfg_checksum (struct function *fn)
637 basic_block bb;
638 unsigned chksum = n_basic_blocks_for_fn (fn);
640 FOR_EACH_BB_FN (bb, fn)
642 edge e;
643 edge_iterator ei;
644 chksum = crc32_byte (chksum, bb->index);
645 FOR_EACH_EDGE (e, ei, bb->succs)
647 chksum = crc32_byte (chksum, e->dest->index);
651 return chksum;
654 /* Begin output to the notes file for the current function.
655 Writes the function header. Returns nonzero if data should be output. */
658 coverage_begin_function (unsigned lineno_checksum, unsigned cfg_checksum)
660 expanded_location xloc;
661 unsigned long offset;
663 /* We don't need to output .gcno file unless we're under -ftest-coverage
664 (e.g. -fprofile-arcs/generate/use don't need .gcno to work). */
665 if (no_coverage || !bbg_file_name)
666 return 0;
668 xloc = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
670 /* Announce function */
671 offset = gcov_write_tag (GCOV_TAG_FUNCTION);
672 if (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID))
673 gcov_write_unsigned (current_function_funcdef_no + 1);
674 else
676 gcc_assert (coverage_node_map_initialized_p ());
677 gcov_write_unsigned (
678 cgraph_node::get (current_function_decl)->profile_id);
681 gcov_write_unsigned (lineno_checksum);
682 gcov_write_unsigned (cfg_checksum);
683 gcov_write_string (IDENTIFIER_POINTER
684 (DECL_ASSEMBLER_NAME (current_function_decl)));
685 gcov_write_string (xloc.file);
686 gcov_write_unsigned (xloc.line);
687 gcov_write_length (offset);
689 return !gcov_is_error ();
692 /* Finish coverage data for the current function. Verify no output
693 error has occurred. Save function coverage counts. */
695 void
696 coverage_end_function (unsigned lineno_checksum, unsigned cfg_checksum)
698 unsigned i;
700 if (bbg_file_name && gcov_is_error ())
702 warning (0, "error writing %qs", bbg_file_name);
703 unlink (bbg_file_name);
704 bbg_file_name = NULL;
707 if (fn_ctr_mask)
709 struct coverage_data *item = 0;
711 item = ggc_alloc<coverage_data> ();
713 if (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID))
714 item->ident = current_function_funcdef_no + 1;
715 else
717 gcc_assert (coverage_node_map_initialized_p ());
718 item->ident = cgraph_node::get (cfun->decl)->profile_id;
721 item->lineno_checksum = lineno_checksum;
722 item->cfg_checksum = cfg_checksum;
724 item->fn_decl = current_function_decl;
725 item->next = 0;
726 *functions_tail = item;
727 functions_tail = &item->next;
729 for (i = 0; i != GCOV_COUNTERS; i++)
731 tree var = fn_v_ctrs[i];
733 if (item)
734 item->ctr_vars[i] = var;
735 if (var)
737 tree array_type = build_index_type (size_int (fn_n_ctrs[i] - 1));
738 array_type = build_array_type (get_gcov_type (), array_type);
739 TREE_TYPE (var) = array_type;
740 DECL_SIZE (var) = TYPE_SIZE (array_type);
741 DECL_SIZE_UNIT (var) = TYPE_SIZE_UNIT (array_type);
742 varpool_node::finalize_decl (var);
745 fn_b_ctrs[i] = fn_n_ctrs[i] = 0;
746 fn_v_ctrs[i] = NULL_TREE;
748 prg_ctr_mask |= fn_ctr_mask;
749 fn_ctr_mask = 0;
753 /* Build a coverage variable of TYPE for function FN_DECL. If COUNTER
754 >= 0 it is a counter array, otherwise it is the function structure. */
756 static tree
757 build_var (tree fn_decl, tree type, int counter)
759 tree var = build_decl (BUILTINS_LOCATION, VAR_DECL, NULL_TREE, type);
760 const char *fn_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fn_decl));
761 char *buf;
762 size_t fn_name_len, len;
764 fn_name = targetm.strip_name_encoding (fn_name);
765 fn_name_len = strlen (fn_name);
766 buf = XALLOCAVEC (char, fn_name_len + 8 + sizeof (int) * 3);
768 if (counter < 0)
769 strcpy (buf, "__gcov__");
770 else
771 sprintf (buf, "__gcov%u_", counter);
772 len = strlen (buf);
773 #ifndef NO_DOT_IN_LABEL
774 buf[len - 1] = '.';
775 #elif !defined NO_DOLLAR_IN_LABEL
776 buf[len - 1] = '$';
777 #endif
778 memcpy (buf + len, fn_name, fn_name_len + 1);
779 DECL_NAME (var) = get_identifier (buf);
780 TREE_STATIC (var) = 1;
781 TREE_ADDRESSABLE (var) = 1;
782 DECL_NONALIASED (var) = 1;
783 DECL_ALIGN (var) = TYPE_ALIGN (type);
785 return var;
788 /* Creates the gcov_fn_info RECORD_TYPE. */
790 static void
791 build_fn_info_type (tree type, unsigned counters, tree gcov_info_type)
793 tree ctr_info = lang_hooks.types.make_type (RECORD_TYPE);
794 tree field, fields;
795 tree array_type;
797 gcc_assert (counters);
799 /* ctr_info::num */
800 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
801 get_gcov_unsigned_t ());
802 fields = field;
804 /* ctr_info::values */
805 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
806 build_pointer_type (get_gcov_type ()));
807 DECL_CHAIN (field) = fields;
808 fields = field;
810 finish_builtin_struct (ctr_info, "__gcov_ctr_info", fields, NULL_TREE);
812 /* key */
813 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
814 build_pointer_type (build_qualified_type
815 (gcov_info_type, TYPE_QUAL_CONST)));
816 fields = field;
818 /* ident */
819 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
820 get_gcov_unsigned_t ());
821 DECL_CHAIN (field) = fields;
822 fields = field;
824 /* lineno_checksum */
825 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
826 get_gcov_unsigned_t ());
827 DECL_CHAIN (field) = fields;
828 fields = field;
830 /* cfg checksum */
831 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
832 get_gcov_unsigned_t ());
833 DECL_CHAIN (field) = fields;
834 fields = field;
836 array_type = build_index_type (size_int (counters - 1));
837 array_type = build_array_type (ctr_info, array_type);
839 /* counters */
840 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE, array_type);
841 DECL_CHAIN (field) = fields;
842 fields = field;
844 finish_builtin_struct (type, "__gcov_fn_info", fields, NULL_TREE);
847 /* Returns a CONSTRUCTOR for a gcov_fn_info. DATA is
848 the coverage data for the function and TYPE is the gcov_fn_info
849 RECORD_TYPE. KEY is the object file key. */
851 static tree
852 build_fn_info (const struct coverage_data *data, tree type, tree key)
854 tree fields = TYPE_FIELDS (type);
855 tree ctr_type;
856 unsigned ix;
857 vec<constructor_elt, va_gc> *v1 = NULL;
858 vec<constructor_elt, va_gc> *v2 = NULL;
860 /* key */
861 CONSTRUCTOR_APPEND_ELT (v1, fields,
862 build1 (ADDR_EXPR, TREE_TYPE (fields), key));
863 fields = DECL_CHAIN (fields);
865 /* ident */
866 CONSTRUCTOR_APPEND_ELT (v1, fields,
867 build_int_cstu (get_gcov_unsigned_t (),
868 data->ident));
869 fields = DECL_CHAIN (fields);
871 /* lineno_checksum */
872 CONSTRUCTOR_APPEND_ELT (v1, fields,
873 build_int_cstu (get_gcov_unsigned_t (),
874 data->lineno_checksum));
875 fields = DECL_CHAIN (fields);
877 /* cfg_checksum */
878 CONSTRUCTOR_APPEND_ELT (v1, fields,
879 build_int_cstu (get_gcov_unsigned_t (),
880 data->cfg_checksum));
881 fields = DECL_CHAIN (fields);
883 /* counters */
884 ctr_type = TREE_TYPE (TREE_TYPE (fields));
885 for (ix = 0; ix != GCOV_COUNTERS; ix++)
886 if (prg_ctr_mask & (1 << ix))
888 vec<constructor_elt, va_gc> *ctr = NULL;
889 tree var = data->ctr_vars[ix];
890 unsigned count = 0;
892 if (var)
893 count
894 = tree_to_shwi (TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (var))))
895 + 1;
897 CONSTRUCTOR_APPEND_ELT (ctr, TYPE_FIELDS (ctr_type),
898 build_int_cstu (get_gcov_unsigned_t (),
899 count));
901 if (var)
902 CONSTRUCTOR_APPEND_ELT (ctr, DECL_CHAIN (TYPE_FIELDS (ctr_type)),
903 build_fold_addr_expr (var));
905 CONSTRUCTOR_APPEND_ELT (v2, NULL, build_constructor (ctr_type, ctr));
908 CONSTRUCTOR_APPEND_ELT (v1, fields,
909 build_constructor (TREE_TYPE (fields), v2));
911 return build_constructor (type, v1);
914 /* Create gcov_info struct. TYPE is the incomplete RECORD_TYPE to be
915 completed, and FN_INFO_PTR_TYPE is a pointer to the function info type. */
917 static void
918 build_info_type (tree type, tree fn_info_ptr_type)
920 tree field, fields = NULL_TREE;
921 tree merge_fn_type;
923 /* Version ident */
924 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
925 get_gcov_unsigned_t ());
926 DECL_CHAIN (field) = fields;
927 fields = field;
929 /* next pointer */
930 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
931 build_pointer_type (build_qualified_type
932 (type, TYPE_QUAL_CONST)));
933 DECL_CHAIN (field) = fields;
934 fields = field;
936 /* stamp */
937 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
938 get_gcov_unsigned_t ());
939 DECL_CHAIN (field) = fields;
940 fields = field;
942 /* Filename */
943 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
944 build_pointer_type (build_qualified_type
945 (char_type_node, TYPE_QUAL_CONST)));
946 DECL_CHAIN (field) = fields;
947 fields = field;
949 /* merge fn array */
950 merge_fn_type
951 = build_function_type_list (void_type_node,
952 build_pointer_type (get_gcov_type ()),
953 get_gcov_unsigned_t (), NULL_TREE);
954 merge_fn_type
955 = build_array_type (build_pointer_type (merge_fn_type),
956 build_index_type (size_int (GCOV_COUNTERS - 1)));
957 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
958 merge_fn_type);
959 DECL_CHAIN (field) = fields;
960 fields = field;
962 /* n_functions */
963 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
964 get_gcov_unsigned_t ());
965 DECL_CHAIN (field) = fields;
966 fields = field;
968 /* function_info pointer pointer */
969 fn_info_ptr_type = build_pointer_type
970 (build_qualified_type (fn_info_ptr_type, TYPE_QUAL_CONST));
971 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
972 fn_info_ptr_type);
973 DECL_CHAIN (field) = fields;
974 fields = field;
976 finish_builtin_struct (type, "__gcov_info", fields, NULL_TREE);
979 /* Returns a CONSTRUCTOR for the gcov_info object. INFO_TYPE is the
980 gcov_info structure type, FN_ARY is the array of pointers to
981 function info objects. */
983 static tree
984 build_info (tree info_type, tree fn_ary)
986 tree info_fields = TYPE_FIELDS (info_type);
987 tree merge_fn_type, n_funcs;
988 unsigned ix;
989 tree filename_string;
990 int da_file_name_len;
991 vec<constructor_elt, va_gc> *v1 = NULL;
992 vec<constructor_elt, va_gc> *v2 = NULL;
994 /* Version ident */
995 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
996 build_int_cstu (TREE_TYPE (info_fields),
997 GCOV_VERSION));
998 info_fields = DECL_CHAIN (info_fields);
1000 /* next -- NULL */
1001 CONSTRUCTOR_APPEND_ELT (v1, info_fields, null_pointer_node);
1002 info_fields = DECL_CHAIN (info_fields);
1004 /* stamp */
1005 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
1006 build_int_cstu (TREE_TYPE (info_fields),
1007 bbg_file_stamp));
1008 info_fields = DECL_CHAIN (info_fields);
1010 /* Filename */
1011 da_file_name_len = strlen (da_file_name);
1012 filename_string = build_string (da_file_name_len + 1, da_file_name);
1013 TREE_TYPE (filename_string) = build_array_type
1014 (char_type_node, build_index_type (size_int (da_file_name_len)));
1015 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
1016 build1 (ADDR_EXPR, TREE_TYPE (info_fields),
1017 filename_string));
1018 info_fields = DECL_CHAIN (info_fields);
1020 /* merge fn array -- NULL slots indicate unmeasured counters */
1021 merge_fn_type = TREE_TYPE (TREE_TYPE (info_fields));
1022 for (ix = 0; ix != GCOV_COUNTERS; ix++)
1024 tree ptr = null_pointer_node;
1026 if ((1u << ix) & prg_ctr_mask)
1028 tree merge_fn = build_decl (BUILTINS_LOCATION,
1029 FUNCTION_DECL,
1030 get_identifier (ctr_merge_functions[ix]),
1031 TREE_TYPE (merge_fn_type));
1032 DECL_EXTERNAL (merge_fn) = 1;
1033 TREE_PUBLIC (merge_fn) = 1;
1034 DECL_ARTIFICIAL (merge_fn) = 1;
1035 TREE_NOTHROW (merge_fn) = 1;
1036 /* Initialize assembler name so we can stream out. */
1037 DECL_ASSEMBLER_NAME (merge_fn);
1038 ptr = build1 (ADDR_EXPR, merge_fn_type, merge_fn);
1040 CONSTRUCTOR_APPEND_ELT (v2, NULL, ptr);
1042 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
1043 build_constructor (TREE_TYPE (info_fields), v2));
1044 info_fields = DECL_CHAIN (info_fields);
1046 /* n_functions */
1047 n_funcs = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (fn_ary)));
1048 n_funcs = fold_build2 (PLUS_EXPR, TREE_TYPE (info_fields),
1049 n_funcs, size_one_node);
1050 CONSTRUCTOR_APPEND_ELT (v1, info_fields, n_funcs);
1051 info_fields = DECL_CHAIN (info_fields);
1053 /* functions */
1054 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
1055 build1 (ADDR_EXPR, TREE_TYPE (info_fields), fn_ary));
1056 info_fields = DECL_CHAIN (info_fields);
1058 gcc_assert (!info_fields);
1059 return build_constructor (info_type, v1);
1062 /* Generate the constructor function to call __gcov_init. */
1064 static void
1065 build_init_ctor (tree gcov_info_type)
1067 tree ctor, stmt, init_fn;
1069 /* Build a decl for __gcov_init. */
1070 init_fn = build_pointer_type (gcov_info_type);
1071 init_fn = build_function_type_list (void_type_node, init_fn, NULL);
1072 init_fn = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
1073 get_identifier ("__gcov_init"), init_fn);
1074 TREE_PUBLIC (init_fn) = 1;
1075 DECL_EXTERNAL (init_fn) = 1;
1076 DECL_ASSEMBLER_NAME (init_fn);
1078 /* Generate a call to __gcov_init(&gcov_info). */
1079 ctor = NULL;
1080 stmt = build_fold_addr_expr (gcov_info_var);
1081 stmt = build_call_expr (init_fn, 1, stmt);
1082 append_to_statement_list (stmt, &ctor);
1084 /* Generate a constructor to run it. */
1085 cgraph_build_static_cdtor ('I', ctor, DEFAULT_INIT_PRIORITY);
1088 /* Create the gcov_info types and object. Generate the constructor
1089 function to call __gcov_init. Does not generate the initializer
1090 for the object. Returns TRUE if coverage data is being emitted. */
1092 static bool
1093 coverage_obj_init (void)
1095 tree gcov_info_type;
1096 unsigned n_counters = 0;
1097 unsigned ix;
1098 struct coverage_data *fn;
1099 struct coverage_data **fn_prev;
1100 char name_buf[32];
1102 no_coverage = 1; /* Disable any further coverage. */
1104 if (!prg_ctr_mask)
1105 return false;
1107 if (symtab->dump_file)
1108 fprintf (symtab->dump_file, "Using data file %s\n", da_file_name);
1110 /* Prune functions. */
1111 for (fn_prev = &functions_head; (fn = *fn_prev);)
1112 if (DECL_STRUCT_FUNCTION (fn->fn_decl))
1113 fn_prev = &fn->next;
1114 else
1115 /* The function is not being emitted, remove from list. */
1116 *fn_prev = fn->next;
1118 if (functions_head == NULL)
1119 return false;
1121 for (ix = 0; ix != GCOV_COUNTERS; ix++)
1122 if ((1u << ix) & prg_ctr_mask)
1123 n_counters++;
1125 /* Build the info and fn_info types. These are mutually recursive. */
1126 gcov_info_type = lang_hooks.types.make_type (RECORD_TYPE);
1127 gcov_fn_info_type = lang_hooks.types.make_type (RECORD_TYPE);
1128 gcov_fn_info_ptr_type = build_pointer_type
1129 (build_qualified_type (gcov_fn_info_type, TYPE_QUAL_CONST));
1130 build_fn_info_type (gcov_fn_info_type, n_counters, gcov_info_type);
1131 build_info_type (gcov_info_type, gcov_fn_info_ptr_type);
1133 /* Build the gcov info var, this is referred to in its own
1134 initializer. */
1135 gcov_info_var = build_decl (BUILTINS_LOCATION,
1136 VAR_DECL, NULL_TREE, gcov_info_type);
1137 TREE_STATIC (gcov_info_var) = 1;
1138 ASM_GENERATE_INTERNAL_LABEL (name_buf, "LPBX", 0);
1139 DECL_NAME (gcov_info_var) = get_identifier (name_buf);
1141 build_init_ctor (gcov_info_type);
1143 return true;
1146 /* Generate the coverage function info for FN and DATA. Append a
1147 pointer to that object to CTOR and return the appended CTOR. */
1149 static vec<constructor_elt, va_gc> *
1150 coverage_obj_fn (vec<constructor_elt, va_gc> *ctor, tree fn,
1151 struct coverage_data const *data)
1153 tree init = build_fn_info (data, gcov_fn_info_type, gcov_info_var);
1154 tree var = build_var (fn, gcov_fn_info_type, -1);
1156 DECL_INITIAL (var) = init;
1157 varpool_node::finalize_decl (var);
1159 CONSTRUCTOR_APPEND_ELT (ctor, NULL,
1160 build1 (ADDR_EXPR, gcov_fn_info_ptr_type, var));
1161 return ctor;
1164 /* Finalize the coverage data. Generates the array of pointers to
1165 function objects from CTOR. Generate the gcov_info initializer. */
1167 static void
1168 coverage_obj_finish (vec<constructor_elt, va_gc> *ctor)
1170 unsigned n_functions = vec_safe_length (ctor);
1171 tree fn_info_ary_type = build_array_type
1172 (build_qualified_type (gcov_fn_info_ptr_type, TYPE_QUAL_CONST),
1173 build_index_type (size_int (n_functions - 1)));
1174 tree fn_info_ary = build_decl (BUILTINS_LOCATION, VAR_DECL, NULL_TREE,
1175 fn_info_ary_type);
1176 char name_buf[32];
1178 TREE_STATIC (fn_info_ary) = 1;
1179 ASM_GENERATE_INTERNAL_LABEL (name_buf, "LPBX", 1);
1180 DECL_NAME (fn_info_ary) = get_identifier (name_buf);
1181 DECL_INITIAL (fn_info_ary) = build_constructor (fn_info_ary_type, ctor);
1182 varpool_node::finalize_decl (fn_info_ary);
1184 DECL_INITIAL (gcov_info_var)
1185 = build_info (TREE_TYPE (gcov_info_var), fn_info_ary);
1186 varpool_node::finalize_decl (gcov_info_var);
1189 /* Perform file-level initialization. Read in data file, generate name
1190 of notes file. */
1192 void
1193 coverage_init (const char *filename)
1195 int len = strlen (filename);
1196 int prefix_len = 0;
1198 /* Since coverage_init is invoked very early, before the pass
1199 manager, we need to set up the dumping explicitly. This is
1200 similar to the handling in finish_optimization_passes. */
1201 int profile_pass_num =
1202 g->get_passes ()->get_pass_profile ()->static_pass_number;
1203 g->get_dumps ()->dump_start (profile_pass_num, NULL);
1205 if (!profile_data_prefix && !IS_ABSOLUTE_PATH (filename))
1206 profile_data_prefix = getpwd ();
1208 if (profile_data_prefix)
1209 prefix_len = strlen (profile_data_prefix);
1211 /* Name of da file. */
1212 da_file_name = XNEWVEC (char, len + strlen (GCOV_DATA_SUFFIX)
1213 + prefix_len + 2);
1215 if (profile_data_prefix)
1217 memcpy (da_file_name, profile_data_prefix, prefix_len);
1218 da_file_name[prefix_len++] = '/';
1220 memcpy (da_file_name + prefix_len, filename, len);
1221 strcpy (da_file_name + prefix_len + len, GCOV_DATA_SUFFIX);
1223 bbg_file_stamp = local_tick;
1225 if (flag_auto_profile)
1226 read_autofdo_file ();
1227 else if (flag_branch_probabilities)
1228 read_counts_file ();
1230 /* Name of bbg file. */
1231 if (flag_test_coverage && !flag_compare_debug)
1233 bbg_file_name = XNEWVEC (char, len + strlen (GCOV_NOTE_SUFFIX) + 1);
1234 memcpy (bbg_file_name, filename, len);
1235 strcpy (bbg_file_name + len, GCOV_NOTE_SUFFIX);
1237 if (!gcov_open (bbg_file_name, -1))
1239 error ("cannot open %s", bbg_file_name);
1240 bbg_file_name = NULL;
1242 else
1244 gcov_write_unsigned (GCOV_NOTE_MAGIC);
1245 gcov_write_unsigned (GCOV_VERSION);
1246 gcov_write_unsigned (bbg_file_stamp);
1250 g->get_dumps ()->dump_finish (profile_pass_num);
1253 /* Performs file-level cleanup. Close notes file, generate coverage
1254 variables and constructor. */
1256 void
1257 coverage_finish (void)
1259 if (bbg_file_name && gcov_close ())
1260 unlink (bbg_file_name);
1262 if (!flag_branch_probabilities && flag_test_coverage
1263 && (!local_tick || local_tick == (unsigned)-1))
1264 /* Only remove the da file, if we're emitting coverage code and
1265 cannot uniquely stamp it. If we can stamp it, libgcov will DTRT. */
1266 unlink (da_file_name);
1268 if (coverage_obj_init ())
1270 vec<constructor_elt, va_gc> *fn_ctor = NULL;
1271 struct coverage_data *fn;
1273 for (fn = functions_head; fn; fn = fn->next)
1274 fn_ctor = coverage_obj_fn (fn_ctor, fn->fn_decl, fn);
1275 coverage_obj_finish (fn_ctor);
1278 XDELETEVEC (da_file_name);
1279 da_file_name = NULL;
1282 #include "gt-coverage.h"