Daily bump.
[official-gcc.git] / gcc / coverage.c
blob2ddf710bdf81792f907f18d5fdac3d0029d3f1b0
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 "cgraph.h"
65 #include "dumpfile.h"
66 #include "diagnostic-core.h"
67 #include "intl.h"
68 #include "filenames.h"
69 #include "target.h"
70 #include "params.h"
71 #include "auto-profile.h"
73 #include "gcov-io.h"
74 #include "gcov-io.c"
76 struct GTY((chain_next ("%h.next"))) coverage_data
78 struct coverage_data *next; /* next function */
79 unsigned ident; /* function ident */
80 unsigned lineno_checksum; /* function lineno checksum */
81 unsigned cfg_checksum; /* function cfg checksum */
82 tree fn_decl; /* the function decl */
83 tree ctr_vars[GCOV_COUNTERS]; /* counter variables. */
86 /* Counts information for a function. */
87 typedef struct counts_entry : pointer_hash <counts_entry>
89 /* We hash by */
90 unsigned ident;
91 unsigned ctr;
93 /* Store */
94 unsigned lineno_checksum;
95 unsigned cfg_checksum;
96 gcov_type *counts;
97 struct gcov_ctr_summary summary;
99 /* hash_table support. */
100 static inline hashval_t hash (const counts_entry *);
101 static int equal (const counts_entry *, const counts_entry *);
102 static void remove (counts_entry *);
103 } counts_entry_t;
105 static GTY(()) struct coverage_data *functions_head = 0;
106 static struct coverage_data **functions_tail = &functions_head;
107 static unsigned no_coverage = 0;
109 /* Cumulative counter information for whole program. */
110 static unsigned prg_ctr_mask; /* Mask of counter types generated. */
112 /* Counter information for current function. */
113 static unsigned fn_ctr_mask; /* Mask of counters used. */
114 static GTY(()) tree fn_v_ctrs[GCOV_COUNTERS]; /* counter variables. */
115 static unsigned fn_n_ctrs[GCOV_COUNTERS]; /* Counters allocated. */
116 static unsigned fn_b_ctrs[GCOV_COUNTERS]; /* Allocation base. */
118 /* Coverage info VAR_DECL and function info type nodes. */
119 static GTY(()) tree gcov_info_var;
120 static GTY(()) tree gcov_fn_info_type;
121 static GTY(()) tree gcov_fn_info_ptr_type;
123 /* Name of the notes (gcno) output file. The "bbg" prefix is for
124 historical reasons, when the notes file contained only the
125 basic block graph notes.
126 If this is NULL we're not writing to the notes file. */
127 static char *bbg_file_name;
129 /* File stamp for notes file. */
130 static unsigned bbg_file_stamp;
132 /* Name of the count data (gcda) file. */
133 static char *da_file_name;
135 /* The names of merge functions for counters. */
136 #define STR(str) #str
137 #define DEF_GCOV_COUNTER(COUNTER, NAME, FN_TYPE) STR(__gcov_merge ## FN_TYPE),
138 static const char *const ctr_merge_functions[GCOV_COUNTERS] = {
139 #include "gcov-counter.def"
141 #undef DEF_GCOV_COUNTER
142 #undef STR
144 #define DEF_GCOV_COUNTER(COUNTER, NAME, FN_TYPE) NAME,
145 static const char *const ctr_names[GCOV_COUNTERS] = {
146 #include "gcov-counter.def"
148 #undef DEF_GCOV_COUNTER
150 /* Forward declarations. */
151 static void read_counts_file (void);
152 static tree build_var (tree, tree, int);
153 static void build_fn_info_type (tree, unsigned, tree);
154 static void build_info_type (tree, tree);
155 static tree build_fn_info (const struct coverage_data *, tree, tree);
156 static tree build_info (tree, tree);
157 static bool coverage_obj_init (void);
158 static vec<constructor_elt, va_gc> *coverage_obj_fn
159 (vec<constructor_elt, va_gc> *, tree, struct coverage_data const *);
160 static void coverage_obj_finish (vec<constructor_elt, va_gc> *);
162 /* Return the type node for gcov_type. */
164 tree
165 get_gcov_type (void)
167 machine_mode mode = smallest_mode_for_size (GCOV_TYPE_SIZE, MODE_INT);
168 return lang_hooks.types.type_for_mode (mode, false);
171 /* Return the type node for gcov_unsigned_t. */
173 static tree
174 get_gcov_unsigned_t (void)
176 machine_mode mode = smallest_mode_for_size (32, MODE_INT);
177 return lang_hooks.types.type_for_mode (mode, true);
180 inline hashval_t
181 counts_entry::hash (const counts_entry *entry)
183 return entry->ident * GCOV_COUNTERS + entry->ctr;
186 inline int
187 counts_entry::equal (const counts_entry *entry1, const counts_entry *entry2)
189 return entry1->ident == entry2->ident && entry1->ctr == entry2->ctr;
192 inline void
193 counts_entry::remove (counts_entry *entry)
195 free (entry->counts);
196 free (entry);
199 /* Hash table of count data. */
200 static hash_table<counts_entry> *counts_hash;
202 /* Read in the counts file, if available. */
204 static void
205 read_counts_file (void)
207 gcov_unsigned_t fn_ident = 0;
208 struct gcov_summary summary;
209 unsigned new_summary = 1;
210 gcov_unsigned_t tag;
211 int is_error = 0;
212 unsigned lineno_checksum = 0;
213 unsigned cfg_checksum = 0;
215 if (!gcov_open (da_file_name, 1))
216 return;
218 if (!gcov_magic (gcov_read_unsigned (), GCOV_DATA_MAGIC))
220 warning (0, "%qs is not a gcov data file", da_file_name);
221 gcov_close ();
222 return;
224 else if ((tag = gcov_read_unsigned ()) != GCOV_VERSION)
226 char v[4], e[4];
228 GCOV_UNSIGNED2STRING (v, tag);
229 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
231 warning (0, "%qs is version %q.*s, expected version %q.*s",
232 da_file_name, 4, v, 4, e);
233 gcov_close ();
234 return;
237 /* Read the stamp, used for creating a generation count. */
238 tag = gcov_read_unsigned ();
239 bbg_file_stamp = crc32_unsigned (bbg_file_stamp, tag);
241 counts_hash = new hash_table<counts_entry> (10);
242 while ((tag = gcov_read_unsigned ()))
244 gcov_unsigned_t length;
245 gcov_position_t offset;
247 length = gcov_read_unsigned ();
248 offset = gcov_position ();
249 if (tag == GCOV_TAG_FUNCTION)
251 if (length)
253 fn_ident = gcov_read_unsigned ();
254 lineno_checksum = gcov_read_unsigned ();
255 cfg_checksum = gcov_read_unsigned ();
257 else
258 fn_ident = lineno_checksum = cfg_checksum = 0;
259 new_summary = 1;
261 else if (tag == GCOV_TAG_PROGRAM_SUMMARY)
263 struct gcov_summary sum;
264 unsigned ix;
266 if (new_summary)
267 memset (&summary, 0, sizeof (summary));
269 gcov_read_summary (&sum);
270 for (ix = 0; ix != GCOV_COUNTERS_SUMMABLE; ix++)
272 summary.ctrs[ix].runs += sum.ctrs[ix].runs;
273 summary.ctrs[ix].sum_all += sum.ctrs[ix].sum_all;
274 if (summary.ctrs[ix].run_max < sum.ctrs[ix].run_max)
275 summary.ctrs[ix].run_max = sum.ctrs[ix].run_max;
276 summary.ctrs[ix].sum_max += sum.ctrs[ix].sum_max;
278 if (new_summary)
279 memcpy (summary.ctrs[GCOV_COUNTER_ARCS].histogram,
280 sum.ctrs[GCOV_COUNTER_ARCS].histogram,
281 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
282 else
283 gcov_histogram_merge (summary.ctrs[GCOV_COUNTER_ARCS].histogram,
284 sum.ctrs[GCOV_COUNTER_ARCS].histogram);
285 new_summary = 0;
287 else if (GCOV_TAG_IS_COUNTER (tag) && fn_ident)
289 counts_entry_t **slot, *entry, elt;
290 unsigned n_counts = GCOV_TAG_COUNTER_NUM (length);
291 unsigned ix;
293 elt.ident = fn_ident;
294 elt.ctr = GCOV_COUNTER_FOR_TAG (tag);
296 slot = counts_hash->find_slot (&elt, INSERT);
297 entry = *slot;
298 if (!entry)
300 *slot = entry = XCNEW (counts_entry_t);
301 entry->ident = fn_ident;
302 entry->ctr = elt.ctr;
303 entry->lineno_checksum = lineno_checksum;
304 entry->cfg_checksum = cfg_checksum;
305 if (elt.ctr < GCOV_COUNTERS_SUMMABLE)
306 entry->summary = summary.ctrs[elt.ctr];
307 entry->summary.num = n_counts;
308 entry->counts = XCNEWVEC (gcov_type, n_counts);
310 else if (entry->lineno_checksum != lineno_checksum
311 || entry->cfg_checksum != cfg_checksum)
313 error ("Profile data for function %u is corrupted", fn_ident);
314 error ("checksum is (%x,%x) instead of (%x,%x)",
315 entry->lineno_checksum, entry->cfg_checksum,
316 lineno_checksum, cfg_checksum);
317 delete counts_hash;
318 counts_hash = NULL;
319 break;
321 else if (entry->summary.num != n_counts)
323 error ("Profile data for function %u is corrupted", fn_ident);
324 error ("number of counters is %d instead of %d", entry->summary.num, n_counts);
325 delete counts_hash;
326 counts_hash = NULL;
327 break;
329 else if (elt.ctr >= GCOV_COUNTERS_SUMMABLE)
331 error ("cannot merge separate %s counters for function %u",
332 ctr_names[elt.ctr], fn_ident);
333 goto skip_merge;
335 else
337 entry->summary.runs += summary.ctrs[elt.ctr].runs;
338 entry->summary.sum_all += summary.ctrs[elt.ctr].sum_all;
339 if (entry->summary.run_max < summary.ctrs[elt.ctr].run_max)
340 entry->summary.run_max = summary.ctrs[elt.ctr].run_max;
341 entry->summary.sum_max += summary.ctrs[elt.ctr].sum_max;
343 for (ix = 0; ix != n_counts; ix++)
344 entry->counts[ix] += gcov_read_counter ();
345 skip_merge:;
347 gcov_sync (offset, length);
348 if ((is_error = gcov_is_error ()))
350 error (is_error < 0 ? "%qs has overflowed" : "%qs is corrupted",
351 da_file_name);
352 delete counts_hash;
353 counts_hash = NULL;
354 break;
358 gcov_close ();
361 /* Returns the counters for a particular tag. */
363 gcov_type *
364 get_coverage_counts (unsigned counter, unsigned expected,
365 unsigned cfg_checksum, unsigned lineno_checksum,
366 const struct gcov_ctr_summary **summary)
368 counts_entry_t *entry, elt;
370 /* No hash table, no counts. */
371 if (!counts_hash)
373 static int warned = 0;
375 if (!warned++ && dump_enabled_p ())
376 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
377 (flag_guess_branch_prob
378 ? "file %s not found, execution counts estimated\n"
379 : "file %s not found, execution counts assumed to "
380 "be zero\n"),
381 da_file_name);
382 return NULL;
384 if (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID))
385 elt.ident = current_function_funcdef_no + 1;
386 else
388 gcc_assert (coverage_node_map_initialized_p ());
389 elt.ident = cgraph_node::get (cfun->decl)->profile_id;
391 elt.ctr = counter;
392 entry = counts_hash->find (&elt);
393 if (!entry || !entry->summary.num)
394 /* The function was not emitted, or is weak and not chosen in the
395 final executable. Silently fail, because there's nothing we
396 can do about it. */
397 return NULL;
399 if (entry->cfg_checksum != cfg_checksum
400 || entry->summary.num != expected)
402 static int warned = 0;
403 bool warning_printed = false;
404 tree id = DECL_ASSEMBLER_NAME (current_function_decl);
406 warning_printed =
407 warning_at (input_location, OPT_Wcoverage_mismatch,
408 "the control flow of function %qE does not match "
409 "its profile data (counter %qs)", id, ctr_names[counter]);
410 if (warning_printed && dump_enabled_p ())
412 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
413 "use -Wno-error=coverage-mismatch to tolerate "
414 "the mismatch but performance may drop if the "
415 "function is hot\n");
417 if (!seen_error ()
418 && !warned++)
420 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
421 "coverage mismatch ignored\n");
422 dump_printf (MSG_OPTIMIZED_LOCATIONS,
423 flag_guess_branch_prob
424 ? G_("execution counts estimated\n")
425 : G_("execution counts assumed to be zero\n"));
426 if (!flag_guess_branch_prob)
427 dump_printf (MSG_OPTIMIZED_LOCATIONS,
428 "this can result in poorly optimized code\n");
432 return NULL;
434 else if (entry->lineno_checksum != lineno_checksum)
436 warning (OPT_Wcoverage_mismatch,
437 "source locations for function %qE have changed,"
438 " the profile data may be out of date",
439 DECL_ASSEMBLER_NAME (current_function_decl));
442 if (summary)
443 *summary = &entry->summary;
445 return entry->counts;
448 /* Allocate NUM counters of type COUNTER. Returns nonzero if the
449 allocation succeeded. */
452 coverage_counter_alloc (unsigned counter, unsigned num)
454 if (no_coverage)
455 return 0;
457 if (!num)
458 return 1;
460 if (!fn_v_ctrs[counter])
462 tree array_type = build_array_type (get_gcov_type (), NULL_TREE);
464 fn_v_ctrs[counter]
465 = build_var (current_function_decl, array_type, counter);
468 fn_b_ctrs[counter] = fn_n_ctrs[counter];
469 fn_n_ctrs[counter] += num;
471 fn_ctr_mask |= 1 << counter;
472 return 1;
475 /* Generate a tree to access COUNTER NO. */
477 tree
478 tree_coverage_counter_ref (unsigned counter, unsigned no)
480 tree gcov_type_node = get_gcov_type ();
482 gcc_assert (no < fn_n_ctrs[counter] - fn_b_ctrs[counter]);
484 no += fn_b_ctrs[counter];
486 /* "no" here is an array index, scaled to bytes later. */
487 return build4 (ARRAY_REF, gcov_type_node, fn_v_ctrs[counter],
488 build_int_cst (integer_type_node, no), NULL, NULL);
491 /* Generate a tree to access the address of COUNTER NO. */
493 tree
494 tree_coverage_counter_addr (unsigned counter, unsigned no)
496 tree gcov_type_node = get_gcov_type ();
498 gcc_assert (no < fn_n_ctrs[counter] - fn_b_ctrs[counter]);
499 no += fn_b_ctrs[counter];
501 /* "no" here is an array index, scaled to bytes later. */
502 return build_fold_addr_expr (build4 (ARRAY_REF, gcov_type_node,
503 fn_v_ctrs[counter],
504 build_int_cst (integer_type_node, no),
505 NULL, NULL));
509 /* Generate a checksum for a string. CHKSUM is the current
510 checksum. */
512 static unsigned
513 coverage_checksum_string (unsigned chksum, const char *string)
515 int i;
516 char *dup = NULL;
518 /* Look for everything that looks if it were produced by
519 get_file_function_name and zero out the second part
520 that may result from flag_random_seed. This is not critical
521 as the checksums are used only for sanity checking. */
522 for (i = 0; string[i]; i++)
524 int offset = 0;
525 if (!strncmp (string + i, "_GLOBAL__N_", 11))
526 offset = 11;
527 if (!strncmp (string + i, "_GLOBAL__", 9))
528 offset = 9;
530 /* C++ namespaces do have scheme:
531 _GLOBAL__N_<filename>_<wrongmagicnumber>_<magicnumber>functionname
532 since filename might contain extra underscores there seems
533 to be no better chance then walk all possible offsets looking
534 for magicnumber. */
535 if (offset)
537 for (i = i + offset; string[i]; i++)
538 if (string[i]=='_')
540 int y;
542 for (y = 1; y < 9; y++)
543 if (!(string[i + y] >= '0' && string[i + y] <= '9')
544 && !(string[i + y] >= 'A' && string[i + y] <= 'F'))
545 break;
546 if (y != 9 || string[i + 9] != '_')
547 continue;
548 for (y = 10; y < 18; y++)
549 if (!(string[i + y] >= '0' && string[i + y] <= '9')
550 && !(string[i + y] >= 'A' && string[i + y] <= 'F'))
551 break;
552 if (y != 18)
553 continue;
554 if (!dup)
555 string = dup = xstrdup (string);
556 for (y = 10; y < 18; y++)
557 dup[i + y] = '0';
559 break;
563 chksum = crc32_string (chksum, string);
564 free (dup);
566 return chksum;
569 /* Compute checksum for the current function. We generate a CRC32. */
571 unsigned
572 coverage_compute_lineno_checksum (void)
574 expanded_location xloc
575 = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
576 unsigned chksum = xloc.line;
578 chksum = coverage_checksum_string (chksum, xloc.file);
579 chksum = coverage_checksum_string
580 (chksum, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl)));
582 return chksum;
585 /* Compute profile ID. This is better to be unique in whole program. */
587 unsigned
588 coverage_compute_profile_id (struct cgraph_node *n)
590 unsigned chksum;
592 /* Externally visible symbols have unique name. */
593 if (TREE_PUBLIC (n->decl) || DECL_EXTERNAL (n->decl) || n->unique_name)
595 chksum = coverage_checksum_string
596 (0, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n->decl)));
598 else
600 expanded_location xloc
601 = expand_location (DECL_SOURCE_LOCATION (n->decl));
602 bool use_name_only = (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID) == 0);
604 chksum = (use_name_only ? 0 : xloc.line);
605 chksum = coverage_checksum_string (chksum, xloc.file);
606 chksum = coverage_checksum_string
607 (chksum, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n->decl)));
608 if (!use_name_only && first_global_object_name)
609 chksum = coverage_checksum_string
610 (chksum, first_global_object_name);
611 chksum = coverage_checksum_string
612 (chksum, aux_base_name);
615 /* Non-negative integers are hopefully small enough to fit in all targets.
616 Gcov file formats wants non-zero function IDs. */
617 chksum = chksum & 0x7fffffff;
618 return chksum + (!chksum);
621 /* Compute cfg checksum for the function FN given as argument.
622 The checksum is calculated carefully so that
623 source code changes that doesn't affect the control flow graph
624 won't change the checksum.
625 This is to make the profile data useable across source code change.
626 The downside of this is that the compiler may use potentially
627 wrong profile data - that the source code change has non-trivial impact
628 on the validity of profile data (e.g. the reversed condition)
629 but the compiler won't detect the change and use the wrong profile data. */
631 unsigned
632 coverage_compute_cfg_checksum (struct function *fn)
634 basic_block bb;
635 unsigned chksum = n_basic_blocks_for_fn (fn);
637 FOR_EACH_BB_FN (bb, fn)
639 edge e;
640 edge_iterator ei;
641 chksum = crc32_byte (chksum, bb->index);
642 FOR_EACH_EDGE (e, ei, bb->succs)
644 chksum = crc32_byte (chksum, e->dest->index);
648 return chksum;
651 /* Begin output to the notes file for the current function.
652 Writes the function header. Returns nonzero if data should be output. */
655 coverage_begin_function (unsigned lineno_checksum, unsigned cfg_checksum)
657 expanded_location xloc;
658 unsigned long offset;
660 /* We don't need to output .gcno file unless we're under -ftest-coverage
661 (e.g. -fprofile-arcs/generate/use don't need .gcno to work). */
662 if (no_coverage || !bbg_file_name)
663 return 0;
665 xloc = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
667 /* Announce function */
668 offset = gcov_write_tag (GCOV_TAG_FUNCTION);
669 if (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID))
670 gcov_write_unsigned (current_function_funcdef_no + 1);
671 else
673 gcc_assert (coverage_node_map_initialized_p ());
674 gcov_write_unsigned (
675 cgraph_node::get (current_function_decl)->profile_id);
678 gcov_write_unsigned (lineno_checksum);
679 gcov_write_unsigned (cfg_checksum);
680 gcov_write_string (IDENTIFIER_POINTER
681 (DECL_ASSEMBLER_NAME (current_function_decl)));
682 gcov_write_string (xloc.file);
683 gcov_write_unsigned (xloc.line);
684 gcov_write_length (offset);
686 return !gcov_is_error ();
689 /* Finish coverage data for the current function. Verify no output
690 error has occurred. Save function coverage counts. */
692 void
693 coverage_end_function (unsigned lineno_checksum, unsigned cfg_checksum)
695 unsigned i;
697 if (bbg_file_name && gcov_is_error ())
699 warning (0, "error writing %qs", bbg_file_name);
700 unlink (bbg_file_name);
701 bbg_file_name = NULL;
704 if (fn_ctr_mask)
706 struct coverage_data *item = 0;
708 item = ggc_alloc<coverage_data> ();
710 if (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID))
711 item->ident = current_function_funcdef_no + 1;
712 else
714 gcc_assert (coverage_node_map_initialized_p ());
715 item->ident = cgraph_node::get (cfun->decl)->profile_id;
718 item->lineno_checksum = lineno_checksum;
719 item->cfg_checksum = cfg_checksum;
721 item->fn_decl = current_function_decl;
722 item->next = 0;
723 *functions_tail = item;
724 functions_tail = &item->next;
726 for (i = 0; i != GCOV_COUNTERS; i++)
728 tree var = fn_v_ctrs[i];
730 if (item)
731 item->ctr_vars[i] = var;
732 if (var)
734 tree array_type = build_index_type (size_int (fn_n_ctrs[i] - 1));
735 array_type = build_array_type (get_gcov_type (), array_type);
736 TREE_TYPE (var) = array_type;
737 DECL_SIZE (var) = TYPE_SIZE (array_type);
738 DECL_SIZE_UNIT (var) = TYPE_SIZE_UNIT (array_type);
739 varpool_node::finalize_decl (var);
742 fn_b_ctrs[i] = fn_n_ctrs[i] = 0;
743 fn_v_ctrs[i] = NULL_TREE;
745 prg_ctr_mask |= fn_ctr_mask;
746 fn_ctr_mask = 0;
750 /* Build a coverage variable of TYPE for function FN_DECL. If COUNTER
751 >= 0 it is a counter array, otherwise it is the function structure. */
753 static tree
754 build_var (tree fn_decl, tree type, int counter)
756 tree var = build_decl (BUILTINS_LOCATION, VAR_DECL, NULL_TREE, type);
757 const char *fn_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fn_decl));
758 char *buf;
759 size_t fn_name_len, len;
761 fn_name = targetm.strip_name_encoding (fn_name);
762 fn_name_len = strlen (fn_name);
763 buf = XALLOCAVEC (char, fn_name_len + 8 + sizeof (int) * 3);
765 if (counter < 0)
766 strcpy (buf, "__gcov__");
767 else
768 sprintf (buf, "__gcov%u_", counter);
769 len = strlen (buf);
770 #ifndef NO_DOT_IN_LABEL
771 buf[len - 1] = '.';
772 #elif !defined NO_DOLLAR_IN_LABEL
773 buf[len - 1] = '$';
774 #endif
775 memcpy (buf + len, fn_name, fn_name_len + 1);
776 DECL_NAME (var) = get_identifier (buf);
777 TREE_STATIC (var) = 1;
778 TREE_ADDRESSABLE (var) = 1;
779 DECL_NONALIASED (var) = 1;
780 DECL_ALIGN (var) = TYPE_ALIGN (type);
782 return var;
785 /* Creates the gcov_fn_info RECORD_TYPE. */
787 static void
788 build_fn_info_type (tree type, unsigned counters, tree gcov_info_type)
790 tree ctr_info = lang_hooks.types.make_type (RECORD_TYPE);
791 tree field, fields;
792 tree array_type;
794 gcc_assert (counters);
796 /* ctr_info::num */
797 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
798 get_gcov_unsigned_t ());
799 fields = field;
801 /* ctr_info::values */
802 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
803 build_pointer_type (get_gcov_type ()));
804 DECL_CHAIN (field) = fields;
805 fields = field;
807 finish_builtin_struct (ctr_info, "__gcov_ctr_info", fields, NULL_TREE);
809 /* key */
810 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
811 build_pointer_type (build_qualified_type
812 (gcov_info_type, TYPE_QUAL_CONST)));
813 fields = field;
815 /* ident */
816 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
817 get_gcov_unsigned_t ());
818 DECL_CHAIN (field) = fields;
819 fields = field;
821 /* lineno_checksum */
822 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
823 get_gcov_unsigned_t ());
824 DECL_CHAIN (field) = fields;
825 fields = field;
827 /* cfg checksum */
828 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
829 get_gcov_unsigned_t ());
830 DECL_CHAIN (field) = fields;
831 fields = field;
833 array_type = build_index_type (size_int (counters - 1));
834 array_type = build_array_type (ctr_info, array_type);
836 /* counters */
837 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE, array_type);
838 DECL_CHAIN (field) = fields;
839 fields = field;
841 finish_builtin_struct (type, "__gcov_fn_info", fields, NULL_TREE);
844 /* Returns a CONSTRUCTOR for a gcov_fn_info. DATA is
845 the coverage data for the function and TYPE is the gcov_fn_info
846 RECORD_TYPE. KEY is the object file key. */
848 static tree
849 build_fn_info (const struct coverage_data *data, tree type, tree key)
851 tree fields = TYPE_FIELDS (type);
852 tree ctr_type;
853 unsigned ix;
854 vec<constructor_elt, va_gc> *v1 = NULL;
855 vec<constructor_elt, va_gc> *v2 = NULL;
857 /* key */
858 CONSTRUCTOR_APPEND_ELT (v1, fields,
859 build1 (ADDR_EXPR, TREE_TYPE (fields), key));
860 fields = DECL_CHAIN (fields);
862 /* ident */
863 CONSTRUCTOR_APPEND_ELT (v1, fields,
864 build_int_cstu (get_gcov_unsigned_t (),
865 data->ident));
866 fields = DECL_CHAIN (fields);
868 /* lineno_checksum */
869 CONSTRUCTOR_APPEND_ELT (v1, fields,
870 build_int_cstu (get_gcov_unsigned_t (),
871 data->lineno_checksum));
872 fields = DECL_CHAIN (fields);
874 /* cfg_checksum */
875 CONSTRUCTOR_APPEND_ELT (v1, fields,
876 build_int_cstu (get_gcov_unsigned_t (),
877 data->cfg_checksum));
878 fields = DECL_CHAIN (fields);
880 /* counters */
881 ctr_type = TREE_TYPE (TREE_TYPE (fields));
882 for (ix = 0; ix != GCOV_COUNTERS; ix++)
883 if (prg_ctr_mask & (1 << ix))
885 vec<constructor_elt, va_gc> *ctr = NULL;
886 tree var = data->ctr_vars[ix];
887 unsigned count = 0;
889 if (var)
890 count
891 = tree_to_shwi (TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (var))))
892 + 1;
894 CONSTRUCTOR_APPEND_ELT (ctr, TYPE_FIELDS (ctr_type),
895 build_int_cstu (get_gcov_unsigned_t (),
896 count));
898 if (var)
899 CONSTRUCTOR_APPEND_ELT (ctr, DECL_CHAIN (TYPE_FIELDS (ctr_type)),
900 build_fold_addr_expr (var));
902 CONSTRUCTOR_APPEND_ELT (v2, NULL, build_constructor (ctr_type, ctr));
905 CONSTRUCTOR_APPEND_ELT (v1, fields,
906 build_constructor (TREE_TYPE (fields), v2));
908 return build_constructor (type, v1);
911 /* Create gcov_info struct. TYPE is the incomplete RECORD_TYPE to be
912 completed, and FN_INFO_PTR_TYPE is a pointer to the function info type. */
914 static void
915 build_info_type (tree type, tree fn_info_ptr_type)
917 tree field, fields = NULL_TREE;
918 tree merge_fn_type;
920 /* Version ident */
921 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
922 get_gcov_unsigned_t ());
923 DECL_CHAIN (field) = fields;
924 fields = field;
926 /* next pointer */
927 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
928 build_pointer_type (build_qualified_type
929 (type, TYPE_QUAL_CONST)));
930 DECL_CHAIN (field) = fields;
931 fields = field;
933 /* stamp */
934 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
935 get_gcov_unsigned_t ());
936 DECL_CHAIN (field) = fields;
937 fields = field;
939 /* Filename */
940 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
941 build_pointer_type (build_qualified_type
942 (char_type_node, TYPE_QUAL_CONST)));
943 DECL_CHAIN (field) = fields;
944 fields = field;
946 /* merge fn array */
947 merge_fn_type
948 = build_function_type_list (void_type_node,
949 build_pointer_type (get_gcov_type ()),
950 get_gcov_unsigned_t (), NULL_TREE);
951 merge_fn_type
952 = build_array_type (build_pointer_type (merge_fn_type),
953 build_index_type (size_int (GCOV_COUNTERS - 1)));
954 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
955 merge_fn_type);
956 DECL_CHAIN (field) = fields;
957 fields = field;
959 /* n_functions */
960 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
961 get_gcov_unsigned_t ());
962 DECL_CHAIN (field) = fields;
963 fields = field;
965 /* function_info pointer pointer */
966 fn_info_ptr_type = build_pointer_type
967 (build_qualified_type (fn_info_ptr_type, TYPE_QUAL_CONST));
968 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
969 fn_info_ptr_type);
970 DECL_CHAIN (field) = fields;
971 fields = field;
973 finish_builtin_struct (type, "__gcov_info", fields, NULL_TREE);
976 /* Returns a CONSTRUCTOR for the gcov_info object. INFO_TYPE is the
977 gcov_info structure type, FN_ARY is the array of pointers to
978 function info objects. */
980 static tree
981 build_info (tree info_type, tree fn_ary)
983 tree info_fields = TYPE_FIELDS (info_type);
984 tree merge_fn_type, n_funcs;
985 unsigned ix;
986 tree filename_string;
987 int da_file_name_len;
988 vec<constructor_elt, va_gc> *v1 = NULL;
989 vec<constructor_elt, va_gc> *v2 = NULL;
991 /* Version ident */
992 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
993 build_int_cstu (TREE_TYPE (info_fields),
994 GCOV_VERSION));
995 info_fields = DECL_CHAIN (info_fields);
997 /* next -- NULL */
998 CONSTRUCTOR_APPEND_ELT (v1, info_fields, null_pointer_node);
999 info_fields = DECL_CHAIN (info_fields);
1001 /* stamp */
1002 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
1003 build_int_cstu (TREE_TYPE (info_fields),
1004 bbg_file_stamp));
1005 info_fields = DECL_CHAIN (info_fields);
1007 /* Filename */
1008 da_file_name_len = strlen (da_file_name);
1009 filename_string = build_string (da_file_name_len + 1, da_file_name);
1010 TREE_TYPE (filename_string) = build_array_type
1011 (char_type_node, build_index_type (size_int (da_file_name_len)));
1012 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
1013 build1 (ADDR_EXPR, TREE_TYPE (info_fields),
1014 filename_string));
1015 info_fields = DECL_CHAIN (info_fields);
1017 /* merge fn array -- NULL slots indicate unmeasured counters */
1018 merge_fn_type = TREE_TYPE (TREE_TYPE (info_fields));
1019 for (ix = 0; ix != GCOV_COUNTERS; ix++)
1021 tree ptr = null_pointer_node;
1023 if ((1u << ix) & prg_ctr_mask)
1025 tree merge_fn = build_decl (BUILTINS_LOCATION,
1026 FUNCTION_DECL,
1027 get_identifier (ctr_merge_functions[ix]),
1028 TREE_TYPE (merge_fn_type));
1029 DECL_EXTERNAL (merge_fn) = 1;
1030 TREE_PUBLIC (merge_fn) = 1;
1031 DECL_ARTIFICIAL (merge_fn) = 1;
1032 TREE_NOTHROW (merge_fn) = 1;
1033 /* Initialize assembler name so we can stream out. */
1034 DECL_ASSEMBLER_NAME (merge_fn);
1035 ptr = build1 (ADDR_EXPR, merge_fn_type, merge_fn);
1037 CONSTRUCTOR_APPEND_ELT (v2, NULL, ptr);
1039 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
1040 build_constructor (TREE_TYPE (info_fields), v2));
1041 info_fields = DECL_CHAIN (info_fields);
1043 /* n_functions */
1044 n_funcs = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (fn_ary)));
1045 n_funcs = fold_build2 (PLUS_EXPR, TREE_TYPE (info_fields),
1046 n_funcs, size_one_node);
1047 CONSTRUCTOR_APPEND_ELT (v1, info_fields, n_funcs);
1048 info_fields = DECL_CHAIN (info_fields);
1050 /* functions */
1051 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
1052 build1 (ADDR_EXPR, TREE_TYPE (info_fields), fn_ary));
1053 info_fields = DECL_CHAIN (info_fields);
1055 gcc_assert (!info_fields);
1056 return build_constructor (info_type, v1);
1059 /* Generate the constructor function to call __gcov_init. */
1061 static void
1062 build_init_ctor (tree gcov_info_type)
1064 tree ctor, stmt, init_fn;
1066 /* Build a decl for __gcov_init. */
1067 init_fn = build_pointer_type (gcov_info_type);
1068 init_fn = build_function_type_list (void_type_node, init_fn, NULL);
1069 init_fn = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
1070 get_identifier ("__gcov_init"), init_fn);
1071 TREE_PUBLIC (init_fn) = 1;
1072 DECL_EXTERNAL (init_fn) = 1;
1073 DECL_ASSEMBLER_NAME (init_fn);
1075 /* Generate a call to __gcov_init(&gcov_info). */
1076 ctor = NULL;
1077 stmt = build_fold_addr_expr (gcov_info_var);
1078 stmt = build_call_expr (init_fn, 1, stmt);
1079 append_to_statement_list (stmt, &ctor);
1081 /* Generate a constructor to run it. */
1082 cgraph_build_static_cdtor ('I', ctor, DEFAULT_INIT_PRIORITY);
1085 /* Create the gcov_info types and object. Generate the constructor
1086 function to call __gcov_init. Does not generate the initializer
1087 for the object. Returns TRUE if coverage data is being emitted. */
1089 static bool
1090 coverage_obj_init (void)
1092 tree gcov_info_type;
1093 unsigned n_counters = 0;
1094 unsigned ix;
1095 struct coverage_data *fn;
1096 struct coverage_data **fn_prev;
1097 char name_buf[32];
1099 no_coverage = 1; /* Disable any further coverage. */
1101 if (!prg_ctr_mask)
1102 return false;
1104 if (symtab->dump_file)
1105 fprintf (symtab->dump_file, "Using data file %s\n", da_file_name);
1107 /* Prune functions. */
1108 for (fn_prev = &functions_head; (fn = *fn_prev);)
1109 if (DECL_STRUCT_FUNCTION (fn->fn_decl))
1110 fn_prev = &fn->next;
1111 else
1112 /* The function is not being emitted, remove from list. */
1113 *fn_prev = fn->next;
1115 if (functions_head == NULL)
1116 return false;
1118 for (ix = 0; ix != GCOV_COUNTERS; ix++)
1119 if ((1u << ix) & prg_ctr_mask)
1120 n_counters++;
1122 /* Build the info and fn_info types. These are mutually recursive. */
1123 gcov_info_type = lang_hooks.types.make_type (RECORD_TYPE);
1124 gcov_fn_info_type = lang_hooks.types.make_type (RECORD_TYPE);
1125 build_fn_info_type (gcov_fn_info_type, n_counters, gcov_info_type);
1126 gcov_info_type = lang_hooks.types.make_type (RECORD_TYPE);
1127 gcov_fn_info_ptr_type = build_pointer_type
1128 (build_qualified_type (gcov_fn_info_type, TYPE_QUAL_CONST));
1129 build_info_type (gcov_info_type, gcov_fn_info_ptr_type);
1131 /* Build the gcov info var, this is referred to in its own
1132 initializer. */
1133 gcov_info_var = build_decl (BUILTINS_LOCATION,
1134 VAR_DECL, NULL_TREE, gcov_info_type);
1135 TREE_STATIC (gcov_info_var) = 1;
1136 ASM_GENERATE_INTERNAL_LABEL (name_buf, "LPBX", 0);
1137 DECL_NAME (gcov_info_var) = get_identifier (name_buf);
1139 build_init_ctor (gcov_info_type);
1141 return true;
1144 /* Generate the coverage function info for FN and DATA. Append a
1145 pointer to that object to CTOR and return the appended CTOR. */
1147 static vec<constructor_elt, va_gc> *
1148 coverage_obj_fn (vec<constructor_elt, va_gc> *ctor, tree fn,
1149 struct coverage_data const *data)
1151 tree init = build_fn_info (data, gcov_fn_info_type, gcov_info_var);
1152 tree var = build_var (fn, gcov_fn_info_type, -1);
1154 DECL_INITIAL (var) = init;
1155 varpool_node::finalize_decl (var);
1157 CONSTRUCTOR_APPEND_ELT (ctor, NULL,
1158 build1 (ADDR_EXPR, gcov_fn_info_ptr_type, var));
1159 return ctor;
1162 /* Finalize the coverage data. Generates the array of pointers to
1163 function objects from CTOR. Generate the gcov_info initializer. */
1165 static void
1166 coverage_obj_finish (vec<constructor_elt, va_gc> *ctor)
1168 unsigned n_functions = vec_safe_length (ctor);
1169 tree fn_info_ary_type = build_array_type
1170 (build_qualified_type (gcov_fn_info_ptr_type, TYPE_QUAL_CONST),
1171 build_index_type (size_int (n_functions - 1)));
1172 tree fn_info_ary = build_decl (BUILTINS_LOCATION, VAR_DECL, NULL_TREE,
1173 fn_info_ary_type);
1174 char name_buf[32];
1176 TREE_STATIC (fn_info_ary) = 1;
1177 ASM_GENERATE_INTERNAL_LABEL (name_buf, "LPBX", 1);
1178 DECL_NAME (fn_info_ary) = get_identifier (name_buf);
1179 DECL_INITIAL (fn_info_ary) = build_constructor (fn_info_ary_type, ctor);
1180 varpool_node::finalize_decl (fn_info_ary);
1182 DECL_INITIAL (gcov_info_var)
1183 = build_info (TREE_TYPE (gcov_info_var), fn_info_ary);
1184 varpool_node::finalize_decl (gcov_info_var);
1187 /* Perform file-level initialization. Read in data file, generate name
1188 of notes file. */
1190 void
1191 coverage_init (const char *filename)
1193 int len = strlen (filename);
1194 int prefix_len = 0;
1196 /* Since coverage_init is invoked very early, before the pass
1197 manager, we need to set up the dumping explicitly. This is
1198 similar to the handling in finish_optimization_passes. */
1199 int profile_pass_num =
1200 g->get_passes ()->get_pass_profile ()->static_pass_number;
1201 g->get_dumps ()->dump_start (profile_pass_num, NULL);
1203 if (!profile_data_prefix && !IS_ABSOLUTE_PATH (filename))
1204 profile_data_prefix = getpwd ();
1206 if (profile_data_prefix)
1207 prefix_len = strlen (profile_data_prefix);
1209 /* Name of da file. */
1210 da_file_name = XNEWVEC (char, len + strlen (GCOV_DATA_SUFFIX)
1211 + prefix_len + 2);
1213 if (profile_data_prefix)
1215 memcpy (da_file_name, profile_data_prefix, prefix_len);
1216 da_file_name[prefix_len++] = '/';
1218 memcpy (da_file_name + prefix_len, filename, len);
1219 strcpy (da_file_name + prefix_len + len, GCOV_DATA_SUFFIX);
1221 bbg_file_stamp = local_tick;
1223 if (flag_auto_profile)
1224 read_autofdo_file ();
1225 else if (flag_branch_probabilities)
1226 read_counts_file ();
1228 /* Name of bbg file. */
1229 if (flag_test_coverage && !flag_compare_debug)
1231 bbg_file_name = XNEWVEC (char, len + strlen (GCOV_NOTE_SUFFIX) + 1);
1232 memcpy (bbg_file_name, filename, len);
1233 strcpy (bbg_file_name + len, GCOV_NOTE_SUFFIX);
1235 if (!gcov_open (bbg_file_name, -1))
1237 error ("cannot open %s", bbg_file_name);
1238 bbg_file_name = NULL;
1240 else
1242 gcov_write_unsigned (GCOV_NOTE_MAGIC);
1243 gcov_write_unsigned (GCOV_VERSION);
1244 gcov_write_unsigned (bbg_file_stamp);
1248 g->get_dumps ()->dump_finish (profile_pass_num);
1251 /* Performs file-level cleanup. Close notes file, generate coverage
1252 variables and constructor. */
1254 void
1255 coverage_finish (void)
1257 if (bbg_file_name && gcov_close ())
1258 unlink (bbg_file_name);
1260 if (!flag_branch_probabilities && flag_test_coverage
1261 && (!local_tick || local_tick == (unsigned)-1))
1262 /* Only remove the da file, if we're emitting coverage code and
1263 cannot uniquely stamp it. If we can stamp it, libgcov will DTRT. */
1264 unlink (da_file_name);
1266 if (coverage_obj_init ())
1268 vec<constructor_elt, va_gc> *fn_ctor = NULL;
1269 struct coverage_data *fn;
1271 for (fn = functions_head; fn; fn = fn->next)
1272 fn_ctor = coverage_obj_fn (fn_ctor, fn->fn_decl, fn);
1273 coverage_obj_finish (fn_ctor);
1276 XDELETEVEC (da_file_name);
1277 da_file_name = NULL;
1280 #include "gt-coverage.h"