c++: ICE on anon struct with base [PR96636]
[official-gcc.git] / gcc / coverage.c
blobac9a9fdad228271f121b3068519f7662bbd7e323
1 /* Read and write coverage files, and associated functionality.
2 Copyright (C) 1990-2021 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 "backend.h"
31 #include "target.h"
32 #include "rtl.h"
33 #include "tree.h"
34 #include "tree-pass.h"
35 #include "memmodel.h"
36 #include "tm_p.h"
37 #include "stringpool.h"
38 #include "cgraph.h"
39 #include "coverage.h"
40 #include "diagnostic-core.h"
41 #include "fold-const.h"
42 #include "stor-layout.h"
43 #include "output.h"
44 #include "toplev.h"
45 #include "langhooks.h"
46 #include "tree-iterator.h"
47 #include "context.h"
48 #include "pass_manager.h"
49 #include "intl.h"
50 #include "auto-profile.h"
51 #include "profile.h"
52 #include "diagnostic.h"
53 #include "varasm.h"
55 #include "gcov-io.c"
57 struct GTY((chain_next ("%h.next"))) coverage_data
59 struct coverage_data *next; /* next function */
60 unsigned ident; /* function ident */
61 unsigned lineno_checksum; /* function lineno checksum */
62 unsigned cfg_checksum; /* function cfg checksum */
63 tree fn_decl; /* the function decl */
64 tree ctr_vars[GCOV_COUNTERS]; /* counter variables. */
67 /* Counts information for a function. */
68 struct counts_entry : pointer_hash <counts_entry>
70 /* We hash by */
71 unsigned ident;
72 unsigned ctr;
74 /* Store */
75 unsigned lineno_checksum;
76 unsigned cfg_checksum;
77 gcov_type *counts;
78 unsigned n_counts;
80 /* hash_table support. */
81 static inline hashval_t hash (const counts_entry *);
82 static int equal (const counts_entry *, const counts_entry *);
83 static void remove (counts_entry *);
86 static GTY(()) struct coverage_data *functions_head = 0;
87 static struct coverage_data **functions_tail = &functions_head;
88 static unsigned no_coverage = 0;
90 /* Cumulative counter information for whole program. */
91 static unsigned prg_ctr_mask; /* Mask of counter types generated. */
93 /* Counter information for current function. */
94 static unsigned fn_ctr_mask; /* Mask of counters used. */
95 static GTY(()) tree fn_v_ctrs[GCOV_COUNTERS]; /* counter variables. */
96 static unsigned fn_n_ctrs[GCOV_COUNTERS]; /* Counters allocated. */
97 static unsigned fn_b_ctrs[GCOV_COUNTERS]; /* Allocation base. */
99 /* Coverage info VAR_DECL and function info type nodes. */
100 static GTY(()) tree gcov_info_var;
101 static GTY(()) tree gcov_fn_info_type;
102 static GTY(()) tree gcov_fn_info_ptr_type;
104 /* Name of the notes (gcno) output file. The "bbg" prefix is for
105 historical reasons, when the notes file contained only the
106 basic block graph notes.
107 If this is NULL we're not writing to the notes file. */
108 static char *bbg_file_name;
110 /* File stamp for notes file. */
111 static unsigned bbg_file_stamp;
113 /* Name of the count data (gcda) file. */
114 static char *da_file_name;
116 /* The names of merge functions for counters. */
117 #define STR(str) #str
118 #define DEF_GCOV_COUNTER(COUNTER, NAME, FN_TYPE) STR(__gcov_merge ## FN_TYPE),
119 static const char *const ctr_merge_functions[GCOV_COUNTERS] = {
120 #include "gcov-counter.def"
122 #undef DEF_GCOV_COUNTER
123 #undef STR
125 #define DEF_GCOV_COUNTER(COUNTER, NAME, FN_TYPE) NAME,
126 static const char *const ctr_names[GCOV_COUNTERS] = {
127 #include "gcov-counter.def"
129 #undef DEF_GCOV_COUNTER
131 /* Forward declarations. */
132 static void read_counts_file (void);
133 static tree build_var (tree, tree, int);
134 static void build_fn_info_type (tree, unsigned, tree);
135 static void build_info_type (tree, tree);
136 static tree build_fn_info (const struct coverage_data *, tree, tree);
137 static tree build_info (tree, tree);
138 static bool coverage_obj_init (void);
139 static vec<constructor_elt, va_gc> *coverage_obj_fn
140 (vec<constructor_elt, va_gc> *, tree, struct coverage_data const *);
141 static void coverage_obj_finish (vec<constructor_elt, va_gc> *);
143 /* Return the type node for gcov_type. */
145 tree
146 get_gcov_type (void)
148 scalar_int_mode mode
149 = smallest_int_mode_for_size (LONG_LONG_TYPE_SIZE > 32 ? 64 : 32);
150 return lang_hooks.types.type_for_mode (mode, false);
153 /* Return the type node for gcov_unsigned_t. */
155 static tree
156 get_gcov_unsigned_t (void)
158 scalar_int_mode mode = smallest_int_mode_for_size (32);
159 return lang_hooks.types.type_for_mode (mode, true);
162 inline hashval_t
163 counts_entry::hash (const counts_entry *entry)
165 return entry->ident * GCOV_COUNTERS + entry->ctr;
168 inline int
169 counts_entry::equal (const counts_entry *entry1, const counts_entry *entry2)
171 return entry1->ident == entry2->ident && entry1->ctr == entry2->ctr;
174 inline void
175 counts_entry::remove (counts_entry *entry)
177 free (entry->counts);
178 free (entry);
181 /* Hash table of count data. */
182 static hash_table<counts_entry> *counts_hash;
184 /* Read in the counts file, if available. */
186 static void
187 read_counts_file (void)
189 gcov_unsigned_t fn_ident = 0;
190 gcov_unsigned_t tag;
191 int is_error = 0;
192 unsigned lineno_checksum = 0;
193 unsigned cfg_checksum = 0;
195 if (!gcov_open (da_file_name, 1))
196 return;
198 if (!gcov_magic (gcov_read_unsigned (), GCOV_DATA_MAGIC))
200 warning (0, "%qs is not a gcov data file", da_file_name);
201 gcov_close ();
202 return;
204 else if ((tag = gcov_read_unsigned ()) != GCOV_VERSION)
206 char v[4], e[4];
208 GCOV_UNSIGNED2STRING (v, tag);
209 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
211 warning (0, "%qs is version %q.*s, expected version %q.*s",
212 da_file_name, 4, v, 4, e);
213 gcov_close ();
214 return;
217 /* Read the stamp, used for creating a generation count. */
218 tag = gcov_read_unsigned ();
219 bbg_file_stamp = crc32_unsigned (bbg_file_stamp, tag);
221 counts_hash = new hash_table<counts_entry> (10);
222 while ((tag = gcov_read_unsigned ()))
224 gcov_unsigned_t length;
225 gcov_position_t offset;
227 length = gcov_read_unsigned ();
228 offset = gcov_position ();
229 if (tag == GCOV_TAG_FUNCTION)
231 if (length)
233 fn_ident = gcov_read_unsigned ();
234 lineno_checksum = gcov_read_unsigned ();
235 cfg_checksum = gcov_read_unsigned ();
237 else
238 fn_ident = lineno_checksum = cfg_checksum = 0;
240 else if (tag == GCOV_TAG_OBJECT_SUMMARY)
242 profile_info = XCNEW (gcov_summary);
243 profile_info->runs = gcov_read_unsigned ();
244 profile_info->sum_max = gcov_read_unsigned ();
246 else if (GCOV_TAG_IS_COUNTER (tag) && fn_ident)
248 counts_entry **slot, *entry, elt;
249 int read_length = (int)length;
250 length = read_length > 0 ? read_length : 0;
251 unsigned n_counts = GCOV_TAG_COUNTER_NUM (abs (read_length));
252 unsigned ix;
254 elt.ident = fn_ident;
255 elt.ctr = GCOV_COUNTER_FOR_TAG (tag);
257 slot = counts_hash->find_slot (&elt, INSERT);
258 entry = *slot;
259 if (!entry)
261 *slot = entry = XCNEW (counts_entry);
262 entry->ident = fn_ident;
263 entry->ctr = elt.ctr;
264 entry->lineno_checksum = lineno_checksum;
265 entry->cfg_checksum = cfg_checksum;
266 entry->counts = XCNEWVEC (gcov_type, n_counts);
267 entry->n_counts = n_counts;
269 else if (entry->lineno_checksum != lineno_checksum
270 || entry->cfg_checksum != cfg_checksum)
272 error ("profile data for function %u is corrupted", fn_ident);
273 error ("checksum is (%x,%x) instead of (%x,%x)",
274 entry->lineno_checksum, entry->cfg_checksum,
275 lineno_checksum, cfg_checksum);
276 delete counts_hash;
277 counts_hash = NULL;
278 break;
280 if (read_length > 0)
281 for (ix = 0; ix != n_counts; ix++)
282 entry->counts[ix] = gcov_read_counter ();
284 gcov_sync (offset, length);
285 if ((is_error = gcov_is_error ()))
287 error (is_error < 0
288 ? G_("%qs has overflowed")
289 : G_("%qs is corrupted"),
290 da_file_name);
291 delete counts_hash;
292 counts_hash = NULL;
293 break;
297 gcov_close ();
300 /* Returns the counters for a particular tag. */
302 gcov_type *
303 get_coverage_counts (unsigned counter, unsigned cfg_checksum,
304 unsigned lineno_checksum, unsigned int n_counts)
306 counts_entry *entry, elt;
308 /* No hash table, no counts. */
309 if (!counts_hash)
311 static int warned = 0;
313 if (!warned++)
315 warning (OPT_Wmissing_profile,
316 "%qs profile count data file not found",
317 da_file_name);
318 if (dump_enabled_p ())
320 dump_user_location_t loc
321 = dump_user_location_t::from_location_t (input_location);
322 dump_printf_loc (MSG_MISSED_OPTIMIZATION, loc,
323 "file %s not found, %s\n", da_file_name,
324 (flag_guess_branch_prob
325 ? "execution counts estimated"
326 : "execution counts assumed to be zero"));
329 return NULL;
331 if (param_profile_func_internal_id)
332 elt.ident = current_function_funcdef_no + 1;
333 else
335 gcc_assert (coverage_node_map_initialized_p ());
336 elt.ident = cgraph_node::get (current_function_decl)->profile_id;
338 elt.ctr = counter;
339 entry = counts_hash->find (&elt);
340 if (!entry)
342 if (counter == GCOV_COUNTER_ARCS)
343 warning_at (DECL_SOURCE_LOCATION (current_function_decl),
344 OPT_Wmissing_profile,
345 "profile for function %qD not found in profile data",
346 current_function_decl);
347 /* The function was not emitted, or is weak and not chosen in the
348 final executable. Silently fail, because there's nothing we
349 can do about it. */
350 return NULL;
353 if (entry->cfg_checksum != cfg_checksum
354 || (counter != GCOV_COUNTER_V_INDIR
355 && counter != GCOV_COUNTER_V_TOPN
356 && entry->n_counts != n_counts))
358 static int warned = 0;
359 bool warning_printed = false;
361 if (entry->n_counts != n_counts)
362 warning_printed =
363 warning_at (DECL_SOURCE_LOCATION (current_function_decl),
364 OPT_Wcoverage_mismatch,
365 "number of counters in profile data for function %qD "
366 "does not match "
367 "its profile data (counter %qs, expected %i and have %i)",
368 current_function_decl,
369 ctr_names[counter], entry->n_counts, n_counts);
370 else
371 warning_printed =
372 warning_at (DECL_SOURCE_LOCATION (current_function_decl),
373 OPT_Wcoverage_mismatch,
374 "the control flow of function %qD does not match "
375 "its profile data (counter %qs)", current_function_decl,
376 ctr_names[counter]);
377 if (warning_printed && dump_enabled_p ())
379 dump_user_location_t loc
380 = dump_user_location_t::from_function_decl (current_function_decl);
381 dump_printf_loc (MSG_MISSED_OPTIMIZATION, loc,
382 "use -Wno-error=coverage-mismatch to tolerate "
383 "the mismatch but performance may drop if the "
384 "function is hot\n");
386 if (!seen_error ()
387 && !warned++)
389 dump_printf_loc (MSG_MISSED_OPTIMIZATION, loc,
390 "coverage mismatch ignored\n");
391 dump_printf (MSG_MISSED_OPTIMIZATION,
392 flag_guess_branch_prob
393 ? G_("execution counts estimated\n")
394 : G_("execution counts assumed to be zero\n"));
395 if (!flag_guess_branch_prob)
396 dump_printf (MSG_MISSED_OPTIMIZATION,
397 "this can result in poorly optimized code\n");
401 return NULL;
403 else if (entry->lineno_checksum != lineno_checksum)
405 warning_at (DECL_SOURCE_LOCATION (current_function_decl),
406 OPT_Wcoverage_mismatch,
407 "source locations for function %qD have changed,"
408 " the profile data may be out of date",
409 current_function_decl);
412 return entry->counts;
415 /* Allocate NUM counters of type COUNTER. Returns nonzero if the
416 allocation succeeded. */
419 coverage_counter_alloc (unsigned counter, unsigned num)
421 if (no_coverage)
422 return 0;
424 if (!num)
425 return 1;
427 if (!fn_v_ctrs[counter])
429 tree array_type = build_array_type (get_gcov_type (), NULL_TREE);
431 fn_v_ctrs[counter]
432 = build_var (current_function_decl, array_type, counter);
435 fn_b_ctrs[counter] = fn_n_ctrs[counter];
436 fn_n_ctrs[counter] += num;
438 fn_ctr_mask |= 1 << counter;
439 return 1;
442 /* Generate a tree to access COUNTER NO. */
444 tree
445 tree_coverage_counter_ref (unsigned counter, unsigned no)
447 tree gcov_type_node = get_gcov_type ();
449 gcc_assert (no < fn_n_ctrs[counter] - fn_b_ctrs[counter]);
451 no += fn_b_ctrs[counter];
453 /* "no" here is an array index, scaled to bytes later. */
454 return build4 (ARRAY_REF, gcov_type_node, fn_v_ctrs[counter],
455 build_int_cst (integer_type_node, no), NULL, NULL);
458 /* Generate a tree to access the address of COUNTER NO. */
460 tree
461 tree_coverage_counter_addr (unsigned counter, unsigned no)
463 tree gcov_type_node = get_gcov_type ();
465 gcc_assert (no < fn_n_ctrs[counter] - fn_b_ctrs[counter]);
466 no += fn_b_ctrs[counter];
468 /* "no" here is an array index, scaled to bytes later. */
469 return build_fold_addr_expr (build4 (ARRAY_REF, gcov_type_node,
470 fn_v_ctrs[counter],
471 build_int_cst (integer_type_node, no),
472 NULL, NULL));
476 /* Generate a checksum for a string. CHKSUM is the current
477 checksum. */
479 static unsigned
480 coverage_checksum_string (unsigned chksum, const char *string)
482 int i;
483 char *dup = NULL;
485 /* Look for everything that looks if it were produced by
486 get_file_function_name and zero out the second part
487 that may result from flag_random_seed. This is not critical
488 as the checksums are used only for sanity checking. */
489 for (i = 0; string[i]; i++)
491 int offset = 0;
492 if (startswith (string + i, "_GLOBAL__N_"))
493 offset = 11;
494 if (startswith (string + i, "_GLOBAL__"))
495 offset = 9;
497 /* C++ namespaces do have scheme:
498 _GLOBAL__N_<filename>_<wrongmagicnumber>_<magicnumber>functionname
499 since filename might contain extra underscores there seems
500 to be no better chance then walk all possible offsets looking
501 for magicnumber. */
502 if (offset)
504 for (i = i + offset; string[i]; i++)
505 if (string[i]=='_')
507 int y;
509 for (y = 1; y < 9; y++)
510 if (!(string[i + y] >= '0' && string[i + y] <= '9')
511 && !(string[i + y] >= 'A' && string[i + y] <= 'F'))
512 break;
513 if (y != 9 || string[i + 9] != '_')
514 continue;
515 for (y = 10; y < 18; y++)
516 if (!(string[i + y] >= '0' && string[i + y] <= '9')
517 && !(string[i + y] >= 'A' && string[i + y] <= 'F'))
518 break;
519 if (y != 18)
520 continue;
521 if (!dup)
522 string = dup = xstrdup (string);
523 for (y = 10; y < 18; y++)
524 dup[i + y] = '0';
526 break;
530 chksum = crc32_string (chksum, string);
531 free (dup);
533 return chksum;
536 /* Compute checksum for the current function. We generate a CRC32. */
538 unsigned
539 coverage_compute_lineno_checksum (void)
541 expanded_location xloc
542 = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
543 unsigned chksum = xloc.line;
545 if (xloc.file)
546 chksum = coverage_checksum_string (chksum, xloc.file);
547 chksum = coverage_checksum_string
548 (chksum, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl)));
550 return chksum;
553 /* Compute profile ID. This is better to be unique in whole program. */
555 unsigned
556 coverage_compute_profile_id (struct cgraph_node *n)
558 unsigned chksum;
560 /* Externally visible symbols have unique name. */
561 if (TREE_PUBLIC (n->decl) || DECL_EXTERNAL (n->decl) || n->unique_name)
563 chksum = coverage_checksum_string
564 (0, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n->decl)));
566 else
568 expanded_location xloc
569 = expand_location (DECL_SOURCE_LOCATION (n->decl));
570 bool use_name_only = (param_profile_func_internal_id == 0);
572 chksum = (use_name_only ? 0 : xloc.line);
573 if (xloc.file)
574 chksum = coverage_checksum_string (chksum, xloc.file);
575 chksum = coverage_checksum_string
576 (chksum, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n->decl)));
577 if (!use_name_only && first_global_object_name)
578 chksum = coverage_checksum_string
579 (chksum, first_global_object_name);
580 chksum = coverage_checksum_string
581 (chksum, aux_base_name);
584 /* Non-negative integers are hopefully small enough to fit in all targets.
585 Gcov file formats wants non-zero function IDs. */
586 chksum = chksum & 0x7fffffff;
587 return chksum + (!chksum);
590 /* Compute cfg checksum for the function FN given as argument.
591 The checksum is calculated carefully so that
592 source code changes that doesn't affect the control flow graph
593 won't change the checksum.
594 This is to make the profile data useable across source code change.
595 The downside of this is that the compiler may use potentially
596 wrong profile data - that the source code change has non-trivial impact
597 on the validity of profile data (e.g. the reversed condition)
598 but the compiler won't detect the change and use the wrong profile data. */
600 unsigned
601 coverage_compute_cfg_checksum (struct function *fn)
603 basic_block bb;
604 unsigned chksum = n_basic_blocks_for_fn (fn);
606 FOR_EACH_BB_FN (bb, fn)
608 edge e;
609 edge_iterator ei;
610 chksum = crc32_byte (chksum, bb->index);
611 FOR_EACH_EDGE (e, ei, bb->succs)
613 chksum = crc32_byte (chksum, e->dest->index);
617 return chksum;
620 /* Begin output to the notes file for the current function.
621 Writes the function header. Returns nonzero if data should be output. */
624 coverage_begin_function (unsigned lineno_checksum, unsigned cfg_checksum)
626 /* We don't need to output .gcno file unless we're under -ftest-coverage
627 (e.g. -fprofile-arcs/generate/use don't need .gcno to work). */
628 if (no_coverage || !bbg_file_name)
629 return 0;
631 expanded_location startloc
632 = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
634 /* Announce function */
635 unsigned long offset = gcov_write_tag (GCOV_TAG_FUNCTION);
636 if (param_profile_func_internal_id)
637 gcov_write_unsigned (current_function_funcdef_no + 1);
638 else
640 gcc_assert (coverage_node_map_initialized_p ());
641 gcov_write_unsigned (
642 cgraph_node::get (current_function_decl)->profile_id);
645 gcov_write_unsigned (lineno_checksum);
646 gcov_write_unsigned (cfg_checksum);
647 gcov_write_string (IDENTIFIER_POINTER
648 (DECL_ASSEMBLER_NAME (current_function_decl)));
649 gcov_write_unsigned (DECL_ARTIFICIAL (current_function_decl)
650 && !DECL_FUNCTION_VERSIONED (current_function_decl)
651 && !DECL_LAMBDA_FUNCTION_P (current_function_decl));
652 gcov_write_filename (startloc.file);
653 gcov_write_unsigned (startloc.line);
654 gcov_write_unsigned (startloc.column);
656 expanded_location endloc = expand_location (cfun->function_end_locus);
658 /* Function can start in a single file and end in another one. */
659 int end_line
660 = endloc.file == startloc.file ? endloc.line : startloc.line;
661 int end_column
662 = endloc.file == startloc.file ? endloc.column: startloc.column;
664 if (startloc.line > end_line)
666 warning_at (DECL_SOURCE_LOCATION (current_function_decl),
667 OPT_Wcoverage_invalid_line_number,
668 "function starts on a higher line number than it ends");
669 end_line = startloc.line;
670 end_column = startloc.column;
673 gcov_write_unsigned (end_line);
674 gcov_write_unsigned (end_column);
675 gcov_write_length (offset);
677 return !gcov_is_error ();
680 /* Finish coverage data for the current function. Verify no output
681 error has occurred. Save function coverage counts. */
683 void
684 coverage_end_function (unsigned lineno_checksum, unsigned cfg_checksum)
686 unsigned i;
688 if (bbg_file_name && gcov_is_error ())
690 warning (0, "error writing %qs", bbg_file_name);
691 unlink (bbg_file_name);
692 bbg_file_name = NULL;
695 if (fn_ctr_mask)
697 struct coverage_data *item = 0;
699 item = ggc_alloc<coverage_data> ();
701 if (param_profile_func_internal_id)
702 item->ident = current_function_funcdef_no + 1;
703 else
705 gcc_assert (coverage_node_map_initialized_p ());
706 item->ident = cgraph_node::get (cfun->decl)->profile_id;
709 item->lineno_checksum = lineno_checksum;
710 item->cfg_checksum = cfg_checksum;
712 item->fn_decl = current_function_decl;
713 item->next = 0;
714 *functions_tail = item;
715 functions_tail = &item->next;
717 for (i = 0; i != GCOV_COUNTERS; i++)
719 tree var = fn_v_ctrs[i];
721 if (item)
722 item->ctr_vars[i] = var;
723 if (var)
725 tree array_type = build_index_type (size_int (fn_n_ctrs[i] - 1));
726 array_type = build_array_type (get_gcov_type (), array_type);
727 TREE_TYPE (var) = array_type;
728 DECL_SIZE (var) = TYPE_SIZE (array_type);
729 DECL_SIZE_UNIT (var) = TYPE_SIZE_UNIT (array_type);
730 varpool_node::finalize_decl (var);
733 fn_b_ctrs[i] = fn_n_ctrs[i] = 0;
734 fn_v_ctrs[i] = NULL_TREE;
736 prg_ctr_mask |= fn_ctr_mask;
737 fn_ctr_mask = 0;
741 /* Remove coverage file if opened. */
743 void
744 coverage_remove_note_file (void)
746 if (bbg_file_name)
748 gcov_close ();
749 unlink (bbg_file_name);
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 buf[len - 1] = symbol_table::symbol_suffix_separator ();
774 memcpy (buf + len, fn_name, fn_name_len + 1);
775 DECL_NAME (var) = get_identifier (buf);
776 TREE_STATIC (var) = 1;
777 TREE_ADDRESSABLE (var) = 1;
778 DECL_NONALIASED (var) = 1;
779 SET_DECL_ALIGN (var, TYPE_ALIGN (type));
781 return var;
784 /* Creates the gcov_fn_info RECORD_TYPE. */
786 static void
787 build_fn_info_type (tree type, unsigned counters, tree gcov_info_type)
789 tree ctr_info = lang_hooks.types.make_type (RECORD_TYPE);
790 tree field, fields;
791 tree array_type;
793 gcc_assert (counters);
795 /* ctr_info::num */
796 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
797 get_gcov_unsigned_t ());
798 fields = field;
800 /* ctr_info::values */
801 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
802 build_pointer_type (get_gcov_type ()));
803 DECL_CHAIN (field) = fields;
804 fields = field;
806 finish_builtin_struct (ctr_info, "__gcov_ctr_info", fields, NULL_TREE);
808 /* key */
809 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
810 build_pointer_type (build_qualified_type
811 (gcov_info_type, TYPE_QUAL_CONST)));
812 fields = field;
814 /* ident */
815 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
816 get_gcov_unsigned_t ());
817 DECL_CHAIN (field) = fields;
818 fields = field;
820 /* lineno_checksum */
821 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
822 get_gcov_unsigned_t ());
823 DECL_CHAIN (field) = fields;
824 fields = field;
826 /* cfg checksum */
827 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
828 get_gcov_unsigned_t ());
829 DECL_CHAIN (field) = fields;
830 fields = field;
832 array_type = build_index_type (size_int (counters - 1));
833 array_type = build_array_type (ctr_info, array_type);
835 /* counters */
836 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE, array_type);
837 DECL_CHAIN (field) = fields;
838 fields = field;
840 finish_builtin_struct (type, "__gcov_fn_info", fields, NULL_TREE);
843 /* Returns a CONSTRUCTOR for a gcov_fn_info. DATA is
844 the coverage data for the function and TYPE is the gcov_fn_info
845 RECORD_TYPE. KEY is the object file key. */
847 static tree
848 build_fn_info (const struct coverage_data *data, tree type, tree key)
850 tree fields = TYPE_FIELDS (type);
851 tree ctr_type;
852 unsigned ix;
853 vec<constructor_elt, va_gc> *v1 = NULL;
854 vec<constructor_elt, va_gc> *v2 = NULL;
856 /* key */
857 CONSTRUCTOR_APPEND_ELT (v1, fields,
858 build1 (ADDR_EXPR, TREE_TYPE (fields), key));
859 fields = DECL_CHAIN (fields);
861 /* ident */
862 CONSTRUCTOR_APPEND_ELT (v1, fields,
863 build_int_cstu (get_gcov_unsigned_t (),
864 data->ident));
865 fields = DECL_CHAIN (fields);
867 /* lineno_checksum */
868 CONSTRUCTOR_APPEND_ELT (v1, fields,
869 build_int_cstu (get_gcov_unsigned_t (),
870 data->lineno_checksum));
871 fields = DECL_CHAIN (fields);
873 /* cfg_checksum */
874 CONSTRUCTOR_APPEND_ELT (v1, fields,
875 build_int_cstu (get_gcov_unsigned_t (),
876 data->cfg_checksum));
877 fields = DECL_CHAIN (fields);
879 /* counters */
880 ctr_type = TREE_TYPE (TREE_TYPE (fields));
881 for (ix = 0; ix != GCOV_COUNTERS; ix++)
882 if (prg_ctr_mask & (1 << ix))
884 vec<constructor_elt, va_gc> *ctr = NULL;
885 tree var = data->ctr_vars[ix];
886 unsigned count = 0;
888 if (var)
889 count
890 = tree_to_shwi (TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (var))))
891 + 1;
893 CONSTRUCTOR_APPEND_ELT (ctr, TYPE_FIELDS (ctr_type),
894 build_int_cstu (get_gcov_unsigned_t (),
895 count));
897 if (var)
898 CONSTRUCTOR_APPEND_ELT (ctr, DECL_CHAIN (TYPE_FIELDS (ctr_type)),
899 build_fold_addr_expr (var));
901 CONSTRUCTOR_APPEND_ELT (v2, NULL, build_constructor (ctr_type, ctr));
904 CONSTRUCTOR_APPEND_ELT (v1, fields,
905 build_constructor (TREE_TYPE (fields), v2));
907 return build_constructor (type, v1);
910 /* Create gcov_info struct. TYPE is the incomplete RECORD_TYPE to be
911 completed, and FN_INFO_PTR_TYPE is a pointer to the function info type. */
913 static void
914 build_info_type (tree type, tree fn_info_ptr_type)
916 tree field, fields = NULL_TREE;
917 tree merge_fn_type;
919 /* Version ident */
920 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
921 get_gcov_unsigned_t ());
922 DECL_CHAIN (field) = fields;
923 fields = field;
925 /* next pointer */
926 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
927 build_pointer_type (build_qualified_type
928 (type, TYPE_QUAL_CONST)));
929 DECL_CHAIN (field) = fields;
930 fields = field;
932 /* stamp */
933 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
934 get_gcov_unsigned_t ());
935 DECL_CHAIN (field) = fields;
936 fields = field;
938 /* Filename */
939 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
940 build_pointer_type (build_qualified_type
941 (char_type_node, TYPE_QUAL_CONST)));
942 DECL_CHAIN (field) = fields;
943 fields = field;
945 /* merge fn array */
946 merge_fn_type
947 = build_function_type_list (void_type_node,
948 build_pointer_type (get_gcov_type ()),
949 get_gcov_unsigned_t (), NULL_TREE);
950 merge_fn_type
951 = build_array_type (build_pointer_type (merge_fn_type),
952 build_index_type (size_int (GCOV_COUNTERS - 1)));
953 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
954 merge_fn_type);
955 DECL_CHAIN (field) = fields;
956 fields = field;
958 /* n_functions */
959 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
960 get_gcov_unsigned_t ());
961 DECL_CHAIN (field) = fields;
962 fields = field;
964 /* function_info pointer pointer */
965 fn_info_ptr_type = build_pointer_type
966 (build_qualified_type (fn_info_ptr_type, TYPE_QUAL_CONST));
967 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
968 fn_info_ptr_type);
969 DECL_CHAIN (field) = fields;
970 fields = field;
972 finish_builtin_struct (type, "__gcov_info", fields, NULL_TREE);
975 /* Returns a CONSTRUCTOR for the gcov_info object. INFO_TYPE is the
976 gcov_info structure type, FN_ARY is the array of pointers to
977 function info objects. */
979 static tree
980 build_info (tree info_type, tree fn_ary)
982 tree info_fields = TYPE_FIELDS (info_type);
983 tree merge_fn_type, n_funcs;
984 unsigned ix;
985 tree filename_string;
986 int da_file_name_len;
987 vec<constructor_elt, va_gc> *v1 = NULL;
988 vec<constructor_elt, va_gc> *v2 = NULL;
990 /* Version ident */
991 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
992 build_int_cstu (TREE_TYPE (info_fields),
993 GCOV_VERSION));
994 info_fields = DECL_CHAIN (info_fields);
996 /* next -- NULL */
997 CONSTRUCTOR_APPEND_ELT (v1, info_fields, null_pointer_node);
998 info_fields = DECL_CHAIN (info_fields);
1000 /* stamp */
1001 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
1002 build_int_cstu (TREE_TYPE (info_fields),
1003 bbg_file_stamp));
1004 info_fields = DECL_CHAIN (info_fields);
1006 /* Filename */
1007 da_file_name_len = strlen (da_file_name);
1008 filename_string = build_string (da_file_name_len + 1, da_file_name);
1009 TREE_TYPE (filename_string) = build_array_type
1010 (char_type_node, build_index_type (size_int (da_file_name_len)));
1011 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
1012 build1 (ADDR_EXPR, TREE_TYPE (info_fields),
1013 filename_string));
1014 info_fields = DECL_CHAIN (info_fields);
1016 /* merge fn array -- NULL slots indicate unmeasured counters */
1017 merge_fn_type = TREE_TYPE (TREE_TYPE (info_fields));
1018 for (ix = 0; ix != GCOV_COUNTERS; ix++)
1020 tree ptr = null_pointer_node;
1022 if ((1u << ix) & prg_ctr_mask)
1024 tree merge_fn = build_decl (BUILTINS_LOCATION,
1025 FUNCTION_DECL,
1026 get_identifier (ctr_merge_functions[ix]),
1027 TREE_TYPE (merge_fn_type));
1028 DECL_EXTERNAL (merge_fn) = 1;
1029 TREE_PUBLIC (merge_fn) = 1;
1030 DECL_ARTIFICIAL (merge_fn) = 1;
1031 TREE_NOTHROW (merge_fn) = 1;
1032 /* Initialize assembler name so we can stream out. */
1033 DECL_ASSEMBLER_NAME (merge_fn);
1034 ptr = build1 (ADDR_EXPR, merge_fn_type, merge_fn);
1036 CONSTRUCTOR_APPEND_ELT (v2, NULL, ptr);
1038 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
1039 build_constructor (TREE_TYPE (info_fields), v2));
1040 info_fields = DECL_CHAIN (info_fields);
1042 /* n_functions */
1043 n_funcs = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (fn_ary)));
1044 n_funcs = fold_build2 (PLUS_EXPR, TREE_TYPE (info_fields),
1045 n_funcs, size_one_node);
1046 CONSTRUCTOR_APPEND_ELT (v1, info_fields, n_funcs);
1047 info_fields = DECL_CHAIN (info_fields);
1049 /* functions */
1050 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
1051 build1 (ADDR_EXPR, TREE_TYPE (info_fields), fn_ary));
1052 info_fields = DECL_CHAIN (info_fields);
1054 gcc_assert (!info_fields);
1055 return build_constructor (info_type, v1);
1058 /* Generate the constructor function to call __gcov_init. */
1060 static void
1061 build_init_ctor (tree gcov_info_type)
1063 tree ctor, stmt, init_fn;
1065 /* Build a decl for __gcov_init. */
1066 init_fn = build_pointer_type (gcov_info_type);
1067 init_fn = build_function_type_list (void_type_node, init_fn, NULL);
1068 init_fn = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
1069 get_identifier ("__gcov_init"), init_fn);
1070 TREE_PUBLIC (init_fn) = 1;
1071 DECL_EXTERNAL (init_fn) = 1;
1072 DECL_ASSEMBLER_NAME (init_fn);
1074 /* Generate a call to __gcov_init(&gcov_info). */
1075 ctor = NULL;
1076 stmt = build_fold_addr_expr (gcov_info_var);
1077 stmt = build_call_expr (init_fn, 1, stmt);
1078 append_to_statement_list (stmt, &ctor);
1080 /* Generate a constructor to run it. */
1081 int priority = SUPPORTS_INIT_PRIORITY
1082 ? MAX_RESERVED_INIT_PRIORITY: DEFAULT_INIT_PRIORITY;
1083 cgraph_build_static_cdtor ('I', ctor, priority);
1086 /* Generate the destructor function to call __gcov_exit. */
1088 static void
1089 build_gcov_exit_decl (void)
1091 tree init_fn = build_function_type_list (void_type_node, NULL);
1092 init_fn = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
1093 get_identifier ("__gcov_exit"), init_fn);
1094 TREE_PUBLIC (init_fn) = 1;
1095 DECL_EXTERNAL (init_fn) = 1;
1096 DECL_ASSEMBLER_NAME (init_fn);
1098 /* Generate a call to __gcov_exit (). */
1099 tree dtor = NULL;
1100 tree stmt = build_call_expr (init_fn, 0);
1101 append_to_statement_list (stmt, &dtor);
1103 /* Generate a destructor to run it. */
1104 int priority = SUPPORTS_INIT_PRIORITY
1105 ? MAX_RESERVED_INIT_PRIORITY: DEFAULT_INIT_PRIORITY;
1107 cgraph_build_static_cdtor ('D', dtor, priority);
1110 /* Generate the pointer to the gcov_info_var in a dedicated section. */
1112 static void
1113 build_gcov_info_var_registration (tree gcov_info_type)
1115 tree var = build_decl (BUILTINS_LOCATION,
1116 VAR_DECL, NULL_TREE,
1117 build_pointer_type (gcov_info_type));
1118 TREE_STATIC (var) = 1;
1119 TREE_READONLY (var) = 1;
1120 char name_buf[32];
1121 ASM_GENERATE_INTERNAL_LABEL (name_buf, "LPBX", 2);
1122 DECL_NAME (var) = get_identifier (name_buf);
1123 get_section (profile_info_section, SECTION_UNNAMED, NULL);
1124 set_decl_section_name (var, profile_info_section);
1125 mark_decl_referenced (var);
1126 DECL_INITIAL (var) = build_fold_addr_expr (gcov_info_var);
1127 varpool_node::finalize_decl (var);
1130 /* Create the gcov_info types and object. Generate the constructor
1131 function to call __gcov_init. Does not generate the initializer
1132 for the object. Returns TRUE if coverage data is being emitted. */
1134 static bool
1135 coverage_obj_init (void)
1137 tree gcov_info_type;
1138 unsigned n_counters = 0;
1139 unsigned ix;
1140 struct coverage_data *fn;
1141 struct coverage_data **fn_prev;
1142 char name_buf[32];
1144 no_coverage = 1; /* Disable any further coverage. */
1146 if (!prg_ctr_mask)
1147 return false;
1149 if (symtab->dump_file)
1150 fprintf (symtab->dump_file, "Using data file %s\n", da_file_name);
1152 /* Prune functions. */
1153 for (fn_prev = &functions_head; (fn = *fn_prev);)
1154 if (DECL_STRUCT_FUNCTION (fn->fn_decl))
1155 fn_prev = &fn->next;
1156 else
1157 /* The function is not being emitted, remove from list. */
1158 *fn_prev = fn->next;
1160 if (functions_head == NULL)
1161 return false;
1163 for (ix = 0; ix != GCOV_COUNTERS; ix++)
1164 if ((1u << ix) & prg_ctr_mask)
1165 n_counters++;
1167 /* Build the info and fn_info types. These are mutually recursive. */
1168 gcov_info_type = lang_hooks.types.make_type (RECORD_TYPE);
1169 gcov_fn_info_type = lang_hooks.types.make_type (RECORD_TYPE);
1170 build_fn_info_type (gcov_fn_info_type, n_counters, gcov_info_type);
1171 gcov_info_type = lang_hooks.types.make_type (RECORD_TYPE);
1172 gcov_fn_info_ptr_type = build_pointer_type
1173 (build_qualified_type (gcov_fn_info_type, TYPE_QUAL_CONST));
1174 build_info_type (gcov_info_type, gcov_fn_info_ptr_type);
1176 /* Build the gcov info var, this is referred to in its own
1177 initializer. */
1178 gcov_info_var = build_decl (BUILTINS_LOCATION,
1179 VAR_DECL, NULL_TREE, gcov_info_type);
1180 TREE_STATIC (gcov_info_var) = 1;
1181 ASM_GENERATE_INTERNAL_LABEL (name_buf, "LPBX", 0);
1182 DECL_NAME (gcov_info_var) = get_identifier (name_buf);
1184 if (profile_info_section)
1185 build_gcov_info_var_registration (gcov_info_type);
1186 else
1188 build_init_ctor (gcov_info_type);
1189 build_gcov_exit_decl ();
1192 return true;
1195 /* Generate the coverage function info for FN and DATA. Append a
1196 pointer to that object to CTOR and return the appended CTOR. */
1198 static vec<constructor_elt, va_gc> *
1199 coverage_obj_fn (vec<constructor_elt, va_gc> *ctor, tree fn,
1200 struct coverage_data const *data)
1202 tree init = build_fn_info (data, gcov_fn_info_type, gcov_info_var);
1203 tree var = build_var (fn, gcov_fn_info_type, -1);
1205 DECL_INITIAL (var) = init;
1206 varpool_node::finalize_decl (var);
1208 CONSTRUCTOR_APPEND_ELT (ctor, NULL,
1209 build1 (ADDR_EXPR, gcov_fn_info_ptr_type, var));
1210 return ctor;
1213 /* Finalize the coverage data. Generates the array of pointers to
1214 function objects from CTOR. Generate the gcov_info initializer. */
1216 static void
1217 coverage_obj_finish (vec<constructor_elt, va_gc> *ctor)
1219 unsigned n_functions = vec_safe_length (ctor);
1220 tree fn_info_ary_type = build_array_type
1221 (build_qualified_type (gcov_fn_info_ptr_type, TYPE_QUAL_CONST),
1222 build_index_type (size_int (n_functions - 1)));
1223 tree fn_info_ary = build_decl (BUILTINS_LOCATION, VAR_DECL, NULL_TREE,
1224 fn_info_ary_type);
1225 char name_buf[32];
1227 TREE_STATIC (fn_info_ary) = 1;
1228 ASM_GENERATE_INTERNAL_LABEL (name_buf, "LPBX", 1);
1229 DECL_NAME (fn_info_ary) = get_identifier (name_buf);
1230 DECL_INITIAL (fn_info_ary) = build_constructor (fn_info_ary_type, ctor);
1231 varpool_node::finalize_decl (fn_info_ary);
1233 DECL_INITIAL (gcov_info_var)
1234 = build_info (TREE_TYPE (gcov_info_var), fn_info_ary);
1235 varpool_node::finalize_decl (gcov_info_var);
1238 /* Perform file-level initialization. Read in data file, generate name
1239 of notes file. */
1241 void
1242 coverage_init (const char *filename)
1244 const char *original_filename = filename;
1245 int original_len = strlen (original_filename);
1246 #if HAVE_DOS_BASED_FILE_SYSTEM
1247 const char *separator = "\\";
1248 #else
1249 const char *separator = "/";
1250 #endif
1251 int len = strlen (filename);
1252 int prefix_len = 0;
1254 /* Since coverage_init is invoked very early, before the pass
1255 manager, we need to set up the dumping explicitly. This is
1256 similar to the handling in finish_optimization_passes. */
1257 int profile_pass_num =
1258 g->get_passes ()->get_pass_profile ()->static_pass_number;
1259 g->get_dumps ()->dump_start (profile_pass_num, NULL);
1261 if (!IS_ABSOLUTE_PATH (filename))
1263 /* When a profile_data_prefix is provided, then mangle full path
1264 of filename in order to prevent file path clashing. */
1265 if (profile_data_prefix)
1267 filename = concat (getpwd (), separator, filename, NULL);
1268 if (profile_prefix_path)
1270 if (startswith (filename, profile_prefix_path))
1272 filename += strlen (profile_prefix_path);
1273 while (*filename == *separator)
1274 filename++;
1276 else
1277 warning (0, "filename %qs does not start with profile "
1278 "prefix %qs", filename, profile_prefix_path);
1280 filename = mangle_path (filename);
1281 len = strlen (filename);
1283 else
1284 profile_data_prefix = getpwd ();
1287 if (profile_data_prefix)
1288 prefix_len = strlen (profile_data_prefix);
1290 /* Name of da file. */
1291 da_file_name = XNEWVEC (char, len + strlen (GCOV_DATA_SUFFIX)
1292 + prefix_len + 2);
1294 if (profile_data_prefix)
1296 memcpy (da_file_name, profile_data_prefix, prefix_len);
1297 da_file_name[prefix_len++] = *separator;
1299 memcpy (da_file_name + prefix_len, filename, len);
1300 strcpy (da_file_name + prefix_len + len, GCOV_DATA_SUFFIX);
1302 bbg_file_stamp = local_tick;
1304 if (flag_auto_profile)
1305 read_autofdo_file ();
1306 else if (flag_branch_probabilities)
1307 read_counts_file ();
1309 /* Name of bbg file. */
1310 if (flag_test_coverage && !flag_compare_debug)
1312 if (profile_note_location)
1313 bbg_file_name = xstrdup (profile_note_location);
1314 else
1316 bbg_file_name = XNEWVEC (char, original_len + strlen (GCOV_NOTE_SUFFIX) + 1);
1317 memcpy (bbg_file_name, original_filename, original_len);
1318 strcpy (bbg_file_name + original_len, GCOV_NOTE_SUFFIX);
1321 if (!gcov_open (bbg_file_name, -1))
1323 error ("cannot open %s", bbg_file_name);
1324 bbg_file_name = NULL;
1326 else
1328 gcov_write_unsigned (GCOV_NOTE_MAGIC);
1329 gcov_write_unsigned (GCOV_VERSION);
1330 gcov_write_unsigned (bbg_file_stamp);
1331 gcov_write_string (getpwd ());
1333 /* Do not support has_unexecuted_blocks for Ada. */
1334 gcov_write_unsigned (strcmp (lang_hooks.name, "GNU Ada") != 0);
1338 g->get_dumps ()->dump_finish (profile_pass_num);
1341 /* Performs file-level cleanup. Close notes file, generate coverage
1342 variables and constructor. */
1344 void
1345 coverage_finish (void)
1347 if (bbg_file_name && gcov_close ())
1348 unlink (bbg_file_name);
1350 if (!flag_branch_probabilities && flag_test_coverage
1351 && (!local_tick || local_tick == (unsigned)-1))
1352 /* Only remove the da file, if we're emitting coverage code and
1353 cannot uniquely stamp it. If we can stamp it, libgcov will DTRT. */
1354 unlink (da_file_name);
1356 if (coverage_obj_init ())
1358 vec<constructor_elt, va_gc> *fn_ctor = NULL;
1359 struct coverage_data *fn;
1361 for (fn = functions_head; fn; fn = fn->next)
1362 fn_ctor = coverage_obj_fn (fn_ctor, fn->fn_decl, fn);
1363 coverage_obj_finish (fn_ctor);
1366 XDELETEVEC (da_file_name);
1367 da_file_name = NULL;
1370 #include "gt-coverage.h"