OSDN Git Service

* lambda-mat.c (lambda_matrix_inverse_hard): Use gcc_assert
[pf3gnuchains/gcc-fork.git] / gcc / predict.c
1 /* Branch prediction routines for the GNU compiler.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING.  If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.  */
20
21 /* References:
22
23    [1] "Branch Prediction for Free"
24        Ball and Larus; PLDI '93.
25    [2] "Static Branch Frequency and Program Profile Analysis"
26        Wu and Larus; MICRO-27.
27    [3] "Corpus-based Static Branch Prediction"
28        Calder, Grunwald, Lindsay, Martin, Mozer, and Zorn; PLDI '95.  */
29
30
31 #include "config.h"
32 #include "system.h"
33 #include "coretypes.h"
34 #include "tm.h"
35 #include "tree.h"
36 #include "rtl.h"
37 #include "tm_p.h"
38 #include "hard-reg-set.h"
39 #include "basic-block.h"
40 #include "insn-config.h"
41 #include "regs.h"
42 #include "flags.h"
43 #include "output.h"
44 #include "function.h"
45 #include "except.h"
46 #include "toplev.h"
47 #include "recog.h"
48 #include "expr.h"
49 #include "predict.h"
50 #include "coverage.h"
51 #include "sreal.h"
52 #include "params.h"
53 #include "target.h"
54 #include "loop.h"
55 #include "cfgloop.h"
56 #include "tree-flow.h"
57 #include "ggc.h"
58 #include "tree-dump.h"
59 #include "tree-pass.h"
60 #include "timevar.h"
61
62 /* real constants: 0, 1, 1-1/REG_BR_PROB_BASE, REG_BR_PROB_BASE,
63                    1/REG_BR_PROB_BASE, 0.5, BB_FREQ_MAX.  */
64 static sreal real_zero, real_one, real_almost_one, real_br_prob_base,
65              real_inv_br_prob_base, real_one_half, real_bb_freq_max;
66
67 /* Random guesstimation given names.  */
68 #define PROB_VERY_UNLIKELY      (REG_BR_PROB_BASE / 10 - 1)
69 #define PROB_EVEN               (REG_BR_PROB_BASE / 2)
70 #define PROB_VERY_LIKELY        (REG_BR_PROB_BASE - PROB_VERY_UNLIKELY)
71 #define PROB_ALWAYS             (REG_BR_PROB_BASE)
72
73 static void combine_predictions_for_insn (rtx, basic_block);
74 static void dump_prediction (FILE *, enum br_predictor, int, basic_block, int);
75 static void estimate_loops_at_level (struct loop *loop);
76 static void propagate_freq (struct loop *);
77 static void estimate_bb_frequencies (struct loops *);
78 static int counts_to_freqs (void);
79 static bool last_basic_block_p (basic_block);
80 static void compute_function_frequency (void);
81 static void choose_function_section (void);
82 static bool can_predict_insn_p (rtx);
83
84 /* Information we hold about each branch predictor.
85    Filled using information from predict.def.  */
86
87 struct predictor_info
88 {
89   const char *const name;       /* Name used in the debugging dumps.  */
90   const int hitrate;            /* Expected hitrate used by
91                                    predict_insn_def call.  */
92   const int flags;
93 };
94
95 /* Use given predictor without Dempster-Shaffer theory if it matches
96    using first_match heuristics.  */
97 #define PRED_FLAG_FIRST_MATCH 1
98
99 /* Recompute hitrate in percent to our representation.  */
100
101 #define HITRATE(VAL) ((int) ((VAL) * REG_BR_PROB_BASE + 50) / 100)
102
103 #define DEF_PREDICTOR(ENUM, NAME, HITRATE, FLAGS) {NAME, HITRATE, FLAGS},
104 static const struct predictor_info predictor_info[]= {
105 #include "predict.def"
106
107   /* Upper bound on predictors.  */
108   {NULL, 0, 0}
109 };
110 #undef DEF_PREDICTOR
111
112 /* Return true in case BB can be CPU intensive and should be optimized
113    for maximal performance.  */
114
115 bool
116 maybe_hot_bb_p (basic_block bb)
117 {
118   if (profile_info && flag_branch_probabilities
119       && (bb->count
120           < profile_info->sum_max / PARAM_VALUE (HOT_BB_COUNT_FRACTION)))
121     return false;
122   if (bb->frequency < BB_FREQ_MAX / PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION))
123     return false;
124   return true;
125 }
126
127 /* Return true in case BB is cold and should be optimized for size.  */
128
129 bool
130 probably_cold_bb_p (basic_block bb)
131 {
132   if (profile_info && flag_branch_probabilities
133       && (bb->count
134           < profile_info->sum_max / PARAM_VALUE (HOT_BB_COUNT_FRACTION)))
135     return true;
136   if (bb->frequency < BB_FREQ_MAX / PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION))
137     return true;
138   return false;
139 }
140
141 /* Return true in case BB is probably never executed.  */
142 bool
143 probably_never_executed_bb_p (basic_block bb)
144 {
145   if (profile_info && flag_branch_probabilities)
146     return ((bb->count + profile_info->runs / 2) / profile_info->runs) == 0;
147   return false;
148 }
149
150 /* Return true if the one of outgoing edges is already predicted by
151    PREDICTOR.  */
152
153 bool
154 rtl_predicted_by_p (basic_block bb, enum br_predictor predictor)
155 {
156   rtx note;
157   if (!INSN_P (BB_END (bb)))
158     return false;
159   for (note = REG_NOTES (BB_END (bb)); note; note = XEXP (note, 1))
160     if (REG_NOTE_KIND (note) == REG_BR_PRED
161         && INTVAL (XEXP (XEXP (note, 0), 0)) == (int)predictor)
162       return true;
163   return false;
164 }
165
166 /* Return true if the one of outgoing edges is already predicted by
167    PREDICTOR.  */
168
169 bool
170 tree_predicted_by_p (basic_block bb, enum br_predictor predictor)
171 {
172   struct edge_prediction *i = bb_ann (bb)->predictions;
173   for (i = bb_ann (bb)->predictions; i; i = i->next)
174     if (i->predictor == predictor)
175       return true;
176   return false;
177 }
178
179 void
180 predict_insn (rtx insn, enum br_predictor predictor, int probability)
181 {
182   gcc_assert (any_condjump_p (insn));
183   if (!flag_guess_branch_prob)
184     return;
185
186   REG_NOTES (insn)
187     = gen_rtx_EXPR_LIST (REG_BR_PRED,
188                          gen_rtx_CONCAT (VOIDmode,
189                                          GEN_INT ((int) predictor),
190                                          GEN_INT ((int) probability)),
191                          REG_NOTES (insn));
192 }
193
194 /* Predict insn by given predictor.  */
195
196 void
197 predict_insn_def (rtx insn, enum br_predictor predictor,
198                   enum prediction taken)
199 {
200    int probability = predictor_info[(int) predictor].hitrate;
201
202    if (taken != TAKEN)
203      probability = REG_BR_PROB_BASE - probability;
204
205    predict_insn (insn, predictor, probability);
206 }
207
208 /* Predict edge E with given probability if possible.  */
209
210 void
211 rtl_predict_edge (edge e, enum br_predictor predictor, int probability)
212 {
213   rtx last_insn;
214   last_insn = BB_END (e->src);
215
216   /* We can store the branch prediction information only about
217      conditional jumps.  */
218   if (!any_condjump_p (last_insn))
219     return;
220
221   /* We always store probability of branching.  */
222   if (e->flags & EDGE_FALLTHRU)
223     probability = REG_BR_PROB_BASE - probability;
224
225   predict_insn (last_insn, predictor, probability);
226 }
227
228 /* Predict edge E with the given PROBABILITY.  */
229 void
230 tree_predict_edge (edge e, enum br_predictor predictor, int probability)
231 {
232   struct edge_prediction *i = ggc_alloc (sizeof (struct edge_prediction));
233
234   i->next = bb_ann (e->src)->predictions;
235   bb_ann (e->src)->predictions = i;
236   i->probability = probability;
237   i->predictor = predictor;
238   i->edge = e;
239 }
240
241 /* Return true when we can store prediction on insn INSN.
242    At the moment we represent predictions only on conditional
243    jumps, not at computed jump or other complicated cases.  */
244 static bool
245 can_predict_insn_p (rtx insn)
246 {
247   return (JUMP_P (insn)
248           && any_condjump_p (insn)
249           && BLOCK_FOR_INSN (insn)->succ->succ_next);
250 }
251
252 /* Predict edge E by given predictor if possible.  */
253
254 void
255 predict_edge_def (edge e, enum br_predictor predictor,
256                   enum prediction taken)
257 {
258    int probability = predictor_info[(int) predictor].hitrate;
259
260    if (taken != TAKEN)
261      probability = REG_BR_PROB_BASE - probability;
262
263    predict_edge (e, predictor, probability);
264 }
265
266 /* Invert all branch predictions or probability notes in the INSN.  This needs
267    to be done each time we invert the condition used by the jump.  */
268
269 void
270 invert_br_probabilities (rtx insn)
271 {
272   rtx note;
273
274   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
275     if (REG_NOTE_KIND (note) == REG_BR_PROB)
276       XEXP (note, 0) = GEN_INT (REG_BR_PROB_BASE - INTVAL (XEXP (note, 0)));
277     else if (REG_NOTE_KIND (note) == REG_BR_PRED)
278       XEXP (XEXP (note, 0), 1)
279         = GEN_INT (REG_BR_PROB_BASE - INTVAL (XEXP (XEXP (note, 0), 1)));
280 }
281
282 /* Dump information about the branch prediction to the output file.  */
283
284 static void
285 dump_prediction (FILE *file, enum br_predictor predictor, int probability,
286                  basic_block bb, int used)
287 {
288   edge e = bb->succ;
289
290   if (!file)
291     return;
292
293   while (e && (e->flags & EDGE_FALLTHRU))
294     e = e->succ_next;
295
296   fprintf (file, "  %s heuristics%s: %.1f%%",
297            predictor_info[predictor].name,
298            used ? "" : " (ignored)", probability * 100.0 / REG_BR_PROB_BASE);
299
300   if (bb->count)
301     {
302       fprintf (file, "  exec ");
303       fprintf (file, HOST_WIDEST_INT_PRINT_DEC, bb->count);
304       if (e)
305         {
306           fprintf (file, " hit ");
307           fprintf (file, HOST_WIDEST_INT_PRINT_DEC, e->count);
308           fprintf (file, " (%.1f%%)", e->count * 100.0 / bb->count);
309         }
310     }
311
312   fprintf (file, "\n");
313 }
314
315 /* We can not predict the probabilities of outgoing edges of bb.  Set them
316    evenly and hope for the best.  */
317 static void
318 set_even_probabilities (basic_block bb)
319 {
320   int nedges = 0;
321   edge e;
322
323   for (e = bb->succ; e; e = e->succ_next)
324     if (!(e->flags & (EDGE_EH | EDGE_FAKE)))
325       nedges ++;
326   for (e = bb->succ; e; e = e->succ_next)
327     if (!(e->flags & (EDGE_EH | EDGE_FAKE)))
328       e->probability = (REG_BR_PROB_BASE + nedges / 2) / nedges;
329     else
330       e->probability = 0;
331 }
332
333 /* Combine all REG_BR_PRED notes into single probability and attach REG_BR_PROB
334    note if not already present.  Remove now useless REG_BR_PRED notes.  */
335
336 static void
337 combine_predictions_for_insn (rtx insn, basic_block bb)
338 {
339   rtx prob_note;
340   rtx *pnote;
341   rtx note;
342   int best_probability = PROB_EVEN;
343   int best_predictor = END_PREDICTORS;
344   int combined_probability = REG_BR_PROB_BASE / 2;
345   int d;
346   bool first_match = false;
347   bool found = false;
348
349   if (!can_predict_insn_p (insn))
350     {
351       set_even_probabilities (bb);
352       return;
353     }
354
355   prob_note = find_reg_note (insn, REG_BR_PROB, 0);
356   pnote = &REG_NOTES (insn);
357   if (dump_file)
358     fprintf (dump_file, "Predictions for insn %i bb %i\n", INSN_UID (insn),
359              bb->index);
360
361   /* We implement "first match" heuristics and use probability guessed
362      by predictor with smallest index.  */
363   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
364     if (REG_NOTE_KIND (note) == REG_BR_PRED)
365       {
366         int predictor = INTVAL (XEXP (XEXP (note, 0), 0));
367         int probability = INTVAL (XEXP (XEXP (note, 0), 1));
368
369         found = true;
370         if (best_predictor > predictor)
371           best_probability = probability, best_predictor = predictor;
372
373         d = (combined_probability * probability
374              + (REG_BR_PROB_BASE - combined_probability)
375              * (REG_BR_PROB_BASE - probability));
376
377         /* Use FP math to avoid overflows of 32bit integers.  */
378         if (d == 0)
379           /* If one probability is 0% and one 100%, avoid division by zero.  */
380           combined_probability = REG_BR_PROB_BASE / 2;
381         else
382           combined_probability = (((double) combined_probability) * probability
383                                   * REG_BR_PROB_BASE / d + 0.5);
384       }
385
386   /* Decide which heuristic to use.  In case we didn't match anything,
387      use no_prediction heuristic, in case we did match, use either
388      first match or Dempster-Shaffer theory depending on the flags.  */
389
390   if (predictor_info [best_predictor].flags & PRED_FLAG_FIRST_MATCH)
391     first_match = true;
392
393   if (!found)
394     dump_prediction (dump_file, PRED_NO_PREDICTION,
395                      combined_probability, bb, true);
396   else
397     {
398       dump_prediction (dump_file, PRED_DS_THEORY, combined_probability,
399                        bb, !first_match);
400       dump_prediction (dump_file, PRED_FIRST_MATCH, best_probability,
401                        bb, first_match);
402     }
403
404   if (first_match)
405     combined_probability = best_probability;
406   dump_prediction (dump_file, PRED_COMBINED, combined_probability, bb, true);
407
408   while (*pnote)
409     {
410       if (REG_NOTE_KIND (*pnote) == REG_BR_PRED)
411         {
412           int predictor = INTVAL (XEXP (XEXP (*pnote, 0), 0));
413           int probability = INTVAL (XEXP (XEXP (*pnote, 0), 1));
414
415           dump_prediction (dump_file, predictor, probability, bb,
416                            !first_match || best_predictor == predictor);
417           *pnote = XEXP (*pnote, 1);
418         }
419       else
420         pnote = &XEXP (*pnote, 1);
421     }
422
423   if (!prob_note)
424     {
425       REG_NOTES (insn)
426         = gen_rtx_EXPR_LIST (REG_BR_PROB,
427                              GEN_INT (combined_probability), REG_NOTES (insn));
428
429       /* Save the prediction into CFG in case we are seeing non-degenerated
430          conditional jump.  */
431       if (bb->succ->succ_next)
432         {
433           BRANCH_EDGE (bb)->probability = combined_probability;
434           FALLTHRU_EDGE (bb)->probability
435             = REG_BR_PROB_BASE - combined_probability;
436         }
437     }
438 }
439
440 /* Combine predictions into single probability and store them into CFG.
441    Remove now useless prediction entries.  */
442
443 static void
444 combine_predictions_for_bb (FILE *file, basic_block bb)
445 {
446   int best_probability = PROB_EVEN;
447   int best_predictor = END_PREDICTORS;
448   int combined_probability = REG_BR_PROB_BASE / 2;
449   int d;
450   bool first_match = false;
451   bool found = false;
452   struct edge_prediction *pred;
453   int nedges = 0;
454   edge e, first = NULL, second = NULL;
455
456   for (e = bb->succ; e; e = e->succ_next)
457     if (!(e->flags & (EDGE_EH | EDGE_FAKE)))
458       {
459         nedges ++;
460         if (first && !second)
461           second = e;
462         if (!first)
463           first = e;
464       }
465
466   /* When there is no successor or only one choice, prediction is easy. 
467
468      We are lazy for now and predict only basic blocks with two outgoing
469      edges.  It is possible to predict generic case too, but we have to
470      ignore first match heuristics and do more involved combining.  Implement
471      this later.  */
472   if (nedges != 2)
473     {
474       if (!bb->count)
475         set_even_probabilities (bb);
476       bb_ann (bb)->predictions = NULL;
477       if (file)
478         fprintf (file, "%i edges in bb %i predicted to even probabilities\n",
479                  nedges, bb->index);
480       return;
481     }
482
483   if (file)
484     fprintf (file, "Predictions for bb %i\n", bb->index);
485
486   /* We implement "first match" heuristics and use probability guessed
487      by predictor with smallest index.  */
488   for (pred = bb_ann (bb)->predictions; pred; pred = pred->next)
489     {
490       int predictor = pred->predictor;
491       int probability = pred->probability;
492
493       if (pred->edge != first)
494         probability = REG_BR_PROB_BASE - probability;
495
496       found = true;
497       if (best_predictor > predictor)
498         best_probability = probability, best_predictor = predictor;
499
500       d = (combined_probability * probability
501            + (REG_BR_PROB_BASE - combined_probability)
502            * (REG_BR_PROB_BASE - probability));
503
504       /* Use FP math to avoid overflows of 32bit integers.  */
505       if (d == 0)
506         /* If one probability is 0% and one 100%, avoid division by zero.  */
507         combined_probability = REG_BR_PROB_BASE / 2;
508       else
509         combined_probability = (((double) combined_probability) * probability
510                                 * REG_BR_PROB_BASE / d + 0.5);
511     }
512
513   /* Decide which heuristic to use.  In case we didn't match anything,
514      use no_prediction heuristic, in case we did match, use either
515      first match or Dempster-Shaffer theory depending on the flags.  */
516
517   if (predictor_info [best_predictor].flags & PRED_FLAG_FIRST_MATCH)
518     first_match = true;
519
520   if (!found)
521     dump_prediction (file, PRED_NO_PREDICTION, combined_probability, bb, true);
522   else
523     {
524       dump_prediction (file, PRED_DS_THEORY, combined_probability, bb,
525                        !first_match);
526       dump_prediction (file, PRED_FIRST_MATCH, best_probability, bb,
527                        first_match);
528     }
529
530   if (first_match)
531     combined_probability = best_probability;
532   dump_prediction (file, PRED_COMBINED, combined_probability, bb, true);
533
534   for (pred = bb_ann (bb)->predictions; pred; pred = pred->next)
535     {
536       int predictor = pred->predictor;
537       int probability = pred->probability;
538
539       if (pred->edge != bb->succ)
540         probability = REG_BR_PROB_BASE - probability;
541       dump_prediction (file, predictor, probability, bb,
542                        !first_match || best_predictor == predictor);
543     }
544   bb_ann (bb)->predictions = NULL;
545
546   if (!bb->count)
547     {
548       first->probability = combined_probability;
549       second->probability = REG_BR_PROB_BASE - combined_probability;
550     }
551 }
552
553 /* Predict edge probabilities by exploiting loop structure.
554    When SIMPLELOOPS is set, attempt to count number of iterations by analyzing
555    RTL.  */
556 static void
557 predict_loops (struct loops *loops_info, bool simpleloops)
558 {
559   unsigned i;
560
561   /* Try to predict out blocks in a loop that are not part of a
562      natural loop.  */
563   for (i = 1; i < loops_info->num; i++)
564     {
565       basic_block bb, *bbs;
566       unsigned j;
567       int exits;
568       struct loop *loop = loops_info->parray[i];
569       struct niter_desc desc;
570       unsigned HOST_WIDE_INT niter;
571
572       flow_loop_scan (loop, LOOP_EXIT_EDGES);
573       exits = loop->num_exits;
574
575       if (simpleloops)
576         {
577           iv_analysis_loop_init (loop);
578           find_simple_exit (loop, &desc);
579
580           if (desc.simple_p && desc.const_iter)
581             {
582               int prob;
583               niter = desc.niter + 1;
584               if (niter == 0)        /* We might overflow here.  */
585                 niter = desc.niter;
586
587               prob = (REG_BR_PROB_BASE
588                       - (REG_BR_PROB_BASE + niter /2) / niter);
589               /* Branch prediction algorithm gives 0 frequency for everything
590                  after the end of loop for loop having 0 probability to finish.  */
591               if (prob == REG_BR_PROB_BASE)
592                 prob = REG_BR_PROB_BASE - 1;
593               predict_edge (desc.in_edge, PRED_LOOP_ITERATIONS,
594                             prob);
595             }
596         }
597
598       bbs = get_loop_body (loop);
599
600       for (j = 0; j < loop->num_nodes; j++)
601         {
602           int header_found = 0;
603           edge e;
604
605           bb = bbs[j];
606
607           /* Bypass loop heuristics on continue statement.  These
608              statements construct loops via "non-loop" constructs
609              in the source language and are better to be handled
610              separately.  */
611           if ((simpleloops && !can_predict_insn_p (BB_END (bb)))
612               || predicted_by_p (bb, PRED_CONTINUE))
613             continue;
614
615           /* Loop branch heuristics - predict an edge back to a
616              loop's head as taken.  */
617           for (e = bb->succ; e; e = e->succ_next)
618             if (e->dest == loop->header
619                 && e->src == loop->latch)
620               {
621                 header_found = 1;
622                 predict_edge_def (e, PRED_LOOP_BRANCH, TAKEN);
623               }
624
625           /* Loop exit heuristics - predict an edge exiting the loop if the
626              conditional has no loop header successors as not taken.  */
627           if (!header_found)
628             for (e = bb->succ; e; e = e->succ_next)
629               if (e->dest->index < 0
630                   || !flow_bb_inside_loop_p (loop, e->dest))
631                 predict_edge
632                   (e, PRED_LOOP_EXIT,
633                    (REG_BR_PROB_BASE
634                     - predictor_info [(int) PRED_LOOP_EXIT].hitrate)
635                    / exits);
636         }
637       
638       /* Free basic blocks from get_loop_body.  */
639       free (bbs);
640     }
641 }
642
643 /* Attempt to predict probabilities of BB outgoing edges using local
644    properties.  */
645 static void
646 bb_estimate_probability_locally (basic_block bb)
647 {
648   rtx last_insn = BB_END (bb);
649   rtx cond;
650
651   if (! can_predict_insn_p (last_insn))
652     return;
653   cond = get_condition (last_insn, NULL, false, false);
654   if (! cond)
655     return;
656
657   /* Try "pointer heuristic."
658      A comparison ptr == 0 is predicted as false.
659      Similarly, a comparison ptr1 == ptr2 is predicted as false.  */
660   if (COMPARISON_P (cond)
661       && ((REG_P (XEXP (cond, 0)) && REG_POINTER (XEXP (cond, 0)))
662           || (REG_P (XEXP (cond, 1)) && REG_POINTER (XEXP (cond, 1)))))
663     {
664       if (GET_CODE (cond) == EQ)
665         predict_insn_def (last_insn, PRED_POINTER, NOT_TAKEN);
666       else if (GET_CODE (cond) == NE)
667         predict_insn_def (last_insn, PRED_POINTER, TAKEN);
668     }
669   else
670
671   /* Try "opcode heuristic."
672      EQ tests are usually false and NE tests are usually true. Also,
673      most quantities are positive, so we can make the appropriate guesses
674      about signed comparisons against zero.  */
675     switch (GET_CODE (cond))
676       {
677       case CONST_INT:
678         /* Unconditional branch.  */
679         predict_insn_def (last_insn, PRED_UNCONDITIONAL,
680                           cond == const0_rtx ? NOT_TAKEN : TAKEN);
681         break;
682
683       case EQ:
684       case UNEQ:
685         /* Floating point comparisons appears to behave in a very
686            unpredictable way because of special role of = tests in
687            FP code.  */
688         if (FLOAT_MODE_P (GET_MODE (XEXP (cond, 0))))
689           ;
690         /* Comparisons with 0 are often used for booleans and there is
691            nothing useful to predict about them.  */
692         else if (XEXP (cond, 1) == const0_rtx
693                  || XEXP (cond, 0) == const0_rtx)
694           ;
695         else
696           predict_insn_def (last_insn, PRED_OPCODE_NONEQUAL, NOT_TAKEN);
697         break;
698
699       case NE:
700       case LTGT:
701         /* Floating point comparisons appears to behave in a very
702            unpredictable way because of special role of = tests in
703            FP code.  */
704         if (FLOAT_MODE_P (GET_MODE (XEXP (cond, 0))))
705           ;
706         /* Comparisons with 0 are often used for booleans and there is
707            nothing useful to predict about them.  */
708         else if (XEXP (cond, 1) == const0_rtx
709                  || XEXP (cond, 0) == const0_rtx)
710           ;
711         else
712           predict_insn_def (last_insn, PRED_OPCODE_NONEQUAL, TAKEN);
713         break;
714
715       case ORDERED:
716         predict_insn_def (last_insn, PRED_FPOPCODE, TAKEN);
717         break;
718
719       case UNORDERED:
720         predict_insn_def (last_insn, PRED_FPOPCODE, NOT_TAKEN);
721         break;
722
723       case LE:
724       case LT:
725         if (XEXP (cond, 1) == const0_rtx || XEXP (cond, 1) == const1_rtx
726             || XEXP (cond, 1) == constm1_rtx)
727           predict_insn_def (last_insn, PRED_OPCODE_POSITIVE, NOT_TAKEN);
728         break;
729
730       case GE:
731       case GT:
732         if (XEXP (cond, 1) == const0_rtx || XEXP (cond, 1) == const1_rtx
733             || XEXP (cond, 1) == constm1_rtx)
734           predict_insn_def (last_insn, PRED_OPCODE_POSITIVE, TAKEN);
735         break;
736
737       default:
738         break;
739       }
740 }
741
742 /* Statically estimate the probability that a branch will be taken and produce
743    estimated profile.  When profile feedback is present never executed portions
744    of function gets estimated.  */
745
746 void
747 estimate_probability (struct loops *loops_info)
748 {
749   basic_block bb;
750
751   connect_infinite_loops_to_exit ();
752   calculate_dominance_info (CDI_DOMINATORS);
753   calculate_dominance_info (CDI_POST_DOMINATORS);
754
755   predict_loops (loops_info, true);
756
757   iv_analysis_done ();
758
759   /* Attempt to predict conditional jumps using a number of heuristics.  */
760   FOR_EACH_BB (bb)
761     {
762       rtx last_insn = BB_END (bb);
763       edge e;
764
765       if (! can_predict_insn_p (last_insn))
766         continue;
767
768       for (e = bb->succ; e; e = e->succ_next)
769         {
770           /* Predict early returns to be probable, as we've already taken
771              care for error returns and other are often used for fast paths
772              trought function.  */
773           if ((e->dest == EXIT_BLOCK_PTR
774                || (e->dest->succ && !e->dest->succ->succ_next
775                    && e->dest->succ->dest == EXIT_BLOCK_PTR))
776                && !predicted_by_p (bb, PRED_NULL_RETURN)
777                && !predicted_by_p (bb, PRED_CONST_RETURN)
778                && !predicted_by_p (bb, PRED_NEGATIVE_RETURN)
779                && !last_basic_block_p (e->dest))
780             predict_edge_def (e, PRED_EARLY_RETURN, TAKEN);
781
782           /* Look for block we are guarding (ie we dominate it,
783              but it doesn't postdominate us).  */
784           if (e->dest != EXIT_BLOCK_PTR && e->dest != bb
785               && dominated_by_p (CDI_DOMINATORS, e->dest, e->src)
786               && !dominated_by_p (CDI_POST_DOMINATORS, e->src, e->dest))
787             {
788               rtx insn;
789
790               /* The call heuristic claims that a guarded function call
791                  is improbable.  This is because such calls are often used
792                  to signal exceptional situations such as printing error
793                  messages.  */
794               for (insn = BB_HEAD (e->dest); insn != NEXT_INSN (BB_END (e->dest));
795                    insn = NEXT_INSN (insn))
796                 if (CALL_P (insn)
797                     /* Constant and pure calls are hardly used to signalize
798                        something exceptional.  */
799                     && ! CONST_OR_PURE_CALL_P (insn))
800                   {
801                     predict_edge_def (e, PRED_CALL, NOT_TAKEN);
802                     break;
803                   }
804             }
805         }
806       bb_estimate_probability_locally (bb);
807     }
808
809   /* Attach the combined probability to each conditional jump.  */
810   FOR_EACH_BB (bb)
811     if (JUMP_P (BB_END (bb))
812         && any_condjump_p (BB_END (bb))
813         && bb->succ->succ_next != NULL)
814       combine_predictions_for_insn (BB_END (bb), bb);
815
816   remove_fake_exit_edges ();
817   /* Fill in the probability values in flowgraph based on the REG_BR_PROB
818      notes.  */
819   FOR_EACH_BB (bb)
820     {
821       rtx last_insn = BB_END (bb);
822
823       if (!can_predict_insn_p (last_insn))
824         {
825           /* We can predict only conditional jumps at the moment.
826              Expect each edge to be equally probable.
827              ?? In the future we want to make abnormal edges improbable.  */
828           int nedges = 0;
829           edge e;
830
831           for (e = bb->succ; e; e = e->succ_next)
832             {
833               nedges++;
834               if (e->probability != 0)
835                 break;
836             }
837           if (!e)
838             for (e = bb->succ; e; e = e->succ_next)
839               e->probability = (REG_BR_PROB_BASE + nedges / 2) / nedges;
840         }
841     }
842   estimate_bb_frequencies (loops_info);
843   free_dominance_info (CDI_POST_DOMINATORS);
844   if (profile_status == PROFILE_ABSENT)
845     profile_status = PROFILE_GUESSED;
846 }
847
848 /* Set edge->probability for each successor edge of BB.  */
849 void
850 guess_outgoing_edge_probabilities (basic_block bb)
851 {
852   bb_estimate_probability_locally (bb);
853   combine_predictions_for_insn (BB_END (bb), bb);
854 }
855 \f
856
857 /* Predict using opcode of the last statement in basic block.  */
858 static void
859 tree_predict_by_opcode (basic_block bb)
860 {
861   tree stmt = last_stmt (bb);
862   edge then_edge;
863   tree cond;
864   tree op0;
865   tree type;
866
867   if (!stmt || TREE_CODE (stmt) != COND_EXPR)
868     return;
869   for (then_edge = bb->succ; then_edge; then_edge = then_edge->succ_next)
870     if (then_edge->flags & EDGE_TRUE_VALUE)
871        break;
872   cond = TREE_OPERAND (stmt, 0);
873   if (TREE_CODE_CLASS (TREE_CODE (cond)) != '<')
874     return;
875   op0 = TREE_OPERAND (cond, 0);
876   type = TREE_TYPE (op0);
877   /* Try "pointer heuristic."
878      A comparison ptr == 0 is predicted as false.
879      Similarly, a comparison ptr1 == ptr2 is predicted as false.  */
880   if (POINTER_TYPE_P (type))
881     {
882       if (TREE_CODE (cond) == EQ_EXPR)
883         predict_edge_def (then_edge, PRED_TREE_POINTER, NOT_TAKEN);
884       else if (TREE_CODE (cond) == NE_EXPR)
885         predict_edge_def (then_edge, PRED_TREE_POINTER, TAKEN);
886     }
887   else
888
889   /* Try "opcode heuristic."
890      EQ tests are usually false and NE tests are usually true. Also,
891      most quantities are positive, so we can make the appropriate guesses
892      about signed comparisons against zero.  */
893     switch (TREE_CODE (cond))
894       {
895       case EQ_EXPR:
896       case UNEQ_EXPR:
897         /* Floating point comparisons appears to behave in a very
898            unpredictable way because of special role of = tests in
899            FP code.  */
900         if (FLOAT_TYPE_P (type))
901           ;
902         /* Comparisons with 0 are often used for booleans and there is
903            nothing useful to predict about them.  */
904         else if (integer_zerop (op0)
905                  || integer_zerop (TREE_OPERAND (cond, 1)))
906           ;
907         else
908           predict_edge_def (then_edge, PRED_TREE_OPCODE_NONEQUAL, NOT_TAKEN);
909         break;
910
911       case NE_EXPR:
912       case LTGT_EXPR:
913         /* Floating point comparisons appears to behave in a very
914            unpredictable way because of special role of = tests in
915            FP code.  */
916         if (FLOAT_TYPE_P (type))
917           ;
918         /* Comparisons with 0 are often used for booleans and there is
919            nothing useful to predict about them.  */
920         else if (integer_zerop (op0)
921                  || integer_zerop (TREE_OPERAND (cond, 1)))
922           ;
923         else
924           predict_edge_def (then_edge, PRED_TREE_OPCODE_NONEQUAL, TAKEN);
925         break;
926
927       case ORDERED_EXPR:
928         predict_edge_def (then_edge, PRED_TREE_FPOPCODE, TAKEN);
929         break;
930
931       case UNORDERED_EXPR:
932         predict_edge_def (then_edge, PRED_TREE_FPOPCODE, NOT_TAKEN);
933         break;
934
935       case LE_EXPR:
936       case LT_EXPR:
937         if (integer_zerop (TREE_OPERAND (cond, 1))
938             || integer_onep (TREE_OPERAND (cond, 1))
939             || integer_all_onesp (TREE_OPERAND (cond, 1))
940             || real_zerop (TREE_OPERAND (cond, 1))
941             || real_onep (TREE_OPERAND (cond, 1))
942             || real_minus_onep (TREE_OPERAND (cond, 1)))
943           predict_edge_def (then_edge, PRED_TREE_OPCODE_POSITIVE, NOT_TAKEN);
944         break;
945
946       case GE_EXPR:
947       case GT_EXPR:
948         if (integer_zerop (TREE_OPERAND (cond, 1))
949             || integer_onep (TREE_OPERAND (cond, 1))
950             || integer_all_onesp (TREE_OPERAND (cond, 1))
951             || real_zerop (TREE_OPERAND (cond, 1))
952             || real_onep (TREE_OPERAND (cond, 1))
953             || real_minus_onep (TREE_OPERAND (cond, 1)))
954           predict_edge_def (then_edge, PRED_TREE_OPCODE_POSITIVE, TAKEN);
955         break;
956
957       default:
958         break;
959       }
960 }
961
962 /* Predict branch probabilities and estimate profile of the tree CFG.  */
963 static void
964 tree_estimate_probability (void)
965 {
966   basic_block bb;
967   struct loops loops_info;
968
969   flow_loops_find (&loops_info, LOOP_TREE);
970   if (dump_file && (dump_flags & TDF_DETAILS))
971     flow_loops_dump (&loops_info, dump_file, NULL, 0);
972
973   connect_infinite_loops_to_exit ();
974   calculate_dominance_info (CDI_DOMINATORS);
975   calculate_dominance_info (CDI_POST_DOMINATORS);
976
977   predict_loops (&loops_info, false);
978
979   FOR_EACH_BB (bb)
980     {
981       edge e;
982
983       for (e = bb->succ; e; e = e->succ_next)
984         {
985           /* Predict early returns to be probable, as we've already taken
986              care for error returns and other are often used for fast paths
987              trought function.  */
988           if ((e->dest == EXIT_BLOCK_PTR
989                || (e->dest->succ && !e->dest->succ->succ_next
990                    && e->dest->succ->dest == EXIT_BLOCK_PTR))
991                && !predicted_by_p (bb, PRED_NULL_RETURN)
992                && !predicted_by_p (bb, PRED_CONST_RETURN)
993                && !predicted_by_p (bb, PRED_NEGATIVE_RETURN)
994                && !last_basic_block_p (e->dest))
995             predict_edge_def (e, PRED_EARLY_RETURN, TAKEN);
996
997           /* Look for block we are guarding (ie we dominate it,
998              but it doesn't postdominate us).  */
999           if (e->dest != EXIT_BLOCK_PTR && e->dest != bb
1000               && dominated_by_p (CDI_DOMINATORS, e->dest, e->src)
1001               && !dominated_by_p (CDI_POST_DOMINATORS, e->src, e->dest))
1002             {
1003               block_stmt_iterator bi;
1004
1005               /* The call heuristic claims that a guarded function call
1006                  is improbable.  This is because such calls are often used
1007                  to signal exceptional situations such as printing error
1008                  messages.  */
1009               for (bi = bsi_start (e->dest); !bsi_end_p (bi);
1010                    bsi_next (&bi))
1011                 {
1012                   tree stmt = bsi_stmt (bi);
1013                   if ((TREE_CODE (stmt) == CALL_EXPR
1014                        || (TREE_CODE (stmt) == MODIFY_EXPR
1015                            && TREE_CODE (TREE_OPERAND (stmt, 1)) == CALL_EXPR))
1016                       /* Constant and pure calls are hardly used to signalize
1017                          something exceptional.  */
1018                       && TREE_SIDE_EFFECTS (stmt))
1019                     {
1020                       predict_edge_def (e, PRED_CALL, NOT_TAKEN);
1021                       break;
1022                     }
1023                 }
1024             }
1025         }
1026       tree_predict_by_opcode (bb);
1027     }
1028   FOR_EACH_BB (bb)
1029     combine_predictions_for_bb (dump_file, bb);
1030
1031   estimate_bb_frequencies (&loops_info);
1032   free_dominance_info (CDI_POST_DOMINATORS);
1033   remove_fake_exit_edges ();
1034   flow_loops_free (&loops_info);
1035   if (dump_file && (dump_flags & TDF_DETAILS))
1036     dump_tree_cfg (dump_file, dump_flags);
1037   if (profile_status == PROFILE_ABSENT)
1038     profile_status = PROFILE_GUESSED;
1039 }
1040 \f
1041 /* __builtin_expect dropped tokens into the insn stream describing expected
1042    values of registers.  Generate branch probabilities based off these
1043    values.  */
1044
1045 void
1046 expected_value_to_br_prob (void)
1047 {
1048   rtx insn, cond, ev = NULL_RTX, ev_reg = NULL_RTX;
1049
1050   for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
1051     {
1052       switch (GET_CODE (insn))
1053         {
1054         case NOTE:
1055           /* Look for expected value notes.  */
1056           if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EXPECTED_VALUE)
1057             {
1058               ev = NOTE_EXPECTED_VALUE (insn);
1059               ev_reg = XEXP (ev, 0);
1060               delete_insn (insn);
1061             }
1062           continue;
1063
1064         case CODE_LABEL:
1065           /* Never propagate across labels.  */
1066           ev = NULL_RTX;
1067           continue;
1068
1069         case JUMP_INSN:
1070           /* Look for simple conditional branches.  If we haven't got an
1071              expected value yet, no point going further.  */
1072           if (!JUMP_P (insn) || ev == NULL_RTX
1073               || ! any_condjump_p (insn))
1074             continue;
1075           break;
1076
1077         default:
1078           /* Look for insns that clobber the EV register.  */
1079           if (ev && reg_set_p (ev_reg, insn))
1080             ev = NULL_RTX;
1081           continue;
1082         }
1083
1084       /* Collect the branch condition, hopefully relative to EV_REG.  */
1085       /* ???  At present we'll miss things like
1086                 (expected_value (eq r70 0))
1087                 (set r71 -1)
1088                 (set r80 (lt r70 r71))
1089                 (set pc (if_then_else (ne r80 0) ...))
1090          as canonicalize_condition will render this to us as
1091                 (lt r70, r71)
1092          Could use cselib to try and reduce this further.  */
1093       cond = XEXP (SET_SRC (pc_set (insn)), 0);
1094       cond = canonicalize_condition (insn, cond, 0, NULL, ev_reg,
1095                                      false, false);
1096       if (! cond || XEXP (cond, 0) != ev_reg
1097           || GET_CODE (XEXP (cond, 1)) != CONST_INT)
1098         continue;
1099
1100       /* Substitute and simplify.  Given that the expression we're
1101          building involves two constants, we should wind up with either
1102          true or false.  */
1103       cond = gen_rtx_fmt_ee (GET_CODE (cond), VOIDmode,
1104                              XEXP (ev, 1), XEXP (cond, 1));
1105       cond = simplify_rtx (cond);
1106
1107       /* Turn the condition into a scaled branch probability.  */
1108       gcc_assert (cond == const_true_rtx || cond == const0_rtx);
1109       predict_insn_def (insn, PRED_BUILTIN_EXPECT,
1110                         cond == const_true_rtx ? TAKEN : NOT_TAKEN);
1111     }
1112 }
1113 \f
1114 /* Check whether this is the last basic block of function.  Commonly
1115    there is one extra common cleanup block.  */
1116 static bool
1117 last_basic_block_p (basic_block bb)
1118 {
1119   if (bb == EXIT_BLOCK_PTR)
1120     return false;
1121
1122   return (bb->next_bb == EXIT_BLOCK_PTR
1123           || (bb->next_bb->next_bb == EXIT_BLOCK_PTR
1124               && bb->succ && !bb->succ->succ_next
1125               && bb->succ->dest->next_bb == EXIT_BLOCK_PTR));
1126 }
1127 \f
1128 /* This is used to carry information about basic blocks.  It is
1129    attached to the AUX field of the standard CFG block.  */
1130
1131 typedef struct block_info_def
1132 {
1133   /* Estimated frequency of execution of basic_block.  */
1134   sreal frequency;
1135
1136   /* To keep queue of basic blocks to process.  */
1137   basic_block next;
1138
1139   /* True if block needs to be visited in propagate_freq.  */
1140   unsigned int tovisit:1;
1141
1142   /* Number of predecessors we need to visit first.  */
1143   int npredecessors;
1144 } *block_info;
1145
1146 /* Similar information for edges.  */
1147 typedef struct edge_info_def
1148 {
1149   /* In case edge is an loopback edge, the probability edge will be reached
1150      in case header is.  Estimated number of iterations of the loop can be
1151      then computed as 1 / (1 - back_edge_prob).  */
1152   sreal back_edge_prob;
1153   /* True if the edge is an loopback edge in the natural loop.  */
1154   unsigned int back_edge:1;
1155 } *edge_info;
1156
1157 #define BLOCK_INFO(B)   ((block_info) (B)->aux)
1158 #define EDGE_INFO(E)    ((edge_info) (E)->aux)
1159
1160 /* Helper function for estimate_bb_frequencies.
1161    Propagate the frequencies for LOOP.  */
1162
1163 static void
1164 propagate_freq (struct loop *loop)
1165 {
1166   basic_block head = loop->header;
1167   basic_block bb;
1168   basic_block last;
1169   edge e;
1170   basic_block nextbb;
1171
1172   /* For each basic block we need to visit count number of his predecessors
1173      we need to visit first.  */
1174   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
1175     {
1176       if (BLOCK_INFO (bb)->tovisit)
1177         {
1178           int count = 0;
1179
1180           for (e = bb->pred; e; e = e->pred_next)
1181             if (BLOCK_INFO (e->src)->tovisit && !(e->flags & EDGE_DFS_BACK))
1182               count++;
1183             else if (BLOCK_INFO (e->src)->tovisit
1184                      && dump_file && !EDGE_INFO (e)->back_edge)
1185               fprintf (dump_file,
1186                        "Irreducible region hit, ignoring edge to %i->%i\n",
1187                        e->src->index, bb->index);
1188           BLOCK_INFO (bb)->npredecessors = count;
1189         }
1190     }
1191
1192   memcpy (&BLOCK_INFO (head)->frequency, &real_one, sizeof (real_one));
1193   last = head;
1194   for (bb = head; bb; bb = nextbb)
1195     {
1196       sreal cyclic_probability, frequency;
1197
1198       memcpy (&cyclic_probability, &real_zero, sizeof (real_zero));
1199       memcpy (&frequency, &real_zero, sizeof (real_zero));
1200
1201       nextbb = BLOCK_INFO (bb)->next;
1202       BLOCK_INFO (bb)->next = NULL;
1203
1204       /* Compute frequency of basic block.  */
1205       if (bb != head)
1206         {
1207 #ifdef ENABLE_CHECKING
1208           for (e = bb->pred; e; e = e->pred_next)
1209             gcc_assert (!BLOCK_INFO (e->src)->tovisit
1210                         || (e->flags & EDGE_DFS_BACK));
1211 #endif
1212
1213           for (e = bb->pred; e; e = e->pred_next)
1214             if (EDGE_INFO (e)->back_edge)
1215               {
1216                 sreal_add (&cyclic_probability, &cyclic_probability,
1217                            &EDGE_INFO (e)->back_edge_prob);
1218               }
1219             else if (!(e->flags & EDGE_DFS_BACK))
1220               {
1221                 sreal tmp;
1222
1223                 /*  frequency += (e->probability
1224                                   * BLOCK_INFO (e->src)->frequency /
1225                                   REG_BR_PROB_BASE);  */
1226
1227                 sreal_init (&tmp, e->probability, 0);
1228                 sreal_mul (&tmp, &tmp, &BLOCK_INFO (e->src)->frequency);
1229                 sreal_mul (&tmp, &tmp, &real_inv_br_prob_base);
1230                 sreal_add (&frequency, &frequency, &tmp);
1231               }
1232
1233           if (sreal_compare (&cyclic_probability, &real_zero) == 0)
1234             {
1235               memcpy (&BLOCK_INFO (bb)->frequency, &frequency,
1236                       sizeof (frequency));
1237             }
1238           else
1239             {
1240               if (sreal_compare (&cyclic_probability, &real_almost_one) > 0)
1241                 {
1242                   memcpy (&cyclic_probability, &real_almost_one,
1243                           sizeof (real_almost_one));
1244                 }
1245
1246               /* BLOCK_INFO (bb)->frequency = frequency
1247                                               / (1 - cyclic_probability) */
1248
1249               sreal_sub (&cyclic_probability, &real_one, &cyclic_probability);
1250               sreal_div (&BLOCK_INFO (bb)->frequency,
1251                          &frequency, &cyclic_probability);
1252             }
1253         }
1254
1255       BLOCK_INFO (bb)->tovisit = 0;
1256
1257       /* Compute back edge frequencies.  */
1258       for (e = bb->succ; e; e = e->succ_next)
1259         if (e->dest == head)
1260           {
1261             sreal tmp;
1262
1263             /* EDGE_INFO (e)->back_edge_prob
1264                   = ((e->probability * BLOCK_INFO (bb)->frequency)
1265                      / REG_BR_PROB_BASE); */
1266
1267             sreal_init (&tmp, e->probability, 0);
1268             sreal_mul (&tmp, &tmp, &BLOCK_INFO (bb)->frequency);
1269             sreal_mul (&EDGE_INFO (e)->back_edge_prob,
1270                        &tmp, &real_inv_br_prob_base);
1271           }
1272
1273       /* Propagate to successor blocks.  */
1274       for (e = bb->succ; e; e = e->succ_next)
1275         if (!(e->flags & EDGE_DFS_BACK)
1276             && BLOCK_INFO (e->dest)->npredecessors)
1277           {
1278             BLOCK_INFO (e->dest)->npredecessors--;
1279             if (!BLOCK_INFO (e->dest)->npredecessors)
1280               {
1281                 if (!nextbb)
1282                   nextbb = e->dest;
1283                 else
1284                   BLOCK_INFO (last)->next = e->dest;
1285
1286                 last = e->dest;
1287               }
1288            }
1289     }
1290 }
1291
1292 /* Estimate probabilities of loopback edges in loops at same nest level.  */
1293
1294 static void
1295 estimate_loops_at_level (struct loop *first_loop)
1296 {
1297   struct loop *loop;
1298
1299   for (loop = first_loop; loop; loop = loop->next)
1300     {
1301       edge e;
1302       basic_block *bbs;
1303       unsigned i;
1304
1305       estimate_loops_at_level (loop->inner);
1306
1307       if (loop->latch->succ)  /* Do not do this for dummy function loop.  */
1308         {
1309           /* Find current loop back edge and mark it.  */
1310           e = loop_latch_edge (loop);
1311           EDGE_INFO (e)->back_edge = 1;
1312        }
1313
1314       bbs = get_loop_body (loop);
1315       for (i = 0; i < loop->num_nodes; i++)
1316         BLOCK_INFO (bbs[i])->tovisit = 1;
1317       free (bbs);
1318       propagate_freq (loop);
1319     }
1320 }
1321
1322 /* Convert counts measured by profile driven feedback to frequencies.
1323    Return nonzero iff there was any nonzero execution count.  */
1324
1325 static int
1326 counts_to_freqs (void)
1327 {
1328   gcov_type count_max, true_count_max = 0;
1329   basic_block bb;
1330
1331   FOR_EACH_BB (bb)
1332     true_count_max = MAX (bb->count, true_count_max);
1333
1334   count_max = MAX (true_count_max, 1);
1335   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
1336     bb->frequency = (bb->count * BB_FREQ_MAX + count_max / 2) / count_max;
1337   return true_count_max;
1338 }
1339
1340 /* Return true if function is likely to be expensive, so there is no point to
1341    optimize performance of prologue, epilogue or do inlining at the expense
1342    of code size growth.  THRESHOLD is the limit of number of instructions
1343    function can execute at average to be still considered not expensive.  */
1344
1345 bool
1346 expensive_function_p (int threshold)
1347 {
1348   unsigned int sum = 0;
1349   basic_block bb;
1350   unsigned int limit;
1351
1352   /* We can not compute accurately for large thresholds due to scaled
1353      frequencies.  */
1354   gcc_assert (threshold < BB_FREQ_MAX);
1355
1356   /* Frequencies are out of range.  This either means that function contains
1357      internal loop executing more than BB_FREQ_MAX times or profile feedback
1358      is available and function has not been executed at all.  */
1359   if (ENTRY_BLOCK_PTR->frequency == 0)
1360     return true;
1361
1362   /* Maximally BB_FREQ_MAX^2 so overflow won't happen.  */
1363   limit = ENTRY_BLOCK_PTR->frequency * threshold;
1364   FOR_EACH_BB (bb)
1365     {
1366       rtx insn;
1367
1368       for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb));
1369            insn = NEXT_INSN (insn))
1370         if (active_insn_p (insn))
1371           {
1372             sum += bb->frequency;
1373             if (sum > limit)
1374               return true;
1375         }
1376     }
1377
1378   return false;
1379 }
1380
1381 /* Estimate basic blocks frequency by given branch probabilities.  */
1382
1383 static void
1384 estimate_bb_frequencies (struct loops *loops)
1385 {
1386   basic_block bb;
1387   sreal freq_max;
1388
1389   if (!flag_branch_probabilities || !counts_to_freqs ())
1390     {
1391       static int real_values_initialized = 0;
1392
1393       if (!real_values_initialized)
1394         {
1395           real_values_initialized = 1;
1396           sreal_init (&real_zero, 0, 0);
1397           sreal_init (&real_one, 1, 0);
1398           sreal_init (&real_br_prob_base, REG_BR_PROB_BASE, 0);
1399           sreal_init (&real_bb_freq_max, BB_FREQ_MAX, 0);
1400           sreal_init (&real_one_half, 1, -1);
1401           sreal_div (&real_inv_br_prob_base, &real_one, &real_br_prob_base);
1402           sreal_sub (&real_almost_one, &real_one, &real_inv_br_prob_base);
1403         }
1404
1405       mark_dfs_back_edges ();
1406
1407       ENTRY_BLOCK_PTR->succ->probability = REG_BR_PROB_BASE;
1408
1409       /* Set up block info for each basic block.  */
1410       alloc_aux_for_blocks (sizeof (struct block_info_def));
1411       alloc_aux_for_edges (sizeof (struct edge_info_def));
1412       FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
1413         {
1414           edge e;
1415
1416           BLOCK_INFO (bb)->tovisit = 0;
1417           for (e = bb->succ; e; e = e->succ_next)
1418             {
1419               sreal_init (&EDGE_INFO (e)->back_edge_prob, e->probability, 0);
1420               sreal_mul (&EDGE_INFO (e)->back_edge_prob,
1421                          &EDGE_INFO (e)->back_edge_prob,
1422                          &real_inv_br_prob_base);
1423             }
1424         }
1425
1426       /* First compute probabilities locally for each loop from innermost
1427          to outermost to examine probabilities for back edges.  */
1428       estimate_loops_at_level (loops->tree_root);
1429
1430       memcpy (&freq_max, &real_zero, sizeof (real_zero));
1431       FOR_EACH_BB (bb)
1432         if (sreal_compare (&freq_max, &BLOCK_INFO (bb)->frequency) < 0)
1433           memcpy (&freq_max, &BLOCK_INFO (bb)->frequency, sizeof (freq_max));
1434
1435       sreal_div (&freq_max, &real_bb_freq_max, &freq_max);
1436       FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
1437         {
1438           sreal tmp;
1439
1440           sreal_mul (&tmp, &BLOCK_INFO (bb)->frequency, &freq_max);
1441           sreal_add (&tmp, &tmp, &real_one_half);
1442           bb->frequency = sreal_to_int (&tmp);
1443         }
1444
1445       free_aux_for_blocks ();
1446       free_aux_for_edges ();
1447     }
1448   compute_function_frequency ();
1449   if (flag_reorder_functions)
1450     choose_function_section ();
1451 }
1452
1453 /* Decide whether function is hot, cold or unlikely executed.  */
1454 static void
1455 compute_function_frequency (void)
1456 {
1457   basic_block bb;
1458
1459   if (!profile_info || !flag_branch_probabilities)
1460     return;
1461   cfun->function_frequency = FUNCTION_FREQUENCY_UNLIKELY_EXECUTED;
1462   FOR_EACH_BB (bb)
1463     {
1464       if (maybe_hot_bb_p (bb))
1465         {
1466           cfun->function_frequency = FUNCTION_FREQUENCY_HOT;
1467           return;
1468         }
1469       if (!probably_never_executed_bb_p (bb))
1470         cfun->function_frequency = FUNCTION_FREQUENCY_NORMAL;
1471     }
1472 }
1473
1474 /* Choose appropriate section for the function.  */
1475 static void
1476 choose_function_section (void)
1477 {
1478   if (DECL_SECTION_NAME (current_function_decl)
1479       || !targetm.have_named_sections
1480       /* Theoretically we can split the gnu.linkonce text section too,
1481          but this requires more work as the frequency needs to match
1482          for all generated objects so we need to merge the frequency
1483          of all instances.  For now just never set frequency for these.  */
1484       || DECL_ONE_ONLY (current_function_decl))
1485     return;
1486
1487   /* If we are doing the partitioning optimization, let the optimization
1488      choose the correct section into which to put things.  */
1489
1490   if (flag_reorder_blocks_and_partition)
1491     return;
1492
1493   if (cfun->function_frequency == FUNCTION_FREQUENCY_HOT)
1494     DECL_SECTION_NAME (current_function_decl) =
1495       build_string (strlen (HOT_TEXT_SECTION_NAME), HOT_TEXT_SECTION_NAME);
1496   if (cfun->function_frequency == FUNCTION_FREQUENCY_UNLIKELY_EXECUTED)
1497     DECL_SECTION_NAME (current_function_decl) =
1498       build_string (strlen (UNLIKELY_EXECUTED_TEXT_SECTION_NAME),
1499                     UNLIKELY_EXECUTED_TEXT_SECTION_NAME);
1500 }
1501
1502
1503 struct tree_opt_pass pass_profile = 
1504 {
1505   "profile",                            /* name */
1506   NULL,                                 /* gate */
1507   tree_estimate_probability,            /* execute */
1508   NULL,                                 /* sub */
1509   NULL,                                 /* next */
1510   0,                                    /* static_pass_number */
1511   TV_BRANCH_PROB,                       /* tv_id */
1512   PROP_cfg,                             /* properties_required */
1513   0,                                    /* properties_provided */
1514   0,                                    /* properties_destroyed */
1515   0,                                    /* todo_flags_start */
1516   TODO_ggc_collect | TODO_verify_ssa,                   /* todo_flags_finish */
1517   0                                     /* letter */
1518 };