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
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
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/>. */
29 #include "coretypes.h"
33 #include "stringpool.h"
34 #include "stor-layout.h"
40 #include "basic-block.h"
45 #include "langhooks.h"
46 #include "hash-table.h"
47 #include "tree-iterator.h"
49 #include "pass_manager.h"
50 #include "tree-pass.h"
55 #include "tree-ssa-alias.h"
56 #include "internal-fn.h"
57 #include "gimple-expr.h"
60 #include "gimple-iterator.h"
61 #include "gimplify-me.h"
62 #include "gimple-ssa.h"
65 #include "diagnostic-core.h"
68 #include "filenames.h"
69 #include "dwarf2asm.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. */
96 struct str_list
*next
;
99 /* Counts information for a function. */
100 typedef struct counts_entry
103 unsigned HOST_WIDEST_INT ident
;
107 unsigned lineno_checksum
;
108 unsigned cfg_checksum
;
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
*);
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
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. */
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. */
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 *. */
222 get_const_string_type (void)
224 return build_pointer_type
225 (build_qualified_type (char_type_node
, TYPE_QUAL_CONST
));
229 counts_entry::hash (const value_type
*entry
)
231 return entry
->ident
* GCOV_COUNTERS
+ entry
->ctr
;
235 counts_entry::equal (const value_type
*entry1
,
236 const compare_type
*entry2
)
238 return entry1
->ident
== entry2
->ident
&& entry1
->ctr
== entry2
->ctr
;
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
);
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 */
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
*) {};
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. */
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
316 check_cg_opts (bool *cg_opt_val
, const char *option_str
)
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. */
332 has_incompatible_cg_opts (bool *cg_opts1
, bool *cg_opts2
, unsigned num_cg_opts
)
336 for (i
= 0; i
< num_cg_opts
; i
++)
338 if (cg_opts1
[i
] != cg_opts2
[i
])
345 /* Returns true if the command-line arguments stored in the given module-infos
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
++)
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
];
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
);
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
];
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
);
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;
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
453 if (num_non_warning_opts1
!= num_non_warning_opts2
)
454 non_warning_mismatch
= true;
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
;
508 /* Hash function for module name */
511 module_name_entry::hash (const value_type
*s
)
513 return htab_hash_string (s
->source_name
);
516 /* Delete function for module name */
519 module_name_entry::remove (value_type
*entry
)
521 /* XDELETE (entry->source_name); */
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. */
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
;
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
)
555 if (!slot2
|| !*slot2
)
558 gcc_assert (slot1
&& *slot1
&& slot2
&& *slot2
);
562 return m_e1
->order
- m_e2
->order
;
565 /* Comparison function for sorting fname array */
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
;
579 slot1
= module_name_tab
.find_slot (&e
, NO_INSERT
);
581 slot2
= module_name_tab
.find_slot (&e
, NO_INSERT
);
583 if (!slot1
|| !*slot1
)
586 if (!slot2
|| !*slot2
)
589 gcc_assert (slot1
&& *slot1
&& slot2
&& *slot2
);
593 return m_e1
->order
- m_e2
->order
;
596 /* Reorder module group according to file IMPORTS_FILE */
599 reorder_module_groups (const char *imports_file
, unsigned max_group
)
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");
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')
619 module_name_entry
**slot
;
620 module_name_entry
*m_e
= XCNEW (module_name_entry
);
622 m_e
->source_name
= xstrdup (line
);
625 slot
= module_name_tab
.find_slot (m_e
, INSERT
);
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 *),
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 ();
660 const char *mod_name
;
663 static vec
<mod_id_to_name_t
> *mod_names
;
666 record_module_name (unsigned int mod_id
, const char *name
)
671 t
.mod_name
= xstrdup (name
);
673 vec_alloc (mod_names
, 10);
674 mod_names
->safe_push (t
);
677 /* Return the module name for module with MOD_ID. */
680 get_module_name (unsigned int mod_id
)
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
;
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. */
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;
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
;
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))
728 inform (input_location
, "file %s not found, disabling profile use",
730 set_profile_use_options (&global_options
, &global_options_set
,
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
;
741 if (!gcov_magic (gcov_read_unsigned (), GCOV_DATA_MAGIC
))
743 warning (0, "%qs is not a gcov data file", da_file_name
);
747 else if ((tag
= gcov_read_unsigned ()) != GCOV_VERSION
)
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
);
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
)
778 fn_ident
= gcov_read_unsigned ();
779 lineno_checksum
= gcov_read_unsigned ();
780 cfg_checksum
= gcov_read_unsigned ();
783 fn_ident
= lineno_checksum
= cfg_checksum
= 0;
786 else if (tag
== GCOV_TAG_PROGRAM_SUMMARY
)
788 struct gcov_summary sum
;
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
;
804 memcpy (summary
.ctrs
[GCOV_COUNTER_ARCS
].histogram
,
805 sum
.ctrs
[GCOV_COUNTER_ARCS
].histogram
,
806 sizeof (gcov_bucket_type
) * GCOV_HISTOGRAM_SIZE
);
808 gcov_histogram_merge (summary
.ctrs
[GCOV_COUNTER_ARCS
].histogram
,
809 sum
.ctrs
[GCOV_COUNTER_ARCS
].histogram
);
812 else if (tag
== GCOV_TAG_BUILD_INFO
)
814 /* Build info strings are not used by the compiler, read and
816 gcov_unsigned_t num_strings
;
817 char **build_info_strings
= gcov_read_build_info (length
,
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
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
);
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
);
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 ();
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 ();
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
);
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 ();
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
;
895 /* each string has at least 8 bytes, so MOD_INFO's
896 persistent length >= in core size. */
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
);
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
);
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"
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"
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
,
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
);
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
,
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"
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",
1021 counts_hash
.dispose ();
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. */
1039 pointer_set_destroy (modset
);
1040 gcc_assert (module_infos_read
== 0
1041 || module_infos_read
== num_in_fnames
);
1044 gcc_assert (primary_module_id
&& num_in_fnames
>= 1);
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
);
1061 gcc_assert (coverage_node_map_initialized_p ());
1062 elt
.ident
= cgraph_get_node (func
->decl
)->profile_id
;
1066 entry
= counts_hash
.find (&elt
);
1071 /* Returns the counters for a particular tag. */
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 "
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",
1103 (DECL_ASSEMBLER_NAME (current_function_decl
)));
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
);
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");
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");
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
));
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. */
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
)
1168 if (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID
))
1169 elt
.ident
= FUNC_DECL_GLOBAL_ID (f
);
1172 gcc_assert (coverage_node_map_initialized_p ());
1173 elt
.ident
= cgraph_get_node (f
->decl
)->profile_id
;
1176 entry
= counts_hash
.find (&elt
);
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
)
1196 if (!fn_v_ctrs
[counter
])
1198 tree array_type
= build_array_type (get_gcov_type (), NULL_TREE
);
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
;
1211 /* Generate a tree to access COUNTER NO. */
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. */
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
,
1240 build_int_cst (integer_type_node
, no
),
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. */
1249 crc32_string_1 (unsigned chksum
, const char *string
, unsigned str_len
)
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
);
1259 chksum
= crc32_string (chksum
, dup
);
1265 /* Generate a checksum for a string. CHKSUM is the current
1269 coverage_checksum_string (unsigned chksum
, const char *string
)
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
)
1281 i
= len
= strlen (string
);
1283 if ((string
[i
] < '0' || string
[i
] > '9'))
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
++)
1300 if (!strncmp (string
+ i
, "_GLOBAL__N_", 11))
1302 if (!strncmp (string
+ i
, "_GLOBAL__", 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
1312 for (i
= i
+ offset
; string
[i
]; i
++)
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'))
1321 if (y
!= 9 || string
[i
+ 9] != '_')
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'))
1330 string
= dup
= xstrdup (string
);
1331 for (y
= 10; y
< 18; y
++)
1338 chksum
= crc32_string_1 (chksum
, string
, lipo_orig_str_len
);
1345 /* Compute checksum for the current function. We generate a CRC32. */
1348 coverage_compute_lineno_checksum (void)
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
;
1356 for (i
= strlen (xloc
.file
); i
>= 0; i
--)
1357 if (IS_DIR_SEPARATOR (pathless_filename
[i
]))
1359 pathless_filename
+= i
+ 1;
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. */
1377 name
= DECL_ASSEMBLER_NAME (current_function_decl
);
1379 name
= DECL_NAME (current_function_decl
);
1381 chksum
= coverage_checksum_string
1382 (chksum
, IDENTIFIER_POINTER (name
));
1387 /* Compute profile ID. This is better to be unique in whole program. */
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. */
1421 coverage_compute_cfg_checksum (void)
1424 unsigned chksum
= n_basic_blocks_for_fn (cfun
);
1426 FOR_EACH_BB_FN (bb
, cfun
)
1430 chksum
= crc32_byte (chksum
, bb
->index
);
1431 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1433 chksum
= crc32_byte (chksum
, e
->dest
->index
);
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
)
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
));
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. */
1482 coverage_end_function (unsigned lineno_checksum
, unsigned cfg_checksum
)
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
;
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
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
);
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
;
1520 *functions_tail
= item
;
1521 functions_tail
= &item
->next
;
1524 for (i
= 0; i
!= GCOV_COUNTERS
; i
++)
1526 tree var
= fn_v_ctrs
[i
];
1529 item
->ctr_vars
[i
] = 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
;
1548 /* True if a function entry corresponding to the given FN_IDENT
1549 is present in the coverage internal data structures. */
1552 coverage_function_present (unsigned fn_ident
)
1554 struct coverage_data
*item
= functions_head
;
1555 while (item
&& item
->ident
!= fn_ident
)
1557 return item
!= NULL
;
1560 /* Update function and program direct-call coverage counts. */
1563 coverage_dc_end_function (void)
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
))
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. */
1581 item
= ggc_alloc_coverage_data ();
1582 *functions_tail
= item
;
1583 functions_tail
= &item
->next
;
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
;
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
;
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. */
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
));
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);
1628 strcpy (buf
, "__gcov__");
1630 sprintf (buf
, "__gcov%u_", counter
);
1632 #ifndef NO_DOT_IN_LABEL
1634 #elif !defined NO_DOLLAR_IN_LABEL
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
);
1647 /* Creates the gcov_fn_info RECORD_TYPE. */
1650 build_fn_info_type (tree type
, unsigned counters
, tree gcov_info_type
)
1652 tree ctr_info
= lang_hooks
.types
.make_type (RECORD_TYPE
);
1656 gcc_assert (counters
);
1659 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
1660 get_gcov_unsigned_t ());
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
;
1669 finish_builtin_struct (ctr_info
, "__gcov_ctr_info", fields
, NULL_TREE
);
1672 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
1673 build_pointer_type (build_qualified_type
1674 (gcov_info_type
, TYPE_QUAL_CONST
)));
1678 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
1679 get_gcov_unsigned_t ());
1680 DECL_CHAIN (field
) = fields
;
1683 /* lineno_checksum */
1684 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
1685 get_gcov_unsigned_t ());
1686 DECL_CHAIN (field
) = fields
;
1690 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
1691 get_gcov_unsigned_t ());
1692 DECL_CHAIN (field
) = fields
;
1695 array_type
= build_index_type (size_int (counters
- 1));
1696 array_type
= build_array_type (ctr_info
, array_type
);
1699 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
, array_type
);
1700 DECL_CHAIN (field
) = fields
;
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. */
1711 build_fn_info (const struct coverage_data
*data
, tree type
, tree key
)
1713 tree fields
= TYPE_FIELDS (type
);
1716 vec
<constructor_elt
, va_gc
> *v1
= NULL
;
1717 vec
<constructor_elt
, va_gc
> *v2
= NULL
;
1720 CONSTRUCTOR_APPEND_ELT (v1
, fields
,
1721 build1 (ADDR_EXPR
, TREE_TYPE (fields
), key
));
1722 fields
= DECL_CHAIN (fields
);
1725 CONSTRUCTOR_APPEND_ELT (v1
, fields
,
1726 build_int_cstu (get_gcov_unsigned_t (),
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
);
1737 CONSTRUCTOR_APPEND_ELT (v1
, fields
,
1738 build_int_cstu (get_gcov_unsigned_t (),
1739 data
->cfg_checksum
));
1740 fields
= DECL_CHAIN (fields
);
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
];
1753 = tree_to_shwi (TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (var
))))
1756 CONSTRUCTOR_APPEND_ELT (ctr
, TYPE_FIELDS (ctr_type
),
1757 build_int_cstu (get_gcov_unsigned_t (),
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. */
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
;
1790 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
1791 get_gcov_unsigned_t ());
1792 DECL_CHAIN (field
) = fields
;
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
;
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
;
1810 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
1811 get_gcov_unsigned_t ());
1812 DECL_CHAIN (field
) = fields
;
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
;
1823 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
,
1824 NULL_TREE
, get_gcov_unsigned_t ());
1825 DECL_CHAIN (field
) = fields
;
1828 /* merge fn array */
1830 = build_function_type_list (void_type_node
,
1831 build_pointer_type (get_gcov_type ()),
1832 get_gcov_unsigned_t (), NULL_TREE
);
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
,
1838 DECL_CHAIN (field
) = fields
;
1842 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
1843 get_gcov_unsigned_t ());
1844 DECL_CHAIN (field
) = fields
;
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
,
1852 DECL_CHAIN (field
) = fields
;
1855 /* build_info string array */
1856 string_type
= build_pointer_type (
1857 build_qualified_type (char_type_node
,
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
;
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
1875 build_inc_path_array_value (tree string_type
, vec
<constructor_elt
, va_gc
> **v
,
1876 cpp_dir
*paths
, int num
)
1880 for (i
= 0, pdir
= paths
; i
< num
; pdir
= pdir
->next
)
1882 const char *path_raw_string
;
1883 int path_string_length
;
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
));
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. */
1902 build_str_array_value (tree str_type
, vec
<constructor_elt
, va_gc
> **v
,
1903 struct str_list
*head
)
1905 const char *raw_str
;
1910 raw_str
= head
->str
;
1911 str_length
= strlen (raw_str
);
1912 str
= build_string (str_length
+ 1, raw_str
);
1914 build_array_type (char_type_node
,
1915 build_index_type (build_int_cst (NULL_TREE
,
1917 CONSTRUCTOR_APPEND_ELT (*v
, NULL
,
1918 build1 (ADDR_EXPR
, str_type
, str
));
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
1929 build_cl_args_array_value (tree string_type
, vec
<constructor_elt
, va_gc
> **v
)
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
,
1941 CONSTRUCTOR_APPEND_ELT (*v
, NULL
,
1942 build1 (ADDR_EXPR
, string_type
, arg_string
));
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. */
1952 emit_function_name (void)
1954 fprintf (stderr
, "Module %s FuncId %u Name %s\n",
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. */
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
,
1980 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
,
1981 NULL_TREE
, get_gcov_unsigned_t ());
1982 DECL_CHAIN (field
) = fields
;
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
;
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
;
2003 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
,
2004 NULL_TREE
, get_gcov_unsigned_t ());
2005 DECL_CHAIN (field
) = fields
;
2008 /* ggc_memory field */
2009 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
,
2010 NULL_TREE
, get_gcov_unsigned_t ());
2011 DECL_CHAIN (field
) = fields
;
2015 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
,
2016 NULL_TREE
, string_type
);
2017 DECL_CHAIN (field
) = fields
;
2021 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
,
2022 NULL_TREE
, string_type
);
2023 DECL_CHAIN (field
) = fields
;
2026 /* Num quote paths */
2027 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
,
2028 NULL_TREE
, get_gcov_unsigned_t ());
2029 DECL_CHAIN (field
) = fields
;
2032 /* Num bracket paths */
2033 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
,
2034 NULL_TREE
, get_gcov_unsigned_t ());
2035 DECL_CHAIN (field
) = fields
;
2038 /* Num system paths */
2039 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
,
2040 NULL_TREE
, get_gcov_unsigned_t ());
2041 DECL_CHAIN (field
) = fields
;
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
;
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
;
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
;
2062 get_include_chains ("e_paths
, &bracket_paths
, &system_paths
);
2063 for (pdir
= quote_paths
; pdir
; pdir
= pdir
->next
)
2065 if (pdir
== bracket_paths
)
2069 for (pdir
= bracket_paths
; pdir
; pdir
= pdir
->next
)
2071 if (pdir
== system_paths
)
2073 num_bracket_paths
++;
2075 for (pdir
= system_paths
; pdir
; pdir
= pdir
->next
)
2079 index_type
= build_index_type (build_int_cst (NULL_TREE
,
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
;
2093 finish_builtin_struct (type
, "__gcov_module_info", fields
, NULL_TREE
);
2098 /* Returns the value of the module info associated with the
2099 current source module being compiled. */
2102 build_gcov_module_info_value (tree mod_type
)
2104 tree info_fields
, mod_info
;
2105 tree value
= NULL_TREE
;
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;
2112 vec
<constructor_elt
,va_gc
> *v
= NULL
, *path_v
= NULL
;
2114 info_fields
= TYPE_FIELDS (mod_type
);
2117 CONSTRUCTOR_APPEND_ELT (v
, info_fields
,
2118 build_int_cstu (get_gcov_unsigned_t (), 0));
2120 info_fields
= DECL_CHAIN (info_fields
);
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
);
2134 CONSTRUCTOR_APPEND_ELT (v
, info_fields
,
2135 build_int_cstu (get_gcov_unsigned_t (), 0));
2136 info_fields
= DECL_CHAIN (info_fields
);
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
;
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
);
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
);
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 ("e_paths
, &bracket_paths
, &system_paths
);
2181 for (pdir
= quote_paths
; pdir
; pdir
= pdir
->next
)
2183 if (pdir
== bracket_paths
)
2187 for (pdir
= bracket_paths
; pdir
; pdir
= pdir
->next
)
2189 if (pdir
== system_paths
)
2191 num_bracket_paths
++;
2193 for (pdir
= system_paths
; pdir
; pdir
= pdir
->next
)
2196 /* Num quote paths */
2197 CONSTRUCTOR_APPEND_ELT (v
, info_fields
,
2198 build_int_cstu (get_gcov_unsigned_t (),
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 (),
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 (),
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 (),
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 (),
2230 info_fields
= DECL_CHAIN (info_fields
);
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
,
2242 build_str_array_value (string_type
, &path_v
,
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
);
2265 /* Returns the value of the build info string read earlier. */
2268 build_gcov_build_info_value (void)
2271 tree value
= NULL_TREE
;
2272 tree string_type
, index_type
, string_array_type
;
2273 vec
<constructor_elt
,va_gc
> *v
= NULL
;
2276 string_type
= build_pointer_type (
2277 build_qualified_type (char_type_node
,
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
);
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. */
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);
2316 /* Read file specified to -fprofile-generate-buildinfo=filename option and
2317 create a list of strings to include in build_info array. */
2320 read_buildinfo (void)
2323 FILE *buildinfo
= fopen (flag_profile_generate_buildinfo
, "r");
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
);
2339 /* Terminate with an empty string. */
2340 str_list_append (&build_info_array_head
, &build_info_array_tail
, "");
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. */
2360 build_info (tree info_type
, tree fn_ary
)
2362 tree info_fields
= TYPE_FIELDS (info_type
);
2363 tree merge_fn_type
, n_funcs
;
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
;
2373 CONSTRUCTOR_APPEND_ELT (v1
, info_fields
,
2374 build_int_cstu (TREE_TYPE (info_fields
),
2376 info_fields
= DECL_CHAIN (info_fields
);
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
)),
2383 CONSTRUCTOR_APPEND_ELT (v1
, info_fields
, mod_value
);
2384 info_fields
= DECL_CHAIN (info_fields
);
2387 CONSTRUCTOR_APPEND_ELT (v1
, info_fields
, null_pointer_node
);
2388 info_fields
= DECL_CHAIN (info_fields
);
2391 CONSTRUCTOR_APPEND_ELT (v1
, info_fields
,
2392 build_int_cstu (TREE_TYPE (info_fields
),
2394 info_fields
= DECL_CHAIN (info_fields
);
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
),
2404 info_fields
= DECL_CHAIN (info_fields
);
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
,
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
);
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
);
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
)
2454 buildinfo_value
= build_gcov_build_info_value ();
2455 CONSTRUCTOR_APPEND_ELT (v1
, info_fields
,
2456 build1 (ADDR_EXPR
, TREE_TYPE (info_fields
),
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. */
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). */
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. */
2498 coverage_obj_init (void)
2500 tree gcov_info_type
;
2501 unsigned n_counters
= 0;
2503 struct coverage_data
*fn
;
2504 struct coverage_data
**fn_prev
;
2507 no_coverage
= 1; /* Disable any further coverage. */
2512 if (cgraph_dump_file
)
2513 fprintf (cgraph_dump_file
, "Using data file %s\n", da_file_name
);
2515 /* Prune functions. */
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
;
2523 /* The function is not being emitted, remove from list. */
2524 *fn_prev
= fn
->next
;
2526 if (functions_head
== NULL
)
2529 for (ix
= 0; ix
!= GCOV_COUNTERS
; ix
++)
2530 if ((1u << ix
) & prg_ctr_mask
)
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
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
);
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
));
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. */
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
,
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. */
2602 get_da_file_name (const char *base_file_name
)
2605 int len
= strlen (base_file_name
);
2606 const char *prefix
= profile_data_prefix
;
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
)
2623 strcpy (da_file_name
, prefix
);
2624 da_file_name
[prefix_len
- 1] = '/';
2625 da_file_name
[prefix_len
] = 0;
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
);
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
);
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. */
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. */
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
;
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
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
))
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
);
2744 inform (UNKNOWN_LOCATION
, "subpath %s not found in path %s",
2745 old_sub
, *orig_inc_path
);
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 */
2763 process_include_paths_1 (struct gcov_module_info
*mod_info
,
2764 char* old_sub
, char *new_sub
)
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] */
2785 process_include_paths (struct gcov_module_info
*mod_info
)
2787 char *sub_pattern
, *cur
, *next
, *new_sub
;
2789 if (!lipo_inc_path_pattern
)
2792 sub_pattern
= xstrdup (lipo_inc_path_pattern
);
2797 next
= strchr (cur
, ',');
2800 new_sub
= strchr (cur
, ':');
2803 error ("Invalid path substibution pattern %s", sub_pattern
);
2808 process_include_paths_1 (mod_info
, cur
, new_sub
);
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. */
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
)
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
)
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
]),
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
]),
2845 for (i
= 0; i
< mod_info
->num_system_paths
; i
++, j
++)
2846 add_path (xstrdup (mod_info
->string_array
[j
]),
2848 register_include_chains (parse_in
, NULL
, NULL
, NULL
,
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);
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
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
);
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
)
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
;
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. */
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. */
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
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
);
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. */
3015 coverage_note_include (const char *filename
)
3017 str_list_append (&cpp_includes_head
, &cpp_includes_tail
, filename
);
3021 /* Mark this module as containing asm statements. */
3024 coverage_has_asm_stmt (void)
3026 has_asm_statement
= flag_ripa_disallow_asm_modules
;
3029 /* Write compilation info to the .note section. */
3032 write_compilation_info_to_asm (void)
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
;
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. */
3054 write_compilation_flags_to_asm (void)
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 ("e_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
)
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
)
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
)
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
)
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
)
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"