2015-05-22 Ed Schonberg <schonberg@adacore.com>
[official-gcc.git] / gcc / coverage.c
blobff7249acaa72f49d72e2d422810da5874126ad68
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 "hash-set.h"
33 #include "machmode.h"
34 #include "vec.h"
35 #include "double-int.h"
36 #include "input.h"
37 #include "alias.h"
38 #include "symtab.h"
39 #include "wide-int.h"
40 #include "inchash.h"
41 #include "tree.h"
42 #include "fold-const.h"
43 #include "stringpool.h"
44 #include "stor-layout.h"
45 #include "flags.h"
46 #include "output.h"
47 #include "regs.h"
48 #include "hashtab.h"
49 #include "hard-reg-set.h"
50 #include "function.h"
51 #include "statistics.h"
52 #include "real.h"
53 #include "fixed-value.h"
54 #include "insn-config.h"
55 #include "expmed.h"
56 #include "dojump.h"
57 #include "explow.h"
58 #include "calls.h"
59 #include "emit-rtl.h"
60 #include "varasm.h"
61 #include "stmt.h"
62 #include "expr.h"
63 #include "predict.h"
64 #include "dominance.h"
65 #include "cfg.h"
66 #include "basic-block.h"
67 #include "toplev.h"
68 #include "tm_p.h"
69 #include "ggc.h"
70 #include "coverage.h"
71 #include "langhooks.h"
72 #include "hash-table.h"
73 #include "tree-iterator.h"
74 #include "context.h"
75 #include "pass_manager.h"
76 #include "tree-pass.h"
77 #include "hash-map.h"
78 #include "is-a.h"
79 #include "plugin-api.h"
80 #include "ipa-ref.h"
81 #include "cgraph.h"
82 #include "dumpfile.h"
83 #include "diagnostic-core.h"
84 #include "intl.h"
85 #include "filenames.h"
86 #include "target.h"
87 #include "params.h"
88 #include "auto-profile.h"
90 #include "gcov-io.h"
91 #include "gcov-io.c"
93 struct GTY((chain_next ("%h.next"))) coverage_data
95 struct coverage_data *next; /* next function */
96 unsigned ident; /* function ident */
97 unsigned lineno_checksum; /* function lineno checksum */
98 unsigned cfg_checksum; /* function cfg checksum */
99 tree fn_decl; /* the function decl */
100 tree ctr_vars[GCOV_COUNTERS]; /* counter variables. */
103 /* Counts information for a function. */
104 typedef struct counts_entry
106 /* We hash by */
107 unsigned ident;
108 unsigned ctr;
110 /* Store */
111 unsigned lineno_checksum;
112 unsigned cfg_checksum;
113 gcov_type *counts;
114 struct gcov_ctr_summary summary;
116 /* hash_table support. */
117 typedef counts_entry *value_type;
118 typedef counts_entry *compare_type;
119 static inline hashval_t hash (const counts_entry *);
120 static int equal (const counts_entry *, const counts_entry *);
121 static void remove (counts_entry *);
122 } counts_entry_t;
124 static GTY(()) struct coverage_data *functions_head = 0;
125 static struct coverage_data **functions_tail = &functions_head;
126 static unsigned no_coverage = 0;
128 /* Cumulative counter information for whole program. */
129 static unsigned prg_ctr_mask; /* Mask of counter types generated. */
131 /* Counter information for current function. */
132 static unsigned fn_ctr_mask; /* Mask of counters used. */
133 static GTY(()) tree fn_v_ctrs[GCOV_COUNTERS]; /* counter variables. */
134 static unsigned fn_n_ctrs[GCOV_COUNTERS]; /* Counters allocated. */
135 static unsigned fn_b_ctrs[GCOV_COUNTERS]; /* Allocation base. */
137 /* Coverage info VAR_DECL and function info type nodes. */
138 static GTY(()) tree gcov_info_var;
139 static GTY(()) tree gcov_fn_info_type;
140 static GTY(()) tree gcov_fn_info_ptr_type;
142 /* Name of the notes (gcno) output file. The "bbg" prefix is for
143 historical reasons, when the notes file contained only the
144 basic block graph notes.
145 If this is NULL we're not writing to the notes file. */
146 static char *bbg_file_name;
148 /* File stamp for notes file. */
149 static unsigned bbg_file_stamp;
151 /* Name of the count data (gcda) file. */
152 static char *da_file_name;
154 /* The names of merge functions for counters. */
155 #define STR(str) #str
156 #define DEF_GCOV_COUNTER(COUNTER, NAME, FN_TYPE) STR(__gcov_merge ## FN_TYPE),
157 static const char *const ctr_merge_functions[GCOV_COUNTERS] = {
158 #include "gcov-counter.def"
160 #undef DEF_GCOV_COUNTER
161 #undef STR
163 #define DEF_GCOV_COUNTER(COUNTER, NAME, FN_TYPE) NAME,
164 static const char *const ctr_names[GCOV_COUNTERS] = {
165 #include "gcov-counter.def"
167 #undef DEF_GCOV_COUNTER
169 /* Forward declarations. */
170 static void read_counts_file (void);
171 static tree build_var (tree, tree, int);
172 static void build_fn_info_type (tree, unsigned, tree);
173 static void build_info_type (tree, tree);
174 static tree build_fn_info (const struct coverage_data *, tree, tree);
175 static tree build_info (tree, tree);
176 static bool coverage_obj_init (void);
177 static vec<constructor_elt, va_gc> *coverage_obj_fn
178 (vec<constructor_elt, va_gc> *, tree, struct coverage_data const *);
179 static void coverage_obj_finish (vec<constructor_elt, va_gc> *);
181 /* Return the type node for gcov_type. */
183 tree
184 get_gcov_type (void)
186 machine_mode mode = smallest_mode_for_size (GCOV_TYPE_SIZE, MODE_INT);
187 return lang_hooks.types.type_for_mode (mode, false);
190 /* Return the type node for gcov_unsigned_t. */
192 static tree
193 get_gcov_unsigned_t (void)
195 machine_mode mode = smallest_mode_for_size (32, MODE_INT);
196 return lang_hooks.types.type_for_mode (mode, true);
199 inline hashval_t
200 counts_entry::hash (const counts_entry *entry)
202 return entry->ident * GCOV_COUNTERS + entry->ctr;
205 inline int
206 counts_entry::equal (const counts_entry *entry1, const counts_entry *entry2)
208 return entry1->ident == entry2->ident && entry1->ctr == entry2->ctr;
211 inline void
212 counts_entry::remove (counts_entry *entry)
214 free (entry->counts);
215 free (entry);
218 /* Hash table of count data. */
219 static hash_table<counts_entry> *counts_hash;
221 /* Read in the counts file, if available. */
223 static void
224 read_counts_file (void)
226 gcov_unsigned_t fn_ident = 0;
227 struct gcov_summary summary;
228 unsigned new_summary = 1;
229 gcov_unsigned_t tag;
230 int is_error = 0;
231 unsigned lineno_checksum = 0;
232 unsigned cfg_checksum = 0;
234 if (!gcov_open (da_file_name, 1))
235 return;
237 if (!gcov_magic (gcov_read_unsigned (), GCOV_DATA_MAGIC))
239 warning (0, "%qs is not a gcov data file", da_file_name);
240 gcov_close ();
241 return;
243 else if ((tag = gcov_read_unsigned ()) != GCOV_VERSION)
245 char v[4], e[4];
247 GCOV_UNSIGNED2STRING (v, tag);
248 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
250 warning (0, "%qs is version %q.*s, expected version %q.*s",
251 da_file_name, 4, v, 4, e);
252 gcov_close ();
253 return;
256 /* Read the stamp, used for creating a generation count. */
257 tag = gcov_read_unsigned ();
258 bbg_file_stamp = crc32_unsigned (bbg_file_stamp, tag);
260 counts_hash = new hash_table<counts_entry> (10);
261 while ((tag = gcov_read_unsigned ()))
263 gcov_unsigned_t length;
264 gcov_position_t offset;
266 length = gcov_read_unsigned ();
267 offset = gcov_position ();
268 if (tag == GCOV_TAG_FUNCTION)
270 if (length)
272 fn_ident = gcov_read_unsigned ();
273 lineno_checksum = gcov_read_unsigned ();
274 cfg_checksum = gcov_read_unsigned ();
276 else
277 fn_ident = lineno_checksum = cfg_checksum = 0;
278 new_summary = 1;
280 else if (tag == GCOV_TAG_PROGRAM_SUMMARY)
282 struct gcov_summary sum;
283 unsigned ix;
285 if (new_summary)
286 memset (&summary, 0, sizeof (summary));
288 gcov_read_summary (&sum);
289 for (ix = 0; ix != GCOV_COUNTERS_SUMMABLE; ix++)
291 summary.ctrs[ix].runs += sum.ctrs[ix].runs;
292 summary.ctrs[ix].sum_all += sum.ctrs[ix].sum_all;
293 if (summary.ctrs[ix].run_max < sum.ctrs[ix].run_max)
294 summary.ctrs[ix].run_max = sum.ctrs[ix].run_max;
295 summary.ctrs[ix].sum_max += sum.ctrs[ix].sum_max;
297 if (new_summary)
298 memcpy (summary.ctrs[GCOV_COUNTER_ARCS].histogram,
299 sum.ctrs[GCOV_COUNTER_ARCS].histogram,
300 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
301 else
302 gcov_histogram_merge (summary.ctrs[GCOV_COUNTER_ARCS].histogram,
303 sum.ctrs[GCOV_COUNTER_ARCS].histogram);
304 new_summary = 0;
306 else if (GCOV_TAG_IS_COUNTER (tag) && fn_ident)
308 counts_entry_t **slot, *entry, elt;
309 unsigned n_counts = GCOV_TAG_COUNTER_NUM (length);
310 unsigned ix;
312 elt.ident = fn_ident;
313 elt.ctr = GCOV_COUNTER_FOR_TAG (tag);
315 slot = counts_hash->find_slot (&elt, INSERT);
316 entry = *slot;
317 if (!entry)
319 *slot = entry = XCNEW (counts_entry_t);
320 entry->ident = fn_ident;
321 entry->ctr = elt.ctr;
322 entry->lineno_checksum = lineno_checksum;
323 entry->cfg_checksum = cfg_checksum;
324 if (elt.ctr < GCOV_COUNTERS_SUMMABLE)
325 entry->summary = summary.ctrs[elt.ctr];
326 entry->summary.num = n_counts;
327 entry->counts = XCNEWVEC (gcov_type, n_counts);
329 else if (entry->lineno_checksum != lineno_checksum
330 || entry->cfg_checksum != cfg_checksum)
332 error ("Profile data for function %u is corrupted", fn_ident);
333 error ("checksum is (%x,%x) instead of (%x,%x)",
334 entry->lineno_checksum, entry->cfg_checksum,
335 lineno_checksum, cfg_checksum);
336 delete counts_hash;
337 counts_hash = NULL;
338 break;
340 else if (entry->summary.num != n_counts)
342 error ("Profile data for function %u is corrupted", fn_ident);
343 error ("number of counters is %d instead of %d", entry->summary.num, n_counts);
344 delete counts_hash;
345 counts_hash = NULL;
346 break;
348 else if (elt.ctr >= GCOV_COUNTERS_SUMMABLE)
350 error ("cannot merge separate %s counters for function %u",
351 ctr_names[elt.ctr], fn_ident);
352 goto skip_merge;
354 else
356 entry->summary.runs += summary.ctrs[elt.ctr].runs;
357 entry->summary.sum_all += summary.ctrs[elt.ctr].sum_all;
358 if (entry->summary.run_max < summary.ctrs[elt.ctr].run_max)
359 entry->summary.run_max = summary.ctrs[elt.ctr].run_max;
360 entry->summary.sum_max += summary.ctrs[elt.ctr].sum_max;
362 for (ix = 0; ix != n_counts; ix++)
363 entry->counts[ix] += gcov_read_counter ();
364 skip_merge:;
366 gcov_sync (offset, length);
367 if ((is_error = gcov_is_error ()))
369 error (is_error < 0 ? "%qs has overflowed" : "%qs is corrupted",
370 da_file_name);
371 delete counts_hash;
372 counts_hash = NULL;
373 break;
377 gcov_close ();
380 /* Returns the counters for a particular tag. */
382 gcov_type *
383 get_coverage_counts (unsigned counter, unsigned expected,
384 unsigned cfg_checksum, unsigned lineno_checksum,
385 const struct gcov_ctr_summary **summary)
387 counts_entry_t *entry, elt;
389 /* No hash table, no counts. */
390 if (!counts_hash)
392 static int warned = 0;
394 if (!warned++ && dump_enabled_p ())
395 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
396 (flag_guess_branch_prob
397 ? "file %s not found, execution counts estimated\n"
398 : "file %s not found, execution counts assumed to "
399 "be zero\n"),
400 da_file_name);
401 return NULL;
403 if (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID))
404 elt.ident = current_function_funcdef_no + 1;
405 else
407 gcc_assert (coverage_node_map_initialized_p ());
408 elt.ident = cgraph_node::get (cfun->decl)->profile_id;
410 elt.ctr = counter;
411 entry = counts_hash->find (&elt);
412 if (!entry || !entry->summary.num)
413 /* The function was not emitted, or is weak and not chosen in the
414 final executable. Silently fail, because there's nothing we
415 can do about it. */
416 return NULL;
418 if (entry->cfg_checksum != cfg_checksum
419 || entry->summary.num != expected)
421 static int warned = 0;
422 bool warning_printed = false;
423 tree id = DECL_ASSEMBLER_NAME (current_function_decl);
425 warning_printed =
426 warning_at (input_location, OPT_Wcoverage_mismatch,
427 "the control flow of function %qE does not match "
428 "its profile data (counter %qs)", id, ctr_names[counter]);
429 if (warning_printed && dump_enabled_p ())
431 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
432 "use -Wno-error=coverage-mismatch to tolerate "
433 "the mismatch but performance may drop if the "
434 "function is hot\n");
436 if (!seen_error ()
437 && !warned++)
439 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
440 "coverage mismatch ignored\n");
441 dump_printf (MSG_OPTIMIZED_LOCATIONS,
442 flag_guess_branch_prob
443 ? G_("execution counts estimated\n")
444 : G_("execution counts assumed to be zero\n"));
445 if (!flag_guess_branch_prob)
446 dump_printf (MSG_OPTIMIZED_LOCATIONS,
447 "this can result in poorly optimized code\n");
451 return NULL;
453 else if (entry->lineno_checksum != lineno_checksum)
455 warning (OPT_Wcoverage_mismatch,
456 "source locations for function %qE have changed,"
457 " the profile data may be out of date",
458 DECL_ASSEMBLER_NAME (current_function_decl));
461 if (summary)
462 *summary = &entry->summary;
464 return entry->counts;
467 /* Allocate NUM counters of type COUNTER. Returns nonzero if the
468 allocation succeeded. */
471 coverage_counter_alloc (unsigned counter, unsigned num)
473 if (no_coverage)
474 return 0;
476 if (!num)
477 return 1;
479 if (!fn_v_ctrs[counter])
481 tree array_type = build_array_type (get_gcov_type (), NULL_TREE);
483 fn_v_ctrs[counter]
484 = build_var (current_function_decl, array_type, counter);
487 fn_b_ctrs[counter] = fn_n_ctrs[counter];
488 fn_n_ctrs[counter] += num;
490 fn_ctr_mask |= 1 << counter;
491 return 1;
494 /* Generate a tree to access COUNTER NO. */
496 tree
497 tree_coverage_counter_ref (unsigned counter, unsigned no)
499 tree gcov_type_node = get_gcov_type ();
501 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 build4 (ARRAY_REF, gcov_type_node, fn_v_ctrs[counter],
507 build_int_cst (integer_type_node, no), NULL, NULL);
510 /* Generate a tree to access the address of COUNTER NO. */
512 tree
513 tree_coverage_counter_addr (unsigned counter, unsigned no)
515 tree gcov_type_node = get_gcov_type ();
517 gcc_assert (no < fn_n_ctrs[counter] - fn_b_ctrs[counter]);
518 no += fn_b_ctrs[counter];
520 /* "no" here is an array index, scaled to bytes later. */
521 return build_fold_addr_expr (build4 (ARRAY_REF, gcov_type_node,
522 fn_v_ctrs[counter],
523 build_int_cst (integer_type_node, no),
524 NULL, NULL));
528 /* Generate a checksum for a string. CHKSUM is the current
529 checksum. */
531 static unsigned
532 coverage_checksum_string (unsigned chksum, const char *string)
534 int i;
535 char *dup = NULL;
537 /* Look for everything that looks if it were produced by
538 get_file_function_name and zero out the second part
539 that may result from flag_random_seed. This is not critical
540 as the checksums are used only for sanity checking. */
541 for (i = 0; string[i]; i++)
543 int offset = 0;
544 if (!strncmp (string + i, "_GLOBAL__N_", 11))
545 offset = 11;
546 if (!strncmp (string + i, "_GLOBAL__", 9))
547 offset = 9;
549 /* C++ namespaces do have scheme:
550 _GLOBAL__N_<filename>_<wrongmagicnumber>_<magicnumber>functionname
551 since filename might contain extra underscores there seems
552 to be no better chance then walk all possible offsets looking
553 for magicnumber. */
554 if (offset)
556 for (i = i + offset; string[i]; i++)
557 if (string[i]=='_')
559 int y;
561 for (y = 1; y < 9; y++)
562 if (!(string[i + y] >= '0' && string[i + y] <= '9')
563 && !(string[i + y] >= 'A' && string[i + y] <= 'F'))
564 break;
565 if (y != 9 || string[i + 9] != '_')
566 continue;
567 for (y = 10; y < 18; y++)
568 if (!(string[i + y] >= '0' && string[i + y] <= '9')
569 && !(string[i + y] >= 'A' && string[i + y] <= 'F'))
570 break;
571 if (y != 18)
572 continue;
573 if (!dup)
574 string = dup = xstrdup (string);
575 for (y = 10; y < 18; y++)
576 dup[i + y] = '0';
578 break;
582 chksum = crc32_string (chksum, string);
583 free (dup);
585 return chksum;
588 /* Compute checksum for the current function. We generate a CRC32. */
590 unsigned
591 coverage_compute_lineno_checksum (void)
593 expanded_location xloc
594 = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
595 unsigned chksum = xloc.line;
597 chksum = coverage_checksum_string (chksum, xloc.file);
598 chksum = coverage_checksum_string
599 (chksum, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl)));
601 return chksum;
604 /* Compute profile ID. This is better to be unique in whole program. */
606 unsigned
607 coverage_compute_profile_id (struct cgraph_node *n)
609 unsigned chksum;
611 /* Externally visible symbols have unique name. */
612 if (TREE_PUBLIC (n->decl) || DECL_EXTERNAL (n->decl) || n->unique_name)
614 chksum = coverage_checksum_string
615 (0, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n->decl)));
617 else
619 expanded_location xloc
620 = expand_location (DECL_SOURCE_LOCATION (n->decl));
621 bool use_name_only = (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID) == 0);
623 chksum = (use_name_only ? 0 : xloc.line);
624 chksum = coverage_checksum_string (chksum, xloc.file);
625 chksum = coverage_checksum_string
626 (chksum, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n->decl)));
627 if (!use_name_only && first_global_object_name)
628 chksum = coverage_checksum_string
629 (chksum, first_global_object_name);
630 chksum = coverage_checksum_string
631 (chksum, aux_base_name);
634 /* Non-negative integers are hopefully small enough to fit in all targets.
635 Gcov file formats wants non-zero function IDs. */
636 chksum = chksum & 0x7fffffff;
637 return chksum + (!chksum);
640 /* Compute cfg checksum for the function FN given as argument.
641 The checksum is calculated carefully so that
642 source code changes that doesn't affect the control flow graph
643 won't change the checksum.
644 This is to make the profile data useable across source code change.
645 The downside of this is that the compiler may use potentially
646 wrong profile data - that the source code change has non-trivial impact
647 on the validity of profile data (e.g. the reversed condition)
648 but the compiler won't detect the change and use the wrong profile data. */
650 unsigned
651 coverage_compute_cfg_checksum (struct function *fn)
653 basic_block bb;
654 unsigned chksum = n_basic_blocks_for_fn (fn);
656 FOR_EACH_BB_FN (bb, fn)
658 edge e;
659 edge_iterator ei;
660 chksum = crc32_byte (chksum, bb->index);
661 FOR_EACH_EDGE (e, ei, bb->succs)
663 chksum = crc32_byte (chksum, e->dest->index);
667 return chksum;
670 /* Begin output to the notes file for the current function.
671 Writes the function header. Returns nonzero if data should be output. */
674 coverage_begin_function (unsigned lineno_checksum, unsigned cfg_checksum)
676 expanded_location xloc;
677 unsigned long offset;
679 /* We don't need to output .gcno file unless we're under -ftest-coverage
680 (e.g. -fprofile-arcs/generate/use don't need .gcno to work). */
681 if (no_coverage || !bbg_file_name)
682 return 0;
684 xloc = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
686 /* Announce function */
687 offset = gcov_write_tag (GCOV_TAG_FUNCTION);
688 if (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID))
689 gcov_write_unsigned (current_function_funcdef_no + 1);
690 else
692 gcc_assert (coverage_node_map_initialized_p ());
693 gcov_write_unsigned (
694 cgraph_node::get (current_function_decl)->profile_id);
697 gcov_write_unsigned (lineno_checksum);
698 gcov_write_unsigned (cfg_checksum);
699 gcov_write_string (IDENTIFIER_POINTER
700 (DECL_ASSEMBLER_NAME (current_function_decl)));
701 gcov_write_string (xloc.file);
702 gcov_write_unsigned (xloc.line);
703 gcov_write_length (offset);
705 return !gcov_is_error ();
708 /* Finish coverage data for the current function. Verify no output
709 error has occurred. Save function coverage counts. */
711 void
712 coverage_end_function (unsigned lineno_checksum, unsigned cfg_checksum)
714 unsigned i;
716 if (bbg_file_name && gcov_is_error ())
718 warning (0, "error writing %qs", bbg_file_name);
719 unlink (bbg_file_name);
720 bbg_file_name = NULL;
723 if (fn_ctr_mask)
725 struct coverage_data *item = 0;
727 item = ggc_alloc<coverage_data> ();
729 if (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID))
730 item->ident = current_function_funcdef_no + 1;
731 else
733 gcc_assert (coverage_node_map_initialized_p ());
734 item->ident = cgraph_node::get (cfun->decl)->profile_id;
737 item->lineno_checksum = lineno_checksum;
738 item->cfg_checksum = cfg_checksum;
740 item->fn_decl = current_function_decl;
741 item->next = 0;
742 *functions_tail = item;
743 functions_tail = &item->next;
745 for (i = 0; i != GCOV_COUNTERS; i++)
747 tree var = fn_v_ctrs[i];
749 if (item)
750 item->ctr_vars[i] = var;
751 if (var)
753 tree array_type = build_index_type (size_int (fn_n_ctrs[i] - 1));
754 array_type = build_array_type (get_gcov_type (), array_type);
755 TREE_TYPE (var) = array_type;
756 DECL_SIZE (var) = TYPE_SIZE (array_type);
757 DECL_SIZE_UNIT (var) = TYPE_SIZE_UNIT (array_type);
758 varpool_node::finalize_decl (var);
761 fn_b_ctrs[i] = fn_n_ctrs[i] = 0;
762 fn_v_ctrs[i] = NULL_TREE;
764 prg_ctr_mask |= fn_ctr_mask;
765 fn_ctr_mask = 0;
769 /* Build a coverage variable of TYPE for function FN_DECL. If COUNTER
770 >= 0 it is a counter array, otherwise it is the function structure. */
772 static tree
773 build_var (tree fn_decl, tree type, int counter)
775 tree var = build_decl (BUILTINS_LOCATION, VAR_DECL, NULL_TREE, type);
776 const char *fn_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fn_decl));
777 char *buf;
778 size_t fn_name_len, len;
780 fn_name = targetm.strip_name_encoding (fn_name);
781 fn_name_len = strlen (fn_name);
782 buf = XALLOCAVEC (char, fn_name_len + 8 + sizeof (int) * 3);
784 if (counter < 0)
785 strcpy (buf, "__gcov__");
786 else
787 sprintf (buf, "__gcov%u_", counter);
788 len = strlen (buf);
789 #ifndef NO_DOT_IN_LABEL
790 buf[len - 1] = '.';
791 #elif !defined NO_DOLLAR_IN_LABEL
792 buf[len - 1] = '$';
793 #endif
794 memcpy (buf + len, fn_name, fn_name_len + 1);
795 DECL_NAME (var) = get_identifier (buf);
796 TREE_STATIC (var) = 1;
797 TREE_ADDRESSABLE (var) = 1;
798 DECL_NONALIASED (var) = 1;
799 DECL_ALIGN (var) = TYPE_ALIGN (type);
801 return var;
804 /* Creates the gcov_fn_info RECORD_TYPE. */
806 static void
807 build_fn_info_type (tree type, unsigned counters, tree gcov_info_type)
809 tree ctr_info = lang_hooks.types.make_type (RECORD_TYPE);
810 tree field, fields;
811 tree array_type;
813 gcc_assert (counters);
815 /* ctr_info::num */
816 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
817 get_gcov_unsigned_t ());
818 fields = field;
820 /* ctr_info::values */
821 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
822 build_pointer_type (get_gcov_type ()));
823 DECL_CHAIN (field) = fields;
824 fields = field;
826 finish_builtin_struct (ctr_info, "__gcov_ctr_info", fields, NULL_TREE);
828 /* key */
829 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
830 build_pointer_type (build_qualified_type
831 (gcov_info_type, TYPE_QUAL_CONST)));
832 fields = field;
834 /* ident */
835 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
836 get_gcov_unsigned_t ());
837 DECL_CHAIN (field) = fields;
838 fields = field;
840 /* lineno_checksum */
841 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
842 get_gcov_unsigned_t ());
843 DECL_CHAIN (field) = fields;
844 fields = field;
846 /* cfg checksum */
847 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
848 get_gcov_unsigned_t ());
849 DECL_CHAIN (field) = fields;
850 fields = field;
852 array_type = build_index_type (size_int (counters - 1));
853 array_type = build_array_type (ctr_info, array_type);
855 /* counters */
856 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE, array_type);
857 DECL_CHAIN (field) = fields;
858 fields = field;
860 finish_builtin_struct (type, "__gcov_fn_info", fields, NULL_TREE);
863 /* Returns a CONSTRUCTOR for a gcov_fn_info. DATA is
864 the coverage data for the function and TYPE is the gcov_fn_info
865 RECORD_TYPE. KEY is the object file key. */
867 static tree
868 build_fn_info (const struct coverage_data *data, tree type, tree key)
870 tree fields = TYPE_FIELDS (type);
871 tree ctr_type;
872 unsigned ix;
873 vec<constructor_elt, va_gc> *v1 = NULL;
874 vec<constructor_elt, va_gc> *v2 = NULL;
876 /* key */
877 CONSTRUCTOR_APPEND_ELT (v1, fields,
878 build1 (ADDR_EXPR, TREE_TYPE (fields), key));
879 fields = DECL_CHAIN (fields);
881 /* ident */
882 CONSTRUCTOR_APPEND_ELT (v1, fields,
883 build_int_cstu (get_gcov_unsigned_t (),
884 data->ident));
885 fields = DECL_CHAIN (fields);
887 /* lineno_checksum */
888 CONSTRUCTOR_APPEND_ELT (v1, fields,
889 build_int_cstu (get_gcov_unsigned_t (),
890 data->lineno_checksum));
891 fields = DECL_CHAIN (fields);
893 /* cfg_checksum */
894 CONSTRUCTOR_APPEND_ELT (v1, fields,
895 build_int_cstu (get_gcov_unsigned_t (),
896 data->cfg_checksum));
897 fields = DECL_CHAIN (fields);
899 /* counters */
900 ctr_type = TREE_TYPE (TREE_TYPE (fields));
901 for (ix = 0; ix != GCOV_COUNTERS; ix++)
902 if (prg_ctr_mask & (1 << ix))
904 vec<constructor_elt, va_gc> *ctr = NULL;
905 tree var = data->ctr_vars[ix];
906 unsigned count = 0;
908 if (var)
909 count
910 = tree_to_shwi (TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (var))))
911 + 1;
913 CONSTRUCTOR_APPEND_ELT (ctr, TYPE_FIELDS (ctr_type),
914 build_int_cstu (get_gcov_unsigned_t (),
915 count));
917 if (var)
918 CONSTRUCTOR_APPEND_ELT (ctr, DECL_CHAIN (TYPE_FIELDS (ctr_type)),
919 build_fold_addr_expr (var));
921 CONSTRUCTOR_APPEND_ELT (v2, NULL, build_constructor (ctr_type, ctr));
924 CONSTRUCTOR_APPEND_ELT (v1, fields,
925 build_constructor (TREE_TYPE (fields), v2));
927 return build_constructor (type, v1);
930 /* Create gcov_info struct. TYPE is the incomplete RECORD_TYPE to be
931 completed, and FN_INFO_PTR_TYPE is a pointer to the function info type. */
933 static void
934 build_info_type (tree type, tree fn_info_ptr_type)
936 tree field, fields = NULL_TREE;
937 tree merge_fn_type;
939 /* Version ident */
940 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
941 get_gcov_unsigned_t ());
942 DECL_CHAIN (field) = fields;
943 fields = field;
945 /* next pointer */
946 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
947 build_pointer_type (build_qualified_type
948 (type, TYPE_QUAL_CONST)));
949 DECL_CHAIN (field) = fields;
950 fields = field;
952 /* stamp */
953 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
954 get_gcov_unsigned_t ());
955 DECL_CHAIN (field) = fields;
956 fields = field;
958 /* Filename */
959 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
960 build_pointer_type (build_qualified_type
961 (char_type_node, TYPE_QUAL_CONST)));
962 DECL_CHAIN (field) = fields;
963 fields = field;
965 /* merge fn array */
966 merge_fn_type
967 = build_function_type_list (void_type_node,
968 build_pointer_type (get_gcov_type ()),
969 get_gcov_unsigned_t (), NULL_TREE);
970 merge_fn_type
971 = build_array_type (build_pointer_type (merge_fn_type),
972 build_index_type (size_int (GCOV_COUNTERS - 1)));
973 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
974 merge_fn_type);
975 DECL_CHAIN (field) = fields;
976 fields = field;
978 /* n_functions */
979 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
980 get_gcov_unsigned_t ());
981 DECL_CHAIN (field) = fields;
982 fields = field;
984 /* function_info pointer pointer */
985 fn_info_ptr_type = build_pointer_type
986 (build_qualified_type (fn_info_ptr_type, TYPE_QUAL_CONST));
987 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
988 fn_info_ptr_type);
989 DECL_CHAIN (field) = fields;
990 fields = field;
992 finish_builtin_struct (type, "__gcov_info", fields, NULL_TREE);
995 /* Returns a CONSTRUCTOR for the gcov_info object. INFO_TYPE is the
996 gcov_info structure type, FN_ARY is the array of pointers to
997 function info objects. */
999 static tree
1000 build_info (tree info_type, tree fn_ary)
1002 tree info_fields = TYPE_FIELDS (info_type);
1003 tree merge_fn_type, n_funcs;
1004 unsigned ix;
1005 tree filename_string;
1006 int da_file_name_len;
1007 vec<constructor_elt, va_gc> *v1 = NULL;
1008 vec<constructor_elt, va_gc> *v2 = NULL;
1010 /* Version ident */
1011 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
1012 build_int_cstu (TREE_TYPE (info_fields),
1013 GCOV_VERSION));
1014 info_fields = DECL_CHAIN (info_fields);
1016 /* next -- NULL */
1017 CONSTRUCTOR_APPEND_ELT (v1, info_fields, null_pointer_node);
1018 info_fields = DECL_CHAIN (info_fields);
1020 /* stamp */
1021 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
1022 build_int_cstu (TREE_TYPE (info_fields),
1023 bbg_file_stamp));
1024 info_fields = DECL_CHAIN (info_fields);
1026 /* Filename */
1027 da_file_name_len = strlen (da_file_name);
1028 filename_string = build_string (da_file_name_len + 1, da_file_name);
1029 TREE_TYPE (filename_string) = build_array_type
1030 (char_type_node, build_index_type (size_int (da_file_name_len)));
1031 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
1032 build1 (ADDR_EXPR, TREE_TYPE (info_fields),
1033 filename_string));
1034 info_fields = DECL_CHAIN (info_fields);
1036 /* merge fn array -- NULL slots indicate unmeasured counters */
1037 merge_fn_type = TREE_TYPE (TREE_TYPE (info_fields));
1038 for (ix = 0; ix != GCOV_COUNTERS; ix++)
1040 tree ptr = null_pointer_node;
1042 if ((1u << ix) & prg_ctr_mask)
1044 tree merge_fn = build_decl (BUILTINS_LOCATION,
1045 FUNCTION_DECL,
1046 get_identifier (ctr_merge_functions[ix]),
1047 TREE_TYPE (merge_fn_type));
1048 DECL_EXTERNAL (merge_fn) = 1;
1049 TREE_PUBLIC (merge_fn) = 1;
1050 DECL_ARTIFICIAL (merge_fn) = 1;
1051 TREE_NOTHROW (merge_fn) = 1;
1052 /* Initialize assembler name so we can stream out. */
1053 DECL_ASSEMBLER_NAME (merge_fn);
1054 ptr = build1 (ADDR_EXPR, merge_fn_type, merge_fn);
1056 CONSTRUCTOR_APPEND_ELT (v2, NULL, ptr);
1058 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
1059 build_constructor (TREE_TYPE (info_fields), v2));
1060 info_fields = DECL_CHAIN (info_fields);
1062 /* n_functions */
1063 n_funcs = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (fn_ary)));
1064 n_funcs = fold_build2 (PLUS_EXPR, TREE_TYPE (info_fields),
1065 n_funcs, size_one_node);
1066 CONSTRUCTOR_APPEND_ELT (v1, info_fields, n_funcs);
1067 info_fields = DECL_CHAIN (info_fields);
1069 /* functions */
1070 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
1071 build1 (ADDR_EXPR, TREE_TYPE (info_fields), fn_ary));
1072 info_fields = DECL_CHAIN (info_fields);
1074 gcc_assert (!info_fields);
1075 return build_constructor (info_type, v1);
1078 /* Generate the constructor function to call __gcov_init. */
1080 static void
1081 build_init_ctor (tree gcov_info_type)
1083 tree ctor, stmt, init_fn;
1085 /* Build a decl for __gcov_init. */
1086 init_fn = build_pointer_type (gcov_info_type);
1087 init_fn = build_function_type_list (void_type_node, init_fn, NULL);
1088 init_fn = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
1089 get_identifier ("__gcov_init"), init_fn);
1090 TREE_PUBLIC (init_fn) = 1;
1091 DECL_EXTERNAL (init_fn) = 1;
1092 DECL_ASSEMBLER_NAME (init_fn);
1094 /* Generate a call to __gcov_init(&gcov_info). */
1095 ctor = NULL;
1096 stmt = build_fold_addr_expr (gcov_info_var);
1097 stmt = build_call_expr (init_fn, 1, stmt);
1098 append_to_statement_list (stmt, &ctor);
1100 /* Generate a constructor to run it. */
1101 cgraph_build_static_cdtor ('I', ctor, DEFAULT_INIT_PRIORITY);
1104 /* Create the gcov_info types and object. Generate the constructor
1105 function to call __gcov_init. Does not generate the initializer
1106 for the object. Returns TRUE if coverage data is being emitted. */
1108 static bool
1109 coverage_obj_init (void)
1111 tree gcov_info_type;
1112 unsigned n_counters = 0;
1113 unsigned ix;
1114 struct coverage_data *fn;
1115 struct coverage_data **fn_prev;
1116 char name_buf[32];
1118 no_coverage = 1; /* Disable any further coverage. */
1120 if (!prg_ctr_mask)
1121 return false;
1123 if (symtab->dump_file)
1124 fprintf (symtab->dump_file, "Using data file %s\n", da_file_name);
1126 /* Prune functions. */
1127 for (fn_prev = &functions_head; (fn = *fn_prev);)
1128 if (DECL_STRUCT_FUNCTION (fn->fn_decl))
1129 fn_prev = &fn->next;
1130 else
1131 /* The function is not being emitted, remove from list. */
1132 *fn_prev = fn->next;
1134 if (functions_head == NULL)
1135 return false;
1137 for (ix = 0; ix != GCOV_COUNTERS; ix++)
1138 if ((1u << ix) & prg_ctr_mask)
1139 n_counters++;
1141 /* Build the info and fn_info types. These are mutually recursive. */
1142 gcov_info_type = lang_hooks.types.make_type (RECORD_TYPE);
1143 gcov_fn_info_type = lang_hooks.types.make_type (RECORD_TYPE);
1144 build_fn_info_type (gcov_fn_info_type, n_counters, gcov_info_type);
1145 gcov_info_type = lang_hooks.types.make_type (RECORD_TYPE);
1146 gcov_fn_info_ptr_type = build_pointer_type
1147 (build_qualified_type (gcov_fn_info_type, TYPE_QUAL_CONST));
1148 build_info_type (gcov_info_type, gcov_fn_info_ptr_type);
1150 /* Build the gcov info var, this is referred to in its own
1151 initializer. */
1152 gcov_info_var = build_decl (BUILTINS_LOCATION,
1153 VAR_DECL, NULL_TREE, gcov_info_type);
1154 TREE_STATIC (gcov_info_var) = 1;
1155 ASM_GENERATE_INTERNAL_LABEL (name_buf, "LPBX", 0);
1156 DECL_NAME (gcov_info_var) = get_identifier (name_buf);
1158 build_init_ctor (gcov_info_type);
1160 return true;
1163 /* Generate the coverage function info for FN and DATA. Append a
1164 pointer to that object to CTOR and return the appended CTOR. */
1166 static vec<constructor_elt, va_gc> *
1167 coverage_obj_fn (vec<constructor_elt, va_gc> *ctor, tree fn,
1168 struct coverage_data const *data)
1170 tree init = build_fn_info (data, gcov_fn_info_type, gcov_info_var);
1171 tree var = build_var (fn, gcov_fn_info_type, -1);
1173 DECL_INITIAL (var) = init;
1174 varpool_node::finalize_decl (var);
1176 CONSTRUCTOR_APPEND_ELT (ctor, NULL,
1177 build1 (ADDR_EXPR, gcov_fn_info_ptr_type, var));
1178 return ctor;
1181 /* Finalize the coverage data. Generates the array of pointers to
1182 function objects from CTOR. Generate the gcov_info initializer. */
1184 static void
1185 coverage_obj_finish (vec<constructor_elt, va_gc> *ctor)
1187 unsigned n_functions = vec_safe_length (ctor);
1188 tree fn_info_ary_type = build_array_type
1189 (build_qualified_type (gcov_fn_info_ptr_type, TYPE_QUAL_CONST),
1190 build_index_type (size_int (n_functions - 1)));
1191 tree fn_info_ary = build_decl (BUILTINS_LOCATION, VAR_DECL, NULL_TREE,
1192 fn_info_ary_type);
1193 char name_buf[32];
1195 TREE_STATIC (fn_info_ary) = 1;
1196 ASM_GENERATE_INTERNAL_LABEL (name_buf, "LPBX", 1);
1197 DECL_NAME (fn_info_ary) = get_identifier (name_buf);
1198 DECL_INITIAL (fn_info_ary) = build_constructor (fn_info_ary_type, ctor);
1199 varpool_node::finalize_decl (fn_info_ary);
1201 DECL_INITIAL (gcov_info_var)
1202 = build_info (TREE_TYPE (gcov_info_var), fn_info_ary);
1203 varpool_node::finalize_decl (gcov_info_var);
1206 /* Perform file-level initialization. Read in data file, generate name
1207 of notes file. */
1209 void
1210 coverage_init (const char *filename)
1212 int len = strlen (filename);
1213 int prefix_len = 0;
1215 /* Since coverage_init is invoked very early, before the pass
1216 manager, we need to set up the dumping explicitly. This is
1217 similar to the handling in finish_optimization_passes. */
1218 int profile_pass_num =
1219 g->get_passes ()->get_pass_profile ()->static_pass_number;
1220 g->get_dumps ()->dump_start (profile_pass_num, NULL);
1222 if (!profile_data_prefix && !IS_ABSOLUTE_PATH (filename))
1223 profile_data_prefix = getpwd ();
1225 if (profile_data_prefix)
1226 prefix_len = strlen (profile_data_prefix);
1228 /* Name of da file. */
1229 da_file_name = XNEWVEC (char, len + strlen (GCOV_DATA_SUFFIX)
1230 + prefix_len + 2);
1232 if (profile_data_prefix)
1234 memcpy (da_file_name, profile_data_prefix, prefix_len);
1235 da_file_name[prefix_len++] = '/';
1237 memcpy (da_file_name + prefix_len, filename, len);
1238 strcpy (da_file_name + prefix_len + len, GCOV_DATA_SUFFIX);
1240 bbg_file_stamp = local_tick;
1242 if (flag_auto_profile)
1243 read_autofdo_file ();
1244 else if (flag_branch_probabilities)
1245 read_counts_file ();
1247 /* Name of bbg file. */
1248 if (flag_test_coverage && !flag_compare_debug)
1250 bbg_file_name = XNEWVEC (char, len + strlen (GCOV_NOTE_SUFFIX) + 1);
1251 memcpy (bbg_file_name, filename, len);
1252 strcpy (bbg_file_name + len, GCOV_NOTE_SUFFIX);
1254 if (!gcov_open (bbg_file_name, -1))
1256 error ("cannot open %s", bbg_file_name);
1257 bbg_file_name = NULL;
1259 else
1261 gcov_write_unsigned (GCOV_NOTE_MAGIC);
1262 gcov_write_unsigned (GCOV_VERSION);
1263 gcov_write_unsigned (bbg_file_stamp);
1267 g->get_dumps ()->dump_finish (profile_pass_num);
1270 /* Performs file-level cleanup. Close notes file, generate coverage
1271 variables and constructor. */
1273 void
1274 coverage_finish (void)
1276 if (bbg_file_name && gcov_close ())
1277 unlink (bbg_file_name);
1279 if (!flag_branch_probabilities && flag_test_coverage
1280 && (!local_tick || local_tick == (unsigned)-1))
1281 /* Only remove the da file, if we're emitting coverage code and
1282 cannot uniquely stamp it. If we can stamp it, libgcov will DTRT. */
1283 unlink (da_file_name);
1285 if (coverage_obj_init ())
1287 vec<constructor_elt, va_gc> *fn_ctor = NULL;
1288 struct coverage_data *fn;
1290 for (fn = functions_head; fn; fn = fn->next)
1291 fn_ctor = coverage_obj_fn (fn_ctor, fn->fn_decl, fn);
1292 coverage_obj_finish (fn_ctor);
1295 XDELETEVEC (da_file_name);
1296 da_file_name = NULL;
1299 #include "gt-coverage.h"