c++: 'this' adjustment for devirtualized call
[official-gcc.git] / gcc / coverage.c
blob5a344cdfc179b699630513f91e3e6bb9a3dc3aca
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"
54 #include "gcov-io.c"
56 struct GTY((chain_next ("%h.next"))) coverage_data
58 struct coverage_data *next; /* next function */
59 unsigned ident; /* function ident */
60 unsigned lineno_checksum; /* function lineno checksum */
61 unsigned cfg_checksum; /* function cfg checksum */
62 tree fn_decl; /* the function decl */
63 tree ctr_vars[GCOV_COUNTERS]; /* counter variables. */
66 /* Counts information for a function. */
67 struct counts_entry : pointer_hash <counts_entry>
69 /* We hash by */
70 unsigned ident;
71 unsigned ctr;
73 /* Store */
74 unsigned lineno_checksum;
75 unsigned cfg_checksum;
76 gcov_type *counts;
77 unsigned n_counts;
79 /* hash_table support. */
80 static inline hashval_t hash (const counts_entry *);
81 static int equal (const counts_entry *, const counts_entry *);
82 static void remove (counts_entry *);
85 static GTY(()) struct coverage_data *functions_head = 0;
86 static struct coverage_data **functions_tail = &functions_head;
87 static unsigned no_coverage = 0;
89 /* Cumulative counter information for whole program. */
90 static unsigned prg_ctr_mask; /* Mask of counter types generated. */
92 /* Counter information for current function. */
93 static unsigned fn_ctr_mask; /* Mask of counters used. */
94 static GTY(()) tree fn_v_ctrs[GCOV_COUNTERS]; /* counter variables. */
95 static unsigned fn_n_ctrs[GCOV_COUNTERS]; /* Counters allocated. */
96 static unsigned fn_b_ctrs[GCOV_COUNTERS]; /* Allocation base. */
98 /* Coverage info VAR_DECL and function info type nodes. */
99 static GTY(()) tree gcov_info_var;
100 static GTY(()) tree gcov_fn_info_type;
101 static GTY(()) tree gcov_fn_info_ptr_type;
103 /* Name of the notes (gcno) output file. The "bbg" prefix is for
104 historical reasons, when the notes file contained only the
105 basic block graph notes.
106 If this is NULL we're not writing to the notes file. */
107 static char *bbg_file_name;
109 /* File stamp for notes file. */
110 static unsigned bbg_file_stamp;
112 /* Name of the count data (gcda) file. */
113 static char *da_file_name;
115 /* The names of merge functions for counters. */
116 #define STR(str) #str
117 #define DEF_GCOV_COUNTER(COUNTER, NAME, FN_TYPE) STR(__gcov_merge ## FN_TYPE),
118 static const char *const ctr_merge_functions[GCOV_COUNTERS] = {
119 #include "gcov-counter.def"
121 #undef DEF_GCOV_COUNTER
122 #undef STR
124 #define DEF_GCOV_COUNTER(COUNTER, NAME, FN_TYPE) NAME,
125 static const char *const ctr_names[GCOV_COUNTERS] = {
126 #include "gcov-counter.def"
128 #undef DEF_GCOV_COUNTER
130 /* Forward declarations. */
131 static void read_counts_file (void);
132 static tree build_var (tree, tree, int);
133 static void build_fn_info_type (tree, unsigned, tree);
134 static void build_info_type (tree, tree);
135 static tree build_fn_info (const struct coverage_data *, tree, tree);
136 static tree build_info (tree, tree);
137 static bool coverage_obj_init (void);
138 static vec<constructor_elt, va_gc> *coverage_obj_fn
139 (vec<constructor_elt, va_gc> *, tree, struct coverage_data const *);
140 static void coverage_obj_finish (vec<constructor_elt, va_gc> *);
142 /* Return the type node for gcov_type. */
144 tree
145 get_gcov_type (void)
147 scalar_int_mode mode
148 = smallest_int_mode_for_size (LONG_LONG_TYPE_SIZE > 32 ? 64 : 32);
149 return lang_hooks.types.type_for_mode (mode, false);
152 /* Return the type node for gcov_unsigned_t. */
154 static tree
155 get_gcov_unsigned_t (void)
157 scalar_int_mode mode = smallest_int_mode_for_size (32);
158 return lang_hooks.types.type_for_mode (mode, true);
161 inline hashval_t
162 counts_entry::hash (const counts_entry *entry)
164 return entry->ident * GCOV_COUNTERS + entry->ctr;
167 inline int
168 counts_entry::equal (const counts_entry *entry1, const counts_entry *entry2)
170 return entry1->ident == entry2->ident && entry1->ctr == entry2->ctr;
173 inline void
174 counts_entry::remove (counts_entry *entry)
176 free (entry->counts);
177 free (entry);
180 /* Hash table of count data. */
181 static hash_table<counts_entry> *counts_hash;
183 /* Read in the counts file, if available. */
185 static void
186 read_counts_file (void)
188 gcov_unsigned_t fn_ident = 0;
189 gcov_unsigned_t tag;
190 int is_error = 0;
191 unsigned lineno_checksum = 0;
192 unsigned cfg_checksum = 0;
194 if (!gcov_open (da_file_name, 1))
195 return;
197 if (!gcov_magic (gcov_read_unsigned (), GCOV_DATA_MAGIC))
199 warning (0, "%qs is not a gcov data file", da_file_name);
200 gcov_close ();
201 return;
203 else if ((tag = gcov_read_unsigned ()) != GCOV_VERSION)
205 char v[4], e[4];
207 GCOV_UNSIGNED2STRING (v, tag);
208 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
210 warning (0, "%qs is version %q.*s, expected version %q.*s",
211 da_file_name, 4, v, 4, e);
212 gcov_close ();
213 return;
216 /* Read the stamp, used for creating a generation count. */
217 tag = gcov_read_unsigned ();
218 bbg_file_stamp = crc32_unsigned (bbg_file_stamp, tag);
220 counts_hash = new hash_table<counts_entry> (10);
221 while ((tag = gcov_read_unsigned ()))
223 gcov_unsigned_t length;
224 gcov_position_t offset;
226 length = gcov_read_unsigned ();
227 offset = gcov_position ();
228 if (tag == GCOV_TAG_FUNCTION)
230 if (length)
232 fn_ident = gcov_read_unsigned ();
233 lineno_checksum = gcov_read_unsigned ();
234 cfg_checksum = gcov_read_unsigned ();
236 else
237 fn_ident = lineno_checksum = cfg_checksum = 0;
239 else if (tag == GCOV_TAG_OBJECT_SUMMARY)
241 profile_info = XCNEW (gcov_summary);
242 profile_info->runs = gcov_read_unsigned ();
243 profile_info->sum_max = gcov_read_unsigned ();
245 else if (GCOV_TAG_IS_COUNTER (tag) && fn_ident)
247 counts_entry **slot, *entry, elt;
248 int read_length = (int)length;
249 length = read_length > 0 ? read_length : 0;
250 unsigned n_counts = GCOV_TAG_COUNTER_NUM (abs (read_length));
251 unsigned ix;
253 elt.ident = fn_ident;
254 elt.ctr = GCOV_COUNTER_FOR_TAG (tag);
256 slot = counts_hash->find_slot (&elt, INSERT);
257 entry = *slot;
258 if (!entry)
260 *slot = entry = XCNEW (counts_entry);
261 entry->ident = fn_ident;
262 entry->ctr = elt.ctr;
263 entry->lineno_checksum = lineno_checksum;
264 entry->cfg_checksum = cfg_checksum;
265 entry->counts = XCNEWVEC (gcov_type, n_counts);
266 entry->n_counts = n_counts;
268 else if (entry->lineno_checksum != lineno_checksum
269 || entry->cfg_checksum != cfg_checksum)
271 error ("profile data for function %u is corrupted", fn_ident);
272 error ("checksum is (%x,%x) instead of (%x,%x)",
273 entry->lineno_checksum, entry->cfg_checksum,
274 lineno_checksum, cfg_checksum);
275 delete counts_hash;
276 counts_hash = NULL;
277 break;
279 if (read_length > 0)
280 for (ix = 0; ix != n_counts; ix++)
281 entry->counts[ix] = gcov_read_counter ();
283 gcov_sync (offset, length);
284 if ((is_error = gcov_is_error ()))
286 error (is_error < 0
287 ? G_("%qs has overflowed")
288 : G_("%qs is corrupted"),
289 da_file_name);
290 delete counts_hash;
291 counts_hash = NULL;
292 break;
296 gcov_close ();
299 /* Returns the counters for a particular tag. */
301 gcov_type *
302 get_coverage_counts (unsigned counter, unsigned cfg_checksum,
303 unsigned lineno_checksum, unsigned int n_counts)
305 counts_entry *entry, elt;
307 /* No hash table, no counts. */
308 if (!counts_hash)
310 static int warned = 0;
312 if (!warned++)
314 warning (OPT_Wmissing_profile,
315 "%qs profile count data file not found",
316 da_file_name);
317 if (dump_enabled_p ())
319 dump_user_location_t loc
320 = dump_user_location_t::from_location_t (input_location);
321 dump_printf_loc (MSG_MISSED_OPTIMIZATION, loc,
322 "file %s not found, %s\n", da_file_name,
323 (flag_guess_branch_prob
324 ? "execution counts estimated"
325 : "execution counts assumed to be zero"));
328 return NULL;
330 if (param_profile_func_internal_id)
331 elt.ident = current_function_funcdef_no + 1;
332 else
334 gcc_assert (coverage_node_map_initialized_p ());
335 elt.ident = cgraph_node::get (current_function_decl)->profile_id;
337 elt.ctr = counter;
338 entry = counts_hash->find (&elt);
339 if (!entry)
341 if (counter == GCOV_COUNTER_ARCS)
342 warning_at (DECL_SOURCE_LOCATION (current_function_decl),
343 OPT_Wmissing_profile,
344 "profile for function %qD not found in profile data",
345 current_function_decl);
346 /* The function was not emitted, or is weak and not chosen in the
347 final executable. Silently fail, because there's nothing we
348 can do about it. */
349 return NULL;
352 if (entry->cfg_checksum != cfg_checksum
353 || (counter != GCOV_COUNTER_V_INDIR
354 && counter != GCOV_COUNTER_V_TOPN
355 && entry->n_counts != n_counts))
357 static int warned = 0;
358 bool warning_printed = false;
360 if (entry->n_counts != n_counts)
361 warning_printed =
362 warning_at (DECL_SOURCE_LOCATION (current_function_decl),
363 OPT_Wcoverage_mismatch,
364 "number of counters in profile data for function %qD "
365 "does not match "
366 "its profile data (counter %qs, expected %i and have %i)",
367 current_function_decl,
368 ctr_names[counter], entry->n_counts, n_counts);
369 else
370 warning_printed =
371 warning_at (DECL_SOURCE_LOCATION (current_function_decl),
372 OPT_Wcoverage_mismatch,
373 "the control flow of function %qD does not match "
374 "its profile data (counter %qs)", current_function_decl,
375 ctr_names[counter]);
376 if (warning_printed && dump_enabled_p ())
378 dump_user_location_t loc
379 = dump_user_location_t::from_function_decl (current_function_decl);
380 dump_printf_loc (MSG_MISSED_OPTIMIZATION, loc,
381 "use -Wno-error=coverage-mismatch to tolerate "
382 "the mismatch but performance may drop if the "
383 "function is hot\n");
385 if (!seen_error ()
386 && !warned++)
388 dump_printf_loc (MSG_MISSED_OPTIMIZATION, loc,
389 "coverage mismatch ignored\n");
390 dump_printf (MSG_MISSED_OPTIMIZATION,
391 flag_guess_branch_prob
392 ? G_("execution counts estimated\n")
393 : G_("execution counts assumed to be zero\n"));
394 if (!flag_guess_branch_prob)
395 dump_printf (MSG_MISSED_OPTIMIZATION,
396 "this can result in poorly optimized code\n");
400 return NULL;
402 else if (entry->lineno_checksum != lineno_checksum)
404 warning_at (DECL_SOURCE_LOCATION (current_function_decl),
405 OPT_Wcoverage_mismatch,
406 "source locations for function %qD have changed,"
407 " the profile data may be out of date",
408 current_function_decl);
411 return entry->counts;
414 /* Allocate NUM counters of type COUNTER. Returns nonzero if the
415 allocation succeeded. */
418 coverage_counter_alloc (unsigned counter, unsigned num)
420 if (no_coverage)
421 return 0;
423 if (!num)
424 return 1;
426 if (!fn_v_ctrs[counter])
428 tree array_type = build_array_type (get_gcov_type (), NULL_TREE);
430 fn_v_ctrs[counter]
431 = build_var (current_function_decl, array_type, counter);
434 fn_b_ctrs[counter] = fn_n_ctrs[counter];
435 fn_n_ctrs[counter] += num;
437 fn_ctr_mask |= 1 << counter;
438 return 1;
441 /* Generate a tree to access COUNTER NO. */
443 tree
444 tree_coverage_counter_ref (unsigned counter, unsigned no)
446 tree gcov_type_node = get_gcov_type ();
448 gcc_assert (no < fn_n_ctrs[counter] - fn_b_ctrs[counter]);
450 no += fn_b_ctrs[counter];
452 /* "no" here is an array index, scaled to bytes later. */
453 return build4 (ARRAY_REF, gcov_type_node, fn_v_ctrs[counter],
454 build_int_cst (integer_type_node, no), NULL, NULL);
457 /* Generate a tree to access the address of COUNTER NO. */
459 tree
460 tree_coverage_counter_addr (unsigned counter, unsigned no)
462 tree gcov_type_node = get_gcov_type ();
464 gcc_assert (no < fn_n_ctrs[counter] - fn_b_ctrs[counter]);
465 no += fn_b_ctrs[counter];
467 /* "no" here is an array index, scaled to bytes later. */
468 return build_fold_addr_expr (build4 (ARRAY_REF, gcov_type_node,
469 fn_v_ctrs[counter],
470 build_int_cst (integer_type_node, no),
471 NULL, NULL));
475 /* Generate a checksum for a string. CHKSUM is the current
476 checksum. */
478 static unsigned
479 coverage_checksum_string (unsigned chksum, const char *string)
481 int i;
482 char *dup = NULL;
484 /* Look for everything that looks if it were produced by
485 get_file_function_name and zero out the second part
486 that may result from flag_random_seed. This is not critical
487 as the checksums are used only for sanity checking. */
488 for (i = 0; string[i]; i++)
490 int offset = 0;
491 if (startswith (string + i, "_GLOBAL__N_"))
492 offset = 11;
493 if (startswith (string + i, "_GLOBAL__"))
494 offset = 9;
496 /* C++ namespaces do have scheme:
497 _GLOBAL__N_<filename>_<wrongmagicnumber>_<magicnumber>functionname
498 since filename might contain extra underscores there seems
499 to be no better chance then walk all possible offsets looking
500 for magicnumber. */
501 if (offset)
503 for (i = i + offset; string[i]; i++)
504 if (string[i]=='_')
506 int y;
508 for (y = 1; y < 9; y++)
509 if (!(string[i + y] >= '0' && string[i + y] <= '9')
510 && !(string[i + y] >= 'A' && string[i + y] <= 'F'))
511 break;
512 if (y != 9 || string[i + 9] != '_')
513 continue;
514 for (y = 10; y < 18; y++)
515 if (!(string[i + y] >= '0' && string[i + y] <= '9')
516 && !(string[i + y] >= 'A' && string[i + y] <= 'F'))
517 break;
518 if (y != 18)
519 continue;
520 if (!dup)
521 string = dup = xstrdup (string);
522 for (y = 10; y < 18; y++)
523 dup[i + y] = '0';
525 break;
529 chksum = crc32_string (chksum, string);
530 free (dup);
532 return chksum;
535 /* Compute checksum for the current function. We generate a CRC32. */
537 unsigned
538 coverage_compute_lineno_checksum (void)
540 expanded_location xloc
541 = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
542 unsigned chksum = xloc.line;
544 if (xloc.file)
545 chksum = coverage_checksum_string (chksum, xloc.file);
546 chksum = coverage_checksum_string
547 (chksum, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl)));
549 return chksum;
552 /* Compute profile ID. This is better to be unique in whole program. */
554 unsigned
555 coverage_compute_profile_id (struct cgraph_node *n)
557 unsigned chksum;
559 /* Externally visible symbols have unique name. */
560 if (TREE_PUBLIC (n->decl) || DECL_EXTERNAL (n->decl) || n->unique_name)
562 chksum = coverage_checksum_string
563 (0, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n->decl)));
565 else
567 expanded_location xloc
568 = expand_location (DECL_SOURCE_LOCATION (n->decl));
569 bool use_name_only = (param_profile_func_internal_id == 0);
571 chksum = (use_name_only ? 0 : xloc.line);
572 if (xloc.file)
573 chksum = coverage_checksum_string (chksum, xloc.file);
574 chksum = coverage_checksum_string
575 (chksum, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n->decl)));
576 if (!use_name_only && first_global_object_name)
577 chksum = coverage_checksum_string
578 (chksum, first_global_object_name);
579 chksum = coverage_checksum_string
580 (chksum, aux_base_name);
583 /* Non-negative integers are hopefully small enough to fit in all targets.
584 Gcov file formats wants non-zero function IDs. */
585 chksum = chksum & 0x7fffffff;
586 return chksum + (!chksum);
589 /* Compute cfg checksum for the function FN given as argument.
590 The checksum is calculated carefully so that
591 source code changes that doesn't affect the control flow graph
592 won't change the checksum.
593 This is to make the profile data useable across source code change.
594 The downside of this is that the compiler may use potentially
595 wrong profile data - that the source code change has non-trivial impact
596 on the validity of profile data (e.g. the reversed condition)
597 but the compiler won't detect the change and use the wrong profile data. */
599 unsigned
600 coverage_compute_cfg_checksum (struct function *fn)
602 basic_block bb;
603 unsigned chksum = n_basic_blocks_for_fn (fn);
605 FOR_EACH_BB_FN (bb, fn)
607 edge e;
608 edge_iterator ei;
609 chksum = crc32_byte (chksum, bb->index);
610 FOR_EACH_EDGE (e, ei, bb->succs)
612 chksum = crc32_byte (chksum, e->dest->index);
616 return chksum;
619 /* Begin output to the notes file for the current function.
620 Writes the function header. Returns nonzero if data should be output. */
623 coverage_begin_function (unsigned lineno_checksum, unsigned cfg_checksum)
625 expanded_location xloc;
626 unsigned long offset;
628 /* We don't need to output .gcno file unless we're under -ftest-coverage
629 (e.g. -fprofile-arcs/generate/use don't need .gcno to work). */
630 if (no_coverage || !bbg_file_name)
631 return 0;
633 xloc = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
635 /* Announce function */
636 offset = gcov_write_tag (GCOV_TAG_FUNCTION);
637 if (param_profile_func_internal_id)
638 gcov_write_unsigned (current_function_funcdef_no + 1);
639 else
641 gcc_assert (coverage_node_map_initialized_p ());
642 gcov_write_unsigned (
643 cgraph_node::get (current_function_decl)->profile_id);
646 gcov_write_unsigned (lineno_checksum);
647 gcov_write_unsigned (cfg_checksum);
648 gcov_write_string (IDENTIFIER_POINTER
649 (DECL_ASSEMBLER_NAME (current_function_decl)));
650 gcov_write_unsigned (DECL_ARTIFICIAL (current_function_decl)
651 && !DECL_FUNCTION_VERSIONED (current_function_decl)
652 && !DECL_LAMBDA_FUNCTION_P (current_function_decl));
653 gcov_write_filename (xloc.file);
654 gcov_write_unsigned (xloc.line);
655 gcov_write_unsigned (xloc.column);
657 expanded_location endloc = expand_location (cfun->function_end_locus);
659 /* Function can start in a single file and end in another one. */
660 int end_line = endloc.file == xloc.file ? endloc.line : xloc.line;
661 int end_column = endloc.file == xloc.file ? endloc.column: xloc.column;
662 gcc_assert (xloc.line <= end_line);
663 gcov_write_unsigned (end_line);
664 gcov_write_unsigned (end_column);
665 gcov_write_length (offset);
667 return !gcov_is_error ();
670 /* Finish coverage data for the current function. Verify no output
671 error has occurred. Save function coverage counts. */
673 void
674 coverage_end_function (unsigned lineno_checksum, unsigned cfg_checksum)
676 unsigned i;
678 if (bbg_file_name && gcov_is_error ())
680 warning (0, "error writing %qs", bbg_file_name);
681 unlink (bbg_file_name);
682 bbg_file_name = NULL;
685 if (fn_ctr_mask)
687 struct coverage_data *item = 0;
689 item = ggc_alloc<coverage_data> ();
691 if (param_profile_func_internal_id)
692 item->ident = current_function_funcdef_no + 1;
693 else
695 gcc_assert (coverage_node_map_initialized_p ());
696 item->ident = cgraph_node::get (cfun->decl)->profile_id;
699 item->lineno_checksum = lineno_checksum;
700 item->cfg_checksum = cfg_checksum;
702 item->fn_decl = current_function_decl;
703 item->next = 0;
704 *functions_tail = item;
705 functions_tail = &item->next;
707 for (i = 0; i != GCOV_COUNTERS; i++)
709 tree var = fn_v_ctrs[i];
711 if (item)
712 item->ctr_vars[i] = var;
713 if (var)
715 tree array_type = build_index_type (size_int (fn_n_ctrs[i] - 1));
716 array_type = build_array_type (get_gcov_type (), array_type);
717 TREE_TYPE (var) = array_type;
718 DECL_SIZE (var) = TYPE_SIZE (array_type);
719 DECL_SIZE_UNIT (var) = TYPE_SIZE_UNIT (array_type);
720 varpool_node::finalize_decl (var);
723 fn_b_ctrs[i] = fn_n_ctrs[i] = 0;
724 fn_v_ctrs[i] = NULL_TREE;
726 prg_ctr_mask |= fn_ctr_mask;
727 fn_ctr_mask = 0;
731 /* Remove coverage file if opened. */
733 void
734 coverage_remove_note_file (void)
736 if (bbg_file_name)
738 gcov_close ();
739 unlink (bbg_file_name);
743 /* Build a coverage variable of TYPE for function FN_DECL. If COUNTER
744 >= 0 it is a counter array, otherwise it is the function structure. */
746 static tree
747 build_var (tree fn_decl, tree type, int counter)
749 tree var = build_decl (BUILTINS_LOCATION, VAR_DECL, NULL_TREE, type);
750 const char *fn_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fn_decl));
751 char *buf;
752 size_t fn_name_len, len;
754 fn_name = targetm.strip_name_encoding (fn_name);
755 fn_name_len = strlen (fn_name);
756 buf = XALLOCAVEC (char, fn_name_len + 8 + sizeof (int) * 3);
758 if (counter < 0)
759 strcpy (buf, "__gcov__");
760 else
761 sprintf (buf, "__gcov%u_", counter);
762 len = strlen (buf);
763 buf[len - 1] = symbol_table::symbol_suffix_separator ();
764 memcpy (buf + len, fn_name, fn_name_len + 1);
765 DECL_NAME (var) = get_identifier (buf);
766 TREE_STATIC (var) = 1;
767 TREE_ADDRESSABLE (var) = 1;
768 DECL_NONALIASED (var) = 1;
769 SET_DECL_ALIGN (var, TYPE_ALIGN (type));
771 return var;
774 /* Creates the gcov_fn_info RECORD_TYPE. */
776 static void
777 build_fn_info_type (tree type, unsigned counters, tree gcov_info_type)
779 tree ctr_info = lang_hooks.types.make_type (RECORD_TYPE);
780 tree field, fields;
781 tree array_type;
783 gcc_assert (counters);
785 /* ctr_info::num */
786 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
787 get_gcov_unsigned_t ());
788 fields = field;
790 /* ctr_info::values */
791 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
792 build_pointer_type (get_gcov_type ()));
793 DECL_CHAIN (field) = fields;
794 fields = field;
796 finish_builtin_struct (ctr_info, "__gcov_ctr_info", fields, NULL_TREE);
798 /* key */
799 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
800 build_pointer_type (build_qualified_type
801 (gcov_info_type, TYPE_QUAL_CONST)));
802 fields = field;
804 /* ident */
805 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
806 get_gcov_unsigned_t ());
807 DECL_CHAIN (field) = fields;
808 fields = field;
810 /* lineno_checksum */
811 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
812 get_gcov_unsigned_t ());
813 DECL_CHAIN (field) = fields;
814 fields = field;
816 /* cfg checksum */
817 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
818 get_gcov_unsigned_t ());
819 DECL_CHAIN (field) = fields;
820 fields = field;
822 array_type = build_index_type (size_int (counters - 1));
823 array_type = build_array_type (ctr_info, array_type);
825 /* counters */
826 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE, array_type);
827 DECL_CHAIN (field) = fields;
828 fields = field;
830 finish_builtin_struct (type, "__gcov_fn_info", fields, NULL_TREE);
833 /* Returns a CONSTRUCTOR for a gcov_fn_info. DATA is
834 the coverage data for the function and TYPE is the gcov_fn_info
835 RECORD_TYPE. KEY is the object file key. */
837 static tree
838 build_fn_info (const struct coverage_data *data, tree type, tree key)
840 tree fields = TYPE_FIELDS (type);
841 tree ctr_type;
842 unsigned ix;
843 vec<constructor_elt, va_gc> *v1 = NULL;
844 vec<constructor_elt, va_gc> *v2 = NULL;
846 /* key */
847 CONSTRUCTOR_APPEND_ELT (v1, fields,
848 build1 (ADDR_EXPR, TREE_TYPE (fields), key));
849 fields = DECL_CHAIN (fields);
851 /* ident */
852 CONSTRUCTOR_APPEND_ELT (v1, fields,
853 build_int_cstu (get_gcov_unsigned_t (),
854 data->ident));
855 fields = DECL_CHAIN (fields);
857 /* lineno_checksum */
858 CONSTRUCTOR_APPEND_ELT (v1, fields,
859 build_int_cstu (get_gcov_unsigned_t (),
860 data->lineno_checksum));
861 fields = DECL_CHAIN (fields);
863 /* cfg_checksum */
864 CONSTRUCTOR_APPEND_ELT (v1, fields,
865 build_int_cstu (get_gcov_unsigned_t (),
866 data->cfg_checksum));
867 fields = DECL_CHAIN (fields);
869 /* counters */
870 ctr_type = TREE_TYPE (TREE_TYPE (fields));
871 for (ix = 0; ix != GCOV_COUNTERS; ix++)
872 if (prg_ctr_mask & (1 << ix))
874 vec<constructor_elt, va_gc> *ctr = NULL;
875 tree var = data->ctr_vars[ix];
876 unsigned count = 0;
878 if (var)
879 count
880 = tree_to_shwi (TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (var))))
881 + 1;
883 CONSTRUCTOR_APPEND_ELT (ctr, TYPE_FIELDS (ctr_type),
884 build_int_cstu (get_gcov_unsigned_t (),
885 count));
887 if (var)
888 CONSTRUCTOR_APPEND_ELT (ctr, DECL_CHAIN (TYPE_FIELDS (ctr_type)),
889 build_fold_addr_expr (var));
891 CONSTRUCTOR_APPEND_ELT (v2, NULL, build_constructor (ctr_type, ctr));
894 CONSTRUCTOR_APPEND_ELT (v1, fields,
895 build_constructor (TREE_TYPE (fields), v2));
897 return build_constructor (type, v1);
900 /* Create gcov_info struct. TYPE is the incomplete RECORD_TYPE to be
901 completed, and FN_INFO_PTR_TYPE is a pointer to the function info type. */
903 static void
904 build_info_type (tree type, tree fn_info_ptr_type)
906 tree field, fields = NULL_TREE;
907 tree merge_fn_type;
909 /* Version ident */
910 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
911 get_gcov_unsigned_t ());
912 DECL_CHAIN (field) = fields;
913 fields = field;
915 /* next pointer */
916 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
917 build_pointer_type (build_qualified_type
918 (type, TYPE_QUAL_CONST)));
919 DECL_CHAIN (field) = fields;
920 fields = field;
922 /* stamp */
923 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
924 get_gcov_unsigned_t ());
925 DECL_CHAIN (field) = fields;
926 fields = field;
928 /* Filename */
929 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
930 build_pointer_type (build_qualified_type
931 (char_type_node, TYPE_QUAL_CONST)));
932 DECL_CHAIN (field) = fields;
933 fields = field;
935 /* merge fn array */
936 merge_fn_type
937 = build_function_type_list (void_type_node,
938 build_pointer_type (get_gcov_type ()),
939 get_gcov_unsigned_t (), NULL_TREE);
940 merge_fn_type
941 = build_array_type (build_pointer_type (merge_fn_type),
942 build_index_type (size_int (GCOV_COUNTERS - 1)));
943 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
944 merge_fn_type);
945 DECL_CHAIN (field) = fields;
946 fields = field;
948 /* n_functions */
949 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
950 get_gcov_unsigned_t ());
951 DECL_CHAIN (field) = fields;
952 fields = field;
954 /* function_info pointer pointer */
955 fn_info_ptr_type = build_pointer_type
956 (build_qualified_type (fn_info_ptr_type, TYPE_QUAL_CONST));
957 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
958 fn_info_ptr_type);
959 DECL_CHAIN (field) = fields;
960 fields = field;
962 finish_builtin_struct (type, "__gcov_info", fields, NULL_TREE);
965 /* Returns a CONSTRUCTOR for the gcov_info object. INFO_TYPE is the
966 gcov_info structure type, FN_ARY is the array of pointers to
967 function info objects. */
969 static tree
970 build_info (tree info_type, tree fn_ary)
972 tree info_fields = TYPE_FIELDS (info_type);
973 tree merge_fn_type, n_funcs;
974 unsigned ix;
975 tree filename_string;
976 int da_file_name_len;
977 vec<constructor_elt, va_gc> *v1 = NULL;
978 vec<constructor_elt, va_gc> *v2 = NULL;
980 /* Version ident */
981 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
982 build_int_cstu (TREE_TYPE (info_fields),
983 GCOV_VERSION));
984 info_fields = DECL_CHAIN (info_fields);
986 /* next -- NULL */
987 CONSTRUCTOR_APPEND_ELT (v1, info_fields, null_pointer_node);
988 info_fields = DECL_CHAIN (info_fields);
990 /* stamp */
991 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
992 build_int_cstu (TREE_TYPE (info_fields),
993 bbg_file_stamp));
994 info_fields = DECL_CHAIN (info_fields);
996 /* Filename */
997 da_file_name_len = strlen (da_file_name);
998 filename_string = build_string (da_file_name_len + 1, da_file_name);
999 TREE_TYPE (filename_string) = build_array_type
1000 (char_type_node, build_index_type (size_int (da_file_name_len)));
1001 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
1002 build1 (ADDR_EXPR, TREE_TYPE (info_fields),
1003 filename_string));
1004 info_fields = DECL_CHAIN (info_fields);
1006 /* merge fn array -- NULL slots indicate unmeasured counters */
1007 merge_fn_type = TREE_TYPE (TREE_TYPE (info_fields));
1008 for (ix = 0; ix != GCOV_COUNTERS; ix++)
1010 tree ptr = null_pointer_node;
1012 if ((1u << ix) & prg_ctr_mask)
1014 tree merge_fn = build_decl (BUILTINS_LOCATION,
1015 FUNCTION_DECL,
1016 get_identifier (ctr_merge_functions[ix]),
1017 TREE_TYPE (merge_fn_type));
1018 DECL_EXTERNAL (merge_fn) = 1;
1019 TREE_PUBLIC (merge_fn) = 1;
1020 DECL_ARTIFICIAL (merge_fn) = 1;
1021 TREE_NOTHROW (merge_fn) = 1;
1022 /* Initialize assembler name so we can stream out. */
1023 DECL_ASSEMBLER_NAME (merge_fn);
1024 ptr = build1 (ADDR_EXPR, merge_fn_type, merge_fn);
1026 CONSTRUCTOR_APPEND_ELT (v2, NULL, ptr);
1028 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
1029 build_constructor (TREE_TYPE (info_fields), v2));
1030 info_fields = DECL_CHAIN (info_fields);
1032 /* n_functions */
1033 n_funcs = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (fn_ary)));
1034 n_funcs = fold_build2 (PLUS_EXPR, TREE_TYPE (info_fields),
1035 n_funcs, size_one_node);
1036 CONSTRUCTOR_APPEND_ELT (v1, info_fields, n_funcs);
1037 info_fields = DECL_CHAIN (info_fields);
1039 /* functions */
1040 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
1041 build1 (ADDR_EXPR, TREE_TYPE (info_fields), fn_ary));
1042 info_fields = DECL_CHAIN (info_fields);
1044 gcc_assert (!info_fields);
1045 return build_constructor (info_type, v1);
1048 /* Generate the constructor function to call __gcov_init. */
1050 static void
1051 build_init_ctor (tree gcov_info_type)
1053 tree ctor, stmt, init_fn;
1055 /* Build a decl for __gcov_init. */
1056 init_fn = build_pointer_type (gcov_info_type);
1057 init_fn = build_function_type_list (void_type_node, init_fn, NULL);
1058 init_fn = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
1059 get_identifier ("__gcov_init"), init_fn);
1060 TREE_PUBLIC (init_fn) = 1;
1061 DECL_EXTERNAL (init_fn) = 1;
1062 DECL_ASSEMBLER_NAME (init_fn);
1064 /* Generate a call to __gcov_init(&gcov_info). */
1065 ctor = NULL;
1066 stmt = build_fold_addr_expr (gcov_info_var);
1067 stmt = build_call_expr (init_fn, 1, stmt);
1068 append_to_statement_list (stmt, &ctor);
1070 /* Generate a constructor to run it. */
1071 int priority = SUPPORTS_INIT_PRIORITY
1072 ? MAX_RESERVED_INIT_PRIORITY: DEFAULT_INIT_PRIORITY;
1073 cgraph_build_static_cdtor ('I', ctor, priority);
1076 /* Generate the destructor function to call __gcov_exit. */
1078 static void
1079 build_gcov_exit_decl (void)
1081 tree init_fn = build_function_type_list (void_type_node, NULL);
1082 init_fn = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
1083 get_identifier ("__gcov_exit"), init_fn);
1084 TREE_PUBLIC (init_fn) = 1;
1085 DECL_EXTERNAL (init_fn) = 1;
1086 DECL_ASSEMBLER_NAME (init_fn);
1088 /* Generate a call to __gcov_exit (). */
1089 tree dtor = NULL;
1090 tree stmt = build_call_expr (init_fn, 0);
1091 append_to_statement_list (stmt, &dtor);
1093 /* Generate a destructor to run it. */
1094 int priority = SUPPORTS_INIT_PRIORITY
1095 ? MAX_RESERVED_INIT_PRIORITY: DEFAULT_INIT_PRIORITY;
1097 cgraph_build_static_cdtor ('D', dtor, priority);
1100 /* Generate the pointer to the gcov_info_var in a dedicated section. */
1102 static void
1103 build_gcov_info_var_registration (tree gcov_info_type)
1105 tree var = build_decl (BUILTINS_LOCATION,
1106 VAR_DECL, NULL_TREE,
1107 build_pointer_type (gcov_info_type));
1108 TREE_STATIC (var) = 1;
1109 TREE_READONLY (var) = 1;
1110 char name_buf[32];
1111 ASM_GENERATE_INTERNAL_LABEL (name_buf, "LPBX", 2);
1112 DECL_NAME (var) = get_identifier (name_buf);
1113 get_section (profile_info_section, SECTION_UNNAMED, NULL);
1114 set_decl_section_name (var, profile_info_section);
1115 DECL_INITIAL (var) = build_fold_addr_expr (gcov_info_var);
1116 varpool_node::finalize_decl (var);
1119 /* Create the gcov_info types and object. Generate the constructor
1120 function to call __gcov_init. Does not generate the initializer
1121 for the object. Returns TRUE if coverage data is being emitted. */
1123 static bool
1124 coverage_obj_init (void)
1126 tree gcov_info_type;
1127 unsigned n_counters = 0;
1128 unsigned ix;
1129 struct coverage_data *fn;
1130 struct coverage_data **fn_prev;
1131 char name_buf[32];
1133 no_coverage = 1; /* Disable any further coverage. */
1135 if (!prg_ctr_mask)
1136 return false;
1138 if (symtab->dump_file)
1139 fprintf (symtab->dump_file, "Using data file %s\n", da_file_name);
1141 /* Prune functions. */
1142 for (fn_prev = &functions_head; (fn = *fn_prev);)
1143 if (DECL_STRUCT_FUNCTION (fn->fn_decl))
1144 fn_prev = &fn->next;
1145 else
1146 /* The function is not being emitted, remove from list. */
1147 *fn_prev = fn->next;
1149 if (functions_head == NULL)
1150 return false;
1152 for (ix = 0; ix != GCOV_COUNTERS; ix++)
1153 if ((1u << ix) & prg_ctr_mask)
1154 n_counters++;
1156 /* Build the info and fn_info types. These are mutually recursive. */
1157 gcov_info_type = lang_hooks.types.make_type (RECORD_TYPE);
1158 gcov_fn_info_type = lang_hooks.types.make_type (RECORD_TYPE);
1159 build_fn_info_type (gcov_fn_info_type, n_counters, gcov_info_type);
1160 gcov_info_type = lang_hooks.types.make_type (RECORD_TYPE);
1161 gcov_fn_info_ptr_type = build_pointer_type
1162 (build_qualified_type (gcov_fn_info_type, TYPE_QUAL_CONST));
1163 build_info_type (gcov_info_type, gcov_fn_info_ptr_type);
1165 /* Build the gcov info var, this is referred to in its own
1166 initializer. */
1167 gcov_info_var = build_decl (BUILTINS_LOCATION,
1168 VAR_DECL, NULL_TREE, gcov_info_type);
1169 TREE_STATIC (gcov_info_var) = 1;
1170 ASM_GENERATE_INTERNAL_LABEL (name_buf, "LPBX", 0);
1171 DECL_NAME (gcov_info_var) = get_identifier (name_buf);
1173 if (profile_info_section)
1174 build_gcov_info_var_registration (gcov_info_type);
1175 else
1177 build_init_ctor (gcov_info_type);
1178 build_gcov_exit_decl ();
1181 return true;
1184 /* Generate the coverage function info for FN and DATA. Append a
1185 pointer to that object to CTOR and return the appended CTOR. */
1187 static vec<constructor_elt, va_gc> *
1188 coverage_obj_fn (vec<constructor_elt, va_gc> *ctor, tree fn,
1189 struct coverage_data const *data)
1191 tree init = build_fn_info (data, gcov_fn_info_type, gcov_info_var);
1192 tree var = build_var (fn, gcov_fn_info_type, -1);
1194 DECL_INITIAL (var) = init;
1195 varpool_node::finalize_decl (var);
1197 CONSTRUCTOR_APPEND_ELT (ctor, NULL,
1198 build1 (ADDR_EXPR, gcov_fn_info_ptr_type, var));
1199 return ctor;
1202 /* Finalize the coverage data. Generates the array of pointers to
1203 function objects from CTOR. Generate the gcov_info initializer. */
1205 static void
1206 coverage_obj_finish (vec<constructor_elt, va_gc> *ctor)
1208 unsigned n_functions = vec_safe_length (ctor);
1209 tree fn_info_ary_type = build_array_type
1210 (build_qualified_type (gcov_fn_info_ptr_type, TYPE_QUAL_CONST),
1211 build_index_type (size_int (n_functions - 1)));
1212 tree fn_info_ary = build_decl (BUILTINS_LOCATION, VAR_DECL, NULL_TREE,
1213 fn_info_ary_type);
1214 char name_buf[32];
1216 TREE_STATIC (fn_info_ary) = 1;
1217 ASM_GENERATE_INTERNAL_LABEL (name_buf, "LPBX", 1);
1218 DECL_NAME (fn_info_ary) = get_identifier (name_buf);
1219 DECL_INITIAL (fn_info_ary) = build_constructor (fn_info_ary_type, ctor);
1220 varpool_node::finalize_decl (fn_info_ary);
1222 DECL_INITIAL (gcov_info_var)
1223 = build_info (TREE_TYPE (gcov_info_var), fn_info_ary);
1224 varpool_node::finalize_decl (gcov_info_var);
1227 /* Perform file-level initialization. Read in data file, generate name
1228 of notes file. */
1230 void
1231 coverage_init (const char *filename)
1233 const char *original_filename = filename;
1234 int original_len = strlen (original_filename);
1235 #if HAVE_DOS_BASED_FILE_SYSTEM
1236 const char *separator = "\\";
1237 #else
1238 const char *separator = "/";
1239 #endif
1240 int len = strlen (filename);
1241 int prefix_len = 0;
1243 /* Since coverage_init is invoked very early, before the pass
1244 manager, we need to set up the dumping explicitly. This is
1245 similar to the handling in finish_optimization_passes. */
1246 int profile_pass_num =
1247 g->get_passes ()->get_pass_profile ()->static_pass_number;
1248 g->get_dumps ()->dump_start (profile_pass_num, NULL);
1250 if (!IS_ABSOLUTE_PATH (filename))
1252 /* When a profile_data_prefix is provided, then mangle full path
1253 of filename in order to prevent file path clashing. */
1254 if (profile_data_prefix)
1256 filename = concat (getpwd (), separator, filename, NULL);
1257 if (profile_prefix_path)
1259 if (startswith (filename, profile_prefix_path))
1261 filename += strlen (profile_prefix_path);
1262 while (*filename == *separator)
1263 filename++;
1265 else
1266 warning (0, "filename %qs does not start with profile "
1267 "prefix %qs", filename, profile_prefix_path);
1269 filename = mangle_path (filename);
1270 len = strlen (filename);
1272 else
1273 profile_data_prefix = getpwd ();
1276 if (profile_data_prefix)
1277 prefix_len = strlen (profile_data_prefix);
1279 /* Name of da file. */
1280 da_file_name = XNEWVEC (char, len + strlen (GCOV_DATA_SUFFIX)
1281 + prefix_len + 2);
1283 if (profile_data_prefix)
1285 memcpy (da_file_name, profile_data_prefix, prefix_len);
1286 da_file_name[prefix_len++] = *separator;
1288 memcpy (da_file_name + prefix_len, filename, len);
1289 strcpy (da_file_name + prefix_len + len, GCOV_DATA_SUFFIX);
1291 bbg_file_stamp = local_tick;
1293 if (flag_auto_profile)
1294 read_autofdo_file ();
1295 else if (flag_branch_probabilities)
1296 read_counts_file ();
1298 /* Name of bbg file. */
1299 if (flag_test_coverage && !flag_compare_debug)
1301 if (profile_note_location)
1302 bbg_file_name = xstrdup (profile_note_location);
1303 else
1305 bbg_file_name = XNEWVEC (char, original_len + strlen (GCOV_NOTE_SUFFIX) + 1);
1306 memcpy (bbg_file_name, original_filename, original_len);
1307 strcpy (bbg_file_name + original_len, GCOV_NOTE_SUFFIX);
1310 if (!gcov_open (bbg_file_name, -1))
1312 error ("cannot open %s", bbg_file_name);
1313 bbg_file_name = NULL;
1315 else
1317 gcov_write_unsigned (GCOV_NOTE_MAGIC);
1318 gcov_write_unsigned (GCOV_VERSION);
1319 gcov_write_unsigned (bbg_file_stamp);
1320 gcov_write_string (getpwd ());
1322 /* Do not support has_unexecuted_blocks for Ada. */
1323 gcov_write_unsigned (strcmp (lang_hooks.name, "GNU Ada") != 0);
1327 g->get_dumps ()->dump_finish (profile_pass_num);
1330 /* Performs file-level cleanup. Close notes file, generate coverage
1331 variables and constructor. */
1333 void
1334 coverage_finish (void)
1336 if (bbg_file_name && gcov_close ())
1337 unlink (bbg_file_name);
1339 if (!flag_branch_probabilities && flag_test_coverage
1340 && (!local_tick || local_tick == (unsigned)-1))
1341 /* Only remove the da file, if we're emitting coverage code and
1342 cannot uniquely stamp it. If we can stamp it, libgcov will DTRT. */
1343 unlink (da_file_name);
1345 if (coverage_obj_init ())
1347 vec<constructor_elt, va_gc> *fn_ctor = NULL;
1348 struct coverage_data *fn;
1350 for (fn = functions_head; fn; fn = fn->next)
1351 fn_ctor = coverage_obj_fn (fn_ctor, fn->fn_decl, fn);
1352 coverage_obj_finish (fn_ctor);
1355 XDELETEVEC (da_file_name);
1356 da_file_name = NULL;
1359 #include "gt-coverage.h"