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)
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. */
49 #include "insn-flags.h"
50 #include "insn-config.h"
59 /* One of these is dynamically created whenever we identify an arc in the
67 unsigned int count_valid : 1;
68 unsigned int on_tree : 1;
69 unsigned int fake : 1;
70 unsigned int fall_through : 1;
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. */
85 struct adj_list *succ;
86 struct adj_list *pred;
90 unsigned int count_valid : 1;
91 unsigned int on_tree : 1;
95 /* Indexed by label number, gives the basic block number containing that
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
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));
161 #ifndef LONG_TYPE_SIZE
162 #define LONG_TYPE_SIZE BITS_PER_WORD
165 /* If non-zero, we need to output a constructor to set up the
166 per-object-file data. */
167 static int need_func_profiler = 0;
170 /* Add arc instrumentation code to the entire insn chain.
172 F is the first insn of the chain.
173 NUM_BLOCKS is the number of basic blocks found in F.
174 DUMP_FILE, if nonzero, is an rtl dump file we can write to. */
177 instrument_arcs (f, num_blocks, dump_file)
183 register struct adj_list *arcptr, *backptr;
185 int num_instr_arcs = 0;
188 /* Instrument the program start. */
189 /* Handle block 0 specially, since it will always be instrumented,
190 but it doesn't have a valid first_insn or branch_insn. We must
191 put the instructions before the NOTE_INSN_FUNCTION_BEG note, so
192 that they don't clobber any of the parameters of the current
194 for (insn = f; insn; insn = NEXT_INSN (insn))
195 if (GET_CODE (insn) == NOTE
196 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_BEG)
198 insn = PREV_INSN (insn);
199 need_func_profiler = 1;
200 output_arc_profiler (total_num_arcs_instrumented + num_instr_arcs++, insn);
202 for (i = 1; i < num_blocks; i++)
203 for (arcptr = bb_graph[i].succ; arcptr; arcptr = arcptr->succ_next)
204 if (! arcptr->on_tree)
207 fprintf (dump_file, "Arc %d to %d instrumented\n", i,
208 ARC_TARGET (arcptr));
210 /* Check to see if this arc is the only exit from its source block,
211 or the only entrance to its target block. In either case,
212 we don't need to create a new block to instrument the arc. */
214 if (bb_graph[i].succ == arcptr && arcptr->succ_next == 0)
216 /* Instrument the source block. */
217 output_arc_profiler (total_num_arcs_instrumented
219 PREV_INSN (bb_graph[i].first_insn));
221 else if (arcptr == bb_graph[ARC_TARGET (arcptr)].pred
222 && arcptr->pred_next == 0)
224 /* Instrument the target block. */
225 output_arc_profiler (total_num_arcs_instrumented
227 PREV_INSN (bb_graph[ARC_TARGET (arcptr)].first_insn));
229 else if (arcptr->fall_through)
231 /* This is a fall-through; put the instrumentation code after
232 the branch that ends this block. */
234 for (backptr = bb_graph[i].succ; backptr;
235 backptr = backptr->succ_next)
236 if (backptr != arcptr)
239 output_arc_profiler (total_num_arcs_instrumented
241 backptr->branch_insn);
245 /* Must emit a new basic block to hold the arc counting code. */
246 enum rtx_code code = GET_CODE (PATTERN (arcptr->branch_insn));
250 /* Create the new basic block right after the branch.
251 Invert the branch so that it jumps past the end of the new
252 block. The new block will consist of the instrumentation
253 code, and a jump to the target of this arc. */
254 int this_is_simplejump = simplejump_p (arcptr->branch_insn);
255 rtx new_label = gen_label_rtx ();
256 rtx old_label, set_src;
257 rtx after = arcptr->branch_insn;
259 /* Simplejumps can't reach here. */
260 if (this_is_simplejump)
263 /* We can't use JUMP_LABEL, because it won't be set if we
264 are compiling without optimization. */
266 set_src = SET_SRC (single_set (arcptr->branch_insn));
267 if (GET_CODE (set_src) == LABEL_REF)
269 else if (GET_CODE (set_src) != IF_THEN_ELSE)
271 else if (XEXP (set_src, 1) == pc_rtx)
272 old_label = XEXP (XEXP (set_src, 2), 0);
274 old_label = XEXP (XEXP (set_src, 1), 0);
276 /* Set the JUMP_LABEL so that redirect_jump will work. */
277 JUMP_LABEL (arcptr->branch_insn) = old_label;
279 /* Add a use for OLD_LABEL that will be needed when we emit
280 the JUMP_INSN below. If we don't do this here,
281 `invert_jump' might delete it for us. We must add two
282 when not optimizing, because the NUSES is zero now,
283 but must be at least two to prevent the label from being
285 LABEL_NUSES (old_label) += 2;
287 /* Emit the insns for the new block in reverse order,
288 since that is most convenient. */
290 if (this_is_simplejump)
292 after = NEXT_INSN (arcptr->branch_insn);
293 if (! redirect_jump (arcptr->branch_insn, new_label))
294 /* Don't know what to do if this branch won't
300 if (! invert_jump (arcptr->branch_insn, new_label))
301 /* Don't know what to do if this branch won't invert. */
304 emit_label_after (new_label, after);
305 LABEL_NUSES (new_label)++;
307 emit_barrier_after (after);
308 emit_jump_insn_after (gen_jump (old_label), after);
309 JUMP_LABEL (NEXT_INSN (after)) = old_label;
311 /* Instrument the source arc. */
312 output_arc_profiler (total_num_arcs_instrumented
315 if (this_is_simplejump)
317 emit_label_after (new_label, after);
318 LABEL_NUSES (new_label)++;
321 else if (code == ADDR_VEC || code == ADDR_DIFF_VEC)
323 /* A table jump. Create a new basic block immediately
324 after the table, by emitting a barrier, a label, a
325 counting note, and a jump to the old label. Put the
326 new label in the table. */
328 rtx new_label = gen_label_rtx ();
329 rtx old_lref, new_lref;
332 /* Must determine the old_label reference, do this
333 by counting the arcs after this one, which will
334 give the index of our label in the table. */
337 for (backptr = arcptr->succ_next; backptr;
338 backptr = backptr->succ_next)
341 old_lref = XVECEXP (PATTERN (arcptr->branch_insn),
342 (code == ADDR_DIFF_VEC), index);
344 /* Emit the insns for the new block in reverse order,
345 since that is most convenient. */
346 emit_jump_insn_after (gen_jump (XEXP (old_lref, 0)),
347 arcptr->branch_insn);
348 JUMP_LABEL (NEXT_INSN (arcptr->branch_insn))
349 = XEXP (old_lref, 0);
351 /* Instrument the source arc. */
352 output_arc_profiler (total_num_arcs_instrumented
354 arcptr->branch_insn);
356 emit_label_after (new_label, arcptr->branch_insn);
357 LABEL_NUSES (NEXT_INSN (arcptr->branch_insn))++;
358 emit_barrier_after (arcptr->branch_insn);
360 /* Fix up the table jump. */
361 new_lref = gen_rtx_LABEL_REF (Pmode, new_label);
362 XVECEXP (PATTERN (arcptr->branch_insn),
363 (code == ADDR_DIFF_VEC), index) = new_lref;
371 "Arc %d to %d needed new basic block\n", i,
372 ARC_TARGET (arcptr));
376 total_num_arcs_instrumented += num_instr_arcs;
377 count_instrumented_arcs = total_num_arcs_instrumented;
379 total_num_blocks_created += num_arcs;
382 fprintf (dump_file, "%d arcs instrumented\n", num_instr_arcs);
383 fprintf (dump_file, "%d extra basic blocks created\n", num_arcs);
387 /* Output STRING to bb_file, surrounded by DELIMITER. */
390 output_gcov_string (string, delimiter)
396 /* Write a delimiter to indicate that a file name follows. */
397 __write_long (delimiter, bb_file, 4);
399 /* Write the string. */
400 temp = strlen (string) + 1;
401 fwrite (string, temp, 1, bb_file);
403 /* Append a few zeros, to align the output to a 4 byte boundary. */
409 c[0] = c[1] = c[2] = c[3] = 0;
410 fwrite (c, sizeof (char), 4 - temp, bb_file);
413 /* Store another delimiter in the .bb file, just to make it easy to find the
414 end of the file name. */
415 __write_long (delimiter, bb_file, 4);
418 /* Return TRUE if this insn must be a tablejump entry insn. This works for
419 the MIPS port, but may give false negatives for some targets. */
422 tablejump_entry_p (insn, label)
425 rtx next = next_active_insn (insn);
426 enum rtx_code code = GET_CODE (PATTERN (next));
428 if (code != ADDR_DIFF_VEC && code != ADDR_VEC)
431 if (PREV_INSN (next) == XEXP (label, 0))
437 /* Instrument and/or analyze program behavior based on program flow graph.
438 In either case, this function builds a flow graph for the function being
439 compiled. The flow graph is stored in BB_GRAPH.
441 When FLAG_PROFILE_ARCS is nonzero, this function instruments the arcs in
442 the flow graph that are needed to reconstruct the dynamic behavior of the
445 When FLAG_BRANCH_PROBABILITIES is nonzero, this function reads auxiliary
446 information from a data file containing arc count information from previous
447 executions of the function being compiled. In this case, the flow graph is
448 annotated with actual execution counts, which are later propagated into the
449 rtl for optimization purposes.
451 Main entry point of this file. */
454 branch_prob (f, dump_file)
459 struct adj_list *arcptr;
460 int num_arcs, changes, passes;
462 int hist_br_prob[20], num_never_executed, num_branches;
463 /* Set to non-zero if we got bad count information. */
466 /* start of a function. */
467 if (flag_test_coverage)
468 output_gcov_string (current_function_name, (long) -2);
470 /* Execute this only if doing arc profiling or branch probabilities. */
471 if (! profile_arc_flag && ! flag_branch_probabilities
472 && ! flag_test_coverage)
475 total_num_times_called++;
477 /* Create an array label_to_bb of ints of size max_label_num. */
478 label_to_bb_size = max_label_num ();
479 label_to_bb = (int *) oballoc (label_to_bb_size * sizeof (int));
480 bzero ((char *) label_to_bb, label_to_bb_size * sizeof (int));
482 /* Scan the insns in the function, count the number of basic blocks
483 present. When a code label is passed, set label_to_bb[label] = bb
486 /* The first block found will be block 1, so that function entry can be
490 register RTX_CODE prev_code = JUMP_INSN;
491 register RTX_CODE code;
494 int block_separator_emitted = 0;
496 ignore_next_note = 0;
498 for (insn = NEXT_INSN (f), i = 0; insn; insn = NEXT_INSN (insn))
500 code = GET_CODE (insn);
504 else if (code == CODE_LABEL)
505 /* This label is part of the next block, but we can't increment
506 block number yet since there might be multiple labels. */
507 label_to_bb[CODE_LABEL_NUMBER (insn)] = i + 1;
508 /* We make NOTE_INSN_SETJMP notes into a block of their own, so that
509 they can be the target of the fake arc for the setjmp call.
510 This avoids creating cycles of fake arcs, which would happen if
511 the block after the setjmp call contained a call insn. */
512 else if ((prev_code == JUMP_INSN || prev_code == CALL_INSN
513 || prev_code == CODE_LABEL || prev_code == BARRIER)
514 && (GET_RTX_CLASS (code) == 'i'
516 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)))
520 /* Emit the block separator if it hasn't already been emitted. */
521 if (flag_test_coverage && ! block_separator_emitted)
523 /* Output a zero to the .bb file to indicate that a new
524 block list is starting. */
525 __write_long (0, bb_file, 4);
527 block_separator_emitted = 0;
529 /* If flag_test_coverage is true, then we must add an entry to the
530 .bb file for every note. */
531 else if (code == NOTE && flag_test_coverage)
533 /* Must ignore the line number notes that immediately follow the
534 end of an inline function to avoid counting it twice. There
535 is a note before the call, and one after the call. */
536 if (NOTE_LINE_NUMBER (insn) == NOTE_REPEATED_LINE_NUMBER)
537 ignore_next_note = 1;
538 else if (NOTE_LINE_NUMBER (insn) > 0)
540 if (ignore_next_note)
541 ignore_next_note = 0;
544 /* Emit a block separator here to ensure that a NOTE
545 immediately following a JUMP_INSN or CALL_INSN will end
546 up in the right basic block list. */
547 if ((prev_code == JUMP_INSN || prev_code == CALL_INSN
548 || prev_code == CODE_LABEL || prev_code == BARRIER)
549 && ! block_separator_emitted)
551 /* Output a zero to the .bb file to indicate that
552 a new block list is starting. */
553 __write_long (0, bb_file, 4);
555 block_separator_emitted = 1;
558 /* If this is a new source file, then output the file's
559 name to the .bb file. */
560 if (! last_bb_file_name
561 || strcmp (NOTE_SOURCE_FILE (insn),
564 if (last_bb_file_name)
565 free (last_bb_file_name);
566 last_bb_file_name = xstrdup (NOTE_SOURCE_FILE (insn));
567 output_gcov_string (NOTE_SOURCE_FILE (insn), (long)-1);
570 /* Output the line number to the .bb file. Must be done
571 after the output_bb_profile_data() call, and after the
572 file name is written, to ensure that it is correctly
574 __write_long (NOTE_LINE_NUMBER (insn), bb_file, 4);
581 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
582 prev_code = CALL_INSN;
585 /* Allocate last `normal' entry for bb_graph. */
587 /* The last insn was a jump, call, or label. In that case we have
588 a block at the end of the function with no insns. */
589 if (prev_code == JUMP_INSN || prev_code == CALL_INSN
590 || prev_code == CODE_LABEL || prev_code == BARRIER)
594 /* Emit the block separator if it hasn't already been emitted. */
595 if (flag_test_coverage && ! block_separator_emitted)
597 /* Output a zero to the .bb file to indicate that a new
598 block list is starting. */
599 __write_long (0, bb_file, 4);
603 /* Create another block to stand for EXIT, and make all return insns, and
604 the last basic block point here. Add one more to account for block
609 total_num_blocks += num_blocks;
611 fprintf (dump_file, "%d basic blocks\n", num_blocks);
613 /* If we are only doing test coverage here, then return now. */
614 if (! profile_arc_flag && ! flag_branch_probabilities)
617 /* Create and initialize the arrays that will hold bb_graph
618 and execution count info. */
620 bb_graph = (struct bb_info *) alloca (num_blocks * sizeof (struct bb_info));
621 bzero ((char *) bb_graph, (sizeof (struct bb_info) * num_blocks));
624 /* Scan the insns again:
625 - at the entry to each basic block, increment the predecessor count
626 (and successor of previous block) if it is a fall through entry,
627 create adj_list entries for this and the previous block
628 - at each jump insn, increment predecessor/successor counts for
629 target/source basic blocks, add this insn to pred/succ lists.
631 This also cannot be broken out as a separate subroutine
632 because it uses `alloca'. */
634 register RTX_CODE prev_code = JUMP_INSN;
635 register RTX_CODE code;
638 int fall_through = 0;
639 struct adj_list *arcptr;
642 /* Block 0 always falls through to block 1. */
644 arcptr = (struct adj_list *) alloca (sizeof (struct adj_list));
645 init_arc (arcptr, 0, 1, 0);
646 arcptr->fall_through = 1;
649 /* Add a fake fall through arc from the last block to block 0, to make the
651 arcptr = (struct adj_list *) alloca (sizeof (struct adj_list));
652 init_arc (arcptr, num_blocks - 1, 0, 0);
656 /* Exit must be one node of the graph, and all exits from the function
657 must point there. When see a return branch, must point the arc to the
660 /* Must start scan with second insn in function as above. */
661 for (insn = NEXT_INSN (f), i = 0; insn; insn = NEXT_INSN (insn))
663 code = GET_CODE (insn);
667 else if (code == CODE_LABEL)
669 /* We make NOTE_INSN_SETJMP notes into a block of their own, so that
670 they can be the target of the fake arc for the setjmp call.
671 This avoids creating cycles of fake arcs, which would happen if
672 the block after the setjmp call ended with a call. */
673 else if ((prev_code == JUMP_INSN || prev_code == CALL_INSN
674 || prev_code == CODE_LABEL || prev_code == BARRIER)
675 && (GET_RTX_CLASS (code) == 'i'
677 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)))
679 /* This is the first insn of the block. */
683 arcptr = (struct adj_list *) alloca (sizeof (struct adj_list));
684 init_arc (arcptr, i - 1, i, 0);
685 arcptr->fall_through = 1;
690 bb_graph[i].first_insn = insn;
692 else if (code == NOTE)
695 if (code == CALL_INSN)
697 /* In the normal case, the call returns, and this is just like
698 a branch fall through. */
701 /* Setjmp may return more times than called, so to make the graph
702 solvable, add a fake arc from the function entrance to the
705 All other functions may return fewer times than called (if
706 a descendent call longjmp or exit), so to make the graph
707 solvable, add a fake arc to the function exit from the
710 Distinguish the cases by checking for a SETJUMP note.
711 A call_insn can be the last ins of a function, so must check
712 to see if next insn actually exists. */
713 arcptr = (struct adj_list *) alloca (sizeof (struct adj_list));
715 && GET_CODE (NEXT_INSN (insn)) == NOTE
716 && NOTE_LINE_NUMBER (NEXT_INSN (insn)) == NOTE_INSN_SETJMP)
717 init_arc (arcptr, 0, i+1, insn);
719 init_arc (arcptr, i, num_blocks-1, insn);
723 else if (code == JUMP_INSN)
725 rtx tem, pattern = PATTERN (insn);
728 /* If running without optimization, then jump label won't be valid,
729 so we must search for the destination label in that case.
730 We have to handle tablejumps and returns specially anyways, so
731 we don't check the JUMP_LABEL at all here. */
733 /* ??? This code should be rewritten. We need a more elegant way
734 to find the LABEL_REF. We need a more elegant way to
735 differentiate tablejump entries from computed gotos.
736 We should perhaps reuse code from flow to compute the CFG
737 instead of trying to compute it here.
739 We can't use current_function_has_computed_jump, because that
740 is calculated later by flow. We can't use computed_jump_p,
741 because that returns true for tablejump entry insns for some
742 targets, e.g. HPPA and MIPS. */
744 if (GET_CODE (pattern) == PARALLEL)
746 /* This assumes that PARALLEL jumps with a USE are
747 tablejump entry jumps. The same assumption can be found
748 in computed_jump_p. */
749 /* Make an arc from this jump to the label of the
750 jump table. This will instrument the number of
751 times the switch statement is executed. */
752 if (GET_CODE (XVECEXP (pattern, 0, 1)) == USE)
754 tem = XEXP (XVECEXP (pattern, 0, 1), 0);
755 if (GET_CODE (tem) != LABEL_REF)
757 dest = label_to_bb[CODE_LABEL_NUMBER (XEXP (tem, 0))];
759 else if (GET_CODE (XVECEXP (pattern, 0, 0)) == SET
760 && SET_DEST (XVECEXP (pattern, 0, 0)) == pc_rtx)
762 tem = SET_SRC (XVECEXP (pattern, 0, 0));
763 if (GET_CODE (tem) == PLUS
764 && GET_CODE (XEXP (tem, 1)) == LABEL_REF)
767 dest = label_to_bb [CODE_LABEL_NUMBER (XEXP (tem, 0))];
773 else if (GET_CODE (pattern) == ADDR_VEC
774 || GET_CODE (pattern) == ADDR_DIFF_VEC)
776 else if (GET_CODE (pattern) == RETURN)
777 dest = num_blocks - 1;
778 else if (GET_CODE (pattern) != SET)
780 else if ((tem = SET_SRC (pattern))
781 && GET_CODE (tem) == LABEL_REF)
782 dest = label_to_bb[CODE_LABEL_NUMBER (XEXP (tem, 0))];
783 /* Recognize HPPA table jump entry. This code is similar to
784 the code above in the PARALLEL case. */
785 else if (GET_CODE (tem) == PLUS
786 && GET_CODE (XEXP (tem, 0)) == MEM
787 && GET_CODE (XEXP (XEXP (tem, 0), 0)) == PLUS
788 && GET_CODE (XEXP (XEXP (XEXP (tem, 0), 0), 0)) == PC
789 && GET_CODE (XEXP (tem, 1)) == LABEL_REF
790 && tablejump_entry_p (insn, XEXP (tem, 1)))
791 dest = label_to_bb[CODE_LABEL_NUMBER (XEXP (XEXP (tem, 1), 0))];
792 /* Recognize the MIPS table jump entry. */
793 else if (GET_CODE (tem) == PLUS
794 && GET_CODE (XEXP (tem, 0)) == REG
795 && GET_CODE (XEXP (tem, 1)) == LABEL_REF
796 && tablejump_entry_p (insn, XEXP (tem, 1)))
797 dest = label_to_bb[CODE_LABEL_NUMBER (XEXP (XEXP (tem, 1), 0))];
802 /* Must be an IF_THEN_ELSE branch. If it isn't, assume it
803 is a computed goto, which aren't supported yet. */
804 if (GET_CODE (tem) != IF_THEN_ELSE)
805 fatal ("-fprofile-arcs does not support computed gotos");
806 if (XEXP (tem, 1) != pc_rtx)
807 label_ref = XEXP (tem, 1);
809 label_ref = XEXP (tem, 2);
810 dest = label_to_bb[CODE_LABEL_NUMBER (XEXP (label_ref, 0))];
815 int diff_vec_p = GET_CODE (tablejump) == ADDR_DIFF_VEC;
816 int len = XVECLEN (tablejump, diff_vec_p);
819 for (k = 0; k < len; k++)
821 rtx tem = XEXP (XVECEXP (tablejump, diff_vec_p, k), 0);
822 dest = label_to_bb[CODE_LABEL_NUMBER (tem)];
824 arcptr = (struct adj_list *) alloca (sizeof(struct adj_list));
825 init_arc (arcptr, i, dest, insn);
832 arcptr = (struct adj_list *) alloca (sizeof (struct adj_list));
833 init_arc (arcptr, i, dest, insn);
838 /* Determine whether or not this jump will fall through.
839 Unconditional jumps and returns are not always followed by
841 pattern = PATTERN (insn);
842 if (GET_CODE (pattern) == PARALLEL
843 || GET_CODE (pattern) == RETURN)
845 else if (GET_CODE (pattern) == ADDR_VEC
846 || GET_CODE (pattern) == ADDR_DIFF_VEC)
847 /* These aren't actually jump insns, but they never fall
852 if (GET_CODE (pattern) != SET || SET_DEST (pattern) != pc_rtx)
854 if (GET_CODE (SET_SRC (pattern)) != IF_THEN_ELSE)
861 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
863 /* Make a fake insn to tag our notes on. */
864 bb_graph[i].first_insn = insn
865 = emit_insn_after (gen_rtx_USE (VOIDmode, stack_pointer_rtx),
867 prev_code = CALL_INSN;
871 /* If the code at the end of the function would give a new block, then
874 if (prev_code == JUMP_INSN || prev_code == CALL_INSN
875 || prev_code == CODE_LABEL || prev_code == BARRIER)
879 arcptr = (struct adj_list *) alloca (sizeof (struct adj_list));
880 init_arc (arcptr, i, i + 1, 0);
881 arcptr->fall_through = 1;
886 /* This may not be a real insn, but that should not cause a problem. */
887 bb_graph[i+1].first_insn = get_last_insn ();
890 /* There is always a fake arc from the last block of the function
891 to the function exit block. */
892 arcptr = (struct adj_list *) alloca (sizeof (struct adj_list));
893 init_arc (arcptr, num_blocks-2, num_blocks-1, 0);
898 total_num_arcs += num_arcs;
900 fprintf (dump_file, "%d arcs\n", num_arcs);
902 /* Create spanning tree from basic block graph, mark each arc that is
903 on the spanning tree. */
905 /* To reduce the instrumentation cost, make two passes over the tree.
906 First, put as many must-split (crowded and fake) arcs on the tree as
907 possible, then on the second pass fill in the rest of the tree.
908 Note that the spanning tree is considered undirected, so that as many
909 must-split arcs as possible can be put on it.
911 Fallthrough arcs which are crowded should not be chosen on the first
912 pass, since they do not require creating a new basic block. These
913 arcs will have fall_through set. */
915 find_spanning_tree (num_blocks);
917 /* Create a .bbg file from which gcov can reconstruct the basic block
918 graph. First output the number of basic blocks, and then for every
919 arc output the source and target basic block numbers.
920 NOTE: The format of this file must be compatible with gcov. */
922 if (flag_test_coverage)
926 __write_long (num_blocks, bbg_file, 4);
927 __write_long (num_arcs, bbg_file, 4);
929 for (i = 0; i < num_blocks; i++)
932 for (arcptr = bb_graph[i].succ; arcptr; arcptr = arcptr->succ_next)
934 __write_long (count, bbg_file, 4);
936 for (arcptr = bb_graph[i].succ; arcptr; arcptr = arcptr->succ_next)
943 if (arcptr->fall_through)
946 __write_long (ARC_TARGET (arcptr), bbg_file, 4);
947 __write_long (flag_bits, bbg_file, 4);
951 /* Emit a -1 to separate the list of all arcs from the list of
952 loop back edges that follows. */
953 __write_long (-1, bbg_file, 4);
956 /* For each arc not on the spanning tree, add counting code as rtl. */
958 if (profile_arc_flag)
960 instrument_arcs (f, num_blocks, dump_file);
961 allocate_reg_info (max_reg_num (), FALSE, FALSE);
964 /* Execute the rest only if doing branch probabilities. */
965 if (! flag_branch_probabilities)
968 /* For each arc not on the spanning tree, set its execution count from
971 /* The first count in the .da file is the number of times that the function
972 was entered. This is the exec_count for block zero. */
975 for (i = 0; i < num_blocks; i++)
976 for (arcptr = bb_graph[i].succ; arcptr; arcptr = arcptr->succ_next)
977 if (! arcptr->on_tree)
983 __read_long (&value, da_file, 8);
984 ARC_COUNT (arcptr) = value;
987 ARC_COUNT (arcptr) = 0;
988 arcptr->count_valid = 1;
989 bb_graph[i].succ_count--;
990 bb_graph[ARC_TARGET (arcptr)].pred_count--;
994 fprintf (dump_file, "%d arc counts read\n", num_arcs);
996 /* For every block in the file,
997 - if every exit/entrance arc has a known count, then set the block count
998 - if the block count is known, and every exit/entrance arc but one has
999 a known execution count, then set the count of the remaining arc
1001 As arc counts are set, decrement the succ/pred count, but don't delete
1002 the arc, that way we can easily tell when all arcs are known, or only
1003 one arc is unknown. */
1005 /* The order that the basic blocks are iterated through is important.
1006 Since the code that finds spanning trees starts with block 0, low numbered
1007 arcs are put on the spanning tree in preference to high numbered arcs.
1008 Hence, most instrumented arcs are at the end. Graph solving works much
1009 faster if we propagate numbers from the end to the start.
1011 This takes an average of slightly more than 3 passes. */
1020 for (i = num_blocks - 1; i >= 0; i--)
1022 struct bb_info *binfo = &bb_graph[i];
1023 if (! binfo->count_valid)
1025 if (binfo->succ_count == 0)
1028 for (arcptr = binfo->succ; arcptr;
1029 arcptr = arcptr->succ_next)
1030 total += ARC_COUNT (arcptr);
1031 binfo->exec_count = total;
1032 binfo->count_valid = 1;
1035 else if (binfo->pred_count == 0)
1038 for (arcptr = binfo->pred; arcptr;
1039 arcptr = arcptr->pred_next)
1040 total += ARC_COUNT (arcptr);
1041 binfo->exec_count = total;
1042 binfo->count_valid = 1;
1046 if (binfo->count_valid)
1048 if (binfo->succ_count == 1)
1051 /* One of the counts will be invalid, but it is zero,
1052 so adding it in also doesn't hurt. */
1053 for (arcptr = binfo->succ; arcptr;
1054 arcptr = arcptr->succ_next)
1055 total += ARC_COUNT (arcptr);
1056 /* Calculate count for remaining arc by conservation. */
1057 total = binfo->exec_count - total;
1058 /* Search for the invalid arc, and set its count. */
1059 for (arcptr = binfo->succ; arcptr;
1060 arcptr = arcptr->succ_next)
1061 if (! arcptr->count_valid)
1065 arcptr->count_valid = 1;
1066 ARC_COUNT (arcptr) = total;
1067 binfo->succ_count--;
1069 bb_graph[ARC_TARGET (arcptr)].pred_count--;
1072 if (binfo->pred_count == 1)
1075 /* One of the counts will be invalid, but it is zero,
1076 so adding it in also doesn't hurt. */
1077 for (arcptr = binfo->pred; arcptr;
1078 arcptr = arcptr->pred_next)
1079 total += ARC_COUNT (arcptr);
1080 /* Calculate count for remaining arc by conservation. */
1081 total = binfo->exec_count - total;
1082 /* Search for the invalid arc, and set its count. */
1083 for (arcptr = binfo->pred; arcptr;
1084 arcptr = arcptr->pred_next)
1085 if (! arcptr->count_valid)
1089 arcptr->count_valid = 1;
1090 ARC_COUNT (arcptr) = total;
1091 binfo->pred_count--;
1093 bb_graph[ARC_SOURCE (arcptr)].succ_count--;
1100 total_num_passes += passes;
1102 fprintf (dump_file, "Graph solving took %d passes.\n\n", passes);
1104 /* If the graph has been correctly solved, every block will have a
1105 succ and pred count of zero. */
1106 for (i = 0; i < num_blocks; i++)
1108 struct bb_info *binfo = &bb_graph[i];
1109 if (binfo->succ_count || binfo->pred_count)
1113 /* For every arc, calculate its branch probability and add a reg_note
1114 to the branch insn to indicate this. */
1116 for (i = 0; i < 20; i++)
1117 hist_br_prob[i] = 0;
1118 num_never_executed = 0;
1121 for (i = 0; i < num_blocks; i++)
1123 struct bb_info *binfo = &bb_graph[i];
1125 total = binfo->exec_count;
1126 for (arcptr = binfo->succ; arcptr; arcptr = arcptr->succ_next)
1128 if (arcptr->branch_insn)
1130 /* This calculates the branch probability as an integer between
1131 0 and REG_BR_PROB_BASE, properly rounded to the nearest
1132 integer. Perform the arithmetic in double to avoid
1133 overflowing the range of ints. */
1139 rtx pat = PATTERN (arcptr->branch_insn);
1141 prob = (((double)ARC_COUNT (arcptr) * REG_BR_PROB_BASE)
1142 + (total >> 1)) / total;
1143 if (prob < 0 || prob > REG_BR_PROB_BASE)
1146 fprintf (dump_file, "bad count: prob for %d-%d thought to be %d (forcibly normalized)\n",
1147 ARC_SOURCE (arcptr), ARC_TARGET (arcptr),
1151 prob = REG_BR_PROB_BASE / 2;
1154 /* Match up probability with JUMP pattern. */
1156 if (GET_CODE (pat) == SET
1157 && GET_CODE (SET_SRC (pat)) == IF_THEN_ELSE)
1159 if (ARC_TARGET (arcptr) == ARC_SOURCE (arcptr) + 1)
1161 /* A fall through arc should never have a
1167 /* This is the arc for the taken branch. */
1168 if (GET_CODE (XEXP (SET_SRC (pat), 2)) != PC)
1169 prob = REG_BR_PROB_BASE - prob;
1175 num_never_executed++;
1178 int index = prob * 20 / REG_BR_PROB_BASE;
1181 hist_br_prob[index]++;
1185 REG_NOTES (arcptr->branch_insn)
1186 = gen_rtx_EXPR_LIST (REG_BR_PROB, GEN_INT (prob),
1187 REG_NOTES (arcptr->branch_insn));
1191 /* Add a REG_EXEC_COUNT note to the first instruction of this block. */
1192 if (! binfo->first_insn
1193 || GET_RTX_CLASS (GET_CODE (binfo->first_insn)) != 'i')
1195 /* Block 0 is a fake block representing function entry, and does
1196 not have a real first insn. The second last block might not
1197 begin with a real insn. */
1198 if (i == num_blocks - 1)
1199 return_label_execution_count = total;
1200 else if (i != 0 && i != num_blocks - 2)
1205 REG_NOTES (binfo->first_insn)
1206 = gen_rtx_EXPR_LIST (REG_EXEC_COUNT, GEN_INT (total),
1207 REG_NOTES (binfo->first_insn));
1208 if (i == num_blocks - 1)
1209 return_label_execution_count = total;
1213 /* This should never happen. */
1215 warning ("Arc profiling: some arc counts were bad.");
1219 fprintf (dump_file, "%d branches\n", num_branches);
1220 fprintf (dump_file, "%d branches never executed\n",
1221 num_never_executed);
1223 for (i = 0; i < 10; i++)
1224 fprintf (dump_file, "%d%% branches in range %d-%d%%\n",
1225 (hist_br_prob[i]+hist_br_prob[19-i])*100/num_branches,
1228 total_num_branches += num_branches;
1229 total_num_never_executed += num_never_executed;
1230 for (i = 0; i < 20; i++)
1231 total_hist_br_prob[i] += hist_br_prob[i];
1236 /* Initialize a new arc.
1237 ARCPTR is the empty adj_list this function fills in.
1238 SOURCE is the block number of the source block.
1239 TARGET is the block number of the target block.
1240 INSN is the insn which transfers control from SOURCE to TARGET,
1241 or zero if the transfer is implicit. */
1244 init_arc (arcptr, source, target, insn)
1245 struct adj_list *arcptr;
1249 ARC_TARGET (arcptr) = target;
1250 ARC_SOURCE (arcptr) = source;
1252 ARC_COUNT (arcptr) = 0;
1253 arcptr->count_valid = 0;
1254 arcptr->on_tree = 0;
1256 arcptr->fall_through = 0;
1257 arcptr->branch_insn = insn;
1259 arcptr->succ_next = bb_graph[source].succ;
1260 bb_graph[source].succ = arcptr;
1261 bb_graph[source].succ_count++;
1263 arcptr->pred_next = bb_graph[target].pred;
1264 bb_graph[target].pred = arcptr;
1265 bb_graph[target].pred_count++;
1268 /* This function searches all of the arcs in the program flow graph, and puts
1269 as many bad arcs as possible onto the spanning tree. Bad arcs include
1270 fake arcs (needed for setjmp(), longjmp(), exit()) which MUST be on the
1271 spanning tree as they can't be instrumented. Also, arcs which must be
1272 split when instrumented should be part of the spanning tree if possible. */
1275 find_spanning_tree (num_blocks)
1279 struct adj_list *arcptr;
1280 struct bb_info *binfo = &bb_graph[0];
1282 /* Fake arcs must be part of the spanning tree, and are always safe to put
1283 on the spanning tree. Fake arcs will either be a successor of node 0,
1284 a predecessor of the last node, or from the last node to node 0. */
1286 for (arcptr = bb_graph[0].succ; arcptr; arcptr = arcptr->succ_next)
1289 /* Adding this arc should never cause a cycle. This is a fatal
1290 error if it would. */
1291 if (bb_graph[ARC_TARGET (arcptr)].on_tree && binfo->on_tree)
1295 arcptr->on_tree = 1;
1296 bb_graph[ARC_TARGET (arcptr)].on_tree = 1;
1301 binfo = &bb_graph[num_blocks-1];
1302 for (arcptr = binfo->pred; arcptr; arcptr = arcptr->pred_next)
1305 /* Adding this arc should never cause a cycle. This is a fatal
1306 error if it would. */
1307 if (bb_graph[ARC_SOURCE (arcptr)].on_tree && binfo->on_tree)
1311 arcptr->on_tree = 1;
1312 bb_graph[ARC_SOURCE (arcptr)].on_tree = 1;
1316 /* The only entrace to node zero is a fake arc. */
1317 bb_graph[0].pred->on_tree = 1;
1319 /* Arcs which are crowded at both the source and target should be put on
1320 the spanning tree if possible, except for fall_throuch arcs which never
1321 require adding a new block even if crowded, add arcs with the same source
1322 and dest which must always be instrumented. */
1323 for (i = 0; i < num_blocks; i++)
1325 binfo = &bb_graph[i];
1327 for (arcptr = binfo->succ; arcptr; arcptr = arcptr->succ_next)
1328 if (! ((binfo->succ == arcptr && arcptr->succ_next == 0)
1329 || (bb_graph[ARC_TARGET (arcptr)].pred
1330 && arcptr->pred_next == 0))
1331 && ! arcptr->fall_through
1332 && ARC_TARGET (arcptr) != i)
1334 /* This is a crowded arc at both source and target. Try to put
1335 in on the spanning tree. Can do this if either the source or
1336 target block is not yet on the tree. */
1337 if (! bb_graph[ARC_TARGET (arcptr)].on_tree || ! binfo->on_tree)
1339 arcptr->on_tree = 1;
1340 bb_graph[ARC_TARGET (arcptr)].on_tree = 1;
1346 /* Clear all of the basic block on_tree bits, so that we can use them to
1347 create the spanning tree. */
1348 for (i = 0; i < num_blocks; i++)
1349 bb_graph[i].on_tree = 0;
1351 /* Now fill in the spanning tree until every basic block is on it.
1352 Don't put the 0 to 1 fall through arc on the tree, since it is
1353 always cheap to instrument, so start filling the tree from node 1. */
1355 for (i = 1; i < num_blocks; i++)
1356 for (arcptr = bb_graph[i].succ; arcptr; arcptr = arcptr->succ_next)
1357 if (! arcptr->on_tree
1358 && ! bb_graph[ARC_TARGET (arcptr)].on_tree)
1360 fill_spanning_tree (i);
1365 /* Add arcs reached from BLOCK to the spanning tree if they are needed and
1366 not already there. */
1369 fill_spanning_tree (block)
1372 struct adj_list *arcptr;
1374 expand_spanning_tree (block);
1376 for (arcptr = bb_graph[block].succ; arcptr; arcptr = arcptr->succ_next)
1377 if (! arcptr->on_tree
1378 && ! bb_graph[ARC_TARGET (arcptr)].on_tree)
1380 arcptr->on_tree = 1;
1381 fill_spanning_tree (ARC_TARGET (arcptr));
1385 /* When first visit a block, must add all blocks that are already connected
1386 to this block via tree arcs to the spanning tree. */
1389 expand_spanning_tree (block)
1392 struct adj_list *arcptr;
1394 bb_graph[block].on_tree = 1;
1396 for (arcptr = bb_graph[block].succ; arcptr; arcptr = arcptr->succ_next)
1397 if (arcptr->on_tree && ! bb_graph[ARC_TARGET (arcptr)].on_tree)
1398 expand_spanning_tree (ARC_TARGET (arcptr));
1400 for (arcptr = bb_graph[block].pred;
1401 arcptr; arcptr = arcptr->pred_next)
1402 if (arcptr->on_tree && ! bb_graph[ARC_SOURCE (arcptr)].on_tree)
1403 expand_spanning_tree (ARC_SOURCE (arcptr));
1406 /* Perform file-level initialization for branch-prob processing. */
1409 init_branch_prob (filename)
1410 const char *filename;
1415 if (flag_test_coverage)
1417 /* Open an output file for the basic block/line number map. */
1418 int len = strlen (filename);
1419 char *data_file = (char *) alloca (len + 4);
1420 strcpy (data_file, filename);
1421 strip_off_ending (data_file, len);
1422 strcat (data_file, ".bb");
1423 if ((bb_file = fopen (data_file, "w")) == 0)
1424 pfatal_with_name (data_file);
1426 /* Open an output file for the program flow graph. */
1427 len = strlen (filename);
1428 bbg_file_name = (char *) alloca (len + 5);
1429 strcpy (bbg_file_name, filename);
1430 strip_off_ending (bbg_file_name, len);
1431 strcat (bbg_file_name, ".bbg");
1432 if ((bbg_file = fopen (bbg_file_name, "w")) == 0)
1433 pfatal_with_name (bbg_file_name);
1435 /* Initialize to zero, to ensure that the first file name will be
1436 written to the .bb file. */
1437 last_bb_file_name = 0;
1440 if (flag_branch_probabilities)
1442 len = strlen (filename);
1443 da_file_name = (char *) alloca (len + 4);
1444 strcpy (da_file_name, filename);
1445 strip_off_ending (da_file_name, len);
1446 strcat (da_file_name, ".da");
1447 if ((da_file = fopen (da_file_name, "r")) == 0)
1448 warning ("file %s not found, execution counts assumed to be zero.",
1451 /* The first word in the .da file gives the number of instrumented arcs,
1452 which is not needed for our purposes. */
1455 __read_long (&len, da_file, 8);
1458 if (profile_arc_flag)
1459 init_arc_profiler ();
1461 total_num_blocks = 0;
1463 total_num_arcs_instrumented = 0;
1464 total_num_blocks_created = 0;
1465 total_num_passes = 0;
1466 total_num_times_called = 0;
1467 total_num_branches = 0;
1468 total_num_never_executed = 0;
1469 for (i = 0; i < 20; i++)
1470 total_hist_br_prob[i] = 0;
1473 /* Performs file-level cleanup after branch-prob processing
1477 end_branch_prob (dump_file)
1480 if (flag_test_coverage)
1486 if (flag_branch_probabilities)
1491 /* This seems slightly dangerous, as it presumes the EOF
1492 flag will not be set until an attempt is made to read
1493 past the end of the file. */
1495 warning (".da file contents exhausted too early\n");
1496 /* Should be at end of file now. */
1497 if (__read_long (&temp, da_file, 8) == 0)
1498 warning (".da file contents not exhausted\n");
1505 fprintf (dump_file, "\n");
1506 fprintf (dump_file, "Total number of blocks: %d\n", total_num_blocks);
1507 fprintf (dump_file, "Total number of arcs: %d\n", total_num_arcs);
1508 fprintf (dump_file, "Total number of instrumented arcs: %d\n",
1509 total_num_arcs_instrumented);
1510 fprintf (dump_file, "Total number of blocks created: %d\n",
1511 total_num_blocks_created);
1512 fprintf (dump_file, "Total number of graph solution passes: %d\n",
1514 if (total_num_times_called != 0)
1515 fprintf (dump_file, "Average number of graph solution passes: %d\n",
1516 (total_num_passes + (total_num_times_called >> 1))
1517 / total_num_times_called);
1518 fprintf (dump_file, "Total number of branches: %d\n", total_num_branches);
1519 fprintf (dump_file, "Total number of branches never executed: %d\n",
1520 total_num_never_executed);
1521 if (total_num_branches)
1525 for (i = 0; i < 10; i++)
1526 fprintf (dump_file, "%d%% branches in range %d-%d%%\n",
1527 (total_hist_br_prob[i] + total_hist_br_prob[19-i]) * 100
1528 / total_num_branches, 5*i, 5*i+5);
1533 /* The label used by the arc profiling code. */
1535 static rtx profiler_label;
1537 /* Initialize the profiler_label. */
1540 init_arc_profiler ()
1542 /* Generate and save a copy of this so it can be shared. */
1543 char *name = xmalloc (20);
1544 ASM_GENERATE_INTERNAL_LABEL (name, "LPBX", 2);
1545 profiler_label = gen_rtx_SYMBOL_REF (Pmode, name);
1546 ggc_add_rtx_root (&profiler_label, 1);
1549 /* Output instructions as RTL to increment the arc execution count. */
1552 output_arc_profiler (arcno, insert_after)
1556 rtx profiler_target_addr
1558 ? gen_rtx_CONST (Pmode,
1559 gen_rtx_PLUS (Pmode, profiler_label,
1560 GEN_INT (LONG_TYPE_SIZE / BITS_PER_UNIT * arcno)))
1562 enum machine_mode mode = mode_for_size (LONG_TYPE_SIZE, MODE_INT, 0);
1563 rtx profiler_reg = gen_reg_rtx (mode);
1564 rtx address_reg = gen_reg_rtx (Pmode);
1565 rtx mem_ref, add_ref;
1568 /* In this case, reload can use explicitly mentioned hard registers for
1569 reloads. It is not safe to output profiling code between a call
1570 and the instruction that copies the result to a pseudo-reg. This
1571 is because reload may allocate one of the profiling code pseudo-regs
1572 to the return value reg, thus clobbering the return value. So we
1573 must check for calls here, and emit the profiling code after the
1574 instruction that uses the return value, if any.
1576 ??? The code here performs the same tests that reload does so hopefully
1577 all the bases are covered. */
1579 if (SMALL_REGISTER_CLASSES
1580 && GET_CODE (insert_after) == CALL_INSN
1581 && (GET_CODE (PATTERN (insert_after)) == SET
1582 || (GET_CODE (PATTERN (insert_after)) == PARALLEL
1583 && GET_CODE (XVECEXP (PATTERN (insert_after), 0, 0)) == SET)))
1586 rtx next_insert_after = next_nonnote_insn (insert_after);
1588 /* The first insn after the call may be a stack pop, skip it. */
1589 if (next_insert_after
1590 && GET_CODE (next_insert_after) == INSN
1591 && GET_CODE (PATTERN (next_insert_after)) == SET
1592 && SET_DEST (PATTERN (next_insert_after)) == stack_pointer_rtx)
1593 next_insert_after = next_nonnote_insn (next_insert_after);
1595 if (next_insert_after
1596 && GET_CODE (next_insert_after) == INSN)
1598 if (GET_CODE (PATTERN (insert_after)) == SET)
1599 return_reg = SET_DEST (PATTERN (insert_after));
1601 return_reg = SET_DEST (XVECEXP (PATTERN (insert_after), 0, 0));
1603 /* Now, NEXT_INSERT_AFTER may be an instruction that uses the
1604 return value. However, it could also be something else,
1605 like a CODE_LABEL, so check that the code is INSN. */
1606 if (next_insert_after != 0
1607 && GET_RTX_CLASS (GET_CODE (next_insert_after)) == 'i'
1608 && reg_referenced_p (return_reg, PATTERN (next_insert_after)))
1609 insert_after = next_insert_after;
1615 emit_move_insn (address_reg, profiler_target_addr);
1616 mem_ref = gen_rtx_MEM (mode, address_reg);
1617 emit_move_insn (profiler_reg, mem_ref);
1619 add_ref = gen_rtx_PLUS (mode, profiler_reg, GEN_INT (1));
1620 emit_move_insn (profiler_reg, add_ref);
1622 /* This is the same rtx as above, but it is not legal to share this rtx. */
1623 mem_ref = gen_rtx_MEM (mode, address_reg);
1624 emit_move_insn (mem_ref, profiler_reg);
1626 sequence = gen_sequence ();
1628 emit_insn_after (sequence, insert_after);
1631 /* Output code for a constructor that will invoke __bb_init_func, if
1632 this has not already been done. */
1635 output_func_start_profiler ()
1637 tree fnname, fndecl;
1638 char *name, *cfnname;
1640 enum machine_mode mode = mode_for_size (LONG_TYPE_SIZE, MODE_INT, 0);
1641 int save_flag_inline_functions = flag_inline_functions;
1643 /* It's either already been output, or we don't need it because we're
1644 not doing profile-arcs. */
1645 if (! need_func_profiler)
1648 need_func_profiler = 0;
1650 /* Synthesize a constructor function to invoke __bb_init_func with a
1651 pointer to this object file's profile block. */
1653 /* Try and make a unique name given the "file function name".
1655 And no, I don't like this either. */
1657 fnname = get_file_function_name ('I');
1658 cfnname = IDENTIFIER_POINTER (fnname);
1659 name = xmalloc (strlen (cfnname) + 5);
1660 sprintf (name, "%sGCOV",cfnname);
1661 fnname = get_identifier (name);
1664 fndecl = build_decl (FUNCTION_DECL, fnname,
1665 build_function_type (void_type_node, NULL_TREE));
1666 DECL_EXTERNAL (fndecl) = 0;
1667 TREE_PUBLIC (fndecl) = 1;
1668 DECL_ASSEMBLER_NAME (fndecl) = fnname;
1669 DECL_RESULT (fndecl) = build_decl (RESULT_DECL, NULL_TREE, void_type_node);
1671 fndecl = pushdecl (fndecl);
1672 rest_of_decl_compilation (fndecl, 0, 1, 0);
1673 announce_function (fndecl);
1674 current_function_decl = fndecl;
1675 DECL_INITIAL (fndecl) = error_mark_node;
1676 temporary_allocation ();
1678 make_function_rtl (fndecl);
1679 init_function_start (fndecl, input_filename, lineno);
1680 expand_function_start (fndecl, 0);
1682 /* Actually generate the code to call __bb_init_func. */
1683 name = xmalloc (20);
1684 ASM_GENERATE_INTERNAL_LABEL (name, "LPBX", 0);
1685 table_address = force_reg (Pmode, gen_rtx_SYMBOL_REF (Pmode, name));
1686 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__bb_init_func"), 0,
1687 mode, 1, table_address, Pmode);
1689 expand_function_end (input_filename, lineno, 0);
1692 /* Since fndecl isn't in the list of globals, it would never be emitted
1693 when it's considered to be 'safe' for inlining, so turn off
1694 flag_inline_functions. */
1695 flag_inline_functions = 0;
1697 rest_of_compilation (fndecl);
1699 /* Reset flag_inline_functions to its original value. */
1700 flag_inline_functions = save_flag_inline_functions;
1703 fflush (asm_out_file);
1704 current_function_decl = NULL_TREE;
1706 assemble_constructor (IDENTIFIER_POINTER (DECL_NAME (fndecl)));