OSDN Git Service

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