PR target/66563
[official-gcc.git] / gcc / coverage.c
blob40d788f368cfb514d6007ec5705c0b974aa242f9
1 /* Read and write coverage files, and associated functionality.
2 Copyright (C) 1990-2015 Free Software Foundation, Inc.
3 Contributed by James E. Wilson, UC Berkeley/Cygnus Support;
4 based on some ideas from Dain Samples of UC Berkeley.
5 Further mangling by Bob Manson, Cygnus Support.
6 Further mangled by Nathan Sidwell, CodeSourcery
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
25 #define GCOV_LINKAGE
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "rtl.h"
32 #include "alias.h"
33 #include "symtab.h"
34 #include "tree.h"
35 #include "fold-const.h"
36 #include "stringpool.h"
37 #include "stor-layout.h"
38 #include "flags.h"
39 #include "output.h"
40 #include "regs.h"
41 #include "hard-reg-set.h"
42 #include "function.h"
43 #include "insn-config.h"
44 #include "expmed.h"
45 #include "dojump.h"
46 #include "explow.h"
47 #include "calls.h"
48 #include "emit-rtl.h"
49 #include "varasm.h"
50 #include "stmt.h"
51 #include "expr.h"
52 #include "predict.h"
53 #include "dominance.h"
54 #include "cfg.h"
55 #include "basic-block.h"
56 #include "toplev.h"
57 #include "tm_p.h"
58 #include "coverage.h"
59 #include "langhooks.h"
60 #include "tree-iterator.h"
61 #include "context.h"
62 #include "pass_manager.h"
63 #include "tree-pass.h"
64 #include "plugin-api.h"
65 #include "ipa-ref.h"
66 #include "cgraph.h"
67 #include "dumpfile.h"
68 #include "diagnostic-core.h"
69 #include "intl.h"
70 #include "filenames.h"
71 #include "target.h"
72 #include "params.h"
73 #include "auto-profile.h"
75 #include "gcov-io.h"
76 #include "gcov-io.c"
78 struct GTY((chain_next ("%h.next"))) coverage_data
80 struct coverage_data *next; /* next function */
81 unsigned ident; /* function ident */
82 unsigned lineno_checksum; /* function lineno checksum */
83 unsigned cfg_checksum; /* function cfg checksum */
84 tree fn_decl; /* the function decl */
85 tree ctr_vars[GCOV_COUNTERS]; /* counter variables. */
88 /* Counts information for a function. */
89 typedef struct counts_entry
91 /* We hash by */
92 unsigned ident;
93 unsigned ctr;
95 /* Store */
96 unsigned lineno_checksum;
97 unsigned cfg_checksum;
98 gcov_type *counts;
99 struct gcov_ctr_summary summary;
101 /* hash_table support. */
102 typedef counts_entry *value_type;
103 typedef counts_entry *compare_type;
104 static inline hashval_t hash (const counts_entry *);
105 static int equal (const counts_entry *, const counts_entry *);
106 static void remove (counts_entry *);
107 } counts_entry_t;
109 static GTY(()) struct coverage_data *functions_head = 0;
110 static struct coverage_data **functions_tail = &functions_head;
111 static unsigned no_coverage = 0;
113 /* Cumulative counter information for whole program. */
114 static unsigned prg_ctr_mask; /* Mask of counter types generated. */
116 /* Counter information for current function. */
117 static unsigned fn_ctr_mask; /* Mask of counters used. */
118 static GTY(()) tree fn_v_ctrs[GCOV_COUNTERS]; /* counter variables. */
119 static unsigned fn_n_ctrs[GCOV_COUNTERS]; /* Counters allocated. */
120 static unsigned fn_b_ctrs[GCOV_COUNTERS]; /* Allocation base. */
122 /* Coverage info VAR_DECL and function info type nodes. */
123 static GTY(()) tree gcov_info_var;
124 static GTY(()) tree gcov_fn_info_type;
125 static GTY(()) tree gcov_fn_info_ptr_type;
127 /* Name of the notes (gcno) output file. The "bbg" prefix is for
128 historical reasons, when the notes file contained only the
129 basic block graph notes.
130 If this is NULL we're not writing to the notes file. */
131 static char *bbg_file_name;
133 /* File stamp for notes file. */
134 static unsigned bbg_file_stamp;
136 /* Name of the count data (gcda) file. */
137 static char *da_file_name;
139 /* The names of merge functions for counters. */
140 #define STR(str) #str
141 #define DEF_GCOV_COUNTER(COUNTER, NAME, FN_TYPE) STR(__gcov_merge ## FN_TYPE),
142 static const char *const ctr_merge_functions[GCOV_COUNTERS] = {
143 #include "gcov-counter.def"
145 #undef DEF_GCOV_COUNTER
146 #undef STR
148 #define DEF_GCOV_COUNTER(COUNTER, NAME, FN_TYPE) NAME,
149 static const char *const ctr_names[GCOV_COUNTERS] = {
150 #include "gcov-counter.def"
152 #undef DEF_GCOV_COUNTER
154 /* Forward declarations. */
155 static void read_counts_file (void);
156 static tree build_var (tree, tree, int);
157 static void build_fn_info_type (tree, unsigned, tree);
158 static void build_info_type (tree, tree);
159 static tree build_fn_info (const struct coverage_data *, tree, tree);
160 static tree build_info (tree, tree);
161 static bool coverage_obj_init (void);
162 static vec<constructor_elt, va_gc> *coverage_obj_fn
163 (vec<constructor_elt, va_gc> *, tree, struct coverage_data const *);
164 static void coverage_obj_finish (vec<constructor_elt, va_gc> *);
166 /* Return the type node for gcov_type. */
168 tree
169 get_gcov_type (void)
171 machine_mode mode = smallest_mode_for_size (GCOV_TYPE_SIZE, MODE_INT);
172 return lang_hooks.types.type_for_mode (mode, false);
175 /* Return the type node for gcov_unsigned_t. */
177 static tree
178 get_gcov_unsigned_t (void)
180 machine_mode mode = smallest_mode_for_size (32, MODE_INT);
181 return lang_hooks.types.type_for_mode (mode, true);
184 inline hashval_t
185 counts_entry::hash (const counts_entry *entry)
187 return entry->ident * GCOV_COUNTERS + entry->ctr;
190 inline int
191 counts_entry::equal (const counts_entry *entry1, const counts_entry *entry2)
193 return entry1->ident == entry2->ident && entry1->ctr == entry2->ctr;
196 inline void
197 counts_entry::remove (counts_entry *entry)
199 free (entry->counts);
200 free (entry);
203 /* Hash table of count data. */
204 static hash_table<counts_entry> *counts_hash;
206 /* Read in the counts file, if available. */
208 static void
209 read_counts_file (void)
211 gcov_unsigned_t fn_ident = 0;
212 struct gcov_summary summary;
213 unsigned new_summary = 1;
214 gcov_unsigned_t tag;
215 int is_error = 0;
216 unsigned lineno_checksum = 0;
217 unsigned cfg_checksum = 0;
219 if (!gcov_open (da_file_name, 1))
220 return;
222 if (!gcov_magic (gcov_read_unsigned (), GCOV_DATA_MAGIC))
224 warning (0, "%qs is not a gcov data file", da_file_name);
225 gcov_close ();
226 return;
228 else if ((tag = gcov_read_unsigned ()) != GCOV_VERSION)
230 char v[4], e[4];
232 GCOV_UNSIGNED2STRING (v, tag);
233 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
235 warning (0, "%qs is version %q.*s, expected version %q.*s",
236 da_file_name, 4, v, 4, e);
237 gcov_close ();
238 return;
241 /* Read the stamp, used for creating a generation count. */
242 tag = gcov_read_unsigned ();
243 bbg_file_stamp = crc32_unsigned (bbg_file_stamp, tag);
245 counts_hash = new hash_table<counts_entry> (10);
246 while ((tag = gcov_read_unsigned ()))
248 gcov_unsigned_t length;
249 gcov_position_t offset;
251 length = gcov_read_unsigned ();
252 offset = gcov_position ();
253 if (tag == GCOV_TAG_FUNCTION)
255 if (length)
257 fn_ident = gcov_read_unsigned ();
258 lineno_checksum = gcov_read_unsigned ();
259 cfg_checksum = gcov_read_unsigned ();
261 else
262 fn_ident = lineno_checksum = cfg_checksum = 0;
263 new_summary = 1;
265 else if (tag == GCOV_TAG_PROGRAM_SUMMARY)
267 struct gcov_summary sum;
268 unsigned ix;
270 if (new_summary)
271 memset (&summary, 0, sizeof (summary));
273 gcov_read_summary (&sum);
274 for (ix = 0; ix != GCOV_COUNTERS_SUMMABLE; ix++)
276 summary.ctrs[ix].runs += sum.ctrs[ix].runs;
277 summary.ctrs[ix].sum_all += sum.ctrs[ix].sum_all;
278 if (summary.ctrs[ix].run_max < sum.ctrs[ix].run_max)
279 summary.ctrs[ix].run_max = sum.ctrs[ix].run_max;
280 summary.ctrs[ix].sum_max += sum.ctrs[ix].sum_max;
282 if (new_summary)
283 memcpy (summary.ctrs[GCOV_COUNTER_ARCS].histogram,
284 sum.ctrs[GCOV_COUNTER_ARCS].histogram,
285 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
286 else
287 gcov_histogram_merge (summary.ctrs[GCOV_COUNTER_ARCS].histogram,
288 sum.ctrs[GCOV_COUNTER_ARCS].histogram);
289 new_summary = 0;
291 else if (GCOV_TAG_IS_COUNTER (tag) && fn_ident)
293 counts_entry_t **slot, *entry, elt;
294 unsigned n_counts = GCOV_TAG_COUNTER_NUM (length);
295 unsigned ix;
297 elt.ident = fn_ident;
298 elt.ctr = GCOV_COUNTER_FOR_TAG (tag);
300 slot = counts_hash->find_slot (&elt, INSERT);
301 entry = *slot;
302 if (!entry)
304 *slot = entry = XCNEW (counts_entry_t);
305 entry->ident = fn_ident;
306 entry->ctr = elt.ctr;
307 entry->lineno_checksum = lineno_checksum;
308 entry->cfg_checksum = cfg_checksum;
309 if (elt.ctr < GCOV_COUNTERS_SUMMABLE)
310 entry->summary = summary.ctrs[elt.ctr];
311 entry->summary.num = n_counts;
312 entry->counts = XCNEWVEC (gcov_type, n_counts);
314 else if (entry->lineno_checksum != lineno_checksum
315 || entry->cfg_checksum != cfg_checksum)
317 error ("Profile data for function %u is corrupted", fn_ident);
318 error ("checksum is (%x,%x) instead of (%x,%x)",
319 entry->lineno_checksum, entry->cfg_checksum,
320 lineno_checksum, cfg_checksum);
321 delete counts_hash;
322 counts_hash = NULL;
323 break;
325 else if (entry->summary.num != n_counts)
327 error ("Profile data for function %u is corrupted", fn_ident);
328 error ("number of counters is %d instead of %d", entry->summary.num, n_counts);
329 delete counts_hash;
330 counts_hash = NULL;
331 break;
333 else if (elt.ctr >= GCOV_COUNTERS_SUMMABLE)
335 error ("cannot merge separate %s counters for function %u",
336 ctr_names[elt.ctr], fn_ident);
337 goto skip_merge;
339 else
341 entry->summary.runs += summary.ctrs[elt.ctr].runs;
342 entry->summary.sum_all += summary.ctrs[elt.ctr].sum_all;
343 if (entry->summary.run_max < summary.ctrs[elt.ctr].run_max)
344 entry->summary.run_max = summary.ctrs[elt.ctr].run_max;
345 entry->summary.sum_max += summary.ctrs[elt.ctr].sum_max;
347 for (ix = 0; ix != n_counts; ix++)
348 entry->counts[ix] += gcov_read_counter ();
349 skip_merge:;
351 gcov_sync (offset, length);
352 if ((is_error = gcov_is_error ()))
354 error (is_error < 0 ? "%qs has overflowed" : "%qs is corrupted",
355 da_file_name);
356 delete counts_hash;
357 counts_hash = NULL;
358 break;
362 gcov_close ();
365 /* Returns the counters for a particular tag. */
367 gcov_type *
368 get_coverage_counts (unsigned counter, unsigned expected,
369 unsigned cfg_checksum, unsigned lineno_checksum,
370 const struct gcov_ctr_summary **summary)
372 counts_entry_t *entry, elt;
374 /* No hash table, no counts. */
375 if (!counts_hash)
377 static int warned = 0;
379 if (!warned++ && dump_enabled_p ())
380 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
381 (flag_guess_branch_prob
382 ? "file %s not found, execution counts estimated\n"
383 : "file %s not found, execution counts assumed to "
384 "be zero\n"),
385 da_file_name);
386 return NULL;
388 if (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID))
389 elt.ident = current_function_funcdef_no + 1;
390 else
392 gcc_assert (coverage_node_map_initialized_p ());
393 elt.ident = cgraph_node::get (cfun->decl)->profile_id;
395 elt.ctr = counter;
396 entry = counts_hash->find (&elt);
397 if (!entry || !entry->summary.num)
398 /* The function was not emitted, or is weak and not chosen in the
399 final executable. Silently fail, because there's nothing we
400 can do about it. */
401 return NULL;
403 if (entry->cfg_checksum != cfg_checksum
404 || entry->summary.num != expected)
406 static int warned = 0;
407 bool warning_printed = false;
408 tree id = DECL_ASSEMBLER_NAME (current_function_decl);
410 warning_printed =
411 warning_at (input_location, OPT_Wcoverage_mismatch,
412 "the control flow of function %qE does not match "
413 "its profile data (counter %qs)", id, ctr_names[counter]);
414 if (warning_printed && dump_enabled_p ())
416 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
417 "use -Wno-error=coverage-mismatch to tolerate "
418 "the mismatch but performance may drop if the "
419 "function is hot\n");
421 if (!seen_error ()
422 && !warned++)
424 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
425 "coverage mismatch ignored\n");
426 dump_printf (MSG_OPTIMIZED_LOCATIONS,
427 flag_guess_branch_prob
428 ? G_("execution counts estimated\n")
429 : G_("execution counts assumed to be zero\n"));
430 if (!flag_guess_branch_prob)
431 dump_printf (MSG_OPTIMIZED_LOCATIONS,
432 "this can result in poorly optimized code\n");
436 return NULL;
438 else if (entry->lineno_checksum != lineno_checksum)
440 warning (OPT_Wcoverage_mismatch,
441 "source locations for function %qE have changed,"
442 " the profile data may be out of date",
443 DECL_ASSEMBLER_NAME (current_function_decl));
446 if (summary)
447 *summary = &entry->summary;
449 return entry->counts;
452 /* Allocate NUM counters of type COUNTER. Returns nonzero if the
453 allocation succeeded. */
456 coverage_counter_alloc (unsigned counter, unsigned num)
458 if (no_coverage)
459 return 0;
461 if (!num)
462 return 1;
464 if (!fn_v_ctrs[counter])
466 tree array_type = build_array_type (get_gcov_type (), NULL_TREE);
468 fn_v_ctrs[counter]
469 = build_var (current_function_decl, array_type, counter);
472 fn_b_ctrs[counter] = fn_n_ctrs[counter];
473 fn_n_ctrs[counter] += num;
475 fn_ctr_mask |= 1 << counter;
476 return 1;
479 /* Generate a tree to access COUNTER NO. */
481 tree
482 tree_coverage_counter_ref (unsigned counter, unsigned no)
484 tree gcov_type_node = get_gcov_type ();
486 gcc_assert (no < fn_n_ctrs[counter] - fn_b_ctrs[counter]);
488 no += fn_b_ctrs[counter];
490 /* "no" here is an array index, scaled to bytes later. */
491 return build4 (ARRAY_REF, gcov_type_node, fn_v_ctrs[counter],
492 build_int_cst (integer_type_node, no), NULL, NULL);
495 /* Generate a tree to access the address of COUNTER NO. */
497 tree
498 tree_coverage_counter_addr (unsigned counter, unsigned no)
500 tree gcov_type_node = get_gcov_type ();
502 gcc_assert (no < fn_n_ctrs[counter] - fn_b_ctrs[counter]);
503 no += fn_b_ctrs[counter];
505 /* "no" here is an array index, scaled to bytes later. */
506 return build_fold_addr_expr (build4 (ARRAY_REF, gcov_type_node,
507 fn_v_ctrs[counter],
508 build_int_cst (integer_type_node, no),
509 NULL, NULL));
513 /* Generate a checksum for a string. CHKSUM is the current
514 checksum. */
516 static unsigned
517 coverage_checksum_string (unsigned chksum, const char *string)
519 int i;
520 char *dup = NULL;
522 /* Look for everything that looks if it were produced by
523 get_file_function_name and zero out the second part
524 that may result from flag_random_seed. This is not critical
525 as the checksums are used only for sanity checking. */
526 for (i = 0; string[i]; i++)
528 int offset = 0;
529 if (!strncmp (string + i, "_GLOBAL__N_", 11))
530 offset = 11;
531 if (!strncmp (string + i, "_GLOBAL__", 9))
532 offset = 9;
534 /* C++ namespaces do have scheme:
535 _GLOBAL__N_<filename>_<wrongmagicnumber>_<magicnumber>functionname
536 since filename might contain extra underscores there seems
537 to be no better chance then walk all possible offsets looking
538 for magicnumber. */
539 if (offset)
541 for (i = i + offset; string[i]; i++)
542 if (string[i]=='_')
544 int y;
546 for (y = 1; y < 9; y++)
547 if (!(string[i + y] >= '0' && string[i + y] <= '9')
548 && !(string[i + y] >= 'A' && string[i + y] <= 'F'))
549 break;
550 if (y != 9 || string[i + 9] != '_')
551 continue;
552 for (y = 10; y < 18; y++)
553 if (!(string[i + y] >= '0' && string[i + y] <= '9')
554 && !(string[i + y] >= 'A' && string[i + y] <= 'F'))
555 break;
556 if (y != 18)
557 continue;
558 if (!dup)
559 string = dup = xstrdup (string);
560 for (y = 10; y < 18; y++)
561 dup[i + y] = '0';
563 break;
567 chksum = crc32_string (chksum, string);
568 free (dup);
570 return chksum;
573 /* Compute checksum for the current function. We generate a CRC32. */
575 unsigned
576 coverage_compute_lineno_checksum (void)
578 expanded_location xloc
579 = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
580 unsigned chksum = xloc.line;
582 chksum = coverage_checksum_string (chksum, xloc.file);
583 chksum = coverage_checksum_string
584 (chksum, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl)));
586 return chksum;
589 /* Compute profile ID. This is better to be unique in whole program. */
591 unsigned
592 coverage_compute_profile_id (struct cgraph_node *n)
594 unsigned chksum;
596 /* Externally visible symbols have unique name. */
597 if (TREE_PUBLIC (n->decl) || DECL_EXTERNAL (n->decl) || n->unique_name)
599 chksum = coverage_checksum_string
600 (0, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n->decl)));
602 else
604 expanded_location xloc
605 = expand_location (DECL_SOURCE_LOCATION (n->decl));
606 bool use_name_only = (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID) == 0);
608 chksum = (use_name_only ? 0 : xloc.line);
609 chksum = coverage_checksum_string (chksum, xloc.file);
610 chksum = coverage_checksum_string
611 (chksum, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n->decl)));
612 if (!use_name_only && first_global_object_name)
613 chksum = coverage_checksum_string
614 (chksum, first_global_object_name);
615 chksum = coverage_checksum_string
616 (chksum, aux_base_name);
619 /* Non-negative integers are hopefully small enough to fit in all targets.
620 Gcov file formats wants non-zero function IDs. */
621 chksum = chksum & 0x7fffffff;
622 return chksum + (!chksum);
625 /* Compute cfg checksum for the function FN given as argument.
626 The checksum is calculated carefully so that
627 source code changes that doesn't affect the control flow graph
628 won't change the checksum.
629 This is to make the profile data useable across source code change.
630 The downside of this is that the compiler may use potentially
631 wrong profile data - that the source code change has non-trivial impact
632 on the validity of profile data (e.g. the reversed condition)
633 but the compiler won't detect the change and use the wrong profile data. */
635 unsigned
636 coverage_compute_cfg_checksum (struct function *fn)
638 basic_block bb;
639 unsigned chksum = n_basic_blocks_for_fn (fn);
641 FOR_EACH_BB_FN (bb, fn)
643 edge e;
644 edge_iterator ei;
645 chksum = crc32_byte (chksum, bb->index);
646 FOR_EACH_EDGE (e, ei, bb->succs)
648 chksum = crc32_byte (chksum, e->dest->index);
652 return chksum;
655 /* Begin output to the notes file for the current function.
656 Writes the function header. Returns nonzero if data should be output. */
659 coverage_begin_function (unsigned lineno_checksum, unsigned cfg_checksum)
661 expanded_location xloc;
662 unsigned long offset;
664 /* We don't need to output .gcno file unless we're under -ftest-coverage
665 (e.g. -fprofile-arcs/generate/use don't need .gcno to work). */
666 if (no_coverage || !bbg_file_name)
667 return 0;
669 xloc = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
671 /* Announce function */
672 offset = gcov_write_tag (GCOV_TAG_FUNCTION);
673 if (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID))
674 gcov_write_unsigned (current_function_funcdef_no + 1);
675 else
677 gcc_assert (coverage_node_map_initialized_p ());
678 gcov_write_unsigned (
679 cgraph_node::get (current_function_decl)->profile_id);
682 gcov_write_unsigned (lineno_checksum);
683 gcov_write_unsigned (cfg_checksum);
684 gcov_write_string (IDENTIFIER_POINTER
685 (DECL_ASSEMBLER_NAME (current_function_decl)));
686 gcov_write_string (xloc.file);
687 gcov_write_unsigned (xloc.line);
688 gcov_write_length (offset);
690 return !gcov_is_error ();
693 /* Finish coverage data for the current function. Verify no output
694 error has occurred. Save function coverage counts. */
696 void
697 coverage_end_function (unsigned lineno_checksum, unsigned cfg_checksum)
699 unsigned i;
701 if (bbg_file_name && gcov_is_error ())
703 warning (0, "error writing %qs", bbg_file_name);
704 unlink (bbg_file_name);
705 bbg_file_name = NULL;
708 if (fn_ctr_mask)
710 struct coverage_data *item = 0;
712 item = ggc_alloc<coverage_data> ();
714 if (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID))
715 item->ident = current_function_funcdef_no + 1;
716 else
718 gcc_assert (coverage_node_map_initialized_p ());
719 item->ident = cgraph_node::get (cfun->decl)->profile_id;
722 item->lineno_checksum = lineno_checksum;
723 item->cfg_checksum = cfg_checksum;
725 item->fn_decl = current_function_decl;
726 item->next = 0;
727 *functions_tail = item;
728 functions_tail = &item->next;
730 for (i = 0; i != GCOV_COUNTERS; i++)
732 tree var = fn_v_ctrs[i];
734 if (item)
735 item->ctr_vars[i] = var;
736 if (var)
738 tree array_type = build_index_type (size_int (fn_n_ctrs[i] - 1));
739 array_type = build_array_type (get_gcov_type (), array_type);
740 TREE_TYPE (var) = array_type;
741 DECL_SIZE (var) = TYPE_SIZE (array_type);
742 DECL_SIZE_UNIT (var) = TYPE_SIZE_UNIT (array_type);
743 varpool_node::finalize_decl (var);
746 fn_b_ctrs[i] = fn_n_ctrs[i] = 0;
747 fn_v_ctrs[i] = NULL_TREE;
749 prg_ctr_mask |= fn_ctr_mask;
750 fn_ctr_mask = 0;
754 /* Build a coverage variable of TYPE for function FN_DECL. If COUNTER
755 >= 0 it is a counter array, otherwise it is the function structure. */
757 static tree
758 build_var (tree fn_decl, tree type, int counter)
760 tree var = build_decl (BUILTINS_LOCATION, VAR_DECL, NULL_TREE, type);
761 const char *fn_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fn_decl));
762 char *buf;
763 size_t fn_name_len, len;
765 fn_name = targetm.strip_name_encoding (fn_name);
766 fn_name_len = strlen (fn_name);
767 buf = XALLOCAVEC (char, fn_name_len + 8 + sizeof (int) * 3);
769 if (counter < 0)
770 strcpy (buf, "__gcov__");
771 else
772 sprintf (buf, "__gcov%u_", counter);
773 len = strlen (buf);
774 #ifndef NO_DOT_IN_LABEL
775 buf[len - 1] = '.';
776 #elif !defined NO_DOLLAR_IN_LABEL
777 buf[len - 1] = '$';
778 #endif
779 memcpy (buf + len, fn_name, fn_name_len + 1);
780 DECL_NAME (var) = get_identifier (buf);
781 TREE_STATIC (var) = 1;
782 TREE_ADDRESSABLE (var) = 1;
783 DECL_NONALIASED (var) = 1;
784 DECL_ALIGN (var) = TYPE_ALIGN (type);
786 return var;
789 /* Creates the gcov_fn_info RECORD_TYPE. */
791 static void
792 build_fn_info_type (tree type, unsigned counters, tree gcov_info_type)
794 tree ctr_info = lang_hooks.types.make_type (RECORD_TYPE);
795 tree field, fields;
796 tree array_type;
798 gcc_assert (counters);
800 /* ctr_info::num */
801 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
802 get_gcov_unsigned_t ());
803 fields = field;
805 /* ctr_info::values */
806 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
807 build_pointer_type (get_gcov_type ()));
808 DECL_CHAIN (field) = fields;
809 fields = field;
811 finish_builtin_struct (ctr_info, "__gcov_ctr_info", fields, NULL_TREE);
813 /* key */
814 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
815 build_pointer_type (build_qualified_type
816 (gcov_info_type, TYPE_QUAL_CONST)));
817 fields = field;
819 /* ident */
820 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
821 get_gcov_unsigned_t ());
822 DECL_CHAIN (field) = fields;
823 fields = field;
825 /* lineno_checksum */
826 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
827 get_gcov_unsigned_t ());
828 DECL_CHAIN (field) = fields;
829 fields = field;
831 /* cfg checksum */
832 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
833 get_gcov_unsigned_t ());
834 DECL_CHAIN (field) = fields;
835 fields = field;
837 array_type = build_index_type (size_int (counters - 1));
838 array_type = build_array_type (ctr_info, array_type);
840 /* counters */
841 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE, array_type);
842 DECL_CHAIN (field) = fields;
843 fields = field;
845 finish_builtin_struct (type, "__gcov_fn_info", fields, NULL_TREE);
848 /* Returns a CONSTRUCTOR for a gcov_fn_info. DATA is
849 the coverage data for the function and TYPE is the gcov_fn_info
850 RECORD_TYPE. KEY is the object file key. */
852 static tree
853 build_fn_info (const struct coverage_data *data, tree type, tree key)
855 tree fields = TYPE_FIELDS (type);
856 tree ctr_type;
857 unsigned ix;
858 vec<constructor_elt, va_gc> *v1 = NULL;
859 vec<constructor_elt, va_gc> *v2 = NULL;
861 /* key */
862 CONSTRUCTOR_APPEND_ELT (v1, fields,
863 build1 (ADDR_EXPR, TREE_TYPE (fields), key));
864 fields = DECL_CHAIN (fields);
866 /* ident */
867 CONSTRUCTOR_APPEND_ELT (v1, fields,
868 build_int_cstu (get_gcov_unsigned_t (),
869 data->ident));
870 fields = DECL_CHAIN (fields);
872 /* lineno_checksum */
873 CONSTRUCTOR_APPEND_ELT (v1, fields,
874 build_int_cstu (get_gcov_unsigned_t (),
875 data->lineno_checksum));
876 fields = DECL_CHAIN (fields);
878 /* cfg_checksum */
879 CONSTRUCTOR_APPEND_ELT (v1, fields,
880 build_int_cstu (get_gcov_unsigned_t (),
881 data->cfg_checksum));
882 fields = DECL_CHAIN (fields);
884 /* counters */
885 ctr_type = TREE_TYPE (TREE_TYPE (fields));
886 for (ix = 0; ix != GCOV_COUNTERS; ix++)
887 if (prg_ctr_mask & (1 << ix))
889 vec<constructor_elt, va_gc> *ctr = NULL;
890 tree var = data->ctr_vars[ix];
891 unsigned count = 0;
893 if (var)
894 count
895 = tree_to_shwi (TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (var))))
896 + 1;
898 CONSTRUCTOR_APPEND_ELT (ctr, TYPE_FIELDS (ctr_type),
899 build_int_cstu (get_gcov_unsigned_t (),
900 count));
902 if (var)
903 CONSTRUCTOR_APPEND_ELT (ctr, DECL_CHAIN (TYPE_FIELDS (ctr_type)),
904 build_fold_addr_expr (var));
906 CONSTRUCTOR_APPEND_ELT (v2, NULL, build_constructor (ctr_type, ctr));
909 CONSTRUCTOR_APPEND_ELT (v1, fields,
910 build_constructor (TREE_TYPE (fields), v2));
912 return build_constructor (type, v1);
915 /* Create gcov_info struct. TYPE is the incomplete RECORD_TYPE to be
916 completed, and FN_INFO_PTR_TYPE is a pointer to the function info type. */
918 static void
919 build_info_type (tree type, tree fn_info_ptr_type)
921 tree field, fields = NULL_TREE;
922 tree merge_fn_type;
924 /* Version ident */
925 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
926 get_gcov_unsigned_t ());
927 DECL_CHAIN (field) = fields;
928 fields = field;
930 /* next pointer */
931 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
932 build_pointer_type (build_qualified_type
933 (type, TYPE_QUAL_CONST)));
934 DECL_CHAIN (field) = fields;
935 fields = field;
937 /* stamp */
938 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
939 get_gcov_unsigned_t ());
940 DECL_CHAIN (field) = fields;
941 fields = field;
943 /* Filename */
944 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
945 build_pointer_type (build_qualified_type
946 (char_type_node, TYPE_QUAL_CONST)));
947 DECL_CHAIN (field) = fields;
948 fields = field;
950 /* merge fn array */
951 merge_fn_type
952 = build_function_type_list (void_type_node,
953 build_pointer_type (get_gcov_type ()),
954 get_gcov_unsigned_t (), NULL_TREE);
955 merge_fn_type
956 = build_array_type (build_pointer_type (merge_fn_type),
957 build_index_type (size_int (GCOV_COUNTERS - 1)));
958 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
959 merge_fn_type);
960 DECL_CHAIN (field) = fields;
961 fields = field;
963 /* n_functions */
964 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
965 get_gcov_unsigned_t ());
966 DECL_CHAIN (field) = fields;
967 fields = field;
969 /* function_info pointer pointer */
970 fn_info_ptr_type = build_pointer_type
971 (build_qualified_type (fn_info_ptr_type, TYPE_QUAL_CONST));
972 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
973 fn_info_ptr_type);
974 DECL_CHAIN (field) = fields;
975 fields = field;
977 finish_builtin_struct (type, "__gcov_info", fields, NULL_TREE);
980 /* Returns a CONSTRUCTOR for the gcov_info object. INFO_TYPE is the
981 gcov_info structure type, FN_ARY is the array of pointers to
982 function info objects. */
984 static tree
985 build_info (tree info_type, tree fn_ary)
987 tree info_fields = TYPE_FIELDS (info_type);
988 tree merge_fn_type, n_funcs;
989 unsigned ix;
990 tree filename_string;
991 int da_file_name_len;
992 vec<constructor_elt, va_gc> *v1 = NULL;
993 vec<constructor_elt, va_gc> *v2 = NULL;
995 /* Version ident */
996 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
997 build_int_cstu (TREE_TYPE (info_fields),
998 GCOV_VERSION));
999 info_fields = DECL_CHAIN (info_fields);
1001 /* next -- NULL */
1002 CONSTRUCTOR_APPEND_ELT (v1, info_fields, null_pointer_node);
1003 info_fields = DECL_CHAIN (info_fields);
1005 /* stamp */
1006 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
1007 build_int_cstu (TREE_TYPE (info_fields),
1008 bbg_file_stamp));
1009 info_fields = DECL_CHAIN (info_fields);
1011 /* Filename */
1012 da_file_name_len = strlen (da_file_name);
1013 filename_string = build_string (da_file_name_len + 1, da_file_name);
1014 TREE_TYPE (filename_string) = build_array_type
1015 (char_type_node, build_index_type (size_int (da_file_name_len)));
1016 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
1017 build1 (ADDR_EXPR, TREE_TYPE (info_fields),
1018 filename_string));
1019 info_fields = DECL_CHAIN (info_fields);
1021 /* merge fn array -- NULL slots indicate unmeasured counters */
1022 merge_fn_type = TREE_TYPE (TREE_TYPE (info_fields));
1023 for (ix = 0; ix != GCOV_COUNTERS; ix++)
1025 tree ptr = null_pointer_node;
1027 if ((1u << ix) & prg_ctr_mask)
1029 tree merge_fn = build_decl (BUILTINS_LOCATION,
1030 FUNCTION_DECL,
1031 get_identifier (ctr_merge_functions[ix]),
1032 TREE_TYPE (merge_fn_type));
1033 DECL_EXTERNAL (merge_fn) = 1;
1034 TREE_PUBLIC (merge_fn) = 1;
1035 DECL_ARTIFICIAL (merge_fn) = 1;
1036 TREE_NOTHROW (merge_fn) = 1;
1037 /* Initialize assembler name so we can stream out. */
1038 DECL_ASSEMBLER_NAME (merge_fn);
1039 ptr = build1 (ADDR_EXPR, merge_fn_type, merge_fn);
1041 CONSTRUCTOR_APPEND_ELT (v2, NULL, ptr);
1043 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
1044 build_constructor (TREE_TYPE (info_fields), v2));
1045 info_fields = DECL_CHAIN (info_fields);
1047 /* n_functions */
1048 n_funcs = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (fn_ary)));
1049 n_funcs = fold_build2 (PLUS_EXPR, TREE_TYPE (info_fields),
1050 n_funcs, size_one_node);
1051 CONSTRUCTOR_APPEND_ELT (v1, info_fields, n_funcs);
1052 info_fields = DECL_CHAIN (info_fields);
1054 /* functions */
1055 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
1056 build1 (ADDR_EXPR, TREE_TYPE (info_fields), fn_ary));
1057 info_fields = DECL_CHAIN (info_fields);
1059 gcc_assert (!info_fields);
1060 return build_constructor (info_type, v1);
1063 /* Generate the constructor function to call __gcov_init. */
1065 static void
1066 build_init_ctor (tree gcov_info_type)
1068 tree ctor, stmt, init_fn;
1070 /* Build a decl for __gcov_init. */
1071 init_fn = build_pointer_type (gcov_info_type);
1072 init_fn = build_function_type_list (void_type_node, init_fn, NULL);
1073 init_fn = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
1074 get_identifier ("__gcov_init"), init_fn);
1075 TREE_PUBLIC (init_fn) = 1;
1076 DECL_EXTERNAL (init_fn) = 1;
1077 DECL_ASSEMBLER_NAME (init_fn);
1079 /* Generate a call to __gcov_init(&gcov_info). */
1080 ctor = NULL;
1081 stmt = build_fold_addr_expr (gcov_info_var);
1082 stmt = build_call_expr (init_fn, 1, stmt);
1083 append_to_statement_list (stmt, &ctor);
1085 /* Generate a constructor to run it. */
1086 cgraph_build_static_cdtor ('I', ctor, DEFAULT_INIT_PRIORITY);
1089 /* Create the gcov_info types and object. Generate the constructor
1090 function to call __gcov_init. Does not generate the initializer
1091 for the object. Returns TRUE if coverage data is being emitted. */
1093 static bool
1094 coverage_obj_init (void)
1096 tree gcov_info_type;
1097 unsigned n_counters = 0;
1098 unsigned ix;
1099 struct coverage_data *fn;
1100 struct coverage_data **fn_prev;
1101 char name_buf[32];
1103 no_coverage = 1; /* Disable any further coverage. */
1105 if (!prg_ctr_mask)
1106 return false;
1108 if (symtab->dump_file)
1109 fprintf (symtab->dump_file, "Using data file %s\n", da_file_name);
1111 /* Prune functions. */
1112 for (fn_prev = &functions_head; (fn = *fn_prev);)
1113 if (DECL_STRUCT_FUNCTION (fn->fn_decl))
1114 fn_prev = &fn->next;
1115 else
1116 /* The function is not being emitted, remove from list. */
1117 *fn_prev = fn->next;
1119 if (functions_head == NULL)
1120 return false;
1122 for (ix = 0; ix != GCOV_COUNTERS; ix++)
1123 if ((1u << ix) & prg_ctr_mask)
1124 n_counters++;
1126 /* Build the info and fn_info types. These are mutually recursive. */
1127 gcov_info_type = lang_hooks.types.make_type (RECORD_TYPE);
1128 gcov_fn_info_type = lang_hooks.types.make_type (RECORD_TYPE);
1129 build_fn_info_type (gcov_fn_info_type, n_counters, gcov_info_type);
1130 gcov_info_type = lang_hooks.types.make_type (RECORD_TYPE);
1131 gcov_fn_info_ptr_type = build_pointer_type
1132 (build_qualified_type (gcov_fn_info_type, TYPE_QUAL_CONST));
1133 build_info_type (gcov_info_type, gcov_fn_info_ptr_type);
1135 /* Build the gcov info var, this is referred to in its own
1136 initializer. */
1137 gcov_info_var = build_decl (BUILTINS_LOCATION,
1138 VAR_DECL, NULL_TREE, gcov_info_type);
1139 TREE_STATIC (gcov_info_var) = 1;
1140 ASM_GENERATE_INTERNAL_LABEL (name_buf, "LPBX", 0);
1141 DECL_NAME (gcov_info_var) = get_identifier (name_buf);
1143 build_init_ctor (gcov_info_type);
1145 return true;
1148 /* Generate the coverage function info for FN and DATA. Append a
1149 pointer to that object to CTOR and return the appended CTOR. */
1151 static vec<constructor_elt, va_gc> *
1152 coverage_obj_fn (vec<constructor_elt, va_gc> *ctor, tree fn,
1153 struct coverage_data const *data)
1155 tree init = build_fn_info (data, gcov_fn_info_type, gcov_info_var);
1156 tree var = build_var (fn, gcov_fn_info_type, -1);
1158 DECL_INITIAL (var) = init;
1159 varpool_node::finalize_decl (var);
1161 CONSTRUCTOR_APPEND_ELT (ctor, NULL,
1162 build1 (ADDR_EXPR, gcov_fn_info_ptr_type, var));
1163 return ctor;
1166 /* Finalize the coverage data. Generates the array of pointers to
1167 function objects from CTOR. Generate the gcov_info initializer. */
1169 static void
1170 coverage_obj_finish (vec<constructor_elt, va_gc> *ctor)
1172 unsigned n_functions = vec_safe_length (ctor);
1173 tree fn_info_ary_type = build_array_type
1174 (build_qualified_type (gcov_fn_info_ptr_type, TYPE_QUAL_CONST),
1175 build_index_type (size_int (n_functions - 1)));
1176 tree fn_info_ary = build_decl (BUILTINS_LOCATION, VAR_DECL, NULL_TREE,
1177 fn_info_ary_type);
1178 char name_buf[32];
1180 TREE_STATIC (fn_info_ary) = 1;
1181 ASM_GENERATE_INTERNAL_LABEL (name_buf, "LPBX", 1);
1182 DECL_NAME (fn_info_ary) = get_identifier (name_buf);
1183 DECL_INITIAL (fn_info_ary) = build_constructor (fn_info_ary_type, ctor);
1184 varpool_node::finalize_decl (fn_info_ary);
1186 DECL_INITIAL (gcov_info_var)
1187 = build_info (TREE_TYPE (gcov_info_var), fn_info_ary);
1188 varpool_node::finalize_decl (gcov_info_var);
1191 /* Perform file-level initialization. Read in data file, generate name
1192 of notes file. */
1194 void
1195 coverage_init (const char *filename)
1197 int len = strlen (filename);
1198 int prefix_len = 0;
1200 /* Since coverage_init is invoked very early, before the pass
1201 manager, we need to set up the dumping explicitly. This is
1202 similar to the handling in finish_optimization_passes. */
1203 int profile_pass_num =
1204 g->get_passes ()->get_pass_profile ()->static_pass_number;
1205 g->get_dumps ()->dump_start (profile_pass_num, NULL);
1207 if (!profile_data_prefix && !IS_ABSOLUTE_PATH (filename))
1208 profile_data_prefix = getpwd ();
1210 if (profile_data_prefix)
1211 prefix_len = strlen (profile_data_prefix);
1213 /* Name of da file. */
1214 da_file_name = XNEWVEC (char, len + strlen (GCOV_DATA_SUFFIX)
1215 + prefix_len + 2);
1217 if (profile_data_prefix)
1219 memcpy (da_file_name, profile_data_prefix, prefix_len);
1220 da_file_name[prefix_len++] = '/';
1222 memcpy (da_file_name + prefix_len, filename, len);
1223 strcpy (da_file_name + prefix_len + len, GCOV_DATA_SUFFIX);
1225 bbg_file_stamp = local_tick;
1227 if (flag_auto_profile)
1228 read_autofdo_file ();
1229 else if (flag_branch_probabilities)
1230 read_counts_file ();
1232 /* Name of bbg file. */
1233 if (flag_test_coverage && !flag_compare_debug)
1235 bbg_file_name = XNEWVEC (char, len + strlen (GCOV_NOTE_SUFFIX) + 1);
1236 memcpy (bbg_file_name, filename, len);
1237 strcpy (bbg_file_name + len, GCOV_NOTE_SUFFIX);
1239 if (!gcov_open (bbg_file_name, -1))
1241 error ("cannot open %s", bbg_file_name);
1242 bbg_file_name = NULL;
1244 else
1246 gcov_write_unsigned (GCOV_NOTE_MAGIC);
1247 gcov_write_unsigned (GCOV_VERSION);
1248 gcov_write_unsigned (bbg_file_stamp);
1252 g->get_dumps ()->dump_finish (profile_pass_num);
1255 /* Performs file-level cleanup. Close notes file, generate coverage
1256 variables and constructor. */
1258 void
1259 coverage_finish (void)
1261 if (bbg_file_name && gcov_close ())
1262 unlink (bbg_file_name);
1264 if (!flag_branch_probabilities && flag_test_coverage
1265 && (!local_tick || local_tick == (unsigned)-1))
1266 /* Only remove the da file, if we're emitting coverage code and
1267 cannot uniquely stamp it. If we can stamp it, libgcov will DTRT. */
1268 unlink (da_file_name);
1270 if (coverage_obj_init ())
1272 vec<constructor_elt, va_gc> *fn_ctor = NULL;
1273 struct coverage_data *fn;
1275 for (fn = functions_head; fn; fn = fn->next)
1276 fn_ctor = coverage_obj_fn (fn_ctor, fn->fn_decl, fn);
1277 coverage_obj_finish (fn_ctor);
1280 XDELETEVEC (da_file_name);
1281 da_file_name = NULL;
1284 #include "gt-coverage.h"