c++, tree: declare some basic functions inline
[official-gcc.git] / gcc / predict.cc
blob4a686b08f36c0393715ce4cb55845a0411636b65
1 /* Branch prediction routines for the GNU compiler.
2 Copyright (C) 2000-2023 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 * param_hot_bb_frequency_fraction
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 * unlikely_frac >= 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->src->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 optimize_size_level
264 optimize_function_for_size_p (struct function *fun)
266 if (!fun || !fun->decl)
267 return optimize_size ? OPTIMIZE_SIZE_MAX : OPTIMIZE_SIZE_NO;
268 cgraph_node *n = cgraph_node::get (fun->decl);
269 if (n)
270 return n->optimize_for_size_p ();
271 return OPTIMIZE_SIZE_NO;
274 /* Return true if function FUN should always be optimized for speed. */
276 bool
277 optimize_function_for_speed_p (struct function *fun)
279 return !optimize_function_for_size_p (fun);
282 /* Return the optimization type that should be used for function FUN. */
284 optimization_type
285 function_optimization_type (struct function *fun)
287 return (optimize_function_for_speed_p (fun)
288 ? OPTIMIZE_FOR_SPEED
289 : OPTIMIZE_FOR_SIZE);
292 /* Return TRUE if basic block BB should be optimized for size. */
294 optimize_size_level
295 optimize_bb_for_size_p (const_basic_block bb)
297 enum optimize_size_level ret = optimize_function_for_size_p (cfun);
299 if (bb && ret < OPTIMIZE_SIZE_MAX && bb->count == profile_count::zero ())
300 ret = OPTIMIZE_SIZE_MAX;
301 if (bb && ret < OPTIMIZE_SIZE_BALANCED && !maybe_hot_bb_p (cfun, bb))
302 ret = OPTIMIZE_SIZE_BALANCED;
303 return ret;
306 /* Return TRUE if basic block BB should be optimized for speed. */
308 bool
309 optimize_bb_for_speed_p (const_basic_block bb)
311 return !optimize_bb_for_size_p (bb);
314 /* Return the optimization type that should be used for basic block BB. */
316 optimization_type
317 bb_optimization_type (const_basic_block bb)
319 return (optimize_bb_for_speed_p (bb)
320 ? OPTIMIZE_FOR_SPEED
321 : OPTIMIZE_FOR_SIZE);
324 /* Return TRUE if edge E should be optimized for size. */
326 optimize_size_level
327 optimize_edge_for_size_p (edge e)
329 enum optimize_size_level ret = optimize_function_for_size_p (cfun);
331 if (ret < OPTIMIZE_SIZE_MAX && unlikely_executed_edge_p (e))
332 ret = OPTIMIZE_SIZE_MAX;
333 if (ret < OPTIMIZE_SIZE_BALANCED && !maybe_hot_edge_p (e))
334 ret = OPTIMIZE_SIZE_BALANCED;
335 return ret;
338 /* Return TRUE if edge E should be optimized for speed. */
340 bool
341 optimize_edge_for_speed_p (edge e)
343 return !optimize_edge_for_size_p (e);
346 /* Return TRUE if the current function is optimized for size. */
348 optimize_size_level
349 optimize_insn_for_size_p (void)
351 enum optimize_size_level ret = optimize_function_for_size_p (cfun);
352 if (ret < OPTIMIZE_SIZE_BALANCED && !crtl->maybe_hot_insn_p)
353 ret = OPTIMIZE_SIZE_BALANCED;
354 return ret;
357 /* Return TRUE if the current function is optimized for speed. */
359 bool
360 optimize_insn_for_speed_p (void)
362 return !optimize_insn_for_size_p ();
365 /* Return the optimization type that should be used for the current
366 instruction. */
368 optimization_type
369 insn_optimization_type ()
371 return (optimize_insn_for_speed_p ()
372 ? OPTIMIZE_FOR_SPEED
373 : OPTIMIZE_FOR_SIZE);
376 /* Return TRUE if LOOP should be optimized for size. */
378 optimize_size_level
379 optimize_loop_for_size_p (class loop *loop)
381 return optimize_bb_for_size_p (loop->header);
384 /* Return TRUE if LOOP should be optimized for speed. */
386 bool
387 optimize_loop_for_speed_p (class loop *loop)
389 return optimize_bb_for_speed_p (loop->header);
392 /* Return TRUE if nest rooted at LOOP should be optimized for speed. */
394 bool
395 optimize_loop_nest_for_speed_p (class loop *loop)
397 class loop *l = loop;
398 if (optimize_loop_for_speed_p (loop))
399 return true;
400 l = loop->inner;
401 while (l && l != loop)
403 if (optimize_loop_for_speed_p (l))
404 return true;
405 if (l->inner)
406 l = l->inner;
407 else if (l->next)
408 l = l->next;
409 else
411 while (l != loop && !l->next)
412 l = loop_outer (l);
413 if (l != loop)
414 l = l->next;
417 return false;
420 /* Return TRUE if nest rooted at LOOP should be optimized for size. */
422 optimize_size_level
423 optimize_loop_nest_for_size_p (class loop *loop)
425 enum optimize_size_level ret = optimize_loop_for_size_p (loop);
426 class loop *l = loop;
428 l = loop->inner;
429 while (l && l != loop)
431 if (ret == OPTIMIZE_SIZE_NO)
432 break;
433 ret = MIN (optimize_loop_for_size_p (l), ret);
434 if (l->inner)
435 l = l->inner;
436 else if (l->next)
437 l = l->next;
438 else
440 while (l != loop && !l->next)
441 l = loop_outer (l);
442 if (l != loop)
443 l = l->next;
446 return ret;
449 /* Return true if edge E is likely to be well predictable by branch
450 predictor. */
452 bool
453 predictable_edge_p (edge e)
455 if (!e->probability.initialized_p ())
456 return false;
457 if ((e->probability.to_reg_br_prob_base ()
458 <= param_predictable_branch_outcome * REG_BR_PROB_BASE / 100)
459 || (REG_BR_PROB_BASE - e->probability.to_reg_br_prob_base ()
460 <= param_predictable_branch_outcome * REG_BR_PROB_BASE / 100))
461 return true;
462 return false;
466 /* Set RTL expansion for BB profile. */
468 void
469 rtl_profile_for_bb (basic_block bb)
471 crtl->maybe_hot_insn_p = maybe_hot_bb_p (cfun, bb);
474 /* Set RTL expansion for edge profile. */
476 void
477 rtl_profile_for_edge (edge e)
479 crtl->maybe_hot_insn_p = maybe_hot_edge_p (e);
482 /* Set RTL expansion to default mode (i.e. when profile info is not known). */
483 void
484 default_rtl_profile (void)
486 crtl->maybe_hot_insn_p = true;
489 /* Return true if the one of outgoing edges is already predicted by
490 PREDICTOR. */
492 bool
493 rtl_predicted_by_p (const_basic_block bb, enum br_predictor predictor)
495 rtx note;
496 if (!INSN_P (BB_END (bb)))
497 return false;
498 for (note = REG_NOTES (BB_END (bb)); note; note = XEXP (note, 1))
499 if (REG_NOTE_KIND (note) == REG_BR_PRED
500 && INTVAL (XEXP (XEXP (note, 0), 0)) == (int)predictor)
501 return true;
502 return false;
505 /* Structure representing predictions in tree level. */
507 struct edge_prediction {
508 struct edge_prediction *ep_next;
509 edge ep_edge;
510 enum br_predictor ep_predictor;
511 int ep_probability;
514 /* This map contains for a basic block the list of predictions for the
515 outgoing edges. */
517 static hash_map<const_basic_block, edge_prediction *> *bb_predictions;
519 /* Return true if the one of outgoing edges is already predicted by
520 PREDICTOR. */
522 bool
523 gimple_predicted_by_p (const_basic_block bb, enum br_predictor predictor)
525 struct edge_prediction *i;
526 edge_prediction **preds = bb_predictions->get (bb);
528 if (!preds)
529 return false;
531 for (i = *preds; i; i = i->ep_next)
532 if (i->ep_predictor == predictor)
533 return true;
534 return false;
537 /* Return true if the one of outgoing edges is already predicted by
538 PREDICTOR for edge E predicted as TAKEN. */
540 bool
541 edge_predicted_by_p (edge e, enum br_predictor predictor, bool taken)
543 struct edge_prediction *i;
544 basic_block bb = e->src;
545 edge_prediction **preds = bb_predictions->get (bb);
546 if (!preds)
547 return false;
549 int probability = predictor_info[(int) predictor].hitrate;
551 if (taken != TAKEN)
552 probability = REG_BR_PROB_BASE - probability;
554 for (i = *preds; i; i = i->ep_next)
555 if (i->ep_predictor == predictor
556 && i->ep_edge == e
557 && i->ep_probability == probability)
558 return true;
559 return false;
562 /* Same predicate as above, working on edges. */
563 bool
564 edge_probability_reliable_p (const_edge e)
566 return e->probability.probably_reliable_p ();
569 /* Same predicate as edge_probability_reliable_p, working on notes. */
570 bool
571 br_prob_note_reliable_p (const_rtx note)
573 gcc_assert (REG_NOTE_KIND (note) == REG_BR_PROB);
574 return profile_probability::from_reg_br_prob_note
575 (XINT (note, 0)).probably_reliable_p ();
578 static void
579 predict_insn (rtx_insn *insn, enum br_predictor predictor, int probability)
581 gcc_assert (any_condjump_p (insn));
582 if (!flag_guess_branch_prob)
583 return;
585 add_reg_note (insn, REG_BR_PRED,
586 gen_rtx_CONCAT (VOIDmode,
587 GEN_INT ((int) predictor),
588 GEN_INT ((int) probability)));
591 /* Predict insn by given predictor. */
593 void
594 predict_insn_def (rtx_insn *insn, enum br_predictor predictor,
595 enum prediction taken)
597 int probability = predictor_info[(int) predictor].hitrate;
598 gcc_assert (probability != PROB_UNINITIALIZED);
600 if (taken != TAKEN)
601 probability = REG_BR_PROB_BASE - probability;
603 predict_insn (insn, predictor, probability);
606 /* Predict edge E with given probability if possible. */
608 void
609 rtl_predict_edge (edge e, enum br_predictor predictor, int probability)
611 rtx_insn *last_insn;
612 last_insn = BB_END (e->src);
614 /* We can store the branch prediction information only about
615 conditional jumps. */
616 if (!any_condjump_p (last_insn))
617 return;
619 /* We always store probability of branching. */
620 if (e->flags & EDGE_FALLTHRU)
621 probability = REG_BR_PROB_BASE - probability;
623 predict_insn (last_insn, predictor, probability);
626 /* Predict edge E with the given PROBABILITY. */
627 void
628 gimple_predict_edge (edge e, enum br_predictor predictor, int probability)
630 if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
631 && EDGE_COUNT (e->src->succs) > 1
632 && flag_guess_branch_prob
633 && optimize)
635 struct edge_prediction *i = XNEW (struct edge_prediction);
636 edge_prediction *&preds = bb_predictions->get_or_insert (e->src);
638 i->ep_next = preds;
639 preds = i;
640 i->ep_probability = probability;
641 i->ep_predictor = predictor;
642 i->ep_edge = e;
646 /* Filter edge predictions PREDS by a function FILTER: if FILTER return false
647 the prediction is removed.
648 DATA are passed to the filter function. */
650 static void
651 filter_predictions (edge_prediction **preds,
652 bool (*filter) (edge_prediction *, void *), void *data)
654 if (!bb_predictions)
655 return;
657 if (preds)
659 struct edge_prediction **prediction = preds;
660 struct edge_prediction *next;
662 while (*prediction)
664 if ((*filter) (*prediction, data))
665 prediction = &((*prediction)->ep_next);
666 else
668 next = (*prediction)->ep_next;
669 free (*prediction);
670 *prediction = next;
676 /* Filter function predicate that returns true for a edge predicate P
677 if its edge is equal to DATA. */
679 static bool
680 not_equal_edge_p (edge_prediction *p, void *data)
682 return p->ep_edge != (edge)data;
685 /* Remove all predictions on given basic block that are attached
686 to edge E. */
687 void
688 remove_predictions_associated_with_edge (edge e)
690 if (!bb_predictions)
691 return;
693 edge_prediction **preds = bb_predictions->get (e->src);
694 filter_predictions (preds, not_equal_edge_p, e);
697 /* Clears the list of predictions stored for BB. */
699 static void
700 clear_bb_predictions (basic_block bb)
702 edge_prediction **preds = bb_predictions->get (bb);
703 struct edge_prediction *pred, *next;
705 if (!preds)
706 return;
708 for (pred = *preds; pred; pred = next)
710 next = pred->ep_next;
711 free (pred);
713 *preds = NULL;
716 /* Return true when we can store prediction on insn INSN.
717 At the moment we represent predictions only on conditional
718 jumps, not at computed jump or other complicated cases. */
719 static bool
720 can_predict_insn_p (const rtx_insn *insn)
722 return (JUMP_P (insn)
723 && any_condjump_p (insn)
724 && EDGE_COUNT (BLOCK_FOR_INSN (insn)->succs) >= 2);
727 /* Predict edge E by given predictor if possible. */
729 void
730 predict_edge_def (edge e, enum br_predictor predictor,
731 enum prediction taken)
733 int probability = predictor_info[(int) predictor].hitrate;
735 if (taken != TAKEN)
736 probability = REG_BR_PROB_BASE - probability;
738 predict_edge (e, predictor, probability);
741 /* Invert all branch predictions or probability notes in the INSN. This needs
742 to be done each time we invert the condition used by the jump. */
744 void
745 invert_br_probabilities (rtx insn)
747 rtx note;
749 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
750 if (REG_NOTE_KIND (note) == REG_BR_PROB)
751 XINT (note, 0) = profile_probability::from_reg_br_prob_note
752 (XINT (note, 0)).invert ().to_reg_br_prob_note ();
753 else if (REG_NOTE_KIND (note) == REG_BR_PRED)
754 XEXP (XEXP (note, 0), 1)
755 = GEN_INT (REG_BR_PROB_BASE - INTVAL (XEXP (XEXP (note, 0), 1)));
758 /* Dump information about the branch prediction to the output file. */
760 static void
761 dump_prediction (FILE *file, enum br_predictor predictor, int probability,
762 basic_block bb, enum predictor_reason reason = REASON_NONE,
763 edge ep_edge = NULL)
765 edge e = ep_edge;
766 edge_iterator ei;
768 if (!file)
769 return;
771 if (e == NULL)
772 FOR_EACH_EDGE (e, ei, bb->succs)
773 if (! (e->flags & EDGE_FALLTHRU))
774 break;
776 char edge_info_str[128];
777 if (ep_edge)
778 sprintf (edge_info_str, " of edge %d->%d", ep_edge->src->index,
779 ep_edge->dest->index);
780 else
781 edge_info_str[0] = '\0';
783 fprintf (file, " %s heuristics%s%s: %.2f%%",
784 predictor_info[predictor].name,
785 edge_info_str, reason_messages[reason],
786 probability * 100.0 / REG_BR_PROB_BASE);
788 if (bb->count.initialized_p ())
790 fprintf (file, " exec ");
791 bb->count.dump (file);
792 if (e)
794 fprintf (file, " hit ");
795 e->count ().dump (file);
796 fprintf (file, " (%.1f%%)", e->count ().to_gcov_type() * 100.0
797 / bb->count.to_gcov_type ());
801 fprintf (file, "\n");
803 /* Print output that be easily read by analyze_brprob.py script. We are
804 interested only in counts that are read from GCDA files. */
805 if (dump_file && (dump_flags & TDF_DETAILS)
806 && bb->count.precise_p ()
807 && reason == REASON_NONE)
809 fprintf (file, ";;heuristics;%s;%" PRId64 ";%" PRId64 ";%.1f;\n",
810 predictor_info[predictor].name,
811 bb->count.to_gcov_type (), e->count ().to_gcov_type (),
812 probability * 100.0 / REG_BR_PROB_BASE);
816 /* Return true if STMT is known to be unlikely executed. */
818 static bool
819 unlikely_executed_stmt_p (gimple *stmt)
821 if (!is_gimple_call (stmt))
822 return false;
823 /* NORETURN attribute alone is not strong enough: exit() may be quite
824 likely executed once during program run. */
825 if (gimple_call_fntype (stmt)
826 && lookup_attribute ("cold",
827 TYPE_ATTRIBUTES (gimple_call_fntype (stmt)))
828 && !lookup_attribute ("cold", DECL_ATTRIBUTES (current_function_decl)))
829 return true;
830 tree decl = gimple_call_fndecl (stmt);
831 if (!decl)
832 return false;
833 if (lookup_attribute ("cold", DECL_ATTRIBUTES (decl))
834 && !lookup_attribute ("cold", DECL_ATTRIBUTES (current_function_decl)))
835 return true;
837 cgraph_node *n = cgraph_node::get (decl);
838 if (!n)
839 return false;
841 availability avail;
842 n = n->ultimate_alias_target (&avail);
843 if (avail < AVAIL_AVAILABLE)
844 return false;
845 if (!n->analyzed
846 || n->decl == current_function_decl)
847 return false;
848 return n->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED;
851 /* Return true if BB is unlikely executed. */
853 static bool
854 unlikely_executed_bb_p (basic_block bb)
856 if (bb->count == profile_count::zero ())
857 return true;
858 if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun) || bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
859 return false;
860 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
861 !gsi_end_p (gsi); gsi_next (&gsi))
863 if (unlikely_executed_stmt_p (gsi_stmt (gsi)))
864 return true;
865 if (stmt_can_terminate_bb_p (gsi_stmt (gsi)))
866 return false;
868 return false;
871 /* We cannot predict the probabilities of outgoing edges of bb. Set them
872 evenly and hope for the best. If UNLIKELY_EDGES is not null, distribute
873 even probability for all edges not mentioned in the set. These edges
874 are given PROB_VERY_UNLIKELY probability. Similarly for LIKELY_EDGES,
875 if we have exactly one likely edge, make the other edges predicted
876 as not probable. */
878 static void
879 set_even_probabilities (basic_block bb,
880 hash_set<edge> *unlikely_edges = NULL,
881 hash_set<edge_prediction *> *likely_edges = NULL)
883 unsigned nedges = 0, unlikely_count = 0;
884 edge e = NULL;
885 edge_iterator ei;
886 profile_probability all = profile_probability::always ();
888 FOR_EACH_EDGE (e, ei, bb->succs)
889 if (e->probability.initialized_p ())
890 all -= e->probability;
891 else if (!unlikely_executed_edge_p (e))
893 nedges++;
894 if (unlikely_edges != NULL && unlikely_edges->contains (e))
896 all -= profile_probability::very_unlikely ();
897 unlikely_count++;
901 /* Make the distribution even if all edges are unlikely. */
902 unsigned likely_count = likely_edges ? likely_edges->elements () : 0;
903 if (unlikely_count == nedges)
905 unlikely_edges = NULL;
906 unlikely_count = 0;
909 /* If we have one likely edge, then use its probability and distribute
910 remaining probabilities as even. */
911 if (likely_count == 1)
913 FOR_EACH_EDGE (e, ei, bb->succs)
914 if (e->probability.initialized_p ())
916 else if (!unlikely_executed_edge_p (e))
918 edge_prediction *prediction = *likely_edges->begin ();
919 int p = prediction->ep_probability;
920 profile_probability prob
921 = profile_probability::from_reg_br_prob_base (p);
923 if (prediction->ep_edge == e)
924 e->probability = prob;
925 else if (unlikely_edges != NULL && unlikely_edges->contains (e))
926 e->probability = profile_probability::very_unlikely ();
927 else
929 profile_probability remainder = prob.invert ();
930 remainder -= (profile_probability::very_unlikely ()
931 * unlikely_count);
932 int count = nedges - unlikely_count - 1;
933 gcc_assert (count >= 0);
935 e->probability = remainder / count;
938 else
939 e->probability = profile_probability::never ();
941 else
943 /* Make all unlikely edges unlikely and the rest will have even
944 probability. */
945 unsigned scale = nedges - unlikely_count;
946 FOR_EACH_EDGE (e, ei, bb->succs)
947 if (e->probability.initialized_p ())
949 else if (!unlikely_executed_edge_p (e))
951 if (unlikely_edges != NULL && unlikely_edges->contains (e))
952 e->probability = profile_probability::very_unlikely ();
953 else
954 e->probability = all / scale;
956 else
957 e->probability = profile_probability::never ();
961 /* Add REG_BR_PROB note to JUMP with PROB. */
963 void
964 add_reg_br_prob_note (rtx_insn *jump, profile_probability prob)
966 gcc_checking_assert (JUMP_P (jump) && !find_reg_note (jump, REG_BR_PROB, 0));
967 add_int_reg_note (jump, REG_BR_PROB, prob.to_reg_br_prob_note ());
970 /* Combine all REG_BR_PRED notes into single probability and attach REG_BR_PROB
971 note if not already present. Remove now useless REG_BR_PRED notes. */
973 static void
974 combine_predictions_for_insn (rtx_insn *insn, basic_block bb)
976 rtx prob_note;
977 rtx *pnote;
978 rtx note;
979 int best_probability = PROB_EVEN;
980 enum br_predictor best_predictor = END_PREDICTORS;
981 int combined_probability = REG_BR_PROB_BASE / 2;
982 int d;
983 bool first_match = false;
984 bool found = false;
986 if (!can_predict_insn_p (insn))
988 set_even_probabilities (bb);
989 return;
992 prob_note = find_reg_note (insn, REG_BR_PROB, 0);
993 pnote = &REG_NOTES (insn);
994 if (dump_file)
995 fprintf (dump_file, "Predictions for insn %i bb %i\n", INSN_UID (insn),
996 bb->index);
998 /* We implement "first match" heuristics and use probability guessed
999 by predictor with smallest index. */
1000 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1001 if (REG_NOTE_KIND (note) == REG_BR_PRED)
1003 enum br_predictor predictor = ((enum br_predictor)
1004 INTVAL (XEXP (XEXP (note, 0), 0)));
1005 int probability = INTVAL (XEXP (XEXP (note, 0), 1));
1007 found = true;
1008 if (best_predictor > predictor
1009 && predictor_info[predictor].flags & PRED_FLAG_FIRST_MATCH)
1010 best_probability = probability, best_predictor = predictor;
1012 d = (combined_probability * probability
1013 + (REG_BR_PROB_BASE - combined_probability)
1014 * (REG_BR_PROB_BASE - probability));
1016 /* Use FP math to avoid overflows of 32bit integers. */
1017 if (d == 0)
1018 /* If one probability is 0% and one 100%, avoid division by zero. */
1019 combined_probability = REG_BR_PROB_BASE / 2;
1020 else
1021 combined_probability = (((double) combined_probability) * probability
1022 * REG_BR_PROB_BASE / d + 0.5);
1025 /* Decide which heuristic to use. In case we didn't match anything,
1026 use no_prediction heuristic, in case we did match, use either
1027 first match or Dempster-Shaffer theory depending on the flags. */
1029 if (best_predictor != END_PREDICTORS)
1030 first_match = true;
1032 if (!found)
1033 dump_prediction (dump_file, PRED_NO_PREDICTION,
1034 combined_probability, bb);
1035 else
1037 if (!first_match)
1038 dump_prediction (dump_file, PRED_DS_THEORY, combined_probability,
1039 bb, !first_match ? REASON_NONE : REASON_IGNORED);
1040 else
1041 dump_prediction (dump_file, PRED_FIRST_MATCH, best_probability,
1042 bb, first_match ? REASON_NONE : REASON_IGNORED);
1045 if (first_match)
1046 combined_probability = best_probability;
1047 dump_prediction (dump_file, PRED_COMBINED, combined_probability, bb);
1049 while (*pnote)
1051 if (REG_NOTE_KIND (*pnote) == REG_BR_PRED)
1053 enum br_predictor predictor = ((enum br_predictor)
1054 INTVAL (XEXP (XEXP (*pnote, 0), 0)));
1055 int probability = INTVAL (XEXP (XEXP (*pnote, 0), 1));
1057 dump_prediction (dump_file, predictor, probability, bb,
1058 (!first_match || best_predictor == predictor)
1059 ? REASON_NONE : REASON_IGNORED);
1060 *pnote = XEXP (*pnote, 1);
1062 else
1063 pnote = &XEXP (*pnote, 1);
1066 if (!prob_note)
1068 profile_probability p
1069 = profile_probability::from_reg_br_prob_base (combined_probability);
1070 add_reg_br_prob_note (insn, p);
1072 /* Save the prediction into CFG in case we are seeing non-degenerated
1073 conditional jump. */
1074 if (!single_succ_p (bb))
1076 BRANCH_EDGE (bb)->probability = p;
1077 FALLTHRU_EDGE (bb)->probability
1078 = BRANCH_EDGE (bb)->probability.invert ();
1081 else if (!single_succ_p (bb))
1083 profile_probability prob = profile_probability::from_reg_br_prob_note
1084 (XINT (prob_note, 0));
1086 BRANCH_EDGE (bb)->probability = prob;
1087 FALLTHRU_EDGE (bb)->probability = prob.invert ();
1089 else
1090 single_succ_edge (bb)->probability = profile_probability::always ();
1093 /* Edge prediction hash traits. */
1095 struct predictor_hash: pointer_hash <edge_prediction>
1098 static inline hashval_t hash (const edge_prediction *);
1099 static inline bool equal (const edge_prediction *, const edge_prediction *);
1102 /* Calculate hash value of an edge prediction P based on predictor and
1103 normalized probability. */
1105 inline hashval_t
1106 predictor_hash::hash (const edge_prediction *p)
1108 inchash::hash hstate;
1109 hstate.add_int (p->ep_predictor);
1111 int prob = p->ep_probability;
1112 if (prob > REG_BR_PROB_BASE / 2)
1113 prob = REG_BR_PROB_BASE - prob;
1115 hstate.add_int (prob);
1117 return hstate.end ();
1120 /* Return true whether edge predictions P1 and P2 use the same predictor and
1121 have equal (or opposed probability). */
1123 inline bool
1124 predictor_hash::equal (const edge_prediction *p1, const edge_prediction *p2)
1126 return (p1->ep_predictor == p2->ep_predictor
1127 && (p1->ep_probability == p2->ep_probability
1128 || p1->ep_probability == REG_BR_PROB_BASE - p2->ep_probability));
1131 struct predictor_hash_traits: predictor_hash,
1132 typed_noop_remove <edge_prediction *> {};
1134 /* Return true if edge prediction P is not in DATA hash set. */
1136 static bool
1137 not_removed_prediction_p (edge_prediction *p, void *data)
1139 hash_set<edge_prediction *> *remove = (hash_set<edge_prediction *> *) data;
1140 return !remove->contains (p);
1143 /* Prune predictions for a basic block BB. Currently we do following
1144 clean-up steps:
1146 1) remove duplicate prediction that is guessed with the same probability
1147 (different than 1/2) to both edge
1148 2) remove duplicates for a prediction that belongs with the same probability
1149 to a single edge
1153 static void
1154 prune_predictions_for_bb (basic_block bb)
1156 edge_prediction **preds = bb_predictions->get (bb);
1158 if (preds)
1160 hash_table <predictor_hash_traits> s (13);
1161 hash_set <edge_prediction *> remove;
1163 /* Step 1: identify predictors that should be removed. */
1164 for (edge_prediction *pred = *preds; pred; pred = pred->ep_next)
1166 edge_prediction *existing = s.find (pred);
1167 if (existing)
1169 if (pred->ep_edge == existing->ep_edge
1170 && pred->ep_probability == existing->ep_probability)
1172 /* Remove a duplicate predictor. */
1173 dump_prediction (dump_file, pred->ep_predictor,
1174 pred->ep_probability, bb,
1175 REASON_SINGLE_EDGE_DUPLICATE, pred->ep_edge);
1177 remove.add (pred);
1179 else if (pred->ep_edge != existing->ep_edge
1180 && pred->ep_probability == existing->ep_probability
1181 && pred->ep_probability != REG_BR_PROB_BASE / 2)
1183 /* Remove both predictors as they predict the same
1184 for both edges. */
1185 dump_prediction (dump_file, existing->ep_predictor,
1186 pred->ep_probability, bb,
1187 REASON_EDGE_PAIR_DUPLICATE,
1188 existing->ep_edge);
1189 dump_prediction (dump_file, pred->ep_predictor,
1190 pred->ep_probability, bb,
1191 REASON_EDGE_PAIR_DUPLICATE,
1192 pred->ep_edge);
1194 remove.add (existing);
1195 remove.add (pred);
1199 edge_prediction **slot2 = s.find_slot (pred, INSERT);
1200 *slot2 = pred;
1203 /* Step 2: Remove predictors. */
1204 filter_predictions (preds, not_removed_prediction_p, &remove);
1208 /* Combine predictions into single probability and store them into CFG.
1209 Remove now useless prediction entries.
1210 If DRY_RUN is set, only produce dumps and do not modify profile. */
1212 static void
1213 combine_predictions_for_bb (basic_block bb, bool dry_run)
1215 int best_probability = PROB_EVEN;
1216 enum br_predictor best_predictor = END_PREDICTORS;
1217 int combined_probability = REG_BR_PROB_BASE / 2;
1218 int d;
1219 bool first_match = false;
1220 bool found = false;
1221 struct edge_prediction *pred;
1222 int nedges = 0;
1223 edge e, first = NULL, second = NULL;
1224 edge_iterator ei;
1225 int nzero = 0;
1226 int nunknown = 0;
1228 FOR_EACH_EDGE (e, ei, bb->succs)
1230 if (!unlikely_executed_edge_p (e))
1232 nedges ++;
1233 if (first && !second)
1234 second = e;
1235 if (!first)
1236 first = e;
1238 else if (!e->probability.initialized_p ())
1239 e->probability = profile_probability::never ();
1240 if (!e->probability.initialized_p ())
1241 nunknown++;
1242 else if (e->probability == profile_probability::never ())
1243 nzero++;
1246 /* When there is no successor or only one choice, prediction is easy.
1248 When we have a basic block with more than 2 successors, the situation
1249 is more complicated as DS theory cannot be used literally.
1250 More precisely, let's assume we predicted edge e1 with probability p1,
1251 thus: m1({b1}) = p1. As we're going to combine more than 2 edges, we
1252 need to find probability of e.g. m1({b2}), which we don't know.
1253 The only approximation is to equally distribute 1-p1 to all edges
1254 different from b1.
1256 According to numbers we've got from SPEC2006 benchark, there's only
1257 one interesting reliable predictor (noreturn call), which can be
1258 handled with a bit easier approach. */
1259 if (nedges != 2)
1261 hash_set<edge> unlikely_edges (4);
1262 hash_set<edge_prediction *> likely_edges (4);
1264 /* Identify all edges that have a probability close to very unlikely.
1265 Doing the approach for very unlikely doesn't worth for doing as
1266 there's no such probability in SPEC2006 benchmark. */
1267 edge_prediction **preds = bb_predictions->get (bb);
1268 if (preds)
1269 for (pred = *preds; pred; pred = pred->ep_next)
1271 if (pred->ep_probability <= PROB_VERY_UNLIKELY
1272 || pred->ep_predictor == PRED_COLD_LABEL)
1273 unlikely_edges.add (pred->ep_edge);
1274 else if (pred->ep_probability >= PROB_VERY_LIKELY
1275 || pred->ep_predictor == PRED_BUILTIN_EXPECT
1276 || pred->ep_predictor == PRED_HOT_LABEL)
1277 likely_edges.add (pred);
1280 /* It can happen that an edge is both in likely_edges and unlikely_edges.
1281 Clear both sets in that situation. */
1282 for (hash_set<edge_prediction *>::iterator it = likely_edges.begin ();
1283 it != likely_edges.end (); ++it)
1284 if (unlikely_edges.contains ((*it)->ep_edge))
1286 likely_edges.empty ();
1287 unlikely_edges.empty ();
1288 break;
1291 if (!dry_run)
1292 set_even_probabilities (bb, &unlikely_edges, &likely_edges);
1293 clear_bb_predictions (bb);
1294 if (dump_file)
1296 fprintf (dump_file, "Predictions for bb %i\n", bb->index);
1297 if (unlikely_edges.is_empty ())
1298 fprintf (dump_file,
1299 "%i edges in bb %i predicted to even probabilities\n",
1300 nedges, bb->index);
1301 else
1303 fprintf (dump_file,
1304 "%i edges in bb %i predicted with some unlikely edges\n",
1305 nedges, bb->index);
1306 FOR_EACH_EDGE (e, ei, bb->succs)
1307 if (!unlikely_executed_edge_p (e))
1308 dump_prediction (dump_file, PRED_COMBINED,
1309 e->probability.to_reg_br_prob_base (), bb, REASON_NONE, e);
1312 return;
1315 if (dump_file)
1316 fprintf (dump_file, "Predictions for bb %i\n", bb->index);
1318 prune_predictions_for_bb (bb);
1320 edge_prediction **preds = bb_predictions->get (bb);
1322 if (preds)
1324 /* We implement "first match" heuristics and use probability guessed
1325 by predictor with smallest index. */
1326 for (pred = *preds; pred; pred = pred->ep_next)
1328 enum br_predictor predictor = pred->ep_predictor;
1329 int probability = pred->ep_probability;
1331 if (pred->ep_edge != first)
1332 probability = REG_BR_PROB_BASE - probability;
1334 found = true;
1335 /* First match heuristics would be widly confused if we predicted
1336 both directions. */
1337 if (best_predictor > predictor
1338 && predictor_info[predictor].flags & PRED_FLAG_FIRST_MATCH)
1340 struct edge_prediction *pred2;
1341 int prob = probability;
1343 for (pred2 = (struct edge_prediction *) *preds;
1344 pred2; pred2 = pred2->ep_next)
1345 if (pred2 != pred && pred2->ep_predictor == pred->ep_predictor)
1347 int probability2 = pred2->ep_probability;
1349 if (pred2->ep_edge != first)
1350 probability2 = REG_BR_PROB_BASE - probability2;
1352 if ((probability < REG_BR_PROB_BASE / 2) !=
1353 (probability2 < REG_BR_PROB_BASE / 2))
1354 break;
1356 /* If the same predictor later gave better result, go for it! */
1357 if ((probability >= REG_BR_PROB_BASE / 2 && (probability2 > probability))
1358 || (probability <= REG_BR_PROB_BASE / 2 && (probability2 < probability)))
1359 prob = probability2;
1361 if (!pred2)
1362 best_probability = prob, best_predictor = predictor;
1365 d = (combined_probability * probability
1366 + (REG_BR_PROB_BASE - combined_probability)
1367 * (REG_BR_PROB_BASE - probability));
1369 /* Use FP math to avoid overflows of 32bit integers. */
1370 if (d == 0)
1371 /* If one probability is 0% and one 100%, avoid division by zero. */
1372 combined_probability = REG_BR_PROB_BASE / 2;
1373 else
1374 combined_probability = (((double) combined_probability)
1375 * probability
1376 * REG_BR_PROB_BASE / d + 0.5);
1380 /* Decide which heuristic to use. In case we didn't match anything,
1381 use no_prediction heuristic, in case we did match, use either
1382 first match or Dempster-Shaffer theory depending on the flags. */
1384 if (best_predictor != END_PREDICTORS)
1385 first_match = true;
1387 if (!found)
1388 dump_prediction (dump_file, PRED_NO_PREDICTION, combined_probability, bb);
1389 else
1391 if (!first_match)
1392 dump_prediction (dump_file, PRED_DS_THEORY, combined_probability, bb,
1393 !first_match ? REASON_NONE : REASON_IGNORED);
1394 else
1395 dump_prediction (dump_file, PRED_FIRST_MATCH, best_probability, bb,
1396 first_match ? REASON_NONE : REASON_IGNORED);
1399 if (first_match)
1400 combined_probability = best_probability;
1401 dump_prediction (dump_file, PRED_COMBINED, combined_probability, bb);
1403 if (preds)
1405 for (pred = (struct edge_prediction *) *preds; pred; pred = pred->ep_next)
1407 enum br_predictor predictor = pred->ep_predictor;
1408 int probability = pred->ep_probability;
1410 dump_prediction (dump_file, predictor, probability, bb,
1411 (!first_match || best_predictor == predictor)
1412 ? REASON_NONE : REASON_IGNORED, pred->ep_edge);
1415 clear_bb_predictions (bb);
1418 /* If we have only one successor which is unknown, we can compute missing
1419 probability. */
1420 if (nunknown == 1)
1422 profile_probability prob = profile_probability::always ();
1423 edge missing = NULL;
1425 FOR_EACH_EDGE (e, ei, bb->succs)
1426 if (e->probability.initialized_p ())
1427 prob -= e->probability;
1428 else if (missing == NULL)
1429 missing = e;
1430 else
1431 gcc_unreachable ();
1432 missing->probability = prob;
1434 /* If nothing is unknown, we have nothing to update. */
1435 else if (!nunknown && nzero != (int)EDGE_COUNT (bb->succs))
1437 else if (!dry_run)
1439 first->probability
1440 = profile_probability::from_reg_br_prob_base (combined_probability);
1441 second->probability = first->probability.invert ();
1445 /* Check if T1 and T2 satisfy the IV_COMPARE condition.
1446 Return the SSA_NAME if the condition satisfies, NULL otherwise.
1448 T1 and T2 should be one of the following cases:
1449 1. T1 is SSA_NAME, T2 is NULL
1450 2. T1 is SSA_NAME, T2 is INTEGER_CST between [-4, 4]
1451 3. T2 is SSA_NAME, T1 is INTEGER_CST between [-4, 4] */
1453 static tree
1454 strips_small_constant (tree t1, tree t2)
1456 tree ret = NULL;
1457 int value = 0;
1459 if (!t1)
1460 return NULL;
1461 else if (TREE_CODE (t1) == SSA_NAME)
1462 ret = t1;
1463 else if (tree_fits_shwi_p (t1))
1464 value = tree_to_shwi (t1);
1465 else
1466 return NULL;
1468 if (!t2)
1469 return ret;
1470 else if (tree_fits_shwi_p (t2))
1471 value = tree_to_shwi (t2);
1472 else if (TREE_CODE (t2) == SSA_NAME)
1474 if (ret)
1475 return NULL;
1476 else
1477 ret = t2;
1480 if (value <= 4 && value >= -4)
1481 return ret;
1482 else
1483 return NULL;
1486 /* Return the SSA_NAME in T or T's operands.
1487 Return NULL if SSA_NAME cannot be found. */
1489 static tree
1490 get_base_value (tree t)
1492 if (TREE_CODE (t) == SSA_NAME)
1493 return t;
1495 if (!BINARY_CLASS_P (t))
1496 return NULL;
1498 switch (TREE_OPERAND_LENGTH (t))
1500 case 1:
1501 return strips_small_constant (TREE_OPERAND (t, 0), NULL);
1502 case 2:
1503 return strips_small_constant (TREE_OPERAND (t, 0),
1504 TREE_OPERAND (t, 1));
1505 default:
1506 return NULL;
1510 /* Check the compare STMT in LOOP. If it compares an induction
1511 variable to a loop invariant, return true, and save
1512 LOOP_INVARIANT, COMPARE_CODE and LOOP_STEP.
1513 Otherwise return false and set LOOP_INVAIANT to NULL. */
1515 static bool
1516 is_comparison_with_loop_invariant_p (gcond *stmt, class loop *loop,
1517 tree *loop_invariant,
1518 enum tree_code *compare_code,
1519 tree *loop_step,
1520 tree *loop_iv_base)
1522 tree op0, op1, bound, base;
1523 affine_iv iv0, iv1;
1524 enum tree_code code;
1525 tree step;
1527 code = gimple_cond_code (stmt);
1528 *loop_invariant = NULL;
1530 switch (code)
1532 case GT_EXPR:
1533 case GE_EXPR:
1534 case NE_EXPR:
1535 case LT_EXPR:
1536 case LE_EXPR:
1537 case EQ_EXPR:
1538 break;
1540 default:
1541 return false;
1544 op0 = gimple_cond_lhs (stmt);
1545 op1 = gimple_cond_rhs (stmt);
1547 if ((TREE_CODE (op0) != SSA_NAME && TREE_CODE (op0) != INTEGER_CST)
1548 || (TREE_CODE (op1) != SSA_NAME && TREE_CODE (op1) != INTEGER_CST))
1549 return false;
1550 if (!simple_iv (loop, loop_containing_stmt (stmt), op0, &iv0, true))
1551 return false;
1552 if (!simple_iv (loop, loop_containing_stmt (stmt), op1, &iv1, true))
1553 return false;
1554 if (TREE_CODE (iv0.step) != INTEGER_CST
1555 || TREE_CODE (iv1.step) != INTEGER_CST)
1556 return false;
1557 if ((integer_zerop (iv0.step) && integer_zerop (iv1.step))
1558 || (!integer_zerop (iv0.step) && !integer_zerop (iv1.step)))
1559 return false;
1561 if (integer_zerop (iv0.step))
1563 if (code != NE_EXPR && code != EQ_EXPR)
1564 code = invert_tree_comparison (code, false);
1565 bound = iv0.base;
1566 base = iv1.base;
1567 if (tree_fits_shwi_p (iv1.step))
1568 step = iv1.step;
1569 else
1570 return false;
1572 else
1574 bound = iv1.base;
1575 base = iv0.base;
1576 if (tree_fits_shwi_p (iv0.step))
1577 step = iv0.step;
1578 else
1579 return false;
1582 if (TREE_CODE (bound) != INTEGER_CST)
1583 bound = get_base_value (bound);
1584 if (!bound)
1585 return false;
1586 if (TREE_CODE (base) != INTEGER_CST)
1587 base = get_base_value (base);
1588 if (!base)
1589 return false;
1591 *loop_invariant = bound;
1592 *compare_code = code;
1593 *loop_step = step;
1594 *loop_iv_base = base;
1595 return true;
1598 /* Compare two SSA_NAMEs: returns TRUE if T1 and T2 are value coherent. */
1600 static bool
1601 expr_coherent_p (tree t1, tree t2)
1603 gimple *stmt;
1604 tree ssa_name_1 = NULL;
1605 tree ssa_name_2 = NULL;
1607 gcc_assert (TREE_CODE (t1) == SSA_NAME || TREE_CODE (t1) == INTEGER_CST);
1608 gcc_assert (TREE_CODE (t2) == SSA_NAME || TREE_CODE (t2) == INTEGER_CST);
1610 if (t1 == t2)
1611 return true;
1613 if (TREE_CODE (t1) == INTEGER_CST && TREE_CODE (t2) == INTEGER_CST)
1614 return true;
1615 if (TREE_CODE (t1) == INTEGER_CST || TREE_CODE (t2) == INTEGER_CST)
1616 return false;
1618 /* Check to see if t1 is expressed/defined with t2. */
1619 stmt = SSA_NAME_DEF_STMT (t1);
1620 gcc_assert (stmt != NULL);
1621 if (is_gimple_assign (stmt))
1623 ssa_name_1 = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_USE);
1624 if (ssa_name_1 && ssa_name_1 == t2)
1625 return true;
1628 /* Check to see if t2 is expressed/defined with t1. */
1629 stmt = SSA_NAME_DEF_STMT (t2);
1630 gcc_assert (stmt != NULL);
1631 if (is_gimple_assign (stmt))
1633 ssa_name_2 = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_USE);
1634 if (ssa_name_2 && ssa_name_2 == t1)
1635 return true;
1638 /* Compare if t1 and t2's def_stmts are identical. */
1639 if (ssa_name_2 != NULL && ssa_name_1 == ssa_name_2)
1640 return true;
1641 else
1642 return false;
1645 /* Return true if E is predicted by one of loop heuristics. */
1647 static bool
1648 predicted_by_loop_heuristics_p (basic_block bb)
1650 struct edge_prediction *i;
1651 edge_prediction **preds = bb_predictions->get (bb);
1653 if (!preds)
1654 return false;
1656 for (i = *preds; i; i = i->ep_next)
1657 if (i->ep_predictor == PRED_LOOP_ITERATIONS_GUESSED
1658 || i->ep_predictor == PRED_LOOP_ITERATIONS_MAX
1659 || i->ep_predictor == PRED_LOOP_ITERATIONS
1660 || i->ep_predictor == PRED_LOOP_EXIT
1661 || i->ep_predictor == PRED_LOOP_EXIT_WITH_RECURSION
1662 || i->ep_predictor == PRED_LOOP_EXTRA_EXIT)
1663 return true;
1664 return false;
1667 /* Predict branch probability of BB when BB contains a branch that compares
1668 an induction variable in LOOP with LOOP_IV_BASE_VAR to LOOP_BOUND_VAR. The
1669 loop exit is compared using LOOP_BOUND_CODE, with step of LOOP_BOUND_STEP.
1671 E.g.
1672 for (int i = 0; i < bound; i++) {
1673 if (i < bound - 2)
1674 computation_1();
1675 else
1676 computation_2();
1679 In this loop, we will predict the branch inside the loop to be taken. */
1681 static void
1682 predict_iv_comparison (class loop *loop, basic_block bb,
1683 tree loop_bound_var,
1684 tree loop_iv_base_var,
1685 enum tree_code loop_bound_code,
1686 int loop_bound_step)
1688 tree compare_var, compare_base;
1689 enum tree_code compare_code;
1690 tree compare_step_var;
1691 edge then_edge;
1692 edge_iterator ei;
1694 if (predicted_by_loop_heuristics_p (bb))
1695 return;
1697 gcond *stmt = safe_dyn_cast <gcond *> (*gsi_last_bb (bb));
1698 if (!stmt)
1699 return;
1700 if (!is_comparison_with_loop_invariant_p (stmt,
1701 loop, &compare_var,
1702 &compare_code,
1703 &compare_step_var,
1704 &compare_base))
1705 return;
1707 /* Find the taken edge. */
1708 FOR_EACH_EDGE (then_edge, ei, bb->succs)
1709 if (then_edge->flags & EDGE_TRUE_VALUE)
1710 break;
1712 /* When comparing an IV to a loop invariant, NE is more likely to be
1713 taken while EQ is more likely to be not-taken. */
1714 if (compare_code == NE_EXPR)
1716 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1717 return;
1719 else if (compare_code == EQ_EXPR)
1721 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, NOT_TAKEN);
1722 return;
1725 if (!expr_coherent_p (loop_iv_base_var, compare_base))
1726 return;
1728 /* If loop bound, base and compare bound are all constants, we can
1729 calculate the probability directly. */
1730 if (tree_fits_shwi_p (loop_bound_var)
1731 && tree_fits_shwi_p (compare_var)
1732 && tree_fits_shwi_p (compare_base))
1734 int probability;
1735 wi::overflow_type overflow;
1736 bool overall_overflow = false;
1737 widest_int compare_count, tem;
1739 /* (loop_bound - base) / compare_step */
1740 tem = wi::sub (wi::to_widest (loop_bound_var),
1741 wi::to_widest (compare_base), SIGNED, &overflow);
1742 overall_overflow |= overflow;
1743 widest_int loop_count = wi::div_trunc (tem,
1744 wi::to_widest (compare_step_var),
1745 SIGNED, &overflow);
1746 overall_overflow |= overflow;
1748 if (!wi::neg_p (wi::to_widest (compare_step_var))
1749 ^ (compare_code == LT_EXPR || compare_code == LE_EXPR))
1751 /* (loop_bound - compare_bound) / compare_step */
1752 tem = wi::sub (wi::to_widest (loop_bound_var),
1753 wi::to_widest (compare_var), SIGNED, &overflow);
1754 overall_overflow |= overflow;
1755 compare_count = wi::div_trunc (tem, wi::to_widest (compare_step_var),
1756 SIGNED, &overflow);
1757 overall_overflow |= overflow;
1759 else
1761 /* (compare_bound - base) / compare_step */
1762 tem = wi::sub (wi::to_widest (compare_var),
1763 wi::to_widest (compare_base), SIGNED, &overflow);
1764 overall_overflow |= overflow;
1765 compare_count = wi::div_trunc (tem, wi::to_widest (compare_step_var),
1766 SIGNED, &overflow);
1767 overall_overflow |= overflow;
1769 if (compare_code == LE_EXPR || compare_code == GE_EXPR)
1770 ++compare_count;
1771 if (loop_bound_code == LE_EXPR || loop_bound_code == GE_EXPR)
1772 ++loop_count;
1773 if (wi::neg_p (compare_count))
1774 compare_count = 0;
1775 if (wi::neg_p (loop_count))
1776 loop_count = 0;
1777 if (loop_count == 0)
1778 probability = 0;
1779 else if (wi::cmps (compare_count, loop_count) == 1)
1780 probability = REG_BR_PROB_BASE;
1781 else
1783 tem = compare_count * REG_BR_PROB_BASE;
1784 tem = wi::udiv_trunc (tem, loop_count);
1785 probability = tem.to_uhwi ();
1788 /* FIXME: The branch prediction seems broken. It has only 20% hitrate. */
1789 if (!overall_overflow)
1790 predict_edge (then_edge, PRED_LOOP_IV_COMPARE, probability);
1792 return;
1795 if (expr_coherent_p (loop_bound_var, compare_var))
1797 if ((loop_bound_code == LT_EXPR || loop_bound_code == LE_EXPR)
1798 && (compare_code == LT_EXPR || compare_code == LE_EXPR))
1799 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1800 else if ((loop_bound_code == GT_EXPR || loop_bound_code == GE_EXPR)
1801 && (compare_code == GT_EXPR || compare_code == GE_EXPR))
1802 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1803 else if (loop_bound_code == NE_EXPR)
1805 /* If the loop backedge condition is "(i != bound)", we do
1806 the comparison based on the step of IV:
1807 * step < 0 : backedge condition is like (i > bound)
1808 * step > 0 : backedge condition is like (i < bound) */
1809 gcc_assert (loop_bound_step != 0);
1810 if (loop_bound_step > 0
1811 && (compare_code == LT_EXPR
1812 || compare_code == LE_EXPR))
1813 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1814 else if (loop_bound_step < 0
1815 && (compare_code == GT_EXPR
1816 || compare_code == GE_EXPR))
1817 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1818 else
1819 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, NOT_TAKEN);
1821 else
1822 /* The branch is predicted not-taken if loop_bound_code is
1823 opposite with compare_code. */
1824 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, NOT_TAKEN);
1826 else if (expr_coherent_p (loop_iv_base_var, compare_var))
1828 /* For cases like:
1829 for (i = s; i < h; i++)
1830 if (i > s + 2) ....
1831 The branch should be predicted taken. */
1832 if (loop_bound_step > 0
1833 && (compare_code == GT_EXPR || compare_code == GE_EXPR))
1834 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1835 else if (loop_bound_step < 0
1836 && (compare_code == LT_EXPR || compare_code == LE_EXPR))
1837 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, TAKEN);
1838 else
1839 predict_edge_def (then_edge, PRED_LOOP_IV_COMPARE_GUESS, NOT_TAKEN);
1843 /* Predict for extra loop exits that will lead to EXIT_EDGE. The extra loop
1844 exits are resulted from short-circuit conditions that will generate an
1845 if_tmp. E.g.:
1847 if (foo() || global > 10)
1848 break;
1850 This will be translated into:
1852 BB3:
1853 loop header...
1854 BB4:
1855 if foo() goto BB6 else goto BB5
1856 BB5:
1857 if global > 10 goto BB6 else goto BB7
1858 BB6:
1859 goto BB7
1860 BB7:
1861 iftmp = (PHI 0(BB5), 1(BB6))
1862 if iftmp == 1 goto BB8 else goto BB3
1863 BB8:
1864 outside of the loop...
1866 The edge BB7->BB8 is loop exit because BB8 is outside of the loop.
1867 From the dataflow, we can infer that BB4->BB6 and BB5->BB6 are also loop
1868 exits. This function takes BB7->BB8 as input, and finds out the extra loop
1869 exits to predict them using PRED_LOOP_EXTRA_EXIT. */
1871 static void
1872 predict_extra_loop_exits (class loop *loop, edge exit_edge)
1874 unsigned i;
1875 bool check_value_one;
1876 gimple *lhs_def_stmt;
1877 gphi *phi_stmt;
1878 tree cmp_rhs, cmp_lhs;
1880 gcond *cmp_stmt = safe_dyn_cast <gcond *> (*gsi_last_bb (exit_edge->src));
1881 if (!cmp_stmt)
1882 return;
1884 cmp_rhs = gimple_cond_rhs (cmp_stmt);
1885 cmp_lhs = gimple_cond_lhs (cmp_stmt);
1886 if (!TREE_CONSTANT (cmp_rhs)
1887 || !(integer_zerop (cmp_rhs) || integer_onep (cmp_rhs)))
1888 return;
1889 if (TREE_CODE (cmp_lhs) != SSA_NAME)
1890 return;
1892 /* If check_value_one is true, only the phi_args with value '1' will lead
1893 to loop exit. Otherwise, only the phi_args with value '0' will lead to
1894 loop exit. */
1895 check_value_one = (((integer_onep (cmp_rhs))
1896 ^ (gimple_cond_code (cmp_stmt) == EQ_EXPR))
1897 ^ ((exit_edge->flags & EDGE_TRUE_VALUE) != 0));
1899 lhs_def_stmt = SSA_NAME_DEF_STMT (cmp_lhs);
1900 if (!lhs_def_stmt)
1901 return;
1903 phi_stmt = dyn_cast <gphi *> (lhs_def_stmt);
1904 if (!phi_stmt)
1905 return;
1907 for (i = 0; i < gimple_phi_num_args (phi_stmt); i++)
1909 edge e1;
1910 edge_iterator ei;
1911 tree val = gimple_phi_arg_def (phi_stmt, i);
1912 edge e = gimple_phi_arg_edge (phi_stmt, i);
1914 if (!TREE_CONSTANT (val) || !(integer_zerop (val) || integer_onep (val)))
1915 continue;
1916 if ((check_value_one ^ integer_onep (val)) == 1)
1917 continue;
1918 if (EDGE_COUNT (e->src->succs) != 1)
1920 predict_paths_leading_to_edge (e, PRED_LOOP_EXTRA_EXIT, NOT_TAKEN,
1921 loop);
1922 continue;
1925 FOR_EACH_EDGE (e1, ei, e->src->preds)
1926 predict_paths_leading_to_edge (e1, PRED_LOOP_EXTRA_EXIT, NOT_TAKEN,
1927 loop);
1932 /* Predict edge probabilities by exploiting loop structure. */
1934 static void
1935 predict_loops (void)
1937 basic_block bb;
1938 hash_set <class loop *> with_recursion(10);
1940 FOR_EACH_BB_FN (bb, cfun)
1942 gimple_stmt_iterator gsi;
1943 tree decl;
1945 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1946 if (is_gimple_call (gsi_stmt (gsi))
1947 && (decl = gimple_call_fndecl (gsi_stmt (gsi))) != NULL
1948 && recursive_call_p (current_function_decl, decl))
1950 class loop *loop = bb->loop_father;
1951 while (loop && !with_recursion.add (loop))
1952 loop = loop_outer (loop);
1956 /* Try to predict out blocks in a loop that are not part of a
1957 natural loop. */
1958 for (auto loop : loops_list (cfun, LI_FROM_INNERMOST))
1960 basic_block bb, *bbs;
1961 unsigned j, n_exits = 0;
1962 class tree_niter_desc niter_desc;
1963 edge ex;
1964 class nb_iter_bound *nb_iter;
1965 enum tree_code loop_bound_code = ERROR_MARK;
1966 tree loop_bound_step = NULL;
1967 tree loop_bound_var = NULL;
1968 tree loop_iv_base = NULL;
1969 gcond *stmt = NULL;
1970 bool recursion = with_recursion.contains (loop);
1972 auto_vec<edge> exits = get_loop_exit_edges (loop);
1973 FOR_EACH_VEC_ELT (exits, j, ex)
1974 if (!unlikely_executed_edge_p (ex) && !(ex->flags & EDGE_ABNORMAL_CALL))
1975 n_exits ++;
1976 if (!n_exits)
1977 continue;
1979 if (dump_file && (dump_flags & TDF_DETAILS))
1980 fprintf (dump_file, "Predicting loop %i%s with %i exits.\n",
1981 loop->num, recursion ? " (with recursion)":"", n_exits);
1982 if (dump_file && (dump_flags & TDF_DETAILS)
1983 && max_loop_iterations_int (loop) >= 0)
1985 fprintf (dump_file,
1986 "Loop %d iterates at most %i times.\n", loop->num,
1987 (int)max_loop_iterations_int (loop));
1989 if (dump_file && (dump_flags & TDF_DETAILS)
1990 && likely_max_loop_iterations_int (loop) >= 0)
1992 fprintf (dump_file, "Loop %d likely iterates at most %i times.\n",
1993 loop->num, (int)likely_max_loop_iterations_int (loop));
1996 FOR_EACH_VEC_ELT (exits, j, ex)
1998 tree niter = NULL;
1999 HOST_WIDE_INT nitercst;
2000 int max = param_max_predicted_iterations;
2001 int probability;
2002 enum br_predictor predictor;
2003 widest_int nit;
2005 if (unlikely_executed_edge_p (ex)
2006 || (ex->flags & EDGE_ABNORMAL_CALL))
2007 continue;
2008 /* Loop heuristics do not expect exit conditional to be inside
2009 inner loop. We predict from innermost to outermost loop. */
2010 if (predicted_by_loop_heuristics_p (ex->src))
2012 if (dump_file && (dump_flags & TDF_DETAILS))
2013 fprintf (dump_file, "Skipping exit %i->%i because "
2014 "it is already predicted.\n",
2015 ex->src->index, ex->dest->index);
2016 continue;
2018 predict_extra_loop_exits (loop, ex);
2020 if (number_of_iterations_exit (loop, ex, &niter_desc, false, false))
2021 niter = niter_desc.niter;
2022 if (!niter || TREE_CODE (niter_desc.niter) != INTEGER_CST)
2023 niter = loop_niter_by_eval (loop, ex);
2024 if (dump_file && (dump_flags & TDF_DETAILS)
2025 && TREE_CODE (niter) == INTEGER_CST)
2027 fprintf (dump_file, "Exit %i->%i %d iterates ",
2028 ex->src->index, ex->dest->index,
2029 loop->num);
2030 print_generic_expr (dump_file, niter, TDF_SLIM);
2031 fprintf (dump_file, " times.\n");
2034 if (TREE_CODE (niter) == INTEGER_CST)
2036 if (tree_fits_uhwi_p (niter)
2037 && max
2038 && compare_tree_int (niter, max - 1) == -1)
2039 nitercst = tree_to_uhwi (niter) + 1;
2040 else
2041 nitercst = max;
2042 predictor = PRED_LOOP_ITERATIONS;
2044 /* If we have just one exit and we can derive some information about
2045 the number of iterations of the loop from the statements inside
2046 the loop, use it to predict this exit. */
2047 else if (n_exits == 1
2048 && estimated_stmt_executions (loop, &nit))
2050 if (wi::gtu_p (nit, max))
2051 nitercst = max;
2052 else
2053 nitercst = nit.to_shwi ();
2054 predictor = PRED_LOOP_ITERATIONS_GUESSED;
2056 /* If we have likely upper bound, trust it for very small iteration
2057 counts. Such loops would otherwise get mispredicted by standard
2058 LOOP_EXIT heuristics. */
2059 else if (n_exits == 1
2060 && likely_max_stmt_executions (loop, &nit)
2061 && wi::ltu_p (nit,
2062 RDIV (REG_BR_PROB_BASE,
2063 REG_BR_PROB_BASE
2064 - predictor_info
2065 [recursion
2066 ? PRED_LOOP_EXIT_WITH_RECURSION
2067 : PRED_LOOP_EXIT].hitrate)))
2069 nitercst = nit.to_shwi ();
2070 predictor = PRED_LOOP_ITERATIONS_MAX;
2072 else
2074 if (dump_file && (dump_flags & TDF_DETAILS))
2075 fprintf (dump_file, "Nothing known about exit %i->%i.\n",
2076 ex->src->index, ex->dest->index);
2077 continue;
2080 if (dump_file && (dump_flags & TDF_DETAILS))
2081 fprintf (dump_file, "Recording prediction to %i iterations by %s.\n",
2082 (int)nitercst, predictor_info[predictor].name);
2083 /* If the prediction for number of iterations is zero, do not
2084 predict the exit edges. */
2085 if (nitercst == 0)
2086 continue;
2088 probability = RDIV (REG_BR_PROB_BASE, nitercst);
2089 predict_edge (ex, predictor, probability);
2092 /* Find information about loop bound variables. */
2093 for (nb_iter = loop->bounds; nb_iter;
2094 nb_iter = nb_iter->next)
2095 if (nb_iter->stmt
2096 && gimple_code (nb_iter->stmt) == GIMPLE_COND)
2098 stmt = as_a <gcond *> (nb_iter->stmt);
2099 break;
2101 if (!stmt)
2102 stmt = safe_dyn_cast <gcond *> (*gsi_last_bb (loop->header));
2103 if (stmt)
2104 is_comparison_with_loop_invariant_p (stmt, loop,
2105 &loop_bound_var,
2106 &loop_bound_code,
2107 &loop_bound_step,
2108 &loop_iv_base);
2110 bbs = get_loop_body (loop);
2112 for (j = 0; j < loop->num_nodes; j++)
2114 edge e;
2115 edge_iterator ei;
2117 bb = bbs[j];
2119 /* Bypass loop heuristics on continue statement. These
2120 statements construct loops via "non-loop" constructs
2121 in the source language and are better to be handled
2122 separately. */
2123 if (predicted_by_p (bb, PRED_CONTINUE))
2125 if (dump_file && (dump_flags & TDF_DETAILS))
2126 fprintf (dump_file, "BB %i predicted by continue.\n",
2127 bb->index);
2128 continue;
2131 /* If we already used more reliable loop exit predictors, do not
2132 bother with PRED_LOOP_EXIT. */
2133 if (!predicted_by_loop_heuristics_p (bb))
2135 /* For loop with many exits we don't want to predict all exits
2136 with the pretty large probability, because if all exits are
2137 considered in row, the loop would be predicted to iterate
2138 almost never. The code to divide probability by number of
2139 exits is very rough. It should compute the number of exits
2140 taken in each patch through function (not the overall number
2141 of exits that might be a lot higher for loops with wide switch
2142 statements in them) and compute n-th square root.
2144 We limit the minimal probability by 2% to avoid
2145 EDGE_PROBABILITY_RELIABLE from trusting the branch prediction
2146 as this was causing regression in perl benchmark containing such
2147 a wide loop. */
2149 int probability = ((REG_BR_PROB_BASE
2150 - predictor_info
2151 [recursion
2152 ? PRED_LOOP_EXIT_WITH_RECURSION
2153 : PRED_LOOP_EXIT].hitrate)
2154 / n_exits);
2155 if (probability < HITRATE (2))
2156 probability = HITRATE (2);
2157 FOR_EACH_EDGE (e, ei, bb->succs)
2158 if (e->dest->index < NUM_FIXED_BLOCKS
2159 || !flow_bb_inside_loop_p (loop, e->dest))
2161 if (dump_file && (dump_flags & TDF_DETAILS))
2162 fprintf (dump_file,
2163 "Predicting exit %i->%i with prob %i.\n",
2164 e->src->index, e->dest->index, probability);
2165 predict_edge (e,
2166 recursion ? PRED_LOOP_EXIT_WITH_RECURSION
2167 : PRED_LOOP_EXIT, probability);
2170 if (loop_bound_var)
2171 predict_iv_comparison (loop, bb, loop_bound_var, loop_iv_base,
2172 loop_bound_code,
2173 tree_to_shwi (loop_bound_step));
2176 /* In the following code
2177 for (loop1)
2178 if (cond)
2179 for (loop2)
2180 body;
2181 guess that cond is unlikely. */
2182 if (loop_outer (loop)->num)
2184 basic_block bb = NULL;
2185 edge preheader_edge = loop_preheader_edge (loop);
2187 if (single_pred_p (preheader_edge->src)
2188 && single_succ_p (preheader_edge->src))
2189 preheader_edge = single_pred_edge (preheader_edge->src);
2191 /* Pattern match fortran loop preheader:
2192 _16 = BUILTIN_EXPECT (_15, 1, PRED_FORTRAN_LOOP_PREHEADER);
2193 _17 = (logical(kind=4)) _16;
2194 if (_17 != 0)
2195 goto <bb 11>;
2196 else
2197 goto <bb 13>;
2199 Loop guard branch prediction says nothing about duplicated loop
2200 headers produced by fortran frontend and in this case we want
2201 to predict paths leading to this preheader. */
2203 gcond *stmt
2204 = safe_dyn_cast <gcond *> (*gsi_last_bb (preheader_edge->src));
2205 if (stmt
2206 && gimple_cond_code (stmt) == NE_EXPR
2207 && TREE_CODE (gimple_cond_lhs (stmt)) == SSA_NAME
2208 && integer_zerop (gimple_cond_rhs (stmt)))
2210 gimple *call_stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
2211 if (gimple_code (call_stmt) == GIMPLE_ASSIGN
2212 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (call_stmt))
2213 && TREE_CODE (gimple_assign_rhs1 (call_stmt)) == SSA_NAME)
2214 call_stmt = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (call_stmt));
2215 if (gimple_call_internal_p (call_stmt, IFN_BUILTIN_EXPECT)
2216 && TREE_CODE (gimple_call_arg (call_stmt, 2)) == INTEGER_CST
2217 && tree_fits_uhwi_p (gimple_call_arg (call_stmt, 2))
2218 && tree_to_uhwi (gimple_call_arg (call_stmt, 2))
2219 == PRED_FORTRAN_LOOP_PREHEADER)
2220 bb = preheader_edge->src;
2222 if (!bb)
2224 if (!dominated_by_p (CDI_DOMINATORS,
2225 loop_outer (loop)->latch, loop->header))
2226 predict_paths_leading_to_edge (loop_preheader_edge (loop),
2227 recursion
2228 ? PRED_LOOP_GUARD_WITH_RECURSION
2229 : PRED_LOOP_GUARD,
2230 NOT_TAKEN,
2231 loop_outer (loop));
2233 else
2235 if (!dominated_by_p (CDI_DOMINATORS,
2236 loop_outer (loop)->latch, bb))
2237 predict_paths_leading_to (bb,
2238 recursion
2239 ? PRED_LOOP_GUARD_WITH_RECURSION
2240 : PRED_LOOP_GUARD,
2241 NOT_TAKEN,
2242 loop_outer (loop));
2246 /* Free basic blocks from get_loop_body. */
2247 free (bbs);
2251 /* Attempt to predict probabilities of BB outgoing edges using local
2252 properties. */
2253 static void
2254 bb_estimate_probability_locally (basic_block bb)
2256 rtx_insn *last_insn = BB_END (bb);
2257 rtx cond;
2259 if (! can_predict_insn_p (last_insn))
2260 return;
2261 cond = get_condition (last_insn, NULL, false, false);
2262 if (! cond)
2263 return;
2265 /* Try "pointer heuristic."
2266 A comparison ptr == 0 is predicted as false.
2267 Similarly, a comparison ptr1 == ptr2 is predicted as false. */
2268 if (COMPARISON_P (cond)
2269 && ((REG_P (XEXP (cond, 0)) && REG_POINTER (XEXP (cond, 0)))
2270 || (REG_P (XEXP (cond, 1)) && REG_POINTER (XEXP (cond, 1)))))
2272 if (GET_CODE (cond) == EQ)
2273 predict_insn_def (last_insn, PRED_POINTER, NOT_TAKEN);
2274 else if (GET_CODE (cond) == NE)
2275 predict_insn_def (last_insn, PRED_POINTER, TAKEN);
2277 else
2279 /* Try "opcode heuristic."
2280 EQ tests are usually false and NE tests are usually true. Also,
2281 most quantities are positive, so we can make the appropriate guesses
2282 about signed comparisons against zero. */
2283 switch (GET_CODE (cond))
2285 case CONST_INT:
2286 /* Unconditional branch. */
2287 predict_insn_def (last_insn, PRED_UNCONDITIONAL,
2288 cond == const0_rtx ? NOT_TAKEN : TAKEN);
2289 break;
2291 case EQ:
2292 case UNEQ:
2293 /* Floating point comparisons appears to behave in a very
2294 unpredictable way because of special role of = tests in
2295 FP code. */
2296 if (FLOAT_MODE_P (GET_MODE (XEXP (cond, 0))))
2298 /* Comparisons with 0 are often used for booleans and there is
2299 nothing useful to predict about them. */
2300 else if (XEXP (cond, 1) == const0_rtx
2301 || XEXP (cond, 0) == const0_rtx)
2303 else
2304 predict_insn_def (last_insn, PRED_OPCODE_NONEQUAL, NOT_TAKEN);
2305 break;
2307 case NE:
2308 case LTGT:
2309 /* Floating point comparisons appears to behave in a very
2310 unpredictable way because of special role of = tests in
2311 FP code. */
2312 if (FLOAT_MODE_P (GET_MODE (XEXP (cond, 0))))
2314 /* Comparisons with 0 are often used for booleans and there is
2315 nothing useful to predict about them. */
2316 else if (XEXP (cond, 1) == const0_rtx
2317 || XEXP (cond, 0) == const0_rtx)
2319 else
2320 predict_insn_def (last_insn, PRED_OPCODE_NONEQUAL, TAKEN);
2321 break;
2323 case ORDERED:
2324 predict_insn_def (last_insn, PRED_FPOPCODE, TAKEN);
2325 break;
2327 case UNORDERED:
2328 predict_insn_def (last_insn, PRED_FPOPCODE, NOT_TAKEN);
2329 break;
2331 case LE:
2332 case LT:
2333 if (XEXP (cond, 1) == const0_rtx || XEXP (cond, 1) == const1_rtx
2334 || XEXP (cond, 1) == constm1_rtx)
2335 predict_insn_def (last_insn, PRED_OPCODE_POSITIVE, NOT_TAKEN);
2336 break;
2338 case GE:
2339 case GT:
2340 if (XEXP (cond, 1) == const0_rtx || XEXP (cond, 1) == const1_rtx
2341 || XEXP (cond, 1) == constm1_rtx)
2342 predict_insn_def (last_insn, PRED_OPCODE_POSITIVE, TAKEN);
2343 break;
2345 default:
2346 break;
2350 /* Set edge->probability for each successor edge of BB. */
2351 void
2352 guess_outgoing_edge_probabilities (basic_block bb)
2354 bb_estimate_probability_locally (bb);
2355 combine_predictions_for_insn (BB_END (bb), bb);
2358 static tree expr_expected_value (tree, bitmap, enum br_predictor *predictor,
2359 HOST_WIDE_INT *probability);
2361 /* Helper function for expr_expected_value. */
2363 static tree
2364 expr_expected_value_1 (tree type, tree op0, enum tree_code code,
2365 tree op1, bitmap visited, enum br_predictor *predictor,
2366 HOST_WIDE_INT *probability)
2368 gimple *def;
2370 /* Reset returned probability value. */
2371 *probability = -1;
2372 *predictor = PRED_UNCONDITIONAL;
2374 if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS)
2376 if (TREE_CONSTANT (op0))
2377 return op0;
2379 if (code == IMAGPART_EXPR)
2381 if (TREE_CODE (TREE_OPERAND (op0, 0)) == SSA_NAME)
2383 def = SSA_NAME_DEF_STMT (TREE_OPERAND (op0, 0));
2384 if (is_gimple_call (def)
2385 && gimple_call_internal_p (def)
2386 && (gimple_call_internal_fn (def)
2387 == IFN_ATOMIC_COMPARE_EXCHANGE))
2389 /* Assume that any given atomic operation has low contention,
2390 and thus the compare-and-swap operation succeeds. */
2391 *predictor = PRED_COMPARE_AND_SWAP;
2392 return build_one_cst (TREE_TYPE (op0));
2397 if (code != SSA_NAME)
2398 return NULL_TREE;
2400 def = SSA_NAME_DEF_STMT (op0);
2402 /* If we were already here, break the infinite cycle. */
2403 if (!bitmap_set_bit (visited, SSA_NAME_VERSION (op0)))
2404 return NULL;
2406 if (gimple_code (def) == GIMPLE_PHI)
2408 /* All the arguments of the PHI node must have the same constant
2409 length. */
2410 int i, n = gimple_phi_num_args (def);
2411 tree val = NULL, new_val;
2413 for (i = 0; i < n; i++)
2415 tree arg = PHI_ARG_DEF (def, i);
2416 enum br_predictor predictor2;
2418 /* If this PHI has itself as an argument, we cannot
2419 determine the string length of this argument. However,
2420 if we can find an expected constant value for the other
2421 PHI args then we can still be sure that this is
2422 likely a constant. So be optimistic and just
2423 continue with the next argument. */
2424 if (arg == PHI_RESULT (def))
2425 continue;
2427 HOST_WIDE_INT probability2;
2428 new_val = expr_expected_value (arg, visited, &predictor2,
2429 &probability2);
2431 /* It is difficult to combine value predictors. Simply assume
2432 that later predictor is weaker and take its prediction. */
2433 if (*predictor < predictor2)
2435 *predictor = predictor2;
2436 *probability = probability2;
2438 if (!new_val)
2439 return NULL;
2440 if (!val)
2441 val = new_val;
2442 else if (!operand_equal_p (val, new_val, false))
2443 return NULL;
2445 return val;
2447 if (is_gimple_assign (def))
2449 if (gimple_assign_lhs (def) != op0)
2450 return NULL;
2452 return expr_expected_value_1 (TREE_TYPE (gimple_assign_lhs (def)),
2453 gimple_assign_rhs1 (def),
2454 gimple_assign_rhs_code (def),
2455 gimple_assign_rhs2 (def),
2456 visited, predictor, probability);
2459 if (is_gimple_call (def))
2461 tree decl = gimple_call_fndecl (def);
2462 if (!decl)
2464 if (gimple_call_internal_p (def)
2465 && gimple_call_internal_fn (def) == IFN_BUILTIN_EXPECT)
2467 gcc_assert (gimple_call_num_args (def) == 3);
2468 tree val = gimple_call_arg (def, 0);
2469 if (TREE_CONSTANT (val))
2470 return val;
2471 tree val2 = gimple_call_arg (def, 2);
2472 gcc_assert (TREE_CODE (val2) == INTEGER_CST
2473 && tree_fits_uhwi_p (val2)
2474 && tree_to_uhwi (val2) < END_PREDICTORS);
2475 *predictor = (enum br_predictor) tree_to_uhwi (val2);
2476 if (*predictor == PRED_BUILTIN_EXPECT)
2477 *probability
2478 = HITRATE (param_builtin_expect_probability);
2479 return gimple_call_arg (def, 1);
2481 return NULL;
2484 if (DECL_IS_MALLOC (decl) || DECL_IS_OPERATOR_NEW_P (decl))
2486 if (predictor)
2487 *predictor = PRED_MALLOC_NONNULL;
2488 return boolean_true_node;
2491 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2492 switch (DECL_FUNCTION_CODE (decl))
2494 case BUILT_IN_EXPECT:
2496 tree val;
2497 if (gimple_call_num_args (def) != 2)
2498 return NULL;
2499 val = gimple_call_arg (def, 0);
2500 if (TREE_CONSTANT (val))
2501 return val;
2502 *predictor = PRED_BUILTIN_EXPECT;
2503 *probability
2504 = HITRATE (param_builtin_expect_probability);
2505 return gimple_call_arg (def, 1);
2507 case BUILT_IN_EXPECT_WITH_PROBABILITY:
2509 tree val;
2510 if (gimple_call_num_args (def) != 3)
2511 return NULL;
2512 val = gimple_call_arg (def, 0);
2513 if (TREE_CONSTANT (val))
2514 return val;
2515 /* Compute final probability as:
2516 probability * REG_BR_PROB_BASE. */
2517 tree prob = gimple_call_arg (def, 2);
2518 tree t = TREE_TYPE (prob);
2519 tree base = build_int_cst (integer_type_node,
2520 REG_BR_PROB_BASE);
2521 base = build_real_from_int_cst (t, base);
2522 tree r = fold_build2_initializer_loc (UNKNOWN_LOCATION,
2523 MULT_EXPR, t, prob, base);
2524 if (TREE_CODE (r) != REAL_CST)
2526 error_at (gimple_location (def),
2527 "probability %qE must be "
2528 "constant floating-point expression", prob);
2529 return NULL;
2531 HOST_WIDE_INT probi
2532 = real_to_integer (TREE_REAL_CST_PTR (r));
2533 if (probi >= 0 && probi <= REG_BR_PROB_BASE)
2535 *predictor = PRED_BUILTIN_EXPECT_WITH_PROBABILITY;
2536 *probability = probi;
2538 else
2539 error_at (gimple_location (def),
2540 "probability %qE is outside "
2541 "the range [0.0, 1.0]", prob);
2543 return gimple_call_arg (def, 1);
2546 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
2547 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_1:
2548 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_2:
2549 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_4:
2550 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_8:
2551 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_16:
2552 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
2553 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
2554 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_1:
2555 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_2:
2556 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4:
2557 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8:
2558 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_16:
2559 /* Assume that any given atomic operation has low contention,
2560 and thus the compare-and-swap operation succeeds. */
2561 *predictor = PRED_COMPARE_AND_SWAP;
2562 return boolean_true_node;
2563 case BUILT_IN_REALLOC:
2564 if (predictor)
2565 *predictor = PRED_MALLOC_NONNULL;
2566 return boolean_true_node;
2567 default:
2568 break;
2572 return NULL;
2575 if (get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS)
2577 tree res;
2578 enum br_predictor predictor2;
2579 HOST_WIDE_INT probability2;
2580 op0 = expr_expected_value (op0, visited, predictor, probability);
2581 if (!op0)
2582 return NULL;
2583 op1 = expr_expected_value (op1, visited, &predictor2, &probability2);
2584 if (!op1)
2585 return NULL;
2586 res = fold_build2 (code, type, op0, op1);
2587 if (TREE_CODE (res) == INTEGER_CST
2588 && TREE_CODE (op0) == INTEGER_CST
2589 && TREE_CODE (op1) == INTEGER_CST)
2591 /* Combine binary predictions. */
2592 if (*probability != -1 || probability2 != -1)
2594 HOST_WIDE_INT p1 = get_predictor_value (*predictor, *probability);
2595 HOST_WIDE_INT p2 = get_predictor_value (predictor2, probability2);
2596 *probability = RDIV (p1 * p2, REG_BR_PROB_BASE);
2599 if (*predictor < predictor2)
2600 *predictor = predictor2;
2602 return res;
2604 return NULL;
2606 if (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS)
2608 tree res;
2609 op0 = expr_expected_value (op0, visited, predictor, probability);
2610 if (!op0)
2611 return NULL;
2612 res = fold_build1 (code, type, op0);
2613 if (TREE_CONSTANT (res))
2614 return res;
2615 return NULL;
2617 return NULL;
2620 /* Return constant EXPR will likely have at execution time, NULL if unknown.
2621 The function is used by builtin_expect branch predictor so the evidence
2622 must come from this construct and additional possible constant folding.
2624 We may want to implement more involved value guess (such as value range
2625 propagation based prediction), but such tricks shall go to new
2626 implementation. */
2628 static tree
2629 expr_expected_value (tree expr, bitmap visited,
2630 enum br_predictor *predictor,
2631 HOST_WIDE_INT *probability)
2633 enum tree_code code;
2634 tree op0, op1;
2636 if (TREE_CONSTANT (expr))
2638 *predictor = PRED_UNCONDITIONAL;
2639 *probability = -1;
2640 return expr;
2643 extract_ops_from_tree (expr, &code, &op0, &op1);
2644 return expr_expected_value_1 (TREE_TYPE (expr),
2645 op0, code, op1, visited, predictor,
2646 probability);
2650 /* Return probability of a PREDICTOR. If the predictor has variable
2651 probability return passed PROBABILITY. */
2653 static HOST_WIDE_INT
2654 get_predictor_value (br_predictor predictor, HOST_WIDE_INT probability)
2656 switch (predictor)
2658 case PRED_BUILTIN_EXPECT:
2659 case PRED_BUILTIN_EXPECT_WITH_PROBABILITY:
2660 gcc_assert (probability != -1);
2661 return probability;
2662 default:
2663 gcc_assert (probability == -1);
2664 return predictor_info[(int) predictor].hitrate;
2668 /* Predict using opcode of the last statement in basic block. */
2669 static void
2670 tree_predict_by_opcode (basic_block bb)
2672 edge then_edge;
2673 tree op0, op1;
2674 tree type;
2675 tree val;
2676 enum tree_code cmp;
2677 edge_iterator ei;
2678 enum br_predictor predictor;
2679 HOST_WIDE_INT probability;
2681 gimple *stmt = *gsi_last_bb (bb);
2682 if (!stmt)
2683 return;
2685 if (gswitch *sw = dyn_cast <gswitch *> (stmt))
2687 tree index = gimple_switch_index (sw);
2688 tree val = expr_expected_value (index, auto_bitmap (),
2689 &predictor, &probability);
2690 if (val && TREE_CODE (val) == INTEGER_CST)
2692 edge e = find_taken_edge_switch_expr (sw, val);
2693 if (predictor == PRED_BUILTIN_EXPECT)
2695 int percent = param_builtin_expect_probability;
2696 gcc_assert (percent >= 0 && percent <= 100);
2697 predict_edge (e, PRED_BUILTIN_EXPECT,
2698 HITRATE (percent));
2700 else
2701 predict_edge_def (e, predictor, TAKEN);
2705 if (gimple_code (stmt) != GIMPLE_COND)
2706 return;
2707 FOR_EACH_EDGE (then_edge, ei, bb->succs)
2708 if (then_edge->flags & EDGE_TRUE_VALUE)
2709 break;
2710 op0 = gimple_cond_lhs (stmt);
2711 op1 = gimple_cond_rhs (stmt);
2712 cmp = gimple_cond_code (stmt);
2713 type = TREE_TYPE (op0);
2714 val = expr_expected_value_1 (boolean_type_node, op0, cmp, op1, auto_bitmap (),
2715 &predictor, &probability);
2716 if (val && TREE_CODE (val) == INTEGER_CST)
2718 HOST_WIDE_INT prob = get_predictor_value (predictor, probability);
2719 if (integer_zerop (val))
2720 prob = REG_BR_PROB_BASE - prob;
2721 predict_edge (then_edge, predictor, prob);
2723 /* Try "pointer heuristic."
2724 A comparison ptr == 0 is predicted as false.
2725 Similarly, a comparison ptr1 == ptr2 is predicted as false. */
2726 if (POINTER_TYPE_P (type))
2728 if (cmp == EQ_EXPR)
2729 predict_edge_def (then_edge, PRED_TREE_POINTER, NOT_TAKEN);
2730 else if (cmp == NE_EXPR)
2731 predict_edge_def (then_edge, PRED_TREE_POINTER, TAKEN);
2733 else
2735 /* Try "opcode heuristic."
2736 EQ tests are usually false and NE tests are usually true. Also,
2737 most quantities are positive, so we can make the appropriate guesses
2738 about signed comparisons against zero. */
2739 switch (cmp)
2741 case EQ_EXPR:
2742 case UNEQ_EXPR:
2743 /* Floating point comparisons appears to behave in a very
2744 unpredictable way because of special role of = tests in
2745 FP code. */
2746 if (FLOAT_TYPE_P (type))
2748 /* Comparisons with 0 are often used for booleans and there is
2749 nothing useful to predict about them. */
2750 else if (integer_zerop (op0) || integer_zerop (op1))
2752 else
2753 predict_edge_def (then_edge, PRED_TREE_OPCODE_NONEQUAL, NOT_TAKEN);
2754 break;
2756 case NE_EXPR:
2757 case LTGT_EXPR:
2758 /* Floating point comparisons appears to behave in a very
2759 unpredictable way because of special role of = tests in
2760 FP code. */
2761 if (FLOAT_TYPE_P (type))
2763 /* Comparisons with 0 are often used for booleans and there is
2764 nothing useful to predict about them. */
2765 else if (integer_zerop (op0)
2766 || integer_zerop (op1))
2768 else
2769 predict_edge_def (then_edge, PRED_TREE_OPCODE_NONEQUAL, TAKEN);
2770 break;
2772 case ORDERED_EXPR:
2773 predict_edge_def (then_edge, PRED_TREE_FPOPCODE, TAKEN);
2774 break;
2776 case UNORDERED_EXPR:
2777 predict_edge_def (then_edge, PRED_TREE_FPOPCODE, NOT_TAKEN);
2778 break;
2780 case LE_EXPR:
2781 case LT_EXPR:
2782 if (integer_zerop (op1)
2783 || integer_onep (op1)
2784 || integer_all_onesp (op1)
2785 || real_zerop (op1)
2786 || real_onep (op1)
2787 || real_minus_onep (op1))
2788 predict_edge_def (then_edge, PRED_TREE_OPCODE_POSITIVE, NOT_TAKEN);
2789 break;
2791 case GE_EXPR:
2792 case GT_EXPR:
2793 if (integer_zerop (op1)
2794 || integer_onep (op1)
2795 || integer_all_onesp (op1)
2796 || real_zerop (op1)
2797 || real_onep (op1)
2798 || real_minus_onep (op1))
2799 predict_edge_def (then_edge, PRED_TREE_OPCODE_POSITIVE, TAKEN);
2800 break;
2802 default:
2803 break;
2807 /* Returns TRUE if the STMT is exit(0) like statement. */
2809 static bool
2810 is_exit_with_zero_arg (const gimple *stmt)
2812 /* This is not exit, _exit or _Exit. */
2813 if (!gimple_call_builtin_p (stmt, BUILT_IN_EXIT)
2814 && !gimple_call_builtin_p (stmt, BUILT_IN__EXIT)
2815 && !gimple_call_builtin_p (stmt, BUILT_IN__EXIT2))
2816 return false;
2818 /* Argument is an interger zero. */
2819 return integer_zerop (gimple_call_arg (stmt, 0));
2822 /* Try to guess whether the value of return means error code. */
2824 static enum br_predictor
2825 return_prediction (tree val, enum prediction *prediction)
2827 /* VOID. */
2828 if (!val)
2829 return PRED_NO_PREDICTION;
2830 /* Different heuristics for pointers and scalars. */
2831 if (POINTER_TYPE_P (TREE_TYPE (val)))
2833 /* NULL is usually not returned. */
2834 if (integer_zerop (val))
2836 *prediction = NOT_TAKEN;
2837 return PRED_NULL_RETURN;
2840 else if (INTEGRAL_TYPE_P (TREE_TYPE (val)))
2842 /* Negative return values are often used to indicate
2843 errors. */
2844 if (TREE_CODE (val) == INTEGER_CST
2845 && tree_int_cst_sgn (val) < 0)
2847 *prediction = NOT_TAKEN;
2848 return PRED_NEGATIVE_RETURN;
2850 /* Constant return values seems to be commonly taken.
2851 Zero/one often represent booleans so exclude them from the
2852 heuristics. */
2853 if (TREE_CONSTANT (val)
2854 && (!integer_zerop (val) && !integer_onep (val)))
2856 *prediction = NOT_TAKEN;
2857 return PRED_CONST_RETURN;
2860 return PRED_NO_PREDICTION;
2863 /* Return zero if phi result could have values other than -1, 0 or 1,
2864 otherwise return a bitmask, with bits 0, 1 and 2 set if -1, 0 and 1
2865 values are used or likely. */
2867 static int
2868 zero_one_minusone (gphi *phi, int limit)
2870 int phi_num_args = gimple_phi_num_args (phi);
2871 int ret = 0;
2872 for (int i = 0; i < phi_num_args; i++)
2874 tree t = PHI_ARG_DEF (phi, i);
2875 if (TREE_CODE (t) != INTEGER_CST)
2876 continue;
2877 wide_int w = wi::to_wide (t);
2878 if (w == -1)
2879 ret |= 1;
2880 else if (w == 0)
2881 ret |= 2;
2882 else if (w == 1)
2883 ret |= 4;
2884 else
2885 return 0;
2887 for (int i = 0; i < phi_num_args; i++)
2889 tree t = PHI_ARG_DEF (phi, i);
2890 if (TREE_CODE (t) == INTEGER_CST)
2891 continue;
2892 if (TREE_CODE (t) != SSA_NAME)
2893 return 0;
2894 gimple *g = SSA_NAME_DEF_STMT (t);
2895 if (gimple_code (g) == GIMPLE_PHI && limit > 0)
2896 if (int r = zero_one_minusone (as_a <gphi *> (g), limit - 1))
2898 ret |= r;
2899 continue;
2901 if (!is_gimple_assign (g))
2902 return 0;
2903 if (gimple_assign_cast_p (g))
2905 tree rhs1 = gimple_assign_rhs1 (g);
2906 if (TREE_CODE (rhs1) != SSA_NAME
2907 || !INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
2908 || TYPE_PRECISION (TREE_TYPE (rhs1)) != 1
2909 || !TYPE_UNSIGNED (TREE_TYPE (rhs1)))
2910 return 0;
2911 ret |= (2 | 4);
2912 continue;
2914 if (TREE_CODE_CLASS (gimple_assign_rhs_code (g)) != tcc_comparison)
2915 return 0;
2916 ret |= (2 | 4);
2918 return ret;
2921 /* Find the basic block with return expression and look up for possible
2922 return value trying to apply RETURN_PREDICTION heuristics. */
2923 static void
2924 apply_return_prediction (void)
2926 greturn *return_stmt = NULL;
2927 tree return_val;
2928 edge e;
2929 gphi *phi;
2930 int phi_num_args, i;
2931 enum br_predictor pred;
2932 enum prediction direction;
2933 edge_iterator ei;
2935 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
2937 gimple *last = last_stmt (e->src);
2938 if (last
2939 && gimple_code (last) == GIMPLE_RETURN)
2941 return_stmt = as_a <greturn *> (last);
2942 break;
2945 if (!e)
2946 return;
2947 return_val = gimple_return_retval (return_stmt);
2948 if (!return_val)
2949 return;
2950 if (TREE_CODE (return_val) != SSA_NAME
2951 || !SSA_NAME_DEF_STMT (return_val)
2952 || gimple_code (SSA_NAME_DEF_STMT (return_val)) != GIMPLE_PHI)
2953 return;
2954 phi = as_a <gphi *> (SSA_NAME_DEF_STMT (return_val));
2955 phi_num_args = gimple_phi_num_args (phi);
2956 pred = return_prediction (PHI_ARG_DEF (phi, 0), &direction);
2958 /* Avoid the case where the function returns -1, 0 and 1 values and
2959 nothing else. Those could be qsort etc. comparison functions
2960 where the negative return isn't less probable than positive.
2961 For this require that the function returns at least -1 or 1
2962 or -1 and a boolean value or comparison result, so that functions
2963 returning just -1 and 0 are treated as if -1 represents error value. */
2964 if (INTEGRAL_TYPE_P (TREE_TYPE (return_val))
2965 && !TYPE_UNSIGNED (TREE_TYPE (return_val))
2966 && TYPE_PRECISION (TREE_TYPE (return_val)) > 1)
2967 if (int r = zero_one_minusone (phi, 3))
2968 if ((r & (1 | 4)) == (1 | 4))
2969 return;
2971 /* Avoid the degenerate case where all return values form the function
2972 belongs to same category (ie they are all positive constants)
2973 so we can hardly say something about them. */
2974 for (i = 1; i < phi_num_args; i++)
2975 if (pred != return_prediction (PHI_ARG_DEF (phi, i), &direction))
2976 break;
2977 if (i != phi_num_args)
2978 for (i = 0; i < phi_num_args; i++)
2980 pred = return_prediction (PHI_ARG_DEF (phi, i), &direction);
2981 if (pred != PRED_NO_PREDICTION)
2982 predict_paths_leading_to_edge (gimple_phi_arg_edge (phi, i), pred,
2983 direction);
2987 /* Look for basic block that contains unlikely to happen events
2988 (such as noreturn calls) and mark all paths leading to execution
2989 of this basic blocks as unlikely. */
2991 static void
2992 tree_bb_level_predictions (void)
2994 basic_block bb;
2995 bool has_return_edges = false;
2996 edge e;
2997 edge_iterator ei;
2999 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
3000 if (!unlikely_executed_edge_p (e) && !(e->flags & EDGE_ABNORMAL_CALL))
3002 has_return_edges = true;
3003 break;
3006 apply_return_prediction ();
3008 FOR_EACH_BB_FN (bb, cfun)
3010 gimple_stmt_iterator gsi;
3012 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3014 gimple *stmt = gsi_stmt (gsi);
3015 tree decl;
3017 if (is_gimple_call (stmt))
3019 if (gimple_call_noreturn_p (stmt)
3020 && has_return_edges
3021 && !is_exit_with_zero_arg (stmt))
3022 predict_paths_leading_to (bb, PRED_NORETURN,
3023 NOT_TAKEN);
3024 decl = gimple_call_fndecl (stmt);
3025 if (decl
3026 && lookup_attribute ("cold",
3027 DECL_ATTRIBUTES (decl)))
3028 predict_paths_leading_to (bb, PRED_COLD_FUNCTION,
3029 NOT_TAKEN);
3030 if (decl && recursive_call_p (current_function_decl, decl))
3031 predict_paths_leading_to (bb, PRED_RECURSIVE_CALL,
3032 NOT_TAKEN);
3034 else if (gimple_code (stmt) == GIMPLE_PREDICT)
3036 predict_paths_leading_to (bb, gimple_predict_predictor (stmt),
3037 gimple_predict_outcome (stmt));
3038 /* Keep GIMPLE_PREDICT around so early inlining will propagate
3039 hints to callers. */
3045 /* Callback for hash_map::traverse, asserts that the pointer map is
3046 empty. */
3048 bool
3049 assert_is_empty (const_basic_block const &, edge_prediction *const &value,
3050 void *)
3052 gcc_assert (!value);
3053 return true;
3056 /* Predict branch probabilities and estimate profile for basic block BB.
3057 When LOCAL_ONLY is set do not use any global properties of CFG. */
3059 static void
3060 tree_estimate_probability_bb (basic_block bb, bool local_only)
3062 edge e;
3063 edge_iterator ei;
3065 FOR_EACH_EDGE (e, ei, bb->succs)
3067 /* Look for block we are guarding (ie we dominate it,
3068 but it doesn't postdominate us). */
3069 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun) && e->dest != bb
3070 && !local_only
3071 && dominated_by_p (CDI_DOMINATORS, e->dest, e->src)
3072 && !dominated_by_p (CDI_POST_DOMINATORS, e->src, e->dest))
3074 gimple_stmt_iterator bi;
3076 /* The call heuristic claims that a guarded function call
3077 is improbable. This is because such calls are often used
3078 to signal exceptional situations such as printing error
3079 messages. */
3080 for (bi = gsi_start_bb (e->dest); !gsi_end_p (bi);
3081 gsi_next (&bi))
3083 gimple *stmt = gsi_stmt (bi);
3084 if (is_gimple_call (stmt)
3085 && !gimple_inexpensive_call_p (as_a <gcall *> (stmt))
3086 /* Constant and pure calls are hardly used to signalize
3087 something exceptional. */
3088 && gimple_has_side_effects (stmt))
3090 if (gimple_call_fndecl (stmt))
3091 predict_edge_def (e, PRED_CALL, NOT_TAKEN);
3092 else if (virtual_method_call_p (gimple_call_fn (stmt)))
3093 predict_edge_def (e, PRED_POLYMORPHIC_CALL, NOT_TAKEN);
3094 else
3095 predict_edge_def (e, PRED_INDIR_CALL, TAKEN);
3096 break;
3101 tree_predict_by_opcode (bb);
3104 /* Predict branch probabilities and estimate profile of the tree CFG.
3105 This function can be called from the loop optimizers to recompute
3106 the profile information.
3107 If DRY_RUN is set, do not modify CFG and only produce dump files. */
3109 void
3110 tree_estimate_probability (bool dry_run)
3112 basic_block bb;
3114 connect_infinite_loops_to_exit ();
3115 /* We use loop_niter_by_eval, which requires that the loops have
3116 preheaders. */
3117 create_preheaders (CP_SIMPLE_PREHEADERS);
3118 calculate_dominance_info (CDI_POST_DOMINATORS);
3119 /* Decide which edges are known to be unlikely. This improves later
3120 branch prediction. */
3121 determine_unlikely_bbs ();
3123 bb_predictions = new hash_map<const_basic_block, edge_prediction *>;
3124 tree_bb_level_predictions ();
3125 record_loop_exits ();
3127 if (number_of_loops (cfun) > 1)
3128 predict_loops ();
3130 FOR_EACH_BB_FN (bb, cfun)
3131 tree_estimate_probability_bb (bb, false);
3133 FOR_EACH_BB_FN (bb, cfun)
3134 combine_predictions_for_bb (bb, dry_run);
3136 if (flag_checking)
3137 bb_predictions->traverse<void *, assert_is_empty> (NULL);
3139 delete bb_predictions;
3140 bb_predictions = NULL;
3142 if (!dry_run)
3143 estimate_bb_frequencies (false);
3144 free_dominance_info (CDI_POST_DOMINATORS);
3145 remove_fake_exit_edges ();
3148 /* Set edge->probability for each successor edge of BB. */
3149 void
3150 tree_guess_outgoing_edge_probabilities (basic_block bb)
3152 bb_predictions = new hash_map<const_basic_block, edge_prediction *>;
3153 tree_estimate_probability_bb (bb, true);
3154 combine_predictions_for_bb (bb, false);
3155 if (flag_checking)
3156 bb_predictions->traverse<void *, assert_is_empty> (NULL);
3157 delete bb_predictions;
3158 bb_predictions = NULL;
3161 /* Filter function predicate that returns true for a edge predicate P
3162 if its edge is equal to DATA. */
3164 static bool
3165 not_loop_guard_equal_edge_p (edge_prediction *p, void *data)
3167 return p->ep_edge != (edge)data || p->ep_predictor != PRED_LOOP_GUARD;
3170 /* Predict edge E with PRED unless it is already predicted by some predictor
3171 considered equivalent. */
3173 static void
3174 maybe_predict_edge (edge e, enum br_predictor pred, enum prediction taken)
3176 if (edge_predicted_by_p (e, pred, taken))
3177 return;
3178 if (pred == PRED_LOOP_GUARD
3179 && edge_predicted_by_p (e, PRED_LOOP_GUARD_WITH_RECURSION, taken))
3180 return;
3181 /* Consider PRED_LOOP_GUARD_WITH_RECURSION superrior to LOOP_GUARD. */
3182 if (pred == PRED_LOOP_GUARD_WITH_RECURSION)
3184 edge_prediction **preds = bb_predictions->get (e->src);
3185 if (preds)
3186 filter_predictions (preds, not_loop_guard_equal_edge_p, e);
3188 predict_edge_def (e, pred, taken);
3190 /* Predict edges to successors of CUR whose sources are not postdominated by
3191 BB by PRED and recurse to all postdominators. */
3193 static void
3194 predict_paths_for_bb (basic_block cur, basic_block bb,
3195 enum br_predictor pred,
3196 enum prediction taken,
3197 bitmap visited, class loop *in_loop = NULL)
3199 edge e;
3200 edge_iterator ei;
3201 basic_block son;
3203 /* If we exited the loop or CUR is unconditional in the loop, there is
3204 nothing to do. */
3205 if (in_loop
3206 && (!flow_bb_inside_loop_p (in_loop, cur)
3207 || dominated_by_p (CDI_DOMINATORS, in_loop->latch, cur)))
3208 return;
3210 /* We are looking for all edges forming edge cut induced by
3211 set of all blocks postdominated by BB. */
3212 FOR_EACH_EDGE (e, ei, cur->preds)
3213 if (e->src->index >= NUM_FIXED_BLOCKS
3214 && !dominated_by_p (CDI_POST_DOMINATORS, e->src, bb))
3216 edge e2;
3217 edge_iterator ei2;
3218 bool found = false;
3220 /* Ignore fake edges and eh, we predict them as not taken anyway. */
3221 if (unlikely_executed_edge_p (e))
3222 continue;
3223 gcc_assert (bb == cur || dominated_by_p (CDI_POST_DOMINATORS, cur, bb));
3225 /* See if there is an edge from e->src that is not abnormal
3226 and does not lead to BB and does not exit the loop. */
3227 FOR_EACH_EDGE (e2, ei2, e->src->succs)
3228 if (e2 != e
3229 && !unlikely_executed_edge_p (e2)
3230 && !dominated_by_p (CDI_POST_DOMINATORS, e2->dest, bb)
3231 && (!in_loop || !loop_exit_edge_p (in_loop, e2)))
3233 found = true;
3234 break;
3237 /* If there is non-abnormal path leaving e->src, predict edge
3238 using predictor. Otherwise we need to look for paths
3239 leading to e->src.
3241 The second may lead to infinite loop in the case we are predicitng
3242 regions that are only reachable by abnormal edges. We simply
3243 prevent visiting given BB twice. */
3244 if (found)
3245 maybe_predict_edge (e, pred, taken);
3246 else if (bitmap_set_bit (visited, e->src->index))
3247 predict_paths_for_bb (e->src, e->src, pred, taken, visited, in_loop);
3249 for (son = first_dom_son (CDI_POST_DOMINATORS, cur);
3250 son;
3251 son = next_dom_son (CDI_POST_DOMINATORS, son))
3252 predict_paths_for_bb (son, bb, pred, taken, visited, in_loop);
3255 /* Sets branch probabilities according to PREDiction and
3256 FLAGS. */
3258 static void
3259 predict_paths_leading_to (basic_block bb, enum br_predictor pred,
3260 enum prediction taken, class loop *in_loop)
3262 predict_paths_for_bb (bb, bb, pred, taken, auto_bitmap (), in_loop);
3265 /* Like predict_paths_leading_to but take edge instead of basic block. */
3267 static void
3268 predict_paths_leading_to_edge (edge e, enum br_predictor pred,
3269 enum prediction taken, class loop *in_loop)
3271 bool has_nonloop_edge = false;
3272 edge_iterator ei;
3273 edge e2;
3275 basic_block bb = e->src;
3276 FOR_EACH_EDGE (e2, ei, bb->succs)
3277 if (e2->dest != e->src && e2->dest != e->dest
3278 && !unlikely_executed_edge_p (e2)
3279 && !dominated_by_p (CDI_POST_DOMINATORS, e->src, e2->dest))
3281 has_nonloop_edge = true;
3282 break;
3285 if (!has_nonloop_edge)
3286 predict_paths_for_bb (bb, bb, pred, taken, auto_bitmap (), in_loop);
3287 else
3288 maybe_predict_edge (e, pred, taken);
3291 /* This is used to carry information about basic blocks. It is
3292 attached to the AUX field of the standard CFG block. */
3294 class block_info
3296 public:
3297 /* Estimated frequency of execution of basic_block. */
3298 sreal frequency;
3300 /* To keep queue of basic blocks to process. */
3301 basic_block next;
3303 /* Number of predecessors we need to visit first. */
3304 int npredecessors;
3307 /* Similar information for edges. */
3308 class edge_prob_info
3310 public:
3311 /* In case edge is a loopback edge, the probability edge will be reached
3312 in case header is. Estimated number of iterations of the loop can be
3313 then computed as 1 / (1 - back_edge_prob). */
3314 sreal back_edge_prob;
3315 /* True if the edge is a loopback edge in the natural loop. */
3316 unsigned int back_edge:1;
3319 #define BLOCK_INFO(B) ((block_info *) (B)->aux)
3320 #undef EDGE_INFO
3321 #define EDGE_INFO(E) ((edge_prob_info *) (E)->aux)
3323 /* Helper function for estimate_bb_frequencies.
3324 Propagate the frequencies in blocks marked in
3325 TOVISIT, starting in HEAD. */
3327 static void
3328 propagate_freq (basic_block head, bitmap tovisit,
3329 sreal max_cyclic_prob)
3331 basic_block bb;
3332 basic_block last;
3333 unsigned i;
3334 edge e;
3335 basic_block nextbb;
3336 bitmap_iterator bi;
3338 /* For each basic block we need to visit count number of his predecessors
3339 we need to visit first. */
3340 EXECUTE_IF_SET_IN_BITMAP (tovisit, 0, i, bi)
3342 edge_iterator ei;
3343 int count = 0;
3345 bb = BASIC_BLOCK_FOR_FN (cfun, i);
3347 FOR_EACH_EDGE (e, ei, bb->preds)
3349 bool visit = bitmap_bit_p (tovisit, e->src->index);
3351 if (visit && !(e->flags & EDGE_DFS_BACK))
3352 count++;
3353 else if (visit && dump_file && !EDGE_INFO (e)->back_edge)
3354 fprintf (dump_file,
3355 "Irreducible region hit, ignoring edge to %i->%i\n",
3356 e->src->index, bb->index);
3358 BLOCK_INFO (bb)->npredecessors = count;
3359 /* When function never returns, we will never process exit block. */
3360 if (!count && bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
3361 bb->count = profile_count::zero ();
3364 BLOCK_INFO (head)->frequency = 1;
3365 last = head;
3366 for (bb = head; bb; bb = nextbb)
3368 edge_iterator ei;
3369 sreal cyclic_probability = 0;
3370 sreal frequency = 0;
3372 nextbb = BLOCK_INFO (bb)->next;
3373 BLOCK_INFO (bb)->next = NULL;
3375 /* Compute frequency of basic block. */
3376 if (bb != head)
3378 if (flag_checking)
3379 FOR_EACH_EDGE (e, ei, bb->preds)
3380 gcc_assert (!bitmap_bit_p (tovisit, e->src->index)
3381 || (e->flags & EDGE_DFS_BACK));
3383 FOR_EACH_EDGE (e, ei, bb->preds)
3384 if (EDGE_INFO (e)->back_edge)
3385 cyclic_probability += EDGE_INFO (e)->back_edge_prob;
3386 else if (!(e->flags & EDGE_DFS_BACK))
3388 /* FIXME: Graphite is producing edges with no profile. Once
3389 this is fixed, drop this. */
3390 sreal tmp = e->probability.initialized_p () ?
3391 e->probability.to_sreal () : 0;
3392 frequency += tmp * BLOCK_INFO (e->src)->frequency;
3395 if (cyclic_probability == 0)
3397 BLOCK_INFO (bb)->frequency = frequency;
3399 else
3401 if (cyclic_probability > max_cyclic_prob)
3403 if (dump_file)
3404 fprintf (dump_file,
3405 "cyclic probability of bb %i is %f (capped to %f)"
3406 "; turning freq %f",
3407 bb->index, cyclic_probability.to_double (),
3408 max_cyclic_prob.to_double (),
3409 frequency.to_double ());
3411 cyclic_probability = max_cyclic_prob;
3413 else if (dump_file)
3414 fprintf (dump_file,
3415 "cyclic probability of bb %i is %f; turning freq %f",
3416 bb->index, cyclic_probability.to_double (),
3417 frequency.to_double ());
3419 BLOCK_INFO (bb)->frequency = frequency
3420 / (sreal (1) - cyclic_probability);
3421 if (dump_file)
3422 fprintf (dump_file, " to %f\n",
3423 BLOCK_INFO (bb)->frequency.to_double ());
3427 bitmap_clear_bit (tovisit, bb->index);
3429 e = find_edge (bb, head);
3430 if (e)
3432 /* FIXME: Graphite is producing edges with no profile. Once
3433 this is fixed, drop this. */
3434 sreal tmp = e->probability.initialized_p () ?
3435 e->probability.to_sreal () : 0;
3436 EDGE_INFO (e)->back_edge_prob = tmp * BLOCK_INFO (bb)->frequency;
3439 /* Propagate to successor blocks. */
3440 FOR_EACH_EDGE (e, ei, bb->succs)
3441 if (!(e->flags & EDGE_DFS_BACK)
3442 && BLOCK_INFO (e->dest)->npredecessors)
3444 BLOCK_INFO (e->dest)->npredecessors--;
3445 if (!BLOCK_INFO (e->dest)->npredecessors)
3447 if (!nextbb)
3448 nextbb = e->dest;
3449 else
3450 BLOCK_INFO (last)->next = e->dest;
3452 last = e->dest;
3458 /* Estimate frequencies in loops at same nest level. */
3460 static void
3461 estimate_loops_at_level (class loop *first_loop, sreal max_cyclic_prob)
3463 class loop *loop;
3465 for (loop = first_loop; loop; loop = loop->next)
3467 edge e;
3468 basic_block *bbs;
3469 unsigned i;
3470 auto_bitmap tovisit;
3472 estimate_loops_at_level (loop->inner, max_cyclic_prob);
3474 /* Find current loop back edge and mark it. */
3475 e = loop_latch_edge (loop);
3476 EDGE_INFO (e)->back_edge = 1;
3478 bbs = get_loop_body (loop);
3479 for (i = 0; i < loop->num_nodes; i++)
3480 bitmap_set_bit (tovisit, bbs[i]->index);
3481 free (bbs);
3482 propagate_freq (loop->header, tovisit, max_cyclic_prob);
3486 /* Propagates frequencies through structure of loops. */
3488 static void
3489 estimate_loops (void)
3491 auto_bitmap tovisit;
3492 basic_block bb;
3493 sreal max_cyclic_prob = (sreal)1
3494 - (sreal)1 / (param_max_predicted_iterations + 1);
3496 /* Start by estimating the frequencies in the loops. */
3497 if (number_of_loops (cfun) > 1)
3498 estimate_loops_at_level (current_loops->tree_root->inner, max_cyclic_prob);
3500 /* Now propagate the frequencies through all the blocks. */
3501 FOR_ALL_BB_FN (bb, cfun)
3503 bitmap_set_bit (tovisit, bb->index);
3505 propagate_freq (ENTRY_BLOCK_PTR_FOR_FN (cfun), tovisit, max_cyclic_prob);
3508 /* Drop the profile for NODE to guessed, and update its frequency based on
3509 whether it is expected to be hot given the CALL_COUNT. */
3511 static void
3512 drop_profile (struct cgraph_node *node, profile_count call_count)
3514 struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
3515 /* In the case where this was called by another function with a
3516 dropped profile, call_count will be 0. Since there are no
3517 non-zero call counts to this function, we don't know for sure
3518 whether it is hot, and therefore it will be marked normal below. */
3519 bool hot = maybe_hot_count_p (NULL, call_count);
3521 if (dump_file)
3522 fprintf (dump_file,
3523 "Dropping 0 profile for %s. %s based on calls.\n",
3524 node->dump_name (),
3525 hot ? "Function is hot" : "Function is normal");
3526 /* We only expect to miss profiles for functions that are reached
3527 via non-zero call edges in cases where the function may have
3528 been linked from another module or library (COMDATs and extern
3529 templates). See the comments below for handle_missing_profiles.
3530 Also, only warn in cases where the missing counts exceed the
3531 number of training runs. In certain cases with an execv followed
3532 by a no-return call the profile for the no-return call is not
3533 dumped and there can be a mismatch. */
3534 if (!DECL_COMDAT (node->decl) && !DECL_EXTERNAL (node->decl)
3535 && call_count > profile_info->runs)
3537 if (flag_profile_correction)
3539 if (dump_file)
3540 fprintf (dump_file,
3541 "Missing counts for called function %s\n",
3542 node->dump_name ());
3544 else
3545 warning (0, "Missing counts for called function %s",
3546 node->dump_name ());
3549 basic_block bb;
3550 if (opt_for_fn (node->decl, flag_guess_branch_prob))
3552 bool clear_zeros
3553 = !ENTRY_BLOCK_PTR_FOR_FN (fn)->count.nonzero_p ();
3554 FOR_ALL_BB_FN (bb, fn)
3555 if (clear_zeros || !(bb->count == profile_count::zero ()))
3556 bb->count = bb->count.guessed_local ();
3557 fn->cfg->count_max = fn->cfg->count_max.guessed_local ();
3559 else
3561 FOR_ALL_BB_FN (bb, fn)
3562 bb->count = profile_count::uninitialized ();
3563 fn->cfg->count_max = profile_count::uninitialized ();
3566 struct cgraph_edge *e;
3567 for (e = node->callees; e; e = e->next_callee)
3568 e->count = gimple_bb (e->call_stmt)->count;
3569 for (e = node->indirect_calls; e; e = e->next_callee)
3570 e->count = gimple_bb (e->call_stmt)->count;
3571 node->count = ENTRY_BLOCK_PTR_FOR_FN (fn)->count;
3573 profile_status_for_fn (fn)
3574 = (flag_guess_branch_prob ? PROFILE_GUESSED : PROFILE_ABSENT);
3575 node->frequency
3576 = hot ? NODE_FREQUENCY_HOT : NODE_FREQUENCY_NORMAL;
3579 /* In the case of COMDAT routines, multiple object files will contain the same
3580 function and the linker will select one for the binary. In that case
3581 all the other copies from the profile instrument binary will be missing
3582 profile counts. Look for cases where this happened, due to non-zero
3583 call counts going to 0-count functions, and drop the profile to guessed
3584 so that we can use the estimated probabilities and avoid optimizing only
3585 for size.
3587 The other case where the profile may be missing is when the routine
3588 is not going to be emitted to the object file, e.g. for "extern template"
3589 class methods. Those will be marked DECL_EXTERNAL. Emit a warning in
3590 all other cases of non-zero calls to 0-count functions. */
3592 void
3593 handle_missing_profiles (void)
3595 const int unlikely_frac = param_unlikely_bb_count_fraction;
3596 struct cgraph_node *node;
3597 auto_vec<struct cgraph_node *, 64> worklist;
3599 /* See if 0 count function has non-0 count callers. In this case we
3600 lost some profile. Drop its function profile to PROFILE_GUESSED. */
3601 FOR_EACH_DEFINED_FUNCTION (node)
3603 struct cgraph_edge *e;
3604 profile_count call_count = profile_count::zero ();
3605 gcov_type max_tp_first_run = 0;
3606 struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
3608 if (node->count.ipa ().nonzero_p ())
3609 continue;
3610 for (e = node->callers; e; e = e->next_caller)
3611 if (e->count.ipa ().initialized_p () && e->count.ipa () > 0)
3613 call_count = call_count + e->count.ipa ();
3615 if (e->caller->tp_first_run > max_tp_first_run)
3616 max_tp_first_run = e->caller->tp_first_run;
3619 /* If time profile is missing, let assign the maximum that comes from
3620 caller functions. */
3621 if (!node->tp_first_run && max_tp_first_run)
3622 node->tp_first_run = max_tp_first_run + 1;
3624 if (call_count > 0
3625 && fn && fn->cfg
3626 && call_count * unlikely_frac >= profile_info->runs)
3628 drop_profile (node, call_count);
3629 worklist.safe_push (node);
3633 /* Propagate the profile dropping to other 0-count COMDATs that are
3634 potentially called by COMDATs we already dropped the profile on. */
3635 while (worklist.length () > 0)
3637 struct cgraph_edge *e;
3639 node = worklist.pop ();
3640 for (e = node->callees; e; e = e->next_caller)
3642 struct cgraph_node *callee = e->callee;
3643 struct function *fn = DECL_STRUCT_FUNCTION (callee->decl);
3645 if (!(e->count.ipa () == profile_count::zero ())
3646 && callee->count.ipa ().nonzero_p ())
3647 continue;
3648 if ((DECL_COMDAT (callee->decl) || DECL_EXTERNAL (callee->decl))
3649 && fn && fn->cfg
3650 && profile_status_for_fn (fn) == PROFILE_READ)
3652 drop_profile (node, profile_count::zero ());
3653 worklist.safe_push (callee);
3659 /* Convert counts measured by profile driven feedback to frequencies.
3660 Return nonzero iff there was any nonzero execution count. */
3662 bool
3663 update_max_bb_count (void)
3665 profile_count true_count_max = profile_count::uninitialized ();
3666 basic_block bb;
3668 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
3669 true_count_max = true_count_max.max (bb->count);
3671 cfun->cfg->count_max = true_count_max;
3673 return true_count_max.ipa ().nonzero_p ();
3676 /* Return true if function is likely to be expensive, so there is no point to
3677 optimize performance of prologue, epilogue or do inlining at the expense
3678 of code size growth. THRESHOLD is the limit of number of instructions
3679 function can execute at average to be still considered not expensive. */
3681 bool
3682 expensive_function_p (int threshold)
3684 basic_block bb;
3686 /* If profile was scaled in a way entry block has count 0, then the function
3687 is deifnitly taking a lot of time. */
3688 if (!ENTRY_BLOCK_PTR_FOR_FN (cfun)->count.nonzero_p ())
3689 return true;
3691 profile_count limit = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count * threshold;
3692 profile_count sum = profile_count::zero ();
3693 FOR_EACH_BB_FN (bb, cfun)
3695 rtx_insn *insn;
3697 if (!bb->count.initialized_p ())
3699 if (dump_file)
3700 fprintf (dump_file, "Function is considered expensive because"
3701 " count of bb %i is not initialized\n", bb->index);
3702 return true;
3705 FOR_BB_INSNS (bb, insn)
3706 if (active_insn_p (insn))
3708 sum += bb->count;
3709 if (sum > limit)
3710 return true;
3714 return false;
3717 /* All basic blocks that are reachable only from unlikely basic blocks are
3718 unlikely. */
3720 void
3721 propagate_unlikely_bbs_forward (void)
3723 auto_vec<basic_block, 64> worklist;
3724 basic_block bb;
3725 edge_iterator ei;
3726 edge e;
3728 if (!(ENTRY_BLOCK_PTR_FOR_FN (cfun)->count == profile_count::zero ()))
3730 ENTRY_BLOCK_PTR_FOR_FN (cfun)->aux = (void *)(size_t) 1;
3731 worklist.safe_push (ENTRY_BLOCK_PTR_FOR_FN (cfun));
3733 while (worklist.length () > 0)
3735 bb = worklist.pop ();
3736 FOR_EACH_EDGE (e, ei, bb->succs)
3737 if (!(e->count () == profile_count::zero ())
3738 && !(e->dest->count == profile_count::zero ())
3739 && !e->dest->aux)
3741 e->dest->aux = (void *)(size_t) 1;
3742 worklist.safe_push (e->dest);
3747 FOR_ALL_BB_FN (bb, cfun)
3749 if (!bb->aux)
3751 if (!(bb->count == profile_count::zero ())
3752 && (dump_file && (dump_flags & TDF_DETAILS)))
3753 fprintf (dump_file,
3754 "Basic block %i is marked unlikely by forward prop\n",
3755 bb->index);
3756 bb->count = profile_count::zero ();
3758 else
3759 bb->aux = NULL;
3763 /* Determine basic blocks/edges that are known to be unlikely executed and set
3764 their counters to zero.
3765 This is done with first identifying obviously unlikely BBs/edges and then
3766 propagating in both directions. */
3768 static void
3769 determine_unlikely_bbs ()
3771 basic_block bb;
3772 auto_vec<basic_block, 64> worklist;
3773 edge_iterator ei;
3774 edge e;
3776 FOR_EACH_BB_FN (bb, cfun)
3778 if (!(bb->count == profile_count::zero ())
3779 && unlikely_executed_bb_p (bb))
3781 if (dump_file && (dump_flags & TDF_DETAILS))
3782 fprintf (dump_file, "Basic block %i is locally unlikely\n",
3783 bb->index);
3784 bb->count = profile_count::zero ();
3787 FOR_EACH_EDGE (e, ei, bb->succs)
3788 if (!(e->probability == profile_probability::never ())
3789 && unlikely_executed_edge_p (e))
3791 if (dump_file && (dump_flags & TDF_DETAILS))
3792 fprintf (dump_file, "Edge %i->%i is locally unlikely\n",
3793 bb->index, e->dest->index);
3794 e->probability = profile_probability::never ();
3797 gcc_checking_assert (!bb->aux);
3799 propagate_unlikely_bbs_forward ();
3801 auto_vec<int, 64> nsuccs;
3802 nsuccs.safe_grow_cleared (last_basic_block_for_fn (cfun), true);
3803 FOR_ALL_BB_FN (bb, cfun)
3804 if (!(bb->count == profile_count::zero ())
3805 && bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
3807 nsuccs[bb->index] = 0;
3808 FOR_EACH_EDGE (e, ei, bb->succs)
3809 if (!(e->probability == profile_probability::never ())
3810 && !(e->dest->count == profile_count::zero ()))
3811 nsuccs[bb->index]++;
3812 if (!nsuccs[bb->index])
3813 worklist.safe_push (bb);
3815 while (worklist.length () > 0)
3817 bb = worklist.pop ();
3818 if (bb->count == profile_count::zero ())
3819 continue;
3820 if (bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
3822 bool found = false;
3823 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
3824 !gsi_end_p (gsi); gsi_next (&gsi))
3825 if (stmt_can_terminate_bb_p (gsi_stmt (gsi))
3826 /* stmt_can_terminate_bb_p special cases noreturns because it
3827 assumes that fake edges are created. We want to know that
3828 noreturn alone does not imply BB to be unlikely. */
3829 || (is_gimple_call (gsi_stmt (gsi))
3830 && (gimple_call_flags (gsi_stmt (gsi)) & ECF_NORETURN)))
3832 found = true;
3833 break;
3835 if (found)
3836 continue;
3838 if (dump_file && (dump_flags & TDF_DETAILS))
3839 fprintf (dump_file,
3840 "Basic block %i is marked unlikely by backward prop\n",
3841 bb->index);
3842 bb->count = profile_count::zero ();
3843 FOR_EACH_EDGE (e, ei, bb->preds)
3844 if (!(e->probability == profile_probability::never ()))
3846 if (!(e->src->count == profile_count::zero ()))
3848 gcc_checking_assert (nsuccs[e->src->index] > 0);
3849 nsuccs[e->src->index]--;
3850 if (!nsuccs[e->src->index])
3851 worklist.safe_push (e->src);
3855 /* Finally all edges from non-0 regions to 0 are unlikely. */
3856 FOR_ALL_BB_FN (bb, cfun)
3858 if (!(bb->count == profile_count::zero ()))
3859 FOR_EACH_EDGE (e, ei, bb->succs)
3860 if (!(e->probability == profile_probability::never ())
3861 && e->dest->count == profile_count::zero ())
3863 if (dump_file && (dump_flags & TDF_DETAILS))
3864 fprintf (dump_file, "Edge %i->%i is unlikely because "
3865 "it enters unlikely block\n",
3866 bb->index, e->dest->index);
3867 e->probability = profile_probability::never ();
3870 edge other = NULL;
3872 FOR_EACH_EDGE (e, ei, bb->succs)
3873 if (e->probability == profile_probability::never ())
3875 else if (other)
3877 other = NULL;
3878 break;
3880 else
3881 other = e;
3882 if (other
3883 && !(other->probability == profile_probability::always ()))
3885 if (dump_file && (dump_flags & TDF_DETAILS))
3886 fprintf (dump_file, "Edge %i->%i is locally likely\n",
3887 bb->index, other->dest->index);
3888 other->probability = profile_probability::always ();
3891 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->count == profile_count::zero ())
3892 cgraph_node::get (current_function_decl)->count = profile_count::zero ();
3895 /* Estimate and propagate basic block frequencies using the given branch
3896 probabilities. If FORCE is true, the frequencies are used to estimate
3897 the counts even when there are already non-zero profile counts. */
3899 void
3900 estimate_bb_frequencies (bool force)
3902 basic_block bb;
3903 sreal freq_max;
3905 determine_unlikely_bbs ();
3907 if (force || profile_status_for_fn (cfun) != PROFILE_READ
3908 || !update_max_bb_count ())
3911 mark_dfs_back_edges ();
3913 single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun))->probability =
3914 profile_probability::always ();
3916 /* Set up block info for each basic block. */
3917 alloc_aux_for_blocks (sizeof (block_info));
3918 alloc_aux_for_edges (sizeof (edge_prob_info));
3919 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
3921 edge e;
3922 edge_iterator ei;
3924 FOR_EACH_EDGE (e, ei, bb->succs)
3926 /* FIXME: Graphite is producing edges with no profile. Once
3927 this is fixed, drop this. */
3928 if (e->probability.initialized_p ())
3929 EDGE_INFO (e)->back_edge_prob
3930 = e->probability.to_sreal ();
3931 else
3932 /* back_edge_prob = 0.5 */
3933 EDGE_INFO (e)->back_edge_prob = sreal (1, -1);
3937 /* First compute frequencies locally for each loop from innermost
3938 to outermost to examine frequencies for back edges. */
3939 estimate_loops ();
3941 freq_max = 0;
3942 FOR_EACH_BB_FN (bb, cfun)
3943 if (freq_max < BLOCK_INFO (bb)->frequency)
3944 freq_max = BLOCK_INFO (bb)->frequency;
3946 /* Scaling frequencies up to maximal profile count may result in
3947 frequent overflows especially when inlining loops.
3948 Small scalling results in unnecesary precision loss. Stay in
3949 the half of the (exponential) range. */
3950 freq_max = (sreal (1) << (profile_count::n_bits / 2)) / freq_max;
3951 if (freq_max < 16)
3952 freq_max = 16;
3953 profile_count ipa_count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count.ipa ();
3954 cfun->cfg->count_max = profile_count::uninitialized ();
3955 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
3957 sreal tmp = BLOCK_INFO (bb)->frequency;
3958 if (tmp >= 1)
3960 gimple_stmt_iterator gsi;
3961 tree decl;
3963 /* Self recursive calls can not have frequency greater than 1
3964 or program will never terminate. This will result in an
3965 inconsistent bb profile but it is better than greatly confusing
3966 IPA cost metrics. */
3967 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3968 if (is_gimple_call (gsi_stmt (gsi))
3969 && (decl = gimple_call_fndecl (gsi_stmt (gsi))) != NULL
3970 && recursive_call_p (current_function_decl, decl))
3972 if (dump_file)
3973 fprintf (dump_file, "Dropping frequency of recursive call"
3974 " in bb %i from %f\n", bb->index,
3975 tmp.to_double ());
3976 tmp = (sreal)9 / (sreal)10;
3977 break;
3980 tmp = tmp * freq_max + sreal (1, -1);
3981 profile_count count = profile_count::from_gcov_type (tmp.to_int ());
3983 /* If we have profile feedback in which this function was never
3984 executed, then preserve this info. */
3985 if (!(bb->count == profile_count::zero ()))
3986 bb->count = count.guessed_local ().combine_with_ipa_count (ipa_count);
3987 cfun->cfg->count_max = cfun->cfg->count_max.max (bb->count);
3990 free_aux_for_blocks ();
3991 free_aux_for_edges ();
3993 compute_function_frequency ();
3996 /* Decide whether function is hot, cold or unlikely executed. */
3997 void
3998 compute_function_frequency (void)
4000 basic_block bb;
4001 struct cgraph_node *node = cgraph_node::get (current_function_decl);
4003 if (DECL_STATIC_CONSTRUCTOR (current_function_decl)
4004 || MAIN_NAME_P (DECL_NAME (current_function_decl)))
4005 node->only_called_at_startup = true;
4006 if (DECL_STATIC_DESTRUCTOR (current_function_decl))
4007 node->only_called_at_exit = true;
4009 if (!ENTRY_BLOCK_PTR_FOR_FN (cfun)->count.ipa_p ())
4011 int flags = flags_from_decl_or_type (current_function_decl);
4012 if (lookup_attribute ("cold", DECL_ATTRIBUTES (current_function_decl))
4013 != NULL)
4014 node->frequency = NODE_FREQUENCY_UNLIKELY_EXECUTED;
4015 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (current_function_decl))
4016 != NULL)
4017 node->frequency = NODE_FREQUENCY_HOT;
4018 else if (flags & ECF_NORETURN)
4019 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
4020 else if (MAIN_NAME_P (DECL_NAME (current_function_decl)))
4021 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
4022 else if (DECL_STATIC_CONSTRUCTOR (current_function_decl)
4023 || DECL_STATIC_DESTRUCTOR (current_function_decl))
4024 node->frequency = NODE_FREQUENCY_EXECUTED_ONCE;
4025 return;
4028 node->frequency = NODE_FREQUENCY_UNLIKELY_EXECUTED;
4029 if (lookup_attribute ("cold", DECL_ATTRIBUTES (current_function_decl))
4030 == NULL)
4031 warn_function_cold (current_function_decl);
4032 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->count.ipa() == profile_count::zero ())
4033 return;
4034 FOR_EACH_BB_FN (bb, cfun)
4036 if (maybe_hot_bb_p (cfun, bb))
4038 node->frequency = NODE_FREQUENCY_HOT;
4039 return;
4041 if (!probably_never_executed_bb_p (cfun, bb))
4042 node->frequency = NODE_FREQUENCY_NORMAL;
4046 /* Build PREDICT_EXPR. */
4047 tree
4048 build_predict_expr (enum br_predictor predictor, enum prediction taken)
4050 tree t = build1 (PREDICT_EXPR, void_type_node,
4051 build_int_cst (integer_type_node, predictor));
4052 SET_PREDICT_EXPR_OUTCOME (t, taken);
4053 return t;
4056 const char *
4057 predictor_name (enum br_predictor predictor)
4059 return predictor_info[predictor].name;
4062 /* Predict branch probabilities and estimate profile of the tree CFG. */
4064 namespace {
4066 const pass_data pass_data_profile =
4068 GIMPLE_PASS, /* type */
4069 "profile_estimate", /* name */
4070 OPTGROUP_NONE, /* optinfo_flags */
4071 TV_BRANCH_PROB, /* tv_id */
4072 PROP_cfg, /* properties_required */
4073 0, /* properties_provided */
4074 0, /* properties_destroyed */
4075 0, /* todo_flags_start */
4076 0, /* todo_flags_finish */
4079 class pass_profile : public gimple_opt_pass
4081 public:
4082 pass_profile (gcc::context *ctxt)
4083 : gimple_opt_pass (pass_data_profile, ctxt)
4086 /* opt_pass methods: */
4087 bool gate (function *) final override { return flag_guess_branch_prob; }
4088 unsigned int execute (function *) final override;
4090 }; // class pass_profile
4092 unsigned int
4093 pass_profile::execute (function *fun)
4095 unsigned nb_loops;
4097 if (profile_status_for_fn (cfun) == PROFILE_GUESSED)
4098 return 0;
4100 loop_optimizer_init (LOOPS_NORMAL);
4101 if (dump_file && (dump_flags & TDF_DETAILS))
4102 flow_loops_dump (dump_file, NULL, 0);
4104 nb_loops = number_of_loops (fun);
4105 if (nb_loops > 1)
4106 scev_initialize ();
4108 tree_estimate_probability (false);
4110 if (nb_loops > 1)
4111 scev_finalize ();
4113 loop_optimizer_finalize ();
4114 if (dump_file && (dump_flags & TDF_DETAILS))
4115 gimple_dump_cfg (dump_file, dump_flags);
4116 if (profile_status_for_fn (fun) == PROFILE_ABSENT)
4117 profile_status_for_fn (fun) = PROFILE_GUESSED;
4118 if (dump_file && (dump_flags & TDF_DETAILS))
4120 for (auto loop : loops_list (cfun, LI_FROM_INNERMOST))
4121 if (loop->header->count.initialized_p ())
4122 fprintf (dump_file, "Loop got predicted %d to iterate %i times.\n",
4123 loop->num,
4124 (int)expected_loop_iterations_unbounded (loop));
4126 return 0;
4129 } // anon namespace
4131 gimple_opt_pass *
4132 make_pass_profile (gcc::context *ctxt)
4134 return new pass_profile (ctxt);
4137 /* Return true when PRED predictor should be removed after early
4138 tree passes. Most of the predictors are beneficial to survive
4139 as early inlining can also distribute then into caller's bodies. */
4141 static bool
4142 strip_predictor_early (enum br_predictor pred)
4144 switch (pred)
4146 case PRED_TREE_EARLY_RETURN:
4147 return true;
4148 default:
4149 return false;
4153 /* Get rid of all builtin_expect calls and GIMPLE_PREDICT statements
4154 we no longer need. EARLY is set to true when called from early
4155 optimizations. */
4157 unsigned int
4158 strip_predict_hints (function *fun, bool early)
4160 basic_block bb;
4161 gimple *ass_stmt;
4162 tree var;
4163 bool changed = false;
4165 FOR_EACH_BB_FN (bb, fun)
4167 gimple_stmt_iterator bi;
4168 for (bi = gsi_start_bb (bb); !gsi_end_p (bi);)
4170 gimple *stmt = gsi_stmt (bi);
4172 if (gimple_code (stmt) == GIMPLE_PREDICT)
4174 if (!early
4175 || strip_predictor_early (gimple_predict_predictor (stmt)))
4177 gsi_remove (&bi, true);
4178 changed = true;
4179 continue;
4182 else if (is_gimple_call (stmt))
4184 tree fndecl = gimple_call_fndecl (stmt);
4186 if (!early
4187 && ((fndecl != NULL_TREE
4188 && fndecl_built_in_p (fndecl, BUILT_IN_EXPECT)
4189 && gimple_call_num_args (stmt) == 2)
4190 || (fndecl != NULL_TREE
4191 && fndecl_built_in_p (fndecl,
4192 BUILT_IN_EXPECT_WITH_PROBABILITY)
4193 && gimple_call_num_args (stmt) == 3)
4194 || (gimple_call_internal_p (stmt)
4195 && gimple_call_internal_fn (stmt) == IFN_BUILTIN_EXPECT)))
4197 var = gimple_call_lhs (stmt);
4198 changed = true;
4199 if (var)
4201 ass_stmt
4202 = gimple_build_assign (var, gimple_call_arg (stmt, 0));
4203 gsi_replace (&bi, ass_stmt, true);
4205 else
4207 gsi_remove (&bi, true);
4208 continue;
4212 gsi_next (&bi);
4215 return changed ? TODO_cleanup_cfg : 0;
4218 namespace {
4220 const pass_data pass_data_strip_predict_hints =
4222 GIMPLE_PASS, /* type */
4223 "*strip_predict_hints", /* name */
4224 OPTGROUP_NONE, /* optinfo_flags */
4225 TV_BRANCH_PROB, /* tv_id */
4226 PROP_cfg, /* properties_required */
4227 0, /* properties_provided */
4228 0, /* properties_destroyed */
4229 0, /* todo_flags_start */
4230 0, /* todo_flags_finish */
4233 class pass_strip_predict_hints : public gimple_opt_pass
4235 public:
4236 pass_strip_predict_hints (gcc::context *ctxt)
4237 : gimple_opt_pass (pass_data_strip_predict_hints, ctxt)
4240 /* opt_pass methods: */
4241 opt_pass * clone () final override
4243 return new pass_strip_predict_hints (m_ctxt);
4245 void set_pass_param (unsigned int n, bool param) final override
4247 gcc_assert (n == 0);
4248 early_p = param;
4251 unsigned int execute (function *) final override;
4253 private:
4254 bool early_p;
4256 }; // class pass_strip_predict_hints
4258 unsigned int
4259 pass_strip_predict_hints::execute (function *fun)
4261 return strip_predict_hints (fun, early_p);
4264 } // anon namespace
4266 gimple_opt_pass *
4267 make_pass_strip_predict_hints (gcc::context *ctxt)
4269 return new pass_strip_predict_hints (ctxt);
4272 /* Rebuild function frequencies. Passes are in general expected to
4273 maintain profile by hand, however in some cases this is not possible:
4274 for example when inlining several functions with loops freuqencies might run
4275 out of scale and thus needs to be recomputed. */
4277 void
4278 rebuild_frequencies (void)
4280 timevar_push (TV_REBUILD_FREQUENCIES);
4282 /* When the max bb count in the function is small, there is a higher
4283 chance that there were truncation errors in the integer scaling
4284 of counts by inlining and other optimizations. This could lead
4285 to incorrect classification of code as being cold when it isn't.
4286 In that case, force the estimation of bb counts/frequencies from the
4287 branch probabilities, rather than computing frequencies from counts,
4288 which may also lead to frequencies incorrectly reduced to 0. There
4289 is less precision in the probabilities, so we only do this for small
4290 max counts. */
4291 cfun->cfg->count_max = profile_count::uninitialized ();
4292 basic_block bb;
4293 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
4294 cfun->cfg->count_max = cfun->cfg->count_max.max (bb->count);
4296 if (profile_status_for_fn (cfun) == PROFILE_GUESSED)
4298 loop_optimizer_init (LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS);
4299 connect_infinite_loops_to_exit ();
4300 estimate_bb_frequencies (true);
4301 remove_fake_exit_edges ();
4302 loop_optimizer_finalize ();
4304 else if (profile_status_for_fn (cfun) == PROFILE_READ)
4305 update_max_bb_count ();
4306 else if (profile_status_for_fn (cfun) == PROFILE_ABSENT
4307 && !flag_guess_branch_prob)
4309 else
4310 gcc_unreachable ();
4311 timevar_pop (TV_REBUILD_FREQUENCIES);
4314 /* Perform a dry run of the branch prediction pass and report comparsion of
4315 the predicted and real profile into the dump file. */
4317 void
4318 report_predictor_hitrates (void)
4320 unsigned nb_loops;
4322 loop_optimizer_init (LOOPS_NORMAL);
4323 if (dump_file && (dump_flags & TDF_DETAILS))
4324 flow_loops_dump (dump_file, NULL, 0);
4326 nb_loops = number_of_loops (cfun);
4327 if (nb_loops > 1)
4328 scev_initialize ();
4330 tree_estimate_probability (true);
4332 if (nb_loops > 1)
4333 scev_finalize ();
4335 loop_optimizer_finalize ();
4338 /* Force edge E to be cold.
4339 If IMPOSSIBLE is true, for edge to have count and probability 0 otherwise
4340 keep low probability to represent possible error in a guess. This is used
4341 i.e. in case we predict loop to likely iterate given number of times but
4342 we are not 100% sure.
4344 This function locally updates profile without attempt to keep global
4345 consistency which cannot be reached in full generality without full profile
4346 rebuild from probabilities alone. Doing so is not necessarily a good idea
4347 because frequencies and counts may be more realistic then probabilities.
4349 In some cases (such as for elimination of early exits during full loop
4350 unrolling) the caller can ensure that profile will get consistent
4351 afterwards. */
4353 void
4354 force_edge_cold (edge e, bool impossible)
4356 profile_count count_sum = profile_count::zero ();
4357 profile_probability prob_sum = profile_probability::never ();
4358 edge_iterator ei;
4359 edge e2;
4360 bool uninitialized_exit = false;
4362 /* When branch probability guesses are not known, then do nothing. */
4363 if (!impossible && !e->count ().initialized_p ())
4364 return;
4366 profile_probability goal = (impossible ? profile_probability::never ()
4367 : profile_probability::very_unlikely ());
4369 /* If edge is already improbably or cold, just return. */
4370 if (e->probability <= goal
4371 && (!impossible || e->count () == profile_count::zero ()))
4372 return;
4373 FOR_EACH_EDGE (e2, ei, e->src->succs)
4374 if (e2 != e)
4376 if (e->flags & EDGE_FAKE)
4377 continue;
4378 if (e2->count ().initialized_p ())
4379 count_sum += e2->count ();
4380 if (e2->probability.initialized_p ())
4381 prob_sum += e2->probability;
4382 else
4383 uninitialized_exit = true;
4386 /* If we are not guessing profiles but have some other edges out,
4387 just assume the control flow goes elsewhere. */
4388 if (uninitialized_exit)
4389 e->probability = goal;
4390 /* If there are other edges out of e->src, redistribute probabilitity
4391 there. */
4392 else if (prob_sum > profile_probability::never ())
4394 if (!(e->probability < goal))
4395 e->probability = goal;
4397 profile_probability prob_comp = prob_sum / e->probability.invert ();
4399 if (dump_file && (dump_flags & TDF_DETAILS))
4400 fprintf (dump_file, "Making edge %i->%i %s by redistributing "
4401 "probability to other edges.\n",
4402 e->src->index, e->dest->index,
4403 impossible ? "impossible" : "cold");
4404 FOR_EACH_EDGE (e2, ei, e->src->succs)
4405 if (e2 != e)
4407 e2->probability /= prob_comp;
4409 if (current_ir_type () != IR_GIMPLE
4410 && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
4411 update_br_prob_note (e->src);
4413 /* If all edges out of e->src are unlikely, the basic block itself
4414 is unlikely. */
4415 else
4417 if (prob_sum == profile_probability::never ())
4418 e->probability = profile_probability::always ();
4419 else
4421 if (impossible)
4422 e->probability = profile_probability::never ();
4423 /* If BB has some edges out that are not impossible, we cannot
4424 assume that BB itself is. */
4425 impossible = false;
4427 if (current_ir_type () != IR_GIMPLE
4428 && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
4429 update_br_prob_note (e->src);
4430 if (e->src->count == profile_count::zero ())
4431 return;
4432 if (count_sum == profile_count::zero () && impossible)
4434 bool found = false;
4435 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
4437 else if (current_ir_type () == IR_GIMPLE)
4438 for (gimple_stmt_iterator gsi = gsi_start_bb (e->src);
4439 !gsi_end_p (gsi); gsi_next (&gsi))
4441 if (stmt_can_terminate_bb_p (gsi_stmt (gsi)))
4443 found = true;
4444 break;
4447 /* FIXME: Implement RTL path. */
4448 else
4449 found = true;
4450 if (!found)
4452 if (dump_file && (dump_flags & TDF_DETAILS))
4453 fprintf (dump_file,
4454 "Making bb %i impossible and dropping count to 0.\n",
4455 e->src->index);
4456 e->src->count = profile_count::zero ();
4457 FOR_EACH_EDGE (e2, ei, e->src->preds)
4458 force_edge_cold (e2, impossible);
4459 return;
4463 /* If we did not adjusting, the source basic block has no likely edeges
4464 leaving other direction. In that case force that bb cold, too.
4465 This in general is difficult task to do, but handle special case when
4466 BB has only one predecestor. This is common case when we are updating
4467 after loop transforms. */
4468 if (!(prob_sum > profile_probability::never ())
4469 && count_sum == profile_count::zero ()
4470 && single_pred_p (e->src) && e->src->count.to_frequency (cfun)
4471 > (impossible ? 0 : 1))
4473 int old_frequency = e->src->count.to_frequency (cfun);
4474 if (dump_file && (dump_flags & TDF_DETAILS))
4475 fprintf (dump_file, "Making bb %i %s.\n", e->src->index,
4476 impossible ? "impossible" : "cold");
4477 int new_frequency = MIN (e->src->count.to_frequency (cfun),
4478 impossible ? 0 : 1);
4479 if (impossible)
4480 e->src->count = profile_count::zero ();
4481 else
4482 e->src->count = e->count ().apply_scale (new_frequency,
4483 old_frequency);
4484 force_edge_cold (single_pred_edge (e->src), impossible);
4486 else if (dump_file && (dump_flags & TDF_DETAILS)
4487 && maybe_hot_bb_p (cfun, e->src))
4488 fprintf (dump_file, "Giving up on making bb %i %s.\n", e->src->index,
4489 impossible ? "impossible" : "cold");
4493 /* Change E's probability to NEW_E_PROB, redistributing the probabilities
4494 of other outgoing edges proportionally.
4496 Note that this function does not change the profile counts of any
4497 basic blocks. The caller must do that instead, using whatever
4498 information it has about the region that needs updating. */
4500 void
4501 change_edge_frequency (edge e, profile_probability new_e_prob)
4503 profile_probability old_e_prob = e->probability;
4504 profile_probability old_other_prob = old_e_prob.invert ();
4505 profile_probability new_other_prob = new_e_prob.invert ();
4507 e->probability = new_e_prob;
4508 profile_probability cumulative_prob = new_e_prob;
4510 unsigned int num_other = EDGE_COUNT (e->src->succs) - 1;
4511 edge other_e;
4512 edge_iterator ei;
4513 FOR_EACH_EDGE (other_e, ei, e->src->succs)
4514 if (other_e != e)
4516 num_other -= 1;
4517 if (num_other == 0)
4518 /* Ensure that the probabilities add up to 1 without
4519 rounding error. */
4520 other_e->probability = cumulative_prob.invert ();
4521 else
4523 other_e->probability /= old_other_prob;
4524 other_e->probability *= new_other_prob;
4525 cumulative_prob += other_e->probability;
4530 #if CHECKING_P
4532 namespace selftest {
4534 /* Test that value range of predictor values defined in predict.def is
4535 within range (50, 100]. */
4537 struct branch_predictor
4539 const char *name;
4540 int probability;
4543 #define DEF_PREDICTOR(ENUM, NAME, HITRATE, FLAGS) { NAME, HITRATE },
4545 static void
4546 test_prediction_value_range ()
4548 branch_predictor predictors[] = {
4549 #include "predict.def"
4550 { NULL, PROB_UNINITIALIZED }
4553 for (unsigned i = 0; predictors[i].name != NULL; i++)
4555 if (predictors[i].probability == PROB_UNINITIALIZED)
4556 continue;
4558 unsigned p = 100 * predictors[i].probability / REG_BR_PROB_BASE;
4559 ASSERT_TRUE (p >= 50 && p <= 100);
4563 #undef DEF_PREDICTOR
4565 /* Run all of the selfests within this file. */
4567 void
4568 predict_cc_tests ()
4570 test_prediction_value_range ();
4573 } // namespace selftest
4574 #endif /* CHECKING_P. */