* cp-tree.def (FUNCTION_NAME): New tree node.
[official-gcc.git] / gcc / profile.c
blob1e8bdf2f9d09edab1788b5164d797f37c5bddbf0
1 /* Calculate branch probabilities, and basic block execution counts.
2 Copyright (C) 1990, 91-94, 96-98, 1999 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.
7 This file is part of GNU CC.
9 GNU CC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
14 GNU CC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU CC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
24 /* ??? Really should not put insns inside of LIBCALL sequences, when putting
25 insns after a call, should look for the insn setting the retval, and
26 insert the insns after that one. */
28 /* ??? Register allocation should use basic block execution counts to
29 give preference to the most commonly executed blocks. */
31 /* ??? The .da files are not safe. Changing the program after creating .da
32 files or using different options when compiling with -fbranch-probabilities
33 can result the arc data not matching the program. Maybe add instrumented
34 arc count to .bbg file? Maybe check whether PFG matches the .bbg file? */
36 /* ??? Should calculate branch probabilities before instrumenting code, since
37 then we can use arc counts to help decide which arcs to instrument. */
39 /* ??? Rearrange code so that the most frequently executed arcs become from
40 one block to the next block (i.e. a fall through), move seldom executed
41 code outside of loops even at the expense of adding a few branches to
42 achieve this, see Dain Sample's UC Berkeley thesis. */
44 #include "config.h"
45 #include "system.h"
46 #include "rtl.h"
47 #include "tree.h"
48 #include "flags.h"
49 #include "insn-flags.h"
50 #include "insn-config.h"
51 #include "output.h"
52 #include "regs.h"
53 #include "function.h"
54 #include "output.h"
55 #include "gcov-io.h"
56 #include "toplev.h"
57 #include "ggc.h"
59 /* One of these is dynamically created whenever we identify an arc in the
60 function. */
62 struct adj_list
64 int source;
65 int target;
66 int arc_count;
67 unsigned int count_valid : 1;
68 unsigned int on_tree : 1;
69 unsigned int fake : 1;
70 unsigned int fall_through : 1;
71 rtx branch_insn;
72 struct adj_list *pred_next;
73 struct adj_list *succ_next;
76 #define ARC_TARGET(ARCPTR) (ARCPTR->target)
77 #define ARC_SOURCE(ARCPTR) (ARCPTR->source)
78 #define ARC_COUNT(ARCPTR) (ARCPTR->arc_count)
80 /* Count the number of basic blocks, and create an array of these structures,
81 one for each bb in the function. */
83 struct bb_info
85 struct adj_list *succ;
86 struct adj_list *pred;
87 int succ_count;
88 int pred_count;
89 int exec_count;
90 unsigned int count_valid : 1;
91 unsigned int on_tree : 1;
92 rtx first_insn;
95 /* Indexed by label number, gives the basic block number containing that
96 label. */
98 static int *label_to_bb;
100 /* Number of valid entries in the label_to_bb array. */
102 static int label_to_bb_size;
104 /* Indexed by block index, holds the basic block graph. */
106 static struct bb_info *bb_graph;
108 /* Name and file pointer of the output file for the basic block graph. */
110 static char *bbg_file_name;
111 static FILE *bbg_file;
113 /* Name and file pointer of the input file for the arc count data. */
115 static char *da_file_name;
116 static FILE *da_file;
118 /* Pointer of the output file for the basic block/line number map. */
119 static FILE *bb_file;
121 /* Last source file name written to bb_file. */
123 static char *last_bb_file_name;
125 /* Indicates whether the next line number note should be output to
126 bb_file or not. Used to eliminate a redundant note after an
127 expanded inline function call. */
129 static int ignore_next_note;
131 /* Used by final, for allocating the proper amount of storage for the
132 instrumented arc execution counts. */
134 int count_instrumented_arcs;
136 /* Number of executions for the return label. */
138 int return_label_execution_count;
140 /* Collect statistics on the performance of this pass for the entire source
141 file. */
143 static int total_num_blocks;
144 static int total_num_arcs;
145 static int total_num_arcs_instrumented;
146 static int total_num_blocks_created;
147 static int total_num_passes;
148 static int total_num_times_called;
149 static int total_hist_br_prob[20];
150 static int total_num_never_executed;
151 static int total_num_branches;
153 /* Forward declarations. */
154 static void init_arc PROTO((struct adj_list *, int, int, rtx));
155 static void find_spanning_tree PROTO((int));
156 static void expand_spanning_tree PROTO((int));
157 static void fill_spanning_tree PROTO((int));
158 static void init_arc_profiler PROTO((void));
159 static void output_arc_profiler PROTO((int, rtx));
160 static void instrument_arcs PROTO((rtx, int, FILE *));
161 static void output_gcov_string PROTO((const char *, long));
162 static int tablejump_entry_p PROTO((rtx, rtx));
163 static void compute_branch_probabilities PROTO((int, FILE *));
165 #ifndef LONG_TYPE_SIZE
166 #define LONG_TYPE_SIZE BITS_PER_WORD
167 #endif
169 /* If non-zero, we need to output a constructor to set up the
170 per-object-file data. */
171 static int need_func_profiler = 0;
174 /* Add arc instrumentation code to the entire insn chain.
176 F is the first insn of the chain.
177 NUM_BLOCKS is the number of basic blocks found in F.
178 DUMP_FILE, if nonzero, is an rtl dump file we can write to. */
180 static void
181 instrument_arcs (f, num_blocks, dump_file)
182 rtx f;
183 int num_blocks;
184 FILE *dump_file;
186 register int i;
187 register struct adj_list *arcptr, *backptr;
188 int num_arcs = 0;
189 int num_instr_arcs = 0;
190 rtx insn;
192 /* Instrument the program start. */
193 /* Handle block 0 specially, since it will always be instrumented,
194 but it doesn't have a valid first_insn or branch_insn. We must
195 put the instructions before the NOTE_INSN_FUNCTION_BEG note, so
196 that they don't clobber any of the parameters of the current
197 function. */
198 for (insn = f; insn; insn = NEXT_INSN (insn))
199 if (GET_CODE (insn) == NOTE
200 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_BEG)
201 break;
202 insn = PREV_INSN (insn);
203 need_func_profiler = 1;
204 output_arc_profiler (total_num_arcs_instrumented + num_instr_arcs++, insn);
206 for (i = 1; i < num_blocks; i++)
207 for (arcptr = bb_graph[i].succ; arcptr; arcptr = arcptr->succ_next)
208 if (! arcptr->on_tree)
210 if (dump_file)
211 fprintf (dump_file, "Arc %d to %d instrumented\n", i,
212 ARC_TARGET (arcptr));
214 /* Check to see if this arc is the only exit from its source block,
215 or the only entrance to its target block. In either case,
216 we don't need to create a new block to instrument the arc. */
218 if (bb_graph[i].succ == arcptr && arcptr->succ_next == 0)
220 /* Instrument the source block. */
221 output_arc_profiler (total_num_arcs_instrumented
222 + num_instr_arcs++,
223 PREV_INSN (bb_graph[i].first_insn));
225 else if (arcptr == bb_graph[ARC_TARGET (arcptr)].pred
226 && arcptr->pred_next == 0)
228 /* Instrument the target block. */
229 output_arc_profiler (total_num_arcs_instrumented
230 + num_instr_arcs++,
231 PREV_INSN (bb_graph[ARC_TARGET (arcptr)].first_insn));
233 else if (arcptr->fall_through)
235 /* This is a fall-through; put the instrumentation code after
236 the branch that ends this block. */
238 for (backptr = bb_graph[i].succ; backptr;
239 backptr = backptr->succ_next)
240 if (backptr != arcptr)
241 break;
243 output_arc_profiler (total_num_arcs_instrumented
244 + num_instr_arcs++,
245 backptr->branch_insn);
247 else
249 /* Must emit a new basic block to hold the arc counting code. */
250 enum rtx_code code = GET_CODE (PATTERN (arcptr->branch_insn));
252 if (code == SET)
254 /* Create the new basic block right after the branch.
255 Invert the branch so that it jumps past the end of the new
256 block. The new block will consist of the instrumentation
257 code, and a jump to the target of this arc. */
258 int this_is_simplejump = simplejump_p (arcptr->branch_insn);
259 rtx new_label = gen_label_rtx ();
260 rtx old_label, set_src;
261 rtx after = arcptr->branch_insn;
263 /* Simplejumps can't reach here. */
264 if (this_is_simplejump)
265 abort ();
267 /* We can't use JUMP_LABEL, because it won't be set if we
268 are compiling without optimization. */
270 set_src = SET_SRC (single_set (arcptr->branch_insn));
271 if (GET_CODE (set_src) == LABEL_REF)
272 old_label = set_src;
273 else if (GET_CODE (set_src) != IF_THEN_ELSE)
274 abort ();
275 else if (XEXP (set_src, 1) == pc_rtx)
276 old_label = XEXP (XEXP (set_src, 2), 0);
277 else
278 old_label = XEXP (XEXP (set_src, 1), 0);
280 /* Set the JUMP_LABEL so that redirect_jump will work. */
281 JUMP_LABEL (arcptr->branch_insn) = old_label;
283 /* Add a use for OLD_LABEL that will be needed when we emit
284 the JUMP_INSN below. If we don't do this here,
285 `invert_jump' might delete it for us. We must add two
286 when not optimizing, because the NUSES is zero now,
287 but must be at least two to prevent the label from being
288 deleted. */
289 LABEL_NUSES (old_label) += 2;
291 /* Emit the insns for the new block in reverse order,
292 since that is most convenient. */
294 if (this_is_simplejump)
296 after = NEXT_INSN (arcptr->branch_insn);
297 if (! redirect_jump (arcptr->branch_insn, new_label))
298 /* Don't know what to do if this branch won't
299 redirect. */
300 abort ();
302 else
304 if (! invert_jump (arcptr->branch_insn, new_label))
305 /* Don't know what to do if this branch won't invert. */
306 abort ();
308 emit_label_after (new_label, after);
309 LABEL_NUSES (new_label)++;
311 emit_barrier_after (after);
312 emit_jump_insn_after (gen_jump (old_label), after);
313 JUMP_LABEL (NEXT_INSN (after)) = old_label;
315 /* Instrument the source arc. */
316 output_arc_profiler (total_num_arcs_instrumented
317 + num_instr_arcs++,
318 after);
319 if (this_is_simplejump)
321 emit_label_after (new_label, after);
322 LABEL_NUSES (new_label)++;
325 else if (code == ADDR_VEC || code == ADDR_DIFF_VEC)
327 /* A table jump. Create a new basic block immediately
328 after the table, by emitting a barrier, a label, a
329 counting note, and a jump to the old label. Put the
330 new label in the table. */
332 rtx new_label = gen_label_rtx ();
333 rtx old_lref, new_lref;
334 int index;
336 /* Must determine the old_label reference, do this
337 by counting the arcs after this one, which will
338 give the index of our label in the table. */
340 index = 0;
341 for (backptr = arcptr->succ_next; backptr;
342 backptr = backptr->succ_next)
343 index++;
345 old_lref = XVECEXP (PATTERN (arcptr->branch_insn),
346 (code == ADDR_DIFF_VEC), index);
348 /* Emit the insns for the new block in reverse order,
349 since that is most convenient. */
350 emit_jump_insn_after (gen_jump (XEXP (old_lref, 0)),
351 arcptr->branch_insn);
352 JUMP_LABEL (NEXT_INSN (arcptr->branch_insn))
353 = XEXP (old_lref, 0);
355 /* Instrument the source arc. */
356 output_arc_profiler (total_num_arcs_instrumented
357 + num_instr_arcs++,
358 arcptr->branch_insn);
360 emit_label_after (new_label, arcptr->branch_insn);
361 LABEL_NUSES (NEXT_INSN (arcptr->branch_insn))++;
362 emit_barrier_after (arcptr->branch_insn);
364 /* Fix up the table jump. */
365 new_lref = gen_rtx_LABEL_REF (Pmode, new_label);
366 XVECEXP (PATTERN (arcptr->branch_insn),
367 (code == ADDR_DIFF_VEC), index) = new_lref;
369 else
370 abort ();
372 num_arcs += 1;
373 if (dump_file)
374 fprintf (dump_file,
375 "Arc %d to %d needed new basic block\n", i,
376 ARC_TARGET (arcptr));
380 total_num_arcs_instrumented += num_instr_arcs;
381 count_instrumented_arcs = total_num_arcs_instrumented;
383 total_num_blocks_created += num_arcs;
384 if (dump_file)
386 fprintf (dump_file, "%d arcs instrumented\n", num_instr_arcs);
387 fprintf (dump_file, "%d extra basic blocks created\n", num_arcs);
391 /* Output STRING to bb_file, surrounded by DELIMITER. */
393 static void
394 output_gcov_string (string, delimiter)
395 const char *string;
396 long delimiter;
398 long temp;
400 /* Write a delimiter to indicate that a file name follows. */
401 __write_long (delimiter, bb_file, 4);
403 /* Write the string. */
404 temp = strlen (string) + 1;
405 fwrite (string, temp, 1, bb_file);
407 /* Append a few zeros, to align the output to a 4 byte boundary. */
408 temp = temp & 0x3;
409 if (temp)
411 char c[4];
413 c[0] = c[1] = c[2] = c[3] = 0;
414 fwrite (c, sizeof (char), 4 - temp, bb_file);
417 /* Store another delimiter in the .bb file, just to make it easy to find the
418 end of the file name. */
419 __write_long (delimiter, bb_file, 4);
422 /* Return TRUE if this insn must be a tablejump entry insn. This works for
423 the MIPS port, but may give false negatives for some targets. */
425 static int
426 tablejump_entry_p (insn, label)
427 rtx insn, label;
429 rtx next = next_active_insn (insn);
430 enum rtx_code code = GET_CODE (PATTERN (next));
432 if (code != ADDR_DIFF_VEC && code != ADDR_VEC)
433 return 0;
435 if (PREV_INSN (next) == XEXP (label, 0))
436 return 1;
438 return 0;
441 /* Compute the branch probabilities for the various branches.
442 Annotate them accordingly. */
444 static void
445 compute_branch_probabilities (num_blocks, dump_file)
446 int num_blocks;
447 FILE *dump_file;
449 int i;
450 int bad_counts = 0;
451 int num_arcs;
452 int changes;
453 int passes;
454 int prob;
455 int total;
456 int num_branches;
457 int num_never_executed;
458 int hist_br_prob[20];
459 struct adj_list *arcptr;
461 /* For each arc not on the spanning tree, set its execution count from
462 the .da file. */
464 /* The first count in the .da file is the number of times that the function
465 was entered. This is the exec_count for block zero. */
467 num_arcs = 0;
468 for (i = 0; i < num_blocks; i++)
469 for (arcptr = bb_graph[i].succ; arcptr; arcptr = arcptr->succ_next)
470 if (! arcptr->on_tree)
472 num_arcs++;
473 if (da_file)
475 long value;
476 __read_long (&value, da_file, 8);
477 ARC_COUNT (arcptr) = value;
479 else
480 ARC_COUNT (arcptr) = 0;
481 arcptr->count_valid = 1;
482 bb_graph[i].succ_count--;
483 bb_graph[ARC_TARGET (arcptr)].pred_count--;
486 if (dump_file)
487 fprintf (dump_file, "%d arc counts read\n", num_arcs);
489 /* For every block in the file,
490 - if every exit/entrance arc has a known count, then set the block count
491 - if the block count is known, and every exit/entrance arc but one has
492 a known execution count, then set the count of the remaining arc
494 As arc counts are set, decrement the succ/pred count, but don't delete
495 the arc, that way we can easily tell when all arcs are known, or only
496 one arc is unknown. */
498 /* The order that the basic blocks are iterated through is important.
499 Since the code that finds spanning trees starts with block 0, low numbered
500 arcs are put on the spanning tree in preference to high numbered arcs.
501 Hence, most instrumented arcs are at the end. Graph solving works much
502 faster if we propagate numbers from the end to the start.
504 This takes an average of slightly more than 3 passes. */
506 changes = 1;
507 passes = 0;
508 while (changes)
510 passes++;
511 changes = 0;
513 for (i = num_blocks - 1; i >= 0; i--)
515 struct bb_info *binfo = &bb_graph[i];
516 if (! binfo->count_valid)
518 if (binfo->succ_count == 0)
520 total = 0;
521 for (arcptr = binfo->succ; arcptr;
522 arcptr = arcptr->succ_next)
523 total += ARC_COUNT (arcptr);
524 binfo->exec_count = total;
525 binfo->count_valid = 1;
526 changes = 1;
528 else if (binfo->pred_count == 0)
530 total = 0;
531 for (arcptr = binfo->pred; arcptr;
532 arcptr = arcptr->pred_next)
533 total += ARC_COUNT (arcptr);
534 binfo->exec_count = total;
535 binfo->count_valid = 1;
536 changes = 1;
539 if (binfo->count_valid)
541 if (binfo->succ_count == 1)
543 total = 0;
544 /* One of the counts will be invalid, but it is zero,
545 so adding it in also doesn't hurt. */
546 for (arcptr = binfo->succ; arcptr;
547 arcptr = arcptr->succ_next)
548 total += ARC_COUNT (arcptr);
549 /* Calculate count for remaining arc by conservation. */
550 total = binfo->exec_count - total;
551 /* Search for the invalid arc, and set its count. */
552 for (arcptr = binfo->succ; arcptr;
553 arcptr = arcptr->succ_next)
554 if (! arcptr->count_valid)
555 break;
556 if (! arcptr)
557 abort ();
558 arcptr->count_valid = 1;
559 ARC_COUNT (arcptr) = total;
560 binfo->succ_count--;
562 bb_graph[ARC_TARGET (arcptr)].pred_count--;
563 changes = 1;
565 if (binfo->pred_count == 1)
567 total = 0;
568 /* One of the counts will be invalid, but it is zero,
569 so adding it in also doesn't hurt. */
570 for (arcptr = binfo->pred; arcptr;
571 arcptr = arcptr->pred_next)
572 total += ARC_COUNT (arcptr);
573 /* Calculate count for remaining arc by conservation. */
574 total = binfo->exec_count - total;
575 /* Search for the invalid arc, and set its count. */
576 for (arcptr = binfo->pred; arcptr;
577 arcptr = arcptr->pred_next)
578 if (! arcptr->count_valid)
579 break;
580 if (! arcptr)
581 abort ();
582 arcptr->count_valid = 1;
583 ARC_COUNT (arcptr) = total;
584 binfo->pred_count--;
586 bb_graph[ARC_SOURCE (arcptr)].succ_count--;
587 changes = 1;
593 total_num_passes += passes;
594 if (dump_file)
595 fprintf (dump_file, "Graph solving took %d passes.\n\n", passes);
597 /* If the graph has been correctly solved, every block will have a
598 succ and pred count of zero. */
599 for (i = 0; i < num_blocks; i++)
601 struct bb_info *binfo = &bb_graph[i];
602 if (binfo->succ_count || binfo->pred_count)
603 abort ();
606 /* For every arc, calculate its branch probability and add a reg_note
607 to the branch insn to indicate this. */
609 for (i = 0; i < 20; i++)
610 hist_br_prob[i] = 0;
611 num_never_executed = 0;
612 num_branches = 0;
614 for (i = 0; i < num_blocks; i++)
616 struct bb_info *binfo = &bb_graph[i];
618 total = binfo->exec_count;
619 for (arcptr = binfo->succ; arcptr; arcptr = arcptr->succ_next)
621 if (arcptr->branch_insn)
623 /* This calculates the branch probability as an integer between
624 0 and REG_BR_PROB_BASE, properly rounded to the nearest
625 integer. Perform the arithmetic in double to avoid
626 overflowing the range of ints. */
628 if (total == 0)
629 prob = -1;
630 else
632 rtx pat = PATTERN (arcptr->branch_insn);
634 prob = (((double)ARC_COUNT (arcptr) * REG_BR_PROB_BASE)
635 + (total >> 1)) / total;
636 if (prob < 0 || prob > REG_BR_PROB_BASE)
638 if (dump_file)
639 fprintf (dump_file, "bad count: prob for %d-%d thought to be %d (forcibly normalized)\n",
640 ARC_SOURCE (arcptr), ARC_TARGET (arcptr),
641 prob);
643 bad_counts = 1;
644 prob = REG_BR_PROB_BASE / 2;
647 /* Match up probability with JUMP pattern. */
649 if (GET_CODE (pat) == SET
650 && GET_CODE (SET_SRC (pat)) == IF_THEN_ELSE)
652 if (ARC_TARGET (arcptr) == ARC_SOURCE (arcptr) + 1)
654 /* A fall through arc should never have a
655 branch insn. */
656 abort ();
658 else
660 /* This is the arc for the taken branch. */
661 if (GET_CODE (XEXP (SET_SRC (pat), 2)) != PC)
662 prob = REG_BR_PROB_BASE - prob;
667 if (prob == -1)
668 num_never_executed++;
669 else
671 int index = prob * 20 / REG_BR_PROB_BASE;
672 if (index == 20)
673 index = 19;
674 hist_br_prob[index]++;
676 num_branches++;
678 REG_NOTES (arcptr->branch_insn)
679 = gen_rtx_EXPR_LIST (REG_BR_PROB, GEN_INT (prob),
680 REG_NOTES (arcptr->branch_insn));
684 /* Add a REG_EXEC_COUNT note to the first instruction of this block. */
685 if (! binfo->first_insn
686 || GET_RTX_CLASS (GET_CODE (binfo->first_insn)) != 'i')
688 /* Block 0 is a fake block representing function entry, and does
689 not have a real first insn. The second last block might not
690 begin with a real insn. */
691 if (i == num_blocks - 1)
692 return_label_execution_count = total;
693 else if (i != 0 && i != num_blocks - 2)
694 abort ();
696 else
698 REG_NOTES (binfo->first_insn)
699 = gen_rtx_EXPR_LIST (REG_EXEC_COUNT, GEN_INT (total),
700 REG_NOTES (binfo->first_insn));
701 if (i == num_blocks - 1)
702 return_label_execution_count = total;
706 /* This should never happen. */
707 if (bad_counts)
708 warning ("Arc profiling: some arc counts were bad.");
710 if (dump_file)
712 fprintf (dump_file, "%d branches\n", num_branches);
713 fprintf (dump_file, "%d branches never executed\n",
714 num_never_executed);
715 if (num_branches)
716 for (i = 0; i < 10; i++)
717 fprintf (dump_file, "%d%% branches in range %d-%d%%\n",
718 (hist_br_prob[i]+hist_br_prob[19-i])*100/num_branches,
719 5*i, 5*i+5);
721 total_num_branches += num_branches;
722 total_num_never_executed += num_never_executed;
723 for (i = 0; i < 20; i++)
724 total_hist_br_prob[i] += hist_br_prob[i];
728 /* Instrument and/or analyze program behavior based on program flow graph.
729 In either case, this function builds a flow graph for the function being
730 compiled. The flow graph is stored in BB_GRAPH.
732 When FLAG_PROFILE_ARCS is nonzero, this function instruments the arcs in
733 the flow graph that are needed to reconstruct the dynamic behavior of the
734 flow graph.
736 When FLAG_BRANCH_PROBABILITIES is nonzero, this function reads auxiliary
737 information from a data file containing arc count information from previous
738 executions of the function being compiled. In this case, the flow graph is
739 annotated with actual execution counts, which are later propagated into the
740 rtl for optimization purposes.
742 Main entry point of this file. */
744 void
745 branch_prob (f, dump_file)
746 rtx f;
747 FILE *dump_file;
749 int i, num_blocks;
750 struct adj_list *arcptr;
751 int num_arcs;
753 /* start of a function. */
754 if (flag_test_coverage)
755 output_gcov_string (current_function_name, (long) -2);
757 /* Execute this only if doing arc profiling or branch probabilities. */
758 if (! profile_arc_flag && ! flag_branch_probabilities
759 && ! flag_test_coverage)
760 abort ();
762 total_num_times_called++;
764 /* Create an array label_to_bb of ints of size max_label_num. */
765 label_to_bb_size = max_label_num ();
766 label_to_bb = (int *) oballoc (label_to_bb_size * sizeof (int));
767 bzero ((char *) label_to_bb, label_to_bb_size * sizeof (int));
769 /* Scan the insns in the function, count the number of basic blocks
770 present. When a code label is passed, set label_to_bb[label] = bb
771 number. */
773 /* The first block found will be block 1, so that function entry can be
774 block 0. */
777 register RTX_CODE prev_code = JUMP_INSN;
778 register RTX_CODE code;
779 register rtx insn;
780 register int i;
781 int block_separator_emitted = 0;
783 ignore_next_note = 0;
785 for (insn = NEXT_INSN (f), i = 0; insn; insn = NEXT_INSN (insn))
787 code = GET_CODE (insn);
789 if (code == BARRIER)
791 else if (code == CODE_LABEL)
792 /* This label is part of the next block, but we can't increment
793 block number yet since there might be multiple labels. */
794 label_to_bb[CODE_LABEL_NUMBER (insn)] = i + 1;
795 /* We make NOTE_INSN_SETJMP notes into a block of their own, so that
796 they can be the target of the fake arc for the setjmp call.
797 This avoids creating cycles of fake arcs, which would happen if
798 the block after the setjmp call contained a call insn. */
799 else if ((prev_code == JUMP_INSN || prev_code == CALL_INSN
800 || prev_code == CODE_LABEL || prev_code == BARRIER)
801 && (GET_RTX_CLASS (code) == 'i'
802 || (code == NOTE
803 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)))
805 i += 1;
807 /* Emit the block separator if it hasn't already been emitted. */
808 if (flag_test_coverage && ! block_separator_emitted)
810 /* Output a zero to the .bb file to indicate that a new
811 block list is starting. */
812 __write_long (0, bb_file, 4);
814 block_separator_emitted = 0;
816 /* If flag_test_coverage is true, then we must add an entry to the
817 .bb file for every note. */
818 else if (code == NOTE && flag_test_coverage)
820 /* Must ignore the line number notes that immediately follow the
821 end of an inline function to avoid counting it twice. There
822 is a note before the call, and one after the call. */
823 if (NOTE_LINE_NUMBER (insn) == NOTE_REPEATED_LINE_NUMBER)
824 ignore_next_note = 1;
825 else if (NOTE_LINE_NUMBER (insn) > 0)
827 if (ignore_next_note)
828 ignore_next_note = 0;
829 else
831 /* Emit a block separator here to ensure that a NOTE
832 immediately following a JUMP_INSN or CALL_INSN will end
833 up in the right basic block list. */
834 if ((prev_code == JUMP_INSN || prev_code == CALL_INSN
835 || prev_code == CODE_LABEL || prev_code == BARRIER)
836 && ! block_separator_emitted)
838 /* Output a zero to the .bb file to indicate that
839 a new block list is starting. */
840 __write_long (0, bb_file, 4);
842 block_separator_emitted = 1;
845 /* If this is a new source file, then output the file's
846 name to the .bb file. */
847 if (! last_bb_file_name
848 || strcmp (NOTE_SOURCE_FILE (insn),
849 last_bb_file_name))
851 if (last_bb_file_name)
852 free (last_bb_file_name);
853 last_bb_file_name = xstrdup (NOTE_SOURCE_FILE (insn));
854 output_gcov_string (NOTE_SOURCE_FILE (insn), (long)-1);
857 /* Output the line number to the .bb file. Must be done
858 after the output_bb_profile_data() call, and after the
859 file name is written, to ensure that it is correctly
860 handled by gcov. */
861 __write_long (NOTE_LINE_NUMBER (insn), bb_file, 4);
866 if (code != NOTE)
867 prev_code = code;
868 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
869 prev_code = CALL_INSN;
872 /* Allocate last `normal' entry for bb_graph. */
874 /* The last insn was a jump, call, or label. In that case we have
875 a block at the end of the function with no insns. */
876 if (prev_code == JUMP_INSN || prev_code == CALL_INSN
877 || prev_code == CODE_LABEL || prev_code == BARRIER)
879 i++;
881 /* Emit the block separator if it hasn't already been emitted. */
882 if (flag_test_coverage && ! block_separator_emitted)
884 /* Output a zero to the .bb file to indicate that a new
885 block list is starting. */
886 __write_long (0, bb_file, 4);
890 /* Create another block to stand for EXIT, and make all return insns, and
891 the last basic block point here. Add one more to account for block
892 zero. */
893 num_blocks = i + 2;
896 total_num_blocks += num_blocks;
897 if (dump_file)
898 fprintf (dump_file, "%d basic blocks\n", num_blocks);
900 /* If we are only doing test coverage here, then return now. */
901 if (! profile_arc_flag && ! flag_branch_probabilities)
902 return;
904 /* Create and initialize the arrays that will hold bb_graph
905 and execution count info. */
907 bb_graph = (struct bb_info *) xcalloc (num_blocks,
908 sizeof (struct bb_info));
911 /* Scan the insns again:
912 - at the entry to each basic block, increment the predecessor count
913 (and successor of previous block) if it is a fall through entry,
914 create adj_list entries for this and the previous block
915 - at each jump insn, increment predecessor/successor counts for
916 target/source basic blocks, add this insn to pred/succ lists.
918 This also cannot be broken out as a separate subroutine
919 because it uses `alloca'. */
921 register RTX_CODE prev_code = JUMP_INSN;
922 register RTX_CODE code;
923 register rtx insn;
924 register int i;
925 int fall_through = 0;
926 struct adj_list *arcptr;
927 int dest = 0;
929 /* Block 0 always falls through to block 1. */
930 num_arcs = 0;
931 arcptr = (struct adj_list *) alloca (sizeof (struct adj_list));
932 init_arc (arcptr, 0, 1, 0);
933 arcptr->fall_through = 1;
934 num_arcs++;
936 /* Add a fake fall through arc from the last block to block 0, to make the
937 graph complete. */
938 arcptr = (struct adj_list *) alloca (sizeof (struct adj_list));
939 init_arc (arcptr, num_blocks - 1, 0, 0);
940 arcptr->fake = 1;
941 num_arcs++;
943 /* Exit must be one node of the graph, and all exits from the function
944 must point there. When see a return branch, must point the arc to the
945 exit node. */
947 /* Must start scan with second insn in function as above. */
948 for (insn = NEXT_INSN (f), i = 0; insn; insn = NEXT_INSN (insn))
950 code = GET_CODE (insn);
952 if (code == BARRIER)
953 fall_through = 0;
954 else if (code == CODE_LABEL)
956 /* We make NOTE_INSN_SETJMP notes into a block of their own, so that
957 they can be the target of the fake arc for the setjmp call.
958 This avoids creating cycles of fake arcs, which would happen if
959 the block after the setjmp call ended with a call. */
960 else if ((prev_code == JUMP_INSN || prev_code == CALL_INSN
961 || prev_code == CODE_LABEL || prev_code == BARRIER)
962 && (GET_RTX_CLASS (code) == 'i'
963 || (code == NOTE
964 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)))
966 /* This is the first insn of the block. */
967 i += 1;
968 if (fall_through)
970 arcptr = (struct adj_list *) alloca (sizeof (struct adj_list));
971 init_arc (arcptr, i - 1, i, 0);
972 arcptr->fall_through = 1;
974 num_arcs++;
976 fall_through = 1;
977 bb_graph[i].first_insn = insn;
979 else if (code == NOTE)
982 if (code == CALL_INSN)
984 /* In the normal case, the call returns, and this is just like
985 a branch fall through. */
986 fall_through = 1;
988 /* Setjmp may return more times than called, so to make the graph
989 solvable, add a fake arc from the function entrance to the
990 next block.
992 All other functions may return fewer times than called (if
993 a descendent call longjmp or exit), so to make the graph
994 solvable, add a fake arc to the function exit from the
995 current block.
997 Distinguish the cases by checking for a SETJUMP note.
998 A call_insn can be the last ins of a function, so must check
999 to see if next insn actually exists. */
1000 arcptr = (struct adj_list *) alloca (sizeof (struct adj_list));
1001 if (NEXT_INSN (insn)
1002 && GET_CODE (NEXT_INSN (insn)) == NOTE
1003 && NOTE_LINE_NUMBER (NEXT_INSN (insn)) == NOTE_INSN_SETJMP)
1004 init_arc (arcptr, 0, i+1, insn);
1005 else
1006 init_arc (arcptr, i, num_blocks-1, insn);
1007 arcptr->fake = 1;
1008 num_arcs++;
1010 else if (code == JUMP_INSN)
1012 rtx tem, pattern = PATTERN (insn);
1013 rtx tablejump = 0;
1015 /* If running without optimization, then jump label won't be valid,
1016 so we must search for the destination label in that case.
1017 We have to handle tablejumps and returns specially anyways, so
1018 we don't check the JUMP_LABEL at all here. */
1020 /* ??? This code should be rewritten. We need a more elegant way
1021 to find the LABEL_REF. We need a more elegant way to
1022 differentiate tablejump entries from computed gotos.
1023 We should perhaps reuse code from flow to compute the CFG
1024 instead of trying to compute it here.
1026 We can't use current_function_has_computed_jump, because that
1027 is calculated later by flow. We can't use computed_jump_p,
1028 because that returns true for tablejump entry insns for some
1029 targets, e.g. HPPA and MIPS. */
1031 if (GET_CODE (pattern) == PARALLEL)
1033 /* This assumes that PARALLEL jumps with a USE are
1034 tablejump entry jumps. The same assumption can be found
1035 in computed_jump_p. */
1036 /* Make an arc from this jump to the label of the
1037 jump table. This will instrument the number of
1038 times the switch statement is executed. */
1039 if (GET_CODE (XVECEXP (pattern, 0, 1)) == USE)
1041 tem = XEXP (XVECEXP (pattern, 0, 1), 0);
1042 if (GET_CODE (tem) != LABEL_REF)
1043 abort ();
1044 dest = label_to_bb[CODE_LABEL_NUMBER (XEXP (tem, 0))];
1046 else if (GET_CODE (XVECEXP (pattern, 0, 0)) == SET
1047 && SET_DEST (XVECEXP (pattern, 0, 0)) == pc_rtx)
1049 tem = SET_SRC (XVECEXP (pattern, 0, 0));
1050 if (GET_CODE (tem) == PLUS
1051 && GET_CODE (XEXP (tem, 1)) == LABEL_REF)
1053 tem = XEXP (tem, 1);
1054 dest = label_to_bb [CODE_LABEL_NUMBER (XEXP (tem, 0))];
1057 else
1058 abort ();
1060 else if (GET_CODE (pattern) == ADDR_VEC
1061 || GET_CODE (pattern) == ADDR_DIFF_VEC)
1062 tablejump = pattern;
1063 else if (GET_CODE (pattern) == RETURN)
1064 dest = num_blocks - 1;
1065 else if (GET_CODE (pattern) != SET)
1066 abort ();
1067 else if ((tem = SET_SRC (pattern))
1068 && GET_CODE (tem) == LABEL_REF)
1069 dest = label_to_bb[CODE_LABEL_NUMBER (XEXP (tem, 0))];
1070 /* Recognize HPPA table jump entry. This code is similar to
1071 the code above in the PARALLEL case. */
1072 else if (GET_CODE (tem) == PLUS
1073 && GET_CODE (XEXP (tem, 0)) == MEM
1074 && GET_CODE (XEXP (XEXP (tem, 0), 0)) == PLUS
1075 && GET_CODE (XEXP (XEXP (XEXP (tem, 0), 0), 0)) == PC
1076 && GET_CODE (XEXP (tem, 1)) == LABEL_REF
1077 && tablejump_entry_p (insn, XEXP (tem, 1)))
1078 dest = label_to_bb[CODE_LABEL_NUMBER (XEXP (XEXP (tem, 1), 0))];
1079 /* Recognize the MIPS table jump entry. */
1080 else if (GET_CODE (tem) == PLUS
1081 && GET_CODE (XEXP (tem, 0)) == REG
1082 && GET_CODE (XEXP (tem, 1)) == LABEL_REF
1083 && tablejump_entry_p (insn, XEXP (tem, 1)))
1084 dest = label_to_bb[CODE_LABEL_NUMBER (XEXP (XEXP (tem, 1), 0))];
1085 else
1087 rtx label_ref;
1089 /* Must be an IF_THEN_ELSE branch. If it isn't, assume it
1090 is a computed goto, which aren't supported yet. */
1091 if (GET_CODE (tem) != IF_THEN_ELSE)
1092 fatal ("-fprofile-arcs does not support computed gotos");
1093 if (XEXP (tem, 1) != pc_rtx)
1094 label_ref = XEXP (tem, 1);
1095 else
1096 label_ref = XEXP (tem, 2);
1097 dest = label_to_bb[CODE_LABEL_NUMBER (XEXP (label_ref, 0))];
1100 if (tablejump)
1102 int diff_vec_p = GET_CODE (tablejump) == ADDR_DIFF_VEC;
1103 int len = XVECLEN (tablejump, diff_vec_p);
1104 int k;
1106 for (k = 0; k < len; k++)
1108 rtx tem = XEXP (XVECEXP (tablejump, diff_vec_p, k), 0);
1109 dest = label_to_bb[CODE_LABEL_NUMBER (tem)];
1111 arcptr = (struct adj_list *) alloca (sizeof(struct adj_list));
1112 init_arc (arcptr, i, dest, insn);
1114 num_arcs++;
1117 else
1119 arcptr = (struct adj_list *) alloca (sizeof (struct adj_list));
1120 init_arc (arcptr, i, dest, insn);
1122 num_arcs++;
1125 /* Determine whether or not this jump will fall through.
1126 Unconditional jumps and returns are not always followed by
1127 barriers. */
1128 pattern = PATTERN (insn);
1129 if (GET_CODE (pattern) == PARALLEL
1130 || GET_CODE (pattern) == RETURN)
1131 fall_through = 0;
1132 else if (GET_CODE (pattern) == ADDR_VEC
1133 || GET_CODE (pattern) == ADDR_DIFF_VEC)
1134 /* These aren't actually jump insns, but they never fall
1135 through, so... */
1136 fall_through = 0;
1137 else
1139 if (GET_CODE (pattern) != SET || SET_DEST (pattern) != pc_rtx)
1140 abort ();
1141 if (GET_CODE (SET_SRC (pattern)) != IF_THEN_ELSE)
1142 fall_through = 0;
1146 if (code != NOTE)
1147 prev_code = code;
1148 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
1150 /* Make a fake insn to tag our notes on. */
1151 bb_graph[i].first_insn = insn
1152 = emit_insn_after (gen_rtx_USE (VOIDmode, stack_pointer_rtx),
1153 insn);
1154 prev_code = CALL_INSN;
1158 /* If the code at the end of the function would give a new block, then
1159 do the following. */
1161 if (prev_code == JUMP_INSN || prev_code == CALL_INSN
1162 || prev_code == CODE_LABEL || prev_code == BARRIER)
1164 if (fall_through)
1166 arcptr = (struct adj_list *) alloca (sizeof (struct adj_list));
1167 init_arc (arcptr, i, i + 1, 0);
1168 arcptr->fall_through = 1;
1170 num_arcs++;
1173 /* This may not be a real insn, but that should not cause a problem. */
1174 bb_graph[i+1].first_insn = get_last_insn ();
1177 /* There is always a fake arc from the last block of the function
1178 to the function exit block. */
1179 arcptr = (struct adj_list *) alloca (sizeof (struct adj_list));
1180 init_arc (arcptr, num_blocks-2, num_blocks-1, 0);
1181 arcptr->fake = 1;
1182 num_arcs++;
1185 total_num_arcs += num_arcs;
1186 if (dump_file)
1187 fprintf (dump_file, "%d arcs\n", num_arcs);
1189 /* Create spanning tree from basic block graph, mark each arc that is
1190 on the spanning tree. */
1192 /* To reduce the instrumentation cost, make two passes over the tree.
1193 First, put as many must-split (crowded and fake) arcs on the tree as
1194 possible, then on the second pass fill in the rest of the tree.
1195 Note that the spanning tree is considered undirected, so that as many
1196 must-split arcs as possible can be put on it.
1198 Fallthrough arcs which are crowded should not be chosen on the first
1199 pass, since they do not require creating a new basic block. These
1200 arcs will have fall_through set. */
1202 find_spanning_tree (num_blocks);
1204 /* Create a .bbg file from which gcov can reconstruct the basic block
1205 graph. First output the number of basic blocks, and then for every
1206 arc output the source and target basic block numbers.
1207 NOTE: The format of this file must be compatible with gcov. */
1209 if (flag_test_coverage)
1211 int flag_bits;
1213 __write_long (num_blocks, bbg_file, 4);
1214 __write_long (num_arcs, bbg_file, 4);
1216 for (i = 0; i < num_blocks; i++)
1218 long count = 0;
1219 for (arcptr = bb_graph[i].succ; arcptr; arcptr = arcptr->succ_next)
1220 count++;
1221 __write_long (count, bbg_file, 4);
1223 for (arcptr = bb_graph[i].succ; arcptr; arcptr = arcptr->succ_next)
1225 flag_bits = 0;
1226 if (arcptr->on_tree)
1227 flag_bits |= 0x1;
1228 if (arcptr->fake)
1229 flag_bits |= 0x2;
1230 if (arcptr->fall_through)
1231 flag_bits |= 0x4;
1233 __write_long (ARC_TARGET (arcptr), bbg_file, 4);
1234 __write_long (flag_bits, bbg_file, 4);
1238 /* Emit a -1 to separate the list of all arcs from the list of
1239 loop back edges that follows. */
1240 __write_long (-1, bbg_file, 4);
1243 /* For each arc not on the spanning tree, add counting code as rtl. */
1245 if (profile_arc_flag)
1247 instrument_arcs (f, num_blocks, dump_file);
1248 allocate_reg_info (max_reg_num (), FALSE, FALSE);
1251 /* Execute the rest only if doing branch probabilities. */
1252 if (flag_branch_probabilities)
1253 compute_branch_probabilities (num_blocks, dump_file);
1255 /* Clean up. */
1256 free (bb_graph);
1259 /* Initialize a new arc.
1260 ARCPTR is the empty adj_list this function fills in.
1261 SOURCE is the block number of the source block.
1262 TARGET is the block number of the target block.
1263 INSN is the insn which transfers control from SOURCE to TARGET,
1264 or zero if the transfer is implicit. */
1266 static void
1267 init_arc (arcptr, source, target, insn)
1268 struct adj_list *arcptr;
1269 int source, target;
1270 rtx insn;
1272 ARC_TARGET (arcptr) = target;
1273 ARC_SOURCE (arcptr) = source;
1275 ARC_COUNT (arcptr) = 0;
1276 arcptr->count_valid = 0;
1277 arcptr->on_tree = 0;
1278 arcptr->fake = 0;
1279 arcptr->fall_through = 0;
1280 arcptr->branch_insn = insn;
1282 arcptr->succ_next = bb_graph[source].succ;
1283 bb_graph[source].succ = arcptr;
1284 bb_graph[source].succ_count++;
1286 arcptr->pred_next = bb_graph[target].pred;
1287 bb_graph[target].pred = arcptr;
1288 bb_graph[target].pred_count++;
1291 /* This function searches all of the arcs in the program flow graph, and puts
1292 as many bad arcs as possible onto the spanning tree. Bad arcs include
1293 fake arcs (needed for setjmp(), longjmp(), exit()) which MUST be on the
1294 spanning tree as they can't be instrumented. Also, arcs which must be
1295 split when instrumented should be part of the spanning tree if possible. */
1297 static void
1298 find_spanning_tree (num_blocks)
1299 int num_blocks;
1301 int i;
1302 struct adj_list *arcptr;
1303 struct bb_info *binfo = &bb_graph[0];
1305 /* Fake arcs must be part of the spanning tree, and are always safe to put
1306 on the spanning tree. Fake arcs will either be a successor of node 0,
1307 a predecessor of the last node, or from the last node to node 0. */
1309 for (arcptr = bb_graph[0].succ; arcptr; arcptr = arcptr->succ_next)
1310 if (arcptr->fake)
1312 /* Adding this arc should never cause a cycle. This is a fatal
1313 error if it would. */
1314 if (bb_graph[ARC_TARGET (arcptr)].on_tree && binfo->on_tree)
1315 abort();
1316 else
1318 arcptr->on_tree = 1;
1319 bb_graph[ARC_TARGET (arcptr)].on_tree = 1;
1320 binfo->on_tree = 1;
1324 binfo = &bb_graph[num_blocks-1];
1325 for (arcptr = binfo->pred; arcptr; arcptr = arcptr->pred_next)
1326 if (arcptr->fake)
1328 /* Adding this arc should never cause a cycle. This is a fatal
1329 error if it would. */
1330 if (bb_graph[ARC_SOURCE (arcptr)].on_tree && binfo->on_tree)
1331 abort();
1332 else
1334 arcptr->on_tree = 1;
1335 bb_graph[ARC_SOURCE (arcptr)].on_tree = 1;
1336 binfo->on_tree = 1;
1339 /* The only entrace to node zero is a fake arc. */
1340 bb_graph[0].pred->on_tree = 1;
1342 /* Arcs which are crowded at both the source and target should be put on
1343 the spanning tree if possible, except for fall_throuch arcs which never
1344 require adding a new block even if crowded, add arcs with the same source
1345 and dest which must always be instrumented. */
1346 for (i = 0; i < num_blocks; i++)
1348 binfo = &bb_graph[i];
1350 for (arcptr = binfo->succ; arcptr; arcptr = arcptr->succ_next)
1351 if (! ((binfo->succ == arcptr && arcptr->succ_next == 0)
1352 || (bb_graph[ARC_TARGET (arcptr)].pred
1353 && arcptr->pred_next == 0))
1354 && ! arcptr->fall_through
1355 && ARC_TARGET (arcptr) != i)
1357 /* This is a crowded arc at both source and target. Try to put
1358 in on the spanning tree. Can do this if either the source or
1359 target block is not yet on the tree. */
1360 if (! bb_graph[ARC_TARGET (arcptr)].on_tree || ! binfo->on_tree)
1362 arcptr->on_tree = 1;
1363 bb_graph[ARC_TARGET (arcptr)].on_tree = 1;
1364 binfo->on_tree = 1;
1369 /* Clear all of the basic block on_tree bits, so that we can use them to
1370 create the spanning tree. */
1371 for (i = 0; i < num_blocks; i++)
1372 bb_graph[i].on_tree = 0;
1374 /* Now fill in the spanning tree until every basic block is on it.
1375 Don't put the 0 to 1 fall through arc on the tree, since it is
1376 always cheap to instrument, so start filling the tree from node 1. */
1378 for (i = 1; i < num_blocks; i++)
1379 for (arcptr = bb_graph[i].succ; arcptr; arcptr = arcptr->succ_next)
1380 if (! arcptr->on_tree
1381 && ! bb_graph[ARC_TARGET (arcptr)].on_tree)
1383 fill_spanning_tree (i);
1384 break;
1388 /* Add arcs reached from BLOCK to the spanning tree if they are needed and
1389 not already there. */
1391 static void
1392 fill_spanning_tree (block)
1393 int block;
1395 struct adj_list *arcptr;
1397 expand_spanning_tree (block);
1399 for (arcptr = bb_graph[block].succ; arcptr; arcptr = arcptr->succ_next)
1400 if (! arcptr->on_tree
1401 && ! bb_graph[ARC_TARGET (arcptr)].on_tree)
1403 arcptr->on_tree = 1;
1404 fill_spanning_tree (ARC_TARGET (arcptr));
1408 /* When first visit a block, must add all blocks that are already connected
1409 to this block via tree arcs to the spanning tree. */
1411 static void
1412 expand_spanning_tree (block)
1413 int block;
1415 struct adj_list *arcptr;
1417 bb_graph[block].on_tree = 1;
1419 for (arcptr = bb_graph[block].succ; arcptr; arcptr = arcptr->succ_next)
1420 if (arcptr->on_tree && ! bb_graph[ARC_TARGET (arcptr)].on_tree)
1421 expand_spanning_tree (ARC_TARGET (arcptr));
1423 for (arcptr = bb_graph[block].pred;
1424 arcptr; arcptr = arcptr->pred_next)
1425 if (arcptr->on_tree && ! bb_graph[ARC_SOURCE (arcptr)].on_tree)
1426 expand_spanning_tree (ARC_SOURCE (arcptr));
1429 /* Perform file-level initialization for branch-prob processing. */
1431 void
1432 init_branch_prob (filename)
1433 const char *filename;
1435 long len;
1436 int i;
1438 if (flag_test_coverage)
1440 /* Open an output file for the basic block/line number map. */
1441 int len = strlen (filename);
1442 char *data_file = (char *) alloca (len + 4);
1443 strcpy (data_file, filename);
1444 strip_off_ending (data_file, len);
1445 strcat (data_file, ".bb");
1446 if ((bb_file = fopen (data_file, "wb")) == 0)
1447 pfatal_with_name (data_file);
1449 /* Open an output file for the program flow graph. */
1450 len = strlen (filename);
1451 bbg_file_name = (char *) alloca (len + 5);
1452 strcpy (bbg_file_name, filename);
1453 strip_off_ending (bbg_file_name, len);
1454 strcat (bbg_file_name, ".bbg");
1455 if ((bbg_file = fopen (bbg_file_name, "wb")) == 0)
1456 pfatal_with_name (bbg_file_name);
1458 /* Initialize to zero, to ensure that the first file name will be
1459 written to the .bb file. */
1460 last_bb_file_name = 0;
1463 if (flag_branch_probabilities)
1465 len = strlen (filename);
1466 da_file_name = (char *) alloca (len + 4);
1467 strcpy (da_file_name, filename);
1468 strip_off_ending (da_file_name, len);
1469 strcat (da_file_name, ".da");
1470 if ((da_file = fopen (da_file_name, "rb")) == 0)
1471 warning ("file %s not found, execution counts assumed to be zero.",
1472 da_file_name);
1474 /* The first word in the .da file gives the number of instrumented arcs,
1475 which is not needed for our purposes. */
1477 if (da_file)
1478 __read_long (&len, da_file, 8);
1481 if (profile_arc_flag)
1482 init_arc_profiler ();
1484 total_num_blocks = 0;
1485 total_num_arcs = 0;
1486 total_num_arcs_instrumented = 0;
1487 total_num_blocks_created = 0;
1488 total_num_passes = 0;
1489 total_num_times_called = 0;
1490 total_num_branches = 0;
1491 total_num_never_executed = 0;
1492 for (i = 0; i < 20; i++)
1493 total_hist_br_prob[i] = 0;
1496 /* Performs file-level cleanup after branch-prob processing
1497 is completed. */
1499 void
1500 end_branch_prob (dump_file)
1501 FILE *dump_file;
1503 if (flag_test_coverage)
1505 fclose (bb_file);
1506 fclose (bbg_file);
1509 if (flag_branch_probabilities)
1511 if (da_file)
1513 long temp;
1514 /* This seems slightly dangerous, as it presumes the EOF
1515 flag will not be set until an attempt is made to read
1516 past the end of the file. */
1517 if (feof (da_file))
1518 warning (".da file contents exhausted too early\n");
1519 /* Should be at end of file now. */
1520 if (__read_long (&temp, da_file, 8) == 0)
1521 warning (".da file contents not exhausted\n");
1522 fclose (da_file);
1526 if (dump_file)
1528 fprintf (dump_file, "\n");
1529 fprintf (dump_file, "Total number of blocks: %d\n", total_num_blocks);
1530 fprintf (dump_file, "Total number of arcs: %d\n", total_num_arcs);
1531 fprintf (dump_file, "Total number of instrumented arcs: %d\n",
1532 total_num_arcs_instrumented);
1533 fprintf (dump_file, "Total number of blocks created: %d\n",
1534 total_num_blocks_created);
1535 fprintf (dump_file, "Total number of graph solution passes: %d\n",
1536 total_num_passes);
1537 if (total_num_times_called != 0)
1538 fprintf (dump_file, "Average number of graph solution passes: %d\n",
1539 (total_num_passes + (total_num_times_called >> 1))
1540 / total_num_times_called);
1541 fprintf (dump_file, "Total number of branches: %d\n", total_num_branches);
1542 fprintf (dump_file, "Total number of branches never executed: %d\n",
1543 total_num_never_executed);
1544 if (total_num_branches)
1546 int i;
1548 for (i = 0; i < 10; i++)
1549 fprintf (dump_file, "%d%% branches in range %d-%d%%\n",
1550 (total_hist_br_prob[i] + total_hist_br_prob[19-i]) * 100
1551 / total_num_branches, 5*i, 5*i+5);
1556 /* The label used by the arc profiling code. */
1558 static rtx profiler_label;
1560 /* Initialize the profiler_label. */
1562 static void
1563 init_arc_profiler ()
1565 /* Generate and save a copy of this so it can be shared. */
1566 char *name = ggc_alloc_string (NULL, 20);
1567 ASM_GENERATE_INTERNAL_LABEL (name, "LPBX", 2);
1568 profiler_label = gen_rtx_SYMBOL_REF (Pmode, name);
1569 ggc_add_rtx_root (&profiler_label, 1);
1572 /* Output instructions as RTL to increment the arc execution count. */
1574 static void
1575 output_arc_profiler (arcno, insert_after)
1576 int arcno;
1577 rtx insert_after;
1579 rtx profiler_target_addr
1580 = (arcno ? plus_constant (profiler_label,
1581 LONG_TYPE_SIZE / BITS_PER_UNIT * arcno)
1582 : profiler_label);
1583 enum machine_mode mode = mode_for_size (LONG_TYPE_SIZE, MODE_INT, 0);
1584 rtx profiler_reg = gen_reg_rtx (mode);
1585 rtx address_reg = gen_reg_rtx (Pmode);
1586 rtx mem_ref, add_ref;
1587 rtx sequence;
1589 /* In this case, reload can use explicitly mentioned hard registers for
1590 reloads. It is not safe to output profiling code between a call
1591 and the instruction that copies the result to a pseudo-reg. This
1592 is because reload may allocate one of the profiling code pseudo-regs
1593 to the return value reg, thus clobbering the return value. So we
1594 must check for calls here, and emit the profiling code after the
1595 instruction that uses the return value, if any.
1597 ??? The code here performs the same tests that reload does so hopefully
1598 all the bases are covered. */
1600 if (SMALL_REGISTER_CLASSES
1601 && GET_CODE (insert_after) == CALL_INSN
1602 && (GET_CODE (PATTERN (insert_after)) == SET
1603 || (GET_CODE (PATTERN (insert_after)) == PARALLEL
1604 && GET_CODE (XVECEXP (PATTERN (insert_after), 0, 0)) == SET)))
1606 rtx return_reg;
1607 rtx next_insert_after = next_nonnote_insn (insert_after);
1609 /* The first insn after the call may be a stack pop, skip it. */
1610 if (next_insert_after
1611 && GET_CODE (next_insert_after) == INSN
1612 && GET_CODE (PATTERN (next_insert_after)) == SET
1613 && SET_DEST (PATTERN (next_insert_after)) == stack_pointer_rtx)
1614 next_insert_after = next_nonnote_insn (next_insert_after);
1616 if (next_insert_after
1617 && GET_CODE (next_insert_after) == INSN)
1619 if (GET_CODE (PATTERN (insert_after)) == SET)
1620 return_reg = SET_DEST (PATTERN (insert_after));
1621 else
1622 return_reg = SET_DEST (XVECEXP (PATTERN (insert_after), 0, 0));
1624 /* Now, NEXT_INSERT_AFTER may be an instruction that uses the
1625 return value. However, it could also be something else,
1626 like a CODE_LABEL, so check that the code is INSN. */
1627 if (next_insert_after != 0
1628 && GET_RTX_CLASS (GET_CODE (next_insert_after)) == 'i'
1629 && reg_referenced_p (return_reg, PATTERN (next_insert_after)))
1630 insert_after = next_insert_after;
1634 start_sequence ();
1636 emit_move_insn (address_reg, profiler_target_addr);
1637 mem_ref = gen_rtx_MEM (mode, address_reg);
1638 emit_move_insn (profiler_reg, mem_ref);
1640 add_ref = gen_rtx_PLUS (mode, profiler_reg, GEN_INT (1));
1641 emit_move_insn (profiler_reg, add_ref);
1643 /* This is the same rtx as above, but it is not legal to share this rtx. */
1644 mem_ref = gen_rtx_MEM (mode, address_reg);
1645 emit_move_insn (mem_ref, profiler_reg);
1647 sequence = gen_sequence ();
1648 end_sequence ();
1649 emit_insn_after (sequence, insert_after);
1652 /* Output code for a constructor that will invoke __bb_init_func, if
1653 this has not already been done. */
1655 void
1656 output_func_start_profiler ()
1658 tree fnname, fndecl;
1659 char *name, *cfnname;
1660 rtx table_address;
1661 enum machine_mode mode = mode_for_size (LONG_TYPE_SIZE, MODE_INT, 0);
1662 int save_flag_inline_functions = flag_inline_functions;
1664 /* It's either already been output, or we don't need it because we're
1665 not doing profile-arcs. */
1666 if (! need_func_profiler)
1667 return;
1669 need_func_profiler = 0;
1671 /* Synthesize a constructor function to invoke __bb_init_func with a
1672 pointer to this object file's profile block. */
1674 /* Try and make a unique name given the "file function name".
1676 And no, I don't like this either. */
1678 fnname = get_file_function_name ('I');
1679 cfnname = IDENTIFIER_POINTER (fnname);
1680 name = xmalloc (strlen (cfnname) + 5);
1681 sprintf (name, "%sGCOV",cfnname);
1682 fnname = get_identifier (name);
1683 free (name);
1685 fndecl = build_decl (FUNCTION_DECL, fnname,
1686 build_function_type (void_type_node, NULL_TREE));
1687 DECL_EXTERNAL (fndecl) = 0;
1688 TREE_PUBLIC (fndecl) = 1;
1689 DECL_ASSEMBLER_NAME (fndecl) = fnname;
1690 DECL_RESULT (fndecl) = build_decl (RESULT_DECL, NULL_TREE, void_type_node);
1692 fndecl = pushdecl (fndecl);
1693 rest_of_decl_compilation (fndecl, 0, 1, 0);
1694 announce_function (fndecl);
1695 current_function_decl = fndecl;
1696 DECL_INITIAL (fndecl) = error_mark_node;
1697 temporary_allocation ();
1698 make_function_rtl (fndecl);
1699 init_function_start (fndecl, input_filename, lineno);
1700 pushlevel (0);
1701 expand_function_start (fndecl, 0);
1703 /* Actually generate the code to call __bb_init_func. */
1704 name = ggc_alloc_string (NULL, 20);
1705 ASM_GENERATE_INTERNAL_LABEL (name, "LPBX", 0);
1706 table_address = force_reg (Pmode, gen_rtx_SYMBOL_REF (Pmode, name));
1707 emit_library_call (gen_rtx_SYMBOL_REF
1708 (Pmode, ggc_alloc_string ("__bb_init_func", 14)), 0,
1709 mode, 1, table_address, Pmode);
1711 expand_function_end (input_filename, lineno, 0);
1712 poplevel (1, 0, 1);
1714 /* Since fndecl isn't in the list of globals, it would never be emitted
1715 when it's considered to be 'safe' for inlining, so turn off
1716 flag_inline_functions. */
1717 flag_inline_functions = 0;
1719 rest_of_compilation (fndecl);
1721 /* Reset flag_inline_functions to its original value. */
1722 flag_inline_functions = save_flag_inline_functions;
1724 if (! quiet_flag)
1725 fflush (asm_out_file);
1726 current_function_decl = NULL_TREE;
1728 assemble_constructor (IDENTIFIER_POINTER (DECL_NAME (fndecl)));