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