PR rtl-optimization/82913
[official-gcc.git] / gcc / predict.c
blob0fd2b72e135841c7ce57381e877ae8c32f85e16e
1 /* Branch prediction routines for the GNU compiler.
2 Copyright (C) 2000-2017 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* References:
22 [1] "Branch Prediction for Free"
23 Ball and Larus; PLDI '93.
24 [2] "Static Branch Frequency and Program Profile Analysis"
25 Wu and Larus; MICRO-27.
26 [3] "Corpus-based Static Branch Prediction"
27 Calder, Grunwald, Lindsay, Martin, Mozer, and Zorn; PLDI '95. */
30 #include "config.h"
31 #include "system.h"
32 #include "coretypes.h"
33 #include "backend.h"
34 #include "rtl.h"
35 #include "tree.h"
36 #include "gimple.h"
37 #include "cfghooks.h"
38 #include "tree-pass.h"
39 #include "ssa.h"
40 #include "memmodel.h"
41 #include "emit-rtl.h"
42 #include "cgraph.h"
43 #include "coverage.h"
44 #include "diagnostic-core.h"
45 #include "gimple-predict.h"
46 #include "fold-const.h"
47 #include "calls.h"
48 #include "cfganal.h"
49 #include "profile.h"
50 #include "sreal.h"
51 #include "params.h"
52 #include "cfgloop.h"
53 #include "gimple-iterator.h"
54 #include "tree-cfg.h"
55 #include "tree-ssa-loop-niter.h"
56 #include "tree-ssa-loop.h"
57 #include "tree-scalar-evolution.h"
58 #include "ipa-utils.h"
59 #include "gimple-pretty-print.h"
60 #include "selftest.h"
61 #include "cfgrtl.h"
62 #include "stringpool.h"
63 #include "attribs.h"
65 /* Enum with reasons why a predictor is ignored. */
67 enum predictor_reason
69 REASON_NONE,
70 REASON_IGNORED,
71 REASON_SINGLE_EDGE_DUPLICATE,
72 REASON_EDGE_PAIR_DUPLICATE
75 /* String messages for the aforementioned enum. */
77 static const char *reason_messages[] = {"", " (ignored)",
78 " (single edge duplicate)", " (edge pair duplicate)"};
80 /* real constants: 0, 1, 1-1/REG_BR_PROB_BASE, REG_BR_PROB_BASE,
81 1/REG_BR_PROB_BASE, 0.5, BB_FREQ_MAX. */
82 static sreal real_almost_one, real_br_prob_base,
83 real_inv_br_prob_base, real_one_half, real_bb_freq_max;
85 static void combine_predictions_for_insn (rtx_insn *, basic_block);
86 static void dump_prediction (FILE *, enum br_predictor, int, basic_block,
87 enum predictor_reason, edge);
88 static void predict_paths_leading_to (basic_block, enum br_predictor,
89 enum prediction,
90 struct loop *in_loop = NULL);
91 static void predict_paths_leading_to_edge (edge, enum br_predictor,
92 enum prediction,
93 struct loop *in_loop = NULL);
94 static bool can_predict_insn_p (const rtx_insn *);
96 /* Information we hold about each branch predictor.
97 Filled using information from predict.def. */
99 struct predictor_info
101 const char *const name; /* Name used in the debugging dumps. */
102 const int hitrate; /* Expected hitrate used by
103 predict_insn_def call. */
104 const int flags;
107 /* Use given predictor without Dempster-Shaffer theory if it matches
108 using first_match heuristics. */
109 #define PRED_FLAG_FIRST_MATCH 1
111 /* Recompute hitrate in percent to our representation. */
113 #define HITRATE(VAL) ((int) ((VAL) * REG_BR_PROB_BASE + 50) / 100)
115 #define DEF_PREDICTOR(ENUM, NAME, HITRATE, FLAGS) {NAME, HITRATE, FLAGS},
116 static const struct predictor_info predictor_info[]= {
117 #include "predict.def"
119 /* Upper bound on predictors. */
120 {NULL, 0, 0}
122 #undef DEF_PREDICTOR
124 /* Return TRUE if frequency FREQ is considered to be hot. */
126 static inline bool
127 maybe_hot_frequency_p (struct function *fun, int freq)
129 struct cgraph_node *node = cgraph_node::get (fun->decl);
130 if (!profile_info || profile_status_for_fn (fun) != PROFILE_READ)
132 if (node->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
133 return false;
134 if (node->frequency == NODE_FREQUENCY_HOT)
135 return true;
137 if (profile_status_for_fn (fun) == PROFILE_ABSENT)
138 return true;
139 if (node->frequency == NODE_FREQUENCY_EXECUTED_ONCE
140 && freq < (ENTRY_BLOCK_PTR_FOR_FN (fun)->count.to_frequency (cfun) * 2 / 3))
141 return false;
142 if (PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION) == 0)
143 return false;
144 if (freq * PARAM_VALUE (HOT_BB_FREQUENCY_FRACTION)
145 < ENTRY_BLOCK_PTR_FOR_FN (fun)->count.to_frequency (cfun))
146 return false;
147 return true;
150 static gcov_type min_count = -1;
152 /* Determine the threshold for hot BB counts. */
154 gcov_type
155 get_hot_bb_threshold ()
157 gcov_working_set_t *ws;
158 if (min_count == -1)
160 ws = find_working_set (PARAM_VALUE (HOT_BB_COUNT_WS_PERMILLE));
161 gcc_assert (ws);
162 min_count = ws->min_counter;
164 return min_count;
167 /* Set the threshold for hot BB counts. */
169 void
170 set_hot_bb_threshold (gcov_type min)
172 min_count = min;
175 /* Return TRUE if frequency FREQ is considered to be hot. */
177 bool
178 maybe_hot_count_p (struct function *fun, profile_count count)
180 if (!count.initialized_p ())
181 return true;
182 if (!count.ipa_p ())
183 return maybe_hot_frequency_p (fun, count.to_frequency (fun));
184 if (count.ipa () == profile_count::zero ())
185 return false;
186 /* Code executed at most once is not hot. */
187 if (count <= MAX (profile_info ? profile_info->runs : 1, 1))
188 return false;
189 return (count.to_gcov_type () >= get_hot_bb_threshold ());
192 /* Return true in case BB can be CPU intensive and should be optimized
193 for maximal performance. */
195 bool
196 maybe_hot_bb_p (struct function *fun, const_basic_block bb)
198 gcc_checking_assert (fun);
199 return maybe_hot_count_p (fun, bb->count);
202 /* Return true in case BB can be CPU intensive and should be optimized
203 for maximal performance. */
205 bool
206 maybe_hot_edge_p (edge e)
208 return maybe_hot_count_p (cfun, e->count ());
211 /* Return true if profile COUNT and FREQUENCY, or function FUN static
212 node frequency reflects never being executed. */
214 static bool
215 probably_never_executed (struct function *fun,
216 profile_count count)
218 gcc_checking_assert (fun);
219 if (count == profile_count::zero ())
220 return true;
221 if (count.initialized_p () && profile_status_for_fn (fun) == PROFILE_READ)
223 int unlikely_count_fraction = PARAM_VALUE (UNLIKELY_BB_COUNT_FRACTION);
224 if (count.apply_scale (unlikely_count_fraction, 1) >= profile_info->runs)
225 return false;
226 return true;
228 if ((!profile_info || profile_status_for_fn (fun) != PROFILE_READ)
229 && (cgraph_node::get (fun->decl)->frequency
230 == NODE_FREQUENCY_UNLIKELY_EXECUTED))
231 return true;
232 return false;
236 /* Return true in case BB is probably never executed. */
238 bool
239 probably_never_executed_bb_p (struct function *fun, const_basic_block bb)
241 return probably_never_executed (fun, bb->count);
245 /* Return true if E is unlikely executed for obvious reasons. */
247 static bool
248 unlikely_executed_edge_p (edge e)
250 return (e->count () == profile_count::zero ()
251 || e->probability == profile_probability::never ())
252 || (e->flags & (EDGE_EH | EDGE_FAKE));
255 /* Return true in case edge E is probably never executed. */
257 bool
258 probably_never_executed_edge_p (struct function *fun, edge e)
260 if (unlikely_executed_edge_p (e))
261 return true;
262 return probably_never_executed (fun, e->count ());
265 /* Return true when current function should always be optimized for size. */
267 bool
268 optimize_function_for_size_p (struct function *fun)
270 if (!fun || !fun->decl)
271 return optimize_size;
272 cgraph_node *n = cgraph_node::get (fun->decl);
273 return n && n->optimize_for_size_p ();
276 /* Return true when current function should always be optimized for speed. */
278 bool
279 optimize_function_for_speed_p (struct function *fun)
281 return !optimize_function_for_size_p (fun);
284 /* Return the optimization type that should be used for the function FUN. */
286 optimization_type
287 function_optimization_type (struct function *fun)
289 return (optimize_function_for_speed_p (fun)
290 ? OPTIMIZE_FOR_SPEED
291 : OPTIMIZE_FOR_SIZE);
294 /* Return TRUE when BB should be optimized for size. */
296 bool
297 optimize_bb_for_size_p (const_basic_block bb)
299 return (optimize_function_for_size_p (cfun)
300 || (bb && !maybe_hot_bb_p (cfun, bb)));
303 /* Return TRUE when BB should be optimized for speed. */
305 bool
306 optimize_bb_for_speed_p (const_basic_block bb)
308 return !optimize_bb_for_size_p (bb);
311 /* Return the optimization type that should be used for block BB. */
313 optimization_type
314 bb_optimization_type (const_basic_block bb)
316 return (optimize_bb_for_speed_p (bb)
317 ? OPTIMIZE_FOR_SPEED
318 : OPTIMIZE_FOR_SIZE);
321 /* Return TRUE when BB should be optimized for size. */
323 bool
324 optimize_edge_for_size_p (edge e)
326 return optimize_function_for_size_p (cfun) || !maybe_hot_edge_p (e);
329 /* Return TRUE when BB should be optimized for speed. */
331 bool
332 optimize_edge_for_speed_p (edge e)
334 return !optimize_edge_for_size_p (e);
337 /* Return TRUE when BB should be optimized for size. */
339 bool
340 optimize_insn_for_size_p (void)
342 return optimize_function_for_size_p (cfun) || !crtl->maybe_hot_insn_p;
345 /* Return TRUE when BB should be optimized for speed. */
347 bool
348 optimize_insn_for_speed_p (void)
350 return !optimize_insn_for_size_p ();
353 /* Return TRUE when LOOP should be optimized for size. */
355 bool
356 optimize_loop_for_size_p (struct loop *loop)
358 return optimize_bb_for_size_p (loop->header);
361 /* Return TRUE when LOOP should be optimized for speed. */
363 bool
364 optimize_loop_for_speed_p (struct loop *loop)
366 return optimize_bb_for_speed_p (loop->header);
369 /* Return TRUE when LOOP nest should be optimized for speed. */
371 bool
372 optimize_loop_nest_for_speed_p (struct loop *loop)
374 struct loop *l = loop;
375 if (optimize_loop_for_speed_p (loop))
376 return true;
377 l = loop->inner;
378 while (l && l != loop)
380 if (optimize_loop_for_speed_p (l))
381 return true;
382 if (l->inner)
383 l = l->inner;
384 else if (l->next)
385 l = l->next;
386 else
388 while (l != loop && !l->next)
389 l = loop_outer (l);
390 if (l != loop)
391 l = l->next;
394 return false;
397 /* Return TRUE when LOOP nest should be optimized for size. */
399 bool
400 optimize_loop_nest_for_size_p (struct loop *loop)
402 return !optimize_loop_nest_for_speed_p (loop);
405 /* Return true when edge E is likely to be well predictable by branch
406 predictor. */
408 bool
409 predictable_edge_p (edge e)
411 if (!e->probability.initialized_p ())
412 return false;
413 if ((e->probability.to_reg_br_prob_base ()
414 <= PARAM_VALUE (PARAM_PREDICTABLE_BRANCH_OUTCOME) * REG_BR_PROB_BASE / 100)
415 || (REG_BR_PROB_BASE - e->probability.to_reg_br_prob_base ()
416 <= PARAM_VALUE (PARAM_PREDICTABLE_BRANCH_OUTCOME) * REG_BR_PROB_BASE / 100))
417 return true;
418 return false;
422 /* Set RTL expansion for BB profile. */
424 void
425 rtl_profile_for_bb (basic_block bb)
427 crtl->maybe_hot_insn_p = maybe_hot_bb_p (cfun, bb);
430 /* Set RTL expansion for edge profile. */
432 void
433 rtl_profile_for_edge (edge e)
435 crtl->maybe_hot_insn_p = maybe_hot_edge_p (e);
438 /* Set RTL expansion to default mode (i.e. when profile info is not known). */
439 void
440 default_rtl_profile (void)
442 crtl->maybe_hot_insn_p = true;
445 /* Return true if the one of outgoing edges is already predicted by
446 PREDICTOR. */
448 bool
449 rtl_predicted_by_p (const_basic_block bb, enum br_predictor predictor)
451 rtx note;
452 if (!INSN_P (BB_END (bb)))
453 return false;
454 for (note = REG_NOTES (BB_END (bb)); note; note = XEXP (note, 1))
455 if (REG_NOTE_KIND (note) == REG_BR_PRED
456 && INTVAL (XEXP (XEXP (note, 0), 0)) == (int)predictor)
457 return true;
458 return false;
461 /* Structure representing predictions in tree level. */
463 struct edge_prediction {
464 struct edge_prediction *ep_next;
465 edge ep_edge;
466 enum br_predictor ep_predictor;
467 int ep_probability;
470 /* This map contains for a basic block the list of predictions for the
471 outgoing edges. */
473 static hash_map<const_basic_block, edge_prediction *> *bb_predictions;
475 /* Return true if the one of outgoing edges is already predicted by
476 PREDICTOR. */
478 bool
479 gimple_predicted_by_p (const_basic_block bb, enum br_predictor predictor)
481 struct edge_prediction *i;
482 edge_prediction **preds = bb_predictions->get (bb);
484 if (!preds)
485 return false;
487 for (i = *preds; i; i = i->ep_next)
488 if (i->ep_predictor == predictor)
489 return true;
490 return false;
493 /* Return true if the one of outgoing edges is already predicted by
494 PREDICTOR for edge E predicted as TAKEN. */
496 bool
497 edge_predicted_by_p (edge e, enum br_predictor predictor, bool taken)
499 struct edge_prediction *i;
500 basic_block bb = e->src;
501 edge_prediction **preds = bb_predictions->get (bb);
502 if (!preds)
503 return false;
505 int probability = predictor_info[(int) predictor].hitrate;
507 if (taken != TAKEN)
508 probability = REG_BR_PROB_BASE - probability;
510 for (i = *preds; i; i = i->ep_next)
511 if (i->ep_predictor == predictor
512 && i->ep_edge == e
513 && i->ep_probability == probability)
514 return true;
515 return false;
518 /* Same predicate as above, working on edges. */
519 bool
520 edge_probability_reliable_p (const_edge e)
522 return e->probability.probably_reliable_p ();
525 /* Same predicate as edge_probability_reliable_p, working on notes. */
526 bool
527 br_prob_note_reliable_p (const_rtx note)
529 gcc_assert (REG_NOTE_KIND (note) == REG_BR_PROB);
530 return profile_probability::from_reg_br_prob_note
531 (XINT (note, 0)).probably_reliable_p ();
534 static void
535 predict_insn (rtx_insn *insn, enum br_predictor predictor, int probability)
537 gcc_assert (any_condjump_p (insn));
538 if (!flag_guess_branch_prob)
539 return;
541 add_reg_note (insn, REG_BR_PRED,
542 gen_rtx_CONCAT (VOIDmode,
543 GEN_INT ((int) predictor),
544 GEN_INT ((int) probability)));
547 /* Predict insn by given predictor. */
549 void
550 predict_insn_def (rtx_insn *insn, enum br_predictor predictor,
551 enum prediction taken)
553 int probability = predictor_info[(int) predictor].hitrate;
555 if (taken != TAKEN)
556 probability = REG_BR_PROB_BASE - probability;
558 predict_insn (insn, predictor, probability);
561 /* Predict edge E with given probability if possible. */
563 void
564 rtl_predict_edge (edge e, enum br_predictor predictor, int probability)
566 rtx_insn *last_insn;
567 last_insn = BB_END (e->src);
569 /* We can store the branch prediction information only about
570 conditional jumps. */
571 if (!any_condjump_p (last_insn))
572 return;
574 /* We always store probability of branching. */
575 if (e->flags & EDGE_FALLTHRU)
576 probability = REG_BR_PROB_BASE - probability;
578 predict_insn (last_insn, predictor, probability);
581 /* Predict edge E with the given PROBABILITY. */
582 void
583 gimple_predict_edge (edge e, enum br_predictor predictor, int probability)
585 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
586 && EDGE_COUNT (e->src->succs) > 1
587 && flag_guess_branch_prob
588 && optimize)
590 struct edge_prediction *i = XNEW (struct edge_prediction);
591 edge_prediction *&preds = bb_predictions->get_or_insert (e->src);
593 i->ep_next = preds;
594 preds = i;
595 i->ep_probability = probability;
596 i->ep_predictor = predictor;
597 i->ep_edge = e;
601 /* Filter edge predictions PREDS by a function FILTER. DATA are passed
602 to the filter function. */
604 void
605 filter_predictions (edge_prediction **preds,
606 bool (*filter) (edge_prediction *, void *), void *data)
608 if (!bb_predictions)
609 return;
611 if (preds)
613 struct edge_prediction **prediction = preds;
614 struct edge_prediction *next;
616 while (*prediction)
618 if ((*filter) (*prediction, data))
619 prediction = &((*prediction)->ep_next);
620 else
622 next = (*prediction)->ep_next;
623 free (*prediction);
624 *prediction = next;
630 /* Filter function predicate that returns true for a edge predicate P
631 if its edge is equal to DATA. */
633 bool
634 equal_edge_p (edge_prediction *p, void *data)
636 return p->ep_edge == (edge)data;
639 /* Remove all predictions on given basic block that are attached
640 to edge E. */
641 void
642 remove_predictions_associated_with_edge (edge e)
644 if (!bb_predictions)
645 return;
647 edge_prediction **preds = bb_predictions->get (e->src);
648 filter_predictions (preds, equal_edge_p, e);
651 /* Clears the list of predictions stored for BB. */
653 static void
654 clear_bb_predictions (basic_block bb)
656 edge_prediction **preds = bb_predictions->get (bb);
657 struct edge_prediction *pred, *next;
659 if (!preds)
660 return;
662 for (pred = *preds; pred; pred = next)
664 next = pred->ep_next;
665 free (pred);
667 *preds = NULL;
670 /* Return true when we can store prediction on insn INSN.
671 At the moment we represent predictions only on conditional
672 jumps, not at computed jump or other complicated cases. */
673 static bool
674 can_predict_insn_p (const rtx_insn *insn)
676 return (JUMP_P (insn)
677 && any_condjump_p (insn)
678 && EDGE_COUNT (BLOCK_FOR_INSN (insn)->succs) >= 2);
681 /* Predict edge E by given predictor if possible. */
683 void
684 predict_edge_def (edge e, enum br_predictor predictor,
685 enum prediction taken)
687 int probability = predictor_info[(int) predictor].hitrate;
689 if (taken != TAKEN)
690 probability = REG_BR_PROB_BASE - probability;
692 predict_edge (e, predictor, probability);
695 /* Invert all branch predictions or probability notes in the INSN. This needs
696 to be done each time we invert the condition used by the jump. */
698 void
699 invert_br_probabilities (rtx insn)
701 rtx note;
703 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
704 if (REG_NOTE_KIND (note) == REG_BR_PROB)
705 XINT (note, 0) = profile_probability::from_reg_br_prob_note
706 (XINT (note, 0)).invert ().to_reg_br_prob_note ();
707 else if (REG_NOTE_KIND (note) == REG_BR_PRED)
708 XEXP (XEXP (note, 0), 1)
709 = GEN_INT (REG_BR_PROB_BASE - INTVAL (XEXP (XEXP (note, 0), 1)));
712 /* Dump information about the branch prediction to the output file. */
714 static void
715 dump_prediction (FILE *file, enum br_predictor predictor, int probability,
716 basic_block bb, enum predictor_reason reason = REASON_NONE,
717 edge ep_edge = NULL)
719 edge e = ep_edge;
720 edge_iterator ei;
722 if (!file)
723 return;
725 if (e == NULL)
726 FOR_EACH_EDGE (e, ei, bb->succs)
727 if (! (e->flags & EDGE_FALLTHRU))
728 break;
730 char edge_info_str[128];
731 if (ep_edge)
732 sprintf (edge_info_str, " of edge %d->%d", ep_edge->src->index,
733 ep_edge->dest->index);
734 else
735 edge_info_str[0] = '\0';
737 fprintf (file, " %s heuristics%s%s: %.1f%%",
738 predictor_info[predictor].name,
739 edge_info_str, reason_messages[reason],
740 probability * 100.0 / REG_BR_PROB_BASE);
742 if (bb->count.initialized_p ())
744 fprintf (file, " exec ");
745 bb->count.dump (file);
746 if (e)
748 fprintf (file, " hit ");
749 e->count ().dump (file);
750 fprintf (file, " (%.1f%%)", e->count ().to_gcov_type() * 100.0
751 / bb->count.to_gcov_type ());
755 fprintf (file, "\n");
758 /* Return true if STMT is known to be unlikely executed. */
760 static bool
761 unlikely_executed_stmt_p (gimple *stmt)
763 if (!is_gimple_call (stmt))
764 return false;
765 /* NORETURN attribute alone is not strong enough: exit() may be quite
766 likely executed once during program run. */
767 if (gimple_call_fntype (stmt)
768 && lookup_attribute ("cold",
769 TYPE_ATTRIBUTES (gimple_call_fntype (stmt)))
770 && !lookup_attribute ("cold", DECL_ATTRIBUTES (current_function_decl)))
771 return true;
772 tree decl = gimple_call_fndecl (stmt);
773 if (!decl)
774 return false;
775 if (lookup_attribute ("cold", DECL_ATTRIBUTES (decl))
776 && !lookup_attribute ("cold", DECL_ATTRIBUTES (current_function_decl)))
777 return true;
779 cgraph_node *n = cgraph_node::get (decl);
780 if (!n)
781 return false;
783 availability avail;
784 n = n->ultimate_alias_target (&avail);
785 if (avail < AVAIL_AVAILABLE)
786 return false;
787 if (!n->analyzed
788 || n->decl == current_function_decl)
789 return false;
790 return n->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED;
793 /* Return true if BB is unlikely executed. */
795 static bool
796 unlikely_executed_bb_p (basic_block bb)
798 if (bb->count == profile_count::zero ())
799 return true;
800 if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun) || bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
801 return false;
802 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
803 !gsi_end_p (gsi); gsi_next (&gsi))
805 if (unlikely_executed_stmt_p (gsi_stmt (gsi)))
806 return true;
807 if (stmt_can_terminate_bb_p (gsi_stmt (gsi)))
808 return false;
810 return false;
813 /* We can not predict the probabilities of outgoing edges of bb. Set them
814 evenly and hope for the best. If UNLIKELY_EDGES is not null, distribute
815 even probability for all edges not mentioned in the set. These edges
816 are given PROB_VERY_UNLIKELY probability. */
818 static void
819 set_even_probabilities (basic_block bb,
820 hash_set<edge> *unlikely_edges = NULL)
822 unsigned nedges = 0, unlikely_count = 0;
823 edge e = NULL;
824 edge_iterator ei;
825 profile_probability all = profile_probability::always ();
827 FOR_EACH_EDGE (e, ei, bb->succs)
828 if (e->probability.initialized_p ())
829 all -= e->probability;
830 else if (!unlikely_executed_edge_p (e))
832 nedges ++;
833 if (unlikely_edges != NULL && unlikely_edges->contains (e))
835 all -= profile_probability::very_unlikely ();
836 unlikely_count++;
840 /* Make the distribution even if all edges are unlikely. */
841 if (unlikely_count == nedges)
843 unlikely_edges = NULL;
844 unlikely_count = 0;
847 unsigned c = nedges - unlikely_count;
849 FOR_EACH_EDGE (e, ei, bb->succs)
850 if (e->probability.initialized_p ())
852 else if (!unlikely_executed_edge_p (e))
854 if (unlikely_edges != NULL && unlikely_edges->contains (e))
855 e->probability = profile_probability::very_unlikely ();
856 else
857 e->probability = all.apply_scale (1, c).guessed ();
859 else
860 e->probability = profile_probability::never ();
863 /* Add REG_BR_PROB note to JUMP with PROB. */
865 void
866 add_reg_br_prob_note (rtx_insn *jump, profile_probability prob)
868 gcc_checking_assert (JUMP_P (jump) && !find_reg_note (jump, REG_BR_PROB, 0));
869 add_int_reg_note (jump, REG_BR_PROB, prob.to_reg_br_prob_note ());
872 /* Combine all REG_BR_PRED notes into single probability and attach REG_BR_PROB
873 note if not already present. Remove now useless REG_BR_PRED notes. */
875 static void
876 combine_predictions_for_insn (rtx_insn *insn, basic_block bb)
878 rtx prob_note;
879 rtx *pnote;
880 rtx note;
881 int best_probability = PROB_EVEN;
882 enum br_predictor best_predictor = END_PREDICTORS;
883 int combined_probability = REG_BR_PROB_BASE / 2;
884 int d;
885 bool first_match = false;
886 bool found = false;
888 if (!can_predict_insn_p (insn))
890 set_even_probabilities (bb);
891 return;
894 prob_note = find_reg_note (insn, REG_BR_PROB, 0);
895 pnote = &REG_NOTES (insn);
896 if (dump_file)
897 fprintf (dump_file, "Predictions for insn %i bb %i\n", INSN_UID (insn),
898 bb->index);
900 /* We implement "first match" heuristics and use probability guessed
901 by predictor with smallest index. */
902 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
903 if (REG_NOTE_KIND (note) == REG_BR_PRED)
905 enum br_predictor predictor = ((enum br_predictor)
906 INTVAL (XEXP (XEXP (note, 0), 0)));
907 int probability = INTVAL (XEXP (XEXP (note, 0), 1));
909 found = true;
910 if (best_predictor > predictor
911 && predictor_info[predictor].flags & PRED_FLAG_FIRST_MATCH)
912 best_probability = probability, best_predictor = predictor;
914 d = (combined_probability * probability
915 + (REG_BR_PROB_BASE - combined_probability)
916 * (REG_BR_PROB_BASE - probability));
918 /* Use FP math to avoid overflows of 32bit integers. */
919 if (d == 0)
920 /* If one probability is 0% and one 100%, avoid division by zero. */
921 combined_probability = REG_BR_PROB_BASE / 2;
922 else
923 combined_probability = (((double) combined_probability) * probability
924 * REG_BR_PROB_BASE / d + 0.5);
927 /* Decide which heuristic to use. In case we didn't match anything,
928 use no_prediction heuristic, in case we did match, use either
929 first match or Dempster-Shaffer theory depending on the flags. */
931 if (best_predictor != END_PREDICTORS)
932 first_match = true;
934 if (!found)
935 dump_prediction (dump_file, PRED_NO_PREDICTION,
936 combined_probability, bb);
937 else
939 if (!first_match)
940 dump_prediction (dump_file, PRED_DS_THEORY, combined_probability,
941 bb, !first_match ? REASON_NONE : REASON_IGNORED);
942 else
943 dump_prediction (dump_file, PRED_FIRST_MATCH, best_probability,
944 bb, first_match ? REASON_NONE : REASON_IGNORED);
947 if (first_match)
948 combined_probability = best_probability;
949 dump_prediction (dump_file, PRED_COMBINED, combined_probability, bb);
951 while (*pnote)
953 if (REG_NOTE_KIND (*pnote) == REG_BR_PRED)
955 enum br_predictor predictor = ((enum br_predictor)
956 INTVAL (XEXP (XEXP (*pnote, 0), 0)));
957 int probability = INTVAL (XEXP (XEXP (*pnote, 0), 1));
959 dump_prediction (dump_file, predictor, probability, bb,
960 (!first_match || best_predictor == predictor)
961 ? REASON_NONE : REASON_IGNORED);
962 *pnote = XEXP (*pnote, 1);
964 else
965 pnote = &XEXP (*pnote, 1);
968 if (!prob_note)
970 profile_probability p
971 = profile_probability::from_reg_br_prob_base (combined_probability);
972 add_reg_br_prob_note (insn, p);
974 /* Save the prediction into CFG in case we are seeing non-degenerated
975 conditional jump. */
976 if (!single_succ_p (bb))
978 BRANCH_EDGE (bb)->probability = p;
979 FALLTHRU_EDGE (bb)->probability
980 = BRANCH_EDGE (bb)->probability.invert ();
983 else if (!single_succ_p (bb))
985 profile_probability prob = profile_probability::from_reg_br_prob_note
986 (XINT (prob_note, 0));
988 BRANCH_EDGE (bb)->probability = prob;
989 FALLTHRU_EDGE (bb)->probability = prob.invert ();
991 else
992 single_succ_edge (bb)->probability = profile_probability::always ();
995 /* Edge prediction hash traits. */
997 struct predictor_hash: pointer_hash <edge_prediction>
1000 static inline hashval_t hash (const edge_prediction *);
1001 static inline bool equal (const edge_prediction *, const edge_prediction *);
1004 /* Calculate hash value of an edge prediction P based on predictor and
1005 normalized probability. */
1007 inline hashval_t
1008 predictor_hash::hash (const edge_prediction *p)
1010 inchash::hash hstate;
1011 hstate.add_int (p->ep_predictor);
1013 int prob = p->ep_probability;
1014 if (prob > REG_BR_PROB_BASE / 2)
1015 prob = REG_BR_PROB_BASE - prob;
1017 hstate.add_int (prob);
1019 return hstate.end ();
1022 /* Return true whether edge predictions P1 and P2 use the same predictor and
1023 have equal (or opposed probability). */
1025 inline bool
1026 predictor_hash::equal (const edge_prediction *p1, const edge_prediction *p2)
1028 return (p1->ep_predictor == p2->ep_predictor
1029 && (p1->ep_probability == p2->ep_probability
1030 || p1->ep_probability == REG_BR_PROB_BASE - p2->ep_probability));
1033 struct predictor_hash_traits: predictor_hash,
1034 typed_noop_remove <edge_prediction *> {};
1036 /* Return true if edge prediction P is not in DATA hash set. */
1038 static bool
1039 not_removed_prediction_p (edge_prediction *p, void *data)
1041 hash_set<edge_prediction *> *remove = (hash_set<edge_prediction *> *) data;
1042 return !remove->contains (p);
1045 /* Prune predictions for a basic block BB. Currently we do following
1046 clean-up steps:
1048 1) remove duplicate prediction that is guessed with the same probability
1049 (different than 1/2) to both edge
1050 2) remove duplicates for a prediction that belongs with the same probability
1051 to a single edge
1055 static void
1056 prune_predictions_for_bb (basic_block bb)
1058 edge_prediction **preds = bb_predictions->get (bb);
1060 if (preds)
1062 hash_table <predictor_hash_traits> s (13);
1063 hash_set <edge_prediction *> remove;
1065 /* Step 1: identify predictors that should be removed. */
1066 for (edge_prediction *pred = *preds; pred; pred = pred->ep_next)
1068 edge_prediction *existing = s.find (pred);
1069 if (existing)
1071 if (pred->ep_edge == existing->ep_edge
1072 && pred->ep_probability == existing->ep_probability)
1074 /* Remove a duplicate predictor. */
1075 dump_prediction (dump_file, pred->ep_predictor,
1076 pred->ep_probability, bb,
1077 REASON_SINGLE_EDGE_DUPLICATE, pred->ep_edge);
1079 remove.add (pred);
1081 else if (pred->ep_edge != existing->ep_edge
1082 && pred->ep_probability == existing->ep_probability
1083 && pred->ep_probability != REG_BR_PROB_BASE / 2)
1085 /* Remove both predictors as they predict the same
1086 for both edges. */
1087 dump_prediction (dump_file, existing->ep_predictor,
1088 pred->ep_probability, bb,
1089 REASON_EDGE_PAIR_DUPLICATE,
1090 existing->ep_edge);
1091 dump_prediction (dump_file, pred->ep_predictor,
1092 pred->ep_probability, bb,
1093 REASON_EDGE_PAIR_DUPLICATE,
1094 pred->ep_edge);
1096 remove.add (existing);
1097 remove.add (pred);
1101 edge_prediction **slot2 = s.find_slot (pred, INSERT);
1102 *slot2 = pred;
1105 /* Step 2: Remove predictors. */
1106 filter_predictions (preds, not_removed_prediction_p, &remove);
1110 /* Combine predictions into single probability and store them into CFG.
1111 Remove now useless prediction entries.
1112 If DRY_RUN is set, only produce dumps and do not modify profile. */
1114 static void
1115 combine_predictions_for_bb (basic_block bb, bool dry_run)
1117 int best_probability = PROB_EVEN;
1118 enum br_predictor best_predictor = END_PREDICTORS;
1119 int combined_probability = REG_BR_PROB_BASE / 2;
1120 int d;
1121 bool first_match = false;
1122 bool found = false;
1123 struct edge_prediction *pred;
1124 int nedges = 0;
1125 edge e, first = NULL, second = NULL;
1126 edge_iterator ei;
1128 FOR_EACH_EDGE (e, ei, bb->succs)
1129 if (!unlikely_executed_edge_p (e))
1131 nedges ++;
1132 if (first && !second)
1133 second = e;
1134 if (!first)
1135 first = e;
1137 else if (!e->probability.initialized_p ())
1138 e->probability = profile_probability::never ();
1140 /* When there is no successor or only one choice, prediction is easy.
1142 When we have a basic block with more than 2 successors, the situation
1143 is more complicated as DS theory cannot be used literally.
1144 More precisely, let's assume we predicted edge e1 with probability p1,
1145 thus: m1({b1}) = p1. As we're going to combine more than 2 edges, we
1146 need to find probability of e.g. m1({b2}), which we don't know.
1147 The only approximation is to equally distribute 1-p1 to all edges
1148 different from b1.
1150 According to numbers we've got from SPEC2006 benchark, there's only
1151 one interesting reliable predictor (noreturn call), which can be
1152 handled with a bit easier approach. */
1153 if (nedges != 2)
1155 hash_set<edge> unlikely_edges (4);
1157 /* Identify all edges that have a probability close to very unlikely.
1158 Doing the approach for very unlikely doesn't worth for doing as
1159 there's no such probability in SPEC2006 benchmark. */
1160 edge_prediction **preds = bb_predictions->get (bb);
1161 if (preds)
1162 for (pred = *preds; pred; pred = pred->ep_next)
1163 if (pred->ep_probability <= PROB_VERY_UNLIKELY)
1164 unlikely_edges.add (pred->ep_edge);
1166 if (!dry_run)
1167 set_even_probabilities (bb, &unlikely_edges);
1168 clear_bb_predictions (bb);
1169 if (dump_file)
1171 fprintf (dump_file, "Predictions for bb %i\n", bb->index);
1172 if (unlikely_edges.elements () == 0)
1173 fprintf (dump_file,
1174 "%i edges in bb %i predicted to even probabilities\n",
1175 nedges, bb->index);
1176 else
1178 fprintf (dump_file,
1179 "%i edges in bb %i predicted with some unlikely edges\n",
1180 nedges, bb->index);
1181 FOR_EACH_EDGE (e, ei, bb->succs)
1182 if (!unlikely_executed_edge_p (e))
1183 dump_prediction (dump_file, PRED_COMBINED,
1184 e->probability.to_reg_br_prob_base (), bb, REASON_NONE, e);
1187 return;
1190 if (dump_file)
1191 fprintf (dump_file, "Predictions for bb %i\n", bb->index);
1193 prune_predictions_for_bb (bb);
1195 edge_prediction **preds = bb_predictions->get (bb);
1197 if (preds)
1199 /* We implement "first match" heuristics and use probability guessed
1200 by predictor with smallest index. */
1201 for (pred = *preds; pred; pred = pred->ep_next)
1203 enum br_predictor predictor = pred->ep_predictor;
1204 int probability = pred->ep_probability;
1206 if (pred->ep_edge != first)
1207 probability = REG_BR_PROB_BASE - probability;
1209 found = true;
1210 /* First match heuristics would be widly confused if we predicted
1211 both directions. */
1212 if (best_predictor > predictor
1213 && predictor_info[predictor].flags & PRED_FLAG_FIRST_MATCH)
1215 struct edge_prediction *pred2;
1216 int prob = probability;
1218 for (pred2 = (struct edge_prediction *) *preds;
1219 pred2; pred2 = pred2->ep_next)
1220 if (pred2 != pred && pred2->ep_predictor == pred->ep_predictor)
1222 int probability2 = pred2->ep_probability;
1224 if (pred2->ep_edge != first)
1225 probability2 = REG_BR_PROB_BASE - probability2;
1227 if ((probability < REG_BR_PROB_BASE / 2) !=
1228 (probability2 < REG_BR_PROB_BASE / 2))
1229 break;
1231 /* If the same predictor later gave better result, go for it! */
1232 if ((probability >= REG_BR_PROB_BASE / 2 && (probability2 > probability))
1233 || (probability <= REG_BR_PROB_BASE / 2 && (probability2 < probability)))
1234 prob = probability2;
1236 if (!pred2)
1237 best_probability = prob, best_predictor = predictor;
1240 d = (combined_probability * probability
1241 + (REG_BR_PROB_BASE - combined_probability)
1242 * (REG_BR_PROB_BASE - probability));
1244 /* Use FP math to avoid overflows of 32bit integers. */
1245 if (d == 0)
1246 /* If one probability is 0% and one 100%, avoid division by zero. */
1247 combined_probability = REG_BR_PROB_BASE / 2;
1248 else
1249 combined_probability = (((double) combined_probability)
1250 * probability
1251 * REG_BR_PROB_BASE / d + 0.5);
1255 /* Decide which heuristic to use. In case we didn't match anything,
1256 use no_prediction heuristic, in case we did match, use either
1257 first match or Dempster-Shaffer theory depending on the flags. */
1259 if (best_predictor != END_PREDICTORS)
1260 first_match = true;
1262 if (!found)
1263 dump_prediction (dump_file, PRED_NO_PREDICTION, combined_probability, bb);
1264 else
1266 if (!first_match)
1267 dump_prediction (dump_file, PRED_DS_THEORY, combined_probability, bb,
1268 !first_match ? REASON_NONE : REASON_IGNORED);
1269 else
1270 dump_prediction (dump_file, PRED_FIRST_MATCH, best_probability, bb,
1271 first_match ? REASON_NONE : REASON_IGNORED);
1274 if (first_match)
1275 combined_probability = best_probability;
1276 dump_prediction (dump_file, PRED_COMBINED, combined_probability, bb);
1278 if (preds)
1280 for (pred = (struct edge_prediction *) *preds; pred; pred = pred->ep_next)
1282 enum br_predictor predictor = pred->ep_predictor;
1283 int probability = pred->ep_probability;
1285 dump_prediction (dump_file, predictor, probability, bb,
1286 (!first_match || best_predictor == predictor)
1287 ? REASON_NONE : REASON_IGNORED, pred->ep_edge);
1290 clear_bb_predictions (bb);
1292 if ((!bb->count.nonzero_p () || !first->probability.initialized_p ())
1293 && !dry_run)
1295 first->probability
1296 = profile_probability::from_reg_br_prob_base (combined_probability);
1297 second->probability = first->probability.invert ();
1301 /* Check if T1 and T2 satisfy the IV_COMPARE condition.
1302 Return the SSA_NAME if the condition satisfies, NULL otherwise.
1304 T1 and T2 should be one of the following cases:
1305 1. T1 is SSA_NAME, T2 is NULL
1306 2. T1 is SSA_NAME, T2 is INTEGER_CST between [-4, 4]
1307 3. T2 is SSA_NAME, T1 is INTEGER_CST between [-4, 4] */
1309 static tree
1310 strips_small_constant (tree t1, tree t2)
1312 tree ret = NULL;
1313 int value = 0;
1315 if (!t1)
1316 return NULL;
1317 else if (TREE_CODE (t1) == SSA_NAME)
1318 ret = t1;
1319 else if (tree_fits_shwi_p (t1))
1320 value = tree_to_shwi (t1);
1321 else
1322 return NULL;
1324 if (!t2)
1325 return ret;
1326 else if (tree_fits_shwi_p (t2))
1327 value = tree_to_shwi (t2);
1328 else if (TREE_CODE (t2) == SSA_NAME)
1330 if (ret)
1331 return NULL;
1332 else
1333 ret = t2;
1336 if (value <= 4 && value >= -4)
1337 return ret;
1338 else
1339 return NULL;
1342 /* Return the SSA_NAME in T or T's operands.
1343 Return NULL if SSA_NAME cannot be found. */
1345 static tree
1346 get_base_value (tree t)
1348 if (TREE_CODE (t) == SSA_NAME)
1349 return t;
1351 if (!BINARY_CLASS_P (t))
1352 return NULL;
1354 switch (TREE_OPERAND_LENGTH (t))
1356 case 1:
1357 return strips_small_constant (TREE_OPERAND (t, 0), NULL);
1358 case 2:
1359 return strips_small_constant (TREE_OPERAND (t, 0),
1360 TREE_OPERAND (t, 1));
1361 default:
1362 return NULL;
1366 /* Check the compare STMT in LOOP. If it compares an induction
1367 variable to a loop invariant, return true, and save
1368 LOOP_INVARIANT, COMPARE_CODE and LOOP_STEP.
1369 Otherwise return false and set LOOP_INVAIANT to NULL. */
1371 static bool
1372 is_comparison_with_loop_invariant_p (gcond *stmt, struct loop *loop,
1373 tree *loop_invariant,
1374 enum tree_code *compare_code,
1375 tree *loop_step,
1376 tree *loop_iv_base)
1378 tree op0, op1, bound, base;
1379 affine_iv iv0, iv1;
1380 enum tree_code code;
1381 tree step;
1383 code = gimple_cond_code (stmt);
1384 *loop_invariant = NULL;
1386 switch (code)
1388 case GT_EXPR:
1389 case GE_EXPR:
1390 case NE_EXPR:
1391 case LT_EXPR:
1392 case LE_EXPR:
1393 case EQ_EXPR:
1394 break;
1396 default:
1397 return false;
1400 op0 = gimple_cond_lhs (stmt);
1401 op1 = gimple_cond_rhs (stmt);
1403 if ((TREE_CODE (op0) != SSA_NAME && TREE_CODE (op0) != INTEGER_CST)
1404 || (TREE_CODE (op1) != SSA_NAME && TREE_CODE (op1) != INTEGER_CST))
1405 return false;
1406 if (!simple_iv (loop, loop_containing_stmt (stmt), op0, &iv0, true))
1407 return false;
1408 if (!simple_iv (loop, loop_containing_stmt (stmt), op1, &iv1, true))
1409 return false;
1410 if (TREE_CODE (iv0.step) != INTEGER_CST
1411 || TREE_CODE (iv1.step) != INTEGER_CST)
1412 return false;
1413 if ((integer_zerop (iv0.step) && integer_zerop (iv1.step))
1414 || (!integer_zerop (iv0.step) && !integer_zerop (iv1.step)))
1415 return false;
1417 if (integer_zerop (iv0.step))
1419 if (code != NE_EXPR && code != EQ_EXPR)
1420 code = invert_tree_comparison (code, false);
1421 bound = iv0.base;
1422 base = iv1.base;
1423 if (tree_fits_shwi_p (iv1.step))
1424 step = iv1.step;
1425 else
1426 return false;
1428 else
1430 bound = iv1.base;
1431 base = iv0.base;
1432 if (tree_fits_shwi_p (iv0.step))
1433 step = iv0.step;
1434 else
1435 return false;
1438 if (TREE_CODE (bound) != INTEGER_CST)
1439 bound = get_base_value (bound);
1440 if (!bound)
1441 return false;
1442 if (TREE_CODE (base) != INTEGER_CST)
1443 base = get_base_value (base);
1444 if (!base)
1445 return false;
1447 *loop_invariant = bound;
1448 *compare_code = code;
1449 *loop_step = step;
1450 *loop_iv_base = base;
1451 return true;
1454 /* Compare two SSA_NAMEs: returns TRUE if T1 and T2 are value coherent. */
1456 static bool
1457 expr_coherent_p (tree t1, tree t2)
1459 gimple *stmt;
1460 tree ssa_name_1 = NULL;
1461 tree ssa_name_2 = NULL;
1463 gcc_assert (TREE_CODE (t1) == SSA_NAME || TREE_CODE (t1) == INTEGER_CST);
1464 gcc_assert (TREE_CODE (t2) == SSA_NAME || TREE_CODE (t2) == INTEGER_CST);
1466 if (t1 == t2)
1467 return true;
1469 if (TREE_CODE (t1) == INTEGER_CST && TREE_CODE (t2) == INTEGER_CST)
1470 return true;
1471 if (TREE_CODE (t1) == INTEGER_CST || TREE_CODE (t2) == INTEGER_CST)
1472 return false;
1474 /* Check to see if t1 is expressed/defined with t2. */
1475 stmt = SSA_NAME_DEF_STMT (t1);
1476 gcc_assert (stmt != NULL);
1477 if (is_gimple_assign (stmt))
1479 ssa_name_1 = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_USE);
1480 if (ssa_name_1 && ssa_name_1 == t2)
1481 return true;
1484 /* Check to see if t2 is expressed/defined with t1. */
1485 stmt = SSA_NAME_DEF_STMT (t2);
1486 gcc_assert (stmt != NULL);
1487 if (is_gimple_assign (stmt))
1489 ssa_name_2 = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_USE);
1490 if (ssa_name_2 && ssa_name_2 == t1)
1491 return true;
1494 /* Compare if t1 and t2's def_stmts are identical. */
1495 if (ssa_name_2 != NULL && ssa_name_1 == ssa_name_2)
1496 return true;
1497 else
1498 return false;
1501 /* Return true if E is predicted by one of loop heuristics. */
1503 static bool
1504 predicted_by_loop_heuristics_p (basic_block bb)
1506 struct edge_prediction *i;
1507 edge_prediction **preds = bb_predictions->get (bb);
1509 if (!preds)
1510 return false;
1512 for (i = *preds; i; i = i->ep_next)
1513 if (i->ep_predictor == PRED_LOOP_ITERATIONS_GUESSED
1514 || i->ep_predictor == PRED_LOOP_ITERATIONS_MAX
1515 || i->ep_predictor == PRED_LOOP_ITERATIONS
1516 || i->ep_predictor == PRED_LOOP_EXIT
1517 || i->ep_predictor == PRED_LOOP_EXIT_WITH_RECURSION
1518 || i->ep_predictor == PRED_LOOP_EXTRA_EXIT)
1519 return true;
1520 return false;
1523 /* Predict branch probability of BB when BB contains a branch that compares
1524 an induction variable in LOOP with LOOP_IV_BASE_VAR to LOOP_BOUND_VAR. The
1525 loop exit is compared using LOOP_BOUND_CODE, with step of LOOP_BOUND_STEP.
1527 E.g.
1528 for (int i = 0; i < bound; i++) {
1529 if (i < bound - 2)
1530 computation_1();
1531 else
1532 computation_2();
1535 In this loop, we will predict the branch inside the loop to be taken. */
1537 static void
1538 predict_iv_comparison (struct loop *loop, basic_block bb,
1539 tree loop_bound_var,
1540 tree loop_iv_base_var,
1541 enum tree_code loop_bound_code,
1542 int loop_bound_step)
1544 gimple *stmt;
1545 tree compare_var, compare_base;
1546 enum tree_code compare_code;
1547 tree compare_step_var;
1548 edge then_edge;
1549 edge_iterator ei;
1551 if (predicted_by_loop_heuristics_p (bb))
1552 return;
1554 stmt = last_stmt (bb);
1555 if (!stmt || gimple_code (stmt) != GIMPLE_COND)
1556 return;
1557 if (!is_comparison_with_loop_invariant_p (as_a <gcond *> (stmt),
1558 loop, &compare_var,
1559 &compare_code,
1560 &compare_step_var,
1561 &compare_base))
1562 return;
1564 /* Find the taken edge. */
1565 FOR_EACH_EDGE (then_edge, ei, bb->succs)
1566 if (then_edge->flags & EDGE_TRUE_VALUE)
1567 break;
1569 /* When comparing an IV to a loop invariant, NE is more likely to be
1570 taken while EQ is more likely to be not-taken. */
1571 if (compare_code == NE_EXPR)
1573 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1574 return;
1576 else if (compare_code == EQ_EXPR)
1578 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, NOT_TAKEN);
1579 return;
1582 if (!expr_coherent_p (loop_iv_base_var, compare_base))
1583 return;
1585 /* If loop bound, base and compare bound are all constants, we can
1586 calculate the probability directly. */
1587 if (tree_fits_shwi_p (loop_bound_var)
1588 && tree_fits_shwi_p (compare_var)
1589 && tree_fits_shwi_p (compare_base))
1591 int probability;
1592 bool overflow, overall_overflow = false;
1593 widest_int compare_count, tem;
1595 /* (loop_bound - base) / compare_step */
1596 tem = wi::sub (wi::to_widest (loop_bound_var),
1597 wi::to_widest (compare_base), SIGNED, &overflow);
1598 overall_overflow |= overflow;
1599 widest_int loop_count = wi::div_trunc (tem,
1600 wi::to_widest (compare_step_var),
1601 SIGNED, &overflow);
1602 overall_overflow |= overflow;
1604 if (!wi::neg_p (wi::to_widest (compare_step_var))
1605 ^ (compare_code == LT_EXPR || compare_code == LE_EXPR))
1607 /* (loop_bound - compare_bound) / compare_step */
1608 tem = wi::sub (wi::to_widest (loop_bound_var),
1609 wi::to_widest (compare_var), SIGNED, &overflow);
1610 overall_overflow |= overflow;
1611 compare_count = wi::div_trunc (tem, wi::to_widest (compare_step_var),
1612 SIGNED, &overflow);
1613 overall_overflow |= overflow;
1615 else
1617 /* (compare_bound - base) / compare_step */
1618 tem = wi::sub (wi::to_widest (compare_var),
1619 wi::to_widest (compare_base), SIGNED, &overflow);
1620 overall_overflow |= overflow;
1621 compare_count = wi::div_trunc (tem, wi::to_widest (compare_step_var),
1622 SIGNED, &overflow);
1623 overall_overflow |= overflow;
1625 if (compare_code == LE_EXPR || compare_code == GE_EXPR)
1626 ++compare_count;
1627 if (loop_bound_code == LE_EXPR || loop_bound_code == GE_EXPR)
1628 ++loop_count;
1629 if (wi::neg_p (compare_count))
1630 compare_count = 0;
1631 if (wi::neg_p (loop_count))
1632 loop_count = 0;
1633 if (loop_count == 0)
1634 probability = 0;
1635 else if (wi::cmps (compare_count, loop_count) == 1)
1636 probability = REG_BR_PROB_BASE;
1637 else
1639 tem = compare_count * REG_BR_PROB_BASE;
1640 tem = wi::udiv_trunc (tem, loop_count);
1641 probability = tem.to_uhwi ();
1644 /* FIXME: The branch prediction seems broken. It has only 20% hitrate. */
1645 if (!overall_overflow)
1646 predict_edge (then_edge, PRED_LOOP_IV_COMPARE, probability);
1648 return;
1651 if (expr_coherent_p (loop_bound_var, compare_var))
1653 if ((loop_bound_code == LT_EXPR || loop_bound_code == LE_EXPR)
1654 && (compare_code == LT_EXPR || compare_code == LE_EXPR))
1655 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1656 else if ((loop_bound_code == GT_EXPR || loop_bound_code == GE_EXPR)
1657 && (compare_code == GT_EXPR || compare_code == GE_EXPR))
1658 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1659 else if (loop_bound_code == NE_EXPR)
1661 /* If the loop backedge condition is "(i != bound)", we do
1662 the comparison based on the step of IV:
1663 * step < 0 : backedge condition is like (i > bound)
1664 * step > 0 : backedge condition is like (i < bound) */
1665 gcc_assert (loop_bound_step != 0);
1666 if (loop_bound_step > 0
1667 && (compare_code == LT_EXPR
1668 || compare_code == LE_EXPR))
1669 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1670 else if (loop_bound_step < 0
1671 && (compare_code == GT_EXPR
1672 || compare_code == GE_EXPR))
1673 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1674 else
1675 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, NOT_TAKEN);
1677 else
1678 /* The branch is predicted not-taken if loop_bound_code is
1679 opposite with compare_code. */
1680 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, NOT_TAKEN);
1682 else if (expr_coherent_p (loop_iv_base_var, compare_var))
1684 /* For cases like:
1685 for (i = s; i < h; i++)
1686 if (i > s + 2) ....
1687 The branch should be predicted taken. */
1688 if (loop_bound_step > 0
1689 && (compare_code == GT_EXPR || compare_code == GE_EXPR))
1690 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1691 else if (loop_bound_step < 0
1692 && (compare_code == LT_EXPR || compare_code == LE_EXPR))
1693 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1694 else
1695 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, NOT_TAKEN);
1699 /* Predict for extra loop exits that will lead to EXIT_EDGE. The extra loop
1700 exits are resulted from short-circuit conditions that will generate an
1701 if_tmp. E.g.:
1703 if (foo() || global > 10)
1704 break;
1706 This will be translated into:
1708 BB3:
1709 loop header...
1710 BB4:
1711 if foo() goto BB6 else goto BB5
1712 BB5:
1713 if global > 10 goto BB6 else goto BB7
1714 BB6:
1715 goto BB7
1716 BB7:
1717 iftmp = (PHI 0(BB5), 1(BB6))
1718 if iftmp == 1 goto BB8 else goto BB3
1719 BB8:
1720 outside of the loop...
1722 The edge BB7->BB8 is loop exit because BB8 is outside of the loop.
1723 From the dataflow, we can infer that BB4->BB6 and BB5->BB6 are also loop
1724 exits. This function takes BB7->BB8 as input, and finds out the extra loop
1725 exits to predict them using PRED_LOOP_EXTRA_EXIT. */
1727 static void
1728 predict_extra_loop_exits (edge exit_edge)
1730 unsigned i;
1731 bool check_value_one;
1732 gimple *lhs_def_stmt;
1733 gphi *phi_stmt;
1734 tree cmp_rhs, cmp_lhs;
1735 gimple *last;
1736 gcond *cmp_stmt;
1738 last = last_stmt (exit_edge->src);
1739 if (!last)
1740 return;
1741 cmp_stmt = dyn_cast <gcond *> (last);
1742 if (!cmp_stmt)
1743 return;
1745 cmp_rhs = gimple_cond_rhs (cmp_stmt);
1746 cmp_lhs = gimple_cond_lhs (cmp_stmt);
1747 if (!TREE_CONSTANT (cmp_rhs)
1748 || !(integer_zerop (cmp_rhs) || integer_onep (cmp_rhs)))
1749 return;
1750 if (TREE_CODE (cmp_lhs) != SSA_NAME)
1751 return;
1753 /* If check_value_one is true, only the phi_args with value '1' will lead
1754 to loop exit. Otherwise, only the phi_args with value '0' will lead to
1755 loop exit. */
1756 check_value_one = (((integer_onep (cmp_rhs))
1757 ^ (gimple_cond_code (cmp_stmt) == EQ_EXPR))
1758 ^ ((exit_edge->flags & EDGE_TRUE_VALUE) != 0));
1760 lhs_def_stmt = SSA_NAME_DEF_STMT (cmp_lhs);
1761 if (!lhs_def_stmt)
1762 return;
1764 phi_stmt = dyn_cast <gphi *> (lhs_def_stmt);
1765 if (!phi_stmt)
1766 return;
1768 for (i = 0; i < gimple_phi_num_args (phi_stmt); i++)
1770 edge e1;
1771 edge_iterator ei;
1772 tree val = gimple_phi_arg_def (phi_stmt, i);
1773 edge e = gimple_phi_arg_edge (phi_stmt, i);
1775 if (!TREE_CONSTANT (val) || !(integer_zerop (val) || integer_onep (val)))
1776 continue;
1777 if ((check_value_one ^ integer_onep (val)) == 1)
1778 continue;
1779 if (EDGE_COUNT (e->src->succs) != 1)
1781 predict_paths_leading_to_edge (e, PRED_LOOP_EXTRA_EXIT, NOT_TAKEN);
1782 continue;
1785 FOR_EACH_EDGE (e1, ei, e->src->preds)
1786 predict_paths_leading_to_edge (e1, PRED_LOOP_EXTRA_EXIT, NOT_TAKEN);
1791 /* Predict edge probabilities by exploiting loop structure. */
1793 static void
1794 predict_loops (void)
1796 struct loop *loop;
1797 basic_block bb;
1798 hash_set <struct loop *> with_recursion(10);
1800 FOR_EACH_BB_FN (bb, cfun)
1802 gimple_stmt_iterator gsi;
1803 tree decl;
1805 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1806 if (is_gimple_call (gsi_stmt (gsi))
1807 && (decl = gimple_call_fndecl (gsi_stmt (gsi))) != NULL
1808 && recursive_call_p (current_function_decl, decl))
1810 loop = bb->loop_father;
1811 while (loop && !with_recursion.add (loop))
1812 loop = loop_outer (loop);
1816 /* Try to predict out blocks in a loop that are not part of a
1817 natural loop. */
1818 FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
1820 basic_block bb, *bbs;
1821 unsigned j, n_exits = 0;
1822 vec<edge> exits;
1823 struct tree_niter_desc niter_desc;
1824 edge ex;
1825 struct nb_iter_bound *nb_iter;
1826 enum tree_code loop_bound_code = ERROR_MARK;
1827 tree loop_bound_step = NULL;
1828 tree loop_bound_var = NULL;
1829 tree loop_iv_base = NULL;
1830 gcond *stmt = NULL;
1831 bool recursion = with_recursion.contains (loop);
1833 exits = get_loop_exit_edges (loop);
1834 FOR_EACH_VEC_ELT (exits, j, ex)
1835 if (!unlikely_executed_edge_p (ex) && !(ex->flags & EDGE_ABNORMAL_CALL))
1836 n_exits ++;
1837 if (!n_exits)
1839 exits.release ();
1840 continue;
1843 if (dump_file && (dump_flags & TDF_DETAILS))
1844 fprintf (dump_file, "Predicting loop %i%s with %i exits.\n",
1845 loop->num, recursion ? " (with recursion)":"", n_exits);
1846 if (dump_file && (dump_flags & TDF_DETAILS)
1847 && max_loop_iterations_int (loop) >= 0)
1849 fprintf (dump_file,
1850 "Loop %d iterates at most %i times.\n", loop->num,
1851 (int)max_loop_iterations_int (loop));
1853 if (dump_file && (dump_flags & TDF_DETAILS)
1854 && likely_max_loop_iterations_int (loop) >= 0)
1856 fprintf (dump_file, "Loop %d likely iterates at most %i times.\n",
1857 loop->num, (int)likely_max_loop_iterations_int (loop));
1860 FOR_EACH_VEC_ELT (exits, j, ex)
1862 tree niter = NULL;
1863 HOST_WIDE_INT nitercst;
1864 int max = PARAM_VALUE (PARAM_MAX_PREDICTED_ITERATIONS);
1865 int probability;
1866 enum br_predictor predictor;
1867 widest_int nit;
1869 if (unlikely_executed_edge_p (ex)
1870 || (ex->flags & EDGE_ABNORMAL_CALL))
1871 continue;
1872 /* Loop heuristics do not expect exit conditional to be inside
1873 inner loop. We predict from innermost to outermost loop. */
1874 if (predicted_by_loop_heuristics_p (ex->src))
1876 if (dump_file && (dump_flags & TDF_DETAILS))
1877 fprintf (dump_file, "Skipping exit %i->%i because "
1878 "it is already predicted.\n",
1879 ex->src->index, ex->dest->index);
1880 continue;
1882 predict_extra_loop_exits (ex);
1884 if (number_of_iterations_exit (loop, ex, &niter_desc, false, false))
1885 niter = niter_desc.niter;
1886 if (!niter || TREE_CODE (niter_desc.niter) != INTEGER_CST)
1887 niter = loop_niter_by_eval (loop, ex);
1888 if (dump_file && (dump_flags & TDF_DETAILS)
1889 && TREE_CODE (niter) == INTEGER_CST)
1891 fprintf (dump_file, "Exit %i->%i %d iterates ",
1892 ex->src->index, ex->dest->index,
1893 loop->num);
1894 print_generic_expr (dump_file, niter, TDF_SLIM);
1895 fprintf (dump_file, " times.\n");
1898 if (TREE_CODE (niter) == INTEGER_CST)
1900 if (tree_fits_uhwi_p (niter)
1901 && max
1902 && compare_tree_int (niter, max - 1) == -1)
1903 nitercst = tree_to_uhwi (niter) + 1;
1904 else
1905 nitercst = max;
1906 predictor = PRED_LOOP_ITERATIONS;
1908 /* If we have just one exit and we can derive some information about
1909 the number of iterations of the loop from the statements inside
1910 the loop, use it to predict this exit. */
1911 else if (n_exits == 1
1912 && estimated_stmt_executions (loop, &nit))
1914 if (wi::gtu_p (nit, max))
1915 nitercst = max;
1916 else
1917 nitercst = nit.to_shwi ();
1918 predictor = PRED_LOOP_ITERATIONS_GUESSED;
1920 /* If we have likely upper bound, trust it for very small iteration
1921 counts. Such loops would otherwise get mispredicted by standard
1922 LOOP_EXIT heuristics. */
1923 else if (n_exits == 1
1924 && likely_max_stmt_executions (loop, &nit)
1925 && wi::ltu_p (nit,
1926 RDIV (REG_BR_PROB_BASE,
1927 REG_BR_PROB_BASE
1928 - predictor_info
1929 [recursion
1930 ? PRED_LOOP_EXIT_WITH_RECURSION
1931 : PRED_LOOP_EXIT].hitrate)))
1933 nitercst = nit.to_shwi ();
1934 predictor = PRED_LOOP_ITERATIONS_MAX;
1936 else
1938 if (dump_file && (dump_flags & TDF_DETAILS))
1939 fprintf (dump_file, "Nothing known about exit %i->%i.\n",
1940 ex->src->index, ex->dest->index);
1941 continue;
1944 if (dump_file && (dump_flags & TDF_DETAILS))
1945 fprintf (dump_file, "Recording prediction to %i iterations by %s.\n",
1946 (int)nitercst, predictor_info[predictor].name);
1947 /* If the prediction for number of iterations is zero, do not
1948 predict the exit edges. */
1949 if (nitercst == 0)
1950 continue;
1952 probability = RDIV (REG_BR_PROB_BASE, nitercst);
1953 predict_edge (ex, predictor, probability);
1955 exits.release ();
1957 /* Find information about loop bound variables. */
1958 for (nb_iter = loop->bounds; nb_iter;
1959 nb_iter = nb_iter->next)
1960 if (nb_iter->stmt
1961 && gimple_code (nb_iter->stmt) == GIMPLE_COND)
1963 stmt = as_a <gcond *> (nb_iter->stmt);
1964 break;
1966 if (!stmt && last_stmt (loop->header)
1967 && gimple_code (last_stmt (loop->header)) == GIMPLE_COND)
1968 stmt = as_a <gcond *> (last_stmt (loop->header));
1969 if (stmt)
1970 is_comparison_with_loop_invariant_p (stmt, loop,
1971 &loop_bound_var,
1972 &loop_bound_code,
1973 &loop_bound_step,
1974 &loop_iv_base);
1976 bbs = get_loop_body (loop);
1978 for (j = 0; j < loop->num_nodes; j++)
1980 edge e;
1981 edge_iterator ei;
1983 bb = bbs[j];
1985 /* Bypass loop heuristics on continue statement. These
1986 statements construct loops via "non-loop" constructs
1987 in the source language and are better to be handled
1988 separately. */
1989 if (predicted_by_p (bb, PRED_CONTINUE))
1991 if (dump_file && (dump_flags & TDF_DETAILS))
1992 fprintf (dump_file, "BB %i predicted by continue.\n",
1993 bb->index);
1994 continue;
1997 /* If we already used more reliable loop exit predictors, do not
1998 bother with PRED_LOOP_EXIT. */
1999 if (!predicted_by_loop_heuristics_p (bb))
2001 /* For loop with many exits we don't want to predict all exits
2002 with the pretty large probability, because if all exits are
2003 considered in row, the loop would be predicted to iterate
2004 almost never. The code to divide probability by number of
2005 exits is very rough. It should compute the number of exits
2006 taken in each patch through function (not the overall number
2007 of exits that might be a lot higher for loops with wide switch
2008 statements in them) and compute n-th square root.
2010 We limit the minimal probability by 2% to avoid
2011 EDGE_PROBABILITY_RELIABLE from trusting the branch prediction
2012 as this was causing regression in perl benchmark containing such
2013 a wide loop. */
2015 int probability = ((REG_BR_PROB_BASE
2016 - predictor_info
2017 [recursion
2018 ? PRED_LOOP_EXIT_WITH_RECURSION
2019 : PRED_LOOP_EXIT].hitrate)
2020 / n_exits);
2021 if (probability < HITRATE (2))
2022 probability = HITRATE (2);
2023 FOR_EACH_EDGE (e, ei, bb->succs)
2024 if (e->dest->index < NUM_FIXED_BLOCKS
2025 || !flow_bb_inside_loop_p (loop, e->dest))
2027 if (dump_file && (dump_flags & TDF_DETAILS))
2028 fprintf (dump_file,
2029 "Predicting exit %i->%i with prob %i.\n",
2030 e->src->index, e->dest->index, probability);
2031 predict_edge (e,
2032 recursion ? PRED_LOOP_EXIT_WITH_RECURSION
2033 : PRED_LOOP_EXIT, probability);
2036 if (loop_bound_var)
2037 predict_iv_comparison (loop, bb, loop_bound_var, loop_iv_base,
2038 loop_bound_code,
2039 tree_to_shwi (loop_bound_step));
2042 /* In the following code
2043 for (loop1)
2044 if (cond)
2045 for (loop2)
2046 body;
2047 guess that cond is unlikely. */
2048 if (loop_outer (loop)->num)
2050 basic_block bb = NULL;
2051 edge preheader_edge = loop_preheader_edge (loop);
2053 if (single_pred_p (preheader_edge->src)
2054 && single_succ_p (preheader_edge->src))
2055 preheader_edge = single_pred_edge (preheader_edge->src);
2057 gimple *stmt = last_stmt (preheader_edge->src);
2058 /* Pattern match fortran loop preheader:
2059 _16 = BUILTIN_EXPECT (_15, 1, PRED_FORTRAN_LOOP_PREHEADER);
2060 _17 = (logical(kind=4)) _16;
2061 if (_17 != 0)
2062 goto <bb 11>;
2063 else
2064 goto <bb 13>;
2066 Loop guard branch prediction says nothing about duplicated loop
2067 headers produced by fortran frontend and in this case we want
2068 to predict paths leading to this preheader. */
2070 if (stmt
2071 && gimple_code (stmt) == GIMPLE_COND
2072 && gimple_cond_code (stmt) == NE_EXPR
2073 && TREE_CODE (gimple_cond_lhs (stmt)) == SSA_NAME
2074 && integer_zerop (gimple_cond_rhs (stmt)))
2076 gimple *call_stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
2077 if (gimple_code (call_stmt) == GIMPLE_ASSIGN
2078 && gimple_expr_code (call_stmt) == NOP_EXPR
2079 && TREE_CODE (gimple_assign_rhs1 (call_stmt)) == SSA_NAME)
2080 call_stmt = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (call_stmt));
2081 if (gimple_call_internal_p (call_stmt, IFN_BUILTIN_EXPECT)
2082 && TREE_CODE (gimple_call_arg (call_stmt, 2)) == INTEGER_CST
2083 && tree_fits_uhwi_p (gimple_call_arg (call_stmt, 2))
2084 && tree_to_uhwi (gimple_call_arg (call_stmt, 2))
2085 == PRED_FORTRAN_LOOP_PREHEADER)
2086 bb = preheader_edge->src;
2088 if (!bb)
2090 if (!dominated_by_p (CDI_DOMINATORS,
2091 loop_outer (loop)->latch, loop->header))
2092 predict_paths_leading_to_edge (loop_preheader_edge (loop),
2093 recursion
2094 ? PRED_LOOP_GUARD_WITH_RECURSION
2095 : PRED_LOOP_GUARD,
2096 NOT_TAKEN,
2097 loop_outer (loop));
2099 else
2101 if (!dominated_by_p (CDI_DOMINATORS,
2102 loop_outer (loop)->latch, bb))
2103 predict_paths_leading_to (bb,
2104 recursion
2105 ? PRED_LOOP_GUARD_WITH_RECURSION
2106 : PRED_LOOP_GUARD,
2107 NOT_TAKEN,
2108 loop_outer (loop));
2112 /* Free basic blocks from get_loop_body. */
2113 free (bbs);
2117 /* Attempt to predict probabilities of BB outgoing edges using local
2118 properties. */
2119 static void
2120 bb_estimate_probability_locally (basic_block bb)
2122 rtx_insn *last_insn = BB_END (bb);
2123 rtx cond;
2125 if (! can_predict_insn_p (last_insn))
2126 return;
2127 cond = get_condition (last_insn, NULL, false, false);
2128 if (! cond)
2129 return;
2131 /* Try "pointer heuristic."
2132 A comparison ptr == 0 is predicted as false.
2133 Similarly, a comparison ptr1 == ptr2 is predicted as false. */
2134 if (COMPARISON_P (cond)
2135 && ((REG_P (XEXP (cond, 0)) && REG_POINTER (XEXP (cond, 0)))
2136 || (REG_P (XEXP (cond, 1)) && REG_POINTER (XEXP (cond, 1)))))
2138 if (GET_CODE (cond) == EQ)
2139 predict_insn_def (last_insn, PRED_POINTER, NOT_TAKEN);
2140 else if (GET_CODE (cond) == NE)
2141 predict_insn_def (last_insn, PRED_POINTER, TAKEN);
2143 else
2145 /* Try "opcode heuristic."
2146 EQ tests are usually false and NE tests are usually true. Also,
2147 most quantities are positive, so we can make the appropriate guesses
2148 about signed comparisons against zero. */
2149 switch (GET_CODE (cond))
2151 case CONST_INT:
2152 /* Unconditional branch. */
2153 predict_insn_def (last_insn, PRED_UNCONDITIONAL,
2154 cond == const0_rtx ? NOT_TAKEN : TAKEN);
2155 break;
2157 case EQ:
2158 case UNEQ:
2159 /* Floating point comparisons appears to behave in a very
2160 unpredictable way because of special role of = tests in
2161 FP code. */
2162 if (FLOAT_MODE_P (GET_MODE (XEXP (cond, 0))))
2164 /* Comparisons with 0 are often used for booleans and there is
2165 nothing useful to predict about them. */
2166 else if (XEXP (cond, 1) == const0_rtx
2167 || XEXP (cond, 0) == const0_rtx)
2169 else
2170 predict_insn_def (last_insn, PRED_OPCODE_NONEQUAL, NOT_TAKEN);
2171 break;
2173 case NE:
2174 case LTGT:
2175 /* Floating point comparisons appears to behave in a very
2176 unpredictable way because of special role of = tests in
2177 FP code. */
2178 if (FLOAT_MODE_P (GET_MODE (XEXP (cond, 0))))
2180 /* Comparisons with 0 are often used for booleans and there is
2181 nothing useful to predict about them. */
2182 else if (XEXP (cond, 1) == const0_rtx
2183 || XEXP (cond, 0) == const0_rtx)
2185 else
2186 predict_insn_def (last_insn, PRED_OPCODE_NONEQUAL, TAKEN);
2187 break;
2189 case ORDERED:
2190 predict_insn_def (last_insn, PRED_FPOPCODE, TAKEN);
2191 break;
2193 case UNORDERED:
2194 predict_insn_def (last_insn, PRED_FPOPCODE, NOT_TAKEN);
2195 break;
2197 case LE:
2198 case LT:
2199 if (XEXP (cond, 1) == const0_rtx || XEXP (cond, 1) == const1_rtx
2200 || XEXP (cond, 1) == constm1_rtx)
2201 predict_insn_def (last_insn, PRED_OPCODE_POSITIVE, NOT_TAKEN);
2202 break;
2204 case GE:
2205 case GT:
2206 if (XEXP (cond, 1) == const0_rtx || XEXP (cond, 1) == const1_rtx
2207 || XEXP (cond, 1) == constm1_rtx)
2208 predict_insn_def (last_insn, PRED_OPCODE_POSITIVE, TAKEN);
2209 break;
2211 default:
2212 break;
2216 /* Set edge->probability for each successor edge of BB. */
2217 void
2218 guess_outgoing_edge_probabilities (basic_block bb)
2220 bb_estimate_probability_locally (bb);
2221 combine_predictions_for_insn (BB_END (bb), bb);
2224 static tree expr_expected_value (tree, bitmap, enum br_predictor *predictor);
2226 /* Helper function for expr_expected_value. */
2228 static tree
2229 expr_expected_value_1 (tree type, tree op0, enum tree_code code,
2230 tree op1, bitmap visited, enum br_predictor *predictor)
2232 gimple *def;
2234 if (predictor)
2235 *predictor = PRED_UNCONDITIONAL;
2237 if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS)
2239 if (TREE_CONSTANT (op0))
2240 return op0;
2242 if (code == IMAGPART_EXPR)
2244 if (TREE_CODE (TREE_OPERAND (op0, 0)) == SSA_NAME)
2246 def = SSA_NAME_DEF_STMT (TREE_OPERAND (op0, 0));
2247 if (is_gimple_call (def)
2248 && gimple_call_internal_p (def)
2249 && (gimple_call_internal_fn (def)
2250 == IFN_ATOMIC_COMPARE_EXCHANGE))
2252 /* Assume that any given atomic operation has low contention,
2253 and thus the compare-and-swap operation succeeds. */
2254 if (predictor)
2255 *predictor = PRED_COMPARE_AND_SWAP;
2256 return build_one_cst (TREE_TYPE (op0));
2261 if (code != SSA_NAME)
2262 return NULL_TREE;
2264 def = SSA_NAME_DEF_STMT (op0);
2266 /* If we were already here, break the infinite cycle. */
2267 if (!bitmap_set_bit (visited, SSA_NAME_VERSION (op0)))
2268 return NULL;
2270 if (gimple_code (def) == GIMPLE_PHI)
2272 /* All the arguments of the PHI node must have the same constant
2273 length. */
2274 int i, n = gimple_phi_num_args (def);
2275 tree val = NULL, new_val;
2277 for (i = 0; i < n; i++)
2279 tree arg = PHI_ARG_DEF (def, i);
2280 enum br_predictor predictor2;
2282 /* If this PHI has itself as an argument, we cannot
2283 determine the string length of this argument. However,
2284 if we can find an expected constant value for the other
2285 PHI args then we can still be sure that this is
2286 likely a constant. So be optimistic and just
2287 continue with the next argument. */
2288 if (arg == PHI_RESULT (def))
2289 continue;
2291 new_val = expr_expected_value (arg, visited, &predictor2);
2293 /* It is difficult to combine value predictors. Simply assume
2294 that later predictor is weaker and take its prediction. */
2295 if (predictor && *predictor < predictor2)
2296 *predictor = predictor2;
2297 if (!new_val)
2298 return NULL;
2299 if (!val)
2300 val = new_val;
2301 else if (!operand_equal_p (val, new_val, false))
2302 return NULL;
2304 return val;
2306 if (is_gimple_assign (def))
2308 if (gimple_assign_lhs (def) != op0)
2309 return NULL;
2311 return expr_expected_value_1 (TREE_TYPE (gimple_assign_lhs (def)),
2312 gimple_assign_rhs1 (def),
2313 gimple_assign_rhs_code (def),
2314 gimple_assign_rhs2 (def),
2315 visited, predictor);
2318 if (is_gimple_call (def))
2320 tree decl = gimple_call_fndecl (def);
2321 if (!decl)
2323 if (gimple_call_internal_p (def)
2324 && gimple_call_internal_fn (def) == IFN_BUILTIN_EXPECT)
2326 gcc_assert (gimple_call_num_args (def) == 3);
2327 tree val = gimple_call_arg (def, 0);
2328 if (TREE_CONSTANT (val))
2329 return val;
2330 if (predictor)
2332 tree val2 = gimple_call_arg (def, 2);
2333 gcc_assert (TREE_CODE (val2) == INTEGER_CST
2334 && tree_fits_uhwi_p (val2)
2335 && tree_to_uhwi (val2) < END_PREDICTORS);
2336 *predictor = (enum br_predictor) tree_to_uhwi (val2);
2338 return gimple_call_arg (def, 1);
2340 return NULL;
2342 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2343 switch (DECL_FUNCTION_CODE (decl))
2345 case BUILT_IN_EXPECT:
2347 tree val;
2348 if (gimple_call_num_args (def) != 2)
2349 return NULL;
2350 val = gimple_call_arg (def, 0);
2351 if (TREE_CONSTANT (val))
2352 return val;
2353 if (predictor)
2354 *predictor = PRED_BUILTIN_EXPECT;
2355 return gimple_call_arg (def, 1);
2358 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
2359 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
2360 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
2361 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
2362 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
2363 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
2364 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
2365 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
2366 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
2367 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
2368 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
2369 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
2370 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
2371 /* Assume that any given atomic operation has low contention,
2372 and thus the compare-and-swap operation succeeds. */
2373 if (predictor)
2374 *predictor = PRED_COMPARE_AND_SWAP;
2375 return boolean_true_node;
2376 default:
2377 break;
2381 return NULL;
2384 if (get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS)
2386 tree res;
2387 enum br_predictor predictor2;
2388 op0 = expr_expected_value (op0, visited, predictor);
2389 if (!op0)
2390 return NULL;
2391 op1 = expr_expected_value (op1, visited, &predictor2);
2392 if (predictor && *predictor < predictor2)
2393 *predictor = predictor2;
2394 if (!op1)
2395 return NULL;
2396 res = fold_build2 (code, type, op0, op1);
2397 if (TREE_CONSTANT (res))
2398 return res;
2399 return NULL;
2401 if (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS)
2403 tree res;
2404 op0 = expr_expected_value (op0, visited, predictor);
2405 if (!op0)
2406 return NULL;
2407 res = fold_build1 (code, type, op0);
2408 if (TREE_CONSTANT (res))
2409 return res;
2410 return NULL;
2412 return NULL;
2415 /* Return constant EXPR will likely have at execution time, NULL if unknown.
2416 The function is used by builtin_expect branch predictor so the evidence
2417 must come from this construct and additional possible constant folding.
2419 We may want to implement more involved value guess (such as value range
2420 propagation based prediction), but such tricks shall go to new
2421 implementation. */
2423 static tree
2424 expr_expected_value (tree expr, bitmap visited,
2425 enum br_predictor *predictor)
2427 enum tree_code code;
2428 tree op0, op1;
2430 if (TREE_CONSTANT (expr))
2432 if (predictor)
2433 *predictor = PRED_UNCONDITIONAL;
2434 return expr;
2437 extract_ops_from_tree (expr, &code, &op0, &op1);
2438 return expr_expected_value_1 (TREE_TYPE (expr),
2439 op0, code, op1, visited, predictor);
2442 /* Predict using opcode of the last statement in basic block. */
2443 static void
2444 tree_predict_by_opcode (basic_block bb)
2446 gimple *stmt = last_stmt (bb);
2447 edge then_edge;
2448 tree op0, op1;
2449 tree type;
2450 tree val;
2451 enum tree_code cmp;
2452 edge_iterator ei;
2453 enum br_predictor predictor;
2455 if (!stmt || gimple_code (stmt) != GIMPLE_COND)
2456 return;
2457 FOR_EACH_EDGE (then_edge, ei, bb->succs)
2458 if (then_edge->flags & EDGE_TRUE_VALUE)
2459 break;
2460 op0 = gimple_cond_lhs (stmt);
2461 op1 = gimple_cond_rhs (stmt);
2462 cmp = gimple_cond_code (stmt);
2463 type = TREE_TYPE (op0);
2464 val = expr_expected_value_1 (boolean_type_node, op0, cmp, op1, auto_bitmap (),
2465 &predictor);
2466 if (val && TREE_CODE (val) == INTEGER_CST)
2468 if (predictor == PRED_BUILTIN_EXPECT)
2470 int percent = PARAM_VALUE (BUILTIN_EXPECT_PROBABILITY);
2472 gcc_assert (percent >= 0 && percent <= 100);
2473 if (integer_zerop (val))
2474 percent = 100 - percent;
2475 predict_edge (then_edge, PRED_BUILTIN_EXPECT, HITRATE (percent));
2477 else
2478 predict_edge_def (then_edge, predictor,
2479 integer_zerop (val) ? NOT_TAKEN : TAKEN);
2481 /* Try "pointer heuristic."
2482 A comparison ptr == 0 is predicted as false.
2483 Similarly, a comparison ptr1 == ptr2 is predicted as false. */
2484 if (POINTER_TYPE_P (type))
2486 if (cmp == EQ_EXPR)
2487 predict_edge_def (then_edge, PRED_TREE_POINTER, NOT_TAKEN);
2488 else if (cmp == NE_EXPR)
2489 predict_edge_def (then_edge, PRED_TREE_POINTER, TAKEN);
2491 else
2493 /* Try "opcode heuristic."
2494 EQ tests are usually false and NE tests are usually true. Also,
2495 most quantities are positive, so we can make the appropriate guesses
2496 about signed comparisons against zero. */
2497 switch (cmp)
2499 case EQ_EXPR:
2500 case UNEQ_EXPR:
2501 /* Floating point comparisons appears to behave in a very
2502 unpredictable way because of special role of = tests in
2503 FP code. */
2504 if (FLOAT_TYPE_P (type))
2506 /* Comparisons with 0 are often used for booleans and there is
2507 nothing useful to predict about them. */
2508 else if (integer_zerop (op0) || integer_zerop (op1))
2510 else
2511 predict_edge_def (then_edge, PRED_TREE_OPCODE_NONEQUAL, NOT_TAKEN);
2512 break;
2514 case NE_EXPR:
2515 case LTGT_EXPR:
2516 /* Floating point comparisons appears to behave in a very
2517 unpredictable way because of special role of = tests in
2518 FP code. */
2519 if (FLOAT_TYPE_P (type))
2521 /* Comparisons with 0 are often used for booleans and there is
2522 nothing useful to predict about them. */
2523 else if (integer_zerop (op0)
2524 || integer_zerop (op1))
2526 else
2527 predict_edge_def (then_edge, PRED_TREE_OPCODE_NONEQUAL, TAKEN);
2528 break;
2530 case ORDERED_EXPR:
2531 predict_edge_def (then_edge, PRED_TREE_FPOPCODE, TAKEN);
2532 break;
2534 case UNORDERED_EXPR:
2535 predict_edge_def (then_edge, PRED_TREE_FPOPCODE, NOT_TAKEN);
2536 break;
2538 case LE_EXPR:
2539 case LT_EXPR:
2540 if (integer_zerop (op1)
2541 || integer_onep (op1)
2542 || integer_all_onesp (op1)
2543 || real_zerop (op1)
2544 || real_onep (op1)
2545 || real_minus_onep (op1))
2546 predict_edge_def (then_edge, PRED_TREE_OPCODE_POSITIVE, NOT_TAKEN);
2547 break;
2549 case GE_EXPR:
2550 case GT_EXPR:
2551 if (integer_zerop (op1)
2552 || integer_onep (op1)
2553 || integer_all_onesp (op1)
2554 || real_zerop (op1)
2555 || real_onep (op1)
2556 || real_minus_onep (op1))
2557 predict_edge_def (then_edge, PRED_TREE_OPCODE_POSITIVE, TAKEN);
2558 break;
2560 default:
2561 break;
2565 /* Returns TRUE if the STMT is exit(0) like statement. */
2567 static bool
2568 is_exit_with_zero_arg (const gimple *stmt)
2570 /* This is not exit, _exit or _Exit. */
2571 if (!gimple_call_builtin_p (stmt, BUILT_IN_EXIT)
2572 && !gimple_call_builtin_p (stmt, BUILT_IN__EXIT)
2573 && !gimple_call_builtin_p (stmt, BUILT_IN__EXIT2))
2574 return false;
2576 /* Argument is an interger zero. */
2577 return integer_zerop (gimple_call_arg (stmt, 0));
2580 /* Try to guess whether the value of return means error code. */
2582 static enum br_predictor
2583 return_prediction (tree val, enum prediction *prediction)
2585 /* VOID. */
2586 if (!val)
2587 return PRED_NO_PREDICTION;
2588 /* Different heuristics for pointers and scalars. */
2589 if (POINTER_TYPE_P (TREE_TYPE (val)))
2591 /* NULL is usually not returned. */
2592 if (integer_zerop (val))
2594 *prediction = NOT_TAKEN;
2595 return PRED_NULL_RETURN;
2598 else if (INTEGRAL_TYPE_P (TREE_TYPE (val)))
2600 /* Negative return values are often used to indicate
2601 errors. */
2602 if (TREE_CODE (val) == INTEGER_CST
2603 && tree_int_cst_sgn (val) < 0)
2605 *prediction = NOT_TAKEN;
2606 return PRED_NEGATIVE_RETURN;
2608 /* Constant return values seems to be commonly taken.
2609 Zero/one often represent booleans so exclude them from the
2610 heuristics. */
2611 if (TREE_CONSTANT (val)
2612 && (!integer_zerop (val) && !integer_onep (val)))
2614 *prediction = NOT_TAKEN;
2615 return PRED_CONST_RETURN;
2618 return PRED_NO_PREDICTION;
2621 /* Find the basic block with return expression and look up for possible
2622 return value trying to apply RETURN_PREDICTION heuristics. */
2623 static void
2624 apply_return_prediction (void)
2626 greturn *return_stmt = NULL;
2627 tree return_val;
2628 edge e;
2629 gphi *phi;
2630 int phi_num_args, i;
2631 enum br_predictor pred;
2632 enum prediction direction;
2633 edge_iterator ei;
2635 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
2637 gimple *last = last_stmt (e->src);
2638 if (last
2639 && gimple_code (last) == GIMPLE_RETURN)
2641 return_stmt = as_a <greturn *> (last);
2642 break;
2645 if (!e)
2646 return;
2647 return_val = gimple_return_retval (return_stmt);
2648 if (!return_val)
2649 return;
2650 if (TREE_CODE (return_val) != SSA_NAME
2651 || !SSA_NAME_DEF_STMT (return_val)
2652 || gimple_code (SSA_NAME_DEF_STMT (return_val)) != GIMPLE_PHI)
2653 return;
2654 phi = as_a <gphi *> (SSA_NAME_DEF_STMT (return_val));
2655 phi_num_args = gimple_phi_num_args (phi);
2656 pred = return_prediction (PHI_ARG_DEF (phi, 0), &direction);
2658 /* Avoid the degenerate case where all return values form the function
2659 belongs to same category (ie they are all positive constants)
2660 so we can hardly say something about them. */
2661 for (i = 1; i < phi_num_args; i++)
2662 if (pred != return_prediction (PHI_ARG_DEF (phi, i), &direction))
2663 break;
2664 if (i != phi_num_args)
2665 for (i = 0; i < phi_num_args; i++)
2667 pred = return_prediction (PHI_ARG_DEF (phi, i), &direction);
2668 if (pred != PRED_NO_PREDICTION)
2669 predict_paths_leading_to_edge (gimple_phi_arg_edge (phi, i), pred,
2670 direction);
2674 /* Look for basic block that contains unlikely to happen events
2675 (such as noreturn calls) and mark all paths leading to execution
2676 of this basic blocks as unlikely. */
2678 static void
2679 tree_bb_level_predictions (void)
2681 basic_block bb;
2682 bool has_return_edges = false;
2683 edge e;
2684 edge_iterator ei;
2686 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
2687 if (!unlikely_executed_edge_p (e) && !(e->flags & EDGE_ABNORMAL_CALL))
2689 has_return_edges = true;
2690 break;
2693 apply_return_prediction ();
2695 FOR_EACH_BB_FN (bb, cfun)
2697 gimple_stmt_iterator gsi;
2699 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2701 gimple *stmt = gsi_stmt (gsi);
2702 tree decl;
2704 if (is_gimple_call (stmt))
2706 if (gimple_call_noreturn_p (stmt)
2707 && has_return_edges
2708 && !is_exit_with_zero_arg (stmt))
2709 predict_paths_leading_to (bb, PRED_NORETURN,
2710 NOT_TAKEN);
2711 decl = gimple_call_fndecl (stmt);
2712 if (decl
2713 && lookup_attribute ("cold",
2714 DECL_ATTRIBUTES (decl)))
2715 predict_paths_leading_to (bb, PRED_COLD_FUNCTION,
2716 NOT_TAKEN);
2717 if (decl && recursive_call_p (current_function_decl, decl))
2718 predict_paths_leading_to (bb, PRED_RECURSIVE_CALL,
2719 NOT_TAKEN);
2721 else if (gimple_code (stmt) == GIMPLE_PREDICT)
2723 predict_paths_leading_to (bb, gimple_predict_predictor (stmt),
2724 gimple_predict_outcome (stmt));
2725 /* Keep GIMPLE_PREDICT around so early inlining will propagate
2726 hints to callers. */
2732 /* Callback for hash_map::traverse, asserts that the pointer map is
2733 empty. */
2735 bool
2736 assert_is_empty (const_basic_block const &, edge_prediction *const &value,
2737 void *)
2739 gcc_assert (!value);
2740 return false;
2743 /* Predict branch probabilities and estimate profile for basic block BB.
2744 When LOCAL_ONLY is set do not use any global properties of CFG. */
2746 static void
2747 tree_estimate_probability_bb (basic_block bb, bool local_only)
2749 edge e;
2750 edge_iterator ei;
2752 FOR_EACH_EDGE (e, ei, bb->succs)
2754 /* Look for block we are guarding (ie we dominate it,
2755 but it doesn't postdominate us). */
2756 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun) && e->dest != bb
2757 && !local_only
2758 && dominated_by_p (CDI_DOMINATORS, e->dest, e->src)
2759 && !dominated_by_p (CDI_POST_DOMINATORS, e->src, e->dest))
2761 gimple_stmt_iterator bi;
2763 /* The call heuristic claims that a guarded function call
2764 is improbable. This is because such calls are often used
2765 to signal exceptional situations such as printing error
2766 messages. */
2767 for (bi = gsi_start_bb (e->dest); !gsi_end_p (bi);
2768 gsi_next (&bi))
2770 gimple *stmt = gsi_stmt (bi);
2771 if (is_gimple_call (stmt)
2772 && !gimple_inexpensive_call_p (as_a <gcall *> (stmt))
2773 /* Constant and pure calls are hardly used to signalize
2774 something exceptional. */
2775 && gimple_has_side_effects (stmt))
2777 if (gimple_call_fndecl (stmt))
2778 predict_edge_def (e, PRED_CALL, NOT_TAKEN);
2779 else if (virtual_method_call_p (gimple_call_fn (stmt)))
2780 predict_edge_def (e, PRED_POLYMORPHIC_CALL, NOT_TAKEN);
2781 else
2782 predict_edge_def (e, PRED_INDIR_CALL, TAKEN);
2783 break;
2788 tree_predict_by_opcode (bb);
2791 /* Predict branch probabilities and estimate profile of the tree CFG.
2792 This function can be called from the loop optimizers to recompute
2793 the profile information.
2794 If DRY_RUN is set, do not modify CFG and only produce dump files. */
2796 void
2797 tree_estimate_probability (bool dry_run)
2799 basic_block bb;
2801 add_noreturn_fake_exit_edges ();
2802 connect_infinite_loops_to_exit ();
2803 /* We use loop_niter_by_eval, which requires that the loops have
2804 preheaders. */
2805 create_preheaders (CP_SIMPLE_PREHEADERS);
2806 calculate_dominance_info (CDI_POST_DOMINATORS);
2808 bb_predictions = new hash_map<const_basic_block, edge_prediction *>;
2809 tree_bb_level_predictions ();
2810 record_loop_exits ();
2812 if (number_of_loops (cfun) > 1)
2813 predict_loops ();
2815 FOR_EACH_BB_FN (bb, cfun)
2816 tree_estimate_probability_bb (bb, false);
2818 FOR_EACH_BB_FN (bb, cfun)
2819 combine_predictions_for_bb (bb, dry_run);
2821 if (flag_checking)
2822 bb_predictions->traverse<void *, assert_is_empty> (NULL);
2824 delete bb_predictions;
2825 bb_predictions = NULL;
2827 if (!dry_run)
2828 estimate_bb_frequencies (false);
2829 free_dominance_info (CDI_POST_DOMINATORS);
2830 remove_fake_exit_edges ();
2833 /* Set edge->probability for each successor edge of BB. */
2834 void
2835 tree_guess_outgoing_edge_probabilities (basic_block bb)
2837 bb_predictions = new hash_map<const_basic_block, edge_prediction *>;
2838 tree_estimate_probability_bb (bb, true);
2839 combine_predictions_for_bb (bb, false);
2840 if (flag_checking)
2841 bb_predictions->traverse<void *, assert_is_empty> (NULL);
2842 delete bb_predictions;
2843 bb_predictions = NULL;
2846 /* Predict edges to successors of CUR whose sources are not postdominated by
2847 BB by PRED and recurse to all postdominators. */
2849 static void
2850 predict_paths_for_bb (basic_block cur, basic_block bb,
2851 enum br_predictor pred,
2852 enum prediction taken,
2853 bitmap visited, struct loop *in_loop = NULL)
2855 edge e;
2856 edge_iterator ei;
2857 basic_block son;
2859 /* If we exited the loop or CUR is unconditional in the loop, there is
2860 nothing to do. */
2861 if (in_loop
2862 && (!flow_bb_inside_loop_p (in_loop, cur)
2863 || dominated_by_p (CDI_DOMINATORS, in_loop->latch, cur)))
2864 return;
2866 /* We are looking for all edges forming edge cut induced by
2867 set of all blocks postdominated by BB. */
2868 FOR_EACH_EDGE (e, ei, cur->preds)
2869 if (e->src->index >= NUM_FIXED_BLOCKS
2870 && !dominated_by_p (CDI_POST_DOMINATORS, e->src, bb))
2872 edge e2;
2873 edge_iterator ei2;
2874 bool found = false;
2876 /* Ignore fake edges and eh, we predict them as not taken anyway. */
2877 if (unlikely_executed_edge_p (e))
2878 continue;
2879 gcc_assert (bb == cur || dominated_by_p (CDI_POST_DOMINATORS, cur, bb));
2881 /* See if there is an edge from e->src that is not abnormal
2882 and does not lead to BB and does not exit the loop. */
2883 FOR_EACH_EDGE (e2, ei2, e->src->succs)
2884 if (e2 != e
2885 && !unlikely_executed_edge_p (e2)
2886 && !dominated_by_p (CDI_POST_DOMINATORS, e2->dest, bb)
2887 && (!in_loop || !loop_exit_edge_p (in_loop, e2)))
2889 found = true;
2890 break;
2893 /* If there is non-abnormal path leaving e->src, predict edge
2894 using predictor. Otherwise we need to look for paths
2895 leading to e->src.
2897 The second may lead to infinite loop in the case we are predicitng
2898 regions that are only reachable by abnormal edges. We simply
2899 prevent visiting given BB twice. */
2900 if (found)
2902 if (!edge_predicted_by_p (e, pred, taken))
2903 predict_edge_def (e, pred, taken);
2905 else if (bitmap_set_bit (visited, e->src->index))
2906 predict_paths_for_bb (e->src, e->src, pred, taken, visited, in_loop);
2908 for (son = first_dom_son (CDI_POST_DOMINATORS, cur);
2909 son;
2910 son = next_dom_son (CDI_POST_DOMINATORS, son))
2911 predict_paths_for_bb (son, bb, pred, taken, visited, in_loop);
2914 /* Sets branch probabilities according to PREDiction and
2915 FLAGS. */
2917 static void
2918 predict_paths_leading_to (basic_block bb, enum br_predictor pred,
2919 enum prediction taken, struct loop *in_loop)
2921 predict_paths_for_bb (bb, bb, pred, taken, auto_bitmap (), in_loop);
2924 /* Like predict_paths_leading_to but take edge instead of basic block. */
2926 static void
2927 predict_paths_leading_to_edge (edge e, enum br_predictor pred,
2928 enum prediction taken, struct loop *in_loop)
2930 bool has_nonloop_edge = false;
2931 edge_iterator ei;
2932 edge e2;
2934 basic_block bb = e->src;
2935 FOR_EACH_EDGE (e2, ei, bb->succs)
2936 if (e2->dest != e->src && e2->dest != e->dest
2937 && !unlikely_executed_edge_p (e)
2938 && !dominated_by_p (CDI_POST_DOMINATORS, e->src, e2->dest))
2940 has_nonloop_edge = true;
2941 break;
2943 if (!has_nonloop_edge)
2945 predict_paths_for_bb (bb, bb, pred, taken, auto_bitmap (), in_loop);
2947 else
2948 predict_edge_def (e, pred, taken);
2951 /* This is used to carry information about basic blocks. It is
2952 attached to the AUX field of the standard CFG block. */
2954 struct block_info
2956 /* Estimated frequency of execution of basic_block. */
2957 sreal frequency;
2959 /* To keep queue of basic blocks to process. */
2960 basic_block next;
2962 /* Number of predecessors we need to visit first. */
2963 int npredecessors;
2966 /* Similar information for edges. */
2967 struct edge_prob_info
2969 /* In case edge is a loopback edge, the probability edge will be reached
2970 in case header is. Estimated number of iterations of the loop can be
2971 then computed as 1 / (1 - back_edge_prob). */
2972 sreal back_edge_prob;
2973 /* True if the edge is a loopback edge in the natural loop. */
2974 unsigned int back_edge:1;
2977 #define BLOCK_INFO(B) ((block_info *) (B)->aux)
2978 #undef EDGE_INFO
2979 #define EDGE_INFO(E) ((edge_prob_info *) (E)->aux)
2981 /* Helper function for estimate_bb_frequencies.
2982 Propagate the frequencies in blocks marked in
2983 TOVISIT, starting in HEAD. */
2985 static void
2986 propagate_freq (basic_block head, bitmap tovisit)
2988 basic_block bb;
2989 basic_block last;
2990 unsigned i;
2991 edge e;
2992 basic_block nextbb;
2993 bitmap_iterator bi;
2995 /* For each basic block we need to visit count number of his predecessors
2996 we need to visit first. */
2997 EXECUTE_IF_SET_IN_BITMAP (tovisit, 0, i, bi)
2999 edge_iterator ei;
3000 int count = 0;
3002 bb = BASIC_BLOCK_FOR_FN (cfun, i);
3004 FOR_EACH_EDGE (e, ei, bb->preds)
3006 bool visit = bitmap_bit_p (tovisit, e->src->index);
3008 if (visit && !(e->flags & EDGE_DFS_BACK))
3009 count++;
3010 else if (visit && dump_file && !EDGE_INFO (e)->back_edge)
3011 fprintf (dump_file,
3012 "Irreducible region hit, ignoring edge to %i->%i\n",
3013 e->src->index, bb->index);
3015 BLOCK_INFO (bb)->npredecessors = count;
3016 /* When function never returns, we will never process exit block. */
3017 if (!count && bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
3018 bb->count = profile_count::zero ();
3021 BLOCK_INFO (head)->frequency = 1;
3022 last = head;
3023 for (bb = head; bb; bb = nextbb)
3025 edge_iterator ei;
3026 sreal cyclic_probability = 0;
3027 sreal frequency = 0;
3029 nextbb = BLOCK_INFO (bb)->next;
3030 BLOCK_INFO (bb)->next = NULL;
3032 /* Compute frequency of basic block. */
3033 if (bb != head)
3035 if (flag_checking)
3036 FOR_EACH_EDGE (e, ei, bb->preds)
3037 gcc_assert (!bitmap_bit_p (tovisit, e->src->index)
3038 || (e->flags & EDGE_DFS_BACK));
3040 FOR_EACH_EDGE (e, ei, bb->preds)
3041 if (EDGE_INFO (e)->back_edge)
3043 cyclic_probability += EDGE_INFO (e)->back_edge_prob;
3045 else if (!(e->flags & EDGE_DFS_BACK))
3047 /* frequency += (e->probability
3048 * BLOCK_INFO (e->src)->frequency /
3049 REG_BR_PROB_BASE); */
3051 /* FIXME: Graphite is producing edges with no profile. Once
3052 this is fixed, drop this. */
3053 sreal tmp = e->probability.initialized_p () ?
3054 e->probability.to_reg_br_prob_base () : 0;
3055 tmp *= BLOCK_INFO (e->src)->frequency;
3056 tmp *= real_inv_br_prob_base;
3057 frequency += tmp;
3060 if (cyclic_probability == 0)
3062 BLOCK_INFO (bb)->frequency = frequency;
3064 else
3066 if (cyclic_probability > real_almost_one)
3067 cyclic_probability = real_almost_one;
3069 /* BLOCK_INFO (bb)->frequency = frequency
3070 / (1 - cyclic_probability) */
3072 cyclic_probability = sreal (1) - cyclic_probability;
3073 BLOCK_INFO (bb)->frequency = frequency / cyclic_probability;
3077 bitmap_clear_bit (tovisit, bb->index);
3079 e = find_edge (bb, head);
3080 if (e)
3082 /* EDGE_INFO (e)->back_edge_prob
3083 = ((e->probability * BLOCK_INFO (bb)->frequency)
3084 / REG_BR_PROB_BASE); */
3086 /* FIXME: Graphite is producing edges with no profile. Once
3087 this is fixed, drop this. */
3088 sreal tmp = e->probability.initialized_p () ?
3089 e->probability.to_reg_br_prob_base () : 0;
3090 tmp *= BLOCK_INFO (bb)->frequency;
3091 EDGE_INFO (e)->back_edge_prob = tmp * real_inv_br_prob_base;
3094 /* Propagate to successor blocks. */
3095 FOR_EACH_EDGE (e, ei, bb->succs)
3096 if (!(e->flags & EDGE_DFS_BACK)
3097 && BLOCK_INFO (e->dest)->npredecessors)
3099 BLOCK_INFO (e->dest)->npredecessors--;
3100 if (!BLOCK_INFO (e->dest)->npredecessors)
3102 if (!nextbb)
3103 nextbb = e->dest;
3104 else
3105 BLOCK_INFO (last)->next = e->dest;
3107 last = e->dest;
3113 /* Estimate frequencies in loops at same nest level. */
3115 static void
3116 estimate_loops_at_level (struct loop *first_loop)
3118 struct loop *loop;
3120 for (loop = first_loop; loop; loop = loop->next)
3122 edge e;
3123 basic_block *bbs;
3124 unsigned i;
3125 auto_bitmap tovisit;
3127 estimate_loops_at_level (loop->inner);
3129 /* Find current loop back edge and mark it. */
3130 e = loop_latch_edge (loop);
3131 EDGE_INFO (e)->back_edge = 1;
3133 bbs = get_loop_body (loop);
3134 for (i = 0; i < loop->num_nodes; i++)
3135 bitmap_set_bit (tovisit, bbs[i]->index);
3136 free (bbs);
3137 propagate_freq (loop->header, tovisit);
3141 /* Propagates frequencies through structure of loops. */
3143 static void
3144 estimate_loops (void)
3146 auto_bitmap tovisit;
3147 basic_block bb;
3149 /* Start by estimating the frequencies in the loops. */
3150 if (number_of_loops (cfun) > 1)
3151 estimate_loops_at_level (current_loops->tree_root->inner);
3153 /* Now propagate the frequencies through all the blocks. */
3154 FOR_ALL_BB_FN (bb, cfun)
3156 bitmap_set_bit (tovisit, bb->index);
3158 propagate_freq (ENTRY_BLOCK_PTR_FOR_FN (cfun), tovisit);
3161 /* Drop the profile for NODE to guessed, and update its frequency based on
3162 whether it is expected to be hot given the CALL_COUNT. */
3164 static void
3165 drop_profile (struct cgraph_node *node, profile_count call_count)
3167 struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
3168 /* In the case where this was called by another function with a
3169 dropped profile, call_count will be 0. Since there are no
3170 non-zero call counts to this function, we don't know for sure
3171 whether it is hot, and therefore it will be marked normal below. */
3172 bool hot = maybe_hot_count_p (NULL, call_count);
3174 if (dump_file)
3175 fprintf (dump_file,
3176 "Dropping 0 profile for %s. %s based on calls.\n",
3177 node->dump_name (),
3178 hot ? "Function is hot" : "Function is normal");
3179 /* We only expect to miss profiles for functions that are reached
3180 via non-zero call edges in cases where the function may have
3181 been linked from another module or library (COMDATs and extern
3182 templates). See the comments below for handle_missing_profiles.
3183 Also, only warn in cases where the missing counts exceed the
3184 number of training runs. In certain cases with an execv followed
3185 by a no-return call the profile for the no-return call is not
3186 dumped and there can be a mismatch. */
3187 if (!DECL_COMDAT (node->decl) && !DECL_EXTERNAL (node->decl)
3188 && call_count > profile_info->runs)
3190 if (flag_profile_correction)
3192 if (dump_file)
3193 fprintf (dump_file,
3194 "Missing counts for called function %s\n",
3195 node->dump_name ());
3197 else
3198 warning (0, "Missing counts for called function %s",
3199 node->dump_name ());
3202 basic_block bb;
3203 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
3204 if (flag_guess_branch_prob)
3206 bool clear_zeros
3207 = ENTRY_BLOCK_PTR_FOR_FN
3208 (DECL_STRUCT_FUNCTION (node->decl))->count.nonzero_p ();
3209 FOR_ALL_BB_FN (bb, fn)
3210 if (clear_zeros || !(bb->count == profile_count::zero ()))
3211 bb->count = bb->count.guessed_local ();
3212 DECL_STRUCT_FUNCTION (node->decl)->cfg->count_max =
3213 DECL_STRUCT_FUNCTION (node->decl)->cfg->count_max.guessed_local ();
3215 else
3217 FOR_ALL_BB_FN (bb, fn)
3218 bb->count = profile_count::uninitialized ();
3219 DECL_STRUCT_FUNCTION (node->decl)->cfg->count_max
3220 = profile_count::uninitialized ();
3222 pop_cfun ();
3224 struct cgraph_edge *e;
3225 for (e = node->callees; e; e = e->next_caller)
3227 e->frequency = compute_call_stmt_bb_frequency (e->caller->decl,
3228 gimple_bb (e->call_stmt));
3231 profile_status_for_fn (fn)
3232 = (flag_guess_branch_prob ? PROFILE_GUESSED : PROFILE_ABSENT);
3233 node->frequency
3234 = hot ? NODE_FREQUENCY_HOT : NODE_FREQUENCY_NORMAL;
3237 /* In the case of COMDAT routines, multiple object files will contain the same
3238 function and the linker will select one for the binary. In that case
3239 all the other copies from the profile instrument binary will be missing
3240 profile counts. Look for cases where this happened, due to non-zero
3241 call counts going to 0-count functions, and drop the profile to guessed
3242 so that we can use the estimated probabilities and avoid optimizing only
3243 for size.
3245 The other case where the profile may be missing is when the routine
3246 is not going to be emitted to the object file, e.g. for "extern template"
3247 class methods. Those will be marked DECL_EXTERNAL. Emit a warning in
3248 all other cases of non-zero calls to 0-count functions. */
3250 void
3251 handle_missing_profiles (void)
3253 struct cgraph_node *node;
3254 int unlikely_count_fraction = PARAM_VALUE (UNLIKELY_BB_COUNT_FRACTION);
3255 auto_vec<struct cgraph_node *, 64> worklist;
3257 /* See if 0 count function has non-0 count callers. In this case we
3258 lost some profile. Drop its function profile to PROFILE_GUESSED. */
3259 FOR_EACH_DEFINED_FUNCTION (node)
3261 struct cgraph_edge *e;
3262 profile_count call_count = profile_count::zero ();
3263 gcov_type max_tp_first_run = 0;
3264 struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
3266 if (!(node->count == profile_count::zero ()))
3267 continue;
3268 for (e = node->callers; e; e = e->next_caller)
3269 if (e->count.initialized_p () && e->count > 0)
3271 call_count = call_count + e->count;
3273 if (e->caller->tp_first_run > max_tp_first_run)
3274 max_tp_first_run = e->caller->tp_first_run;
3277 /* If time profile is missing, let assign the maximum that comes from
3278 caller functions. */
3279 if (!node->tp_first_run && max_tp_first_run)
3280 node->tp_first_run = max_tp_first_run + 1;
3282 if (call_count > 0
3283 && fn && fn->cfg
3284 && (call_count.apply_scale (unlikely_count_fraction, 1)
3285 >= profile_info->runs))
3287 drop_profile (node, call_count);
3288 worklist.safe_push (node);
3292 /* Propagate the profile dropping to other 0-count COMDATs that are
3293 potentially called by COMDATs we already dropped the profile on. */
3294 while (worklist.length () > 0)
3296 struct cgraph_edge *e;
3298 node = worklist.pop ();
3299 for (e = node->callees; e; e = e->next_caller)
3301 struct cgraph_node *callee = e->callee;
3302 struct function *fn = DECL_STRUCT_FUNCTION (callee->decl);
3304 if (callee->count > 0)
3305 continue;
3306 if ((DECL_COMDAT (callee->decl) || DECL_EXTERNAL (callee->decl))
3307 && fn && fn->cfg
3308 && profile_status_for_fn (fn) == PROFILE_READ)
3310 drop_profile (node, profile_count::zero ());
3311 worklist.safe_push (callee);
3317 /* Convert counts measured by profile driven feedback to frequencies.
3318 Return nonzero iff there was any nonzero execution count. */
3320 bool
3321 counts_to_freqs (void)
3323 profile_count true_count_max = profile_count::uninitialized ();
3324 basic_block bb;
3326 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
3327 true_count_max = true_count_max.max (bb->count);
3329 cfun->cfg->count_max = true_count_max;
3331 return true_count_max.nonzero_p ();
3334 /* Return true if function is likely to be expensive, so there is no point to
3335 optimize performance of prologue, epilogue or do inlining at the expense
3336 of code size growth. THRESHOLD is the limit of number of instructions
3337 function can execute at average to be still considered not expensive. */
3339 bool
3340 expensive_function_p (int threshold)
3342 unsigned int sum = 0;
3343 basic_block bb;
3344 unsigned int limit;
3346 /* We can not compute accurately for large thresholds due to scaled
3347 frequencies. */
3348 gcc_assert (threshold <= BB_FREQ_MAX);
3350 /* Frequencies are out of range. This either means that function contains
3351 internal loop executing more than BB_FREQ_MAX times or profile feedback
3352 is available and function has not been executed at all. */
3353 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->count.to_frequency (cfun) == 0)
3354 return true;
3356 /* Maximally BB_FREQ_MAX^2 so overflow won't happen. */
3357 limit = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count.to_frequency (cfun) * threshold;
3358 FOR_EACH_BB_FN (bb, cfun)
3360 rtx_insn *insn;
3362 FOR_BB_INSNS (bb, insn)
3363 if (active_insn_p (insn))
3365 sum += bb->count.to_frequency (cfun);
3366 if (sum > limit)
3367 return true;
3371 return false;
3374 /* All basic blocks that are reachable only from unlikely basic blocks are
3375 unlikely. */
3377 void
3378 propagate_unlikely_bbs_forward (void)
3380 auto_vec<basic_block, 64> worklist;
3381 basic_block bb;
3382 edge_iterator ei;
3383 edge e;
3385 if (!(ENTRY_BLOCK_PTR_FOR_FN (cfun)->count == profile_count::zero ()))
3387 ENTRY_BLOCK_PTR_FOR_FN (cfun)->aux = (void *)(size_t) 1;
3388 worklist.safe_push (ENTRY_BLOCK_PTR_FOR_FN (cfun));
3390 while (worklist.length () > 0)
3392 bb = worklist.pop ();
3393 FOR_EACH_EDGE (e, ei, bb->succs)
3394 if (!(e->count () == profile_count::zero ())
3395 && !(e->dest->count == profile_count::zero ())
3396 && !e->dest->aux)
3398 e->dest->aux = (void *)(size_t) 1;
3399 worklist.safe_push (e->dest);
3404 FOR_ALL_BB_FN (bb, cfun)
3406 if (!bb->aux)
3408 if (!(bb->count == profile_count::zero ())
3409 && (dump_file && (dump_flags & TDF_DETAILS)))
3410 fprintf (dump_file,
3411 "Basic block %i is marked unlikely by forward prop\n",
3412 bb->index);
3413 bb->count = profile_count::zero ();
3415 else
3416 bb->aux = NULL;
3420 /* Determine basic blocks/edges that are known to be unlikely executed and set
3421 their counters to zero.
3422 This is done with first identifying obviously unlikely BBs/edges and then
3423 propagating in both directions. */
3425 static void
3426 determine_unlikely_bbs ()
3428 basic_block bb;
3429 auto_vec<basic_block, 64> worklist;
3430 edge_iterator ei;
3431 edge e;
3433 FOR_EACH_BB_FN (bb, cfun)
3435 if (!(bb->count == profile_count::zero ())
3436 && unlikely_executed_bb_p (bb))
3438 if (dump_file && (dump_flags & TDF_DETAILS))
3439 fprintf (dump_file, "Basic block %i is locally unlikely\n",
3440 bb->index);
3441 bb->count = profile_count::zero ();
3444 FOR_EACH_EDGE (e, ei, bb->succs)
3445 if (!(e->probability == profile_probability::never ())
3446 && unlikely_executed_edge_p (e))
3448 if (dump_file && (dump_flags & TDF_DETAILS))
3449 fprintf (dump_file, "Edge %i->%i is locally unlikely\n",
3450 bb->index, e->dest->index);
3451 e->probability = profile_probability::never ();
3454 gcc_checking_assert (!bb->aux);
3457 auto_vec<int, 64> nsuccs;
3458 nsuccs.safe_grow_cleared (last_basic_block_for_fn (cfun));
3459 FOR_ALL_BB_FN (bb, cfun)
3460 if (!(bb->count == profile_count::zero ())
3461 && bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
3463 nsuccs[bb->index] = 0;
3464 FOR_EACH_EDGE (e, ei, bb->succs)
3465 if (!(e->probability == profile_probability::never ())
3466 && !(e->dest->count == profile_count::zero ()))
3467 nsuccs[bb->index]++;
3468 if (!nsuccs[bb->index])
3469 worklist.safe_push (bb);
3471 while (worklist.length () > 0)
3473 bb = worklist.pop ();
3474 if (bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
3476 bool found = false;
3477 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
3478 !gsi_end_p (gsi); gsi_next (&gsi))
3479 if (stmt_can_terminate_bb_p (gsi_stmt (gsi))
3480 /* stmt_can_terminate_bb_p special cases noreturns because it
3481 assumes that fake edges are created. We want to know that
3482 noreturn alone does not imply BB to be unlikely. */
3483 || (is_gimple_call (gsi_stmt (gsi))
3484 && (gimple_call_flags (gsi_stmt (gsi)) & ECF_NORETURN)))
3486 found = true;
3487 break;
3489 if (found)
3490 continue;
3492 if (!(bb->count == profile_count::zero ())
3493 && (dump_file && (dump_flags & TDF_DETAILS)))
3494 fprintf (dump_file,
3495 "Basic block %i is marked unlikely by backward prop\n",
3496 bb->index);
3497 bb->count = profile_count::zero ();
3498 FOR_EACH_EDGE (e, ei, bb->preds)
3499 if (!(e->probability == profile_probability::never ()))
3501 e->probability = profile_probability::never ();
3502 if (!(e->src->count == profile_count::zero ()))
3504 nsuccs[e->src->index]--;
3505 if (!nsuccs[e->src->index])
3506 worklist.safe_push (e->src);
3512 /* Estimate and propagate basic block frequencies using the given branch
3513 probabilities. If FORCE is true, the frequencies are used to estimate
3514 the counts even when there are already non-zero profile counts. */
3516 void
3517 estimate_bb_frequencies (bool force)
3519 basic_block bb;
3520 sreal freq_max;
3522 determine_unlikely_bbs ();
3524 if (force || profile_status_for_fn (cfun) != PROFILE_READ
3525 || !counts_to_freqs ())
3527 static int real_values_initialized = 0;
3529 if (!real_values_initialized)
3531 real_values_initialized = 1;
3532 real_br_prob_base = REG_BR_PROB_BASE;
3533 real_bb_freq_max = BB_FREQ_MAX;
3534 real_one_half = sreal (1, -1);
3535 real_inv_br_prob_base = sreal (1) / real_br_prob_base;
3536 real_almost_one = sreal (1) - real_inv_br_prob_base;
3539 mark_dfs_back_edges ();
3541 single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun))->probability =
3542 profile_probability::always ();
3544 /* Set up block info for each basic block. */
3545 alloc_aux_for_blocks (sizeof (block_info));
3546 alloc_aux_for_edges (sizeof (edge_prob_info));
3547 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
3549 edge e;
3550 edge_iterator ei;
3552 FOR_EACH_EDGE (e, ei, bb->succs)
3554 /* FIXME: Graphite is producing edges with no profile. Once
3555 this is fixed, drop this. */
3556 if (e->probability.initialized_p ())
3557 EDGE_INFO (e)->back_edge_prob
3558 = e->probability.to_reg_br_prob_base ();
3559 else
3560 EDGE_INFO (e)->back_edge_prob = REG_BR_PROB_BASE / 2;
3561 EDGE_INFO (e)->back_edge_prob *= real_inv_br_prob_base;
3565 /* First compute frequencies locally for each loop from innermost
3566 to outermost to examine frequencies for back edges. */
3567 estimate_loops ();
3569 bool global0 = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count.initialized_p ()
3570 && ENTRY_BLOCK_PTR_FOR_FN (cfun)->count.ipa_p ();
3572 freq_max = 0;
3573 FOR_EACH_BB_FN (bb, cfun)
3574 if (freq_max < BLOCK_INFO (bb)->frequency)
3575 freq_max = BLOCK_INFO (bb)->frequency;
3577 freq_max = real_bb_freq_max / freq_max;
3578 cfun->cfg->count_max = profile_count::uninitialized ();
3579 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
3581 sreal tmp = BLOCK_INFO (bb)->frequency * freq_max + real_one_half;
3582 profile_count count = profile_count::from_gcov_type (tmp.to_int ());
3584 /* If we have profile feedback in which this function was never
3585 executed, then preserve this info. */
3586 if (global0)
3587 bb->count = count.global0 ();
3588 else if (!(bb->count == profile_count::zero ()))
3589 bb->count = count.guessed_local ();
3590 cfun->cfg->count_max = cfun->cfg->count_max.max (bb->count);
3593 free_aux_for_blocks ();
3594 free_aux_for_edges ();
3596 compute_function_frequency ();
3599 /* Decide whether function is hot, cold or unlikely executed. */
3600 void
3601 compute_function_frequency (void)
3603 basic_block bb;
3604 struct cgraph_node *node = cgraph_node::get (current_function_decl);
3606 if (DECL_STATIC_CONSTRUCTOR (current_function_decl)
3607 || MAIN_NAME_P (DECL_NAME (current_function_decl)))
3608 node->only_called_at_startup = true;
3609 if (DECL_STATIC_DESTRUCTOR (current_function_decl))
3610 node->only_called_at_exit = true;
3612 if (profile_status_for_fn (cfun) != PROFILE_READ)
3614 int flags = flags_from_decl_or_type (current_function_decl);
3615 if ((ENTRY_BLOCK_PTR_FOR_FN (cfun)->count.ipa_p ()
3616 && ENTRY_BLOCK_PTR_FOR_FN (cfun)->count.ipa() == profile_count::zero ())
3617 || lookup_attribute ("cold", DECL_ATTRIBUTES (current_function_decl))
3618 != NULL)
3620 node->frequency = NODE_FREQUENCY_UNLIKELY_EXECUTED;
3621 warn_function_cold (current_function_decl);
3623 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (current_function_decl))
3624 != NULL)
3625 node->frequency = NODE_FREQUENCY_HOT;
3626 else if (flags & ECF_NORETURN)
3627 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
3628 else if (MAIN_NAME_P (DECL_NAME (current_function_decl)))
3629 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
3630 else if (DECL_STATIC_CONSTRUCTOR (current_function_decl)
3631 || DECL_STATIC_DESTRUCTOR (current_function_decl))
3632 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
3633 return;
3636 /* Only first time try to drop function into unlikely executed.
3637 After inlining the roundoff errors may confuse us.
3638 Ipa-profile pass will drop functions only called from unlikely
3639 functions to unlikely and that is most of what we care about. */
3640 if (!cfun->after_inlining)
3642 node->frequency = NODE_FREQUENCY_UNLIKELY_EXECUTED;
3643 warn_function_cold (current_function_decl);
3645 FOR_EACH_BB_FN (bb, cfun)
3647 if (maybe_hot_bb_p (cfun, bb))
3649 node->frequency = NODE_FREQUENCY_HOT;
3650 return;
3652 if (!probably_never_executed_bb_p (cfun, bb))
3653 node->frequency = NODE_FREQUENCY_NORMAL;
3657 /* Build PREDICT_EXPR. */
3658 tree
3659 build_predict_expr (enum br_predictor predictor, enum prediction taken)
3661 tree t = build1 (PREDICT_EXPR, void_type_node,
3662 build_int_cst (integer_type_node, predictor));
3663 SET_PREDICT_EXPR_OUTCOME (t, taken);
3664 return t;
3667 const char *
3668 predictor_name (enum br_predictor predictor)
3670 return predictor_info[predictor].name;
3673 /* Predict branch probabilities and estimate profile of the tree CFG. */
3675 namespace {
3677 const pass_data pass_data_profile =
3679 GIMPLE_PASS, /* type */
3680 "profile_estimate", /* name */
3681 OPTGROUP_NONE, /* optinfo_flags */
3682 TV_BRANCH_PROB, /* tv_id */
3683 PROP_cfg, /* properties_required */
3684 0, /* properties_provided */
3685 0, /* properties_destroyed */
3686 0, /* todo_flags_start */
3687 0, /* todo_flags_finish */
3690 class pass_profile : public gimple_opt_pass
3692 public:
3693 pass_profile (gcc::context *ctxt)
3694 : gimple_opt_pass (pass_data_profile, ctxt)
3697 /* opt_pass methods: */
3698 virtual bool gate (function *) { return flag_guess_branch_prob; }
3699 virtual unsigned int execute (function *);
3701 }; // class pass_profile
3703 unsigned int
3704 pass_profile::execute (function *fun)
3706 unsigned nb_loops;
3708 if (profile_status_for_fn (cfun) == PROFILE_GUESSED)
3709 return 0;
3711 loop_optimizer_init (LOOPS_NORMAL);
3712 if (dump_file && (dump_flags & TDF_DETAILS))
3713 flow_loops_dump (dump_file, NULL, 0);
3715 mark_irreducible_loops ();
3717 nb_loops = number_of_loops (fun);
3718 if (nb_loops > 1)
3719 scev_initialize ();
3721 tree_estimate_probability (false);
3723 if (nb_loops > 1)
3724 scev_finalize ();
3726 loop_optimizer_finalize ();
3727 if (dump_file && (dump_flags & TDF_DETAILS))
3728 gimple_dump_cfg (dump_file, dump_flags);
3729 if (profile_status_for_fn (fun) == PROFILE_ABSENT)
3730 profile_status_for_fn (fun) = PROFILE_GUESSED;
3731 if (dump_file && (dump_flags & TDF_DETAILS))
3733 struct loop *loop;
3734 FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
3735 if (loop->header->count.initialized_p ())
3736 fprintf (dump_file, "Loop got predicted %d to iterate %i times.\n",
3737 loop->num,
3738 (int)expected_loop_iterations_unbounded (loop));
3740 return 0;
3743 } // anon namespace
3745 gimple_opt_pass *
3746 make_pass_profile (gcc::context *ctxt)
3748 return new pass_profile (ctxt);
3751 namespace {
3753 const pass_data pass_data_strip_predict_hints =
3755 GIMPLE_PASS, /* type */
3756 "*strip_predict_hints", /* name */
3757 OPTGROUP_NONE, /* optinfo_flags */
3758 TV_BRANCH_PROB, /* tv_id */
3759 PROP_cfg, /* properties_required */
3760 0, /* properties_provided */
3761 0, /* properties_destroyed */
3762 0, /* todo_flags_start */
3763 0, /* todo_flags_finish */
3766 class pass_strip_predict_hints : public gimple_opt_pass
3768 public:
3769 pass_strip_predict_hints (gcc::context *ctxt)
3770 : gimple_opt_pass (pass_data_strip_predict_hints, ctxt)
3773 /* opt_pass methods: */
3774 opt_pass * clone () { return new pass_strip_predict_hints (m_ctxt); }
3775 virtual unsigned int execute (function *);
3777 }; // class pass_strip_predict_hints
3779 /* Get rid of all builtin_expect calls and GIMPLE_PREDICT statements
3780 we no longer need. */
3781 unsigned int
3782 pass_strip_predict_hints::execute (function *fun)
3784 basic_block bb;
3785 gimple *ass_stmt;
3786 tree var;
3787 bool changed = false;
3789 FOR_EACH_BB_FN (bb, fun)
3791 gimple_stmt_iterator bi;
3792 for (bi = gsi_start_bb (bb); !gsi_end_p (bi);)
3794 gimple *stmt = gsi_stmt (bi);
3796 if (gimple_code (stmt) == GIMPLE_PREDICT)
3798 gsi_remove (&bi, true);
3799 changed = true;
3800 continue;
3802 else if (is_gimple_call (stmt))
3804 tree fndecl = gimple_call_fndecl (stmt);
3806 if ((fndecl
3807 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
3808 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT
3809 && gimple_call_num_args (stmt) == 2)
3810 || (gimple_call_internal_p (stmt)
3811 && gimple_call_internal_fn (stmt) == IFN_BUILTIN_EXPECT))
3813 var = gimple_call_lhs (stmt);
3814 changed = true;
3815 if (var)
3817 ass_stmt
3818 = gimple_build_assign (var, gimple_call_arg (stmt, 0));
3819 gsi_replace (&bi, ass_stmt, true);
3821 else
3823 gsi_remove (&bi, true);
3824 continue;
3828 gsi_next (&bi);
3831 return changed ? TODO_cleanup_cfg : 0;
3834 } // anon namespace
3836 gimple_opt_pass *
3837 make_pass_strip_predict_hints (gcc::context *ctxt)
3839 return new pass_strip_predict_hints (ctxt);
3842 /* Rebuild function frequencies. Passes are in general expected to
3843 maintain profile by hand, however in some cases this is not possible:
3844 for example when inlining several functions with loops freuqencies might run
3845 out of scale and thus needs to be recomputed. */
3847 void
3848 rebuild_frequencies (void)
3850 timevar_push (TV_REBUILD_FREQUENCIES);
3852 /* When the max bb count in the function is small, there is a higher
3853 chance that there were truncation errors in the integer scaling
3854 of counts by inlining and other optimizations. This could lead
3855 to incorrect classification of code as being cold when it isn't.
3856 In that case, force the estimation of bb counts/frequencies from the
3857 branch probabilities, rather than computing frequencies from counts,
3858 which may also lead to frequencies incorrectly reduced to 0. There
3859 is less precision in the probabilities, so we only do this for small
3860 max counts. */
3861 cfun->cfg->count_max = profile_count::uninitialized ();
3862 basic_block bb;
3863 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
3864 cfun->cfg->count_max = cfun->cfg->count_max.max (bb->count);
3866 if (profile_status_for_fn (cfun) == PROFILE_GUESSED)
3868 loop_optimizer_init (0);
3869 add_noreturn_fake_exit_edges ();
3870 mark_irreducible_loops ();
3871 connect_infinite_loops_to_exit ();
3872 estimate_bb_frequencies (true);
3873 remove_fake_exit_edges ();
3874 loop_optimizer_finalize ();
3876 else if (profile_status_for_fn (cfun) == PROFILE_READ)
3877 counts_to_freqs ();
3878 else
3879 gcc_unreachable ();
3880 timevar_pop (TV_REBUILD_FREQUENCIES);
3883 /* Perform a dry run of the branch prediction pass and report comparsion of
3884 the predicted and real profile into the dump file. */
3886 void
3887 report_predictor_hitrates (void)
3889 unsigned nb_loops;
3891 loop_optimizer_init (LOOPS_NORMAL);
3892 if (dump_file && (dump_flags & TDF_DETAILS))
3893 flow_loops_dump (dump_file, NULL, 0);
3895 mark_irreducible_loops ();
3897 nb_loops = number_of_loops (cfun);
3898 if (nb_loops > 1)
3899 scev_initialize ();
3901 tree_estimate_probability (true);
3903 if (nb_loops > 1)
3904 scev_finalize ();
3906 loop_optimizer_finalize ();
3909 /* Force edge E to be cold.
3910 If IMPOSSIBLE is true, for edge to have count and probability 0 otherwise
3911 keep low probability to represent possible error in a guess. This is used
3912 i.e. in case we predict loop to likely iterate given number of times but
3913 we are not 100% sure.
3915 This function locally updates profile without attempt to keep global
3916 consistency which can not be reached in full generality without full profile
3917 rebuild from probabilities alone. Doing so is not necessarily a good idea
3918 because frequencies and counts may be more realistic then probabilities.
3920 In some cases (such as for elimination of early exits during full loop
3921 unrolling) the caller can ensure that profile will get consistent
3922 afterwards. */
3924 void
3925 force_edge_cold (edge e, bool impossible)
3927 profile_count count_sum = profile_count::zero ();
3928 profile_probability prob_sum = profile_probability::never ();
3929 edge_iterator ei;
3930 edge e2;
3931 bool uninitialized_exit = false;
3933 profile_probability goal = (impossible ? profile_probability::never ()
3934 : profile_probability::very_unlikely ());
3936 /* If edge is already improbably or cold, just return. */
3937 if (e->probability <= goal
3938 && (!impossible || e->count () == profile_count::zero ()))
3939 return;
3940 FOR_EACH_EDGE (e2, ei, e->src->succs)
3941 if (e2 != e)
3943 if (e2->count ().initialized_p ())
3944 count_sum += e2->count ();
3945 else
3946 uninitialized_exit = true;
3947 if (e2->probability.initialized_p ())
3948 prob_sum += e2->probability;
3951 /* If there are other edges out of e->src, redistribute probabilitity
3952 there. */
3953 if (prob_sum > profile_probability::never ())
3955 if (!(e->probability < goal))
3956 e->probability = goal;
3958 profile_probability prob_comp = prob_sum / e->probability.invert ();
3960 if (dump_file && (dump_flags & TDF_DETAILS))
3961 fprintf (dump_file, "Making edge %i->%i %s by redistributing "
3962 "probability to other edges.\n",
3963 e->src->index, e->dest->index,
3964 impossible ? "impossible" : "cold");
3965 FOR_EACH_EDGE (e2, ei, e->src->succs)
3966 if (e2 != e)
3968 e2->probability /= prob_comp;
3970 if (current_ir_type () != IR_GIMPLE
3971 && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
3972 update_br_prob_note (e->src);
3974 /* If all edges out of e->src are unlikely, the basic block itself
3975 is unlikely. */
3976 else
3978 if (prob_sum == profile_probability::never ())
3979 e->probability = profile_probability::always ();
3980 else
3982 if (impossible)
3983 e->probability = profile_probability::never ();
3984 /* If BB has some edges out that are not impossible, we can not
3985 assume that BB itself is. */
3986 impossible = false;
3988 if (current_ir_type () != IR_GIMPLE
3989 && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
3990 update_br_prob_note (e->src);
3991 if (e->src->count == profile_count::zero ())
3992 return;
3993 if (count_sum == profile_count::zero () && !uninitialized_exit
3994 && impossible)
3996 bool found = false;
3997 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
3999 else if (current_ir_type () == IR_GIMPLE)
4000 for (gimple_stmt_iterator gsi = gsi_start_bb (e->src);
4001 !gsi_end_p (gsi); gsi_next (&gsi))
4003 if (stmt_can_terminate_bb_p (gsi_stmt (gsi)))
4005 found = true;
4006 break;
4009 /* FIXME: Implement RTL path. */
4010 else
4011 found = true;
4012 if (!found)
4014 if (dump_file && (dump_flags & TDF_DETAILS))
4015 fprintf (dump_file,
4016 "Making bb %i impossible and dropping count to 0.\n",
4017 e->src->index);
4018 e->src->count = profile_count::zero ();
4019 FOR_EACH_EDGE (e2, ei, e->src->preds)
4020 force_edge_cold (e2, impossible);
4021 return;
4025 /* If we did not adjusting, the source basic block has no likely edeges
4026 leaving other direction. In that case force that bb cold, too.
4027 This in general is difficult task to do, but handle special case when
4028 BB has only one predecestor. This is common case when we are updating
4029 after loop transforms. */
4030 if (!(prob_sum > profile_probability::never ())
4031 && count_sum == profile_count::zero ()
4032 && single_pred_p (e->src) && e->src->count.to_frequency (cfun)
4033 > (impossible ? 0 : 1))
4035 int old_frequency = e->src->count.to_frequency (cfun);
4036 if (dump_file && (dump_flags & TDF_DETAILS))
4037 fprintf (dump_file, "Making bb %i %s.\n", e->src->index,
4038 impossible ? "impossible" : "cold");
4039 int new_frequency = MIN (e->src->count.to_frequency (cfun),
4040 impossible ? 0 : 1);
4041 if (impossible)
4042 e->src->count = profile_count::zero ();
4043 else
4044 e->src->count = e->count ().apply_scale (new_frequency,
4045 old_frequency);
4046 force_edge_cold (single_pred_edge (e->src), impossible);
4048 else if (dump_file && (dump_flags & TDF_DETAILS)
4049 && maybe_hot_bb_p (cfun, e->src))
4050 fprintf (dump_file, "Giving up on making bb %i %s.\n", e->src->index,
4051 impossible ? "impossible" : "cold");
4055 #if CHECKING_P
4057 namespace selftest {
4059 /* Test that value range of predictor values defined in predict.def is
4060 within range (50, 100]. */
4062 struct branch_predictor
4064 const char *name;
4065 unsigned probability;
4068 #define DEF_PREDICTOR(ENUM, NAME, HITRATE, FLAGS) { NAME, HITRATE },
4070 static void
4071 test_prediction_value_range ()
4073 branch_predictor predictors[] = {
4074 #include "predict.def"
4075 {NULL, -1U}
4078 for (unsigned i = 0; predictors[i].name != NULL; i++)
4080 unsigned p = 100 * predictors[i].probability / REG_BR_PROB_BASE;
4081 ASSERT_TRUE (p > 50 && p <= 100);
4085 #undef DEF_PREDICTOR
4087 /* Run all of the selfests within this file. */
4089 void
4090 predict_c_tests ()
4092 test_prediction_value_range ();
4095 } // namespace selftest
4096 #endif /* CHECKING_P. */