OSDN Git Service

* gcc.target/i386/cold-attribute-1.c: New testcase.
[pf3gnuchains/gcc-fork.git] / gcc / predict.c
1 /* Branch prediction routines for the GNU compiler.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008
3    Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
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 "cfgloop.h"
55 #include "tree-flow.h"
56 #include "ggc.h"
57 #include "tree-dump.h"
58 #include "tree-pass.h"
59 #include "timevar.h"
60 #include "tree-scalar-evolution.h"
61 #include "cfgloop.h"
62 #include "pointer-set.h"
63
64 /* real constants: 0, 1, 1-1/REG_BR_PROB_BASE, REG_BR_PROB_BASE,
65                    1/REG_BR_PROB_BASE, 0.5, BB_FREQ_MAX.  */
66 static sreal real_zero, real_one, real_almost_one, real_br_prob_base,
67              real_inv_br_prob_base, real_one_half, real_bb_freq_max;
68
69 /* Random guesstimation given names.  
70    PROV_VERY_UNLIKELY should be small enough so basic block predicted
71    by it gets bellow HOT_BB_FREQUENCY_FRANCTION.  */
72 #define PROB_VERY_UNLIKELY      (REG_BR_PROB_BASE / 2000 - 1)
73 #define PROB_EVEN               (REG_BR_PROB_BASE / 2)
74 #define PROB_VERY_LIKELY        (REG_BR_PROB_BASE - PROB_VERY_UNLIKELY)
75 #define PROB_ALWAYS             (REG_BR_PROB_BASE)
76
77 static void combine_predictions_for_insn (rtx, basic_block);
78 static void dump_prediction (FILE *, enum br_predictor, int, basic_block, int);
79 static void predict_paths_leading_to (basic_block, enum br_predictor, enum prediction);
80 static void compute_function_frequency (void);
81 static void choose_function_section (void);
82 static bool can_predict_insn_p (const_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 if frequency FREQ is considered to be hot.  */
113 static bool
114 maybe_hot_frequency_p (int freq)
115 {
116   if (!profile_info || !flag_branch_probabilities)
117     {
118       if (cfun->function_frequency == FUNCTION_FREQUENCY_UNLIKELY_EXECUTED)
119         return false;
120       if (cfun->function_frequency == FUNCTION_FREQUENCY_HOT)
121         return true;
122     }
123   if (profile_status == PROFILE_ABSENT)
124     return true;
125   if (freq < BB_FREQ_MAX / PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION))
126     return false;
127   return true;
128 }
129
130 /* Return true in case BB can be CPU intensive and should be optimized
131    for maximal performance.  */
132
133 bool
134 maybe_hot_bb_p (const_basic_block bb)
135 {
136   if (profile_info && flag_branch_probabilities
137       && (bb->count
138           < profile_info->sum_max / PARAM_VALUE (HOT_BB_COUNT_FRACTION)))
139     return false;
140   return maybe_hot_frequency_p (bb->frequency);
141 }
142
143 /* Return true if the call can be hot.  */
144
145 bool
146 cgraph_maybe_hot_edge_p (struct cgraph_edge *edge)
147 {
148   if (profile_info && flag_branch_probabilities
149       && (edge->count
150           <= profile_info->sum_max / PARAM_VALUE (HOT_BB_COUNT_FRACTION)))
151     return false;
152   if (lookup_attribute ("cold", DECL_ATTRIBUTES (edge->callee->decl))
153       || lookup_attribute ("cold", DECL_ATTRIBUTES (edge->caller->decl)))
154     return false;
155   if (lookup_attribute ("hot", DECL_ATTRIBUTES (edge->caller->decl)))
156     return true;
157   if (flag_guess_branch_prob
158       && edge->frequency < (CGRAPH_FREQ_MAX
159                             / PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION)))
160     return false;
161   return true;
162 }
163
164 /* Return true in case BB can be CPU intensive and should be optimized
165    for maximal performance.  */
166
167 bool
168 maybe_hot_edge_p (edge e)
169 {
170   if (profile_info && flag_branch_probabilities
171       && (e->count
172           < profile_info->sum_max / PARAM_VALUE (HOT_BB_COUNT_FRACTION)))
173     return false;
174   return maybe_hot_frequency_p (EDGE_FREQUENCY (e));
175 }
176
177 /* Return true in case BB is cold and should be optimized for size.  */
178
179 bool
180 probably_cold_bb_p (const_basic_block bb)
181 {
182   if (profile_info && flag_branch_probabilities
183       && (bb->count
184           < profile_info->sum_max / PARAM_VALUE (HOT_BB_COUNT_FRACTION)))
185     return true;
186   if ((!profile_info || !flag_branch_probabilities)
187       && cfun->function_frequency == FUNCTION_FREQUENCY_UNLIKELY_EXECUTED)
188     return true;
189   if (bb->frequency < BB_FREQ_MAX / PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION))
190     return true;
191   return false;
192 }
193
194 /* Return true in case BB is probably never executed.  */
195 bool
196 probably_never_executed_bb_p (const_basic_block bb)
197 {
198   if (profile_info && flag_branch_probabilities)
199     return ((bb->count + profile_info->runs / 2) / profile_info->runs) == 0;
200   if ((!profile_info || !flag_branch_probabilities)
201       && cfun->function_frequency == FUNCTION_FREQUENCY_UNLIKELY_EXECUTED)
202     return true;
203   return false;
204 }
205
206 /* Return true when current function should always be optimized for size.  */
207
208 bool
209 optimize_function_for_size_p (struct function *fun)
210 {
211   return (optimize_size
212           || fun->function_frequency == FUNCTION_FREQUENCY_UNLIKELY_EXECUTED);
213 }
214
215 /* Return true when current function should always be optimized for speed.  */
216
217 bool
218 optimize_function_for_speed_p (struct function *fun)
219 {
220   return !optimize_function_for_size_p (fun);
221 }
222
223 /* Return TRUE when BB should be optimized for size.  */
224
225 bool
226 optimize_bb_for_size_p (const_basic_block bb)
227 {
228   return optimize_function_for_size_p (cfun) || !maybe_hot_bb_p (bb);
229 }
230
231 /* Return TRUE when BB should be optimized for speed.  */
232
233 bool
234 optimize_bb_for_speed_p (const_basic_block bb)
235 {
236   return !optimize_bb_for_size_p (bb);
237 }
238
239 /* Return TRUE when BB should be optimized for size.  */
240
241 bool
242 optimize_edge_for_size_p (edge e)
243 {
244   return optimize_function_for_size_p (cfun) || !maybe_hot_edge_p (e);
245 }
246
247 /* Return TRUE when BB should be optimized for speed.  */
248
249 bool
250 optimize_edge_for_speed_p (edge e)
251 {
252   return !optimize_edge_for_size_p (e);
253 }
254
255 /* Return TRUE when BB should be optimized for size.  */
256
257 bool
258 optimize_insn_for_size_p (void)
259 {
260   return optimize_function_for_size_p (cfun) || !crtl->maybe_hot_insn_p;
261 }
262
263 /* Return TRUE when BB should be optimized for speed.  */
264
265 bool
266 optimize_insn_for_speed_p (void)
267 {
268   return !optimize_insn_for_size_p ();
269 }
270
271 /* Return TRUE when LOOP should be optimized for size.  */
272
273 bool
274 optimize_loop_for_size_p (struct loop *loop)
275 {
276   return optimize_bb_for_size_p (loop->header);
277 }
278
279 /* Return TRUE when LOOP should be optimized for speed.  */
280
281 bool
282 optimize_loop_for_speed_p (struct loop *loop)
283 {
284   return optimize_bb_for_speed_p (loop->header);
285 }
286
287 /* Return TRUE when LOOP nest should be optimized for speed.  */
288
289 bool
290 optimize_loop_nest_for_speed_p (struct loop *loop)
291 {
292   struct loop *l = loop;
293   if (optimize_loop_for_speed_p (loop))
294     return true;
295   l = loop->inner;
296   while (l && l != loop)
297     {
298       if (optimize_loop_for_speed_p (l))
299         return true;
300       if (l->inner)
301         l = l->inner;
302       else if (l->next)
303         l = l->next;
304       else
305         {
306           while (l != loop && !l->next)
307             l = loop_outer (l);
308           if (l != loop)
309             l = l->next;
310         }
311     }
312   return false;
313 }
314
315 /* Return TRUE when LOOP nest should be optimized for size.  */
316
317 bool
318 optimize_loop_nest_for_size_p (struct loop *loop)
319 {
320   return !optimize_loop_nest_for_speed_p (loop);
321 }
322
323 /* Return true when edge E is likely to be well predictable by branch
324    predictor.  */
325
326 bool
327 predictable_edge_p (edge e)
328 {
329   if (profile_status == PROFILE_ABSENT)
330     return false;
331   if ((e->probability
332        <= PARAM_VALUE (PARAM_PREDICTABLE_BRANCH_OUTCOME) * REG_BR_PROB_BASE / 100)
333       || (REG_BR_PROB_BASE - e->probability
334           <= PARAM_VALUE (PARAM_PREDICTABLE_BRANCH_OUTCOME) * REG_BR_PROB_BASE / 100))
335     return true;
336   return false;
337 }
338
339
340 /* Set RTL expansion for BB profile.  */
341
342 void
343 rtl_profile_for_bb (basic_block bb)
344 {
345   crtl->maybe_hot_insn_p = maybe_hot_bb_p (bb);
346 }
347
348 /* Set RTL expansion for edge profile.  */
349
350 void
351 rtl_profile_for_edge (edge e)
352 {
353   crtl->maybe_hot_insn_p = maybe_hot_edge_p (e);
354 }
355
356 /* Set RTL expansion to default mode (i.e. when profile info is not known).  */
357 void
358 default_rtl_profile (void)
359 {
360   crtl->maybe_hot_insn_p = true;
361 }
362
363 /* Return true if the one of outgoing edges is already predicted by
364    PREDICTOR.  */
365
366 bool
367 rtl_predicted_by_p (const_basic_block bb, enum br_predictor predictor)
368 {
369   rtx note;
370   if (!INSN_P (BB_END (bb)))
371     return false;
372   for (note = REG_NOTES (BB_END (bb)); note; note = XEXP (note, 1))
373     if (REG_NOTE_KIND (note) == REG_BR_PRED
374         && INTVAL (XEXP (XEXP (note, 0), 0)) == (int)predictor)
375       return true;
376   return false;
377 }
378
379 /* This map contains for a basic block the list of predictions for the
380    outgoing edges.  */
381
382 static struct pointer_map_t *bb_predictions;
383
384 /* Return true if the one of outgoing edges is already predicted by
385    PREDICTOR.  */
386
387 bool
388 gimple_predicted_by_p (const_basic_block bb, enum br_predictor predictor)
389 {
390   struct edge_prediction *i;
391   void **preds = pointer_map_contains (bb_predictions, bb);
392
393   if (!preds)
394     return false;
395   
396   for (i = (struct edge_prediction *) *preds; i; i = i->ep_next)
397     if (i->ep_predictor == predictor)
398       return true;
399   return false;
400 }
401
402 /* Return true when the probability of edge is reliable.
403   
404    The profile guessing code is good at predicting branch outcome (ie.
405    taken/not taken), that is predicted right slightly over 75% of time.
406    It is however notoriously poor on predicting the probability itself.
407    In general the profile appear a lot flatter (with probabilities closer
408    to 50%) than the reality so it is bad idea to use it to drive optimization
409    such as those disabling dynamic branch prediction for well predictable
410    branches.
411
412    There are two exceptions - edges leading to noreturn edges and edges
413    predicted by number of iterations heuristics are predicted well.  This macro
414    should be able to distinguish those, but at the moment it simply check for
415    noreturn heuristic that is only one giving probability over 99% or bellow
416    1%.  In future we might want to propagate reliability information across the
417    CFG if we find this information useful on multiple places.   */
418 static bool
419 probability_reliable_p (int prob)
420 {
421   return (profile_status == PROFILE_READ
422           || (profile_status == PROFILE_GUESSED
423               && (prob <= HITRATE (1) || prob >= HITRATE (99))));
424 }
425
426 /* Same predicate as above, working on edges.  */
427 bool
428 edge_probability_reliable_p (const_edge e)
429 {
430   return probability_reliable_p (e->probability);
431 }
432
433 /* Same predicate as edge_probability_reliable_p, working on notes.  */
434 bool
435 br_prob_note_reliable_p (const_rtx note)
436 {
437   gcc_assert (REG_NOTE_KIND (note) == REG_BR_PROB);
438   return probability_reliable_p (INTVAL (XEXP (note, 0)));
439 }
440
441 static void
442 predict_insn (rtx insn, enum br_predictor predictor, int probability)
443 {
444   gcc_assert (any_condjump_p (insn));
445   if (!flag_guess_branch_prob)
446     return;
447
448   add_reg_note (insn, REG_BR_PRED,
449                 gen_rtx_CONCAT (VOIDmode,
450                                 GEN_INT ((int) predictor),
451                                 GEN_INT ((int) probability)));
452 }
453
454 /* Predict insn by given predictor.  */
455
456 void
457 predict_insn_def (rtx insn, enum br_predictor predictor,
458                   enum prediction taken)
459 {
460    int probability = predictor_info[(int) predictor].hitrate;
461
462    if (taken != TAKEN)
463      probability = REG_BR_PROB_BASE - probability;
464
465    predict_insn (insn, predictor, probability);
466 }
467
468 /* Predict edge E with given probability if possible.  */
469
470 void
471 rtl_predict_edge (edge e, enum br_predictor predictor, int probability)
472 {
473   rtx last_insn;
474   last_insn = BB_END (e->src);
475
476   /* We can store the branch prediction information only about
477      conditional jumps.  */
478   if (!any_condjump_p (last_insn))
479     return;
480
481   /* We always store probability of branching.  */
482   if (e->flags & EDGE_FALLTHRU)
483     probability = REG_BR_PROB_BASE - probability;
484
485   predict_insn (last_insn, predictor, probability);
486 }
487
488 /* Predict edge E with the given PROBABILITY.  */
489 void
490 gimple_predict_edge (edge e, enum br_predictor predictor, int probability)
491 {
492   gcc_assert (profile_status != PROFILE_GUESSED);
493   if ((e->src != ENTRY_BLOCK_PTR && EDGE_COUNT (e->src->succs) > 1)
494       && flag_guess_branch_prob && optimize)
495     {
496       struct edge_prediction *i = XNEW (struct edge_prediction);
497       void **preds = pointer_map_insert (bb_predictions, e->src);
498
499       i->ep_next = (struct edge_prediction *) *preds;
500       *preds = i;
501       i->ep_probability = probability;
502       i->ep_predictor = predictor;
503       i->ep_edge = e;
504     }
505 }
506
507 /* Remove all predictions on given basic block that are attached
508    to edge E.  */
509 void
510 remove_predictions_associated_with_edge (edge e)
511 {
512   void **preds;
513   
514   if (!bb_predictions)
515     return;
516
517   preds = pointer_map_contains (bb_predictions, e->src);
518
519   if (preds)
520     {
521       struct edge_prediction **prediction = (struct edge_prediction **) preds;
522       struct edge_prediction *next;
523
524       while (*prediction)
525         {
526           if ((*prediction)->ep_edge == e)
527             {
528               next = (*prediction)->ep_next;
529               free (*prediction);
530               *prediction = next;
531             }
532           else
533             prediction = &((*prediction)->ep_next);
534         }
535     }
536 }
537
538 /* Clears the list of predictions stored for BB.  */
539
540 static void
541 clear_bb_predictions (basic_block bb)
542 {
543   void **preds = pointer_map_contains (bb_predictions, bb);
544   struct edge_prediction *pred, *next;
545
546   if (!preds)
547     return;
548
549   for (pred = (struct edge_prediction *) *preds; pred; pred = next)
550     {
551       next = pred->ep_next;
552       free (pred);
553     }
554   *preds = NULL;
555 }
556
557 /* Return true when we can store prediction on insn INSN.
558    At the moment we represent predictions only on conditional
559    jumps, not at computed jump or other complicated cases.  */
560 static bool
561 can_predict_insn_p (const_rtx insn)
562 {
563   return (JUMP_P (insn)
564           && any_condjump_p (insn)
565           && EDGE_COUNT (BLOCK_FOR_INSN (insn)->succs) >= 2);
566 }
567
568 /* Predict edge E by given predictor if possible.  */
569
570 void
571 predict_edge_def (edge e, enum br_predictor predictor,
572                   enum prediction taken)
573 {
574    int probability = predictor_info[(int) predictor].hitrate;
575
576    if (taken != TAKEN)
577      probability = REG_BR_PROB_BASE - probability;
578
579    predict_edge (e, predictor, probability);
580 }
581
582 /* Invert all branch predictions or probability notes in the INSN.  This needs
583    to be done each time we invert the condition used by the jump.  */
584
585 void
586 invert_br_probabilities (rtx insn)
587 {
588   rtx note;
589
590   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
591     if (REG_NOTE_KIND (note) == REG_BR_PROB)
592       XEXP (note, 0) = GEN_INT (REG_BR_PROB_BASE - INTVAL (XEXP (note, 0)));
593     else if (REG_NOTE_KIND (note) == REG_BR_PRED)
594       XEXP (XEXP (note, 0), 1)
595         = GEN_INT (REG_BR_PROB_BASE - INTVAL (XEXP (XEXP (note, 0), 1)));
596 }
597
598 /* Dump information about the branch prediction to the output file.  */
599
600 static void
601 dump_prediction (FILE *file, enum br_predictor predictor, int probability,
602                  basic_block bb, int used)
603 {
604   edge e;
605   edge_iterator ei;
606
607   if (!file)
608     return;
609
610   FOR_EACH_EDGE (e, ei, bb->succs)
611     if (! (e->flags & EDGE_FALLTHRU))
612       break;
613
614   fprintf (file, "  %s heuristics%s: %.1f%%",
615            predictor_info[predictor].name,
616            used ? "" : " (ignored)", probability * 100.0 / REG_BR_PROB_BASE);
617
618   if (bb->count)
619     {
620       fprintf (file, "  exec ");
621       fprintf (file, HOST_WIDEST_INT_PRINT_DEC, bb->count);
622       if (e)
623         {
624           fprintf (file, " hit ");
625           fprintf (file, HOST_WIDEST_INT_PRINT_DEC, e->count);
626           fprintf (file, " (%.1f%%)", e->count * 100.0 / bb->count);
627         }
628     }
629
630   fprintf (file, "\n");
631 }
632
633 /* We can not predict the probabilities of outgoing edges of bb.  Set them
634    evenly and hope for the best.  */
635 static void
636 set_even_probabilities (basic_block bb)
637 {
638   int nedges = 0;
639   edge e;
640   edge_iterator ei;
641
642   FOR_EACH_EDGE (e, ei, bb->succs)
643     if (!(e->flags & (EDGE_EH | EDGE_FAKE)))
644       nedges ++;
645   FOR_EACH_EDGE (e, ei, bb->succs)
646     if (!(e->flags & (EDGE_EH | EDGE_FAKE)))
647       e->probability = (REG_BR_PROB_BASE + nedges / 2) / nedges;
648     else
649       e->probability = 0;
650 }
651
652 /* Combine all REG_BR_PRED notes into single probability and attach REG_BR_PROB
653    note if not already present.  Remove now useless REG_BR_PRED notes.  */
654
655 static void
656 combine_predictions_for_insn (rtx insn, basic_block bb)
657 {
658   rtx prob_note;
659   rtx *pnote;
660   rtx note;
661   int best_probability = PROB_EVEN;
662   int best_predictor = END_PREDICTORS;
663   int combined_probability = REG_BR_PROB_BASE / 2;
664   int d;
665   bool first_match = false;
666   bool found = false;
667
668   if (!can_predict_insn_p (insn))
669     {
670       set_even_probabilities (bb);
671       return;
672     }
673
674   prob_note = find_reg_note (insn, REG_BR_PROB, 0);
675   pnote = &REG_NOTES (insn);
676   if (dump_file)
677     fprintf (dump_file, "Predictions for insn %i bb %i\n", INSN_UID (insn),
678              bb->index);
679
680   /* We implement "first match" heuristics and use probability guessed
681      by predictor with smallest index.  */
682   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
683     if (REG_NOTE_KIND (note) == REG_BR_PRED)
684       {
685         int predictor = INTVAL (XEXP (XEXP (note, 0), 0));
686         int probability = INTVAL (XEXP (XEXP (note, 0), 1));
687
688         found = true;
689         if (best_predictor > predictor)
690           best_probability = probability, best_predictor = predictor;
691
692         d = (combined_probability * probability
693              + (REG_BR_PROB_BASE - combined_probability)
694              * (REG_BR_PROB_BASE - probability));
695
696         /* Use FP math to avoid overflows of 32bit integers.  */
697         if (d == 0)
698           /* If one probability is 0% and one 100%, avoid division by zero.  */
699           combined_probability = REG_BR_PROB_BASE / 2;
700         else
701           combined_probability = (((double) combined_probability) * probability
702                                   * REG_BR_PROB_BASE / d + 0.5);
703       }
704
705   /* Decide which heuristic to use.  In case we didn't match anything,
706      use no_prediction heuristic, in case we did match, use either
707      first match or Dempster-Shaffer theory depending on the flags.  */
708
709   if (predictor_info [best_predictor].flags & PRED_FLAG_FIRST_MATCH)
710     first_match = true;
711
712   if (!found)
713     dump_prediction (dump_file, PRED_NO_PREDICTION,
714                      combined_probability, bb, true);
715   else
716     {
717       dump_prediction (dump_file, PRED_DS_THEORY, combined_probability,
718                        bb, !first_match);
719       dump_prediction (dump_file, PRED_FIRST_MATCH, best_probability,
720                        bb, first_match);
721     }
722
723   if (first_match)
724     combined_probability = best_probability;
725   dump_prediction (dump_file, PRED_COMBINED, combined_probability, bb, true);
726
727   while (*pnote)
728     {
729       if (REG_NOTE_KIND (*pnote) == REG_BR_PRED)
730         {
731           int predictor = INTVAL (XEXP (XEXP (*pnote, 0), 0));
732           int probability = INTVAL (XEXP (XEXP (*pnote, 0), 1));
733
734           dump_prediction (dump_file, predictor, probability, bb,
735                            !first_match || best_predictor == predictor);
736           *pnote = XEXP (*pnote, 1);
737         }
738       else
739         pnote = &XEXP (*pnote, 1);
740     }
741
742   if (!prob_note)
743     {
744       add_reg_note (insn, REG_BR_PROB, GEN_INT (combined_probability));
745
746       /* Save the prediction into CFG in case we are seeing non-degenerated
747          conditional jump.  */
748       if (!single_succ_p (bb))
749         {
750           BRANCH_EDGE (bb)->probability = combined_probability;
751           FALLTHRU_EDGE (bb)->probability
752             = REG_BR_PROB_BASE - combined_probability;
753         }
754     }
755   else if (!single_succ_p (bb))
756     {
757       int prob = INTVAL (XEXP (prob_note, 0));
758
759       BRANCH_EDGE (bb)->probability = prob;
760       FALLTHRU_EDGE (bb)->probability = REG_BR_PROB_BASE - prob;
761     }
762   else
763     single_succ_edge (bb)->probability = REG_BR_PROB_BASE;
764 }
765
766 /* Combine predictions into single probability and store them into CFG.
767    Remove now useless prediction entries.  */
768
769 static void
770 combine_predictions_for_bb (basic_block bb)
771 {
772   int best_probability = PROB_EVEN;
773   int best_predictor = END_PREDICTORS;
774   int combined_probability = REG_BR_PROB_BASE / 2;
775   int d;
776   bool first_match = false;
777   bool found = false;
778   struct edge_prediction *pred;
779   int nedges = 0;
780   edge e, first = NULL, second = NULL;
781   edge_iterator ei;
782   void **preds;
783
784   FOR_EACH_EDGE (e, ei, bb->succs)
785     if (!(e->flags & (EDGE_EH | EDGE_FAKE)))
786       {
787         nedges ++;
788         if (first && !second)
789           second = e;
790         if (!first)
791           first = e;
792       }
793
794   /* When there is no successor or only one choice, prediction is easy. 
795
796      We are lazy for now and predict only basic blocks with two outgoing
797      edges.  It is possible to predict generic case too, but we have to
798      ignore first match heuristics and do more involved combining.  Implement
799      this later.  */
800   if (nedges != 2)
801     {
802       if (!bb->count)
803         set_even_probabilities (bb);
804       clear_bb_predictions (bb);
805       if (dump_file)
806         fprintf (dump_file, "%i edges in bb %i predicted to even probabilities\n",
807                  nedges, bb->index);
808       return;
809     }
810
811   if (dump_file)
812     fprintf (dump_file, "Predictions for bb %i\n", bb->index);
813
814   preds = pointer_map_contains (bb_predictions, bb);
815   if (preds)
816     {
817       /* We implement "first match" heuristics and use probability guessed
818          by predictor with smallest index.  */
819       for (pred = (struct edge_prediction *) *preds; pred; pred = pred->ep_next)
820         {
821           int predictor = pred->ep_predictor;
822           int probability = pred->ep_probability;
823
824           if (pred->ep_edge != first)
825             probability = REG_BR_PROB_BASE - probability;
826
827           found = true;
828           if (best_predictor > predictor)
829             best_probability = probability, best_predictor = predictor;
830
831           d = (combined_probability * probability
832                + (REG_BR_PROB_BASE - combined_probability)
833                * (REG_BR_PROB_BASE - probability));
834
835           /* Use FP math to avoid overflows of 32bit integers.  */
836           if (d == 0)
837             /* If one probability is 0% and one 100%, avoid division by zero.  */
838             combined_probability = REG_BR_PROB_BASE / 2;
839           else
840             combined_probability = (((double) combined_probability)
841                                     * probability
842                                     * REG_BR_PROB_BASE / d + 0.5);
843         }
844     }
845
846   /* Decide which heuristic to use.  In case we didn't match anything,
847      use no_prediction heuristic, in case we did match, use either
848      first match or Dempster-Shaffer theory depending on the flags.  */
849
850   if (predictor_info [best_predictor].flags & PRED_FLAG_FIRST_MATCH)
851     first_match = true;
852
853   if (!found)
854     dump_prediction (dump_file, PRED_NO_PREDICTION, combined_probability, bb, true);
855   else
856     {
857       dump_prediction (dump_file, PRED_DS_THEORY, combined_probability, bb,
858                        !first_match);
859       dump_prediction (dump_file, PRED_FIRST_MATCH, best_probability, bb,
860                        first_match);
861     }
862
863   if (first_match)
864     combined_probability = best_probability;
865   dump_prediction (dump_file, PRED_COMBINED, combined_probability, bb, true);
866
867   if (preds)
868     {
869       for (pred = (struct edge_prediction *) *preds; pred; pred = pred->ep_next)
870         {
871           int predictor = pred->ep_predictor;
872           int probability = pred->ep_probability;
873
874           if (pred->ep_edge != EDGE_SUCC (bb, 0))
875             probability = REG_BR_PROB_BASE - probability;
876           dump_prediction (dump_file, predictor, probability, bb,
877                            !first_match || best_predictor == predictor);
878         }
879     }
880   clear_bb_predictions (bb);
881
882   if (!bb->count)
883     {
884       first->probability = combined_probability;
885       second->probability = REG_BR_PROB_BASE - combined_probability;
886     }
887 }
888
889 /* Predict edge probabilities by exploiting loop structure.  */
890
891 static void
892 predict_loops (void)
893 {
894   loop_iterator li;
895   struct loop *loop;
896
897   scev_initialize ();
898
899   /* Try to predict out blocks in a loop that are not part of a
900      natural loop.  */
901   FOR_EACH_LOOP (li, loop, 0)
902     {
903       basic_block bb, *bbs;
904       unsigned j, n_exits;
905       VEC (edge, heap) *exits;
906       struct tree_niter_desc niter_desc;
907       edge ex;
908
909       exits = get_loop_exit_edges (loop);
910       n_exits = VEC_length (edge, exits);
911
912       for (j = 0; VEC_iterate (edge, exits, j, ex); j++)
913         {
914           tree niter = NULL;
915           HOST_WIDE_INT nitercst;
916           int max = PARAM_VALUE (PARAM_MAX_PREDICTED_ITERATIONS);
917           int probability;
918           enum br_predictor predictor;
919
920           if (number_of_iterations_exit (loop, ex, &niter_desc, false))
921             niter = niter_desc.niter;
922           if (!niter || TREE_CODE (niter_desc.niter) != INTEGER_CST)
923             niter = loop_niter_by_eval (loop, ex);
924
925           if (TREE_CODE (niter) == INTEGER_CST)
926             {
927               if (host_integerp (niter, 1)
928                   && compare_tree_int (niter, max-1) == -1)
929                 nitercst = tree_low_cst (niter, 1) + 1;
930               else
931                 nitercst = max;
932               predictor = PRED_LOOP_ITERATIONS;
933             }
934           /* If we have just one exit and we can derive some information about
935              the number of iterations of the loop from the statements inside
936              the loop, use it to predict this exit.  */
937           else if (n_exits == 1)
938             {
939               nitercst = estimated_loop_iterations_int (loop, false);
940               if (nitercst < 0)
941                 continue;
942               if (nitercst > max)
943                 nitercst = max;
944
945               predictor = PRED_LOOP_ITERATIONS_GUESSED;
946             }
947           else
948             continue;
949
950           probability = ((REG_BR_PROB_BASE + nitercst / 2) / nitercst);
951           predict_edge (ex, predictor, probability);
952         }
953       VEC_free (edge, heap, exits);
954
955       bbs = get_loop_body (loop);
956
957       for (j = 0; j < loop->num_nodes; j++)
958         {
959           int header_found = 0;
960           edge e;
961           edge_iterator ei;
962
963           bb = bbs[j];
964
965           /* Bypass loop heuristics on continue statement.  These
966              statements construct loops via "non-loop" constructs
967              in the source language and are better to be handled
968              separately.  */
969           if (predicted_by_p (bb, PRED_CONTINUE))
970             continue;
971
972           /* Loop branch heuristics - predict an edge back to a
973              loop's head as taken.  */
974           if (bb == loop->latch)
975             {
976               e = find_edge (loop->latch, loop->header);
977               if (e)
978                 {
979                   header_found = 1;
980                   predict_edge_def (e, PRED_LOOP_BRANCH, TAKEN);
981                 }
982             }
983
984           /* Loop exit heuristics - predict an edge exiting the loop if the
985              conditional has no loop header successors as not taken.  */
986           if (!header_found
987               /* If we already used more reliable loop exit predictors, do not
988                  bother with PRED_LOOP_EXIT.  */
989               && !predicted_by_p (bb, PRED_LOOP_ITERATIONS_GUESSED)
990               && !predicted_by_p (bb, PRED_LOOP_ITERATIONS))
991             {
992               /* For loop with many exits we don't want to predict all exits
993                  with the pretty large probability, because if all exits are
994                  considered in row, the loop would be predicted to iterate
995                  almost never.  The code to divide probability by number of
996                  exits is very rough.  It should compute the number of exits
997                  taken in each patch through function (not the overall number
998                  of exits that might be a lot higher for loops with wide switch
999                  statements in them) and compute n-th square root.
1000
1001                  We limit the minimal probability by 2% to avoid
1002                  EDGE_PROBABILITY_RELIABLE from trusting the branch prediction
1003                  as this was causing regression in perl benchmark containing such
1004                  a wide loop.  */
1005                 
1006               int probability = ((REG_BR_PROB_BASE
1007                                   - predictor_info [(int) PRED_LOOP_EXIT].hitrate)
1008                                  / n_exits);
1009               if (probability < HITRATE (2))
1010                 probability = HITRATE (2);
1011               FOR_EACH_EDGE (e, ei, bb->succs)
1012                 if (e->dest->index < NUM_FIXED_BLOCKS
1013                     || !flow_bb_inside_loop_p (loop, e->dest))
1014                   predict_edge (e, PRED_LOOP_EXIT, probability);
1015             }
1016         }
1017       
1018       /* Free basic blocks from get_loop_body.  */
1019       free (bbs);
1020     }
1021
1022   scev_finalize ();
1023 }
1024
1025 /* Attempt to predict probabilities of BB outgoing edges using local
1026    properties.  */
1027 static void
1028 bb_estimate_probability_locally (basic_block bb)
1029 {
1030   rtx last_insn = BB_END (bb);
1031   rtx cond;
1032
1033   if (! can_predict_insn_p (last_insn))
1034     return;
1035   cond = get_condition (last_insn, NULL, false, false);
1036   if (! cond)
1037     return;
1038
1039   /* Try "pointer heuristic."
1040      A comparison ptr == 0 is predicted as false.
1041      Similarly, a comparison ptr1 == ptr2 is predicted as false.  */
1042   if (COMPARISON_P (cond)
1043       && ((REG_P (XEXP (cond, 0)) && REG_POINTER (XEXP (cond, 0)))
1044           || (REG_P (XEXP (cond, 1)) && REG_POINTER (XEXP (cond, 1)))))
1045     {
1046       if (GET_CODE (cond) == EQ)
1047         predict_insn_def (last_insn, PRED_POINTER, NOT_TAKEN);
1048       else if (GET_CODE (cond) == NE)
1049         predict_insn_def (last_insn, PRED_POINTER, TAKEN);
1050     }
1051   else
1052
1053   /* Try "opcode heuristic."
1054      EQ tests are usually false and NE tests are usually true. Also,
1055      most quantities are positive, so we can make the appropriate guesses
1056      about signed comparisons against zero.  */
1057     switch (GET_CODE (cond))
1058       {
1059       case CONST_INT:
1060         /* Unconditional branch.  */
1061         predict_insn_def (last_insn, PRED_UNCONDITIONAL,
1062                           cond == const0_rtx ? NOT_TAKEN : TAKEN);
1063         break;
1064
1065       case EQ:
1066       case UNEQ:
1067         /* Floating point comparisons appears to behave in a very
1068            unpredictable way because of special role of = tests in
1069            FP code.  */
1070         if (FLOAT_MODE_P (GET_MODE (XEXP (cond, 0))))
1071           ;
1072         /* Comparisons with 0 are often used for booleans and there is
1073            nothing useful to predict about them.  */
1074         else if (XEXP (cond, 1) == const0_rtx
1075                  || XEXP (cond, 0) == const0_rtx)
1076           ;
1077         else
1078           predict_insn_def (last_insn, PRED_OPCODE_NONEQUAL, NOT_TAKEN);
1079         break;
1080
1081       case NE:
1082       case LTGT:
1083         /* Floating point comparisons appears to behave in a very
1084            unpredictable way because of special role of = tests in
1085            FP code.  */
1086         if (FLOAT_MODE_P (GET_MODE (XEXP (cond, 0))))
1087           ;
1088         /* Comparisons with 0 are often used for booleans and there is
1089            nothing useful to predict about them.  */
1090         else if (XEXP (cond, 1) == const0_rtx
1091                  || XEXP (cond, 0) == const0_rtx)
1092           ;
1093         else
1094           predict_insn_def (last_insn, PRED_OPCODE_NONEQUAL, TAKEN);
1095         break;
1096
1097       case ORDERED:
1098         predict_insn_def (last_insn, PRED_FPOPCODE, TAKEN);
1099         break;
1100
1101       case UNORDERED:
1102         predict_insn_def (last_insn, PRED_FPOPCODE, NOT_TAKEN);
1103         break;
1104
1105       case LE:
1106       case LT:
1107         if (XEXP (cond, 1) == const0_rtx || XEXP (cond, 1) == const1_rtx
1108             || XEXP (cond, 1) == constm1_rtx)
1109           predict_insn_def (last_insn, PRED_OPCODE_POSITIVE, NOT_TAKEN);
1110         break;
1111
1112       case GE:
1113       case GT:
1114         if (XEXP (cond, 1) == const0_rtx || XEXP (cond, 1) == const1_rtx
1115             || XEXP (cond, 1) == constm1_rtx)
1116           predict_insn_def (last_insn, PRED_OPCODE_POSITIVE, TAKEN);
1117         break;
1118
1119       default:
1120         break;
1121       }
1122 }
1123
1124 /* Set edge->probability for each successor edge of BB.  */
1125 void
1126 guess_outgoing_edge_probabilities (basic_block bb)
1127 {
1128   bb_estimate_probability_locally (bb);
1129   combine_predictions_for_insn (BB_END (bb), bb);
1130 }
1131 \f
1132 static tree expr_expected_value (tree, bitmap);
1133
1134 /* Helper function for expr_expected_value.  */
1135
1136 static tree
1137 expr_expected_value_1 (tree type, tree op0, enum tree_code code, tree op1, bitmap visited)
1138 {
1139   gimple def;
1140
1141   if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS)
1142     {
1143       if (TREE_CONSTANT (op0))
1144         return op0;
1145
1146       if (code != SSA_NAME)
1147         return NULL_TREE;
1148
1149       def = SSA_NAME_DEF_STMT (op0);
1150
1151       /* If we were already here, break the infinite cycle.  */
1152       if (bitmap_bit_p (visited, SSA_NAME_VERSION (op0)))
1153         return NULL;
1154       bitmap_set_bit (visited, SSA_NAME_VERSION (op0));
1155
1156       if (gimple_code (def) == GIMPLE_PHI)
1157         {
1158           /* All the arguments of the PHI node must have the same constant
1159              length.  */
1160           int i, n = gimple_phi_num_args (def);
1161           tree val = NULL, new_val;
1162
1163           for (i = 0; i < n; i++)
1164             {
1165               tree arg = PHI_ARG_DEF (def, i);
1166
1167               /* If this PHI has itself as an argument, we cannot
1168                  determine the string length of this argument.  However,
1169                  if we can find an expected constant value for the other
1170                  PHI args then we can still be sure that this is
1171                  likely a constant.  So be optimistic and just
1172                  continue with the next argument.  */
1173               if (arg == PHI_RESULT (def))
1174                 continue;
1175
1176               new_val = expr_expected_value (arg, visited);
1177               if (!new_val)
1178                 return NULL;
1179               if (!val)
1180                 val = new_val;
1181               else if (!operand_equal_p (val, new_val, false))
1182                 return NULL;
1183             }
1184           return val;
1185         }
1186       if (is_gimple_assign (def))
1187         {
1188           if (gimple_assign_lhs (def) != op0)
1189             return NULL;
1190
1191           return expr_expected_value_1 (TREE_TYPE (gimple_assign_lhs (def)),
1192                                         gimple_assign_rhs1 (def),
1193                                         gimple_assign_rhs_code (def),
1194                                         gimple_assign_rhs2 (def),
1195                                         visited);
1196         }
1197
1198       if (is_gimple_call (def))
1199         {
1200           tree decl = gimple_call_fndecl (def);
1201           if (!decl)
1202             return NULL;
1203           if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
1204               && DECL_FUNCTION_CODE (decl) == BUILT_IN_EXPECT)
1205             {
1206               tree val;
1207
1208               if (gimple_call_num_args (def) != 2)
1209                 return NULL;
1210               val = gimple_call_arg (def, 0);
1211               if (TREE_CONSTANT (val))
1212                 return val;
1213               return gimple_call_arg (def, 1);
1214             }
1215         }
1216
1217       return NULL;
1218     }
1219
1220   if (get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS)
1221     {
1222       tree res;
1223       op0 = expr_expected_value (op0, visited);
1224       if (!op0)
1225         return NULL;
1226       op1 = expr_expected_value (op1, visited);
1227       if (!op1)
1228         return NULL;
1229       res = fold_build2 (code, type, op0, op1);
1230       if (TREE_CONSTANT (res))
1231         return res;
1232       return NULL;
1233     }
1234   if (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS)
1235     {
1236       tree res;
1237       op0 = expr_expected_value (op0, visited);
1238       if (!op0)
1239         return NULL;
1240       res = fold_build1 (code, type, op0);
1241       if (TREE_CONSTANT (res))
1242         return res;
1243       return NULL;
1244     }
1245   return NULL;
1246 }
1247
1248 /* Return constant EXPR will likely have at execution time, NULL if unknown. 
1249    The function is used by builtin_expect branch predictor so the evidence
1250    must come from this construct and additional possible constant folding.
1251   
1252    We may want to implement more involved value guess (such as value range
1253    propagation based prediction), but such tricks shall go to new
1254    implementation.  */
1255
1256 static tree
1257 expr_expected_value (tree expr, bitmap visited)
1258 {
1259   enum tree_code code;
1260   tree op0, op1;
1261
1262   if (TREE_CONSTANT (expr))
1263     return expr;
1264
1265   extract_ops_from_tree (expr, &code, &op0, &op1);
1266   return expr_expected_value_1 (TREE_TYPE (expr),
1267                                 op0, code, op1, visited);
1268 }
1269
1270 \f
1271 /* Get rid of all builtin_expect calls and GIMPLE_PREDICT statements
1272    we no longer need.  */
1273 static unsigned int
1274 strip_predict_hints (void)
1275 {
1276   basic_block bb;
1277   gimple ass_stmt;
1278   tree var;
1279
1280   FOR_EACH_BB (bb)
1281     {
1282       gimple_stmt_iterator bi;
1283       for (bi = gsi_start_bb (bb); !gsi_end_p (bi);)
1284         {
1285           gimple stmt = gsi_stmt (bi);
1286
1287           if (gimple_code (stmt) == GIMPLE_PREDICT)
1288             {
1289               gsi_remove (&bi, true);
1290               continue;
1291             }
1292           else if (gimple_code (stmt) == GIMPLE_CALL)
1293             {
1294               tree fndecl = gimple_call_fndecl (stmt);
1295
1296               if (fndecl
1297                   && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
1298                   && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT
1299                   && gimple_call_num_args (stmt) == 2)
1300                 {
1301                   var = gimple_call_lhs (stmt);
1302                   ass_stmt = gimple_build_assign (var, gimple_call_arg (stmt, 0));
1303
1304                   gsi_replace (&bi, ass_stmt, true);
1305                 }
1306             }
1307           gsi_next (&bi);
1308         }
1309     }
1310   return 0;
1311 }
1312 \f
1313 /* Predict using opcode of the last statement in basic block.  */
1314 static void
1315 tree_predict_by_opcode (basic_block bb)
1316 {
1317   gimple stmt = last_stmt (bb);
1318   edge then_edge;
1319   tree op0, op1;
1320   tree type;
1321   tree val;
1322   enum tree_code cmp;
1323   bitmap visited;
1324   edge_iterator ei;
1325
1326   if (!stmt || gimple_code (stmt) != GIMPLE_COND)
1327     return;
1328   FOR_EACH_EDGE (then_edge, ei, bb->succs)
1329     if (then_edge->flags & EDGE_TRUE_VALUE)
1330       break;
1331   op0 = gimple_cond_lhs (stmt);
1332   op1 = gimple_cond_rhs (stmt);
1333   cmp = gimple_cond_code (stmt);
1334   type = TREE_TYPE (op0);
1335   visited = BITMAP_ALLOC (NULL);
1336   val = expr_expected_value_1 (boolean_type_node, op0, cmp, op1, visited);
1337   BITMAP_FREE (visited);
1338   if (val)
1339     {
1340       if (integer_zerop (val))
1341         predict_edge_def (then_edge, PRED_BUILTIN_EXPECT, NOT_TAKEN);
1342       else
1343         predict_edge_def (then_edge, PRED_BUILTIN_EXPECT, TAKEN);
1344       return;
1345     }
1346   /* Try "pointer heuristic."
1347      A comparison ptr == 0 is predicted as false.
1348      Similarly, a comparison ptr1 == ptr2 is predicted as false.  */
1349   if (POINTER_TYPE_P (type))
1350     {
1351       if (cmp == EQ_EXPR)
1352         predict_edge_def (then_edge, PRED_TREE_POINTER, NOT_TAKEN);
1353       else if (cmp == NE_EXPR)
1354         predict_edge_def (then_edge, PRED_TREE_POINTER, TAKEN);
1355     }
1356   else
1357
1358   /* Try "opcode heuristic."
1359      EQ tests are usually false and NE tests are usually true. Also,
1360      most quantities are positive, so we can make the appropriate guesses
1361      about signed comparisons against zero.  */
1362     switch (cmp)
1363       {
1364       case EQ_EXPR:
1365       case UNEQ_EXPR:
1366         /* Floating point comparisons appears to behave in a very
1367            unpredictable way because of special role of = tests in
1368            FP code.  */
1369         if (FLOAT_TYPE_P (type))
1370           ;
1371         /* Comparisons with 0 are often used for booleans and there is
1372            nothing useful to predict about them.  */
1373         else if (integer_zerop (op0) || integer_zerop (op1))
1374           ;
1375         else
1376           predict_edge_def (then_edge, PRED_TREE_OPCODE_NONEQUAL, NOT_TAKEN);
1377         break;
1378
1379       case NE_EXPR:
1380       case LTGT_EXPR:
1381         /* Floating point comparisons appears to behave in a very
1382            unpredictable way because of special role of = tests in
1383            FP code.  */
1384         if (FLOAT_TYPE_P (type))
1385           ;
1386         /* Comparisons with 0 are often used for booleans and there is
1387            nothing useful to predict about them.  */
1388         else if (integer_zerop (op0)
1389                  || integer_zerop (op1))
1390           ;
1391         else
1392           predict_edge_def (then_edge, PRED_TREE_OPCODE_NONEQUAL, TAKEN);
1393         break;
1394
1395       case ORDERED_EXPR:
1396         predict_edge_def (then_edge, PRED_TREE_FPOPCODE, TAKEN);
1397         break;
1398
1399       case UNORDERED_EXPR:
1400         predict_edge_def (then_edge, PRED_TREE_FPOPCODE, NOT_TAKEN);
1401         break;
1402
1403       case LE_EXPR:
1404       case LT_EXPR:
1405         if (integer_zerop (op1)
1406             || integer_onep (op1)
1407             || integer_all_onesp (op1)
1408             || real_zerop (op1)
1409             || real_onep (op1)
1410             || real_minus_onep (op1))
1411           predict_edge_def (then_edge, PRED_TREE_OPCODE_POSITIVE, NOT_TAKEN);
1412         break;
1413
1414       case GE_EXPR:
1415       case GT_EXPR:
1416         if (integer_zerop (op1)
1417             || integer_onep (op1)
1418             || integer_all_onesp (op1)
1419             || real_zerop (op1)
1420             || real_onep (op1)
1421             || real_minus_onep (op1))
1422           predict_edge_def (then_edge, PRED_TREE_OPCODE_POSITIVE, TAKEN);
1423         break;
1424
1425       default:
1426         break;
1427       }
1428 }
1429
1430 /* Try to guess whether the value of return means error code.  */
1431
1432 static enum br_predictor
1433 return_prediction (tree val, enum prediction *prediction)
1434 {
1435   /* VOID.  */
1436   if (!val)
1437     return PRED_NO_PREDICTION;
1438   /* Different heuristics for pointers and scalars.  */
1439   if (POINTER_TYPE_P (TREE_TYPE (val)))
1440     {
1441       /* NULL is usually not returned.  */
1442       if (integer_zerop (val))
1443         {
1444           *prediction = NOT_TAKEN;
1445           return PRED_NULL_RETURN;
1446         }
1447     }
1448   else if (INTEGRAL_TYPE_P (TREE_TYPE (val)))
1449     {
1450       /* Negative return values are often used to indicate
1451          errors.  */
1452       if (TREE_CODE (val) == INTEGER_CST
1453           && tree_int_cst_sgn (val) < 0)
1454         {
1455           *prediction = NOT_TAKEN;
1456           return PRED_NEGATIVE_RETURN;
1457         }
1458       /* Constant return values seems to be commonly taken.
1459          Zero/one often represent booleans so exclude them from the
1460          heuristics.  */
1461       if (TREE_CONSTANT (val)
1462           && (!integer_zerop (val) && !integer_onep (val)))
1463         {
1464           *prediction = TAKEN;
1465           return PRED_CONST_RETURN;
1466         }
1467     }
1468   return PRED_NO_PREDICTION;
1469 }
1470
1471 /* Find the basic block with return expression and look up for possible
1472    return value trying to apply RETURN_PREDICTION heuristics.  */
1473 static void
1474 apply_return_prediction (void)
1475 {
1476   gimple return_stmt = NULL;
1477   tree return_val;
1478   edge e;
1479   gimple phi;
1480   int phi_num_args, i;
1481   enum br_predictor pred;
1482   enum prediction direction;
1483   edge_iterator ei;
1484
1485   FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
1486     {
1487       return_stmt = last_stmt (e->src);
1488       if (return_stmt
1489           && gimple_code (return_stmt) == GIMPLE_RETURN)
1490         break;
1491     }
1492   if (!e)
1493     return;
1494   return_val = gimple_return_retval (return_stmt);
1495   if (!return_val)
1496     return;
1497   if (TREE_CODE (return_val) != SSA_NAME
1498       || !SSA_NAME_DEF_STMT (return_val)
1499       || gimple_code (SSA_NAME_DEF_STMT (return_val)) != GIMPLE_PHI)
1500     return;
1501   phi = SSA_NAME_DEF_STMT (return_val);
1502   phi_num_args = gimple_phi_num_args (phi);
1503   pred = return_prediction (PHI_ARG_DEF (phi, 0), &direction);
1504
1505   /* Avoid the degenerate case where all return values form the function
1506      belongs to same category (ie they are all positive constants)
1507      so we can hardly say something about them.  */
1508   for (i = 1; i < phi_num_args; i++)
1509     if (pred != return_prediction (PHI_ARG_DEF (phi, i), &direction))
1510       break;
1511   if (i != phi_num_args)
1512     for (i = 0; i < phi_num_args; i++)
1513       {
1514         pred = return_prediction (PHI_ARG_DEF (phi, i), &direction);
1515         if (pred != PRED_NO_PREDICTION)
1516           predict_paths_leading_to (gimple_phi_arg_edge (phi, i)->src, pred,
1517                                     direction);
1518       }
1519 }
1520
1521 /* Look for basic block that contains unlikely to happen events
1522    (such as noreturn calls) and mark all paths leading to execution
1523    of this basic blocks as unlikely.  */
1524
1525 static void
1526 tree_bb_level_predictions (void)
1527 {
1528   basic_block bb;
1529
1530   apply_return_prediction ();
1531
1532   FOR_EACH_BB (bb)
1533     {
1534       gimple_stmt_iterator gsi;
1535
1536       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1537         {
1538           gimple stmt = gsi_stmt (gsi);
1539           tree decl;
1540
1541           if (is_gimple_call (stmt))
1542             {
1543               if (gimple_call_flags (stmt) & ECF_NORETURN)
1544                 predict_paths_leading_to (bb, PRED_NORETURN,
1545                                           NOT_TAKEN);
1546               decl = gimple_call_fndecl (stmt);
1547               if (decl
1548                   && lookup_attribute ("cold",
1549                                        DECL_ATTRIBUTES (decl)))
1550                 predict_paths_leading_to (bb, PRED_COLD_FUNCTION,
1551                                           NOT_TAKEN);
1552             }
1553           else if (gimple_code (stmt) == GIMPLE_PREDICT)
1554             {
1555               predict_paths_leading_to (bb, gimple_predict_predictor (stmt),
1556                                         gimple_predict_outcome (stmt));
1557               /* Keep GIMPLE_PREDICT around so early inlining will propagate
1558                  hints to callers.  */
1559             }
1560         }
1561     }
1562 }
1563
1564 #ifdef ENABLE_CHECKING
1565
1566 /* Callback for pointer_map_traverse, asserts that the pointer map is
1567    empty.  */
1568
1569 static bool
1570 assert_is_empty (const void *key ATTRIBUTE_UNUSED, void **value,
1571                  void *data ATTRIBUTE_UNUSED)
1572 {
1573   gcc_assert (!*value);
1574   return false;
1575 }
1576 #endif
1577
1578 /* Predict branch probabilities and estimate profile of the tree CFG.  */
1579 static unsigned int
1580 tree_estimate_probability (void)
1581 {
1582   basic_block bb;
1583
1584   loop_optimizer_init (0);
1585   if (dump_file && (dump_flags & TDF_DETAILS))
1586     flow_loops_dump (dump_file, NULL, 0);
1587
1588   add_noreturn_fake_exit_edges ();
1589   connect_infinite_loops_to_exit ();
1590   /* We use loop_niter_by_eval, which requires that the loops have
1591      preheaders.  */
1592   create_preheaders (CP_SIMPLE_PREHEADERS);
1593   calculate_dominance_info (CDI_POST_DOMINATORS);
1594
1595   bb_predictions = pointer_map_create ();
1596   tree_bb_level_predictions ();
1597
1598   mark_irreducible_loops ();
1599   record_loop_exits ();
1600   if (number_of_loops () > 1)
1601     predict_loops ();
1602
1603   FOR_EACH_BB (bb)
1604     {
1605       edge e;
1606       edge_iterator ei;
1607
1608       FOR_EACH_EDGE (e, ei, bb->succs)
1609         {
1610           /* Predict early returns to be probable, as we've already taken
1611              care for error returns and other cases are often used for
1612              fast paths through function. 
1613
1614              Since we've already removed the return statements, we are
1615              looking for CFG like:
1616
1617                if (conditional)
1618                  {
1619                    ..
1620                    goto return_block
1621                  }
1622                some other blocks
1623              return_block:
1624                return_stmt.  */
1625           if (e->dest != bb->next_bb
1626               && e->dest != EXIT_BLOCK_PTR
1627               && single_succ_p (e->dest)
1628               && single_succ_edge (e->dest)->dest == EXIT_BLOCK_PTR
1629               && gimple_code (last_stmt (e->dest)) == GIMPLE_RETURN)
1630             {
1631               edge e1;
1632               edge_iterator ei1;
1633
1634               if (single_succ_p (bb))
1635                 {
1636                   FOR_EACH_EDGE (e1, ei1, bb->preds)
1637                     if (!predicted_by_p (e1->src, PRED_NULL_RETURN)
1638                         && !predicted_by_p (e1->src, PRED_CONST_RETURN)
1639                         && !predicted_by_p (e1->src, PRED_NEGATIVE_RETURN))
1640                       predict_edge_def (e1, PRED_TREE_EARLY_RETURN, NOT_TAKEN);
1641                 }
1642                else
1643                 if (!predicted_by_p (e->src, PRED_NULL_RETURN)
1644                     && !predicted_by_p (e->src, PRED_CONST_RETURN)
1645                     && !predicted_by_p (e->src, PRED_NEGATIVE_RETURN))
1646                   predict_edge_def (e, PRED_TREE_EARLY_RETURN, NOT_TAKEN);
1647             }
1648
1649           /* Look for block we are guarding (ie we dominate it,
1650              but it doesn't postdominate us).  */
1651           if (e->dest != EXIT_BLOCK_PTR && e->dest != bb
1652               && dominated_by_p (CDI_DOMINATORS, e->dest, e->src)
1653               && !dominated_by_p (CDI_POST_DOMINATORS, e->src, e->dest))
1654             {
1655               gimple_stmt_iterator bi;
1656
1657               /* The call heuristic claims that a guarded function call
1658                  is improbable.  This is because such calls are often used
1659                  to signal exceptional situations such as printing error
1660                  messages.  */
1661               for (bi = gsi_start_bb (e->dest); !gsi_end_p (bi);
1662                    gsi_next (&bi))
1663                 {
1664                   gimple stmt = gsi_stmt (bi);
1665                   if (is_gimple_call (stmt)
1666                       /* Constant and pure calls are hardly used to signalize
1667                          something exceptional.  */
1668                       && gimple_has_side_effects (stmt))
1669                     {
1670                       predict_edge_def (e, PRED_CALL, NOT_TAKEN);
1671                       break;
1672                     }
1673                 }
1674             }
1675         }
1676       tree_predict_by_opcode (bb);
1677     }
1678   FOR_EACH_BB (bb)
1679     combine_predictions_for_bb (bb);
1680
1681 #ifdef ENABLE_CHECKING
1682   pointer_map_traverse (bb_predictions, assert_is_empty, NULL);
1683 #endif
1684   pointer_map_destroy (bb_predictions);
1685   bb_predictions = NULL;
1686
1687   estimate_bb_frequencies ();
1688   free_dominance_info (CDI_POST_DOMINATORS);
1689   remove_fake_exit_edges ();
1690   loop_optimizer_finalize ();
1691   if (dump_file && (dump_flags & TDF_DETAILS))
1692     gimple_dump_cfg (dump_file, dump_flags);
1693   if (profile_status == PROFILE_ABSENT)
1694     profile_status = PROFILE_GUESSED;
1695   return 0;
1696 }
1697 \f
1698 /* Predict edges to successors of CUR whose sources are not postdominated by
1699    BB by PRED and recurse to all postdominators.  */
1700
1701 static void
1702 predict_paths_for_bb (basic_block cur, basic_block bb,
1703                       enum br_predictor pred,
1704                       enum prediction taken)
1705 {
1706   edge e;
1707   edge_iterator ei;
1708   basic_block son;
1709
1710   /* We are looking for all edges forming edge cut induced by
1711      set of all blocks postdominated by BB.  */
1712   FOR_EACH_EDGE (e, ei, cur->preds)
1713     if (e->src->index >= NUM_FIXED_BLOCKS
1714         && !dominated_by_p (CDI_POST_DOMINATORS, e->src, bb))
1715     {
1716       gcc_assert (bb == cur || dominated_by_p (CDI_POST_DOMINATORS, cur, bb));
1717       predict_edge_def (e, pred, taken);
1718     }
1719   for (son = first_dom_son (CDI_POST_DOMINATORS, cur);
1720        son;
1721        son = next_dom_son (CDI_POST_DOMINATORS, son))
1722     predict_paths_for_bb (son, bb, pred, taken);
1723 }
1724
1725 /* Sets branch probabilities according to PREDiction and
1726    FLAGS.  */
1727
1728 static void
1729 predict_paths_leading_to (basic_block bb, enum br_predictor pred,
1730                           enum prediction taken)
1731 {
1732   predict_paths_for_bb (bb, bb, pred, taken);
1733 }
1734 \f
1735 /* This is used to carry information about basic blocks.  It is
1736    attached to the AUX field of the standard CFG block.  */
1737
1738 typedef struct block_info_def
1739 {
1740   /* Estimated frequency of execution of basic_block.  */
1741   sreal frequency;
1742
1743   /* To keep queue of basic blocks to process.  */
1744   basic_block next;
1745
1746   /* Number of predecessors we need to visit first.  */
1747   int npredecessors;
1748 } *block_info;
1749
1750 /* Similar information for edges.  */
1751 typedef struct edge_info_def
1752 {
1753   /* In case edge is a loopback edge, the probability edge will be reached
1754      in case header is.  Estimated number of iterations of the loop can be
1755      then computed as 1 / (1 - back_edge_prob).  */
1756   sreal back_edge_prob;
1757   /* True if the edge is a loopback edge in the natural loop.  */
1758   unsigned int back_edge:1;
1759 } *edge_info;
1760
1761 #define BLOCK_INFO(B)   ((block_info) (B)->aux)
1762 #define EDGE_INFO(E)    ((edge_info) (E)->aux)
1763
1764 /* Helper function for estimate_bb_frequencies.
1765    Propagate the frequencies in blocks marked in
1766    TOVISIT, starting in HEAD.  */
1767
1768 static void
1769 propagate_freq (basic_block head, bitmap tovisit)
1770 {
1771   basic_block bb;
1772   basic_block last;
1773   unsigned i;
1774   edge e;
1775   basic_block nextbb;
1776   bitmap_iterator bi;
1777
1778   /* For each basic block we need to visit count number of his predecessors
1779      we need to visit first.  */
1780   EXECUTE_IF_SET_IN_BITMAP (tovisit, 0, i, bi)
1781     {
1782       edge_iterator ei;
1783       int count = 0;
1784
1785        /* The outermost "loop" includes the exit block, which we can not
1786           look up via BASIC_BLOCK.  Detect this and use EXIT_BLOCK_PTR
1787           directly.  Do the same for the entry block.  */
1788       bb = BASIC_BLOCK (i);
1789
1790       FOR_EACH_EDGE (e, ei, bb->preds)
1791         {
1792           bool visit = bitmap_bit_p (tovisit, e->src->index);
1793
1794           if (visit && !(e->flags & EDGE_DFS_BACK))
1795             count++;
1796           else if (visit && dump_file && !EDGE_INFO (e)->back_edge)
1797             fprintf (dump_file,
1798                      "Irreducible region hit, ignoring edge to %i->%i\n",
1799                      e->src->index, bb->index);
1800         }
1801       BLOCK_INFO (bb)->npredecessors = count;
1802     }
1803
1804   memcpy (&BLOCK_INFO (head)->frequency, &real_one, sizeof (real_one));
1805   last = head;
1806   for (bb = head; bb; bb = nextbb)
1807     {
1808       edge_iterator ei;
1809       sreal cyclic_probability, frequency;
1810
1811       memcpy (&cyclic_probability, &real_zero, sizeof (real_zero));
1812       memcpy (&frequency, &real_zero, sizeof (real_zero));
1813
1814       nextbb = BLOCK_INFO (bb)->next;
1815       BLOCK_INFO (bb)->next = NULL;
1816
1817       /* Compute frequency of basic block.  */
1818       if (bb != head)
1819         {
1820 #ifdef ENABLE_CHECKING
1821           FOR_EACH_EDGE (e, ei, bb->preds)
1822             gcc_assert (!bitmap_bit_p (tovisit, e->src->index)
1823                         || (e->flags & EDGE_DFS_BACK));
1824 #endif
1825
1826           FOR_EACH_EDGE (e, ei, bb->preds)
1827             if (EDGE_INFO (e)->back_edge)
1828               {
1829                 sreal_add (&cyclic_probability, &cyclic_probability,
1830                            &EDGE_INFO (e)->back_edge_prob);
1831               }
1832             else if (!(e->flags & EDGE_DFS_BACK))
1833               {
1834                 sreal tmp;
1835
1836                 /*  frequency += (e->probability
1837                                   * BLOCK_INFO (e->src)->frequency /
1838                                   REG_BR_PROB_BASE);  */
1839
1840                 sreal_init (&tmp, e->probability, 0);
1841                 sreal_mul (&tmp, &tmp, &BLOCK_INFO (e->src)->frequency);
1842                 sreal_mul (&tmp, &tmp, &real_inv_br_prob_base);
1843                 sreal_add (&frequency, &frequency, &tmp);
1844               }
1845
1846           if (sreal_compare (&cyclic_probability, &real_zero) == 0)
1847             {
1848               memcpy (&BLOCK_INFO (bb)->frequency, &frequency,
1849                       sizeof (frequency));
1850             }
1851           else
1852             {
1853               if (sreal_compare (&cyclic_probability, &real_almost_one) > 0)
1854                 {
1855                   memcpy (&cyclic_probability, &real_almost_one,
1856                           sizeof (real_almost_one));
1857                 }
1858
1859               /* BLOCK_INFO (bb)->frequency = frequency
1860                                               / (1 - cyclic_probability) */
1861
1862               sreal_sub (&cyclic_probability, &real_one, &cyclic_probability);
1863               sreal_div (&BLOCK_INFO (bb)->frequency,
1864                          &frequency, &cyclic_probability);
1865             }
1866         }
1867
1868       bitmap_clear_bit (tovisit, bb->index);
1869
1870       e = find_edge (bb, head);
1871       if (e)
1872         {
1873           sreal tmp;
1874             
1875           /* EDGE_INFO (e)->back_edge_prob
1876              = ((e->probability * BLOCK_INFO (bb)->frequency)
1877              / REG_BR_PROB_BASE); */
1878             
1879           sreal_init (&tmp, e->probability, 0);
1880           sreal_mul (&tmp, &tmp, &BLOCK_INFO (bb)->frequency);
1881           sreal_mul (&EDGE_INFO (e)->back_edge_prob,
1882                      &tmp, &real_inv_br_prob_base);
1883         }
1884
1885       /* Propagate to successor blocks.  */
1886       FOR_EACH_EDGE (e, ei, bb->succs)
1887         if (!(e->flags & EDGE_DFS_BACK)
1888             && BLOCK_INFO (e->dest)->npredecessors)
1889           {
1890             BLOCK_INFO (e->dest)->npredecessors--;
1891             if (!BLOCK_INFO (e->dest)->npredecessors)
1892               {
1893                 if (!nextbb)
1894                   nextbb = e->dest;
1895                 else
1896                   BLOCK_INFO (last)->next = e->dest;
1897                 
1898                 last = e->dest;
1899               }
1900           }
1901     }
1902 }
1903
1904 /* Estimate probabilities of loopback edges in loops at same nest level.  */
1905
1906 static void
1907 estimate_loops_at_level (struct loop *first_loop)
1908 {
1909   struct loop *loop;
1910
1911   for (loop = first_loop; loop; loop = loop->next)
1912     {
1913       edge e;
1914       basic_block *bbs;
1915       unsigned i;
1916       bitmap tovisit = BITMAP_ALLOC (NULL);
1917
1918       estimate_loops_at_level (loop->inner);
1919
1920       /* Find current loop back edge and mark it.  */
1921       e = loop_latch_edge (loop);
1922       EDGE_INFO (e)->back_edge = 1;
1923
1924       bbs = get_loop_body (loop);
1925       for (i = 0; i < loop->num_nodes; i++)
1926         bitmap_set_bit (tovisit, bbs[i]->index);
1927       free (bbs);
1928       propagate_freq (loop->header, tovisit);
1929       BITMAP_FREE (tovisit);
1930     }
1931 }
1932
1933 /* Propagates frequencies through structure of loops.  */
1934
1935 static void
1936 estimate_loops (void)
1937 {
1938   bitmap tovisit = BITMAP_ALLOC (NULL);
1939   basic_block bb;
1940
1941   /* Start by estimating the frequencies in the loops.  */
1942   if (number_of_loops () > 1)
1943     estimate_loops_at_level (current_loops->tree_root->inner);
1944
1945   /* Now propagate the frequencies through all the blocks.  */
1946   FOR_ALL_BB (bb)
1947     {
1948       bitmap_set_bit (tovisit, bb->index);
1949     }
1950   propagate_freq (ENTRY_BLOCK_PTR, tovisit);
1951   BITMAP_FREE (tovisit);
1952 }
1953
1954 /* Convert counts measured by profile driven feedback to frequencies.
1955    Return nonzero iff there was any nonzero execution count.  */
1956
1957 int
1958 counts_to_freqs (void)
1959 {
1960   gcov_type count_max, true_count_max = 0;
1961   basic_block bb;
1962
1963   FOR_EACH_BB (bb)
1964     true_count_max = MAX (bb->count, true_count_max);
1965
1966   count_max = MAX (true_count_max, 1);
1967   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
1968     bb->frequency = (bb->count * BB_FREQ_MAX + count_max / 2) / count_max;
1969
1970   return true_count_max;
1971 }
1972
1973 /* Return true if function is likely to be expensive, so there is no point to
1974    optimize performance of prologue, epilogue or do inlining at the expense
1975    of code size growth.  THRESHOLD is the limit of number of instructions
1976    function can execute at average to be still considered not expensive.  */
1977
1978 bool
1979 expensive_function_p (int threshold)
1980 {
1981   unsigned int sum = 0;
1982   basic_block bb;
1983   unsigned int limit;
1984
1985   /* We can not compute accurately for large thresholds due to scaled
1986      frequencies.  */
1987   gcc_assert (threshold <= BB_FREQ_MAX);
1988
1989   /* Frequencies are out of range.  This either means that function contains
1990      internal loop executing more than BB_FREQ_MAX times or profile feedback
1991      is available and function has not been executed at all.  */
1992   if (ENTRY_BLOCK_PTR->frequency == 0)
1993     return true;
1994
1995   /* Maximally BB_FREQ_MAX^2 so overflow won't happen.  */
1996   limit = ENTRY_BLOCK_PTR->frequency * threshold;
1997   FOR_EACH_BB (bb)
1998     {
1999       rtx insn;
2000
2001       for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb));
2002            insn = NEXT_INSN (insn))
2003         if (active_insn_p (insn))
2004           {
2005             sum += bb->frequency;
2006             if (sum > limit)
2007               return true;
2008         }
2009     }
2010
2011   return false;
2012 }
2013
2014 /* Estimate basic blocks frequency by given branch probabilities.  */
2015
2016 void
2017 estimate_bb_frequencies (void)
2018 {
2019   basic_block bb;
2020   sreal freq_max;
2021
2022   if (!flag_branch_probabilities || !counts_to_freqs ())
2023     {
2024       static int real_values_initialized = 0;
2025
2026       if (!real_values_initialized)
2027         {
2028           real_values_initialized = 1;
2029           sreal_init (&real_zero, 0, 0);
2030           sreal_init (&real_one, 1, 0);
2031           sreal_init (&real_br_prob_base, REG_BR_PROB_BASE, 0);
2032           sreal_init (&real_bb_freq_max, BB_FREQ_MAX, 0);
2033           sreal_init (&real_one_half, 1, -1);
2034           sreal_div (&real_inv_br_prob_base, &real_one, &real_br_prob_base);
2035           sreal_sub (&real_almost_one, &real_one, &real_inv_br_prob_base);
2036         }
2037
2038       mark_dfs_back_edges ();
2039
2040       single_succ_edge (ENTRY_BLOCK_PTR)->probability = REG_BR_PROB_BASE;
2041
2042       /* Set up block info for each basic block.  */
2043       alloc_aux_for_blocks (sizeof (struct block_info_def));
2044       alloc_aux_for_edges (sizeof (struct edge_info_def));
2045       FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
2046         {
2047           edge e;
2048           edge_iterator ei;
2049
2050           FOR_EACH_EDGE (e, ei, bb->succs)
2051             {
2052               sreal_init (&EDGE_INFO (e)->back_edge_prob, e->probability, 0);
2053               sreal_mul (&EDGE_INFO (e)->back_edge_prob,
2054                          &EDGE_INFO (e)->back_edge_prob,
2055                          &real_inv_br_prob_base);
2056             }
2057         }
2058
2059       /* First compute probabilities locally for each loop from innermost
2060          to outermost to examine probabilities for back edges.  */
2061       estimate_loops ();
2062
2063       memcpy (&freq_max, &real_zero, sizeof (real_zero));
2064       FOR_EACH_BB (bb)
2065         if (sreal_compare (&freq_max, &BLOCK_INFO (bb)->frequency) < 0)
2066           memcpy (&freq_max, &BLOCK_INFO (bb)->frequency, sizeof (freq_max));
2067
2068       sreal_div (&freq_max, &real_bb_freq_max, &freq_max);
2069       FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
2070         {
2071           sreal tmp;
2072
2073           sreal_mul (&tmp, &BLOCK_INFO (bb)->frequency, &freq_max);
2074           sreal_add (&tmp, &tmp, &real_one_half);
2075           bb->frequency = sreal_to_int (&tmp);
2076         }
2077
2078       free_aux_for_blocks ();
2079       free_aux_for_edges ();
2080     }
2081   compute_function_frequency ();
2082   if (flag_reorder_functions)
2083     choose_function_section ();
2084 }
2085
2086 /* Decide whether function is hot, cold or unlikely executed.  */
2087 static void
2088 compute_function_frequency (void)
2089 {
2090   basic_block bb;
2091
2092   if (!profile_info || !flag_branch_probabilities)
2093     {
2094       if (lookup_attribute ("cold", DECL_ATTRIBUTES (current_function_decl))
2095           != NULL)
2096         cfun->function_frequency = FUNCTION_FREQUENCY_UNLIKELY_EXECUTED;
2097       else if (lookup_attribute ("hot", DECL_ATTRIBUTES (current_function_decl))
2098                != NULL)
2099         cfun->function_frequency = FUNCTION_FREQUENCY_HOT;
2100       return;
2101     }
2102   cfun->function_frequency = FUNCTION_FREQUENCY_UNLIKELY_EXECUTED;
2103   FOR_EACH_BB (bb)
2104     {
2105       if (maybe_hot_bb_p (bb))
2106         {
2107           cfun->function_frequency = FUNCTION_FREQUENCY_HOT;
2108           return;
2109         }
2110       if (!probably_never_executed_bb_p (bb))
2111         cfun->function_frequency = FUNCTION_FREQUENCY_NORMAL;
2112     }
2113 }
2114
2115 /* Choose appropriate section for the function.  */
2116 static void
2117 choose_function_section (void)
2118 {
2119   if (DECL_SECTION_NAME (current_function_decl)
2120       || !targetm.have_named_sections
2121       /* Theoretically we can split the gnu.linkonce text section too,
2122          but this requires more work as the frequency needs to match
2123          for all generated objects so we need to merge the frequency
2124          of all instances.  For now just never set frequency for these.  */
2125       || DECL_ONE_ONLY (current_function_decl))
2126     return;
2127
2128   /* If we are doing the partitioning optimization, let the optimization
2129      choose the correct section into which to put things.  */
2130
2131   if (flag_reorder_blocks_and_partition)
2132     return;
2133
2134   if (cfun->function_frequency == FUNCTION_FREQUENCY_HOT)
2135     DECL_SECTION_NAME (current_function_decl) =
2136       build_string (strlen (HOT_TEXT_SECTION_NAME), HOT_TEXT_SECTION_NAME);
2137   if (cfun->function_frequency == FUNCTION_FREQUENCY_UNLIKELY_EXECUTED)
2138     DECL_SECTION_NAME (current_function_decl) =
2139       build_string (strlen (UNLIKELY_EXECUTED_TEXT_SECTION_NAME),
2140                     UNLIKELY_EXECUTED_TEXT_SECTION_NAME);
2141 }
2142
2143 static bool
2144 gate_estimate_probability (void)
2145 {
2146   return flag_guess_branch_prob;
2147 }
2148
2149 /* Build PREDICT_EXPR.  */
2150 tree
2151 build_predict_expr (enum br_predictor predictor, enum prediction taken)
2152 {
2153   tree t = build1 (PREDICT_EXPR, void_type_node,
2154                    build_int_cst (NULL, predictor));
2155   PREDICT_EXPR_OUTCOME (t) = taken;
2156   return t;
2157 }
2158
2159 const char *
2160 predictor_name (enum br_predictor predictor)
2161 {
2162   return predictor_info[predictor].name;
2163 }
2164
2165 struct gimple_opt_pass pass_profile = 
2166 {
2167  {
2168   GIMPLE_PASS,
2169   "profile",                            /* name */
2170   gate_estimate_probability,            /* gate */
2171   tree_estimate_probability,            /* execute */
2172   NULL,                                 /* sub */
2173   NULL,                                 /* next */
2174   0,                                    /* static_pass_number */
2175   TV_BRANCH_PROB,                       /* tv_id */
2176   PROP_cfg,                             /* properties_required */
2177   0,                                    /* properties_provided */
2178   0,                                    /* properties_destroyed */
2179   0,                                    /* todo_flags_start */
2180   TODO_ggc_collect | TODO_verify_ssa                    /* todo_flags_finish */
2181  }
2182 };
2183
2184 struct gimple_opt_pass pass_strip_predict_hints = 
2185 {
2186  {
2187   GIMPLE_PASS,
2188   "",                                   /* name */
2189   NULL,                                 /* gate */
2190   strip_predict_hints,                  /* execute */
2191   NULL,                                 /* sub */
2192   NULL,                                 /* next */
2193   0,                                    /* static_pass_number */
2194   TV_BRANCH_PROB,                       /* tv_id */
2195   PROP_cfg,                             /* properties_required */
2196   0,                                    /* properties_provided */
2197   0,                                    /* properties_destroyed */
2198   0,                                    /* todo_flags_start */
2199   TODO_ggc_collect | TODO_verify_ssa                    /* todo_flags_finish */
2200  }
2201 };