* invoke.texi (Wsuggest-attribute=cold): Document.
[official-gcc.git] / gcc / predict.c
blobe534502aaf58df3f1a14c0c7c3f5473b6654e500
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)->frequency * 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)->frequency)
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 *, profile_count count)
180 if (!count.initialized_p ())
181 return true;
182 /* Code executed at most once is not hot. */
183 if (count <= MAX (profile_info ? profile_info->runs : 1, 1))
184 return false;
185 return (count.to_gcov_type () >= get_hot_bb_threshold ());
188 /* Return true in case BB can be CPU intensive and should be optimized
189 for maximal performance. */
191 bool
192 maybe_hot_bb_p (struct function *fun, const_basic_block bb)
194 gcc_checking_assert (fun);
195 if (!maybe_hot_count_p (fun, bb->count))
196 return false;
197 return maybe_hot_frequency_p (fun, bb->frequency);
200 /* Return true in case BB can be CPU intensive and should be optimized
201 for maximal performance. */
203 bool
204 maybe_hot_edge_p (edge e)
206 if (!maybe_hot_count_p (cfun, e->count))
207 return false;
208 return maybe_hot_frequency_p (cfun, EDGE_FREQUENCY (e));
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, int)
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, bb->frequency);
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, EDGE_FREQUENCY (e));
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.initialized_p () && !dry_run)
1294 first->probability
1295 = profile_probability::from_reg_br_prob_base (combined_probability);
1296 second->probability = first->probability.invert ();
1300 /* Check if T1 and T2 satisfy the IV_COMPARE condition.
1301 Return the SSA_NAME if the condition satisfies, NULL otherwise.
1303 T1 and T2 should be one of the following cases:
1304 1. T1 is SSA_NAME, T2 is NULL
1305 2. T1 is SSA_NAME, T2 is INTEGER_CST between [-4, 4]
1306 3. T2 is SSA_NAME, T1 is INTEGER_CST between [-4, 4] */
1308 static tree
1309 strips_small_constant (tree t1, tree t2)
1311 tree ret = NULL;
1312 int value = 0;
1314 if (!t1)
1315 return NULL;
1316 else if (TREE_CODE (t1) == SSA_NAME)
1317 ret = t1;
1318 else if (tree_fits_shwi_p (t1))
1319 value = tree_to_shwi (t1);
1320 else
1321 return NULL;
1323 if (!t2)
1324 return ret;
1325 else if (tree_fits_shwi_p (t2))
1326 value = tree_to_shwi (t2);
1327 else if (TREE_CODE (t2) == SSA_NAME)
1329 if (ret)
1330 return NULL;
1331 else
1332 ret = t2;
1335 if (value <= 4 && value >= -4)
1336 return ret;
1337 else
1338 return NULL;
1341 /* Return the SSA_NAME in T or T's operands.
1342 Return NULL if SSA_NAME cannot be found. */
1344 static tree
1345 get_base_value (tree t)
1347 if (TREE_CODE (t) == SSA_NAME)
1348 return t;
1350 if (!BINARY_CLASS_P (t))
1351 return NULL;
1353 switch (TREE_OPERAND_LENGTH (t))
1355 case 1:
1356 return strips_small_constant (TREE_OPERAND (t, 0), NULL);
1357 case 2:
1358 return strips_small_constant (TREE_OPERAND (t, 0),
1359 TREE_OPERAND (t, 1));
1360 default:
1361 return NULL;
1365 /* Check the compare STMT in LOOP. If it compares an induction
1366 variable to a loop invariant, return true, and save
1367 LOOP_INVARIANT, COMPARE_CODE and LOOP_STEP.
1368 Otherwise return false and set LOOP_INVAIANT to NULL. */
1370 static bool
1371 is_comparison_with_loop_invariant_p (gcond *stmt, struct loop *loop,
1372 tree *loop_invariant,
1373 enum tree_code *compare_code,
1374 tree *loop_step,
1375 tree *loop_iv_base)
1377 tree op0, op1, bound, base;
1378 affine_iv iv0, iv1;
1379 enum tree_code code;
1380 tree step;
1382 code = gimple_cond_code (stmt);
1383 *loop_invariant = NULL;
1385 switch (code)
1387 case GT_EXPR:
1388 case GE_EXPR:
1389 case NE_EXPR:
1390 case LT_EXPR:
1391 case LE_EXPR:
1392 case EQ_EXPR:
1393 break;
1395 default:
1396 return false;
1399 op0 = gimple_cond_lhs (stmt);
1400 op1 = gimple_cond_rhs (stmt);
1402 if ((TREE_CODE (op0) != SSA_NAME && TREE_CODE (op0) != INTEGER_CST)
1403 || (TREE_CODE (op1) != SSA_NAME && TREE_CODE (op1) != INTEGER_CST))
1404 return false;
1405 if (!simple_iv (loop, loop_containing_stmt (stmt), op0, &iv0, true))
1406 return false;
1407 if (!simple_iv (loop, loop_containing_stmt (stmt), op1, &iv1, true))
1408 return false;
1409 if (TREE_CODE (iv0.step) != INTEGER_CST
1410 || TREE_CODE (iv1.step) != INTEGER_CST)
1411 return false;
1412 if ((integer_zerop (iv0.step) && integer_zerop (iv1.step))
1413 || (!integer_zerop (iv0.step) && !integer_zerop (iv1.step)))
1414 return false;
1416 if (integer_zerop (iv0.step))
1418 if (code != NE_EXPR && code != EQ_EXPR)
1419 code = invert_tree_comparison (code, false);
1420 bound = iv0.base;
1421 base = iv1.base;
1422 if (tree_fits_shwi_p (iv1.step))
1423 step = iv1.step;
1424 else
1425 return false;
1427 else
1429 bound = iv1.base;
1430 base = iv0.base;
1431 if (tree_fits_shwi_p (iv0.step))
1432 step = iv0.step;
1433 else
1434 return false;
1437 if (TREE_CODE (bound) != INTEGER_CST)
1438 bound = get_base_value (bound);
1439 if (!bound)
1440 return false;
1441 if (TREE_CODE (base) != INTEGER_CST)
1442 base = get_base_value (base);
1443 if (!base)
1444 return false;
1446 *loop_invariant = bound;
1447 *compare_code = code;
1448 *loop_step = step;
1449 *loop_iv_base = base;
1450 return true;
1453 /* Compare two SSA_NAMEs: returns TRUE if T1 and T2 are value coherent. */
1455 static bool
1456 expr_coherent_p (tree t1, tree t2)
1458 gimple *stmt;
1459 tree ssa_name_1 = NULL;
1460 tree ssa_name_2 = NULL;
1462 gcc_assert (TREE_CODE (t1) == SSA_NAME || TREE_CODE (t1) == INTEGER_CST);
1463 gcc_assert (TREE_CODE (t2) == SSA_NAME || TREE_CODE (t2) == INTEGER_CST);
1465 if (t1 == t2)
1466 return true;
1468 if (TREE_CODE (t1) == INTEGER_CST && TREE_CODE (t2) == INTEGER_CST)
1469 return true;
1470 if (TREE_CODE (t1) == INTEGER_CST || TREE_CODE (t2) == INTEGER_CST)
1471 return false;
1473 /* Check to see if t1 is expressed/defined with t2. */
1474 stmt = SSA_NAME_DEF_STMT (t1);
1475 gcc_assert (stmt != NULL);
1476 if (is_gimple_assign (stmt))
1478 ssa_name_1 = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_USE);
1479 if (ssa_name_1 && ssa_name_1 == t2)
1480 return true;
1483 /* Check to see if t2 is expressed/defined with t1. */
1484 stmt = SSA_NAME_DEF_STMT (t2);
1485 gcc_assert (stmt != NULL);
1486 if (is_gimple_assign (stmt))
1488 ssa_name_2 = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_USE);
1489 if (ssa_name_2 && ssa_name_2 == t1)
1490 return true;
1493 /* Compare if t1 and t2's def_stmts are identical. */
1494 if (ssa_name_2 != NULL && ssa_name_1 == ssa_name_2)
1495 return true;
1496 else
1497 return false;
1500 /* Return true if E is predicted by one of loop heuristics. */
1502 static bool
1503 predicted_by_loop_heuristics_p (basic_block bb)
1505 struct edge_prediction *i;
1506 edge_prediction **preds = bb_predictions->get (bb);
1508 if (!preds)
1509 return false;
1511 for (i = *preds; i; i = i->ep_next)
1512 if (i->ep_predictor == PRED_LOOP_ITERATIONS_GUESSED
1513 || i->ep_predictor == PRED_LOOP_ITERATIONS_MAX
1514 || i->ep_predictor == PRED_LOOP_ITERATIONS
1515 || i->ep_predictor == PRED_LOOP_EXIT
1516 || i->ep_predictor == PRED_LOOP_EXIT_WITH_RECURSION
1517 || i->ep_predictor == PRED_LOOP_EXTRA_EXIT)
1518 return true;
1519 return false;
1522 /* Predict branch probability of BB when BB contains a branch that compares
1523 an induction variable in LOOP with LOOP_IV_BASE_VAR to LOOP_BOUND_VAR. The
1524 loop exit is compared using LOOP_BOUND_CODE, with step of LOOP_BOUND_STEP.
1526 E.g.
1527 for (int i = 0; i < bound; i++) {
1528 if (i < bound - 2)
1529 computation_1();
1530 else
1531 computation_2();
1534 In this loop, we will predict the branch inside the loop to be taken. */
1536 static void
1537 predict_iv_comparison (struct loop *loop, basic_block bb,
1538 tree loop_bound_var,
1539 tree loop_iv_base_var,
1540 enum tree_code loop_bound_code,
1541 int loop_bound_step)
1543 gimple *stmt;
1544 tree compare_var, compare_base;
1545 enum tree_code compare_code;
1546 tree compare_step_var;
1547 edge then_edge;
1548 edge_iterator ei;
1550 if (predicted_by_loop_heuristics_p (bb))
1551 return;
1553 stmt = last_stmt (bb);
1554 if (!stmt || gimple_code (stmt) != GIMPLE_COND)
1555 return;
1556 if (!is_comparison_with_loop_invariant_p (as_a <gcond *> (stmt),
1557 loop, &compare_var,
1558 &compare_code,
1559 &compare_step_var,
1560 &compare_base))
1561 return;
1563 /* Find the taken edge. */
1564 FOR_EACH_EDGE (then_edge, ei, bb->succs)
1565 if (then_edge->flags & EDGE_TRUE_VALUE)
1566 break;
1568 /* When comparing an IV to a loop invariant, NE is more likely to be
1569 taken while EQ is more likely to be not-taken. */
1570 if (compare_code == NE_EXPR)
1572 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1573 return;
1575 else if (compare_code == EQ_EXPR)
1577 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, NOT_TAKEN);
1578 return;
1581 if (!expr_coherent_p (loop_iv_base_var, compare_base))
1582 return;
1584 /* If loop bound, base and compare bound are all constants, we can
1585 calculate the probability directly. */
1586 if (tree_fits_shwi_p (loop_bound_var)
1587 && tree_fits_shwi_p (compare_var)
1588 && tree_fits_shwi_p (compare_base))
1590 int probability;
1591 bool overflow, overall_overflow = false;
1592 widest_int compare_count, tem;
1594 /* (loop_bound - base) / compare_step */
1595 tem = wi::sub (wi::to_widest (loop_bound_var),
1596 wi::to_widest (compare_base), SIGNED, &overflow);
1597 overall_overflow |= overflow;
1598 widest_int loop_count = wi::div_trunc (tem,
1599 wi::to_widest (compare_step_var),
1600 SIGNED, &overflow);
1601 overall_overflow |= overflow;
1603 if (!wi::neg_p (wi::to_widest (compare_step_var))
1604 ^ (compare_code == LT_EXPR || compare_code == LE_EXPR))
1606 /* (loop_bound - compare_bound) / compare_step */
1607 tem = wi::sub (wi::to_widest (loop_bound_var),
1608 wi::to_widest (compare_var), SIGNED, &overflow);
1609 overall_overflow |= overflow;
1610 compare_count = wi::div_trunc (tem, wi::to_widest (compare_step_var),
1611 SIGNED, &overflow);
1612 overall_overflow |= overflow;
1614 else
1616 /* (compare_bound - base) / compare_step */
1617 tem = wi::sub (wi::to_widest (compare_var),
1618 wi::to_widest (compare_base), SIGNED, &overflow);
1619 overall_overflow |= overflow;
1620 compare_count = wi::div_trunc (tem, wi::to_widest (compare_step_var),
1621 SIGNED, &overflow);
1622 overall_overflow |= overflow;
1624 if (compare_code == LE_EXPR || compare_code == GE_EXPR)
1625 ++compare_count;
1626 if (loop_bound_code == LE_EXPR || loop_bound_code == GE_EXPR)
1627 ++loop_count;
1628 if (wi::neg_p (compare_count))
1629 compare_count = 0;
1630 if (wi::neg_p (loop_count))
1631 loop_count = 0;
1632 if (loop_count == 0)
1633 probability = 0;
1634 else if (wi::cmps (compare_count, loop_count) == 1)
1635 probability = REG_BR_PROB_BASE;
1636 else
1638 tem = compare_count * REG_BR_PROB_BASE;
1639 tem = wi::udiv_trunc (tem, loop_count);
1640 probability = tem.to_uhwi ();
1643 /* FIXME: The branch prediction seems broken. It has only 20% hitrate. */
1644 if (!overall_overflow)
1645 predict_edge (then_edge, PRED_LOOP_IV_COMPARE, probability);
1647 return;
1650 if (expr_coherent_p (loop_bound_var, compare_var))
1652 if ((loop_bound_code == LT_EXPR || loop_bound_code == LE_EXPR)
1653 && (compare_code == LT_EXPR || compare_code == LE_EXPR))
1654 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1655 else if ((loop_bound_code == GT_EXPR || loop_bound_code == GE_EXPR)
1656 && (compare_code == GT_EXPR || compare_code == GE_EXPR))
1657 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1658 else if (loop_bound_code == NE_EXPR)
1660 /* If the loop backedge condition is "(i != bound)", we do
1661 the comparison based on the step of IV:
1662 * step < 0 : backedge condition is like (i > bound)
1663 * step > 0 : backedge condition is like (i < bound) */
1664 gcc_assert (loop_bound_step != 0);
1665 if (loop_bound_step > 0
1666 && (compare_code == LT_EXPR
1667 || compare_code == LE_EXPR))
1668 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1669 else if (loop_bound_step < 0
1670 && (compare_code == GT_EXPR
1671 || compare_code == GE_EXPR))
1672 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1673 else
1674 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, NOT_TAKEN);
1676 else
1677 /* The branch is predicted not-taken if loop_bound_code is
1678 opposite with compare_code. */
1679 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, NOT_TAKEN);
1681 else if (expr_coherent_p (loop_iv_base_var, compare_var))
1683 /* For cases like:
1684 for (i = s; i < h; i++)
1685 if (i > s + 2) ....
1686 The branch should be predicted taken. */
1687 if (loop_bound_step > 0
1688 && (compare_code == GT_EXPR || compare_code == GE_EXPR))
1689 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1690 else if (loop_bound_step < 0
1691 && (compare_code == LT_EXPR || compare_code == LE_EXPR))
1692 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1693 else
1694 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, NOT_TAKEN);
1698 /* Predict for extra loop exits that will lead to EXIT_EDGE. The extra loop
1699 exits are resulted from short-circuit conditions that will generate an
1700 if_tmp. E.g.:
1702 if (foo() || global > 10)
1703 break;
1705 This will be translated into:
1707 BB3:
1708 loop header...
1709 BB4:
1710 if foo() goto BB6 else goto BB5
1711 BB5:
1712 if global > 10 goto BB6 else goto BB7
1713 BB6:
1714 goto BB7
1715 BB7:
1716 iftmp = (PHI 0(BB5), 1(BB6))
1717 if iftmp == 1 goto BB8 else goto BB3
1718 BB8:
1719 outside of the loop...
1721 The edge BB7->BB8 is loop exit because BB8 is outside of the loop.
1722 From the dataflow, we can infer that BB4->BB6 and BB5->BB6 are also loop
1723 exits. This function takes BB7->BB8 as input, and finds out the extra loop
1724 exits to predict them using PRED_LOOP_EXTRA_EXIT. */
1726 static void
1727 predict_extra_loop_exits (edge exit_edge)
1729 unsigned i;
1730 bool check_value_one;
1731 gimple *lhs_def_stmt;
1732 gphi *phi_stmt;
1733 tree cmp_rhs, cmp_lhs;
1734 gimple *last;
1735 gcond *cmp_stmt;
1737 last = last_stmt (exit_edge->src);
1738 if (!last)
1739 return;
1740 cmp_stmt = dyn_cast <gcond *> (last);
1741 if (!cmp_stmt)
1742 return;
1744 cmp_rhs = gimple_cond_rhs (cmp_stmt);
1745 cmp_lhs = gimple_cond_lhs (cmp_stmt);
1746 if (!TREE_CONSTANT (cmp_rhs)
1747 || !(integer_zerop (cmp_rhs) || integer_onep (cmp_rhs)))
1748 return;
1749 if (TREE_CODE (cmp_lhs) != SSA_NAME)
1750 return;
1752 /* If check_value_one is true, only the phi_args with value '1' will lead
1753 to loop exit. Otherwise, only the phi_args with value '0' will lead to
1754 loop exit. */
1755 check_value_one = (((integer_onep (cmp_rhs))
1756 ^ (gimple_cond_code (cmp_stmt) == EQ_EXPR))
1757 ^ ((exit_edge->flags & EDGE_TRUE_VALUE) != 0));
1759 lhs_def_stmt = SSA_NAME_DEF_STMT (cmp_lhs);
1760 if (!lhs_def_stmt)
1761 return;
1763 phi_stmt = dyn_cast <gphi *> (lhs_def_stmt);
1764 if (!phi_stmt)
1765 return;
1767 for (i = 0; i < gimple_phi_num_args (phi_stmt); i++)
1769 edge e1;
1770 edge_iterator ei;
1771 tree val = gimple_phi_arg_def (phi_stmt, i);
1772 edge e = gimple_phi_arg_edge (phi_stmt, i);
1774 if (!TREE_CONSTANT (val) || !(integer_zerop (val) || integer_onep (val)))
1775 continue;
1776 if ((check_value_one ^ integer_onep (val)) == 1)
1777 continue;
1778 if (EDGE_COUNT (e->src->succs) != 1)
1780 predict_paths_leading_to_edge (e, PRED_LOOP_EXTRA_EXIT, NOT_TAKEN);
1781 continue;
1784 FOR_EACH_EDGE (e1, ei, e->src->preds)
1785 predict_paths_leading_to_edge (e1, PRED_LOOP_EXTRA_EXIT, NOT_TAKEN);
1790 /* Predict edge probabilities by exploiting loop structure. */
1792 static void
1793 predict_loops (void)
1795 struct loop *loop;
1796 basic_block bb;
1797 hash_set <struct loop *> with_recursion(10);
1799 FOR_EACH_BB_FN (bb, cfun)
1801 gimple_stmt_iterator gsi;
1802 tree decl;
1804 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1805 if (is_gimple_call (gsi_stmt (gsi))
1806 && (decl = gimple_call_fndecl (gsi_stmt (gsi))) != NULL
1807 && recursive_call_p (current_function_decl, decl))
1809 loop = bb->loop_father;
1810 while (loop && !with_recursion.add (loop))
1811 loop = loop_outer (loop);
1815 /* Try to predict out blocks in a loop that are not part of a
1816 natural loop. */
1817 FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
1819 basic_block bb, *bbs;
1820 unsigned j, n_exits = 0;
1821 vec<edge> exits;
1822 struct tree_niter_desc niter_desc;
1823 edge ex;
1824 struct nb_iter_bound *nb_iter;
1825 enum tree_code loop_bound_code = ERROR_MARK;
1826 tree loop_bound_step = NULL;
1827 tree loop_bound_var = NULL;
1828 tree loop_iv_base = NULL;
1829 gcond *stmt = NULL;
1830 bool recursion = with_recursion.contains (loop);
1832 exits = get_loop_exit_edges (loop);
1833 FOR_EACH_VEC_ELT (exits, j, ex)
1834 if (!unlikely_executed_edge_p (ex) && !(ex->flags & EDGE_ABNORMAL_CALL))
1835 n_exits ++;
1836 if (!n_exits)
1838 exits.release ();
1839 continue;
1842 if (dump_file && (dump_flags & TDF_DETAILS))
1843 fprintf (dump_file, "Predicting loop %i%s with %i exits.\n",
1844 loop->num, recursion ? " (with recursion)":"", n_exits);
1845 if (dump_file && (dump_flags & TDF_DETAILS)
1846 && max_loop_iterations_int (loop) >= 0)
1848 fprintf (dump_file,
1849 "Loop %d iterates at most %i times.\n", loop->num,
1850 (int)max_loop_iterations_int (loop));
1852 if (dump_file && (dump_flags & TDF_DETAILS)
1853 && likely_max_loop_iterations_int (loop) >= 0)
1855 fprintf (dump_file, "Loop %d likely iterates at most %i times.\n",
1856 loop->num, (int)likely_max_loop_iterations_int (loop));
1859 FOR_EACH_VEC_ELT (exits, j, ex)
1861 tree niter = NULL;
1862 HOST_WIDE_INT nitercst;
1863 int max = PARAM_VALUE (PARAM_MAX_PREDICTED_ITERATIONS);
1864 int probability;
1865 enum br_predictor predictor;
1866 widest_int nit;
1868 if (unlikely_executed_edge_p (ex)
1869 || (ex->flags & EDGE_ABNORMAL_CALL))
1870 continue;
1871 /* Loop heuristics do not expect exit conditional to be inside
1872 inner loop. We predict from innermost to outermost loop. */
1873 if (predicted_by_loop_heuristics_p (ex->src))
1875 if (dump_file && (dump_flags & TDF_DETAILS))
1876 fprintf (dump_file, "Skipping exit %i->%i because "
1877 "it is already predicted.\n",
1878 ex->src->index, ex->dest->index);
1879 continue;
1881 predict_extra_loop_exits (ex);
1883 if (number_of_iterations_exit (loop, ex, &niter_desc, false, false))
1884 niter = niter_desc.niter;
1885 if (!niter || TREE_CODE (niter_desc.niter) != INTEGER_CST)
1886 niter = loop_niter_by_eval (loop, ex);
1887 if (dump_file && (dump_flags & TDF_DETAILS)
1888 && TREE_CODE (niter) == INTEGER_CST)
1890 fprintf (dump_file, "Exit %i->%i %d iterates ",
1891 ex->src->index, ex->dest->index,
1892 loop->num);
1893 print_generic_expr (dump_file, niter, TDF_SLIM);
1894 fprintf (dump_file, " times.\n");
1897 if (TREE_CODE (niter) == INTEGER_CST)
1899 if (tree_fits_uhwi_p (niter)
1900 && max
1901 && compare_tree_int (niter, max - 1) == -1)
1902 nitercst = tree_to_uhwi (niter) + 1;
1903 else
1904 nitercst = max;
1905 predictor = PRED_LOOP_ITERATIONS;
1907 /* If we have just one exit and we can derive some information about
1908 the number of iterations of the loop from the statements inside
1909 the loop, use it to predict this exit. */
1910 else if (n_exits == 1
1911 && estimated_stmt_executions (loop, &nit))
1913 if (wi::gtu_p (nit, max))
1914 nitercst = max;
1915 else
1916 nitercst = nit.to_shwi ();
1917 predictor = PRED_LOOP_ITERATIONS_GUESSED;
1919 /* If we have likely upper bound, trust it for very small iteration
1920 counts. Such loops would otherwise get mispredicted by standard
1921 LOOP_EXIT heuristics. */
1922 else if (n_exits == 1
1923 && likely_max_stmt_executions (loop, &nit)
1924 && wi::ltu_p (nit,
1925 RDIV (REG_BR_PROB_BASE,
1926 REG_BR_PROB_BASE
1927 - predictor_info
1928 [recursion
1929 ? PRED_LOOP_EXIT_WITH_RECURSION
1930 : PRED_LOOP_EXIT].hitrate)))
1932 nitercst = nit.to_shwi ();
1933 predictor = PRED_LOOP_ITERATIONS_MAX;
1935 else
1937 if (dump_file && (dump_flags & TDF_DETAILS))
1938 fprintf (dump_file, "Nothing known about exit %i->%i.\n",
1939 ex->src->index, ex->dest->index);
1940 continue;
1943 if (dump_file && (dump_flags & TDF_DETAILS))
1944 fprintf (dump_file, "Recording prediction to %i iterations by %s.\n",
1945 (int)nitercst, predictor_info[predictor].name);
1946 /* If the prediction for number of iterations is zero, do not
1947 predict the exit edges. */
1948 if (nitercst == 0)
1949 continue;
1951 probability = RDIV (REG_BR_PROB_BASE, nitercst);
1952 predict_edge (ex, predictor, probability);
1954 exits.release ();
1956 /* Find information about loop bound variables. */
1957 for (nb_iter = loop->bounds; nb_iter;
1958 nb_iter = nb_iter->next)
1959 if (nb_iter->stmt
1960 && gimple_code (nb_iter->stmt) == GIMPLE_COND)
1962 stmt = as_a <gcond *> (nb_iter->stmt);
1963 break;
1965 if (!stmt && last_stmt (loop->header)
1966 && gimple_code (last_stmt (loop->header)) == GIMPLE_COND)
1967 stmt = as_a <gcond *> (last_stmt (loop->header));
1968 if (stmt)
1969 is_comparison_with_loop_invariant_p (stmt, loop,
1970 &loop_bound_var,
1971 &loop_bound_code,
1972 &loop_bound_step,
1973 &loop_iv_base);
1975 bbs = get_loop_body (loop);
1977 for (j = 0; j < loop->num_nodes; j++)
1979 edge e;
1980 edge_iterator ei;
1982 bb = bbs[j];
1984 /* Bypass loop heuristics on continue statement. These
1985 statements construct loops via "non-loop" constructs
1986 in the source language and are better to be handled
1987 separately. */
1988 if (predicted_by_p (bb, PRED_CONTINUE))
1990 if (dump_file && (dump_flags & TDF_DETAILS))
1991 fprintf (dump_file, "BB %i predicted by continue.\n",
1992 bb->index);
1993 continue;
1996 /* If we already used more reliable loop exit predictors, do not
1997 bother with PRED_LOOP_EXIT. */
1998 if (!predicted_by_loop_heuristics_p (bb))
2000 /* For loop with many exits we don't want to predict all exits
2001 with the pretty large probability, because if all exits are
2002 considered in row, the loop would be predicted to iterate
2003 almost never. The code to divide probability by number of
2004 exits is very rough. It should compute the number of exits
2005 taken in each patch through function (not the overall number
2006 of exits that might be a lot higher for loops with wide switch
2007 statements in them) and compute n-th square root.
2009 We limit the minimal probability by 2% to avoid
2010 EDGE_PROBABILITY_RELIABLE from trusting the branch prediction
2011 as this was causing regression in perl benchmark containing such
2012 a wide loop. */
2014 int probability = ((REG_BR_PROB_BASE
2015 - predictor_info
2016 [recursion
2017 ? PRED_LOOP_EXIT_WITH_RECURSION
2018 : PRED_LOOP_EXIT].hitrate)
2019 / n_exits);
2020 if (probability < HITRATE (2))
2021 probability = HITRATE (2);
2022 FOR_EACH_EDGE (e, ei, bb->succs)
2023 if (e->dest->index < NUM_FIXED_BLOCKS
2024 || !flow_bb_inside_loop_p (loop, e->dest))
2026 if (dump_file && (dump_flags & TDF_DETAILS))
2027 fprintf (dump_file,
2028 "Predicting exit %i->%i with prob %i.\n",
2029 e->src->index, e->dest->index, probability);
2030 predict_edge (e,
2031 recursion ? PRED_LOOP_EXIT_WITH_RECURSION
2032 : PRED_LOOP_EXIT, probability);
2035 if (loop_bound_var)
2036 predict_iv_comparison (loop, bb, loop_bound_var, loop_iv_base,
2037 loop_bound_code,
2038 tree_to_shwi (loop_bound_step));
2041 /* In the following code
2042 for (loop1)
2043 if (cond)
2044 for (loop2)
2045 body;
2046 guess that cond is unlikely. */
2047 if (loop_outer (loop)->num)
2049 basic_block bb = NULL;
2050 edge preheader_edge = loop_preheader_edge (loop);
2052 if (single_pred_p (preheader_edge->src)
2053 && single_succ_p (preheader_edge->src))
2054 preheader_edge = single_pred_edge (preheader_edge->src);
2056 gimple *stmt = last_stmt (preheader_edge->src);
2057 /* Pattern match fortran loop preheader:
2058 _16 = BUILTIN_EXPECT (_15, 1, PRED_FORTRAN_LOOP_PREHEADER);
2059 _17 = (logical(kind=4)) _16;
2060 if (_17 != 0)
2061 goto <bb 11>;
2062 else
2063 goto <bb 13>;
2065 Loop guard branch prediction says nothing about duplicated loop
2066 headers produced by fortran frontend and in this case we want
2067 to predict paths leading to this preheader. */
2069 if (stmt
2070 && gimple_code (stmt) == GIMPLE_COND
2071 && gimple_cond_code (stmt) == NE_EXPR
2072 && TREE_CODE (gimple_cond_lhs (stmt)) == SSA_NAME
2073 && integer_zerop (gimple_cond_rhs (stmt)))
2075 gimple *call_stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
2076 if (gimple_code (call_stmt) == GIMPLE_ASSIGN
2077 && gimple_expr_code (call_stmt) == NOP_EXPR
2078 && TREE_CODE (gimple_assign_rhs1 (call_stmt)) == SSA_NAME)
2079 call_stmt = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (call_stmt));
2080 if (gimple_call_internal_p (call_stmt, IFN_BUILTIN_EXPECT)
2081 && TREE_CODE (gimple_call_arg (call_stmt, 2)) == INTEGER_CST
2082 && tree_fits_uhwi_p (gimple_call_arg (call_stmt, 2))
2083 && tree_to_uhwi (gimple_call_arg (call_stmt, 2))
2084 == PRED_FORTRAN_LOOP_PREHEADER)
2085 bb = preheader_edge->src;
2087 if (!bb)
2089 if (!dominated_by_p (CDI_DOMINATORS,
2090 loop_outer (loop)->latch, loop->header))
2091 predict_paths_leading_to_edge (loop_preheader_edge (loop),
2092 recursion
2093 ? PRED_LOOP_GUARD_WITH_RECURSION
2094 : PRED_LOOP_GUARD,
2095 NOT_TAKEN,
2096 loop_outer (loop));
2098 else
2100 if (!dominated_by_p (CDI_DOMINATORS,
2101 loop_outer (loop)->latch, bb))
2102 predict_paths_leading_to (bb,
2103 recursion
2104 ? PRED_LOOP_GUARD_WITH_RECURSION
2105 : PRED_LOOP_GUARD,
2106 NOT_TAKEN,
2107 loop_outer (loop));
2111 /* Free basic blocks from get_loop_body. */
2112 free (bbs);
2116 /* Attempt to predict probabilities of BB outgoing edges using local
2117 properties. */
2118 static void
2119 bb_estimate_probability_locally (basic_block bb)
2121 rtx_insn *last_insn = BB_END (bb);
2122 rtx cond;
2124 if (! can_predict_insn_p (last_insn))
2125 return;
2126 cond = get_condition (last_insn, NULL, false, false);
2127 if (! cond)
2128 return;
2130 /* Try "pointer heuristic."
2131 A comparison ptr == 0 is predicted as false.
2132 Similarly, a comparison ptr1 == ptr2 is predicted as false. */
2133 if (COMPARISON_P (cond)
2134 && ((REG_P (XEXP (cond, 0)) && REG_POINTER (XEXP (cond, 0)))
2135 || (REG_P (XEXP (cond, 1)) && REG_POINTER (XEXP (cond, 1)))))
2137 if (GET_CODE (cond) == EQ)
2138 predict_insn_def (last_insn, PRED_POINTER, NOT_TAKEN);
2139 else if (GET_CODE (cond) == NE)
2140 predict_insn_def (last_insn, PRED_POINTER, TAKEN);
2142 else
2144 /* Try "opcode heuristic."
2145 EQ tests are usually false and NE tests are usually true. Also,
2146 most quantities are positive, so we can make the appropriate guesses
2147 about signed comparisons against zero. */
2148 switch (GET_CODE (cond))
2150 case CONST_INT:
2151 /* Unconditional branch. */
2152 predict_insn_def (last_insn, PRED_UNCONDITIONAL,
2153 cond == const0_rtx ? NOT_TAKEN : TAKEN);
2154 break;
2156 case EQ:
2157 case UNEQ:
2158 /* Floating point comparisons appears to behave in a very
2159 unpredictable way because of special role of = tests in
2160 FP code. */
2161 if (FLOAT_MODE_P (GET_MODE (XEXP (cond, 0))))
2163 /* Comparisons with 0 are often used for booleans and there is
2164 nothing useful to predict about them. */
2165 else if (XEXP (cond, 1) == const0_rtx
2166 || XEXP (cond, 0) == const0_rtx)
2168 else
2169 predict_insn_def (last_insn, PRED_OPCODE_NONEQUAL, NOT_TAKEN);
2170 break;
2172 case NE:
2173 case LTGT:
2174 /* Floating point comparisons appears to behave in a very
2175 unpredictable way because of special role of = tests in
2176 FP code. */
2177 if (FLOAT_MODE_P (GET_MODE (XEXP (cond, 0))))
2179 /* Comparisons with 0 are often used for booleans and there is
2180 nothing useful to predict about them. */
2181 else if (XEXP (cond, 1) == const0_rtx
2182 || XEXP (cond, 0) == const0_rtx)
2184 else
2185 predict_insn_def (last_insn, PRED_OPCODE_NONEQUAL, TAKEN);
2186 break;
2188 case ORDERED:
2189 predict_insn_def (last_insn, PRED_FPOPCODE, TAKEN);
2190 break;
2192 case UNORDERED:
2193 predict_insn_def (last_insn, PRED_FPOPCODE, NOT_TAKEN);
2194 break;
2196 case LE:
2197 case LT:
2198 if (XEXP (cond, 1) == const0_rtx || XEXP (cond, 1) == const1_rtx
2199 || XEXP (cond, 1) == constm1_rtx)
2200 predict_insn_def (last_insn, PRED_OPCODE_POSITIVE, NOT_TAKEN);
2201 break;
2203 case GE:
2204 case GT:
2205 if (XEXP (cond, 1) == const0_rtx || XEXP (cond, 1) == const1_rtx
2206 || XEXP (cond, 1) == constm1_rtx)
2207 predict_insn_def (last_insn, PRED_OPCODE_POSITIVE, TAKEN);
2208 break;
2210 default:
2211 break;
2215 /* Set edge->probability for each successor edge of BB. */
2216 void
2217 guess_outgoing_edge_probabilities (basic_block bb)
2219 bb_estimate_probability_locally (bb);
2220 combine_predictions_for_insn (BB_END (bb), bb);
2223 static tree expr_expected_value (tree, bitmap, enum br_predictor *predictor);
2225 /* Helper function for expr_expected_value. */
2227 static tree
2228 expr_expected_value_1 (tree type, tree op0, enum tree_code code,
2229 tree op1, bitmap visited, enum br_predictor *predictor)
2231 gimple *def;
2233 if (predictor)
2234 *predictor = PRED_UNCONDITIONAL;
2236 if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS)
2238 if (TREE_CONSTANT (op0))
2239 return op0;
2241 if (code == IMAGPART_EXPR)
2243 if (TREE_CODE (TREE_OPERAND (op0, 0)) == SSA_NAME)
2245 def = SSA_NAME_DEF_STMT (TREE_OPERAND (op0, 0));
2246 if (is_gimple_call (def)
2247 && gimple_call_internal_p (def)
2248 && (gimple_call_internal_fn (def)
2249 == IFN_ATOMIC_COMPARE_EXCHANGE))
2251 /* Assume that any given atomic operation has low contention,
2252 and thus the compare-and-swap operation succeeds. */
2253 if (predictor)
2254 *predictor = PRED_COMPARE_AND_SWAP;
2255 return build_one_cst (TREE_TYPE (op0));
2260 if (code != SSA_NAME)
2261 return NULL_TREE;
2263 def = SSA_NAME_DEF_STMT (op0);
2265 /* If we were already here, break the infinite cycle. */
2266 if (!bitmap_set_bit (visited, SSA_NAME_VERSION (op0)))
2267 return NULL;
2269 if (gimple_code (def) == GIMPLE_PHI)
2271 /* All the arguments of the PHI node must have the same constant
2272 length. */
2273 int i, n = gimple_phi_num_args (def);
2274 tree val = NULL, new_val;
2276 for (i = 0; i < n; i++)
2278 tree arg = PHI_ARG_DEF (def, i);
2279 enum br_predictor predictor2;
2281 /* If this PHI has itself as an argument, we cannot
2282 determine the string length of this argument. However,
2283 if we can find an expected constant value for the other
2284 PHI args then we can still be sure that this is
2285 likely a constant. So be optimistic and just
2286 continue with the next argument. */
2287 if (arg == PHI_RESULT (def))
2288 continue;
2290 new_val = expr_expected_value (arg, visited, &predictor2);
2292 /* It is difficult to combine value predictors. Simply assume
2293 that later predictor is weaker and take its prediction. */
2294 if (predictor && *predictor < predictor2)
2295 *predictor = predictor2;
2296 if (!new_val)
2297 return NULL;
2298 if (!val)
2299 val = new_val;
2300 else if (!operand_equal_p (val, new_val, false))
2301 return NULL;
2303 return val;
2305 if (is_gimple_assign (def))
2307 if (gimple_assign_lhs (def) != op0)
2308 return NULL;
2310 return expr_expected_value_1 (TREE_TYPE (gimple_assign_lhs (def)),
2311 gimple_assign_rhs1 (def),
2312 gimple_assign_rhs_code (def),
2313 gimple_assign_rhs2 (def),
2314 visited, predictor);
2317 if (is_gimple_call (def))
2319 tree decl = gimple_call_fndecl (def);
2320 if (!decl)
2322 if (gimple_call_internal_p (def)
2323 && gimple_call_internal_fn (def) == IFN_BUILTIN_EXPECT)
2325 gcc_assert (gimple_call_num_args (def) == 3);
2326 tree val = gimple_call_arg (def, 0);
2327 if (TREE_CONSTANT (val))
2328 return val;
2329 if (predictor)
2331 tree val2 = gimple_call_arg (def, 2);
2332 gcc_assert (TREE_CODE (val2) == INTEGER_CST
2333 && tree_fits_uhwi_p (val2)
2334 && tree_to_uhwi (val2) < END_PREDICTORS);
2335 *predictor = (enum br_predictor) tree_to_uhwi (val2);
2337 return gimple_call_arg (def, 1);
2339 return NULL;
2341 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2342 switch (DECL_FUNCTION_CODE (decl))
2344 case BUILT_IN_EXPECT:
2346 tree val;
2347 if (gimple_call_num_args (def) != 2)
2348 return NULL;
2349 val = gimple_call_arg (def, 0);
2350 if (TREE_CONSTANT (val))
2351 return val;
2352 if (predictor)
2353 *predictor = PRED_BUILTIN_EXPECT;
2354 return gimple_call_arg (def, 1);
2357 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
2358 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
2359 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
2360 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
2361 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
2362 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
2363 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
2364 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
2365 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
2366 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
2367 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
2368 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
2369 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
2370 /* Assume that any given atomic operation has low contention,
2371 and thus the compare-and-swap operation succeeds. */
2372 if (predictor)
2373 *predictor = PRED_COMPARE_AND_SWAP;
2374 return boolean_true_node;
2375 default:
2376 break;
2380 return NULL;
2383 if (get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS)
2385 tree res;
2386 enum br_predictor predictor2;
2387 op0 = expr_expected_value (op0, visited, predictor);
2388 if (!op0)
2389 return NULL;
2390 op1 = expr_expected_value (op1, visited, &predictor2);
2391 if (predictor && *predictor < predictor2)
2392 *predictor = predictor2;
2393 if (!op1)
2394 return NULL;
2395 res = fold_build2 (code, type, op0, op1);
2396 if (TREE_CONSTANT (res))
2397 return res;
2398 return NULL;
2400 if (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS)
2402 tree res;
2403 op0 = expr_expected_value (op0, visited, predictor);
2404 if (!op0)
2405 return NULL;
2406 res = fold_build1 (code, type, op0);
2407 if (TREE_CONSTANT (res))
2408 return res;
2409 return NULL;
2411 return NULL;
2414 /* Return constant EXPR will likely have at execution time, NULL if unknown.
2415 The function is used by builtin_expect branch predictor so the evidence
2416 must come from this construct and additional possible constant folding.
2418 We may want to implement more involved value guess (such as value range
2419 propagation based prediction), but such tricks shall go to new
2420 implementation. */
2422 static tree
2423 expr_expected_value (tree expr, bitmap visited,
2424 enum br_predictor *predictor)
2426 enum tree_code code;
2427 tree op0, op1;
2429 if (TREE_CONSTANT (expr))
2431 if (predictor)
2432 *predictor = PRED_UNCONDITIONAL;
2433 return expr;
2436 extract_ops_from_tree (expr, &code, &op0, &op1);
2437 return expr_expected_value_1 (TREE_TYPE (expr),
2438 op0, code, op1, visited, predictor);
2441 /* Predict using opcode of the last statement in basic block. */
2442 static void
2443 tree_predict_by_opcode (basic_block bb)
2445 gimple *stmt = last_stmt (bb);
2446 edge then_edge;
2447 tree op0, op1;
2448 tree type;
2449 tree val;
2450 enum tree_code cmp;
2451 edge_iterator ei;
2452 enum br_predictor predictor;
2454 if (!stmt || gimple_code (stmt) != GIMPLE_COND)
2455 return;
2456 FOR_EACH_EDGE (then_edge, ei, bb->succs)
2457 if (then_edge->flags & EDGE_TRUE_VALUE)
2458 break;
2459 op0 = gimple_cond_lhs (stmt);
2460 op1 = gimple_cond_rhs (stmt);
2461 cmp = gimple_cond_code (stmt);
2462 type = TREE_TYPE (op0);
2463 val = expr_expected_value_1 (boolean_type_node, op0, cmp, op1, auto_bitmap (),
2464 &predictor);
2465 if (val && TREE_CODE (val) == INTEGER_CST)
2467 if (predictor == PRED_BUILTIN_EXPECT)
2469 int percent = PARAM_VALUE (BUILTIN_EXPECT_PROBABILITY);
2471 gcc_assert (percent >= 0 && percent <= 100);
2472 if (integer_zerop (val))
2473 percent = 100 - percent;
2474 predict_edge (then_edge, PRED_BUILTIN_EXPECT, HITRATE (percent));
2476 else
2477 predict_edge_def (then_edge, predictor,
2478 integer_zerop (val) ? NOT_TAKEN : TAKEN);
2480 /* Try "pointer heuristic."
2481 A comparison ptr == 0 is predicted as false.
2482 Similarly, a comparison ptr1 == ptr2 is predicted as false. */
2483 if (POINTER_TYPE_P (type))
2485 if (cmp == EQ_EXPR)
2486 predict_edge_def (then_edge, PRED_TREE_POINTER, NOT_TAKEN);
2487 else if (cmp == NE_EXPR)
2488 predict_edge_def (then_edge, PRED_TREE_POINTER, TAKEN);
2490 else
2492 /* Try "opcode heuristic."
2493 EQ tests are usually false and NE tests are usually true. Also,
2494 most quantities are positive, so we can make the appropriate guesses
2495 about signed comparisons against zero. */
2496 switch (cmp)
2498 case EQ_EXPR:
2499 case UNEQ_EXPR:
2500 /* Floating point comparisons appears to behave in a very
2501 unpredictable way because of special role of = tests in
2502 FP code. */
2503 if (FLOAT_TYPE_P (type))
2505 /* Comparisons with 0 are often used for booleans and there is
2506 nothing useful to predict about them. */
2507 else if (integer_zerop (op0) || integer_zerop (op1))
2509 else
2510 predict_edge_def (then_edge, PRED_TREE_OPCODE_NONEQUAL, NOT_TAKEN);
2511 break;
2513 case NE_EXPR:
2514 case LTGT_EXPR:
2515 /* Floating point comparisons appears to behave in a very
2516 unpredictable way because of special role of = tests in
2517 FP code. */
2518 if (FLOAT_TYPE_P (type))
2520 /* Comparisons with 0 are often used for booleans and there is
2521 nothing useful to predict about them. */
2522 else if (integer_zerop (op0)
2523 || integer_zerop (op1))
2525 else
2526 predict_edge_def (then_edge, PRED_TREE_OPCODE_NONEQUAL, TAKEN);
2527 break;
2529 case ORDERED_EXPR:
2530 predict_edge_def (then_edge, PRED_TREE_FPOPCODE, TAKEN);
2531 break;
2533 case UNORDERED_EXPR:
2534 predict_edge_def (then_edge, PRED_TREE_FPOPCODE, NOT_TAKEN);
2535 break;
2537 case LE_EXPR:
2538 case LT_EXPR:
2539 if (integer_zerop (op1)
2540 || integer_onep (op1)
2541 || integer_all_onesp (op1)
2542 || real_zerop (op1)
2543 || real_onep (op1)
2544 || real_minus_onep (op1))
2545 predict_edge_def (then_edge, PRED_TREE_OPCODE_POSITIVE, NOT_TAKEN);
2546 break;
2548 case GE_EXPR:
2549 case GT_EXPR:
2550 if (integer_zerop (op1)
2551 || integer_onep (op1)
2552 || integer_all_onesp (op1)
2553 || real_zerop (op1)
2554 || real_onep (op1)
2555 || real_minus_onep (op1))
2556 predict_edge_def (then_edge, PRED_TREE_OPCODE_POSITIVE, TAKEN);
2557 break;
2559 default:
2560 break;
2564 /* Returns TRUE if the STMT is exit(0) like statement. */
2566 static bool
2567 is_exit_with_zero_arg (const gimple *stmt)
2569 /* This is not exit, _exit or _Exit. */
2570 if (!gimple_call_builtin_p (stmt, BUILT_IN_EXIT)
2571 && !gimple_call_builtin_p (stmt, BUILT_IN__EXIT)
2572 && !gimple_call_builtin_p (stmt, BUILT_IN__EXIT2))
2573 return false;
2575 /* Argument is an interger zero. */
2576 return integer_zerop (gimple_call_arg (stmt, 0));
2579 /* Try to guess whether the value of return means error code. */
2581 static enum br_predictor
2582 return_prediction (tree val, enum prediction *prediction)
2584 /* VOID. */
2585 if (!val)
2586 return PRED_NO_PREDICTION;
2587 /* Different heuristics for pointers and scalars. */
2588 if (POINTER_TYPE_P (TREE_TYPE (val)))
2590 /* NULL is usually not returned. */
2591 if (integer_zerop (val))
2593 *prediction = NOT_TAKEN;
2594 return PRED_NULL_RETURN;
2597 else if (INTEGRAL_TYPE_P (TREE_TYPE (val)))
2599 /* Negative return values are often used to indicate
2600 errors. */
2601 if (TREE_CODE (val) == INTEGER_CST
2602 && tree_int_cst_sgn (val) < 0)
2604 *prediction = NOT_TAKEN;
2605 return PRED_NEGATIVE_RETURN;
2607 /* Constant return values seems to be commonly taken.
2608 Zero/one often represent booleans so exclude them from the
2609 heuristics. */
2610 if (TREE_CONSTANT (val)
2611 && (!integer_zerop (val) && !integer_onep (val)))
2613 *prediction = NOT_TAKEN;
2614 return PRED_CONST_RETURN;
2617 return PRED_NO_PREDICTION;
2620 /* Find the basic block with return expression and look up for possible
2621 return value trying to apply RETURN_PREDICTION heuristics. */
2622 static void
2623 apply_return_prediction (void)
2625 greturn *return_stmt = NULL;
2626 tree return_val;
2627 edge e;
2628 gphi *phi;
2629 int phi_num_args, i;
2630 enum br_predictor pred;
2631 enum prediction direction;
2632 edge_iterator ei;
2634 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
2636 gimple *last = last_stmt (e->src);
2637 if (last
2638 && gimple_code (last) == GIMPLE_RETURN)
2640 return_stmt = as_a <greturn *> (last);
2641 break;
2644 if (!e)
2645 return;
2646 return_val = gimple_return_retval (return_stmt);
2647 if (!return_val)
2648 return;
2649 if (TREE_CODE (return_val) != SSA_NAME
2650 || !SSA_NAME_DEF_STMT (return_val)
2651 || gimple_code (SSA_NAME_DEF_STMT (return_val)) != GIMPLE_PHI)
2652 return;
2653 phi = as_a <gphi *> (SSA_NAME_DEF_STMT (return_val));
2654 phi_num_args = gimple_phi_num_args (phi);
2655 pred = return_prediction (PHI_ARG_DEF (phi, 0), &direction);
2657 /* Avoid the degenerate case where all return values form the function
2658 belongs to same category (ie they are all positive constants)
2659 so we can hardly say something about them. */
2660 for (i = 1; i < phi_num_args; i++)
2661 if (pred != return_prediction (PHI_ARG_DEF (phi, i), &direction))
2662 break;
2663 if (i != phi_num_args)
2664 for (i = 0; i < phi_num_args; i++)
2666 pred = return_prediction (PHI_ARG_DEF (phi, i), &direction);
2667 if (pred != PRED_NO_PREDICTION)
2668 predict_paths_leading_to_edge (gimple_phi_arg_edge (phi, i), pred,
2669 direction);
2673 /* Look for basic block that contains unlikely to happen events
2674 (such as noreturn calls) and mark all paths leading to execution
2675 of this basic blocks as unlikely. */
2677 static void
2678 tree_bb_level_predictions (void)
2680 basic_block bb;
2681 bool has_return_edges = false;
2682 edge e;
2683 edge_iterator ei;
2685 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
2686 if (!unlikely_executed_edge_p (e) && !(e->flags & EDGE_ABNORMAL_CALL))
2688 has_return_edges = true;
2689 break;
2692 apply_return_prediction ();
2694 FOR_EACH_BB_FN (bb, cfun)
2696 gimple_stmt_iterator gsi;
2698 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2700 gimple *stmt = gsi_stmt (gsi);
2701 tree decl;
2703 if (is_gimple_call (stmt))
2705 if (gimple_call_noreturn_p (stmt)
2706 && has_return_edges
2707 && !is_exit_with_zero_arg (stmt))
2708 predict_paths_leading_to (bb, PRED_NORETURN,
2709 NOT_TAKEN);
2710 decl = gimple_call_fndecl (stmt);
2711 if (decl
2712 && lookup_attribute ("cold",
2713 DECL_ATTRIBUTES (decl)))
2714 predict_paths_leading_to (bb, PRED_COLD_FUNCTION,
2715 NOT_TAKEN);
2716 if (decl && recursive_call_p (current_function_decl, decl))
2717 predict_paths_leading_to (bb, PRED_RECURSIVE_CALL,
2718 NOT_TAKEN);
2720 else if (gimple_code (stmt) == GIMPLE_PREDICT)
2722 predict_paths_leading_to (bb, gimple_predict_predictor (stmt),
2723 gimple_predict_outcome (stmt));
2724 /* Keep GIMPLE_PREDICT around so early inlining will propagate
2725 hints to callers. */
2731 /* Callback for hash_map::traverse, asserts that the pointer map is
2732 empty. */
2734 bool
2735 assert_is_empty (const_basic_block const &, edge_prediction *const &value,
2736 void *)
2738 gcc_assert (!value);
2739 return false;
2742 /* Predict branch probabilities and estimate profile for basic block BB.
2743 When LOCAL_ONLY is set do not use any global properties of CFG. */
2745 static void
2746 tree_estimate_probability_bb (basic_block bb, bool local_only)
2748 edge e;
2749 edge_iterator ei;
2751 FOR_EACH_EDGE (e, ei, bb->succs)
2753 /* Look for block we are guarding (ie we dominate it,
2754 but it doesn't postdominate us). */
2755 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun) && e->dest != bb
2756 && !local_only
2757 && dominated_by_p (CDI_DOMINATORS, e->dest, e->src)
2758 && !dominated_by_p (CDI_POST_DOMINATORS, e->src, e->dest))
2760 gimple_stmt_iterator bi;
2762 /* The call heuristic claims that a guarded function call
2763 is improbable. This is because such calls are often used
2764 to signal exceptional situations such as printing error
2765 messages. */
2766 for (bi = gsi_start_bb (e->dest); !gsi_end_p (bi);
2767 gsi_next (&bi))
2769 gimple *stmt = gsi_stmt (bi);
2770 if (is_gimple_call (stmt)
2771 && !gimple_inexpensive_call_p (as_a <gcall *> (stmt))
2772 /* Constant and pure calls are hardly used to signalize
2773 something exceptional. */
2774 && gimple_has_side_effects (stmt))
2776 if (gimple_call_fndecl (stmt))
2777 predict_edge_def (e, PRED_CALL, NOT_TAKEN);
2778 else if (virtual_method_call_p (gimple_call_fn (stmt)))
2779 predict_edge_def (e, PRED_POLYMORPHIC_CALL, NOT_TAKEN);
2780 else
2781 predict_edge_def (e, PRED_INDIR_CALL, TAKEN);
2782 break;
2787 tree_predict_by_opcode (bb);
2790 /* Predict branch probabilities and estimate profile of the tree CFG.
2791 This function can be called from the loop optimizers to recompute
2792 the profile information.
2793 If DRY_RUN is set, do not modify CFG and only produce dump files. */
2795 void
2796 tree_estimate_probability (bool dry_run)
2798 basic_block bb;
2800 add_noreturn_fake_exit_edges ();
2801 connect_infinite_loops_to_exit ();
2802 /* We use loop_niter_by_eval, which requires that the loops have
2803 preheaders. */
2804 create_preheaders (CP_SIMPLE_PREHEADERS);
2805 calculate_dominance_info (CDI_POST_DOMINATORS);
2807 bb_predictions = new hash_map<const_basic_block, edge_prediction *>;
2808 tree_bb_level_predictions ();
2809 record_loop_exits ();
2811 if (number_of_loops (cfun) > 1)
2812 predict_loops ();
2814 FOR_EACH_BB_FN (bb, cfun)
2815 tree_estimate_probability_bb (bb, false);
2817 FOR_EACH_BB_FN (bb, cfun)
2818 combine_predictions_for_bb (bb, dry_run);
2820 if (flag_checking)
2821 bb_predictions->traverse<void *, assert_is_empty> (NULL);
2823 delete bb_predictions;
2824 bb_predictions = NULL;
2826 if (!dry_run)
2827 estimate_bb_frequencies (false);
2828 free_dominance_info (CDI_POST_DOMINATORS);
2829 remove_fake_exit_edges ();
2832 /* Set edge->probability for each successor edge of BB. */
2833 void
2834 tree_guess_outgoing_edge_probabilities (basic_block bb)
2836 bb_predictions = new hash_map<const_basic_block, edge_prediction *>;
2837 tree_estimate_probability_bb (bb, true);
2838 combine_predictions_for_bb (bb, false);
2839 if (flag_checking)
2840 bb_predictions->traverse<void *, assert_is_empty> (NULL);
2841 delete bb_predictions;
2842 bb_predictions = NULL;
2845 /* Predict edges to successors of CUR whose sources are not postdominated by
2846 BB by PRED and recurse to all postdominators. */
2848 static void
2849 predict_paths_for_bb (basic_block cur, basic_block bb,
2850 enum br_predictor pred,
2851 enum prediction taken,
2852 bitmap visited, struct loop *in_loop = NULL)
2854 edge e;
2855 edge_iterator ei;
2856 basic_block son;
2858 /* If we exited the loop or CUR is unconditional in the loop, there is
2859 nothing to do. */
2860 if (in_loop
2861 && (!flow_bb_inside_loop_p (in_loop, cur)
2862 || dominated_by_p (CDI_DOMINATORS, in_loop->latch, cur)))
2863 return;
2865 /* We are looking for all edges forming edge cut induced by
2866 set of all blocks postdominated by BB. */
2867 FOR_EACH_EDGE (e, ei, cur->preds)
2868 if (e->src->index >= NUM_FIXED_BLOCKS
2869 && !dominated_by_p (CDI_POST_DOMINATORS, e->src, bb))
2871 edge e2;
2872 edge_iterator ei2;
2873 bool found = false;
2875 /* Ignore fake edges and eh, we predict them as not taken anyway. */
2876 if (unlikely_executed_edge_p (e))
2877 continue;
2878 gcc_assert (bb == cur || dominated_by_p (CDI_POST_DOMINATORS, cur, bb));
2880 /* See if there is an edge from e->src that is not abnormal
2881 and does not lead to BB and does not exit the loop. */
2882 FOR_EACH_EDGE (e2, ei2, e->src->succs)
2883 if (e2 != e
2884 && !unlikely_executed_edge_p (e2)
2885 && !dominated_by_p (CDI_POST_DOMINATORS, e2->dest, bb)
2886 && (!in_loop || !loop_exit_edge_p (in_loop, e2)))
2888 found = true;
2889 break;
2892 /* If there is non-abnormal path leaving e->src, predict edge
2893 using predictor. Otherwise we need to look for paths
2894 leading to e->src.
2896 The second may lead to infinite loop in the case we are predicitng
2897 regions that are only reachable by abnormal edges. We simply
2898 prevent visiting given BB twice. */
2899 if (found)
2901 if (!edge_predicted_by_p (e, pred, taken))
2902 predict_edge_def (e, pred, taken);
2904 else if (bitmap_set_bit (visited, e->src->index))
2905 predict_paths_for_bb (e->src, e->src, pred, taken, visited, in_loop);
2907 for (son = first_dom_son (CDI_POST_DOMINATORS, cur);
2908 son;
2909 son = next_dom_son (CDI_POST_DOMINATORS, son))
2910 predict_paths_for_bb (son, bb, pred, taken, visited, in_loop);
2913 /* Sets branch probabilities according to PREDiction and
2914 FLAGS. */
2916 static void
2917 predict_paths_leading_to (basic_block bb, enum br_predictor pred,
2918 enum prediction taken, struct loop *in_loop)
2920 predict_paths_for_bb (bb, bb, pred, taken, auto_bitmap (), in_loop);
2923 /* Like predict_paths_leading_to but take edge instead of basic block. */
2925 static void
2926 predict_paths_leading_to_edge (edge e, enum br_predictor pred,
2927 enum prediction taken, struct loop *in_loop)
2929 bool has_nonloop_edge = false;
2930 edge_iterator ei;
2931 edge e2;
2933 basic_block bb = e->src;
2934 FOR_EACH_EDGE (e2, ei, bb->succs)
2935 if (e2->dest != e->src && e2->dest != e->dest
2936 && !unlikely_executed_edge_p (e)
2937 && !dominated_by_p (CDI_POST_DOMINATORS, e->src, e2->dest))
2939 has_nonloop_edge = true;
2940 break;
2942 if (!has_nonloop_edge)
2944 predict_paths_for_bb (bb, bb, pred, taken, auto_bitmap (), in_loop);
2946 else
2947 predict_edge_def (e, pred, taken);
2950 /* This is used to carry information about basic blocks. It is
2951 attached to the AUX field of the standard CFG block. */
2953 struct block_info
2955 /* Estimated frequency of execution of basic_block. */
2956 sreal frequency;
2958 /* To keep queue of basic blocks to process. */
2959 basic_block next;
2961 /* Number of predecessors we need to visit first. */
2962 int npredecessors;
2965 /* Similar information for edges. */
2966 struct edge_prob_info
2968 /* In case edge is a loopback edge, the probability edge will be reached
2969 in case header is. Estimated number of iterations of the loop can be
2970 then computed as 1 / (1 - back_edge_prob). */
2971 sreal back_edge_prob;
2972 /* True if the edge is a loopback edge in the natural loop. */
2973 unsigned int back_edge:1;
2976 #define BLOCK_INFO(B) ((block_info *) (B)->aux)
2977 #undef EDGE_INFO
2978 #define EDGE_INFO(E) ((edge_prob_info *) (E)->aux)
2980 /* Helper function for estimate_bb_frequencies.
2981 Propagate the frequencies in blocks marked in
2982 TOVISIT, starting in HEAD. */
2984 static void
2985 propagate_freq (basic_block head, bitmap tovisit)
2987 basic_block bb;
2988 basic_block last;
2989 unsigned i;
2990 edge e;
2991 basic_block nextbb;
2992 bitmap_iterator bi;
2994 /* For each basic block we need to visit count number of his predecessors
2995 we need to visit first. */
2996 EXECUTE_IF_SET_IN_BITMAP (tovisit, 0, i, bi)
2998 edge_iterator ei;
2999 int count = 0;
3001 bb = BASIC_BLOCK_FOR_FN (cfun, i);
3003 FOR_EACH_EDGE (e, ei, bb->preds)
3005 bool visit = bitmap_bit_p (tovisit, e->src->index);
3007 if (visit && !(e->flags & EDGE_DFS_BACK))
3008 count++;
3009 else if (visit && dump_file && !EDGE_INFO (e)->back_edge)
3010 fprintf (dump_file,
3011 "Irreducible region hit, ignoring edge to %i->%i\n",
3012 e->src->index, bb->index);
3014 BLOCK_INFO (bb)->npredecessors = count;
3015 /* When function never returns, we will never process exit block. */
3016 if (!count && bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
3018 bb->count = profile_count::zero ();
3019 bb->frequency = 0;
3023 BLOCK_INFO (head)->frequency = 1;
3024 last = head;
3025 for (bb = head; bb; bb = nextbb)
3027 edge_iterator ei;
3028 sreal cyclic_probability = 0;
3029 sreal frequency = 0;
3031 nextbb = BLOCK_INFO (bb)->next;
3032 BLOCK_INFO (bb)->next = NULL;
3034 /* Compute frequency of basic block. */
3035 if (bb != head)
3037 if (flag_checking)
3038 FOR_EACH_EDGE (e, ei, bb->preds)
3039 gcc_assert (!bitmap_bit_p (tovisit, e->src->index)
3040 || (e->flags & EDGE_DFS_BACK));
3042 FOR_EACH_EDGE (e, ei, bb->preds)
3043 if (EDGE_INFO (e)->back_edge)
3045 cyclic_probability += EDGE_INFO (e)->back_edge_prob;
3047 else if (!(e->flags & EDGE_DFS_BACK))
3049 /* frequency += (e->probability
3050 * BLOCK_INFO (e->src)->frequency /
3051 REG_BR_PROB_BASE); */
3053 sreal tmp = e->probability.to_reg_br_prob_base ();
3054 tmp *= BLOCK_INFO (e->src)->frequency;
3055 tmp *= real_inv_br_prob_base;
3056 frequency += tmp;
3059 if (cyclic_probability == 0)
3061 BLOCK_INFO (bb)->frequency = frequency;
3063 else
3065 if (cyclic_probability > real_almost_one)
3066 cyclic_probability = real_almost_one;
3068 /* BLOCK_INFO (bb)->frequency = frequency
3069 / (1 - cyclic_probability) */
3071 cyclic_probability = sreal (1) - cyclic_probability;
3072 BLOCK_INFO (bb)->frequency = frequency / cyclic_probability;
3076 bitmap_clear_bit (tovisit, bb->index);
3078 e = find_edge (bb, head);
3079 if (e)
3081 /* EDGE_INFO (e)->back_edge_prob
3082 = ((e->probability * BLOCK_INFO (bb)->frequency)
3083 / REG_BR_PROB_BASE); */
3085 sreal tmp = e->probability.to_reg_br_prob_base ();
3086 tmp *= BLOCK_INFO (bb)->frequency;
3087 EDGE_INFO (e)->back_edge_prob = tmp * real_inv_br_prob_base;
3090 /* Propagate to successor blocks. */
3091 FOR_EACH_EDGE (e, ei, bb->succs)
3092 if (!(e->flags & EDGE_DFS_BACK)
3093 && BLOCK_INFO (e->dest)->npredecessors)
3095 BLOCK_INFO (e->dest)->npredecessors--;
3096 if (!BLOCK_INFO (e->dest)->npredecessors)
3098 if (!nextbb)
3099 nextbb = e->dest;
3100 else
3101 BLOCK_INFO (last)->next = e->dest;
3103 last = e->dest;
3109 /* Estimate frequencies in loops at same nest level. */
3111 static void
3112 estimate_loops_at_level (struct loop *first_loop)
3114 struct loop *loop;
3116 for (loop = first_loop; loop; loop = loop->next)
3118 edge e;
3119 basic_block *bbs;
3120 unsigned i;
3121 auto_bitmap tovisit;
3123 estimate_loops_at_level (loop->inner);
3125 /* Find current loop back edge and mark it. */
3126 e = loop_latch_edge (loop);
3127 EDGE_INFO (e)->back_edge = 1;
3129 bbs = get_loop_body (loop);
3130 for (i = 0; i < loop->num_nodes; i++)
3131 bitmap_set_bit (tovisit, bbs[i]->index);
3132 free (bbs);
3133 propagate_freq (loop->header, tovisit);
3137 /* Propagates frequencies through structure of loops. */
3139 static void
3140 estimate_loops (void)
3142 auto_bitmap tovisit;
3143 basic_block bb;
3145 /* Start by estimating the frequencies in the loops. */
3146 if (number_of_loops (cfun) > 1)
3147 estimate_loops_at_level (current_loops->tree_root->inner);
3149 /* Now propagate the frequencies through all the blocks. */
3150 FOR_ALL_BB_FN (bb, cfun)
3152 bitmap_set_bit (tovisit, bb->index);
3154 propagate_freq (ENTRY_BLOCK_PTR_FOR_FN (cfun), tovisit);
3157 /* Drop the profile for NODE to guessed, and update its frequency based on
3158 whether it is expected to be hot given the CALL_COUNT. */
3160 static void
3161 drop_profile (struct cgraph_node *node, profile_count call_count)
3163 struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
3164 /* In the case where this was called by another function with a
3165 dropped profile, call_count will be 0. Since there are no
3166 non-zero call counts to this function, we don't know for sure
3167 whether it is hot, and therefore it will be marked normal below. */
3168 bool hot = maybe_hot_count_p (NULL, call_count);
3170 if (dump_file)
3171 fprintf (dump_file,
3172 "Dropping 0 profile for %s. %s based on calls.\n",
3173 node->dump_name (),
3174 hot ? "Function is hot" : "Function is normal");
3175 /* We only expect to miss profiles for functions that are reached
3176 via non-zero call edges in cases where the function may have
3177 been linked from another module or library (COMDATs and extern
3178 templates). See the comments below for handle_missing_profiles.
3179 Also, only warn in cases where the missing counts exceed the
3180 number of training runs. In certain cases with an execv followed
3181 by a no-return call the profile for the no-return call is not
3182 dumped and there can be a mismatch. */
3183 if (!DECL_COMDAT (node->decl) && !DECL_EXTERNAL (node->decl)
3184 && call_count > profile_info->runs)
3186 if (flag_profile_correction)
3188 if (dump_file)
3189 fprintf (dump_file,
3190 "Missing counts for called function %s\n",
3191 node->dump_name ());
3193 else
3194 warning (0, "Missing counts for called function %s",
3195 node->dump_name ());
3198 basic_block bb;
3199 FOR_ALL_BB_FN (bb, fn)
3201 bb->count = profile_count::uninitialized ();
3203 edge_iterator ei;
3204 edge e;
3205 FOR_EACH_EDGE (e, ei, bb->preds)
3206 e->count = profile_count::uninitialized ();
3209 struct cgraph_edge *e;
3210 for (e = node->callees; e; e = e->next_caller)
3212 e->count = profile_count::uninitialized ();
3213 e->frequency = compute_call_stmt_bb_frequency (e->caller->decl,
3214 gimple_bb (e->call_stmt));
3216 node->count = profile_count::uninitialized ();
3218 profile_status_for_fn (fn)
3219 = (flag_guess_branch_prob ? PROFILE_GUESSED : PROFILE_ABSENT);
3220 node->frequency
3221 = hot ? NODE_FREQUENCY_HOT : NODE_FREQUENCY_NORMAL;
3224 /* In the case of COMDAT routines, multiple object files will contain the same
3225 function and the linker will select one for the binary. In that case
3226 all the other copies from the profile instrument binary will be missing
3227 profile counts. Look for cases where this happened, due to non-zero
3228 call counts going to 0-count functions, and drop the profile to guessed
3229 so that we can use the estimated probabilities and avoid optimizing only
3230 for size.
3232 The other case where the profile may be missing is when the routine
3233 is not going to be emitted to the object file, e.g. for "extern template"
3234 class methods. Those will be marked DECL_EXTERNAL. Emit a warning in
3235 all other cases of non-zero calls to 0-count functions. */
3237 void
3238 handle_missing_profiles (void)
3240 struct cgraph_node *node;
3241 int unlikely_count_fraction = PARAM_VALUE (UNLIKELY_BB_COUNT_FRACTION);
3242 auto_vec<struct cgraph_node *, 64> worklist;
3244 /* See if 0 count function has non-0 count callers. In this case we
3245 lost some profile. Drop its function profile to PROFILE_GUESSED. */
3246 FOR_EACH_DEFINED_FUNCTION (node)
3248 struct cgraph_edge *e;
3249 profile_count call_count = profile_count::zero ();
3250 gcov_type max_tp_first_run = 0;
3251 struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
3253 if (!(node->count == profile_count::zero ()))
3254 continue;
3255 for (e = node->callers; e; e = e->next_caller)
3256 if (e->count.initialized_p () && e->count > 0)
3258 call_count = call_count + e->count;
3260 if (e->caller->tp_first_run > max_tp_first_run)
3261 max_tp_first_run = e->caller->tp_first_run;
3264 /* If time profile is missing, let assign the maximum that comes from
3265 caller functions. */
3266 if (!node->tp_first_run && max_tp_first_run)
3267 node->tp_first_run = max_tp_first_run + 1;
3269 if (call_count > 0
3270 && fn && fn->cfg
3271 && (call_count.apply_scale (unlikely_count_fraction, 1)
3272 >= profile_info->runs))
3274 drop_profile (node, call_count);
3275 worklist.safe_push (node);
3279 /* Propagate the profile dropping to other 0-count COMDATs that are
3280 potentially called by COMDATs we already dropped the profile on. */
3281 while (worklist.length () > 0)
3283 struct cgraph_edge *e;
3285 node = worklist.pop ();
3286 for (e = node->callees; e; e = e->next_caller)
3288 struct cgraph_node *callee = e->callee;
3289 struct function *fn = DECL_STRUCT_FUNCTION (callee->decl);
3291 if (callee->count > 0)
3292 continue;
3293 if ((DECL_COMDAT (callee->decl) || DECL_EXTERNAL (callee->decl))
3294 && fn && fn->cfg
3295 && profile_status_for_fn (fn) == PROFILE_READ)
3297 drop_profile (node, profile_count::zero ());
3298 worklist.safe_push (callee);
3304 /* Convert counts measured by profile driven feedback to frequencies.
3305 Return nonzero iff there was any nonzero execution count. */
3307 bool
3308 counts_to_freqs (void)
3310 gcov_type count_max;
3311 profile_count true_count_max = profile_count::zero ();
3312 basic_block bb;
3314 /* Don't overwrite the estimated frequencies when the profile for
3315 the function is missing. We may drop this function PROFILE_GUESSED
3316 later in drop_profile (). */
3317 if (!ENTRY_BLOCK_PTR_FOR_FN (cfun)->count.initialized_p ()
3318 || ENTRY_BLOCK_PTR_FOR_FN (cfun)->count == profile_count::zero ())
3319 return false;
3321 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
3322 if (bb->count > true_count_max)
3323 true_count_max = bb->count;
3325 /* If we have no counts to base frequencies on, keep those that are
3326 already there. */
3327 if (!(true_count_max > 0))
3328 return false;
3330 count_max = true_count_max.to_gcov_type ();
3332 FOR_ALL_BB_FN (bb, cfun)
3333 if (bb->count.initialized_p ())
3334 bb->frequency = RDIV (bb->count.to_gcov_type () * BB_FREQ_MAX, count_max);
3336 return true;
3339 /* Return true if function is likely to be expensive, so there is no point to
3340 optimize performance of prologue, epilogue or do inlining at the expense
3341 of code size growth. THRESHOLD is the limit of number of instructions
3342 function can execute at average to be still considered not expensive. */
3344 bool
3345 expensive_function_p (int threshold)
3347 unsigned int sum = 0;
3348 basic_block bb;
3349 unsigned int limit;
3351 /* We can not compute accurately for large thresholds due to scaled
3352 frequencies. */
3353 gcc_assert (threshold <= BB_FREQ_MAX);
3355 /* Frequencies are out of range. This either means that function contains
3356 internal loop executing more than BB_FREQ_MAX times or profile feedback
3357 is available and function has not been executed at all. */
3358 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency == 0)
3359 return true;
3361 /* Maximally BB_FREQ_MAX^2 so overflow won't happen. */
3362 limit = ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency * threshold;
3363 FOR_EACH_BB_FN (bb, cfun)
3365 rtx_insn *insn;
3367 FOR_BB_INSNS (bb, insn)
3368 if (active_insn_p (insn))
3370 sum += bb->frequency;
3371 if (sum > limit)
3372 return true;
3376 return false;
3379 /* All basic blocks that are reachable only from unlikely basic blocks are
3380 unlikely. */
3382 void
3383 propagate_unlikely_bbs_forward (void)
3385 auto_vec<basic_block, 64> worklist;
3386 basic_block bb;
3387 edge_iterator ei;
3388 edge e;
3390 if (!(ENTRY_BLOCK_PTR_FOR_FN (cfun)->count == profile_count::zero ()))
3392 ENTRY_BLOCK_PTR_FOR_FN (cfun)->aux = (void *)(size_t) 1;
3393 worklist.safe_push (ENTRY_BLOCK_PTR_FOR_FN (cfun));
3395 while (worklist.length () > 0)
3397 bb = worklist.pop ();
3398 FOR_EACH_EDGE (e, ei, bb->succs)
3399 if (!(e->count == profile_count::zero ())
3400 && !(e->dest->count == profile_count::zero ())
3401 && !e->dest->aux)
3403 e->dest->aux = (void *)(size_t) 1;
3404 worklist.safe_push (e->dest);
3409 FOR_ALL_BB_FN (bb, cfun)
3411 if (!bb->aux)
3413 if (!(bb->count == profile_count::zero ())
3414 && (dump_file && (dump_flags & TDF_DETAILS)))
3415 fprintf (dump_file,
3416 "Basic block %i is marked unlikely by forward prop\n",
3417 bb->index);
3418 bb->count = profile_count::zero ();
3419 bb->frequency = 0;
3420 FOR_EACH_EDGE (e, ei, bb->succs)
3421 e->count = profile_count::zero ();
3423 else
3424 bb->aux = NULL;
3428 /* Determine basic blocks/edges that are known to be unlikely executed and set
3429 their counters to zero.
3430 This is done with first identifying obviously unlikely BBs/edges and then
3431 propagating in both directions. */
3433 static void
3434 determine_unlikely_bbs ()
3436 basic_block bb;
3437 auto_vec<basic_block, 64> worklist;
3438 edge_iterator ei;
3439 edge e;
3441 FOR_EACH_BB_FN (bb, cfun)
3443 if (!(bb->count == profile_count::zero ())
3444 && unlikely_executed_bb_p (bb))
3446 if (dump_file && (dump_flags & TDF_DETAILS))
3447 fprintf (dump_file, "Basic block %i is locally unlikely\n",
3448 bb->index);
3449 bb->count = profile_count::zero ();
3452 if (bb->count == profile_count::zero ())
3454 bb->frequency = 0;
3455 FOR_EACH_EDGE (e, ei, bb->preds)
3456 e->count = profile_count::zero ();
3459 FOR_EACH_EDGE (e, ei, bb->succs)
3460 if (!(e->count == profile_count::zero ())
3461 && unlikely_executed_edge_p (e))
3463 if (dump_file && (dump_flags & TDF_DETAILS))
3464 fprintf (dump_file, "Edge %i->%i is locally unlikely\n",
3465 bb->index, e->dest->index);
3466 e->count = profile_count::zero ();
3469 gcc_checking_assert (!bb->aux);
3472 auto_vec<int, 64> nsuccs;
3473 nsuccs.safe_grow_cleared (last_basic_block_for_fn (cfun));
3474 FOR_ALL_BB_FN (bb, cfun)
3475 if (!(bb->count == profile_count::zero ())
3476 && bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
3478 nsuccs[bb->index] = 0;
3479 FOR_EACH_EDGE (e, ei, bb->succs)
3480 if (!(e->count == profile_count::zero ()))
3481 nsuccs[bb->index]++;
3482 if (!nsuccs[bb->index])
3483 worklist.safe_push (bb);
3485 while (worklist.length () > 0)
3487 bb = worklist.pop ();
3488 if (bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
3490 bool found = false;
3491 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
3492 !gsi_end_p (gsi); gsi_next (&gsi))
3493 if (stmt_can_terminate_bb_p (gsi_stmt (gsi))
3494 /* stmt_can_terminate_bb_p special cases noreturns because it
3495 assumes that fake edges are created. We want to know that
3496 noreturn alone does not imply BB to be unlikely. */
3497 || (is_gimple_call (gsi_stmt (gsi))
3498 && (gimple_call_flags (gsi_stmt (gsi)) & ECF_NORETURN)))
3500 found = true;
3501 break;
3503 if (found)
3504 continue;
3506 if (!(bb->count == profile_count::zero ())
3507 && (dump_file && (dump_flags & TDF_DETAILS)))
3508 fprintf (dump_file,
3509 "Basic block %i is marked unlikely by backward prop\n",
3510 bb->index);
3511 bb->count = profile_count::zero ();
3512 bb->frequency = 0;
3513 FOR_EACH_EDGE (e, ei, bb->preds)
3514 if (!(e->count == profile_count::zero ()))
3516 e->count = profile_count::zero ();
3517 if (!(e->src->count == profile_count::zero ()))
3519 nsuccs[e->src->index]--;
3520 if (!nsuccs[e->src->index])
3521 worklist.safe_push (e->src);
3527 /* Estimate and propagate basic block frequencies using the given branch
3528 probabilities. If FORCE is true, the frequencies are used to estimate
3529 the counts even when there are already non-zero profile counts. */
3531 void
3532 estimate_bb_frequencies (bool force)
3534 basic_block bb;
3535 sreal freq_max;
3537 determine_unlikely_bbs ();
3539 if (force || profile_status_for_fn (cfun) != PROFILE_READ
3540 || !counts_to_freqs ())
3542 static int real_values_initialized = 0;
3544 if (!real_values_initialized)
3546 real_values_initialized = 1;
3547 real_br_prob_base = REG_BR_PROB_BASE;
3548 real_bb_freq_max = BB_FREQ_MAX;
3549 real_one_half = sreal (1, -1);
3550 real_inv_br_prob_base = sreal (1) / real_br_prob_base;
3551 real_almost_one = sreal (1) - real_inv_br_prob_base;
3554 mark_dfs_back_edges ();
3556 single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun))->probability =
3557 profile_probability::always ();
3559 /* Set up block info for each basic block. */
3560 alloc_aux_for_blocks (sizeof (block_info));
3561 alloc_aux_for_edges (sizeof (edge_prob_info));
3562 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
3564 edge e;
3565 edge_iterator ei;
3567 FOR_EACH_EDGE (e, ei, bb->succs)
3569 EDGE_INFO (e)->back_edge_prob
3570 = e->probability.to_reg_br_prob_base ();
3571 EDGE_INFO (e)->back_edge_prob *= real_inv_br_prob_base;
3575 /* First compute frequencies locally for each loop from innermost
3576 to outermost to examine frequencies for back edges. */
3577 estimate_loops ();
3579 freq_max = 0;
3580 FOR_EACH_BB_FN (bb, cfun)
3581 if (freq_max < BLOCK_INFO (bb)->frequency)
3582 freq_max = BLOCK_INFO (bb)->frequency;
3584 freq_max = real_bb_freq_max / freq_max;
3585 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
3587 sreal tmp = BLOCK_INFO (bb)->frequency * freq_max + real_one_half;
3588 bb->frequency = tmp.to_int ();
3591 free_aux_for_blocks ();
3592 free_aux_for_edges ();
3594 compute_function_frequency ();
3597 /* Decide whether function is hot, cold or unlikely executed. */
3598 void
3599 compute_function_frequency (void)
3601 basic_block bb;
3602 struct cgraph_node *node = cgraph_node::get (current_function_decl);
3604 if (DECL_STATIC_CONSTRUCTOR (current_function_decl)
3605 || MAIN_NAME_P (DECL_NAME (current_function_decl)))
3606 node->only_called_at_startup = true;
3607 if (DECL_STATIC_DESTRUCTOR (current_function_decl))
3608 node->only_called_at_exit = true;
3610 if (profile_status_for_fn (cfun) != PROFILE_READ)
3612 int flags = flags_from_decl_or_type (current_function_decl);
3613 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->count == profile_count::zero ()
3614 || lookup_attribute ("cold", DECL_ATTRIBUTES (current_function_decl))
3615 != NULL)
3617 node->frequency = NODE_FREQUENCY_UNLIKELY_EXECUTED;
3618 warn_function_cold (current_function_decl);
3620 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (current_function_decl))
3621 != NULL)
3622 node->frequency = NODE_FREQUENCY_HOT;
3623 else if (flags & ECF_NORETURN)
3624 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
3625 else if (MAIN_NAME_P (DECL_NAME (current_function_decl)))
3626 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
3627 else if (DECL_STATIC_CONSTRUCTOR (current_function_decl)
3628 || DECL_STATIC_DESTRUCTOR (current_function_decl))
3629 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
3630 return;
3633 /* Only first time try to drop function into unlikely executed.
3634 After inlining the roundoff errors may confuse us.
3635 Ipa-profile pass will drop functions only called from unlikely
3636 functions to unlikely and that is most of what we care about. */
3637 if (!cfun->after_inlining)
3639 node->frequency = NODE_FREQUENCY_UNLIKELY_EXECUTED;
3640 warn_function_cold (current_function_decl);
3642 FOR_EACH_BB_FN (bb, cfun)
3644 if (maybe_hot_bb_p (cfun, bb))
3646 node->frequency = NODE_FREQUENCY_HOT;
3647 return;
3649 if (!probably_never_executed_bb_p (cfun, bb))
3650 node->frequency = NODE_FREQUENCY_NORMAL;
3654 /* Build PREDICT_EXPR. */
3655 tree
3656 build_predict_expr (enum br_predictor predictor, enum prediction taken)
3658 tree t = build1 (PREDICT_EXPR, void_type_node,
3659 build_int_cst (integer_type_node, predictor));
3660 SET_PREDICT_EXPR_OUTCOME (t, taken);
3661 return t;
3664 const char *
3665 predictor_name (enum br_predictor predictor)
3667 return predictor_info[predictor].name;
3670 /* Predict branch probabilities and estimate profile of the tree CFG. */
3672 namespace {
3674 const pass_data pass_data_profile =
3676 GIMPLE_PASS, /* type */
3677 "profile_estimate", /* name */
3678 OPTGROUP_NONE, /* optinfo_flags */
3679 TV_BRANCH_PROB, /* tv_id */
3680 PROP_cfg, /* properties_required */
3681 0, /* properties_provided */
3682 0, /* properties_destroyed */
3683 0, /* todo_flags_start */
3684 0, /* todo_flags_finish */
3687 class pass_profile : public gimple_opt_pass
3689 public:
3690 pass_profile (gcc::context *ctxt)
3691 : gimple_opt_pass (pass_data_profile, ctxt)
3694 /* opt_pass methods: */
3695 virtual bool gate (function *) { return flag_guess_branch_prob; }
3696 virtual unsigned int execute (function *);
3698 }; // class pass_profile
3700 unsigned int
3701 pass_profile::execute (function *fun)
3703 unsigned nb_loops;
3705 if (profile_status_for_fn (cfun) == PROFILE_GUESSED)
3706 return 0;
3708 loop_optimizer_init (LOOPS_NORMAL);
3709 if (dump_file && (dump_flags & TDF_DETAILS))
3710 flow_loops_dump (dump_file, NULL, 0);
3712 mark_irreducible_loops ();
3714 nb_loops = number_of_loops (fun);
3715 if (nb_loops > 1)
3716 scev_initialize ();
3718 tree_estimate_probability (false);
3720 if (nb_loops > 1)
3721 scev_finalize ();
3723 loop_optimizer_finalize ();
3724 if (dump_file && (dump_flags & TDF_DETAILS))
3725 gimple_dump_cfg (dump_file, dump_flags);
3726 if (profile_status_for_fn (fun) == PROFILE_ABSENT)
3727 profile_status_for_fn (fun) = PROFILE_GUESSED;
3728 if (dump_file && (dump_flags & TDF_DETAILS))
3730 struct loop *loop;
3731 FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
3732 if (loop->header->frequency)
3733 fprintf (dump_file, "Loop got predicted %d to iterate %i times.\n",
3734 loop->num,
3735 (int)expected_loop_iterations_unbounded (loop));
3737 return 0;
3740 } // anon namespace
3742 gimple_opt_pass *
3743 make_pass_profile (gcc::context *ctxt)
3745 return new pass_profile (ctxt);
3748 namespace {
3750 const pass_data pass_data_strip_predict_hints =
3752 GIMPLE_PASS, /* type */
3753 "*strip_predict_hints", /* name */
3754 OPTGROUP_NONE, /* optinfo_flags */
3755 TV_BRANCH_PROB, /* tv_id */
3756 PROP_cfg, /* properties_required */
3757 0, /* properties_provided */
3758 0, /* properties_destroyed */
3759 0, /* todo_flags_start */
3760 0, /* todo_flags_finish */
3763 class pass_strip_predict_hints : public gimple_opt_pass
3765 public:
3766 pass_strip_predict_hints (gcc::context *ctxt)
3767 : gimple_opt_pass (pass_data_strip_predict_hints, ctxt)
3770 /* opt_pass methods: */
3771 opt_pass * clone () { return new pass_strip_predict_hints (m_ctxt); }
3772 virtual unsigned int execute (function *);
3774 }; // class pass_strip_predict_hints
3776 /* Get rid of all builtin_expect calls and GIMPLE_PREDICT statements
3777 we no longer need. */
3778 unsigned int
3779 pass_strip_predict_hints::execute (function *fun)
3781 basic_block bb;
3782 gimple *ass_stmt;
3783 tree var;
3784 bool changed = false;
3786 FOR_EACH_BB_FN (bb, fun)
3788 gimple_stmt_iterator bi;
3789 for (bi = gsi_start_bb (bb); !gsi_end_p (bi);)
3791 gimple *stmt = gsi_stmt (bi);
3793 if (gimple_code (stmt) == GIMPLE_PREDICT)
3795 gsi_remove (&bi, true);
3796 changed = true;
3797 continue;
3799 else if (is_gimple_call (stmt))
3801 tree fndecl = gimple_call_fndecl (stmt);
3803 if ((fndecl
3804 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
3805 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_EXPECT
3806 && gimple_call_num_args (stmt) == 2)
3807 || (gimple_call_internal_p (stmt)
3808 && gimple_call_internal_fn (stmt) == IFN_BUILTIN_EXPECT))
3810 var = gimple_call_lhs (stmt);
3811 changed = true;
3812 if (var)
3814 ass_stmt
3815 = gimple_build_assign (var, gimple_call_arg (stmt, 0));
3816 gsi_replace (&bi, ass_stmt, true);
3818 else
3820 gsi_remove (&bi, true);
3821 continue;
3825 gsi_next (&bi);
3828 return changed ? TODO_cleanup_cfg : 0;
3831 } // anon namespace
3833 gimple_opt_pass *
3834 make_pass_strip_predict_hints (gcc::context *ctxt)
3836 return new pass_strip_predict_hints (ctxt);
3839 /* Rebuild function frequencies. Passes are in general expected to
3840 maintain profile by hand, however in some cases this is not possible:
3841 for example when inlining several functions with loops freuqencies might run
3842 out of scale and thus needs to be recomputed. */
3844 void
3845 rebuild_frequencies (void)
3847 timevar_push (TV_REBUILD_FREQUENCIES);
3849 /* When the max bb count in the function is small, there is a higher
3850 chance that there were truncation errors in the integer scaling
3851 of counts by inlining and other optimizations. This could lead
3852 to incorrect classification of code as being cold when it isn't.
3853 In that case, force the estimation of bb counts/frequencies from the
3854 branch probabilities, rather than computing frequencies from counts,
3855 which may also lead to frequencies incorrectly reduced to 0. There
3856 is less precision in the probabilities, so we only do this for small
3857 max counts. */
3858 profile_count count_max = profile_count::zero ();
3859 basic_block bb;
3860 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
3861 if (bb->count > count_max)
3862 count_max = bb->count;
3864 if (profile_status_for_fn (cfun) == PROFILE_GUESSED
3865 || (!flag_auto_profile && profile_status_for_fn (cfun) == PROFILE_READ
3866 && count_max < REG_BR_PROB_BASE / 10))
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 profile_count old_count = e->count;
3932 profile_probability old_probability = e->probability;
3933 bool uninitialized_exit = false;
3935 profile_probability goal = (impossible ? profile_probability::never ()
3936 : profile_probability::very_unlikely ());
3938 /* If edge is already improbably or cold, just return. */
3939 if (e->probability <= goal
3940 && (!impossible || e->count == profile_count::zero ()))
3941 return;
3942 FOR_EACH_EDGE (e2, ei, e->src->succs)
3943 if (e2 != e)
3945 if (e2->count.initialized_p ())
3946 count_sum += e2->count;
3947 else
3948 uninitialized_exit = true;
3949 if (e2->probability.initialized_p ())
3950 prob_sum += e2->probability;
3953 /* If there are other edges out of e->src, redistribute probabilitity
3954 there. */
3955 if (prob_sum > profile_probability::never ())
3957 if (!(e->probability < goal))
3958 e->probability = goal;
3959 if (impossible)
3960 e->count = profile_count::zero ();
3961 else if (old_probability > profile_probability::never ())
3962 e->count = e->count.apply_probability (e->probability
3963 / old_probability);
3964 else
3965 e->count = e->count.apply_scale (1, REG_BR_PROB_BASE);
3967 profile_probability prob_comp = prob_sum / e->probability.invert ();
3969 if (dump_file && (dump_flags & TDF_DETAILS))
3970 fprintf (dump_file, "Making edge %i->%i %s by redistributing "
3971 "probability to other edges.\n",
3972 e->src->index, e->dest->index,
3973 impossible ? "impossible" : "cold");
3974 profile_count count_sum2 = count_sum + old_count - e->count;
3975 FOR_EACH_EDGE (e2, ei, e->src->succs)
3976 if (e2 != e)
3978 if (count_sum > 0)
3979 e2->count.apply_scale (count_sum2, count_sum);
3980 e2->probability /= prob_comp;
3982 if (current_ir_type () != IR_GIMPLE
3983 && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
3984 update_br_prob_note (e->src);
3986 /* If all edges out of e->src are unlikely, the basic block itself
3987 is unlikely. */
3988 else
3990 if (prob_sum == profile_probability::never ())
3991 e->probability = profile_probability::always ();
3992 else
3994 if (impossible)
3995 e->probability = profile_probability::never ();
3996 /* If BB has some edges out that are not impossible, we can not
3997 assume that BB itself is. */
3998 impossible = false;
4000 if (current_ir_type () != IR_GIMPLE
4001 && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
4002 update_br_prob_note (e->src);
4003 if (e->src->count == profile_count::zero ())
4004 return;
4005 if (count_sum == profile_count::zero () && !uninitialized_exit
4006 && impossible)
4008 bool found = false;
4009 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
4011 else if (current_ir_type () == IR_GIMPLE)
4012 for (gimple_stmt_iterator gsi = gsi_start_bb (e->src);
4013 !gsi_end_p (gsi); gsi_next (&gsi))
4015 if (stmt_can_terminate_bb_p (gsi_stmt (gsi)))
4017 found = true;
4018 break;
4021 /* FIXME: Implement RTL path. */
4022 else
4023 found = true;
4024 if (!found)
4026 if (dump_file && (dump_flags & TDF_DETAILS))
4027 fprintf (dump_file,
4028 "Making bb %i impossible and dropping count to 0.\n",
4029 e->src->index);
4030 e->count = profile_count::zero ();
4031 e->src->count = profile_count::zero ();
4032 FOR_EACH_EDGE (e2, ei, e->src->preds)
4033 force_edge_cold (e2, impossible);
4034 return;
4038 /* If we did not adjusting, the source basic block has no likely edeges
4039 leaving other direction. In that case force that bb cold, too.
4040 This in general is difficult task to do, but handle special case when
4041 BB has only one predecestor. This is common case when we are updating
4042 after loop transforms. */
4043 if (!(prob_sum > profile_probability::never ())
4044 && count_sum == profile_count::zero ()
4045 && single_pred_p (e->src) && e->src->frequency > (impossible ? 0 : 1))
4047 int old_frequency = e->src->frequency;
4048 if (dump_file && (dump_flags & TDF_DETAILS))
4049 fprintf (dump_file, "Making bb %i %s.\n", e->src->index,
4050 impossible ? "impossible" : "cold");
4051 e->src->frequency = MIN (e->src->frequency, impossible ? 0 : 1);
4052 if (impossible)
4053 e->src->count = e->count = profile_count::zero ();
4054 else
4055 e->src->count = e->count = e->count.apply_scale (e->src->frequency,
4056 old_frequency);
4057 force_edge_cold (single_pred_edge (e->src), impossible);
4059 else if (dump_file && (dump_flags & TDF_DETAILS)
4060 && maybe_hot_bb_p (cfun, e->src))
4061 fprintf (dump_file, "Giving up on making bb %i %s.\n", e->src->index,
4062 impossible ? "impossible" : "cold");
4066 #if CHECKING_P
4068 namespace selftest {
4070 /* Test that value range of predictor values defined in predict.def is
4071 within range (50, 100]. */
4073 struct branch_predictor
4075 const char *name;
4076 unsigned probability;
4079 #define DEF_PREDICTOR(ENUM, NAME, HITRATE, FLAGS) { NAME, HITRATE },
4081 static void
4082 test_prediction_value_range ()
4084 branch_predictor predictors[] = {
4085 #include "predict.def"
4086 {NULL, -1U}
4089 for (unsigned i = 0; predictors[i].name != NULL; i++)
4091 unsigned p = 100 * predictors[i].probability / REG_BR_PROB_BASE;
4092 ASSERT_TRUE (p > 50 && p <= 100);
4096 #undef DEF_PREDICTOR
4098 /* Run all of the selfests within this file. */
4100 void
4101 predict_c_tests ()
4103 test_prediction_value_range ();
4106 } // namespace selftest
4107 #endif /* CHECKING_P. */