Merge from trunk: 215733-215743
[official-gcc.git] / gcc-4_6_3-mobile / gcc / coverage.c
blobc28eb7269ddc135376e4664dfa5a1d02eb4f4b94
1 /* Read and write coverage files, and associated functionality.
2 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1996, 1997, 1998, 1999,
3 2000, 2001, 2003, 2004, 2005, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
5 Contributed by James E. Wilson, UC Berkeley/Cygnus Support;
6 based on some ideas from Dain Samples of UC Berkeley.
7 Further mangling by Bob Manson, Cygnus Support.
8 Further mangled by Nathan Sidwell, CodeSourcery
10 This file is part of GCC.
12 GCC is free software; you can redistribute it and/or modify it under
13 the terms of the GNU General Public License as published by the Free
14 Software Foundation; either version 3, or (at your option) any later
15 version.
17 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
18 WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 for more details.
22 You should have received a copy of the GNU General Public License
23 along with GCC; see the file COPYING3. If not see
24 <http://www.gnu.org/licenses/>. */
27 #define GCOV_LINKAGE
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "rtl.h"
34 #include "tree.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 "hashtab.h"
47 #include "tree-iterator.h"
48 #include "cgraph.h"
49 #include "tree-pass.h"
50 #include "opts.h"
51 #include "gcov-io.h"
52 #include "tree-flow.h"
53 #include "cpplib.h"
54 #include "incpath.h"
55 #include "diagnostic-core.h"
56 #include "intl.h"
57 #include "l-ipo.h"
58 #include "dwarf2asm.h"
60 #include "gcov-io.h"
61 #include "gcov-io.c"
62 #include "params.h"
63 #include "dbgcnt.h"
64 #include "input.h"
66 /* Defined in tree-profile.c. */
67 void gimple_init_instrumentation_sampling (void);
69 struct function_list
71 struct function_list *next; /* next function */
72 unsigned ident; /* function ident */
73 unsigned lineno_checksum; /* function lineno checksum */
74 unsigned cfg_checksum; /* function cfg checksum */
75 unsigned n_ctrs[GCOV_COUNTERS];/* number of counters. */
76 unsigned dc_offset; /* offset of counters to direct calls. */
79 /* Linked list of -D/-U/-imacro/-include strings for a source module. */
80 struct str_list
82 char *str;
83 struct str_list *next;
86 /* Counts information for a function. */
87 typedef struct counts_entry
89 /* We hash by */
90 unsigned HOST_WIDEST_INT ident;
91 unsigned ctr;
93 /* Store */
94 unsigned lineno_checksum;
95 unsigned cfg_checksum;
96 gcov_type *counts;
97 struct gcov_ctr_summary summary;
99 /* Workspace */
100 struct counts_entry *chain;
102 } counts_entry_t;
104 static struct function_list *functions_head = 0;
105 static struct function_list **functions_tail = &functions_head;
106 static unsigned no_coverage = 0;
108 /* Cumulative counter information for whole program. */
109 static unsigned prg_ctr_mask; /* Mask of counter types generated. */
110 static unsigned prg_n_ctrs[GCOV_COUNTERS]; /* Total counters allocated. */
112 /* Counter information for current function. */
113 static unsigned fn_ctr_mask; /* Mask of counters used. */
114 static unsigned fn_n_ctrs[GCOV_COUNTERS]; /* Counters allocated. */
115 static unsigned fn_b_ctrs[GCOV_COUNTERS]; /* Allocation base. */
117 /* Name of the output file for coverage output file. */
118 static char *bbg_file_name;
119 static unsigned bbg_file_opened;
120 static int bbg_function_announced;
122 /* Name of the count data file. */
123 static char *da_file_name;
124 static char *da_base_file_name;
125 static char *main_input_file_name;
127 /* Filename for the global pmu profile */
128 static char pmu_profile_filename[] = "pmuprofile";
130 /* Hash table of count data. */
131 static htab_t counts_hash = NULL;
133 /* Trees representing the counter table arrays. */
134 static GTY(()) tree tree_ctr_tables[GCOV_COUNTERS];
136 /* The names of merge functions for counters. */
137 static const char *const ctr_merge_functions[GCOV_COUNTERS] = GCOV_MERGE_FUNCTIONS;
138 static const char *const ctr_names[GCOV_COUNTERS] = GCOV_COUNTER_NAMES;
140 /* True during the period that counts_hash is being rebuilt. */
141 static bool rebuilding_counts_hash = false;
143 struct gcov_module_info **module_infos = NULL;
145 /* List of -D/-U options. */
146 static struct str_list *cpp_defines_head = NULL, *cpp_defines_tail = NULL;
147 static unsigned num_cpp_defines = 0;
149 /* List of -imcaro/-include options. */
150 static struct str_list *cpp_includes_head = NULL, *cpp_includes_tail = NULL;
151 static unsigned num_cpp_includes = 0;
153 /* True if the current module has any asm statements. */
154 static bool has_asm_statement;
156 /* extern const char * __gcov_pmu_profile_filename */
157 static tree gcov_pmu_filename_decl = NULL_TREE;
158 /* extern const char * __gcov_pmu_profile_options */
159 static tree gcov_pmu_options_decl = NULL_TREE;
160 /* extern gcov_unsigned_t __gcov_pmu_top_n_address */
161 static tree gcov_pmu_top_n_address_decl = NULL_TREE;
163 /* To ensure that the above variables are initialized only once. */
164 static int pmu_profiling_initialized = 0;
166 /* Forward declarations. */
167 static hashval_t htab_counts_entry_hash (const void *);
168 static int htab_counts_entry_eq (const void *, const void *);
169 static void htab_counts_entry_del (void *);
170 static void read_counts_file (const char *, unsigned);
171 static tree build_fn_info_type (unsigned);
172 static tree build_fn_info_value (const struct function_list *, tree);
173 static tree build_ctr_info_type (void);
174 static tree build_ctr_info_value (unsigned, tree);
175 static tree build_gcov_info (void);
176 static void create_coverage (void);
177 static char * get_da_file_name (const char *);
178 static void init_pmu_profiling (void);
179 static bool profiling_enabled_p (void);
181 /* Return the type node for gcov_type. */
183 tree
184 get_gcov_type (void)
186 return lang_hooks.types.type_for_size (GCOV_TYPE_SIZE, false);
189 /* Return the type node for gcov_unsigned_t. */
191 tree
192 get_gcov_unsigned_t (void)
194 return lang_hooks.types.type_for_size (32, true);
197 /* Return the type node for const char *. */
199 static tree
200 get_const_string_type (void)
202 return build_pointer_type
203 (build_qualified_type (char_type_node, TYPE_QUAL_CONST));
206 static hashval_t
207 htab_counts_entry_hash (const void *of)
209 const counts_entry_t *const entry = (const counts_entry_t *) of;
211 return entry->ident * GCOV_COUNTERS + entry->ctr;
214 static int
215 htab_counts_entry_eq (const void *of1, const void *of2)
217 const counts_entry_t *const entry1 = (const counts_entry_t *) of1;
218 const counts_entry_t *const entry2 = (const counts_entry_t *) of2;
220 return entry1->ident == entry2->ident && entry1->ctr == entry2->ctr;
223 static void
224 htab_counts_entry_del (void *of)
226 counts_entry_t *const entry = (counts_entry_t *) of;
228 /* When rebuilding counts_hash, we will reuse the entry. */
229 if (!rebuilding_counts_hash)
231 free (entry->counts);
232 free (entry);
236 /* Returns true if MOD_ID is the id of the last source module. */
239 is_last_module (unsigned mod_id)
241 return (mod_id == module_infos[num_in_fnames - 1]->ident);
244 /* String hash function */
246 static hashval_t
247 str_hash (const void *p)
249 const char *s = (const char *)p;
250 return htab_hash_string (s);
253 /* String equal function */
255 static int
256 str_eq (const void *p1, const void *p2)
258 const char *s1 = (const char *)p1;
259 const char *s2 = (const char *)p2;
261 return !strcmp (s1, s2);
265 /* Returns true if the command-line arguments stored in the given module-infos
266 are incompatible. */
267 static bool
268 incompatible_cl_args (struct gcov_module_info* mod_info1,
269 struct gcov_module_info* mod_info2)
271 char **warning_opts1 = XNEWVEC (char *, mod_info1->num_cl_args);
272 char **warning_opts2 = XNEWVEC (char *, mod_info2->num_cl_args);
273 char **non_warning_opts1 = XNEWVEC (char *, mod_info1->num_cl_args);
274 char **non_warning_opts2 = XNEWVEC (char *, mod_info2->num_cl_args);
275 unsigned int i, num_warning_opts1 = 0, num_warning_opts2 = 0;
276 unsigned int num_non_warning_opts1 = 0, num_non_warning_opts2 = 0;
277 bool warning_mismatch = false;
278 bool non_warning_mismatch = false;
279 bool with_fexceptions1 = true;
280 bool with_fexceptions2 = true;
281 htab_t option_tab1, option_tab2;
282 unsigned int start_index1 = mod_info1->num_quote_paths +
283 mod_info1->num_bracket_paths + mod_info1->num_cpp_defines +
284 mod_info1->num_cpp_includes;
285 unsigned int start_index2 = mod_info2->num_quote_paths +
286 mod_info2->num_bracket_paths + mod_info2->num_cpp_defines +
287 mod_info2->num_cpp_includes;
289 option_tab1 = htab_create (10, str_hash, str_eq, NULL);
290 option_tab2 = htab_create (10, str_hash, str_eq, NULL);
292 /* First, separate the warning and non-warning options. */
293 for (i = 0; i < mod_info1->num_cl_args; i++)
294 if (mod_info1->string_array[start_index1 + i][1] == 'W')
295 warning_opts1[num_warning_opts1++] =
296 mod_info1->string_array[start_index1 + i];
297 else
299 void **slot;
300 char* option_string = mod_info1->string_array[start_index1 + i];
302 if (!strcmp ("-fexceptions", option_string))
303 with_fexceptions1 = true;
304 else if (!strcmp ("-fno-exceptions", option_string))
305 with_fexceptions1 = false;
307 slot = htab_find_slot (option_tab1, option_string, INSERT);
308 if (!*slot)
310 *slot = option_string;
311 non_warning_opts1[num_non_warning_opts1++] = option_string;
315 for (i = 0; i < mod_info2->num_cl_args; i++)
316 if (mod_info2->string_array[start_index2 + i][1] == 'W')
317 warning_opts2[num_warning_opts2++] =
318 mod_info2->string_array[start_index2 + i];
319 else
321 void **slot;
322 char* option_string = mod_info2->string_array[start_index2 + i];
324 if (!strcmp ("-fexceptions", option_string))
325 with_fexceptions2 = true;
326 else if (!strcmp ("-fno-exceptions", option_string))
327 with_fexceptions2 = false;
328 slot = htab_find_slot (option_tab2, option_string, INSERT);
329 if (!*slot)
331 *slot = option_string;
332 non_warning_opts2[num_non_warning_opts2++] = option_string;
336 /* Compare warning options. If these mismatch, we emit a warning. */
337 if (num_warning_opts1 != num_warning_opts2)
338 warning_mismatch = true;
339 else
340 for (i = 0; i < num_warning_opts1 && !warning_mismatch; i++)
341 warning_mismatch = strcmp (warning_opts1[i], warning_opts2[i]) != 0;
343 /* Compare non-warning options. If these mismatch, we emit a warning, and if
344 -fripa-disallow-opt-mismatch is supplied, the two modules are also
345 incompatible. */
346 if (num_non_warning_opts1 != num_non_warning_opts2)
347 non_warning_mismatch = true;
348 else
349 for (i = 0; i < num_non_warning_opts1 && !non_warning_mismatch; i++)
350 non_warning_mismatch =
351 strcmp (non_warning_opts1[i], non_warning_opts2[i]) != 0;
353 if (warn_ripa_opt_mismatch && (warning_mismatch || non_warning_mismatch))
354 warning (OPT_Wripa_opt_mismatch, "command line arguments mismatch for %s "
355 "and %s", mod_info1->source_filename, mod_info2->source_filename);
357 if (warn_ripa_opt_mismatch && non_warning_mismatch
358 && (flag_opt_info >= OPT_INFO_MED))
360 inform (UNKNOWN_LOCATION, "Options for %s", mod_info1->source_filename);
361 for (i = 0; i < num_non_warning_opts1; i++)
362 inform (UNKNOWN_LOCATION, non_warning_opts1[i]);
363 inform (UNKNOWN_LOCATION, "Options for %s", mod_info2->source_filename);
364 for (i = 0; i < num_non_warning_opts2; i++)
365 inform (UNKNOWN_LOCATION, non_warning_opts2[i]);
368 XDELETEVEC (warning_opts1);
369 XDELETEVEC (warning_opts2);
370 XDELETEVEC (non_warning_opts1);
371 XDELETEVEC (non_warning_opts2);
372 htab_delete (option_tab1);
373 htab_delete (option_tab2);
374 return ((flag_ripa_disallow_opt_mismatch && non_warning_mismatch)
375 || (with_fexceptions1 != with_fexceptions2));
378 /* Create a mgf file and write primary module info to it. */
380 static FILE *
381 create_mgf_file (struct gcov_module_info* mod_info)
383 #define MODULE_GROUPING_FILE_SUFFIX ".mgf"
384 char mgf_name_buf[1024];
385 const char *mgf_name = mgf_name_buf;
386 FILE *mgf_fd;
388 if (ripa_mgf_gen_fname == NULL)
389 sprintf(mgf_name_buf, "%s%s", main_input_basename,
390 MODULE_GROUPING_FILE_SUFFIX);
391 else
392 mgf_name = (const char *) ripa_mgf_gen_fname;
394 if ((mgf_fd = fopen (mgf_name, "w")) == NULL)
396 warning (UNKNOWN_LOCATION, "error in opening %s, exiting",
397 mgf_name);
398 return NULL;
400 fprintf(mgf_fd, "P %d %s \n", mod_info->ident,
401 mod_info->source_filename);
402 return mgf_fd;
405 /* Write module grouping info to mgf file. */
407 static void
408 write_mgf_file (FILE *mgf_fd, struct gcov_module_info* mod_info)
410 int i;
411 int i_pos;
412 int ignored_cl_args = 0;
414 fprintf (mgf_fd, "#\nA %d %s\n",
415 mod_info->ident,
416 mod_info->source_filename);
418 fprintf (mgf_fd, "G %s\n", mod_info->da_filename);
420 i_pos = mod_info->num_quote_paths;
421 for(i=0;i < i_pos; i++)
422 fprintf (mgf_fd, "Q %s\n", mod_info->string_array[i]);
424 i_pos += mod_info->num_bracket_paths;
425 for(;i < i_pos; i++)
426 fprintf (mgf_fd, "B %s\n", mod_info->string_array[i]);
428 i_pos += mod_info->num_cpp_includes;
429 for(;i < i_pos; i++)
430 fprintf (mgf_fd, "I %s\n", mod_info->string_array[i]);
432 i_pos += mod_info->num_cpp_defines;
433 for(;i < i_pos; i++)
434 fprintf (mgf_fd, "D %s\n", mod_info->string_array[i]);
436 i_pos += mod_info->num_cl_args;
437 for(;i < i_pos; i++)
439 char *str = mod_info->string_array[i];
440 if (!strcmp (str, "-fripa") ||
441 !strncmp (str, "-fprofile-generate", 18) ||
442 !strncmp (str, "-fprofile-arcs", 14))
444 ignored_cl_args++;
445 continue;
447 fprintf (mgf_fd, "C %s\n", str);
449 fprintf (mgf_fd, "Z %d %s, %d %d %d %d %d\n", mod_info->ident,
450 mod_info->source_filename,
451 mod_info->num_quote_paths,
452 mod_info->num_bracket_paths,
453 mod_info->num_cpp_defines,
454 mod_info->num_cpp_includes,
455 mod_info->num_cl_args - ignored_cl_args);
459 /* Read in the counts file, if available. DA_FILE_NAME is the
460 name of the gcda file, and MODULE_ID is the module id of the
461 associated source module.
462 For FE based LIPO, MODULE_ID will always be 0 for primary
463 modules. */
465 static void
466 read_counts_file (const char *da_file_name, unsigned module_id)
468 gcov_unsigned_t fn_ident = 0;
469 counts_entry_t *summaried = NULL;
470 unsigned seen_summary = 0;
471 gcov_unsigned_t tag;
472 int is_error = 0;
473 unsigned module_infos_read = 0;
474 struct pointer_set_t *modset = 0;
475 unsigned max_group = PARAM_VALUE (PARAM_MAX_LIPO_GROUP);
476 unsigned lineno_checksum = 0;
477 unsigned cfg_checksum = 0;
478 FILE *mgf_fd = NULL;
480 if (max_group == 0)
481 max_group = (unsigned) -1;
483 if (!gcov_open (da_file_name, 1))
485 if (PARAM_VALUE (PARAM_GCOV_DEBUG))
487 /* Try to find .gcda file in the current working dir. */
488 da_file_name = lbasename (da_file_name);
489 if (!gcov_open (da_file_name, 1))
490 return;
492 else
493 return;
496 if (!gcov_magic (gcov_read_unsigned (), GCOV_DATA_MAGIC))
498 warning (0, "%qs is not a gcov data file", da_file_name);
499 gcov_close ();
500 return;
502 else if ((tag = gcov_read_unsigned ()) != GCOV_VERSION)
504 char v[4], e[4];
506 GCOV_UNSIGNED2STRING (v, tag);
507 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
509 warning (0, "%qs is version %q.*s, expected version %q.*s",
510 da_file_name, 4, v, 4, e);
511 gcov_close ();
512 return;
515 /* Read and discard the stamp. */
516 gcov_read_unsigned ();
518 if (!counts_hash)
519 counts_hash = htab_create (10,
520 htab_counts_entry_hash, htab_counts_entry_eq,
521 htab_counts_entry_del);
522 while ((tag = gcov_read_unsigned ()))
524 gcov_unsigned_t length;
525 gcov_position_t offset;
527 length = gcov_read_unsigned ();
528 offset = gcov_position ();
529 if (tag == GCOV_TAG_FUNCTION)
531 fn_ident = gcov_read_unsigned ();
532 lineno_checksum = gcov_read_unsigned ();
533 cfg_checksum = gcov_read_unsigned ();
534 if (seen_summary)
536 /* We have already seen a summary, this means that this
537 new function begins a new set of program runs. We
538 must unlink the summaried chain. */
539 counts_entry_t *entry, *chain;
541 for (entry = summaried; entry; entry = chain)
543 chain = entry->chain;
544 entry->chain = NULL;
546 summaried = NULL;
547 seen_summary = 0;
550 else if (tag == GCOV_TAG_PROGRAM_SUMMARY)
552 counts_entry_t *entry;
553 struct gcov_summary summary;
555 gcov_read_summary (&summary);
556 seen_summary = 1;
557 for (entry = summaried; entry; entry = entry->chain)
559 struct gcov_ctr_summary *csum = &summary.ctrs[entry->ctr];
561 entry->summary.runs += csum->runs;
562 entry->summary.sum_all += csum->sum_all;
563 if (entry->summary.run_max < csum->run_max)
564 entry->summary.run_max = csum->run_max;
565 entry->summary.sum_max += csum->sum_max;
568 else if (GCOV_TAG_IS_COUNTER (tag) && fn_ident)
570 counts_entry_t **slot, *entry, elt;
571 unsigned n_counts = GCOV_TAG_COUNTER_NUM (length);
572 unsigned ix;
574 elt.ident = GEN_FUNC_GLOBAL_ID (module_id, fn_ident);
575 elt.ctr = GCOV_COUNTER_FOR_TAG (tag);
577 slot = (counts_entry_t **) htab_find_slot
578 (counts_hash, &elt, INSERT);
579 entry = *slot;
580 if (!entry)
582 *slot = entry = XCNEW (counts_entry_t);
583 entry->ident = elt.ident;
584 entry->ctr = elt.ctr;
585 entry->lineno_checksum = lineno_checksum;
586 entry->cfg_checksum = cfg_checksum;
587 entry->summary.num = n_counts;
588 entry->counts = XCNEWVEC (gcov_type, n_counts);
590 else if (entry->lineno_checksum != lineno_checksum
591 || entry->cfg_checksum != cfg_checksum)
593 error ("Profile data for function %u is corrupted", fn_ident);
594 error ("checksum is (%x,%x) instead of (%x,%x)",
595 entry->lineno_checksum, entry->cfg_checksum,
596 lineno_checksum, cfg_checksum);
597 htab_delete (counts_hash);
598 break;
600 else if (entry->summary.num != n_counts)
602 error ("Profile data for function %u is corrupted", fn_ident);
603 error ("number of counters is %d instead of %d", entry->summary.num, n_counts);
604 htab_delete (counts_hash);
605 break;
607 else if (elt.ctr >= GCOV_COUNTERS_SUMMABLE)
609 error ("cannot merge separate %s counters for function %u",
610 ctr_names[elt.ctr], fn_ident);
611 goto skip_merge;
614 if (elt.ctr < GCOV_COUNTERS_SUMMABLE
615 /* This should always be true for a just allocated entry,
616 and always false for an existing one. Check this way, in
617 case the gcov file is corrupt. */
618 && (!entry->chain || summaried != entry))
620 entry->chain = summaried;
621 summaried = entry;
623 for (ix = 0; ix != n_counts; ix++)
624 entry->counts[ix] += gcov_read_counter ();
625 skip_merge:;
627 /* Skip the MODULE_INFO records if not in dyn-ipa mode, or when reading
628 auxiliary modules. */
629 else if (tag == GCOV_TAG_MODULE_INFO && (flag_ripa_stream || flag_dyn_ipa)
630 && !module_id)
632 struct gcov_module_info* mod_info;
633 size_t info_sz;
634 /* each string has at least 8 bytes, so MOD_INFO's
635 persistent length >= in core size. */
636 mod_info
637 = (struct gcov_module_info *) alloca ((length + 2)
638 * sizeof (gcov_unsigned_t));
639 gcov_read_module_info (mod_info, length);
640 info_sz = (sizeof (struct gcov_module_info) +
641 sizeof (void *) * (mod_info->num_quote_paths +
642 mod_info->num_bracket_paths +
643 mod_info->num_cpp_defines +
644 mod_info->num_cpp_includes +
645 mod_info->num_cl_args));
646 /* The first MODULE_INFO record must be for the primary module. */
647 if (module_infos_read == 0)
649 gcc_assert (mod_info->is_primary && !modset);
650 module_infos_read++;
651 modset = pointer_set_create ();
652 pointer_set_insert (modset, (void *)(size_t)mod_info->ident);
653 primary_module_id = mod_info->ident;
654 module_infos = XCNEWVEC (struct gcov_module_info *, 1);
655 module_infos[0] = XCNEWVAR (struct gcov_module_info, info_sz);
656 memcpy (module_infos[0], mod_info, info_sz);
658 if (L_IPO_STREAM_FE_COMP_MODE_PRIM && ripa_mgf_use_fname == NULL)
659 if (!(mgf_fd = create_mgf_file (mod_info)))
661 gcov_close ();
662 return;
665 else
667 int fd;
668 char *aux_da_filename = get_da_file_name (mod_info->da_filename);
669 gcc_assert (!mod_info->is_primary);
670 if (pointer_set_insert (modset, (void *)(size_t)mod_info->ident))
672 if (flag_opt_info >= OPT_INFO_MAX)
673 inform (input_location, "Not importing %s: already imported",
674 mod_info->source_filename);
676 else if ((module_infos[0]->lang & GCOV_MODULE_LANG_MASK) !=
677 (mod_info->lang & GCOV_MODULE_LANG_MASK))
679 if (flag_opt_info >= OPT_INFO_MAX)
680 inform (input_location, "Not importing %s: source language"
681 " different from primary module's source language",
682 mod_info->source_filename);
684 else if (module_infos_read == max_group)
686 if (flag_opt_info >= OPT_INFO_MAX)
687 inform (input_location, "Not importing %s: maximum group"
688 " size reached", mod_info->source_filename);
690 else if (incompatible_cl_args (module_infos[0], mod_info))
692 if (flag_opt_info >= OPT_INFO_MAX)
693 inform (input_location, "Not importing %s: command-line"
694 " arguments not compatible with primary module",
695 mod_info->source_filename);
697 else if ((fd = open (aux_da_filename, O_RDONLY)) < 0)
699 if (flag_opt_info >= OPT_INFO_MAX)
700 inform (input_location, "Not importing %s: couldn't open %s",
701 mod_info->source_filename, aux_da_filename);
703 else if ((mod_info->lang & GCOV_MODULE_ASM_STMTS)
704 && flag_ripa_disallow_asm_modules)
706 if (flag_opt_info >= OPT_INFO_MAX)
707 inform (input_location, "Not importing %s: contains "
708 "assembler statements", mod_info->source_filename);
710 else
712 close (fd);
713 if (L_IPO_STREAM_FE_COMP_MODE_PRIM && ripa_mgf_use_fname == NULL)
715 gcc_assert (mgf_fd);
716 write_mgf_file (mgf_fd, mod_info);
717 module_infos_read++;
719 else if (!L_IPO_STREAM_FE_COMP_MODE_PRIM)
721 module_infos_read++;
722 add_input_filename (mod_info->source_filename);
723 module_infos = XRESIZEVEC (struct gcov_module_info *,
724 module_infos, num_in_fnames);
725 gcc_assert (num_in_fnames == module_infos_read);
726 module_infos[module_infos_read - 1]
727 = XCNEWVAR (struct gcov_module_info, info_sz);
728 memcpy (module_infos[module_infos_read - 1], mod_info,
729 info_sz);
734 if (flag_opt_info >= OPT_INFO_MAX)
736 inform (input_location,
737 "MODULE Id=%d, Is_Primary=%s,"
738 " Is_Exported=%s, Name=%s (%s)",
739 mod_info->ident, mod_info->is_primary?"yes":"no",
740 mod_info->is_exported?"yes":"no", mod_info->source_filename,
741 mod_info->da_filename);
744 gcov_sync (offset, length);
745 if ((is_error = gcov_is_error ()))
747 error (is_error < 0 ? "%qs has overflowed" : "%qs is corrupted",
748 da_file_name);
749 htab_delete (counts_hash);
750 break;
754 if (flag_ripa_aux_mod_id)
755 current_module_id = flag_ripa_aux_mod_id;
756 else
757 current_module_id = primary_module_id;
759 if (primary_module_id)
760 primary_module_exported = PRIMARY_MODULE_EXPORTED;
762 /* TODO: profile based multiple module compilation does not work
763 together with command line (-combine) based ipo -- add a nice
764 warning and bail out instead of asserting. */
766 if (modset)
767 pointer_set_destroy (modset);
769 if (!L_IPO_STREAM_FE_COMP_MODE)
770 gcc_assert (module_infos_read == 0
771 || module_infos_read == num_in_fnames);
773 if (flag_dyn_ipa)
774 gcc_assert (primary_module_id && num_in_fnames >= 1);
776 gcov_close ();
778 if (mgf_fd)
779 fclose (mgf_fd);
782 /* Returns the coverage data entry for counter type COUNTER of function
783 FUNC. EXPECTED is the number of expected counter entries. */
785 static counts_entry_t *
786 get_coverage_counts_entry (struct function *func, unsigned counter)
788 counts_entry_t *entry, elt;
790 elt.ident = FUNC_DECL_GLOBAL_ID (func);
791 elt.ctr = counter;
792 entry = (counts_entry_t *) htab_find (counts_hash, &elt);
794 return entry;
797 /* Returns the counters for a particular tag. */
799 gcov_type *
800 get_coverage_counts (unsigned counter, unsigned expected,
801 unsigned cfg_checksum, unsigned lineno_checksum,
802 const struct gcov_ctr_summary **summary)
804 counts_entry_t *entry;
806 /* No hash table, no counts. */
807 if (!counts_hash)
809 static int warned = 0;
811 if ((flag_opt_info >= OPT_INFO_MIN) && !warned++)
812 inform (input_location, (flag_guess_branch_prob
813 ? "file %s not found, execution counts estimated"
814 : "file %s not found, execution counts assumed to be zero"),
815 da_file_name);
816 return NULL;
819 entry = get_coverage_counts_entry (cfun, counter);
821 if (!entry)
823 if ((flag_opt_info >= OPT_INFO_MIN) && !flag_dyn_ipa)
824 warning (0, "no coverage for function %qE found",
825 DECL_ASSEMBLER_NAME (current_function_decl));
826 return NULL;
829 if (entry->cfg_checksum != cfg_checksum
830 || entry->summary.num != expected)
832 static int warned = 0;
833 bool warning_printed = false;
834 tree id = DECL_ASSEMBLER_NAME (current_function_decl);
836 warning_printed =
837 warning_at (input_location, OPT_Wcoverage_mismatch,
838 "The control flow of function %qE does not match "
839 "its profile data (counter %qs)", id, ctr_names[counter]);
840 if ((flag_opt_info >= OPT_INFO_MIN) && warning_printed)
842 inform (input_location, "Use -Wno-error=coverage-mismatch to tolerate "
843 "the mismatch but performance may drop if the function is hot");
845 if (!seen_error ()
846 && !warned++)
848 inform (input_location, "coverage mismatch ignored");
849 inform (input_location, flag_guess_branch_prob
850 ? G_("execution counts estimated")
851 : G_("execution counts assumed to be zero"));
852 if (!flag_guess_branch_prob)
853 inform (input_location,
854 "this can result in poorly optimized code");
858 return NULL;
860 else if (entry->lineno_checksum != lineno_checksum)
862 warning (OPT_Wcoverage_mismatch,
863 "Source location for function %qE have changed,"
864 " the profile data may be out of date",
865 DECL_ASSEMBLER_NAME (current_function_decl));
868 if (summary)
869 *summary = &entry->summary;
871 return entry->counts;
874 /* Returns the coverage data entry for counter type COUNTER of function
875 FUNC. On return, *N_COUNTS is set to the number of entries in the counter. */
877 gcov_type *
878 get_coverage_counts_no_warn (struct function *f, unsigned counter, unsigned *n_counts)
880 counts_entry_t *entry, elt;
882 /* No hash table, no counts. */
883 if (!counts_hash || !f)
884 return NULL;
886 elt.ident = FUNC_DECL_GLOBAL_ID (f);
887 elt.ctr = counter;
888 entry = (counts_entry_t *) htab_find (counts_hash, &elt);
889 if (!entry)
890 return NULL;
892 *n_counts = entry->summary.num;
893 return entry->counts;
896 /* Allocate NUM counters of type COUNTER. Returns nonzero if the
897 allocation succeeded. */
900 coverage_counter_alloc (unsigned counter, unsigned num)
902 if (no_coverage)
903 return 0;
905 if (!num)
906 return 1;
908 if (!tree_ctr_tables[counter])
910 /* Generate and save a copy of this so it can be shared. Leave
911 the index type unspecified for now; it will be set after all
912 functions have been compiled. */
913 char buf[20];
914 tree gcov_type_node = get_gcov_type ();
915 tree gcov_type_array_type
916 = build_array_type (gcov_type_node, NULL_TREE);
917 tree_ctr_tables[counter]
918 = build_decl (BUILTINS_LOCATION,
919 VAR_DECL, NULL_TREE, gcov_type_array_type);
920 TREE_STATIC (tree_ctr_tables[counter]) = 1;
921 ASM_GENERATE_INTERNAL_LABEL (buf, "LPBX", counter + 1);
922 DECL_NAME (tree_ctr_tables[counter]) = get_identifier (buf);
923 DECL_ALIGN (tree_ctr_tables[counter]) = TYPE_ALIGN (gcov_type_node);
925 if (dump_file)
926 fprintf (dump_file, "Using data file %s\n", da_file_name);
928 fn_b_ctrs[counter] = fn_n_ctrs[counter];
929 fn_n_ctrs[counter] += num;
930 fn_ctr_mask |= 1 << counter;
931 return 1;
934 /* Generate a tree to access COUNTER NO. */
936 tree
937 tree_coverage_counter_ref (unsigned counter, unsigned no)
939 tree gcov_type_node = get_gcov_type ();
941 gcc_assert (no < fn_n_ctrs[counter] - fn_b_ctrs[counter]);
942 no += prg_n_ctrs[counter] + fn_b_ctrs[counter];
944 /* "no" here is an array index, scaled to bytes later. */
945 return build4 (ARRAY_REF, gcov_type_node, tree_ctr_tables[counter],
946 build_int_cst (NULL_TREE, no), NULL, NULL);
949 /* Generate a tree to access the address of COUNTER NO. */
951 tree
952 tree_coverage_counter_addr (unsigned counter, unsigned no)
954 tree gcov_type_node = get_gcov_type ();
956 gcc_assert (no < fn_n_ctrs[counter] - fn_b_ctrs[counter]);
957 no += prg_n_ctrs[counter] + fn_b_ctrs[counter];
959 TREE_ADDRESSABLE (tree_ctr_tables[counter]) = 1;
961 /* "no" here is an array index, scaled to bytes later. */
962 return build_fold_addr_expr (build4 (ARRAY_REF, gcov_type_node,
963 tree_ctr_tables[counter],
964 build_int_cst (NULL_TREE, no),
965 NULL, NULL));
969 /* Generate a checksum for a string. CHKSUM is the current
970 checksum. */
972 static unsigned
973 coverage_checksum_string (unsigned chksum, const char *string)
975 int i;
976 char *dup = NULL;
978 /* Look for everything that looks if it were produced by
979 get_file_function_name and zero out the second part
980 that may result from flag_random_seed. This is not critical
981 as the checksums are used only for sanity checking. */
982 for (i = 0; string[i]; i++)
984 int offset = 0;
985 if (!strncmp (string + i, "_GLOBAL__N_", 11))
986 offset = 11;
987 if (!strncmp (string + i, "_GLOBAL__", 9))
988 offset = 9;
990 /* C++ namespaces do have scheme:
991 _GLOBAL__N_<filename>_<wrongmagicnumber>_<magicnumber>functionname
992 since filename might contain extra underscores there seems
993 to be no better chance then walk all possible offsets looking
994 for magicnumber. */
995 if (offset)
997 for (i = i + offset; string[i]; i++)
998 if (string[i]=='_')
1000 int y;
1002 for (y = 1; y < 9; y++)
1003 if (!(string[i + y] >= '0' && string[i + y] <= '9')
1004 && !(string[i + y] >= 'A' && string[i + y] <= 'F'))
1005 break;
1006 if (y != 9 || string[i + 9] != '_')
1007 continue;
1008 for (y = 10; y < 18; y++)
1009 if (!(string[i + y] >= '0' && string[i + y] <= '9')
1010 && !(string[i + y] >= 'A' && string[i + y] <= 'F'))
1011 break;
1012 if (y != 18)
1013 continue;
1014 if (!dup)
1015 string = dup = xstrdup (string);
1016 for (y = 10; y < 18; y++)
1017 dup[i + y] = '0';
1019 break;
1023 chksum = crc32_string (chksum, string);
1024 if (dup)
1025 free (dup);
1027 return chksum;
1030 /* Compute checksum for the current function. We generate a CRC32. */
1032 unsigned
1033 coverage_compute_lineno_checksum (void)
1035 tree name;
1036 expanded_location xloc
1037 = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
1038 unsigned chksum = xloc.line;
1039 const char *pathless_filename = xloc.file;
1040 int i;
1041 for (i = strlen (xloc.file); i >= 0; i--)
1042 if (IS_DIR_SEPARATOR (pathless_filename[i]))
1044 pathless_filename += i + 1;
1045 break;
1048 chksum = coverage_checksum_string (chksum, pathless_filename);
1050 /* Note: it is a bad design that C++ FE associate the convertion function type
1051 with the name of the decl. This leads to cross contamination between different
1052 conversion operators in different modules (If conv_type_names map is cleared
1053 at the end of parsing of each module). */
1054 if (flag_dyn_ipa && lang_hooks.user_conv_function_p (current_function_decl))
1055 name = DECL_ASSEMBLER_NAME (current_function_decl);
1056 else
1057 name = DECL_NAME (current_function_decl);
1059 chksum = coverage_checksum_string
1060 (chksum, IDENTIFIER_POINTER (name));
1062 return chksum;
1065 /* Compute cfg checksum for the current function.
1066 The checksum is calculated carefully so that
1067 source code changes that doesn't affect the control flow graph
1068 won't change the checksum.
1069 This is to make the profile data useable across source code change.
1070 The downside of this is that the compiler may use potentially
1071 wrong profile data - that the source code change has non-trivial impact
1072 on the validity of profile data (e.g. the reversed condition)
1073 but the compiler won't detect the change and use the wrong profile data. */
1075 unsigned
1076 coverage_compute_cfg_checksum (void)
1078 basic_block bb;
1079 unsigned chksum = n_basic_blocks;
1081 FOR_EACH_BB (bb)
1083 edge e;
1084 edge_iterator ei;
1085 chksum = crc32_byte (chksum, bb->index);
1086 FOR_EACH_EDGE (e, ei, bb->succs)
1088 chksum = crc32_byte (chksum, e->dest->index);
1092 return chksum;
1095 /* Begin output to the graph file for the current function.
1096 Opens the output file, if not already done. Writes the
1097 function header, if not already done. Returns nonzero if data
1098 should be output. */
1101 coverage_begin_output (unsigned lineno_checksum, unsigned cfg_checksum)
1103 /* We don't need to output .gcno file unless we're under -ftest-coverage
1104 (e.g. -fprofile-arcs/generate/use don't need .gcno to work). */
1105 if (no_coverage || !flag_test_coverage || flag_compare_debug)
1106 return 0;
1108 if (!bbg_function_announced)
1110 expanded_location xloc
1111 = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
1112 unsigned long offset;
1114 if (!bbg_file_opened)
1116 if (!gcov_open (bbg_file_name, -1))
1117 error ("cannot open %s", bbg_file_name);
1118 else
1120 gcov_write_unsigned (GCOV_NOTE_MAGIC);
1121 gcov_write_unsigned (GCOV_VERSION);
1122 gcov_write_unsigned (local_tick);
1124 bbg_file_opened = 1;
1128 /* Announce function */
1129 offset = gcov_write_tag (GCOV_TAG_FUNCTION);
1130 gcov_write_unsigned (FUNC_DECL_FUNC_ID (cfun));
1131 gcov_write_unsigned (lineno_checksum);
1132 gcov_write_unsigned (cfg_checksum);
1133 gcov_write_string (IDENTIFIER_POINTER
1134 (DECL_ASSEMBLER_NAME (current_function_decl)));
1135 gcov_write_string (xloc.file);
1136 gcov_write_unsigned (xloc.line);
1137 gcov_write_length (offset);
1139 bbg_function_announced = 1;
1141 return !gcov_is_error ();
1144 /* Finish coverage data for the current function. Verify no output
1145 error has occurred. Save function coverage counts. */
1147 void
1148 coverage_end_function (unsigned lineno_checksum, unsigned cfg_checksum)
1150 unsigned i;
1152 if (bbg_file_opened > 1 && gcov_is_error ())
1154 warning (0, "error writing %qs", bbg_file_name);
1155 bbg_file_opened = -1;
1158 if (fn_ctr_mask)
1160 struct function_list *item;
1162 item = XCNEW (struct function_list);
1164 *functions_tail = item;
1165 functions_tail = &item->next;
1168 item->next = 0;
1169 item->ident = FUNC_DECL_FUNC_ID (cfun);
1170 item->lineno_checksum = lineno_checksum;
1171 item->cfg_checksum = cfg_checksum;
1172 for (i = 0; i != GCOV_COUNTERS; i++)
1174 item->n_ctrs[i] = fn_n_ctrs[i];
1175 prg_n_ctrs[i] += fn_n_ctrs[i];
1176 fn_n_ctrs[i] = fn_b_ctrs[i] = 0;
1178 prg_ctr_mask |= fn_ctr_mask;
1179 fn_ctr_mask = 0;
1181 bbg_function_announced = 0;
1184 /* True if a function entry corresponding to the given FN_IDENT
1185 is present in the coverage internal data structures. */
1187 bool
1188 coverage_function_present (unsigned fn_ident)
1190 struct function_list *item = functions_head;
1191 while (item && item->ident != fn_ident)
1192 item = item->next;
1193 return item != NULL;
1196 /* Update function and program direct-call coverage counts. */
1198 void
1199 coverage_dc_end_function (void)
1201 if (fn_ctr_mask)
1203 const unsigned idx = GCOV_COUNTER_DIRECT_CALL;
1204 struct function_list *item = functions_head;
1205 while (item && item->ident != (unsigned) FUNC_DECL_FUNC_ID (cfun))
1206 item = item->next;
1208 /* If a matching function entry hasn't been found, either this function
1209 has had no coverage counts added in the profile pass, or this
1210 is a new function (function versioning, etc). Create a new entry. */
1211 if (!item)
1213 int i;
1214 item = XCNEW (struct function_list);
1215 *functions_tail = item;
1216 functions_tail = &item->next;
1217 item->next = 0;
1218 item->ident = FUNC_DECL_FUNC_ID (cfun);
1219 item->lineno_checksum = coverage_compute_lineno_checksum ();
1220 item->cfg_checksum = coverage_compute_cfg_checksum ();
1221 for (i = 0; i < GCOV_COUNTERS; i++)
1222 item->n_ctrs[i] = 0;
1225 item->n_ctrs[idx] += fn_n_ctrs[idx];
1226 item->dc_offset = prg_n_ctrs[idx];
1227 prg_n_ctrs[idx] += fn_n_ctrs[idx];
1228 fn_n_ctrs[idx] = fn_b_ctrs[idx] = 0;
1229 prg_ctr_mask |= fn_ctr_mask;
1230 fn_ctr_mask = 0;
1231 add_referenced_var (tree_ctr_tables[idx]);
1235 /* Creates the gcov_fn_info RECORD_TYPE. */
1237 static tree
1238 build_fn_info_type (unsigned int counters)
1240 tree type = lang_hooks.types.make_type (RECORD_TYPE);
1241 tree field, fields;
1242 tree array_type;
1244 /* ident */
1245 fields = build_decl (BUILTINS_LOCATION,
1246 FIELD_DECL, NULL_TREE, get_gcov_unsigned_t ());
1247 /* lineno_checksum */
1248 field = build_decl (BUILTINS_LOCATION,
1249 FIELD_DECL, NULL_TREE, get_gcov_unsigned_t ());
1250 DECL_CHAIN (field) = fields;
1251 fields = field;
1253 /* cfg checksum */
1254 field = build_decl (BUILTINS_LOCATION,
1255 FIELD_DECL, NULL_TREE, get_gcov_unsigned_t ());
1256 DECL_CHAIN (field) = fields;
1257 fields = field;
1259 /* dc offset */
1260 field = build_decl (BUILTINS_LOCATION,
1261 FIELD_DECL, NULL_TREE, get_gcov_unsigned_t ());
1262 TREE_CHAIN (field) = fields;
1263 fields = field;
1265 array_type = build_int_cst (NULL_TREE, counters - 1);
1266 array_type = build_index_type (array_type);
1267 array_type = build_array_type (get_gcov_unsigned_t (), array_type);
1269 /* counters */
1270 field = build_decl (BUILTINS_LOCATION,
1271 FIELD_DECL, NULL_TREE, array_type);
1272 DECL_CHAIN (field) = fields;
1273 fields = field;
1275 finish_builtin_struct (type, "__gcov_fn_info", fields, NULL_TREE);
1277 return type;
1280 /* Creates a CONSTRUCTOR for a gcov_fn_info. FUNCTION is
1281 the function being processed and TYPE is the gcov_fn_info
1282 RECORD_TYPE. */
1284 static tree
1285 build_fn_info_value (const struct function_list *function, tree type)
1287 tree fields = TYPE_FIELDS (type);
1288 unsigned ix;
1289 VEC(constructor_elt,gc) *v1 = NULL;
1290 VEC(constructor_elt,gc) *v2 = NULL;
1292 /* ident */
1293 CONSTRUCTOR_APPEND_ELT (v1, fields,
1294 build_int_cstu (get_gcov_unsigned_t (),
1295 function->ident));
1296 fields = DECL_CHAIN (fields);
1298 /* lineno_checksum */
1299 CONSTRUCTOR_APPEND_ELT (v1, fields,
1300 build_int_cstu (get_gcov_unsigned_t (),
1301 function->lineno_checksum));
1302 fields = DECL_CHAIN (fields);
1304 /* cfg_checksum */
1305 CONSTRUCTOR_APPEND_ELT (v1, fields,
1306 build_int_cstu (get_gcov_unsigned_t (),
1307 function->cfg_checksum));
1308 fields = DECL_CHAIN (fields);
1310 /* dc offset */
1311 CONSTRUCTOR_APPEND_ELT (v1, fields,
1312 build_int_cstu (get_gcov_unsigned_t (),
1313 function->dc_offset));
1314 fields = TREE_CHAIN (fields);
1316 /* counters */
1317 for (ix = 0; ix != GCOV_COUNTERS; ix++)
1318 if (prg_ctr_mask & (1 << ix))
1319 CONSTRUCTOR_APPEND_ELT (v2, NULL,
1320 build_int_cstu (get_gcov_unsigned_t (),
1321 function->n_ctrs[ix]));
1323 CONSTRUCTOR_APPEND_ELT (v1, fields,
1324 build_constructor (TREE_TYPE (fields), v2));
1326 return build_constructor (type, v1);
1329 /* Creates the gcov_ctr_info RECORD_TYPE. */
1331 static tree
1332 build_ctr_info_type (void)
1334 tree type = lang_hooks.types.make_type (RECORD_TYPE);
1335 tree field, fields = NULL_TREE;
1336 tree gcov_ptr_type = build_pointer_type (get_gcov_type ());
1337 tree gcov_merge_fn_type;
1339 /* counters */
1340 field = build_decl (BUILTINS_LOCATION,
1341 FIELD_DECL, NULL_TREE, get_gcov_unsigned_t ());
1342 DECL_CHAIN (field) = fields;
1343 fields = field;
1345 /* values */
1346 field = build_decl (BUILTINS_LOCATION,
1347 FIELD_DECL, NULL_TREE, gcov_ptr_type);
1348 DECL_CHAIN (field) = fields;
1349 fields = field;
1351 /* merge */
1352 gcov_merge_fn_type =
1353 build_function_type_list (void_type_node,
1354 gcov_ptr_type, get_gcov_unsigned_t (),
1355 NULL_TREE);
1356 field = build_decl (BUILTINS_LOCATION,
1357 FIELD_DECL, NULL_TREE,
1358 build_pointer_type (gcov_merge_fn_type));
1359 DECL_CHAIN (field) = fields;
1360 fields = field;
1362 finish_builtin_struct (type, "__gcov_ctr_info", fields, NULL_TREE);
1364 return type;
1367 /* Creates a CONSTRUCTOR for a gcov_ctr_info. COUNTER is
1368 the counter being processed and TYPE is the gcov_ctr_info
1369 RECORD_TYPE. */
1371 static tree
1372 build_ctr_info_value (unsigned int counter, tree type)
1374 tree fields = TYPE_FIELDS (type);
1375 tree fn;
1376 VEC(constructor_elt,gc) *v = NULL;
1378 /* counters */
1379 CONSTRUCTOR_APPEND_ELT (v, fields,
1380 build_int_cstu (get_gcov_unsigned_t (),
1381 prg_n_ctrs[counter]));
1382 fields = DECL_CHAIN (fields);
1384 if (prg_n_ctrs[counter])
1386 tree array_type;
1388 array_type = build_int_cstu (get_gcov_unsigned_t (),
1389 prg_n_ctrs[counter] - 1);
1390 array_type = build_index_type (array_type);
1391 array_type = build_array_type (TREE_TYPE (TREE_TYPE (fields)),
1392 array_type);
1394 TREE_TYPE (tree_ctr_tables[counter]) = array_type;
1395 DECL_SIZE (tree_ctr_tables[counter]) = TYPE_SIZE (array_type);
1396 DECL_SIZE_UNIT (tree_ctr_tables[counter]) = TYPE_SIZE_UNIT (array_type);
1397 varpool_finalize_decl (tree_ctr_tables[counter]);
1399 CONSTRUCTOR_APPEND_ELT (v, fields,
1400 build1 (ADDR_EXPR, TREE_TYPE (fields),
1401 tree_ctr_tables[counter]));
1403 else
1404 CONSTRUCTOR_APPEND_ELT (v, fields, null_pointer_node);
1405 fields = DECL_CHAIN (fields);
1407 fn = build_decl (BUILTINS_LOCATION,
1408 FUNCTION_DECL,
1409 get_identifier (ctr_merge_functions[counter]),
1410 TREE_TYPE (TREE_TYPE (fields)));
1411 DECL_EXTERNAL (fn) = 1;
1412 TREE_PUBLIC (fn) = 1;
1413 DECL_ARTIFICIAL (fn) = 1;
1414 TREE_NOTHROW (fn) = 1;
1415 DECL_ASSEMBLER_NAME (fn); /* Initialize assembler name so we can stream out. */
1416 CONSTRUCTOR_APPEND_ELT (v, fields, build1 (ADDR_EXPR, TREE_TYPE (fields), fn));
1418 return build_constructor (type, v);
1421 /* Returns an array (tree) of include path strings. STRING_TYPE is
1422 the path string type, INC_PATH_VALUE is the initial value of the
1423 path array, PATHS gives raw path string values, and NUM is the
1424 number of paths. */
1426 static tree
1427 build_inc_path_array_value (tree string_type, tree inc_path_value,
1428 cpp_dir *paths, int num)
1430 int i;
1431 cpp_dir *pdir;
1432 for (i = 0, pdir = paths; i < num; pdir = pdir->next)
1434 const char *path_raw_string;
1435 int path_string_length;
1436 tree path_string;
1437 path_raw_string = pdir->name;
1438 path_string_length = strlen (path_raw_string);
1439 path_string = build_string (path_string_length + 1, path_raw_string);
1440 TREE_TYPE (path_string) = build_array_type
1441 (char_type_node, build_index_type
1442 (build_int_cst (NULL_TREE, path_string_length)));
1443 inc_path_value = tree_cons (NULL_TREE,
1444 build1 (ADDR_EXPR, string_type, path_string),
1445 inc_path_value);
1446 i++;
1448 return inc_path_value;
1451 /* Returns an array (tree) of strings. STR_TYPE is the string type,
1452 STR_ARRAY_VALUE is the initial value of the string array, and HEAD gives
1453 the list of raw strings. */
1455 static tree
1456 build_str_array_value (tree str_type, tree str_array_value,
1457 struct str_list *head)
1459 const char *raw_str;
1460 int str_length;
1461 while (head)
1463 tree str;
1464 raw_str = head->str;
1465 str_length = strlen (raw_str);
1466 str = build_string (str_length + 1, raw_str);
1467 TREE_TYPE (str) =
1468 build_array_type (char_type_node,
1469 build_index_type (build_int_cst (NULL_TREE,
1470 str_length)));
1471 str_array_value = tree_cons (NULL_TREE,
1472 build1 (ADDR_EXPR, str_type, str),
1473 str_array_value);
1474 head = head->next;
1476 return str_array_value;
1479 /* Returns an array (tree) of command-line argument strings. STRING_TYPE is
1480 the string type, CL_ARGS_VALUE is the initial value of the command-line
1481 args array. */
1483 static tree
1484 build_cl_args_array_value (tree string_type, tree cl_args_value)
1486 unsigned int i;
1487 for (i = 0; i < num_lipo_cl_args; i++)
1489 int arg_length = strlen (lipo_cl_args[i]);
1490 tree arg_string = build_string (arg_length + 1, lipo_cl_args[i]);
1491 TREE_TYPE (arg_string) =
1492 build_array_type (char_type_node,
1493 build_index_type (build_int_cst (NULL_TREE,
1494 arg_length)));
1495 cl_args_value = tree_cons (NULL_TREE,
1496 build1 (ADDR_EXPR, string_type, arg_string),
1497 cl_args_value);
1499 return cl_args_value;
1502 /* Returns the value of the module info associated with the
1503 current source module being compiled. */
1505 static tree
1506 build_gcov_module_info_value (void)
1508 tree type, field, fields = NULL_TREE;
1509 tree string_type, index_type, string_array_type;
1510 tree value = NULL_TREE, string_array = NULL_TREE, mod_info;
1511 int file_name_len;
1512 tree filename_string;
1513 cpp_dir *quote_paths, *bracket_paths, *pdir;
1514 int num_quote_paths = 0, num_bracket_paths = 0;
1515 unsigned lang;
1516 char name_buf[50];
1518 type = lang_hooks.types.make_type (RECORD_TYPE);
1519 string_type = build_pointer_type (
1520 build_qualified_type (char_type_node,
1521 TYPE_QUAL_CONST));
1523 /* ident */
1524 field = build_decl (UNKNOWN_LOCATION, FIELD_DECL,
1525 NULL_TREE, get_gcov_unsigned_t ());
1526 TREE_CHAIN (field) = fields;
1527 fields = field;
1528 value = tree_cons (fields, build_int_cstu (get_gcov_unsigned_t (),
1529 0), value);
1532 /* is_primary */
1533 /* We also overload this field to store a flag that indicates whether this
1534 module was built in regular FDO or LIPO mode (-fripa). When reading this
1535 field from a GCDA file, it should be used as the IS_PRIMARY flag. When
1536 reading this field from the binary's data section, it should be used
1537 as a FDO/LIPO flag. */
1538 field = build_decl (UNKNOWN_LOCATION, FIELD_DECL,
1539 NULL_TREE, get_gcov_unsigned_t ());
1540 TREE_CHAIN (field) = fields;
1541 fields = field;
1542 value = tree_cons (field, build_int_cstu (get_gcov_unsigned_t (),
1543 flag_dyn_ipa ? 1 :0), value);
1545 /* is_exported */
1546 field = build_decl (UNKNOWN_LOCATION, FIELD_DECL,
1547 NULL_TREE, get_gcov_unsigned_t ());
1548 TREE_CHAIN (field) = fields;
1549 fields = field;
1550 value = tree_cons (field, build_int_cstu (get_gcov_unsigned_t (),
1551 0), value);
1553 /* lang field */
1554 field = build_decl (UNKNOWN_LOCATION, FIELD_DECL,
1555 NULL_TREE, get_gcov_unsigned_t ());
1556 TREE_CHAIN (field) = fields;
1557 fields = field;
1558 if (!strcmp (lang_hooks.name, "GNU C"))
1559 lang = GCOV_MODULE_C_LANG;
1560 else if (!strcmp (lang_hooks.name, "GNU C++"))
1561 lang = GCOV_MODULE_CPP_LANG;
1562 else
1563 lang = GCOV_MODULE_UNKNOWN_LANG;
1564 if (has_asm_statement)
1565 lang |= GCOV_MODULE_ASM_STMTS;
1566 value = tree_cons (field, build_int_cstu (get_gcov_unsigned_t (),
1567 lang), value);
1569 /* da_filename */
1570 field = build_decl (UNKNOWN_LOCATION, FIELD_DECL,
1571 NULL_TREE, string_type);
1572 TREE_CHAIN (field) = fields;
1573 fields = field;
1575 file_name_len = strlen (da_base_file_name);
1576 filename_string = build_string (file_name_len + 1, da_base_file_name);
1577 TREE_TYPE (filename_string) = build_array_type
1578 (char_type_node, build_index_type
1579 (build_int_cst (NULL_TREE, file_name_len)));
1580 value = tree_cons (field, build1 (ADDR_EXPR, string_type, filename_string),
1581 value);
1583 /* Source name */
1584 field = build_decl (UNKNOWN_LOCATION, FIELD_DECL,
1585 NULL_TREE, string_type);
1586 TREE_CHAIN (field) = fields;
1587 fields = field;
1588 file_name_len = strlen (main_input_file_name);
1589 filename_string = build_string (file_name_len + 1, main_input_file_name);
1590 TREE_TYPE (filename_string) = build_array_type
1591 (char_type_node, build_index_type
1592 (build_int_cst (NULL_TREE, file_name_len)));
1593 value = tree_cons (field, build1 (ADDR_EXPR, string_type, filename_string),
1594 value);
1596 get_include_chains (&quote_paths, &bracket_paths);
1597 for (pdir = quote_paths; pdir; pdir = pdir->next)
1599 if (pdir == bracket_paths)
1600 break;
1601 num_quote_paths++;
1603 for (pdir = bracket_paths; pdir; pdir = pdir->next)
1604 num_bracket_paths++;
1606 /* Num quote paths */
1607 field = build_decl (UNKNOWN_LOCATION, FIELD_DECL,
1608 NULL_TREE, get_gcov_unsigned_t ());
1609 TREE_CHAIN (field) = fields;
1610 fields = field;
1611 value = tree_cons (field, build_int_cstu (get_gcov_unsigned_t (),
1612 num_quote_paths), value);
1613 /* Num bracket paths */
1614 field = build_decl (UNKNOWN_LOCATION, FIELD_DECL,
1615 NULL_TREE, get_gcov_unsigned_t ());
1616 TREE_CHAIN (field) = fields;
1617 fields = field;
1618 value = tree_cons (field, build_int_cstu (get_gcov_unsigned_t (),
1619 num_bracket_paths), value);
1621 /* Num -D/-U options. */
1622 field = build_decl (UNKNOWN_LOCATION, FIELD_DECL,
1623 NULL_TREE, get_gcov_unsigned_t ());
1624 TREE_CHAIN (field) = fields;
1625 fields = field;
1626 value = tree_cons (field, build_int_cstu (get_gcov_unsigned_t (),
1627 num_cpp_defines), value);
1629 /* Num -imacro/-include options. */
1630 field = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE,
1631 get_gcov_unsigned_t ());
1632 TREE_CHAIN (field) = fields;
1633 fields = field;
1634 value = tree_cons (field, build_int_cstu (get_gcov_unsigned_t (),
1635 num_cpp_includes), value);
1637 /* Num command-line args. */
1638 field = build_decl (UNKNOWN_LOCATION, FIELD_DECL,
1639 NULL_TREE, get_gcov_unsigned_t ());
1640 TREE_CHAIN (field) = fields;
1641 fields = field;
1642 value = tree_cons (field, build_int_cstu (get_gcov_unsigned_t (),
1643 num_lipo_cl_args), value);
1645 /* string array */
1646 index_type = build_index_type (build_int_cst (NULL_TREE,
1647 num_quote_paths +
1648 num_bracket_paths +
1649 num_cpp_defines +
1650 num_cpp_includes +
1651 num_lipo_cl_args));
1652 string_array_type = build_array_type (string_type, index_type);
1653 string_array = build_inc_path_array_value (string_type, string_array,
1654 quote_paths, num_quote_paths);
1655 string_array = build_inc_path_array_value (string_type, string_array,
1656 bracket_paths, num_bracket_paths);
1657 string_array = build_str_array_value (string_type, string_array,
1658 cpp_defines_head);
1659 string_array = build_str_array_value (string_type, string_array,
1660 cpp_includes_head);
1661 string_array = build_cl_args_array_value (string_type, string_array);
1662 string_array = build_constructor_from_list (string_array_type,
1663 nreverse (string_array));
1664 field = build_decl (UNKNOWN_LOCATION, FIELD_DECL,
1665 NULL_TREE, string_array_type);
1666 TREE_CHAIN (field) = fields;
1667 fields = field;
1668 value = tree_cons (field, string_array, value);
1670 finish_builtin_struct (type, "__gcov_module_info", fields, NULL_TREE);
1672 /* FIXME: use build_constructor directly. */
1673 value = build_constructor_from_list (type, nreverse (value));
1676 mod_info = build_decl (UNKNOWN_LOCATION, VAR_DECL,
1677 NULL_TREE, TREE_TYPE (value));
1678 TREE_STATIC (mod_info) = 1;
1679 ASM_GENERATE_INTERNAL_LABEL (name_buf, "MODINFO", 0);
1680 DECL_NAME (mod_info) = get_identifier (name_buf);
1681 DECL_INITIAL (mod_info) = value;
1683 /* Build structure. */
1684 varpool_finalize_decl (mod_info);
1686 return mod_info;
1689 /* Creates the gcov_info RECORD_TYPE and initializer for it. Returns a
1690 CONSTRUCTOR. */
1692 static tree
1693 build_gcov_info (void)
1695 unsigned n_ctr_types, ix;
1696 tree type, const_type;
1697 tree fn_info_type, fn_info_value = NULL_TREE;
1698 tree fn_info_ptr_type;
1699 tree ctr_info_type, ctr_info_ary_type, ctr_info_value = NULL_TREE;
1700 tree field, fields = NULL_TREE;
1701 tree mod_value = NULL_TREE, mod_type = NULL_TREE;
1702 tree filename_string;
1703 int da_file_name_len;
1704 unsigned n_fns;
1705 const struct function_list *fn;
1706 tree string_type;
1707 VEC(constructor_elt,gc) *v1 = NULL;
1708 VEC(constructor_elt,gc) *v2 = NULL;
1710 /* Count the number of active counters. */
1711 for (n_ctr_types = 0, ix = 0; ix != GCOV_COUNTERS; ix++)
1712 if (prg_ctr_mask & (1 << ix))
1713 n_ctr_types++;
1715 type = lang_hooks.types.make_type (RECORD_TYPE);
1716 const_type = build_qualified_type (type, TYPE_QUAL_CONST);
1718 /* Version ident */
1719 field = build_decl (BUILTINS_LOCATION,
1720 FIELD_DECL, NULL_TREE, get_gcov_unsigned_t ());
1721 DECL_CHAIN (field) = fields;
1722 fields = field;
1723 CONSTRUCTOR_APPEND_ELT (v1, field,
1724 build_int_cstu (TREE_TYPE (field), GCOV_VERSION));
1726 /* mod_info */
1727 mod_value = build_gcov_module_info_value ();
1728 mod_type = build_pointer_type (TREE_TYPE (mod_value));
1729 field = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE, mod_type);
1730 TREE_CHAIN (field) = fields;
1731 fields = field;
1732 mod_value = build1 (ADDR_EXPR, mod_type, mod_value);
1733 CONSTRUCTOR_APPEND_ELT (v1, field, mod_value);
1736 /* next -- NULL */
1737 field = build_decl (BUILTINS_LOCATION,
1738 FIELD_DECL, NULL_TREE, build_pointer_type (const_type));
1739 DECL_CHAIN (field) = fields;
1740 fields = field;
1741 CONSTRUCTOR_APPEND_ELT (v1, field, null_pointer_node);
1743 /* stamp */
1744 field = build_decl (BUILTINS_LOCATION,
1745 FIELD_DECL, NULL_TREE, get_gcov_unsigned_t ());
1746 DECL_CHAIN (field) = fields;
1747 fields = field;
1748 CONSTRUCTOR_APPEND_ELT (v1, field,
1749 build_int_cstu (TREE_TYPE (field), local_tick));
1751 /* Filename */
1752 string_type = build_pointer_type (build_qualified_type (char_type_node,
1753 TYPE_QUAL_CONST));
1754 field = build_decl (BUILTINS_LOCATION,
1755 FIELD_DECL, NULL_TREE, string_type);
1756 DECL_CHAIN (field) = fields;
1757 fields = field;
1758 da_file_name_len = strlen (da_file_name);
1759 filename_string = build_string (da_file_name_len + 1, da_file_name);
1760 TREE_TYPE (filename_string) = build_array_type
1761 (char_type_node, build_index_type
1762 (build_int_cst (NULL_TREE, da_file_name_len)));
1763 CONSTRUCTOR_APPEND_ELT (v1, field,
1764 build1 (ADDR_EXPR, string_type, filename_string));
1766 /* eof_pos */
1767 field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1768 NULL_TREE, get_gcov_unsigned_t ());
1769 TREE_CHAIN (field) = fields;
1770 fields = field;
1771 CONSTRUCTOR_APPEND_ELT (v1, field, build_int_cstu (TREE_TYPE (field), 0));
1773 /* Build the fn_info type and initializer. */
1774 fn_info_type = build_fn_info_type (n_ctr_types);
1775 fn_info_ptr_type = build_pointer_type (build_qualified_type
1776 (fn_info_type, TYPE_QUAL_CONST));
1777 for (fn = functions_head, n_fns = 0; fn; fn = fn->next, n_fns++)
1778 CONSTRUCTOR_APPEND_ELT (v2, NULL_TREE,
1779 build_fn_info_value (fn, fn_info_type));
1781 if (n_fns)
1783 tree array_type;
1785 array_type = build_index_type (build_int_cst (NULL_TREE, n_fns - 1));
1786 array_type = build_array_type (fn_info_type, array_type);
1788 fn_info_value = build_constructor (array_type, v2);
1789 fn_info_value = build1 (ADDR_EXPR, fn_info_ptr_type, fn_info_value);
1791 else
1792 fn_info_value = null_pointer_node;
1794 /* number of functions */
1795 field = build_decl (BUILTINS_LOCATION,
1796 FIELD_DECL, NULL_TREE, get_gcov_unsigned_t ());
1797 DECL_CHAIN (field) = fields;
1798 fields = field;
1799 CONSTRUCTOR_APPEND_ELT (v1, field,
1800 build_int_cstu (get_gcov_unsigned_t (), n_fns));
1802 /* fn_info table */
1803 field = build_decl (BUILTINS_LOCATION,
1804 FIELD_DECL, NULL_TREE, fn_info_ptr_type);
1805 DECL_CHAIN (field) = fields;
1806 fields = field;
1807 CONSTRUCTOR_APPEND_ELT (v1, field, fn_info_value);
1809 /* counter_mask */
1810 field = build_decl (BUILTINS_LOCATION,
1811 FIELD_DECL, NULL_TREE, get_gcov_unsigned_t ());
1812 DECL_CHAIN (field) = fields;
1813 fields = field;
1814 CONSTRUCTOR_APPEND_ELT (v1, field,
1815 build_int_cstu (get_gcov_unsigned_t (),
1816 prg_ctr_mask));
1818 /* counters */
1819 ctr_info_type = build_ctr_info_type ();
1820 ctr_info_ary_type = build_index_type (build_int_cst (NULL_TREE,
1821 n_ctr_types));
1822 ctr_info_ary_type = build_array_type (ctr_info_type, ctr_info_ary_type);
1823 v2 = NULL;
1824 for (ix = 0; ix != GCOV_COUNTERS; ix++)
1825 if (prg_ctr_mask & (1 << ix))
1826 CONSTRUCTOR_APPEND_ELT (v2, NULL_TREE,
1827 build_ctr_info_value (ix, ctr_info_type));
1828 ctr_info_value = build_constructor (ctr_info_ary_type, v2);
1830 field = build_decl (BUILTINS_LOCATION,
1831 FIELD_DECL, NULL_TREE, ctr_info_ary_type);
1832 DECL_CHAIN (field) = fields;
1833 fields = field;
1834 CONSTRUCTOR_APPEND_ELT (v1, field, ctr_info_value);
1836 finish_builtin_struct (type, "__gcov_info", fields, NULL_TREE);
1838 return build_constructor (type, v1);
1841 /* Write out the structure which libgcov uses to locate all the
1842 counters. The structures used here must match those defined in
1843 gcov-io.h. Write out the constructor to call __gcov_init. */
1845 static void
1846 create_coverage (void)
1848 tree gcov_info, gcov_init, body, t;
1849 char name_buf[32];
1851 no_coverage = 1; /* Disable any further coverage. */
1853 if (!prg_ctr_mask && !flag_pmu_profile_generate)
1854 return;
1856 t = build_gcov_info ();
1858 gcov_info = build_decl (BUILTINS_LOCATION,
1859 VAR_DECL, NULL_TREE, TREE_TYPE (t));
1860 TREE_STATIC (gcov_info) = 1;
1861 ASM_GENERATE_INTERNAL_LABEL (name_buf, "LPBX", 0);
1862 DECL_NAME (gcov_info) = get_identifier (name_buf);
1863 DECL_INITIAL (gcov_info) = t;
1865 /* Build structure. */
1866 varpool_finalize_decl (gcov_info);
1868 /* Build a decl for __gcov_init. */
1869 t = build_pointer_type (TREE_TYPE (gcov_info));
1870 t = build_function_type_list (void_type_node, t, NULL);
1871 t = build_decl (BUILTINS_LOCATION,
1872 FUNCTION_DECL, get_identifier ("__gcov_init"), t);
1873 TREE_PUBLIC (t) = 1;
1874 DECL_EXTERNAL (t) = 1;
1875 DECL_ASSEMBLER_NAME (t); /* Initialize assembler name so we can stream out. */
1876 gcov_init = t;
1878 /* Generate a call to __gcov_init(&gcov_info). */
1879 body = NULL;
1880 t = build_fold_addr_expr (gcov_info);
1881 t = build_call_expr (gcov_init, 1, t);
1882 append_to_statement_list (t, &body);
1884 /* Generate a constructor to run it. */
1885 cgraph_build_static_cdtor ('I', body, DEFAULT_INIT_PRIORITY);
1888 /* Get the da file name, given base file name. */
1890 static char *
1891 get_da_file_name (const char *orig_base_file_name)
1893 const char *base_file_name;
1894 char *da_file_name;
1895 int len;
1896 const char *prefix = profile_data_prefix;
1897 /* + 1 for extra '/', in case prefix doesn't end with /. */
1898 int prefix_len;
1900 if (gcov_da_name)
1901 base_file_name = gcov_da_name;
1902 else
1903 base_file_name = orig_base_file_name;
1905 len = strlen (base_file_name);
1907 if (prefix == 0 && base_file_name[0] != '/')
1908 prefix = getpwd ();
1910 prefix_len = (prefix) ? strlen (prefix) + 1 : 0;
1912 /* Name of da file. */
1913 da_file_name = XNEWVEC (char, len + strlen (GCOV_DATA_SUFFIX)
1914 + prefix_len + 1);
1916 if (prefix)
1918 strcpy (da_file_name, prefix);
1919 da_file_name[prefix_len - 1] = '/';
1920 da_file_name[prefix_len] = 0;
1922 else
1923 da_file_name[0] = 0;
1924 strcat (da_file_name, base_file_name);
1925 strcat (da_file_name, GCOV_DATA_SUFFIX);
1926 return da_file_name;
1929 /* Rebuild counts_hash already built the primary module. This hashtable
1930 was built with a module-id of zero. It needs to be rebuilt taking the
1931 correct primary module-id into account. */
1933 static int
1934 rebuild_counts_hash_entry (void **x, void *y)
1936 counts_entry_t *entry = (counts_entry_t *) *x;
1937 htab_t *new_counts_hash = (htab_t *) y;
1938 counts_entry_t **slot;
1939 entry->ident = GEN_FUNC_GLOBAL_ID (primary_module_id, entry->ident);
1940 slot = (counts_entry_t **) htab_find_slot (*new_counts_hash, entry, INSERT);
1941 *slot = entry;
1942 return 1;
1945 /* Rebuild counts_hash already built the primary module. This hashtable
1946 was built with a module-id of zero. It needs to be rebuilt taking the
1947 correct primary module-id into account. */
1949 static void
1950 rebuild_counts_hash (void)
1952 htab_t new_counts_hash =
1953 htab_create (10, htab_counts_entry_hash, htab_counts_entry_eq,
1954 htab_counts_entry_del);
1955 gcc_assert (primary_module_id);
1956 rebuilding_counts_hash = true;
1957 htab_traverse_noresize (counts_hash, rebuild_counts_hash_entry, &new_counts_hash);
1958 htab_delete (counts_hash);
1959 rebuilding_counts_hash = false;
1960 counts_hash = new_counts_hash;
1963 /* Add the module information record for the module with id
1964 MODULE_ID. IS_PRIMARY is true if the module is the primary module.
1965 INDEX is the index of the new record in the module info array. */
1967 void
1968 add_module_info (unsigned module_id, bool is_primary, int index)
1970 struct gcov_module_info *cur_info;
1971 module_infos = XRESIZEVEC (struct gcov_module_info *,
1972 module_infos, index + 1);
1973 module_infos[index] = XNEW (struct gcov_module_info);
1974 cur_info = module_infos[index];
1975 cur_info->ident = module_id;
1976 cur_info->is_exported = true;
1977 cur_info->num_quote_paths = 0;
1978 cur_info->num_bracket_paths = 0;
1979 cur_info->da_filename = NULL;
1980 cur_info->source_filename = NULL;
1981 if (is_primary)
1982 primary_module_id = module_id;
1985 /* Process the include paths needed for parsing the aux modules.
1986 The sub_pattern is in the form SUB_PATH:NEW_SUB_PATH. If it is
1987 defined, the SUB_PATH in ORIG_INC_PATH will be replaced with
1988 NEW_SUB_PATH. */
1990 static void
1991 process_include (char **orig_inc_path, char* old_sub, char *new_sub)
1993 char *inc_path, *orig_sub;
1995 if (strlen (*orig_inc_path) < strlen (old_sub))
1996 return;
1998 inc_path = (char*) xmalloc (strlen (*orig_inc_path) + strlen (new_sub)
1999 - strlen (old_sub) + 1);
2000 orig_sub = strstr (*orig_inc_path, old_sub);
2001 if (!orig_sub)
2003 inform (UNKNOWN_LOCATION, "subpath %s not found in path %s",
2004 old_sub, *orig_inc_path);
2005 free (inc_path);
2006 return;
2009 strncpy (inc_path, *orig_inc_path, orig_sub - *orig_inc_path);
2010 inc_path[orig_sub - *orig_inc_path] = '\0';
2011 strcat (inc_path, new_sub);
2012 strcat (inc_path, orig_sub + strlen (old_sub));
2014 free (*orig_inc_path);
2015 *orig_inc_path = inc_path;
2018 /* Process include paths for MOD_INFO according to option
2019 -fripa-inc-path-sub=OLD_SUB:NEW_SUB */
2021 static void
2022 process_include_paths_1 (struct gcov_module_info *mod_info,
2023 char* old_sub, char *new_sub)
2025 unsigned i, j;
2027 for (i = 0; i < mod_info->num_quote_paths; i++)
2028 process_include (&mod_info->string_array[i], old_sub, new_sub);
2030 for (i = 0, j = mod_info->num_quote_paths;
2031 i < mod_info->num_bracket_paths; i++, j++)
2032 process_include (&mod_info->string_array[j], old_sub, new_sub);
2034 for (i = 0, j = mod_info->num_quote_paths + mod_info->num_bracket_paths +
2035 mod_info->num_cpp_defines; i < mod_info->num_cpp_includes; i++, j++)
2036 process_include (&mod_info->string_array[j], old_sub, new_sub);
2040 /* Process include paths for MOD_INFO according to option
2041 -fripa-inc-path-sub=old_sub1:new_sub1[,old_sub2:new_sub2] */
2043 static void
2044 process_include_paths (struct gcov_module_info *mod_info)
2046 char *sub_pattern, *cur, *next, *new_sub;
2048 if (!lipo_inc_path_pattern)
2049 return;
2051 sub_pattern = xstrdup (lipo_inc_path_pattern);
2052 cur = sub_pattern;
2056 next = strchr (cur, ',');
2057 if (next)
2058 *next++ = '\0';
2059 new_sub = strchr (cur, ':');
2060 if (!new_sub)
2062 error ("Invalid path substibution pattern %s", sub_pattern);
2063 free (sub_pattern);
2064 return;
2066 *new_sub++ = '\0';
2067 process_include_paths_1 (mod_info, cur, new_sub);
2068 cur = next;
2069 } while (cur);
2070 free (sub_pattern);
2073 /* Set the prepreprocessing context (include search paths, -D/-U).
2074 PARSE_IN is the preprocessor reader, I is the index of the module,
2075 and VERBOSE is the verbose flag. */
2077 void
2078 set_lipo_c_parsing_context (struct cpp_reader *parse_in, int i, bool verbose)
2080 struct gcov_module_info *mod_info;
2081 if (!L_IPO_COMP_MODE)
2082 return;
2084 mod_info = module_infos[i];
2086 gcc_assert (flag_dyn_ipa);
2087 current_module_id = mod_info->ident;
2088 reset_funcdef_no ();
2090 if (current_module_id != primary_module_id)
2092 unsigned i, j;
2094 process_include_paths (mod_info);
2095 /* Setup include paths. */
2096 clear_include_chains ();
2097 for (i = 0; i < mod_info->num_quote_paths; i++)
2098 add_path (xstrdup (mod_info->string_array[i]),
2099 QUOTE, 0, 1);
2100 for (i = 0, j = mod_info->num_quote_paths;
2101 i < mod_info->num_bracket_paths; i++, j++)
2102 add_path (xstrdup (mod_info->string_array[j]),
2103 BRACKET, 0, 1);
2104 register_include_chains (parse_in, NULL, NULL, NULL,
2105 0, 0, verbose);
2107 /* Setup defines/undefs. */
2108 for (i = 0, j = mod_info->num_quote_paths + mod_info->num_bracket_paths;
2109 i < mod_info->num_cpp_defines; i++, j++)
2110 if (mod_info->string_array[j][0] == 'D')
2111 cpp_define (parse_in, mod_info->string_array[j] + 1);
2112 else
2113 cpp_undef (parse_in, mod_info->string_array[j] + 1);
2115 /* Setup -imacro/-include. */
2116 for (i = 0, j = mod_info->num_quote_paths + mod_info->num_bracket_paths +
2117 mod_info->num_cpp_defines; i < mod_info->num_cpp_includes;
2118 i++, j++)
2119 cpp_push_include (parse_in, mod_info->string_array[j]);
2123 /* Perform file-level initialization. Read in data file, generate name
2124 of graph file. */
2126 void
2127 coverage_init (const char *filename, const char* source_name)
2129 char* src_name_prefix = 0;
2130 int src_name_prefix_len = 0;
2131 int len = strlen (filename);
2133 has_asm_statement = false;
2134 da_file_name = get_da_file_name (filename);
2135 da_base_file_name = XNEWVEC (char, strlen (filename) + 1);
2136 strcpy (da_base_file_name, filename);
2138 /* Name of bbg file. */
2139 bbg_file_name = XNEWVEC (char, len + strlen (GCOV_NOTE_SUFFIX) + 1);
2140 strcpy (bbg_file_name, filename);
2141 strcat (bbg_file_name, GCOV_NOTE_SUFFIX);
2143 if (profile_data_prefix == 0 && !IS_ABSOLUTE_PATH (source_name))
2145 src_name_prefix = getpwd ();
2146 src_name_prefix_len = strlen (src_name_prefix) + 1;
2148 main_input_file_name = XNEWVEC (char, strlen (source_name) + 1
2149 + src_name_prefix_len);
2150 if (!src_name_prefix)
2151 strcpy (main_input_file_name, source_name);
2152 else
2154 strcpy (main_input_file_name, src_name_prefix);
2155 strcat (main_input_file_name, "/");
2156 strcat (main_input_file_name, source_name);
2159 if (flag_profile_use)
2161 unsigned mod_id = 0;
2162 if (L_IPO_STREAM_FE_COMP_MODE_AUX)
2163 mod_id = flag_ripa_aux_mod_id;
2164 read_counts_file (da_file_name, mod_id);
2167 /* Rebuild counts_hash and read the auxiliary GCDA files. */
2168 if (flag_profile_use && (L_IPO_COMP_MODE || L_IPO_STREAM_FE_COMP_MODE_PRIM))
2170 unsigned i;
2171 gcc_assert (flag_dyn_ipa || flag_ripa_stream);
2172 rebuild_counts_hash ();
2173 for (i = 1; i < num_in_fnames; i++)
2174 read_counts_file (get_da_file_name (module_infos[i]->da_filename),
2175 module_infos[i]->ident);
2178 /* Define variables which are referenced at runtime by libgcov. */
2179 if (profiling_enabled_p ())
2181 init_pmu_profiling ();
2182 gimple_init_instrumentation_sampling ();
2186 /* Return True if any type of profiling is enabled which requires linking
2187 in libgcov otherwise return False. */
2189 static bool
2190 profiling_enabled_p (void)
2192 return flag_pmu_profile_generate
2193 || profile_arc_flag
2194 || flag_profile_generate_sampling
2195 || flag_profile_reusedist;
2198 /* Construct variables for PMU profiling.
2199 1) __gcov_pmu_profile_filename,
2200 2) __gcov_pmu_profile_options,
2201 3) __gcov_pmu_top_n_address. */
2203 static void
2204 init_pmu_profiling (void)
2206 if (!pmu_profiling_initialized)
2208 unsigned top_n_addr = PARAM_VALUE (PARAM_PMU_PROFILE_N_ADDRESS);
2209 tree filename_ptr, options_ptr;
2211 /* Construct an initializer for __gcov_pmu_profile_filename. */
2212 gcov_pmu_filename_decl =
2213 build_decl (UNKNOWN_LOCATION, VAR_DECL,
2214 get_identifier ("__gcov_pmu_profile_filename"),
2215 get_const_string_type ());
2216 TREE_PUBLIC (gcov_pmu_filename_decl) = 1;
2217 DECL_ARTIFICIAL (gcov_pmu_filename_decl) = 1;
2218 make_decl_one_only (gcov_pmu_filename_decl,
2219 DECL_ASSEMBLER_NAME (gcov_pmu_filename_decl));
2220 TREE_STATIC (gcov_pmu_filename_decl) = 1;
2222 if (flag_pmu_profile_generate)
2224 const char *filename = get_da_file_name (pmu_profile_filename);
2225 int file_name_len;
2226 tree filename_string;
2227 file_name_len = strlen (filename);
2228 filename_string = build_string (file_name_len + 1, filename);
2229 TREE_TYPE (filename_string) = build_array_type
2230 (char_type_node, build_index_type
2231 (build_int_cst (NULL_TREE, file_name_len)));
2232 filename_ptr = build1 (ADDR_EXPR, get_const_string_type (),
2233 filename_string);
2235 else
2236 filename_ptr = null_pointer_node;
2238 DECL_INITIAL (gcov_pmu_filename_decl) = filename_ptr;
2239 assemble_variable (gcov_pmu_filename_decl, 0, 0, 0);
2241 /* Construct an initializer for __gcov_pmu_profile_options. */
2242 gcov_pmu_options_decl =
2243 build_decl (UNKNOWN_LOCATION, VAR_DECL,
2244 get_identifier ("__gcov_pmu_profile_options"),
2245 get_const_string_type ());
2246 TREE_PUBLIC (gcov_pmu_options_decl) = 1;
2247 DECL_ARTIFICIAL (gcov_pmu_options_decl) = 1;
2248 make_decl_one_only (gcov_pmu_options_decl,
2249 DECL_ASSEMBLER_NAME (gcov_pmu_options_decl));
2250 TREE_STATIC (gcov_pmu_options_decl) = 1;
2252 /* If the flag is false we generate a null pointer to indicate
2253 that we are not doing the pmu profiling. */
2254 if (flag_pmu_profile_generate)
2256 const char *pmu_options = flag_pmu_profile_generate;
2257 int pmu_options_len;
2258 tree pmu_options_string;
2260 pmu_options_len = strlen (pmu_options);
2261 pmu_options_string = build_string (pmu_options_len + 1, pmu_options);
2262 TREE_TYPE (pmu_options_string) = build_array_type
2263 (char_type_node, build_index_type (build_int_cst
2264 (NULL_TREE, pmu_options_len)));
2265 options_ptr = build1 (ADDR_EXPR, get_const_string_type (),
2266 pmu_options_string);
2268 else
2269 options_ptr = null_pointer_node;
2271 DECL_INITIAL (gcov_pmu_options_decl) = options_ptr;
2272 assemble_variable (gcov_pmu_options_decl, 0, 0, 0);
2274 /* Construct an initializer for __gcov_pmu_top_n_address. We
2275 don't need to guard this with the flag_pmu_profile generate
2276 because the value of __gcov_pmu_top_n_address is ignored when
2277 not doing profiling. */
2278 gcov_pmu_top_n_address_decl =
2279 build_decl (UNKNOWN_LOCATION, VAR_DECL,
2280 get_identifier ("__gcov_pmu_top_n_address"),
2281 get_gcov_unsigned_t ());
2282 TREE_PUBLIC (gcov_pmu_top_n_address_decl) = 1;
2283 DECL_ARTIFICIAL (gcov_pmu_top_n_address_decl) = 1;
2284 make_decl_one_only (gcov_pmu_top_n_address_decl,
2285 DECL_ASSEMBLER_NAME (gcov_pmu_top_n_address_decl));
2286 TREE_STATIC (gcov_pmu_top_n_address_decl) = 1;
2287 DECL_INITIAL (gcov_pmu_top_n_address_decl) =
2288 build_int_cstu (get_gcov_unsigned_t (), top_n_addr);
2289 assemble_variable (gcov_pmu_top_n_address_decl, 0, 0, 0);
2291 pmu_profiling_initialized = 1;
2294 /* Performs file-level cleanup. Close graph file, generate coverage
2295 variables and constructor. */
2297 void
2298 coverage_finish (void)
2300 create_coverage ();
2301 if (bbg_file_opened)
2303 int error = gcov_close ();
2305 if (error)
2306 unlink (bbg_file_name);
2307 if (!local_tick)
2308 /* Only remove the da file, if we cannot stamp it. If we can
2309 stamp it, libgcov will DTRT. */
2310 unlink (da_file_name);
2314 /* Add S to the end of the string-list, the head and tail of which are
2315 pointed-to by HEAD and TAIL, respectively. */
2317 static void
2318 str_list_append (struct str_list **head, struct str_list **tail, const char *s)
2320 struct str_list *e = XNEW (struct str_list);
2321 e->str = XNEWVEC (char, strlen (s) + 1);
2322 strcpy (e->str, s);
2323 e->next = NULL;
2324 if (*tail)
2325 (*tail)->next = e;
2326 else
2327 *head = e;
2328 *tail = e;
2331 extern bool is_kernel_build;
2333 #define KERNEL_BUILD_PREDEF_STRING "__KERNEL__"
2335 /* Copies the macro def or undef CPP_DEF and saves the copy
2336 in a list. IS_DEF is a flag indicating if CPP_DEF represents
2337 a -D or -U. */
2339 void
2340 coverage_note_define (const char *cpp_def, bool is_def)
2342 char *s = XNEWVEC (char, strlen (cpp_def) + 2);
2343 s[0] = is_def ? 'D' : 'U';
2344 strcpy (s + 1, cpp_def);
2345 str_list_append (&cpp_defines_head, &cpp_defines_tail, s);
2346 num_cpp_defines++;
2348 /* When -D__KERNEL__ is in the option list, we assume this is
2349 compilation for Linux Kernel. */
2350 if (!strcmp(cpp_def, KERNEL_BUILD_PREDEF_STRING))
2351 is_kernel_build = is_def;
2354 /* Copies the -imacro/-include FILENAME and saves the copy in a list. */
2356 void
2357 coverage_note_include (const char *filename)
2359 str_list_append (&cpp_includes_head, &cpp_includes_tail, filename);
2360 num_cpp_includes++;
2363 /* Mark this module as containing asm statements. */
2365 void
2366 coverage_has_asm_stmt (void)
2368 has_asm_statement = flag_ripa_disallow_asm_modules;
2371 /* Check the command line OPTIONS passed to
2372 -fpmu-profile-generate. Return 0 if the options are valid, non-zero
2373 otherwise. */
2376 check_pmu_profile_options (const char *options)
2378 if (strcmp(options, "load-latency") &&
2379 strcmp(options, "load-latency-verbose") &&
2380 strcmp(options, "branch-mispredict") &&
2381 strcmp(options, "branch-mispredict-verbose"))
2382 return 1;
2383 return 0;
2386 /* Write command line options to the .note section. */
2388 void
2389 write_opts_to_asm (void)
2391 size_t i;
2392 cpp_dir *quote_paths, *bracket_paths, *pdir;
2393 struct str_list *pdef, *pinc;
2394 int num_quote_paths = 0;
2395 int num_bracket_paths = 0;
2397 get_include_chains (&quote_paths, &bracket_paths);
2399 /* Write quote_paths to ASM section. */
2400 switch_to_section (get_section (".gnu.switches.text.quote_paths",
2401 SECTION_DEBUG, NULL));
2402 for (pdir = quote_paths; pdir; pdir = pdir->next)
2404 if (pdir == bracket_paths)
2405 break;
2406 num_quote_paths++;
2408 dw2_asm_output_nstring (in_fnames[0], (size_t)-1, NULL);
2409 dw2_asm_output_data_uleb128 (num_quote_paths, NULL);
2410 for (pdir = quote_paths; pdir; pdir = pdir->next)
2412 if (pdir == bracket_paths)
2413 break;
2414 dw2_asm_output_nstring (pdir->name, (size_t)-1, NULL);
2417 /* Write bracket_paths to ASM section. */
2418 switch_to_section (get_section (".gnu.switches.text.bracket_paths",
2419 SECTION_DEBUG, NULL));
2420 for (pdir = bracket_paths; pdir; pdir = pdir->next)
2421 num_bracket_paths++;
2422 dw2_asm_output_nstring (in_fnames[0], (size_t)-1, NULL);
2423 dw2_asm_output_data_uleb128 (num_bracket_paths, NULL);
2424 for (pdir = bracket_paths; pdir; pdir = pdir->next)
2425 dw2_asm_output_nstring (pdir->name, (size_t)-1, NULL);
2427 /* Write cpp_defines to ASM section. */
2428 switch_to_section (get_section (".gnu.switches.text.cpp_defines",
2429 SECTION_DEBUG, NULL));
2430 dw2_asm_output_nstring (in_fnames[0], (size_t)-1, NULL);
2431 dw2_asm_output_data_uleb128 (num_cpp_defines, NULL);
2432 for (pdef = cpp_defines_head; pdef; pdef = pdef->next)
2433 dw2_asm_output_nstring (pdef->str, (size_t)-1, NULL);
2435 /* Write cpp_includes to ASM section. */
2436 switch_to_section (get_section (".gnu.switches.text.cpp_includes",
2437 SECTION_DEBUG, NULL));
2438 dw2_asm_output_nstring (in_fnames[0], (size_t)-1, NULL);
2439 dw2_asm_output_data_uleb128 (num_cpp_includes, NULL);
2440 for (pinc = cpp_includes_head; pinc; pinc = pinc->next)
2441 dw2_asm_output_nstring (pinc->str, (size_t)-1, NULL);
2443 /* Write cl_args to ASM section. */
2444 switch_to_section (get_section (".gnu.switches.text.cl_args",
2445 SECTION_DEBUG, NULL));
2446 dw2_asm_output_nstring (in_fnames[0], (size_t)-1, NULL);
2447 dw2_asm_output_data_uleb128 (num_lipo_cl_args, NULL);
2448 for (i = 0; i < num_lipo_cl_args; i++)
2449 dw2_asm_output_nstring (lipo_cl_args[i], (size_t)-1, NULL);
2451 #include "gt-coverage.h"