Merge aosp-toolchain/gcc/gcc-4_9 changes.
[official-gcc.git] / gcc-4_9 / gcc / coverage.c
blob93f394d84976a30d4e86239130efe9b5c0437690
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 /* List of lines read from -fprofile-generate-buildinfo=filename. */
181 struct str_list *build_info_array_head = NULL, *build_info_array_tail = NULL;
182 static unsigned num_build_info = 0;
184 /* True if the current module has any asm statements. */
185 static bool has_asm_statement;
187 /* Forward declarations. */
188 static void read_counts_file (const char *, unsigned);
189 static tree build_var (tree, tree, int);
190 static void build_fn_info_type (tree, unsigned, tree);
191 static void build_info_type (tree, tree);
192 static tree build_fn_info (const struct coverage_data *, tree, tree);
193 static tree build_info (tree, tree);
194 static bool coverage_obj_init (void);
195 static vec<constructor_elt, va_gc> *coverage_obj_fn
196 (vec<constructor_elt, va_gc> *, tree, struct coverage_data const *);
197 static void coverage_obj_finish (vec<constructor_elt, va_gc> *);
198 static char * get_da_file_name (const char *);
199 static tree build_gcov_module_info_type (void);
201 /* Return the type node for gcov_type. */
203 tree
204 get_gcov_type (void)
206 enum machine_mode mode = smallest_mode_for_size (GCOV_TYPE_SIZE, MODE_INT);
207 return lang_hooks.types.type_for_mode (mode, false);
210 /* Return the type node for gcov_unsigned_t. */
212 tree
213 get_gcov_unsigned_t (void)
215 enum machine_mode mode = smallest_mode_for_size (32, MODE_INT);
216 return lang_hooks.types.type_for_mode (mode, true);
219 /* Return the type node for const char *. */
221 tree
222 get_const_string_type (void)
224 return build_pointer_type
225 (build_qualified_type (char_type_node, TYPE_QUAL_CONST));
228 inline hashval_t
229 counts_entry::hash (const value_type *entry)
231 return entry->ident * GCOV_COUNTERS + entry->ctr;
234 inline int
235 counts_entry::equal (const value_type *entry1,
236 const compare_type *entry2)
238 return entry1->ident == entry2->ident && entry1->ctr == entry2->ctr;
241 inline void
242 counts_entry::remove (value_type *entry)
244 /* When rebuilding counts_hash, we will reuse the entry. */
245 if (!rebuilding_counts_hash)
247 free (entry->counts);
248 free (entry);
252 /* Hash table of count data. */
253 static hash_table <counts_entry> counts_hash;
255 /* Returns true if MOD_ID is the id of the last source module. */
257 is_last_module (unsigned mod_id)
259 return (mod_id == module_infos[num_in_fnames - 1]->ident);
262 /* String hash function */
264 struct string_hasher
266 /* hash_table support. */
267 typedef char value_type;
268 typedef char compare_type;
269 static inline hashval_t hash (const value_type *);
270 static int equal (const value_type *, const compare_type *);
271 static void remove (value_type *) {};
274 hashval_t
275 string_hasher::hash (const char* s)
277 return htab_hash_string (s);
280 /* String equal function */
283 string_hasher::equal (const char *s1, const char *s2)
285 return !strcmp (s1, s2);
288 /* Command line option descriptor. */
290 struct opt_desc
292 const char *opt_str;
293 const char *opt_neg_str;
294 bool default_val; /* TODO better handling of default */
297 static struct opt_desc force_matching_cg_opts[] =
299 { "-fexceptions", "-fno-exceptions", true },
300 { "-fsized-delete", "-fno-sized-delete", false },
301 { "-frtti", "-fno-rtti", true },
302 { "-fstrict-aliasing", "-fno-strict-aliasing", true },
303 { "-fsigned-char", "-funsigned-char", true },
304 /* { "-fsigned-char", "-fno-signed-char", true },
305 { "-funsigned-char", "-fno-unsigned-char", false }, */
306 { "-ansi", "", false },
307 { NULL, NULL, false }
310 /* A helper function to check if OPTION_STRING is one of the codegen
311 options specified in FORCE_MATCHING_CG_ARGS. If yes, set the
312 corresponding entry in CG_ARG_VAL to the value of the option specified
313 in OPTION_STRING. */
315 static void
316 check_cg_opts (bool *cg_opt_val, const char *option_str)
318 unsigned int i;
319 for (i = 0; force_matching_cg_opts[i].opt_str; i++)
321 if (!strcmp (force_matching_cg_opts[i].opt_str, option_str))
322 cg_opt_val[i] = true;
323 else if (!strcmp (force_matching_cg_opts[i].opt_neg_str, option_str))
324 cg_opt_val[i] = false;
328 /* A helper function to check if CG_OPTS1 and CG_OPTS are identical. It returns
329 true if yes, false otherwise. */
331 static bool
332 has_incompatible_cg_opts (bool *cg_opts1, bool *cg_opts2, unsigned num_cg_opts)
334 unsigned i;
336 for (i = 0; i < num_cg_opts; i++)
338 if (cg_opts1[i] != cg_opts2[i])
339 return true;
342 return false;
345 /* Returns true if the command-line arguments stored in the given module-infos
346 are incompatible. */
347 bool
348 incompatible_cl_args (struct gcov_module_info* mod_info1,
349 struct gcov_module_info* mod_info2)
351 char **warning_opts1 = XNEWVEC (char *, mod_info1->num_cl_args);
352 char **warning_opts2 = XNEWVEC (char *, mod_info2->num_cl_args);
353 char **non_warning_opts1 = XNEWVEC (char *, mod_info1->num_cl_args);
354 char **non_warning_opts2 = XNEWVEC (char *, mod_info2->num_cl_args);
355 char *std_opts1 = NULL, *std_opts2 = NULL;
356 unsigned arch_isa1 = 0, arch_isa2 = 0;
357 unsigned int i, num_warning_opts1 = 0, num_warning_opts2 = 0;
358 unsigned int num_non_warning_opts1 = 0, num_non_warning_opts2 = 0;
359 bool warning_mismatch = false;
360 bool non_warning_mismatch = false;
361 hash_table <string_hasher> option_tab1, option_tab2;
362 unsigned int start_index1 = mod_info1->num_quote_paths
363 + mod_info1->num_bracket_paths + mod_info1->num_system_paths
364 + mod_info1->num_cpp_defines + mod_info1->num_cpp_includes;
365 unsigned int start_index2 = mod_info2->num_quote_paths
366 + mod_info2->num_bracket_paths + mod_info2->num_system_paths
367 + mod_info2->num_cpp_defines + mod_info2->num_cpp_includes;
369 bool *cg_opts1, *cg_opts2, has_any_incompatible_cg_opts, has_incompatible_std;
370 bool has_incompatible_arch_isa;
371 unsigned int num_cg_opts = 0;
373 for (i = 0; force_matching_cg_opts[i].opt_str; i++)
374 num_cg_opts++;
376 cg_opts1 = XCNEWVEC (bool, num_cg_opts);
377 cg_opts2 = XCNEWVEC (bool, num_cg_opts);
379 /* Initialize the array to default value */
380 for (i = 0; force_matching_cg_opts[i].opt_str; i++)
382 cg_opts1[i] = force_matching_cg_opts[i].default_val;
383 cg_opts2[i] = force_matching_cg_opts[i].default_val;
386 option_tab1.create (10);
387 option_tab2.create (10);
389 /* First, separate the warning and non-warning options. */
390 for (i = 0; i < mod_info1->num_cl_args; i++)
391 if (mod_info1->string_array[start_index1 + i][1] == 'W')
392 warning_opts1[num_warning_opts1++] =
393 mod_info1->string_array[start_index1 + i];
394 else
396 char **slot;
397 char *option_string = mod_info1->string_array[start_index1 + i];
399 check_cg_opts (cg_opts1, option_string);
400 if (strstr (option_string, "-std="))
401 std_opts1 = option_string;
403 if (!strncmp (option_string, "-m",2))
404 arch_isa1 = crc32_string (arch_isa1, option_string);
406 slot = option_tab1.find_slot (option_string, INSERT);
407 if (!*slot)
409 *slot = option_string;
410 non_warning_opts1[num_non_warning_opts1++] = option_string;
414 for (i = 0; i < mod_info2->num_cl_args; i++)
415 if (mod_info2->string_array[start_index2 + i][1] == 'W')
416 warning_opts2[num_warning_opts2++] =
417 mod_info2->string_array[start_index2 + i];
418 else
420 char **slot;
421 char *option_string = mod_info2->string_array[start_index2 + i];
423 check_cg_opts (cg_opts2, option_string);
424 if (strstr (option_string, "-std="))
425 std_opts2 = option_string;
427 if (!strncmp (option_string, "-m",2))
428 arch_isa2 = crc32_string (arch_isa2, option_string);
430 slot = option_tab2.find_slot (option_string, INSERT);
431 if (!*slot)
433 *slot = option_string;
434 non_warning_opts2[num_non_warning_opts2++] = option_string;
438 has_incompatible_std =
439 std_opts1 != std_opts2 && (std_opts1 == NULL || std_opts2 == NULL
440 || strcmp (std_opts1, std_opts2));
442 has_incompatible_arch_isa = (arch_isa1 != arch_isa2);
443 /* Compare warning options. If these mismatch, we emit a warning. */
444 if (num_warning_opts1 != num_warning_opts2)
445 warning_mismatch = true;
446 else
447 for (i = 0; i < num_warning_opts1 && !warning_mismatch; i++)
448 warning_mismatch = strcmp (warning_opts1[i], warning_opts2[i]) != 0;
450 /* Compare non-warning options. If these mismatch, we emit a warning, and if
451 -fripa-disallow-opt-mismatch is supplied, the two modules are also
452 incompatible. */
453 if (num_non_warning_opts1 != num_non_warning_opts2)
454 non_warning_mismatch = true;
455 else
456 for (i = 0; i < num_non_warning_opts1 && !non_warning_mismatch; i++)
457 non_warning_mismatch =
458 strcmp (non_warning_opts1[i], non_warning_opts2[i]) != 0;
460 if (warn_ripa_opt_mismatch && (warning_mismatch || non_warning_mismatch))
461 warning (OPT_Wripa_opt_mismatch, "command line arguments mismatch for %s "
462 "and %s", mod_info1->source_filename, mod_info2->source_filename);
464 if (warn_ripa_opt_mismatch && non_warning_mismatch && dump_enabled_p ())
466 dump_printf_loc (MSG_MISSED_OPTIMIZATION, UNKNOWN_LOCATION,
467 "Options for %s", mod_info1->source_filename);
468 for (i = 0; i < num_non_warning_opts1; i++)
469 dump_printf_loc (MSG_MISSED_OPTIMIZATION, UNKNOWN_LOCATION,
470 non_warning_opts1[i]);
471 dump_printf_loc (MSG_MISSED_OPTIMIZATION, UNKNOWN_LOCATION,
472 "Options for %s", mod_info2->source_filename);
473 for (i = 0; i < num_non_warning_opts2; i++)
474 dump_printf_loc (MSG_MISSED_OPTIMIZATION, UNKNOWN_LOCATION,
475 non_warning_opts2[i]);
478 has_any_incompatible_cg_opts
479 = has_incompatible_cg_opts (cg_opts1, cg_opts2, num_cg_opts);
481 XDELETEVEC (warning_opts1);
482 XDELETEVEC (warning_opts2);
483 XDELETEVEC (non_warning_opts1);
484 XDELETEVEC (non_warning_opts2);
485 XDELETEVEC (cg_opts1);
486 XDELETEVEC (cg_opts2);
487 option_tab1.dispose ();
488 option_tab2.dispose ();
489 return ((flag_ripa_disallow_opt_mismatch && non_warning_mismatch)
490 || has_any_incompatible_cg_opts || has_incompatible_std
491 || has_incompatible_arch_isa);
495 /* Support for module sorting based on user specfication. */
496 struct module_name_entry
498 typedef module_name_entry value_type;
499 typedef module_name_entry compare_type;
500 static inline hashval_t hash (const value_type *entry);
501 static inline int equal (const value_type *entry1, const compare_type *entry2);
502 static inline void remove (value_type *v);
504 const char *source_name;
505 int order;
508 /* Hash function for module name */
510 hashval_t
511 module_name_entry::hash (const value_type *s)
513 return htab_hash_string (s->source_name);
516 /* Delete function for module name */
518 void
519 module_name_entry::remove (value_type *entry)
521 /* XDELETE (entry->source_name); */
522 XDELETE (entry);
525 /* Equal function for module name */
528 module_name_entry::equal (const value_type *s1, const compare_type *s2)
530 return !strcmp (s1->source_name, s2->source_name);
533 static hash_table<module_name_entry> module_name_tab;
535 /* Comparison function for sorting module_infos array. */
537 static int
538 cmp_module_name_entry (const void *p1, const void *p2)
540 module_name_entry **slot1, **slot2;
541 module_name_entry *m_e1, *m_e2;
543 struct gcov_module_info *const *e1 = (struct gcov_module_info *const *) p1;
544 struct gcov_module_info *const *e2 = (struct gcov_module_info *const *) p2;
546 module_name_entry e;
547 e.source_name = (*e1)->source_filename;
548 slot1 = module_name_tab.find_slot (&e, NO_INSERT);
549 e.source_name = (*e2)->source_filename;
550 slot2 = module_name_tab.find_slot (&e, NO_INSERT);
552 if (!slot1 || !*slot1)
553 return 1;
555 if (!slot2 || !*slot2)
556 return -1;
558 gcc_assert (slot1 && *slot1 && slot2 && *slot2);
559 m_e1 = *slot1;
560 m_e2 = *slot2;
562 return m_e1->order - m_e2->order;
565 /* Comparison function for sorting fname array */
567 static int
568 cmp_fname_entry (const void *p1, const void *p2)
570 module_name_entry **slot1, **slot2;
571 module_name_entry *m_e1, *m_e2;
573 const char *const *e1 = (const char *const *) p1;
574 const char *const *e2 = (const char *const *) p2;
576 module_name_entry e;
578 e.source_name = *e1;
579 slot1 = module_name_tab.find_slot (&e, NO_INSERT);
580 e.source_name = *e2;
581 slot2 = module_name_tab.find_slot (&e, NO_INSERT);
583 if (!slot1 || !*slot1)
584 return 1;
586 if (!slot2 || !*slot2)
587 return -1;
589 gcc_assert (slot1 && *slot1 && slot2 && *slot2);
590 m_e1 = *slot1;
591 m_e2 = *slot2;
593 return m_e1->order - m_e2->order;
596 /* Reorder module group according to file IMPORTS_FILE */
598 static void
599 reorder_module_groups (const char *imports_file, unsigned max_group)
601 FILE *f;
602 int order = 0;
603 const int max_line_size = (1 << 16);
604 char line[max_line_size];
606 module_name_tab.create (20);
608 f = fopen (imports_file, "r");
609 if (!f)
610 error ("Can't open file %s", imports_file);
612 while (fgets (line, max_line_size, f))
614 size_t n = strlen (line);
615 gcc_assert (n < max_line_size - 1);
616 if (line[n - 1] == '\n')
617 line[n - 1] = '\0';
619 module_name_entry **slot;
620 module_name_entry *m_e = XCNEW (module_name_entry);
622 m_e->source_name = xstrdup (line);
623 m_e->order = order;
625 slot = module_name_tab.find_slot (m_e, INSERT);
626 gcc_assert (!*slot);
627 *slot = m_e;
629 order++;
632 /* Now do the sorting */
634 qsort (&module_infos[1], num_in_fnames - 1, sizeof (void *),
635 cmp_module_name_entry);
636 qsort (&in_fnames[1], num_in_fnames - 1, sizeof (void *),
637 cmp_fname_entry);
640 unsigned i;
642 for (i = 0; i < num_in_fnames; i++)
643 fprintf (stderr, "*** %s (%s)\n", in_fnames[i],
644 i < max_group ? "Kept":"Skipped");
646 for (i = 0; i < num_in_fnames; i++)
647 fprintf (stderr, "### %s (%s)\n", module_infos[i]->source_filename,
648 i < max_group ? "Kept":"Skipped");
652 if (num_in_fnames > max_group)
653 num_in_fnames = max_group;
655 module_name_tab.dispose ();
658 typedef struct {
659 unsigned int mod_id;
660 const char *mod_name;
661 } mod_id_to_name_t;
663 static vec<mod_id_to_name_t> *mod_names;
665 void
666 record_module_name (unsigned int mod_id, const char *name)
668 mod_id_to_name_t t;
670 t.mod_id = mod_id;
671 t.mod_name = xstrdup (name);
672 if (!mod_names)
673 vec_alloc (mod_names, 10);
674 mod_names->safe_push (t);
677 /* Return the module name for module with MOD_ID. */
679 const char *
680 get_module_name (unsigned int mod_id)
682 size_t i;
683 mod_id_to_name_t *elt;
685 for (i = 0; mod_names->iterate (i, &elt); i++)
687 if (elt->mod_id == mod_id)
688 return elt->mod_name;
691 gcc_assert (0);
692 return NULL;
695 /* Read in the counts file, if available. DA_FILE_NAME is the
696 name of the gcda file, and MODULE_ID is the module id of the
697 associated source module. */
699 static void
700 read_counts_file (const char *da_file_name, unsigned module_id)
702 gcov_unsigned_t fn_ident = 0;
703 struct gcov_summary summary;
704 unsigned new_summary = 1;
705 gcov_unsigned_t tag;
706 int is_error = 0;
707 unsigned module_infos_read = 0;
708 struct pointer_set_t *modset = 0;
709 unsigned max_group = PARAM_VALUE (PARAM_MAX_LIPO_GROUP);
710 unsigned lineno_checksum = 0;
711 unsigned cfg_checksum = 0;
712 const char *imports_filename;
714 if (max_group == 0)
715 max_group = (unsigned) -1;
717 if (!gcov_open (da_file_name, 1))
719 if (PARAM_VALUE (PARAM_GCOV_DEBUG))
721 /* Try to find .gcda file in the current working dir. */
722 da_file_name = lbasename (da_file_name);
723 if (!gcov_open (da_file_name, 1))
724 return;
726 else
728 inform (input_location, "file %s not found, disabling profile use",
729 da_file_name);
730 set_profile_use_options (&global_options, &global_options_set,
731 false, true);
732 /* RESET is invoked during covrerage_init when process_options is done.
733 Need to reset optimization_default_node and optimization_current_node. */
734 /* Save the current optimization options. */
735 optimization_default_node = build_optimization_node (&global_options);
736 optimization_current_node = optimization_default_node;
737 return;
741 if (!gcov_magic (gcov_read_unsigned (), GCOV_DATA_MAGIC))
743 warning (0, "%qs is not a gcov data file", da_file_name);
744 gcov_close ();
745 return;
747 else if ((tag = gcov_read_unsigned ()) != GCOV_VERSION)
749 char v[4], e[4];
751 GCOV_UNSIGNED2STRING (v, tag);
752 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
754 warning (0, "%qs is version %q.*s, expected version %q.*s",
755 da_file_name, 4, v, 4, e);
756 gcov_close ();
757 return;
760 /* Read the stamp, used for creating a generation count. */
761 tag = gcov_read_unsigned ();
762 bbg_file_stamp = crc32_unsigned (bbg_file_stamp, tag);
764 if (!counts_hash.is_created ())
765 counts_hash.create (10);
767 while ((tag = gcov_read_unsigned ()))
769 gcov_unsigned_t length;
770 gcov_position_t offset;
772 length = gcov_read_unsigned ();
773 offset = gcov_position ();
774 if (tag == GCOV_TAG_FUNCTION)
776 if (length)
778 fn_ident = gcov_read_unsigned ();
779 lineno_checksum = gcov_read_unsigned ();
780 cfg_checksum = gcov_read_unsigned ();
782 else
783 fn_ident = lineno_checksum = cfg_checksum = 0;
784 new_summary = 1;
786 else if (tag == GCOV_TAG_PROGRAM_SUMMARY)
788 struct gcov_summary sum;
789 unsigned ix;
791 if (new_summary)
792 memset (&summary, 0, sizeof (summary));
794 gcov_read_summary (&sum);
795 for (ix = 0; ix != GCOV_COUNTERS_SUMMABLE; ix++)
797 summary.ctrs[ix].runs += sum.ctrs[ix].runs;
798 summary.ctrs[ix].sum_all += sum.ctrs[ix].sum_all;
799 if (summary.ctrs[ix].run_max < sum.ctrs[ix].run_max)
800 summary.ctrs[ix].run_max = sum.ctrs[ix].run_max;
801 summary.ctrs[ix].sum_max += sum.ctrs[ix].sum_max;
803 if (new_summary)
804 memcpy (summary.ctrs[GCOV_COUNTER_ARCS].histogram,
805 sum.ctrs[GCOV_COUNTER_ARCS].histogram,
806 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
807 else
808 gcov_histogram_merge (summary.ctrs[GCOV_COUNTER_ARCS].histogram,
809 sum.ctrs[GCOV_COUNTER_ARCS].histogram);
810 new_summary = 0;
812 else if (tag == GCOV_TAG_BUILD_INFO)
814 /* Build info strings are not used by the compiler, read and
815 ignore. */
816 gcov_unsigned_t num_strings;
817 char **build_info_strings = gcov_read_build_info (length,
818 &num_strings);
819 for (unsigned i = 0; i < num_strings; i++)
820 free (build_info_strings[i]);
821 free (build_info_strings);
823 else if (tag == GCOV_TAG_COMDAT_ZERO_FIXUP)
825 /* Zero-profile fixup flags are not used by the compiler, read and
826 ignore. */
827 gcov_unsigned_t num_fn;
828 int *zero_fixup_flags = gcov_read_comdat_zero_fixup (length, &num_fn);
829 free (zero_fixup_flags);
831 else if (GCOV_TAG_IS_COUNTER (tag) && fn_ident)
833 counts_entry_t **slot, *entry, elt;
834 unsigned n_counts = GCOV_TAG_COUNTER_NUM (length);
835 unsigned ix;
837 elt.ident = GEN_FUNC_GLOBAL_ID (module_id, fn_ident);
838 elt.ctr = GCOV_COUNTER_FOR_TAG (tag);
840 slot = counts_hash.find_slot (&elt, INSERT);
841 entry = *slot;
842 if (!entry)
844 *slot = entry = XCNEW (counts_entry_t);
845 entry->ident = elt.ident;
846 entry->ctr = elt.ctr;
847 entry->lineno_checksum = lineno_checksum;
848 entry->cfg_checksum = cfg_checksum;
849 if (elt.ctr < GCOV_COUNTERS_SUMMABLE)
850 entry->summary = summary.ctrs[elt.ctr];
851 entry->summary.num = n_counts;
852 entry->counts = XCNEWVEC (gcov_type, n_counts);
854 else if (entry->lineno_checksum != lineno_checksum
855 || entry->cfg_checksum != cfg_checksum)
857 error ("Profile data for function %u is corrupted", fn_ident);
858 error ("checksum is (%x,%x) instead of (%x,%x)",
859 entry->lineno_checksum, entry->cfg_checksum,
860 lineno_checksum, cfg_checksum);
861 counts_hash.dispose ();
862 break;
864 else if (entry->summary.num != n_counts)
866 error ("Profile data for function %u is corrupted", fn_ident);
867 error ("number of counters is %d instead of %d", entry->summary.num, n_counts);
868 counts_hash.dispose ();
869 break;
871 else if (elt.ctr >= GCOV_COUNTERS_SUMMABLE)
873 error ("cannot merge separate %s counters for function %u",
874 ctr_names[elt.ctr], fn_ident);
875 goto skip_merge;
877 else
879 entry->summary.runs += summary.ctrs[elt.ctr].runs;
880 entry->summary.sum_all += summary.ctrs[elt.ctr].sum_all;
881 if (entry->summary.run_max < summary.ctrs[elt.ctr].run_max)
882 entry->summary.run_max = summary.ctrs[elt.ctr].run_max;
883 entry->summary.sum_max += summary.ctrs[elt.ctr].sum_max;
885 for (ix = 0; ix != n_counts; ix++)
886 entry->counts[ix] += gcov_read_counter ();
887 skip_merge:;
889 /* Skip the MODULE_INFO records if not in dyn-ipa mode, or when reading
890 auxiliary modules. */
891 else if (tag == GCOV_TAG_MODULE_INFO && flag_dyn_ipa && !module_id)
893 struct gcov_module_info* mod_info;
894 size_t info_sz;
895 /* each string has at least 8 bytes, so MOD_INFO's
896 persistent length >= in core size. */
897 mod_info
898 = (struct gcov_module_info *) alloca ((length + 2)
899 * sizeof (gcov_unsigned_t));
900 gcov_read_module_info (mod_info, length);
901 info_sz = (sizeof (struct gcov_module_info) +
902 sizeof (void *) * (mod_info->num_quote_paths +
903 mod_info->num_bracket_paths +
904 mod_info->num_system_paths +
905 mod_info->num_cpp_defines +
906 mod_info->num_cpp_includes +
907 mod_info->num_cl_args));
908 /* The first MODULE_INFO record must be for the primary module. */
909 if (module_infos_read == 0)
911 gcc_assert (mod_info->is_primary && !modset);
912 module_infos_read++;
913 modset = pointer_set_create ();
914 pointer_set_insert (modset, (void *)(size_t)mod_info->ident);
915 primary_module_id = mod_info->ident;
916 include_all_aux = MODULE_INCLUDE_ALL_AUX_FLAG (mod_info);
917 module_infos = XCNEWVEC (struct gcov_module_info *, 1);
918 module_infos[0] = XCNEWVAR (struct gcov_module_info, info_sz);
919 memcpy (module_infos[0], mod_info, info_sz);
921 else
923 int fd;
924 char *aux_da_filename = get_da_file_name (mod_info->da_filename);
925 gcc_assert (!mod_info->is_primary);
926 if (pointer_set_insert (modset, (void *)(size_t)mod_info->ident))
928 if (dump_enabled_p ())
929 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
930 "Not importing %s: already imported",
931 mod_info->source_filename);
933 else if ((module_infos[0]->lang & GCOV_MODULE_LANG_MASK) !=
934 (mod_info->lang & GCOV_MODULE_LANG_MASK))
936 if (dump_enabled_p ())
937 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
938 "Not importing %s: source language"
939 " different from primary module's source"
940 " language",
941 mod_info->source_filename);
943 else if (module_infos_read == max_group
944 /* If reordering is specified, delay the cutoff
945 until after sorting. */
946 && !getenv ("LIPO_REORDER_GROUP"))
948 if (dump_enabled_p ())
949 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
950 "Not importing %s: maximum group size"
951 " reached", mod_info->source_filename);
953 else if (incompatible_cl_args (module_infos[0], mod_info))
955 if (dump_enabled_p ())
956 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
957 "Not importing %s: command-line"
958 " arguments not compatible with primary"
959 " module",
960 mod_info->source_filename);
962 else if ((fd = open (aux_da_filename, O_RDONLY)) < 0)
964 if (dump_enabled_p ())
965 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
966 "Not importing %s: couldn't open %s",
967 mod_info->source_filename,
968 aux_da_filename);
970 else if ((mod_info->lang & GCOV_MODULE_ASM_STMTS)
971 && flag_ripa_disallow_asm_modules)
973 if (dump_enabled_p ())
974 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
975 "Not importing %s: contains assembler"
976 " statements", mod_info->source_filename);
978 else if (mod_info->is_primary == false
979 && MODULE_EXPORTED_FLAG (mod_info) == false)
981 warning (0, "MODULE_ID=%d (%s) is an auxiliary module, "
982 "but export_bit is not set. \n",
983 mod_info->ident, mod_info->source_filename);
985 else
987 close (fd);
988 module_infos_read++;
989 add_input_filename (mod_info->source_filename);
990 module_infos = XRESIZEVEC (struct gcov_module_info *,
991 module_infos, num_in_fnames);
992 gcc_assert (num_in_fnames == module_infos_read);
993 module_infos[module_infos_read - 1]
994 = XCNEWVAR (struct gcov_module_info, info_sz);
995 memcpy (module_infos[module_infos_read - 1], mod_info,
996 info_sz);
1000 record_module_name (mod_info->ident,
1001 lbasename (mod_info->source_filename));
1003 if (dump_enabled_p ())
1005 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
1006 "MODULE Id=%d, Is_Primary=%s,"
1007 " Is_Exported=%s, Include_all=%s, Name=%s (%s)",
1008 mod_info->ident, mod_info->is_primary?"yes":"no",
1009 MODULE_EXPORTED_FLAG (mod_info)?"yes":"no",
1010 MODULE_INCLUDE_ALL_AUX_FLAG (mod_info)?"yes"
1011 :"no",
1012 mod_info->source_filename,
1013 mod_info->da_filename);
1016 gcov_sync (offset, length);
1017 if ((is_error = gcov_is_error ()))
1019 error (is_error < 0 ? "%qs has overflowed" : "%qs is corrupted",
1020 da_file_name);
1021 counts_hash.dispose ();
1022 break;
1026 if ((imports_filename = getenv ("LIPO_REORDER_GROUP"))
1027 && flag_dyn_ipa && !module_id)
1029 reorder_module_groups (imports_filename, max_group);
1030 if (module_infos_read != num_in_fnames)
1031 module_infos_read = num_in_fnames;
1034 /* TODO: profile based multiple module compilation does not work
1035 together with command line (-combine) based ipo -- add a nice
1036 warning and bail out instead of asserting. */
1038 if (modset)
1039 pointer_set_destroy (modset);
1040 gcc_assert (module_infos_read == 0
1041 || module_infos_read == num_in_fnames);
1043 if (flag_dyn_ipa)
1044 gcc_assert (primary_module_id && num_in_fnames >= 1);
1046 gcov_close ();
1049 /* Returns the coverage data entry for counter type COUNTER of function
1050 FUNC. EXPECTED is the number of expected counter entries. */
1052 static counts_entry_t *
1053 get_coverage_counts_entry (struct function *func, unsigned counter)
1055 counts_entry_t *entry, elt;
1057 if (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID))
1058 elt.ident = FUNC_DECL_GLOBAL_ID (func);
1059 else
1061 gcc_assert (coverage_node_map_initialized_p ());
1062 elt.ident = cgraph_get_node (func->decl)->profile_id;
1065 elt.ctr = counter;
1066 entry = counts_hash.find (&elt);
1068 return entry;
1071 /* Returns the counters for a particular tag. */
1073 gcov_type *
1074 get_coverage_counts (unsigned counter, unsigned expected,
1075 unsigned cfg_checksum, unsigned lineno_checksum,
1076 const struct gcov_ctr_summary **summary)
1078 counts_entry_t *entry;
1080 /* No hash table, no counts. */
1081 if (!counts_hash.is_created ())
1083 static int warned = 0;
1085 if (!warned++ && dump_enabled_p ())
1086 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
1087 (flag_guess_branch_prob
1088 ? "file %s not found, execution counts estimated\n"
1089 : "file %s not found, execution counts assumed to "
1090 "be zero\n"),
1091 da_file_name);
1092 return NULL;
1095 entry = get_coverage_counts_entry (cfun, counter);
1097 if (!entry || !entry->summary.num)
1099 if (!flag_dyn_ipa && dump_enabled_p ())
1100 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
1101 "no coverage for function %s found",
1102 IDENTIFIER_POINTER
1103 (DECL_ASSEMBLER_NAME (current_function_decl)));
1104 return NULL;
1107 if (entry->cfg_checksum != cfg_checksum
1108 || entry->summary.num != expected)
1110 static int warned = 0;
1111 bool warning_printed = false;
1112 tree id = DECL_ASSEMBLER_NAME (current_function_decl);
1114 warning_printed =
1115 warning_at (input_location, OPT_Wcoverage_mismatch,
1116 "the control flow of function %qE does not match "
1117 "its profile data (counter %qs)", id, ctr_names[counter]);
1118 if (warning_printed && dump_enabled_p ())
1120 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
1121 "use -Wno-error=coverage-mismatch to tolerate "
1122 "the mismatch but performance may drop if the "
1123 "function is hot\n");
1125 if (!seen_error ()
1126 && !warned++)
1128 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
1129 "coverage mismatch ignored\n");
1130 dump_printf (MSG_OPTIMIZED_LOCATIONS,
1131 flag_guess_branch_prob
1132 ? G_("execution counts estimated\n")
1133 : G_("execution counts assumed to be zero\n"));
1134 if (!flag_guess_branch_prob)
1135 dump_printf (MSG_OPTIMIZED_LOCATIONS,
1136 "this can result in poorly optimized code\n");
1140 return NULL;
1142 else if (entry->lineno_checksum != lineno_checksum)
1144 warning (OPT_Wripa_opt_mismatch,
1145 "Source location for function %qE have changed,"
1146 " the profile data may be out of date",
1147 DECL_ASSEMBLER_NAME (current_function_decl));
1150 if (summary)
1151 *summary = &entry->summary;
1153 return entry->counts;
1156 /* Returns the coverage data entry for counter type COUNTER of function
1157 FUNC. On return, *N_COUNTS is set to the number of entries in the counter. */
1159 gcov_type *
1160 get_coverage_counts_no_warn (struct function *f, unsigned counter, unsigned *n_counts)
1162 counts_entry_t *entry, elt;
1164 /* No hash table, no counts. */
1165 if (!counts_hash.is_created () || !f)
1166 return NULL;
1168 if (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID))
1169 elt.ident = FUNC_DECL_GLOBAL_ID (f);
1170 else
1172 gcc_assert (coverage_node_map_initialized_p ());
1173 elt.ident = cgraph_get_node (f->decl)->profile_id;
1175 elt.ctr = counter;
1176 entry = counts_hash.find (&elt);
1177 if (!entry)
1178 return NULL;
1180 *n_counts = entry->summary.num;
1181 return entry->counts;
1184 /* Allocate NUM counters of type COUNTER. Returns nonzero if the
1185 allocation succeeded. */
1188 coverage_counter_alloc (unsigned counter, unsigned num)
1190 if (no_coverage)
1191 return 0;
1193 if (!num)
1194 return 1;
1196 if (!fn_v_ctrs[counter])
1198 tree array_type = build_array_type (get_gcov_type (), NULL_TREE);
1200 fn_v_ctrs[counter]
1201 = build_var (current_function_decl, array_type, counter);
1204 fn_b_ctrs[counter] = fn_n_ctrs[counter];
1205 fn_n_ctrs[counter] += num;
1207 fn_ctr_mask |= 1 << counter;
1208 return 1;
1211 /* Generate a tree to access COUNTER NO. */
1213 tree
1214 tree_coverage_counter_ref (unsigned counter, unsigned no)
1216 tree gcov_type_node = get_gcov_type ();
1218 gcc_assert (no < fn_n_ctrs[counter] - fn_b_ctrs[counter]);
1220 no += fn_b_ctrs[counter];
1222 /* "no" here is an array index, scaled to bytes later. */
1223 return build4 (ARRAY_REF, gcov_type_node, fn_v_ctrs[counter],
1224 build_int_cst (integer_type_node, no), NULL, NULL);
1227 /* Generate a tree to access the address of COUNTER NO. */
1229 tree
1230 tree_coverage_counter_addr (unsigned counter, unsigned no)
1232 tree gcov_type_node = get_gcov_type ();
1234 gcc_assert (no < fn_n_ctrs[counter] - fn_b_ctrs[counter]);
1235 no += fn_b_ctrs[counter];
1237 /* "no" here is an array index, scaled to bytes later. */
1238 return build_fold_addr_expr (build4 (ARRAY_REF, gcov_type_node,
1239 fn_v_ctrs[counter],
1240 build_int_cst (integer_type_node, no),
1241 NULL, NULL));
1245 /* Generate a crc32 of a string with specified STR_LEN when it's not 0.
1246 Non-zero STR_LEN should only be seen in LIPO mode. */
1248 static unsigned
1249 crc32_string_1 (unsigned chksum, const char *string, unsigned str_len)
1251 char *dup;
1253 if (!L_IPO_COMP_MODE || str_len == 0)
1254 return crc32_string (chksum, string);
1256 gcc_assert (str_len > 0 && str_len < strlen (string));
1257 dup = xstrdup (string);
1258 dup[str_len] = 0;
1259 chksum = crc32_string (chksum, dup);
1260 free (dup);
1262 return chksum;
1265 /* Generate a checksum for a string. CHKSUM is the current
1266 checksum. */
1268 static unsigned
1269 coverage_checksum_string (unsigned chksum, const char *string)
1271 int i;
1272 char *dup = NULL;
1273 unsigned lipo_orig_str_len = 0;
1275 /* Strip out the ending ".cmo.[0-9]*" string from function
1276 name. Otherwise we will have lineno checksum mismatch. */
1277 if (L_IPO_COMP_MODE)
1279 int len;
1281 i = len = strlen (string);
1282 while (i--)
1283 if ((string[i] < '0' || string[i] > '9'))
1284 break;
1285 if ((i > 5) && (i != len - 1))
1287 if (!strncmp (string + i - 4, ".cmo.", 5))
1288 lipo_orig_str_len = i - 4;
1293 /* Look for everything that looks if it were produced by
1294 get_file_function_name and zero out the second part
1295 that may result from flag_random_seed. This is not critical
1296 as the checksums are used only for sanity checking. */
1297 for (i = 0; string[i]; i++)
1299 int offset = 0;
1300 if (!strncmp (string + i, "_GLOBAL__N_", 11))
1301 offset = 11;
1302 if (!strncmp (string + i, "_GLOBAL__", 9))
1303 offset = 9;
1305 /* C++ namespaces do have scheme:
1306 _GLOBAL__N_<filename>_<wrongmagicnumber>_<magicnumber>functionname
1307 since filename might contain extra underscores there seems
1308 to be no better chance then walk all possible offsets looking
1309 for magicnumber. */
1310 if (offset)
1312 for (i = i + offset; string[i]; i++)
1313 if (string[i]=='_')
1315 int y;
1317 for (y = 1; y < 9; y++)
1318 if (!(string[i + y] >= '0' && string[i + y] <= '9')
1319 && !(string[i + y] >= 'A' && string[i + y] <= 'F'))
1320 break;
1321 if (y != 9 || string[i + 9] != '_')
1322 continue;
1323 for (y = 10; y < 18; y++)
1324 if (!(string[i + y] >= '0' && string[i + y] <= '9')
1325 && !(string[i + y] >= 'A' && string[i + y] <= 'F'))
1326 break;
1327 if (y != 18)
1328 continue;
1329 if (!dup)
1330 string = dup = xstrdup (string);
1331 for (y = 10; y < 18; y++)
1332 dup[i + y] = '0';
1334 break;
1338 chksum = crc32_string_1 (chksum, string, lipo_orig_str_len);
1339 if (dup)
1340 free (dup);
1342 return chksum;
1345 /* Compute checksum for the current function. We generate a CRC32. */
1347 unsigned
1348 coverage_compute_lineno_checksum (void)
1350 tree name;
1351 expanded_location xloc
1352 = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
1353 unsigned chksum = xloc.line;
1354 const char *pathless_filename = xloc.file;
1355 int i;
1356 for (i = strlen (xloc.file); i >= 0; i--)
1357 if (IS_DIR_SEPARATOR (pathless_filename[i]))
1359 pathless_filename += i + 1;
1360 break;
1363 chksum = coverage_checksum_string (chksum, pathless_filename);
1365 /* Note: it is a bad design that C++ FE associate the convertion function type
1366 with the name of the decl. This leads to cross contamination between different
1367 conversion operators in different modules (If conv_type_names map is cleared
1368 at the end of parsing of each module).
1370 For LIPO always use the full mangled name to help disambiguate different
1371 template instantiations. This is important for LIPO because we use the
1372 checksums to identify matching copies of the same COMDAT to handle
1373 missing profiles in the copies not selected by the linker, and to update
1374 indirect call profiles when the target COMDAT is a copy that is not
1375 in the module group. */
1376 if (flag_dyn_ipa)
1377 name = DECL_ASSEMBLER_NAME (current_function_decl);
1378 else
1379 name = DECL_NAME (current_function_decl);
1381 chksum = coverage_checksum_string
1382 (chksum, IDENTIFIER_POINTER (name));
1384 return chksum;
1387 /* Compute profile ID. This is better to be unique in whole program. */
1389 unsigned
1390 coverage_compute_profile_id (struct cgraph_node *n)
1392 expanded_location xloc
1393 = expand_location (DECL_SOURCE_LOCATION (n->decl));
1394 bool use_name_only = (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID) == 0);
1395 unsigned chksum = (use_name_only ? 0 : xloc.line);
1397 chksum = coverage_checksum_string (chksum, xloc.file);
1398 chksum = coverage_checksum_string
1399 (chksum, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n->decl)));
1400 if (!use_name_only && first_global_object_name)
1401 chksum = coverage_checksum_string
1402 (chksum, first_global_object_name);
1403 chksum = coverage_checksum_string
1404 (chksum, aux_base_name);
1406 /* Non-negative integers are hopefully small enough to fit in all targets. */
1407 return chksum & 0x7fffffff;
1410 /* Compute cfg checksum for the current function.
1411 The checksum is calculated carefully so that
1412 source code changes that doesn't affect the control flow graph
1413 won't change the checksum.
1414 This is to make the profile data useable across source code change.
1415 The downside of this is that the compiler may use potentially
1416 wrong profile data - that the source code change has non-trivial impact
1417 on the validity of profile data (e.g. the reversed condition)
1418 but the compiler won't detect the change and use the wrong profile data. */
1420 unsigned
1421 coverage_compute_cfg_checksum (void)
1423 basic_block bb;
1424 unsigned chksum = n_basic_blocks_for_fn (cfun);
1426 FOR_EACH_BB_FN (bb, cfun)
1428 edge e;
1429 edge_iterator ei;
1430 chksum = crc32_byte (chksum, bb->index);
1431 FOR_EACH_EDGE (e, ei, bb->succs)
1433 chksum = crc32_byte (chksum, e->dest->index);
1437 return chksum;
1440 /* Begin output to the notes file for the current function.
1441 Writes the function header. Returns nonzero if data should be output. */
1444 coverage_begin_function (unsigned lineno_checksum, unsigned cfg_checksum)
1446 expanded_location xloc;
1447 unsigned long offset;
1449 /* We don't need to output .gcno file unless we're under -ftest-coverage
1450 (e.g. -fprofile-arcs/generate/use don't need .gcno to work). */
1451 if (no_coverage || !bbg_file_name)
1452 return 0;
1454 xloc = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
1456 /* Announce function */
1457 offset = gcov_write_tag (GCOV_TAG_FUNCTION);
1458 if (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID))
1459 gcov_write_unsigned (FUNC_DECL_FUNC_ID (cfun));
1460 else
1462 gcc_assert (coverage_node_map_initialized_p ());
1463 gcov_write_unsigned (
1464 cgraph_get_node (current_function_decl)->profile_id);
1467 gcov_write_unsigned (lineno_checksum);
1468 gcov_write_unsigned (cfg_checksum);
1469 gcov_write_string (IDENTIFIER_POINTER
1470 (DECL_ASSEMBLER_NAME (current_function_decl)));
1471 gcov_write_string (xloc.file);
1472 gcov_write_unsigned (xloc.line);
1473 gcov_write_length (offset);
1475 return !gcov_is_error ();
1478 /* Finish coverage data for the current function. Verify no output
1479 error has occurred. Save function coverage counts. */
1481 void
1482 coverage_end_function (unsigned lineno_checksum, unsigned cfg_checksum)
1484 unsigned i;
1486 if (bbg_file_name && gcov_is_error ())
1488 warning (0, "error writing %qs", bbg_file_name);
1489 unlink (bbg_file_name);
1490 bbg_file_name = NULL;
1493 if (fn_ctr_mask)
1495 struct coverage_data *item = 0;
1497 /* If the function is extern (i.e. extern inline), then we won't
1498 be outputting it, so don't chain it onto the function
1499 list. */
1500 if (!DECL_EXTERNAL (current_function_decl))
1502 item = ggc_alloc_coverage_data ();
1504 if (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID))
1505 item->ident = FUNC_DECL_FUNC_ID (cfun);
1506 else
1508 if (flag_dyn_ipa)
1509 error ("param=profile-func-internal-id=0 is not"
1510 " supported in LIPO mode. ");
1511 gcc_assert (coverage_node_map_initialized_p ());
1512 item->ident = cgraph_get_node (cfun->decl)->profile_id;
1515 item->lineno_checksum = lineno_checksum;
1516 item->cfg_checksum = cfg_checksum;
1518 item->fn_decl = current_function_decl;
1519 item->next = 0;
1520 *functions_tail = item;
1521 functions_tail = &item->next;
1524 for (i = 0; i != GCOV_COUNTERS; i++)
1526 tree var = fn_v_ctrs[i];
1528 if (item)
1529 item->ctr_vars[i] = var;
1530 if (var)
1532 tree array_type = build_index_type (size_int (fn_n_ctrs[i] - 1));
1533 array_type = build_array_type (get_gcov_type (), array_type);
1534 TREE_TYPE (var) = array_type;
1535 DECL_SIZE (var) = TYPE_SIZE (array_type);
1536 DECL_SIZE_UNIT (var) = TYPE_SIZE_UNIT (array_type);
1537 varpool_finalize_decl (var);
1540 fn_b_ctrs[i] = fn_n_ctrs[i] = 0;
1541 fn_v_ctrs[i] = NULL_TREE;
1543 prg_ctr_mask |= fn_ctr_mask;
1544 fn_ctr_mask = 0;
1548 /* True if a function entry corresponding to the given FN_IDENT
1549 is present in the coverage internal data structures. */
1551 bool
1552 coverage_function_present (unsigned fn_ident)
1554 struct coverage_data *item = functions_head;
1555 while (item && item->ident != fn_ident)
1556 item = item->next;
1557 return item != NULL;
1560 /* Update function and program direct-call coverage counts. */
1562 void
1563 coverage_dc_end_function (void)
1565 tree var;
1567 if (fn_ctr_mask)
1569 const unsigned idx = GCOV_COUNTER_DIRECT_CALL;
1570 struct coverage_data *item = functions_head;
1571 while (item && item->ident != (unsigned) FUNC_DECL_FUNC_ID (cfun))
1572 item = item->next;
1574 /* If a matching function entry hasn't been found, either this function
1575 has had no coverage counts added in the profile pass, or this
1576 is a new function (function versioning, etc). Create a new entry. */
1577 if (!item)
1579 int cnt;
1581 item = ggc_alloc_coverage_data ();
1582 *functions_tail = item;
1583 functions_tail = &item->next;
1584 item->next = 0;
1585 item->ident = FUNC_DECL_FUNC_ID (cfun);
1586 item->fn_decl = current_function_decl;
1587 item->lineno_checksum = coverage_compute_lineno_checksum ();
1588 item->cfg_checksum = coverage_compute_cfg_checksum ();
1589 for (cnt = 0; cnt < GCOV_COUNTERS; cnt++)
1590 item->ctr_vars[cnt] = NULL_TREE;
1593 var = fn_v_ctrs[idx];
1594 item->ctr_vars[idx] = var;
1595 if (var)
1597 tree array_type = build_index_type (size_int (fn_n_ctrs[idx] - 1));
1598 array_type = build_array_type (get_gcov_type (), array_type);
1599 TREE_TYPE (var) = array_type;
1600 DECL_SIZE (var) = TYPE_SIZE (array_type);
1601 DECL_SIZE_UNIT (var) = TYPE_SIZE_UNIT (array_type);
1602 varpool_finalize_decl (var);
1605 fn_n_ctrs[idx] = fn_b_ctrs[idx] = 0;
1606 fn_v_ctrs[idx] = NULL_TREE;
1607 prg_ctr_mask |= fn_ctr_mask;
1608 fn_ctr_mask = 0;
1612 /* Build a coverage variable of TYPE for function FN_DECL. If COUNTER
1613 >= 0 it is a counter array, otherwise it is the function structure. */
1615 static tree
1616 build_var (tree fn_decl, tree type, int counter)
1618 tree var = build_decl (BUILTINS_LOCATION, VAR_DECL, NULL_TREE, type);
1619 const char *fn_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fn_decl));
1620 char *buf;
1621 size_t fn_name_len, len;
1623 fn_name = targetm.strip_name_encoding (fn_name);
1624 fn_name_len = strlen (fn_name);
1625 buf = XALLOCAVEC (char, fn_name_len + 8 + sizeof (int) * 3);
1627 if (counter < 0)
1628 strcpy (buf, "__gcov__");
1629 else
1630 sprintf (buf, "__gcov%u_", counter);
1631 len = strlen (buf);
1632 #ifndef NO_DOT_IN_LABEL
1633 buf[len - 1] = '.';
1634 #elif !defined NO_DOLLAR_IN_LABEL
1635 buf[len - 1] = '$';
1636 #endif
1637 memcpy (buf + len, fn_name, fn_name_len + 1);
1638 DECL_NAME (var) = get_identifier (buf);
1639 TREE_STATIC (var) = 1;
1640 TREE_ADDRESSABLE (var) = 1;
1641 DECL_NONALIASED (var) = 1;
1642 DECL_ALIGN (var) = TYPE_ALIGN (type);
1644 return var;
1647 /* Creates the gcov_fn_info RECORD_TYPE. */
1649 static void
1650 build_fn_info_type (tree type, unsigned counters, tree gcov_info_type)
1652 tree ctr_info = lang_hooks.types.make_type (RECORD_TYPE);
1653 tree field, fields;
1654 tree array_type;
1656 gcc_assert (counters);
1658 /* ctr_info::num */
1659 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1660 get_gcov_unsigned_t ());
1661 fields = field;
1663 /* ctr_info::values */
1664 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1665 build_pointer_type (get_gcov_type ()));
1666 DECL_CHAIN (field) = fields;
1667 fields = field;
1669 finish_builtin_struct (ctr_info, "__gcov_ctr_info", fields, NULL_TREE);
1671 /* key */
1672 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1673 build_pointer_type (build_qualified_type
1674 (gcov_info_type, TYPE_QUAL_CONST)));
1675 fields = field;
1677 /* ident */
1678 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1679 get_gcov_unsigned_t ());
1680 DECL_CHAIN (field) = fields;
1681 fields = field;
1683 /* lineno_checksum */
1684 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1685 get_gcov_unsigned_t ());
1686 DECL_CHAIN (field) = fields;
1687 fields = field;
1689 /* cfg checksum */
1690 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1691 get_gcov_unsigned_t ());
1692 DECL_CHAIN (field) = fields;
1693 fields = field;
1695 array_type = build_index_type (size_int (counters - 1));
1696 array_type = build_array_type (ctr_info, array_type);
1698 /* counters */
1699 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE, array_type);
1700 DECL_CHAIN (field) = fields;
1701 fields = field;
1703 finish_builtin_struct (type, "__gcov_fn_info", fields, NULL_TREE);
1706 /* Returns a CONSTRUCTOR for a gcov_fn_info. DATA is
1707 the coverage data for the function and TYPE is the gcov_fn_info
1708 RECORD_TYPE. KEY is the object file key. */
1710 static tree
1711 build_fn_info (const struct coverage_data *data, tree type, tree key)
1713 tree fields = TYPE_FIELDS (type);
1714 tree ctr_type;
1715 unsigned ix;
1716 vec<constructor_elt, va_gc> *v1 = NULL;
1717 vec<constructor_elt, va_gc> *v2 = NULL;
1719 /* key */
1720 CONSTRUCTOR_APPEND_ELT (v1, fields,
1721 build1 (ADDR_EXPR, TREE_TYPE (fields), key));
1722 fields = DECL_CHAIN (fields);
1724 /* ident */
1725 CONSTRUCTOR_APPEND_ELT (v1, fields,
1726 build_int_cstu (get_gcov_unsigned_t (),
1727 data->ident));
1728 fields = DECL_CHAIN (fields);
1730 /* lineno_checksum */
1731 CONSTRUCTOR_APPEND_ELT (v1, fields,
1732 build_int_cstu (get_gcov_unsigned_t (),
1733 data->lineno_checksum));
1734 fields = DECL_CHAIN (fields);
1736 /* cfg_checksum */
1737 CONSTRUCTOR_APPEND_ELT (v1, fields,
1738 build_int_cstu (get_gcov_unsigned_t (),
1739 data->cfg_checksum));
1740 fields = DECL_CHAIN (fields);
1742 /* counters */
1743 ctr_type = TREE_TYPE (TREE_TYPE (fields));
1744 for (ix = 0; ix != GCOV_COUNTERS; ix++)
1745 if (prg_ctr_mask & (1 << ix))
1747 vec<constructor_elt, va_gc> *ctr = NULL;
1748 tree var = data->ctr_vars[ix];
1749 unsigned count = 0;
1751 if (var)
1752 count
1753 = tree_to_shwi (TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (var))))
1754 + 1;
1756 CONSTRUCTOR_APPEND_ELT (ctr, TYPE_FIELDS (ctr_type),
1757 build_int_cstu (get_gcov_unsigned_t (),
1758 count));
1760 if (var)
1761 CONSTRUCTOR_APPEND_ELT (ctr, DECL_CHAIN (TYPE_FIELDS (ctr_type)),
1762 build_fold_addr_expr (var));
1764 CONSTRUCTOR_APPEND_ELT (v2, NULL, build_constructor (ctr_type, ctr));
1766 /* In LIPO mode, coverage_finish is called late when pruning can not be
1767 * done, so we need to force emitting counter variables even for
1768 * eliminated functions to avoid unsat. */
1769 if (flag_dyn_ipa && var)
1770 varpool_finalize_decl (var);
1773 CONSTRUCTOR_APPEND_ELT (v1, fields,
1774 build_constructor (TREE_TYPE (fields), v2));
1776 return build_constructor (type, v1);
1779 /* Create gcov_info struct. TYPE is the incomplete RECORD_TYPE to be
1780 completed, and FN_INFO_PTR_TYPE is a pointer to the function info type. */
1782 static void
1783 build_info_type (tree type, tree fn_info_ptr_type)
1785 tree field, fields = NULL_TREE;
1786 tree merge_fn_type, mod_type;
1787 tree string_type, string_ptr_type;
1789 /* Version ident */
1790 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1791 get_gcov_unsigned_t ());
1792 DECL_CHAIN (field) = fields;
1793 fields = field;
1795 /* mod_info */
1796 mod_type = build_gcov_module_info_type ();
1797 mod_type = build_pointer_type (mod_type);
1798 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE, mod_type);
1799 DECL_CHAIN (field) = fields;
1800 fields = field;
1802 /* next pointer */
1803 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1804 build_pointer_type (build_qualified_type
1805 (type, TYPE_QUAL_CONST)));
1806 DECL_CHAIN (field) = fields;
1807 fields = field;
1809 /* stamp */
1810 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1811 get_gcov_unsigned_t ());
1812 DECL_CHAIN (field) = fields;
1813 fields = field;
1815 /* Filename */
1816 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1817 build_pointer_type (build_qualified_type
1818 (char_type_node, TYPE_QUAL_CONST)));
1819 DECL_CHAIN (field) = fields;
1820 fields = field;
1822 /* eof_pos */
1823 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1824 NULL_TREE, get_gcov_unsigned_t ());
1825 DECL_CHAIN (field) = fields;
1826 fields = field;
1828 /* merge fn array */
1829 merge_fn_type
1830 = build_function_type_list (void_type_node,
1831 build_pointer_type (get_gcov_type ()),
1832 get_gcov_unsigned_t (), NULL_TREE);
1833 merge_fn_type
1834 = build_array_type (build_pointer_type (merge_fn_type),
1835 build_index_type (size_int (GCOV_COUNTERS - 1)));
1836 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1837 merge_fn_type);
1838 DECL_CHAIN (field) = fields;
1839 fields = field;
1841 /* n_functions */
1842 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1843 get_gcov_unsigned_t ());
1844 DECL_CHAIN (field) = fields;
1845 fields = field;
1847 /* function_info pointer pointer */
1848 fn_info_ptr_type = build_pointer_type
1849 (build_qualified_type (fn_info_ptr_type, TYPE_QUAL_CONST));
1850 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
1851 fn_info_ptr_type);
1852 DECL_CHAIN (field) = fields;
1853 fields = field;
1855 /* build_info string array */
1856 string_type = build_pointer_type (
1857 build_qualified_type (char_type_node,
1858 TYPE_QUAL_CONST));
1859 string_ptr_type = build_pointer_type
1860 (build_qualified_type (string_type, TYPE_QUAL_CONST));
1861 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1862 NULL_TREE, string_ptr_type);
1863 DECL_CHAIN (field) = fields;
1864 fields = field;
1866 finish_builtin_struct (type, "__gcov_info", fields, NULL_TREE);
1869 /* Compute an array (tree) of include path strings. STRING_TYPE is
1870 the path string type, INC_PATH_VALUE is the initial value of the
1871 path array, PATHS gives raw path string values, and NUM is the
1872 number of paths. */
1874 static void
1875 build_inc_path_array_value (tree string_type, vec<constructor_elt, va_gc> **v,
1876 cpp_dir *paths, int num)
1878 int i;
1879 cpp_dir *pdir;
1880 for (i = 0, pdir = paths; i < num; pdir = pdir->next)
1882 const char *path_raw_string;
1883 int path_string_length;
1884 tree path_string;
1885 path_raw_string = pdir->name;
1886 path_string_length = strlen (path_raw_string);
1887 path_string = build_string (path_string_length + 1, path_raw_string);
1888 TREE_TYPE (path_string) = build_array_type
1889 (char_type_node, build_index_type
1890 (build_int_cst (NULL_TREE, path_string_length)));
1891 CONSTRUCTOR_APPEND_ELT (*v, NULL,
1892 build1 (ADDR_EXPR, string_type, path_string));
1893 i++;
1897 /* Compute an array (tree) of strings. STR_TYPE is the string type,
1898 STR_ARRAY_VALUE is the initial value of the string array, and HEAD gives
1899 the list of raw strings. */
1901 static void
1902 build_str_array_value (tree str_type, vec<constructor_elt, va_gc> **v,
1903 struct str_list *head)
1905 const char *raw_str;
1906 int str_length;
1907 while (head)
1909 tree str;
1910 raw_str = head->str;
1911 str_length = strlen (raw_str);
1912 str = build_string (str_length + 1, raw_str);
1913 TREE_TYPE (str) =
1914 build_array_type (char_type_node,
1915 build_index_type (build_int_cst (NULL_TREE,
1916 str_length)));
1917 CONSTRUCTOR_APPEND_ELT (*v, NULL,
1918 build1 (ADDR_EXPR, str_type, str));
1919 head = head->next;
1921 return;
1924 /* Compute an array (tree) of command-line argument strings. STRING_TYPE is
1925 the string type, CL_ARGS_VALUE is the initial value of the command-line
1926 args array. */
1928 static void
1929 build_cl_args_array_value (tree string_type, vec<constructor_elt, va_gc> **v)
1931 unsigned int i;
1933 for (i = 0; i < num_lipo_cl_args; i++)
1935 int arg_length = strlen (lipo_cl_args[i]);
1936 tree arg_string = build_string (arg_length + 1, lipo_cl_args[i]);
1937 TREE_TYPE (arg_string) =
1938 build_array_type (char_type_node,
1939 build_index_type (build_int_cst (NULL_TREE,
1940 arg_length)));
1941 CONSTRUCTOR_APPEND_ELT (*v, NULL,
1942 build1 (ADDR_EXPR, string_type, arg_string));
1944 return;
1947 /* Emit mapping between module name and function id to the function's
1948 assembler name, for use in correlating function idents in the gcda file
1949 with the function name. */
1951 void
1952 emit_function_name (void)
1954 fprintf (stderr, "Module %s FuncId %u Name %s\n",
1955 (L_IPO_COMP_MODE
1956 ? get_module_name (FUNC_DECL_MODULE_ID (cfun))
1957 : main_input_file_name),
1958 FUNC_DECL_FUNC_ID (cfun),
1959 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl)));
1962 /* Returns the type of the module info associated with the
1963 current source module being compiled. */
1965 static tree
1966 build_gcov_module_info_type (void)
1968 tree type, field, fields = NULL_TREE;
1969 tree string_type, index_type, string_array_type;
1971 cpp_dir *quote_paths, *bracket_paths, *system_paths, *pdir;
1972 int num_quote_paths = 0, num_bracket_paths = 0, num_system_paths = 0;
1974 type = lang_hooks.types.make_type (RECORD_TYPE);
1975 string_type = build_pointer_type (
1976 build_qualified_type (char_type_node,
1977 TYPE_QUAL_CONST));
1979 /* ident */
1980 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1981 NULL_TREE, get_gcov_unsigned_t ());
1982 DECL_CHAIN (field) = fields;
1983 fields = field;
1985 /* is_primary */
1986 /* We also overload this field to store a flag that indicates whether this
1987 module was built in regular FDO or LIPO mode (-fripa). When reading this
1988 field from a GCDA file, it should be used as the IS_PRIMARY flag. When
1989 reading this field from the binary's data section, it should be used
1990 as a FDO/LIPO flag. */
1991 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1992 NULL_TREE, get_gcov_unsigned_t ());
1993 DECL_CHAIN (field) = fields;
1994 fields = field;
1996 /* flags: is_exported and include_all_aux flag. */
1997 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1998 NULL_TREE, get_gcov_unsigned_t ());
1999 DECL_CHAIN (field) = fields;
2000 fields = field;
2002 /* lang field */
2003 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
2004 NULL_TREE, get_gcov_unsigned_t ());
2005 DECL_CHAIN (field) = fields;
2006 fields = field;
2008 /* ggc_memory field */
2009 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
2010 NULL_TREE, get_gcov_unsigned_t ());
2011 DECL_CHAIN (field) = fields;
2012 fields = field;
2014 /* da_filename */
2015 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
2016 NULL_TREE, string_type);
2017 DECL_CHAIN (field) = fields;
2018 fields = field;
2020 /* Source name */
2021 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
2022 NULL_TREE, string_type);
2023 DECL_CHAIN (field) = fields;
2024 fields = field;
2026 /* Num quote paths */
2027 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
2028 NULL_TREE, get_gcov_unsigned_t ());
2029 DECL_CHAIN (field) = fields;
2030 fields = field;
2032 /* Num bracket paths */
2033 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
2034 NULL_TREE, get_gcov_unsigned_t ());
2035 DECL_CHAIN (field) = fields;
2036 fields = field;
2038 /* Num system paths */
2039 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
2040 NULL_TREE, get_gcov_unsigned_t ());
2041 DECL_CHAIN (field) = fields;
2042 fields = field;
2044 /* Num -D/-U options. */
2045 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
2046 NULL_TREE, get_gcov_unsigned_t ());
2047 DECL_CHAIN (field) = fields;
2048 fields = field;
2050 /* Num -imacro/-include options. */
2051 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
2052 get_gcov_unsigned_t ());
2053 DECL_CHAIN (field) = fields;
2054 fields = field;
2056 /* Num command-line args. */
2057 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
2058 NULL_TREE, get_gcov_unsigned_t ());
2059 DECL_CHAIN (field) = fields;
2060 fields = field;
2062 get_include_chains (&quote_paths, &bracket_paths, &system_paths);
2063 for (pdir = quote_paths; pdir; pdir = pdir->next)
2065 if (pdir == bracket_paths)
2066 break;
2067 num_quote_paths++;
2069 for (pdir = bracket_paths; pdir; pdir = pdir->next)
2071 if (pdir == system_paths)
2072 break;
2073 num_bracket_paths++;
2075 for (pdir = system_paths; pdir; pdir = pdir->next)
2076 num_system_paths++;
2078 /* string array */
2079 index_type = build_index_type (build_int_cst (NULL_TREE,
2080 num_quote_paths +
2081 num_bracket_paths +
2082 num_system_paths +
2083 num_cpp_defines +
2084 num_cpp_includes +
2085 num_lipo_cl_args));
2087 string_array_type = build_array_type (string_type, index_type);
2088 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
2089 NULL_TREE, string_array_type);
2090 DECL_CHAIN (field) = fields;
2091 fields = field;
2093 finish_builtin_struct (type, "__gcov_module_info", fields, NULL_TREE);
2095 return type;
2098 /* Returns the value of the module info associated with the
2099 current source module being compiled. */
2101 static tree
2102 build_gcov_module_info_value (tree mod_type)
2104 tree info_fields, mod_info;
2105 tree value = NULL_TREE;
2106 int file_name_len;
2107 tree filename_string, string_array_type, string_type;
2108 cpp_dir *quote_paths, *bracket_paths, *system_paths, *pdir;
2109 int num_quote_paths = 0, num_bracket_paths = 0, num_system_paths = 0;
2110 unsigned lang;
2111 char name_buf[50];
2112 vec<constructor_elt,va_gc> *v = NULL, *path_v = NULL;
2114 info_fields = TYPE_FIELDS (mod_type);
2116 /* ident */
2117 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2118 build_int_cstu (get_gcov_unsigned_t (), 0));
2120 info_fields = DECL_CHAIN (info_fields);
2122 /* is_primary */
2123 /* We also overload this field to store a flag that indicates whether this
2124 module was built in regular FDO or LIPO mode (-fripa). When reading this
2125 field from a GCDA file, it should be used as the IS_PRIMARY flag. When
2126 reading this field from the binary's data section, it should be used
2127 as a FDO/LIPO flag. */
2128 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2129 build_int_cstu (get_gcov_unsigned_t (),
2130 flag_dyn_ipa ? 1 : 0));
2131 info_fields = DECL_CHAIN (info_fields);
2133 /* flags */
2134 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2135 build_int_cstu (get_gcov_unsigned_t (), 0));
2136 info_fields = DECL_CHAIN (info_fields);
2138 /* lang field */
2139 if (!strcmp (lang_hooks.name, "GNU C"))
2140 lang = GCOV_MODULE_C_LANG;
2141 else if (!strcmp (lang_hooks.name, "GNU C++"))
2142 lang = GCOV_MODULE_CPP_LANG;
2143 else
2144 lang = GCOV_MODULE_UNKNOWN_LANG;
2145 if (has_asm_statement)
2146 lang |= GCOV_MODULE_ASM_STMTS;
2148 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2149 build_int_cstu (get_gcov_unsigned_t (), lang));
2150 info_fields = DECL_CHAIN (info_fields);
2152 /* ggc_memory field */
2153 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2154 build_int_cstu (get_gcov_unsigned_t (), ggc_total_memory));
2155 info_fields = DECL_CHAIN (info_fields);
2157 /* da_filename */
2159 string_type = TREE_TYPE (info_fields);
2160 file_name_len = strlen (da_base_file_name);
2161 filename_string = build_string (file_name_len + 1, da_base_file_name);
2162 TREE_TYPE (filename_string) = build_array_type
2163 (char_type_node, build_index_type
2164 (build_int_cst (NULL_TREE, file_name_len)));
2165 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2166 build1 (ADDR_EXPR, string_type, filename_string));
2167 info_fields = DECL_CHAIN (info_fields);
2169 /* Source name */
2171 file_name_len = strlen (main_input_file_name);
2172 filename_string = build_string (file_name_len + 1, main_input_file_name);
2173 TREE_TYPE (filename_string) = build_array_type
2174 (char_type_node, build_index_type
2175 (build_int_cst (NULL_TREE, file_name_len)));
2176 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2177 build1 (ADDR_EXPR, string_type, filename_string));
2178 info_fields = DECL_CHAIN (info_fields);
2180 get_include_chains (&quote_paths, &bracket_paths, &system_paths);
2181 for (pdir = quote_paths; pdir; pdir = pdir->next)
2183 if (pdir == bracket_paths)
2184 break;
2185 num_quote_paths++;
2187 for (pdir = bracket_paths; pdir; pdir = pdir->next)
2189 if (pdir == system_paths)
2190 break;
2191 num_bracket_paths++;
2193 for (pdir = system_paths; pdir; pdir = pdir->next)
2194 num_system_paths++;
2196 /* Num quote paths */
2197 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2198 build_int_cstu (get_gcov_unsigned_t (),
2199 num_quote_paths));
2200 info_fields = DECL_CHAIN (info_fields);
2202 /* Num bracket paths */
2203 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2204 build_int_cstu (get_gcov_unsigned_t (),
2205 num_bracket_paths));
2206 info_fields = DECL_CHAIN (info_fields);
2208 /* Num system paths */
2209 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2210 build_int_cstu (get_gcov_unsigned_t (),
2211 num_system_paths));
2212 info_fields = DECL_CHAIN (info_fields);
2214 /* Num -D/-U options. */
2215 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2216 build_int_cstu (get_gcov_unsigned_t (),
2217 num_cpp_defines));
2218 info_fields = DECL_CHAIN (info_fields);
2220 /* Num -imacro/-include options. */
2221 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2222 build_int_cstu (get_gcov_unsigned_t (),
2223 num_cpp_includes));
2224 info_fields = DECL_CHAIN (info_fields);
2226 /* Num command-line args. */
2227 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2228 build_int_cstu (get_gcov_unsigned_t (),
2229 num_lipo_cl_args));
2230 info_fields = DECL_CHAIN (info_fields);
2232 /* string array */
2233 string_array_type = TREE_TYPE (info_fields);
2234 build_inc_path_array_value (string_type, &path_v,
2235 quote_paths, num_quote_paths);
2236 build_inc_path_array_value (string_type, &path_v,
2237 bracket_paths, num_bracket_paths);
2238 build_inc_path_array_value (string_type, &path_v,
2239 system_paths, num_system_paths);
2240 build_str_array_value (string_type, &path_v,
2241 cpp_defines_head);
2242 build_str_array_value (string_type, &path_v,
2243 cpp_includes_head);
2244 build_cl_args_array_value (string_type, &path_v);
2245 CONSTRUCTOR_APPEND_ELT (v, info_fields,
2246 build_constructor (string_array_type, path_v));
2247 info_fields = DECL_CHAIN (info_fields);
2249 gcc_assert (!info_fields);
2250 value = build_constructor (mod_type, v);
2252 mod_info = build_decl (BUILTINS_LOCATION, VAR_DECL,
2253 NULL_TREE, TREE_TYPE (value));
2254 TREE_STATIC (mod_info) = 1;
2255 ASM_GENERATE_INTERNAL_LABEL (name_buf, "MODINFO", 0);
2256 DECL_NAME (mod_info) = get_identifier (name_buf);
2257 DECL_INITIAL (mod_info) = value;
2259 /* Build structure. */
2260 varpool_finalize_decl (mod_info);
2262 return mod_info;
2265 /* Returns the value of the build info string read earlier. */
2267 static tree
2268 build_gcov_build_info_value (void)
2270 tree build_info;
2271 tree value = NULL_TREE;
2272 tree string_type, index_type, string_array_type;
2273 vec<constructor_elt,va_gc> *v = NULL;
2274 char name_buf[50];
2276 string_type = build_pointer_type (
2277 build_qualified_type (char_type_node,
2278 TYPE_QUAL_CONST));
2279 index_type = build_index_type (build_int_cst (NULL_TREE, num_build_info));
2280 string_array_type = build_array_type (string_type, index_type);
2282 build_str_array_value (string_type, &v,
2283 build_info_array_head);
2284 value = build_constructor (string_array_type, v);
2286 build_info = build_decl (BUILTINS_LOCATION, VAR_DECL,
2287 NULL_TREE, TREE_TYPE (value));
2288 TREE_STATIC (build_info) = 1;
2289 ASM_GENERATE_INTERNAL_LABEL (name_buf, "BUILDINFO", 0);
2290 DECL_NAME (build_info) = get_identifier (name_buf);
2291 DECL_INITIAL (build_info) = value;
2293 /* Build structure. */
2294 varpool_finalize_decl (build_info);
2296 return build_info;
2299 /* Add S to the end of the string-list, the head and tail of which are
2300 pointed-to by HEAD and TAIL, respectively. */
2302 static void
2303 str_list_append (struct str_list **head, struct str_list **tail, const char *s)
2305 struct str_list *e = XNEW (struct str_list);
2306 e->str = XNEWVEC (char, strlen (s) + 1);
2307 strcpy (e->str, s);
2308 e->next = NULL;
2309 if (*tail)
2310 (*tail)->next = e;
2311 else
2312 *head = e;
2313 *tail = e;
2316 /* Read file specified to -fprofile-generate-buildinfo=filename option and
2317 create a list of strings to include in build_info array. */
2319 static void
2320 read_buildinfo (void)
2322 char buf[1024];
2323 FILE *buildinfo = fopen (flag_profile_generate_buildinfo, "r");
2324 if (!buildinfo)
2326 error ("could not open -fprofile-generate-buildinfo file %qs: %m",
2327 flag_profile_generate_buildinfo);
2330 while (fgets (buf, sizeof buf, buildinfo) != NULL)
2332 /* Remove end of line. */
2333 int len = strlen (buf);
2334 if (len >= 1 && buf[len - 1] =='\n')
2335 buf[len - 1] = '\0';
2336 str_list_append (&build_info_array_head, &build_info_array_tail, buf);
2337 num_build_info++;
2339 /* Terminate with an empty string. */
2340 str_list_append (&build_info_array_head, &build_info_array_tail, "");
2341 num_build_info++;
2342 if (ferror (buildinfo))
2344 error ("error reading -fprofile-generate-buildinfo file %qs: %m",
2345 flag_profile_generate_buildinfo);
2348 if (fclose (buildinfo))
2350 error ("could not close -fprofile-generate-buildinfo file %qs: %m",
2351 flag_profile_generate_buildinfo);
2355 /* Returns a CONSTRUCTOR for the gcov_info object. INFO_TYPE is the
2356 gcov_info structure type, FN_ARY is the array of pointers to
2357 function info objects. */
2359 static tree
2360 build_info (tree info_type, tree fn_ary)
2362 tree info_fields = TYPE_FIELDS (info_type);
2363 tree merge_fn_type, n_funcs;
2364 unsigned ix;
2365 tree mod_value = NULL_TREE;
2366 tree buildinfo_value = NULL_TREE;
2367 tree filename_string;
2368 int da_file_name_len;
2369 vec<constructor_elt, va_gc> *v1 = NULL;
2370 vec<constructor_elt, va_gc> *v2 = NULL;
2372 /* Version ident */
2373 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
2374 build_int_cstu (TREE_TYPE (info_fields),
2375 GCOV_VERSION));
2376 info_fields = DECL_CHAIN (info_fields);
2378 /* mod_info */
2379 mod_value = build_gcov_module_info_value (TREE_TYPE (TREE_TYPE (info_fields)));
2380 mod_value = build1 (ADDR_EXPR,
2381 build_pointer_type (TREE_TYPE (mod_value)),
2382 mod_value);
2383 CONSTRUCTOR_APPEND_ELT (v1, info_fields, mod_value);
2384 info_fields = DECL_CHAIN (info_fields);
2386 /* next -- NULL */
2387 CONSTRUCTOR_APPEND_ELT (v1, info_fields, null_pointer_node);
2388 info_fields = DECL_CHAIN (info_fields);
2390 /* stamp */
2391 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
2392 build_int_cstu (TREE_TYPE (info_fields),
2393 bbg_file_stamp));
2394 info_fields = DECL_CHAIN (info_fields);
2396 /* Filename */
2397 da_file_name_len = strlen (da_file_name);
2398 filename_string = build_string (da_file_name_len + 1, da_file_name);
2399 TREE_TYPE (filename_string) = build_array_type
2400 (char_type_node, build_index_type (size_int (da_file_name_len)));
2401 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
2402 build1 (ADDR_EXPR, TREE_TYPE (info_fields),
2403 filename_string));
2404 info_fields = DECL_CHAIN (info_fields);
2406 /* eof_pos */
2407 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
2408 build_int_cstu (TREE_TYPE (info_fields), 0));
2409 info_fields = DECL_CHAIN (info_fields);
2411 /* merge fn array -- NULL slots indicate unmeasured counters */
2412 merge_fn_type = TREE_TYPE (TREE_TYPE (info_fields));
2413 for (ix = 0; ix != GCOV_COUNTERS; ix++)
2415 tree ptr = null_pointer_node;
2417 if ((1u << ix) & prg_ctr_mask)
2419 tree merge_fn = build_decl (BUILTINS_LOCATION,
2420 FUNCTION_DECL,
2421 get_identifier (ctr_merge_functions[ix]),
2422 TREE_TYPE (merge_fn_type));
2423 DECL_EXTERNAL (merge_fn) = 1;
2424 TREE_PUBLIC (merge_fn) = 1;
2425 DECL_ARTIFICIAL (merge_fn) = 1;
2426 TREE_NOTHROW (merge_fn) = 1;
2427 /* Initialize assembler name so we can stream out. */
2428 DECL_ASSEMBLER_NAME (merge_fn);
2429 ptr = build1 (ADDR_EXPR, merge_fn_type, merge_fn);
2431 CONSTRUCTOR_APPEND_ELT (v2, NULL, ptr);
2433 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
2434 build_constructor (TREE_TYPE (info_fields), v2));
2435 info_fields = DECL_CHAIN (info_fields);
2437 /* n_functions */
2438 n_funcs = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (fn_ary)));
2439 n_funcs = fold_build2 (PLUS_EXPR, TREE_TYPE (info_fields),
2440 n_funcs, size_one_node);
2441 CONSTRUCTOR_APPEND_ELT (v1, info_fields, n_funcs);
2442 info_fields = DECL_CHAIN (info_fields);
2444 /* functions */
2445 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
2446 build1 (ADDR_EXPR, TREE_TYPE (info_fields), fn_ary));
2447 info_fields = DECL_CHAIN (info_fields);
2449 /* build_info string array */
2450 if (flag_profile_generate_buildinfo)
2451 read_buildinfo ();
2452 if (num_build_info)
2454 buildinfo_value = build_gcov_build_info_value ();
2455 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
2456 build1 (ADDR_EXPR, TREE_TYPE (info_fields),
2457 buildinfo_value));
2459 else
2460 CONSTRUCTOR_APPEND_ELT (v1, info_fields, null_pointer_node);
2461 info_fields = DECL_CHAIN (info_fields);
2463 gcc_assert (!info_fields);
2464 return build_constructor (info_type, v1);
2467 /* Generate the constructor function to call __gcov_init. */
2469 static void
2470 build_init_ctor (tree gcov_info_type)
2472 tree ctor, stmt, init_fn;
2474 /* Build a decl for __gcov_init. */
2475 init_fn = build_pointer_type (gcov_info_type);
2476 init_fn = build_function_type_list (void_type_node, init_fn, NULL);
2477 init_fn = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
2478 get_identifier ("__gcov_init"), init_fn);
2479 TREE_PUBLIC (init_fn) = 1;
2480 DECL_EXTERNAL (init_fn) = 1;
2481 DECL_ASSEMBLER_NAME (init_fn);
2483 /* Generate a call to __gcov_init(&gcov_info). */
2484 ctor = NULL;
2485 stmt = build_fold_addr_expr (gcov_info_var);
2486 stmt = build_call_expr (init_fn, 1, stmt);
2487 append_to_statement_list (stmt, &ctor);
2489 /* Generate a constructor to run it. */
2490 cgraph_build_static_cdtor ('I', ctor, DEFAULT_INIT_PRIORITY);
2493 /* Create the gcov_info types and object. Generate the constructor
2494 function to call __gcov_init. Does not generate the initializer
2495 for the object. Returns TRUE if coverage data is being emitted. */
2497 static bool
2498 coverage_obj_init (void)
2500 tree gcov_info_type;
2501 unsigned n_counters = 0;
2502 unsigned ix;
2503 struct coverage_data *fn;
2504 struct coverage_data **fn_prev;
2505 char name_buf[32];
2507 no_coverage = 1; /* Disable any further coverage. */
2509 if (!prg_ctr_mask)
2510 return false;
2512 if (cgraph_dump_file)
2513 fprintf (cgraph_dump_file, "Using data file %s\n", da_file_name);
2515 /* Prune functions. */
2516 if (!flag_dyn_ipa)
2517 /* in lipo mode, coverage_finish is called when function struct is cleared,
2518 so pruning code here will skip all functions. */
2519 for (fn_prev = &functions_head; (fn = *fn_prev);)
2520 if (DECL_STRUCT_FUNCTION (fn->fn_decl))
2521 fn_prev = &fn->next;
2522 else
2523 /* The function is not being emitted, remove from list. */
2524 *fn_prev = fn->next;
2526 if (functions_head == NULL)
2527 return false;
2529 for (ix = 0; ix != GCOV_COUNTERS; ix++)
2530 if ((1u << ix) & prg_ctr_mask)
2531 n_counters++;
2533 /* Build the info and fn_info types. These are mutually recursive. */
2534 gcov_info_type = lang_hooks.types.make_type (RECORD_TYPE);
2535 gcov_fn_info_type = lang_hooks.types.make_type (RECORD_TYPE);
2536 gcov_fn_info_ptr_type = build_pointer_type
2537 (build_qualified_type (gcov_fn_info_type, TYPE_QUAL_CONST));
2538 build_fn_info_type (gcov_fn_info_type, n_counters, gcov_info_type);
2539 build_info_type (gcov_info_type, gcov_fn_info_ptr_type);
2541 /* Build the gcov info var, this is referred to in its own
2542 initializer. */
2543 gcov_info_var = build_decl (BUILTINS_LOCATION,
2544 VAR_DECL, NULL_TREE, gcov_info_type);
2545 TREE_STATIC (gcov_info_var) = 1;
2546 ASM_GENERATE_INTERNAL_LABEL (name_buf, "LPBX", 0);
2547 DECL_NAME (gcov_info_var) = get_identifier (name_buf);
2549 return true;
2552 /* Generate the coverage function info for FN and DATA. Append a
2553 pointer to that object to CTOR and return the appended CTOR. */
2555 static vec<constructor_elt, va_gc> *
2556 coverage_obj_fn (vec<constructor_elt, va_gc> *ctor, tree fn,
2557 struct coverage_data const *data)
2559 tree init = build_fn_info (data, gcov_fn_info_type, gcov_info_var);
2560 tree var = build_var (fn, gcov_fn_info_type, -1);
2562 DECL_INITIAL (var) = init;
2563 varpool_finalize_decl (var);
2565 CONSTRUCTOR_APPEND_ELT (ctor, NULL,
2566 build1 (ADDR_EXPR, gcov_fn_info_ptr_type, var));
2567 return ctor;
2570 /* Finalize the coverage data. Generates the array of pointers to
2571 function objects from CTOR. Generate the gcov_info initializer.
2572 Generate the constructor function to call __gcov_init. */
2574 static void
2575 coverage_obj_finish (vec<constructor_elt, va_gc> *ctor)
2577 unsigned n_functions = vec_safe_length (ctor);
2578 tree fn_info_ary_type = build_array_type
2579 (build_qualified_type (gcov_fn_info_ptr_type, TYPE_QUAL_CONST),
2580 build_index_type (size_int (n_functions - 1)));
2581 tree fn_info_ary = build_decl (BUILTINS_LOCATION, VAR_DECL, NULL_TREE,
2582 fn_info_ary_type);
2583 char name_buf[32];
2585 TREE_STATIC (fn_info_ary) = 1;
2586 ASM_GENERATE_INTERNAL_LABEL (name_buf, "LPBX", 1);
2587 DECL_NAME (fn_info_ary) = get_identifier (name_buf);
2588 DECL_INITIAL (fn_info_ary) = build_constructor (fn_info_ary_type, ctor);
2589 varpool_finalize_decl (fn_info_ary);
2591 DECL_INITIAL (gcov_info_var)
2592 = build_info (TREE_TYPE (gcov_info_var), fn_info_ary);
2594 build_init_ctor (TREE_TYPE (gcov_info_var));
2596 varpool_finalize_decl (gcov_info_var);
2599 /* Get the da file name, given base file name. */
2601 static char *
2602 get_da_file_name (const char *base_file_name)
2604 char *da_file_name;
2605 int len = strlen (base_file_name);
2606 const char *prefix = profile_data_prefix;
2607 int prefix_len = 0;
2609 if (profile_data_prefix == 0 && !IS_ABSOLUTE_PATH(&base_file_name[0]))
2611 profile_data_prefix = getpwd ();
2612 prefix = profile_data_prefix;
2615 prefix_len = (prefix) ? strlen (prefix) + 1 : 0;
2617 /* Name of da file. */
2618 da_file_name = XNEWVEC (char, len + strlen (GCOV_DATA_SUFFIX)
2619 + prefix_len + 2);
2621 if (prefix)
2623 strcpy (da_file_name, prefix);
2624 da_file_name[prefix_len - 1] = '/';
2625 da_file_name[prefix_len] = 0;
2627 else
2628 da_file_name[0] = 0;
2629 strcat (da_file_name, base_file_name);
2630 if (profile_base_name_suffix_to_strip)
2632 int base_name_len = strlen (da_file_name);
2633 int suffix_to_strip_len = strlen (profile_base_name_suffix_to_strip);
2635 if (base_name_len > suffix_to_strip_len
2636 && !strcmp (da_file_name + (base_name_len - suffix_to_strip_len),
2637 profile_base_name_suffix_to_strip))
2638 da_file_name[base_name_len - suffix_to_strip_len] = '\0';
2641 strcat (da_file_name, GCOV_DATA_SUFFIX);
2642 return da_file_name;
2645 /* Callback to move counts_entry from one hash table to
2646 the target hashtable */
2649 move_hash_entry_callback (counts_entry **x,
2650 hash_table <counts_entry> *target_counts_hash)
2652 counts_entry *entry = *x;
2653 counts_entry **slot;
2654 slot = target_counts_hash->find_slot (entry, INSERT);
2655 *slot = entry;
2656 return 1;
2659 /* Rebuild counts_hash already built the primary module. This hashtable
2660 was built with a module-id of zero. It needs to be rebuilt taking the
2661 correct primary module-id into account. */
2664 rehash_callback (counts_entry **x,
2665 hash_table <counts_entry> *target_counts_hash)
2667 counts_entry *entry = *x;
2668 counts_entry **slot;
2670 entry->ident = GEN_FUNC_GLOBAL_ID (primary_module_id, entry->ident);
2671 slot = target_counts_hash->find_slot (entry, INSERT);
2672 *slot = entry;
2673 return 1;
2676 /* Rebuild counts_hash already built the primary module. This hashtable
2677 was built with a module-id of zero. It needs to be rebuilt taking the
2678 correct primary module-id into account. */
2680 static void
2681 rebuild_counts_hash (void)
2683 hash_table <counts_entry> tmp_counts_hash;
2684 tmp_counts_hash.create (10);
2685 gcc_assert (primary_module_id);
2687 rebuilding_counts_hash = true;
2689 /* Move the counts entries to the temporary hashtable. */
2690 counts_hash.traverse_noresize <
2691 hash_table <counts_entry> *,
2692 move_hash_entry_callback> (&tmp_counts_hash);
2693 counts_hash.empty ();
2695 /* Now rehash and copy back. */
2696 tmp_counts_hash.traverse_noresize <
2697 hash_table <counts_entry> *,
2698 rehash_callback> (&counts_hash);
2699 tmp_counts_hash.dispose();
2701 rebuilding_counts_hash = false;
2704 /* Add the module information record for the module with id
2705 MODULE_ID. IS_PRIMARY is true if the module is the primary module.
2706 INDEX is the index of the new record in the module info array. */
2708 void
2709 add_module_info (unsigned module_id, bool is_primary, int index)
2711 struct gcov_module_info *cur_info;
2712 module_infos = XRESIZEVEC (struct gcov_module_info *,
2713 module_infos, index + 1);
2714 module_infos[index] = XNEW (struct gcov_module_info);
2715 cur_info = module_infos[index];
2716 cur_info->ident = module_id;
2717 SET_MODULE_EXPORTED (cur_info);
2718 cur_info->num_quote_paths = 0;
2719 cur_info->num_bracket_paths = 0;
2720 cur_info->da_filename = NULL;
2721 cur_info->source_filename = NULL;
2722 if (is_primary)
2723 primary_module_id = module_id;
2726 /* Process the include paths needed for parsing the aux modules.
2727 The sub_pattern is in the form SUB_PATH:NEW_SUB_PATH. If it is
2728 defined, the SUB_PATH in ORIG_INC_PATH will be replaced with
2729 NEW_SUB_PATH. */
2731 static void
2732 process_include (char **orig_inc_path, char* old_sub, char *new_sub)
2734 char *inc_path, *orig_sub;
2736 if (strlen (*orig_inc_path) < strlen (old_sub))
2737 return;
2739 inc_path = (char*) xmalloc (strlen (*orig_inc_path) + strlen (new_sub)
2740 - strlen (old_sub) + 1);
2741 orig_sub = strstr (*orig_inc_path, old_sub);
2742 if (!orig_sub)
2744 inform (UNKNOWN_LOCATION, "subpath %s not found in path %s",
2745 old_sub, *orig_inc_path);
2746 free (inc_path);
2747 return;
2750 strncpy (inc_path, *orig_inc_path, orig_sub - *orig_inc_path);
2751 inc_path[orig_sub - *orig_inc_path] = '\0';
2752 strcat (inc_path, new_sub);
2753 strcat (inc_path, orig_sub + strlen (old_sub));
2755 free (*orig_inc_path);
2756 *orig_inc_path = inc_path;
2759 /* Process include paths for MOD_INFO according to option
2760 -fripa-inc-path-sub=OLD_SUB:NEW_SUB */
2762 static void
2763 process_include_paths_1 (struct gcov_module_info *mod_info,
2764 char* old_sub, char *new_sub)
2766 unsigned i, j;
2768 for (i = 0; i < mod_info->num_quote_paths; i++)
2769 process_include (&mod_info->string_array[i], old_sub, new_sub);
2771 for (i = 0, j = mod_info->num_quote_paths;
2772 i < mod_info->num_bracket_paths; i++, j++)
2773 process_include (&mod_info->string_array[j], old_sub, new_sub);
2775 for (i = 0, j = mod_info->num_quote_paths + mod_info->num_bracket_paths +
2776 mod_info->num_cpp_defines; i < mod_info->num_cpp_includes; i++, j++)
2777 process_include (&mod_info->string_array[j], old_sub, new_sub);
2781 /* Process include paths for MOD_INFO according to option
2782 -fripa-inc-path-sub=old_sub1:new_sub1[,old_sub2:new_sub2] */
2784 static void
2785 process_include_paths (struct gcov_module_info *mod_info)
2787 char *sub_pattern, *cur, *next, *new_sub;
2789 if (!lipo_inc_path_pattern)
2790 return;
2792 sub_pattern = xstrdup (lipo_inc_path_pattern);
2793 cur = sub_pattern;
2797 next = strchr (cur, ',');
2798 if (next)
2799 *next++ = '\0';
2800 new_sub = strchr (cur, ':');
2801 if (!new_sub)
2803 error ("Invalid path substibution pattern %s", sub_pattern);
2804 free (sub_pattern);
2805 return;
2807 *new_sub++ = '\0';
2808 process_include_paths_1 (mod_info, cur, new_sub);
2809 cur = next;
2810 } while (cur);
2811 free (sub_pattern);
2814 /* Set the prepreprocessing context (include search paths, -D/-U).
2815 PARSE_IN is the preprocessor reader, I is the index of the module,
2816 and VERBOSE is the verbose flag. */
2818 void
2819 set_lipo_c_parsing_context (struct cpp_reader *parse_in, int i, bool verbose)
2821 struct gcov_module_info *mod_info;
2822 if (!L_IPO_COMP_MODE)
2823 return;
2825 mod_info = module_infos[i];
2827 gcc_assert (flag_dyn_ipa);
2828 current_module_id = mod_info->ident;
2829 reset_funcdef_no ();
2831 if (current_module_id != primary_module_id)
2833 unsigned i, j;
2835 process_include_paths (mod_info);
2836 /* Setup include paths. */
2837 clear_include_chains ();
2838 for (i = 0; i < mod_info->num_quote_paths; i++)
2839 add_path (xstrdup (mod_info->string_array[i]),
2840 QUOTE, 0, 1);
2841 for (i = 0, j = mod_info->num_quote_paths;
2842 i < mod_info->num_bracket_paths; i++, j++)
2843 add_path (xstrdup (mod_info->string_array[j]),
2844 BRACKET, 0, 1);
2845 for (i = 0; i < mod_info->num_system_paths; i++, j++)
2846 add_path (xstrdup (mod_info->string_array[j]),
2847 SYSTEM, 0, 1);
2848 register_include_chains (parse_in, NULL, NULL, NULL,
2849 0, 0, verbose);
2851 /* Setup defines/undefs. */
2852 for (i = 0; i < mod_info->num_cpp_defines; i++, j++)
2853 if (mod_info->string_array[j][0] == 'D')
2854 cpp_define (parse_in, mod_info->string_array[j] + 1);
2855 else
2856 cpp_undef (parse_in, mod_info->string_array[j] + 1);
2858 /* Setup -imacro/-include. */
2859 for (i = 0; i < mod_info->num_cpp_includes; i++, j++)
2860 cpp_push_include (parse_in, mod_info->string_array[j]);
2864 /* Perform file-level initialization. Read in data file, generate name
2865 of graph file. */
2867 void
2868 coverage_init (const char *filename, const char* source_name)
2870 char* src_name_prefix = 0;
2871 int src_name_prefix_len = 0;
2872 int len = strlen (filename);
2874 /* Since coverage_init is invoked very early, before the pass
2875 manager, we need to set up the dumping explicitly. This is
2876 similar to the handling in finish_optimization_passes. */
2877 int profile_pass_num =
2878 g->get_passes ()->get_pass_profile ()->static_pass_number;
2879 g->get_dumps ()->dump_start (profile_pass_num, NULL);
2881 has_asm_statement = false;
2882 da_file_name = get_da_file_name (filename);
2883 da_base_file_name = XNEWVEC (char, strlen (filename) + 1);
2884 strcpy (da_base_file_name, filename);
2886 if (profile_data_prefix == 0 && !IS_ABSOLUTE_PATH (source_name))
2888 src_name_prefix = getpwd ();
2889 src_name_prefix_len = strlen (src_name_prefix) + 1;
2891 main_input_file_name = XNEWVEC (char, strlen (source_name) + 1
2892 + src_name_prefix_len);
2893 if (!src_name_prefix)
2894 strcpy (main_input_file_name, source_name);
2895 else
2897 strcpy (main_input_file_name, src_name_prefix);
2898 strcat (main_input_file_name, "/");
2899 strcat (main_input_file_name, source_name);
2902 bbg_file_stamp = local_tick;
2904 if (flag_branch_probabilities && !flag_auto_profile)
2905 read_counts_file (da_file_name, 0);
2907 /* Rebuild counts_hash and read the auxiliary GCDA files. */
2908 if (flag_profile_use && L_IPO_COMP_MODE)
2910 unsigned i;
2911 gcc_assert (flag_dyn_ipa);
2912 rebuild_counts_hash ();
2913 for (i = 1; i < num_in_fnames; i++)
2914 read_counts_file (get_da_file_name (module_infos[i]->da_filename),
2915 module_infos[i]->ident);
2918 /* Define variables which are referenced at runtime by libgcov. */
2919 if (profiling_enabled_p ())
2921 tree_init_instrumentation ();
2922 tree_init_dyn_ipa_parameters ();
2923 tree_init_instrumentation_sampling ();
2925 if (flag_auto_profile)
2926 init_auto_profile ();
2928 /* Name of bbg file. */
2929 if (flag_test_coverage && !flag_compare_debug)
2931 bbg_file_name = XNEWVEC (char, len + strlen (GCOV_NOTE_SUFFIX) + 1);
2932 memcpy (bbg_file_name, filename, len);
2933 strcpy (bbg_file_name + len, GCOV_NOTE_SUFFIX);
2934 if (!gcov_open (bbg_file_name, -1))
2936 error ("cannot open %s", bbg_file_name);
2937 bbg_file_name = NULL;
2939 else
2941 gcov_write_unsigned (GCOV_NOTE_MAGIC);
2942 gcov_write_unsigned (GCOV_VERSION);
2943 gcov_write_unsigned (bbg_file_stamp);
2947 g->get_dumps ()->dump_finish (profile_pass_num);
2950 /* Return True if any type of profiling is enabled which requires linking
2951 in libgcov otherwise return False. */
2953 static bool
2954 profiling_enabled_p (void)
2956 return profile_arc_flag
2957 || flag_profile_generate_sampling;
2960 /* Performs file-level cleanup. Close graph file, generate coverage
2961 variables and constructor. */
2963 void
2964 coverage_finish (void)
2966 if (bbg_file_name && gcov_close ())
2967 unlink (bbg_file_name);
2969 if (!flag_branch_probabilities && flag_test_coverage
2970 && (!local_tick || local_tick == (unsigned)-1))
2971 /* Only remove the da file, if we're emitting coverage code and
2972 cannot uniquely stamp it. If we can stamp it, libgcov will DTRT. */
2973 unlink (da_file_name);
2975 if (coverage_obj_init ())
2977 vec<constructor_elt, va_gc> *fn_ctor = NULL;
2978 struct coverage_data *fn;
2980 for (fn = functions_head; fn; fn = fn->next)
2981 fn_ctor = coverage_obj_fn (fn_ctor, fn->fn_decl, fn);
2982 coverage_obj_finish (fn_ctor);
2985 XDELETEVEC (da_file_name);
2986 da_file_name = NULL;
2989 extern bool is_kernel_build;
2991 #define KERNEL_BUILD_PREDEF_STRING "__KERNEL__"
2993 /* Copies the macro def or undef CPP_DEF and saves the copy
2994 in a list. IS_DEF is a flag indicating if CPP_DEF represents
2995 a -D or -U. */
2997 void
2998 coverage_note_define (const char *cpp_def, bool is_def)
3000 char *s = XNEWVEC (char, strlen (cpp_def) + 2);
3001 s[0] = is_def ? 'D' : 'U';
3002 strcpy (s + 1, cpp_def);
3003 str_list_append (&cpp_defines_head, &cpp_defines_tail, s);
3004 num_cpp_defines++;
3006 /* When -D__KERNEL__ is in the option list, we assume this is
3007 compilation for Linux Kernel. */
3008 if (!strcmp(cpp_def, KERNEL_BUILD_PREDEF_STRING))
3009 is_kernel_build = is_def;
3012 /* Copies the -imacro/-include FILENAME and saves the copy in a list. */
3014 void
3015 coverage_note_include (const char *filename)
3017 str_list_append (&cpp_includes_head, &cpp_includes_tail, filename);
3018 num_cpp_includes++;
3021 /* Mark this module as containing asm statements. */
3023 void
3024 coverage_has_asm_stmt (void)
3026 has_asm_statement = flag_ripa_disallow_asm_modules;
3029 /* Write compilation info to the .note section. */
3031 void
3032 write_compilation_info_to_asm (void)
3034 unsigned lang;
3035 /* Write lang, ggc_memory to ASM section. */
3036 switch_to_section (get_section (".gnu.switches.text.lipo_info",
3037 SECTION_DEBUG, NULL));
3038 if (!strcmp (lang_hooks.name, "GNU C"))
3039 lang = GCOV_MODULE_C_LANG;
3040 else if (!strcmp (lang_hooks.name, "GNU C++"))
3041 lang = GCOV_MODULE_CPP_LANG;
3042 else
3043 lang = GCOV_MODULE_UNKNOWN_LANG;
3044 if (has_asm_statement)
3045 lang |= GCOV_MODULE_ASM_STMTS;
3046 dw2_asm_output_nstring (in_fnames[0], (size_t)-1, NULL);
3047 dw2_asm_output_data_uleb128 (lang, NULL);
3048 dw2_asm_output_data_uleb128 (ggc_total_memory, NULL);
3051 /* Write command line options to the .note section. */
3053 void
3054 write_compilation_flags_to_asm (void)
3056 size_t i;
3057 cpp_dir *quote_paths, *bracket_paths, *system_paths, *pdir;
3058 struct str_list *pdef, *pinc;
3059 int num_quote_paths = 0;
3060 int num_bracket_paths = 0;
3061 int num_system_paths = 0;
3063 get_include_chains (&quote_paths, &bracket_paths, &system_paths);
3065 /* Write quote_paths to ASM section. */
3066 switch_to_section (get_section (".gnu.switches.text.quote_paths",
3067 SECTION_DEBUG, NULL));
3068 for (pdir = quote_paths; pdir; pdir = pdir->next)
3070 if (pdir == bracket_paths)
3071 break;
3072 num_quote_paths++;
3074 dw2_asm_output_nstring (in_fnames[0], (size_t)-1, NULL);
3075 dw2_asm_output_data_uleb128 (num_quote_paths, NULL);
3076 for (pdir = quote_paths; pdir; pdir = pdir->next)
3078 if (pdir == bracket_paths)
3079 break;
3080 dw2_asm_output_nstring (pdir->name, (size_t)-1, NULL);
3083 /* Write bracket_paths to ASM section. */
3084 switch_to_section (get_section (".gnu.switches.text.bracket_paths",
3085 SECTION_DEBUG, NULL));
3086 for (pdir = bracket_paths; pdir; pdir = pdir->next)
3088 if (pdir == system_paths)
3089 break;
3090 num_bracket_paths++;
3092 dw2_asm_output_nstring (in_fnames[0], (size_t)-1, NULL);
3093 dw2_asm_output_data_uleb128 (num_bracket_paths, NULL);
3094 for (pdir = bracket_paths; pdir; pdir = pdir->next)
3096 if (pdir == system_paths)
3097 break;
3098 dw2_asm_output_nstring (pdir->name, (size_t)-1, NULL);
3101 /* Write system_paths to ASM section. */
3102 switch_to_section (get_section (".gnu.switches.text.system_paths",
3103 SECTION_DEBUG, NULL));
3104 for (pdir = system_paths; pdir; pdir = pdir->next)
3105 num_system_paths++;
3106 dw2_asm_output_nstring (in_fnames[0], (size_t)-1, NULL);
3107 dw2_asm_output_data_uleb128 (num_system_paths, NULL);
3108 for (pdir = system_paths; pdir; pdir = pdir->next)
3109 dw2_asm_output_nstring (pdir->name, (size_t)-1, NULL);
3111 /* Write cpp_defines to ASM section. */
3112 switch_to_section (get_section (".gnu.switches.text.cpp_defines",
3113 SECTION_DEBUG, NULL));
3114 dw2_asm_output_nstring (in_fnames[0], (size_t)-1, NULL);
3115 dw2_asm_output_data_uleb128 (num_cpp_defines, NULL);
3116 for (pdef = cpp_defines_head; pdef; pdef = pdef->next)
3117 dw2_asm_output_nstring (pdef->str, (size_t)-1, NULL);
3119 /* Write cpp_includes to ASM section. */
3120 switch_to_section (get_section (".gnu.switches.text.cpp_includes",
3121 SECTION_DEBUG, NULL));
3122 dw2_asm_output_nstring (in_fnames[0], (size_t)-1, NULL);
3123 dw2_asm_output_data_uleb128 (num_cpp_includes, NULL);
3124 for (pinc = cpp_includes_head; pinc; pinc = pinc->next)
3125 dw2_asm_output_nstring (pinc->str, (size_t)-1, NULL);
3127 /* Write cl_args to ASM section. */
3128 switch_to_section (get_section (".gnu.switches.text.cl_args",
3129 SECTION_DEBUG, NULL));
3130 dw2_asm_output_nstring (in_fnames[0], (size_t)-1, NULL);
3131 dw2_asm_output_data_uleb128 (num_lipo_cl_args, NULL);
3132 for (i = 0; i < num_lipo_cl_args; i++)
3133 dw2_asm_output_nstring (lipo_cl_args[i], (size_t)-1, NULL);
3135 #include "gt-coverage.h"