1 /* Read and write coverage files, and associated functionality.
2 Copyright (C) 1990-2022 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
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
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/>. */
29 #include "coretypes.h"
34 #include "tree-pass.h"
37 #include "stringpool.h"
40 #include "diagnostic-core.h"
41 #include "fold-const.h"
42 #include "stor-layout.h"
45 #include "langhooks.h"
46 #include "tree-iterator.h"
48 #include "pass_manager.h"
50 #include "auto-profile.h"
52 #include "diagnostic.h"
54 #include "file-prefix-map.h"
58 struct GTY((chain_next ("%h.next"))) coverage_data
60 struct coverage_data *next; /* next function */
61 unsigned ident; /* function ident */
62 unsigned lineno_checksum; /* function lineno checksum */
63 unsigned cfg_checksum; /* function cfg checksum */
64 tree fn_decl; /* the function decl */
65 tree ctr_vars[GCOV_COUNTERS]; /* counter variables. */
68 /* Counts information for a function. */
69 struct counts_entry : pointer_hash <counts_entry>
76 unsigned lineno_checksum;
77 unsigned cfg_checksum;
81 /* hash_table support. */
82 static inline hashval_t hash (const counts_entry *);
83 static int equal (const counts_entry *, const counts_entry *);
84 static void remove (counts_entry *);
87 static GTY(()) struct coverage_data *functions_head = 0;
88 static struct coverage_data **functions_tail = &functions_head;
89 static unsigned no_coverage = 0;
91 /* Cumulative counter information for whole program. */
92 static unsigned prg_ctr_mask; /* Mask of counter types generated. */
94 /* Counter information for current function. */
95 static unsigned fn_ctr_mask; /* Mask of counters used. */
96 static GTY(()) tree fn_v_ctrs[GCOV_COUNTERS]; /* counter variables. */
97 static unsigned fn_n_ctrs[GCOV_COUNTERS]; /* Counters allocated. */
98 static unsigned fn_b_ctrs[GCOV_COUNTERS]; /* Allocation base. */
100 /* Coverage info VAR_DECL and function info type nodes. */
101 static GTY(()) tree gcov_info_var;
102 static GTY(()) tree gcov_fn_info_type;
103 static GTY(()) tree gcov_fn_info_ptr_type;
105 /* Name of the notes (gcno) output file. The "bbg" prefix is for
106 historical reasons, when the notes file contained only the
107 basic block graph notes.
108 If this is NULL we're not writing to the notes file. */
109 static char *bbg_file_name;
111 /* File stamp for notes file. */
112 static unsigned bbg_file_stamp;
114 /* Name of the count data (gcda) file. */
115 static char *da_file_name;
117 /* The names of merge functions for counters. */
118 #define STR(str) #str
119 #define DEF_GCOV_COUNTER(COUNTER, NAME, FN_TYPE) STR(__gcov_merge ## FN_TYPE),
120 static const char *const ctr_merge_functions[GCOV_COUNTERS] = {
121 #include "gcov-counter.def"
123 #undef DEF_GCOV_COUNTER
126 #define DEF_GCOV_COUNTER(COUNTER, NAME, FN_TYPE) NAME,
127 static const char *const ctr_names[GCOV_COUNTERS] = {
128 #include "gcov-counter.def"
130 #undef DEF_GCOV_COUNTER
132 /* Forward declarations. */
133 static tree build_var (tree, tree, int);
135 /* Return the type node for gcov_type. */
141 = smallest_int_mode_for_size (targetm.gcov_type_size ());
142 return lang_hooks.types.type_for_mode (mode, false);
145 /* Return the type node for gcov_unsigned_t. */
148 get_gcov_unsigned_t (void)
150 scalar_int_mode mode = smallest_int_mode_for_size (32);
151 return lang_hooks.types.type_for_mode (mode, true);
155 counts_entry::hash (const counts_entry *entry)
157 return entry->ident * GCOV_COUNTERS + entry->ctr;
161 counts_entry::equal (const counts_entry *entry1, const counts_entry *entry2)
163 return entry1->ident == entry2->ident && entry1->ctr == entry2->ctr;
167 counts_entry::remove (counts_entry *entry)
169 free (entry->counts);
173 /* Hash table of count data. */
174 static hash_table<counts_entry> *counts_hash;
176 /* Read in the counts file, if available. */
179 read_counts_file (void)
181 gcov_unsigned_t fn_ident = 0;
184 unsigned lineno_checksum = 0;
185 unsigned cfg_checksum = 0;
187 if (!gcov_open (da_file_name, 1))
190 if (!gcov_magic (gcov_read_unsigned (), GCOV_DATA_MAGIC))
192 warning (0, "%qs is not a gcov data file", da_file_name);
196 else if ((tag = gcov_read_unsigned ()) != GCOV_VERSION)
200 GCOV_UNSIGNED2STRING (v, tag);
201 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
203 warning (0, "%qs is version %q.*s, expected version %q.*s",
204 da_file_name, 4, v, 4, e);
209 /* Read the stamp, used for creating a generation count. */
210 tag = gcov_read_unsigned ();
211 bbg_file_stamp = crc32_unsigned (bbg_file_stamp, tag);
214 gcov_read_unsigned ();
216 counts_hash = new hash_table<counts_entry> (10);
217 while ((tag = gcov_read_unsigned ()))
219 gcov_unsigned_t length;
220 gcov_position_t offset;
222 length = gcov_read_unsigned ();
223 offset = gcov_position ();
224 if (tag == GCOV_TAG_FUNCTION)
228 fn_ident = gcov_read_unsigned ();
229 lineno_checksum = gcov_read_unsigned ();
230 cfg_checksum = gcov_read_unsigned ();
233 fn_ident = lineno_checksum = cfg_checksum = 0;
235 else if (tag == GCOV_TAG_OBJECT_SUMMARY)
237 profile_info = XCNEW (gcov_summary);
238 profile_info->runs = gcov_read_unsigned ();
239 profile_info->sum_max = gcov_read_unsigned ();
241 else if (GCOV_TAG_IS_COUNTER (tag) && fn_ident)
243 counts_entry **slot, *entry, elt;
244 int read_length = (int)length;
245 length = read_length > 0 ? read_length : 0;
246 unsigned n_counts = GCOV_TAG_COUNTER_NUM (abs (read_length));
249 elt.ident = fn_ident;
250 elt.ctr = GCOV_COUNTER_FOR_TAG (tag);
252 slot = counts_hash->find_slot (&elt, INSERT);
256 *slot = entry = XCNEW (counts_entry);
257 entry->ident = fn_ident;
258 entry->ctr = elt.ctr;
259 entry->lineno_checksum = lineno_checksum;
260 entry->cfg_checksum = cfg_checksum;
261 entry->counts = XCNEWVEC (gcov_type, n_counts);
262 entry->n_counts = n_counts;
264 else if (entry->lineno_checksum != lineno_checksum
265 || entry->cfg_checksum != cfg_checksum)
267 error ("profile data for function %u is corrupted", fn_ident);
268 error ("checksum is (%x,%x) instead of (%x,%x)",
269 entry->lineno_checksum, entry->cfg_checksum,
270 lineno_checksum, cfg_checksum);
276 for (ix = 0; ix != n_counts; ix++)
277 entry->counts[ix] = gcov_read_counter ();
279 gcov_sync (offset, length);
280 if ((is_error = gcov_is_error ()))
283 ? G_("%qs has overflowed")
284 : G_("%qs is corrupted"),
295 /* Returns the counters for a particular tag. */
298 get_coverage_counts (unsigned counter, unsigned cfg_checksum,
299 unsigned lineno_checksum, unsigned int n_counts)
301 counts_entry *entry, elt;
303 /* No hash table, no counts. */
306 static int warned = 0;
310 warning (OPT_Wmissing_profile,
311 "%qs profile count data file not found",
313 if (dump_enabled_p ())
315 dump_user_location_t loc
316 = dump_user_location_t::from_location_t (input_location);
317 dump_printf_loc (MSG_MISSED_OPTIMIZATION, loc,
318 "file %s not found, %s\n", da_file_name,
319 (flag_guess_branch_prob
320 ? "execution counts estimated"
321 : "execution counts assumed to be zero"));
326 if (param_profile_func_internal_id)
327 elt.ident = current_function_funcdef_no + 1;
330 gcc_assert (coverage_node_map_initialized_p ());
331 elt.ident = cgraph_node::get (current_function_decl)->profile_id;
334 entry = counts_hash->find (&elt);
337 if (counter == GCOV_COUNTER_ARCS)
338 warning_at (DECL_SOURCE_LOCATION (current_function_decl),
339 OPT_Wmissing_profile,
340 "profile for function %qD not found in profile data",
341 current_function_decl);
342 /* The function was not emitted, or is weak and not chosen in the
343 final executable. Silently fail, because there's nothing we
348 if (entry->cfg_checksum != cfg_checksum
349 || (counter != GCOV_COUNTER_V_INDIR
350 && counter != GCOV_COUNTER_V_TOPN
351 && entry->n_counts != n_counts))
353 static int warned = 0;
354 bool warning_printed = false;
356 if (entry->n_counts != n_counts)
358 warning_at (DECL_SOURCE_LOCATION (current_function_decl),
359 OPT_Wcoverage_mismatch,
360 "number of counters in profile data for function %qD "
362 "its profile data (counter %qs, expected %i and have %i)",
363 current_function_decl,
364 ctr_names[counter], entry->n_counts, n_counts);
367 warning_at (DECL_SOURCE_LOCATION (current_function_decl),
368 OPT_Wcoverage_mismatch,
369 "the control flow of function %qD does not match "
370 "its profile data (counter %qs)", current_function_decl,
372 if (warning_printed && dump_enabled_p ())
374 dump_user_location_t loc
375 = dump_user_location_t::from_function_decl (current_function_decl);
376 dump_printf_loc (MSG_MISSED_OPTIMIZATION, loc,
377 "use -Wno-error=coverage-mismatch to tolerate "
378 "the mismatch but performance may drop if the "
379 "function is hot\n");
384 dump_printf_loc (MSG_MISSED_OPTIMIZATION, loc,
385 "coverage mismatch ignored\n");
386 dump_printf (MSG_MISSED_OPTIMIZATION,
387 flag_guess_branch_prob
388 ? G_("execution counts estimated\n")
389 : G_("execution counts assumed to be zero\n"));
390 if (!flag_guess_branch_prob)
391 dump_printf (MSG_MISSED_OPTIMIZATION,
392 "this can result in poorly optimized code\n");
398 else if (entry->lineno_checksum != lineno_checksum)
400 warning_at (DECL_SOURCE_LOCATION (current_function_decl),
401 OPT_Wcoverage_mismatch,
402 "source locations for function %qD have changed,"
403 " the profile data may be out of date",
404 current_function_decl);
407 return entry->counts;
410 /* Allocate NUM counters of type COUNTER. Returns nonzero if the
411 allocation succeeded. */
414 coverage_counter_alloc (unsigned counter, unsigned num)
422 if (!fn_v_ctrs[counter])
424 tree array_type = build_array_type (get_gcov_type (), NULL_TREE);
427 = build_var (current_function_decl, array_type, counter);
430 fn_b_ctrs[counter] = fn_n_ctrs[counter];
431 fn_n_ctrs[counter] += num;
433 fn_ctr_mask |= 1 << counter;
437 /* Generate a tree to access COUNTER NO. */
440 tree_coverage_counter_ref (unsigned counter, unsigned no)
442 tree gcov_type_node = get_gcov_type ();
444 gcc_assert (no < fn_n_ctrs[counter] - fn_b_ctrs[counter]);
446 no += fn_b_ctrs[counter];
448 /* "no" here is an array index, scaled to bytes later. */
449 return build4 (ARRAY_REF, gcov_type_node, fn_v_ctrs[counter],
450 build_int_cst (integer_type_node, no), NULL, NULL);
453 /* Generate a tree to access the address of COUNTER NO. */
456 tree_coverage_counter_addr (unsigned counter, unsigned no)
458 tree gcov_type_node = get_gcov_type ();
460 gcc_assert (no < fn_n_ctrs[counter] - fn_b_ctrs[counter]);
461 no += fn_b_ctrs[counter];
463 /* "no" here is an array index, scaled to bytes later. */
464 return build_fold_addr_expr (build4 (ARRAY_REF, gcov_type_node,
466 build_int_cst (integer_type_node, no),
471 /* Generate a checksum for a string. CHKSUM is the current
475 coverage_checksum_string (unsigned chksum, const char *string)
480 /* Look for everything that looks if it were produced by
481 get_file_function_name and zero out the second part
482 that may result from flag_random_seed. This is not critical
483 as the checksums are used only for sanity checking. */
484 for (i = 0; string[i]; i++)
487 if (startswith (string + i, "_GLOBAL__N_"))
489 if (startswith (string + i, "_GLOBAL__"))
492 /* C++ namespaces do have scheme:
493 _GLOBAL__N_<filename>_<wrongmagicnumber>_<magicnumber>functionname
494 since filename might contain extra underscores there seems
495 to be no better chance then walk all possible offsets looking
499 for (i = i + offset; string[i]; i++)
504 for (y = 1; y < 9; y++)
505 if (!(string[i + y] >= '0' && string[i + y] <= '9')
506 && !(string[i + y] >= 'A' && string[i + y] <= 'F'))
508 if (y != 9 || string[i + 9] != '_')
510 for (y = 10; y < 18; y++)
511 if (!(string[i + y] >= '0' && string[i + y] <= '9')
512 && !(string[i + y] >= 'A' && string[i + y] <= 'F'))
517 string = dup = xstrdup (string);
518 for (y = 10; y < 18; y++)
525 chksum = crc32_string (chksum, string);
531 /* Compute checksum for the current function. We generate a CRC32. */
534 coverage_compute_lineno_checksum (void)
536 expanded_location xloc
537 = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
538 unsigned chksum = xloc.line;
541 chksum = coverage_checksum_string (chksum, xloc.file);
542 chksum = coverage_checksum_string
543 (chksum, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl)));
548 /* Compute profile ID. This is better to be unique in whole program. */
551 coverage_compute_profile_id (struct cgraph_node *n)
555 /* Externally visible symbols have unique name. */
556 if (TREE_PUBLIC (n->decl) || DECL_EXTERNAL (n->decl) || n->unique_name)
558 chksum = coverage_checksum_string
559 (0, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n->decl)));
563 expanded_location xloc
564 = expand_location (DECL_SOURCE_LOCATION (n->decl));
565 bool use_name_only = (param_profile_func_internal_id == 0);
567 chksum = (use_name_only ? 0 : xloc.line);
569 chksum = coverage_checksum_string (chksum, xloc.file);
570 chksum = coverage_checksum_string
571 (chksum, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n->decl)));
572 if (!use_name_only && first_global_object_name)
573 chksum = coverage_checksum_string
574 (chksum, first_global_object_name);
575 char *base_name = xstrdup (aux_base_name);
576 if (endswith (base_name, ".gk"))
577 base_name[strlen (base_name) - 3] = '\0';
578 chksum = coverage_checksum_string (chksum, base_name);
582 /* Non-negative integers are hopefully small enough to fit in all targets.
583 Gcov file formats wants non-zero function IDs. */
584 chksum = chksum & 0x7fffffff;
585 return chksum + (!chksum);
588 /* Compute cfg checksum for the function FN given as argument.
589 The checksum is calculated carefully so that
590 source code changes that doesn't affect the control flow graph
591 won't change the checksum.
592 This is to make the profile data useable across source code change.
593 The downside of this is that the compiler may use potentially
594 wrong profile data - that the source code change has non-trivial impact
595 on the validity of profile data (e.g. the reversed condition)
596 but the compiler won't detect the change and use the wrong profile data. */
599 coverage_compute_cfg_checksum (struct function *fn)
602 unsigned chksum = n_basic_blocks_for_fn (fn);
604 FOR_EACH_BB_FN (bb, fn)
608 chksum = crc32_byte (chksum, bb->index);
609 FOR_EACH_EDGE (e, ei, bb->succs)
611 chksum = crc32_byte (chksum, e->dest->index);
618 /* Begin output to the notes file for the current function.
619 Writes the function header. Returns nonzero if data should be output. */
622 coverage_begin_function (unsigned lineno_checksum, unsigned cfg_checksum)
624 /* We don't need to output .gcno file unless we're under -ftest-coverage
625 (e.g. -fprofile-arcs/generate/use don't need .gcno to work). */
626 if (no_coverage || !bbg_file_name)
629 expanded_location startloc
630 = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
632 /* Announce function */
633 unsigned long offset = gcov_write_tag (GCOV_TAG_FUNCTION);
634 if (param_profile_func_internal_id)
635 gcov_write_unsigned (current_function_funcdef_no + 1);
638 gcc_assert (coverage_node_map_initialized_p ());
639 gcov_write_unsigned (
640 cgraph_node::get (current_function_decl)->profile_id);
643 gcov_write_unsigned (lineno_checksum);
644 gcov_write_unsigned (cfg_checksum);
645 gcov_write_string (IDENTIFIER_POINTER
646 (DECL_ASSEMBLER_NAME (current_function_decl)));
647 gcov_write_unsigned (DECL_ARTIFICIAL (current_function_decl)
648 && !DECL_FUNCTION_VERSIONED (current_function_decl)
649 && !DECL_LAMBDA_FUNCTION_P (current_function_decl));
650 gcov_write_filename (remap_profile_filename (startloc.file));
651 gcov_write_unsigned (startloc.line);
652 gcov_write_unsigned (startloc.column);
654 expanded_location endloc = expand_location (cfun->function_end_locus);
656 /* Function can start in a single file and end in another one. */
658 = endloc.file == startloc.file ? endloc.line : startloc.line;
660 = endloc.file == startloc.file ? endloc.column: startloc.column;
662 if (startloc.line > end_line)
664 warning_at (DECL_SOURCE_LOCATION (current_function_decl),
665 OPT_Wcoverage_invalid_line_number,
666 "function starts on a higher line number than it ends");
667 end_line = startloc.line;
668 end_column = startloc.column;
671 gcov_write_unsigned (end_line);
672 gcov_write_unsigned (end_column);
673 gcov_write_length (offset);
675 return !gcov_is_error ();
678 /* Finish coverage data for the current function. Verify no output
679 error has occurred. Save function coverage counts. */
682 coverage_end_function (unsigned lineno_checksum, unsigned cfg_checksum)
686 if (bbg_file_name && gcov_is_error ())
688 warning (0, "error writing %qs", bbg_file_name);
689 unlink (bbg_file_name);
690 bbg_file_name = NULL;
695 struct coverage_data *item = 0;
697 item = ggc_alloc<coverage_data> ();
699 if (param_profile_func_internal_id)
700 item->ident = current_function_funcdef_no + 1;
703 gcc_assert (coverage_node_map_initialized_p ());
704 item->ident = cgraph_node::get (cfun->decl)->profile_id;
707 item->lineno_checksum = lineno_checksum;
708 item->cfg_checksum = cfg_checksum;
710 item->fn_decl = current_function_decl;
712 *functions_tail = item;
713 functions_tail = &item->next;
715 for (i = 0; i != GCOV_COUNTERS; i++)
717 tree var = fn_v_ctrs[i];
720 item->ctr_vars[i] = var;
723 tree array_type = build_index_type (size_int (fn_n_ctrs[i] - 1));
724 array_type = build_array_type (get_gcov_type (), array_type);
725 TREE_TYPE (var) = array_type;
726 DECL_SIZE (var) = TYPE_SIZE (array_type);
727 DECL_SIZE_UNIT (var) = TYPE_SIZE_UNIT (array_type);
728 varpool_node::finalize_decl (var);
731 fn_b_ctrs[i] = fn_n_ctrs[i] = 0;
732 fn_v_ctrs[i] = NULL_TREE;
734 prg_ctr_mask |= fn_ctr_mask;
739 /* Remove coverage file if opened. */
742 coverage_remove_note_file (void)
747 unlink (bbg_file_name);
751 /* Build a coverage variable of TYPE for function FN_DECL. If COUNTER
752 >= 0 it is a counter array, otherwise it is the function structure. */
755 build_var (tree fn_decl, tree type, int counter)
757 tree var = build_decl (BUILTINS_LOCATION, VAR_DECL, NULL_TREE, type);
758 const char *fn_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fn_decl));
760 size_t fn_name_len, len;
762 fn_name = targetm.strip_name_encoding (fn_name);
763 fn_name_len = strlen (fn_name);
764 buf = XALLOCAVEC (char, fn_name_len + 8 + sizeof (int) * 3);
767 strcpy (buf, "__gcov__");
769 sprintf (buf, "__gcov%u_", counter);
771 buf[len - 1] = symbol_table::symbol_suffix_separator ();
772 memcpy (buf + len, fn_name, fn_name_len + 1);
773 DECL_NAME (var) = get_identifier (buf);
774 TREE_STATIC (var) = 1;
775 TREE_ADDRESSABLE (var) = 1;
776 DECL_NONALIASED (var) = 1;
777 SET_DECL_ALIGN (var, TYPE_ALIGN (type));
782 /* Creates the gcov_fn_info RECORD_TYPE. */
785 build_fn_info_type (tree type, unsigned counters, tree gcov_info_type)
787 tree ctr_info = lang_hooks.types.make_type (RECORD_TYPE);
791 gcc_assert (counters);
794 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
795 get_gcov_unsigned_t ());
798 /* ctr_info::values */
799 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
800 build_pointer_type (get_gcov_type ()));
801 DECL_CHAIN (field) = fields;
804 finish_builtin_struct (ctr_info, "__gcov_ctr_info", fields, NULL_TREE);
807 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
808 build_pointer_type (build_qualified_type
809 (gcov_info_type, TYPE_QUAL_CONST)));
813 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
814 get_gcov_unsigned_t ());
815 DECL_CHAIN (field) = fields;
818 /* lineno_checksum */
819 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
820 get_gcov_unsigned_t ());
821 DECL_CHAIN (field) = fields;
825 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
826 get_gcov_unsigned_t ());
827 DECL_CHAIN (field) = fields;
830 array_type = build_index_type (size_int (counters - 1));
831 array_type = build_array_type (ctr_info, array_type);
834 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE, array_type);
835 DECL_CHAIN (field) = fields;
838 finish_builtin_struct (type, "__gcov_fn_info", fields, NULL_TREE);
841 /* Returns a CONSTRUCTOR for a gcov_fn_info. DATA is
842 the coverage data for the function and TYPE is the gcov_fn_info
843 RECORD_TYPE. KEY is the object file key. */
846 build_fn_info (const struct coverage_data *data, tree type, tree key)
848 tree fields = TYPE_FIELDS (type);
851 vec<constructor_elt, va_gc> *v1 = NULL;
852 vec<constructor_elt, va_gc> *v2 = NULL;
855 CONSTRUCTOR_APPEND_ELT (v1, fields,
856 build1 (ADDR_EXPR, TREE_TYPE (fields), key));
857 fields = DECL_CHAIN (fields);
860 CONSTRUCTOR_APPEND_ELT (v1, fields,
861 build_int_cstu (get_gcov_unsigned_t (),
863 fields = DECL_CHAIN (fields);
865 /* lineno_checksum */
866 CONSTRUCTOR_APPEND_ELT (v1, fields,
867 build_int_cstu (get_gcov_unsigned_t (),
868 data->lineno_checksum));
869 fields = DECL_CHAIN (fields);
872 CONSTRUCTOR_APPEND_ELT (v1, fields,
873 build_int_cstu (get_gcov_unsigned_t (),
874 data->cfg_checksum));
875 fields = DECL_CHAIN (fields);
878 ctr_type = TREE_TYPE (TREE_TYPE (fields));
879 for (ix = 0; ix != GCOV_COUNTERS; ix++)
880 if (prg_ctr_mask & (1 << ix))
882 vec<constructor_elt, va_gc> *ctr = NULL;
883 tree var = data->ctr_vars[ix];
888 = tree_to_shwi (TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (var))))
891 CONSTRUCTOR_APPEND_ELT (ctr, TYPE_FIELDS (ctr_type),
892 build_int_cstu (get_gcov_unsigned_t (),
896 CONSTRUCTOR_APPEND_ELT (ctr, DECL_CHAIN (TYPE_FIELDS (ctr_type)),
897 build_fold_addr_expr (var));
899 CONSTRUCTOR_APPEND_ELT (v2, NULL, build_constructor (ctr_type, ctr));
902 CONSTRUCTOR_APPEND_ELT (v1, fields,
903 build_constructor (TREE_TYPE (fields), v2));
905 return build_constructor (type, v1);
908 /* Create gcov_info struct. TYPE is the incomplete RECORD_TYPE to be
909 completed, and FN_INFO_PTR_TYPE is a pointer to the function info type. */
912 build_info_type (tree type, tree fn_info_ptr_type)
914 tree field, fields = NULL_TREE;
918 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
919 get_gcov_unsigned_t ());
920 DECL_CHAIN (field) = fields;
924 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
925 build_pointer_type (build_qualified_type
926 (type, TYPE_QUAL_CONST)));
927 DECL_CHAIN (field) = fields;
931 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
932 get_gcov_unsigned_t ());
933 DECL_CHAIN (field) = fields;
937 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
938 get_gcov_unsigned_t ());
939 DECL_CHAIN (field) = fields;
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;
951 = build_function_type_list (void_type_node,
952 build_pointer_type (get_gcov_type ()),
953 get_gcov_unsigned_t (), NULL_TREE);
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,
959 DECL_CHAIN (field) = fields;
963 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
964 get_gcov_unsigned_t ());
965 DECL_CHAIN (field) = fields;
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,
973 DECL_CHAIN (field) = fields;
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. */
984 build_info (tree info_type, tree fn_ary, unsigned object_checksum)
986 tree info_fields = TYPE_FIELDS (info_type);
987 tree merge_fn_type, n_funcs;
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;
995 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
996 build_int_cstu (TREE_TYPE (info_fields),
998 info_fields = DECL_CHAIN (info_fields);
1001 CONSTRUCTOR_APPEND_ELT (v1, info_fields, null_pointer_node);
1002 info_fields = DECL_CHAIN (info_fields);
1005 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
1006 build_int_cstu (TREE_TYPE (info_fields),
1008 info_fields = DECL_CHAIN (info_fields);
1011 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
1012 build_int_cstu (TREE_TYPE (info_fields),
1014 info_fields = DECL_CHAIN (info_fields);
1017 da_file_name_len = strlen (da_file_name);
1018 filename_string = build_string (da_file_name_len + 1, da_file_name);
1019 TREE_TYPE (filename_string) = build_array_type
1020 (char_type_node, build_index_type (size_int (da_file_name_len)));
1021 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
1022 build1 (ADDR_EXPR, TREE_TYPE (info_fields),
1024 info_fields = DECL_CHAIN (info_fields);
1026 /* merge fn array -- NULL slots indicate unmeasured counters */
1027 merge_fn_type = TREE_TYPE (TREE_TYPE (info_fields));
1028 for (ix = 0; ix != GCOV_COUNTERS; ix++)
1030 tree ptr = null_pointer_node;
1032 if ((1u << ix) & prg_ctr_mask)
1034 tree merge_fn = build_decl (BUILTINS_LOCATION,
1036 get_identifier (ctr_merge_functions[ix]),
1037 TREE_TYPE (merge_fn_type));
1038 DECL_EXTERNAL (merge_fn) = 1;
1039 TREE_PUBLIC (merge_fn) = 1;
1040 DECL_ARTIFICIAL (merge_fn) = 1;
1041 TREE_NOTHROW (merge_fn) = 1;
1042 /* Initialize assembler name so we can stream out. */
1043 DECL_ASSEMBLER_NAME (merge_fn);
1044 ptr = build1 (ADDR_EXPR, merge_fn_type, merge_fn);
1046 CONSTRUCTOR_APPEND_ELT (v2, NULL, ptr);
1048 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
1049 build_constructor (TREE_TYPE (info_fields), v2));
1050 info_fields = DECL_CHAIN (info_fields);
1053 n_funcs = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (fn_ary)));
1054 n_funcs = fold_build2 (PLUS_EXPR, TREE_TYPE (info_fields),
1055 n_funcs, size_one_node);
1056 CONSTRUCTOR_APPEND_ELT (v1, info_fields, n_funcs);
1057 info_fields = DECL_CHAIN (info_fields);
1060 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
1061 build1 (ADDR_EXPR, TREE_TYPE (info_fields), fn_ary));
1062 info_fields = DECL_CHAIN (info_fields);
1064 gcc_assert (!info_fields);
1065 return build_constructor (info_type, v1);
1068 /* Generate the constructor function to call __gcov_init. */
1071 build_init_ctor (tree gcov_info_type)
1073 tree ctor, stmt, init_fn;
1075 /* Build a decl for __gcov_init. */
1076 init_fn = build_pointer_type (gcov_info_type);
1077 init_fn = build_function_type_list (void_type_node, init_fn, NULL);
1078 init_fn = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
1079 get_identifier ("__gcov_init"), init_fn);
1080 TREE_PUBLIC (init_fn) = 1;
1081 DECL_EXTERNAL (init_fn) = 1;
1082 DECL_ASSEMBLER_NAME (init_fn);
1084 /* Generate a call to __gcov_init(&gcov_info). */
1086 stmt = build_fold_addr_expr (gcov_info_var);
1087 stmt = build_call_expr (init_fn, 1, stmt);
1088 append_to_statement_list (stmt, &ctor);
1090 /* Generate a constructor to run it. */
1091 int priority = SUPPORTS_INIT_PRIORITY
1092 ? MAX_RESERVED_INIT_PRIORITY: DEFAULT_INIT_PRIORITY;
1093 cgraph_build_static_cdtor ('I', ctor, priority);
1096 /* Generate the destructor function to call __gcov_exit. */
1099 build_gcov_exit_decl (void)
1101 tree init_fn = build_function_type_list (void_type_node, NULL);
1102 init_fn = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
1103 get_identifier ("__gcov_exit"), init_fn);
1104 TREE_PUBLIC (init_fn) = 1;
1105 DECL_EXTERNAL (init_fn) = 1;
1106 DECL_ASSEMBLER_NAME (init_fn);
1108 /* Generate a call to __gcov_exit (). */
1110 tree stmt = build_call_expr (init_fn, 0);
1111 append_to_statement_list (stmt, &dtor);
1113 /* Generate a destructor to run it. */
1114 int priority = SUPPORTS_INIT_PRIORITY
1115 ? MAX_RESERVED_INIT_PRIORITY: DEFAULT_INIT_PRIORITY;
1117 cgraph_build_static_cdtor ('D', dtor, priority);
1120 /* Generate the pointer to the gcov_info_var in a dedicated section. */
1123 build_gcov_info_var_registration (tree gcov_info_type)
1125 tree var = build_decl (BUILTINS_LOCATION,
1126 VAR_DECL, NULL_TREE,
1127 build_pointer_type (gcov_info_type));
1128 TREE_STATIC (var) = 1;
1129 TREE_READONLY (var) = 1;
1131 ASM_GENERATE_INTERNAL_LABEL (name_buf, "LPBX", 2);
1132 DECL_NAME (var) = get_identifier (name_buf);
1133 get_section (profile_info_section, SECTION_UNNAMED, NULL);
1134 set_decl_section_name (var, profile_info_section);
1135 mark_decl_referenced (var);
1136 DECL_INITIAL (var) = build_fold_addr_expr (gcov_info_var);
1137 varpool_node::finalize_decl (var);
1140 /* Create the gcov_info types and object. Generate the constructor
1141 function to call __gcov_init. Does not generate the initializer
1142 for the object. Returns TRUE if coverage data is being emitted. */
1145 coverage_obj_init (void)
1147 tree gcov_info_type;
1148 unsigned n_counters = 0;
1150 struct coverage_data *fn;
1151 struct coverage_data **fn_prev;
1154 no_coverage = 1; /* Disable any further coverage. */
1159 if (symtab->dump_file)
1160 fprintf (symtab->dump_file, "Using data file %s\n", da_file_name);
1162 /* Prune functions. */
1163 for (fn_prev = &functions_head; (fn = *fn_prev);)
1164 if (DECL_STRUCT_FUNCTION (fn->fn_decl))
1165 fn_prev = &fn->next;
1167 /* The function is not being emitted, remove from list. */
1168 *fn_prev = fn->next;
1170 if (functions_head == NULL)
1173 for (ix = 0; ix != GCOV_COUNTERS; ix++)
1174 if ((1u << ix) & prg_ctr_mask)
1177 /* Build the info and fn_info types. These are mutually recursive. */
1178 gcov_info_type = lang_hooks.types.make_type (RECORD_TYPE);
1179 gcov_fn_info_type = lang_hooks.types.make_type (RECORD_TYPE);
1180 build_fn_info_type (gcov_fn_info_type, n_counters, gcov_info_type);
1181 gcov_info_type = lang_hooks.types.make_type (RECORD_TYPE);
1182 gcov_fn_info_ptr_type = build_pointer_type
1183 (build_qualified_type (gcov_fn_info_type, TYPE_QUAL_CONST));
1184 build_info_type (gcov_info_type, gcov_fn_info_ptr_type);
1186 /* Build the gcov info var, this is referred to in its own
1188 gcov_info_var = build_decl (BUILTINS_LOCATION,
1189 VAR_DECL, NULL_TREE, gcov_info_type);
1190 TREE_STATIC (gcov_info_var) = 1;
1191 ASM_GENERATE_INTERNAL_LABEL (name_buf, "LPBX", 0);
1192 DECL_NAME (gcov_info_var) = get_identifier (name_buf);
1194 if (profile_info_section)
1195 build_gcov_info_var_registration (gcov_info_type);
1198 build_init_ctor (gcov_info_type);
1199 build_gcov_exit_decl ();
1205 /* Generate the coverage function info for FN and DATA. Append a
1206 pointer to that object to CTOR and return the appended CTOR. */
1208 static vec<constructor_elt, va_gc> *
1209 coverage_obj_fn (vec<constructor_elt, va_gc> *ctor, tree fn,
1210 struct coverage_data const *data)
1212 tree init = build_fn_info (data, gcov_fn_info_type, gcov_info_var);
1213 tree var = build_var (fn, gcov_fn_info_type, -1);
1215 DECL_INITIAL (var) = init;
1216 varpool_node::finalize_decl (var);
1218 CONSTRUCTOR_APPEND_ELT (ctor, NULL,
1219 build1 (ADDR_EXPR, gcov_fn_info_ptr_type, var));
1223 /* Finalize the coverage data. Generates the array of pointers to
1224 function objects from CTOR. Generate the gcov_info initializer. */
1227 coverage_obj_finish (vec<constructor_elt, va_gc> *ctor,
1228 unsigned object_checksum)
1230 unsigned n_functions = vec_safe_length (ctor);
1231 tree fn_info_ary_type = build_array_type
1232 (build_qualified_type (gcov_fn_info_ptr_type, TYPE_QUAL_CONST),
1233 build_index_type (size_int (n_functions - 1)));
1234 tree fn_info_ary = build_decl (BUILTINS_LOCATION, VAR_DECL, NULL_TREE,
1238 TREE_STATIC (fn_info_ary) = 1;
1239 ASM_GENERATE_INTERNAL_LABEL (name_buf, "LPBX", 1);
1240 DECL_NAME (fn_info_ary) = get_identifier (name_buf);
1241 DECL_INITIAL (fn_info_ary) = build_constructor (fn_info_ary_type, ctor);
1242 varpool_node::finalize_decl (fn_info_ary);
1244 DECL_INITIAL (gcov_info_var)
1245 = build_info (TREE_TYPE (gcov_info_var), fn_info_ary, object_checksum);
1246 varpool_node::finalize_decl (gcov_info_var);
1249 /* Perform file-level initialization. Read in data file, generate name
1253 coverage_init (const char *filename)
1255 const char *original_filename = filename;
1256 int original_len = strlen (original_filename);
1257 #if HAVE_DOS_BASED_FILE_SYSTEM
1258 const char *separator = "\\";
1260 const char *separator = "/";
1262 int len = strlen (filename);
1265 /* Since coverage_init is invoked very early, before the pass
1266 manager, we need to set up the dumping explicitly. This is
1267 similar to the handling in finish_optimization_passes. */
1268 int profile_pass_num =
1269 g->get_passes ()->get_pass_profile ()->static_pass_number;
1270 g->get_dumps ()->dump_start (profile_pass_num, NULL);
1272 if (!IS_ABSOLUTE_PATH (filename))
1274 /* When a profile_data_prefix is provided, then mangle full path
1275 of filename in order to prevent file path clashing. */
1276 if (profile_data_prefix)
1278 filename = concat (getpwd (), separator, filename, NULL);
1279 if (profile_prefix_path)
1281 if (startswith (filename, profile_prefix_path))
1283 filename += strlen (profile_prefix_path);
1284 while (*filename == *separator)
1288 warning (0, "filename %qs does not start with profile "
1289 "prefix %qs", filename, profile_prefix_path);
1291 filename = mangle_path (filename);
1292 len = strlen (filename);
1295 profile_data_prefix = getpwd ();
1298 if (profile_data_prefix)
1299 prefix_len = strlen (profile_data_prefix);
1301 /* Name of da file. */
1302 da_file_name = XNEWVEC (char, len + strlen (GCOV_DATA_SUFFIX)
1305 if (profile_data_prefix)
1307 memcpy (da_file_name, profile_data_prefix, prefix_len);
1308 da_file_name[prefix_len++] = *separator;
1310 memcpy (da_file_name + prefix_len, filename, len);
1311 strcpy (da_file_name + prefix_len + len, GCOV_DATA_SUFFIX);
1313 bbg_file_stamp = local_tick;
1314 if (flag_auto_profile)
1315 read_autofdo_file ();
1316 else if (flag_branch_probabilities)
1317 read_counts_file ();
1319 /* Name of bbg file. */
1320 if (flag_test_coverage && !flag_compare_debug)
1322 if (profile_note_location)
1323 bbg_file_name = xstrdup (profile_note_location);
1326 bbg_file_name = XNEWVEC (char, original_len + strlen (GCOV_NOTE_SUFFIX) + 1);
1327 memcpy (bbg_file_name, original_filename, original_len);
1328 strcpy (bbg_file_name + original_len, GCOV_NOTE_SUFFIX);
1331 if (!gcov_open (bbg_file_name, -1))
1333 error ("cannot open %s", bbg_file_name);
1334 bbg_file_name = NULL;
1338 gcov_write_unsigned (GCOV_NOTE_MAGIC);
1339 gcov_write_unsigned (GCOV_VERSION);
1340 gcov_write_unsigned (bbg_file_stamp);
1341 /* Use an arbitrary checksum */
1342 gcov_write_unsigned (0);
1343 gcov_write_string (getpwd ());
1345 /* Do not support has_unexecuted_blocks for Ada. */
1346 gcov_write_unsigned (strcmp (lang_hooks.name, "GNU Ada") != 0);
1350 g->get_dumps ()->dump_finish (profile_pass_num);
1353 /* Performs file-level cleanup. Close notes file, generate coverage
1354 variables and constructor. */
1357 coverage_finish (void)
1359 if (bbg_file_name && gcov_close ())
1360 unlink (bbg_file_name);
1362 if (!flag_branch_probabilities && flag_test_coverage
1363 && (!local_tick || local_tick == (unsigned)-1))
1364 /* Only remove the da file, if we're emitting coverage code and
1365 cannot uniquely stamp it. If we can stamp it, libgcov will DTRT. */
1366 unlink (da_file_name);
1368 /* Global GCDA checksum that aggregates all functions. */
1369 unsigned object_checksum = 0;
1371 if (coverage_obj_init ())
1373 vec<constructor_elt, va_gc> *fn_ctor = NULL;
1374 struct coverage_data *fn;
1376 for (fn = functions_head; fn; fn = fn->next)
1378 fn_ctor = coverage_obj_fn (fn_ctor, fn->fn_decl, fn);
1380 object_checksum = crc32_unsigned (object_checksum, fn->ident);
1381 object_checksum = crc32_unsigned (object_checksum,
1382 fn->lineno_checksum);
1383 object_checksum = crc32_unsigned (object_checksum, fn->cfg_checksum);
1385 coverage_obj_finish (fn_ctor, object_checksum);
1388 XDELETEVEC (da_file_name);
1389 da_file_name = NULL;
1392 #include "gt-coverage.h"