port r209961 from v18
[official-gcc.git] / main / gcc / coverage.c
blobb3ef8e2369b8f0e86ea2758fe19f3145d3c7b727
1 /* Read and write coverage files, and associated functionality.
2 Copyright (C) 1990-2014 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 "tree.h"
33 #include "stringpool.h"
34 #include "stor-layout.h"
35 #include "flags.h"
36 #include "output.h"
37 #include "regs.h"
38 #include "expr.h"
39 #include "function.h"
40 #include "basic-block.h"
41 #include "toplev.h"
42 #include "tm_p.h"
43 #include "ggc.h"
44 #include "coverage.h"
45 #include "langhooks.h"
46 #include "hash-table.h"
47 #include "tree-iterator.h"
48 #include "context.h"
49 #include "pass_manager.h"
50 #include "tree-pass.h"
51 #include "cgraph.h"
52 #include "dumpfile.h"
53 #include "opts.h"
54 #include "gcov-io.h"
55 #include "tree-ssa-alias.h"
56 #include "internal-fn.h"
57 #include "gimple-expr.h"
58 #include "gimple.h"
59 #include "gimplify.h"
60 #include "gimple-iterator.h"
61 #include "gimplify-me.h"
62 #include "gimple-ssa.h"
63 #include "cpplib.h"
64 #include "incpath.h"
65 #include "diagnostic-core.h"
66 #include "intl.h"
67 #include "l-ipo.h"
68 #include "filenames.h"
69 #include "dwarf2asm.h"
70 #include "target.h"
72 #include "gcov-io.h"
73 #include "gcov-io.c"
74 #include "params.h"
75 #include "dbgcnt.h"
76 #include "input.h"
77 #include "pointer-set.h"
79 struct GTY((chain_next ("%h.next"))) coverage_data
81 struct coverage_data *next; /* next function */
82 unsigned ident; /* function ident */
83 unsigned lineno_checksum; /* function lineno checksum */
84 unsigned cfg_checksum; /* function cfg checksum */
85 tree fn_decl; /* the function decl */
86 tree ctr_vars[GCOV_COUNTERS]; /* counter variables. */
89 static bool profiling_enabled_p (void);
91 /* Linked list of -D/-U/-imacro/-include strings for a source module. */
92 struct str_list
94 char *str;
95 struct str_list *next;
98 /* Counts information for a function. */
99 typedef struct counts_entry
101 /* We hash by */
102 unsigned HOST_WIDEST_INT ident;
103 unsigned ctr;
105 /* Store */
106 unsigned lineno_checksum;
107 unsigned cfg_checksum;
108 gcov_type *counts;
109 struct gcov_ctr_summary summary;
111 /* hash_table support. */
112 typedef counts_entry value_type;
113 typedef counts_entry compare_type;
114 static inline hashval_t hash (const value_type *);
115 static int equal (const value_type *, const compare_type *);
116 static void remove (value_type *);
117 } counts_entry_t;
119 static GTY(()) struct coverage_data *functions_head = 0;
120 static struct coverage_data **functions_tail = &functions_head;
121 static unsigned no_coverage = 0;
123 /* Cumulative counter information for whole program. */
124 static unsigned prg_ctr_mask; /* Mask of counter types generated. */
126 /* Counter information for current function. */
127 static unsigned fn_ctr_mask; /* Mask of counters used. */
128 static GTY(()) tree fn_v_ctrs[GCOV_COUNTERS]; /* counter variables. */
129 static unsigned fn_n_ctrs[GCOV_COUNTERS]; /* Counters allocated. */
130 static unsigned fn_b_ctrs[GCOV_COUNTERS]; /* Allocation base. */
132 /* Coverage info VAR_DECL and function info type nodes. */
133 static GTY(()) tree gcov_info_var;
134 static GTY(()) tree gcov_fn_info_type;
135 static GTY(()) tree gcov_fn_info_ptr_type;
137 /* Name of the notes (gcno) output file. The "bbg" prefix is for
138 historical reasons, when the notes file contained only the
139 basic block graph notes.
140 If this is NULL we're not writing to the notes file. */
141 static char *bbg_file_name;
143 /* File stamp for notes file. */
144 static unsigned bbg_file_stamp;
146 /* Name of the count data (gcda) file. */
147 static char *da_file_name;
148 static char *da_base_file_name;
149 static char *main_input_file_name;
151 /* The names of merge functions for counters. */
152 static const char *const ctr_merge_functions[GCOV_COUNTERS] = GCOV_MERGE_FUNCTIONS;
153 static const char *const ctr_names[GCOV_COUNTERS] = GCOV_COUNTER_NAMES;
155 /* True during the period that counts_hash is being rebuilt. */
156 static bool rebuilding_counts_hash = false;
158 struct gcov_module_info **module_infos = NULL;
160 /* List of -D/-U options. */
161 static struct str_list *cpp_defines_head = NULL, *cpp_defines_tail = NULL;
162 static unsigned num_cpp_defines = 0;
164 /* List of -imcaro/-include options. */
165 static struct str_list *cpp_includes_head = NULL, *cpp_includes_tail = NULL;
166 static unsigned num_cpp_includes = 0;
168 /* True if the current module has any asm statements. */
169 static bool has_asm_statement;
171 /* Forward declarations. */
172 static void read_counts_file (const char *, unsigned);
173 static tree build_var (tree, tree, int);
174 static void build_fn_info_type (tree, unsigned, tree);
175 static void build_info_type (tree, tree);
176 static tree build_fn_info (const struct coverage_data *, tree, tree);
177 static tree build_info (tree, tree);
178 static bool coverage_obj_init (void);
179 static vec<constructor_elt, va_gc> *coverage_obj_fn
180 (vec<constructor_elt, va_gc> *, tree, struct coverage_data const *);
181 static void coverage_obj_finish (vec<constructor_elt, va_gc> *);
182 static char * get_da_file_name (const char *);
183 static tree build_gcov_module_info_type (void);
185 /* Return the type node for gcov_type. */
187 tree
188 get_gcov_type (void)
190 enum machine_mode mode = smallest_mode_for_size (GCOV_TYPE_SIZE, MODE_INT);
191 return lang_hooks.types.type_for_mode (mode, false);
194 /* Return the type node for gcov_unsigned_t. */
196 tree
197 get_gcov_unsigned_t (void)
199 enum machine_mode mode = smallest_mode_for_size (32, MODE_INT);
200 return lang_hooks.types.type_for_mode (mode, true);
203 /* Return the type node for const char *. */
205 tree
206 get_const_string_type (void)
208 return build_pointer_type
209 (build_qualified_type (char_type_node, TYPE_QUAL_CONST));
212 inline hashval_t
213 counts_entry::hash (const value_type *entry)
215 return entry->ident * GCOV_COUNTERS + entry->ctr;
218 inline int
219 counts_entry::equal (const value_type *entry1,
220 const compare_type *entry2)
222 return entry1->ident == entry2->ident && entry1->ctr == entry2->ctr;
225 inline void
226 counts_entry::remove (value_type *entry)
228 /* When rebuilding counts_hash, we will reuse the entry. */
229 if (!rebuilding_counts_hash)
231 free (entry->counts);
232 free (entry);
236 /* Hash table of count data. */
237 static hash_table <counts_entry> counts_hash;
239 /* Returns true if MOD_ID is the id of the last source module. */
241 is_last_module (unsigned mod_id)
243 return (mod_id == module_infos[num_in_fnames - 1]->ident);
246 /* String hash function */
248 struct string_hasher
250 /* hash_table support. */
251 typedef char value_type;
252 typedef char compare_type;
253 static inline hashval_t hash (const value_type *);
254 static int equal (const value_type *, const compare_type *);
255 static void remove (value_type *) {};
258 hashval_t
259 string_hasher::hash (const char* s)
261 return htab_hash_string (s);
264 /* String equal function */
267 string_hasher::equal (const char *s1, const char *s2)
269 return !strcmp (s1, s2);
272 /* Command line option descriptor. */
274 struct opt_desc
276 const char *opt_str;
277 const char *opt_neg_str;
278 bool default_val; /* TODO better handling of default */
281 static struct opt_desc force_matching_cg_opts[] =
283 { "-fexceptions", "-fno-exceptions", true },
284 { "-fsized-delete", "-fno-sized-delete", false },
285 { "-frtti", "-fno-rtti", true },
286 { "-fstrict-aliasing", "-fno-strict-aliasing", true },
287 { "-fsigned-char", "-funsigned-char", true },
288 /* { "-fsigned-char", "-fno-signed-char", true },
289 { "-funsigned-char", "-fno-unsigned-char", false }, */
290 { "-ansi", "", false },
291 { NULL, NULL, false }
294 /* A helper function to check if OPTION_STRING is one of the codegen
295 options specified in FORCE_MATCHING_CG_ARGS. If yes, set the
296 corresponding entry in CG_ARG_VAL to the value of the option specified
297 in OPTION_STRING. */
299 static void
300 check_cg_opts (bool *cg_opt_val, const char *option_str)
302 unsigned int i;
303 for (i = 0; force_matching_cg_opts[i].opt_str; i++)
305 if (!strcmp (force_matching_cg_opts[i].opt_str, option_str))
306 cg_opt_val[i] = true;
307 else if (!strcmp (force_matching_cg_opts[i].opt_neg_str, option_str))
308 cg_opt_val[i] = false;
312 /* A helper function to check if CG_OPTS1 and CG_OPTS are identical. It returns
313 true if yes, false otherwise. */
315 static bool
316 has_incompatible_cg_opts (bool *cg_opts1, bool *cg_opts2, unsigned num_cg_opts)
318 unsigned i;
320 for (i = 0; i < num_cg_opts; i++)
322 if (cg_opts1[i] != cg_opts2[i])
323 return true;
326 return false;
329 /* Returns true if the command-line arguments stored in the given module-infos
330 are incompatible. */
331 static bool
332 incompatible_cl_args (struct gcov_module_info* mod_info1,
333 struct gcov_module_info* mod_info2)
335 char **warning_opts1 = XNEWVEC (char *, mod_info1->num_cl_args);
336 char **warning_opts2 = XNEWVEC (char *, mod_info2->num_cl_args);
337 char **non_warning_opts1 = XNEWVEC (char *, mod_info1->num_cl_args);
338 char **non_warning_opts2 = XNEWVEC (char *, mod_info2->num_cl_args);
339 char *std_opts1 = NULL, *std_opts2 = NULL;
340 unsigned arch_isa1 = 0, arch_isa2 = 0;
341 unsigned int i, num_warning_opts1 = 0, num_warning_opts2 = 0;
342 unsigned int num_non_warning_opts1 = 0, num_non_warning_opts2 = 0;
343 bool warning_mismatch = false;
344 bool non_warning_mismatch = false;
345 hash_table <string_hasher> option_tab1, option_tab2;
346 unsigned int start_index1 = mod_info1->num_quote_paths
347 + mod_info1->num_bracket_paths + mod_info1->num_system_paths
348 + mod_info1->num_cpp_defines + mod_info1->num_cpp_includes;
349 unsigned int start_index2 = mod_info2->num_quote_paths
350 + mod_info2->num_bracket_paths + mod_info2->num_system_paths
351 + mod_info2->num_cpp_defines + mod_info2->num_cpp_includes;
353 bool *cg_opts1, *cg_opts2, has_any_incompatible_cg_opts, has_incompatible_std;
354 bool has_incompatible_arch_isa;
355 unsigned int num_cg_opts = 0;
357 for (i = 0; force_matching_cg_opts[i].opt_str; i++)
358 num_cg_opts++;
360 cg_opts1 = XCNEWVEC (bool, num_cg_opts);
361 cg_opts2 = XCNEWVEC (bool, num_cg_opts);
363 /* Initialize the array to default value */
364 for (i = 0; force_matching_cg_opts[i].opt_str; i++)
366 cg_opts1[i] = force_matching_cg_opts[i].default_val;
367 cg_opts2[i] = force_matching_cg_opts[i].default_val;
370 option_tab1.create (10);
371 option_tab2.create (10);
373 /* First, separate the warning and non-warning options. */
374 for (i = 0; i < mod_info1->num_cl_args; i++)
375 if (mod_info1->string_array[start_index1 + i][1] == 'W')
376 warning_opts1[num_warning_opts1++] =
377 mod_info1->string_array[start_index1 + i];
378 else
380 char **slot;
381 char *option_string = mod_info1->string_array[start_index1 + i];
383 check_cg_opts (cg_opts1, option_string);
384 if (strstr (option_string, "-std="))
385 std_opts1 = option_string;
387 if (!strncmp (option_string, "-m",2))
388 arch_isa1 = crc32_string (arch_isa1, option_string);
390 slot = option_tab1.find_slot (option_string, INSERT);
391 if (!*slot)
393 *slot = option_string;
394 non_warning_opts1[num_non_warning_opts1++] = option_string;
398 for (i = 0; i < mod_info2->num_cl_args; i++)
399 if (mod_info2->string_array[start_index2 + i][1] == 'W')
400 warning_opts2[num_warning_opts2++] =
401 mod_info2->string_array[start_index2 + i];
402 else
404 char **slot;
405 char *option_string = mod_info2->string_array[start_index2 + i];
407 check_cg_opts (cg_opts2, option_string);
408 if (strstr (option_string, "-std="))
409 std_opts2 = option_string;
411 if (!strncmp (option_string, "-m",2))
412 arch_isa2 = crc32_string (arch_isa2, option_string);
414 slot = option_tab2.find_slot (option_string, INSERT);
415 if (!*slot)
417 *slot = option_string;
418 non_warning_opts2[num_non_warning_opts2++] = option_string;
422 has_incompatible_std =
423 std_opts1 != std_opts2 && (std_opts1 == NULL || std_opts2 == NULL
424 || strcmp (std_opts1, std_opts2));
426 has_incompatible_arch_isa = (arch_isa1 != arch_isa2);
427 /* Compare warning options. If these mismatch, we emit a warning. */
428 if (num_warning_opts1 != num_warning_opts2)
429 warning_mismatch = true;
430 else
431 for (i = 0; i < num_warning_opts1 && !warning_mismatch; i++)
432 warning_mismatch = strcmp (warning_opts1[i], warning_opts2[i]) != 0;
434 /* Compare non-warning options. If these mismatch, we emit a warning, and if
435 -fripa-disallow-opt-mismatch is supplied, the two modules are also
436 incompatible. */
437 if (num_non_warning_opts1 != num_non_warning_opts2)
438 non_warning_mismatch = true;
439 else
440 for (i = 0; i < num_non_warning_opts1 && !non_warning_mismatch; i++)
441 non_warning_mismatch =
442 strcmp (non_warning_opts1[i], non_warning_opts2[i]) != 0;
444 if (warn_ripa_opt_mismatch && (warning_mismatch || non_warning_mismatch))
445 warning (OPT_Wripa_opt_mismatch, "command line arguments mismatch for %s "
446 "and %s", mod_info1->source_filename, mod_info2->source_filename);
448 if (warn_ripa_opt_mismatch && non_warning_mismatch && dump_enabled_p ())
450 dump_printf_loc (MSG_MISSED_OPTIMIZATION, UNKNOWN_LOCATION,
451 "Options for %s", mod_info1->source_filename);
452 for (i = 0; i < num_non_warning_opts1; i++)
453 dump_printf_loc (MSG_MISSED_OPTIMIZATION, UNKNOWN_LOCATION,
454 non_warning_opts1[i]);
455 dump_printf_loc (MSG_MISSED_OPTIMIZATION, UNKNOWN_LOCATION,
456 "Options for %s", mod_info2->source_filename);
457 for (i = 0; i < num_non_warning_opts2; i++)
458 dump_printf_loc (MSG_MISSED_OPTIMIZATION, UNKNOWN_LOCATION,
459 non_warning_opts2[i]);
462 has_any_incompatible_cg_opts
463 = has_incompatible_cg_opts (cg_opts1, cg_opts2, num_cg_opts);
465 XDELETEVEC (warning_opts1);
466 XDELETEVEC (warning_opts2);
467 XDELETEVEC (non_warning_opts1);
468 XDELETEVEC (non_warning_opts2);
469 XDELETEVEC (cg_opts1);
470 XDELETEVEC (cg_opts2);
471 option_tab1.dispose ();
472 option_tab2.dispose ();
473 return ((flag_ripa_disallow_opt_mismatch && non_warning_mismatch)
474 || has_any_incompatible_cg_opts || has_incompatible_std
475 || has_incompatible_arch_isa);
479 /* Support for module sorting based on user specfication. */
480 struct module_name_entry
482 typedef module_name_entry value_type;
483 typedef module_name_entry compare_type;
484 static inline hashval_t hash (const value_type *entry);
485 static inline int equal (const value_type *entry1, const compare_type *entry2);
486 static inline void remove (value_type *v);
488 const char *source_name;
489 int order;
492 /* Hash function for module name */
494 hashval_t
495 module_name_entry::hash (const value_type *s)
497 return htab_hash_string (s->source_name);
500 /* Delete function for module name */
502 void
503 module_name_entry::remove (value_type *entry)
505 /* XDELETE (entry->source_name); */
506 XDELETE (entry);
509 /* Equal function for module name */
512 module_name_entry::equal (const value_type *s1, const compare_type *s2)
514 return !strcmp (s1->source_name, s2->source_name);
517 static hash_table<module_name_entry> module_name_tab;
519 /* Comparison function for sorting module_infos array. */
521 static int
522 cmp_module_name_entry (const void *p1, const void *p2)
524 module_name_entry **slot1, **slot2;
525 module_name_entry *m_e1, *m_e2;
527 struct gcov_module_info *const *e1 = (struct gcov_module_info *const *) p1;
528 struct gcov_module_info *const *e2 = (struct gcov_module_info *const *) p2;
530 module_name_entry e;
531 e.source_name = (*e1)->source_filename;
532 slot1 = module_name_tab.find_slot (&e, NO_INSERT);
533 e.source_name = (*e2)->source_filename;
534 slot2 = module_name_tab.find_slot (&e, NO_INSERT);
536 if (!slot1 || !*slot1)
537 return 1;
539 if (!slot2 || !*slot2)
540 return -1;
542 gcc_assert (slot1 && *slot1 && slot2 && *slot2);
543 m_e1 = *slot1;
544 m_e2 = *slot2;
546 return m_e1->order - m_e2->order;
549 /* Comparison function for sorting fname array */
551 static int
552 cmp_fname_entry (const void *p1, const void *p2)
554 module_name_entry **slot1, **slot2;
555 module_name_entry *m_e1, *m_e2;
557 const char *const *e1 = (const char *const *) p1;
558 const char *const *e2 = (const char *const *) p2;
560 module_name_entry e;
562 e.source_name = *e1;
563 slot1 = module_name_tab.find_slot (&e, NO_INSERT);
564 e.source_name = *e2;
565 slot2 = module_name_tab.find_slot (&e, NO_INSERT);
567 if (!slot1 || !*slot1)
568 return 1;
570 if (!slot2 || !*slot2)
571 return -1;
573 gcc_assert (slot1 && *slot1 && slot2 && *slot2);
574 m_e1 = *slot1;
575 m_e2 = *slot2;
577 return m_e1->order - m_e2->order;
580 /* Reorder module group according to file IMPORTS_FILE */
582 static void
583 reorder_module_groups (const char *imports_file, unsigned max_group)
585 FILE *f;
586 int n, order = 0;
587 size_t len;
588 char *line = NULL;
590 module_name_tab.create (20);
592 f = fopen (imports_file, "r");
593 if (!f)
594 error ("Can't open file %s", imports_file);
596 while ((n = getline (&line, &len, f)) != -1)
598 module_name_entry **slot;
599 module_name_entry *m_e = XCNEW (module_name_entry);
601 line[n - 1] = '\0';
602 m_e->source_name = line;
603 m_e->order = order;
605 slot = module_name_tab.find_slot (m_e, INSERT);
606 gcc_assert (!*slot);
607 *slot = m_e;
609 line = NULL;
610 order++;
613 /* Now do the sorting */
615 qsort (&module_infos[1], num_in_fnames - 1, sizeof (void *),
616 cmp_module_name_entry);
617 qsort (&in_fnames[1], num_in_fnames - 1, sizeof (void *),
618 cmp_fname_entry);
621 unsigned i;
623 for (i = 0; i < num_in_fnames; i++)
624 fprintf (stderr, "*** %s (%s)\n", in_fnames[i],
625 i < max_group ? "Kept":"Skipped");
627 for (i = 0; i < num_in_fnames; i++)
628 fprintf (stderr, "### %s (%s)\n", module_infos[i]->source_filename,
629 i < max_group ? "Kept":"Skipped");
633 if (num_in_fnames > max_group)
634 num_in_fnames = max_group;
636 module_name_tab.dispose ();
639 typedef struct {
640 unsigned int mod_id;
641 const char *mod_name;
642 } mod_id_to_name_t;
644 static vec<mod_id_to_name_t> *mod_names;
646 static void
647 record_module_name (unsigned int mod_id, const char *name)
649 mod_id_to_name_t t;
651 t.mod_id = mod_id;
652 t.mod_name = xstrdup (name);
653 if (!mod_names)
654 vec_alloc (mod_names, 10);
655 mod_names->safe_push (t);
658 /* Return the module name for module with MOD_ID. */
660 const char *
661 get_module_name (unsigned int mod_id)
663 size_t i;
664 mod_id_to_name_t *elt;
666 for (i = 0; mod_names->iterate (i, &elt); i++)
668 if (elt->mod_id == mod_id)
669 return elt->mod_name;
672 gcc_assert (0);
673 return NULL;
676 /* Read in the counts file, if available. DA_FILE_NAME is the
677 name of the gcda file, and MODULE_ID is the module id of the
678 associated source module. */
680 static void
681 read_counts_file (const char *da_file_name, unsigned module_id)
683 gcov_unsigned_t fn_ident = 0;
684 struct gcov_summary summary;
685 unsigned new_summary = 1;
686 gcov_unsigned_t tag;
687 int is_error = 0;
688 unsigned module_infos_read = 0;
689 struct pointer_set_t *modset = 0;
690 unsigned max_group = PARAM_VALUE (PARAM_MAX_LIPO_GROUP);
691 unsigned lineno_checksum = 0;
692 unsigned cfg_checksum = 0;
693 const char *imports_filename;
695 if (max_group == 0)
696 max_group = (unsigned) -1;
698 if (!gcov_open (da_file_name, 1))
700 if (PARAM_VALUE (PARAM_GCOV_DEBUG))
702 /* Try to find .gcda file in the current working dir. */
703 da_file_name = lbasename (da_file_name);
704 if (!gcov_open (da_file_name, 1))
705 return;
707 else
708 return;
711 if (!gcov_magic (gcov_read_unsigned (), GCOV_DATA_MAGIC))
713 warning (0, "%qs is not a gcov data file", da_file_name);
714 gcov_close ();
715 return;
717 else if ((tag = gcov_read_unsigned ()) != GCOV_VERSION)
719 char v[4], e[4];
721 GCOV_UNSIGNED2STRING (v, tag);
722 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
724 warning (0, "%qs is version %q.*s, expected version %q.*s",
725 da_file_name, 4, v, 4, e);
726 gcov_close ();
727 return;
730 /* Read the stamp, used for creating a generation count. */
731 tag = gcov_read_unsigned ();
732 bbg_file_stamp = crc32_unsigned (bbg_file_stamp, tag);
734 if (!counts_hash.is_created ())
735 counts_hash.create (10);
737 while ((tag = gcov_read_unsigned ()))
739 gcov_unsigned_t length;
740 gcov_position_t offset;
742 length = gcov_read_unsigned ();
743 offset = gcov_position ();
744 if (tag == GCOV_TAG_FUNCTION)
746 if (length)
748 fn_ident = gcov_read_unsigned ();
749 lineno_checksum = gcov_read_unsigned ();
750 cfg_checksum = gcov_read_unsigned ();
752 else
753 fn_ident = lineno_checksum = cfg_checksum = 0;
754 new_summary = 1;
756 else if (tag == GCOV_TAG_PROGRAM_SUMMARY)
758 struct gcov_summary sum;
759 unsigned ix;
761 if (new_summary)
762 memset (&summary, 0, sizeof (summary));
764 gcov_read_summary (&sum);
765 for (ix = 0; ix != GCOV_COUNTERS_SUMMABLE; ix++)
767 summary.ctrs[ix].runs += sum.ctrs[ix].runs;
768 summary.ctrs[ix].sum_all += sum.ctrs[ix].sum_all;
769 if (summary.ctrs[ix].run_max < sum.ctrs[ix].run_max)
770 summary.ctrs[ix].run_max = sum.ctrs[ix].run_max;
771 summary.ctrs[ix].sum_max += sum.ctrs[ix].sum_max;
773 if (new_summary)
774 memcpy (summary.ctrs[GCOV_COUNTER_ARCS].histogram,
775 sum.ctrs[GCOV_COUNTER_ARCS].histogram,
776 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
777 else
778 gcov_histogram_merge (summary.ctrs[GCOV_COUNTER_ARCS].histogram,
779 sum.ctrs[GCOV_COUNTER_ARCS].histogram);
780 new_summary = 0;
782 else if (GCOV_TAG_IS_COUNTER (tag) && fn_ident)
784 counts_entry_t **slot, *entry, elt;
785 unsigned n_counts = GCOV_TAG_COUNTER_NUM (length);
786 unsigned ix;
788 elt.ident = GEN_FUNC_GLOBAL_ID (module_id, fn_ident);
789 elt.ctr = GCOV_COUNTER_FOR_TAG (tag);
791 slot = counts_hash.find_slot (&elt, INSERT);
792 entry = *slot;
793 if (!entry)
795 *slot = entry = XCNEW (counts_entry_t);
796 entry->ident = elt.ident;
797 entry->ctr = elt.ctr;
798 entry->lineno_checksum = lineno_checksum;
799 entry->cfg_checksum = cfg_checksum;
800 if (elt.ctr < GCOV_COUNTERS_SUMMABLE)
801 entry->summary = summary.ctrs[elt.ctr];
802 entry->summary.num = n_counts;
803 entry->counts = XCNEWVEC (gcov_type, n_counts);
805 else if (entry->lineno_checksum != lineno_checksum
806 || entry->cfg_checksum != cfg_checksum)
808 error ("Profile data for function %u is corrupted", fn_ident);
809 error ("checksum is (%x,%x) instead of (%x,%x)",
810 entry->lineno_checksum, entry->cfg_checksum,
811 lineno_checksum, cfg_checksum);
812 counts_hash.dispose ();
813 break;
815 else if (entry->summary.num != n_counts)
817 error ("Profile data for function %u is corrupted", fn_ident);
818 error ("number of counters is %d instead of %d", entry->summary.num, n_counts);
819 counts_hash.dispose ();
820 break;
822 else if (elt.ctr >= GCOV_COUNTERS_SUMMABLE)
824 error ("cannot merge separate %s counters for function %u",
825 ctr_names[elt.ctr], fn_ident);
826 goto skip_merge;
828 else
830 entry->summary.runs += summary.ctrs[elt.ctr].runs;
831 entry->summary.sum_all += summary.ctrs[elt.ctr].sum_all;
832 if (entry->summary.run_max < summary.ctrs[elt.ctr].run_max)
833 entry->summary.run_max = summary.ctrs[elt.ctr].run_max;
834 entry->summary.sum_max += summary.ctrs[elt.ctr].sum_max;
836 for (ix = 0; ix != n_counts; ix++)
837 entry->counts[ix] += gcov_read_counter ();
838 skip_merge:;
840 /* Skip the MODULE_INFO records if not in dyn-ipa mode, or when reading
841 auxiliary modules. */
842 else if (tag == GCOV_TAG_MODULE_INFO && flag_dyn_ipa && !module_id)
844 struct gcov_module_info* mod_info;
845 size_t info_sz;
846 /* each string has at least 8 bytes, so MOD_INFO's
847 persistent length >= in core size. */
848 mod_info
849 = (struct gcov_module_info *) alloca ((length + 2)
850 * sizeof (gcov_unsigned_t));
851 gcov_read_module_info (mod_info, length);
852 info_sz = (sizeof (struct gcov_module_info) +
853 sizeof (void *) * (mod_info->num_quote_paths +
854 mod_info->num_bracket_paths +
855 mod_info->num_system_paths +
856 mod_info->num_cpp_defines +
857 mod_info->num_cpp_includes +
858 mod_info->num_cl_args));
859 /* The first MODULE_INFO record must be for the primary module. */
860 if (module_infos_read == 0)
862 gcc_assert (mod_info->is_primary && !modset);
863 module_infos_read++;
864 modset = pointer_set_create ();
865 pointer_set_insert (modset, (void *)(size_t)mod_info->ident);
866 primary_module_id = mod_info->ident;
867 include_all_aux = MODULE_INCLUDE_ALL_AUX_FLAG (mod_info);
868 module_infos = XCNEWVEC (struct gcov_module_info *, 1);
869 module_infos[0] = XCNEWVAR (struct gcov_module_info, info_sz);
870 memcpy (module_infos[0], mod_info, info_sz);
872 else
874 int fd;
875 char *aux_da_filename = get_da_file_name (mod_info->da_filename);
876 gcc_assert (!mod_info->is_primary);
877 if (pointer_set_insert (modset, (void *)(size_t)mod_info->ident))
879 if (dump_enabled_p ())
880 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
881 "Not importing %s: already imported",
882 mod_info->source_filename);
884 else if ((module_infos[0]->lang & GCOV_MODULE_LANG_MASK) !=
885 (mod_info->lang & GCOV_MODULE_LANG_MASK))
887 if (dump_enabled_p ())
888 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
889 "Not importing %s: source language"
890 " different from primary module's source"
891 " language",
892 mod_info->source_filename);
894 else if (module_infos_read == max_group
895 /* If reordering is specified, delay the cutoff
896 until after sorting. */
897 && !getenv ("LIPO_REORDER_GROUP"))
899 if (dump_enabled_p ())
900 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
901 "Not importing %s: maximum group size"
902 " reached", mod_info->source_filename);
904 else if (incompatible_cl_args (module_infos[0], mod_info))
906 if (dump_enabled_p ())
907 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
908 "Not importing %s: command-line"
909 " arguments not compatible with primary"
910 " module",
911 mod_info->source_filename);
913 else if ((fd = open (aux_da_filename, O_RDONLY)) < 0)
915 if (dump_enabled_p ())
916 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
917 "Not importing %s: couldn't open %s",
918 mod_info->source_filename,
919 aux_da_filename);
921 else if ((mod_info->lang & GCOV_MODULE_ASM_STMTS)
922 && flag_ripa_disallow_asm_modules)
924 if (dump_enabled_p ())
925 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
926 "Not importing %s: contains assembler"
927 " statements", mod_info->source_filename);
929 else if (mod_info->is_primary == false
930 && MODULE_EXPORTED_FLAG (mod_info) == false)
932 warning (0, "MODULE_ID=%d (%s) is an auxiliary module, "
933 "but export_bit is not set. \n",
934 mod_info->ident, mod_info->source_filename);
936 else
938 close (fd);
939 module_infos_read++;
940 add_input_filename (mod_info->source_filename);
941 module_infos = XRESIZEVEC (struct gcov_module_info *,
942 module_infos, num_in_fnames);
943 gcc_assert (num_in_fnames == module_infos_read);
944 module_infos[module_infos_read - 1]
945 = XCNEWVAR (struct gcov_module_info, info_sz);
946 memcpy (module_infos[module_infos_read - 1], mod_info,
947 info_sz);
951 record_module_name (mod_info->ident,
952 lbasename (mod_info->source_filename));
954 if (dump_enabled_p ())
956 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
957 "MODULE Id=%d, Is_Primary=%s,"
958 " Is_Exported=%s, Include_all=%s, Name=%s (%s)",
959 mod_info->ident, mod_info->is_primary?"yes":"no",
960 MODULE_EXPORTED_FLAG (mod_info)?"yes":"no",
961 MODULE_INCLUDE_ALL_AUX_FLAG (mod_info)?"yes"
962 :"no",
963 mod_info->source_filename,
964 mod_info->da_filename);
967 gcov_sync (offset, length);
968 if ((is_error = gcov_is_error ()))
970 error (is_error < 0 ? "%qs has overflowed" : "%qs is corrupted",
971 da_file_name);
972 counts_hash.dispose ();
973 break;
977 if ((imports_filename = getenv ("LIPO_REORDER_GROUP"))
978 && flag_dyn_ipa && !module_id)
980 reorder_module_groups (imports_filename, max_group);
981 if (module_infos_read != num_in_fnames)
982 module_infos_read = num_in_fnames;
985 /* TODO: profile based multiple module compilation does not work
986 together with command line (-combine) based ipo -- add a nice
987 warning and bail out instead of asserting. */
989 if (modset)
990 pointer_set_destroy (modset);
991 gcc_assert (module_infos_read == 0
992 || module_infos_read == num_in_fnames);
994 if (flag_dyn_ipa)
995 gcc_assert (primary_module_id && num_in_fnames >= 1);
997 gcov_close ();
1000 /* Returns the coverage data entry for counter type COUNTER of function
1001 FUNC. EXPECTED is the number of expected counter entries. */
1003 static counts_entry_t *
1004 get_coverage_counts_entry (struct function *func, unsigned counter)
1006 counts_entry_t *entry, elt;
1008 elt.ident = FUNC_DECL_GLOBAL_ID (func);
1009 elt.ctr = counter;
1010 entry = counts_hash.find (&elt);
1012 return entry;
1015 /* Returns the counters for a particular tag. */
1017 gcov_type *
1018 get_coverage_counts (unsigned counter, unsigned expected,
1019 unsigned cfg_checksum, unsigned lineno_checksum,
1020 const struct gcov_ctr_summary **summary)
1022 counts_entry_t *entry;
1024 /* No hash table, no counts. */
1025 if (!counts_hash.is_created ())
1027 static int warned = 0;
1029 if (!warned++ && dump_enabled_p ())
1030 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
1031 (flag_guess_branch_prob
1032 ? "file %s not found, execution counts estimated\n"
1033 : "file %s not found, execution counts assumed to "
1034 "be zero\n"),
1035 da_file_name);
1036 return NULL;
1039 entry = get_coverage_counts_entry (cfun, counter);
1041 if (!entry || !entry->summary.num)
1043 if (!flag_dyn_ipa && dump_enabled_p ())
1044 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
1045 "no coverage for function %s found",
1046 IDENTIFIER_POINTER
1047 (DECL_ASSEMBLER_NAME (current_function_decl)));
1048 return NULL;
1051 if (entry->cfg_checksum != cfg_checksum
1052 || entry->summary.num != expected)
1054 static int warned = 0;
1055 bool warning_printed = false;
1056 tree id = DECL_ASSEMBLER_NAME (current_function_decl);
1058 warning_printed =
1059 warning_at (input_location, OPT_Wcoverage_mismatch,
1060 "the control flow of function %qE does not match "
1061 "its profile data (counter %qs)", id, ctr_names[counter]);
1062 if (warning_printed && dump_enabled_p ())
1064 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
1065 "use -Wno-error=coverage-mismatch to tolerate "
1066 "the mismatch but performance may drop if the "
1067 "function is hot\n");
1069 if (!seen_error ()
1070 && !warned++)
1072 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
1073 "coverage mismatch ignored\n");
1074 dump_printf (MSG_OPTIMIZED_LOCATIONS,
1075 flag_guess_branch_prob
1076 ? G_("execution counts estimated\n")
1077 : G_("execution counts assumed to be zero\n"));
1078 if (!flag_guess_branch_prob)
1079 dump_printf (MSG_OPTIMIZED_LOCATIONS,
1080 "this can result in poorly optimized code\n");
1084 return NULL;
1086 else if (entry->lineno_checksum != lineno_checksum)
1088 warning (OPT_Wripa_opt_mismatch,
1089 "Source location for function %qE have changed,"
1090 " the profile data may be out of date",
1091 DECL_ASSEMBLER_NAME (current_function_decl));
1094 if (summary)
1095 *summary = &entry->summary;
1097 return entry->counts;
1100 /* Returns the coverage data entry for counter type COUNTER of function
1101 FUNC. On return, *N_COUNTS is set to the number of entries in the counter. */
1103 gcov_type *
1104 get_coverage_counts_no_warn (struct function *f, unsigned counter, unsigned *n_counts)
1106 counts_entry_t *entry, elt;
1108 /* No hash table, no counts. */
1109 if (!counts_hash.is_created () || !f)
1110 return NULL;
1112 elt.ident = FUNC_DECL_GLOBAL_ID (f);
1113 elt.ctr = counter;
1114 entry = counts_hash.find (&elt);
1115 if (!entry)
1116 return NULL;
1118 *n_counts = entry->summary.num;
1119 return entry->counts;
1122 /* Allocate NUM counters of type COUNTER. Returns nonzero if the
1123 allocation succeeded. */
1126 coverage_counter_alloc (unsigned counter, unsigned num)
1128 if (no_coverage)
1129 return 0;
1131 if (!num)
1132 return 1;
1134 if (!fn_v_ctrs[counter])
1136 tree array_type = build_array_type (get_gcov_type (), NULL_TREE);
1138 fn_v_ctrs[counter]
1139 = build_var (current_function_decl, array_type, counter);
1142 fn_b_ctrs[counter] = fn_n_ctrs[counter];
1143 fn_n_ctrs[counter] += num;
1145 fn_ctr_mask |= 1 << counter;
1146 return 1;
1149 /* Generate a tree to access COUNTER NO. */
1151 tree
1152 tree_coverage_counter_ref (unsigned counter, unsigned no)
1154 tree gcov_type_node = get_gcov_type ();
1156 gcc_assert (no < fn_n_ctrs[counter] - fn_b_ctrs[counter]);
1158 no += fn_b_ctrs[counter];
1160 /* "no" here is an array index, scaled to bytes later. */
1161 return build4 (ARRAY_REF, gcov_type_node, fn_v_ctrs[counter],
1162 build_int_cst (integer_type_node, no), NULL, NULL);
1165 /* Generate a tree to access the address of COUNTER NO. */
1167 tree
1168 tree_coverage_counter_addr (unsigned counter, unsigned no)
1170 tree gcov_type_node = get_gcov_type ();
1172 gcc_assert (no < fn_n_ctrs[counter] - fn_b_ctrs[counter]);
1173 no += fn_b_ctrs[counter];
1175 /* "no" here is an array index, scaled to bytes later. */
1176 return build_fold_addr_expr (build4 (ARRAY_REF, gcov_type_node,
1177 fn_v_ctrs[counter],
1178 build_int_cst (integer_type_node, no),
1179 NULL, NULL));
1183 /* Generate a crc32 of a string with specified STR_LEN when it's not 0.
1184 Non-zero STR_LEN should only be seen in LIPO mode. */
1186 static unsigned
1187 crc32_string_1 (unsigned chksum, const char *string, unsigned str_len)
1189 char *dup;
1191 if (!L_IPO_COMP_MODE || str_len == 0)
1192 return crc32_string (chksum, string);
1194 gcc_assert (str_len > 0 && str_len < strlen (string));
1195 dup = xstrdup (string);
1196 dup[str_len] = 0;
1197 chksum = crc32_string (chksum, dup);
1198 free (dup);
1200 return chksum;
1203 /* Generate a checksum for a string. CHKSUM is the current
1204 checksum. */
1206 static unsigned
1207 coverage_checksum_string (unsigned chksum, const char *string)
1209 int i;
1210 char *dup = NULL;
1211 unsigned lipo_orig_str_len = 0;
1213 /* Strip out the ending ".cmo.[0-9]*" string from function
1214 name. Otherwise we will have lineno checksum mismatch. */
1215 if (L_IPO_COMP_MODE)
1217 int len;
1219 i = len = strlen (string);
1220 while (i--)
1221 if ((string[i] < '0' || string[i] > '9'))
1222 break;
1223 if ((i > 5) && (i != len - 1))
1225 if (!strncmp (string + i - 4, ".cmo.", 5))
1226 lipo_orig_str_len = i - 4;
1231 /* Look for everything that looks if it were produced by
1232 get_file_function_name and zero out the second part
1233 that may result from flag_random_seed. This is not critical
1234 as the checksums are used only for sanity checking. */
1235 for (i = 0; string[i]; i++)
1237 int offset = 0;
1238 if (!strncmp (string + i, "_GLOBAL__N_", 11))
1239 offset = 11;
1240 if (!strncmp (string + i, "_GLOBAL__", 9))
1241 offset = 9;
1243 /* C++ namespaces do have scheme:
1244 _GLOBAL__N_<filename>_<wrongmagicnumber>_<magicnumber>functionname
1245 since filename might contain extra underscores there seems
1246 to be no better chance then walk all possible offsets looking
1247 for magicnumber. */
1248 if (offset)
1250 for (i = i + offset; string[i]; i++)
1251 if (string[i]=='_')
1253 int y;
1255 for (y = 1; y < 9; y++)
1256 if (!(string[i + y] >= '0' && string[i + y] <= '9')
1257 && !(string[i + y] >= 'A' && string[i + y] <= 'F'))
1258 break;
1259 if (y != 9 || string[i + 9] != '_')
1260 continue;
1261 for (y = 10; y < 18; y++)
1262 if (!(string[i + y] >= '0' && string[i + y] <= '9')
1263 && !(string[i + y] >= 'A' && string[i + y] <= 'F'))
1264 break;
1265 if (y != 18)
1266 continue;
1267 if (!dup)
1268 string = dup = xstrdup (string);
1269 for (y = 10; y < 18; y++)
1270 dup[i + y] = '0';
1272 break;
1276 chksum = crc32_string_1 (chksum, string, lipo_orig_str_len);
1277 if (dup)
1278 free (dup);
1280 return chksum;
1283 /* Compute checksum for the current function. We generate a CRC32. */
1285 unsigned
1286 coverage_compute_lineno_checksum (void)
1288 tree name;
1289 expanded_location xloc
1290 = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
1291 unsigned chksum = xloc.line;
1292 const char *pathless_filename = xloc.file;
1293 int i;
1294 for (i = strlen (xloc.file); i >= 0; i--)
1295 if (IS_DIR_SEPARATOR (pathless_filename[i]))
1297 pathless_filename += i + 1;
1298 break;
1301 chksum = coverage_checksum_string (chksum, pathless_filename);
1303 /* Note: it is a bad design that C++ FE associate the convertion function type
1304 with the name of the decl. This leads to cross contamination between different
1305 conversion operators in different modules (If conv_type_names map is cleared
1306 at the end of parsing of each module). */
1307 if (flag_dyn_ipa && lang_hooks.user_conv_function_p (current_function_decl))
1308 name = DECL_ASSEMBLER_NAME (current_function_decl);
1309 else
1310 name = DECL_NAME (current_function_decl);
1312 chksum = coverage_checksum_string
1313 (chksum, IDENTIFIER_POINTER (name));
1315 return chksum;
1318 /* Compute profile ID. This is better to be unique in whole program. */
1320 unsigned
1321 coverage_compute_profile_id (struct cgraph_node *n)
1323 expanded_location xloc
1324 = expand_location (DECL_SOURCE_LOCATION (n->decl));
1325 unsigned chksum = xloc.line;
1327 chksum = coverage_checksum_string (chksum, xloc.file);
1328 chksum = coverage_checksum_string
1329 (chksum, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n->decl)));
1330 if (first_global_object_name)
1331 chksum = coverage_checksum_string
1332 (chksum, first_global_object_name);
1333 chksum = coverage_checksum_string
1334 (chksum, aux_base_name);
1336 /* Non-negative integers are hopefully small enough to fit in all targets. */
1337 return chksum & 0x7fffffff;
1340 /* Compute cfg checksum for the current function.
1341 The checksum is calculated carefully so that
1342 source code changes that doesn't affect the control flow graph
1343 won't change the checksum.
1344 This is to make the profile data useable across source code change.
1345 The downside of this is that the compiler may use potentially
1346 wrong profile data - that the source code change has non-trivial impact
1347 on the validity of profile data (e.g. the reversed condition)
1348 but the compiler won't detect the change and use the wrong profile data. */
1350 unsigned
1351 coverage_compute_cfg_checksum (void)
1353 basic_block bb;
1354 unsigned chksum = n_basic_blocks_for_fn (cfun);
1356 FOR_EACH_BB_FN (bb, cfun)
1358 edge e;
1359 edge_iterator ei;
1360 chksum = crc32_byte (chksum, bb->index);
1361 FOR_EACH_EDGE (e, ei, bb->succs)
1363 chksum = crc32_byte (chksum, e->dest->index);
1367 return chksum;
1370 /* Begin output to the notes file for the current function.
1371 Writes the function header. Returns nonzero if data should be output. */
1374 coverage_begin_function (unsigned lineno_checksum, unsigned cfg_checksum)
1376 expanded_location xloc;
1377 unsigned long offset;
1379 /* We don't need to output .gcno file unless we're under -ftest-coverage
1380 (e.g. -fprofile-arcs/generate/use don't need .gcno to work). */
1381 if (no_coverage || !bbg_file_name)
1382 return 0;
1384 xloc = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
1386 /* Announce function */
1387 offset = gcov_write_tag (GCOV_TAG_FUNCTION);
1388 gcov_write_unsigned (FUNC_DECL_FUNC_ID (cfun));
1389 gcov_write_unsigned (lineno_checksum);
1390 gcov_write_unsigned (cfg_checksum);
1391 gcov_write_string (IDENTIFIER_POINTER
1392 (DECL_ASSEMBLER_NAME (current_function_decl)));
1393 gcov_write_string (xloc.file);
1394 gcov_write_unsigned (xloc.line);
1395 gcov_write_length (offset);
1397 return !gcov_is_error ();
1400 /* Finish coverage data for the current function. Verify no output
1401 error has occurred. Save function coverage counts. */
1403 void
1404 coverage_end_function (unsigned lineno_checksum, unsigned cfg_checksum)
1406 unsigned i;
1408 if (bbg_file_name && gcov_is_error ())
1410 warning (0, "error writing %qs", bbg_file_name);
1411 unlink (bbg_file_name);
1412 bbg_file_name = NULL;
1415 if (fn_ctr_mask)
1417 struct coverage_data *item = 0;
1419 /* If the function is extern (i.e. extern inline), then we won't
1420 be outputting it, so don't chain it onto the function
1421 list. */
1422 if (!DECL_EXTERNAL (current_function_decl))
1424 item = ggc_alloc_coverage_data ();
1426 item->ident = FUNC_DECL_FUNC_ID (cfun);
1427 item->lineno_checksum = lineno_checksum;
1428 item->cfg_checksum = cfg_checksum;
1430 item->fn_decl = current_function_decl;
1431 item->next = 0;
1432 *functions_tail = item;
1433 functions_tail = &item->next;
1436 for (i = 0; i != GCOV_COUNTERS; i++)
1438 tree var = fn_v_ctrs[i];
1440 if (item)
1441 item->ctr_vars[i] = var;
1442 if (var)
1444 tree array_type = build_index_type (size_int (fn_n_ctrs[i] - 1));
1445 array_type = build_array_type (get_gcov_type (), array_type);
1446 TREE_TYPE (var) = array_type;
1447 DECL_SIZE (var) = TYPE_SIZE (array_type);
1448 DECL_SIZE_UNIT (var) = TYPE_SIZE_UNIT (array_type);
1449 varpool_finalize_decl (var);
1452 fn_b_ctrs[i] = fn_n_ctrs[i] = 0;
1453 fn_v_ctrs[i] = NULL_TREE;
1455 prg_ctr_mask |= fn_ctr_mask;
1456 fn_ctr_mask = 0;
1460 /* True if a function entry corresponding to the given FN_IDENT
1461 is present in the coverage internal data structures. */
1463 bool
1464 coverage_function_present (unsigned fn_ident)
1466 struct coverage_data *item = functions_head;
1467 while (item && item->ident != fn_ident)
1468 item = item->next;
1469 return item != NULL;
1472 /* Update function and program direct-call coverage counts. */
1474 void
1475 coverage_dc_end_function (void)
1477 tree var;
1479 if (fn_ctr_mask)
1481 const unsigned idx = GCOV_COUNTER_DIRECT_CALL;
1482 struct coverage_data *item = functions_head;
1483 while (item && item->ident != (unsigned) FUNC_DECL_FUNC_ID (cfun))
1484 item = item->next;
1486 /* If a matching function entry hasn't been found, either this function
1487 has had no coverage counts added in the profile pass, or this
1488 is a new function (function versioning, etc). Create a new entry. */
1489 if (!item)
1491 int cnt;
1493 item = ggc_alloc_coverage_data ();
1494 *functions_tail = item;
1495 functions_tail = &item->next;
1496 item->next = 0;
1497 item->ident = FUNC_DECL_FUNC_ID (cfun);
1498 item->fn_decl = current_function_decl;
1499 item->lineno_checksum = coverage_compute_lineno_checksum ();
1500 item->cfg_checksum = coverage_compute_cfg_checksum ();
1501 for (cnt = 0; cnt < GCOV_COUNTERS; cnt++)
1502 item->ctr_vars[cnt] = NULL_TREE;
1505 var = fn_v_ctrs[idx];
1506 item->ctr_vars[idx] = var;
1507 if (var)
1509 tree array_type = build_index_type (size_int (fn_n_ctrs[idx] - 1));
1510 array_type = build_array_type (get_gcov_type (), array_type);
1511 TREE_TYPE (var) = array_type;
1512 DECL_SIZE (var) = TYPE_SIZE (array_type);
1513 DECL_SIZE_UNIT (var) = TYPE_SIZE_UNIT (array_type);
1514 varpool_finalize_decl (var);
1517 fn_n_ctrs[idx] = fn_b_ctrs[idx] = 0;
1518 fn_v_ctrs[idx] = NULL_TREE;
1519 prg_ctr_mask |= fn_ctr_mask;
1520 fn_ctr_mask = 0;
1524 /* Build a coverage variable of TYPE for function FN_DECL. If COUNTER
1525 >= 0 it is a counter array, otherwise it is the function structure. */
1527 static tree
1528 build_var (tree fn_decl, tree type, int counter)
1530 tree var = build_decl (BUILTINS_LOCATION, VAR_DECL, NULL_TREE, type);
1531 const char *fn_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fn_decl));
1532 char *buf;
1533 size_t fn_name_len, len;
1535 fn_name = targetm.strip_name_encoding (fn_name);
1536 fn_name_len = strlen (fn_name);
1537 buf = XALLOCAVEC (char, fn_name_len + 8 + sizeof (int) * 3);
1539 if (counter < 0)
1540 strcpy (buf, "__gcov__");
1541 else
1542 sprintf (buf, "__gcov%u_", counter);
1543 len = strlen (buf);
1544 #ifndef NO_DOT_IN_LABEL
1545 buf[len - 1] = '.';
1546 #elif !defined NO_DOLLAR_IN_LABEL
1547 buf[len - 1] = '$';
1548 #endif
1549 memcpy (buf + len, fn_name, fn_name_len + 1);
1550 DECL_NAME (var) = get_identifier (buf);
1551 TREE_STATIC (var) = 1;
1552 TREE_ADDRESSABLE (var) = 1;
1553 DECL_NONALIASED (var) = 1;
1554 DECL_ALIGN (var) = TYPE_ALIGN (type);
1556 return var;
1559 /* Creates the gcov_fn_info RECORD_TYPE. */
1561 static void
1562 build_fn_info_type (tree type, unsigned counters, tree gcov_info_type)
1564 tree ctr_info = lang_hooks.types.make_type (RECORD_TYPE);
1565 tree field, fields;
1566 tree array_type;
1568 gcc_assert (counters);
1570 /* ctr_info::num */
1571 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1572 get_gcov_unsigned_t ());
1573 fields = field;
1575 /* ctr_info::values */
1576 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1577 build_pointer_type (get_gcov_type ()));
1578 DECL_CHAIN (field) = fields;
1579 fields = field;
1581 finish_builtin_struct (ctr_info, "__gcov_ctr_info", fields, NULL_TREE);
1583 /* key */
1584 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1585 build_pointer_type (build_qualified_type
1586 (gcov_info_type, TYPE_QUAL_CONST)));
1587 fields = field;
1589 /* ident */
1590 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1591 get_gcov_unsigned_t ());
1592 DECL_CHAIN (field) = fields;
1593 fields = field;
1595 /* lineno_checksum */
1596 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1597 get_gcov_unsigned_t ());
1598 DECL_CHAIN (field) = fields;
1599 fields = field;
1601 /* cfg checksum */
1602 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1603 get_gcov_unsigned_t ());
1604 DECL_CHAIN (field) = fields;
1605 fields = field;
1607 array_type = build_index_type (size_int (counters - 1));
1608 array_type = build_array_type (ctr_info, array_type);
1610 /* counters */
1611 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE, array_type);
1612 DECL_CHAIN (field) = fields;
1613 fields = field;
1615 finish_builtin_struct (type, "__gcov_fn_info", fields, NULL_TREE);
1618 /* Returns a CONSTRUCTOR for a gcov_fn_info. DATA is
1619 the coverage data for the function and TYPE is the gcov_fn_info
1620 RECORD_TYPE. KEY is the object file key. */
1622 static tree
1623 build_fn_info (const struct coverage_data *data, tree type, tree key)
1625 tree fields = TYPE_FIELDS (type);
1626 tree ctr_type;
1627 unsigned ix;
1628 vec<constructor_elt, va_gc> *v1 = NULL;
1629 vec<constructor_elt, va_gc> *v2 = NULL;
1631 /* key */
1632 CONSTRUCTOR_APPEND_ELT (v1, fields,
1633 build1 (ADDR_EXPR, TREE_TYPE (fields), key));
1634 fields = DECL_CHAIN (fields);
1636 /* ident */
1637 CONSTRUCTOR_APPEND_ELT (v1, fields,
1638 build_int_cstu (get_gcov_unsigned_t (),
1639 data->ident));
1640 fields = DECL_CHAIN (fields);
1642 /* lineno_checksum */
1643 CONSTRUCTOR_APPEND_ELT (v1, fields,
1644 build_int_cstu (get_gcov_unsigned_t (),
1645 data->lineno_checksum));
1646 fields = DECL_CHAIN (fields);
1648 /* cfg_checksum */
1649 CONSTRUCTOR_APPEND_ELT (v1, fields,
1650 build_int_cstu (get_gcov_unsigned_t (),
1651 data->cfg_checksum));
1652 fields = DECL_CHAIN (fields);
1654 /* counters */
1655 ctr_type = TREE_TYPE (TREE_TYPE (fields));
1656 for (ix = 0; ix != GCOV_COUNTERS; ix++)
1657 if (prg_ctr_mask & (1 << ix))
1659 vec<constructor_elt, va_gc> *ctr = NULL;
1660 tree var = data->ctr_vars[ix];
1661 unsigned count = 0;
1663 if (var)
1664 count
1665 = tree_to_shwi (TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (var))))
1666 + 1;
1668 CONSTRUCTOR_APPEND_ELT (ctr, TYPE_FIELDS (ctr_type),
1669 build_int_cstu (get_gcov_unsigned_t (),
1670 count));
1672 if (var)
1673 CONSTRUCTOR_APPEND_ELT (ctr, DECL_CHAIN (TYPE_FIELDS (ctr_type)),
1674 build_fold_addr_expr (var));
1676 CONSTRUCTOR_APPEND_ELT (v2, NULL, build_constructor (ctr_type, ctr));
1678 /* In LIPO mode, coverage_finish is called late when pruning can not be
1679 * done, so we need to force emitting counter variables even for
1680 * eliminated functions to avoid unsat. */
1681 if (flag_dyn_ipa && var)
1682 varpool_finalize_decl (var);
1685 CONSTRUCTOR_APPEND_ELT (v1, fields,
1686 build_constructor (TREE_TYPE (fields), v2));
1688 return build_constructor (type, v1);
1691 /* Create gcov_info struct. TYPE is the incomplete RECORD_TYPE to be
1692 completed, and FN_INFO_PTR_TYPE is a pointer to the function info type. */
1694 static void
1695 build_info_type (tree type, tree fn_info_ptr_type)
1697 tree field, fields = NULL_TREE;
1698 tree merge_fn_type, mod_type;
1700 /* Version ident */
1701 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1702 get_gcov_unsigned_t ());
1703 DECL_CHAIN (field) = fields;
1704 fields = field;
1706 /* mod_info */
1707 mod_type = build_gcov_module_info_type ();
1708 mod_type = build_pointer_type (mod_type);
1709 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE, mod_type);
1710 DECL_CHAIN (field) = fields;
1711 fields = field;
1713 /* next pointer */
1714 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1715 build_pointer_type (build_qualified_type
1716 (type, TYPE_QUAL_CONST)));
1717 DECL_CHAIN (field) = fields;
1718 fields = field;
1720 /* stamp */
1721 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1722 get_gcov_unsigned_t ());
1723 DECL_CHAIN (field) = fields;
1724 fields = field;
1726 /* Filename */
1727 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1728 build_pointer_type (build_qualified_type
1729 (char_type_node, TYPE_QUAL_CONST)));
1730 DECL_CHAIN (field) = fields;
1731 fields = field;
1733 /* eof_pos */
1734 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1735 NULL_TREE, get_gcov_unsigned_t ());
1736 DECL_CHAIN (field) = fields;
1737 fields = field;
1739 /* merge fn array */
1740 merge_fn_type
1741 = build_function_type_list (void_type_node,
1742 build_pointer_type (get_gcov_type ()),
1743 get_gcov_unsigned_t (), NULL_TREE);
1744 merge_fn_type
1745 = build_array_type (build_pointer_type (merge_fn_type),
1746 build_index_type (size_int (GCOV_COUNTERS - 1)));
1747 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1748 merge_fn_type);
1749 DECL_CHAIN (field) = fields;
1750 fields = field;
1752 /* n_functions */
1753 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1754 get_gcov_unsigned_t ());
1755 DECL_CHAIN (field) = fields;
1756 fields = field;
1758 /* function_info pointer pointer */
1759 fn_info_ptr_type = build_pointer_type
1760 (build_qualified_type (fn_info_ptr_type, TYPE_QUAL_CONST));
1761 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1762 fn_info_ptr_type);
1763 DECL_CHAIN (field) = fields;
1764 fields = field;
1766 finish_builtin_struct (type, "__gcov_info", fields, NULL_TREE);
1769 /* Compute an array (tree) of include path strings. STRING_TYPE is
1770 the path string type, INC_PATH_VALUE is the initial value of the
1771 path array, PATHS gives raw path string values, and NUM is the
1772 number of paths. */
1774 static void
1775 build_inc_path_array_value (tree string_type, vec<constructor_elt, va_gc> **v,
1776 cpp_dir *paths, int num)
1778 int i;
1779 cpp_dir *pdir;
1780 for (i = 0, pdir = paths; i < num; pdir = pdir->next)
1782 const char *path_raw_string;
1783 int path_string_length;
1784 tree path_string;
1785 path_raw_string = pdir->name;
1786 path_string_length = strlen (path_raw_string);
1787 path_string = build_string (path_string_length + 1, path_raw_string);
1788 TREE_TYPE (path_string) = build_array_type
1789 (char_type_node, build_index_type
1790 (build_int_cst (NULL_TREE, path_string_length)));
1791 CONSTRUCTOR_APPEND_ELT (*v, NULL,
1792 build1 (ADDR_EXPR, string_type, path_string));
1793 i++;
1797 /* Compute an array (tree) of strings. STR_TYPE is the string type,
1798 STR_ARRAY_VALUE is the initial value of the string array, and HEAD gives
1799 the list of raw strings. */
1801 static void
1802 build_str_array_value (tree str_type, vec<constructor_elt, va_gc> **v,
1803 struct str_list *head)
1805 const char *raw_str;
1806 int str_length;
1807 while (head)
1809 tree str;
1810 raw_str = head->str;
1811 str_length = strlen (raw_str);
1812 str = build_string (str_length + 1, raw_str);
1813 TREE_TYPE (str) =
1814 build_array_type (char_type_node,
1815 build_index_type (build_int_cst (NULL_TREE,
1816 str_length)));
1817 CONSTRUCTOR_APPEND_ELT (*v, NULL,
1818 build1 (ADDR_EXPR, str_type, str));
1819 head = head->next;
1821 return;
1824 /* Compute an array (tree) of command-line argument strings. STRING_TYPE is
1825 the string type, CL_ARGS_VALUE is the initial value of the command-line
1826 args array. */
1828 static void
1829 build_cl_args_array_value (tree string_type, vec<constructor_elt, va_gc> **v)
1831 unsigned int i;
1833 for (i = 0; i < num_lipo_cl_args; i++)
1835 int arg_length = strlen (lipo_cl_args[i]);
1836 tree arg_string = build_string (arg_length + 1, lipo_cl_args[i]);
1837 TREE_TYPE (arg_string) =
1838 build_array_type (char_type_node,
1839 build_index_type (build_int_cst (NULL_TREE,
1840 arg_length)));
1841 CONSTRUCTOR_APPEND_ELT (*v, NULL,
1842 build1 (ADDR_EXPR, string_type, arg_string));
1844 return;
1847 /* Returns the type of the module info associated with the
1848 current source module being compiled. */
1850 static tree
1851 build_gcov_module_info_type (void)
1853 tree type, field, fields = NULL_TREE;
1854 tree string_type, index_type, string_array_type;
1856 cpp_dir *quote_paths, *bracket_paths, *system_paths, *pdir;
1857 int num_quote_paths = 0, num_bracket_paths = 0, num_system_paths = 0;
1859 type = lang_hooks.types.make_type (RECORD_TYPE);
1860 string_type = build_pointer_type (
1861 build_qualified_type (char_type_node,
1862 TYPE_QUAL_CONST));
1864 /* ident */
1865 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1866 NULL_TREE, get_gcov_unsigned_t ());
1867 DECL_CHAIN (field) = fields;
1868 fields = field;
1870 /* is_primary */
1871 /* We also overload this field to store a flag that indicates whether this
1872 module was built in regular FDO or LIPO mode (-fripa). When reading this
1873 field from a GCDA file, it should be used as the IS_PRIMARY flag. When
1874 reading this field from the binary's data section, it should be used
1875 as a FDO/LIPO flag. */
1876 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1877 NULL_TREE, get_gcov_unsigned_t ());
1878 DECL_CHAIN (field) = fields;
1879 fields = field;
1881 /* flags: is_exported and include_all_aux flag. */
1882 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1883 NULL_TREE, get_gcov_unsigned_t ());
1884 DECL_CHAIN (field) = fields;
1885 fields = field;
1887 /* lang field */
1888 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1889 NULL_TREE, get_gcov_unsigned_t ());
1890 DECL_CHAIN (field) = fields;
1891 fields = field;
1893 /* ggc_memory field */
1894 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1895 NULL_TREE, get_gcov_unsigned_t ());
1896 DECL_CHAIN (field) = fields;
1897 fields = field;
1899 /* da_filename */
1900 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1901 NULL_TREE, string_type);
1902 DECL_CHAIN (field) = fields;
1903 fields = field;
1905 /* Source name */
1906 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1907 NULL_TREE, string_type);
1908 DECL_CHAIN (field) = fields;
1909 fields = field;
1911 /* Num quote paths */
1912 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1913 NULL_TREE, get_gcov_unsigned_t ());
1914 DECL_CHAIN (field) = fields;
1915 fields = field;
1917 /* Num bracket paths */
1918 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1919 NULL_TREE, get_gcov_unsigned_t ());
1920 DECL_CHAIN (field) = fields;
1921 fields = field;
1923 /* Num system paths */
1924 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1925 NULL_TREE, get_gcov_unsigned_t ());
1926 DECL_CHAIN (field) = fields;
1927 fields = field;
1929 /* Num -D/-U options. */
1930 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1931 NULL_TREE, get_gcov_unsigned_t ());
1932 DECL_CHAIN (field) = fields;
1933 fields = field;
1935 /* Num -imacro/-include options. */
1936 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1937 get_gcov_unsigned_t ());
1938 DECL_CHAIN (field) = fields;
1939 fields = field;
1941 /* Num command-line args. */
1942 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1943 NULL_TREE, get_gcov_unsigned_t ());
1944 DECL_CHAIN (field) = fields;
1945 fields = field;
1947 get_include_chains (&quote_paths, &bracket_paths, &system_paths);
1948 for (pdir = quote_paths; pdir; pdir = pdir->next)
1950 if (pdir == bracket_paths)
1951 break;
1952 num_quote_paths++;
1954 for (pdir = bracket_paths; pdir; pdir = pdir->next)
1956 if (pdir == system_paths)
1957 break;
1958 num_bracket_paths++;
1960 for (pdir = system_paths; pdir; pdir = pdir->next)
1961 num_system_paths++;
1963 /* string array */
1964 index_type = build_index_type (build_int_cst (NULL_TREE,
1965 num_quote_paths +
1966 num_bracket_paths +
1967 num_system_paths +
1968 num_cpp_defines +
1969 num_cpp_includes +
1970 num_lipo_cl_args));
1972 string_array_type = build_array_type (string_type, index_type);
1973 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1974 NULL_TREE, string_array_type);
1975 DECL_CHAIN (field) = fields;
1976 fields = field;
1978 finish_builtin_struct (type, "__gcov_module_info", fields, NULL_TREE);
1980 return type;
1983 /* Returns the value of the module info associated with the
1984 current source module being compiled. */
1986 static tree
1987 build_gcov_module_info_value (tree mod_type)
1989 tree info_fields, mod_info;
1990 tree value = NULL_TREE;
1991 int file_name_len;
1992 tree filename_string, string_array_type, string_type;
1993 cpp_dir *quote_paths, *bracket_paths, *system_paths, *pdir;
1994 int num_quote_paths = 0, num_bracket_paths = 0, num_system_paths = 0;
1995 unsigned lang;
1996 char name_buf[50];
1997 vec<constructor_elt,va_gc> *v = NULL, *path_v = NULL;
1999 info_fields = TYPE_FIELDS (mod_type);
2001 /* ident */
2002 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2003 build_int_cstu (get_gcov_unsigned_t (), 0));
2005 info_fields = DECL_CHAIN (info_fields);
2007 /* is_primary */
2008 /* We also overload this field to store a flag that indicates whether this
2009 module was built in regular FDO or LIPO mode (-fripa). When reading this
2010 field from a GCDA file, it should be used as the IS_PRIMARY flag. When
2011 reading this field from the binary's data section, it should be used
2012 as a FDO/LIPO flag. */
2013 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2014 build_int_cstu (get_gcov_unsigned_t (),
2015 flag_dyn_ipa ? 1 : 0));
2016 info_fields = DECL_CHAIN (info_fields);
2018 /* flags */
2019 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2020 build_int_cstu (get_gcov_unsigned_t (), 0));
2021 info_fields = DECL_CHAIN (info_fields);
2023 /* lang field */
2024 if (!strcmp (lang_hooks.name, "GNU C"))
2025 lang = GCOV_MODULE_C_LANG;
2026 else if (!strcmp (lang_hooks.name, "GNU C++"))
2027 lang = GCOV_MODULE_CPP_LANG;
2028 else
2029 lang = GCOV_MODULE_UNKNOWN_LANG;
2030 if (has_asm_statement)
2031 lang |= GCOV_MODULE_ASM_STMTS;
2033 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2034 build_int_cstu (get_gcov_unsigned_t (), lang));
2035 info_fields = DECL_CHAIN (info_fields);
2037 /* ggc_memory field */
2038 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2039 build_int_cstu (get_gcov_unsigned_t (), ggc_total_memory));
2040 info_fields = DECL_CHAIN (info_fields);
2042 /* da_filename */
2044 string_type = TREE_TYPE (info_fields);
2045 file_name_len = strlen (da_base_file_name);
2046 filename_string = build_string (file_name_len + 1, da_base_file_name);
2047 TREE_TYPE (filename_string) = build_array_type
2048 (char_type_node, build_index_type
2049 (build_int_cst (NULL_TREE, file_name_len)));
2050 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2051 build1 (ADDR_EXPR, string_type, filename_string));
2052 info_fields = DECL_CHAIN (info_fields);
2054 /* Source name */
2056 file_name_len = strlen (main_input_file_name);
2057 filename_string = build_string (file_name_len + 1, main_input_file_name);
2058 TREE_TYPE (filename_string) = build_array_type
2059 (char_type_node, build_index_type
2060 (build_int_cst (NULL_TREE, file_name_len)));
2061 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2062 build1 (ADDR_EXPR, string_type, filename_string));
2063 info_fields = DECL_CHAIN (info_fields);
2065 get_include_chains (&quote_paths, &bracket_paths, &system_paths);
2066 for (pdir = quote_paths; pdir; pdir = pdir->next)
2068 if (pdir == bracket_paths)
2069 break;
2070 num_quote_paths++;
2072 for (pdir = bracket_paths; pdir; pdir = pdir->next)
2074 if (pdir == system_paths)
2075 break;
2076 num_bracket_paths++;
2078 for (pdir = system_paths; pdir; pdir = pdir->next)
2079 num_system_paths++;
2081 /* Num quote paths */
2082 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2083 build_int_cstu (get_gcov_unsigned_t (),
2084 num_quote_paths));
2085 info_fields = DECL_CHAIN (info_fields);
2087 /* Num bracket paths */
2088 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2089 build_int_cstu (get_gcov_unsigned_t (),
2090 num_bracket_paths));
2091 info_fields = DECL_CHAIN (info_fields);
2093 /* Num system paths */
2094 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2095 build_int_cstu (get_gcov_unsigned_t (),
2096 num_system_paths));
2097 info_fields = DECL_CHAIN (info_fields);
2099 /* Num -D/-U options. */
2100 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2101 build_int_cstu (get_gcov_unsigned_t (),
2102 num_cpp_defines));
2103 info_fields = DECL_CHAIN (info_fields);
2105 /* Num -imacro/-include options. */
2106 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2107 build_int_cstu (get_gcov_unsigned_t (),
2108 num_cpp_includes));
2109 info_fields = DECL_CHAIN (info_fields);
2111 /* Num command-line args. */
2112 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2113 build_int_cstu (get_gcov_unsigned_t (),
2114 num_lipo_cl_args));
2115 info_fields = DECL_CHAIN (info_fields);
2117 /* string array */
2118 string_array_type = TREE_TYPE (info_fields);
2119 build_inc_path_array_value (string_type, &path_v,
2120 quote_paths, num_quote_paths);
2121 build_inc_path_array_value (string_type, &path_v,
2122 bracket_paths, num_bracket_paths);
2123 build_inc_path_array_value (string_type, &path_v,
2124 system_paths, num_system_paths);
2125 build_str_array_value (string_type, &path_v,
2126 cpp_defines_head);
2127 build_str_array_value (string_type, &path_v,
2128 cpp_includes_head);
2129 build_cl_args_array_value (string_type, &path_v);
2130 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2131 build_constructor (string_array_type, path_v));
2132 info_fields = DECL_CHAIN (info_fields);
2134 gcc_assert (!info_fields);
2135 value = build_constructor (mod_type, v);
2137 mod_info = build_decl (BUILTINS_LOCATION, VAR_DECL,
2138 NULL_TREE, TREE_TYPE (value));
2139 TREE_STATIC (mod_info) = 1;
2140 ASM_GENERATE_INTERNAL_LABEL (name_buf, "MODINFO", 0);
2141 DECL_NAME (mod_info) = get_identifier (name_buf);
2142 DECL_INITIAL (mod_info) = value;
2144 /* Build structure. */
2145 varpool_finalize_decl (mod_info);
2147 return mod_info;
2150 /* Returns a CONSTRUCTOR for the gcov_info object. INFO_TYPE is the
2151 gcov_info structure type, FN_ARY is the array of pointers to
2152 function info objects. */
2154 static tree
2155 build_info (tree info_type, tree fn_ary)
2157 tree info_fields = TYPE_FIELDS (info_type);
2158 tree merge_fn_type, n_funcs;
2159 unsigned ix;
2160 tree mod_value = NULL_TREE;
2161 tree filename_string;
2162 int da_file_name_len;
2163 vec<constructor_elt, va_gc> *v1 = NULL;
2164 vec<constructor_elt, va_gc> *v2 = NULL;
2166 /* Version ident */
2167 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
2168 build_int_cstu (TREE_TYPE (info_fields),
2169 GCOV_VERSION));
2170 info_fields = DECL_CHAIN (info_fields);
2172 /* mod_info */
2173 mod_value = build_gcov_module_info_value (TREE_TYPE (TREE_TYPE (info_fields)));
2174 mod_value = build1 (ADDR_EXPR,
2175 build_pointer_type (TREE_TYPE (mod_value)),
2176 mod_value);
2177 CONSTRUCTOR_APPEND_ELT (v1, info_fields, mod_value);
2178 info_fields = DECL_CHAIN (info_fields);
2180 /* next -- NULL */
2181 CONSTRUCTOR_APPEND_ELT (v1, info_fields, null_pointer_node);
2182 info_fields = DECL_CHAIN (info_fields);
2184 /* stamp */
2185 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
2186 build_int_cstu (TREE_TYPE (info_fields),
2187 bbg_file_stamp));
2188 info_fields = DECL_CHAIN (info_fields);
2190 /* Filename */
2191 da_file_name_len = strlen (da_file_name);
2192 filename_string = build_string (da_file_name_len + 1, da_file_name);
2193 TREE_TYPE (filename_string) = build_array_type
2194 (char_type_node, build_index_type (size_int (da_file_name_len)));
2195 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
2196 build1 (ADDR_EXPR, TREE_TYPE (info_fields),
2197 filename_string));
2198 info_fields = DECL_CHAIN (info_fields);
2200 /* eof_pos */
2201 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
2202 build_int_cstu (TREE_TYPE (info_fields), 0));
2203 info_fields = DECL_CHAIN (info_fields);
2205 /* merge fn array -- NULL slots indicate unmeasured counters */
2206 merge_fn_type = TREE_TYPE (TREE_TYPE (info_fields));
2207 for (ix = 0; ix != GCOV_COUNTERS; ix++)
2209 tree ptr = null_pointer_node;
2211 if ((1u << ix) & prg_ctr_mask)
2213 tree merge_fn = build_decl (BUILTINS_LOCATION,
2214 FUNCTION_DECL,
2215 get_identifier (ctr_merge_functions[ix]),
2216 TREE_TYPE (merge_fn_type));
2217 DECL_EXTERNAL (merge_fn) = 1;
2218 TREE_PUBLIC (merge_fn) = 1;
2219 DECL_ARTIFICIAL (merge_fn) = 1;
2220 TREE_NOTHROW (merge_fn) = 1;
2221 /* Initialize assembler name so we can stream out. */
2222 DECL_ASSEMBLER_NAME (merge_fn);
2223 ptr = build1 (ADDR_EXPR, merge_fn_type, merge_fn);
2225 CONSTRUCTOR_APPEND_ELT (v2, NULL, ptr);
2227 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
2228 build_constructor (TREE_TYPE (info_fields), v2));
2229 info_fields = DECL_CHAIN (info_fields);
2231 /* n_functions */
2232 n_funcs = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (fn_ary)));
2233 n_funcs = fold_build2 (PLUS_EXPR, TREE_TYPE (info_fields),
2234 n_funcs, size_one_node);
2235 CONSTRUCTOR_APPEND_ELT (v1, info_fields, n_funcs);
2236 info_fields = DECL_CHAIN (info_fields);
2238 /* functions */
2239 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
2240 build1 (ADDR_EXPR, TREE_TYPE (info_fields), fn_ary));
2241 info_fields = DECL_CHAIN (info_fields);
2243 gcc_assert (!info_fields);
2244 return build_constructor (info_type, v1);
2247 /* Generate the constructor function to call __gcov_init. */
2249 static void
2250 build_init_ctor (tree gcov_info_type)
2252 tree ctor, stmt, init_fn;
2254 /* Build a decl for __gcov_init. */
2255 init_fn = build_pointer_type (gcov_info_type);
2256 init_fn = build_function_type_list (void_type_node, init_fn, NULL);
2257 init_fn = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
2258 get_identifier ("__gcov_init"), init_fn);
2259 TREE_PUBLIC (init_fn) = 1;
2260 DECL_EXTERNAL (init_fn) = 1;
2261 DECL_ASSEMBLER_NAME (init_fn);
2263 /* Generate a call to __gcov_init(&gcov_info). */
2264 ctor = NULL;
2265 stmt = build_fold_addr_expr (gcov_info_var);
2266 stmt = build_call_expr (init_fn, 1, stmt);
2267 append_to_statement_list (stmt, &ctor);
2269 /* Generate a constructor to run it. */
2270 cgraph_build_static_cdtor ('I', ctor, DEFAULT_INIT_PRIORITY);
2273 /* Create the gcov_info types and object. Generate the constructor
2274 function to call __gcov_init. Does not generate the initializer
2275 for the object. Returns TRUE if coverage data is being emitted. */
2277 static bool
2278 coverage_obj_init (void)
2280 tree gcov_info_type;
2281 unsigned n_counters = 0;
2282 unsigned ix;
2283 struct coverage_data *fn;
2284 struct coverage_data **fn_prev;
2285 char name_buf[32];
2287 no_coverage = 1; /* Disable any further coverage. */
2289 if (!prg_ctr_mask)
2290 return false;
2292 if (cgraph_dump_file)
2293 fprintf (cgraph_dump_file, "Using data file %s\n", da_file_name);
2295 /* Prune functions. */
2296 if (!flag_dyn_ipa)
2297 /* in lipo mode, coverage_finish is called when function struct is cleared,
2298 so pruning code here will skip all functions. */
2299 for (fn_prev = &functions_head; (fn = *fn_prev);)
2300 if (DECL_STRUCT_FUNCTION (fn->fn_decl))
2301 fn_prev = &fn->next;
2302 else
2303 /* The function is not being emitted, remove from list. */
2304 *fn_prev = fn->next;
2306 if (functions_head == NULL)
2307 return false;
2309 for (ix = 0; ix != GCOV_COUNTERS; ix++)
2310 if ((1u << ix) & prg_ctr_mask)
2311 n_counters++;
2313 /* Build the info and fn_info types. These are mutually recursive. */
2314 gcov_info_type = lang_hooks.types.make_type (RECORD_TYPE);
2315 gcov_fn_info_type = lang_hooks.types.make_type (RECORD_TYPE);
2316 gcov_fn_info_ptr_type = build_pointer_type
2317 (build_qualified_type (gcov_fn_info_type, TYPE_QUAL_CONST));
2318 build_fn_info_type (gcov_fn_info_type, n_counters, gcov_info_type);
2319 build_info_type (gcov_info_type, gcov_fn_info_ptr_type);
2321 /* Build the gcov info var, this is referred to in its own
2322 initializer. */
2323 gcov_info_var = build_decl (BUILTINS_LOCATION,
2324 VAR_DECL, NULL_TREE, gcov_info_type);
2325 TREE_STATIC (gcov_info_var) = 1;
2326 ASM_GENERATE_INTERNAL_LABEL (name_buf, "LPBX", 0);
2327 DECL_NAME (gcov_info_var) = get_identifier (name_buf);
2329 return true;
2332 /* Generate the coverage function info for FN and DATA. Append a
2333 pointer to that object to CTOR and return the appended CTOR. */
2335 static vec<constructor_elt, va_gc> *
2336 coverage_obj_fn (vec<constructor_elt, va_gc> *ctor, tree fn,
2337 struct coverage_data const *data)
2339 tree init = build_fn_info (data, gcov_fn_info_type, gcov_info_var);
2340 tree var = build_var (fn, gcov_fn_info_type, -1);
2342 DECL_INITIAL (var) = init;
2343 varpool_finalize_decl (var);
2345 CONSTRUCTOR_APPEND_ELT (ctor, NULL,
2346 build1 (ADDR_EXPR, gcov_fn_info_ptr_type, var));
2347 return ctor;
2350 /* Finalize the coverage data. Generates the array of pointers to
2351 function objects from CTOR. Generate the gcov_info initializer.
2352 Generate the constructor function to call __gcov_init. */
2354 static void
2355 coverage_obj_finish (vec<constructor_elt, va_gc> *ctor)
2357 unsigned n_functions = vec_safe_length (ctor);
2358 tree fn_info_ary_type = build_array_type
2359 (build_qualified_type (gcov_fn_info_ptr_type, TYPE_QUAL_CONST),
2360 build_index_type (size_int (n_functions - 1)));
2361 tree fn_info_ary = build_decl (BUILTINS_LOCATION, VAR_DECL, NULL_TREE,
2362 fn_info_ary_type);
2363 char name_buf[32];
2365 TREE_STATIC (fn_info_ary) = 1;
2366 ASM_GENERATE_INTERNAL_LABEL (name_buf, "LPBX", 1);
2367 DECL_NAME (fn_info_ary) = get_identifier (name_buf);
2368 DECL_INITIAL (fn_info_ary) = build_constructor (fn_info_ary_type, ctor);
2369 varpool_finalize_decl (fn_info_ary);
2371 DECL_INITIAL (gcov_info_var)
2372 = build_info (TREE_TYPE (gcov_info_var), fn_info_ary);
2374 build_init_ctor (TREE_TYPE (gcov_info_var));
2376 varpool_finalize_decl (gcov_info_var);
2379 /* Get the da file name, given base file name. */
2381 static char *
2382 get_da_file_name (const char *base_file_name)
2384 char *da_file_name;
2385 int len = strlen (base_file_name);
2386 const char *prefix = profile_data_prefix;
2387 int prefix_len = 0;
2389 if (profile_data_prefix == 0 && !IS_ABSOLUTE_PATH(&base_file_name[0]))
2391 profile_data_prefix = getpwd ();
2392 prefix = profile_data_prefix;
2395 prefix_len = (prefix) ? strlen (prefix) + 1 : 0;
2397 /* Name of da file. */
2398 da_file_name = XNEWVEC (char, len + strlen (GCOV_DATA_SUFFIX)
2399 + prefix_len + 2);
2401 if (prefix)
2403 strcpy (da_file_name, prefix);
2404 da_file_name[prefix_len - 1] = '/';
2405 da_file_name[prefix_len] = 0;
2407 else
2408 da_file_name[0] = 0;
2409 strcat (da_file_name, base_file_name);
2410 if (profile_base_name_suffix_to_strip)
2412 int base_name_len = strlen (da_file_name);
2413 int suffix_to_strip_len = strlen (profile_base_name_suffix_to_strip);
2415 if (base_name_len > suffix_to_strip_len
2416 && !strcmp (da_file_name + (base_name_len - suffix_to_strip_len),
2417 profile_base_name_suffix_to_strip))
2418 da_file_name[base_name_len - suffix_to_strip_len] = '\0';
2421 strcat (da_file_name, GCOV_DATA_SUFFIX);
2422 return da_file_name;
2425 /* Callback to move counts_entry from one hash table to
2426 the target hashtable */
2429 move_hash_entry_callback (counts_entry **x,
2430 hash_table <counts_entry> *target_counts_hash)
2432 counts_entry *entry = *x;
2433 counts_entry **slot;
2434 slot = target_counts_hash->find_slot (entry, INSERT);
2435 *slot = entry;
2436 return 1;
2439 /* Rebuild counts_hash already built the primary module. This hashtable
2440 was built with a module-id of zero. It needs to be rebuilt taking the
2441 correct primary module-id into account. */
2444 rehash_callback (counts_entry **x,
2445 hash_table <counts_entry> *target_counts_hash)
2447 counts_entry *entry = *x;
2448 counts_entry **slot;
2450 entry->ident = GEN_FUNC_GLOBAL_ID (primary_module_id, entry->ident);
2451 slot = target_counts_hash->find_slot (entry, INSERT);
2452 *slot = entry;
2453 return 1;
2456 /* Rebuild counts_hash already built the primary module. This hashtable
2457 was built with a module-id of zero. It needs to be rebuilt taking the
2458 correct primary module-id into account. */
2460 static void
2461 rebuild_counts_hash (void)
2463 hash_table <counts_entry> tmp_counts_hash;
2464 tmp_counts_hash.create (10);
2465 gcc_assert (primary_module_id);
2467 rebuilding_counts_hash = true;
2469 /* Move the counts entries to the temporary hashtable. */
2470 counts_hash.traverse_noresize <
2471 hash_table <counts_entry> *,
2472 move_hash_entry_callback> (&tmp_counts_hash);
2473 counts_hash.empty ();
2475 /* Now rehash and copy back. */
2476 tmp_counts_hash.traverse_noresize <
2477 hash_table <counts_entry> *,
2478 rehash_callback> (&counts_hash);
2479 tmp_counts_hash.dispose();
2481 rebuilding_counts_hash = false;
2484 /* Add the module information record for the module with id
2485 MODULE_ID. IS_PRIMARY is true if the module is the primary module.
2486 INDEX is the index of the new record in the module info array. */
2488 void
2489 add_module_info (unsigned module_id, bool is_primary, int index)
2491 struct gcov_module_info *cur_info;
2492 module_infos = XRESIZEVEC (struct gcov_module_info *,
2493 module_infos, index + 1);
2494 module_infos[index] = XNEW (struct gcov_module_info);
2495 cur_info = module_infos[index];
2496 cur_info->ident = module_id;
2497 SET_MODULE_EXPORTED (cur_info);
2498 cur_info->num_quote_paths = 0;
2499 cur_info->num_bracket_paths = 0;
2500 cur_info->da_filename = NULL;
2501 cur_info->source_filename = NULL;
2502 if (is_primary)
2503 primary_module_id = module_id;
2506 /* Process the include paths needed for parsing the aux modules.
2507 The sub_pattern is in the form SUB_PATH:NEW_SUB_PATH. If it is
2508 defined, the SUB_PATH in ORIG_INC_PATH will be replaced with
2509 NEW_SUB_PATH. */
2511 static void
2512 process_include (char **orig_inc_path, char* old_sub, char *new_sub)
2514 char *inc_path, *orig_sub;
2516 if (strlen (*orig_inc_path) < strlen (old_sub))
2517 return;
2519 inc_path = (char*) xmalloc (strlen (*orig_inc_path) + strlen (new_sub)
2520 - strlen (old_sub) + 1);
2521 orig_sub = strstr (*orig_inc_path, old_sub);
2522 if (!orig_sub)
2524 inform (UNKNOWN_LOCATION, "subpath %s not found in path %s",
2525 old_sub, *orig_inc_path);
2526 free (inc_path);
2527 return;
2530 strncpy (inc_path, *orig_inc_path, orig_sub - *orig_inc_path);
2531 inc_path[orig_sub - *orig_inc_path] = '\0';
2532 strcat (inc_path, new_sub);
2533 strcat (inc_path, orig_sub + strlen (old_sub));
2535 free (*orig_inc_path);
2536 *orig_inc_path = inc_path;
2539 /* Process include paths for MOD_INFO according to option
2540 -fripa-inc-path-sub=OLD_SUB:NEW_SUB */
2542 static void
2543 process_include_paths_1 (struct gcov_module_info *mod_info,
2544 char* old_sub, char *new_sub)
2546 unsigned i, j;
2548 for (i = 0; i < mod_info->num_quote_paths; i++)
2549 process_include (&mod_info->string_array[i], old_sub, new_sub);
2551 for (i = 0, j = mod_info->num_quote_paths;
2552 i < mod_info->num_bracket_paths; i++, j++)
2553 process_include (&mod_info->string_array[j], old_sub, new_sub);
2555 for (i = 0, j = mod_info->num_quote_paths + mod_info->num_bracket_paths +
2556 mod_info->num_cpp_defines; i < mod_info->num_cpp_includes; i++, j++)
2557 process_include (&mod_info->string_array[j], old_sub, new_sub);
2561 /* Process include paths for MOD_INFO according to option
2562 -fripa-inc-path-sub=old_sub1:new_sub1[,old_sub2:new_sub2] */
2564 static void
2565 process_include_paths (struct gcov_module_info *mod_info)
2567 char *sub_pattern, *cur, *next, *new_sub;
2569 if (!lipo_inc_path_pattern)
2570 return;
2572 sub_pattern = xstrdup (lipo_inc_path_pattern);
2573 cur = sub_pattern;
2577 next = strchr (cur, ',');
2578 if (next)
2579 *next++ = '\0';
2580 new_sub = strchr (cur, ':');
2581 if (!new_sub)
2583 error ("Invalid path substibution pattern %s", sub_pattern);
2584 free (sub_pattern);
2585 return;
2587 *new_sub++ = '\0';
2588 process_include_paths_1 (mod_info, cur, new_sub);
2589 cur = next;
2590 } while (cur);
2591 free (sub_pattern);
2594 /* Set the prepreprocessing context (include search paths, -D/-U).
2595 PARSE_IN is the preprocessor reader, I is the index of the module,
2596 and VERBOSE is the verbose flag. */
2598 void
2599 set_lipo_c_parsing_context (struct cpp_reader *parse_in, int i, bool verbose)
2601 struct gcov_module_info *mod_info;
2602 if (!L_IPO_COMP_MODE)
2603 return;
2605 mod_info = module_infos[i];
2607 gcc_assert (flag_dyn_ipa);
2608 current_module_id = mod_info->ident;
2609 reset_funcdef_no ();
2611 if (current_module_id != primary_module_id)
2613 unsigned i, j;
2615 process_include_paths (mod_info);
2616 /* Setup include paths. */
2617 clear_include_chains ();
2618 for (i = 0; i < mod_info->num_quote_paths; i++)
2619 add_path (xstrdup (mod_info->string_array[i]),
2620 QUOTE, 0, 1);
2621 for (i = 0, j = mod_info->num_quote_paths;
2622 i < mod_info->num_bracket_paths; i++, j++)
2623 add_path (xstrdup (mod_info->string_array[j]),
2624 BRACKET, 0, 1);
2625 for (i = 0; i < mod_info->num_system_paths; i++, j++)
2626 add_path (xstrdup (mod_info->string_array[j]),
2627 SYSTEM, 0, 1);
2628 register_include_chains (parse_in, NULL, NULL, NULL,
2629 0, 0, verbose);
2631 /* Setup defines/undefs. */
2632 for (i = 0; i < mod_info->num_cpp_defines; i++, j++)
2633 if (mod_info->string_array[j][0] == 'D')
2634 cpp_define (parse_in, mod_info->string_array[j] + 1);
2635 else
2636 cpp_undef (parse_in, mod_info->string_array[j] + 1);
2638 /* Setup -imacro/-include. */
2639 for (i = 0; i < mod_info->num_cpp_includes; i++, j++)
2640 cpp_push_include (parse_in, mod_info->string_array[j]);
2644 /* Perform file-level initialization. Read in data file, generate name
2645 of graph file. */
2647 void
2648 coverage_init (const char *filename, const char* source_name)
2650 char* src_name_prefix = 0;
2651 int src_name_prefix_len = 0;
2652 int len = strlen (filename);
2654 /* Since coverage_init is invoked very early, before the pass
2655 manager, we need to set up the dumping explicitly. This is
2656 similar to the handling in finish_optimization_passes. */
2657 int profile_pass_num =
2658 g->get_passes ()->get_pass_profile ()->static_pass_number;
2659 g->get_dumps ()->dump_start (profile_pass_num, NULL);
2661 has_asm_statement = false;
2662 da_file_name = get_da_file_name (filename);
2663 da_base_file_name = XNEWVEC (char, strlen (filename) + 1);
2664 strcpy (da_base_file_name, filename);
2666 if (profile_data_prefix == 0 && !IS_ABSOLUTE_PATH (source_name))
2668 src_name_prefix = getpwd ();
2669 src_name_prefix_len = strlen (src_name_prefix) + 1;
2671 main_input_file_name = XNEWVEC (char, strlen (source_name) + 1
2672 + src_name_prefix_len);
2673 if (!src_name_prefix)
2674 strcpy (main_input_file_name, source_name);
2675 else
2677 strcpy (main_input_file_name, src_name_prefix);
2678 strcat (main_input_file_name, "/");
2679 strcat (main_input_file_name, source_name);
2682 bbg_file_stamp = local_tick;
2684 if (flag_branch_probabilities)
2685 read_counts_file (da_file_name, 0);
2687 /* Rebuild counts_hash and read the auxiliary GCDA files. */
2688 if (flag_profile_use && L_IPO_COMP_MODE)
2690 unsigned i;
2691 gcc_assert (flag_dyn_ipa);
2692 rebuild_counts_hash ();
2693 for (i = 1; i < num_in_fnames; i++)
2694 read_counts_file (get_da_file_name (module_infos[i]->da_filename),
2695 module_infos[i]->ident);
2698 /* Define variables which are referenced at runtime by libgcov. */
2699 if (profiling_enabled_p ())
2701 tree_init_instrumentation ();
2702 tree_init_dyn_ipa_parameters ();
2703 tree_init_instrumentation_sampling ();
2706 /* Name of bbg file. */
2707 if (flag_test_coverage && !flag_compare_debug)
2709 bbg_file_name = XNEWVEC (char, len + strlen (GCOV_NOTE_SUFFIX) + 1);
2710 memcpy (bbg_file_name, filename, len);
2711 strcpy (bbg_file_name + len, GCOV_NOTE_SUFFIX);
2712 if (!gcov_open (bbg_file_name, -1))
2714 error ("cannot open %s", bbg_file_name);
2715 bbg_file_name = NULL;
2717 else
2719 gcov_write_unsigned (GCOV_NOTE_MAGIC);
2720 gcov_write_unsigned (GCOV_VERSION);
2721 gcov_write_unsigned (bbg_file_stamp);
2725 g->get_dumps ()->dump_finish (profile_pass_num);
2728 /* Return True if any type of profiling is enabled which requires linking
2729 in libgcov otherwise return False. */
2731 static bool
2732 profiling_enabled_p (void)
2734 return profile_arc_flag
2735 || flag_profile_generate_sampling;
2738 /* Performs file-level cleanup. Close graph file, generate coverage
2739 variables and constructor. */
2741 void
2742 coverage_finish (void)
2744 if (bbg_file_name && gcov_close ())
2745 unlink (bbg_file_name);
2747 if (!flag_branch_probabilities && flag_test_coverage
2748 && (!local_tick || local_tick == (unsigned)-1))
2749 /* Only remove the da file, if we're emitting coverage code and
2750 cannot uniquely stamp it. If we can stamp it, libgcov will DTRT. */
2751 unlink (da_file_name);
2753 if (coverage_obj_init ())
2755 vec<constructor_elt, va_gc> *fn_ctor = NULL;
2756 struct coverage_data *fn;
2758 for (fn = functions_head; fn; fn = fn->next)
2759 fn_ctor = coverage_obj_fn (fn_ctor, fn->fn_decl, fn);
2760 coverage_obj_finish (fn_ctor);
2763 XDELETEVEC (da_file_name);
2764 da_file_name = NULL;
2767 /* Add S to the end of the string-list, the head and tail of which are
2768 pointed-to by HEAD and TAIL, respectively. */
2770 static void
2771 str_list_append (struct str_list **head, struct str_list **tail, const char *s)
2773 struct str_list *e = XNEW (struct str_list);
2774 e->str = XNEWVEC (char, strlen (s) + 1);
2775 strcpy (e->str, s);
2776 e->next = NULL;
2777 if (*tail)
2778 (*tail)->next = e;
2779 else
2780 *head = e;
2781 *tail = e;
2784 /* Copies the macro def or undef CPP_DEF and saves the copy
2785 in a list. IS_DEF is a flag indicating if CPP_DEF represents
2786 a -D or -U. */
2788 void
2789 coverage_note_define (const char *cpp_def, bool is_def)
2791 char *s = XNEWVEC (char, strlen (cpp_def) + 2);
2792 s[0] = is_def ? 'D' : 'U';
2793 strcpy (s + 1, cpp_def);
2794 str_list_append (&cpp_defines_head, &cpp_defines_tail, s);
2795 num_cpp_defines++;
2798 /* Copies the -imacro/-include FILENAME and saves the copy in a list. */
2800 void
2801 coverage_note_include (const char *filename)
2803 str_list_append (&cpp_includes_head, &cpp_includes_tail, filename);
2804 num_cpp_includes++;
2807 /* Mark this module as containing asm statements. */
2809 void
2810 coverage_has_asm_stmt (void)
2812 has_asm_statement = flag_ripa_disallow_asm_modules;
2815 /* Write command line options to the .note section. */
2817 void
2818 write_opts_to_asm (void)
2820 size_t i;
2821 cpp_dir *quote_paths, *bracket_paths, *system_paths, *pdir;
2822 struct str_list *pdef, *pinc;
2823 int num_quote_paths = 0;
2824 int num_bracket_paths = 0;
2825 int num_system_paths = 0;
2827 get_include_chains (&quote_paths, &bracket_paths, &system_paths);
2829 /* Write quote_paths to ASM section. */
2830 switch_to_section (get_section (".gnu.switches.text.quote_paths",
2831 SECTION_DEBUG, NULL));
2832 for (pdir = quote_paths; pdir; pdir = pdir->next)
2834 if (pdir == bracket_paths)
2835 break;
2836 num_quote_paths++;
2838 dw2_asm_output_nstring (in_fnames[0], (size_t)-1, NULL);
2839 dw2_asm_output_data_uleb128 (num_quote_paths, NULL);
2840 for (pdir = quote_paths; pdir; pdir = pdir->next)
2842 if (pdir == bracket_paths)
2843 break;
2844 dw2_asm_output_nstring (pdir->name, (size_t)-1, NULL);
2847 /* Write bracket_paths to ASM section. */
2848 switch_to_section (get_section (".gnu.switches.text.bracket_paths",
2849 SECTION_DEBUG, NULL));
2850 for (pdir = bracket_paths; pdir; pdir = pdir->next)
2852 if (pdir == system_paths)
2853 break;
2854 num_bracket_paths++;
2856 dw2_asm_output_nstring (in_fnames[0], (size_t)-1, NULL);
2857 dw2_asm_output_data_uleb128 (num_bracket_paths, NULL);
2858 for (pdir = bracket_paths; pdir; pdir = pdir->next)
2860 if (pdir == system_paths)
2861 break;
2862 dw2_asm_output_nstring (pdir->name, (size_t)-1, NULL);
2865 /* Write system_paths to ASM section. */
2866 switch_to_section (get_section (".gnu.switches.text.system_paths",
2867 SECTION_DEBUG, NULL));
2868 for (pdir = system_paths; pdir; pdir = pdir->next)
2869 num_system_paths++;
2870 dw2_asm_output_nstring (in_fnames[0], (size_t)-1, NULL);
2871 dw2_asm_output_data_uleb128 (num_system_paths, NULL);
2872 for (pdir = system_paths; pdir; pdir = pdir->next)
2873 dw2_asm_output_nstring (pdir->name, (size_t)-1, NULL);
2875 /* Write cpp_defines to ASM section. */
2876 switch_to_section (get_section (".gnu.switches.text.cpp_defines",
2877 SECTION_DEBUG, NULL));
2878 dw2_asm_output_nstring (in_fnames[0], (size_t)-1, NULL);
2879 dw2_asm_output_data_uleb128 (num_cpp_defines, NULL);
2880 for (pdef = cpp_defines_head; pdef; pdef = pdef->next)
2881 dw2_asm_output_nstring (pdef->str, (size_t)-1, NULL);
2883 /* Write cpp_includes to ASM section. */
2884 switch_to_section (get_section (".gnu.switches.text.cpp_includes",
2885 SECTION_DEBUG, NULL));
2886 dw2_asm_output_nstring (in_fnames[0], (size_t)-1, NULL);
2887 dw2_asm_output_data_uleb128 (num_cpp_includes, NULL);
2888 for (pinc = cpp_includes_head; pinc; pinc = pinc->next)
2889 dw2_asm_output_nstring (pinc->str, (size_t)-1, NULL);
2891 /* Write cl_args to ASM section. */
2892 switch_to_section (get_section (".gnu.switches.text.cl_args",
2893 SECTION_DEBUG, NULL));
2894 dw2_asm_output_nstring (in_fnames[0], (size_t)-1, NULL);
2895 dw2_asm_output_data_uleb128 (num_lipo_cl_args, NULL);
2896 for (i = 0; i < num_lipo_cl_args; i++)
2897 dw2_asm_output_nstring (lipo_cl_args[i], (size_t)-1, NULL);
2899 #include "gt-coverage.h"