c-lex.c: Include <stdlib.h> and <string.h>/<strings.h>.
[gcc.git] / gcc / profile.c
1 /* Calculate branch probabilities, and basic block execution counts.
2 Copyright (C) 1990, 91, 92, 93, 94, 96, 1997 Free Software Foundation, Inc.
3 Contributed by James E. Wilson, UC Berkeley/Cygnus Support;
4 based on some ideas from Dain Samples of UC Berkeley.
5 Further mangling by Bob Manson, Cygnus Support.
6
7 This file is part of GNU CC.
8
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.
13
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.
18
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, 675 Mass Ave, Cambridge, MA 02139, USA. */
22
23 /* ??? Really should not put insns inside of LIBCALL sequences, when putting
24 insns after a call, should look for the insn setting the retval, and
25 insert the insns after that one. */
26
27 /* ??? Register allocation should use basic block execution counts to
28 give preference to the most commonly executed blocks. */
29
30 /* ??? The .da files are not safe. Changing the program after creating .da
31 files or using different options when compiling with -fbranch-probabilities
32 can result the arc data not matching the program. Maybe add instrumented
33 arc count to .bbg file? Maybe check whether PFG matches the .bbg file? */
34
35 /* ??? Should calculate branch probabilities before instrumenting code, since
36 then we can use arc counts to help decide which arcs to instrument. */
37
38 /* ??? Rearrange code so that the most frequently executed arcs become from
39 one block to the next block (i.e. a fall through), move seldom executed
40 code outside of loops even at the expense of adding a few branches to
41 achieve this, see Dain Sample's UC Berkeley thesis. */
42
43 #include "config.h"
44 #include <stdio.h>
45 #ifdef HAVE_STRING_H
46 #include <string.h>
47 #else
48 #ifdef HAVE_STRINGS_H
49 #include <strings.h>
50 #endif
51 #endif
52 #include "rtl.h"
53 #include "flags.h"
54 #include "insn-flags.h"
55 #include "insn-config.h"
56 #include "output.h"
57 #include "regs.h"
58 #include "tree.h"
59 #include "output.h"
60 #include "gcov-io.h"
61
62 extern char * xmalloc ();
63 extern void free ();
64
65 /* One of these is dynamically created whenever we identify an arc in the
66 function. */
67
68 struct adj_list
69 {
70 int source;
71 int target;
72 int arc_count;
73 unsigned int count_valid : 1;
74 unsigned int on_tree : 1;
75 unsigned int fake : 1;
76 unsigned int fall_through : 1;
77 rtx branch_insn;
78 struct adj_list *pred_next;
79 struct adj_list *succ_next;
80 };
81
82 #define ARC_TARGET(ARCPTR) (ARCPTR->target)
83 #define ARC_SOURCE(ARCPTR) (ARCPTR->source)
84 #define ARC_COUNT(ARCPTR) (ARCPTR->arc_count)
85
86 /* Count the number of basic blocks, and create an array of these structures,
87 one for each bb in the function. */
88
89 struct bb_info
90 {
91 struct adj_list *succ;
92 struct adj_list *pred;
93 int succ_count;
94 int pred_count;
95 int exec_count;
96 unsigned int count_valid : 1;
97 unsigned int on_tree : 1;
98 rtx first_insn;
99 };
100
101 /* Indexed by label number, gives the basic block number containing that
102 label. */
103
104 static int *label_to_bb;
105
106 /* Number of valid entries in the label_to_bb array. */
107
108 static int label_to_bb_size;
109
110 /* Indexed by block index, holds the basic block graph. */
111
112 static struct bb_info *bb_graph;
113
114 /* Name and file pointer of the output file for the basic block graph. */
115
116 static char *bbg_file_name;
117 static FILE *bbg_file;
118
119 /* Name and file pointer of the input file for the arc count data. */
120
121 static char *da_file_name;
122 static FILE *da_file;
123
124 /* Pointer of the output file for the basic block/line number map. */
125 static FILE *bb_file;
126
127 /* Last source file name written to bb_file. */
128
129 static char *last_bb_file_name;
130
131 /* Indicates whether the next line number note should be output to
132 bb_file or not. Used to eliminate a redundant note after an
133 expanded inline function call. */
134
135 static int ignore_next_note;
136
137 /* Used by final, for allocating the proper amount of storage for the
138 instrumented arc execution counts. */
139
140 int count_instrumented_arcs;
141
142 /* Number of executions for the return label. */
143
144 int return_label_execution_count;
145
146 /* Collect statistics on the performance of this pass for the entire source
147 file. */
148
149 static int total_num_blocks;
150 static int total_num_arcs;
151 static int total_num_arcs_instrumented;
152 static int total_num_blocks_created;
153 static int total_num_passes;
154 static int total_num_times_called;
155 static int total_hist_br_prob[20];
156 static int total_num_never_executed;
157 static int total_num_branches;
158
159 /* Forward declarations. */
160 static void init_arc PROTO((struct adj_list *, int, int, rtx));
161 static void find_spanning_tree PROTO((int));
162 static void expand_spanning_tree PROTO((int));
163 static void fill_spanning_tree PROTO((int));
164 static void init_arc_profiler PROTO((void));
165 static void output_arc_profiler PROTO((int, rtx));
166
167 #ifndef LONG_TYPE_SIZE
168 #define LONG_TYPE_SIZE BITS_PER_WORD
169 #endif
170
171 /* If non-zero, we need to output a constructor to set up the
172 per-object-file data. */
173 static int need_func_profiler = 0;
174
175 \f
176 /* Add arc instrumentation code to the entire insn chain.
177
178 F is the first insn of the chain.
179 NUM_BLOCKS is the number of basic blocks found in F.
180 DUMP_FILE, if nonzero, is an rtl dump file we can write to. */
181
182 static void
183 instrument_arcs (f, num_blocks, dump_file)
184 rtx f;
185 int num_blocks;
186 FILE *dump_file;
187 {
188 register int i;
189 register struct adj_list *arcptr, *backptr;
190 int num_arcs = 0;
191 int num_instr_arcs = 0;
192 rtx insn;
193
194 /* Instrument the program start. */
195 /* Handle block 0 specially, since it will always be instrumented,
196 but it doesn't have a valid first_insn or branch_insn. We must
197 put the instructions before the NOTE_INSN_FUNCTION_BEG note, so
198 that they don't clobber any of the parameters of the current
199 function. */
200 for (insn = f; insn; insn = NEXT_INSN (insn))
201 if (GET_CODE (insn) == NOTE
202 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_BEG)
203 break;
204 insn = PREV_INSN (insn);
205 need_func_profiler = 1;
206 output_arc_profiler (total_num_arcs_instrumented + num_instr_arcs++, insn);
207
208 for (i = 1; i < num_blocks; i++)
209 for (arcptr = bb_graph[i].succ; arcptr; arcptr = arcptr->succ_next)
210 if (! arcptr->on_tree)
211 {
212 if (dump_file)
213 fprintf (dump_file, "Arc %d to %d instrumented\n", i,
214 ARC_TARGET (arcptr));
215
216 /* Check to see if this arc is the only exit from its source block,
217 or the only entrance to its target block. In either case,
218 we don't need to create a new block to instrument the arc. */
219
220 if (bb_graph[i].succ == arcptr && arcptr->succ_next == 0)
221 {
222 /* Instrument the source block. */
223 output_arc_profiler (total_num_arcs_instrumented
224 + num_instr_arcs++,
225 PREV_INSN (bb_graph[i].first_insn));
226 }
227 else if (arcptr == bb_graph[ARC_TARGET (arcptr)].pred
228 && arcptr->pred_next == 0)
229 {
230 /* Instrument the target block. */
231 output_arc_profiler (total_num_arcs_instrumented
232 + num_instr_arcs++,
233 PREV_INSN (bb_graph[ARC_TARGET (arcptr)].first_insn));
234 }
235 else if (arcptr->fall_through)
236 {
237 /* This is a fall-through; put the instrumentation code after
238 the branch that ends this block. */
239
240 for (backptr = bb_graph[i].succ; backptr;
241 backptr = backptr->succ_next)
242 if (backptr != arcptr)
243 break;
244
245 output_arc_profiler (total_num_arcs_instrumented
246 + num_instr_arcs++,
247 backptr->branch_insn);
248 }
249 else
250 {
251 /* Must emit a new basic block to hold the arc counting code. */
252 enum rtx_code code = GET_CODE (PATTERN (arcptr->branch_insn));
253
254 if (code == SET)
255 {
256 /* Create the new basic block right after the branch.
257 Invert the branch so that it jumps past the end of the new
258 block. The new block will consist of the instrumentation
259 code, and a jump to the target of this arc. */
260 int this_is_simplejump = simplejump_p (arcptr->branch_insn);
261 rtx new_label = gen_label_rtx ();
262 rtx old_label, set_src;
263 rtx after = arcptr->branch_insn;
264
265 /* Simplejumps can't reach here. */
266 if (this_is_simplejump)
267 abort ();
268
269 /* We can't use JUMP_LABEL, because it won't be set if we
270 are compiling without optimization. */
271
272 set_src = SET_SRC (single_set (arcptr->branch_insn));
273 if (GET_CODE (set_src) == LABEL_REF)
274 old_label = set_src;
275 else if (GET_CODE (set_src) != IF_THEN_ELSE)
276 abort ();
277 else if (XEXP (set_src, 1) == pc_rtx)
278 old_label = XEXP (XEXP (set_src, 2), 0);
279 else
280 old_label = XEXP (XEXP (set_src, 1), 0);
281
282 /* Set the JUMP_LABEL so that redirect_jump will work. */
283 JUMP_LABEL (arcptr->branch_insn) = old_label;
284
285 /* Add a use for OLD_LABEL that will be needed when we emit
286 the JUMP_INSN below. If we don't do this here,
287 `invert_jump' might delete it for us. We must add two
288 when not optimizing, because the NUSES is zero now,
289 but must be at least two to prevent the label from being
290 deleted. */
291 LABEL_NUSES (old_label) += 2;
292
293 /* Emit the insns for the new block in reverse order,
294 since that is most convenient. */
295
296 if (this_is_simplejump)
297 {
298 after = NEXT_INSN (arcptr->branch_insn);
299 if (! redirect_jump (arcptr->branch_insn, new_label))
300 /* Don't know what to do if this branch won't
301 redirect. */
302 abort ();
303 }
304 else
305 {
306 if (! invert_jump (arcptr->branch_insn, new_label))
307 /* Don't know what to do if this branch won't invert. */
308 abort ();
309
310 emit_label_after (new_label, after);
311 LABEL_NUSES (new_label)++;
312 }
313 emit_barrier_after (after);
314 emit_jump_insn_after (gen_jump (old_label), after);
315 JUMP_LABEL (NEXT_INSN (after)) = old_label;
316
317 /* Instrument the source arc. */
318 output_arc_profiler (total_num_arcs_instrumented
319 + num_instr_arcs++,
320 after);
321 if (this_is_simplejump)
322 {
323 emit_label_after (new_label, after);
324 LABEL_NUSES (new_label)++;
325 }
326 }
327 else if (code == ADDR_VEC || code == ADDR_DIFF_VEC)
328 {
329 /* A table jump. Create a new basic block immediately
330 after the table, by emitting a barrier, a label, a
331 counting note, and a jump to the old label. Put the
332 new label in the table. */
333
334 rtx new_label = gen_label_rtx ();
335 rtx old_lref, new_lref;
336 int index;
337
338 /* Must determine the old_label reference, do this
339 by counting the arcs after this one, which will
340 give the index of our label in the table. */
341
342 index = 0;
343 for (backptr = arcptr->succ_next; backptr;
344 backptr = backptr->succ_next)
345 index++;
346
347 old_lref = XVECEXP (PATTERN (arcptr->branch_insn),
348 (code == ADDR_DIFF_VEC), index);
349
350 /* Emit the insns for the new block in reverse order,
351 since that is most convenient. */
352 emit_jump_insn_after (gen_jump (XEXP (old_lref, 0)),
353 arcptr->branch_insn);
354 JUMP_LABEL (NEXT_INSN (arcptr->branch_insn))
355 = XEXP (old_lref, 0);
356
357 /* Instrument the source arc. */
358 output_arc_profiler (total_num_arcs_instrumented
359 + num_instr_arcs++,
360 arcptr->branch_insn);
361
362 emit_label_after (new_label, arcptr->branch_insn);
363 LABEL_NUSES (NEXT_INSN (arcptr->branch_insn))++;
364 emit_barrier_after (arcptr->branch_insn);
365
366 /* Fix up the table jump. */
367 new_lref = gen_rtx_LABEL_REF (Pmode, new_label);
368 XVECEXP (PATTERN (arcptr->branch_insn),
369 (code == ADDR_DIFF_VEC), index) = new_lref;
370 }
371 else
372 abort ();
373
374 num_arcs += 1;
375 if (dump_file)
376 fprintf (dump_file,
377 "Arc %d to %d needed new basic block\n", i,
378 ARC_TARGET (arcptr));
379 }
380 }
381
382 total_num_arcs_instrumented += num_instr_arcs;
383 count_instrumented_arcs = total_num_arcs_instrumented;
384
385 total_num_blocks_created += num_arcs;
386 if (dump_file)
387 {
388 fprintf (dump_file, "%d arcs instrumented\n", num_instr_arcs);
389 fprintf (dump_file, "%d extra basic blocks created\n", num_arcs);
390 }
391 }
392
393 /* Output STRING to bb_file, surrounded by DELIMITER. */
394
395 static void
396 output_gcov_string (string, delimiter)
397 char *string;
398 long delimiter;
399 {
400 long temp;
401
402 /* Write a delimiter to indicate that a file name follows. */
403 __write_long (delimiter, bb_file, 4);
404
405 /* Write the string. */
406 temp = strlen (string) + 1;
407 fwrite (string, temp, 1, bb_file);
408
409 /* Append a few zeros, to align the output to a 4 byte boundary. */
410 temp = temp & 0x3;
411 if (temp)
412 {
413 char c[4];
414
415 c[0] = c[1] = c[2] = c[3] = 0;
416 fwrite (c, sizeof (char), 4 - temp, bb_file);
417 }
418
419 /* Store another delimiter in the .bb file, just to make it easy to find the
420 end of the file name. */
421 __write_long (delimiter, bb_file, 4);
422 }
423 \f
424 /* Instrument and/or analyze program behavior based on program flow graph.
425 In either case, this function builds a flow graph for the function being
426 compiled. The flow graph is stored in BB_GRAPH.
427
428 When FLAG_PROFILE_ARCS is nonzero, this function instruments the arcs in
429 the flow graph that are needed to reconstruct the dynamic behavior of the
430 flow graph.
431
432 When FLAG_BRANCH_PROBABILITIES is nonzero, this function reads auxiliary
433 information from a data file containing arc count information from previous
434 executions of the function being compiled. In this case, the flow graph is
435 annotated with actual execution counts, which are later propagated into the
436 rtl for optimization purposes.
437
438 Main entry point of this file. */
439
440 void
441 branch_prob (f, dump_file)
442 rtx f;
443 FILE *dump_file;
444 {
445 int i, num_blocks;
446 int dest;
447 rtx insn;
448 struct adj_list *arcptr;
449 int num_arcs, changes, passes;
450 int total, prob;
451 int hist_br_prob[20], num_never_executed, num_branches;
452 /* Set to non-zero if we got bad count information. */
453 int bad_counts = 0;
454
455 /* start of a function. */
456 if (flag_test_coverage)
457 output_gcov_string (current_function_name, (long) -2);
458
459 /* Execute this only if doing arc profiling or branch probabilities. */
460 if (! profile_arc_flag && ! flag_branch_probabilities
461 && ! flag_test_coverage)
462 abort ();
463
464 total_num_times_called++;
465
466 /* Create an array label_to_bb of ints of size max_label_num. */
467 label_to_bb_size = max_label_num ();
468 label_to_bb = (int *) oballoc (label_to_bb_size * sizeof (int));
469 bzero ((char *) label_to_bb, label_to_bb_size * sizeof (int));
470
471 /* Scan the insns in the function, count the number of basic blocks
472 present. When a code label is passed, set label_to_bb[label] = bb
473 number. */
474
475 /* The first block found will be block 1, so that function entry can be
476 block 0. */
477
478 {
479 register RTX_CODE prev_code = JUMP_INSN;
480 register RTX_CODE code;
481 register rtx insn;
482 register int i;
483 int block_separator_emitted = 0;
484
485 ignore_next_note = 0;
486
487 for (insn = NEXT_INSN (f), i = 0; insn; insn = NEXT_INSN (insn))
488 {
489 code = GET_CODE (insn);
490
491 if (code == BARRIER)
492 ;
493 else if (code == CODE_LABEL)
494 /* This label is part of the next block, but we can't increment
495 block number yet since there might be multiple labels. */
496 label_to_bb[CODE_LABEL_NUMBER (insn)] = i + 1;
497 /* We make NOTE_INSN_SETJMP notes into a block of their own, so that
498 they can be the target of the fake arc for the setjmp call.
499 This avoids creating cycles of fake arcs, which would happen if
500 the block after the setjmp call contained a call insn. */
501 else if ((prev_code == JUMP_INSN || prev_code == CALL_INSN
502 || prev_code == CODE_LABEL || prev_code == BARRIER)
503 && (GET_RTX_CLASS (code) == 'i'
504 || (code == NOTE
505 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)))
506 {
507 i += 1;
508
509 /* Emit the block separator if it hasn't already been emitted. */
510 if (flag_test_coverage && ! block_separator_emitted)
511 {
512 /* Output a zero to the .bb file to indicate that a new
513 block list is starting. */
514 __write_long (0, bb_file, 4);
515 }
516 block_separator_emitted = 0;
517 }
518 /* If flag_test_coverage is true, then we must add an entry to the
519 .bb file for every note. */
520 else if (code == NOTE && flag_test_coverage)
521 {
522 /* Must ignore the line number notes that immediately follow the
523 end of an inline function to avoid counting it twice. There
524 is a note before the call, and one after the call. */
525 if (NOTE_LINE_NUMBER (insn) == NOTE_REPEATED_LINE_NUMBER)
526 ignore_next_note = 1;
527 else if (NOTE_LINE_NUMBER (insn) > 0)
528 {
529 if (ignore_next_note)
530 ignore_next_note = 0;
531 else
532 {
533 /* Emit a block separator here to ensure that a NOTE
534 immediately following a JUMP_INSN or CALL_INSN will end
535 up in the right basic block list. */
536 if ((prev_code == JUMP_INSN || prev_code == CALL_INSN
537 || prev_code == CODE_LABEL || prev_code == BARRIER)
538 && ! block_separator_emitted)
539 {
540 /* Output a zero to the .bb file to indicate that
541 a new block list is starting. */
542 __write_long (0, bb_file, 4);
543
544 block_separator_emitted = 1;
545 }
546
547 /* If this is a new source file, then output the file's
548 name to the .bb file. */
549 if (! last_bb_file_name
550 || strcmp (NOTE_SOURCE_FILE (insn),
551 last_bb_file_name))
552 {
553 if (last_bb_file_name)
554 free (last_bb_file_name);
555 last_bb_file_name
556 = xmalloc (strlen (NOTE_SOURCE_FILE (insn)) + 1);
557 strcpy (last_bb_file_name, NOTE_SOURCE_FILE (insn));
558 output_gcov_string (NOTE_SOURCE_FILE (insn), (long)-1);
559 }
560
561 /* Output the line number to the .bb file. Must be done
562 after the output_bb_profile_data() call, and after the
563 file name is written, to ensure that it is correctly
564 handled by gcov. */
565 __write_long (NOTE_LINE_NUMBER (insn), bb_file, 4);
566 }
567 }
568 }
569
570 if (code != NOTE)
571 prev_code = code;
572 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
573 prev_code = CALL_INSN;
574 }
575
576 /* Allocate last `normal' entry for bb_graph. */
577
578 /* The last insn was a jump, call, or label. In that case we have
579 a block at the end of the function with no insns. */
580 if (prev_code == JUMP_INSN || prev_code == CALL_INSN
581 || prev_code == CODE_LABEL || prev_code == BARRIER)
582 {
583 i++;
584
585 /* Emit the block separator if it hasn't already been emitted. */
586 if (flag_test_coverage && ! block_separator_emitted)
587 {
588 /* Output a zero to the .bb file to indicate that a new
589 block list is starting. */
590 __write_long (0, bb_file, 4);
591 }
592 }
593
594 /* Create another block to stand for EXIT, and make all return insns, and
595 the last basic block point here. Add one more to account for block
596 zero. */
597 num_blocks = i + 2;
598 }
599
600 total_num_blocks += num_blocks;
601 if (dump_file)
602 fprintf (dump_file, "%d basic blocks\n", num_blocks);
603
604 /* If we are only doing test coverage here, then return now. */
605 if (! profile_arc_flag && ! flag_branch_probabilities)
606 return;
607
608 /* Create and initialize the arrays that will hold bb_graph
609 and execution count info. */
610
611 bb_graph = (struct bb_info *) alloca (num_blocks * sizeof (struct bb_info));
612 bzero ((char *) bb_graph, (sizeof (struct bb_info) * num_blocks));
613
614 {
615 /* Scan the insns again:
616 - at the entry to each basic block, increment the predecessor count
617 (and successor of previous block) if it is a fall through entry,
618 create adj_list entries for this and the previous block
619 - at each jump insn, increment predecessor/successor counts for
620 target/source basic blocks, add this insn to pred/succ lists.
621
622 This also cannot be broken out as a separate subroutine
623 because it uses `alloca'. */
624
625 register RTX_CODE prev_code = JUMP_INSN;
626 register RTX_CODE code;
627 register rtx insn;
628 register int i;
629 int fall_through = 0;
630 struct adj_list *arcptr;
631 int dest;
632
633 /* Block 0 always falls through to block 1. */
634 num_arcs = 0;
635 arcptr = (struct adj_list *) alloca (sizeof (struct adj_list));
636 init_arc (arcptr, 0, 1, 0);
637 arcptr->fall_through = 1;
638 num_arcs++;
639
640 /* Add a fake fall through arc from the last block to block 0, to make the
641 graph complete. */
642 arcptr = (struct adj_list *) alloca (sizeof (struct adj_list));
643 init_arc (arcptr, num_blocks - 1, 0, 0);
644 arcptr->fake = 1;
645 num_arcs++;
646
647 /* Exit must be one node of the graph, and all exits from the function
648 must point there. When see a return branch, must point the arc to the
649 exit node. */
650
651 /* Must start scan with second insn in function as above. */
652 for (insn = NEXT_INSN (f), i = 0; insn; insn = NEXT_INSN (insn))
653 {
654 code = GET_CODE (insn);
655
656 if (code == BARRIER)
657 fall_through = 0;
658 else if (code == CODE_LABEL)
659 ;
660 /* We make NOTE_INSN_SETJMP notes into a block of their own, so that
661 they can be the target of the fake arc for the setjmp call.
662 This avoids creating cycles of fake arcs, which would happen if
663 the block after the setjmp call ended with a call. */
664 else if ((prev_code == JUMP_INSN || prev_code == CALL_INSN
665 || prev_code == CODE_LABEL || prev_code == BARRIER)
666 && (GET_RTX_CLASS (code) == 'i'
667 || (code == NOTE
668 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)))
669 {
670 /* This is the first insn of the block. */
671 i += 1;
672 if (fall_through)
673 {
674 arcptr = (struct adj_list *) alloca (sizeof (struct adj_list));
675 init_arc (arcptr, i - 1, i, 0);
676 arcptr->fall_through = 1;
677
678 num_arcs++;
679 }
680 fall_through = 1;
681 bb_graph[i].first_insn = insn;
682 }
683 else if (code == NOTE)
684 {;}
685
686 if (code == CALL_INSN)
687 {
688 /* In the normal case, the call returns, and this is just like
689 a branch fall through. */
690 fall_through = 1;
691
692 /* Setjmp may return more times than called, so to make the graph
693 solvable, add a fake arc from the function entrance to the
694 next block.
695
696 All other functions may return fewer times than called (if
697 a descendent call longjmp or exit), so to make the graph
698 solvable, add a fake arc to the function exit from the
699 current block.
700
701 Distinguish the cases by checking for a SETJUMP note.
702 A call_insn can be the last ins of a function, so must check
703 to see if next insn actually exists. */
704 arcptr = (struct adj_list *) alloca (sizeof (struct adj_list));
705 if (NEXT_INSN (insn)
706 && GET_CODE (NEXT_INSN (insn)) == NOTE
707 && NOTE_LINE_NUMBER (NEXT_INSN (insn)) == NOTE_INSN_SETJMP)
708 init_arc (arcptr, 0, i+1, insn);
709 else
710 init_arc (arcptr, i, num_blocks-1, insn);
711 arcptr->fake = 1;
712 num_arcs++;
713 }
714 else if (code == JUMP_INSN)
715 {
716 rtx tem, pattern = PATTERN (insn);
717 rtx tablejump = 0;
718
719 /* If running without optimization, then jump label won't be valid,
720 so we must search for the destination label in that case.
721 We have to handle tablejumps and returns specially anyways, so
722 we don't check the JUMP_LABEL at all here. */
723
724 if (GET_CODE (pattern) == PARALLEL)
725 {
726 /* This assumes that PARALLEL jumps are tablejump entry
727 jumps. */
728 /* Make an arc from this jump to the label of the
729 jump table. This will instrument the number of
730 times the switch statement is executed. */
731 if (GET_CODE (XVECEXP (pattern, 0, 1)) == USE)
732 {
733 tem = XEXP (XVECEXP (pattern, 0, 1), 0);
734 if (GET_CODE (tem) != LABEL_REF)
735 abort ();
736 dest = label_to_bb[CODE_LABEL_NUMBER (XEXP (tem, 0))];
737 }
738 else if (GET_CODE (XVECEXP (pattern, 0, 0)) == SET
739 && SET_DEST (XVECEXP (pattern, 0, 0)) == pc_rtx)
740 {
741 tem = SET_SRC (XVECEXP (pattern, 0, 0));
742 if (GET_CODE (tem) == PLUS
743 && GET_CODE (XEXP (tem, 1)) == LABEL_REF)
744 {
745 tem = XEXP (tem, 1);
746 dest = label_to_bb [CODE_LABEL_NUMBER (XEXP (tem, 0))];
747 }
748 }
749 else
750 abort ();
751 }
752 else if (GET_CODE (pattern) == ADDR_VEC
753 || GET_CODE (pattern) == ADDR_DIFF_VEC)
754 tablejump = pattern;
755 else if (GET_CODE (pattern) == RETURN)
756 dest = num_blocks - 1;
757 else if ((tem = SET_SRC (pattern))
758 && GET_CODE (tem) == LABEL_REF)
759 dest = label_to_bb[CODE_LABEL_NUMBER (XEXP (tem, 0))];
760 else
761 {
762 rtx label_ref;
763
764 /* Must be an IF_THEN_ELSE branch. */
765 if (GET_CODE (tem) != IF_THEN_ELSE)
766 abort ();
767 if (XEXP (tem, 1) != pc_rtx)
768 label_ref = XEXP (tem, 1);
769 else
770 label_ref = XEXP (tem, 2);
771 dest = label_to_bb[CODE_LABEL_NUMBER (XEXP (label_ref, 0))];
772 }
773
774 if (tablejump)
775 {
776 int diff_vec_p = GET_CODE (tablejump) == ADDR_DIFF_VEC;
777 int len = XVECLEN (tablejump, diff_vec_p);
778 int k;
779
780 for (k = 0; k < len; k++)
781 {
782 rtx tem = XEXP (XVECEXP (tablejump, diff_vec_p, k), 0);
783 dest = label_to_bb[CODE_LABEL_NUMBER (tem)];
784
785 arcptr = (struct adj_list *) alloca (sizeof(struct adj_list));
786 init_arc (arcptr, i, dest, insn);
787
788 num_arcs++;
789 }
790 }
791 else
792 {
793 arcptr = (struct adj_list *) alloca (sizeof (struct adj_list));
794 init_arc (arcptr, i, dest, insn);
795
796 num_arcs++;
797 }
798
799 /* Determine whether or not this jump will fall through.
800 Unconditional jumps and returns are not always followed by
801 barriers. */
802 pattern = PATTERN (insn);
803 if (GET_CODE (pattern) == PARALLEL
804 || GET_CODE (pattern) == RETURN)
805 fall_through = 0;
806 else if (GET_CODE (pattern) == ADDR_VEC
807 || GET_CODE (pattern) == ADDR_DIFF_VEC)
808 /* These aren't actually jump insns, but they never fall
809 through, so... */
810 fall_through = 0;
811 else
812 {
813 if (GET_CODE (pattern) != SET || SET_DEST (pattern) != pc_rtx)
814 abort ();
815 if (GET_CODE (SET_SRC (pattern)) != IF_THEN_ELSE)
816 fall_through = 0;
817 }
818 }
819
820 if (code != NOTE)
821 prev_code = code;
822 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
823 {
824 /* Make a fake insn to tag our notes on. */
825 bb_graph[i].first_insn = insn
826 = emit_insn_after (gen_rtx_USE (VOIDmode, stack_pointer_rtx),
827 insn);
828 prev_code = CALL_INSN;
829 }
830 }
831
832 /* If the code at the end of the function would give a new block, then
833 do the following. */
834
835 if (prev_code == JUMP_INSN || prev_code == CALL_INSN
836 || prev_code == CODE_LABEL || prev_code == BARRIER)
837 {
838 if (fall_through)
839 {
840 arcptr = (struct adj_list *) alloca (sizeof (struct adj_list));
841 init_arc (arcptr, i, i + 1, 0);
842 arcptr->fall_through = 1;
843
844 num_arcs++;
845 }
846
847 /* This may not be a real insn, but that should not cause a problem. */
848 bb_graph[i+1].first_insn = get_last_insn ();
849 }
850
851 /* There is always a fake arc from the last block of the function
852 to the function exit block. */
853 arcptr = (struct adj_list *) alloca (sizeof (struct adj_list));
854 init_arc (arcptr, num_blocks-2, num_blocks-1, 0);
855 arcptr->fake = 1;
856 num_arcs++;
857 }
858
859 total_num_arcs += num_arcs;
860 if (dump_file)
861 fprintf (dump_file, "%d arcs\n", num_arcs);
862
863 /* Create spanning tree from basic block graph, mark each arc that is
864 on the spanning tree. */
865
866 /* To reduce the instrumentation cost, make two passes over the tree.
867 First, put as many must-split (crowded and fake) arcs on the tree as
868 possible, then on the second pass fill in the rest of the tree.
869 Note that the spanning tree is considered undirected, so that as many
870 must-split arcs as possible can be put on it.
871
872 Fallthrough arcs which are crowded should not be chosen on the first
873 pass, since they do not require creating a new basic block. These
874 arcs will have fall_through set. */
875
876 find_spanning_tree (num_blocks);
877
878 /* Create a .bbg file from which gcov can reconstruct the basic block
879 graph. First output the number of basic blocks, and then for every
880 arc output the source and target basic block numbers.
881 NOTE: The format of this file must be compatible with gcov. */
882
883 if (flag_test_coverage)
884 {
885 int flag_bits;
886
887 __write_long (num_blocks, bbg_file, 4);
888 __write_long (num_arcs, bbg_file, 4);
889
890 for (i = 0; i < num_blocks; i++)
891 {
892 long count = 0;
893 for (arcptr = bb_graph[i].succ; arcptr; arcptr = arcptr->succ_next)
894 count++;
895 __write_long (count, bbg_file, 4);
896
897 for (arcptr = bb_graph[i].succ; arcptr; arcptr = arcptr->succ_next)
898 {
899 flag_bits = 0;
900 if (arcptr->on_tree)
901 flag_bits |= 0x1;
902 if (arcptr->fake)
903 flag_bits |= 0x2;
904 if (arcptr->fall_through)
905 flag_bits |= 0x4;
906
907 __write_long (ARC_TARGET (arcptr), bbg_file, 4);
908 __write_long (flag_bits, bbg_file, 4);
909 }
910 }
911
912 /* Emit a -1 to separate the list of all arcs from the list of
913 loop back edges that follows. */
914 __write_long (-1, bbg_file, 4);
915 }
916
917 /* For each arc not on the spanning tree, add counting code as rtl. */
918
919 if (profile_arc_flag)
920 instrument_arcs (f, num_blocks, dump_file);
921
922 /* Execute the rest only if doing branch probabilities. */
923 if (! flag_branch_probabilities)
924 return;
925
926 /* For each arc not on the spanning tree, set its execution count from
927 the .da file. */
928
929 /* The first count in the .da file is the number of times that the function
930 was entered. This is the exec_count for block zero. */
931
932 num_arcs = 0;
933 for (i = 0; i < num_blocks; i++)
934 for (arcptr = bb_graph[i].succ; arcptr; arcptr = arcptr->succ_next)
935 if (! arcptr->on_tree)
936 {
937 num_arcs++;
938 if (da_file)
939 {
940 long value;
941 __read_long (&value, da_file, 8);
942 ARC_COUNT (arcptr) = value;
943 }
944 else
945 ARC_COUNT (arcptr) = 0;
946 arcptr->count_valid = 1;
947 bb_graph[i].succ_count--;
948 bb_graph[ARC_TARGET (arcptr)].pred_count--;
949 }
950
951 if (dump_file)
952 fprintf (dump_file, "%d arc counts read\n", num_arcs);
953
954 /* For every block in the file,
955 - if every exit/entrance arc has a known count, then set the block count
956 - if the block count is known, and every exit/entrance arc but one has
957 a known execution count, then set the count of the remaining arc
958
959 As arc counts are set, decrement the succ/pred count, but don't delete
960 the arc, that way we can easily tell when all arcs are known, or only
961 one arc is unknown. */
962
963 /* The order that the basic blocks are iterated through is important.
964 Since the code that finds spanning trees starts with block 0, low numbered
965 arcs are put on the spanning tree in preference to high numbered arcs.
966 Hence, most instrumented arcs are at the end. Graph solving works much
967 faster if we propagate numbers from the end to the start.
968
969 This takes an average of slightly more than 3 passes. */
970
971 changes = 1;
972 passes = 0;
973 while (changes)
974 {
975 passes++;
976 changes = 0;
977
978 for (i = num_blocks - 1; i >= 0; i--)
979 {
980 struct bb_info *binfo = &bb_graph[i];
981 if (! binfo->count_valid)
982 {
983 if (binfo->succ_count == 0)
984 {
985 total = 0;
986 for (arcptr = binfo->succ; arcptr;
987 arcptr = arcptr->succ_next)
988 total += ARC_COUNT (arcptr);
989 binfo->exec_count = total;
990 binfo->count_valid = 1;
991 changes = 1;
992 }
993 else if (binfo->pred_count == 0)
994 {
995 total = 0;
996 for (arcptr = binfo->pred; arcptr;
997 arcptr = arcptr->pred_next)
998 total += ARC_COUNT (arcptr);
999 binfo->exec_count = total;
1000 binfo->count_valid = 1;
1001 changes = 1;
1002 }
1003 }
1004 if (binfo->count_valid)
1005 {
1006 if (binfo->succ_count == 1)
1007 {
1008 total = 0;
1009 /* One of the counts will be invalid, but it is zero,
1010 so adding it in also doesn't hurt. */
1011 for (arcptr = binfo->succ; arcptr;
1012 arcptr = arcptr->succ_next)
1013 total += ARC_COUNT (arcptr);
1014 /* Calculate count for remaining arc by conservation. */
1015 total = binfo->exec_count - total;
1016 /* Search for the invalid arc, and set its count. */
1017 for (arcptr = binfo->succ; arcptr;
1018 arcptr = arcptr->succ_next)
1019 if (! arcptr->count_valid)
1020 break;
1021 if (! arcptr)
1022 abort ();
1023 arcptr->count_valid = 1;
1024 ARC_COUNT (arcptr) = total;
1025 binfo->succ_count--;
1026
1027 bb_graph[ARC_TARGET (arcptr)].pred_count--;
1028 changes = 1;
1029 }
1030 if (binfo->pred_count == 1)
1031 {
1032 total = 0;
1033 /* One of the counts will be invalid, but it is zero,
1034 so adding it in also doesn't hurt. */
1035 for (arcptr = binfo->pred; arcptr;
1036 arcptr = arcptr->pred_next)
1037 total += ARC_COUNT (arcptr);
1038 /* Calculate count for remaining arc by conservation. */
1039 total = binfo->exec_count - total;
1040 /* Search for the invalid arc, and set its count. */
1041 for (arcptr = binfo->pred; arcptr;
1042 arcptr = arcptr->pred_next)
1043 if (! arcptr->count_valid)
1044 break;
1045 if (! arcptr)
1046 abort ();
1047 arcptr->count_valid = 1;
1048 ARC_COUNT (arcptr) = total;
1049 binfo->pred_count--;
1050
1051 bb_graph[ARC_SOURCE (arcptr)].succ_count--;
1052 changes = 1;
1053 }
1054 }
1055 }
1056 }
1057
1058 total_num_passes += passes;
1059 if (dump_file)
1060 fprintf (dump_file, "Graph solving took %d passes.\n\n", passes);
1061
1062 /* If the graph has been correctly solved, every block will have a
1063 succ and pred count of zero. */
1064 for (i = 0; i < num_blocks; i++)
1065 {
1066 struct bb_info *binfo = &bb_graph[i];
1067 if (binfo->succ_count || binfo->pred_count)
1068 abort ();
1069 }
1070
1071 /* For every arc, calculate its branch probability and add a reg_note
1072 to the branch insn to indicate this. */
1073
1074 for (i = 0; i < 20; i++)
1075 hist_br_prob[i] = 0;
1076 num_never_executed = 0;
1077 num_branches = 0;
1078
1079 for (i = 0; i < num_blocks; i++)
1080 {
1081 struct bb_info *binfo = &bb_graph[i];
1082
1083 total = binfo->exec_count;
1084 for (arcptr = binfo->succ; arcptr; arcptr = arcptr->succ_next)
1085 {
1086 if (arcptr->branch_insn)
1087 {
1088 /* This calculates the branch probability as an integer between
1089 0 and REG_BR_PROB_BASE, properly rounded to the nearest
1090 integer. Perform the arithmetic in double to avoid
1091 overflowing the range of ints. */
1092
1093 if (total == 0)
1094 prob = -1;
1095 else
1096 {
1097 rtx pat = PATTERN (arcptr->branch_insn);
1098
1099 prob = (((double)ARC_COUNT (arcptr) * REG_BR_PROB_BASE)
1100 + (total >> 1)) / total;
1101 if (prob < 0 || prob > REG_BR_PROB_BASE)
1102 {
1103 if (dump_file)
1104 fprintf (dump_file, "bad count: prob for %d-%d thought to be %d (forcibly normalized)\n",
1105 ARC_SOURCE (arcptr), ARC_TARGET (arcptr),
1106 prob);
1107
1108 bad_counts = 1;
1109 prob = REG_BR_PROB_BASE / 2;
1110 }
1111
1112 /* Match up probability with JUMP pattern. */
1113
1114 if (GET_CODE (pat) == SET
1115 && GET_CODE (SET_SRC (pat)) == IF_THEN_ELSE)
1116 {
1117 if (ARC_TARGET (arcptr) == ARC_SOURCE (arcptr) + 1)
1118 {
1119 /* A fall through arc should never have a
1120 branch insn. */
1121 abort ();
1122 }
1123 else
1124 {
1125 /* This is the arc for the taken branch. */
1126 if (GET_CODE (XEXP (SET_SRC (pat), 2)) != PC)
1127 prob = REG_BR_PROB_BASE - prob;
1128 }
1129 }
1130 }
1131
1132 if (prob == -1)
1133 num_never_executed++;
1134 else
1135 {
1136 int index = prob * 20 / REG_BR_PROB_BASE;
1137 if (index == 20)
1138 index = 19;
1139 hist_br_prob[index]++;
1140 }
1141 num_branches++;
1142
1143 REG_NOTES (arcptr->branch_insn)
1144 = gen_rtx_EXPR_LIST (REG_BR_PROB, GEN_INT (prob),
1145 REG_NOTES (arcptr->branch_insn));
1146 }
1147 }
1148
1149 /* Add a REG_EXEC_COUNT note to the first instruction of this block. */
1150 if (! binfo->first_insn
1151 || GET_RTX_CLASS (GET_CODE (binfo->first_insn)) != 'i')
1152 {
1153 /* Block 0 is a fake block representing function entry, and does
1154 not have a real first insn. The second last block might not
1155 begin with a real insn. */
1156 if (i == num_blocks - 1)
1157 return_label_execution_count = total;
1158 else if (i != 0 && i != num_blocks - 2)
1159 abort ();
1160 }
1161 else
1162 {
1163 REG_NOTES (binfo->first_insn)
1164 = gen_rtx_EXPR_LIST (REG_EXEC_COUNT, GEN_INT (total),
1165 REG_NOTES (binfo->first_insn));
1166 if (i == num_blocks - 1)
1167 return_label_execution_count = total;
1168 }
1169 }
1170
1171 /* This should never happen. */
1172 if (bad_counts)
1173 warning ("Arc profiling: some arc counts were bad.");
1174
1175 if (dump_file)
1176 {
1177 fprintf (dump_file, "%d branches\n", num_branches);
1178 fprintf (dump_file, "%d branches never executed\n",
1179 num_never_executed);
1180 if (num_branches)
1181 for (i = 0; i < 10; i++)
1182 fprintf (dump_file, "%d%% branches in range %d-%d%%\n",
1183 (hist_br_prob[i]+hist_br_prob[19-i])*100/num_branches,
1184 5*i, 5*i+5);
1185
1186 total_num_branches += num_branches;
1187 total_num_never_executed += num_never_executed;
1188 for (i = 0; i < 20; i++)
1189 total_hist_br_prob[i] += hist_br_prob[i];
1190 }
1191
1192 }
1193 \f
1194 /* Initialize a new arc.
1195 ARCPTR is the empty adj_list this function fills in.
1196 SOURCE is the block number of the source block.
1197 TARGET is the block number of the target block.
1198 INSN is the insn which transfers control from SOURCE to TARGET,
1199 or zero if the transfer is implicit. */
1200
1201 static void
1202 init_arc (arcptr, source, target, insn)
1203 struct adj_list *arcptr;
1204 int source, target;
1205 rtx insn;
1206 {
1207 ARC_TARGET (arcptr) = target;
1208 ARC_SOURCE (arcptr) = source;
1209
1210 ARC_COUNT (arcptr) = 0;
1211 arcptr->count_valid = 0;
1212 arcptr->on_tree = 0;
1213 arcptr->fake = 0;
1214 arcptr->fall_through = 0;
1215 arcptr->branch_insn = insn;
1216
1217 arcptr->succ_next = bb_graph[source].succ;
1218 bb_graph[source].succ = arcptr;
1219 bb_graph[source].succ_count++;
1220
1221 arcptr->pred_next = bb_graph[target].pred;
1222 bb_graph[target].pred = arcptr;
1223 bb_graph[target].pred_count++;
1224 }
1225
1226 /* This function searches all of the arcs in the program flow graph, and puts
1227 as many bad arcs as possible onto the spanning tree. Bad arcs include
1228 fake arcs (needed for setjmp(), longjmp(), exit()) which MUST be on the
1229 spanning tree as they can't be instrumented. Also, arcs which must be
1230 split when instrumented should be part of the spanning tree if possible. */
1231
1232 static void
1233 find_spanning_tree (num_blocks)
1234 int num_blocks;
1235 {
1236 int i;
1237 struct adj_list *arcptr;
1238 struct bb_info *binfo = &bb_graph[0];
1239
1240 /* Fake arcs must be part of the spanning tree, and are always safe to put
1241 on the spanning tree. Fake arcs will either be a successor of node 0,
1242 a predecessor of the last node, or from the last node to node 0. */
1243
1244 for (arcptr = bb_graph[0].succ; arcptr; arcptr = arcptr->succ_next)
1245 if (arcptr->fake)
1246 {
1247 /* Adding this arc should never cause a cycle. This is a fatal
1248 error if it would. */
1249 if (bb_graph[ARC_TARGET (arcptr)].on_tree && binfo->on_tree)
1250 abort();
1251 else
1252 {
1253 arcptr->on_tree = 1;
1254 bb_graph[ARC_TARGET (arcptr)].on_tree = 1;
1255 binfo->on_tree = 1;
1256 }
1257 }
1258
1259 binfo = &bb_graph[num_blocks-1];
1260 for (arcptr = binfo->pred; arcptr; arcptr = arcptr->pred_next)
1261 if (arcptr->fake)
1262 {
1263 /* Adding this arc should never cause a cycle. This is a fatal
1264 error if it would. */
1265 if (bb_graph[ARC_SOURCE (arcptr)].on_tree && binfo->on_tree)
1266 abort();
1267 else
1268 {
1269 arcptr->on_tree = 1;
1270 bb_graph[ARC_SOURCE (arcptr)].on_tree = 1;
1271 binfo->on_tree = 1;
1272 }
1273 }
1274 /* The only entrace to node zero is a fake arc. */
1275 bb_graph[0].pred->on_tree = 1;
1276
1277 /* Arcs which are crowded at both the source and target should be put on
1278 the spanning tree if possible, except for fall_throuch arcs which never
1279 require adding a new block even if crowded, add arcs with the same source
1280 and dest which must always be instrumented. */
1281 for (i = 0; i < num_blocks; i++)
1282 {
1283 binfo = &bb_graph[i];
1284
1285 for (arcptr = binfo->succ; arcptr; arcptr = arcptr->succ_next)
1286 if (! ((binfo->succ == arcptr && arcptr->succ_next == 0)
1287 || (bb_graph[ARC_TARGET (arcptr)].pred
1288 && arcptr->pred_next == 0))
1289 && ! arcptr->fall_through
1290 && ARC_TARGET (arcptr) != i)
1291 {
1292 /* This is a crowded arc at both source and target. Try to put
1293 in on the spanning tree. Can do this if either the source or
1294 target block is not yet on the tree. */
1295 if (! bb_graph[ARC_TARGET (arcptr)].on_tree || ! binfo->on_tree)
1296 {
1297 arcptr->on_tree = 1;
1298 bb_graph[ARC_TARGET (arcptr)].on_tree = 1;
1299 binfo->on_tree = 1;
1300 }
1301 }
1302 }
1303
1304 /* Clear all of the basic block on_tree bits, so that we can use them to
1305 create the spanning tree. */
1306 for (i = 0; i < num_blocks; i++)
1307 bb_graph[i].on_tree = 0;
1308
1309 /* Now fill in the spanning tree until every basic block is on it.
1310 Don't put the 0 to 1 fall through arc on the tree, since it is
1311 always cheap to instrument, so start filling the tree from node 1. */
1312
1313 for (i = 1; i < num_blocks; i++)
1314 for (arcptr = bb_graph[i].succ; arcptr; arcptr = arcptr->succ_next)
1315 if (! arcptr->on_tree
1316 && ! bb_graph[ARC_TARGET (arcptr)].on_tree)
1317 {
1318 fill_spanning_tree (i);
1319 break;
1320 }
1321 }
1322
1323 /* Add arcs reached from BLOCK to the spanning tree if they are needed and
1324 not already there. */
1325
1326 static void
1327 fill_spanning_tree (block)
1328 int block;
1329 {
1330 struct adj_list *arcptr;
1331
1332 expand_spanning_tree (block);
1333
1334 for (arcptr = bb_graph[block].succ; arcptr; arcptr = arcptr->succ_next)
1335 if (! arcptr->on_tree
1336 && ! bb_graph[ARC_TARGET (arcptr)].on_tree)
1337 {
1338 arcptr->on_tree = 1;
1339 fill_spanning_tree (ARC_TARGET (arcptr));
1340 }
1341 }
1342
1343 /* When first visit a block, must add all blocks that are already connected
1344 to this block via tree arcs to the spanning tree. */
1345
1346 static void
1347 expand_spanning_tree (block)
1348 int block;
1349 {
1350 struct adj_list *arcptr;
1351
1352 bb_graph[block].on_tree = 1;
1353
1354 for (arcptr = bb_graph[block].succ; arcptr; arcptr = arcptr->succ_next)
1355 if (arcptr->on_tree && ! bb_graph[ARC_TARGET (arcptr)].on_tree)
1356 expand_spanning_tree (ARC_TARGET (arcptr));
1357
1358 for (arcptr = bb_graph[block].pred;
1359 arcptr; arcptr = arcptr->pred_next)
1360 if (arcptr->on_tree && ! bb_graph[ARC_SOURCE (arcptr)].on_tree)
1361 expand_spanning_tree (ARC_SOURCE (arcptr));
1362 }
1363 \f
1364 /* Perform file-level initialization for branch-prob processing. */
1365
1366 void
1367 init_branch_prob (filename)
1368 char *filename;
1369 {
1370 long len;
1371 int i;
1372
1373 if (flag_test_coverage)
1374 {
1375 /* Open an output file for the basic block/line number map. */
1376 int len = strlen (filename);
1377 char *data_file = (char *) alloca (len + 4);
1378 strcpy (data_file, filename);
1379 strip_off_ending (data_file, len);
1380 strcat (data_file, ".bb");
1381 if ((bb_file = fopen (data_file, "w")) == 0)
1382 pfatal_with_name (data_file);
1383
1384 /* Open an output file for the program flow graph. */
1385 len = strlen (filename);
1386 bbg_file_name = (char *) alloca (len + 5);
1387 strcpy (bbg_file_name, filename);
1388 strip_off_ending (bbg_file_name, len);
1389 strcat (bbg_file_name, ".bbg");
1390 if ((bbg_file = fopen (bbg_file_name, "w")) == 0)
1391 pfatal_with_name (bbg_file_name);
1392
1393 /* Initialize to zero, to ensure that the first file name will be
1394 written to the .bb file. */
1395 last_bb_file_name = 0;
1396 }
1397
1398 if (flag_branch_probabilities)
1399 {
1400 len = strlen (filename);
1401 da_file_name = (char *) alloca (len + 4);
1402 strcpy (da_file_name, filename);
1403 strip_off_ending (da_file_name, len);
1404 strcat (da_file_name, ".da");
1405 if ((da_file = fopen (da_file_name, "r")) == 0)
1406 warning ("file %s not found, execution counts assumed to be zero.",
1407 da_file_name);
1408
1409 /* The first word in the .da file gives the number of instrumented arcs,
1410 which is not needed for our purposes. */
1411
1412 if (da_file)
1413 __read_long (&len, da_file, 8);
1414 }
1415
1416 if (profile_arc_flag)
1417 init_arc_profiler ();
1418
1419 total_num_blocks = 0;
1420 total_num_arcs = 0;
1421 total_num_arcs_instrumented = 0;
1422 total_num_blocks_created = 0;
1423 total_num_passes = 0;
1424 total_num_times_called = 0;
1425 total_num_branches = 0;
1426 total_num_never_executed = 0;
1427 for (i = 0; i < 20; i++)
1428 total_hist_br_prob[i] = 0;
1429 }
1430
1431 /* Performs file-level cleanup after branch-prob processing
1432 is completed. */
1433
1434 void
1435 end_branch_prob (dump_file)
1436 FILE *dump_file;
1437 {
1438 if (flag_test_coverage)
1439 {
1440 fclose (bb_file);
1441 fclose (bbg_file);
1442 }
1443
1444 if (flag_branch_probabilities)
1445 {
1446 if (da_file)
1447 {
1448 long temp;
1449 /* This seems slightly dangerous, as it presumes the EOF
1450 flag will not be set until an attempt is made to read
1451 past the end of the file. */
1452 if (feof (da_file))
1453 warning (".da file contents exhausted too early\n");
1454 /* Should be at end of file now. */
1455 if (__read_long (&temp, da_file, 8) == 0)
1456 warning (".da file contents not exhausted\n");
1457 fclose (da_file);
1458 }
1459 }
1460
1461 if (dump_file)
1462 {
1463 fprintf (dump_file, "\n");
1464 fprintf (dump_file, "Total number of blocks: %d\n", total_num_blocks);
1465 fprintf (dump_file, "Total number of arcs: %d\n", total_num_arcs);
1466 fprintf (dump_file, "Total number of instrumented arcs: %d\n",
1467 total_num_arcs_instrumented);
1468 fprintf (dump_file, "Total number of blocks created: %d\n",
1469 total_num_blocks_created);
1470 fprintf (dump_file, "Total number of graph solution passes: %d\n",
1471 total_num_passes);
1472 if (total_num_times_called != 0)
1473 fprintf (dump_file, "Average number of graph solution passes: %d\n",
1474 (total_num_passes + (total_num_times_called >> 1))
1475 / total_num_times_called);
1476 fprintf (dump_file, "Total number of branches: %d\n", total_num_branches);
1477 fprintf (dump_file, "Total number of branches never executed: %d\n",
1478 total_num_never_executed);
1479 if (total_num_branches)
1480 {
1481 int i;
1482
1483 for (i = 0; i < 10; i++)
1484 fprintf (dump_file, "%d%% branches in range %d-%d%%\n",
1485 (total_hist_br_prob[i] + total_hist_br_prob[19-i]) * 100
1486 / total_num_branches, 5*i, 5*i+5);
1487 }
1488 }
1489 }
1490 \f
1491 /* The label used by the arc profiling code. */
1492
1493 static rtx profiler_label;
1494
1495 /* Initialize the profiler_label. */
1496
1497 static void
1498 init_arc_profiler ()
1499 {
1500 /* Generate and save a copy of this so it can be shared. */
1501 char *name = xmalloc (20);
1502 ASM_GENERATE_INTERNAL_LABEL (name, "LPBX", 2);
1503 profiler_label = gen_rtx_SYMBOL_REF (Pmode, name);
1504 }
1505
1506 /* Output instructions as RTL to increment the arc execution count. */
1507
1508 static void
1509 output_arc_profiler (arcno, insert_after)
1510 int arcno;
1511 rtx insert_after;
1512 {
1513 rtx profiler_target_addr
1514 = (arcno
1515 ? gen_rtx_CONST (Pmode,
1516 gen_rtx_PLUS (Pmode, profiler_label,
1517 GEN_INT (LONG_TYPE_SIZE / BITS_PER_UNIT * arcno)))
1518 : profiler_label);
1519 enum machine_mode mode = mode_for_size (LONG_TYPE_SIZE, MODE_INT, 0);
1520 rtx profiler_reg = gen_reg_rtx (mode);
1521 rtx address_reg = gen_reg_rtx (Pmode);
1522 rtx mem_ref, add_ref;
1523 rtx sequence;
1524
1525 /* In this case, reload can use explicitly mentioned hard registers for
1526 reloads. It is not safe to output profiling code between a call
1527 and the instruction that copies the result to a pseudo-reg. This
1528 is because reload may allocate one of the profiling code pseudo-regs
1529 to the return value reg, thus clobbering the return value. So we
1530 must check for calls here, and emit the profiling code after the
1531 instruction that uses the return value, if any.
1532
1533 ??? The code here performs the same tests that reload does so hopefully
1534 all the bases are covered. */
1535
1536 if (SMALL_REGISTER_CLASSES
1537 && GET_CODE (insert_after) == CALL_INSN
1538 && (GET_CODE (PATTERN (insert_after)) == SET
1539 || (GET_CODE (PATTERN (insert_after)) == PARALLEL
1540 && GET_CODE (XVECEXP (PATTERN (insert_after), 0, 0)) == SET)))
1541 {
1542 rtx return_reg;
1543 rtx next_insert_after = next_nonnote_insn (insert_after);
1544
1545 /* The first insn after the call may be a stack pop, skip it. */
1546 if (next_insert_after
1547 && GET_CODE (next_insert_after) == INSN
1548 && GET_CODE (PATTERN (next_insert_after)) == SET
1549 && SET_DEST (PATTERN (next_insert_after)) == stack_pointer_rtx)
1550 next_insert_after = next_nonnote_insn (next_insert_after);
1551
1552 if (next_insert_after
1553 && GET_CODE (next_insert_after) == INSN)
1554 {
1555 if (GET_CODE (PATTERN (insert_after)) == SET)
1556 return_reg = SET_DEST (PATTERN (insert_after));
1557 else
1558 return_reg = SET_DEST (XVECEXP (PATTERN (insert_after), 0, 0));
1559
1560 /* Now, NEXT_INSERT_AFTER may be an instruction that uses the
1561 return value. However, it could also be something else,
1562 like a CODE_LABEL, so check that the code is INSN. */
1563 if (next_insert_after != 0
1564 && GET_RTX_CLASS (GET_CODE (next_insert_after)) == 'i'
1565 && reg_referenced_p (return_reg, PATTERN (next_insert_after)))
1566 insert_after = next_insert_after;
1567 }
1568 }
1569
1570 start_sequence ();
1571
1572 emit_move_insn (address_reg, profiler_target_addr);
1573 mem_ref = gen_rtx_MEM (mode, address_reg);
1574 emit_move_insn (profiler_reg, mem_ref);
1575
1576 add_ref = gen_rtx_PLUS (mode, profiler_reg, GEN_INT (1));
1577 emit_move_insn (profiler_reg, add_ref);
1578
1579 /* This is the same rtx as above, but it is not legal to share this rtx. */
1580 mem_ref = gen_rtx_MEM (mode, address_reg);
1581 emit_move_insn (mem_ref, profiler_reg);
1582
1583 sequence = gen_sequence ();
1584 end_sequence ();
1585 emit_insn_after (sequence, insert_after);
1586 }
1587
1588 /* Output code for a constructor that will invoke __bb_init_func, if
1589 this has not already been done. */
1590
1591 void
1592 output_func_start_profiler ()
1593 {
1594 tree fnname, fndecl;
1595 char *name, *cfnname;
1596 rtx table_address;
1597 enum machine_mode mode = mode_for_size (LONG_TYPE_SIZE, MODE_INT, 0);
1598 int save_flag_inline_functions = flag_inline_functions;
1599
1600 /* It's either already been output, or we don't need it because we're
1601 not doing profile-arcs. */
1602 if (! need_func_profiler)
1603 return;
1604
1605 need_func_profiler = 0;
1606
1607 /* Synthesize a constructor function to invoke __bb_init_func with a
1608 pointer to this object file's profile block. */
1609 start_sequence ();
1610
1611 /* Try and make a unique name given the "file function name".
1612
1613 And no, I don't like this either. */
1614
1615 fnname = get_file_function_name ('I');
1616 cfnname = IDENTIFIER_POINTER (fnname);
1617 name = xmalloc (strlen (cfnname) + 5);
1618 sprintf (name, "%sGCOV",cfnname);
1619 fnname = get_identifier (name);
1620 free (name);
1621
1622 fndecl = build_decl (FUNCTION_DECL, fnname,
1623 build_function_type (void_type_node, NULL_TREE));
1624 DECL_EXTERNAL (fndecl) = 0;
1625 TREE_PUBLIC (fndecl) = 1;
1626 DECL_ASSEMBLER_NAME (fndecl) = fnname;
1627 DECL_RESULT (fndecl) = build_decl (RESULT_DECL, NULL_TREE, void_type_node);
1628 current_function_decl = fndecl;
1629 pushlevel (0);
1630 make_function_rtl (fndecl);
1631 init_function_start (fndecl, input_filename, lineno);
1632 expand_function_start (fndecl, 0);
1633
1634 /* Actually generate the code to call __bb_init_func. */
1635 name = xmalloc (20);
1636 ASM_GENERATE_INTERNAL_LABEL (name, "LPBX", 0);
1637 table_address = force_reg (Pmode, gen_rtx_SYMBOL_REF (Pmode, name));
1638 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__bb_init_func"), 0,
1639 mode, 1, table_address, Pmode);
1640
1641 expand_function_end (input_filename, lineno, 0);
1642 poplevel (1, 0, 1);
1643
1644 /* Since fndecl isn't in the list of globals, it would never be emitted
1645 when it's considered to be 'safe' for inlining, so turn off
1646 flag_inline_functions. */
1647 flag_inline_functions = 0;
1648
1649 rest_of_compilation (fndecl);
1650
1651 /* Reset flag_inline_functions to its original value. */
1652 flag_inline_functions = save_flag_inline_functions;
1653
1654 fflush (asm_out_file);
1655 current_function_decl = NULL_TREE;
1656
1657 assemble_constructor (IDENTIFIER_POINTER (DECL_NAME (fndecl)));
1658 }