Turn off profile-use optimizations when profile is not available
[official-gcc.git] / gcc-4_9 / gcc / coverage.c
blobf360ee137dcef50c5d2f5d402125ac348388e8c8
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"
78 #include "auto-profile.h"
80 struct GTY((chain_next ("%h.next"))) coverage_data
82 struct coverage_data *next; /* next function */
83 unsigned ident; /* function ident */
84 unsigned lineno_checksum; /* function lineno checksum */
85 unsigned cfg_checksum; /* function cfg checksum */
86 tree fn_decl; /* the function decl */
87 tree ctr_vars[GCOV_COUNTERS]; /* counter variables. */
90 static bool profiling_enabled_p (void);
92 /* Linked list of -D/-U/-imacro/-include strings for a source module. */
93 struct str_list
95 char *str;
96 struct str_list *next;
99 /* Counts information for a function. */
100 typedef struct counts_entry
102 /* We hash by */
103 unsigned HOST_WIDEST_INT ident;
104 unsigned ctr;
106 /* Store */
107 unsigned lineno_checksum;
108 unsigned cfg_checksum;
109 gcov_type *counts;
110 struct gcov_ctr_summary summary;
112 /* hash_table support. */
113 typedef counts_entry value_type;
114 typedef counts_entry compare_type;
115 static inline hashval_t hash (const value_type *);
116 static int equal (const value_type *, const compare_type *);
117 static void remove (value_type *);
118 } counts_entry_t;
120 static GTY(()) struct coverage_data *functions_head = 0;
121 static struct coverage_data **functions_tail = &functions_head;
122 static unsigned no_coverage = 0;
124 /* Cumulative counter information for whole program. */
125 static unsigned prg_ctr_mask; /* Mask of counter types generated. */
127 /* Counter information for current function. */
128 static unsigned fn_ctr_mask; /* Mask of counters used. */
129 static GTY(()) tree fn_v_ctrs[GCOV_COUNTERS]; /* counter variables. */
130 static unsigned fn_n_ctrs[GCOV_COUNTERS]; /* Counters allocated. */
131 static unsigned fn_b_ctrs[GCOV_COUNTERS]; /* Allocation base. */
133 /* Coverage info VAR_DECL and function info type nodes. */
134 static GTY(()) tree gcov_info_var;
135 static GTY(()) tree gcov_fn_info_type;
136 static GTY(()) tree gcov_fn_info_ptr_type;
138 /* Name of the notes (gcno) output file. The "bbg" prefix is for
139 historical reasons, when the notes file contained only the
140 basic block graph notes.
141 If this is NULL we're not writing to the notes file. */
142 static char *bbg_file_name;
144 /* File stamp for notes file. */
145 static unsigned bbg_file_stamp;
147 /* Name of the count data (gcda) file. */
148 static char *da_file_name;
149 static char *da_base_file_name;
150 static char *main_input_file_name;
152 /* The names of merge functions for counters. */
153 #define STR(str) #str
154 #define DEF_GCOV_COUNTER(COUNTER, NAME, FN_TYPE) STR(__gcov_merge ## FN_TYPE),
155 static const char *const ctr_merge_functions[GCOV_COUNTERS] = {
156 #include "gcov-counter.def"
158 #undef DEF_GCOV_COUNTER
159 #undef STR
161 #define DEF_GCOV_COUNTER(COUNTER, NAME, FN_TYPE) NAME,
162 static const char *const ctr_names[GCOV_COUNTERS] = {
163 #include "gcov-counter.def"
165 #undef DEF_GCOV_COUNTER
167 /* True during the period that counts_hash is being rebuilt. */
168 static bool rebuilding_counts_hash = false;
170 struct gcov_module_info **module_infos = NULL;
172 /* List of -D/-U options. */
173 static struct str_list *cpp_defines_head = NULL, *cpp_defines_tail = NULL;
174 static unsigned num_cpp_defines = 0;
176 /* List of -imcaro/-include options. */
177 static struct str_list *cpp_includes_head = NULL, *cpp_includes_tail = NULL;
178 static unsigned num_cpp_includes = 0;
180 /* True if the current module has any asm statements. */
181 static bool has_asm_statement;
183 /* Forward declarations. */
184 static void read_counts_file (const char *, unsigned);
185 static tree build_var (tree, tree, int);
186 static void build_fn_info_type (tree, unsigned, tree);
187 static void build_info_type (tree, tree);
188 static tree build_fn_info (const struct coverage_data *, tree, tree);
189 static tree build_info (tree, tree);
190 static bool coverage_obj_init (void);
191 static vec<constructor_elt, va_gc> *coverage_obj_fn
192 (vec<constructor_elt, va_gc> *, tree, struct coverage_data const *);
193 static void coverage_obj_finish (vec<constructor_elt, va_gc> *);
194 static char * get_da_file_name (const char *);
195 static tree build_gcov_module_info_type (void);
197 /* Return the type node for gcov_type. */
199 tree
200 get_gcov_type (void)
202 enum machine_mode mode = smallest_mode_for_size (GCOV_TYPE_SIZE, MODE_INT);
203 return lang_hooks.types.type_for_mode (mode, false);
206 /* Return the type node for gcov_unsigned_t. */
208 tree
209 get_gcov_unsigned_t (void)
211 enum machine_mode mode = smallest_mode_for_size (32, MODE_INT);
212 return lang_hooks.types.type_for_mode (mode, true);
215 /* Return the type node for const char *. */
217 tree
218 get_const_string_type (void)
220 return build_pointer_type
221 (build_qualified_type (char_type_node, TYPE_QUAL_CONST));
224 inline hashval_t
225 counts_entry::hash (const value_type *entry)
227 return entry->ident * GCOV_COUNTERS + entry->ctr;
230 inline int
231 counts_entry::equal (const value_type *entry1,
232 const compare_type *entry2)
234 return entry1->ident == entry2->ident && entry1->ctr == entry2->ctr;
237 inline void
238 counts_entry::remove (value_type *entry)
240 /* When rebuilding counts_hash, we will reuse the entry. */
241 if (!rebuilding_counts_hash)
243 free (entry->counts);
244 free (entry);
248 /* Hash table of count data. */
249 static hash_table <counts_entry> counts_hash;
251 /* Returns true if MOD_ID is the id of the last source module. */
253 is_last_module (unsigned mod_id)
255 return (mod_id == module_infos[num_in_fnames - 1]->ident);
258 /* String hash function */
260 struct string_hasher
262 /* hash_table support. */
263 typedef char value_type;
264 typedef char compare_type;
265 static inline hashval_t hash (const value_type *);
266 static int equal (const value_type *, const compare_type *);
267 static void remove (value_type *) {};
270 hashval_t
271 string_hasher::hash (const char* s)
273 return htab_hash_string (s);
276 /* String equal function */
279 string_hasher::equal (const char *s1, const char *s2)
281 return !strcmp (s1, s2);
284 /* Command line option descriptor. */
286 struct opt_desc
288 const char *opt_str;
289 const char *opt_neg_str;
290 bool default_val; /* TODO better handling of default */
293 static struct opt_desc force_matching_cg_opts[] =
295 { "-fexceptions", "-fno-exceptions", true },
296 { "-fsized-delete", "-fno-sized-delete", false },
297 { "-frtti", "-fno-rtti", true },
298 { "-fstrict-aliasing", "-fno-strict-aliasing", true },
299 { "-fsigned-char", "-funsigned-char", true },
300 /* { "-fsigned-char", "-fno-signed-char", true },
301 { "-funsigned-char", "-fno-unsigned-char", false }, */
302 { "-ansi", "", false },
303 { NULL, NULL, false }
306 /* A helper function to check if OPTION_STRING is one of the codegen
307 options specified in FORCE_MATCHING_CG_ARGS. If yes, set the
308 corresponding entry in CG_ARG_VAL to the value of the option specified
309 in OPTION_STRING. */
311 static void
312 check_cg_opts (bool *cg_opt_val, const char *option_str)
314 unsigned int i;
315 for (i = 0; force_matching_cg_opts[i].opt_str; i++)
317 if (!strcmp (force_matching_cg_opts[i].opt_str, option_str))
318 cg_opt_val[i] = true;
319 else if (!strcmp (force_matching_cg_opts[i].opt_neg_str, option_str))
320 cg_opt_val[i] = false;
324 /* A helper function to check if CG_OPTS1 and CG_OPTS are identical. It returns
325 true if yes, false otherwise. */
327 static bool
328 has_incompatible_cg_opts (bool *cg_opts1, bool *cg_opts2, unsigned num_cg_opts)
330 unsigned i;
332 for (i = 0; i < num_cg_opts; i++)
334 if (cg_opts1[i] != cg_opts2[i])
335 return true;
338 return false;
341 /* Returns true if the command-line arguments stored in the given module-infos
342 are incompatible. */
343 bool
344 incompatible_cl_args (struct gcov_module_info* mod_info1,
345 struct gcov_module_info* mod_info2)
347 char **warning_opts1 = XNEWVEC (char *, mod_info1->num_cl_args);
348 char **warning_opts2 = XNEWVEC (char *, mod_info2->num_cl_args);
349 char **non_warning_opts1 = XNEWVEC (char *, mod_info1->num_cl_args);
350 char **non_warning_opts2 = XNEWVEC (char *, mod_info2->num_cl_args);
351 char *std_opts1 = NULL, *std_opts2 = NULL;
352 unsigned arch_isa1 = 0, arch_isa2 = 0;
353 unsigned int i, num_warning_opts1 = 0, num_warning_opts2 = 0;
354 unsigned int num_non_warning_opts1 = 0, num_non_warning_opts2 = 0;
355 bool warning_mismatch = false;
356 bool non_warning_mismatch = false;
357 hash_table <string_hasher> option_tab1, option_tab2;
358 unsigned int start_index1 = mod_info1->num_quote_paths
359 + mod_info1->num_bracket_paths + mod_info1->num_system_paths
360 + mod_info1->num_cpp_defines + mod_info1->num_cpp_includes;
361 unsigned int start_index2 = mod_info2->num_quote_paths
362 + mod_info2->num_bracket_paths + mod_info2->num_system_paths
363 + mod_info2->num_cpp_defines + mod_info2->num_cpp_includes;
365 bool *cg_opts1, *cg_opts2, has_any_incompatible_cg_opts, has_incompatible_std;
366 bool has_incompatible_arch_isa;
367 unsigned int num_cg_opts = 0;
369 for (i = 0; force_matching_cg_opts[i].opt_str; i++)
370 num_cg_opts++;
372 cg_opts1 = XCNEWVEC (bool, num_cg_opts);
373 cg_opts2 = XCNEWVEC (bool, num_cg_opts);
375 /* Initialize the array to default value */
376 for (i = 0; force_matching_cg_opts[i].opt_str; i++)
378 cg_opts1[i] = force_matching_cg_opts[i].default_val;
379 cg_opts2[i] = force_matching_cg_opts[i].default_val;
382 option_tab1.create (10);
383 option_tab2.create (10);
385 /* First, separate the warning and non-warning options. */
386 for (i = 0; i < mod_info1->num_cl_args; i++)
387 if (mod_info1->string_array[start_index1 + i][1] == 'W')
388 warning_opts1[num_warning_opts1++] =
389 mod_info1->string_array[start_index1 + i];
390 else
392 char **slot;
393 char *option_string = mod_info1->string_array[start_index1 + i];
395 check_cg_opts (cg_opts1, option_string);
396 if (strstr (option_string, "-std="))
397 std_opts1 = option_string;
399 if (!strncmp (option_string, "-m",2))
400 arch_isa1 = crc32_string (arch_isa1, option_string);
402 slot = option_tab1.find_slot (option_string, INSERT);
403 if (!*slot)
405 *slot = option_string;
406 non_warning_opts1[num_non_warning_opts1++] = option_string;
410 for (i = 0; i < mod_info2->num_cl_args; i++)
411 if (mod_info2->string_array[start_index2 + i][1] == 'W')
412 warning_opts2[num_warning_opts2++] =
413 mod_info2->string_array[start_index2 + i];
414 else
416 char **slot;
417 char *option_string = mod_info2->string_array[start_index2 + i];
419 check_cg_opts (cg_opts2, option_string);
420 if (strstr (option_string, "-std="))
421 std_opts2 = option_string;
423 if (!strncmp (option_string, "-m",2))
424 arch_isa2 = crc32_string (arch_isa2, option_string);
426 slot = option_tab2.find_slot (option_string, INSERT);
427 if (!*slot)
429 *slot = option_string;
430 non_warning_opts2[num_non_warning_opts2++] = option_string;
434 has_incompatible_std =
435 std_opts1 != std_opts2 && (std_opts1 == NULL || std_opts2 == NULL
436 || strcmp (std_opts1, std_opts2));
438 has_incompatible_arch_isa = (arch_isa1 != arch_isa2);
439 /* Compare warning options. If these mismatch, we emit a warning. */
440 if (num_warning_opts1 != num_warning_opts2)
441 warning_mismatch = true;
442 else
443 for (i = 0; i < num_warning_opts1 && !warning_mismatch; i++)
444 warning_mismatch = strcmp (warning_opts1[i], warning_opts2[i]) != 0;
446 /* Compare non-warning options. If these mismatch, we emit a warning, and if
447 -fripa-disallow-opt-mismatch is supplied, the two modules are also
448 incompatible. */
449 if (num_non_warning_opts1 != num_non_warning_opts2)
450 non_warning_mismatch = true;
451 else
452 for (i = 0; i < num_non_warning_opts1 && !non_warning_mismatch; i++)
453 non_warning_mismatch =
454 strcmp (non_warning_opts1[i], non_warning_opts2[i]) != 0;
456 if (warn_ripa_opt_mismatch && (warning_mismatch || non_warning_mismatch))
457 warning (OPT_Wripa_opt_mismatch, "command line arguments mismatch for %s "
458 "and %s", mod_info1->source_filename, mod_info2->source_filename);
460 if (warn_ripa_opt_mismatch && non_warning_mismatch && dump_enabled_p ())
462 dump_printf_loc (MSG_MISSED_OPTIMIZATION, UNKNOWN_LOCATION,
463 "Options for %s", mod_info1->source_filename);
464 for (i = 0; i < num_non_warning_opts1; i++)
465 dump_printf_loc (MSG_MISSED_OPTIMIZATION, UNKNOWN_LOCATION,
466 non_warning_opts1[i]);
467 dump_printf_loc (MSG_MISSED_OPTIMIZATION, UNKNOWN_LOCATION,
468 "Options for %s", mod_info2->source_filename);
469 for (i = 0; i < num_non_warning_opts2; i++)
470 dump_printf_loc (MSG_MISSED_OPTIMIZATION, UNKNOWN_LOCATION,
471 non_warning_opts2[i]);
474 has_any_incompatible_cg_opts
475 = has_incompatible_cg_opts (cg_opts1, cg_opts2, num_cg_opts);
477 XDELETEVEC (warning_opts1);
478 XDELETEVEC (warning_opts2);
479 XDELETEVEC (non_warning_opts1);
480 XDELETEVEC (non_warning_opts2);
481 XDELETEVEC (cg_opts1);
482 XDELETEVEC (cg_opts2);
483 option_tab1.dispose ();
484 option_tab2.dispose ();
485 return ((flag_ripa_disallow_opt_mismatch && non_warning_mismatch)
486 || has_any_incompatible_cg_opts || has_incompatible_std
487 || has_incompatible_arch_isa);
491 /* Support for module sorting based on user specfication. */
492 struct module_name_entry
494 typedef module_name_entry value_type;
495 typedef module_name_entry compare_type;
496 static inline hashval_t hash (const value_type *entry);
497 static inline int equal (const value_type *entry1, const compare_type *entry2);
498 static inline void remove (value_type *v);
500 const char *source_name;
501 int order;
504 /* Hash function for module name */
506 hashval_t
507 module_name_entry::hash (const value_type *s)
509 return htab_hash_string (s->source_name);
512 /* Delete function for module name */
514 void
515 module_name_entry::remove (value_type *entry)
517 /* XDELETE (entry->source_name); */
518 XDELETE (entry);
521 /* Equal function for module name */
524 module_name_entry::equal (const value_type *s1, const compare_type *s2)
526 return !strcmp (s1->source_name, s2->source_name);
529 static hash_table<module_name_entry> module_name_tab;
531 /* Comparison function for sorting module_infos array. */
533 static int
534 cmp_module_name_entry (const void *p1, const void *p2)
536 module_name_entry **slot1, **slot2;
537 module_name_entry *m_e1, *m_e2;
539 struct gcov_module_info *const *e1 = (struct gcov_module_info *const *) p1;
540 struct gcov_module_info *const *e2 = (struct gcov_module_info *const *) p2;
542 module_name_entry e;
543 e.source_name = (*e1)->source_filename;
544 slot1 = module_name_tab.find_slot (&e, NO_INSERT);
545 e.source_name = (*e2)->source_filename;
546 slot2 = module_name_tab.find_slot (&e, NO_INSERT);
548 if (!slot1 || !*slot1)
549 return 1;
551 if (!slot2 || !*slot2)
552 return -1;
554 gcc_assert (slot1 && *slot1 && slot2 && *slot2);
555 m_e1 = *slot1;
556 m_e2 = *slot2;
558 return m_e1->order - m_e2->order;
561 /* Comparison function for sorting fname array */
563 static int
564 cmp_fname_entry (const void *p1, const void *p2)
566 module_name_entry **slot1, **slot2;
567 module_name_entry *m_e1, *m_e2;
569 const char *const *e1 = (const char *const *) p1;
570 const char *const *e2 = (const char *const *) p2;
572 module_name_entry e;
574 e.source_name = *e1;
575 slot1 = module_name_tab.find_slot (&e, NO_INSERT);
576 e.source_name = *e2;
577 slot2 = module_name_tab.find_slot (&e, NO_INSERT);
579 if (!slot1 || !*slot1)
580 return 1;
582 if (!slot2 || !*slot2)
583 return -1;
585 gcc_assert (slot1 && *slot1 && slot2 && *slot2);
586 m_e1 = *slot1;
587 m_e2 = *slot2;
589 return m_e1->order - m_e2->order;
592 /* Reorder module group according to file IMPORTS_FILE */
594 static void
595 reorder_module_groups (const char *imports_file, unsigned max_group)
597 FILE *f;
598 int order = 0;
599 const int max_line_size = (1 << 16);
600 char line[max_line_size];
602 module_name_tab.create (20);
604 f = fopen (imports_file, "r");
605 if (!f)
606 error ("Can't open file %s", imports_file);
608 while (fgets (line, max_line_size, f))
610 size_t n = strlen (line);
611 gcc_assert (n < max_line_size - 1);
612 if (line[n - 1] == '\n')
613 line[n - 1] = '\0';
615 module_name_entry **slot;
616 module_name_entry *m_e = XCNEW (module_name_entry);
618 m_e->source_name = xstrdup (line);
619 m_e->order = order;
621 slot = module_name_tab.find_slot (m_e, INSERT);
622 gcc_assert (!*slot);
623 *slot = m_e;
625 order++;
628 /* Now do the sorting */
630 qsort (&module_infos[1], num_in_fnames - 1, sizeof (void *),
631 cmp_module_name_entry);
632 qsort (&in_fnames[1], num_in_fnames - 1, sizeof (void *),
633 cmp_fname_entry);
636 unsigned i;
638 for (i = 0; i < num_in_fnames; i++)
639 fprintf (stderr, "*** %s (%s)\n", in_fnames[i],
640 i < max_group ? "Kept":"Skipped");
642 for (i = 0; i < num_in_fnames; i++)
643 fprintf (stderr, "### %s (%s)\n", module_infos[i]->source_filename,
644 i < max_group ? "Kept":"Skipped");
648 if (num_in_fnames > max_group)
649 num_in_fnames = max_group;
651 module_name_tab.dispose ();
654 typedef struct {
655 unsigned int mod_id;
656 const char *mod_name;
657 } mod_id_to_name_t;
659 static vec<mod_id_to_name_t> *mod_names;
661 static void
662 record_module_name (unsigned int mod_id, const char *name)
664 mod_id_to_name_t t;
666 t.mod_id = mod_id;
667 t.mod_name = xstrdup (name);
668 if (!mod_names)
669 vec_alloc (mod_names, 10);
670 mod_names->safe_push (t);
673 /* Return the module name for module with MOD_ID. */
675 const char *
676 get_module_name (unsigned int mod_id)
678 size_t i;
679 mod_id_to_name_t *elt;
681 for (i = 0; mod_names->iterate (i, &elt); i++)
683 if (elt->mod_id == mod_id)
684 return elt->mod_name;
687 gcc_assert (0);
688 return NULL;
691 /* Read in the counts file, if available. DA_FILE_NAME is the
692 name of the gcda file, and MODULE_ID is the module id of the
693 associated source module. */
695 static void
696 read_counts_file (const char *da_file_name, unsigned module_id)
698 gcov_unsigned_t fn_ident = 0;
699 struct gcov_summary summary;
700 unsigned new_summary = 1;
701 gcov_unsigned_t tag;
702 int is_error = 0;
703 unsigned module_infos_read = 0;
704 struct pointer_set_t *modset = 0;
705 unsigned max_group = PARAM_VALUE (PARAM_MAX_LIPO_GROUP);
706 unsigned lineno_checksum = 0;
707 unsigned cfg_checksum = 0;
708 const char *imports_filename;
710 if (max_group == 0)
711 max_group = (unsigned) -1;
713 if (!gcov_open (da_file_name, 1))
715 if (PARAM_VALUE (PARAM_GCOV_DEBUG))
717 /* Try to find .gcda file in the current working dir. */
718 da_file_name = lbasename (da_file_name);
719 if (!gcov_open (da_file_name, 1))
720 return;
722 else
724 inform (input_location, "file %s not found, disabling profile use",
725 da_file_name);
726 set_profile_use_options (&global_options, &global_options_set,
727 false, true);
728 return;
732 if (!gcov_magic (gcov_read_unsigned (), GCOV_DATA_MAGIC))
734 warning (0, "%qs is not a gcov data file", da_file_name);
735 gcov_close ();
736 return;
738 else if ((tag = gcov_read_unsigned ()) != GCOV_VERSION)
740 char v[4], e[4];
742 GCOV_UNSIGNED2STRING (v, tag);
743 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
745 warning (0, "%qs is version %q.*s, expected version %q.*s",
746 da_file_name, 4, v, 4, e);
747 gcov_close ();
748 return;
751 /* Read the stamp, used for creating a generation count. */
752 tag = gcov_read_unsigned ();
753 bbg_file_stamp = crc32_unsigned (bbg_file_stamp, tag);
755 if (!counts_hash.is_created ())
756 counts_hash.create (10);
758 while ((tag = gcov_read_unsigned ()))
760 gcov_unsigned_t length;
761 gcov_position_t offset;
763 length = gcov_read_unsigned ();
764 offset = gcov_position ();
765 if (tag == GCOV_TAG_FUNCTION)
767 if (length)
769 fn_ident = gcov_read_unsigned ();
770 lineno_checksum = gcov_read_unsigned ();
771 cfg_checksum = gcov_read_unsigned ();
773 else
774 fn_ident = lineno_checksum = cfg_checksum = 0;
775 new_summary = 1;
777 else if (tag == GCOV_TAG_PROGRAM_SUMMARY)
779 struct gcov_summary sum;
780 unsigned ix;
782 if (new_summary)
783 memset (&summary, 0, sizeof (summary));
785 gcov_read_summary (&sum);
786 for (ix = 0; ix != GCOV_COUNTERS_SUMMABLE; ix++)
788 summary.ctrs[ix].runs += sum.ctrs[ix].runs;
789 summary.ctrs[ix].sum_all += sum.ctrs[ix].sum_all;
790 if (summary.ctrs[ix].run_max < sum.ctrs[ix].run_max)
791 summary.ctrs[ix].run_max = sum.ctrs[ix].run_max;
792 summary.ctrs[ix].sum_max += sum.ctrs[ix].sum_max;
794 if (new_summary)
795 memcpy (summary.ctrs[GCOV_COUNTER_ARCS].histogram,
796 sum.ctrs[GCOV_COUNTER_ARCS].histogram,
797 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
798 else
799 gcov_histogram_merge (summary.ctrs[GCOV_COUNTER_ARCS].histogram,
800 sum.ctrs[GCOV_COUNTER_ARCS].histogram);
801 new_summary = 0;
803 else if (GCOV_TAG_IS_COUNTER (tag) && fn_ident)
805 counts_entry_t **slot, *entry, elt;
806 unsigned n_counts = GCOV_TAG_COUNTER_NUM (length);
807 unsigned ix;
809 elt.ident = GEN_FUNC_GLOBAL_ID (module_id, fn_ident);
810 elt.ctr = GCOV_COUNTER_FOR_TAG (tag);
812 slot = counts_hash.find_slot (&elt, INSERT);
813 entry = *slot;
814 if (!entry)
816 *slot = entry = XCNEW (counts_entry_t);
817 entry->ident = elt.ident;
818 entry->ctr = elt.ctr;
819 entry->lineno_checksum = lineno_checksum;
820 entry->cfg_checksum = cfg_checksum;
821 if (elt.ctr < GCOV_COUNTERS_SUMMABLE)
822 entry->summary = summary.ctrs[elt.ctr];
823 entry->summary.num = n_counts;
824 entry->counts = XCNEWVEC (gcov_type, n_counts);
826 else if (entry->lineno_checksum != lineno_checksum
827 || entry->cfg_checksum != cfg_checksum)
829 error ("Profile data for function %u is corrupted", fn_ident);
830 error ("checksum is (%x,%x) instead of (%x,%x)",
831 entry->lineno_checksum, entry->cfg_checksum,
832 lineno_checksum, cfg_checksum);
833 counts_hash.dispose ();
834 break;
836 else if (entry->summary.num != n_counts)
838 error ("Profile data for function %u is corrupted", fn_ident);
839 error ("number of counters is %d instead of %d", entry->summary.num, n_counts);
840 counts_hash.dispose ();
841 break;
843 else if (elt.ctr >= GCOV_COUNTERS_SUMMABLE)
845 error ("cannot merge separate %s counters for function %u",
846 ctr_names[elt.ctr], fn_ident);
847 goto skip_merge;
849 else
851 entry->summary.runs += summary.ctrs[elt.ctr].runs;
852 entry->summary.sum_all += summary.ctrs[elt.ctr].sum_all;
853 if (entry->summary.run_max < summary.ctrs[elt.ctr].run_max)
854 entry->summary.run_max = summary.ctrs[elt.ctr].run_max;
855 entry->summary.sum_max += summary.ctrs[elt.ctr].sum_max;
857 for (ix = 0; ix != n_counts; ix++)
858 entry->counts[ix] += gcov_read_counter ();
859 skip_merge:;
861 /* Skip the MODULE_INFO records if not in dyn-ipa mode, or when reading
862 auxiliary modules. */
863 else if (tag == GCOV_TAG_MODULE_INFO && flag_dyn_ipa && !module_id)
865 struct gcov_module_info* mod_info;
866 size_t info_sz;
867 /* each string has at least 8 bytes, so MOD_INFO's
868 persistent length >= in core size. */
869 mod_info
870 = (struct gcov_module_info *) alloca ((length + 2)
871 * sizeof (gcov_unsigned_t));
872 gcov_read_module_info (mod_info, length);
873 info_sz = (sizeof (struct gcov_module_info) +
874 sizeof (void *) * (mod_info->num_quote_paths +
875 mod_info->num_bracket_paths +
876 mod_info->num_system_paths +
877 mod_info->num_cpp_defines +
878 mod_info->num_cpp_includes +
879 mod_info->num_cl_args));
880 /* The first MODULE_INFO record must be for the primary module. */
881 if (module_infos_read == 0)
883 gcc_assert (mod_info->is_primary && !modset);
884 module_infos_read++;
885 modset = pointer_set_create ();
886 pointer_set_insert (modset, (void *)(size_t)mod_info->ident);
887 primary_module_id = mod_info->ident;
888 include_all_aux = MODULE_INCLUDE_ALL_AUX_FLAG (mod_info);
889 module_infos = XCNEWVEC (struct gcov_module_info *, 1);
890 module_infos[0] = XCNEWVAR (struct gcov_module_info, info_sz);
891 memcpy (module_infos[0], mod_info, info_sz);
893 else
895 int fd;
896 char *aux_da_filename = get_da_file_name (mod_info->da_filename);
897 gcc_assert (!mod_info->is_primary);
898 if (pointer_set_insert (modset, (void *)(size_t)mod_info->ident))
900 if (dump_enabled_p ())
901 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
902 "Not importing %s: already imported",
903 mod_info->source_filename);
905 else if ((module_infos[0]->lang & GCOV_MODULE_LANG_MASK) !=
906 (mod_info->lang & GCOV_MODULE_LANG_MASK))
908 if (dump_enabled_p ())
909 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
910 "Not importing %s: source language"
911 " different from primary module's source"
912 " language",
913 mod_info->source_filename);
915 else if (module_infos_read == max_group
916 /* If reordering is specified, delay the cutoff
917 until after sorting. */
918 && !getenv ("LIPO_REORDER_GROUP"))
920 if (dump_enabled_p ())
921 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
922 "Not importing %s: maximum group size"
923 " reached", mod_info->source_filename);
925 else if (incompatible_cl_args (module_infos[0], mod_info))
927 if (dump_enabled_p ())
928 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
929 "Not importing %s: command-line"
930 " arguments not compatible with primary"
931 " module",
932 mod_info->source_filename);
934 else if ((fd = open (aux_da_filename, O_RDONLY)) < 0)
936 if (dump_enabled_p ())
937 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
938 "Not importing %s: couldn't open %s",
939 mod_info->source_filename,
940 aux_da_filename);
942 else if ((mod_info->lang & GCOV_MODULE_ASM_STMTS)
943 && flag_ripa_disallow_asm_modules)
945 if (dump_enabled_p ())
946 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
947 "Not importing %s: contains assembler"
948 " statements", mod_info->source_filename);
950 else if (mod_info->is_primary == false
951 && MODULE_EXPORTED_FLAG (mod_info) == false)
953 warning (0, "MODULE_ID=%d (%s) is an auxiliary module, "
954 "but export_bit is not set. \n",
955 mod_info->ident, mod_info->source_filename);
957 else
959 close (fd);
960 module_infos_read++;
961 add_input_filename (mod_info->source_filename);
962 module_infos = XRESIZEVEC (struct gcov_module_info *,
963 module_infos, num_in_fnames);
964 gcc_assert (num_in_fnames == module_infos_read);
965 module_infos[module_infos_read - 1]
966 = XCNEWVAR (struct gcov_module_info, info_sz);
967 memcpy (module_infos[module_infos_read - 1], mod_info,
968 info_sz);
972 record_module_name (mod_info->ident,
973 lbasename (mod_info->source_filename));
975 if (dump_enabled_p ())
977 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
978 "MODULE Id=%d, Is_Primary=%s,"
979 " Is_Exported=%s, Include_all=%s, Name=%s (%s)",
980 mod_info->ident, mod_info->is_primary?"yes":"no",
981 MODULE_EXPORTED_FLAG (mod_info)?"yes":"no",
982 MODULE_INCLUDE_ALL_AUX_FLAG (mod_info)?"yes"
983 :"no",
984 mod_info->source_filename,
985 mod_info->da_filename);
988 gcov_sync (offset, length);
989 if ((is_error = gcov_is_error ()))
991 error (is_error < 0 ? "%qs has overflowed" : "%qs is corrupted",
992 da_file_name);
993 counts_hash.dispose ();
994 break;
998 if ((imports_filename = getenv ("LIPO_REORDER_GROUP"))
999 && flag_dyn_ipa && !module_id)
1001 reorder_module_groups (imports_filename, max_group);
1002 if (module_infos_read != num_in_fnames)
1003 module_infos_read = num_in_fnames;
1006 /* TODO: profile based multiple module compilation does not work
1007 together with command line (-combine) based ipo -- add a nice
1008 warning and bail out instead of asserting. */
1010 if (modset)
1011 pointer_set_destroy (modset);
1012 gcc_assert (module_infos_read == 0
1013 || module_infos_read == num_in_fnames);
1015 if (flag_dyn_ipa)
1016 gcc_assert (primary_module_id && num_in_fnames >= 1);
1018 gcov_close ();
1021 /* Returns the coverage data entry for counter type COUNTER of function
1022 FUNC. EXPECTED is the number of expected counter entries. */
1024 static counts_entry_t *
1025 get_coverage_counts_entry (struct function *func, unsigned counter)
1027 counts_entry_t *entry, elt;
1029 if (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID))
1030 elt.ident = FUNC_DECL_GLOBAL_ID (func);
1031 else
1033 gcc_assert (coverage_node_map_initialized_p ());
1034 elt.ident = cgraph_get_node (func->decl)->profile_id;
1037 elt.ctr = counter;
1038 entry = counts_hash.find (&elt);
1040 return entry;
1043 /* Returns the counters for a particular tag. */
1045 gcov_type *
1046 get_coverage_counts (unsigned counter, unsigned expected,
1047 unsigned cfg_checksum, unsigned lineno_checksum,
1048 const struct gcov_ctr_summary **summary)
1050 counts_entry_t *entry;
1052 /* No hash table, no counts. */
1053 if (!counts_hash.is_created ())
1055 static int warned = 0;
1057 if (!warned++ && dump_enabled_p ())
1058 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
1059 (flag_guess_branch_prob
1060 ? "file %s not found, execution counts estimated\n"
1061 : "file %s not found, execution counts assumed to "
1062 "be zero\n"),
1063 da_file_name);
1064 return NULL;
1067 entry = get_coverage_counts_entry (cfun, counter);
1069 if (!entry || !entry->summary.num)
1071 if (!flag_dyn_ipa && dump_enabled_p ())
1072 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
1073 "no coverage for function %s found",
1074 IDENTIFIER_POINTER
1075 (DECL_ASSEMBLER_NAME (current_function_decl)));
1076 return NULL;
1079 if (entry->cfg_checksum != cfg_checksum
1080 || entry->summary.num != expected)
1082 static int warned = 0;
1083 bool warning_printed = false;
1084 tree id = DECL_ASSEMBLER_NAME (current_function_decl);
1086 warning_printed =
1087 warning_at (input_location, OPT_Wcoverage_mismatch,
1088 "the control flow of function %qE does not match "
1089 "its profile data (counter %qs)", id, ctr_names[counter]);
1090 if (warning_printed && dump_enabled_p ())
1092 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
1093 "use -Wno-error=coverage-mismatch to tolerate "
1094 "the mismatch but performance may drop if the "
1095 "function is hot\n");
1097 if (!seen_error ()
1098 && !warned++)
1100 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
1101 "coverage mismatch ignored\n");
1102 dump_printf (MSG_OPTIMIZED_LOCATIONS,
1103 flag_guess_branch_prob
1104 ? G_("execution counts estimated\n")
1105 : G_("execution counts assumed to be zero\n"));
1106 if (!flag_guess_branch_prob)
1107 dump_printf (MSG_OPTIMIZED_LOCATIONS,
1108 "this can result in poorly optimized code\n");
1112 return NULL;
1114 else if (entry->lineno_checksum != lineno_checksum)
1116 warning (OPT_Wripa_opt_mismatch,
1117 "Source location for function %qE have changed,"
1118 " the profile data may be out of date",
1119 DECL_ASSEMBLER_NAME (current_function_decl));
1122 if (summary)
1123 *summary = &entry->summary;
1125 return entry->counts;
1128 /* Returns the coverage data entry for counter type COUNTER of function
1129 FUNC. On return, *N_COUNTS is set to the number of entries in the counter. */
1131 gcov_type *
1132 get_coverage_counts_no_warn (struct function *f, unsigned counter, unsigned *n_counts)
1134 counts_entry_t *entry, elt;
1136 /* No hash table, no counts. */
1137 if (!counts_hash.is_created () || !f)
1138 return NULL;
1140 if (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID))
1141 elt.ident = FUNC_DECL_GLOBAL_ID (f);
1142 else
1144 gcc_assert (coverage_node_map_initialized_p ());
1145 elt.ident = cgraph_get_node (f->decl)->profile_id;
1147 elt.ctr = counter;
1148 entry = counts_hash.find (&elt);
1149 if (!entry)
1150 return NULL;
1152 *n_counts = entry->summary.num;
1153 return entry->counts;
1156 /* Allocate NUM counters of type COUNTER. Returns nonzero if the
1157 allocation succeeded. */
1160 coverage_counter_alloc (unsigned counter, unsigned num)
1162 if (no_coverage)
1163 return 0;
1165 if (!num)
1166 return 1;
1168 if (!fn_v_ctrs[counter])
1170 tree array_type = build_array_type (get_gcov_type (), NULL_TREE);
1172 fn_v_ctrs[counter]
1173 = build_var (current_function_decl, array_type, counter);
1176 fn_b_ctrs[counter] = fn_n_ctrs[counter];
1177 fn_n_ctrs[counter] += num;
1179 fn_ctr_mask |= 1 << counter;
1180 return 1;
1183 /* Generate a tree to access COUNTER NO. */
1185 tree
1186 tree_coverage_counter_ref (unsigned counter, unsigned no)
1188 tree gcov_type_node = get_gcov_type ();
1190 gcc_assert (no < fn_n_ctrs[counter] - fn_b_ctrs[counter]);
1192 no += fn_b_ctrs[counter];
1194 /* "no" here is an array index, scaled to bytes later. */
1195 return build4 (ARRAY_REF, gcov_type_node, fn_v_ctrs[counter],
1196 build_int_cst (integer_type_node, no), NULL, NULL);
1199 /* Generate a tree to access the address of COUNTER NO. */
1201 tree
1202 tree_coverage_counter_addr (unsigned counter, unsigned no)
1204 tree gcov_type_node = get_gcov_type ();
1206 gcc_assert (no < fn_n_ctrs[counter] - fn_b_ctrs[counter]);
1207 no += fn_b_ctrs[counter];
1209 /* "no" here is an array index, scaled to bytes later. */
1210 return build_fold_addr_expr (build4 (ARRAY_REF, gcov_type_node,
1211 fn_v_ctrs[counter],
1212 build_int_cst (integer_type_node, no),
1213 NULL, NULL));
1217 /* Generate a crc32 of a string with specified STR_LEN when it's not 0.
1218 Non-zero STR_LEN should only be seen in LIPO mode. */
1220 static unsigned
1221 crc32_string_1 (unsigned chksum, const char *string, unsigned str_len)
1223 char *dup;
1225 if (!L_IPO_COMP_MODE || str_len == 0)
1226 return crc32_string (chksum, string);
1228 gcc_assert (str_len > 0 && str_len < strlen (string));
1229 dup = xstrdup (string);
1230 dup[str_len] = 0;
1231 chksum = crc32_string (chksum, dup);
1232 free (dup);
1234 return chksum;
1237 /* Generate a checksum for a string. CHKSUM is the current
1238 checksum. */
1240 static unsigned
1241 coverage_checksum_string (unsigned chksum, const char *string)
1243 int i;
1244 char *dup = NULL;
1245 unsigned lipo_orig_str_len = 0;
1247 /* Strip out the ending ".cmo.[0-9]*" string from function
1248 name. Otherwise we will have lineno checksum mismatch. */
1249 if (L_IPO_COMP_MODE)
1251 int len;
1253 i = len = strlen (string);
1254 while (i--)
1255 if ((string[i] < '0' || string[i] > '9'))
1256 break;
1257 if ((i > 5) && (i != len - 1))
1259 if (!strncmp (string + i - 4, ".cmo.", 5))
1260 lipo_orig_str_len = i - 4;
1265 /* Look for everything that looks if it were produced by
1266 get_file_function_name and zero out the second part
1267 that may result from flag_random_seed. This is not critical
1268 as the checksums are used only for sanity checking. */
1269 for (i = 0; string[i]; i++)
1271 int offset = 0;
1272 if (!strncmp (string + i, "_GLOBAL__N_", 11))
1273 offset = 11;
1274 if (!strncmp (string + i, "_GLOBAL__", 9))
1275 offset = 9;
1277 /* C++ namespaces do have scheme:
1278 _GLOBAL__N_<filename>_<wrongmagicnumber>_<magicnumber>functionname
1279 since filename might contain extra underscores there seems
1280 to be no better chance then walk all possible offsets looking
1281 for magicnumber. */
1282 if (offset)
1284 for (i = i + offset; string[i]; i++)
1285 if (string[i]=='_')
1287 int y;
1289 for (y = 1; y < 9; y++)
1290 if (!(string[i + y] >= '0' && string[i + y] <= '9')
1291 && !(string[i + y] >= 'A' && string[i + y] <= 'F'))
1292 break;
1293 if (y != 9 || string[i + 9] != '_')
1294 continue;
1295 for (y = 10; y < 18; y++)
1296 if (!(string[i + y] >= '0' && string[i + y] <= '9')
1297 && !(string[i + y] >= 'A' && string[i + y] <= 'F'))
1298 break;
1299 if (y != 18)
1300 continue;
1301 if (!dup)
1302 string = dup = xstrdup (string);
1303 for (y = 10; y < 18; y++)
1304 dup[i + y] = '0';
1306 break;
1310 chksum = crc32_string_1 (chksum, string, lipo_orig_str_len);
1311 if (dup)
1312 free (dup);
1314 return chksum;
1317 /* Compute checksum for the current function. We generate a CRC32. */
1319 unsigned
1320 coverage_compute_lineno_checksum (void)
1322 tree name;
1323 expanded_location xloc
1324 = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
1325 unsigned chksum = xloc.line;
1326 const char *pathless_filename = xloc.file;
1327 int i;
1328 for (i = strlen (xloc.file); i >= 0; i--)
1329 if (IS_DIR_SEPARATOR (pathless_filename[i]))
1331 pathless_filename += i + 1;
1332 break;
1335 chksum = coverage_checksum_string (chksum, pathless_filename);
1337 /* Note: it is a bad design that C++ FE associate the convertion function type
1338 with the name of the decl. This leads to cross contamination between different
1339 conversion operators in different modules (If conv_type_names map is cleared
1340 at the end of parsing of each module).
1342 For LIPO always use the full mangled name to help disambiguate different
1343 template instantiations. This is important for LIPO because we use the
1344 checksums to identify matching copies of the same COMDAT to handle
1345 missing profiles in the copies not selected by the linker, and to update
1346 indirect call profiles when the target COMDAT is a copy that is not
1347 in the module group. */
1348 if (flag_dyn_ipa)
1349 name = DECL_ASSEMBLER_NAME (current_function_decl);
1350 else
1351 name = DECL_NAME (current_function_decl);
1353 chksum = coverage_checksum_string
1354 (chksum, IDENTIFIER_POINTER (name));
1356 return chksum;
1359 /* Compute profile ID. This is better to be unique in whole program. */
1361 unsigned
1362 coverage_compute_profile_id (struct cgraph_node *n)
1364 expanded_location xloc
1365 = expand_location (DECL_SOURCE_LOCATION (n->decl));
1366 bool use_name_only = (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID) == 0);
1367 unsigned chksum = (use_name_only ? 0 : xloc.line);
1369 chksum = coverage_checksum_string (chksum, xloc.file);
1370 chksum = coverage_checksum_string
1371 (chksum, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n->decl)));
1372 if (!use_name_only && first_global_object_name)
1373 chksum = coverage_checksum_string
1374 (chksum, first_global_object_name);
1375 chksum = coverage_checksum_string
1376 (chksum, aux_base_name);
1378 /* Non-negative integers are hopefully small enough to fit in all targets. */
1379 return chksum & 0x7fffffff;
1382 /* Compute cfg checksum for the current function.
1383 The checksum is calculated carefully so that
1384 source code changes that doesn't affect the control flow graph
1385 won't change the checksum.
1386 This is to make the profile data useable across source code change.
1387 The downside of this is that the compiler may use potentially
1388 wrong profile data - that the source code change has non-trivial impact
1389 on the validity of profile data (e.g. the reversed condition)
1390 but the compiler won't detect the change and use the wrong profile data. */
1392 unsigned
1393 coverage_compute_cfg_checksum (void)
1395 basic_block bb;
1396 unsigned chksum = n_basic_blocks_for_fn (cfun);
1398 FOR_EACH_BB_FN (bb, cfun)
1400 edge e;
1401 edge_iterator ei;
1402 chksum = crc32_byte (chksum, bb->index);
1403 FOR_EACH_EDGE (e, ei, bb->succs)
1405 chksum = crc32_byte (chksum, e->dest->index);
1409 return chksum;
1412 /* Begin output to the notes file for the current function.
1413 Writes the function header. Returns nonzero if data should be output. */
1416 coverage_begin_function (unsigned lineno_checksum, unsigned cfg_checksum)
1418 expanded_location xloc;
1419 unsigned long offset;
1421 /* We don't need to output .gcno file unless we're under -ftest-coverage
1422 (e.g. -fprofile-arcs/generate/use don't need .gcno to work). */
1423 if (no_coverage || !bbg_file_name)
1424 return 0;
1426 xloc = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
1428 /* Announce function */
1429 offset = gcov_write_tag (GCOV_TAG_FUNCTION);
1430 if (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID))
1431 gcov_write_unsigned (FUNC_DECL_FUNC_ID (cfun));
1432 else
1434 gcc_assert (coverage_node_map_initialized_p ());
1435 gcov_write_unsigned (
1436 cgraph_get_node (current_function_decl)->profile_id);
1439 gcov_write_unsigned (lineno_checksum);
1440 gcov_write_unsigned (cfg_checksum);
1441 gcov_write_string (IDENTIFIER_POINTER
1442 (DECL_ASSEMBLER_NAME (current_function_decl)));
1443 gcov_write_string (xloc.file);
1444 gcov_write_unsigned (xloc.line);
1445 gcov_write_length (offset);
1447 return !gcov_is_error ();
1450 /* Finish coverage data for the current function. Verify no output
1451 error has occurred. Save function coverage counts. */
1453 void
1454 coverage_end_function (unsigned lineno_checksum, unsigned cfg_checksum)
1456 unsigned i;
1458 if (bbg_file_name && gcov_is_error ())
1460 warning (0, "error writing %qs", bbg_file_name);
1461 unlink (bbg_file_name);
1462 bbg_file_name = NULL;
1465 if (fn_ctr_mask)
1467 struct coverage_data *item = 0;
1469 /* If the function is extern (i.e. extern inline), then we won't
1470 be outputting it, so don't chain it onto the function
1471 list. */
1472 if (!DECL_EXTERNAL (current_function_decl))
1474 item = ggc_alloc_coverage_data ();
1476 if (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID))
1477 item->ident = FUNC_DECL_FUNC_ID (cfun);
1478 else
1480 if (flag_dyn_ipa)
1481 error ("param=profile-func-internal-id=0 is not"
1482 " supported in LIPO mode. ");
1483 gcc_assert (coverage_node_map_initialized_p ());
1484 item->ident = cgraph_get_node (cfun->decl)->profile_id;
1487 item->lineno_checksum = lineno_checksum;
1488 item->cfg_checksum = cfg_checksum;
1490 item->fn_decl = current_function_decl;
1491 item->next = 0;
1492 *functions_tail = item;
1493 functions_tail = &item->next;
1496 for (i = 0; i != GCOV_COUNTERS; i++)
1498 tree var = fn_v_ctrs[i];
1500 if (item)
1501 item->ctr_vars[i] = var;
1502 if (var)
1504 tree array_type = build_index_type (size_int (fn_n_ctrs[i] - 1));
1505 array_type = build_array_type (get_gcov_type (), array_type);
1506 TREE_TYPE (var) = array_type;
1507 DECL_SIZE (var) = TYPE_SIZE (array_type);
1508 DECL_SIZE_UNIT (var) = TYPE_SIZE_UNIT (array_type);
1509 varpool_finalize_decl (var);
1512 fn_b_ctrs[i] = fn_n_ctrs[i] = 0;
1513 fn_v_ctrs[i] = NULL_TREE;
1515 prg_ctr_mask |= fn_ctr_mask;
1516 fn_ctr_mask = 0;
1520 /* True if a function entry corresponding to the given FN_IDENT
1521 is present in the coverage internal data structures. */
1523 bool
1524 coverage_function_present (unsigned fn_ident)
1526 struct coverage_data *item = functions_head;
1527 while (item && item->ident != fn_ident)
1528 item = item->next;
1529 return item != NULL;
1532 /* Update function and program direct-call coverage counts. */
1534 void
1535 coverage_dc_end_function (void)
1537 tree var;
1539 if (fn_ctr_mask)
1541 const unsigned idx = GCOV_COUNTER_DIRECT_CALL;
1542 struct coverage_data *item = functions_head;
1543 while (item && item->ident != (unsigned) FUNC_DECL_FUNC_ID (cfun))
1544 item = item->next;
1546 /* If a matching function entry hasn't been found, either this function
1547 has had no coverage counts added in the profile pass, or this
1548 is a new function (function versioning, etc). Create a new entry. */
1549 if (!item)
1551 int cnt;
1553 item = ggc_alloc_coverage_data ();
1554 *functions_tail = item;
1555 functions_tail = &item->next;
1556 item->next = 0;
1557 item->ident = FUNC_DECL_FUNC_ID (cfun);
1558 item->fn_decl = current_function_decl;
1559 item->lineno_checksum = coverage_compute_lineno_checksum ();
1560 item->cfg_checksum = coverage_compute_cfg_checksum ();
1561 for (cnt = 0; cnt < GCOV_COUNTERS; cnt++)
1562 item->ctr_vars[cnt] = NULL_TREE;
1565 var = fn_v_ctrs[idx];
1566 item->ctr_vars[idx] = var;
1567 if (var)
1569 tree array_type = build_index_type (size_int (fn_n_ctrs[idx] - 1));
1570 array_type = build_array_type (get_gcov_type (), array_type);
1571 TREE_TYPE (var) = array_type;
1572 DECL_SIZE (var) = TYPE_SIZE (array_type);
1573 DECL_SIZE_UNIT (var) = TYPE_SIZE_UNIT (array_type);
1574 varpool_finalize_decl (var);
1577 fn_n_ctrs[idx] = fn_b_ctrs[idx] = 0;
1578 fn_v_ctrs[idx] = NULL_TREE;
1579 prg_ctr_mask |= fn_ctr_mask;
1580 fn_ctr_mask = 0;
1584 /* Build a coverage variable of TYPE for function FN_DECL. If COUNTER
1585 >= 0 it is a counter array, otherwise it is the function structure. */
1587 static tree
1588 build_var (tree fn_decl, tree type, int counter)
1590 tree var = build_decl (BUILTINS_LOCATION, VAR_DECL, NULL_TREE, type);
1591 const char *fn_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fn_decl));
1592 char *buf;
1593 size_t fn_name_len, len;
1595 fn_name = targetm.strip_name_encoding (fn_name);
1596 fn_name_len = strlen (fn_name);
1597 buf = XALLOCAVEC (char, fn_name_len + 8 + sizeof (int) * 3);
1599 if (counter < 0)
1600 strcpy (buf, "__gcov__");
1601 else
1602 sprintf (buf, "__gcov%u_", counter);
1603 len = strlen (buf);
1604 #ifndef NO_DOT_IN_LABEL
1605 buf[len - 1] = '.';
1606 #elif !defined NO_DOLLAR_IN_LABEL
1607 buf[len - 1] = '$';
1608 #endif
1609 memcpy (buf + len, fn_name, fn_name_len + 1);
1610 DECL_NAME (var) = get_identifier (buf);
1611 TREE_STATIC (var) = 1;
1612 TREE_ADDRESSABLE (var) = 1;
1613 DECL_NONALIASED (var) = 1;
1614 DECL_ALIGN (var) = TYPE_ALIGN (type);
1616 return var;
1619 /* Creates the gcov_fn_info RECORD_TYPE. */
1621 static void
1622 build_fn_info_type (tree type, unsigned counters, tree gcov_info_type)
1624 tree ctr_info = lang_hooks.types.make_type (RECORD_TYPE);
1625 tree field, fields;
1626 tree array_type;
1628 gcc_assert (counters);
1630 /* ctr_info::num */
1631 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1632 get_gcov_unsigned_t ());
1633 fields = field;
1635 /* ctr_info::values */
1636 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1637 build_pointer_type (get_gcov_type ()));
1638 DECL_CHAIN (field) = fields;
1639 fields = field;
1641 finish_builtin_struct (ctr_info, "__gcov_ctr_info", fields, NULL_TREE);
1643 /* key */
1644 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1645 build_pointer_type (build_qualified_type
1646 (gcov_info_type, TYPE_QUAL_CONST)));
1647 fields = field;
1649 /* ident */
1650 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1651 get_gcov_unsigned_t ());
1652 DECL_CHAIN (field) = fields;
1653 fields = field;
1655 /* lineno_checksum */
1656 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1657 get_gcov_unsigned_t ());
1658 DECL_CHAIN (field) = fields;
1659 fields = field;
1661 /* cfg checksum */
1662 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1663 get_gcov_unsigned_t ());
1664 DECL_CHAIN (field) = fields;
1665 fields = field;
1667 array_type = build_index_type (size_int (counters - 1));
1668 array_type = build_array_type (ctr_info, array_type);
1670 /* counters */
1671 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE, array_type);
1672 DECL_CHAIN (field) = fields;
1673 fields = field;
1675 finish_builtin_struct (type, "__gcov_fn_info", fields, NULL_TREE);
1678 /* Returns a CONSTRUCTOR for a gcov_fn_info. DATA is
1679 the coverage data for the function and TYPE is the gcov_fn_info
1680 RECORD_TYPE. KEY is the object file key. */
1682 static tree
1683 build_fn_info (const struct coverage_data *data, tree type, tree key)
1685 tree fields = TYPE_FIELDS (type);
1686 tree ctr_type;
1687 unsigned ix;
1688 vec<constructor_elt, va_gc> *v1 = NULL;
1689 vec<constructor_elt, va_gc> *v2 = NULL;
1691 /* key */
1692 CONSTRUCTOR_APPEND_ELT (v1, fields,
1693 build1 (ADDR_EXPR, TREE_TYPE (fields), key));
1694 fields = DECL_CHAIN (fields);
1696 /* ident */
1697 CONSTRUCTOR_APPEND_ELT (v1, fields,
1698 build_int_cstu (get_gcov_unsigned_t (),
1699 data->ident));
1700 fields = DECL_CHAIN (fields);
1702 /* lineno_checksum */
1703 CONSTRUCTOR_APPEND_ELT (v1, fields,
1704 build_int_cstu (get_gcov_unsigned_t (),
1705 data->lineno_checksum));
1706 fields = DECL_CHAIN (fields);
1708 /* cfg_checksum */
1709 CONSTRUCTOR_APPEND_ELT (v1, fields,
1710 build_int_cstu (get_gcov_unsigned_t (),
1711 data->cfg_checksum));
1712 fields = DECL_CHAIN (fields);
1714 /* counters */
1715 ctr_type = TREE_TYPE (TREE_TYPE (fields));
1716 for (ix = 0; ix != GCOV_COUNTERS; ix++)
1717 if (prg_ctr_mask & (1 << ix))
1719 vec<constructor_elt, va_gc> *ctr = NULL;
1720 tree var = data->ctr_vars[ix];
1721 unsigned count = 0;
1723 if (var)
1724 count
1725 = tree_to_shwi (TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (var))))
1726 + 1;
1728 CONSTRUCTOR_APPEND_ELT (ctr, TYPE_FIELDS (ctr_type),
1729 build_int_cstu (get_gcov_unsigned_t (),
1730 count));
1732 if (var)
1733 CONSTRUCTOR_APPEND_ELT (ctr, DECL_CHAIN (TYPE_FIELDS (ctr_type)),
1734 build_fold_addr_expr (var));
1736 CONSTRUCTOR_APPEND_ELT (v2, NULL, build_constructor (ctr_type, ctr));
1738 /* In LIPO mode, coverage_finish is called late when pruning can not be
1739 * done, so we need to force emitting counter variables even for
1740 * eliminated functions to avoid unsat. */
1741 if (flag_dyn_ipa && var)
1742 varpool_finalize_decl (var);
1745 CONSTRUCTOR_APPEND_ELT (v1, fields,
1746 build_constructor (TREE_TYPE (fields), v2));
1748 return build_constructor (type, v1);
1751 /* Create gcov_info struct. TYPE is the incomplete RECORD_TYPE to be
1752 completed, and FN_INFO_PTR_TYPE is a pointer to the function info type. */
1754 static void
1755 build_info_type (tree type, tree fn_info_ptr_type)
1757 tree field, fields = NULL_TREE;
1758 tree merge_fn_type, mod_type;
1760 /* Version ident */
1761 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1762 get_gcov_unsigned_t ());
1763 DECL_CHAIN (field) = fields;
1764 fields = field;
1766 /* mod_info */
1767 mod_type = build_gcov_module_info_type ();
1768 mod_type = build_pointer_type (mod_type);
1769 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE, mod_type);
1770 DECL_CHAIN (field) = fields;
1771 fields = field;
1773 /* next pointer */
1774 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1775 build_pointer_type (build_qualified_type
1776 (type, TYPE_QUAL_CONST)));
1777 DECL_CHAIN (field) = fields;
1778 fields = field;
1780 /* stamp */
1781 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1782 get_gcov_unsigned_t ());
1783 DECL_CHAIN (field) = fields;
1784 fields = field;
1786 /* Filename */
1787 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1788 build_pointer_type (build_qualified_type
1789 (char_type_node, TYPE_QUAL_CONST)));
1790 DECL_CHAIN (field) = fields;
1791 fields = field;
1793 /* eof_pos */
1794 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1795 NULL_TREE, get_gcov_unsigned_t ());
1796 DECL_CHAIN (field) = fields;
1797 fields = field;
1799 /* merge fn array */
1800 merge_fn_type
1801 = build_function_type_list (void_type_node,
1802 build_pointer_type (get_gcov_type ()),
1803 get_gcov_unsigned_t (), NULL_TREE);
1804 merge_fn_type
1805 = build_array_type (build_pointer_type (merge_fn_type),
1806 build_index_type (size_int (GCOV_COUNTERS - 1)));
1807 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1808 merge_fn_type);
1809 DECL_CHAIN (field) = fields;
1810 fields = field;
1812 /* n_functions */
1813 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1814 get_gcov_unsigned_t ());
1815 DECL_CHAIN (field) = fields;
1816 fields = field;
1818 /* function_info pointer pointer */
1819 fn_info_ptr_type = build_pointer_type
1820 (build_qualified_type (fn_info_ptr_type, TYPE_QUAL_CONST));
1821 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1822 fn_info_ptr_type);
1823 DECL_CHAIN (field) = fields;
1824 fields = field;
1826 finish_builtin_struct (type, "__gcov_info", fields, NULL_TREE);
1829 /* Compute an array (tree) of include path strings. STRING_TYPE is
1830 the path string type, INC_PATH_VALUE is the initial value of the
1831 path array, PATHS gives raw path string values, and NUM is the
1832 number of paths. */
1834 static void
1835 build_inc_path_array_value (tree string_type, vec<constructor_elt, va_gc> **v,
1836 cpp_dir *paths, int num)
1838 int i;
1839 cpp_dir *pdir;
1840 for (i = 0, pdir = paths; i < num; pdir = pdir->next)
1842 const char *path_raw_string;
1843 int path_string_length;
1844 tree path_string;
1845 path_raw_string = pdir->name;
1846 path_string_length = strlen (path_raw_string);
1847 path_string = build_string (path_string_length + 1, path_raw_string);
1848 TREE_TYPE (path_string) = build_array_type
1849 (char_type_node, build_index_type
1850 (build_int_cst (NULL_TREE, path_string_length)));
1851 CONSTRUCTOR_APPEND_ELT (*v, NULL,
1852 build1 (ADDR_EXPR, string_type, path_string));
1853 i++;
1857 /* Compute an array (tree) of strings. STR_TYPE is the string type,
1858 STR_ARRAY_VALUE is the initial value of the string array, and HEAD gives
1859 the list of raw strings. */
1861 static void
1862 build_str_array_value (tree str_type, vec<constructor_elt, va_gc> **v,
1863 struct str_list *head)
1865 const char *raw_str;
1866 int str_length;
1867 while (head)
1869 tree str;
1870 raw_str = head->str;
1871 str_length = strlen (raw_str);
1872 str = build_string (str_length + 1, raw_str);
1873 TREE_TYPE (str) =
1874 build_array_type (char_type_node,
1875 build_index_type (build_int_cst (NULL_TREE,
1876 str_length)));
1877 CONSTRUCTOR_APPEND_ELT (*v, NULL,
1878 build1 (ADDR_EXPR, str_type, str));
1879 head = head->next;
1881 return;
1884 /* Compute an array (tree) of command-line argument strings. STRING_TYPE is
1885 the string type, CL_ARGS_VALUE is the initial value of the command-line
1886 args array. */
1888 static void
1889 build_cl_args_array_value (tree string_type, vec<constructor_elt, va_gc> **v)
1891 unsigned int i;
1893 for (i = 0; i < num_lipo_cl_args; i++)
1895 int arg_length = strlen (lipo_cl_args[i]);
1896 tree arg_string = build_string (arg_length + 1, lipo_cl_args[i]);
1897 TREE_TYPE (arg_string) =
1898 build_array_type (char_type_node,
1899 build_index_type (build_int_cst (NULL_TREE,
1900 arg_length)));
1901 CONSTRUCTOR_APPEND_ELT (*v, NULL,
1902 build1 (ADDR_EXPR, string_type, arg_string));
1904 return;
1907 /* Emit mapping between module name and function id to the function's
1908 assembler name, for use in correlating function idents in the gcda file
1909 with the function name. */
1911 void
1912 emit_function_name (void)
1914 fprintf (stderr, "Module %s FuncId %u Name %s\n",
1915 (L_IPO_COMP_MODE
1916 ? get_module_name (FUNC_DECL_MODULE_ID (cfun))
1917 : main_input_file_name),
1918 FUNC_DECL_FUNC_ID (cfun),
1919 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl)));
1922 /* Returns the type of the module info associated with the
1923 current source module being compiled. */
1925 static tree
1926 build_gcov_module_info_type (void)
1928 tree type, field, fields = NULL_TREE;
1929 tree string_type, index_type, string_array_type;
1931 cpp_dir *quote_paths, *bracket_paths, *system_paths, *pdir;
1932 int num_quote_paths = 0, num_bracket_paths = 0, num_system_paths = 0;
1934 type = lang_hooks.types.make_type (RECORD_TYPE);
1935 string_type = build_pointer_type (
1936 build_qualified_type (char_type_node,
1937 TYPE_QUAL_CONST));
1939 /* ident */
1940 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1941 NULL_TREE, get_gcov_unsigned_t ());
1942 DECL_CHAIN (field) = fields;
1943 fields = field;
1945 /* is_primary */
1946 /* We also overload this field to store a flag that indicates whether this
1947 module was built in regular FDO or LIPO mode (-fripa). When reading this
1948 field from a GCDA file, it should be used as the IS_PRIMARY flag. When
1949 reading this field from the binary's data section, it should be used
1950 as a FDO/LIPO flag. */
1951 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1952 NULL_TREE, get_gcov_unsigned_t ());
1953 DECL_CHAIN (field) = fields;
1954 fields = field;
1956 /* flags: is_exported and include_all_aux flag. */
1957 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1958 NULL_TREE, get_gcov_unsigned_t ());
1959 DECL_CHAIN (field) = fields;
1960 fields = field;
1962 /* lang field */
1963 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1964 NULL_TREE, get_gcov_unsigned_t ());
1965 DECL_CHAIN (field) = fields;
1966 fields = field;
1968 /* ggc_memory field */
1969 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1970 NULL_TREE, get_gcov_unsigned_t ());
1971 DECL_CHAIN (field) = fields;
1972 fields = field;
1974 /* da_filename */
1975 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1976 NULL_TREE, string_type);
1977 DECL_CHAIN (field) = fields;
1978 fields = field;
1980 /* Source name */
1981 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1982 NULL_TREE, string_type);
1983 DECL_CHAIN (field) = fields;
1984 fields = field;
1986 /* Num quote paths */
1987 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1988 NULL_TREE, get_gcov_unsigned_t ());
1989 DECL_CHAIN (field) = fields;
1990 fields = field;
1992 /* Num bracket paths */
1993 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1994 NULL_TREE, get_gcov_unsigned_t ());
1995 DECL_CHAIN (field) = fields;
1996 fields = field;
1998 /* Num system paths */
1999 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
2000 NULL_TREE, get_gcov_unsigned_t ());
2001 DECL_CHAIN (field) = fields;
2002 fields = field;
2004 /* Num -D/-U options. */
2005 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
2006 NULL_TREE, get_gcov_unsigned_t ());
2007 DECL_CHAIN (field) = fields;
2008 fields = field;
2010 /* Num -imacro/-include options. */
2011 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
2012 get_gcov_unsigned_t ());
2013 DECL_CHAIN (field) = fields;
2014 fields = field;
2016 /* Num command-line args. */
2017 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
2018 NULL_TREE, get_gcov_unsigned_t ());
2019 DECL_CHAIN (field) = fields;
2020 fields = field;
2022 get_include_chains (&quote_paths, &bracket_paths, &system_paths);
2023 for (pdir = quote_paths; pdir; pdir = pdir->next)
2025 if (pdir == bracket_paths)
2026 break;
2027 num_quote_paths++;
2029 for (pdir = bracket_paths; pdir; pdir = pdir->next)
2031 if (pdir == system_paths)
2032 break;
2033 num_bracket_paths++;
2035 for (pdir = system_paths; pdir; pdir = pdir->next)
2036 num_system_paths++;
2038 /* string array */
2039 index_type = build_index_type (build_int_cst (NULL_TREE,
2040 num_quote_paths +
2041 num_bracket_paths +
2042 num_system_paths +
2043 num_cpp_defines +
2044 num_cpp_includes +
2045 num_lipo_cl_args));
2047 string_array_type = build_array_type (string_type, index_type);
2048 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
2049 NULL_TREE, string_array_type);
2050 DECL_CHAIN (field) = fields;
2051 fields = field;
2053 finish_builtin_struct (type, "__gcov_module_info", fields, NULL_TREE);
2055 return type;
2058 /* Returns the value of the module info associated with the
2059 current source module being compiled. */
2061 static tree
2062 build_gcov_module_info_value (tree mod_type)
2064 tree info_fields, mod_info;
2065 tree value = NULL_TREE;
2066 int file_name_len;
2067 tree filename_string, string_array_type, string_type;
2068 cpp_dir *quote_paths, *bracket_paths, *system_paths, *pdir;
2069 int num_quote_paths = 0, num_bracket_paths = 0, num_system_paths = 0;
2070 unsigned lang;
2071 char name_buf[50];
2072 vec<constructor_elt,va_gc> *v = NULL, *path_v = NULL;
2074 info_fields = TYPE_FIELDS (mod_type);
2076 /* ident */
2077 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2078 build_int_cstu (get_gcov_unsigned_t (), 0));
2080 info_fields = DECL_CHAIN (info_fields);
2082 /* is_primary */
2083 /* We also overload this field to store a flag that indicates whether this
2084 module was built in regular FDO or LIPO mode (-fripa). When reading this
2085 field from a GCDA file, it should be used as the IS_PRIMARY flag. When
2086 reading this field from the binary's data section, it should be used
2087 as a FDO/LIPO flag. */
2088 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2089 build_int_cstu (get_gcov_unsigned_t (),
2090 flag_dyn_ipa ? 1 : 0));
2091 info_fields = DECL_CHAIN (info_fields);
2093 /* flags */
2094 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2095 build_int_cstu (get_gcov_unsigned_t (), 0));
2096 info_fields = DECL_CHAIN (info_fields);
2098 /* lang field */
2099 if (!strcmp (lang_hooks.name, "GNU C"))
2100 lang = GCOV_MODULE_C_LANG;
2101 else if (!strcmp (lang_hooks.name, "GNU C++"))
2102 lang = GCOV_MODULE_CPP_LANG;
2103 else
2104 lang = GCOV_MODULE_UNKNOWN_LANG;
2105 if (has_asm_statement)
2106 lang |= GCOV_MODULE_ASM_STMTS;
2108 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2109 build_int_cstu (get_gcov_unsigned_t (), lang));
2110 info_fields = DECL_CHAIN (info_fields);
2112 /* ggc_memory field */
2113 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2114 build_int_cstu (get_gcov_unsigned_t (), ggc_total_memory));
2115 info_fields = DECL_CHAIN (info_fields);
2117 /* da_filename */
2119 string_type = TREE_TYPE (info_fields);
2120 file_name_len = strlen (da_base_file_name);
2121 filename_string = build_string (file_name_len + 1, da_base_file_name);
2122 TREE_TYPE (filename_string) = build_array_type
2123 (char_type_node, build_index_type
2124 (build_int_cst (NULL_TREE, file_name_len)));
2125 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2126 build1 (ADDR_EXPR, string_type, filename_string));
2127 info_fields = DECL_CHAIN (info_fields);
2129 /* Source name */
2131 file_name_len = strlen (main_input_file_name);
2132 filename_string = build_string (file_name_len + 1, main_input_file_name);
2133 TREE_TYPE (filename_string) = build_array_type
2134 (char_type_node, build_index_type
2135 (build_int_cst (NULL_TREE, file_name_len)));
2136 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2137 build1 (ADDR_EXPR, string_type, filename_string));
2138 info_fields = DECL_CHAIN (info_fields);
2140 get_include_chains (&quote_paths, &bracket_paths, &system_paths);
2141 for (pdir = quote_paths; pdir; pdir = pdir->next)
2143 if (pdir == bracket_paths)
2144 break;
2145 num_quote_paths++;
2147 for (pdir = bracket_paths; pdir; pdir = pdir->next)
2149 if (pdir == system_paths)
2150 break;
2151 num_bracket_paths++;
2153 for (pdir = system_paths; pdir; pdir = pdir->next)
2154 num_system_paths++;
2156 /* Num quote paths */
2157 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2158 build_int_cstu (get_gcov_unsigned_t (),
2159 num_quote_paths));
2160 info_fields = DECL_CHAIN (info_fields);
2162 /* Num bracket paths */
2163 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2164 build_int_cstu (get_gcov_unsigned_t (),
2165 num_bracket_paths));
2166 info_fields = DECL_CHAIN (info_fields);
2168 /* Num system paths */
2169 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2170 build_int_cstu (get_gcov_unsigned_t (),
2171 num_system_paths));
2172 info_fields = DECL_CHAIN (info_fields);
2174 /* Num -D/-U options. */
2175 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2176 build_int_cstu (get_gcov_unsigned_t (),
2177 num_cpp_defines));
2178 info_fields = DECL_CHAIN (info_fields);
2180 /* Num -imacro/-include options. */
2181 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2182 build_int_cstu (get_gcov_unsigned_t (),
2183 num_cpp_includes));
2184 info_fields = DECL_CHAIN (info_fields);
2186 /* Num command-line args. */
2187 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2188 build_int_cstu (get_gcov_unsigned_t (),
2189 num_lipo_cl_args));
2190 info_fields = DECL_CHAIN (info_fields);
2192 /* string array */
2193 string_array_type = TREE_TYPE (info_fields);
2194 build_inc_path_array_value (string_type, &path_v,
2195 quote_paths, num_quote_paths);
2196 build_inc_path_array_value (string_type, &path_v,
2197 bracket_paths, num_bracket_paths);
2198 build_inc_path_array_value (string_type, &path_v,
2199 system_paths, num_system_paths);
2200 build_str_array_value (string_type, &path_v,
2201 cpp_defines_head);
2202 build_str_array_value (string_type, &path_v,
2203 cpp_includes_head);
2204 build_cl_args_array_value (string_type, &path_v);
2205 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2206 build_constructor (string_array_type, path_v));
2207 info_fields = DECL_CHAIN (info_fields);
2209 gcc_assert (!info_fields);
2210 value = build_constructor (mod_type, v);
2212 mod_info = build_decl (BUILTINS_LOCATION, VAR_DECL,
2213 NULL_TREE, TREE_TYPE (value));
2214 TREE_STATIC (mod_info) = 1;
2215 ASM_GENERATE_INTERNAL_LABEL (name_buf, "MODINFO", 0);
2216 DECL_NAME (mod_info) = get_identifier (name_buf);
2217 DECL_INITIAL (mod_info) = value;
2219 /* Build structure. */
2220 varpool_finalize_decl (mod_info);
2222 return mod_info;
2225 /* Returns a CONSTRUCTOR for the gcov_info object. INFO_TYPE is the
2226 gcov_info structure type, FN_ARY is the array of pointers to
2227 function info objects. */
2229 static tree
2230 build_info (tree info_type, tree fn_ary)
2232 tree info_fields = TYPE_FIELDS (info_type);
2233 tree merge_fn_type, n_funcs;
2234 unsigned ix;
2235 tree mod_value = NULL_TREE;
2236 tree filename_string;
2237 int da_file_name_len;
2238 vec<constructor_elt, va_gc> *v1 = NULL;
2239 vec<constructor_elt, va_gc> *v2 = NULL;
2241 /* Version ident */
2242 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
2243 build_int_cstu (TREE_TYPE (info_fields),
2244 GCOV_VERSION));
2245 info_fields = DECL_CHAIN (info_fields);
2247 /* mod_info */
2248 mod_value = build_gcov_module_info_value (TREE_TYPE (TREE_TYPE (info_fields)));
2249 mod_value = build1 (ADDR_EXPR,
2250 build_pointer_type (TREE_TYPE (mod_value)),
2251 mod_value);
2252 CONSTRUCTOR_APPEND_ELT (v1, info_fields, mod_value);
2253 info_fields = DECL_CHAIN (info_fields);
2255 /* next -- NULL */
2256 CONSTRUCTOR_APPEND_ELT (v1, info_fields, null_pointer_node);
2257 info_fields = DECL_CHAIN (info_fields);
2259 /* stamp */
2260 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
2261 build_int_cstu (TREE_TYPE (info_fields),
2262 bbg_file_stamp));
2263 info_fields = DECL_CHAIN (info_fields);
2265 /* Filename */
2266 da_file_name_len = strlen (da_file_name);
2267 filename_string = build_string (da_file_name_len + 1, da_file_name);
2268 TREE_TYPE (filename_string) = build_array_type
2269 (char_type_node, build_index_type (size_int (da_file_name_len)));
2270 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
2271 build1 (ADDR_EXPR, TREE_TYPE (info_fields),
2272 filename_string));
2273 info_fields = DECL_CHAIN (info_fields);
2275 /* eof_pos */
2276 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
2277 build_int_cstu (TREE_TYPE (info_fields), 0));
2278 info_fields = DECL_CHAIN (info_fields);
2280 /* merge fn array -- NULL slots indicate unmeasured counters */
2281 merge_fn_type = TREE_TYPE (TREE_TYPE (info_fields));
2282 for (ix = 0; ix != GCOV_COUNTERS; ix++)
2284 tree ptr = null_pointer_node;
2286 if ((1u << ix) & prg_ctr_mask)
2288 tree merge_fn = build_decl (BUILTINS_LOCATION,
2289 FUNCTION_DECL,
2290 get_identifier (ctr_merge_functions[ix]),
2291 TREE_TYPE (merge_fn_type));
2292 DECL_EXTERNAL (merge_fn) = 1;
2293 TREE_PUBLIC (merge_fn) = 1;
2294 DECL_ARTIFICIAL (merge_fn) = 1;
2295 TREE_NOTHROW (merge_fn) = 1;
2296 /* Initialize assembler name so we can stream out. */
2297 DECL_ASSEMBLER_NAME (merge_fn);
2298 ptr = build1 (ADDR_EXPR, merge_fn_type, merge_fn);
2300 CONSTRUCTOR_APPEND_ELT (v2, NULL, ptr);
2302 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
2303 build_constructor (TREE_TYPE (info_fields), v2));
2304 info_fields = DECL_CHAIN (info_fields);
2306 /* n_functions */
2307 n_funcs = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (fn_ary)));
2308 n_funcs = fold_build2 (PLUS_EXPR, TREE_TYPE (info_fields),
2309 n_funcs, size_one_node);
2310 CONSTRUCTOR_APPEND_ELT (v1, info_fields, n_funcs);
2311 info_fields = DECL_CHAIN (info_fields);
2313 /* functions */
2314 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
2315 build1 (ADDR_EXPR, TREE_TYPE (info_fields), fn_ary));
2316 info_fields = DECL_CHAIN (info_fields);
2318 gcc_assert (!info_fields);
2319 return build_constructor (info_type, v1);
2322 /* Generate the constructor function to call __gcov_init. */
2324 static void
2325 build_init_ctor (tree gcov_info_type)
2327 tree ctor, stmt, init_fn;
2329 /* Build a decl for __gcov_init. */
2330 init_fn = build_pointer_type (gcov_info_type);
2331 init_fn = build_function_type_list (void_type_node, init_fn, NULL);
2332 init_fn = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
2333 get_identifier ("__gcov_init"), init_fn);
2334 TREE_PUBLIC (init_fn) = 1;
2335 DECL_EXTERNAL (init_fn) = 1;
2336 DECL_ASSEMBLER_NAME (init_fn);
2338 /* Generate a call to __gcov_init(&gcov_info). */
2339 ctor = NULL;
2340 stmt = build_fold_addr_expr (gcov_info_var);
2341 stmt = build_call_expr (init_fn, 1, stmt);
2342 append_to_statement_list (stmt, &ctor);
2344 /* Generate a constructor to run it. */
2345 cgraph_build_static_cdtor ('I', ctor, DEFAULT_INIT_PRIORITY);
2348 /* Create the gcov_info types and object. Generate the constructor
2349 function to call __gcov_init. Does not generate the initializer
2350 for the object. Returns TRUE if coverage data is being emitted. */
2352 static bool
2353 coverage_obj_init (void)
2355 tree gcov_info_type;
2356 unsigned n_counters = 0;
2357 unsigned ix;
2358 struct coverage_data *fn;
2359 struct coverage_data **fn_prev;
2360 char name_buf[32];
2362 no_coverage = 1; /* Disable any further coverage. */
2364 if (!prg_ctr_mask)
2365 return false;
2367 if (cgraph_dump_file)
2368 fprintf (cgraph_dump_file, "Using data file %s\n", da_file_name);
2370 /* Prune functions. */
2371 if (!flag_dyn_ipa)
2372 /* in lipo mode, coverage_finish is called when function struct is cleared,
2373 so pruning code here will skip all functions. */
2374 for (fn_prev = &functions_head; (fn = *fn_prev);)
2375 if (DECL_STRUCT_FUNCTION (fn->fn_decl))
2376 fn_prev = &fn->next;
2377 else
2378 /* The function is not being emitted, remove from list. */
2379 *fn_prev = fn->next;
2381 if (functions_head == NULL)
2382 return false;
2384 for (ix = 0; ix != GCOV_COUNTERS; ix++)
2385 if ((1u << ix) & prg_ctr_mask)
2386 n_counters++;
2388 /* Build the info and fn_info types. These are mutually recursive. */
2389 gcov_info_type = lang_hooks.types.make_type (RECORD_TYPE);
2390 gcov_fn_info_type = lang_hooks.types.make_type (RECORD_TYPE);
2391 gcov_fn_info_ptr_type = build_pointer_type
2392 (build_qualified_type (gcov_fn_info_type, TYPE_QUAL_CONST));
2393 build_fn_info_type (gcov_fn_info_type, n_counters, gcov_info_type);
2394 build_info_type (gcov_info_type, gcov_fn_info_ptr_type);
2396 /* Build the gcov info var, this is referred to in its own
2397 initializer. */
2398 gcov_info_var = build_decl (BUILTINS_LOCATION,
2399 VAR_DECL, NULL_TREE, gcov_info_type);
2400 TREE_STATIC (gcov_info_var) = 1;
2401 ASM_GENERATE_INTERNAL_LABEL (name_buf, "LPBX", 0);
2402 DECL_NAME (gcov_info_var) = get_identifier (name_buf);
2404 return true;
2407 /* Generate the coverage function info for FN and DATA. Append a
2408 pointer to that object to CTOR and return the appended CTOR. */
2410 static vec<constructor_elt, va_gc> *
2411 coverage_obj_fn (vec<constructor_elt, va_gc> *ctor, tree fn,
2412 struct coverage_data const *data)
2414 tree init = build_fn_info (data, gcov_fn_info_type, gcov_info_var);
2415 tree var = build_var (fn, gcov_fn_info_type, -1);
2417 DECL_INITIAL (var) = init;
2418 varpool_finalize_decl (var);
2420 CONSTRUCTOR_APPEND_ELT (ctor, NULL,
2421 build1 (ADDR_EXPR, gcov_fn_info_ptr_type, var));
2422 return ctor;
2425 /* Finalize the coverage data. Generates the array of pointers to
2426 function objects from CTOR. Generate the gcov_info initializer.
2427 Generate the constructor function to call __gcov_init. */
2429 static void
2430 coverage_obj_finish (vec<constructor_elt, va_gc> *ctor)
2432 unsigned n_functions = vec_safe_length (ctor);
2433 tree fn_info_ary_type = build_array_type
2434 (build_qualified_type (gcov_fn_info_ptr_type, TYPE_QUAL_CONST),
2435 build_index_type (size_int (n_functions - 1)));
2436 tree fn_info_ary = build_decl (BUILTINS_LOCATION, VAR_DECL, NULL_TREE,
2437 fn_info_ary_type);
2438 char name_buf[32];
2440 TREE_STATIC (fn_info_ary) = 1;
2441 ASM_GENERATE_INTERNAL_LABEL (name_buf, "LPBX", 1);
2442 DECL_NAME (fn_info_ary) = get_identifier (name_buf);
2443 DECL_INITIAL (fn_info_ary) = build_constructor (fn_info_ary_type, ctor);
2444 varpool_finalize_decl (fn_info_ary);
2446 DECL_INITIAL (gcov_info_var)
2447 = build_info (TREE_TYPE (gcov_info_var), fn_info_ary);
2449 build_init_ctor (TREE_TYPE (gcov_info_var));
2451 varpool_finalize_decl (gcov_info_var);
2454 /* Get the da file name, given base file name. */
2456 static char *
2457 get_da_file_name (const char *base_file_name)
2459 char *da_file_name;
2460 int len = strlen (base_file_name);
2461 const char *prefix = profile_data_prefix;
2462 int prefix_len = 0;
2464 if (profile_data_prefix == 0 && !IS_ABSOLUTE_PATH(&base_file_name[0]))
2466 profile_data_prefix = getpwd ();
2467 prefix = profile_data_prefix;
2470 prefix_len = (prefix) ? strlen (prefix) + 1 : 0;
2472 /* Name of da file. */
2473 da_file_name = XNEWVEC (char, len + strlen (GCOV_DATA_SUFFIX)
2474 + prefix_len + 2);
2476 if (prefix)
2478 strcpy (da_file_name, prefix);
2479 da_file_name[prefix_len - 1] = '/';
2480 da_file_name[prefix_len] = 0;
2482 else
2483 da_file_name[0] = 0;
2484 strcat (da_file_name, base_file_name);
2485 if (profile_base_name_suffix_to_strip)
2487 int base_name_len = strlen (da_file_name);
2488 int suffix_to_strip_len = strlen (profile_base_name_suffix_to_strip);
2490 if (base_name_len > suffix_to_strip_len
2491 && !strcmp (da_file_name + (base_name_len - suffix_to_strip_len),
2492 profile_base_name_suffix_to_strip))
2493 da_file_name[base_name_len - suffix_to_strip_len] = '\0';
2496 strcat (da_file_name, GCOV_DATA_SUFFIX);
2497 return da_file_name;
2500 /* Callback to move counts_entry from one hash table to
2501 the target hashtable */
2504 move_hash_entry_callback (counts_entry **x,
2505 hash_table <counts_entry> *target_counts_hash)
2507 counts_entry *entry = *x;
2508 counts_entry **slot;
2509 slot = target_counts_hash->find_slot (entry, INSERT);
2510 *slot = entry;
2511 return 1;
2514 /* Rebuild counts_hash already built the primary module. This hashtable
2515 was built with a module-id of zero. It needs to be rebuilt taking the
2516 correct primary module-id into account. */
2519 rehash_callback (counts_entry **x,
2520 hash_table <counts_entry> *target_counts_hash)
2522 counts_entry *entry = *x;
2523 counts_entry **slot;
2525 entry->ident = GEN_FUNC_GLOBAL_ID (primary_module_id, entry->ident);
2526 slot = target_counts_hash->find_slot (entry, INSERT);
2527 *slot = entry;
2528 return 1;
2531 /* Rebuild counts_hash already built the primary module. This hashtable
2532 was built with a module-id of zero. It needs to be rebuilt taking the
2533 correct primary module-id into account. */
2535 static void
2536 rebuild_counts_hash (void)
2538 hash_table <counts_entry> tmp_counts_hash;
2539 tmp_counts_hash.create (10);
2540 gcc_assert (primary_module_id);
2542 rebuilding_counts_hash = true;
2544 /* Move the counts entries to the temporary hashtable. */
2545 counts_hash.traverse_noresize <
2546 hash_table <counts_entry> *,
2547 move_hash_entry_callback> (&tmp_counts_hash);
2548 counts_hash.empty ();
2550 /* Now rehash and copy back. */
2551 tmp_counts_hash.traverse_noresize <
2552 hash_table <counts_entry> *,
2553 rehash_callback> (&counts_hash);
2554 tmp_counts_hash.dispose();
2556 rebuilding_counts_hash = false;
2559 /* Add the module information record for the module with id
2560 MODULE_ID. IS_PRIMARY is true if the module is the primary module.
2561 INDEX is the index of the new record in the module info array. */
2563 void
2564 add_module_info (unsigned module_id, bool is_primary, int index)
2566 struct gcov_module_info *cur_info;
2567 module_infos = XRESIZEVEC (struct gcov_module_info *,
2568 module_infos, index + 1);
2569 module_infos[index] = XNEW (struct gcov_module_info);
2570 cur_info = module_infos[index];
2571 cur_info->ident = module_id;
2572 SET_MODULE_EXPORTED (cur_info);
2573 cur_info->num_quote_paths = 0;
2574 cur_info->num_bracket_paths = 0;
2575 cur_info->da_filename = NULL;
2576 cur_info->source_filename = NULL;
2577 if (is_primary)
2578 primary_module_id = module_id;
2581 /* Process the include paths needed for parsing the aux modules.
2582 The sub_pattern is in the form SUB_PATH:NEW_SUB_PATH. If it is
2583 defined, the SUB_PATH in ORIG_INC_PATH will be replaced with
2584 NEW_SUB_PATH. */
2586 static void
2587 process_include (char **orig_inc_path, char* old_sub, char *new_sub)
2589 char *inc_path, *orig_sub;
2591 if (strlen (*orig_inc_path) < strlen (old_sub))
2592 return;
2594 inc_path = (char*) xmalloc (strlen (*orig_inc_path) + strlen (new_sub)
2595 - strlen (old_sub) + 1);
2596 orig_sub = strstr (*orig_inc_path, old_sub);
2597 if (!orig_sub)
2599 inform (UNKNOWN_LOCATION, "subpath %s not found in path %s",
2600 old_sub, *orig_inc_path);
2601 free (inc_path);
2602 return;
2605 strncpy (inc_path, *orig_inc_path, orig_sub - *orig_inc_path);
2606 inc_path[orig_sub - *orig_inc_path] = '\0';
2607 strcat (inc_path, new_sub);
2608 strcat (inc_path, orig_sub + strlen (old_sub));
2610 free (*orig_inc_path);
2611 *orig_inc_path = inc_path;
2614 /* Process include paths for MOD_INFO according to option
2615 -fripa-inc-path-sub=OLD_SUB:NEW_SUB */
2617 static void
2618 process_include_paths_1 (struct gcov_module_info *mod_info,
2619 char* old_sub, char *new_sub)
2621 unsigned i, j;
2623 for (i = 0; i < mod_info->num_quote_paths; i++)
2624 process_include (&mod_info->string_array[i], old_sub, new_sub);
2626 for (i = 0, j = mod_info->num_quote_paths;
2627 i < mod_info->num_bracket_paths; i++, j++)
2628 process_include (&mod_info->string_array[j], old_sub, new_sub);
2630 for (i = 0, j = mod_info->num_quote_paths + mod_info->num_bracket_paths +
2631 mod_info->num_cpp_defines; i < mod_info->num_cpp_includes; i++, j++)
2632 process_include (&mod_info->string_array[j], old_sub, new_sub);
2636 /* Process include paths for MOD_INFO according to option
2637 -fripa-inc-path-sub=old_sub1:new_sub1[,old_sub2:new_sub2] */
2639 static void
2640 process_include_paths (struct gcov_module_info *mod_info)
2642 char *sub_pattern, *cur, *next, *new_sub;
2644 if (!lipo_inc_path_pattern)
2645 return;
2647 sub_pattern = xstrdup (lipo_inc_path_pattern);
2648 cur = sub_pattern;
2652 next = strchr (cur, ',');
2653 if (next)
2654 *next++ = '\0';
2655 new_sub = strchr (cur, ':');
2656 if (!new_sub)
2658 error ("Invalid path substibution pattern %s", sub_pattern);
2659 free (sub_pattern);
2660 return;
2662 *new_sub++ = '\0';
2663 process_include_paths_1 (mod_info, cur, new_sub);
2664 cur = next;
2665 } while (cur);
2666 free (sub_pattern);
2669 /* Set the prepreprocessing context (include search paths, -D/-U).
2670 PARSE_IN is the preprocessor reader, I is the index of the module,
2671 and VERBOSE is the verbose flag. */
2673 void
2674 set_lipo_c_parsing_context (struct cpp_reader *parse_in, int i, bool verbose)
2676 struct gcov_module_info *mod_info;
2677 if (!L_IPO_COMP_MODE)
2678 return;
2680 mod_info = module_infos[i];
2682 gcc_assert (flag_dyn_ipa);
2683 current_module_id = mod_info->ident;
2684 reset_funcdef_no ();
2686 if (current_module_id != primary_module_id)
2688 unsigned i, j;
2690 process_include_paths (mod_info);
2691 /* Setup include paths. */
2692 clear_include_chains ();
2693 for (i = 0; i < mod_info->num_quote_paths; i++)
2694 add_path (xstrdup (mod_info->string_array[i]),
2695 QUOTE, 0, 1);
2696 for (i = 0, j = mod_info->num_quote_paths;
2697 i < mod_info->num_bracket_paths; i++, j++)
2698 add_path (xstrdup (mod_info->string_array[j]),
2699 BRACKET, 0, 1);
2700 for (i = 0; i < mod_info->num_system_paths; i++, j++)
2701 add_path (xstrdup (mod_info->string_array[j]),
2702 SYSTEM, 0, 1);
2703 register_include_chains (parse_in, NULL, NULL, NULL,
2704 0, 0, verbose);
2706 /* Setup defines/undefs. */
2707 for (i = 0; i < mod_info->num_cpp_defines; i++, j++)
2708 if (mod_info->string_array[j][0] == 'D')
2709 cpp_define (parse_in, mod_info->string_array[j] + 1);
2710 else
2711 cpp_undef (parse_in, mod_info->string_array[j] + 1);
2713 /* Setup -imacro/-include. */
2714 for (i = 0; i < mod_info->num_cpp_includes; i++, j++)
2715 cpp_push_include (parse_in, mod_info->string_array[j]);
2719 /* Perform file-level initialization. Read in data file, generate name
2720 of graph file. */
2722 void
2723 coverage_init (const char *filename, const char* source_name)
2725 char* src_name_prefix = 0;
2726 int src_name_prefix_len = 0;
2727 int len = strlen (filename);
2729 /* Since coverage_init is invoked very early, before the pass
2730 manager, we need to set up the dumping explicitly. This is
2731 similar to the handling in finish_optimization_passes. */
2732 int profile_pass_num =
2733 g->get_passes ()->get_pass_profile ()->static_pass_number;
2734 g->get_dumps ()->dump_start (profile_pass_num, NULL);
2736 has_asm_statement = false;
2737 da_file_name = get_da_file_name (filename);
2738 da_base_file_name = XNEWVEC (char, strlen (filename) + 1);
2739 strcpy (da_base_file_name, filename);
2741 if (profile_data_prefix == 0 && !IS_ABSOLUTE_PATH (source_name))
2743 src_name_prefix = getpwd ();
2744 src_name_prefix_len = strlen (src_name_prefix) + 1;
2746 main_input_file_name = XNEWVEC (char, strlen (source_name) + 1
2747 + src_name_prefix_len);
2748 if (!src_name_prefix)
2749 strcpy (main_input_file_name, source_name);
2750 else
2752 strcpy (main_input_file_name, src_name_prefix);
2753 strcat (main_input_file_name, "/");
2754 strcat (main_input_file_name, source_name);
2757 bbg_file_stamp = local_tick;
2759 if (flag_branch_probabilities && !flag_auto_profile)
2760 read_counts_file (da_file_name, 0);
2762 /* Rebuild counts_hash and read the auxiliary GCDA files. */
2763 if (flag_profile_use && L_IPO_COMP_MODE)
2765 unsigned i;
2766 gcc_assert (flag_dyn_ipa);
2767 rebuild_counts_hash ();
2768 for (i = 1; i < num_in_fnames; i++)
2769 read_counts_file (get_da_file_name (module_infos[i]->da_filename),
2770 module_infos[i]->ident);
2773 /* Define variables which are referenced at runtime by libgcov. */
2774 if (profiling_enabled_p ())
2776 tree_init_instrumentation ();
2777 tree_init_dyn_ipa_parameters ();
2778 tree_init_instrumentation_sampling ();
2780 if (flag_auto_profile)
2781 init_auto_profile ();
2783 /* Name of bbg file. */
2784 if (flag_test_coverage && !flag_compare_debug)
2786 bbg_file_name = XNEWVEC (char, len + strlen (GCOV_NOTE_SUFFIX) + 1);
2787 memcpy (bbg_file_name, filename, len);
2788 strcpy (bbg_file_name + len, GCOV_NOTE_SUFFIX);
2789 if (!gcov_open (bbg_file_name, -1))
2791 error ("cannot open %s", bbg_file_name);
2792 bbg_file_name = NULL;
2794 else
2796 gcov_write_unsigned (GCOV_NOTE_MAGIC);
2797 gcov_write_unsigned (GCOV_VERSION);
2798 gcov_write_unsigned (bbg_file_stamp);
2802 g->get_dumps ()->dump_finish (profile_pass_num);
2805 /* Return True if any type of profiling is enabled which requires linking
2806 in libgcov otherwise return False. */
2808 static bool
2809 profiling_enabled_p (void)
2811 return profile_arc_flag
2812 || flag_profile_generate_sampling;
2815 /* Performs file-level cleanup. Close graph file, generate coverage
2816 variables and constructor. */
2818 void
2819 coverage_finish (void)
2821 if (bbg_file_name && gcov_close ())
2822 unlink (bbg_file_name);
2824 if (!flag_branch_probabilities && flag_test_coverage
2825 && (!local_tick || local_tick == (unsigned)-1))
2826 /* Only remove the da file, if we're emitting coverage code and
2827 cannot uniquely stamp it. If we can stamp it, libgcov will DTRT. */
2828 unlink (da_file_name);
2830 if (coverage_obj_init ())
2832 vec<constructor_elt, va_gc> *fn_ctor = NULL;
2833 struct coverage_data *fn;
2835 for (fn = functions_head; fn; fn = fn->next)
2836 fn_ctor = coverage_obj_fn (fn_ctor, fn->fn_decl, fn);
2837 coverage_obj_finish (fn_ctor);
2840 XDELETEVEC (da_file_name);
2841 da_file_name = NULL;
2844 /* Add S to the end of the string-list, the head and tail of which are
2845 pointed-to by HEAD and TAIL, respectively. */
2847 static void
2848 str_list_append (struct str_list **head, struct str_list **tail, const char *s)
2850 struct str_list *e = XNEW (struct str_list);
2851 e->str = XNEWVEC (char, strlen (s) + 1);
2852 strcpy (e->str, s);
2853 e->next = NULL;
2854 if (*tail)
2855 (*tail)->next = e;
2856 else
2857 *head = e;
2858 *tail = e;
2861 /* Copies the macro def or undef CPP_DEF and saves the copy
2862 in a list. IS_DEF is a flag indicating if CPP_DEF represents
2863 a -D or -U. */
2865 void
2866 coverage_note_define (const char *cpp_def, bool is_def)
2868 char *s = XNEWVEC (char, strlen (cpp_def) + 2);
2869 s[0] = is_def ? 'D' : 'U';
2870 strcpy (s + 1, cpp_def);
2871 str_list_append (&cpp_defines_head, &cpp_defines_tail, s);
2872 num_cpp_defines++;
2875 /* Copies the -imacro/-include FILENAME and saves the copy in a list. */
2877 void
2878 coverage_note_include (const char *filename)
2880 str_list_append (&cpp_includes_head, &cpp_includes_tail, filename);
2881 num_cpp_includes++;
2884 /* Mark this module as containing asm statements. */
2886 void
2887 coverage_has_asm_stmt (void)
2889 has_asm_statement = flag_ripa_disallow_asm_modules;
2892 /* Write compilation info to the .note section. */
2894 void
2895 write_compilation_info_to_asm (void)
2897 unsigned lang;
2898 /* Write lang, ggc_memory to ASM section. */
2899 switch_to_section (get_section (".gnu.switches.text.lipo_info",
2900 SECTION_DEBUG, NULL));
2901 if (!strcmp (lang_hooks.name, "GNU C"))
2902 lang = GCOV_MODULE_C_LANG;
2903 else if (!strcmp (lang_hooks.name, "GNU C++"))
2904 lang = GCOV_MODULE_CPP_LANG;
2905 else
2906 lang = GCOV_MODULE_UNKNOWN_LANG;
2907 if (has_asm_statement)
2908 lang |= GCOV_MODULE_ASM_STMTS;
2909 dw2_asm_output_nstring (in_fnames[0], (size_t)-1, NULL);
2910 dw2_asm_output_data_uleb128 (lang, NULL);
2911 dw2_asm_output_data_uleb128 (ggc_total_memory, NULL);
2914 /* Write command line options to the .note section. */
2916 void
2917 write_compilation_flags_to_asm (void)
2919 size_t i;
2920 cpp_dir *quote_paths, *bracket_paths, *system_paths, *pdir;
2921 struct str_list *pdef, *pinc;
2922 int num_quote_paths = 0;
2923 int num_bracket_paths = 0;
2924 int num_system_paths = 0;
2926 get_include_chains (&quote_paths, &bracket_paths, &system_paths);
2928 /* Write quote_paths to ASM section. */
2929 switch_to_section (get_section (".gnu.switches.text.quote_paths",
2930 SECTION_DEBUG, NULL));
2931 for (pdir = quote_paths; pdir; pdir = pdir->next)
2933 if (pdir == bracket_paths)
2934 break;
2935 num_quote_paths++;
2937 dw2_asm_output_nstring (in_fnames[0], (size_t)-1, NULL);
2938 dw2_asm_output_data_uleb128 (num_quote_paths, NULL);
2939 for (pdir = quote_paths; pdir; pdir = pdir->next)
2941 if (pdir == bracket_paths)
2942 break;
2943 dw2_asm_output_nstring (pdir->name, (size_t)-1, NULL);
2946 /* Write bracket_paths to ASM section. */
2947 switch_to_section (get_section (".gnu.switches.text.bracket_paths",
2948 SECTION_DEBUG, NULL));
2949 for (pdir = bracket_paths; pdir; pdir = pdir->next)
2951 if (pdir == system_paths)
2952 break;
2953 num_bracket_paths++;
2955 dw2_asm_output_nstring (in_fnames[0], (size_t)-1, NULL);
2956 dw2_asm_output_data_uleb128 (num_bracket_paths, NULL);
2957 for (pdir = bracket_paths; pdir; pdir = pdir->next)
2959 if (pdir == system_paths)
2960 break;
2961 dw2_asm_output_nstring (pdir->name, (size_t)-1, NULL);
2964 /* Write system_paths to ASM section. */
2965 switch_to_section (get_section (".gnu.switches.text.system_paths",
2966 SECTION_DEBUG, NULL));
2967 for (pdir = system_paths; pdir; pdir = pdir->next)
2968 num_system_paths++;
2969 dw2_asm_output_nstring (in_fnames[0], (size_t)-1, NULL);
2970 dw2_asm_output_data_uleb128 (num_system_paths, NULL);
2971 for (pdir = system_paths; pdir; pdir = pdir->next)
2972 dw2_asm_output_nstring (pdir->name, (size_t)-1, NULL);
2974 /* Write cpp_defines to ASM section. */
2975 switch_to_section (get_section (".gnu.switches.text.cpp_defines",
2976 SECTION_DEBUG, NULL));
2977 dw2_asm_output_nstring (in_fnames[0], (size_t)-1, NULL);
2978 dw2_asm_output_data_uleb128 (num_cpp_defines, NULL);
2979 for (pdef = cpp_defines_head; pdef; pdef = pdef->next)
2980 dw2_asm_output_nstring (pdef->str, (size_t)-1, NULL);
2982 /* Write cpp_includes to ASM section. */
2983 switch_to_section (get_section (".gnu.switches.text.cpp_includes",
2984 SECTION_DEBUG, NULL));
2985 dw2_asm_output_nstring (in_fnames[0], (size_t)-1, NULL);
2986 dw2_asm_output_data_uleb128 (num_cpp_includes, NULL);
2987 for (pinc = cpp_includes_head; pinc; pinc = pinc->next)
2988 dw2_asm_output_nstring (pinc->str, (size_t)-1, NULL);
2990 /* Write cl_args to ASM section. */
2991 switch_to_section (get_section (".gnu.switches.text.cl_args",
2992 SECTION_DEBUG, NULL));
2993 dw2_asm_output_nstring (in_fnames[0], (size_t)-1, NULL);
2994 dw2_asm_output_data_uleb128 (num_lipo_cl_args, NULL);
2995 for (i = 0; i < num_lipo_cl_args; i++)
2996 dw2_asm_output_nstring (lipo_cl_args[i], (size_t)-1, NULL);
2998 #include "gt-coverage.h"