aix: Support libsupc++ as a FAT library
[official-gcc.git] / gcc / predict.c
blob4c4bba549398523f86b26684e068c130322db29b
1 /* Branch prediction routines for the GNU compiler.
2 Copyright (C) 2000-2020 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 "cfgloop.h"
52 #include "gimple-iterator.h"
53 #include "tree-cfg.h"
54 #include "tree-ssa-loop-niter.h"
55 #include "tree-ssa-loop.h"
56 #include "tree-scalar-evolution.h"
57 #include "ipa-utils.h"
58 #include "gimple-pretty-print.h"
59 #include "selftest.h"
60 #include "cfgrtl.h"
61 #include "stringpool.h"
62 #include "attribs.h"
64 /* Enum with reasons why a predictor is ignored. */
66 enum predictor_reason
68 REASON_NONE,
69 REASON_IGNORED,
70 REASON_SINGLE_EDGE_DUPLICATE,
71 REASON_EDGE_PAIR_DUPLICATE
74 /* String messages for the aforementioned enum. */
76 static const char *reason_messages[] = {"", " (ignored)",
77 " (single edge duplicate)", " (edge pair duplicate)"};
80 static void combine_predictions_for_insn (rtx_insn *, basic_block);
81 static void dump_prediction (FILE *, enum br_predictor, int, basic_block,
82 enum predictor_reason, edge);
83 static void predict_paths_leading_to (basic_block, enum br_predictor,
84 enum prediction,
85 class loop *in_loop = NULL);
86 static void predict_paths_leading_to_edge (edge, enum br_predictor,
87 enum prediction,
88 class loop *in_loop = NULL);
89 static bool can_predict_insn_p (const rtx_insn *);
90 static HOST_WIDE_INT get_predictor_value (br_predictor, HOST_WIDE_INT);
91 static void determine_unlikely_bbs ();
93 /* Information we hold about each branch predictor.
94 Filled using information from predict.def. */
96 struct predictor_info
98 const char *const name; /* Name used in the debugging dumps. */
99 const int hitrate; /* Expected hitrate used by
100 predict_insn_def call. */
101 const int flags;
104 /* Use given predictor without Dempster-Shaffer theory if it matches
105 using first_match heuristics. */
106 #define PRED_FLAG_FIRST_MATCH 1
108 /* Recompute hitrate in percent to our representation. */
110 #define HITRATE(VAL) ((int) ((VAL) * REG_BR_PROB_BASE + 50) / 100)
112 #define DEF_PREDICTOR(ENUM, NAME, HITRATE, FLAGS) {NAME, HITRATE, FLAGS},
113 static const struct predictor_info predictor_info[]= {
114 #include "predict.def"
116 /* Upper bound on predictors. */
117 {NULL, 0, 0}
119 #undef DEF_PREDICTOR
121 static gcov_type min_count = -1;
123 /* Determine the threshold for hot BB counts. */
125 gcov_type
126 get_hot_bb_threshold ()
128 if (min_count == -1)
130 const int hot_frac = param_hot_bb_count_fraction;
131 const gcov_type min_hot_count
132 = hot_frac
133 ? profile_info->sum_max / hot_frac
134 : (gcov_type)profile_count::max_count;
135 set_hot_bb_threshold (min_hot_count);
136 if (dump_file)
137 fprintf (dump_file, "Setting hotness threshold to %" PRId64 ".\n",
138 min_hot_count);
140 return min_count;
143 /* Set the threshold for hot BB counts. */
145 void
146 set_hot_bb_threshold (gcov_type min)
148 min_count = min;
151 /* Return TRUE if COUNT is considered to be hot in function FUN. */
153 bool
154 maybe_hot_count_p (struct function *fun, profile_count count)
156 if (!count.initialized_p ())
157 return true;
158 if (count.ipa () == profile_count::zero ())
159 return false;
160 if (!count.ipa_p ())
162 struct cgraph_node *node = cgraph_node::get (fun->decl);
163 if (!profile_info || profile_status_for_fn (fun) != PROFILE_READ)
165 if (node->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
166 return false;
167 if (node->frequency == NODE_FREQUENCY_HOT)
168 return true;
170 if (profile_status_for_fn (fun) == PROFILE_ABSENT)
171 return true;
172 if (node->frequency == NODE_FREQUENCY_EXECUTED_ONCE
173 && count < (ENTRY_BLOCK_PTR_FOR_FN (fun)->count.apply_scale (2, 3)))
174 return false;
175 if (count.apply_scale (param_hot_bb_frequency_fraction, 1)
176 < ENTRY_BLOCK_PTR_FOR_FN (fun)->count)
177 return false;
178 return true;
180 /* Code executed at most once is not hot. */
181 if (count <= MAX (profile_info ? profile_info->runs : 1, 1))
182 return false;
183 return (count >= get_hot_bb_threshold ());
186 /* Return true if basic block BB of function FUN can be CPU intensive
187 and should thus be optimized for maximum performance. */
189 bool
190 maybe_hot_bb_p (struct function *fun, const_basic_block bb)
192 gcc_checking_assert (fun);
193 return maybe_hot_count_p (fun, bb->count);
196 /* Return true if edge E can be CPU intensive and should thus be optimized
197 for maximum performance. */
199 bool
200 maybe_hot_edge_p (edge e)
202 return maybe_hot_count_p (cfun, e->count ());
205 /* Return true if COUNT is considered to be never executed in function FUN
206 or if function FUN is considered so in the static profile. */
208 static bool
209 probably_never_executed (struct function *fun, profile_count count)
211 gcc_checking_assert (fun);
212 if (count.ipa () == profile_count::zero ())
213 return true;
214 /* Do not trust adjusted counts. This will make us to drop int cold section
215 code with low execution count as a result of inlining. These low counts
216 are not safe even with read profile and may lead us to dropping
217 code which actually gets executed into cold section of binary that is not
218 desirable. */
219 if (count.precise_p () && profile_status_for_fn (fun) == PROFILE_READ)
221 const int unlikely_frac = param_unlikely_bb_count_fraction;
222 if (count.apply_scale (unlikely_frac, 1) >= profile_info->runs)
223 return false;
224 return true;
226 if ((!profile_info || profile_status_for_fn (fun) != PROFILE_READ)
227 && (cgraph_node::get (fun->decl)->frequency
228 == NODE_FREQUENCY_UNLIKELY_EXECUTED))
229 return true;
230 return false;
233 /* Return true if basic block BB of function FUN is probably never executed. */
235 bool
236 probably_never_executed_bb_p (struct function *fun, const_basic_block bb)
238 return probably_never_executed (fun, bb->count);
241 /* Return true if edge E is unlikely executed for obvious reasons. */
243 static bool
244 unlikely_executed_edge_p (edge e)
246 return (e->count () == profile_count::zero ()
247 || e->probability == profile_probability::never ())
248 || (e->flags & (EDGE_EH | EDGE_FAKE));
251 /* Return true if edge E of function FUN is probably never executed. */
253 bool
254 probably_never_executed_edge_p (struct function *fun, edge e)
256 if (unlikely_executed_edge_p (e))
257 return true;
258 return probably_never_executed (fun, e->count ());
261 /* Return true if function FUN should always be optimized for size. */
263 bool
264 optimize_function_for_size_p (struct function *fun)
266 if (!fun || !fun->decl)
267 return optimize_size;
268 cgraph_node *n = cgraph_node::get (fun->decl);
269 return n && n->optimize_for_size_p ();
272 /* Return true if function FUN should always be optimized for speed. */
274 bool
275 optimize_function_for_speed_p (struct function *fun)
277 return !optimize_function_for_size_p (fun);
280 /* Return the optimization type that should be used for function FUN. */
282 optimization_type
283 function_optimization_type (struct function *fun)
285 return (optimize_function_for_speed_p (fun)
286 ? OPTIMIZE_FOR_SPEED
287 : OPTIMIZE_FOR_SIZE);
290 /* Return TRUE if basic block BB should be optimized for size. */
292 bool
293 optimize_bb_for_size_p (const_basic_block bb)
295 return (optimize_function_for_size_p (cfun)
296 || (bb && !maybe_hot_bb_p (cfun, bb)));
299 /* Return TRUE if basic block BB should be optimized for speed. */
301 bool
302 optimize_bb_for_speed_p (const_basic_block bb)
304 return !optimize_bb_for_size_p (bb);
307 /* Return the optimization type that should be used for basic block BB. */
309 optimization_type
310 bb_optimization_type (const_basic_block bb)
312 return (optimize_bb_for_speed_p (bb)
313 ? OPTIMIZE_FOR_SPEED
314 : OPTIMIZE_FOR_SIZE);
317 /* Return TRUE if edge E should be optimized for size. */
319 bool
320 optimize_edge_for_size_p (edge e)
322 return optimize_function_for_size_p (cfun) || !maybe_hot_edge_p (e);
325 /* Return TRUE if edge E should be optimized for speed. */
327 bool
328 optimize_edge_for_speed_p (edge e)
330 return !optimize_edge_for_size_p (e);
333 /* Return TRUE if the current function is optimized for size. */
335 bool
336 optimize_insn_for_size_p (void)
338 return optimize_function_for_size_p (cfun) || !crtl->maybe_hot_insn_p;
341 /* Return TRUE if the current function is optimized for speed. */
343 bool
344 optimize_insn_for_speed_p (void)
346 return !optimize_insn_for_size_p ();
349 /* Return TRUE if LOOP should be optimized for size. */
351 bool
352 optimize_loop_for_size_p (class loop *loop)
354 return optimize_bb_for_size_p (loop->header);
357 /* Return TRUE if LOOP should be optimized for speed. */
359 bool
360 optimize_loop_for_speed_p (class loop *loop)
362 return optimize_bb_for_speed_p (loop->header);
365 /* Return TRUE if nest rooted at LOOP should be optimized for speed. */
367 bool
368 optimize_loop_nest_for_speed_p (class loop *loop)
370 class loop *l = loop;
371 if (optimize_loop_for_speed_p (loop))
372 return true;
373 l = loop->inner;
374 while (l && l != loop)
376 if (optimize_loop_for_speed_p (l))
377 return true;
378 if (l->inner)
379 l = l->inner;
380 else if (l->next)
381 l = l->next;
382 else
384 while (l != loop && !l->next)
385 l = loop_outer (l);
386 if (l != loop)
387 l = l->next;
390 return false;
393 /* Return TRUE if nest rooted at LOOP should be optimized for size. */
395 bool
396 optimize_loop_nest_for_size_p (class loop *loop)
398 return !optimize_loop_nest_for_speed_p (loop);
401 /* Return true if edge E is likely to be well predictable by branch
402 predictor. */
404 bool
405 predictable_edge_p (edge e)
407 if (!e->probability.initialized_p ())
408 return false;
409 if ((e->probability.to_reg_br_prob_base ()
410 <= param_predictable_branch_outcome * REG_BR_PROB_BASE / 100)
411 || (REG_BR_PROB_BASE - e->probability.to_reg_br_prob_base ()
412 <= param_predictable_branch_outcome * REG_BR_PROB_BASE / 100))
413 return true;
414 return false;
418 /* Set RTL expansion for BB profile. */
420 void
421 rtl_profile_for_bb (basic_block bb)
423 crtl->maybe_hot_insn_p = maybe_hot_bb_p (cfun, bb);
426 /* Set RTL expansion for edge profile. */
428 void
429 rtl_profile_for_edge (edge e)
431 crtl->maybe_hot_insn_p = maybe_hot_edge_p (e);
434 /* Set RTL expansion to default mode (i.e. when profile info is not known). */
435 void
436 default_rtl_profile (void)
438 crtl->maybe_hot_insn_p = true;
441 /* Return true if the one of outgoing edges is already predicted by
442 PREDICTOR. */
444 bool
445 rtl_predicted_by_p (const_basic_block bb, enum br_predictor predictor)
447 rtx note;
448 if (!INSN_P (BB_END (bb)))
449 return false;
450 for (note = REG_NOTES (BB_END (bb)); note; note = XEXP (note, 1))
451 if (REG_NOTE_KIND (note) == REG_BR_PRED
452 && INTVAL (XEXP (XEXP (note, 0), 0)) == (int)predictor)
453 return true;
454 return false;
457 /* Structure representing predictions in tree level. */
459 struct edge_prediction {
460 struct edge_prediction *ep_next;
461 edge ep_edge;
462 enum br_predictor ep_predictor;
463 int ep_probability;
466 /* This map contains for a basic block the list of predictions for the
467 outgoing edges. */
469 static hash_map<const_basic_block, edge_prediction *> *bb_predictions;
471 /* Return true if the one of outgoing edges is already predicted by
472 PREDICTOR. */
474 bool
475 gimple_predicted_by_p (const_basic_block bb, enum br_predictor predictor)
477 struct edge_prediction *i;
478 edge_prediction **preds = bb_predictions->get (bb);
480 if (!preds)
481 return false;
483 for (i = *preds; i; i = i->ep_next)
484 if (i->ep_predictor == predictor)
485 return true;
486 return false;
489 /* Return true if the one of outgoing edges is already predicted by
490 PREDICTOR for edge E predicted as TAKEN. */
492 bool
493 edge_predicted_by_p (edge e, enum br_predictor predictor, bool taken)
495 struct edge_prediction *i;
496 basic_block bb = e->src;
497 edge_prediction **preds = bb_predictions->get (bb);
498 if (!preds)
499 return false;
501 int probability = predictor_info[(int) predictor].hitrate;
503 if (taken != TAKEN)
504 probability = REG_BR_PROB_BASE - probability;
506 for (i = *preds; i; i = i->ep_next)
507 if (i->ep_predictor == predictor
508 && i->ep_edge == e
509 && i->ep_probability == probability)
510 return true;
511 return false;
514 /* Same predicate as above, working on edges. */
515 bool
516 edge_probability_reliable_p (const_edge e)
518 return e->probability.probably_reliable_p ();
521 /* Same predicate as edge_probability_reliable_p, working on notes. */
522 bool
523 br_prob_note_reliable_p (const_rtx note)
525 gcc_assert (REG_NOTE_KIND (note) == REG_BR_PROB);
526 return profile_probability::from_reg_br_prob_note
527 (XINT (note, 0)).probably_reliable_p ();
530 static void
531 predict_insn (rtx_insn *insn, enum br_predictor predictor, int probability)
533 gcc_assert (any_condjump_p (insn));
534 if (!flag_guess_branch_prob)
535 return;
537 add_reg_note (insn, REG_BR_PRED,
538 gen_rtx_CONCAT (VOIDmode,
539 GEN_INT ((int) predictor),
540 GEN_INT ((int) probability)));
543 /* Predict insn by given predictor. */
545 void
546 predict_insn_def (rtx_insn *insn, enum br_predictor predictor,
547 enum prediction taken)
549 int probability = predictor_info[(int) predictor].hitrate;
550 gcc_assert (probability != PROB_UNINITIALIZED);
552 if (taken != TAKEN)
553 probability = REG_BR_PROB_BASE - probability;
555 predict_insn (insn, predictor, probability);
558 /* Predict edge E with given probability if possible. */
560 void
561 rtl_predict_edge (edge e, enum br_predictor predictor, int probability)
563 rtx_insn *last_insn;
564 last_insn = BB_END (e->src);
566 /* We can store the branch prediction information only about
567 conditional jumps. */
568 if (!any_condjump_p (last_insn))
569 return;
571 /* We always store probability of branching. */
572 if (e->flags & EDGE_FALLTHRU)
573 probability = REG_BR_PROB_BASE - probability;
575 predict_insn (last_insn, predictor, probability);
578 /* Predict edge E with the given PROBABILITY. */
579 void
580 gimple_predict_edge (edge e, enum br_predictor predictor, int probability)
582 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
583 && EDGE_COUNT (e->src->succs) > 1
584 && flag_guess_branch_prob
585 && optimize)
587 struct edge_prediction *i = XNEW (struct edge_prediction);
588 edge_prediction *&preds = bb_predictions->get_or_insert (e->src);
590 i->ep_next = preds;
591 preds = i;
592 i->ep_probability = probability;
593 i->ep_predictor = predictor;
594 i->ep_edge = e;
598 /* Filter edge predictions PREDS by a function FILTER: if FILTER return false
599 the prediction is removed.
600 DATA are passed to the filter function. */
602 static void
603 filter_predictions (edge_prediction **preds,
604 bool (*filter) (edge_prediction *, void *), void *data)
606 if (!bb_predictions)
607 return;
609 if (preds)
611 struct edge_prediction **prediction = preds;
612 struct edge_prediction *next;
614 while (*prediction)
616 if ((*filter) (*prediction, data))
617 prediction = &((*prediction)->ep_next);
618 else
620 next = (*prediction)->ep_next;
621 free (*prediction);
622 *prediction = next;
628 /* Filter function predicate that returns true for a edge predicate P
629 if its edge is equal to DATA. */
631 static bool
632 not_equal_edge_p (edge_prediction *p, void *data)
634 return p->ep_edge != (edge)data;
637 /* Remove all predictions on given basic block that are attached
638 to edge E. */
639 void
640 remove_predictions_associated_with_edge (edge e)
642 if (!bb_predictions)
643 return;
645 edge_prediction **preds = bb_predictions->get (e->src);
646 filter_predictions (preds, not_equal_edge_p, e);
649 /* Clears the list of predictions stored for BB. */
651 static void
652 clear_bb_predictions (basic_block bb)
654 edge_prediction **preds = bb_predictions->get (bb);
655 struct edge_prediction *pred, *next;
657 if (!preds)
658 return;
660 for (pred = *preds; pred; pred = next)
662 next = pred->ep_next;
663 free (pred);
665 *preds = NULL;
668 /* Return true when we can store prediction on insn INSN.
669 At the moment we represent predictions only on conditional
670 jumps, not at computed jump or other complicated cases. */
671 static bool
672 can_predict_insn_p (const rtx_insn *insn)
674 return (JUMP_P (insn)
675 && any_condjump_p (insn)
676 && EDGE_COUNT (BLOCK_FOR_INSN (insn)->succs) >= 2);
679 /* Predict edge E by given predictor if possible. */
681 void
682 predict_edge_def (edge e, enum br_predictor predictor,
683 enum prediction taken)
685 int probability = predictor_info[(int) predictor].hitrate;
687 if (taken != TAKEN)
688 probability = REG_BR_PROB_BASE - probability;
690 predict_edge (e, predictor, probability);
693 /* Invert all branch predictions or probability notes in the INSN. This needs
694 to be done each time we invert the condition used by the jump. */
696 void
697 invert_br_probabilities (rtx insn)
699 rtx note;
701 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
702 if (REG_NOTE_KIND (note) == REG_BR_PROB)
703 XINT (note, 0) = profile_probability::from_reg_br_prob_note
704 (XINT (note, 0)).invert ().to_reg_br_prob_note ();
705 else if (REG_NOTE_KIND (note) == REG_BR_PRED)
706 XEXP (XEXP (note, 0), 1)
707 = GEN_INT (REG_BR_PROB_BASE - INTVAL (XEXP (XEXP (note, 0), 1)));
710 /* Dump information about the branch prediction to the output file. */
712 static void
713 dump_prediction (FILE *file, enum br_predictor predictor, int probability,
714 basic_block bb, enum predictor_reason reason = REASON_NONE,
715 edge ep_edge = NULL)
717 edge e = ep_edge;
718 edge_iterator ei;
720 if (!file)
721 return;
723 if (e == NULL)
724 FOR_EACH_EDGE (e, ei, bb->succs)
725 if (! (e->flags & EDGE_FALLTHRU))
726 break;
728 char edge_info_str[128];
729 if (ep_edge)
730 sprintf (edge_info_str, " of edge %d->%d", ep_edge->src->index,
731 ep_edge->dest->index);
732 else
733 edge_info_str[0] = '\0';
735 fprintf (file, " %s heuristics%s%s: %.2f%%",
736 predictor_info[predictor].name,
737 edge_info_str, reason_messages[reason],
738 probability * 100.0 / REG_BR_PROB_BASE);
740 if (bb->count.initialized_p ())
742 fprintf (file, " exec ");
743 bb->count.dump (file);
744 if (e)
746 fprintf (file, " hit ");
747 e->count ().dump (file);
748 fprintf (file, " (%.1f%%)", e->count ().to_gcov_type() * 100.0
749 / bb->count.to_gcov_type ());
753 fprintf (file, "\n");
755 /* Print output that be easily read by analyze_brprob.py script. We are
756 interested only in counts that are read from GCDA files. */
757 if (dump_file && (dump_flags & TDF_DETAILS)
758 && bb->count.precise_p ()
759 && reason == REASON_NONE)
761 fprintf (file, ";;heuristics;%s;%" PRId64 ";%" PRId64 ";%.1f;\n",
762 predictor_info[predictor].name,
763 bb->count.to_gcov_type (), e->count ().to_gcov_type (),
764 probability * 100.0 / REG_BR_PROB_BASE);
768 /* Return true if STMT is known to be unlikely executed. */
770 static bool
771 unlikely_executed_stmt_p (gimple *stmt)
773 if (!is_gimple_call (stmt))
774 return false;
775 /* NORETURN attribute alone is not strong enough: exit() may be quite
776 likely executed once during program run. */
777 if (gimple_call_fntype (stmt)
778 && lookup_attribute ("cold",
779 TYPE_ATTRIBUTES (gimple_call_fntype (stmt)))
780 && !lookup_attribute ("cold", DECL_ATTRIBUTES (current_function_decl)))
781 return true;
782 tree decl = gimple_call_fndecl (stmt);
783 if (!decl)
784 return false;
785 if (lookup_attribute ("cold", DECL_ATTRIBUTES (decl))
786 && !lookup_attribute ("cold", DECL_ATTRIBUTES (current_function_decl)))
787 return true;
789 cgraph_node *n = cgraph_node::get (decl);
790 if (!n)
791 return false;
793 availability avail;
794 n = n->ultimate_alias_target (&avail);
795 if (avail < AVAIL_AVAILABLE)
796 return false;
797 if (!n->analyzed
798 || n->decl == current_function_decl)
799 return false;
800 return n->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED;
803 /* Return true if BB is unlikely executed. */
805 static bool
806 unlikely_executed_bb_p (basic_block bb)
808 if (bb->count == profile_count::zero ())
809 return true;
810 if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun) || bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
811 return false;
812 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
813 !gsi_end_p (gsi); gsi_next (&gsi))
815 if (unlikely_executed_stmt_p (gsi_stmt (gsi)))
816 return true;
817 if (stmt_can_terminate_bb_p (gsi_stmt (gsi)))
818 return false;
820 return false;
823 /* We cannot predict the probabilities of outgoing edges of bb. Set them
824 evenly and hope for the best. If UNLIKELY_EDGES is not null, distribute
825 even probability for all edges not mentioned in the set. These edges
826 are given PROB_VERY_UNLIKELY probability. Similarly for LIKELY_EDGES,
827 if we have exactly one likely edge, make the other edges predicted
828 as not probable. */
830 static void
831 set_even_probabilities (basic_block bb,
832 hash_set<edge> *unlikely_edges = NULL,
833 hash_set<edge_prediction *> *likely_edges = NULL)
835 unsigned nedges = 0, unlikely_count = 0;
836 edge e = NULL;
837 edge_iterator ei;
838 profile_probability all = profile_probability::always ();
840 FOR_EACH_EDGE (e, ei, bb->succs)
841 if (e->probability.initialized_p ())
842 all -= e->probability;
843 else if (!unlikely_executed_edge_p (e))
845 nedges++;
846 if (unlikely_edges != NULL && unlikely_edges->contains (e))
848 all -= profile_probability::very_unlikely ();
849 unlikely_count++;
853 /* Make the distribution even if all edges are unlikely. */
854 unsigned likely_count = likely_edges ? likely_edges->elements () : 0;
855 if (unlikely_count == nedges)
857 unlikely_edges = NULL;
858 unlikely_count = 0;
861 /* If we have one likely edge, then use its probability and distribute
862 remaining probabilities as even. */
863 if (likely_count == 1)
865 FOR_EACH_EDGE (e, ei, bb->succs)
866 if (e->probability.initialized_p ())
868 else if (!unlikely_executed_edge_p (e))
870 edge_prediction *prediction = *likely_edges->begin ();
871 int p = prediction->ep_probability;
872 profile_probability prob
873 = profile_probability::from_reg_br_prob_base (p);
875 if (prediction->ep_edge == e)
876 e->probability = prob;
877 else if (unlikely_edges != NULL && unlikely_edges->contains (e))
878 e->probability = profile_probability::very_unlikely ();
879 else
881 profile_probability remainder = prob.invert ();
882 remainder -= profile_probability::very_unlikely ()
883 .apply_scale (unlikely_count, 1);
884 int count = nedges - unlikely_count - 1;
885 gcc_assert (count >= 0);
887 e->probability = remainder.apply_scale (1, count);
890 else
891 e->probability = profile_probability::never ();
893 else
895 /* Make all unlikely edges unlikely and the rest will have even
896 probability. */
897 unsigned scale = nedges - unlikely_count;
898 FOR_EACH_EDGE (e, ei, bb->succs)
899 if (e->probability.initialized_p ())
901 else if (!unlikely_executed_edge_p (e))
903 if (unlikely_edges != NULL && unlikely_edges->contains (e))
904 e->probability = profile_probability::very_unlikely ();
905 else
906 e->probability = all.apply_scale (1, scale);
908 else
909 e->probability = profile_probability::never ();
913 /* Add REG_BR_PROB note to JUMP with PROB. */
915 void
916 add_reg_br_prob_note (rtx_insn *jump, profile_probability prob)
918 gcc_checking_assert (JUMP_P (jump) && !find_reg_note (jump, REG_BR_PROB, 0));
919 add_int_reg_note (jump, REG_BR_PROB, prob.to_reg_br_prob_note ());
922 /* Combine all REG_BR_PRED notes into single probability and attach REG_BR_PROB
923 note if not already present. Remove now useless REG_BR_PRED notes. */
925 static void
926 combine_predictions_for_insn (rtx_insn *insn, basic_block bb)
928 rtx prob_note;
929 rtx *pnote;
930 rtx note;
931 int best_probability = PROB_EVEN;
932 enum br_predictor best_predictor = END_PREDICTORS;
933 int combined_probability = REG_BR_PROB_BASE / 2;
934 int d;
935 bool first_match = false;
936 bool found = false;
938 if (!can_predict_insn_p (insn))
940 set_even_probabilities (bb);
941 return;
944 prob_note = find_reg_note (insn, REG_BR_PROB, 0);
945 pnote = &REG_NOTES (insn);
946 if (dump_file)
947 fprintf (dump_file, "Predictions for insn %i bb %i\n", INSN_UID (insn),
948 bb->index);
950 /* We implement "first match" heuristics and use probability guessed
951 by predictor with smallest index. */
952 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
953 if (REG_NOTE_KIND (note) == REG_BR_PRED)
955 enum br_predictor predictor = ((enum br_predictor)
956 INTVAL (XEXP (XEXP (note, 0), 0)));
957 int probability = INTVAL (XEXP (XEXP (note, 0), 1));
959 found = true;
960 if (best_predictor > predictor
961 && predictor_info[predictor].flags & PRED_FLAG_FIRST_MATCH)
962 best_probability = probability, best_predictor = predictor;
964 d = (combined_probability * probability
965 + (REG_BR_PROB_BASE - combined_probability)
966 * (REG_BR_PROB_BASE - probability));
968 /* Use FP math to avoid overflows of 32bit integers. */
969 if (d == 0)
970 /* If one probability is 0% and one 100%, avoid division by zero. */
971 combined_probability = REG_BR_PROB_BASE / 2;
972 else
973 combined_probability = (((double) combined_probability) * probability
974 * REG_BR_PROB_BASE / d + 0.5);
977 /* Decide which heuristic to use. In case we didn't match anything,
978 use no_prediction heuristic, in case we did match, use either
979 first match or Dempster-Shaffer theory depending on the flags. */
981 if (best_predictor != END_PREDICTORS)
982 first_match = true;
984 if (!found)
985 dump_prediction (dump_file, PRED_NO_PREDICTION,
986 combined_probability, bb);
987 else
989 if (!first_match)
990 dump_prediction (dump_file, PRED_DS_THEORY, combined_probability,
991 bb, !first_match ? REASON_NONE : REASON_IGNORED);
992 else
993 dump_prediction (dump_file, PRED_FIRST_MATCH, best_probability,
994 bb, first_match ? REASON_NONE : REASON_IGNORED);
997 if (first_match)
998 combined_probability = best_probability;
999 dump_prediction (dump_file, PRED_COMBINED, combined_probability, bb);
1001 while (*pnote)
1003 if (REG_NOTE_KIND (*pnote) == REG_BR_PRED)
1005 enum br_predictor predictor = ((enum br_predictor)
1006 INTVAL (XEXP (XEXP (*pnote, 0), 0)));
1007 int probability = INTVAL (XEXP (XEXP (*pnote, 0), 1));
1009 dump_prediction (dump_file, predictor, probability, bb,
1010 (!first_match || best_predictor == predictor)
1011 ? REASON_NONE : REASON_IGNORED);
1012 *pnote = XEXP (*pnote, 1);
1014 else
1015 pnote = &XEXP (*pnote, 1);
1018 if (!prob_note)
1020 profile_probability p
1021 = profile_probability::from_reg_br_prob_base (combined_probability);
1022 add_reg_br_prob_note (insn, p);
1024 /* Save the prediction into CFG in case we are seeing non-degenerated
1025 conditional jump. */
1026 if (!single_succ_p (bb))
1028 BRANCH_EDGE (bb)->probability = p;
1029 FALLTHRU_EDGE (bb)->probability
1030 = BRANCH_EDGE (bb)->probability.invert ();
1033 else if (!single_succ_p (bb))
1035 profile_probability prob = profile_probability::from_reg_br_prob_note
1036 (XINT (prob_note, 0));
1038 BRANCH_EDGE (bb)->probability = prob;
1039 FALLTHRU_EDGE (bb)->probability = prob.invert ();
1041 else
1042 single_succ_edge (bb)->probability = profile_probability::always ();
1045 /* Edge prediction hash traits. */
1047 struct predictor_hash: pointer_hash <edge_prediction>
1050 static inline hashval_t hash (const edge_prediction *);
1051 static inline bool equal (const edge_prediction *, const edge_prediction *);
1054 /* Calculate hash value of an edge prediction P based on predictor and
1055 normalized probability. */
1057 inline hashval_t
1058 predictor_hash::hash (const edge_prediction *p)
1060 inchash::hash hstate;
1061 hstate.add_int (p->ep_predictor);
1063 int prob = p->ep_probability;
1064 if (prob > REG_BR_PROB_BASE / 2)
1065 prob = REG_BR_PROB_BASE - prob;
1067 hstate.add_int (prob);
1069 return hstate.end ();
1072 /* Return true whether edge predictions P1 and P2 use the same predictor and
1073 have equal (or opposed probability). */
1075 inline bool
1076 predictor_hash::equal (const edge_prediction *p1, const edge_prediction *p2)
1078 return (p1->ep_predictor == p2->ep_predictor
1079 && (p1->ep_probability == p2->ep_probability
1080 || p1->ep_probability == REG_BR_PROB_BASE - p2->ep_probability));
1083 struct predictor_hash_traits: predictor_hash,
1084 typed_noop_remove <edge_prediction *> {};
1086 /* Return true if edge prediction P is not in DATA hash set. */
1088 static bool
1089 not_removed_prediction_p (edge_prediction *p, void *data)
1091 hash_set<edge_prediction *> *remove = (hash_set<edge_prediction *> *) data;
1092 return !remove->contains (p);
1095 /* Prune predictions for a basic block BB. Currently we do following
1096 clean-up steps:
1098 1) remove duplicate prediction that is guessed with the same probability
1099 (different than 1/2) to both edge
1100 2) remove duplicates for a prediction that belongs with the same probability
1101 to a single edge
1105 static void
1106 prune_predictions_for_bb (basic_block bb)
1108 edge_prediction **preds = bb_predictions->get (bb);
1110 if (preds)
1112 hash_table <predictor_hash_traits> s (13);
1113 hash_set <edge_prediction *> remove;
1115 /* Step 1: identify predictors that should be removed. */
1116 for (edge_prediction *pred = *preds; pred; pred = pred->ep_next)
1118 edge_prediction *existing = s.find (pred);
1119 if (existing)
1121 if (pred->ep_edge == existing->ep_edge
1122 && pred->ep_probability == existing->ep_probability)
1124 /* Remove a duplicate predictor. */
1125 dump_prediction (dump_file, pred->ep_predictor,
1126 pred->ep_probability, bb,
1127 REASON_SINGLE_EDGE_DUPLICATE, pred->ep_edge);
1129 remove.add (pred);
1131 else if (pred->ep_edge != existing->ep_edge
1132 && pred->ep_probability == existing->ep_probability
1133 && pred->ep_probability != REG_BR_PROB_BASE / 2)
1135 /* Remove both predictors as they predict the same
1136 for both edges. */
1137 dump_prediction (dump_file, existing->ep_predictor,
1138 pred->ep_probability, bb,
1139 REASON_EDGE_PAIR_DUPLICATE,
1140 existing->ep_edge);
1141 dump_prediction (dump_file, pred->ep_predictor,
1142 pred->ep_probability, bb,
1143 REASON_EDGE_PAIR_DUPLICATE,
1144 pred->ep_edge);
1146 remove.add (existing);
1147 remove.add (pred);
1151 edge_prediction **slot2 = s.find_slot (pred, INSERT);
1152 *slot2 = pred;
1155 /* Step 2: Remove predictors. */
1156 filter_predictions (preds, not_removed_prediction_p, &remove);
1160 /* Combine predictions into single probability and store them into CFG.
1161 Remove now useless prediction entries.
1162 If DRY_RUN is set, only produce dumps and do not modify profile. */
1164 static void
1165 combine_predictions_for_bb (basic_block bb, bool dry_run)
1167 int best_probability = PROB_EVEN;
1168 enum br_predictor best_predictor = END_PREDICTORS;
1169 int combined_probability = REG_BR_PROB_BASE / 2;
1170 int d;
1171 bool first_match = false;
1172 bool found = false;
1173 struct edge_prediction *pred;
1174 int nedges = 0;
1175 edge e, first = NULL, second = NULL;
1176 edge_iterator ei;
1177 int nzero = 0;
1178 int nunknown = 0;
1180 FOR_EACH_EDGE (e, ei, bb->succs)
1182 if (!unlikely_executed_edge_p (e))
1184 nedges ++;
1185 if (first && !second)
1186 second = e;
1187 if (!first)
1188 first = e;
1190 else if (!e->probability.initialized_p ())
1191 e->probability = profile_probability::never ();
1192 if (!e->probability.initialized_p ())
1193 nunknown++;
1194 else if (e->probability == profile_probability::never ())
1195 nzero++;
1198 /* When there is no successor or only one choice, prediction is easy.
1200 When we have a basic block with more than 2 successors, the situation
1201 is more complicated as DS theory cannot be used literally.
1202 More precisely, let's assume we predicted edge e1 with probability p1,
1203 thus: m1({b1}) = p1. As we're going to combine more than 2 edges, we
1204 need to find probability of e.g. m1({b2}), which we don't know.
1205 The only approximation is to equally distribute 1-p1 to all edges
1206 different from b1.
1208 According to numbers we've got from SPEC2006 benchark, there's only
1209 one interesting reliable predictor (noreturn call), which can be
1210 handled with a bit easier approach. */
1211 if (nedges != 2)
1213 hash_set<edge> unlikely_edges (4);
1214 hash_set<edge_prediction *> likely_edges (4);
1216 /* Identify all edges that have a probability close to very unlikely.
1217 Doing the approach for very unlikely doesn't worth for doing as
1218 there's no such probability in SPEC2006 benchmark. */
1219 edge_prediction **preds = bb_predictions->get (bb);
1220 if (preds)
1221 for (pred = *preds; pred; pred = pred->ep_next)
1223 if (pred->ep_probability <= PROB_VERY_UNLIKELY
1224 || pred->ep_predictor == PRED_COLD_LABEL)
1225 unlikely_edges.add (pred->ep_edge);
1226 else if (pred->ep_probability >= PROB_VERY_LIKELY
1227 || pred->ep_predictor == PRED_BUILTIN_EXPECT
1228 || pred->ep_predictor == PRED_HOT_LABEL)
1229 likely_edges.add (pred);
1232 /* It can happen that an edge is both in likely_edges and unlikely_edges.
1233 Clear both sets in that situation. */
1234 for (hash_set<edge_prediction *>::iterator it = likely_edges.begin ();
1235 it != likely_edges.end (); ++it)
1236 if (unlikely_edges.contains ((*it)->ep_edge))
1238 likely_edges.empty ();
1239 unlikely_edges.empty ();
1240 break;
1243 if (!dry_run)
1244 set_even_probabilities (bb, &unlikely_edges, &likely_edges);
1245 clear_bb_predictions (bb);
1246 if (dump_file)
1248 fprintf (dump_file, "Predictions for bb %i\n", bb->index);
1249 if (unlikely_edges.is_empty ())
1250 fprintf (dump_file,
1251 "%i edges in bb %i predicted to even probabilities\n",
1252 nedges, bb->index);
1253 else
1255 fprintf (dump_file,
1256 "%i edges in bb %i predicted with some unlikely edges\n",
1257 nedges, bb->index);
1258 FOR_EACH_EDGE (e, ei, bb->succs)
1259 if (!unlikely_executed_edge_p (e))
1260 dump_prediction (dump_file, PRED_COMBINED,
1261 e->probability.to_reg_br_prob_base (), bb, REASON_NONE, e);
1264 return;
1267 if (dump_file)
1268 fprintf (dump_file, "Predictions for bb %i\n", bb->index);
1270 prune_predictions_for_bb (bb);
1272 edge_prediction **preds = bb_predictions->get (bb);
1274 if (preds)
1276 /* We implement "first match" heuristics and use probability guessed
1277 by predictor with smallest index. */
1278 for (pred = *preds; pred; pred = pred->ep_next)
1280 enum br_predictor predictor = pred->ep_predictor;
1281 int probability = pred->ep_probability;
1283 if (pred->ep_edge != first)
1284 probability = REG_BR_PROB_BASE - probability;
1286 found = true;
1287 /* First match heuristics would be widly confused if we predicted
1288 both directions. */
1289 if (best_predictor > predictor
1290 && predictor_info[predictor].flags & PRED_FLAG_FIRST_MATCH)
1292 struct edge_prediction *pred2;
1293 int prob = probability;
1295 for (pred2 = (struct edge_prediction *) *preds;
1296 pred2; pred2 = pred2->ep_next)
1297 if (pred2 != pred && pred2->ep_predictor == pred->ep_predictor)
1299 int probability2 = pred2->ep_probability;
1301 if (pred2->ep_edge != first)
1302 probability2 = REG_BR_PROB_BASE - probability2;
1304 if ((probability < REG_BR_PROB_BASE / 2) !=
1305 (probability2 < REG_BR_PROB_BASE / 2))
1306 break;
1308 /* If the same predictor later gave better result, go for it! */
1309 if ((probability >= REG_BR_PROB_BASE / 2 && (probability2 > probability))
1310 || (probability <= REG_BR_PROB_BASE / 2 && (probability2 < probability)))
1311 prob = probability2;
1313 if (!pred2)
1314 best_probability = prob, best_predictor = predictor;
1317 d = (combined_probability * probability
1318 + (REG_BR_PROB_BASE - combined_probability)
1319 * (REG_BR_PROB_BASE - probability));
1321 /* Use FP math to avoid overflows of 32bit integers. */
1322 if (d == 0)
1323 /* If one probability is 0% and one 100%, avoid division by zero. */
1324 combined_probability = REG_BR_PROB_BASE / 2;
1325 else
1326 combined_probability = (((double) combined_probability)
1327 * probability
1328 * REG_BR_PROB_BASE / d + 0.5);
1332 /* Decide which heuristic to use. In case we didn't match anything,
1333 use no_prediction heuristic, in case we did match, use either
1334 first match or Dempster-Shaffer theory depending on the flags. */
1336 if (best_predictor != END_PREDICTORS)
1337 first_match = true;
1339 if (!found)
1340 dump_prediction (dump_file, PRED_NO_PREDICTION, combined_probability, bb);
1341 else
1343 if (!first_match)
1344 dump_prediction (dump_file, PRED_DS_THEORY, combined_probability, bb,
1345 !first_match ? REASON_NONE : REASON_IGNORED);
1346 else
1347 dump_prediction (dump_file, PRED_FIRST_MATCH, best_probability, bb,
1348 first_match ? REASON_NONE : REASON_IGNORED);
1351 if (first_match)
1352 combined_probability = best_probability;
1353 dump_prediction (dump_file, PRED_COMBINED, combined_probability, bb);
1355 if (preds)
1357 for (pred = (struct edge_prediction *) *preds; pred; pred = pred->ep_next)
1359 enum br_predictor predictor = pred->ep_predictor;
1360 int probability = pred->ep_probability;
1362 dump_prediction (dump_file, predictor, probability, bb,
1363 (!first_match || best_predictor == predictor)
1364 ? REASON_NONE : REASON_IGNORED, pred->ep_edge);
1367 clear_bb_predictions (bb);
1370 /* If we have only one successor which is unknown, we can compute missing
1371 probability. */
1372 if (nunknown == 1)
1374 profile_probability prob = profile_probability::always ();
1375 edge missing = NULL;
1377 FOR_EACH_EDGE (e, ei, bb->succs)
1378 if (e->probability.initialized_p ())
1379 prob -= e->probability;
1380 else if (missing == NULL)
1381 missing = e;
1382 else
1383 gcc_unreachable ();
1384 missing->probability = prob;
1386 /* If nothing is unknown, we have nothing to update. */
1387 else if (!nunknown && nzero != (int)EDGE_COUNT (bb->succs))
1389 else if (!dry_run)
1391 first->probability
1392 = profile_probability::from_reg_br_prob_base (combined_probability);
1393 second->probability = first->probability.invert ();
1397 /* Check if T1 and T2 satisfy the IV_COMPARE condition.
1398 Return the SSA_NAME if the condition satisfies, NULL otherwise.
1400 T1 and T2 should be one of the following cases:
1401 1. T1 is SSA_NAME, T2 is NULL
1402 2. T1 is SSA_NAME, T2 is INTEGER_CST between [-4, 4]
1403 3. T2 is SSA_NAME, T1 is INTEGER_CST between [-4, 4] */
1405 static tree
1406 strips_small_constant (tree t1, tree t2)
1408 tree ret = NULL;
1409 int value = 0;
1411 if (!t1)
1412 return NULL;
1413 else if (TREE_CODE (t1) == SSA_NAME)
1414 ret = t1;
1415 else if (tree_fits_shwi_p (t1))
1416 value = tree_to_shwi (t1);
1417 else
1418 return NULL;
1420 if (!t2)
1421 return ret;
1422 else if (tree_fits_shwi_p (t2))
1423 value = tree_to_shwi (t2);
1424 else if (TREE_CODE (t2) == SSA_NAME)
1426 if (ret)
1427 return NULL;
1428 else
1429 ret = t2;
1432 if (value <= 4 && value >= -4)
1433 return ret;
1434 else
1435 return NULL;
1438 /* Return the SSA_NAME in T or T's operands.
1439 Return NULL if SSA_NAME cannot be found. */
1441 static tree
1442 get_base_value (tree t)
1444 if (TREE_CODE (t) == SSA_NAME)
1445 return t;
1447 if (!BINARY_CLASS_P (t))
1448 return NULL;
1450 switch (TREE_OPERAND_LENGTH (t))
1452 case 1:
1453 return strips_small_constant (TREE_OPERAND (t, 0), NULL);
1454 case 2:
1455 return strips_small_constant (TREE_OPERAND (t, 0),
1456 TREE_OPERAND (t, 1));
1457 default:
1458 return NULL;
1462 /* Check the compare STMT in LOOP. If it compares an induction
1463 variable to a loop invariant, return true, and save
1464 LOOP_INVARIANT, COMPARE_CODE and LOOP_STEP.
1465 Otherwise return false and set LOOP_INVAIANT to NULL. */
1467 static bool
1468 is_comparison_with_loop_invariant_p (gcond *stmt, class loop *loop,
1469 tree *loop_invariant,
1470 enum tree_code *compare_code,
1471 tree *loop_step,
1472 tree *loop_iv_base)
1474 tree op0, op1, bound, base;
1475 affine_iv iv0, iv1;
1476 enum tree_code code;
1477 tree step;
1479 code = gimple_cond_code (stmt);
1480 *loop_invariant = NULL;
1482 switch (code)
1484 case GT_EXPR:
1485 case GE_EXPR:
1486 case NE_EXPR:
1487 case LT_EXPR:
1488 case LE_EXPR:
1489 case EQ_EXPR:
1490 break;
1492 default:
1493 return false;
1496 op0 = gimple_cond_lhs (stmt);
1497 op1 = gimple_cond_rhs (stmt);
1499 if ((TREE_CODE (op0) != SSA_NAME && TREE_CODE (op0) != INTEGER_CST)
1500 || (TREE_CODE (op1) != SSA_NAME && TREE_CODE (op1) != INTEGER_CST))
1501 return false;
1502 if (!simple_iv (loop, loop_containing_stmt (stmt), op0, &iv0, true))
1503 return false;
1504 if (!simple_iv (loop, loop_containing_stmt (stmt), op1, &iv1, true))
1505 return false;
1506 if (TREE_CODE (iv0.step) != INTEGER_CST
1507 || TREE_CODE (iv1.step) != INTEGER_CST)
1508 return false;
1509 if ((integer_zerop (iv0.step) && integer_zerop (iv1.step))
1510 || (!integer_zerop (iv0.step) && !integer_zerop (iv1.step)))
1511 return false;
1513 if (integer_zerop (iv0.step))
1515 if (code != NE_EXPR && code != EQ_EXPR)
1516 code = invert_tree_comparison (code, false);
1517 bound = iv0.base;
1518 base = iv1.base;
1519 if (tree_fits_shwi_p (iv1.step))
1520 step = iv1.step;
1521 else
1522 return false;
1524 else
1526 bound = iv1.base;
1527 base = iv0.base;
1528 if (tree_fits_shwi_p (iv0.step))
1529 step = iv0.step;
1530 else
1531 return false;
1534 if (TREE_CODE (bound) != INTEGER_CST)
1535 bound = get_base_value (bound);
1536 if (!bound)
1537 return false;
1538 if (TREE_CODE (base) != INTEGER_CST)
1539 base = get_base_value (base);
1540 if (!base)
1541 return false;
1543 *loop_invariant = bound;
1544 *compare_code = code;
1545 *loop_step = step;
1546 *loop_iv_base = base;
1547 return true;
1550 /* Compare two SSA_NAMEs: returns TRUE if T1 and T2 are value coherent. */
1552 static bool
1553 expr_coherent_p (tree t1, tree t2)
1555 gimple *stmt;
1556 tree ssa_name_1 = NULL;
1557 tree ssa_name_2 = NULL;
1559 gcc_assert (TREE_CODE (t1) == SSA_NAME || TREE_CODE (t1) == INTEGER_CST);
1560 gcc_assert (TREE_CODE (t2) == SSA_NAME || TREE_CODE (t2) == INTEGER_CST);
1562 if (t1 == t2)
1563 return true;
1565 if (TREE_CODE (t1) == INTEGER_CST && TREE_CODE (t2) == INTEGER_CST)
1566 return true;
1567 if (TREE_CODE (t1) == INTEGER_CST || TREE_CODE (t2) == INTEGER_CST)
1568 return false;
1570 /* Check to see if t1 is expressed/defined with t2. */
1571 stmt = SSA_NAME_DEF_STMT (t1);
1572 gcc_assert (stmt != NULL);
1573 if (is_gimple_assign (stmt))
1575 ssa_name_1 = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_USE);
1576 if (ssa_name_1 && ssa_name_1 == t2)
1577 return true;
1580 /* Check to see if t2 is expressed/defined with t1. */
1581 stmt = SSA_NAME_DEF_STMT (t2);
1582 gcc_assert (stmt != NULL);
1583 if (is_gimple_assign (stmt))
1585 ssa_name_2 = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_USE);
1586 if (ssa_name_2 && ssa_name_2 == t1)
1587 return true;
1590 /* Compare if t1 and t2's def_stmts are identical. */
1591 if (ssa_name_2 != NULL && ssa_name_1 == ssa_name_2)
1592 return true;
1593 else
1594 return false;
1597 /* Return true if E is predicted by one of loop heuristics. */
1599 static bool
1600 predicted_by_loop_heuristics_p (basic_block bb)
1602 struct edge_prediction *i;
1603 edge_prediction **preds = bb_predictions->get (bb);
1605 if (!preds)
1606 return false;
1608 for (i = *preds; i; i = i->ep_next)
1609 if (i->ep_predictor == PRED_LOOP_ITERATIONS_GUESSED
1610 || i->ep_predictor == PRED_LOOP_ITERATIONS_MAX
1611 || i->ep_predictor == PRED_LOOP_ITERATIONS
1612 || i->ep_predictor == PRED_LOOP_EXIT
1613 || i->ep_predictor == PRED_LOOP_EXIT_WITH_RECURSION
1614 || i->ep_predictor == PRED_LOOP_EXTRA_EXIT)
1615 return true;
1616 return false;
1619 /* Predict branch probability of BB when BB contains a branch that compares
1620 an induction variable in LOOP with LOOP_IV_BASE_VAR to LOOP_BOUND_VAR. The
1621 loop exit is compared using LOOP_BOUND_CODE, with step of LOOP_BOUND_STEP.
1623 E.g.
1624 for (int i = 0; i < bound; i++) {
1625 if (i < bound - 2)
1626 computation_1();
1627 else
1628 computation_2();
1631 In this loop, we will predict the branch inside the loop to be taken. */
1633 static void
1634 predict_iv_comparison (class loop *loop, basic_block bb,
1635 tree loop_bound_var,
1636 tree loop_iv_base_var,
1637 enum tree_code loop_bound_code,
1638 int loop_bound_step)
1640 gimple *stmt;
1641 tree compare_var, compare_base;
1642 enum tree_code compare_code;
1643 tree compare_step_var;
1644 edge then_edge;
1645 edge_iterator ei;
1647 if (predicted_by_loop_heuristics_p (bb))
1648 return;
1650 stmt = last_stmt (bb);
1651 if (!stmt || gimple_code (stmt) != GIMPLE_COND)
1652 return;
1653 if (!is_comparison_with_loop_invariant_p (as_a <gcond *> (stmt),
1654 loop, &compare_var,
1655 &compare_code,
1656 &compare_step_var,
1657 &compare_base))
1658 return;
1660 /* Find the taken edge. */
1661 FOR_EACH_EDGE (then_edge, ei, bb->succs)
1662 if (then_edge->flags & EDGE_TRUE_VALUE)
1663 break;
1665 /* When comparing an IV to a loop invariant, NE is more likely to be
1666 taken while EQ is more likely to be not-taken. */
1667 if (compare_code == NE_EXPR)
1669 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1670 return;
1672 else if (compare_code == EQ_EXPR)
1674 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, NOT_TAKEN);
1675 return;
1678 if (!expr_coherent_p (loop_iv_base_var, compare_base))
1679 return;
1681 /* If loop bound, base and compare bound are all constants, we can
1682 calculate the probability directly. */
1683 if (tree_fits_shwi_p (loop_bound_var)
1684 && tree_fits_shwi_p (compare_var)
1685 && tree_fits_shwi_p (compare_base))
1687 int probability;
1688 wi::overflow_type overflow;
1689 bool overall_overflow = false;
1690 widest_int compare_count, tem;
1692 /* (loop_bound - base) / compare_step */
1693 tem = wi::sub (wi::to_widest (loop_bound_var),
1694 wi::to_widest (compare_base), SIGNED, &overflow);
1695 overall_overflow |= overflow;
1696 widest_int loop_count = wi::div_trunc (tem,
1697 wi::to_widest (compare_step_var),
1698 SIGNED, &overflow);
1699 overall_overflow |= overflow;
1701 if (!wi::neg_p (wi::to_widest (compare_step_var))
1702 ^ (compare_code == LT_EXPR || compare_code == LE_EXPR))
1704 /* (loop_bound - compare_bound) / compare_step */
1705 tem = wi::sub (wi::to_widest (loop_bound_var),
1706 wi::to_widest (compare_var), SIGNED, &overflow);
1707 overall_overflow |= overflow;
1708 compare_count = wi::div_trunc (tem, wi::to_widest (compare_step_var),
1709 SIGNED, &overflow);
1710 overall_overflow |= overflow;
1712 else
1714 /* (compare_bound - base) / compare_step */
1715 tem = wi::sub (wi::to_widest (compare_var),
1716 wi::to_widest (compare_base), SIGNED, &overflow);
1717 overall_overflow |= overflow;
1718 compare_count = wi::div_trunc (tem, wi::to_widest (compare_step_var),
1719 SIGNED, &overflow);
1720 overall_overflow |= overflow;
1722 if (compare_code == LE_EXPR || compare_code == GE_EXPR)
1723 ++compare_count;
1724 if (loop_bound_code == LE_EXPR || loop_bound_code == GE_EXPR)
1725 ++loop_count;
1726 if (wi::neg_p (compare_count))
1727 compare_count = 0;
1728 if (wi::neg_p (loop_count))
1729 loop_count = 0;
1730 if (loop_count == 0)
1731 probability = 0;
1732 else if (wi::cmps (compare_count, loop_count) == 1)
1733 probability = REG_BR_PROB_BASE;
1734 else
1736 tem = compare_count * REG_BR_PROB_BASE;
1737 tem = wi::udiv_trunc (tem, loop_count);
1738 probability = tem.to_uhwi ();
1741 /* FIXME: The branch prediction seems broken. It has only 20% hitrate. */
1742 if (!overall_overflow)
1743 predict_edge (then_edge, PRED_LOOP_IV_COMPARE, probability);
1745 return;
1748 if (expr_coherent_p (loop_bound_var, compare_var))
1750 if ((loop_bound_code == LT_EXPR || loop_bound_code == LE_EXPR)
1751 && (compare_code == LT_EXPR || compare_code == LE_EXPR))
1752 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1753 else if ((loop_bound_code == GT_EXPR || loop_bound_code == GE_EXPR)
1754 && (compare_code == GT_EXPR || compare_code == GE_EXPR))
1755 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1756 else if (loop_bound_code == NE_EXPR)
1758 /* If the loop backedge condition is "(i != bound)", we do
1759 the comparison based on the step of IV:
1760 * step < 0 : backedge condition is like (i > bound)
1761 * step > 0 : backedge condition is like (i < bound) */
1762 gcc_assert (loop_bound_step != 0);
1763 if (loop_bound_step > 0
1764 && (compare_code == LT_EXPR
1765 || compare_code == LE_EXPR))
1766 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1767 else if (loop_bound_step < 0
1768 && (compare_code == GT_EXPR
1769 || compare_code == GE_EXPR))
1770 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1771 else
1772 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, NOT_TAKEN);
1774 else
1775 /* The branch is predicted not-taken if loop_bound_code is
1776 opposite with compare_code. */
1777 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, NOT_TAKEN);
1779 else if (expr_coherent_p (loop_iv_base_var, compare_var))
1781 /* For cases like:
1782 for (i = s; i < h; i++)
1783 if (i > s + 2) ....
1784 The branch should be predicted taken. */
1785 if (loop_bound_step > 0
1786 && (compare_code == GT_EXPR || compare_code == GE_EXPR))
1787 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1788 else if (loop_bound_step < 0
1789 && (compare_code == LT_EXPR || compare_code == LE_EXPR))
1790 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1791 else
1792 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, NOT_TAKEN);
1796 /* Predict for extra loop exits that will lead to EXIT_EDGE. The extra loop
1797 exits are resulted from short-circuit conditions that will generate an
1798 if_tmp. E.g.:
1800 if (foo() || global > 10)
1801 break;
1803 This will be translated into:
1805 BB3:
1806 loop header...
1807 BB4:
1808 if foo() goto BB6 else goto BB5
1809 BB5:
1810 if global > 10 goto BB6 else goto BB7
1811 BB6:
1812 goto BB7
1813 BB7:
1814 iftmp = (PHI 0(BB5), 1(BB6))
1815 if iftmp == 1 goto BB8 else goto BB3
1816 BB8:
1817 outside of the loop...
1819 The edge BB7->BB8 is loop exit because BB8 is outside of the loop.
1820 From the dataflow, we can infer that BB4->BB6 and BB5->BB6 are also loop
1821 exits. This function takes BB7->BB8 as input, and finds out the extra loop
1822 exits to predict them using PRED_LOOP_EXTRA_EXIT. */
1824 static void
1825 predict_extra_loop_exits (edge exit_edge)
1827 unsigned i;
1828 bool check_value_one;
1829 gimple *lhs_def_stmt;
1830 gphi *phi_stmt;
1831 tree cmp_rhs, cmp_lhs;
1832 gimple *last;
1833 gcond *cmp_stmt;
1835 last = last_stmt (exit_edge->src);
1836 if (!last)
1837 return;
1838 cmp_stmt = dyn_cast <gcond *> (last);
1839 if (!cmp_stmt)
1840 return;
1842 cmp_rhs = gimple_cond_rhs (cmp_stmt);
1843 cmp_lhs = gimple_cond_lhs (cmp_stmt);
1844 if (!TREE_CONSTANT (cmp_rhs)
1845 || !(integer_zerop (cmp_rhs) || integer_onep (cmp_rhs)))
1846 return;
1847 if (TREE_CODE (cmp_lhs) != SSA_NAME)
1848 return;
1850 /* If check_value_one is true, only the phi_args with value '1' will lead
1851 to loop exit. Otherwise, only the phi_args with value '0' will lead to
1852 loop exit. */
1853 check_value_one = (((integer_onep (cmp_rhs))
1854 ^ (gimple_cond_code (cmp_stmt) == EQ_EXPR))
1855 ^ ((exit_edge->flags & EDGE_TRUE_VALUE) != 0));
1857 lhs_def_stmt = SSA_NAME_DEF_STMT (cmp_lhs);
1858 if (!lhs_def_stmt)
1859 return;
1861 phi_stmt = dyn_cast <gphi *> (lhs_def_stmt);
1862 if (!phi_stmt)
1863 return;
1865 for (i = 0; i < gimple_phi_num_args (phi_stmt); i++)
1867 edge e1;
1868 edge_iterator ei;
1869 tree val = gimple_phi_arg_def (phi_stmt, i);
1870 edge e = gimple_phi_arg_edge (phi_stmt, i);
1872 if (!TREE_CONSTANT (val) || !(integer_zerop (val) || integer_onep (val)))
1873 continue;
1874 if ((check_value_one ^ integer_onep (val)) == 1)
1875 continue;
1876 if (EDGE_COUNT (e->src->succs) != 1)
1878 predict_paths_leading_to_edge (e, PRED_LOOP_EXTRA_EXIT, NOT_TAKEN);
1879 continue;
1882 FOR_EACH_EDGE (e1, ei, e->src->preds)
1883 predict_paths_leading_to_edge (e1, PRED_LOOP_EXTRA_EXIT, NOT_TAKEN);
1888 /* Predict edge probabilities by exploiting loop structure. */
1890 static void
1891 predict_loops (void)
1893 class loop *loop;
1894 basic_block bb;
1895 hash_set <class loop *> with_recursion(10);
1897 FOR_EACH_BB_FN (bb, cfun)
1899 gimple_stmt_iterator gsi;
1900 tree decl;
1902 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1903 if (is_gimple_call (gsi_stmt (gsi))
1904 && (decl = gimple_call_fndecl (gsi_stmt (gsi))) != NULL
1905 && recursive_call_p (current_function_decl, decl))
1907 loop = bb->loop_father;
1908 while (loop && !with_recursion.add (loop))
1909 loop = loop_outer (loop);
1913 /* Try to predict out blocks in a loop that are not part of a
1914 natural loop. */
1915 FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
1917 basic_block bb, *bbs;
1918 unsigned j, n_exits = 0;
1919 vec<edge> exits;
1920 class tree_niter_desc niter_desc;
1921 edge ex;
1922 class nb_iter_bound *nb_iter;
1923 enum tree_code loop_bound_code = ERROR_MARK;
1924 tree loop_bound_step = NULL;
1925 tree loop_bound_var = NULL;
1926 tree loop_iv_base = NULL;
1927 gcond *stmt = NULL;
1928 bool recursion = with_recursion.contains (loop);
1930 exits = get_loop_exit_edges (loop);
1931 FOR_EACH_VEC_ELT (exits, j, ex)
1932 if (!unlikely_executed_edge_p (ex) && !(ex->flags & EDGE_ABNORMAL_CALL))
1933 n_exits ++;
1934 if (!n_exits)
1936 exits.release ();
1937 continue;
1940 if (dump_file && (dump_flags & TDF_DETAILS))
1941 fprintf (dump_file, "Predicting loop %i%s with %i exits.\n",
1942 loop->num, recursion ? " (with recursion)":"", n_exits);
1943 if (dump_file && (dump_flags & TDF_DETAILS)
1944 && max_loop_iterations_int (loop) >= 0)
1946 fprintf (dump_file,
1947 "Loop %d iterates at most %i times.\n", loop->num,
1948 (int)max_loop_iterations_int (loop));
1950 if (dump_file && (dump_flags & TDF_DETAILS)
1951 && likely_max_loop_iterations_int (loop) >= 0)
1953 fprintf (dump_file, "Loop %d likely iterates at most %i times.\n",
1954 loop->num, (int)likely_max_loop_iterations_int (loop));
1957 FOR_EACH_VEC_ELT (exits, j, ex)
1959 tree niter = NULL;
1960 HOST_WIDE_INT nitercst;
1961 int max = param_max_predicted_iterations;
1962 int probability;
1963 enum br_predictor predictor;
1964 widest_int nit;
1966 if (unlikely_executed_edge_p (ex)
1967 || (ex->flags & EDGE_ABNORMAL_CALL))
1968 continue;
1969 /* Loop heuristics do not expect exit conditional to be inside
1970 inner loop. We predict from innermost to outermost loop. */
1971 if (predicted_by_loop_heuristics_p (ex->src))
1973 if (dump_file && (dump_flags & TDF_DETAILS))
1974 fprintf (dump_file, "Skipping exit %i->%i because "
1975 "it is already predicted.\n",
1976 ex->src->index, ex->dest->index);
1977 continue;
1979 predict_extra_loop_exits (ex);
1981 if (number_of_iterations_exit (loop, ex, &niter_desc, false, false))
1982 niter = niter_desc.niter;
1983 if (!niter || TREE_CODE (niter_desc.niter) != INTEGER_CST)
1984 niter = loop_niter_by_eval (loop, ex);
1985 if (dump_file && (dump_flags & TDF_DETAILS)
1986 && TREE_CODE (niter) == INTEGER_CST)
1988 fprintf (dump_file, "Exit %i->%i %d iterates ",
1989 ex->src->index, ex->dest->index,
1990 loop->num);
1991 print_generic_expr (dump_file, niter, TDF_SLIM);
1992 fprintf (dump_file, " times.\n");
1995 if (TREE_CODE (niter) == INTEGER_CST)
1997 if (tree_fits_uhwi_p (niter)
1998 && max
1999 && compare_tree_int (niter, max - 1) == -1)
2000 nitercst = tree_to_uhwi (niter) + 1;
2001 else
2002 nitercst = max;
2003 predictor = PRED_LOOP_ITERATIONS;
2005 /* If we have just one exit and we can derive some information about
2006 the number of iterations of the loop from the statements inside
2007 the loop, use it to predict this exit. */
2008 else if (n_exits == 1
2009 && estimated_stmt_executions (loop, &nit))
2011 if (wi::gtu_p (nit, max))
2012 nitercst = max;
2013 else
2014 nitercst = nit.to_shwi ();
2015 predictor = PRED_LOOP_ITERATIONS_GUESSED;
2017 /* If we have likely upper bound, trust it for very small iteration
2018 counts. Such loops would otherwise get mispredicted by standard
2019 LOOP_EXIT heuristics. */
2020 else if (n_exits == 1
2021 && likely_max_stmt_executions (loop, &nit)
2022 && wi::ltu_p (nit,
2023 RDIV (REG_BR_PROB_BASE,
2024 REG_BR_PROB_BASE
2025 - predictor_info
2026 [recursion
2027 ? PRED_LOOP_EXIT_WITH_RECURSION
2028 : PRED_LOOP_EXIT].hitrate)))
2030 nitercst = nit.to_shwi ();
2031 predictor = PRED_LOOP_ITERATIONS_MAX;
2033 else
2035 if (dump_file && (dump_flags & TDF_DETAILS))
2036 fprintf (dump_file, "Nothing known about exit %i->%i.\n",
2037 ex->src->index, ex->dest->index);
2038 continue;
2041 if (dump_file && (dump_flags & TDF_DETAILS))
2042 fprintf (dump_file, "Recording prediction to %i iterations by %s.\n",
2043 (int)nitercst, predictor_info[predictor].name);
2044 /* If the prediction for number of iterations is zero, do not
2045 predict the exit edges. */
2046 if (nitercst == 0)
2047 continue;
2049 probability = RDIV (REG_BR_PROB_BASE, nitercst);
2050 predict_edge (ex, predictor, probability);
2052 exits.release ();
2054 /* Find information about loop bound variables. */
2055 for (nb_iter = loop->bounds; nb_iter;
2056 nb_iter = nb_iter->next)
2057 if (nb_iter->stmt
2058 && gimple_code (nb_iter->stmt) == GIMPLE_COND)
2060 stmt = as_a <gcond *> (nb_iter->stmt);
2061 break;
2063 if (!stmt && last_stmt (loop->header)
2064 && gimple_code (last_stmt (loop->header)) == GIMPLE_COND)
2065 stmt = as_a <gcond *> (last_stmt (loop->header));
2066 if (stmt)
2067 is_comparison_with_loop_invariant_p (stmt, loop,
2068 &loop_bound_var,
2069 &loop_bound_code,
2070 &loop_bound_step,
2071 &loop_iv_base);
2073 bbs = get_loop_body (loop);
2075 for (j = 0; j < loop->num_nodes; j++)
2077 edge e;
2078 edge_iterator ei;
2080 bb = bbs[j];
2082 /* Bypass loop heuristics on continue statement. These
2083 statements construct loops via "non-loop" constructs
2084 in the source language and are better to be handled
2085 separately. */
2086 if (predicted_by_p (bb, PRED_CONTINUE))
2088 if (dump_file && (dump_flags & TDF_DETAILS))
2089 fprintf (dump_file, "BB %i predicted by continue.\n",
2090 bb->index);
2091 continue;
2094 /* If we already used more reliable loop exit predictors, do not
2095 bother with PRED_LOOP_EXIT. */
2096 if (!predicted_by_loop_heuristics_p (bb))
2098 /* For loop with many exits we don't want to predict all exits
2099 with the pretty large probability, because if all exits are
2100 considered in row, the loop would be predicted to iterate
2101 almost never. The code to divide probability by number of
2102 exits is very rough. It should compute the number of exits
2103 taken in each patch through function (not the overall number
2104 of exits that might be a lot higher for loops with wide switch
2105 statements in them) and compute n-th square root.
2107 We limit the minimal probability by 2% to avoid
2108 EDGE_PROBABILITY_RELIABLE from trusting the branch prediction
2109 as this was causing regression in perl benchmark containing such
2110 a wide loop. */
2112 int probability = ((REG_BR_PROB_BASE
2113 - predictor_info
2114 [recursion
2115 ? PRED_LOOP_EXIT_WITH_RECURSION
2116 : PRED_LOOP_EXIT].hitrate)
2117 / n_exits);
2118 if (probability < HITRATE (2))
2119 probability = HITRATE (2);
2120 FOR_EACH_EDGE (e, ei, bb->succs)
2121 if (e->dest->index < NUM_FIXED_BLOCKS
2122 || !flow_bb_inside_loop_p (loop, e->dest))
2124 if (dump_file && (dump_flags & TDF_DETAILS))
2125 fprintf (dump_file,
2126 "Predicting exit %i->%i with prob %i.\n",
2127 e->src->index, e->dest->index, probability);
2128 predict_edge (e,
2129 recursion ? PRED_LOOP_EXIT_WITH_RECURSION
2130 : PRED_LOOP_EXIT, probability);
2133 if (loop_bound_var)
2134 predict_iv_comparison (loop, bb, loop_bound_var, loop_iv_base,
2135 loop_bound_code,
2136 tree_to_shwi (loop_bound_step));
2139 /* In the following code
2140 for (loop1)
2141 if (cond)
2142 for (loop2)
2143 body;
2144 guess that cond is unlikely. */
2145 if (loop_outer (loop)->num)
2147 basic_block bb = NULL;
2148 edge preheader_edge = loop_preheader_edge (loop);
2150 if (single_pred_p (preheader_edge->src)
2151 && single_succ_p (preheader_edge->src))
2152 preheader_edge = single_pred_edge (preheader_edge->src);
2154 gimple *stmt = last_stmt (preheader_edge->src);
2155 /* Pattern match fortran loop preheader:
2156 _16 = BUILTIN_EXPECT (_15, 1, PRED_FORTRAN_LOOP_PREHEADER);
2157 _17 = (logical(kind=4)) _16;
2158 if (_17 != 0)
2159 goto <bb 11>;
2160 else
2161 goto <bb 13>;
2163 Loop guard branch prediction says nothing about duplicated loop
2164 headers produced by fortran frontend and in this case we want
2165 to predict paths leading to this preheader. */
2167 if (stmt
2168 && gimple_code (stmt) == GIMPLE_COND
2169 && gimple_cond_code (stmt) == NE_EXPR
2170 && TREE_CODE (gimple_cond_lhs (stmt)) == SSA_NAME
2171 && integer_zerop (gimple_cond_rhs (stmt)))
2173 gimple *call_stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
2174 if (gimple_code (call_stmt) == GIMPLE_ASSIGN
2175 && gimple_expr_code (call_stmt) == NOP_EXPR
2176 && TREE_CODE (gimple_assign_rhs1 (call_stmt)) == SSA_NAME)
2177 call_stmt = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (call_stmt));
2178 if (gimple_call_internal_p (call_stmt, IFN_BUILTIN_EXPECT)
2179 && TREE_CODE (gimple_call_arg (call_stmt, 2)) == INTEGER_CST
2180 && tree_fits_uhwi_p (gimple_call_arg (call_stmt, 2))
2181 && tree_to_uhwi (gimple_call_arg (call_stmt, 2))
2182 == PRED_FORTRAN_LOOP_PREHEADER)
2183 bb = preheader_edge->src;
2185 if (!bb)
2187 if (!dominated_by_p (CDI_DOMINATORS,
2188 loop_outer (loop)->latch, loop->header))
2189 predict_paths_leading_to_edge (loop_preheader_edge (loop),
2190 recursion
2191 ? PRED_LOOP_GUARD_WITH_RECURSION
2192 : PRED_LOOP_GUARD,
2193 NOT_TAKEN,
2194 loop_outer (loop));
2196 else
2198 if (!dominated_by_p (CDI_DOMINATORS,
2199 loop_outer (loop)->latch, bb))
2200 predict_paths_leading_to (bb,
2201 recursion
2202 ? PRED_LOOP_GUARD_WITH_RECURSION
2203 : PRED_LOOP_GUARD,
2204 NOT_TAKEN,
2205 loop_outer (loop));
2209 /* Free basic blocks from get_loop_body. */
2210 free (bbs);
2214 /* Attempt to predict probabilities of BB outgoing edges using local
2215 properties. */
2216 static void
2217 bb_estimate_probability_locally (basic_block bb)
2219 rtx_insn *last_insn = BB_END (bb);
2220 rtx cond;
2222 if (! can_predict_insn_p (last_insn))
2223 return;
2224 cond = get_condition (last_insn, NULL, false, false);
2225 if (! cond)
2226 return;
2228 /* Try "pointer heuristic."
2229 A comparison ptr == 0 is predicted as false.
2230 Similarly, a comparison ptr1 == ptr2 is predicted as false. */
2231 if (COMPARISON_P (cond)
2232 && ((REG_P (XEXP (cond, 0)) && REG_POINTER (XEXP (cond, 0)))
2233 || (REG_P (XEXP (cond, 1)) && REG_POINTER (XEXP (cond, 1)))))
2235 if (GET_CODE (cond) == EQ)
2236 predict_insn_def (last_insn, PRED_POINTER, NOT_TAKEN);
2237 else if (GET_CODE (cond) == NE)
2238 predict_insn_def (last_insn, PRED_POINTER, TAKEN);
2240 else
2242 /* Try "opcode heuristic."
2243 EQ tests are usually false and NE tests are usually true. Also,
2244 most quantities are positive, so we can make the appropriate guesses
2245 about signed comparisons against zero. */
2246 switch (GET_CODE (cond))
2248 case CONST_INT:
2249 /* Unconditional branch. */
2250 predict_insn_def (last_insn, PRED_UNCONDITIONAL,
2251 cond == const0_rtx ? NOT_TAKEN : TAKEN);
2252 break;
2254 case EQ:
2255 case UNEQ:
2256 /* Floating point comparisons appears to behave in a very
2257 unpredictable way because of special role of = tests in
2258 FP code. */
2259 if (FLOAT_MODE_P (GET_MODE (XEXP (cond, 0))))
2261 /* Comparisons with 0 are often used for booleans and there is
2262 nothing useful to predict about them. */
2263 else if (XEXP (cond, 1) == const0_rtx
2264 || XEXP (cond, 0) == const0_rtx)
2266 else
2267 predict_insn_def (last_insn, PRED_OPCODE_NONEQUAL, NOT_TAKEN);
2268 break;
2270 case NE:
2271 case LTGT:
2272 /* Floating point comparisons appears to behave in a very
2273 unpredictable way because of special role of = tests in
2274 FP code. */
2275 if (FLOAT_MODE_P (GET_MODE (XEXP (cond, 0))))
2277 /* Comparisons with 0 are often used for booleans and there is
2278 nothing useful to predict about them. */
2279 else if (XEXP (cond, 1) == const0_rtx
2280 || XEXP (cond, 0) == const0_rtx)
2282 else
2283 predict_insn_def (last_insn, PRED_OPCODE_NONEQUAL, TAKEN);
2284 break;
2286 case ORDERED:
2287 predict_insn_def (last_insn, PRED_FPOPCODE, TAKEN);
2288 break;
2290 case UNORDERED:
2291 predict_insn_def (last_insn, PRED_FPOPCODE, NOT_TAKEN);
2292 break;
2294 case LE:
2295 case LT:
2296 if (XEXP (cond, 1) == const0_rtx || XEXP (cond, 1) == const1_rtx
2297 || XEXP (cond, 1) == constm1_rtx)
2298 predict_insn_def (last_insn, PRED_OPCODE_POSITIVE, NOT_TAKEN);
2299 break;
2301 case GE:
2302 case GT:
2303 if (XEXP (cond, 1) == const0_rtx || XEXP (cond, 1) == const1_rtx
2304 || XEXP (cond, 1) == constm1_rtx)
2305 predict_insn_def (last_insn, PRED_OPCODE_POSITIVE, TAKEN);
2306 break;
2308 default:
2309 break;
2313 /* Set edge->probability for each successor edge of BB. */
2314 void
2315 guess_outgoing_edge_probabilities (basic_block bb)
2317 bb_estimate_probability_locally (bb);
2318 combine_predictions_for_insn (BB_END (bb), bb);
2321 static tree expr_expected_value (tree, bitmap, enum br_predictor *predictor,
2322 HOST_WIDE_INT *probability);
2324 /* Helper function for expr_expected_value. */
2326 static tree
2327 expr_expected_value_1 (tree type, tree op0, enum tree_code code,
2328 tree op1, bitmap visited, enum br_predictor *predictor,
2329 HOST_WIDE_INT *probability)
2331 gimple *def;
2333 /* Reset returned probability value. */
2334 *probability = -1;
2335 *predictor = PRED_UNCONDITIONAL;
2337 if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS)
2339 if (TREE_CONSTANT (op0))
2340 return op0;
2342 if (code == IMAGPART_EXPR)
2344 if (TREE_CODE (TREE_OPERAND (op0, 0)) == SSA_NAME)
2346 def = SSA_NAME_DEF_STMT (TREE_OPERAND (op0, 0));
2347 if (is_gimple_call (def)
2348 && gimple_call_internal_p (def)
2349 && (gimple_call_internal_fn (def)
2350 == IFN_ATOMIC_COMPARE_EXCHANGE))
2352 /* Assume that any given atomic operation has low contention,
2353 and thus the compare-and-swap operation succeeds. */
2354 *predictor = PRED_COMPARE_AND_SWAP;
2355 return build_one_cst (TREE_TYPE (op0));
2360 if (code != SSA_NAME)
2361 return NULL_TREE;
2363 def = SSA_NAME_DEF_STMT (op0);
2365 /* If we were already here, break the infinite cycle. */
2366 if (!bitmap_set_bit (visited, SSA_NAME_VERSION (op0)))
2367 return NULL;
2369 if (gimple_code (def) == GIMPLE_PHI)
2371 /* All the arguments of the PHI node must have the same constant
2372 length. */
2373 int i, n = gimple_phi_num_args (def);
2374 tree val = NULL, new_val;
2376 for (i = 0; i < n; i++)
2378 tree arg = PHI_ARG_DEF (def, i);
2379 enum br_predictor predictor2;
2381 /* If this PHI has itself as an argument, we cannot
2382 determine the string length of this argument. However,
2383 if we can find an expected constant value for the other
2384 PHI args then we can still be sure that this is
2385 likely a constant. So be optimistic and just
2386 continue with the next argument. */
2387 if (arg == PHI_RESULT (def))
2388 continue;
2390 HOST_WIDE_INT probability2;
2391 new_val = expr_expected_value (arg, visited, &predictor2,
2392 &probability2);
2394 /* It is difficult to combine value predictors. Simply assume
2395 that later predictor is weaker and take its prediction. */
2396 if (*predictor < predictor2)
2398 *predictor = predictor2;
2399 *probability = probability2;
2401 if (!new_val)
2402 return NULL;
2403 if (!val)
2404 val = new_val;
2405 else if (!operand_equal_p (val, new_val, false))
2406 return NULL;
2408 return val;
2410 if (is_gimple_assign (def))
2412 if (gimple_assign_lhs (def) != op0)
2413 return NULL;
2415 return expr_expected_value_1 (TREE_TYPE (gimple_assign_lhs (def)),
2416 gimple_assign_rhs1 (def),
2417 gimple_assign_rhs_code (def),
2418 gimple_assign_rhs2 (def),
2419 visited, predictor, probability);
2422 if (is_gimple_call (def))
2424 tree decl = gimple_call_fndecl (def);
2425 if (!decl)
2427 if (gimple_call_internal_p (def)
2428 && gimple_call_internal_fn (def) == IFN_BUILTIN_EXPECT)
2430 gcc_assert (gimple_call_num_args (def) == 3);
2431 tree val = gimple_call_arg (def, 0);
2432 if (TREE_CONSTANT (val))
2433 return val;
2434 tree val2 = gimple_call_arg (def, 2);
2435 gcc_assert (TREE_CODE (val2) == INTEGER_CST
2436 && tree_fits_uhwi_p (val2)
2437 && tree_to_uhwi (val2) < END_PREDICTORS);
2438 *predictor = (enum br_predictor) tree_to_uhwi (val2);
2439 if (*predictor == PRED_BUILTIN_EXPECT)
2440 *probability
2441 = HITRATE (param_builtin_expect_probability);
2442 return gimple_call_arg (def, 1);
2444 return NULL;
2447 if (DECL_IS_MALLOC (decl) || DECL_IS_OPERATOR_NEW_P (decl))
2449 if (predictor)
2450 *predictor = PRED_MALLOC_NONNULL;
2451 return boolean_true_node;
2454 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2455 switch (DECL_FUNCTION_CODE (decl))
2457 case BUILT_IN_EXPECT:
2459 tree val;
2460 if (gimple_call_num_args (def) != 2)
2461 return NULL;
2462 val = gimple_call_arg (def, 0);
2463 if (TREE_CONSTANT (val))
2464 return val;
2465 *predictor = PRED_BUILTIN_EXPECT;
2466 *probability
2467 = HITRATE (param_builtin_expect_probability);
2468 return gimple_call_arg (def, 1);
2470 case BUILT_IN_EXPECT_WITH_PROBABILITY:
2472 tree val;
2473 if (gimple_call_num_args (def) != 3)
2474 return NULL;
2475 val = gimple_call_arg (def, 0);
2476 if (TREE_CONSTANT (val))
2477 return val;
2478 /* Compute final probability as:
2479 probability * REG_BR_PROB_BASE. */
2480 tree prob = gimple_call_arg (def, 2);
2481 tree t = TREE_TYPE (prob);
2482 tree base = build_int_cst (integer_type_node,
2483 REG_BR_PROB_BASE);
2484 base = build_real_from_int_cst (t, base);
2485 tree r = fold_build2_initializer_loc (UNKNOWN_LOCATION,
2486 MULT_EXPR, t, prob, base);
2487 if (TREE_CODE (r) != REAL_CST)
2489 error_at (gimple_location (def),
2490 "probability %qE must be "
2491 "constant floating-point expression", prob);
2492 return NULL;
2494 HOST_WIDE_INT probi
2495 = real_to_integer (TREE_REAL_CST_PTR (r));
2496 if (probi >= 0 && probi <= REG_BR_PROB_BASE)
2498 *predictor = PRED_BUILTIN_EXPECT_WITH_PROBABILITY;
2499 *probability = probi;
2501 else
2502 error_at (gimple_location (def),
2503 "probability %qE is outside "
2504 "the range [0.0, 1.0]", prob);
2506 return gimple_call_arg (def, 1);
2509 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
2510 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
2511 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
2512 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
2513 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
2514 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
2515 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
2516 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
2517 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
2518 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
2519 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
2520 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
2521 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
2522 /* Assume that any given atomic operation has low contention,
2523 and thus the compare-and-swap operation succeeds. */
2524 *predictor = PRED_COMPARE_AND_SWAP;
2525 return boolean_true_node;
2526 case BUILT_IN_REALLOC:
2527 if (predictor)
2528 *predictor = PRED_MALLOC_NONNULL;
2529 return boolean_true_node;
2530 default:
2531 break;
2535 return NULL;
2538 if (get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS)
2540 tree res;
2541 enum br_predictor predictor2;
2542 HOST_WIDE_INT probability2;
2543 op0 = expr_expected_value (op0, visited, predictor, probability);
2544 if (!op0)
2545 return NULL;
2546 op1 = expr_expected_value (op1, visited, &predictor2, &probability2);
2547 if (!op1)
2548 return NULL;
2549 res = fold_build2 (code, type, op0, op1);
2550 if (TREE_CODE (res) == INTEGER_CST
2551 && TREE_CODE (op0) == INTEGER_CST
2552 && TREE_CODE (op1) == INTEGER_CST)
2554 /* Combine binary predictions. */
2555 if (*probability != -1 || probability2 != -1)
2557 HOST_WIDE_INT p1 = get_predictor_value (*predictor, *probability);
2558 HOST_WIDE_INT p2 = get_predictor_value (predictor2, probability2);
2559 *probability = RDIV (p1 * p2, REG_BR_PROB_BASE);
2562 if (*predictor < predictor2)
2563 *predictor = predictor2;
2565 return res;
2567 return NULL;
2569 if (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS)
2571 tree res;
2572 op0 = expr_expected_value (op0, visited, predictor, probability);
2573 if (!op0)
2574 return NULL;
2575 res = fold_build1 (code, type, op0);
2576 if (TREE_CONSTANT (res))
2577 return res;
2578 return NULL;
2580 return NULL;
2583 /* Return constant EXPR will likely have at execution time, NULL if unknown.
2584 The function is used by builtin_expect branch predictor so the evidence
2585 must come from this construct and additional possible constant folding.
2587 We may want to implement more involved value guess (such as value range
2588 propagation based prediction), but such tricks shall go to new
2589 implementation. */
2591 static tree
2592 expr_expected_value (tree expr, bitmap visited,
2593 enum br_predictor *predictor,
2594 HOST_WIDE_INT *probability)
2596 enum tree_code code;
2597 tree op0, op1;
2599 if (TREE_CONSTANT (expr))
2601 *predictor = PRED_UNCONDITIONAL;
2602 *probability = -1;
2603 return expr;
2606 extract_ops_from_tree (expr, &code, &op0, &op1);
2607 return expr_expected_value_1 (TREE_TYPE (expr),
2608 op0, code, op1, visited, predictor,
2609 probability);
2613 /* Return probability of a PREDICTOR. If the predictor has variable
2614 probability return passed PROBABILITY. */
2616 static HOST_WIDE_INT
2617 get_predictor_value (br_predictor predictor, HOST_WIDE_INT probability)
2619 switch (predictor)
2621 case PRED_BUILTIN_EXPECT:
2622 case PRED_BUILTIN_EXPECT_WITH_PROBABILITY:
2623 gcc_assert (probability != -1);
2624 return probability;
2625 default:
2626 gcc_assert (probability == -1);
2627 return predictor_info[(int) predictor].hitrate;
2631 /* Predict using opcode of the last statement in basic block. */
2632 static void
2633 tree_predict_by_opcode (basic_block bb)
2635 gimple *stmt = last_stmt (bb);
2636 edge then_edge;
2637 tree op0, op1;
2638 tree type;
2639 tree val;
2640 enum tree_code cmp;
2641 edge_iterator ei;
2642 enum br_predictor predictor;
2643 HOST_WIDE_INT probability;
2645 if (!stmt)
2646 return;
2648 if (gswitch *sw = dyn_cast <gswitch *> (stmt))
2650 tree index = gimple_switch_index (sw);
2651 tree val = expr_expected_value (index, auto_bitmap (),
2652 &predictor, &probability);
2653 if (val && TREE_CODE (val) == INTEGER_CST)
2655 edge e = find_taken_edge_switch_expr (sw, val);
2656 if (predictor == PRED_BUILTIN_EXPECT)
2658 int percent = param_builtin_expect_probability;
2659 gcc_assert (percent >= 0 && percent <= 100);
2660 predict_edge (e, PRED_BUILTIN_EXPECT,
2661 HITRATE (percent));
2663 else
2664 predict_edge_def (e, predictor, TAKEN);
2668 if (gimple_code (stmt) != GIMPLE_COND)
2669 return;
2670 FOR_EACH_EDGE (then_edge, ei, bb->succs)
2671 if (then_edge->flags & EDGE_TRUE_VALUE)
2672 break;
2673 op0 = gimple_cond_lhs (stmt);
2674 op1 = gimple_cond_rhs (stmt);
2675 cmp = gimple_cond_code (stmt);
2676 type = TREE_TYPE (op0);
2677 val = expr_expected_value_1 (boolean_type_node, op0, cmp, op1, auto_bitmap (),
2678 &predictor, &probability);
2679 if (val && TREE_CODE (val) == INTEGER_CST)
2681 HOST_WIDE_INT prob = get_predictor_value (predictor, probability);
2682 if (integer_zerop (val))
2683 prob = REG_BR_PROB_BASE - prob;
2684 predict_edge (then_edge, predictor, prob);
2686 /* Try "pointer heuristic."
2687 A comparison ptr == 0 is predicted as false.
2688 Similarly, a comparison ptr1 == ptr2 is predicted as false. */
2689 if (POINTER_TYPE_P (type))
2691 if (cmp == EQ_EXPR)
2692 predict_edge_def (then_edge, PRED_TREE_POINTER, NOT_TAKEN);
2693 else if (cmp == NE_EXPR)
2694 predict_edge_def (then_edge, PRED_TREE_POINTER, TAKEN);
2696 else
2698 /* Try "opcode heuristic."
2699 EQ tests are usually false and NE tests are usually true. Also,
2700 most quantities are positive, so we can make the appropriate guesses
2701 about signed comparisons against zero. */
2702 switch (cmp)
2704 case EQ_EXPR:
2705 case UNEQ_EXPR:
2706 /* Floating point comparisons appears to behave in a very
2707 unpredictable way because of special role of = tests in
2708 FP code. */
2709 if (FLOAT_TYPE_P (type))
2711 /* Comparisons with 0 are often used for booleans and there is
2712 nothing useful to predict about them. */
2713 else if (integer_zerop (op0) || integer_zerop (op1))
2715 else
2716 predict_edge_def (then_edge, PRED_TREE_OPCODE_NONEQUAL, NOT_TAKEN);
2717 break;
2719 case NE_EXPR:
2720 case LTGT_EXPR:
2721 /* Floating point comparisons appears to behave in a very
2722 unpredictable way because of special role of = tests in
2723 FP code. */
2724 if (FLOAT_TYPE_P (type))
2726 /* Comparisons with 0 are often used for booleans and there is
2727 nothing useful to predict about them. */
2728 else if (integer_zerop (op0)
2729 || integer_zerop (op1))
2731 else
2732 predict_edge_def (then_edge, PRED_TREE_OPCODE_NONEQUAL, TAKEN);
2733 break;
2735 case ORDERED_EXPR:
2736 predict_edge_def (then_edge, PRED_TREE_FPOPCODE, TAKEN);
2737 break;
2739 case UNORDERED_EXPR:
2740 predict_edge_def (then_edge, PRED_TREE_FPOPCODE, NOT_TAKEN);
2741 break;
2743 case LE_EXPR:
2744 case LT_EXPR:
2745 if (integer_zerop (op1)
2746 || integer_onep (op1)
2747 || integer_all_onesp (op1)
2748 || real_zerop (op1)
2749 || real_onep (op1)
2750 || real_minus_onep (op1))
2751 predict_edge_def (then_edge, PRED_TREE_OPCODE_POSITIVE, NOT_TAKEN);
2752 break;
2754 case GE_EXPR:
2755 case GT_EXPR:
2756 if (integer_zerop (op1)
2757 || integer_onep (op1)
2758 || integer_all_onesp (op1)
2759 || real_zerop (op1)
2760 || real_onep (op1)
2761 || real_minus_onep (op1))
2762 predict_edge_def (then_edge, PRED_TREE_OPCODE_POSITIVE, TAKEN);
2763 break;
2765 default:
2766 break;
2770 /* Returns TRUE if the STMT is exit(0) like statement. */
2772 static bool
2773 is_exit_with_zero_arg (const gimple *stmt)
2775 /* This is not exit, _exit or _Exit. */
2776 if (!gimple_call_builtin_p (stmt, BUILT_IN_EXIT)
2777 && !gimple_call_builtin_p (stmt, BUILT_IN__EXIT)
2778 && !gimple_call_builtin_p (stmt, BUILT_IN__EXIT2))
2779 return false;
2781 /* Argument is an interger zero. */
2782 return integer_zerop (gimple_call_arg (stmt, 0));
2785 /* Try to guess whether the value of return means error code. */
2787 static enum br_predictor
2788 return_prediction (tree val, enum prediction *prediction)
2790 /* VOID. */
2791 if (!val)
2792 return PRED_NO_PREDICTION;
2793 /* Different heuristics for pointers and scalars. */
2794 if (POINTER_TYPE_P (TREE_TYPE (val)))
2796 /* NULL is usually not returned. */
2797 if (integer_zerop (val))
2799 *prediction = NOT_TAKEN;
2800 return PRED_NULL_RETURN;
2803 else if (INTEGRAL_TYPE_P (TREE_TYPE (val)))
2805 /* Negative return values are often used to indicate
2806 errors. */
2807 if (TREE_CODE (val) == INTEGER_CST
2808 && tree_int_cst_sgn (val) < 0)
2810 *prediction = NOT_TAKEN;
2811 return PRED_NEGATIVE_RETURN;
2813 /* Constant return values seems to be commonly taken.
2814 Zero/one often represent booleans so exclude them from the
2815 heuristics. */
2816 if (TREE_CONSTANT (val)
2817 && (!integer_zerop (val) && !integer_onep (val)))
2819 *prediction = NOT_TAKEN;
2820 return PRED_CONST_RETURN;
2823 return PRED_NO_PREDICTION;
2826 /* Return zero if phi result could have values other than -1, 0 or 1,
2827 otherwise return a bitmask, with bits 0, 1 and 2 set if -1, 0 and 1
2828 values are used or likely. */
2830 static int
2831 zero_one_minusone (gphi *phi, int limit)
2833 int phi_num_args = gimple_phi_num_args (phi);
2834 int ret = 0;
2835 for (int i = 0; i < phi_num_args; i++)
2837 tree t = PHI_ARG_DEF (phi, i);
2838 if (TREE_CODE (t) != INTEGER_CST)
2839 continue;
2840 wide_int w = wi::to_wide (t);
2841 if (w == -1)
2842 ret |= 1;
2843 else if (w == 0)
2844 ret |= 2;
2845 else if (w == 1)
2846 ret |= 4;
2847 else
2848 return 0;
2850 for (int i = 0; i < phi_num_args; i++)
2852 tree t = PHI_ARG_DEF (phi, i);
2853 if (TREE_CODE (t) == INTEGER_CST)
2854 continue;
2855 if (TREE_CODE (t) != SSA_NAME)
2856 return 0;
2857 gimple *g = SSA_NAME_DEF_STMT (t);
2858 if (gimple_code (g) == GIMPLE_PHI && limit > 0)
2859 if (int r = zero_one_minusone (as_a <gphi *> (g), limit - 1))
2861 ret |= r;
2862 continue;
2864 if (!is_gimple_assign (g))
2865 return 0;
2866 if (gimple_assign_cast_p (g))
2868 tree rhs1 = gimple_assign_rhs1 (g);
2869 if (TREE_CODE (rhs1) != SSA_NAME
2870 || !INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
2871 || TYPE_PRECISION (TREE_TYPE (rhs1)) != 1
2872 || !TYPE_UNSIGNED (TREE_TYPE (rhs1)))
2873 return 0;
2874 ret |= (2 | 4);
2875 continue;
2877 if (TREE_CODE_CLASS (gimple_assign_rhs_code (g)) != tcc_comparison)
2878 return 0;
2879 ret |= (2 | 4);
2881 return ret;
2884 /* Find the basic block with return expression and look up for possible
2885 return value trying to apply RETURN_PREDICTION heuristics. */
2886 static void
2887 apply_return_prediction (void)
2889 greturn *return_stmt = NULL;
2890 tree return_val;
2891 edge e;
2892 gphi *phi;
2893 int phi_num_args, i;
2894 enum br_predictor pred;
2895 enum prediction direction;
2896 edge_iterator ei;
2898 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
2900 gimple *last = last_stmt (e->src);
2901 if (last
2902 && gimple_code (last) == GIMPLE_RETURN)
2904 return_stmt = as_a <greturn *> (last);
2905 break;
2908 if (!e)
2909 return;
2910 return_val = gimple_return_retval (return_stmt);
2911 if (!return_val)
2912 return;
2913 if (TREE_CODE (return_val) != SSA_NAME
2914 || !SSA_NAME_DEF_STMT (return_val)
2915 || gimple_code (SSA_NAME_DEF_STMT (return_val)) != GIMPLE_PHI)
2916 return;
2917 phi = as_a <gphi *> (SSA_NAME_DEF_STMT (return_val));
2918 phi_num_args = gimple_phi_num_args (phi);
2919 pred = return_prediction (PHI_ARG_DEF (phi, 0), &direction);
2921 /* Avoid the case where the function returns -1, 0 and 1 values and
2922 nothing else. Those could be qsort etc. comparison functions
2923 where the negative return isn't less probable than positive.
2924 For this require that the function returns at least -1 or 1
2925 or -1 and a boolean value or comparison result, so that functions
2926 returning just -1 and 0 are treated as if -1 represents error value. */
2927 if (INTEGRAL_TYPE_P (TREE_TYPE (return_val))
2928 && !TYPE_UNSIGNED (TREE_TYPE (return_val))
2929 && TYPE_PRECISION (TREE_TYPE (return_val)) > 1)
2930 if (int r = zero_one_minusone (phi, 3))
2931 if ((r & (1 | 4)) == (1 | 4))
2932 return;
2934 /* Avoid the degenerate case where all return values form the function
2935 belongs to same category (ie they are all positive constants)
2936 so we can hardly say something about them. */
2937 for (i = 1; i < phi_num_args; i++)
2938 if (pred != return_prediction (PHI_ARG_DEF (phi, i), &direction))
2939 break;
2940 if (i != phi_num_args)
2941 for (i = 0; i < phi_num_args; i++)
2943 pred = return_prediction (PHI_ARG_DEF (phi, i), &direction);
2944 if (pred != PRED_NO_PREDICTION)
2945 predict_paths_leading_to_edge (gimple_phi_arg_edge (phi, i), pred,
2946 direction);
2950 /* Look for basic block that contains unlikely to happen events
2951 (such as noreturn calls) and mark all paths leading to execution
2952 of this basic blocks as unlikely. */
2954 static void
2955 tree_bb_level_predictions (void)
2957 basic_block bb;
2958 bool has_return_edges = false;
2959 edge e;
2960 edge_iterator ei;
2962 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
2963 if (!unlikely_executed_edge_p (e) && !(e->flags & EDGE_ABNORMAL_CALL))
2965 has_return_edges = true;
2966 break;
2969 apply_return_prediction ();
2971 FOR_EACH_BB_FN (bb, cfun)
2973 gimple_stmt_iterator gsi;
2975 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2977 gimple *stmt = gsi_stmt (gsi);
2978 tree decl;
2980 if (is_gimple_call (stmt))
2982 if (gimple_call_noreturn_p (stmt)
2983 && has_return_edges
2984 && !is_exit_with_zero_arg (stmt))
2985 predict_paths_leading_to (bb, PRED_NORETURN,
2986 NOT_TAKEN);
2987 decl = gimple_call_fndecl (stmt);
2988 if (decl
2989 && lookup_attribute ("cold",
2990 DECL_ATTRIBUTES (decl)))
2991 predict_paths_leading_to (bb, PRED_COLD_FUNCTION,
2992 NOT_TAKEN);
2993 if (decl && recursive_call_p (current_function_decl, decl))
2994 predict_paths_leading_to (bb, PRED_RECURSIVE_CALL,
2995 NOT_TAKEN);
2997 else if (gimple_code (stmt) == GIMPLE_PREDICT)
2999 predict_paths_leading_to (bb, gimple_predict_predictor (stmt),
3000 gimple_predict_outcome (stmt));
3001 /* Keep GIMPLE_PREDICT around so early inlining will propagate
3002 hints to callers. */
3008 /* Callback for hash_map::traverse, asserts that the pointer map is
3009 empty. */
3011 bool
3012 assert_is_empty (const_basic_block const &, edge_prediction *const &value,
3013 void *)
3015 gcc_assert (!value);
3016 return false;
3019 /* Predict branch probabilities and estimate profile for basic block BB.
3020 When LOCAL_ONLY is set do not use any global properties of CFG. */
3022 static void
3023 tree_estimate_probability_bb (basic_block bb, bool local_only)
3025 edge e;
3026 edge_iterator ei;
3028 FOR_EACH_EDGE (e, ei, bb->succs)
3030 /* Look for block we are guarding (ie we dominate it,
3031 but it doesn't postdominate us). */
3032 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun) && e->dest != bb
3033 && !local_only
3034 && dominated_by_p (CDI_DOMINATORS, e->dest, e->src)
3035 && !dominated_by_p (CDI_POST_DOMINATORS, e->src, e->dest))
3037 gimple_stmt_iterator bi;
3039 /* The call heuristic claims that a guarded function call
3040 is improbable. This is because such calls are often used
3041 to signal exceptional situations such as printing error
3042 messages. */
3043 for (bi = gsi_start_bb (e->dest); !gsi_end_p (bi);
3044 gsi_next (&bi))
3046 gimple *stmt = gsi_stmt (bi);
3047 if (is_gimple_call (stmt)
3048 && !gimple_inexpensive_call_p (as_a <gcall *> (stmt))
3049 /* Constant and pure calls are hardly used to signalize
3050 something exceptional. */
3051 && gimple_has_side_effects (stmt))
3053 if (gimple_call_fndecl (stmt))
3054 predict_edge_def (e, PRED_CALL, NOT_TAKEN);
3055 else if (virtual_method_call_p (gimple_call_fn (stmt)))
3056 predict_edge_def (e, PRED_POLYMORPHIC_CALL, NOT_TAKEN);
3057 else
3058 predict_edge_def (e, PRED_INDIR_CALL, TAKEN);
3059 break;
3064 tree_predict_by_opcode (bb);
3067 /* Predict branch probabilities and estimate profile of the tree CFG.
3068 This function can be called from the loop optimizers to recompute
3069 the profile information.
3070 If DRY_RUN is set, do not modify CFG and only produce dump files. */
3072 void
3073 tree_estimate_probability (bool dry_run)
3075 basic_block bb;
3077 add_noreturn_fake_exit_edges ();
3078 connect_infinite_loops_to_exit ();
3079 /* We use loop_niter_by_eval, which requires that the loops have
3080 preheaders. */
3081 create_preheaders (CP_SIMPLE_PREHEADERS);
3082 calculate_dominance_info (CDI_POST_DOMINATORS);
3083 /* Decide which edges are known to be unlikely. This improves later
3084 branch prediction. */
3085 determine_unlikely_bbs ();
3087 bb_predictions = new hash_map<const_basic_block, edge_prediction *>;
3088 tree_bb_level_predictions ();
3089 record_loop_exits ();
3091 if (number_of_loops (cfun) > 1)
3092 predict_loops ();
3094 FOR_EACH_BB_FN (bb, cfun)
3095 tree_estimate_probability_bb (bb, false);
3097 FOR_EACH_BB_FN (bb, cfun)
3098 combine_predictions_for_bb (bb, dry_run);
3100 if (flag_checking)
3101 bb_predictions->traverse<void *, assert_is_empty> (NULL);
3103 delete bb_predictions;
3104 bb_predictions = NULL;
3106 if (!dry_run)
3107 estimate_bb_frequencies (false);
3108 free_dominance_info (CDI_POST_DOMINATORS);
3109 remove_fake_exit_edges ();
3112 /* Set edge->probability for each successor edge of BB. */
3113 void
3114 tree_guess_outgoing_edge_probabilities (basic_block bb)
3116 bb_predictions = new hash_map<const_basic_block, edge_prediction *>;
3117 tree_estimate_probability_bb (bb, true);
3118 combine_predictions_for_bb (bb, false);
3119 if (flag_checking)
3120 bb_predictions->traverse<void *, assert_is_empty> (NULL);
3121 delete bb_predictions;
3122 bb_predictions = NULL;
3125 /* Filter function predicate that returns true for a edge predicate P
3126 if its edge is equal to DATA. */
3128 static bool
3129 not_loop_guard_equal_edge_p (edge_prediction *p, void *data)
3131 return p->ep_edge != (edge)data || p->ep_predictor != PRED_LOOP_GUARD;
3134 /* Predict edge E with PRED unless it is already predicted by some predictor
3135 considered equivalent. */
3137 static void
3138 maybe_predict_edge (edge e, enum br_predictor pred, enum prediction taken)
3140 if (edge_predicted_by_p (e, pred, taken))
3141 return;
3142 if (pred == PRED_LOOP_GUARD
3143 && edge_predicted_by_p (e, PRED_LOOP_GUARD_WITH_RECURSION, taken))
3144 return;
3145 /* Consider PRED_LOOP_GUARD_WITH_RECURSION superrior to LOOP_GUARD. */
3146 if (pred == PRED_LOOP_GUARD_WITH_RECURSION)
3148 edge_prediction **preds = bb_predictions->get (e->src);
3149 if (preds)
3150 filter_predictions (preds, not_loop_guard_equal_edge_p, e);
3152 predict_edge_def (e, pred, taken);
3154 /* Predict edges to successors of CUR whose sources are not postdominated by
3155 BB by PRED and recurse to all postdominators. */
3157 static void
3158 predict_paths_for_bb (basic_block cur, basic_block bb,
3159 enum br_predictor pred,
3160 enum prediction taken,
3161 bitmap visited, class loop *in_loop = NULL)
3163 edge e;
3164 edge_iterator ei;
3165 basic_block son;
3167 /* If we exited the loop or CUR is unconditional in the loop, there is
3168 nothing to do. */
3169 if (in_loop
3170 && (!flow_bb_inside_loop_p (in_loop, cur)
3171 || dominated_by_p (CDI_DOMINATORS, in_loop->latch, cur)))
3172 return;
3174 /* We are looking for all edges forming edge cut induced by
3175 set of all blocks postdominated by BB. */
3176 FOR_EACH_EDGE (e, ei, cur->preds)
3177 if (e->src->index >= NUM_FIXED_BLOCKS
3178 && !dominated_by_p (CDI_POST_DOMINATORS, e->src, bb))
3180 edge e2;
3181 edge_iterator ei2;
3182 bool found = false;
3184 /* Ignore fake edges and eh, we predict them as not taken anyway. */
3185 if (unlikely_executed_edge_p (e))
3186 continue;
3187 gcc_assert (bb == cur || dominated_by_p (CDI_POST_DOMINATORS, cur, bb));
3189 /* See if there is an edge from e->src that is not abnormal
3190 and does not lead to BB and does not exit the loop. */
3191 FOR_EACH_EDGE (e2, ei2, e->src->succs)
3192 if (e2 != e
3193 && !unlikely_executed_edge_p (e2)
3194 && !dominated_by_p (CDI_POST_DOMINATORS, e2->dest, bb)
3195 && (!in_loop || !loop_exit_edge_p (in_loop, e2)))
3197 found = true;
3198 break;
3201 /* If there is non-abnormal path leaving e->src, predict edge
3202 using predictor. Otherwise we need to look for paths
3203 leading to e->src.
3205 The second may lead to infinite loop in the case we are predicitng
3206 regions that are only reachable by abnormal edges. We simply
3207 prevent visiting given BB twice. */
3208 if (found)
3209 maybe_predict_edge (e, pred, taken);
3210 else if (bitmap_set_bit (visited, e->src->index))
3211 predict_paths_for_bb (e->src, e->src, pred, taken, visited, in_loop);
3213 for (son = first_dom_son (CDI_POST_DOMINATORS, cur);
3214 son;
3215 son = next_dom_son (CDI_POST_DOMINATORS, son))
3216 predict_paths_for_bb (son, bb, pred, taken, visited, in_loop);
3219 /* Sets branch probabilities according to PREDiction and
3220 FLAGS. */
3222 static void
3223 predict_paths_leading_to (basic_block bb, enum br_predictor pred,
3224 enum prediction taken, class loop *in_loop)
3226 predict_paths_for_bb (bb, bb, pred, taken, auto_bitmap (), in_loop);
3229 /* Like predict_paths_leading_to but take edge instead of basic block. */
3231 static void
3232 predict_paths_leading_to_edge (edge e, enum br_predictor pred,
3233 enum prediction taken, class loop *in_loop)
3235 bool has_nonloop_edge = false;
3236 edge_iterator ei;
3237 edge e2;
3239 basic_block bb = e->src;
3240 FOR_EACH_EDGE (e2, ei, bb->succs)
3241 if (e2->dest != e->src && e2->dest != e->dest
3242 && !unlikely_executed_edge_p (e2)
3243 && !dominated_by_p (CDI_POST_DOMINATORS, e->src, e2->dest))
3245 has_nonloop_edge = true;
3246 break;
3249 if (!has_nonloop_edge)
3250 predict_paths_for_bb (bb, bb, pred, taken, auto_bitmap (), in_loop);
3251 else
3252 maybe_predict_edge (e, pred, taken);
3255 /* This is used to carry information about basic blocks. It is
3256 attached to the AUX field of the standard CFG block. */
3258 class block_info
3260 public:
3261 /* Estimated frequency of execution of basic_block. */
3262 sreal frequency;
3264 /* To keep queue of basic blocks to process. */
3265 basic_block next;
3267 /* Number of predecessors we need to visit first. */
3268 int npredecessors;
3271 /* Similar information for edges. */
3272 class edge_prob_info
3274 public:
3275 /* In case edge is a loopback edge, the probability edge will be reached
3276 in case header is. Estimated number of iterations of the loop can be
3277 then computed as 1 / (1 - back_edge_prob). */
3278 sreal back_edge_prob;
3279 /* True if the edge is a loopback edge in the natural loop. */
3280 unsigned int back_edge:1;
3283 #define BLOCK_INFO(B) ((block_info *) (B)->aux)
3284 #undef EDGE_INFO
3285 #define EDGE_INFO(E) ((edge_prob_info *) (E)->aux)
3287 /* Helper function for estimate_bb_frequencies.
3288 Propagate the frequencies in blocks marked in
3289 TOVISIT, starting in HEAD. */
3291 static void
3292 propagate_freq (basic_block head, bitmap tovisit,
3293 sreal max_cyclic_prob)
3295 basic_block bb;
3296 basic_block last;
3297 unsigned i;
3298 edge e;
3299 basic_block nextbb;
3300 bitmap_iterator bi;
3302 /* For each basic block we need to visit count number of his predecessors
3303 we need to visit first. */
3304 EXECUTE_IF_SET_IN_BITMAP (tovisit, 0, i, bi)
3306 edge_iterator ei;
3307 int count = 0;
3309 bb = BASIC_BLOCK_FOR_FN (cfun, i);
3311 FOR_EACH_EDGE (e, ei, bb->preds)
3313 bool visit = bitmap_bit_p (tovisit, e->src->index);
3315 if (visit && !(e->flags & EDGE_DFS_BACK))
3316 count++;
3317 else if (visit && dump_file && !EDGE_INFO (e)->back_edge)
3318 fprintf (dump_file,
3319 "Irreducible region hit, ignoring edge to %i->%i\n",
3320 e->src->index, bb->index);
3322 BLOCK_INFO (bb)->npredecessors = count;
3323 /* When function never returns, we will never process exit block. */
3324 if (!count && bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
3325 bb->count = profile_count::zero ();
3328 BLOCK_INFO (head)->frequency = 1;
3329 last = head;
3330 for (bb = head; bb; bb = nextbb)
3332 edge_iterator ei;
3333 sreal cyclic_probability = 0;
3334 sreal frequency = 0;
3336 nextbb = BLOCK_INFO (bb)->next;
3337 BLOCK_INFO (bb)->next = NULL;
3339 /* Compute frequency of basic block. */
3340 if (bb != head)
3342 if (flag_checking)
3343 FOR_EACH_EDGE (e, ei, bb->preds)
3344 gcc_assert (!bitmap_bit_p (tovisit, e->src->index)
3345 || (e->flags & EDGE_DFS_BACK));
3347 FOR_EACH_EDGE (e, ei, bb->preds)
3348 if (EDGE_INFO (e)->back_edge)
3349 cyclic_probability += EDGE_INFO (e)->back_edge_prob;
3350 else if (!(e->flags & EDGE_DFS_BACK))
3352 /* FIXME: Graphite is producing edges with no profile. Once
3353 this is fixed, drop this. */
3354 sreal tmp = e->probability.initialized_p () ?
3355 e->probability.to_sreal () : 0;
3356 frequency += tmp * BLOCK_INFO (e->src)->frequency;
3359 if (cyclic_probability == 0)
3361 BLOCK_INFO (bb)->frequency = frequency;
3363 else
3365 if (cyclic_probability > max_cyclic_prob)
3367 if (dump_file)
3368 fprintf (dump_file,
3369 "cyclic probability of bb %i is %f (capped to %f)"
3370 "; turning freq %f",
3371 bb->index, cyclic_probability.to_double (),
3372 max_cyclic_prob.to_double (),
3373 frequency.to_double ());
3375 cyclic_probability = max_cyclic_prob;
3377 else if (dump_file)
3378 fprintf (dump_file,
3379 "cyclic probability of bb %i is %f; turning freq %f",
3380 bb->index, cyclic_probability.to_double (),
3381 frequency.to_double ());
3383 BLOCK_INFO (bb)->frequency = frequency
3384 / (sreal (1) - cyclic_probability);
3385 if (dump_file)
3386 fprintf (dump_file, " to %f\n",
3387 BLOCK_INFO (bb)->frequency.to_double ());
3391 bitmap_clear_bit (tovisit, bb->index);
3393 e = find_edge (bb, head);
3394 if (e)
3396 /* FIXME: Graphite is producing edges with no profile. Once
3397 this is fixed, drop this. */
3398 sreal tmp = e->probability.initialized_p () ?
3399 e->probability.to_sreal () : 0;
3400 EDGE_INFO (e)->back_edge_prob = tmp * BLOCK_INFO (bb)->frequency;
3403 /* Propagate to successor blocks. */
3404 FOR_EACH_EDGE (e, ei, bb->succs)
3405 if (!(e->flags & EDGE_DFS_BACK)
3406 && BLOCK_INFO (e->dest)->npredecessors)
3408 BLOCK_INFO (e->dest)->npredecessors--;
3409 if (!BLOCK_INFO (e->dest)->npredecessors)
3411 if (!nextbb)
3412 nextbb = e->dest;
3413 else
3414 BLOCK_INFO (last)->next = e->dest;
3416 last = e->dest;
3422 /* Estimate frequencies in loops at same nest level. */
3424 static void
3425 estimate_loops_at_level (class loop *first_loop, sreal max_cyclic_prob)
3427 class loop *loop;
3429 for (loop = first_loop; loop; loop = loop->next)
3431 edge e;
3432 basic_block *bbs;
3433 unsigned i;
3434 auto_bitmap tovisit;
3436 estimate_loops_at_level (loop->inner, max_cyclic_prob);
3438 /* Find current loop back edge and mark it. */
3439 e = loop_latch_edge (loop);
3440 EDGE_INFO (e)->back_edge = 1;
3442 bbs = get_loop_body (loop);
3443 for (i = 0; i < loop->num_nodes; i++)
3444 bitmap_set_bit (tovisit, bbs[i]->index);
3445 free (bbs);
3446 propagate_freq (loop->header, tovisit, max_cyclic_prob);
3450 /* Propagates frequencies through structure of loops. */
3452 static void
3453 estimate_loops (void)
3455 auto_bitmap tovisit;
3456 basic_block bb;
3457 sreal max_cyclic_prob = (sreal)1
3458 - (sreal)1 / (param_max_predicted_iterations + 1);
3460 /* Start by estimating the frequencies in the loops. */
3461 if (number_of_loops (cfun) > 1)
3462 estimate_loops_at_level (current_loops->tree_root->inner, max_cyclic_prob);
3464 /* Now propagate the frequencies through all the blocks. */
3465 FOR_ALL_BB_FN (bb, cfun)
3467 bitmap_set_bit (tovisit, bb->index);
3469 propagate_freq (ENTRY_BLOCK_PTR_FOR_FN (cfun), tovisit, max_cyclic_prob);
3472 /* Drop the profile for NODE to guessed, and update its frequency based on
3473 whether it is expected to be hot given the CALL_COUNT. */
3475 static void
3476 drop_profile (struct cgraph_node *node, profile_count call_count)
3478 struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
3479 /* In the case where this was called by another function with a
3480 dropped profile, call_count will be 0. Since there are no
3481 non-zero call counts to this function, we don't know for sure
3482 whether it is hot, and therefore it will be marked normal below. */
3483 bool hot = maybe_hot_count_p (NULL, call_count);
3485 if (dump_file)
3486 fprintf (dump_file,
3487 "Dropping 0 profile for %s. %s based on calls.\n",
3488 node->dump_name (),
3489 hot ? "Function is hot" : "Function is normal");
3490 /* We only expect to miss profiles for functions that are reached
3491 via non-zero call edges in cases where the function may have
3492 been linked from another module or library (COMDATs and extern
3493 templates). See the comments below for handle_missing_profiles.
3494 Also, only warn in cases where the missing counts exceed the
3495 number of training runs. In certain cases with an execv followed
3496 by a no-return call the profile for the no-return call is not
3497 dumped and there can be a mismatch. */
3498 if (!DECL_COMDAT (node->decl) && !DECL_EXTERNAL (node->decl)
3499 && call_count > profile_info->runs)
3501 if (flag_profile_correction)
3503 if (dump_file)
3504 fprintf (dump_file,
3505 "Missing counts for called function %s\n",
3506 node->dump_name ());
3508 else
3509 warning (0, "Missing counts for called function %s",
3510 node->dump_name ());
3513 basic_block bb;
3514 if (opt_for_fn (node->decl, flag_guess_branch_prob))
3516 bool clear_zeros
3517 = !ENTRY_BLOCK_PTR_FOR_FN (fn)->count.nonzero_p ();
3518 FOR_ALL_BB_FN (bb, fn)
3519 if (clear_zeros || !(bb->count == profile_count::zero ()))
3520 bb->count = bb->count.guessed_local ();
3521 fn->cfg->count_max = fn->cfg->count_max.guessed_local ();
3523 else
3525 FOR_ALL_BB_FN (bb, fn)
3526 bb->count = profile_count::uninitialized ();
3527 fn->cfg->count_max = profile_count::uninitialized ();
3530 struct cgraph_edge *e;
3531 for (e = node->callees; e; e = e->next_callee)
3532 e->count = gimple_bb (e->call_stmt)->count;
3533 for (e = node->indirect_calls; e; e = e->next_callee)
3534 e->count = gimple_bb (e->call_stmt)->count;
3535 node->count = ENTRY_BLOCK_PTR_FOR_FN (fn)->count;
3537 profile_status_for_fn (fn)
3538 = (flag_guess_branch_prob ? PROFILE_GUESSED : PROFILE_ABSENT);
3539 node->frequency
3540 = hot ? NODE_FREQUENCY_HOT : NODE_FREQUENCY_NORMAL;
3543 /* In the case of COMDAT routines, multiple object files will contain the same
3544 function and the linker will select one for the binary. In that case
3545 all the other copies from the profile instrument binary will be missing
3546 profile counts. Look for cases where this happened, due to non-zero
3547 call counts going to 0-count functions, and drop the profile to guessed
3548 so that we can use the estimated probabilities and avoid optimizing only
3549 for size.
3551 The other case where the profile may be missing is when the routine
3552 is not going to be emitted to the object file, e.g. for "extern template"
3553 class methods. Those will be marked DECL_EXTERNAL. Emit a warning in
3554 all other cases of non-zero calls to 0-count functions. */
3556 void
3557 handle_missing_profiles (void)
3559 const int unlikely_frac = param_unlikely_bb_count_fraction;
3560 struct cgraph_node *node;
3561 auto_vec<struct cgraph_node *, 64> worklist;
3563 /* See if 0 count function has non-0 count callers. In this case we
3564 lost some profile. Drop its function profile to PROFILE_GUESSED. */
3565 FOR_EACH_DEFINED_FUNCTION (node)
3567 struct cgraph_edge *e;
3568 profile_count call_count = profile_count::zero ();
3569 gcov_type max_tp_first_run = 0;
3570 struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
3572 if (node->count.ipa ().nonzero_p ())
3573 continue;
3574 for (e = node->callers; e; e = e->next_caller)
3575 if (e->count.ipa ().initialized_p () && e->count.ipa () > 0)
3577 call_count = call_count + e->count.ipa ();
3579 if (e->caller->tp_first_run > max_tp_first_run)
3580 max_tp_first_run = e->caller->tp_first_run;
3583 /* If time profile is missing, let assign the maximum that comes from
3584 caller functions. */
3585 if (!node->tp_first_run && max_tp_first_run)
3586 node->tp_first_run = max_tp_first_run + 1;
3588 if (call_count > 0
3589 && fn && fn->cfg
3590 && call_count.apply_scale (unlikely_frac, 1) >= profile_info->runs)
3592 drop_profile (node, call_count);
3593 worklist.safe_push (node);
3597 /* Propagate the profile dropping to other 0-count COMDATs that are
3598 potentially called by COMDATs we already dropped the profile on. */
3599 while (worklist.length () > 0)
3601 struct cgraph_edge *e;
3603 node = worklist.pop ();
3604 for (e = node->callees; e; e = e->next_caller)
3606 struct cgraph_node *callee = e->callee;
3607 struct function *fn = DECL_STRUCT_FUNCTION (callee->decl);
3609 if (!(e->count.ipa () == profile_count::zero ())
3610 && callee->count.ipa ().nonzero_p ())
3611 continue;
3612 if ((DECL_COMDAT (callee->decl) || DECL_EXTERNAL (callee->decl))
3613 && fn && fn->cfg
3614 && profile_status_for_fn (fn) == PROFILE_READ)
3616 drop_profile (node, profile_count::zero ());
3617 worklist.safe_push (callee);
3623 /* Convert counts measured by profile driven feedback to frequencies.
3624 Return nonzero iff there was any nonzero execution count. */
3626 bool
3627 update_max_bb_count (void)
3629 profile_count true_count_max = profile_count::uninitialized ();
3630 basic_block bb;
3632 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
3633 true_count_max = true_count_max.max (bb->count);
3635 cfun->cfg->count_max = true_count_max;
3637 return true_count_max.ipa ().nonzero_p ();
3640 /* Return true if function is likely to be expensive, so there is no point to
3641 optimize performance of prologue, epilogue or do inlining at the expense
3642 of code size growth. THRESHOLD is the limit of number of instructions
3643 function can execute at average to be still considered not expensive. */
3645 bool
3646 expensive_function_p (int threshold)
3648 basic_block bb;
3650 /* If profile was scaled in a way entry block has count 0, then the function
3651 is deifnitly taking a lot of time. */
3652 if (!ENTRY_BLOCK_PTR_FOR_FN (cfun)->count.nonzero_p ())
3653 return true;
3655 profile_count limit = ENTRY_BLOCK_PTR_FOR_FN
3656 (cfun)->count.apply_scale (threshold, 1);
3657 profile_count sum = profile_count::zero ();
3658 FOR_EACH_BB_FN (bb, cfun)
3660 rtx_insn *insn;
3662 if (!bb->count.initialized_p ())
3664 if (dump_file)
3665 fprintf (dump_file, "Function is considered expensive because"
3666 " count of bb %i is not initialized\n", bb->index);
3667 return true;
3670 FOR_BB_INSNS (bb, insn)
3671 if (active_insn_p (insn))
3673 sum += bb->count;
3674 if (sum > limit)
3675 return true;
3679 return false;
3682 /* All basic blocks that are reachable only from unlikely basic blocks are
3683 unlikely. */
3685 void
3686 propagate_unlikely_bbs_forward (void)
3688 auto_vec<basic_block, 64> worklist;
3689 basic_block bb;
3690 edge_iterator ei;
3691 edge e;
3693 if (!(ENTRY_BLOCK_PTR_FOR_FN (cfun)->count == profile_count::zero ()))
3695 ENTRY_BLOCK_PTR_FOR_FN (cfun)->aux = (void *)(size_t) 1;
3696 worklist.safe_push (ENTRY_BLOCK_PTR_FOR_FN (cfun));
3698 while (worklist.length () > 0)
3700 bb = worklist.pop ();
3701 FOR_EACH_EDGE (e, ei, bb->succs)
3702 if (!(e->count () == profile_count::zero ())
3703 && !(e->dest->count == profile_count::zero ())
3704 && !e->dest->aux)
3706 e->dest->aux = (void *)(size_t) 1;
3707 worklist.safe_push (e->dest);
3712 FOR_ALL_BB_FN (bb, cfun)
3714 if (!bb->aux)
3716 if (!(bb->count == profile_count::zero ())
3717 && (dump_file && (dump_flags & TDF_DETAILS)))
3718 fprintf (dump_file,
3719 "Basic block %i is marked unlikely by forward prop\n",
3720 bb->index);
3721 bb->count = profile_count::zero ();
3723 else
3724 bb->aux = NULL;
3728 /* Determine basic blocks/edges that are known to be unlikely executed and set
3729 their counters to zero.
3730 This is done with first identifying obviously unlikely BBs/edges and then
3731 propagating in both directions. */
3733 static void
3734 determine_unlikely_bbs ()
3736 basic_block bb;
3737 auto_vec<basic_block, 64> worklist;
3738 edge_iterator ei;
3739 edge e;
3741 FOR_EACH_BB_FN (bb, cfun)
3743 if (!(bb->count == profile_count::zero ())
3744 && unlikely_executed_bb_p (bb))
3746 if (dump_file && (dump_flags & TDF_DETAILS))
3747 fprintf (dump_file, "Basic block %i is locally unlikely\n",
3748 bb->index);
3749 bb->count = profile_count::zero ();
3752 FOR_EACH_EDGE (e, ei, bb->succs)
3753 if (!(e->probability == profile_probability::never ())
3754 && unlikely_executed_edge_p (e))
3756 if (dump_file && (dump_flags & TDF_DETAILS))
3757 fprintf (dump_file, "Edge %i->%i is locally unlikely\n",
3758 bb->index, e->dest->index);
3759 e->probability = profile_probability::never ();
3762 gcc_checking_assert (!bb->aux);
3764 propagate_unlikely_bbs_forward ();
3766 auto_vec<int, 64> nsuccs;
3767 nsuccs.safe_grow_cleared (last_basic_block_for_fn (cfun));
3768 FOR_ALL_BB_FN (bb, cfun)
3769 if (!(bb->count == profile_count::zero ())
3770 && bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
3772 nsuccs[bb->index] = 0;
3773 FOR_EACH_EDGE (e, ei, bb->succs)
3774 if (!(e->probability == profile_probability::never ())
3775 && !(e->dest->count == profile_count::zero ()))
3776 nsuccs[bb->index]++;
3777 if (!nsuccs[bb->index])
3778 worklist.safe_push (bb);
3780 while (worklist.length () > 0)
3782 bb = worklist.pop ();
3783 if (bb->count == profile_count::zero ())
3784 continue;
3785 if (bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
3787 bool found = false;
3788 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
3789 !gsi_end_p (gsi); gsi_next (&gsi))
3790 if (stmt_can_terminate_bb_p (gsi_stmt (gsi))
3791 /* stmt_can_terminate_bb_p special cases noreturns because it
3792 assumes that fake edges are created. We want to know that
3793 noreturn alone does not imply BB to be unlikely. */
3794 || (is_gimple_call (gsi_stmt (gsi))
3795 && (gimple_call_flags (gsi_stmt (gsi)) & ECF_NORETURN)))
3797 found = true;
3798 break;
3800 if (found)
3801 continue;
3803 if (dump_file && (dump_flags & TDF_DETAILS))
3804 fprintf (dump_file,
3805 "Basic block %i is marked unlikely by backward prop\n",
3806 bb->index);
3807 bb->count = profile_count::zero ();
3808 FOR_EACH_EDGE (e, ei, bb->preds)
3809 if (!(e->probability == profile_probability::never ()))
3811 if (!(e->src->count == profile_count::zero ()))
3813 gcc_checking_assert (nsuccs[e->src->index] > 0);
3814 nsuccs[e->src->index]--;
3815 if (!nsuccs[e->src->index])
3816 worklist.safe_push (e->src);
3820 /* Finally all edges from non-0 regions to 0 are unlikely. */
3821 FOR_ALL_BB_FN (bb, cfun)
3823 if (!(bb->count == profile_count::zero ()))
3824 FOR_EACH_EDGE (e, ei, bb->succs)
3825 if (!(e->probability == profile_probability::never ())
3826 && e->dest->count == profile_count::zero ())
3828 if (dump_file && (dump_flags & TDF_DETAILS))
3829 fprintf (dump_file, "Edge %i->%i is unlikely because "
3830 "it enters unlikely block\n",
3831 bb->index, e->dest->index);
3832 e->probability = profile_probability::never ();
3835 edge other = NULL;
3837 FOR_EACH_EDGE (e, ei, bb->succs)
3838 if (e->probability == profile_probability::never ())
3840 else if (other)
3842 other = NULL;
3843 break;
3845 else
3846 other = e;
3847 if (other
3848 && !(other->probability == profile_probability::always ()))
3850 if (dump_file && (dump_flags & TDF_DETAILS))
3851 fprintf (dump_file, "Edge %i->%i is locally likely\n",
3852 bb->index, other->dest->index);
3853 other->probability = profile_probability::always ();
3856 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->count == profile_count::zero ())
3857 cgraph_node::get (current_function_decl)->count = profile_count::zero ();
3860 /* Estimate and propagate basic block frequencies using the given branch
3861 probabilities. If FORCE is true, the frequencies are used to estimate
3862 the counts even when there are already non-zero profile counts. */
3864 void
3865 estimate_bb_frequencies (bool force)
3867 basic_block bb;
3868 sreal freq_max;
3870 determine_unlikely_bbs ();
3872 if (force || profile_status_for_fn (cfun) != PROFILE_READ
3873 || !update_max_bb_count ())
3876 mark_dfs_back_edges ();
3878 single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun))->probability =
3879 profile_probability::always ();
3881 /* Set up block info for each basic block. */
3882 alloc_aux_for_blocks (sizeof (block_info));
3883 alloc_aux_for_edges (sizeof (edge_prob_info));
3884 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
3886 edge e;
3887 edge_iterator ei;
3889 FOR_EACH_EDGE (e, ei, bb->succs)
3891 /* FIXME: Graphite is producing edges with no profile. Once
3892 this is fixed, drop this. */
3893 if (e->probability.initialized_p ())
3894 EDGE_INFO (e)->back_edge_prob
3895 = e->probability.to_sreal ();
3896 else
3897 /* back_edge_prob = 0.5 */
3898 EDGE_INFO (e)->back_edge_prob = sreal (1, -1);
3902 /* First compute frequencies locally for each loop from innermost
3903 to outermost to examine frequencies for back edges. */
3904 estimate_loops ();
3906 freq_max = 0;
3907 FOR_EACH_BB_FN (bb, cfun)
3908 if (freq_max < BLOCK_INFO (bb)->frequency)
3909 freq_max = BLOCK_INFO (bb)->frequency;
3911 /* Scaling frequencies up to maximal profile count may result in
3912 frequent overflows especially when inlining loops.
3913 Small scalling results in unnecesary precision loss. Stay in
3914 the half of the (exponential) range. */
3915 freq_max = (sreal (1) << (profile_count::n_bits / 2)) / freq_max;
3916 if (freq_max < 16)
3917 freq_max = 16;
3918 profile_count ipa_count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count.ipa ();
3919 cfun->cfg->count_max = profile_count::uninitialized ();
3920 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
3922 sreal tmp = BLOCK_INFO (bb)->frequency;
3923 if (tmp >= 1)
3925 gimple_stmt_iterator gsi;
3926 tree decl;
3928 /* Self recursive calls can not have frequency greater than 1
3929 or program will never terminate. This will result in an
3930 inconsistent bb profile but it is better than greatly confusing
3931 IPA cost metrics. */
3932 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3933 if (is_gimple_call (gsi_stmt (gsi))
3934 && (decl = gimple_call_fndecl (gsi_stmt (gsi))) != NULL
3935 && recursive_call_p (current_function_decl, decl))
3937 if (dump_file)
3938 fprintf (dump_file, "Dropping frequency of recursive call"
3939 " in bb %i from %f\n", bb->index,
3940 tmp.to_double ());
3941 tmp = (sreal)9 / (sreal)10;
3942 break;
3945 tmp = tmp * freq_max + sreal (1, -1);
3946 profile_count count = profile_count::from_gcov_type (tmp.to_int ());
3948 /* If we have profile feedback in which this function was never
3949 executed, then preserve this info. */
3950 if (!(bb->count == profile_count::zero ()))
3951 bb->count = count.guessed_local ().combine_with_ipa_count (ipa_count);
3952 cfun->cfg->count_max = cfun->cfg->count_max.max (bb->count);
3955 free_aux_for_blocks ();
3956 free_aux_for_edges ();
3958 compute_function_frequency ();
3961 /* Decide whether function is hot, cold or unlikely executed. */
3962 void
3963 compute_function_frequency (void)
3965 basic_block bb;
3966 struct cgraph_node *node = cgraph_node::get (current_function_decl);
3968 if (DECL_STATIC_CONSTRUCTOR (current_function_decl)
3969 || MAIN_NAME_P (DECL_NAME (current_function_decl)))
3970 node->only_called_at_startup = true;
3971 if (DECL_STATIC_DESTRUCTOR (current_function_decl))
3972 node->only_called_at_exit = true;
3974 if (!ENTRY_BLOCK_PTR_FOR_FN (cfun)->count.ipa_p ())
3976 int flags = flags_from_decl_or_type (current_function_decl);
3977 if (lookup_attribute ("cold", DECL_ATTRIBUTES (current_function_decl))
3978 != NULL)
3979 node->frequency = NODE_FREQUENCY_UNLIKELY_EXECUTED;
3980 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (current_function_decl))
3981 != NULL)
3982 node->frequency = NODE_FREQUENCY_HOT;
3983 else if (flags & ECF_NORETURN)
3984 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
3985 else if (MAIN_NAME_P (DECL_NAME (current_function_decl)))
3986 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
3987 else if (DECL_STATIC_CONSTRUCTOR (current_function_decl)
3988 || DECL_STATIC_DESTRUCTOR (current_function_decl))
3989 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
3990 return;
3993 node->frequency = NODE_FREQUENCY_UNLIKELY_EXECUTED;
3994 warn_function_cold (current_function_decl);
3995 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->count.ipa() == profile_count::zero ())
3996 return;
3997 FOR_EACH_BB_FN (bb, cfun)
3999 if (maybe_hot_bb_p (cfun, bb))
4001 node->frequency = NODE_FREQUENCY_HOT;
4002 return;
4004 if (!probably_never_executed_bb_p (cfun, bb))
4005 node->frequency = NODE_FREQUENCY_NORMAL;
4009 /* Build PREDICT_EXPR. */
4010 tree
4011 build_predict_expr (enum br_predictor predictor, enum prediction taken)
4013 tree t = build1 (PREDICT_EXPR, void_type_node,
4014 build_int_cst (integer_type_node, predictor));
4015 SET_PREDICT_EXPR_OUTCOME (t, taken);
4016 return t;
4019 const char *
4020 predictor_name (enum br_predictor predictor)
4022 return predictor_info[predictor].name;
4025 /* Predict branch probabilities and estimate profile of the tree CFG. */
4027 namespace {
4029 const pass_data pass_data_profile =
4031 GIMPLE_PASS, /* type */
4032 "profile_estimate", /* name */
4033 OPTGROUP_NONE, /* optinfo_flags */
4034 TV_BRANCH_PROB, /* tv_id */
4035 PROP_cfg, /* properties_required */
4036 0, /* properties_provided */
4037 0, /* properties_destroyed */
4038 0, /* todo_flags_start */
4039 0, /* todo_flags_finish */
4042 class pass_profile : public gimple_opt_pass
4044 public:
4045 pass_profile (gcc::context *ctxt)
4046 : gimple_opt_pass (pass_data_profile, ctxt)
4049 /* opt_pass methods: */
4050 virtual bool gate (function *) { return flag_guess_branch_prob; }
4051 virtual unsigned int execute (function *);
4053 }; // class pass_profile
4055 unsigned int
4056 pass_profile::execute (function *fun)
4058 unsigned nb_loops;
4060 if (profile_status_for_fn (cfun) == PROFILE_GUESSED)
4061 return 0;
4063 loop_optimizer_init (LOOPS_NORMAL);
4064 if (dump_file && (dump_flags & TDF_DETAILS))
4065 flow_loops_dump (dump_file, NULL, 0);
4067 mark_irreducible_loops ();
4069 nb_loops = number_of_loops (fun);
4070 if (nb_loops > 1)
4071 scev_initialize ();
4073 tree_estimate_probability (false);
4075 if (nb_loops > 1)
4076 scev_finalize ();
4078 loop_optimizer_finalize ();
4079 if (dump_file && (dump_flags & TDF_DETAILS))
4080 gimple_dump_cfg (dump_file, dump_flags);
4081 if (profile_status_for_fn (fun) == PROFILE_ABSENT)
4082 profile_status_for_fn (fun) = PROFILE_GUESSED;
4083 if (dump_file && (dump_flags & TDF_DETAILS))
4085 class loop *loop;
4086 FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
4087 if (loop->header->count.initialized_p ())
4088 fprintf (dump_file, "Loop got predicted %d to iterate %i times.\n",
4089 loop->num,
4090 (int)expected_loop_iterations_unbounded (loop));
4092 return 0;
4095 } // anon namespace
4097 gimple_opt_pass *
4098 make_pass_profile (gcc::context *ctxt)
4100 return new pass_profile (ctxt);
4103 /* Return true when PRED predictor should be removed after early
4104 tree passes. Most of the predictors are beneficial to survive
4105 as early inlining can also distribute then into caller's bodies. */
4107 static bool
4108 strip_predictor_early (enum br_predictor pred)
4110 switch (pred)
4112 case PRED_TREE_EARLY_RETURN:
4113 return true;
4114 default:
4115 return false;
4119 /* Get rid of all builtin_expect calls and GIMPLE_PREDICT statements
4120 we no longer need. EARLY is set to true when called from early
4121 optimizations. */
4123 unsigned int
4124 strip_predict_hints (function *fun, bool early)
4126 basic_block bb;
4127 gimple *ass_stmt;
4128 tree var;
4129 bool changed = false;
4131 FOR_EACH_BB_FN (bb, fun)
4133 gimple_stmt_iterator bi;
4134 for (bi = gsi_start_bb (bb); !gsi_end_p (bi);)
4136 gimple *stmt = gsi_stmt (bi);
4138 if (gimple_code (stmt) == GIMPLE_PREDICT)
4140 if (!early
4141 || strip_predictor_early (gimple_predict_predictor (stmt)))
4143 gsi_remove (&bi, true);
4144 changed = true;
4145 continue;
4148 else if (is_gimple_call (stmt))
4150 tree fndecl = gimple_call_fndecl (stmt);
4152 if (!early
4153 && ((fndecl != NULL_TREE
4154 && fndecl_built_in_p (fndecl, BUILT_IN_EXPECT)
4155 && gimple_call_num_args (stmt) == 2)
4156 || (fndecl != NULL_TREE
4157 && fndecl_built_in_p (fndecl,
4158 BUILT_IN_EXPECT_WITH_PROBABILITY)
4159 && gimple_call_num_args (stmt) == 3)
4160 || (gimple_call_internal_p (stmt)
4161 && gimple_call_internal_fn (stmt) == IFN_BUILTIN_EXPECT)))
4163 var = gimple_call_lhs (stmt);
4164 changed = true;
4165 if (var)
4167 ass_stmt
4168 = gimple_build_assign (var, gimple_call_arg (stmt, 0));
4169 gsi_replace (&bi, ass_stmt, true);
4171 else
4173 gsi_remove (&bi, true);
4174 continue;
4178 gsi_next (&bi);
4181 return changed ? TODO_cleanup_cfg : 0;
4184 namespace {
4186 const pass_data pass_data_strip_predict_hints =
4188 GIMPLE_PASS, /* type */
4189 "*strip_predict_hints", /* name */
4190 OPTGROUP_NONE, /* optinfo_flags */
4191 TV_BRANCH_PROB, /* tv_id */
4192 PROP_cfg, /* properties_required */
4193 0, /* properties_provided */
4194 0, /* properties_destroyed */
4195 0, /* todo_flags_start */
4196 0, /* todo_flags_finish */
4199 class pass_strip_predict_hints : public gimple_opt_pass
4201 public:
4202 pass_strip_predict_hints (gcc::context *ctxt)
4203 : gimple_opt_pass (pass_data_strip_predict_hints, ctxt)
4206 /* opt_pass methods: */
4207 opt_pass * clone () { return new pass_strip_predict_hints (m_ctxt); }
4208 void set_pass_param (unsigned int n, bool param)
4210 gcc_assert (n == 0);
4211 early_p = param;
4214 virtual unsigned int execute (function *);
4216 private:
4217 bool early_p;
4219 }; // class pass_strip_predict_hints
4221 unsigned int
4222 pass_strip_predict_hints::execute (function *fun)
4224 return strip_predict_hints (fun, early_p);
4227 } // anon namespace
4229 gimple_opt_pass *
4230 make_pass_strip_predict_hints (gcc::context *ctxt)
4232 return new pass_strip_predict_hints (ctxt);
4235 /* Rebuild function frequencies. Passes are in general expected to
4236 maintain profile by hand, however in some cases this is not possible:
4237 for example when inlining several functions with loops freuqencies might run
4238 out of scale and thus needs to be recomputed. */
4240 void
4241 rebuild_frequencies (void)
4243 timevar_push (TV_REBUILD_FREQUENCIES);
4245 /* When the max bb count in the function is small, there is a higher
4246 chance that there were truncation errors in the integer scaling
4247 of counts by inlining and other optimizations. This could lead
4248 to incorrect classification of code as being cold when it isn't.
4249 In that case, force the estimation of bb counts/frequencies from the
4250 branch probabilities, rather than computing frequencies from counts,
4251 which may also lead to frequencies incorrectly reduced to 0. There
4252 is less precision in the probabilities, so we only do this for small
4253 max counts. */
4254 cfun->cfg->count_max = profile_count::uninitialized ();
4255 basic_block bb;
4256 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
4257 cfun->cfg->count_max = cfun->cfg->count_max.max (bb->count);
4259 if (profile_status_for_fn (cfun) == PROFILE_GUESSED)
4261 loop_optimizer_init (0);
4262 add_noreturn_fake_exit_edges ();
4263 mark_irreducible_loops ();
4264 connect_infinite_loops_to_exit ();
4265 estimate_bb_frequencies (true);
4266 remove_fake_exit_edges ();
4267 loop_optimizer_finalize ();
4269 else if (profile_status_for_fn (cfun) == PROFILE_READ)
4270 update_max_bb_count ();
4271 else if (profile_status_for_fn (cfun) == PROFILE_ABSENT
4272 && !flag_guess_branch_prob)
4274 else
4275 gcc_unreachable ();
4276 timevar_pop (TV_REBUILD_FREQUENCIES);
4279 /* Perform a dry run of the branch prediction pass and report comparsion of
4280 the predicted and real profile into the dump file. */
4282 void
4283 report_predictor_hitrates (void)
4285 unsigned nb_loops;
4287 loop_optimizer_init (LOOPS_NORMAL);
4288 if (dump_file && (dump_flags & TDF_DETAILS))
4289 flow_loops_dump (dump_file, NULL, 0);
4291 mark_irreducible_loops ();
4293 nb_loops = number_of_loops (cfun);
4294 if (nb_loops > 1)
4295 scev_initialize ();
4297 tree_estimate_probability (true);
4299 if (nb_loops > 1)
4300 scev_finalize ();
4302 loop_optimizer_finalize ();
4305 /* Force edge E to be cold.
4306 If IMPOSSIBLE is true, for edge to have count and probability 0 otherwise
4307 keep low probability to represent possible error in a guess. This is used
4308 i.e. in case we predict loop to likely iterate given number of times but
4309 we are not 100% sure.
4311 This function locally updates profile without attempt to keep global
4312 consistency which cannot be reached in full generality without full profile
4313 rebuild from probabilities alone. Doing so is not necessarily a good idea
4314 because frequencies and counts may be more realistic then probabilities.
4316 In some cases (such as for elimination of early exits during full loop
4317 unrolling) the caller can ensure that profile will get consistent
4318 afterwards. */
4320 void
4321 force_edge_cold (edge e, bool impossible)
4323 profile_count count_sum = profile_count::zero ();
4324 profile_probability prob_sum = profile_probability::never ();
4325 edge_iterator ei;
4326 edge e2;
4327 bool uninitialized_exit = false;
4329 /* When branch probability guesses are not known, then do nothing. */
4330 if (!impossible && !e->count ().initialized_p ())
4331 return;
4333 profile_probability goal = (impossible ? profile_probability::never ()
4334 : profile_probability::very_unlikely ());
4336 /* If edge is already improbably or cold, just return. */
4337 if (e->probability <= goal
4338 && (!impossible || e->count () == profile_count::zero ()))
4339 return;
4340 FOR_EACH_EDGE (e2, ei, e->src->succs)
4341 if (e2 != e)
4343 if (e->flags & EDGE_FAKE)
4344 continue;
4345 if (e2->count ().initialized_p ())
4346 count_sum += e2->count ();
4347 if (e2->probability.initialized_p ())
4348 prob_sum += e2->probability;
4349 else
4350 uninitialized_exit = true;
4353 /* If we are not guessing profiles but have some other edges out,
4354 just assume the control flow goes elsewhere. */
4355 if (uninitialized_exit)
4356 e->probability = goal;
4357 /* If there are other edges out of e->src, redistribute probabilitity
4358 there. */
4359 else if (prob_sum > profile_probability::never ())
4361 if (!(e->probability < goal))
4362 e->probability = goal;
4364 profile_probability prob_comp = prob_sum / e->probability.invert ();
4366 if (dump_file && (dump_flags & TDF_DETAILS))
4367 fprintf (dump_file, "Making edge %i->%i %s by redistributing "
4368 "probability to other edges.\n",
4369 e->src->index, e->dest->index,
4370 impossible ? "impossible" : "cold");
4371 FOR_EACH_EDGE (e2, ei, e->src->succs)
4372 if (e2 != e)
4374 e2->probability /= prob_comp;
4376 if (current_ir_type () != IR_GIMPLE
4377 && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
4378 update_br_prob_note (e->src);
4380 /* If all edges out of e->src are unlikely, the basic block itself
4381 is unlikely. */
4382 else
4384 if (prob_sum == profile_probability::never ())
4385 e->probability = profile_probability::always ();
4386 else
4388 if (impossible)
4389 e->probability = profile_probability::never ();
4390 /* If BB has some edges out that are not impossible, we cannot
4391 assume that BB itself is. */
4392 impossible = false;
4394 if (current_ir_type () != IR_GIMPLE
4395 && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
4396 update_br_prob_note (e->src);
4397 if (e->src->count == profile_count::zero ())
4398 return;
4399 if (count_sum == profile_count::zero () && impossible)
4401 bool found = false;
4402 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
4404 else if (current_ir_type () == IR_GIMPLE)
4405 for (gimple_stmt_iterator gsi = gsi_start_bb (e->src);
4406 !gsi_end_p (gsi); gsi_next (&gsi))
4408 if (stmt_can_terminate_bb_p (gsi_stmt (gsi)))
4410 found = true;
4411 break;
4414 /* FIXME: Implement RTL path. */
4415 else
4416 found = true;
4417 if (!found)
4419 if (dump_file && (dump_flags & TDF_DETAILS))
4420 fprintf (dump_file,
4421 "Making bb %i impossible and dropping count to 0.\n",
4422 e->src->index);
4423 e->src->count = profile_count::zero ();
4424 FOR_EACH_EDGE (e2, ei, e->src->preds)
4425 force_edge_cold (e2, impossible);
4426 return;
4430 /* If we did not adjusting, the source basic block has no likely edeges
4431 leaving other direction. In that case force that bb cold, too.
4432 This in general is difficult task to do, but handle special case when
4433 BB has only one predecestor. This is common case when we are updating
4434 after loop transforms. */
4435 if (!(prob_sum > profile_probability::never ())
4436 && count_sum == profile_count::zero ()
4437 && single_pred_p (e->src) && e->src->count.to_frequency (cfun)
4438 > (impossible ? 0 : 1))
4440 int old_frequency = e->src->count.to_frequency (cfun);
4441 if (dump_file && (dump_flags & TDF_DETAILS))
4442 fprintf (dump_file, "Making bb %i %s.\n", e->src->index,
4443 impossible ? "impossible" : "cold");
4444 int new_frequency = MIN (e->src->count.to_frequency (cfun),
4445 impossible ? 0 : 1);
4446 if (impossible)
4447 e->src->count = profile_count::zero ();
4448 else
4449 e->src->count = e->count ().apply_scale (new_frequency,
4450 old_frequency);
4451 force_edge_cold (single_pred_edge (e->src), impossible);
4453 else if (dump_file && (dump_flags & TDF_DETAILS)
4454 && maybe_hot_bb_p (cfun, e->src))
4455 fprintf (dump_file, "Giving up on making bb %i %s.\n", e->src->index,
4456 impossible ? "impossible" : "cold");
4460 #if CHECKING_P
4462 namespace selftest {
4464 /* Test that value range of predictor values defined in predict.def is
4465 within range (50, 100]. */
4467 struct branch_predictor
4469 const char *name;
4470 int probability;
4473 #define DEF_PREDICTOR(ENUM, NAME, HITRATE, FLAGS) { NAME, HITRATE },
4475 static void
4476 test_prediction_value_range ()
4478 branch_predictor predictors[] = {
4479 #include "predict.def"
4480 { NULL, PROB_UNINITIALIZED }
4483 for (unsigned i = 0; predictors[i].name != NULL; i++)
4485 if (predictors[i].probability == PROB_UNINITIALIZED)
4486 continue;
4488 unsigned p = 100 * predictors[i].probability / REG_BR_PROB_BASE;
4489 ASSERT_TRUE (p >= 50 && p <= 100);
4493 #undef DEF_PREDICTOR
4495 /* Run all of the selfests within this file. */
4497 void
4498 predict_c_tests ()
4500 test_prediction_value_range ();
4503 } // namespace selftest
4504 #endif /* CHECKING_P. */